diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index fb3cd395cf..20ed87326b 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3789,7 +3789,9 @@ dependencies = [ name = "node-template" version = "2.0.0-rc5" dependencies = [ + "jsonrpc-core", "node-template-runtime", + "pallet-transaction-payment-rpc", "sc-basic-authorship", "sc-cli", "sc-client-api", @@ -3797,8 +3799,13 @@ dependencies = [ "sc-consensus-aura", "sc-executor", "sc-finality-grandpa", + "sc-rpc", + "sc-rpc-api", "sc-service", "sc-transaction-pool", + "sp-api", + "sp-block-builder", + "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-core", @@ -3808,6 +3815,7 @@ dependencies = [ "sp-transaction-pool", "structopt", "substrate-build-script-utils", + "substrate-frame-rpc-system", ] [[package]] @@ -3817,6 +3825,7 @@ dependencies = [ "frame-executive", "frame-support", "frame-system", + "frame-system-rpc-runtime-api", "pallet-aura", "pallet-balances", "pallet-grandpa", @@ -3825,6 +3834,7 @@ dependencies = [ "pallet-template", "pallet-timestamp", "pallet-transaction-payment", + "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", "serde", "sp-api", diff --git a/substrate/bin/node-template/node/Cargo.toml b/substrate/bin/node-template/node/Cargo.toml index 82c2d7ad43..0c988ebd1a 100644 --- a/substrate/bin/node-template/node/Cargo.toml +++ b/substrate/bin/node-template/node/Cargo.toml @@ -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" } diff --git a/substrate/bin/node-template/node/src/lib.rs b/substrate/bin/node-template/node/src/lib.rs index 38e43372ca..777c4f0a77 100644 --- a/substrate/bin/node-template/node/src/lib.rs +++ b/substrate/bin/node-template/node/src/lib.rs @@ -1,2 +1,3 @@ pub mod chain_spec; pub mod service; +pub mod rpc; diff --git a/substrate/bin/node-template/node/src/main.rs b/substrate/bin/node-template/node/src/main.rs index 369e6932a0..4449d28b9f 100644 --- a/substrate/bin/node-template/node/src/main.rs +++ b/substrate/bin/node-template/node/src/main.rs @@ -6,6 +6,7 @@ mod chain_spec; mod service; mod cli; mod command; +mod rpc; fn main() -> sc_cli::Result<()> { command::run() diff --git a/substrate/bin/node-template/node/src/rpc.rs b/substrate/bin/node-template/node/src/rpc.rs new file mode 100644 index 0000000000..c1f0e0a845 --- /dev/null +++ b/substrate/bin/node-template/node/src/rpc.rs @@ -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 { + /// The client instance to use. + pub client: Arc, + /// Transaction pool instance. + pub pool: Arc

, + /// Whether to deny unsafe calls + pub deny_unsafe: DenyUnsafe, +} + +/// Instantiate all full RPC extensions. +pub fn create_full( + deps: FullDeps, +) -> jsonrpc_core::IoHandler where + C: ProvideRuntimeApi, + C: HeaderBackend + HeaderMetadata + 'static, + C: Send + Sync + 'static, + C::Api: substrate_frame_rpc_system::AccountNonceApi, + C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, + C::Api: BlockBuilder, + 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 +} diff --git a/substrate/bin/node-template/node/src/service.rs b/substrate/bin/node-template/node/src/service.rs index 0de17103b0..339aafbefc 100644 --- a/substrate/bin/node-template/node/src/service.rs +++ b/substrate/bin/node-template/node/src/service.rs @@ -73,7 +73,7 @@ pub fn new_partial(config: &Configuration) -> Result Result { +pub fn new_full(config: Configuration) -> Result { 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 { 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 { 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 { 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 { &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 { 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, })?; diff --git a/substrate/bin/node-template/runtime/Cargo.toml b/substrate/bin/node-template/runtime/Cargo.toml index 2bf3182542..011916880b 100644 --- a/substrate/bin/node-template/runtime/Cargo.toml +++ b/substrate/bin/node-template/runtime/Cargo.toml @@ -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" } @@ -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] @@ -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", ] diff --git a/substrate/bin/node-template/runtime/src/lib.rs b/substrate/bin/node-template/runtime/src/lib.rs index c46d515a3e..c7cd80868b 100644 --- a/substrate/bin/node-template/runtime/src/lib.rs +++ b/substrate/bin/node-template/runtime/src/lib.rs @@ -417,4 +417,19 @@ impl_runtime_apis! { None } } + + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + fn account_nonce(account: AccountId) -> Index { + System::account_nonce(account) + } + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi for Runtime { + fn query_info( + uxt: ::Extrinsic, + len: u32, + ) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo { + TransactionPayment::query_info(uxt, len) + } + } }