Add RPC Builder to Substrate Node Template (#6808)

* Pulled RPC from node and populated the node-template's RPC builder with one example implementation

* surpress build errror

* dead_code

* Fixed module usage, removed copyright, removed rpc builder for light client + some comments

* added a comment for rpc extension

* Update bin/node-template/node/src/rpc.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update rpc.rs

* fix spacing

* more space to tabs

* more space to tabs

* Documenation nitpick

* Documentation nitpick

* Documentation nitpick

* Documentation nitpick

* Documentation nitpick

* pre-format

* Updated transaction payment API implemented for node template

* fix space and commented code

* fix long line

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Dan Forbes <dan@danforbes.dev>
This commit is contained in:
Hamza Tokuchi
2020-08-06 12:31:03 +02:00
committed by GitHub
parent 886d79e0cb
commit 9e438c2fc1
8 changed files with 133 additions and 12 deletions
@@ -1,2 +1,3 @@
pub mod chain_spec;
pub mod service;
pub mod rpc;
@@ -6,6 +6,7 @@ mod chain_spec;
mod service;
mod cli;
mod command;
mod rpc;
fn main() -> sc_cli::Result<()> {
command::run()
@@ -0,0 +1,64 @@
//! A collection of node-specific RPC methods.
//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer
//! used by Substrate nodes. This file extends those RPC definitions with
//! capabilities that are specific to this project's runtime configuration.
#![warn(missing_docs)]
use std::sync::Arc;
use node_template_runtime::{opaque::Block, AccountId, Balance, Index};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sp_block_builder::BlockBuilder;
pub use sc_rpc_api::DenyUnsafe;
use sp_transaction_pool::TransactionPool;
/// Full client dependencies.
pub struct FullDeps<C, P> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Whether to deny unsafe calls
pub deny_unsafe: DenyUnsafe,
}
/// 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 substrate_frame_rpc_system::{FullSystem, SystemApi};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
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
}
+25 -10
View File
@@ -73,7 +73,7 @@ pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponen
}
/// Builds a new service for a full client.
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool,
inherent_data_providers,
@@ -93,7 +93,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
on_demand: None,
block_announce_validator_builder: None,
finality_proof_request_builder: None,
finality_proof_provider: Some(finality_proof_provider.clone()),
finality_proof_provider: Some(finality_proof_provider.clone()),
})?;
if config.offchain_worker.enabled {
@@ -109,6 +109,21 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let prometheus_registry = config.prometheus_registry().cloned();
let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default();
let rpc_extensions_builder = {
let client = client.clone();
let pool = transaction_pool.clone();
Box::new(move |deny_unsafe| {
let deps = crate::rpc::FullDeps {
client: client.clone(),
pool: pool.clone(),
deny_unsafe,
};
crate::rpc::create_full(deps)
})
};
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
network: network.clone(),
client: client.clone(),
@@ -116,7 +131,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
task_manager: &mut task_manager,
transaction_pool: transaction_pool.clone(),
telemetry_connection_sinks: telemetry_connection_sinks.clone(),
rpc_extensions_builder: Box::new(|_| ()),
rpc_extensions_builder: rpc_extensions_builder,
on_demand: None,
remote_blockchain: None,
backend, network_status_sinks, system_rpc_tx, config,
@@ -256,7 +271,7 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),
);
}
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
remote_blockchain: Some(backend.remote_blockchain()),
transaction_pool,
@@ -264,12 +279,12 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
on_demand: Some(on_demand),
rpc_extensions_builder: Box::new(|_| ()),
telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),
config,
client,
keystore,
backend,
network,
network_status_sinks,
config,
client,
keystore,
backend,
network,
network_status_sinks,
system_rpc_tx,
})?;