Skip to content

Commit

Permalink
Add RPC Builder to Substrate Node Template (paritytech#6808)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
Co-authored-by: Dan Forbes <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2020
1 parent 60d67dc commit bcf9dc5
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ sc-finality-grandpa = { version = "0.8.0-rc5", path = "../../../client/finality-
sp-finality-grandpa = { version = "2.0.0-rc5", path = "../../../primitives/finality-grandpa" }
sc-client-api = { version = "2.0.0-rc5", path = "../../../client/api" }
sp-runtime = { version = "2.0.0-rc5", path = "../../../primitives/runtime" }
sc-basic-authorship = { path = "../../../client/basic-authorship", version = "0.8.0-rc5"}

# These dependencies are used for the node template's RPCs
jsonrpc-core = "14.0.3"
sc-rpc = { version = "2.0.0-rc5", path = "../../../client/rpc" }
sp-api = { version = "2.0.0-rc5", path = "../../../primitives/api" }
sc-rpc-api = { version = "0.8.0-rc5", path = "../../../client/rpc-api" }
sp-blockchain = { version = "2.0.0-rc5", path = "../../../primitives/blockchain" }
sp-block-builder = { version = "2.0.0-rc5", path = "../../../primitives/block-builder" }
sc-basic-authorship = { version = "0.8.0-rc5", path = "../../../client/basic-authorship" }
substrate-frame-rpc-system = { version = "2.0.0-rc5", path = "../../../utils/frame/rpc/system" }
pallet-transaction-payment-rpc = { version = "2.0.0-rc5", path = "../../../frame/transaction-payment/rpc/" }

node-template-runtime = { version = "2.0.0-rc5", path = "../runtime" }

Expand Down
1 change: 1 addition & 0 deletions bin/node-template/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod chain_spec;
pub mod service;
pub mod rpc;
1 change: 1 addition & 0 deletions bin/node-template/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod chain_spec;
mod service;
mod cli;
mod command;
mod rpc;

fn main() -> sc_cli::Result<()> {
command::run()
Expand Down
64 changes: 64 additions & 0 deletions bin/node-template/node/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 25 additions & 10 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -109,14 +109,29 @@ 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(),
keystore: keystore.clone(),
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,
Expand Down Expand Up @@ -256,20 +271,20 @@ 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,
task_manager: &mut task_manager,
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,
})?;

Expand Down
7 changes: 6 additions & 1 deletion bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }

aura = { version = "2.0.0-rc5", default-features = false, package = "pallet-aura", path = "../../../frame/aura" }
balances = { version = "2.0.0-rc5", default-features = false, package = "pallet-balances", path = "../../../frame/balances" }
frame-support = { version = "2.0.0-rc5", default-features = false, path = "../../../frame/support" }
Expand All @@ -36,6 +35,10 @@ sp-std = { version = "2.0.0-rc5", default-features = false, path = "../../../pri
sp-transaction-pool = { version = "2.0.0-rc5", default-features = false, path = "../../../primitives/transaction-pool" }
sp-version = { version = "2.0.0-rc5", default-features = false, path = "../../../primitives/version" }

# Used for the node template's RPCs
frame-system-rpc-runtime-api = { version = "2.0.0-rc5", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0-rc5", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" }

template = { version = "2.0.0-rc5", default-features = false, path = "../pallets/template", package = "pallet-template" }

[build-dependencies]
Expand Down Expand Up @@ -67,5 +70,7 @@ std = [
"system/std",
"timestamp/std",
"transaction-payment/std",
"frame-system-rpc-runtime-api/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"template/std",
]
15 changes: 15 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,19 @@ impl_runtime_apis! {
None
}
}

impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
fn account_nonce(account: AccountId) -> Index {
System::account_nonce(account)
}
}

impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {
fn query_info(
uxt: <Block as BlockT>::Extrinsic,
len: u32,
) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo<Balance> {
TransactionPayment::query_info(uxt, len)
}
}
}

0 comments on commit bcf9dc5

Please sign in to comment.