From ac9323464a676f5b575639e192999ecd20a81d62 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 2 Jun 2020 11:35:23 +0800 Subject: [PATCH] add: missing RPC (#153) --- Cargo.lock | 80 +++++++++++++++++++++++++++ bin/node-template/node/Cargo.toml | 15 +++-- bin/node-template/node/src/rpc.rs | 76 ++++++++++++++++++++++--- bin/node-template/node/src/service.rs | 50 +++++++++++++---- bin/node-template/runtime/Cargo.toml | 4 ++ bin/node-template/runtime/src/lib.rs | 17 ++++++ 6 files changed, 219 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c80417ae8..1772ac721d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1553,6 +1553,15 @@ dependencies = [ "sp-version", ] +[[package]] +name = "frame-system-rpc-runtime-api" +version = "2.0.0-rc2" +source = "git+https://github.com/darwinia-network/substrate.git?tag=v2.0.0-rc.darwinia.1#45b9f0a9cbf901abaa9f1fca5fe8baeed029133d" +dependencies = [ + "parity-scale-codec", + "sp-api", +] + [[package]] name = "fs-swap" version = "0.2.4" @@ -3210,16 +3219,22 @@ dependencies = [ "log", "node-template-runtime", "pallet-im-online", + "pallet-transaction-payment-rpc", + "parity-scale-codec", "sc-basic-authorship", "sc-cli", "sc-client-api", "sc-consensus", "sc-consensus-babe", + "sc-consensus-babe-rpc", + "sc-consensus-epochs", "sc-executor", "sc-finality-grandpa", "sc-finality-grandpa-rpc", + "sc-keystore", "sc-network", "sc-rpc", + "sc-rpc-api", "sc-service", "sc-transaction-pool", "sp-api", @@ -3234,6 +3249,7 @@ dependencies = [ "sp-transaction-pool", "structopt", "substrate-build-script-utils", + "substrate-frame-rpc-system", ] [[package]] @@ -3256,6 +3272,7 @@ dependencies = [ "frame-executive", "frame-support", "frame-system", + "frame-system-rpc-runtime-api", "pallet-authority-discovery", "pallet-authorship", "pallet-babe", @@ -3269,6 +3286,7 @@ dependencies = [ "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", + "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", "serde", "sp-api", @@ -3663,6 +3681,24 @@ dependencies = [ "sp-std 2.0.0-rc2", ] +[[package]] +name = "pallet-transaction-payment-rpc" +version = "2.0.0-rc2" +source = "git+https://github.com/darwinia-network/substrate.git?tag=v2.0.0-rc.darwinia.1#45b9f0a9cbf901abaa9f1fca5fe8baeed029133d" +dependencies = [ + "jsonrpc-core", + "jsonrpc-core-client", + "jsonrpc-derive", + "pallet-transaction-payment-rpc-runtime-api", + "parity-scale-codec", + "serde", + "sp-api", + "sp-blockchain", + "sp-core", + "sp-rpc", + "sp-runtime", +] + [[package]] name = "pallet-transaction-payment-rpc-runtime-api" version = "2.0.0-rc2" @@ -4896,6 +4932,29 @@ dependencies = [ "substrate-prometheus-endpoint", ] +[[package]] +name = "sc-consensus-babe-rpc" +version = "0.8.0-rc2" +source = "git+https://github.com/darwinia-network/substrate.git?tag=v2.0.0-rc.darwinia.1#45b9f0a9cbf901abaa9f1fca5fe8baeed029133d" +dependencies = [ + "derive_more", + "futures 0.3.5", + "jsonrpc-core", + "jsonrpc-core-client", + "jsonrpc-derive", + "sc-consensus-babe", + "sc-consensus-epochs", + "sc-keystore", + "sc-rpc-api", + "serde", + "sp-api", + "sp-blockchain", + "sp-consensus", + "sp-consensus-babe", + "sp-core", + "sp-runtime", +] + [[package]] name = "sc-consensus-epochs" version = "0.8.0-rc2" @@ -6443,6 +6502,27 @@ dependencies = [ "platforms", ] +[[package]] +name = "substrate-frame-rpc-system" +version = "2.0.0-rc2" +source = "git+https://github.com/darwinia-network/substrate.git?tag=v2.0.0-rc.darwinia.1#45b9f0a9cbf901abaa9f1fca5fe8baeed029133d" +dependencies = [ + "frame-system-rpc-runtime-api", + "futures 0.3.5", + "jsonrpc-core", + "jsonrpc-core-client", + "jsonrpc-derive", + "log", + "parity-scale-codec", + "sc-client-api", + "serde", + "sp-api", + "sp-blockchain", + "sp-core", + "sp-runtime", + "sp-transaction-pool", +] + [[package]] name = "substrate-prometheus-endpoint" version = "0.8.0-rc2" diff --git a/bin/node-template/node/Cargo.toml b/bin/node-template/node/Cargo.toml index b59bd70ea1..0cb09de0bc 100644 --- a/bin/node-template/node/Cargo.toml +++ b/bin/node-template/node/Cargo.toml @@ -16,10 +16,11 @@ name = "node-template" # anonymous node-template-runtime = { path = "../runtime" } # crates -futures = "0.3.4" -jsonrpc-core = "14.0.3" -log = "0.4.8" -structopt = "0.3.13" +codec = { package = "parity-scale-codec", version = "1.3.0" } +futures = { version = "0.3.4" } +jsonrpc-core = { version = "14.0.3" } +log = { version = "0.4.8" } +structopt = { version = "0.3.13" } # darwinia darwinia-balances-rpc = { path = "../../../frame/balances/rpc" } darwinia-claims = { path = "../../../frame/claims" } @@ -29,16 +30,21 @@ darwinia-staking-rpc = { path = "../../../frame/staking/rpc" } # substrate frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-im-online = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +pallet-transaction-payment-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-basic-authorship = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-client-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-consensus = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-consensus-babe = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +sc-consensus-babe-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +sc-consensus-epochs = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-executor = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-finality-grandpa = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-finality-grandpa-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +sc-keystore = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-network = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-rpc = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +sc-rpc-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-service = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sc-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } @@ -51,6 +57,7 @@ sp-inherents = { git = "https://github.com/darwinia-network/substrate.git", tag sp-finality-grandpa = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-runtime = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +substrate-frame-rpc-system = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } [build-dependencies] # substrate diff --git a/bin/node-template/node/src/rpc.rs b/bin/node-template/node/src/rpc.rs index d4bab5dd03..b77dda26b9 100644 --- a/bin/node-template/node/src/rpc.rs +++ b/bin/node-template/node/src/rpc.rs @@ -16,47 +16,107 @@ // --- std --- use std::sync::Arc; // --- substrate --- -use sc_finality_grandpa::{SharedAuthoritySet, SharedVoterState}; use sp_api::ProvideRuntimeApi; // --- darwinia --- -use node_template_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Hash, Power}; +use node_template_runtime::{ + opaque::Block, + primitives::{AccountId, Balance, BlockNumber, Hash, Nonce, Power}, +}; /// A type representing all RPC extensions. pub type RpcExtension = jsonrpc_core::IoHandler; +/// Extra dependencies for BABE. +pub struct BabeDeps { + /// BABE protocol config. + pub babe_config: sc_consensus_babe::Config, + /// BABE pending epoch changes. + pub shared_epoch_changes: + sc_consensus_epochs::SharedEpochChanges, + /// The keystore that manages the keys of the node. + pub keystore: sc_keystore::KeyStorePtr, +} + /// Extra dependencies for GRANDPA pub struct GrandpaDeps { /// Voting round info. - pub shared_voter_state: SharedVoterState, + pub shared_voter_state: sc_finality_grandpa::SharedVoterState, /// Authority set info. - pub shared_authority_set: SharedAuthoritySet, + pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet, } /// Full client dependencies. -pub struct FullDeps { +pub struct FullDeps { /// The client instance to use. pub client: Arc, + /// Transaction pool instance. + pub pool: Arc

, + /// The SelectChain Strategy + pub select_chain: SC, + /// Whether to deny unsafe calls + pub deny_unsafe: sc_rpc_api::DenyUnsafe, + /// BABE specific dependencies. + pub babe: BabeDeps, /// GRANDPA specific dependencies. pub grandpa: GrandpaDeps, } -pub fn create(deps: FullDeps) -> RpcExtension +pub fn create(deps: FullDeps) -> RpcExtension where C: ProvideRuntimeApi, - C: sp_blockchain::HeaderBackend, + C: sp_blockchain::HeaderBackend + + sp_blockchain::HeaderMetadata, C: 'static + Send + Sync, + C::Api: substrate_frame_rpc_system::AccountNonceApi, + C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, + C::Api: sc_consensus_babe::BabeApi, C::Api: darwinia_balances_rpc::BalancesRuntimeApi, C::Api: darwinia_staking_rpc::StakingRuntimeApi, + P: 'static + sp_transaction_pool::TransactionPool, + SC: 'static + sp_consensus::SelectChain, + UE: 'static + Send + Sync + codec::Codec, { // --- substrate --- + use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi}; + use sc_consensus_babe_rpc::{BabeApi, BabeRpcHandler}; use sc_finality_grandpa_rpc::GrandpaRpcHandler; + use substrate_frame_rpc_system::{FullSystem, SystemApi}; // --- darwinia --- use darwinia_balances_rpc::{Balances, BalancesApi}; use darwinia_staking_rpc::{Staking, StakingApi}; - let FullDeps { client, grandpa } = deps; + let FullDeps { + client, + pool, + select_chain, + deny_unsafe, + babe, + grandpa, + } = deps; let mut io = jsonrpc_core::IoHandler::default(); + io.extend_with(SystemApi::to_delegate(FullSystem::new( + client.clone(), + pool, + ))); + io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new( + client.clone(), + ))); + { + let BabeDeps { + keystore, + babe_config, + shared_epoch_changes, + } = babe; + io.extend_with(BabeApi::to_delegate(BabeRpcHandler::new( + client.clone(), + shared_epoch_changes, + keystore, + babe_config, + select_chain, + deny_unsafe, + ))); + }; { let GrandpaDeps { shared_voter_state, diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index 40b803aab7..6eca3dc83c 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -92,24 +92,52 @@ macro_rules! new_full_start { Ok(import_queue) }, )? - .with_rpc_extensions(|builder| -> Result { + .with_rpc_extensions_builder(|builder| { let grandpa_link = import_setup .as_ref() .map(|s| &s.1) .expect("GRANDPA LinkHalf is present for full services or set up failed; qed."); - let shared_authority_set = grandpa_link.shared_authority_set(); + + let shared_authority_set = grandpa_link.shared_authority_set().clone(); let shared_voter_state = sc_finality_grandpa::SharedVoterState::empty(); - let deps = crate::rpc::FullDeps { - client: builder.client().clone(), - grandpa: crate::rpc::GrandpaDeps { - shared_voter_state: shared_voter_state.clone(), - shared_authority_set: shared_authority_set.clone(), - }, - }; - rpc_setup = Some((shared_voter_state)); + rpc_setup = Some((shared_voter_state.clone())); - Ok(crate::rpc::create(deps)) + let babe_link = import_setup + .as_ref() + .map(|s| &s.2) + .expect("BabeLink is present for full services or set up failed; qed."); + + let babe_config = babe_link.config().clone(); + let shared_epoch_changes = babe_link.epoch_changes().clone(); + + let client = builder.client().clone(); + let pool = builder.pool().clone(); + let select_chain = builder + .select_chain() + .cloned() + .expect("SelectChain is present for full services or set up failed; qed."); + let keystore = builder.keystore().clone(); + + Ok(move |deny_unsafe| -> crate::rpc::RpcExtension { + let deps = crate::rpc::FullDeps { + client: client.clone(), + pool: pool.clone(), + select_chain: select_chain.clone(), + deny_unsafe, + babe: crate::rpc::BabeDeps { + babe_config: babe_config.clone(), + shared_epoch_changes: shared_epoch_changes.clone(), + keystore: keystore.clone(), + }, + grandpa: crate::rpc::GrandpaDeps { + shared_voter_state: shared_voter_state.clone(), + shared_authority_set: shared_authority_set.clone(), + }, + }; + + crate::rpc::create(deps) + }) })?; (builder, import_setup, inherent_data_providers, rpc_setup) diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml index 9fbeaed21e..2d8103ae2c 100644 --- a/bin/node-template/runtime/Cargo.toml +++ b/bin/node-template/runtime/Cargo.toml @@ -32,6 +32,7 @@ frame-benchmarking = { default-features = false, optional = true, git = "https:/ frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +frame-system-rpc-runtime-api = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-authority-discovery = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-authorship = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-babe = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } @@ -45,6 +46,7 @@ pallet-session = { default-features = false, git = "https://github.com/darwinia- pallet-sudo = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-timestamp = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } pallet-transaction-payment = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } +pallet-transaction-payment-rpc-runtime-api = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-api = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-application-crypto = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } sp-authority-discovery = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-rc.darwinia.1" } @@ -90,6 +92,7 @@ std = [ "frame-executive/std", "frame-support/std", "frame-system/std", + "frame-system-rpc-runtime-api/std", "pallet-authority-discovery/std", "pallet-authorship/std", "pallet-babe/std", @@ -103,6 +106,7 @@ std = [ "pallet-sudo/std", "pallet-timestamp/std", "pallet-transaction-payment/std", + "pallet-transaction-payment-rpc-runtime-api/std", "sp-api/std", "sp-application-crypto/std", "sp-authority-discovery/std", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index f4db36fdee..daf7ecb398 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -295,6 +295,7 @@ use pallet_grandpa::{ }; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use pallet_session::historical as pallet_session_historical; +use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo as TransactionPaymentRuntimeDispatchInfo; use sp_api::impl_runtime_apis; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; use sp_core::{ @@ -984,6 +985,22 @@ impl_runtime_apis! { } } + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + fn account_nonce(account: AccountId) -> Nonce { + System::account_nonce(account) + } + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< + Block, + Balance, + UncheckedExtrinsic, + > for Runtime { + fn query_info(uxt: UncheckedExtrinsic, len: u32) -> TransactionPaymentRuntimeDispatchInfo { + TransactionPayment::query_info(uxt, len) + } + } + impl darwinia_balances_rpc_runtime_api::BalancesApi for Runtime { fn usable_balance(instance: u8, account: AccountId) -> BalancesRuntimeDispatchInfo { match instance {