diff --git a/Cargo.lock b/Cargo.lock index c557716d58aff..8173d5258b6a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4276,11 +4276,17 @@ dependencies = [ "parity-scale-codec", "polkadot-primitives", "sc-client-api", + "sc-consensus-babe", + "sc-consensus-babe-rpc", + "sc-consensus-epochs", "sc-finality-grandpa", "sc-finality-grandpa-rpc", + "sc-keystore", "sc-rpc", "sp-api", "sp-blockchain", + "sp-consensus", + "sp-consensus-babe", "sp-runtime", "sp-transaction-pool", "substrate-frame-rpc-system", @@ -5536,6 +5542,28 @@ dependencies = [ "substrate-prometheus-endpoint", ] +[[package]] +name = "sc-consensus-babe-rpc" +version = "0.8.0-dev" +source = "git+https://github.com/paritytech/substrate#c0ccc24d02080ab4fbb2c65440327fc72acb6c42" +dependencies = [ + "derive_more 0.99.5", + "futures 0.3.4", + "jsonrpc-core", + "jsonrpc-core-client", + "jsonrpc-derive", + "sc-consensus-babe", + "sc-consensus-epochs", + "sc-keystore", + "serde", + "sp-api", + "sp-blockchain", + "sp-consensus", + "sp-consensus-babe", + "sp-core", + "sp-runtime", +] + [[package]] name = "sc-consensus-epochs" version = "0.8.0-alpha.8" diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 738c60153eff6..c108406970f55 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -11,9 +11,15 @@ sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "mas sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master"} +sc-consensus-babe-rpc = { git = "https://github.com/paritytech/substrate", branch = "master"} +sc-consensus-epochs = { git = "https://github.com/paritytech/substrate", branch = "master"} sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-finality-grandpa-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-keystore = { git = "https://github.com/paritytech/substrate", branch = "master"} txpool-api = { package = "sp-transaction-pool", git = "https://github.com/paritytech/substrate", branch = "master" } frame-rpc-system = { package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "master" } pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 0063c95ddb221..7fa0238b9374d 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -23,12 +23,37 @@ use std::sync::Arc; use polkadot_primitives::{Block, BlockNumber, AccountId, Nonce, Balance, Hash}; use sp_api::ProvideRuntimeApi; use txpool_api::TransactionPool; -use sp_blockchain::HeaderBackend; +use sp_blockchain::{HeaderBackend, HeaderMetadata, Error as BlockChainError}; +use sp_consensus::SelectChain; use sc_client_api::light::{Fetcher, RemoteBlockchain}; +use sc_consensus_babe::Epoch; +use sp_consensus_babe::BabeApi; /// A type representing all RPC extensions. pub type RpcExtension = jsonrpc_core::IoHandler; +/// Light client extra dependencies. +pub struct LightDeps { + /// The client instance to use. + pub client: Arc, + /// Transaction pool instance. + pub pool: Arc

, + /// Remote access to the blockchain (async). + pub remote_blockchain: Arc>, + /// Fetcher instance. + pub fetcher: Arc, +} + +/// 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, +} + /// Dependencies for GRANDPA pub struct GrandpaDeps { /// Voting round info. @@ -37,31 +62,65 @@ pub struct GrandpaDeps { pub shared_authority_set: sc_finality_grandpa::SharedAuthoritySet, } +/// Full client dependencies +pub struct FullDeps { + /// The client instance to use. + pub client: Arc, + /// Transaction pool instance. + pub pool: Arc

, + /// The SelectChain Strategy + pub select_chain: SC, + /// BABE specific dependencies. + pub babe: BabeDeps, + /// GRANDPA specific dependencies. + pub grandpa: GrandpaDeps, +} + /// Instantiate all RPC extensions. -pub fn create_full(client: Arc, pool: Arc

