Javascript/NodeJS Function to Check the Plugins on Steem Blockchain RPC Nodes

in witness-category •  4 years ago 

Previously: API Added - Check Plugins of RPC Node

The Steem Blockchain RPC nodes have a few plugins that can be configured enabled/disabled. We can use the following Javascript function (NodeJS) to check if those plugins are enabled or disabled.

To check if a plugin is enabled or not, we need to sent the API (POST) with the corresponding parameters to invoke some API of that plugin. If it returns errors containing the "cannot find method", the plugin is disabled.

See below JS function callAPI which takes the RPC Node Server, the Plugin Name, the API Method, and its parameters. It returns true if the plugin has been enabled - otherwise meaning the plugin has been deactivated.

async function callAPI(server, plugin, api, params) {
  try {
    const body = JSON.stringify({"id":1,"jsonrpc":"2.0","method":plugin+"."+api,"params":params});
    const response = await fetch(server, {
      method: "POST",
      headers: {
        'Content-Type': 'application/json'
      },
      body: body
    });
    if (response.ok) {
      const data = await response.text();      
      const jsonData = JSON.parse(data);  
      const magicString = "Assert Exception:api_itr != _registered_apis.end()";
      const succ = (!jsonData["error"]) || (!jsonData["error"]["message"].includes(magicString));
      return succ;
    } else {
      return false;
    }
  } catch (ex) {
    return false;
  }
}

The list of plugins, and their one example API, with parameters can be defined as:

  const plugins = {
    "account_by_key_api": ["get_key_references", {"keys":["STM5jZtLoV8YbxCxr4imnbWn61zMB24wwonpnVhfXRmv7j6fk3dTH"]}],
    "account_history_api": ["get_account_history", {"account":"steemit", "start":1000, "limit":1000}],
    "block_api": ["get_block",{"block_num":1}],
    "database_api": ["list_account_recovery_requests",{"start":"", "limit":10, "order":"by_account"}],  
    "condenser_api": ["get_accounts", [["justyy"]]],
    "debug_node_api": ["debug_pop_block"],
    "follow_api": ["get_account_reputations",{"account_lower_bound":"steemit", "limit":1}],
    "market_history_api": ["get_ticker"],
    "rc_api": ["find_rc_accounts",{"accounts":["alice","bob"]}],
    "reputation_api": ["get_account_reputations",{"account_lower_bound": "steem"}],
    "rewards_api": ["simulate_curve_payouts",{"curve": "quadratic", "var1": "2000000000000"}],
    "tags_api": ["get_discussion",{"author":"alice", "permlink":"a-post-by-alice"}],
    "transaction_status_api": ["find_transaction",{"transaction_id": "0000000000000000000000000000000000000000", "expiration":"2016-03-24T18:00:21"}],
    "network_broadcast_api": ["broadcast_block",{"trx":{"ref_block_num":1097,"ref_block_prefix":2181793527,"expiration":"2016-03-24T18:00:21","operations":[{"type":"vote_operation","value":{"voter":"steemit","author":"alice","permlink":"a-post-by-alice","weight":10000}}],"extensions":[],"signatures":[]},"max_block_age":50}],
  };

And we can test each plugin one by one:

  let result = {
    "node": node,
    "enabled_plugins": [],
    "disabled_plugins": [],
  };
  const pluginNames = Object.keys(plugins);
  for (let i = 0; i < pluginNames.length; ++ i) {
    const plugin = pluginNames[i];
    const api = plugins[plugin];
    const params = api.slice(1)[0];
    const succ = await callAPI(node, plugin, api[0], params);
    if (succ) {
      result["enabled_plugins"].push(plugin);
    } else {
      result["disabled_plugins"].push(plugin);
    }
  }

See also: How to Get Blockchain Version of Steem RPC Node using Javascript?

Reposted to Blog

@steemcurator01
@steemcurator02
@steemcurator03
@steemcurator04
@steemcurator05
@steemcurator06
@steemcurator07
@steemcurator08
@steemcurator09

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

我以为行长没发今天的编程教程,结果翻了一下19小时前发过了,哈哈!

[WhereIn Android] (http://www.wherein.io)

  ·  4 years ago 

哇哈哈