4

The node-template says its service.rs is a specialisation of that on the substrate master branch. Hence, it's sort of hard to relate the two. Nonetheless, I'd like to expose an RPC endpoint that's accessible to both off-chain workers and (implicitly) the polkadotjs API, such as the system_peers endpoint that requires the NetworkWorker. But this is hidden in the node-template implementation. how/where would I look to obtain an instance of it or an equivalent struct in the node-template?

Please note that I'm unfamiliar with asynchronous rust, and implementing custom RPCs, hence the wording might be a bit off, But I'm open to good resources for practical uses of both.

Thanks in advance!

2
  • 2
    Is your question "how to add a new RPC to Substrate"?
    – Shawn Tabrizi
    Mar 27, 2022 at 21:50
  • Yes, that's right. I'll edit the title to make it clearer.
    – islami00
    Mar 28, 2022 at 10:27

1 Answer 1

3

To add a new RPC to the Substrate Node Template, you should look at the rpc.rs file included with it that contains an example and docs for adding a new RPC:

/node/src/rpc.rs

/// Instantiate all full RPC extensions.
pub fn create_full<C, P>(deps: FullDeps<C, P>) -> jsonrpc_core::IoHandler<sc_rpc::Metadata>
where
    C: ProvideRuntimeApi<Block>,
    C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
    C: Send + Sync + 'static,
    C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
    C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
    C::Api: BlockBuilder<Block>,
    P: TransactionPool + 'static,
{
    use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
    use substrate_frame_rpc_system::{FullSystem, SystemApi};

    let mut io = jsonrpc_core::IoHandler::default();
    let FullDeps { client, pool, deny_unsafe } = deps;

    io.extend_with(SystemApi::to_delegate(FullSystem::new(client.clone(), pool, deny_unsafe)));

    io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())));

    // Extend this RPC with a custom API by using the following syntax.
    // `YourRpcStruct` should have a reference to a client, which is needed
    // to call into the runtime.
    // `io.extend_with(YourRpcTrait::to_delegate(YourRpcStruct::new(ReferenceToClient, ...)));`

    io
}

As for specifically implementing the NetworkWorker into your node, you should take a look at the implementation in the main Substrate Node, and pull that in. Otherwise, you can open a new issue, and ask a developer to include it into the node-template for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.