diff --git a/Cargo.lock b/Cargo.lock index 3f883af3a9ef99a9245fbdd64f92c89420bc8965..21181bfff2c21069586a762c2c05c29a6c609994 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1905,6 +1905,7 @@ dependencies = [ "sc-client-db", "sc-consensus", "sc-consensus-babe", + "sc-consensus-babe-rpc", "sc-consensus-grandpa", "sc-consensus-manual-seal", "sc-executor", @@ -7633,6 +7634,28 @@ dependencies = [ "thiserror", ] +[[package]] +name = "sc-consensus-babe-rpc" +version = "0.10.0-dev" +source = "git+https://github.com/duniter/substrate?branch=duniter-substrate-v0.9.42#87ef489034ca2b15c4f30da387e06b1f6716d9a2" +dependencies = [ + "futures 0.3.28", + "jsonrpsee", + "sc-consensus-babe", + "sc-consensus-epochs", + "sc-rpc-api", + "serde", + "sp-api", + "sp-application-crypto", + "sp-blockchain", + "sp-consensus", + "sp-consensus-babe", + "sp-core", + "sp-keystore", + "sp-runtime", + "thiserror", +] + [[package]] name = "sc-consensus-epochs" version = "0.10.0-dev" @@ -10629,7 +10652,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 1.0.0", + "cfg-if 0.1.10", "digest 0.10.7", "rand 0.8.5", "static_assertions", diff --git a/Cargo.toml b/Cargo.toml index 8041f7d5b222e4c549f3f5095777689ed6a65bd7..800c67c268aef651afe689441aaba49412e7ddb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,7 @@ sc-cli = { git = "https://github.com/duniter/substrate", branch = "duniter-subst sc-client-api = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } sc-consensus = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } sc-consensus-babe = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } +sc-consensus-babe-rpc = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } sc-consensus-manual-seal = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } sc-client-db = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } sc-executor = { git = "https://github.com/duniter/substrate", branch = "duniter-substrate-v0.9.42", default-features = false } diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 039e43e02642535f4ca2658600adb7aa626064c4..2d41cd61c40c6638e03fc18ffa7a6c50572e5b6d 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -26,29 +26,47 @@ pub use sc_rpc_api::DenyUnsafe; use common_runtime::Block; use common_runtime::{AccountId, Balance, Index}; use jsonrpsee::RpcModule; +use sc_consensus_babe::BabeApi; +use sc_consensus_babe::BabeWorkerHandle; use sc_transaction_pool_api::TransactionPool; use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata}; +use sp_consensus::SelectChain; +use sp_keystore::KeystorePtr; use std::sync::Arc; +/// Extra dependencies for BABE. +pub struct BabeDeps { + /// A handle to the BABE worker for issuing requests. + pub babe_worker_handle: BabeWorkerHandle<Block>, + /// The keystore that manages the keys of the node. + pub keystore: KeystorePtr, +} + /// Full client dependencies. -pub struct FullDeps<C, P> { +pub struct FullDeps<C, P, SC> { /// The client instance to use. pub client: Arc<C>, /// Transaction pool instance. pub pool: Arc<P>, + /// The SelectChain Strategy + pub select_chain: SC, + /// A copy of the chain spec. + pub chain_spec: Box<dyn sc_chain_spec::ChainSpec>, /// Whether to deny unsafe calls pub deny_unsafe: DenyUnsafe, /// Manual seal command sink pub command_sink_opt: Option< futures::channel::mpsc::Sender<sc_consensus_manual_seal::EngineCommand<sp_core::H256>>, >, + /// BABE specific dependencies. + pub babe: BabeDeps, } /// Instantiate all full RPC extensions. -pub fn create_full<C, P>( - deps: FullDeps<C, P>, +pub fn create_full<C, P, SC>( + deps: FullDeps<C, P, SC>, ) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>> where C: ProvideRuntimeApi<Block>, @@ -56,10 +74,13 @@ where 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: BabeApi<Block>, C::Api: BlockBuilder<Block>, P: TransactionPool + 'static, + SC: SelectChain<Block> + 'static, { use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; + use sc_consensus_babe_rpc::{Babe, BabeApiServer}; use sc_consensus_manual_seal::rpc::{ManualSeal, ManualSealApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; @@ -67,13 +88,30 @@ where let FullDeps { client, pool, + select_chain, + chain_spec: _, deny_unsafe, command_sink_opt, + babe, } = deps; - module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; - module.merge(TransactionPayment::new(client).into_rpc())?; + let BabeDeps { + babe_worker_handle, + keystore, + } = babe; + module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?; + module.merge(TransactionPayment::new(client.clone()).into_rpc())?; + module.merge( + Babe::new( + client.clone(), + babe_worker_handle.clone(), + keystore, + select_chain, + deny_unsafe, + ) + .into_rpc(), + )?; if let Some(command_sink) = command_sink_opt { // We provide the rpc handler with the sending end of the channel to allow the rpc // send EngineCommands to the background block authorship task. diff --git a/node/src/service.rs b/node/src/service.rs index 55443d3fb7c915e240357efde911592784238107..331a77b377604b6d03477dbd429dfab51e4d93a5 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -218,6 +218,7 @@ pub fn new_partial<RuntimeApi, Executor>( FullGrandpaBlockImport<RuntimeApi, Executor>, >, sc_consensus_babe::BabeLink<Block>, + sc_consensus_babe::BabeWorkerHandle<Block>, sc_consensus_grandpa::LinkHalf< Block, FullClient<RuntimeApi, Executor>, @@ -237,13 +238,6 @@ where RuntimeApiCollection<StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>, Executor: sc_executor::NativeExecutionDispatch + 'static, { - /*if config.keystore_remote.is_some() { - return Err(ServiceError::Other( - "Remote Keystores are not supported.".to_owned(), - )); - }*/ - // TODO deprecated ? - let telemetry = config .telemetry_endpoints .clone() @@ -297,37 +291,35 @@ where client.clone(), )?; - let import_queue = if consensus_manual { - sc_consensus_manual_seal::import_queue( + let slot_duration = babe_link.config().slot_duration(); + let (mut import_queue, babe_worker_handle) = sc_consensus_babe::import_queue( + babe_link.clone(), + babe_block_import.clone(), + Some(Box::new(justification_import)), + client.clone(), + select_chain.clone(), + move |_, ()| async move { + let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); + + let slot = + sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_slot_duration( + *timestamp, + slot_duration, + ); + Ok((slot, timestamp)) + }, + &task_manager.spawn_essential_handle(), + config.prometheus_registry(), + telemetry.as_ref().map(|x| x.handle()), + )?; + + if consensus_manual { + import_queue = sc_consensus_manual_seal::import_queue( Box::new(babe_block_import.clone()), &task_manager.spawn_essential_handle(), config.prometheus_registry(), - ) - } else { - let slot_duration = babe_link.config().slot_duration(); - let (import_queue, _) = sc_consensus_babe::import_queue( - babe_link.clone(), - babe_block_import.clone(), - Some(Box::new(justification_import)), - client.clone(), - select_chain.clone(), - move |_, ()| async move { - let timestamp = sp_timestamp::InherentDataProvider::from_system_time(); - - let slot = - sp_consensus_babe::inherents::InherentDataProvider::from_timestamp_and_slot_duration( - *timestamp, - slot_duration, - ); - - Ok((slot, timestamp)) - }, - &task_manager.spawn_essential_handle(), - config.prometheus_registry(), - telemetry.as_ref().map(|x| x.handle()), - )?; - import_queue - }; + ); + } Ok(sc_service::PartialComponents { client, @@ -337,7 +329,13 @@ where keystore_container, select_chain, transaction_pool, - other: (babe_block_import, babe_link, grandpa_link, telemetry), + other: ( + babe_block_import, + babe_link, + babe_worker_handle, + grandpa_link, + telemetry, + ), }) } @@ -363,21 +361,9 @@ where keystore_container, select_chain, transaction_pool, - other: (block_import, babe_link, grandpa_link, mut telemetry), + other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry), } = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?; - /*if let Some(url) = &config.keystore_remote { - match remote_keystore(url) { - Ok(k) => keystore_container.set_remote_keystore(k), - Err(e) => { - return Err(ServiceError::Other(format!( - "Error hooking up remote keystore for {}: {}", - url, e - ))) - } - }; - }*/// TODO deprecated ? - let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name( &client .block_hash(0) @@ -398,11 +384,9 @@ where Vec::default(), )); - //let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network); let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) = sc_service::build_network(sc_service::BuildNetworkParams { config: &config, - //net_config: net_config, client: client.clone(), transaction_pool: transaction_pool.clone(), spawn_handle: task_manager.spawn_handle(), @@ -497,7 +481,7 @@ where client: client.clone(), pool: transaction_pool.clone(), commands_stream, - select_chain, + select_chain: select_chain.clone(), consensus_data_provider: Some(Box::new(babe_consensus_data_provider)), create_inherent_data_providers: move |_, _| { let client = client_clone.clone(); @@ -521,7 +505,7 @@ where let babe_config = sc_consensus_babe::BabeParams { keystore: keystore_container.keystore(), client: client.clone(), - select_chain, + select_chain: select_chain.clone(), block_import, env: proposer_factory, sync_oracle: sync_service.clone(), @@ -568,23 +552,22 @@ where let rpc_extensions_builder = { let client = client.clone(); - //let keystore = keystore_container.sync_keystore(); let pool = transaction_pool.clone(); - //let select_chain = select_chain.clone(); - //let chain_spec = config.chain_spec.cloned_box(); + let select_chain = select_chain.clone(); + let chain_spec = config.chain_spec.cloned_box(); + let keystore = keystore_container.keystore().clone(); Box::new(move |deny_unsafe, _| { let deps = crate::rpc::FullDeps { client: client.clone(), pool: pool.clone(), - //select_chain: select_chain.clone(), - //chain_spec: chain_spec.cloned_box(), + select_chain: select_chain.clone(), + chain_spec: chain_spec.cloned_box(), deny_unsafe, - /*babe: crate::rpc::BabeDeps { - babe_config: babe_config.clone(), - shared_epoch_changes: shared_epoch_changes.clone(), + babe: crate::rpc::BabeDeps { + babe_worker_handle: babe_worker_handle.clone(), keystore: keystore.clone(), - },*/ + }, command_sink_opt: command_sink_opt.clone(), };