, grandpa_deps: GrandpaDeps) -> RpcExtension where +pub fn create_full(deps: FullDeps) -> RpcExtension where C: ProvideRuntimeApi, - C: HeaderBackend, + C: HeaderBackend + HeaderMetadata, C: Send + Sync + 'static, C::Api: frame_rpc_system::AccountNonceApi, C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, + C::Api: BabeApi, P: TransactionPool + Sync + Send + 'static, UE: codec::Codec + Send + Sync + 'static, + SC: SelectChain + 'static, { use frame_rpc_system::{FullSystem, SystemApi}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi}; use sc_finality_grandpa_rpc::{GrandpaApi, GrandpaRpcHandler}; + use sc_consensus_babe_rpc::BabeRPCHandler; let mut io = jsonrpc_core::IoHandler::default(); + let FullDeps { + client, + pool, + select_chain, + babe, + grandpa, + } = deps; + let BabeDeps { + keystore, + babe_config, + shared_epoch_changes, + } = babe; let GrandpaDeps { shared_voter_state, shared_authority_set, - } = grandpa_deps; + } = grandpa; io.extend_with( SystemApi::to_delegate(FullSystem::new(client.clone(), pool)) ); io.extend_with( - TransactionPaymentApi::to_delegate(TransactionPayment::new(client)) + TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())) + ); + io.extend_with( + sc_consensus_babe_rpc::BabeApi::to_delegate( + BabeRPCHandler::new(client, shared_epoch_changes, keystore, babe_config, select_chain) + ) ); io.extend_with( GrandpaApi::to_delegate(GrandpaRpcHandler::new( @@ -73,12 +132,7 @@ pub fn create_full(client: Arc, pool: Arc

, grandpa_deps: Grandpa } /// Instantiate all RPC extensions for light node. -pub fn create_light( - client: Arc, - remote_blockchain: Arc>, - fetcher: Arc, - pool: Arc

, -) -> RpcExtension +pub fn create_light(deps: LightDeps) -> RpcExtension where C: ProvideRuntimeApi, C: HeaderBackend, @@ -91,6 +145,12 @@ pub fn create_light( { use frame_rpc_system::{LightSystem, SystemApi}; + let LightDeps { + client, + pool, + remote_blockchain, + fetcher, + } = deps; let mut io = jsonrpc_core::IoHandler::default(); io.extend_with( SystemApi::::to_delegate(LightSystem::new(client, remote_blockchain, fetcher, pool)) diff --git a/service/src/lib.rs b/service/src/lib.rs index 0b50a1fe93be1..4879555b22238 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -214,16 +214,29 @@ macro_rules! new_full_start { Ok(import_queue) })? .with_rpc_extensions(|builder| -> Result { + let babe_link = import_setup.as_ref().map(|s| &s.2) + .expect("BabeLink is present for full services or set up faile; qed."); 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_voter_state = SharedVoterState::empty(); - let grandpa_deps = polkadot_rpc::GrandpaDeps { - shared_voter_state: shared_voter_state.clone(), - shared_authority_set: shared_authority_set.clone(), + let deps = polkadot_rpc::FullDeps { + client: builder.client().clone(), + pool: builder.pool(), + select_chain: builder.select_chain().cloned() + .expect("SelectChain is present for full services or set up failed; qed."), + babe: polkadot_rpc::BabeDeps { + keystore: builder.keystore(), + babe_config: babe::BabeLink::config(babe_link).clone(), + shared_epoch_changes: babe::BabeLink::epoch_changes(babe_link).clone(), + }, + grandpa: polkadot_rpc::GrandpaDeps { + shared_voter_state: shared_voter_state.clone(), + shared_authority_set: shared_authority_set.clone(), + }, }; rpc_setup = Some((shared_voter_state)); - Ok(polkadot_rpc::create_full(builder.client().clone(), builder.pool(), grandpa_deps)) + Ok(polkadot_rpc::create_full(deps)) })?; (builder, import_setup, inherent_data_providers, rpc_setup) @@ -586,7 +599,13 @@ macro_rules! new_light { let remote_blockchain = builder.remote_backend() .ok_or_else(|| "Trying to start node RPC without active remote blockchain")?; - Ok(polkadot_rpc::create_light(builder.client().clone(), remote_blockchain, fetcher, builder.pool())) + let light_deps = polkadot_rpc::LightDeps { + remote_blockchain, + fetcher, + client: builder.client().clone(), + pool: builder.pool(), + }; + Ok(polkadot_rpc::create_light(light_deps)) })? .build() }}