From c3f77d1e37af686514854486b6893740a5354004 Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 14 Oct 2021 17:55:39 +0200 Subject: [PATCH 01/56] Move `retrieve_dmq_contents` and `retrieve_all_inbound_hrmp_channel_contents` to interface --- Cargo.lock | 2 + client/collator/Cargo.toml | 1 + client/collator/src/lib.rs | 128 +++++++++++++++++- polkadot-parachains/src/service.rs | 9 +- primitives/parachain-inherent/Cargo.toml | 2 + .../parachain-inherent/src/client_side.rs | 95 +++---------- 6 files changed, 154 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a5fa57fc8a..3ef52fb4e6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1423,6 +1423,7 @@ dependencies = [ "futures 0.3.17", "parity-scale-codec", "parking_lot 0.10.2", + "polkadot-client", "polkadot-node-primitives", "polkadot-node-subsystem", "polkadot-node-subsystem-test-helpers", @@ -1767,6 +1768,7 @@ name = "cumulus-primitives-parachain-inherent" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-client-collator", "cumulus-primitives-core", "cumulus-test-relay-sproof-builder", "parity-scale-codec", diff --git a/client/collator/Cargo.toml b/client/collator/Cargo.toml index 6f65d480298..8be7f92580c 100644 --- a/client/collator/Cargo.toml +++ b/client/collator/Cargo.toml @@ -17,6 +17,7 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-subsystem = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } # Cumulus dependencies cumulus-client-network = { path = "../network" } diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index 4799d441ee2..d4cbb2a575b 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -17,7 +17,10 @@ //! Cumulus Collator implementation for Substrate. use cumulus_client_network::WaitToAnnounce; -use cumulus_primitives_core::{CollectCollationInfo, ParachainBlockData, PersistedValidationData}; +use cumulus_primitives_core::{ + relay_chain::{v1::ParachainHost, Block as PBlock, Hash as PHash}, + CollectCollationInfo, InboundDownwardMessage, ParachainBlockData, PersistedValidationData, +}; use sc_client_api::BlockBackend; use sp_api::ProvideRuntimeApi; @@ -34,11 +37,14 @@ use polkadot_node_primitives::{ }; use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage}; use polkadot_overseer::Handle as OverseerHandle; -use polkadot_primitives::v1::{CollatorPair, Hash as PHash, HeadData, Id as ParaId}; +use polkadot_primitives::v1::{CollatorPair, HeadData, Id as ParaId}; use codec::{Decode, Encode}; +use cumulus_primitives_core::relay_chain::InboundHrmpMessage; use futures::{channel::oneshot, FutureExt}; use parking_lot::Mutex; +use polkadot_client::{ClientHandle, ExecuteWithClient}; +use sp_core::sp_std::collections::btree_map::BTreeMap; use std::sync::Arc; use tracing::Instrument; @@ -267,6 +273,124 @@ pub struct StartCollatorParams { pub parachain_consensus: Box>, } +pub trait RelayChainInterface { + /// Returns the whole contents of the downward message queue for the parachain we are collating + /// for. + /// + /// Returns `None` in case of an error. + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>; + + /// Returns channels contents for each inbound HRMP channel addressed to the parachain we are + /// collating for. + /// + /// Empty channels are also included. + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>>; +} + +pub struct RelayChainDirect { + pub polkadot_client: polkadot_client::Client, +} + +/// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. +struct DmqContentsWithClient { + relay_parent: PHash, + para_id: ParaId, +} + +impl ExecuteWithClient for DmqContentsWithClient { + type Output = Option>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + let my_client = &*client; + my_client + .runtime_api() + .dmq_contents_with_context( + &BlockId::hash(self.relay_parent), + sp_core::ExecutionContext::Importing, + self.para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?self.relay_parent, + error = ?e, + "An error occured during requesting the downward messages.", + ); + }) + .ok() + } +} + +struct InboundHrmpMessageWithClient { + relay_parent: PHash, + para_id: ParaId, +} + +impl ExecuteWithClient for InboundHrmpMessageWithClient { + type Output = Option>>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + let my_client = &*client; + my_client + .runtime_api() + .inbound_hrmp_channels_contents_with_context( + &BlockId::hash(self.relay_parent), + sp_core::ExecutionContext::Importing, + self.para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?self.relay_parent, + error = ?e, + "An error occured during requesting the inbound HRMP messages.", + ); + }) + .ok() + } +} + +impl RelayChainInterface for RelayChainDirect { + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + self.polkadot_client.execute_with(DmqContentsWithClient { + para_id, + relay_parent, + }) + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + self.polkadot_client + .execute_with(InboundHrmpMessageWithClient { + para_id, + relay_parent, + }) + } +} + /// Start the collator. pub async fn start_collator( StartCollatorParams { diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index a896cad242b..9855eecbef1 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -13,6 +13,8 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . + +use cumulus_client_collator::RelayChainDirect; use cumulus_client_consensus_aura::{ build_aura_consensus, BuildAuraConsensusParams, SlotProportion, }; @@ -704,7 +706,8 @@ pub async fn start_rococo_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_client = relay_chain_node.client.clone(); + let relay_chain_direct = RelayChainDirect{ polkadot_client: relay_chain_node.client.clone() }; + Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -720,9 +723,9 @@ pub async fn start_rococo_parachain_node( proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at_with_client( + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &relay_chain_client, + &relay_chain_direct, &*relay_chain_backend, &validation_data, id, diff --git a/primitives/parachain-inherent/Cargo.toml b/primitives/parachain-inherent/Cargo.toml index b76ec7822d1..aecdaca42b9 100644 --- a/primitives/parachain-inherent/Cargo.toml +++ b/primitives/parachain-inherent/Cargo.toml @@ -21,6 +21,7 @@ polkadot-client = { git = "https://github.com/paritytech/polkadot", optional = t # Cumulus dependencies cumulus-primitives-core = { path = "../core", default-features = false } cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder", optional = true } +cumulus-client-collator = { path = "../../client/collator", optional = true } # Other dependencies async-trait = { version = "0.1.42", optional = true } @@ -45,5 +46,6 @@ std = [ "sc-client-api", "sp-api", "polkadot-client", + "cumulus-client-collator", "cumulus-test-relay-sproof-builder" ] diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 3190b992e7a..d17eaeb3034 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -18,85 +18,23 @@ use crate::ParachainInherentData; use codec::Decode; +use cumulus_client_collator::RelayChainInterface; use cumulus_primitives_core::{ relay_chain::{ self, v1::{HrmpChannelId, ParachainHost}, Block as PBlock, Hash as PHash, }, - InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData, + ParaId, PersistedValidationData, }; use polkadot_client::{Client, ClientHandle, ExecuteWithClient}; use sc_client_api::Backend; use sp_api::ProvideRuntimeApi; use sp_runtime::generic::BlockId; use sp_state_machine::Backend as _; -use std::collections::BTreeMap; const LOG_TARGET: &str = "parachain-inherent"; -/// Returns the whole contents of the downward message queue for the parachain we are collating -/// for. -/// -/// Returns `None` in case of an error. -fn retrieve_dmq_contents( - polkadot_client: &PClient, - para_id: ParaId, - relay_parent: PHash, -) -> Option> -where - PClient: ProvideRuntimeApi, - PClient::Api: ParachainHost, -{ - polkadot_client - .runtime_api() - .dmq_contents_with_context( - &BlockId::hash(relay_parent), - sp_core::ExecutionContext::Importing, - para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "An error occured during requesting the downward messages.", - ); - }) - .ok() -} - -/// Returns channels contents for each inbound HRMP channel addressed to the parachain we are -/// collating for. -/// -/// Empty channels are also included. -fn retrieve_all_inbound_hrmp_channel_contents( - polkadot_client: &PClient, - para_id: ParaId, - relay_parent: PHash, -) -> Option>> -where - PClient: ProvideRuntimeApi, - PClient::Api: ParachainHost, -{ - polkadot_client - .runtime_api() - .inbound_hrmp_channels_contents_with_context( - &BlockId::hash(relay_parent), - sp_core::ExecutionContext::Importing, - para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "An error occured during requesting the inbound HRMP messages.", - ); - }) - .ok() -} - /// Collect the relevant relay chain state in form of a proof for putting it into the validation /// data inherent. fn collect_relay_storage_proof( @@ -197,22 +135,22 @@ impl ParachainInherentData { /// Create the [`ParachainInherentData`] at the given `relay_parent`. /// /// Returns `None` if the creation failed. - pub fn create_at( + pub fn create_at( relay_parent: PHash, - polkadot_client: &PClient, + relay_chain_interface: &T, polkadot_backend: &impl Backend, validation_data: &PersistedValidationData, para_id: ParaId, ) -> Option where - PClient: ProvideRuntimeApi, - PClient::Api: ParachainHost, + T: RelayChainInterface, { let relay_chain_state = collect_relay_storage_proof(polkadot_backend, para_id, relay_parent)?; - let downward_messages = retrieve_dmq_contents(polkadot_client, para_id, relay_parent)?; - let horizontal_messages = - retrieve_all_inbound_hrmp_channel_contents(polkadot_client, para_id, relay_parent)?; + let downward_messages = + relay_chain_interface.retrieve_dmq_contents(para_id, relay_parent)?; + let horizontal_messages = relay_chain_interface + .retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent)?; Some(ParachainInherentData { downward_messages, @@ -281,12 +219,13 @@ where Client: ProvideRuntimeApi, Client::Api: ParachainHost, { - ParachainInherentData::create_at( - self.relay_parent, - &*client, - self.relay_chain_backend, - self.validation_data, - self.para_id, - ) + todo!(); + // ParachainInherentData::create_at( + // self.relay_parent, + // &*client, + // self.relay_chain_backend, + // self.validation_data, + // self.para_id, + // ) } } From 626055478ae1a82818b0ff543e5d27556f5ab608 Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 15 Oct 2021 10:37:32 +0200 Subject: [PATCH 02/56] Add cumulus-client-collator to polkadot-parachains package --- Cargo.lock | 1 + polkadot-parachains/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 3ef52fb4e6f..b887bd328d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6617,6 +6617,7 @@ dependencies = [ "assert_cmd", "async-trait", "cumulus-client-cli", + "cumulus-client-collator", "cumulus-client-consensus-aura", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", diff --git a/polkadot-parachains/Cargo.toml b/polkadot-parachains/Cargo.toml index 3e0a82b754c..cd8e2075b70 100644 --- a/polkadot-parachains/Cargo.toml +++ b/polkadot-parachains/Cargo.toml @@ -70,6 +70,7 @@ cumulus-client-consensus-relay-chain = { path = "../client/consensus/relay-chain cumulus-client-consensus-common = { path = "../client/consensus/common" } cumulus-client-service = { path = "../client/service" } cumulus-client-network = { path = "../client/network" } +cumulus-client-collator = { path = "../client/collator" } cumulus-primitives-core = { path = "../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../primitives/parachain-inherent" } From 830f8e0400d765457eb9d68f9c792c3a2423fc4c Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 18 Oct 2021 11:42:03 +0200 Subject: [PATCH 03/56] Remove more direct references to clients --- Cargo.lock | 3 +- client/collator/src/lib.rs | 18 ++- client/consensus/aura/Cargo.toml | 1 + client/consensus/aura/src/lib.rs | 114 ++++++------------ parachain-template/node/src/service.rs | 23 ++-- polkadot-parachains/src/service.rs | 48 +++++--- primitives/parachain-inherent/Cargo.toml | 4 - .../parachain-inherent/src/client_side.rs | 59 +-------- test/service/Cargo.toml | 1 + test/service/src/lib.rs | 33 ++++- 10 files changed, 117 insertions(+), 187 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b887bd328d8..8e3fd7462b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1444,6 +1444,7 @@ name = "cumulus-client-consensus-aura" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", "futures 0.3.17", @@ -1772,7 +1773,6 @@ dependencies = [ "cumulus-primitives-core", "cumulus-test-relay-sproof-builder", "parity-scale-codec", - "polkadot-client", "sc-client-api", "scale-info", "sp-api", @@ -1937,6 +1937,7 @@ name = "cumulus-test-service" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", "cumulus-client-network", diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index d4cbb2a575b..1e4f8240ec2 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -295,8 +295,9 @@ pub trait RelayChainInterface { ) -> Option>>; } -pub struct RelayChainDirect { - pub polkadot_client: polkadot_client::Client, +#[derive(Clone)] +pub struct RelayChainDirect { + pub polkadot_client: T, } /// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. @@ -366,16 +367,14 @@ impl ExecuteWithClient for InboundHrmpMessageWithClient { } } -impl RelayChainInterface for RelayChainDirect { +impl RelayChainInterface for RelayChainDirect { fn retrieve_dmq_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option> { - self.polkadot_client.execute_with(DmqContentsWithClient { - para_id, - relay_parent, - }) + self.polkadot_client + .execute_with(DmqContentsWithClient { para_id, relay_parent }) } fn retrieve_all_inbound_hrmp_channel_contents( @@ -384,10 +383,7 @@ impl RelayChainInterface for RelayChainDirect { relay_parent: PHash, ) -> Option>> { self.polkadot_client - .execute_with(InboundHrmpMessageWithClient { - para_id, - relay_parent, - }) + .execute_with(InboundHrmpMessageWithClient { para_id, relay_parent }) } } diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 35a0e47fd88..538d7816ca4 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -29,6 +29,7 @@ polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "ma # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } +cumulus-client-collator = { path = "../../../client/collator" } cumulus-primitives-core = { path = "../../../primitives/core" } # Other deps diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 9af7aad1839..734c2a618ec 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -27,11 +27,10 @@ use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; use cumulus_primitives_core::{ - relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost}, + relay_chain::v1::{Block as PBlock, Hash as PHash}, PersistedValidationData, }; use futures::lock::Mutex; -use polkadot_client::ClientHandle; use sc_client_api::{backend::AuxStore, Backend, BlockOf}; use sc_consensus::BlockImport; use sc_consensus_slots::{BackoffAuthoringBlocksStrategy, SlotInfo}; @@ -51,6 +50,7 @@ use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc}; mod import_queue; +use cumulus_client_collator::RelayChainInterface; pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, ImportQueueParams}; pub use sc_consensus_aura::{ slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotDuration, SlotProportion, @@ -60,9 +60,9 @@ pub use sc_consensus_slots::InherentDataProviderExt; const LOG_TARGET: &str = "aura::cumulus"; /// The implementation of the AURA consensus for parachains. -pub struct AuraConsensus { +pub struct AuraConsensus { create_inherent_data_providers: Arc, - relay_chain_client: Arc, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, aura_worker: Arc< Mutex< @@ -74,23 +74,25 @@ pub struct AuraConsensus { slot_duration: SlotDuration, } -impl Clone for AuraConsensus { +impl Clone for AuraConsensus +where + RCInterface: Clone + Send + Sync, +{ fn clone(&self) -> Self { Self { create_inherent_data_providers: self.create_inherent_data_providers.clone(), relay_chain_backend: self.relay_chain_backend.clone(), - relay_chain_client: self.relay_chain_client.clone(), + relay_chain_interface: self.relay_chain_interface.clone(), aura_worker: self.aura_worker.clone(), slot_duration: self.slot_duration, } } } -impl AuraConsensus +impl AuraConsensus where B: BlockT, - RClient: ProvideRuntimeApi, - RClient::Api: ParachainHost, + RCInterface: RelayChainInterface + Clone + Send + Sync, RBackend: Backend, CIDP: CreateInherentDataProviders, CIDP::InherentDataProviders: InherentDataProviderExt, @@ -105,7 +107,7 @@ where backoff_authoring_blocks: Option, keystore: SyncCryptoStorePtr, create_inherent_data_providers: CIDP, - polkadot_client: Arc, + relay_chain_interface: RCInterface, polkadot_backend: Arc, slot_duration: SlotDuration, telemetry: Option, @@ -157,7 +159,7 @@ where Self { create_inherent_data_providers: Arc::new(create_inherent_data_providers), relay_chain_backend: polkadot_backend, - relay_chain_client: polkadot_client, + relay_chain_interface, aura_worker: Arc::new(Mutex::new(worker)), slot_duration, } @@ -200,11 +202,13 @@ where } #[async_trait::async_trait] -impl ParachainConsensus for AuraConsensus +impl ParachainConsensus + for AuraConsensus where B: BlockT, - RClient: ProvideRuntimeApi + Send + Sync, - RClient::Api: ParachainHost, + RCInterface: RelayChainInterface + Clone + Send + Sync, + // RClient: ProvideRuntimeApi + Send + Sync, + // RClient::Api: ParachainHost, RBackend: Backend, CIDP: CreateInherentDataProviders + Send + Sync, CIDP::InherentDataProviders: InherentDataProviderExt + Send, @@ -238,11 +242,11 @@ where } /// Paramaters of [`build_aura_consensus`]. -pub struct BuildAuraConsensusParams { +pub struct BuildAuraConsensusParams { pub proposer_factory: PF, pub create_inherent_data_providers: CIDP, pub block_import: BI, - pub relay_chain_client: polkadot_client::Client, + pub relay_chain_interface: RCInterface, pub relay_chain_backend: Arc, pub para_client: Arc, pub backoff_authoring_blocks: Option, @@ -258,12 +262,12 @@ pub struct BuildAuraConsensusParams { /// Build the [`AuraConsensus`]. /// /// Returns a boxed [`ParachainConsensus`]. -pub fn build_aura_consensus( +pub fn build_aura_consensus( BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers, block_import, - relay_chain_client, + relay_chain_interface, relay_chain_backend, para_client, backoff_authoring_blocks, @@ -274,7 +278,7 @@ pub fn build_aura_consensus, + }: BuildAuraConsensusParams, ) -> Box> where Block: BlockT, @@ -311,12 +315,13 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - AuraConsensusBuilder::::new( + AuraConsensusBuilder::::new( proposer_factory, block_import, create_inherent_data_providers, - relay_chain_client, + relay_chain_interface, relay_chain_backend, para_client, backoff_authoring_blocks, @@ -337,13 +342,13 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instance. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct AuraConsensusBuilder { +struct AuraConsensusBuilder { _phantom: PhantomData<(Block, Error, P)>, proposer_factory: PF, create_inherent_data_providers: CIDP, block_import: BI, relay_chain_backend: Arc, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, para_client: Arc, backoff_authoring_blocks: Option, sync_oracle: SO, @@ -355,8 +360,8 @@ struct AuraConsensusBuilder, } -impl - AuraConsensusBuilder +impl + AuraConsensusBuilder where Block: BlockT, RBackend: Backend + 'static, @@ -392,13 +397,14 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. fn new( proposer_factory: PF, block_import: BI, create_inherent_data_providers: CIDP, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, para_client: Arc, backoff_authoring_blocks: Option, @@ -416,7 +422,7 @@ where block_import, create_inherent_data_providers, relay_chain_backend, - relay_chain_client, + relay_chain_interface, para_client, backoff_authoring_blocks, sync_oracle, @@ -431,58 +437,6 @@ where /// Build the relay chain consensus. fn build(self) -> Box> { - self.relay_chain_client.clone().execute_with(self) - } -} - -impl polkadot_client::ExecuteWithClient - for AuraConsensusBuilder -where - Block: BlockT, - RBackend: Backend + 'static, - CIDP: CreateInherentDataProviders - + Send - + Sync - + 'static, - CIDP::InherentDataProviders: InherentDataProviderExt + Send, - Client: ProvideRuntimeApi - + BlockOf - + ProvideCache - + AuxStore - + HeaderBackend - + Send - + Sync - + 'static, - Client::Api: AuraApi, - BI: BlockImport> - + Send - + Sync - + 'static, - SO: SyncOracle + Send + Sync + Clone + 'static, - BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, - PF: Environment + Send + Sync + 'static, - PF::Proposer: Proposer< - Block, - Error = Error, - Transaction = sp_api::TransactionFor, - ProofRecording = EnableProofRecording, - Proof = ::Proof, - >, - Error: std::error::Error + Send + From + 'static, - P: Pair + Send + Sync, - P::Public: AppPublic + Hash + Member + Encode + Decode, - P::Signature: TryFrom> + Hash + Member + Encode + Decode, -{ - type Output = Box>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: sp_api::StateBackend>, - PBackend: Backend, - PBackend::State: sp_api::StateBackend, - Api: polkadot_client::RuntimeApiCollection, - PClient: polkadot_client::AbstractClient + 'static, - { Box::new(AuraConsensus::new::( self.para_client, self.block_import, @@ -492,7 +446,7 @@ where self.backoff_authoring_blocks, self.keystore, self.create_inherent_data_providers, - client.clone(), + self.relay_chain_interface.clone(), self.relay_chain_backend, self.slot_duration, self.telemetry, diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index ff11038ad0f..1f9c89d907c 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,6 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; // Substrate Imports +use cumulus_client_collator::RelayChainDirect; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -431,7 +432,8 @@ pub async fn start_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_client = relay_chain_node.client.clone(); + let relay_chain_interface = + RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -443,17 +445,18 @@ pub async fn start_parachain_node( _, _, _, + _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at_with_client( - relay_parent, - &relay_chain_client, - &*relay_chain_backend, - &validation_data, - id, - ); + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( + relay_parent, + &relay_chain_interface, + &*relay_chain_backend, + &validation_data, + id, + ); async move { let time = sp_timestamp::InherentDataProvider::from_system_time(); @@ -472,7 +475,9 @@ pub async fn start_parachain_node( } }, block_import: client.clone(), - relay_chain_client: relay_chain_node.client.clone(), + relay_chain_interface: RelayChainDirect { + polkadot_client: relay_chain_node.client.clone(), + }, relay_chain_backend: relay_chain_node.backend.clone(), para_client: client, backoff_authoring_blocks: Option::<()>::None, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 9855eecbef1..42d5f5cf0a5 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -719,6 +719,7 @@ pub async fn start_rococo_parachain_node( _, _, _, + _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -748,7 +749,7 @@ pub async fn start_rococo_parachain_node( } }, block_import: client.clone(), - relay_chain_client: relay_chain_node.client.clone(), + relay_chain_interface: RelayChainDirect{polkadot_client: relay_chain_node.client.clone()} , relay_chain_backend: relay_chain_node.backend.clone(), para_client: client.clone(), backoff_authoring_blocks: Option::<()>::None, @@ -827,7 +828,8 @@ pub async fn start_shell_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_client = relay_chain_node.client.clone(); + let relay_chain_interface = + RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { @@ -838,13 +840,13 @@ pub async fn start_shell_node( relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at_with_client( - relay_parent, - &relay_chain_client, - &*relay_chain_backend, - &validation_data, - id, - ); + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( + relay_parent, + &relay_chain_interface, + &*relay_chain_backend, + &validation_data, + id, + ); async move { let parachain_inherent = parachain_inherent.ok_or_else(|| { Box::::from( @@ -1110,6 +1112,8 @@ where let relay_chain_backend2 = relay_chain_backend.clone(); let relay_chain_client2 = relay_chain_client.clone(); + let relay_chain_interface = + RelayChainDirect { polkadot_client: relay_chain_client.clone() }; build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -1122,17 +1126,18 @@ where _, _, _, + _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at_with_client( - relay_parent, - &relay_chain_client, - &*relay_chain_backend, - &validation_data, - id, - ); + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( + relay_parent, + &relay_chain_interface, + &*relay_chain_backend, + &validation_data, + id, + ); async move { let time = sp_timestamp::InherentDataProvider::from_system_time(); @@ -1151,7 +1156,9 @@ where } }, block_import: client2.clone(), - relay_chain_client: relay_chain_client2, + relay_chain_interface: RelayChainDirect { + polkadot_client: relay_chain_client2.clone(), + }, relay_chain_backend: relay_chain_backend2, para_client: client2.clone(), backoff_authoring_blocks: Option::<()>::None, @@ -1176,7 +1183,8 @@ where ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_client = relay_chain_node.client.clone(); + let relay_chain_interface = + RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; let relay_chain_consensus = cumulus_client_consensus_relay_chain::build_relay_chain_consensus( @@ -1189,9 +1197,9 @@ where create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at_with_client( + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &relay_chain_client, + &relay_chain_interface, &*relay_chain_backend, &validation_data, id, diff --git a/primitives/parachain-inherent/Cargo.toml b/primitives/parachain-inherent/Cargo.toml index aecdaca42b9..dcb653fa398 100644 --- a/primitives/parachain-inherent/Cargo.toml +++ b/primitives/parachain-inherent/Cargo.toml @@ -15,9 +15,6 @@ sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = " sp-trie = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true } -# Polkadot dependencies -polkadot-client = { git = "https://github.com/paritytech/polkadot", optional = true, branch = "master" } - # Cumulus dependencies cumulus-primitives-core = { path = "../core", default-features = false } cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder", optional = true } @@ -45,7 +42,6 @@ std = [ "sp-runtime", "sc-client-api", "sp-api", - "polkadot-client", "cumulus-client-collator", "cumulus-test-relay-sproof-builder" ] diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index d17eaeb3034..7ccf84d6c5b 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -20,16 +20,10 @@ use crate::ParachainInherentData; use codec::Decode; use cumulus_client_collator::RelayChainInterface; use cumulus_primitives_core::{ - relay_chain::{ - self, - v1::{HrmpChannelId, ParachainHost}, - Block as PBlock, Hash as PHash, - }, + relay_chain::{self, v1::HrmpChannelId, Block as PBlock, Hash as PHash}, ParaId, PersistedValidationData, }; -use polkadot_client::{Client, ClientHandle, ExecuteWithClient}; use sc_client_api::Backend; -use sp_api::ProvideRuntimeApi; use sp_runtime::generic::BlockId; use sp_state_machine::Backend as _; @@ -159,24 +153,6 @@ impl ParachainInherentData { relay_chain_state, }) } - - /// Create the [`ParachainInherentData`] at the given `relay_parent`. - /// - /// Returns `None` if the creation failed. - pub fn create_at_with_client( - relay_parent: PHash, - polkadot_client: &Client, - relay_chain_backend: &impl Backend, - validation_data: &PersistedValidationData, - para_id: ParaId, - ) -> Option { - polkadot_client.execute_with(CreateAtWithClient { - relay_chain_backend, - validation_data, - para_id, - relay_parent, - }) - } } #[async_trait::async_trait] @@ -196,36 +172,3 @@ impl sp_inherents::InherentDataProvider for ParachainInherentData { None } } - -/// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. -struct CreateAtWithClient<'a, B> { - relay_parent: PHash, - relay_chain_backend: &'a B, - validation_data: &'a PersistedValidationData, - para_id: ParaId, -} - -impl<'a, B> ExecuteWithClient for CreateAtWithClient<'a, B> -where - B: Backend, -{ - type Output = Option; - - fn execute_with_client( - self, - client: std::sync::Arc, - ) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - todo!(); - // ParachainInherentData::create_at( - // self.relay_parent, - // &*client, - // self.relay_chain_backend, - // self.validation_data, - // self.para_id, - // ) - } -} diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index f0cfe177c92..2b8417cdfba 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -43,6 +43,7 @@ polkadot-test-service = { git = "https://github.com/paritytech/polkadot", branch cumulus-client-consensus-relay-chain = { path = "../../client/consensus/relay-chain" } cumulus-client-network = { path = "../../client/network" } cumulus-client-service = { path = "../../client/service" } +cumulus-client-collator = { path = "../../client/collator" } cumulus-client-consensus-common = { path = "../../client/consensus/common" } cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index d255eb378eb..9160cf8030a 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -22,12 +22,13 @@ mod chain_spec; mod genesis; use core::future::Future; +use cumulus_client_collator::RelayChainInterface; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; -use cumulus_primitives_core::ParaId; +use cumulus_primitives_core::{InboundDownwardMessage, ParaId}; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use sc_client_api::execution_extensions::ExecutionStrategies; @@ -47,12 +48,13 @@ use sp_keyring::Sr25519Keyring; use sp_runtime::{codec::Encode, generic, traits::BlakeTwo256}; use sp_state_machine::BasicExternalities; use sp_trie::PrefixedMemoryDB; -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc}; use substrate_test_client::{ BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput, }; pub use chain_spec::*; +use cumulus_primitives_core::relay_chain::InboundHrmpMessage; pub use cumulus_test_runtime as runtime; pub use genesis::*; pub use sp_keyring::Sr25519Keyring as Keyring; @@ -157,6 +159,28 @@ pub fn new_partial( Ok(params) } +struct RelayChainFullClient { + full_client: Arc, +} + +impl RelayChainInterface for RelayChainFullClient { + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + todo!() + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + todo!() + } +} + /// Start a node with the given parachain `Configuration` and relay chain `Configuration`. /// /// This is the actual implementation that is abstract over the executor and the runtime api. @@ -270,9 +294,10 @@ where prometheus_registry.as_ref(), None, ); - let relay_chain_client = relay_chain_full_node.client.clone(); let relay_chain_backend = relay_chain_full_node.backend.clone(); + let relay_chain_interface = + RelayChainFullClient { full_client: relay_chain_client }; Box::new(cumulus_client_consensus_relay_chain::RelayChainConsensus::new( para_id, @@ -281,7 +306,7 @@ where let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &*relay_chain_client, + &relay_chain_interface, &*relay_chain_backend, &validation_data, para_id, From 387b32f39c1b3f0a8e6395023c510f54c89af7d6 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 19 Oct 2021 16:02:47 +0200 Subject: [PATCH 04/56] Remove more direct references to clients --- Cargo.lock | 3 +- client/consensus/aura/Cargo.toml | 3 - client/consensus/aura/src/lib.rs | 2 +- client/consensus/relay-chain/Cargo.toml | 2 +- client/consensus/relay-chain/src/lib.rs | 94 +++++++++---------------- polkadot-parachains/src/service.rs | 8 ++- 6 files changed, 44 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e3fd7462b0..792acf379d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1449,7 +1449,6 @@ dependencies = [ "cumulus-primitives-core", "futures 0.3.17", "parity-scale-codec", - "polkadot-client", "sc-client-api", "sc-consensus", "sc-consensus-aura", @@ -1496,11 +1495,11 @@ name = "cumulus-client-consensus-relay-chain" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", "futures 0.3.17", "parking_lot 0.10.2", - "polkadot-client", "sc-client-api", "sc-consensus", "sp-api", diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 538d7816ca4..6c9b0b8b05d 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -24,9 +24,6 @@ sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "mast sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", branch = "master" } -# Polkadot dependencies -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } - # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } cumulus-client-collator = { path = "../../../client/collator" } diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 734c2a618ec..f4b1b0d2148 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -45,7 +45,7 @@ use sp_consensus_aura::AuraApi; use sp_core::crypto::Pair; use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider}; use sp_keystore::SyncCryptoStorePtr; -use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT, Member, NumberFor}; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member, NumberFor}; use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc}; mod import_queue; diff --git a/client/consensus/relay-chain/Cargo.toml b/client/consensus/relay-chain/Cargo.toml index 5da2af5bf11..1005f28e8be 100644 --- a/client/consensus/relay-chain/Cargo.toml +++ b/client/consensus/relay-chain/Cargo.toml @@ -19,11 +19,11 @@ sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "mast substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", branch = "master" } # Polkadot dependencies -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } cumulus-primitives-core = { path = "../../../primitives/core" } +cumulus-client-collator = { path = "../../../client/collator" } # Other deps futures = { version = "0.3.8", features = ["compat"] } diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 61eeba1c3b1..485c8649f82 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -33,23 +33,22 @@ //! //! 5. After the parachain candidate got backed and included, all collators start at 1. +use cumulus_client_collator::RelayChainInterface; use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; use cumulus_primitives_core::{ - relay_chain::v1::{Block as PBlock, Hash as PHash, ParachainHost}, + relay_chain::v1::{Block as PBlock, Hash as PHash}, ParaId, PersistedValidationData, }; use parking_lot::Mutex; -use polkadot_client::ClientHandle; use sc_client_api::Backend; use sc_consensus::{BlockImport, BlockImportParams}; -use sp_api::ProvideRuntimeApi; use sp_consensus::{ BlockOrigin, EnableProofRecording, Environment, ProofRecording, Proposal, Proposer, }; use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider}; -use sp_runtime::traits::{Block as BlockT, HashFor, Header as HeaderT}; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{marker::PhantomData, sync::Arc, time::Duration}; mod import_queue; @@ -58,18 +57,20 @@ pub use import_queue::{import_queue, Verifier}; const LOG_TARGET: &str = "cumulus-consensus-relay-chain"; /// The implementation of the relay-chain provided consensus for parachains. -pub struct RelayChainConsensus { +pub struct RelayChainConsensus { para_id: ParaId, _phantom: PhantomData, proposer_factory: Arc>, create_inherent_data_providers: Arc, block_import: Arc>>, - relay_chain_client: Arc, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, } -impl Clone - for RelayChainConsensus +impl Clone + for RelayChainConsensus +where + RCInterface: Clone, { fn clone(&self) -> Self { Self { @@ -79,16 +80,18 @@ impl Clone create_inherent_data_providers: self.create_inherent_data_providers.clone(), block_import: self.block_import.clone(), relay_chain_backend: self.relay_chain_backend.clone(), - relay_chain_client: self.relay_chain_client.clone(), + relay_chain_interface: self.relay_chain_interface.clone(), } } } -impl RelayChainConsensus +impl + RelayChainConsensus where B: BlockT, - RClient: ProvideRuntimeApi, - RClient::Api: ParachainHost, + // RClient: ProvideRuntimeApi, + // RClient::Api: ParachainHost, + RCInterface: RelayChainInterface, RBackend: Backend, CIDP: CreateInherentDataProviders, { @@ -98,7 +101,7 @@ where proposer_factory: PF, create_inherent_data_providers: CIDP, block_import: BI, - polkadot_client: Arc, + relay_chain_interface: RCInterface, polkadot_backend: Arc, ) -> Self { Self { @@ -109,7 +112,7 @@ where block_import, ))), relay_chain_backend: polkadot_backend, - relay_chain_client: polkadot_client, + relay_chain_interface, _phantom: PhantomData, } } @@ -148,12 +151,11 @@ where } #[async_trait::async_trait] -impl ParachainConsensus - for RelayChainConsensus +impl ParachainConsensus + for RelayChainConsensus where B: BlockT, - RClient: ProvideRuntimeApi + Send + Sync, - RClient::Api: ParachainHost, + RCInterface: RelayChainInterface + Send + Sync + Clone, RBackend: Backend, BI: BlockImport + Send + Sync, PF: Environment + Send + Sync, @@ -229,27 +231,27 @@ where } /// Paramaters of [`build_relay_chain_consensus`]. -pub struct BuildRelayChainConsensusParams { +pub struct BuildRelayChainConsensusParams { pub para_id: ParaId, pub proposer_factory: PF, pub create_inherent_data_providers: CIDP, pub block_import: BI, - pub relay_chain_client: polkadot_client::Client, + pub relay_chain_interface: RCInterface, pub relay_chain_backend: Arc, } /// Build the [`RelayChainConsensus`]. /// /// Returns a boxed [`ParachainConsensus`]. -pub fn build_relay_chain_consensus( +pub fn build_relay_chain_consensus( BuildRelayChainConsensusParams { para_id, proposer_factory, create_inherent_data_providers, block_import, - relay_chain_client, + relay_chain_interface, relay_chain_backend, - }: BuildRelayChainConsensusParams, + }: BuildRelayChainConsensusParams, ) -> Box> where Block: BlockT, @@ -263,13 +265,14 @@ where BI: BlockImport + Send + Sync + 'static, RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { RelayChainConsensusBuilder::new( para_id, proposer_factory, block_import, create_inherent_data_providers, - relay_chain_client, + relay_chain_interface, relay_chain_backend, ) .build() @@ -281,17 +284,18 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct RelayChainConsensusBuilder { +struct RelayChainConsensusBuilder { para_id: ParaId, _phantom: PhantomData, proposer_factory: PF, create_inherent_data_providers: CIDP, block_import: BI, relay_chain_backend: Arc, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, } -impl RelayChainConsensusBuilder +impl + RelayChainConsensusBuilder where Block: BlockT, PF: Environment + Send + Sync + 'static, @@ -304,6 +308,7 @@ where BI: BlockImport + Send + Sync + 'static, RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, { /// Create a new instance of the builder. fn new( @@ -311,7 +316,7 @@ where proposer_factory: PF, block_import: BI, create_inherent_data_providers: CIDP, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, ) -> Self { Self { @@ -321,47 +326,18 @@ where block_import, create_inherent_data_providers, relay_chain_backend, - relay_chain_client, + relay_chain_interface, } } /// Build the relay chain consensus. fn build(self) -> Box> { - self.relay_chain_client.clone().execute_with(self) - } -} - -impl polkadot_client::ExecuteWithClient - for RelayChainConsensusBuilder -where - Block: BlockT, - PF: Environment + Send + Sync + 'static, - PF::Proposer: Proposer< - Block, - Transaction = BI::Transaction, - ProofRecording = EnableProofRecording, - Proof = ::Proof, - >, - BI: BlockImport + Send + Sync + 'static, - RBackend: Backend + 'static, - CIDP: CreateInherentDataProviders + 'static, -{ - type Output = Box>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: sp_api::StateBackend>, - PBackend: Backend, - PBackend::State: sp_api::StateBackend, - Api: polkadot_client::RuntimeApiCollection, - PClient: polkadot_client::AbstractClient + 'static, - { Box::new(RelayChainConsensus::new( self.para_id, self.proposer_factory, self.create_inherent_data_providers, self.block_import, - client.clone(), + self.relay_chain_interface, self.relay_chain_backend, )) } diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 42d5f5cf0a5..f933f108ae0 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -836,7 +836,9 @@ pub async fn start_shell_node( para_id: id, proposer_factory, block_import: client.clone(), - relay_chain_client: relay_chain_node.client.clone(), + relay_chain_interface: RelayChainDirect { + polkadot_client: relay_chain_node.client.clone(), + }, relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = @@ -1192,7 +1194,9 @@ where para_id: id, proposer_factory, block_import: client.clone(), - relay_chain_client: relay_chain_node.client.clone(), + relay_chain_interface: RelayChainDirect { + polkadot_client: relay_chain_node.client.clone(), + }, relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { From b178df19ebb2962e305966d8e8d5d82f1929c4f6 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 20 Oct 2021 17:22:52 +0200 Subject: [PATCH 05/56] Move interface to its own crate --- Cargo.lock | 21 +++- Cargo.toml | 1 + client/collator/Cargo.toml | 2 +- client/collator/src/lib.rs | 115 +----------------- client/consensus/aura/Cargo.toml | 1 + client/consensus/aura/src/lib.rs | 2 +- client/consensus/relay-chain/Cargo.toml | 2 +- client/consensus/relay-chain/src/lib.rs | 2 +- client/network/Cargo.toml | 2 + client/network/src/lib.rs | 3 +- parachain-template/node/Cargo.toml | 1 + parachain-template/node/src/service.rs | 2 +- polkadot-parachains/Cargo.toml | 1 + polkadot-parachains/src/service.rs | 2 +- primitives/parachain-inherent/Cargo.toml | 4 +- .../parachain-inherent/src/client_side.rs | 2 +- test/service/Cargo.toml | 2 +- 17 files changed, 38 insertions(+), 127 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 792acf379d9..6235b80819b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1418,6 +1418,7 @@ dependencies = [ "cumulus-client-consensus-common", "cumulus-client-network", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-client", "cumulus-test-runtime", "futures 0.3.17", @@ -1447,6 +1448,7 @@ dependencies = [ "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.17", "parity-scale-codec", "sc-client-api", @@ -1495,9 +1497,9 @@ name = "cumulus-client-consensus-relay-chain" version = "0.1.0" dependencies = [ "async-trait", - "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.17", "parking_lot 0.10.2", "sc-client-api", @@ -1518,6 +1520,7 @@ name = "cumulus-client-network" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-service", "derive_more", "futures 0.3.17", @@ -1768,8 +1771,8 @@ name = "cumulus-primitives-parachain-inherent" version = "0.1.0" dependencies = [ "async-trait", - "cumulus-client-collator", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-relay-sproof-builder", "parity-scale-codec", "sc-client-api", @@ -1817,6 +1820,17 @@ dependencies = [ "xcm", ] +[[package]] +name = "cumulus-relay-chain-interface" +version = "0.1.0" +dependencies = [ + "cumulus-primitives-core", + "polkadot-client", + "sp-api", + "sp-core", + "tracing", +] + [[package]] name = "cumulus-test-client" version = "0.1.0" @@ -1943,6 +1957,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "cumulus-test-relay-validation-worker-provider", "cumulus-test-runtime", "cumulus-test-runtime-upgrade", @@ -6011,6 +6026,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "derive_more", "frame-benchmarking", "frame-benchmarking-cli", @@ -6625,6 +6641,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "frame-benchmarking", "frame-benchmarking-cli", "futures 0.3.17", diff --git a/Cargo.toml b/Cargo.toml index c89e88b214e..7845c62f249 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "client/network", "client/pov-recovery", "client/service", + "client/relay-chain-interface", "pallets/asset-tx-payment", "pallets/aura-ext", "pallets/collator-selection", diff --git a/client/collator/Cargo.toml b/client/collator/Cargo.toml index 8be7f92580c..35bcd6848f6 100644 --- a/client/collator/Cargo.toml +++ b/client/collator/Cargo.toml @@ -23,7 +23,7 @@ polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "ma cumulus-client-network = { path = "../network" } cumulus-client-consensus-common = { path = "../consensus/common" } cumulus-primitives-core = { path = "../../primitives/core" } - +cumulus-relay-chain-interface = { path = "../relay-chain-interface" } # Other dependencies codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } futures = { version = "0.3.1", features = ["compat"] } diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index 1e4f8240ec2..70b5f02de4a 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -21,6 +21,7 @@ use cumulus_primitives_core::{ relay_chain::{v1::ParachainHost, Block as PBlock, Hash as PHash}, CollectCollationInfo, InboundDownwardMessage, ParachainBlockData, PersistedValidationData, }; +use cumulus_relay_chain_interface::RelayChainInterface; use sc_client_api::BlockBackend; use sp_api::ProvideRuntimeApi; @@ -273,120 +274,6 @@ pub struct StartCollatorParams { pub parachain_consensus: Box>, } -pub trait RelayChainInterface { - /// Returns the whole contents of the downward message queue for the parachain we are collating - /// for. - /// - /// Returns `None` in case of an error. - fn retrieve_dmq_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>; - - /// Returns channels contents for each inbound HRMP channel addressed to the parachain we are - /// collating for. - /// - /// Empty channels are also included. - fn retrieve_all_inbound_hrmp_channel_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>>; -} - -#[derive(Clone)] -pub struct RelayChainDirect { - pub polkadot_client: T, -} - -/// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. -struct DmqContentsWithClient { - relay_parent: PHash, - para_id: ParaId, -} - -impl ExecuteWithClient for DmqContentsWithClient { - type Output = Option>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - let my_client = &*client; - my_client - .runtime_api() - .dmq_contents_with_context( - &BlockId::hash(self.relay_parent), - sp_core::ExecutionContext::Importing, - self.para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?self.relay_parent, - error = ?e, - "An error occured during requesting the downward messages.", - ); - }) - .ok() - } -} - -struct InboundHrmpMessageWithClient { - relay_parent: PHash, - para_id: ParaId, -} - -impl ExecuteWithClient for InboundHrmpMessageWithClient { - type Output = Option>>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - let my_client = &*client; - my_client - .runtime_api() - .inbound_hrmp_channels_contents_with_context( - &BlockId::hash(self.relay_parent), - sp_core::ExecutionContext::Importing, - self.para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?self.relay_parent, - error = ?e, - "An error occured during requesting the inbound HRMP messages.", - ); - }) - .ok() - } -} - -impl RelayChainInterface for RelayChainDirect { - fn retrieve_dmq_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option> { - self.polkadot_client - .execute_with(DmqContentsWithClient { para_id, relay_parent }) - } - - fn retrieve_all_inbound_hrmp_channel_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>> { - self.polkadot_client - .execute_with(InboundHrmpMessageWithClient { para_id, relay_parent }) - } -} - /// Start the collator. pub async fn start_collator( StartCollatorParams { diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 6c9b0b8b05d..2d495474e76 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -28,6 +28,7 @@ substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate cumulus-client-consensus-common = { path = "../common" } cumulus-client-collator = { path = "../../../client/collator" } cumulus-primitives-core = { path = "../../../primitives/core" } +cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } # Other deps futures = { version = "0.3.8", features = ["compat"] } diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index f4b1b0d2148..ed5ff06cc14 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -50,7 +50,7 @@ use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc}; mod import_queue; -use cumulus_client_collator::RelayChainInterface; +use cumulus_relay_chain_interface::RelayChainInterface; pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, ImportQueueParams}; pub use sc_consensus_aura::{ slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotDuration, SlotProportion, diff --git a/client/consensus/relay-chain/Cargo.toml b/client/consensus/relay-chain/Cargo.toml index 1005f28e8be..4a94ddef12b 100644 --- a/client/consensus/relay-chain/Cargo.toml +++ b/client/consensus/relay-chain/Cargo.toml @@ -23,7 +23,7 @@ substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } cumulus-primitives-core = { path = "../../../primitives/core" } -cumulus-client-collator = { path = "../../../client/collator" } +cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } # Other deps futures = { version = "0.3.8", features = ["compat"] } diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 485c8649f82..1f0c4fa3554 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -33,7 +33,6 @@ //! //! 5. After the parachain candidate got backed and included, all collators start at 1. -use cumulus_client_collator::RelayChainInterface; use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; @@ -41,6 +40,7 @@ use cumulus_primitives_core::{ relay_chain::v1::{Block as PBlock, Hash as PHash}, ParaId, PersistedValidationData, }; +use cumulus_relay_chain_interface::RelayChainInterface; use parking_lot::Mutex; use sc_client_api::Backend; use sc_consensus::{BlockImport, BlockImportParams}; diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index ecdf4c9704a..107becb35ae 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -28,6 +28,8 @@ tracing = "0.1.22" parking_lot = "0.10.2" derive_more = "0.99.2" +cumulus-relay-chain-interface = {path = "../relay-chain-interface"} + [dev-dependencies] tokio = { version = "1.10", features = ["macros"] } diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index e183fcd461c..ac225eed9cb 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -33,7 +33,7 @@ use sp_runtime::{ traits::{Block as BlockT, Header as HeaderT}, }; -use polkadot_client::ClientHandle; +use cumulus_relay_chain_interface::RelayChainInterface; use polkadot_node_primitives::{CollationSecondedSignal, Statement}; use polkadot_parachain::primitives::HeadData; use polkadot_primitives::v1::{ @@ -48,6 +48,7 @@ use futures::{ Future, }; +use polkadot_client::ClientHandle; use std::{convert::TryFrom, fmt, marker::PhantomData, pin::Pin, sync::Arc}; use wait_on_relay_chain_block::WaitOnRelayChainBlock; diff --git a/parachain-template/node/Cargo.toml b/parachain-template/node/Cargo.toml index 58e10bae35b..d21f64cdc51 100644 --- a/parachain-template/node/Cargo.toml +++ b/parachain-template/node/Cargo.toml @@ -86,6 +86,7 @@ cumulus-client-network = { path = "../../client/network" } cumulus-client-service = { path = "../../client/service" } cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } +cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface" } # Polkadot dependencies polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 1f9c89d907c..d5aec1d02e4 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,7 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; // Substrate Imports -use cumulus_client_collator::RelayChainDirect; +use cumulus_relay_chain_interface::RelayChainDirect; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; diff --git a/polkadot-parachains/Cargo.toml b/polkadot-parachains/Cargo.toml index cd8e2075b70..879f430a994 100644 --- a/polkadot-parachains/Cargo.toml +++ b/polkadot-parachains/Cargo.toml @@ -73,6 +73,7 @@ cumulus-client-network = { path = "../client/network" } cumulus-client-collator = { path = "../client/collator" } cumulus-primitives-core = { path = "../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../primitives/parachain-inherent" } +cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" } # Polkadot dependencies polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index f933f108ae0..9c7414dc242 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use cumulus_client_collator::RelayChainDirect; use cumulus_client_consensus_aura::{ build_aura_consensus, BuildAuraConsensusParams, SlotProportion, }; @@ -29,6 +28,7 @@ use cumulus_primitives_core::{ relay_chain::v1::{Hash as PHash, PersistedValidationData}, ParaId, }; +use cumulus_relay_chain_interface::RelayChainDirect; use polkadot_service::NativeExecutionDispatch; use crate::rpc; diff --git a/primitives/parachain-inherent/Cargo.toml b/primitives/parachain-inherent/Cargo.toml index dcb653fa398..ccd23dde3bc 100644 --- a/primitives/parachain-inherent/Cargo.toml +++ b/primitives/parachain-inherent/Cargo.toml @@ -18,7 +18,7 @@ sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", o # Cumulus dependencies cumulus-primitives-core = { path = "../core", default-features = false } cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder", optional = true } -cumulus-client-collator = { path = "../../client/collator", optional = true } +cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface", optional = true } # Other dependencies async-trait = { version = "0.1.42", optional = true } @@ -42,6 +42,6 @@ std = [ "sp-runtime", "sc-client-api", "sp-api", - "cumulus-client-collator", + "cumulus-relay-chain-interface", "cumulus-test-relay-sproof-builder" ] diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 7ccf84d6c5b..d456751782c 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -18,11 +18,11 @@ use crate::ParachainInherentData; use codec::Decode; -use cumulus_client_collator::RelayChainInterface; use cumulus_primitives_core::{ relay_chain::{self, v1::HrmpChannelId, Block as PBlock, Hash as PHash}, ParaId, PersistedValidationData, }; +use cumulus_relay_chain_interface::RelayChainInterface; use sc_client_api::Backend; use sp_runtime::generic::BlockId; use sp_state_machine::Backend as _; diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index 2b8417cdfba..35470e3ed66 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -49,7 +49,7 @@ cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } cumulus-test-runtime = { path = "../runtime" } cumulus-test-relay-validation-worker-provider = { path = "../relay-validation-worker-provider" } - +cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface" } # RPC related dependencies jsonrpc-core = "18.0.0" From 5b4b9d74034ad50a833f1435fd4cb305cbc75907 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 20 Oct 2021 17:23:21 +0200 Subject: [PATCH 06/56] Remove more direct references to clients --- client/relay-chain-interface/Cargo.toml | 16 +++ client/relay-chain-interface/src/lib.rs | 123 ++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 client/relay-chain-interface/Cargo.toml create mode 100644 client/relay-chain-interface/src/lib.rs diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml new file mode 100644 index 00000000000..4854e5c1221 --- /dev/null +++ b/client/relay-chain-interface/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "cumulus-relay-chain-interface" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } + +cumulus-primitives-core = { path = "../../primitives/core" } + +sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } + +tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs new file mode 100644 index 00000000000..b2bbbc39b63 --- /dev/null +++ b/client/relay-chain-interface/src/lib.rs @@ -0,0 +1,123 @@ +use cumulus_primitives_core::{ + relay_chain::{v1::ParachainHost, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage}, + InboundDownwardMessage, ParaId, +}; +use polkadot_client::{ClientHandle, ExecuteWithClient}; +use sp_api::ProvideRuntimeApi; +use sp_core::sp_std::{collections::btree_map::BTreeMap, sync::Arc}; + +const LOG_TARGET: &str = "cumulus-collator"; + +pub trait RelayChainInterface { + /// Returns the whole contents of the downward message queue for the parachain we are collating + /// for. + /// + /// Returns `None` in case of an error. + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>; + + /// Returns channels contents for each inbound HRMP channel addressed to the parachain we are + /// collating for. + /// + /// Empty channels are also included. + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>>; +} + +#[derive(Clone)] +pub struct RelayChainDirect { + pub polkadot_client: T, +} + +/// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. +struct DmqContentsWithClient { + relay_parent: PHash, + para_id: ParaId, +} + +impl ExecuteWithClient for DmqContentsWithClient { + type Output = Option>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + let my_client = &*client; + my_client + .runtime_api() + .dmq_contents_with_context( + &BlockId::hash(self.relay_parent), + sp_core::ExecutionContext::Importing, + self.para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?self.relay_parent, + error = ?e, + "An error occured during requesting the downward messages.", + ); + }) + .ok() + } +} + +struct InboundHrmpMessageWithClient { + relay_parent: PHash, + para_id: ParaId, +} + +impl ExecuteWithClient for InboundHrmpMessageWithClient { + type Output = Option>>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + let my_client = &*client; + my_client + .runtime_api() + .inbound_hrmp_channels_contents_with_context( + &BlockId::hash(self.relay_parent), + sp_core::ExecutionContext::Importing, + self.para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?self.relay_parent, + error = ?e, + "An error occured during requesting the inbound HRMP messages.", + ); + }) + .ok() + } +} + +impl RelayChainInterface for RelayChainDirect { + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + self.polkadot_client + .execute_with(DmqContentsWithClient { para_id, relay_parent }) + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + self.polkadot_client + .execute_with(InboundHrmpMessageWithClient { para_id, relay_parent }) + } +} From cc66c2f3319c8b8752303904b7cab999f17e9992 Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 22 Oct 2021 11:44:56 +0200 Subject: [PATCH 07/56] Remove more direct references to clients --- Cargo.lock | 3 +- client/network/Cargo.toml | 1 - client/network/src/lib.rs | 112 ++++++++++++------------ client/relay-chain-interface/Cargo.toml | 2 + client/relay-chain-interface/src/lib.rs | 92 ++++++++++++++++++- 5 files changed, 150 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6235b80819b..f6e1905c31b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1527,7 +1527,6 @@ dependencies = [ "futures-timer 3.0.2", "parity-scale-codec", "parking_lot 0.10.2", - "polkadot-client", "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", @@ -1826,8 +1825,10 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "polkadot-client", + "sc-client-api", "sp-api", "sp-core", + "sp-runtime", "tracing", ] diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 107becb35ae..195dc1188a3 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -17,7 +17,6 @@ sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "mas # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } # other deps diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index ac225eed9cb..be6a467f997 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -48,7 +48,6 @@ use futures::{ Future, }; -use polkadot_client::ClientHandle; use std::{convert::TryFrom, fmt, marker::PhantomData, pin::Pin, sync::Arc}; use wait_on_relay_chain_block::WaitOnRelayChainBlock; @@ -231,27 +230,27 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// chain. If it is at the tip, it is required to provide a justification or otherwise we reject /// it. However, if the announcement is for a block below the tip the announcement is accepted /// as it probably comes from a node that is currently syncing the chain. -pub struct BlockAnnounceValidator { +pub struct BlockAnnounceValidator { phantom: PhantomData, - relay_chain_client: Arc, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, wait_on_relay_chain_block: WaitOnRelayChainBlock, } -impl BlockAnnounceValidator { +impl BlockAnnounceValidator { /// Create a new [`BlockAnnounceValidator`]. pub fn new( - relay_chain_client: Arc, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, - relay_chain_blockchain_events: Arc, + relay_chain_blockchain_events: RCInterface, ) -> Self { Self { phantom: Default::default(), - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend: relay_chain_backend.clone(), @@ -263,20 +262,18 @@ impl BlockAnnounceValidator { } } -impl BlockAnnounceValidator +impl BlockAnnounceValidator where - R: ProvideRuntimeApi + Send + Sync + 'static, - R::Api: ParachainHost, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, B: Backend + 'static, { /// Get the included block of the given parachain in the relay chain. fn included_block( - relay_chain_client: &R, + relay_chain_interface: &RCInterface, block_id: &BlockId, para_id: ParaId, ) -> Result { - let validation_data = relay_chain_client - .runtime_api() + let validation_data = relay_chain_interface .persisted_validation_data(block_id, para_id, OccupiedCoreAssumption::TimedOut) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)? .ok_or_else(|| { @@ -294,12 +291,11 @@ where /// Get the backed block hash of the given parachain in the relay chain. fn backed_block_hash( - relay_chain_client: &R, + relay_chain_interface: &RCInterface, block_id: &BlockId, para_id: ParaId, ) -> Result, BoxedError> { - let candidate_receipt = relay_chain_client - .runtime_api() + let candidate_receipt = relay_chain_interface .candidate_pending_availability(block_id, para_id) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)?; @@ -311,7 +307,7 @@ where &self, header: Block::Header, ) -> impl Future> { - let relay_chain_client = self.relay_chain_client.clone(); + let relay_chain_interface = self.relay_chain_interface.clone(); let relay_chain_backend = self.relay_chain_backend.clone(); let para_id = self.para_id; @@ -322,10 +318,10 @@ where let block_number = header.number(); let best_head = - Self::included_block(&*relay_chain_client, &runtime_api_block_id, para_id)?; + Self::included_block(&relay_chain_interface, &runtime_api_block_id, para_id)?; let known_best_number = best_head.number(); let backed_block = - || Self::backed_block_hash(&*relay_chain_client, &runtime_api_block_id, para_id); + || Self::backed_block_hash(&relay_chain_interface, &runtime_api_block_id, para_id); if best_head == header { tracing::debug!(target: LOG_TARGET, "Announced block matches best block.",); @@ -349,11 +345,10 @@ where } } -impl BlockAnnounceValidatorT - for BlockAnnounceValidator +impl BlockAnnounceValidatorT + for BlockAnnounceValidator where - P: ProvideRuntimeApi + Send + Sync + 'static, - P::Api: ParachainHost, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, B: Backend + 'static, BCE: BlockchainEvents + 'static + Send + Sync, { @@ -382,7 +377,7 @@ where .boxed(), }; - let relay_chain_client = self.relay_chain_client.clone(); + let relay_chain_client = self.relay_chain_interface.clone(); let header_encoded = header.encode(); let wait_on_relay_chain_block = self.wait_on_relay_chain_block.clone(); @@ -409,17 +404,18 @@ where /// Build a block announce validator instance. /// /// Returns a boxed [`BlockAnnounceValidator`]. -pub fn build_block_announce_validator( - relay_chain_client: polkadot_client::Client, +pub fn build_block_announce_validator( + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Box + Send> where B: Backend + Send + 'static, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, { BlockAnnounceValidatorBuilder::new( - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -433,27 +429,28 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct BlockAnnounceValidatorBuilder { +struct BlockAnnounceValidatorBuilder { phantom: PhantomData, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, } -impl BlockAnnounceValidatorBuilder +impl BlockAnnounceValidatorBuilder where B: Backend + Send + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. fn new( - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Self { Self { - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -463,36 +460,43 @@ where /// Build the block announce validator. fn build(self) -> Box + Send> { - self.relay_chain_client.clone().execute_with(self) - } -} - -impl polkadot_client::ExecuteWithClient - for BlockAnnounceValidatorBuilder -where - B: Backend + Send + 'static, -{ - type Output = Box + Send>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: - sp_api::StateBackend, - PBackend: Backend, - PBackend::State: sp_api::StateBackend, - Api: polkadot_client::RuntimeApiCollection, - PClient: polkadot_client::AbstractClient + 'static, - { Box::new(BlockAnnounceValidator::new( - client.clone(), + self.relay_chain_interface, self.para_id, self.relay_chain_sync_oracle, self.relay_chain_backend, - client, + self.relay_chain_interface.clone(), )) } } +// impl polkadot_client::ExecuteWithClient +// for BlockAnnounceValidatorBuilder +// where +// B: Backend + Send + 'static, +// RCInterface: RelayChainInterface + Send + 'static, +// { +// type Output = Box + Send>; +// +// fn execute_with_client(self, client: Arc) -> Self::Output +// where +// >::StateBackend: +// sp_api::StateBackend, +// PBackend: Backend, +// PBackend::State: sp_api::StateBackend, +// Api: polkadot_client::RuntimeApiCollection, +// PClient: polkadot_client::AbstractClient + 'static, +// { +// Box::new(BlockAnnounceValidator::new( +// self.relay_chain_interface, +// self.para_id, +// self.relay_chain_sync_oracle, +// self.relay_chain_backend, +// self.relay_chain_interface.clone(), +// )) +// } +// } + /// Wait before announcing a block that a candidate message has been received for this block, then /// add this message as justification for the block announcement. /// diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 4854e5c1221..8e37a83ebaa 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -12,5 +12,7 @@ cumulus-primitives-core = { path = "../../primitives/core" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index b2bbbc39b63..dc1f7b2694e 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -1,10 +1,17 @@ use cumulus_primitives_core::{ - relay_chain::{v1::ParachainHost, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage}, - InboundDownwardMessage, ParaId, + relay_chain::{ + v1::{CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost}, + BlakeTwo256, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, + }, + InboundDownwardMessage, ParaId, PersistedValidationData, }; -use polkadot_client::{ClientHandle, ExecuteWithClient}; -use sp_api::ProvideRuntimeApi; +use polkadot_client::{AbstractClient, ClientHandle, ExecuteWithClient, RuntimeApiCollection}; +use sc_client_api::{ + BlockchainEvents, FinalityNotifications, ImportNotifications, StorageEventStream, StorageKey, +}; +use sp_api::{ApiError, ApiExt, ProvideRuntimeApi}; use sp_core::sp_std::{collections::btree_map::BTreeMap, sync::Arc}; +use std::marker::PhantomData; const LOG_TARGET: &str = "cumulus-collator"; @@ -28,6 +35,19 @@ pub trait RelayChainInterface { para_id: ParaId, relay_parent: PHash, ) -> Option>>; + + fn persisted_validation_data( + &self, + block_id: &BlockId, + para_id: ParaId, + _: OccupiedCoreAssumption, + ) -> Result, ApiError>; + + fn candidate_pending_availability( + &self, + block_id: &BlockId, + para_id: ParaId, + ) -> Result, ApiError>; } #[derive(Clone)] @@ -101,6 +121,46 @@ impl ExecuteWithClient for InboundHrmpMessageWithClient { .ok() } } +struct CandidatePendingAvailabilityWithClient { + block_id: BlockId, + para_id: ParaId, +} + +impl ExecuteWithClient for CandidatePendingAvailabilityWithClient { + type Output = Result, ApiError>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + client + .runtime_api() + .candidate_pending_availability(&self.block_id, self.para_id) + } +} + +struct PersistedValidationDataWithClient { + block_id: BlockId, + para_id: ParaId, + occupied_core_assumption: OccupiedCoreAssumption, +} + +impl ExecuteWithClient for PersistedValidationDataWithClient { + type Output = Result, ApiError>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, + { + client.runtime_api().persisted_validation_data( + &self.block_id, + self.para_id, + self.occupied_core_assumption, + ) + } +} impl RelayChainInterface for RelayChainDirect { fn retrieve_dmq_contents( @@ -120,4 +180,28 @@ impl RelayChainInterface for RelayChainDirect { self.polkadot_client .execute_with(InboundHrmpMessageWithClient { para_id, relay_parent }) } + + fn persisted_validation_data( + &self, + block_id: &BlockId, + para_id: ParaId, + occupied_core_assumption: OccupiedCoreAssumption, + ) -> Result, ApiError> { + self.polkadot_client.execute_with(PersistedValidationDataWithClient { + block_id: block_id.clone(), + para_id, + occupied_core_assumption, + }) + } + + fn candidate_pending_availability( + &self, + block_id: &BlockId, + para_id: ParaId, + ) -> Result, ApiError> { + self.polkadot_client.execute_with(CandidatePendingAvailabilityWithClient { + block_id: block_id.clone(), + para_id, + }) + } } From 91c6a5c661ebe49d7c850a896ceadfbd31f71c9b Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 22 Oct 2021 15:32:13 +0200 Subject: [PATCH 08/56] revert network changes --- client/network/Cargo.toml | 3 +- client/network/src/lib.rs | 113 ++++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 61 deletions(-) diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 195dc1188a3..ecdf4c9704a 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -17,6 +17,7 @@ sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "mas # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } # other deps @@ -27,8 +28,6 @@ tracing = "0.1.22" parking_lot = "0.10.2" derive_more = "0.99.2" -cumulus-relay-chain-interface = {path = "../relay-chain-interface"} - [dev-dependencies] tokio = { version = "1.10", features = ["macros"] } diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index be6a467f997..e183fcd461c 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -33,7 +33,7 @@ use sp_runtime::{ traits::{Block as BlockT, Header as HeaderT}, }; -use cumulus_relay_chain_interface::RelayChainInterface; +use polkadot_client::ClientHandle; use polkadot_node_primitives::{CollationSecondedSignal, Statement}; use polkadot_parachain::primitives::HeadData; use polkadot_primitives::v1::{ @@ -230,27 +230,27 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// chain. If it is at the tip, it is required to provide a justification or otherwise we reject /// it. However, if the announcement is for a block below the tip the announcement is accepted /// as it probably comes from a node that is currently syncing the chain. -pub struct BlockAnnounceValidator { +pub struct BlockAnnounceValidator { phantom: PhantomData, - relay_chain_interface: RCInterface, + relay_chain_client: Arc, relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, wait_on_relay_chain_block: WaitOnRelayChainBlock, } -impl BlockAnnounceValidator { +impl BlockAnnounceValidator { /// Create a new [`BlockAnnounceValidator`]. pub fn new( - relay_chain_interface: RCInterface, + relay_chain_client: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, - relay_chain_blockchain_events: RCInterface, + relay_chain_blockchain_events: Arc, ) -> Self { Self { phantom: Default::default(), - relay_chain_interface, + relay_chain_client, para_id, relay_chain_sync_oracle, relay_chain_backend: relay_chain_backend.clone(), @@ -262,18 +262,20 @@ impl BlockAnnounceValidator BlockAnnounceValidator +impl BlockAnnounceValidator where - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, + R: ProvideRuntimeApi + Send + Sync + 'static, + R::Api: ParachainHost, B: Backend + 'static, { /// Get the included block of the given parachain in the relay chain. fn included_block( - relay_chain_interface: &RCInterface, + relay_chain_client: &R, block_id: &BlockId, para_id: ParaId, ) -> Result { - let validation_data = relay_chain_interface + let validation_data = relay_chain_client + .runtime_api() .persisted_validation_data(block_id, para_id, OccupiedCoreAssumption::TimedOut) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)? .ok_or_else(|| { @@ -291,11 +293,12 @@ where /// Get the backed block hash of the given parachain in the relay chain. fn backed_block_hash( - relay_chain_interface: &RCInterface, + relay_chain_client: &R, block_id: &BlockId, para_id: ParaId, ) -> Result, BoxedError> { - let candidate_receipt = relay_chain_interface + let candidate_receipt = relay_chain_client + .runtime_api() .candidate_pending_availability(block_id, para_id) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)?; @@ -307,7 +310,7 @@ where &self, header: Block::Header, ) -> impl Future> { - let relay_chain_interface = self.relay_chain_interface.clone(); + let relay_chain_client = self.relay_chain_client.clone(); let relay_chain_backend = self.relay_chain_backend.clone(); let para_id = self.para_id; @@ -318,10 +321,10 @@ where let block_number = header.number(); let best_head = - Self::included_block(&relay_chain_interface, &runtime_api_block_id, para_id)?; + Self::included_block(&*relay_chain_client, &runtime_api_block_id, para_id)?; let known_best_number = best_head.number(); let backed_block = - || Self::backed_block_hash(&relay_chain_interface, &runtime_api_block_id, para_id); + || Self::backed_block_hash(&*relay_chain_client, &runtime_api_block_id, para_id); if best_head == header { tracing::debug!(target: LOG_TARGET, "Announced block matches best block.",); @@ -345,10 +348,11 @@ where } } -impl BlockAnnounceValidatorT - for BlockAnnounceValidator +impl BlockAnnounceValidatorT + for BlockAnnounceValidator where - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, + P: ProvideRuntimeApi + Send + Sync + 'static, + P::Api: ParachainHost, B: Backend + 'static, BCE: BlockchainEvents + 'static + Send + Sync, { @@ -377,7 +381,7 @@ where .boxed(), }; - let relay_chain_client = self.relay_chain_interface.clone(); + let relay_chain_client = self.relay_chain_client.clone(); let header_encoded = header.encode(); let wait_on_relay_chain_block = self.wait_on_relay_chain_block.clone(); @@ -404,18 +408,17 @@ where /// Build a block announce validator instance. /// /// Returns a boxed [`BlockAnnounceValidator`]. -pub fn build_block_announce_validator( - relay_chain_interface: RCInterface, +pub fn build_block_announce_validator( + relay_chain_client: polkadot_client::Client, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Box + Send> where B: Backend + Send + 'static, - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, { BlockAnnounceValidatorBuilder::new( - relay_chain_interface, + relay_chain_client, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -429,28 +432,27 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct BlockAnnounceValidatorBuilder { +struct BlockAnnounceValidatorBuilder { phantom: PhantomData, - relay_chain_interface: RCInterface, + relay_chain_client: polkadot_client::Client, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, } -impl BlockAnnounceValidatorBuilder +impl BlockAnnounceValidatorBuilder where B: Backend + Send + 'static, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. fn new( - relay_chain_interface: RCInterface, + relay_chain_client: polkadot_client::Client, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Self { Self { - relay_chain_interface, + relay_chain_client, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -460,43 +462,36 @@ where /// Build the block announce validator. fn build(self) -> Box + Send> { + self.relay_chain_client.clone().execute_with(self) + } +} + +impl polkadot_client::ExecuteWithClient + for BlockAnnounceValidatorBuilder +where + B: Backend + Send + 'static, +{ + type Output = Box + Send>; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + >::StateBackend: + sp_api::StateBackend, + PBackend: Backend, + PBackend::State: sp_api::StateBackend, + Api: polkadot_client::RuntimeApiCollection, + PClient: polkadot_client::AbstractClient + 'static, + { Box::new(BlockAnnounceValidator::new( - self.relay_chain_interface, + client.clone(), self.para_id, self.relay_chain_sync_oracle, self.relay_chain_backend, - self.relay_chain_interface.clone(), + client, )) } } -// impl polkadot_client::ExecuteWithClient -// for BlockAnnounceValidatorBuilder -// where -// B: Backend + Send + 'static, -// RCInterface: RelayChainInterface + Send + 'static, -// { -// type Output = Box + Send>; -// -// fn execute_with_client(self, client: Arc) -> Self::Output -// where -// >::StateBackend: -// sp_api::StateBackend, -// PBackend: Backend, -// PBackend::State: sp_api::StateBackend, -// Api: polkadot_client::RuntimeApiCollection, -// PClient: polkadot_client::AbstractClient + 'static, -// { -// Box::new(BlockAnnounceValidator::new( -// self.relay_chain_interface, -// self.para_id, -// self.relay_chain_sync_oracle, -// self.relay_chain_backend, -// self.relay_chain_interface.clone(), -// )) -// } -// } - /// Wait before announcing a block that a candidate message has been received for this block, then /// add this message as justification for the block announcement. /// From ea57405b40a9c401481596d2c4915f5406ae0ae3 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 15 Nov 2021 14:16:26 +0100 Subject: [PATCH 09/56] Implement relaychaininterface for Arc --- Cargo.lock | 13 +- client/collator/src/lib.rs | 9 +- client/relay-chain-interface/src/lib.rs | 174 +++++++++++------------- parachain-template/node/src/service.rs | 10 +- polkadot-parachains/src/service.rs | 34 ++--- test/service/src/lib.rs | 19 ++- 6 files changed, 123 insertions(+), 136 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b082b80ab9..1e39bbee9d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1434,7 +1434,6 @@ dependencies = [ "cumulus-relay-chain-interface", "futures 0.3.17", "parity-scale-codec", - "polkadot-client", "sc-client-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sc-consensus 0.10.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sc-consensus-aura", @@ -1486,7 +1485,6 @@ dependencies = [ "cumulus-relay-chain-interface", "futures 0.3.17", "parking_lot 0.10.2", - "polkadot-client", "sc-client-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sc-consensus 0.10.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sp-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", @@ -1505,13 +1503,13 @@ name = "cumulus-client-network" version = "0.1.0" dependencies = [ "cumulus-primitives-core", - "cumulus-relay-chain-interface", "cumulus-test-service", "derive_more", "futures 0.3.17", "futures-timer 3.0.2", "parity-scale-codec", "parking_lot 0.10.2", + "polkadot-client", "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", @@ -1759,7 +1757,6 @@ dependencies = [ "cumulus-relay-chain-interface", "cumulus-test-relay-sproof-builder", "parity-scale-codec", - "polkadot-client", "sc-client-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "scale-info", "sp-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", @@ -1811,10 +1808,10 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "polkadot-client", - "sc-client-api", - "sp-api", - "sp-core", - "sp-runtime", + "sc-client-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", + "sp-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", + "sp-core 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", + "sp-runtime 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "tracing", ] diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index 70b5f02de4a..75a0eeb859d 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -18,10 +18,9 @@ use cumulus_client_network::WaitToAnnounce; use cumulus_primitives_core::{ - relay_chain::{v1::ParachainHost, Block as PBlock, Hash as PHash}, - CollectCollationInfo, InboundDownwardMessage, ParachainBlockData, PersistedValidationData, + relay_chain::{Hash as PHash}, + ParachainBlockData, PersistedValidationData, }; -use cumulus_relay_chain_interface::RelayChainInterface; use sc_client_api::BlockBackend; use sp_api::ProvideRuntimeApi; @@ -41,11 +40,9 @@ use polkadot_overseer::Handle as OverseerHandle; use polkadot_primitives::v1::{CollatorPair, HeadData, Id as ParaId}; use codec::{Decode, Encode}; -use cumulus_primitives_core::relay_chain::InboundHrmpMessage; +use cumulus_primitives_core::CollectCollationInfo; use futures::{channel::oneshot, FutureExt}; use parking_lot::Mutex; -use polkadot_client::{ClientHandle, ExecuteWithClient}; -use sp_core::sp_std::collections::btree_map::BTreeMap; use std::sync::Arc; use tracing::Instrument; diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index dc1f7b2694e..530cb10081e 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -1,17 +1,15 @@ +use std::sync::Arc; + use cumulus_primitives_core::{ relay_chain::{ v1::{CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost}, - BlakeTwo256, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, + Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use polkadot_client::{AbstractClient, ClientHandle, ExecuteWithClient, RuntimeApiCollection}; -use sc_client_api::{ - BlockchainEvents, FinalityNotifications, ImportNotifications, StorageEventStream, StorageKey, -}; -use sp_api::{ApiError, ApiExt, ProvideRuntimeApi}; -use sp_core::sp_std::{collections::btree_map::BTreeMap, sync::Arc}; -use std::marker::PhantomData; +use sp_core::sp_std::collections::btree_map::BTreeMap; +use polkadot_client::{ClientHandle, ExecuteWithClient}; +use sp_api::{ApiError, ProvideRuntimeApi}; const LOG_TARGET: &str = "cumulus-collator"; @@ -50,158 +48,144 @@ pub trait RelayChainInterface { ) -> Result, ApiError>; } -#[derive(Clone)] -pub struct RelayChainDirect { - pub polkadot_client: T, +pub struct RelayChainDirect { + pub polkadot_client: Arc, } -/// Special structure to run [`ParachainInherentData::create_at`] with a [`Client`]. -struct DmqContentsWithClient { - relay_parent: PHash, - para_id: ParaId, -} -impl ExecuteWithClient for DmqContentsWithClient { - type Output = Option>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - let my_client = &*client; - my_client +impl RelayChainInterface for RelayChainDirect +where + Client: ProvideRuntimeApi, + Client::Api: ParachainHost, +{ + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + self.polkadot_client .runtime_api() .dmq_contents_with_context( - &BlockId::hash(self.relay_parent), + &BlockId::hash(relay_parent), sp_core::ExecutionContext::Importing, - self.para_id, + para_id, ) .map_err(|e| { tracing::error!( target: LOG_TARGET, - relay_parent = ?self.relay_parent, + relay_parent = ?relay_parent, error = ?e, "An error occured during requesting the downward messages.", ); }) .ok() } -} - -struct InboundHrmpMessageWithClient { - relay_parent: PHash, - para_id: ParaId, -} -impl ExecuteWithClient for InboundHrmpMessageWithClient { - type Output = Option>>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - let my_client = &*client; - my_client + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + self.polkadot_client .runtime_api() .inbound_hrmp_channels_contents_with_context( - &BlockId::hash(self.relay_parent), + &BlockId::hash(relay_parent), sp_core::ExecutionContext::Importing, - self.para_id, + para_id, ) .map_err(|e| { tracing::error!( target: LOG_TARGET, - relay_parent = ?self.relay_parent, + relay_parent = ?relay_parent, error = ?e, "An error occured during requesting the inbound HRMP messages.", ); }) .ok() } -} -struct CandidatePendingAvailabilityWithClient { - block_id: BlockId, - para_id: ParaId, -} -impl ExecuteWithClient for CandidatePendingAvailabilityWithClient { - type Output = Result, ApiError>; + fn persisted_validation_data( + &self, + block_id: &BlockId, + para_id: ParaId, + occupied_core_assumption: OccupiedCoreAssumption, + ) -> Result, ApiError> { + self.polkadot_client.runtime_api().persisted_validation_data( + block_id, + para_id, + occupied_core_assumption, + ) + } - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi, - Client::Api: ParachainHost, - { - client + fn candidate_pending_availability( + &self, + block_id: &BlockId, + para_id: ParaId, + ) -> Result, ApiError> { + self.polkadot_client .runtime_api() - .candidate_pending_availability(&self.block_id, self.para_id) + .candidate_pending_availability(block_id, para_id) } } -struct PersistedValidationDataWithClient { - block_id: BlockId, - para_id: ParaId, - occupied_core_assumption: OccupiedCoreAssumption, +pub struct RelayChainDirectBuilder { + polkadot_client: polkadot_client::Client, +} + +impl RelayChainDirectBuilder { + pub fn build(self) -> Arc { + self.polkadot_client.clone().execute_with(self) + } } -impl ExecuteWithClient for PersistedValidationDataWithClient { - type Output = Result, ApiError>; +impl ExecuteWithClient for RelayChainDirectBuilder { + type Output = Arc; fn execute_with_client(self, client: Arc) -> Self::Output where - Client: ProvideRuntimeApi, + Client: ProvideRuntimeApi + 'static + Sync + Send, Client::Api: ParachainHost, { - client.runtime_api().persisted_validation_data( - &self.block_id, - self.para_id, - self.occupied_core_assumption, - ) + Arc::new(RelayChainDirect { polkadot_client: client }) } } -impl RelayChainInterface for RelayChainDirect { - fn retrieve_dmq_contents( +impl RelayChainInterface for Arc { + fn retrieve_dmq_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option> { - self.polkadot_client - .execute_with(DmqContentsWithClient { para_id, relay_parent }) - } + (**self).retrieve_dmq_contents(para_id, relay_parent) + } - fn retrieve_all_inbound_hrmp_channel_contents( + fn retrieve_all_inbound_hrmp_channel_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option>> { - self.polkadot_client - .execute_with(InboundHrmpMessageWithClient { para_id, relay_parent }) - } + (**self).retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent) + } - fn persisted_validation_data( + fn persisted_validation_data( &self, block_id: &BlockId, para_id: ParaId, occupied_core_assumption: OccupiedCoreAssumption, ) -> Result, ApiError> { - self.polkadot_client.execute_with(PersistedValidationDataWithClient { - block_id: block_id.clone(), - para_id, - occupied_core_assumption, - }) - } + (**self).persisted_validation_data(block_id, para_id, occupied_core_assumption) + } - fn candidate_pending_availability( + fn candidate_pending_availability( &self, block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError> { - self.polkadot_client.execute_with(CandidatePendingAvailabilityWithClient { - block_id: block_id.clone(), - para_id, - }) + (**self).candidate_pending_availability(block_id, para_id) } } + +pub fn build_relay_chain_direct(client: polkadot_client::Client) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { + let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client }; + relay_chain_builder.build() +} diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index d5aec1d02e4..30d7bd553b5 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,7 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; // Substrate Imports -use cumulus_relay_chain_interface::RelayChainDirect; +use cumulus_relay_chain_interface::{RelayChainDirect, RelayChainDirectBuilder, build_relay_chain_direct}; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -432,8 +432,8 @@ pub async fn start_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = - RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; + let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -475,9 +475,7 @@ pub async fn start_parachain_node( } }, block_import: client.clone(), - relay_chain_interface: RelayChainDirect { - polkadot_client: relay_chain_node.client.clone(), - }, + relay_chain_interface: relay_chain_interface2, relay_chain_backend: relay_chain_node.backend.clone(), para_client: client, backoff_authoring_blocks: Option::<()>::None, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 9c7414dc242..08cf1c29278 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -28,7 +28,7 @@ use cumulus_primitives_core::{ relay_chain::v1::{Hash as PHash, PersistedValidationData}, ParaId, }; -use cumulus_relay_chain_interface::RelayChainDirect; +use cumulus_relay_chain_interface::build_relay_chain_direct; use polkadot_service::NativeExecutionDispatch; use crate::rpc; @@ -116,7 +116,9 @@ impl sc_executor::NativeExecutionDispatch for StatemineRuntimeExecutor { } } -/// Native Westmint executor instance. +/** +Native Westmint executor instance. +*/ pub struct WestmintRuntimeExecutor; impl sc_executor::NativeExecutionDispatch for WestmintRuntimeExecutor { @@ -706,7 +708,8 @@ pub async fn start_rococo_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_direct = RelayChainDirect{ polkadot_client: relay_chain_node.client.clone() }; + let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -749,7 +752,7 @@ pub async fn start_rococo_parachain_node( } }, block_import: client.clone(), - relay_chain_interface: RelayChainDirect{polkadot_client: relay_chain_node.client.clone()} , + relay_chain_interface: relay_chain_direct_for_aura_consensus, relay_chain_backend: relay_chain_node.backend.clone(), para_client: client.clone(), backoff_authoring_blocks: Option::<()>::None, @@ -828,17 +831,14 @@ pub async fn start_shell_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = - RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; + let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { para_id: id, proposer_factory, block_import: client.clone(), - relay_chain_interface: RelayChainDirect { - polkadot_client: relay_chain_node.client.clone(), - }, + relay_chain_interface: relay_chain_interface.clone(), relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = @@ -1113,9 +1113,8 @@ where ); let relay_chain_backend2 = relay_chain_backend.clone(); - let relay_chain_client2 = relay_chain_client.clone(); - let relay_chain_interface = - RelayChainDirect { polkadot_client: relay_chain_client.clone() }; + let relay_chain_interface = build_relay_chain_direct(relay_chain_client.clone()); + let relay_chain_interface2 = relay_chain_interface.clone(); build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -1158,9 +1157,7 @@ where } }, block_import: client2.clone(), - relay_chain_interface: RelayChainDirect { - polkadot_client: relay_chain_client2.clone(), - }, + relay_chain_interface: relay_chain_interface2, relay_chain_backend: relay_chain_backend2, para_client: client2.clone(), backoff_authoring_blocks: Option::<()>::None, @@ -1185,8 +1182,7 @@ where ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = - RelayChainDirect { polkadot_client: relay_chain_node.client.clone() }; + let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); let relay_chain_consensus = cumulus_client_consensus_relay_chain::build_relay_chain_consensus( @@ -1194,9 +1190,7 @@ where para_id: id, proposer_factory, block_import: client.clone(), - relay_chain_interface: RelayChainDirect { - polkadot_client: relay_chain_node.client.clone(), - }, + relay_chain_interface: relay_chain_interface.clone(), relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 2a20875cebc..c14bdef5b8c 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -22,7 +22,7 @@ mod chain_spec; mod genesis; use core::future::Future; -use cumulus_client_collator::RelayChainInterface; +use cumulus_relay_chain_interface::RelayChainInterface; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ @@ -179,6 +179,23 @@ impl RelayChainInterface for RelayChainFullClient { ) -> Option>> { todo!() } + +fn persisted_validation_data( + &self, + block_id: &polkadot_service::BlockId, + para_id: ParaId, + _: polkadot_primitives::v1::OccupiedCoreAssumption, + ) -> Result, ApiError> { + todo!() + } + +fn candidate_pending_availability( + &self, + block_id: &polkadot_service::BlockId, + para_id: ParaId, + ) -> Result, ApiError> { + todo!() + } } /// Start a node with the given parachain `Configuration` and relay chain `Configuration`. From d7a7ffb2eaac656224af83348d1b4fea450ba7a3 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 17 Nov 2021 12:27:47 +0100 Subject: [PATCH 10/56] Remove unused code. Clean up imports. Remove polkadot-client fromcollator crate. --- .rustfmt.toml | 1 + Cargo.lock | 2 +- client/collator/Cargo.toml | 2 +- client/relay-chain-interface/src/lib.rs | 38 +++++++++++++++++ parachain-template/node/src/service.rs | 2 +- test/service/Cargo.toml | 1 + test/service/src/lib.rs | 54 ++++--------------------- 7 files changed, 51 insertions(+), 49 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index 3018614bb16..e81dc030e50 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -21,3 +21,4 @@ spaces_around_ranges = false trailing_comma = "Vertical" trailing_semicolon = false use_field_init_shorthand = true +edition = 2018 diff --git a/Cargo.lock b/Cargo.lock index 1e39bbee9d8..73b554ee3d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1407,7 +1407,6 @@ dependencies = [ "futures 0.3.17", "parity-scale-codec", "parking_lot 0.10.2", - "polkadot-client", "polkadot-node-primitives", "polkadot-node-subsystem", "polkadot-node-subsystem-test-helpers", @@ -1932,6 +1931,7 @@ dependencies = [ "sc-tracing 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sc-transaction-pool 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "serde", + "sp-api 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sp-arithmetic 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sp-blockchain 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", "sp-core 4.0.0-dev (git+https://github.com/paritytech/substrate?branch=master)", diff --git a/client/collator/Cargo.toml b/client/collator/Cargo.toml index 35bcd6848f6..e74647847c6 100644 --- a/client/collator/Cargo.toml +++ b/client/collator/Cargo.toml @@ -17,7 +17,7 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-subsystem = { git = "https://github.com/paritytech/polkadot", branch = "master" } -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +# polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } # Cumulus dependencies cumulus-client-network = { path = "../network" } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 530cb10081e..4c5dffaef4a 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -185,6 +185,44 @@ impl RelayChainInterface for Arc { } } +impl RelayChainInterface for Arc> + where + Client: ProvideRuntimeApi + 'static + Sync + Send, + Client::Api: ParachainHost, { + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + (**self).retrieve_dmq_contents(para_id, relay_parent) + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + (**self).retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent) + } + + fn persisted_validation_data( + &self, + block_id: &BlockId, + para_id: ParaId, + occupied_core_assumption: OccupiedCoreAssumption, + ) -> Result, ApiError> { + (**self).persisted_validation_data(block_id, para_id, occupied_core_assumption) + } + + fn candidate_pending_availability( + &self, + block_id: &BlockId, + para_id: ParaId, + ) -> Result, ApiError> { + (**self).candidate_pending_availability(block_id, para_id) + } +} + pub fn build_relay_chain_direct(client: polkadot_client::Client) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client }; relay_chain_builder.build() diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 30d7bd553b5..d216597a247 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,7 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; // Substrate Imports -use cumulus_relay_chain_interface::{RelayChainDirect, RelayChainDirectBuilder, build_relay_chain_direct}; +use cumulus_relay_chain_interface::build_relay_chain_direct; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index f1bb1c02e35..877273fdd0c 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -27,6 +27,7 @@ sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index c14bdef5b8c..2481749da20 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -22,13 +22,13 @@ mod chain_spec; mod genesis; use core::future::Future; -use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; -use cumulus_primitives_core::{InboundDownwardMessage, ParaId}; +use cumulus_primitives_core::ParaId; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use sc_client_api::execution_extensions::ExecutionStrategies; @@ -48,13 +48,13 @@ use sp_keyring::Sr25519Keyring; use sp_runtime::{codec::Encode, generic, traits::BlakeTwo256}; use sp_state_machine::BasicExternalities; use sp_trie::PrefixedMemoryDB; -use std::{collections::BTreeMap, sync::Arc}; +use std::sync::Arc; use substrate_test_client::{ BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput, }; + pub use chain_spec::*; -use cumulus_primitives_core::relay_chain::InboundHrmpMessage; pub use cumulus_test_runtime as runtime; pub use genesis::*; pub use sp_keyring::Sr25519Keyring as Keyring; @@ -159,45 +159,6 @@ pub fn new_partial( Ok(params) } -struct RelayChainFullClient { - full_client: Arc, -} - -impl RelayChainInterface for RelayChainFullClient { - fn retrieve_dmq_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option> { - todo!() - } - - fn retrieve_all_inbound_hrmp_channel_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>> { - todo!() - } - -fn persisted_validation_data( - &self, - block_id: &polkadot_service::BlockId, - para_id: ParaId, - _: polkadot_primitives::v1::OccupiedCoreAssumption, - ) -> Result, ApiError> { - todo!() - } - -fn candidate_pending_availability( - &self, - block_id: &polkadot_service::BlockId, - para_id: ParaId, - ) -> Result, ApiError> { - todo!() - } -} - /// Start a node with the given parachain `Configuration` and relay chain `Configuration`. /// /// This is the actual implementation that is abstract over the executor and the runtime api. @@ -311,11 +272,12 @@ where prometheus_registry.as_ref(), None, ); - let relay_chain_client = relay_chain_full_node.client.clone(); let relay_chain_backend = relay_chain_full_node.backend.clone(); + let relay_chain_interface = - RelayChainFullClient { full_client: relay_chain_client }; + Arc::new(RelayChainDirect {polkadot_client: relay_chain_full_node.client.clone() }); + let relay_chain_interface2 = relay_chain_interface.clone(); Box::new(cumulus_client_consensus_relay_chain::RelayChainConsensus::new( para_id, proposer_factory, @@ -341,7 +303,7 @@ where } }, client.clone(), - relay_chain_full_node.client.clone(), + relay_chain_interface2, relay_chain_full_node.backend.clone(), )) }, From 1c14ea50027ac40b9d4a00880eacb66f85346158 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 17 Nov 2021 15:39:13 +0100 Subject: [PATCH 11/56] Extract cumulu-client-network usages of polkadot-client --- Cargo.lock | 1 + client/network/Cargo.toml | 2 + client/network/src/lib.rs | 117 ++++++++++++------------ client/relay-chain-interface/src/lib.rs | 78 +++++++++++----- 4 files changed, 116 insertions(+), 82 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73b554ee3d5..d1d00d32eda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1502,6 +1502,7 @@ name = "cumulus-client-network" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-service", "derive_more", "futures 0.3.17", diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index ecdf4c9704a..6c797ef64b5 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -20,6 +20,8 @@ polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", bra polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } +cumulus-relay-chain-interface = { path = "../relay-chain-interface" } + # other deps codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } futures = { version = "0.3.1", features = ["compat"] } diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index e183fcd461c..03e74b1985b 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -21,7 +21,6 @@ //! and [`WaitToAnnounce`] for more information about this implementation. use sc_client_api::{Backend, BlockchainEvents}; -use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_consensus::{ block_validation::{BlockAnnounceValidator as BlockAnnounceValidatorT, Validation}, @@ -33,12 +32,12 @@ use sp_runtime::{ traits::{Block as BlockT, Header as HeaderT}, }; -use polkadot_client::ClientHandle; +use cumulus_relay_chain_interface::RelayChainInterface; use polkadot_node_primitives::{CollationSecondedSignal, Statement}; use polkadot_parachain::primitives::HeadData; use polkadot_primitives::v1::{ Block as PBlock, CandidateReceipt, CompactStatement, Hash as PHash, Id as ParaId, - OccupiedCoreAssumption, ParachainHost, SigningContext, UncheckedSigned, + OccupiedCoreAssumption, SigningContext, UncheckedSigned, }; use codec::{Decode, DecodeAll, Encode}; @@ -135,19 +134,15 @@ impl BlockAnnounceData { /// Check the signature of the statement. /// /// Returns an `Err(_)` if it failed. - fn check_signature

( - self, - relay_chain_client: &Arc

, - ) -> Result + fn check_signature

(self, relay_chain_client: &P) -> Result where - P: ProvideRuntimeApi + Send + Sync + 'static, - P::Api: ParachainHost, + P: RelayChainInterface + Send + Sync + 'static, { - let runtime_api = relay_chain_client.runtime_api(); let validator_index = self.statement.unchecked_validator_index(); let runtime_api_block_id = BlockId::Hash(self.relay_parent); - let session_index = match runtime_api.session_index_for_child(&runtime_api_block_id) { + let session_index = match relay_chain_client.session_index_for_child(&runtime_api_block_id) + { Ok(r) => r, Err(e) => return Err(BlockAnnounceError(format!("{:?}", e))), }; @@ -155,7 +150,7 @@ impl BlockAnnounceData { let signing_context = SigningContext { parent_hash: self.relay_parent, session_index }; // Check that the signer is a legit validator. - let authorities = match runtime_api.validators(&runtime_api_block_id) { + let authorities = match relay_chain_client.validators(&runtime_api_block_id) { Ok(r) => r, Err(e) => return Err(BlockAnnounceError(format!("{:?}", e))), }; @@ -232,7 +227,7 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// as it probably comes from a node that is currently syncing the chain. pub struct BlockAnnounceValidator { phantom: PhantomData, - relay_chain_client: Arc, + relay_chain_interface: R, relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, @@ -242,7 +237,7 @@ pub struct BlockAnnounceValidator { impl BlockAnnounceValidator { /// Create a new [`BlockAnnounceValidator`]. pub fn new( - relay_chain_client: Arc, + relay_chain_interface: R, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, @@ -250,7 +245,7 @@ impl BlockAnnounceValidator { ) -> Self { Self { phantom: Default::default(), - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend: relay_chain_backend.clone(), @@ -264,18 +259,16 @@ impl BlockAnnounceValidator { impl BlockAnnounceValidator where - R: ProvideRuntimeApi + Send + Sync + 'static, - R::Api: ParachainHost, + R: RelayChainInterface + Clone, B: Backend + 'static, { /// Get the included block of the given parachain in the relay chain. fn included_block( - relay_chain_client: &R, + relay_chain_interface: &R, block_id: &BlockId, para_id: ParaId, ) -> Result { - let validation_data = relay_chain_client - .runtime_api() + let validation_data = relay_chain_interface .persisted_validation_data(block_id, para_id, OccupiedCoreAssumption::TimedOut) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)? .ok_or_else(|| { @@ -293,12 +286,11 @@ where /// Get the backed block hash of the given parachain in the relay chain. fn backed_block_hash( - relay_chain_client: &R, + relay_chain_interface: &R, block_id: &BlockId, para_id: ParaId, ) -> Result, BoxedError> { - let candidate_receipt = relay_chain_client - .runtime_api() + let candidate_receipt = relay_chain_interface .candidate_pending_availability(block_id, para_id) .map_err(|e| Box::new(BlockAnnounceError(format!("{:?}", e))) as Box<_>)?; @@ -310,7 +302,7 @@ where &self, header: Block::Header, ) -> impl Future> { - let relay_chain_client = self.relay_chain_client.clone(); + let relay_chain_interface = self.relay_chain_interface.clone(); let relay_chain_backend = self.relay_chain_backend.clone(); let para_id = self.para_id; @@ -321,10 +313,10 @@ where let block_number = header.number(); let best_head = - Self::included_block(&*relay_chain_client, &runtime_api_block_id, para_id)?; + Self::included_block(&relay_chain_interface, &runtime_api_block_id, para_id)?; let known_best_number = best_head.number(); let backed_block = - || Self::backed_block_hash(&*relay_chain_client, &runtime_api_block_id, para_id); + || Self::backed_block_hash(&relay_chain_interface, &runtime_api_block_id, para_id); if best_head == header { tracing::debug!(target: LOG_TARGET, "Announced block matches best block.",); @@ -351,8 +343,7 @@ where impl BlockAnnounceValidatorT for BlockAnnounceValidator where - P: ProvideRuntimeApi + Send + Sync + 'static, - P::Api: ParachainHost, + P: RelayChainInterface + Clone + Send + Sync + 'static, B: Backend + 'static, BCE: BlockchainEvents + 'static + Send + Sync, { @@ -381,7 +372,7 @@ where .boxed(), }; - let relay_chain_client = self.relay_chain_client.clone(); + let relay_chain_interface = self.relay_chain_interface.clone(); let header_encoded = header.encode(); let wait_on_relay_chain_block = self.wait_on_relay_chain_block.clone(); @@ -398,7 +389,7 @@ where .map_err(|e| Box::new(BlockAnnounceError(e.to_string())) as Box<_>)?; block_announce_data - .check_signature(&relay_chain_client) + .check_signature(&relay_chain_interface) .map_err(|e| Box::new(e) as Box<_>) } .boxed() @@ -408,17 +399,18 @@ where /// Build a block announce validator instance. /// /// Returns a boxed [`BlockAnnounceValidator`]. -pub fn build_block_announce_validator( - relay_chain_client: polkadot_client::Client, +pub fn build_block_announce_validator( + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Box + Send> where B: Backend + Send + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync, { BlockAnnounceValidatorBuilder::new( - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -432,27 +424,28 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct BlockAnnounceValidatorBuilder { +struct BlockAnnounceValidatorBuilder { phantom: PhantomData, - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, } -impl BlockAnnounceValidatorBuilder +impl BlockAnnounceValidatorBuilder where B: Backend + Send + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync, { /// Create a new instance of the builder. fn new( - relay_chain_client: polkadot_client::Client, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, ) -> Self { Self { - relay_chain_client, + relay_chain_interface, para_id, relay_chain_sync_oracle, relay_chain_backend, @@ -462,28 +455,8 @@ where /// Build the block announce validator. fn build(self) -> Box + Send> { - self.relay_chain_client.clone().execute_with(self) - } -} - -impl polkadot_client::ExecuteWithClient - for BlockAnnounceValidatorBuilder -where - B: Backend + Send + 'static, -{ - type Output = Box + Send>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: - sp_api::StateBackend, - PBackend: Backend, - PBackend::State: sp_api::StateBackend, - Api: polkadot_client::RuntimeApiCollection, - PClient: polkadot_client::AbstractClient + 'static, - { Box::new(BlockAnnounceValidator::new( - client.clone(), + self.relay_chain_interface.clone(), self.para_id, self.relay_chain_sync_oracle, self.relay_chain_backend, @@ -492,6 +465,32 @@ where } } +// impl polkadot_client::ExecuteWithClient +// for BlockAnnounceValidatorBuilder +// where +// B: Backend + Send + 'static, +// { +// type Output = Box + Send>; + +// fn execute_with_client(self, client: Arc) -> Self::Output +// where +// >::StateBackend: +// sp_api::StateBackend, +// PBackend: Backend, +// PBackend::State: sp_api::StateBackend, +// Api: polkadot_client::RuntimeApiCollection, +// PClient: polkadot_client::AbstractClient + 'static, +// { +// Box::new(BlockAnnounceValidator::new( +// client.clone(), +// self.para_id, +// self.relay_chain_sync_oracle, +// self.relay_chain_backend, +// client, +// )) +// } +// } + /// Wait before announcing a block that a candidate message has been received for this block, then /// add this message as justification for the block announcement. /// diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 4c5dffaef4a..1bd752a5f01 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -2,18 +2,23 @@ use std::sync::Arc; use cumulus_primitives_core::{ relay_chain::{ - v1::{CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost}, + v1::{ + CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost, SessionIndex, + ValidatorId, + }, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use sp_core::sp_std::collections::btree_map::BTreeMap; use polkadot_client::{ClientHandle, ExecuteWithClient}; use sp_api::{ApiError, ProvideRuntimeApi}; +use sp_core::sp_std::collections::btree_map::BTreeMap; const LOG_TARGET: &str = "cumulus-collator"; pub trait RelayChainInterface { + fn validators(&self, block_id: &BlockId) -> Result, ApiError>; + /// Returns the whole contents of the downward message queue for the parachain we are collating /// for. /// @@ -46,14 +51,15 @@ pub trait RelayChainInterface { block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError>; + + fn session_index_for_child(&self, block_id: &BlockId) -> Result; } pub struct RelayChainDirect { pub polkadot_client: Arc, } - -impl RelayChainInterface for RelayChainDirect +impl RelayChainInterface for RelayChainDirect where Client: ProvideRuntimeApi, Client::Api: ParachainHost, @@ -126,6 +132,14 @@ where .runtime_api() .candidate_pending_availability(block_id, para_id) } + + fn session_index_for_child(&self, block_id: &BlockId) -> Result { + self.polkadot_client.runtime_api().session_index_for_child(block_id) + } + + fn validators(&self, block_id: &BlockId) -> Result, ApiError> { + self.polkadot_client.runtime_api().validators(block_id) + } } pub struct RelayChainDirectBuilder { @@ -151,79 +165,97 @@ impl ExecuteWithClient for RelayChainDirectBuilder { } impl RelayChainInterface for Arc { - fn retrieve_dmq_contents( + fn retrieve_dmq_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option> { (**self).retrieve_dmq_contents(para_id, relay_parent) - } + } - fn retrieve_all_inbound_hrmp_channel_contents( + fn retrieve_all_inbound_hrmp_channel_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option>> { (**self).retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent) - } + } - fn persisted_validation_data( + fn persisted_validation_data( &self, block_id: &BlockId, para_id: ParaId, occupied_core_assumption: OccupiedCoreAssumption, ) -> Result, ApiError> { (**self).persisted_validation_data(block_id, para_id, occupied_core_assumption) - } + } - fn candidate_pending_availability( + fn candidate_pending_availability( &self, block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError> { (**self).candidate_pending_availability(block_id, para_id) } + + fn session_index_for_child(&self, block_id: &BlockId) -> Result { + (**self).session_index_for_child(block_id) + } + + fn validators(&self, block_id: &BlockId) -> Result, ApiError> { + (**self).validators(block_id) + } } -impl RelayChainInterface for Arc> - where - Client: ProvideRuntimeApi + 'static + Sync + Send, - Client::Api: ParachainHost, { - fn retrieve_dmq_contents( +impl RelayChainInterface for Arc +where + T: RelayChainInterface, +{ + fn retrieve_dmq_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option> { (**self).retrieve_dmq_contents(para_id, relay_parent) - } + } - fn retrieve_all_inbound_hrmp_channel_contents( + fn retrieve_all_inbound_hrmp_channel_contents( &self, para_id: ParaId, relay_parent: PHash, ) -> Option>> { (**self).retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent) - } + } - fn persisted_validation_data( + fn persisted_validation_data( &self, block_id: &BlockId, para_id: ParaId, occupied_core_assumption: OccupiedCoreAssumption, ) -> Result, ApiError> { (**self).persisted_validation_data(block_id, para_id, occupied_core_assumption) - } + } - fn candidate_pending_availability( + fn candidate_pending_availability( &self, block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError> { (**self).candidate_pending_availability(block_id, para_id) } + + fn session_index_for_child(&self, block_id: &BlockId) -> Result { + (**self).session_index_for_child(block_id) + } + + fn validators(&self, block_id: &BlockId) -> Result, ApiError> { + (**self).validators(block_id) + } } -pub fn build_relay_chain_direct(client: polkadot_client::Client) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { +pub fn build_relay_chain_direct( + client: polkadot_client::Client, +) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client }; relay_chain_builder.build() } From 2ffa0c318cdc61b94e031ad0b805ce15080251f5 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 17 Nov 2021 15:40:03 +0100 Subject: [PATCH 12/56] Change edition to string in rustfmt --- .rustfmt.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index e81dc030e50..1f7a8a8088b 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -21,4 +21,4 @@ spaces_around_ranges = false trailing_comma = "Vertical" trailing_semicolon = false use_field_init_shorthand = true -edition = 2018 +edition = "2018" From 026832d183f187daf427aeaacb04fb9fbd2e9982 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 17 Nov 2021 17:19:41 +0100 Subject: [PATCH 13/56] Change BlockchainEvent parameters --- client/network/src/lib.rs | 4 ++-- client/network/src/wait_on_relay_chain_block.rs | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 03e74b1985b..c77fefad29d 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -241,7 +241,7 @@ impl BlockAnnounceValidator { para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, - relay_chain_blockchain_events: Arc, + relay_chain_blockchain_events: BCE, ) -> Self { Self { phantom: Default::default(), @@ -460,7 +460,7 @@ where self.para_id, self.relay_chain_sync_oracle, self.relay_chain_backend, - client, + self.relay_chain_interface.clone(), )) } } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 5bb086fba83..701b00444f8 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -24,6 +24,7 @@ use sc_client_api::{ }; use sp_runtime::generic::BlockId; use std::{sync::Arc, time::Duration}; +use cumulus_relay_chain_interface::RelayChainInterface; /// The timeout in seconds after that the waiting for a block should be aborted. const TIMEOUT_IN_SECONDS: u64 = 6; @@ -64,7 +65,7 @@ pub enum Error { /// The timeout is set to 6 seconds. This should be enough time to import the block in the current /// round and if not, the new round of the relay chain already started anyway. pub struct WaitOnRelayChainBlock { - block_chain_events: Arc, + block_chain_events: BCE, backend: Arc, } @@ -76,15 +77,15 @@ impl Clone for WaitOnRelayChainBlock { impl WaitOnRelayChainBlock { /// Creates a new instance of `Self`. - pub fn new(backend: Arc, block_chain_events: Arc) -> Self { + pub fn new(backend: Arc, block_chain_events: BCE) -> Self { Self { backend, block_chain_events } } } -impl WaitOnRelayChainBlock +impl WaitOnRelayChainBlock where B: Backend, - BCE: BlockchainEvents, + RCInterface: RelayChainInterface, { pub fn wait_on_relay_chain_block( &self, From 6350c8a7707d36c07b4bb2395771a9b51ae6b439 Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 18 Nov 2021 14:46:29 +0100 Subject: [PATCH 14/56] Remove polkadot-client dependency from cumulus-network --- Cargo.lock | 1 - client/consensus/aura/src/lib.rs | 8 +- client/consensus/relay-chain/src/lib.rs | 10 +- client/network/Cargo.toml | 1 - client/network/src/lib.rs | 35 ++++--- .../network/src/wait_on_relay_chain_block.rs | 19 ++-- client/relay-chain-interface/src/lib.rs | 92 ++++++++++++++++--- parachain-template/node/src/service.rs | 3 +- polkadot-parachains/src/service.rs | 7 +- .../parachain-inherent/src/client_side.rs | 2 +- test/service/src/lib.rs | 14 +-- 11 files changed, 133 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1d00d32eda..23dc4e05f14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1509,7 +1509,6 @@ dependencies = [ "futures-timer 3.0.2", "parity-scale-codec", "parking_lot 0.10.2", - "polkadot-client", "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index ed5ff06cc14..2593d699fef 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -92,7 +92,7 @@ where impl AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone + Send + Sync, RBackend: Backend, CIDP: CreateInherentDataProviders, CIDP::InherentDataProviders: InherentDataProviderExt, @@ -206,7 +206,7 @@ impl ParachainConsensus for AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone + Send + Sync, // RClient: ProvideRuntimeApi + Send + Sync, // RClient::Api: ParachainHost, RBackend: Backend, @@ -315,7 +315,7 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { AuraConsensusBuilder::::new( proposer_factory, @@ -397,7 +397,7 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. fn new( diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 1f0c4fa3554..ca1aebadb7c 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -89,9 +89,7 @@ impl RelayChainConsensus where B: BlockT, - // RClient: ProvideRuntimeApi, - // RClient::Api: ParachainHost, - RCInterface: RelayChainInterface, + RCInterface: RelayChainInterface, RBackend: Backend, CIDP: CreateInherentDataProviders, { @@ -155,7 +153,7 @@ impl ParachainConsensus for RelayChainConsensus where B: BlockT, - RCInterface: RelayChainInterface + Send + Sync + Clone, + RCInterface: RelayChainInterface + Send + Sync + Clone, RBackend: Backend, BI: BlockImport + Send + Sync, PF: Environment + Send + Sync, @@ -265,7 +263,7 @@ where BI: BlockImport + Send + Sync + 'static, RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { RelayChainConsensusBuilder::new( para_id, @@ -308,7 +306,7 @@ where BI: BlockImport + Send + Sync + 'static, RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, { /// Create a new instance of the builder. fn new( diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 6c797ef64b5..806e2c8ee75 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -17,7 +17,6 @@ sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "mas # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-relay-chain-interface = { path = "../relay-chain-interface" } diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index c77fefad29d..846fdd3f4f0 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -20,7 +20,7 @@ //! that use the relay chain provided consensus. See [`BlockAnnounceValidator`] //! and [`WaitToAnnounce`] for more information about this implementation. -use sc_client_api::{Backend, BlockchainEvents}; +use sc_client_api::Backend; use sp_blockchain::HeaderBackend; use sp_consensus::{ block_validation::{BlockAnnounceValidator as BlockAnnounceValidatorT, Validation}, @@ -136,7 +136,7 @@ impl BlockAnnounceData { /// Returns an `Err(_)` if it failed. fn check_signature

(self, relay_chain_client: &P) -> Result where - P: RelayChainInterface + Send + Sync + 'static, + P: RelayChainInterface + Send + Sync + 'static, { let validator_index = self.statement.unchecked_validator_index(); @@ -225,41 +225,43 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// chain. If it is at the tip, it is required to provide a justification or otherwise we reject /// it. However, if the announcement is for a block below the tip the announcement is accepted /// as it probably comes from a node that is currently syncing the chain. -pub struct BlockAnnounceValidator { +pub struct BlockAnnounceValidator { phantom: PhantomData, relay_chain_interface: R, relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, - wait_on_relay_chain_block: WaitOnRelayChainBlock, + wait_on_relay_chain_block: WaitOnRelayChainBlock, } -impl BlockAnnounceValidator { +impl BlockAnnounceValidator +where + RCInterface: Clone, +{ /// Create a new [`BlockAnnounceValidator`]. pub fn new( - relay_chain_interface: R, + relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, relay_chain_backend: Arc, - relay_chain_blockchain_events: BCE, ) -> Self { Self { phantom: Default::default(), - relay_chain_interface, + relay_chain_interface: relay_chain_interface.clone(), para_id, relay_chain_sync_oracle, relay_chain_backend: relay_chain_backend.clone(), wait_on_relay_chain_block: WaitOnRelayChainBlock::new( relay_chain_backend, - relay_chain_blockchain_events, + relay_chain_interface, ), } } } -impl BlockAnnounceValidator +impl BlockAnnounceValidator where - R: RelayChainInterface + Clone, + R: RelayChainInterface + Clone, B: Backend + 'static, { /// Get the included block of the given parachain in the relay chain. @@ -340,12 +342,10 @@ where } } -impl BlockAnnounceValidatorT - for BlockAnnounceValidator +impl BlockAnnounceValidatorT for BlockAnnounceValidator where - P: RelayChainInterface + Clone + Send + Sync + 'static, + P: RelayChainInterface + Clone + Send + Sync + 'static, B: Backend + 'static, - BCE: BlockchainEvents + 'static + Send + Sync, { fn validate( &mut self, @@ -407,7 +407,7 @@ pub fn build_block_announce_validator( ) -> Box + Send> where B: Backend + Send + 'static, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { BlockAnnounceValidatorBuilder::new( relay_chain_interface, @@ -435,7 +435,7 @@ struct BlockAnnounceValidatorBuilder { impl BlockAnnounceValidatorBuilder where B: Backend + Send + 'static, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. fn new( @@ -460,7 +460,6 @@ where self.para_id, self.relay_chain_sync_oracle, self.relay_chain_backend, - self.relay_chain_interface.clone(), )) } } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 701b00444f8..9d5fabd4567 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -16,15 +16,15 @@ //! Provides the [`WaitOnRelayChainBlock`] type. +use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; use sc_client_api::{ blockchain::{self, BlockStatus, HeaderBackend}, - Backend, BlockchainEvents, + Backend, }; use sp_runtime::generic::BlockId; use std::{sync::Arc, time::Duration}; -use cumulus_relay_chain_interface::RelayChainInterface; /// The timeout in seconds after that the waiting for a block should be aborted. const TIMEOUT_IN_SECONDS: u64 = 6; @@ -64,20 +64,23 @@ pub enum Error { /// /// The timeout is set to 6 seconds. This should be enough time to import the block in the current /// round and if not, the new round of the relay chain already started anyway. -pub struct WaitOnRelayChainBlock { - block_chain_events: BCE, +pub struct WaitOnRelayChainBlock { + block_chain_events: R, backend: Arc, } -impl Clone for WaitOnRelayChainBlock { +impl Clone for WaitOnRelayChainBlock +where + RCInterface: Clone, +{ fn clone(&self) -> Self { Self { backend: self.backend.clone(), block_chain_events: self.block_chain_events.clone() } } } -impl WaitOnRelayChainBlock { +impl WaitOnRelayChainBlock { /// Creates a new instance of `Self`. - pub fn new(backend: Arc, block_chain_events: BCE) -> Self { + pub fn new(backend: Arc, block_chain_events: RCInterface) -> Self { Self { backend, block_chain_events } } } @@ -85,7 +88,7 @@ impl WaitOnRelayChainBlock { impl WaitOnRelayChainBlock where B: Backend, - RCInterface: RelayChainInterface, + RCInterface: RelayChainInterface, { pub fn wait_on_relay_chain_block( &self, diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 1bd752a5f01..29ed75ef9c5 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -11,12 +11,13 @@ use cumulus_primitives_core::{ InboundDownwardMessage, ParaId, PersistedValidationData, }; use polkadot_client::{ClientHandle, ExecuteWithClient}; -use sp_api::{ApiError, ProvideRuntimeApi}; +use sc_client_api::BlockchainEvents; +use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_core::sp_std::collections::btree_map::BTreeMap; const LOG_TARGET: &str = "cumulus-collator"; -pub trait RelayChainInterface { +pub trait RelayChainInterface { fn validators(&self, block_id: &BlockId) -> Result, ApiError>; /// Returns the whole contents of the downward message queue for the parachain we are collating @@ -53,16 +54,27 @@ pub trait RelayChainInterface { ) -> Result, ApiError>; fn session_index_for_child(&self, block_id: &BlockId) -> Result; + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result>; } pub struct RelayChainDirect { pub polkadot_client: Arc, } -impl RelayChainInterface for RelayChainDirect +impl RelayChainInterface for RelayChainDirect where - Client: ProvideRuntimeApi, + Client: ProvideRuntimeApi + BlockchainEvents, Client::Api: ParachainHost, + Block: BlockT, { fn retrieve_dmq_contents( &self, @@ -140,6 +152,25 @@ where fn validators(&self, block_id: &BlockId) -> Result, ApiError> { self.polkadot_client.runtime_api().validators(block_id) } + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + self.polkadot_client.import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + self.polkadot_client.finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result> { + self.polkadot_client + .storage_changes_notification_stream(filter_keys, child_filter_keys) + } } pub struct RelayChainDirectBuilder { @@ -147,24 +178,26 @@ pub struct RelayChainDirectBuilder { } impl RelayChainDirectBuilder { - pub fn build(self) -> Arc { + pub fn build(self) -> Arc + Sync + Send> { self.polkadot_client.clone().execute_with(self) } } impl ExecuteWithClient for RelayChainDirectBuilder { - type Output = Arc; + type Output = Arc + Sync + Send>; fn execute_with_client(self, client: Arc) -> Self::Output where - Client: ProvideRuntimeApi + 'static + Sync + Send, + Client: ProvideRuntimeApi + BlockchainEvents + 'static + Sync + Send, Client::Api: ParachainHost, { Arc::new(RelayChainDirect { polkadot_client: client }) } } -impl RelayChainInterface for Arc { +impl RelayChainInterface + for Arc + Sync + Send> +{ fn retrieve_dmq_contents( &self, para_id: ParaId, @@ -205,11 +238,30 @@ impl RelayChainInterface for Arc { fn validators(&self, block_id: &BlockId) -> Result, ApiError> { (**self).validators(block_id) } + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + (**self).import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + (**self).finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result> { + (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) + } } -impl RelayChainInterface for Arc +impl RelayChainInterface for Arc where - T: RelayChainInterface, + T: RelayChainInterface, + Block: BlockT, { fn retrieve_dmq_contents( &self, @@ -251,11 +303,29 @@ where fn validators(&self, block_id: &BlockId) -> Result, ApiError> { (**self).validators(block_id) } + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + (**self).import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + (**self).finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result> { + (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) + } } pub fn build_relay_chain_direct( client: polkadot_client::Client, -) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { +) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client }; relay_chain_builder.build() } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index d216597a247..17b5e00f02c 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -246,8 +246,9 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); let block_announce_validator = build_block_announce_validator( - relay_chain_full_node.client.clone(), + relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), relay_chain_full_node.backend.clone(), diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 08cf1c29278..4bfbb8c5dd4 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -329,8 +329,10 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + + let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); let block_announce_validator = build_block_announce_validator( - relay_chain_full_node.client.clone(), + relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), relay_chain_full_node.backend.clone(), @@ -507,8 +509,9 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); let block_announce_validator = build_block_announce_validator( - relay_chain_full_node.client.clone(), + relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), relay_chain_full_node.backend.clone(), diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index d456751782c..00b5d3a5e36 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -137,7 +137,7 @@ impl ParachainInherentData { para_id: ParaId, ) -> Option where - T: RelayChainInterface, + T: RelayChainInterface, { let relay_chain_state = collect_relay_storage_proof(polkadot_backend, para_id, relay_parent)?; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 2481749da20..2242f4ec347 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -22,13 +22,13 @@ mod chain_spec; mod genesis; use core::future::Future; -use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; +use cumulus_relay_chain_interface::{build_relay_chain_direct, RelayChainDirect}; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use sc_client_api::execution_extensions::ExecutionStrategies; @@ -53,7 +53,6 @@ use substrate_test_client::{ BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput, }; - pub use chain_spec::*; pub use cumulus_test_runtime as runtime; pub use genesis::*; @@ -209,12 +208,14 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + + let relay_chain_interface = + Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone() }); let block_announce_validator = BlockAnnounceValidator::new( - relay_chain_full_node.client.clone(), + relay_chain_interface, para_id, Box::new(relay_chain_full_node.network.clone()), relay_chain_full_node.backend.clone(), - relay_chain_full_node.client.clone(), ); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; @@ -274,8 +275,9 @@ where ); let relay_chain_backend = relay_chain_full_node.backend.clone(); - let relay_chain_interface = - Arc::new(RelayChainDirect {polkadot_client: relay_chain_full_node.client.clone() }); + let relay_chain_interface = Arc::new(RelayChainDirect { + polkadot_client: relay_chain_full_node.client.clone(), + }); let relay_chain_interface2 = relay_chain_interface.clone(); Box::new(cumulus_client_consensus_relay_chain::RelayChainConsensus::new( From 0fabdf09aa58d75a379818252eaf449800ddced8 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 23 Nov 2021 16:26:36 +0100 Subject: [PATCH 15/56] Modify tests to compile again --- client/network/src/lib.rs | 6 ++--- client/network/src/tests.rs | 8 +++---- .../network/src/wait_on_relay_chain_block.rs | 22 ++++++++++--------- client/relay-chain-interface/src/lib.rs | 6 +++++ test/service/src/lib.rs | 2 +- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 846fdd3f4f0..9c8714dcfa1 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -225,13 +225,13 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// chain. If it is at the tip, it is required to provide a justification or otherwise we reject /// it. However, if the announcement is for a block below the tip the announcement is accepted /// as it probably comes from a node that is currently syncing the chain. -pub struct BlockAnnounceValidator { +pub struct BlockAnnounceValidator { phantom: PhantomData, - relay_chain_interface: R, + relay_chain_interface: RCInterface, relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, - wait_on_relay_chain_block: WaitOnRelayChainBlock, + wait_on_relay_chain_block: WaitOnRelayChainBlock, } impl BlockAnnounceValidator diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 7124ebca85d..273072ba6dd 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -15,6 +15,7 @@ // along with Polkadot. If not, see . use super::*; +use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; use parking_lot::Mutex; @@ -62,16 +63,15 @@ impl SyncOracle for DummyCollatorNetwork { } fn make_validator_and_api( -) -> (BlockAnnounceValidator, Arc) { +) -> (BlockAnnounceValidator, PBackend>, Arc) { let api = Arc::new(TestApi::new()); - + let relay_chain_interface = RelayChainDirect { polkadot_client: api.relay_client.clone() }; ( BlockAnnounceValidator::new( - api.clone(), + relay_chain_interface, ParaId::from(56), Box::new(DummyCollatorNetwork), api.relay_backend.clone(), - api.relay_client.clone(), ), api, ) diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 9d5fabd4567..031fd7d36fd 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -129,6 +129,7 @@ where mod tests { use super::*; + use cumulus_relay_chain_interface::RelayChainDirect; use polkadot_test_client::{ construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, DefaultTestClientBuilderExt, ExecutionStrategy, FullBackend, InitPolkadotBlockBuilder, @@ -139,7 +140,8 @@ mod tests { use futures::{executor::block_on, poll, task::Poll}; - fn build_client_backend_and_block() -> (Arc, Arc, PBlock) { + fn build_client_backend_and_block( + ) -> (Arc, Arc, PBlock, RelayChainDirect) { let builder = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); let backend = builder.backend(); @@ -148,17 +150,17 @@ mod tests { let block_builder = client.init_polkadot_block_builder(); let block = block_builder.build().expect("Finalizes the block").block; - (client, backend, block) + (client.clone(), backend, block, RelayChainDirect { polkadot_client: client }) } #[test] fn returns_directly_for_available_block() { - let (mut client, backend, block) = build_client_backend_and_block(); + let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block"); - let wait = WaitOnRelayChainBlock::new(backend, client); + let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); block_on(async move { // Should be ready on the first poll @@ -168,10 +170,10 @@ mod tests { #[test] fn resolve_after_block_import_notification_was_received() { - let (mut client, backend, block) = build_client_backend_and_block(); + let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); - let wait = WaitOnRelayChainBlock::new(backend, client.clone()); + let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); block_on(async move { let mut future = wait.wait_on_relay_chain_block(hash); @@ -188,17 +190,17 @@ mod tests { #[test] fn wait_for_block_time_out_when_block_is_not_imported() { - let (client, backend, block) = build_client_backend_and_block(); + let (_, backend, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); - let wait = WaitOnRelayChainBlock::new(backend, client.clone()); + let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); assert!(matches!(block_on(wait.wait_on_relay_chain_block(hash)), Err(Error::Timeout(_)))); } #[test] fn do_not_resolve_after_different_block_import_notification_was_received() { - let (mut client, backend, block) = build_client_backend_and_block(); + let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); let ext = construct_transfer_extrinsic( @@ -213,7 +215,7 @@ mod tests { let block2 = block_builder.build().expect("Build second block").block; let hash2 = block2.hash(); - let wait = WaitOnRelayChainBlock::new(backend, client.clone()); + let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); block_on(async move { let mut future = wait.wait_on_relay_chain_block(hash); diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 29ed75ef9c5..d92788210e1 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -70,6 +70,12 @@ pub struct RelayChainDirect { pub polkadot_client: Arc, } +impl Clone for RelayChainDirect { + fn clone(&self) -> Self { + Self { polkadot_client: self.polkadot_client.clone() } + } +} + impl RelayChainInterface for RelayChainDirect where Client: ProvideRuntimeApi + BlockchainEvents, diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 2242f4ec347..66bb74f3508 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -28,7 +28,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::{build_relay_chain_direct, RelayChainDirect}; +use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use sc_client_api::execution_extensions::ExecutionStrategies; From 3d0af0c55a8d909d7fc17a4ab6fab76ebecebf6c Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 23 Nov 2021 21:41:50 +0100 Subject: [PATCH 16/56] Cleanup --- .rustfmt.toml | 1 - Cargo.lock | 29 +++++++++++++++++++++---- Cargo.toml | 1 - client/relay-chain-interface/Cargo.toml | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index 3c3ba173067..bfa2448ee17 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -22,4 +22,3 @@ spaces_around_ranges = false trailing_comma = "Vertical" trailing_semicolon = false use_field_init_shorthand = true -edition = "2018" diff --git a/Cargo.lock b/Cargo.lock index ca3dbd3d4a3..4abbadfb5f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1459,6 +1459,7 @@ dependencies = [ "cumulus-client-consensus-common", "cumulus-client-network", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-client", "cumulus-test-runtime", "futures 0.3.17", @@ -1484,11 +1485,12 @@ name = "cumulus-client-consensus-aura" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.17", "parity-scale-codec", - "polkadot-client", "sc-client-api", "sc-consensus", "sc-consensus-aura", @@ -1537,9 +1539,9 @@ dependencies = [ "async-trait", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.17", "parking_lot 0.10.2", - "polkadot-client", "sc-client-api", "sc-consensus", "sp-api", @@ -1558,13 +1560,13 @@ name = "cumulus-client-network" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-service", "derive_more", "futures 0.3.17", "futures-timer 3.0.2", "parity-scale-codec", "parking_lot 0.10.2", - "polkadot-client", "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", @@ -1809,9 +1811,9 @@ version = "0.1.0" dependencies = [ "async-trait", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-relay-sproof-builder", "parity-scale-codec", - "polkadot-client", "sc-client-api", "scale-info", "sp-api", @@ -1857,6 +1859,19 @@ dependencies = [ "xcm", ] +[[package]] +name = "cumulus-relay-chain-interface" +version = "0.1.0" +dependencies = [ + "cumulus-primitives-core", + "polkadot-client", + "sc-client-api", + "sp-api", + "sp-core", + "sp-runtime", + "tracing", +] + [[package]] name = "cumulus-test-client" version = "0.1.0" @@ -1945,12 +1960,14 @@ version = "0.1.0" dependencies = [ "async-trait", "criterion", + "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", "cumulus-client-network", "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "cumulus-test-relay-validation-worker-provider", "cumulus-test-runtime", "frame-system", @@ -1976,6 +1993,7 @@ dependencies = [ "sc-transaction-pool", "sc-transaction-pool-api", "serde", + "sp-api", "sp-arithmetic", "sp-blockchain", "sp-core", @@ -6037,6 +6055,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "derive_more", "frame-benchmarking", "frame-benchmarking-cli", @@ -6679,6 +6698,7 @@ dependencies = [ "assert_cmd", "async-trait", "cumulus-client-cli", + "cumulus-client-collator", "cumulus-client-consensus-aura", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", @@ -6686,6 +6706,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", + "cumulus-relay-chain-interface", "frame-benchmarking", "frame-benchmarking-cli", "futures 0.3.17", diff --git a/Cargo.toml b/Cargo.toml index 9aaffd4a932..d3725ff4a59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ members = [ "client/pov-recovery", "client/service", "client/relay-chain-interface", - "pallets/asset-tx-payment", "pallets/aura-ext", "pallets/collator-selection", "pallets/dmp-queue", diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 8e37a83ebaa..3e4582be6c2 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cumulus-relay-chain-interface" version = "0.1.0" -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 1033b8be9c7a242fdd2c17c12cde2a69c9c72a10 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 23 Nov 2021 23:04:23 +0100 Subject: [PATCH 17/56] Implement BlockchainEvents for TestApi --- client/network/src/tests.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 273072ba6dd..d603fa0c087 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -15,6 +15,7 @@ // along with Polkadot. If not, see . use super::*; +use cumulus_primitives_core::relay_chain::BlakeTwo256; use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; @@ -31,13 +32,14 @@ use polkadot_test_client::{ Client as PClient, ClientBlockImportExt, DefaultTestClientBuilderExt, FullBackend as PBackend, InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; +use sc_client_api::BlockchainEvents; use sp_api::{ApiRef, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; use sp_core::H256; use sp_keyring::Sr25519Keyring; use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr}; -use sp_runtime::RuntimeAppPublic; +use sp_runtime::{OpaqueExtrinsic, RuntimeAppPublic}; use std::collections::BTreeMap; fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) { @@ -63,9 +65,9 @@ impl SyncOracle for DummyCollatorNetwork { } fn make_validator_and_api( -) -> (BlockAnnounceValidator, PBackend>, Arc) { +) -> (BlockAnnounceValidator, PBackend>, Arc) { let api = Arc::new(TestApi::new()); - let relay_chain_interface = RelayChainDirect { polkadot_client: api.relay_client.clone() }; + let relay_chain_interface = RelayChainDirect { polkadot_client: api.clone() }; ( BlockAnnounceValidator::new( relay_chain_interface, @@ -413,6 +415,34 @@ impl ProvideRuntimeApi for TestApi { } } +impl BlockchainEvents for TestApi { + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + self.relay_client.import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + self.relay_client.finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sp_blockchain::Result< + sc_client_api::StorageEventStream< + , + OpaqueExtrinsic, + > as BlockT>::Hash, + >, + > { + self.relay_client + .storage_changes_notification_stream(filter_keys, child_filter_keys) + } +} + sp_api::mock_impl_runtime_apis! { impl ParachainHost for RuntimeApi { fn validators(&self) -> Vec { From d40f3281e4ba32ab35aa3ef7d2c4fe22b25567fb Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 24 Nov 2021 15:47:22 +0100 Subject: [PATCH 18/56] Remove references to backend from BlockAnnounceValidator --- Cargo.lock | 2 + client/network/src/lib.rs | 87 ++++--------------- client/network/src/tests.rs | 6 +- .../network/src/wait_on_relay_chain_block.rs | 60 ++++++------- client/relay-chain-interface/Cargo.toml | 2 + client/relay-chain-interface/src/lib.rs | 55 ++++++++++-- parachain-template/node/src/service.rs | 12 ++- polkadot-parachains/src/service.rs | 31 +++++-- .../service/benches/transaction_throughput.rs | 2 +- test/service/src/lib.rs | 8 +- 10 files changed, 140 insertions(+), 125 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4abbadfb5f4..a6f4c97607b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1864,9 +1864,11 @@ name = "cumulus-relay-chain-interface" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "parking_lot 0.11.2", "polkadot-client", "sc-client-api", "sp-api", + "sp-blockchain", "sp-core", "sp-runtime", "tracing", diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 58cb1c50995..56eb76ddbed 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -20,8 +20,6 @@ //! that use the relay chain provided consensus. See [`BlockAnnounceValidator`] //! and [`WaitToAnnounce`] for more information about this implementation. -use sc_client_api::Backend; -use sp_blockchain::HeaderBackend; use sp_consensus::{ block_validation::{BlockAnnounceValidator as BlockAnnounceValidatorT, Validation}, SyncOracle, @@ -225,16 +223,15 @@ impl TryFrom<&'_ CollationSecondedSignal> for BlockAnnounceData { /// chain. If it is at the tip, it is required to provide a justification or otherwise we reject /// it. However, if the announcement is for a block below the tip the announcement is accepted /// as it probably comes from a node that is currently syncing the chain. -pub struct BlockAnnounceValidator { +pub struct BlockAnnounceValidator { phantom: PhantomData, relay_chain_interface: RCInterface, - relay_chain_backend: Arc, para_id: ParaId, relay_chain_sync_oracle: Box, - wait_on_relay_chain_block: WaitOnRelayChainBlock, + wait_on_relay_chain_block: WaitOnRelayChainBlock, } -impl BlockAnnounceValidator +impl BlockAnnounceValidator where RCInterface: Clone, { @@ -243,26 +240,20 @@ where relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, - relay_chain_backend: Arc, ) -> Self { Self { phantom: Default::default(), relay_chain_interface: relay_chain_interface.clone(), para_id, relay_chain_sync_oracle, - relay_chain_backend: relay_chain_backend.clone(), - wait_on_relay_chain_block: WaitOnRelayChainBlock::new( - relay_chain_backend, - relay_chain_interface, - ), + wait_on_relay_chain_block: WaitOnRelayChainBlock::new(relay_chain_interface), } } } -impl BlockAnnounceValidator +impl BlockAnnounceValidator where R: RelayChainInterface + Clone, - B: Backend + 'static, { /// Get the included block of the given parachain in the relay chain. fn included_block( @@ -305,13 +296,12 @@ where header: Block::Header, ) -> impl Future> { let relay_chain_interface = self.relay_chain_interface.clone(); - let relay_chain_backend = self.relay_chain_backend.clone(); let para_id = self.para_id; async move { // Check if block is equal or higher than best (this requires a justification) - let relay_chain_info = relay_chain_backend.blockchain().info(); - let runtime_api_block_id = BlockId::Hash(relay_chain_info.best_hash); + let relay_chain_best_hash = relay_chain_interface.best_block_hash(); + let runtime_api_block_id = BlockId::Hash(relay_chain_best_hash); let block_number = header.number(); let best_head = @@ -342,10 +332,9 @@ where } } -impl BlockAnnounceValidatorT for BlockAnnounceValidator +impl BlockAnnounceValidatorT for BlockAnnounceValidator where P: RelayChainInterface + Clone + Send + Sync + 'static, - B: Backend + 'static, { fn validate( &mut self, @@ -399,42 +388,30 @@ where /// Build a block announce validator instance. /// /// Returns a boxed [`BlockAnnounceValidator`]. -pub fn build_block_announce_validator( +pub fn build_block_announce_validator( relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, - relay_chain_backend: Arc, ) -> Box + Send> where - B: Backend + Send + 'static, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - BlockAnnounceValidatorBuilder::new( - relay_chain_interface, - para_id, - relay_chain_sync_oracle, - relay_chain_backend, - ) - .build() + BlockAnnounceValidatorBuilder::new(relay_chain_interface, para_id, relay_chain_sync_oracle) + .build() } /// Block announce validator builder. /// -/// Builds a [`BlockAnnounceValidator`] for a parachain. As this requires -/// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] -/// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance. -struct BlockAnnounceValidatorBuilder { +/// Builds a [`BlockAnnounceValidator`] for a parachain. +struct BlockAnnounceValidatorBuilder { phantom: PhantomData, relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, - relay_chain_backend: Arc, } -impl BlockAnnounceValidatorBuilder +impl BlockAnnounceValidatorBuilder where - B: Backend + Send + 'static, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. @@ -442,15 +419,8 @@ where relay_chain_interface: RCInterface, para_id: ParaId, relay_chain_sync_oracle: Box, - relay_chain_backend: Arc, ) -> Self { - Self { - relay_chain_interface, - para_id, - relay_chain_sync_oracle, - relay_chain_backend, - phantom: PhantomData, - } + Self { relay_chain_interface, para_id, relay_chain_sync_oracle, phantom: PhantomData } } /// Build the block announce validator. @@ -459,37 +429,10 @@ where self.relay_chain_interface.clone(), self.para_id, self.relay_chain_sync_oracle, - self.relay_chain_backend, )) } } -// impl polkadot_client::ExecuteWithClient -// for BlockAnnounceValidatorBuilder -// where -// B: Backend + Send + 'static, -// { -// type Output = Box + Send>; - -// fn execute_with_client(self, client: Arc) -> Self::Output -// where -// >::StateBackend: -// sp_api::StateBackend, -// PBackend: Backend, -// PBackend::State: sp_api::StateBackend, -// Api: polkadot_client::RuntimeApiCollection, -// PClient: polkadot_client::AbstractClient + 'static, -// { -// Box::new(BlockAnnounceValidator::new( -// client.clone(), -// self.para_id, -// self.relay_chain_sync_oracle, -// self.relay_chain_backend, -// client, -// )) -// } -// } - /// Wait before announcing a block that a candidate message has been received for this block, then /// add this message as justification for the block announcement. /// diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index d603fa0c087..e14b805d474 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -65,15 +65,15 @@ impl SyncOracle for DummyCollatorNetwork { } fn make_validator_and_api( -) -> (BlockAnnounceValidator, PBackend>, Arc) { +) -> (BlockAnnounceValidator>, Arc) { let api = Arc::new(TestApi::new()); - let relay_chain_interface = RelayChainDirect { polkadot_client: api.clone() }; + let relay_chain_interface = + RelayChainDirect { polkadot_client: api.clone(), backend: api.relay_backend.clone() }; ( BlockAnnounceValidator::new( relay_chain_interface, ParaId::from(56), Box::new(DummyCollatorNetwork), - api.relay_backend.clone(), ), api, ) diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 031fd7d36fd..3dfeb105459 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -19,12 +19,9 @@ use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; -use sc_client_api::{ - blockchain::{self, BlockStatus, HeaderBackend}, - Backend, -}; +use sc_client_api::blockchain::{self, BlockStatus}; use sp_runtime::generic::BlockId; -use std::{sync::Arc, time::Duration}; +use std::time::Duration; /// The timeout in seconds after that the waiting for a block should be aborted. const TIMEOUT_IN_SECONDS: u64 = 6; @@ -64,44 +61,43 @@ pub enum Error { /// /// The timeout is set to 6 seconds. This should be enough time to import the block in the current /// round and if not, the new round of the relay chain already started anyway. -pub struct WaitOnRelayChainBlock { - block_chain_events: R, - backend: Arc, +pub struct WaitOnRelayChainBlock { + relay_chain_interface: R, } -impl Clone for WaitOnRelayChainBlock +impl Clone for WaitOnRelayChainBlock where RCInterface: Clone, { fn clone(&self) -> Self { - Self { backend: self.backend.clone(), block_chain_events: self.block_chain_events.clone() } + Self { relay_chain_interface: self.relay_chain_interface.clone() } } } -impl WaitOnRelayChainBlock { +impl WaitOnRelayChainBlock { /// Creates a new instance of `Self`. - pub fn new(backend: Arc, block_chain_events: RCInterface) -> Self { - Self { backend, block_chain_events } + pub fn new(relay_chain_interface: RCInterface) -> Self { + Self { relay_chain_interface } } } -impl WaitOnRelayChainBlock +impl WaitOnRelayChainBlock where - B: Backend, RCInterface: RelayChainInterface, { pub fn wait_on_relay_chain_block( &self, hash: PHash, ) -> impl Future> { - let _lock = self.backend.get_import_lock().read(); - match self.backend.blockchain().status(BlockId::Hash(hash)) { + let _lock = self.relay_chain_interface.get_import_lock().read(); + + match self.relay_chain_interface.block_status(BlockId::Hash(hash)) { Ok(BlockStatus::InChain) => return ready(Ok(())).boxed(), Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(), _ => {}, } - let mut listener = self.block_chain_events.import_notification_stream(); + let mut listener = self.relay_chain_interface.import_notification_stream(); // Now it is safe to drop the lock, even when the block is now imported, it should show // up in our registered listener. drop(_lock); @@ -132,16 +128,16 @@ mod tests { use cumulus_relay_chain_interface::RelayChainDirect; use polkadot_test_client::{ construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, - DefaultTestClientBuilderExt, ExecutionStrategy, FullBackend, InitPolkadotBlockBuilder, + DefaultTestClientBuilderExt, ExecutionStrategy, InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; + use sc_service::Arc; use sp_consensus::BlockOrigin; use sp_runtime::traits::Block as BlockT; use futures::{executor::block_on, poll, task::Poll}; - fn build_client_backend_and_block( - ) -> (Arc, Arc, PBlock, RelayChainDirect) { + fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainDirect) { let builder = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); let backend = builder.backend(); @@ -150,17 +146,21 @@ mod tests { let block_builder = client.init_polkadot_block_builder(); let block = block_builder.build().expect("Finalizes the block").block; - (client.clone(), backend, block, RelayChainDirect { polkadot_client: client }) + ( + client.clone(), + block, + RelayChainDirect { polkadot_client: client, backend: backend.clone() }, + ) } #[test] fn returns_directly_for_available_block() { - let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block"); - let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); + let wait = WaitOnRelayChainBlock::new(relay_chain_interface); block_on(async move { // Should be ready on the first poll @@ -170,10 +170,10 @@ mod tests { #[test] fn resolve_after_block_import_notification_was_received() { - let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); - let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); + let wait = WaitOnRelayChainBlock::new(relay_chain_interface); block_on(async move { let mut future = wait.wait_on_relay_chain_block(hash); @@ -190,17 +190,17 @@ mod tests { #[test] fn wait_for_block_time_out_when_block_is_not_imported() { - let (_, backend, block, relay_chain_interface) = build_client_backend_and_block(); + let (_, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); - let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); + let wait = WaitOnRelayChainBlock::new(relay_chain_interface); assert!(matches!(block_on(wait.wait_on_relay_chain_block(hash)), Err(Error::Timeout(_)))); } #[test] fn do_not_resolve_after_different_block_import_notification_was_received() { - let (mut client, backend, block, relay_chain_interface) = build_client_backend_and_block(); + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); let hash = block.hash(); let ext = construct_transfer_extrinsic( @@ -215,7 +215,7 @@ mod tests { let block2 = block_builder.build().expect("Build second block").block; let hash2 = block2.hash(); - let wait = WaitOnRelayChainBlock::new(backend, relay_chain_interface); + let wait = WaitOnRelayChainBlock::new(relay_chain_interface); block_on(async move { let mut future = wait.wait_on_relay_chain_block(hash); diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 3e4582be6c2..904d1026fbf 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -13,6 +13,8 @@ cumulus-primitives-core = { path = "../../primitives/core" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +parking_lot = "0.11.1" tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index d92788210e1..2da02a6e60d 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -10,16 +10,22 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use polkadot_client::{ClientHandle, ExecuteWithClient}; -use sc_client_api::BlockchainEvents; +use parking_lot::RwLock; +use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; +use sc_client_api::{blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend}; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_core::sp_std::collections::btree_map::BTreeMap; const LOG_TARGET: &str = "cumulus-collator"; pub trait RelayChainInterface { + fn get_import_lock(&self) -> &RwLock<()>; + fn validators(&self, block_id: &BlockId) -> Result, ApiError>; + fn block_status(&self, block_id: BlockId) -> Result; + + fn best_block_hash(&self) -> PHash; /// Returns the whole contents of the downward message queue for the parachain we are collating /// for. /// @@ -68,11 +74,12 @@ pub trait RelayChainInterface { pub struct RelayChainDirect { pub polkadot_client: Arc, + pub backend: Arc, } impl Clone for RelayChainDirect { fn clone(&self) -> Self { - Self { polkadot_client: self.polkadot_client.clone() } + Self { polkadot_client: self.polkadot_client.clone(), backend: self.backend.clone() } } } @@ -177,10 +184,23 @@ where self.polkadot_client .storage_changes_notification_stream(filter_keys, child_filter_keys) } + + fn best_block_hash(&self) -> PHash { + self.backend.blockchain().info().best_hash + } + + fn block_status(&self, block_id: BlockId) -> Result { + self.backend.blockchain().status(block_id) + } + + fn get_import_lock(&self) -> &RwLock<()> { + self.backend.get_import_lock() + } } pub struct RelayChainDirectBuilder { polkadot_client: polkadot_client::Client, + backend: Arc, } impl RelayChainDirectBuilder { @@ -197,7 +217,7 @@ impl ExecuteWithClient for RelayChainDirectBuilder { Client: ProvideRuntimeApi + BlockchainEvents + 'static + Sync + Send, Client::Api: ParachainHost, { - Arc::new(RelayChainDirect { polkadot_client: client }) + Arc::new(RelayChainDirect { polkadot_client: client, backend: self.backend }) } } @@ -262,6 +282,18 @@ impl RelayChainInterface ) -> sc_client_api::blockchain::Result> { (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) } + + fn best_block_hash(&self) -> PHash { + (**self).best_block_hash() + } + + fn block_status(&self, block_id: BlockId) -> Result { + (**self).block_status(block_id) + } + + fn get_import_lock(&self) -> &RwLock<()> { + (**self).get_import_lock() + } } impl RelayChainInterface for Arc @@ -327,11 +359,24 @@ where ) -> sc_client_api::blockchain::Result> { (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) } + + fn best_block_hash(&self) -> PHash { + (**self).best_block_hash() + } + + fn block_status(&self, block_id: BlockId) -> Result { + (**self).block_status(block_id) + } + + fn get_import_lock(&self) -> &RwLock<()> { + (**self).get_import_lock() + } } pub fn build_relay_chain_direct( client: polkadot_client::Client, + backend: Arc, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client }; + let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend }; relay_chain_builder.build() } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index cc5c4662cc1..b98511cde23 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -246,12 +246,15 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_full_node.client.clone(), + relay_chain_full_node.backend.clone(), + ); + let block_announce_validator = build_block_announce_validator( relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), - relay_chain_full_node.backend.clone(), ); let force_authoring = parachain_config.force_authoring; @@ -430,7 +433,10 @@ pub async fn start_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_node.client.clone(), + relay_chain_backend.clone(), + ); let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 6817ed4ea19..68d698a8042 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -330,12 +330,15 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_full_node.client.clone(), + relay_chain_full_node.backend.clone(), + ); + let block_announce_validator = build_block_announce_validator( relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), - relay_chain_full_node.backend.clone(), ); let force_authoring = parachain_config.force_authoring; @@ -506,12 +509,15 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_full_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_full_node.client.clone(), + relay_chain_full_node.backend.clone(), + ); + let block_announce_validator = build_block_announce_validator( relay_chain_interface, id, Box::new(relay_chain_full_node.network.clone()), - relay_chain_full_node.backend.clone(), ); let force_authoring = parachain_config.force_authoring; @@ -705,7 +711,7 @@ pub async fn start_rococo_parachain_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_backend.clone()); let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); Ok(build_aura_consensus::< @@ -828,7 +834,10 @@ pub async fn start_shell_node( ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_node.client.clone(), + relay_chain_backend.clone(), + ); Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { @@ -1110,7 +1119,10 @@ where ); let relay_chain_backend2 = relay_chain_backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_client.clone(), + relay_chain_backend2.clone(), + ); let relay_chain_interface2 = relay_chain_interface.clone(); build_aura_consensus::< @@ -1179,7 +1191,10 @@ where ); let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_interface = build_relay_chain_direct(relay_chain_node.client.clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_node.client.clone(), + relay_chain_backend.clone(), + ); let relay_chain_consensus = cumulus_client_consensus_relay_chain::build_relay_chain_consensus( diff --git a/test/service/benches/transaction_throughput.rs b/test/service/benches/transaction_throughput.rs index 70fadcd42e1..239394d0bce 100644 --- a/test/service/benches/transaction_throughput.rs +++ b/test/service/benches/transaction_throughput.rs @@ -18,7 +18,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput}; use cumulus_test_runtime::{AccountId, BalancesCall, SudoCall}; -use futures::{future, join, StreamExt}; +use futures::{future, StreamExt}; use polkadot_service::polkadot_runtime::constants::currency::DOLLARS; use sc_transaction_pool_api::{TransactionPool as _, TransactionSource, TransactionStatus}; use sp_core::{crypto::Pair, sr25519}; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 0d668e121e4..863833910e6 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -215,13 +215,14 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = - Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone() }); + let relay_chain_interface = Arc::new(RelayChainDirect { + polkadot_client: relay_chain_full_node.client.clone(), + backend: relay_chain_full_node.backend.clone(), + }); let block_announce_validator = BlockAnnounceValidator::new( relay_chain_interface, para_id, Box::new(relay_chain_full_node.network.clone()), - relay_chain_full_node.backend.clone(), ); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; @@ -280,6 +281,7 @@ where let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), + backend: relay_chain_backend.clone(), }); let relay_chain_interface2 = relay_chain_interface.clone(); From a938b70b73b1c5907c8bde2c25194c543874204b Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 25 Nov 2021 09:52:28 +0100 Subject: [PATCH 19/56] Extract get_state to RelayChainInterface --- client/relay-chain-interface/src/lib.rs | 26 +++++++++++++++++++ parachain-template/node/src/service.rs | 1 - polkadot-parachains/src/service.rs | 4 --- .../parachain-inherent/src/client_side.rs | 10 +++---- test/service/src/lib.rs | 1 - 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 2da02a6e60d..9f8655ab048 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -19,6 +19,11 @@ use sp_core::sp_std::collections::btree_map::BTreeMap; const LOG_TARGET: &str = "cumulus-collator"; pub trait RelayChainInterface { + fn get_state_at( + &self, + block_id: BlockId, + ) -> Result<>::State, sp_blockchain::Error>; + fn get_import_lock(&self) -> &RwLock<()>; fn validators(&self, block_id: &BlockId) -> Result, ApiError>; @@ -196,6 +201,13 @@ where fn get_import_lock(&self) -> &RwLock<()> { self.backend.get_import_lock() } + + fn get_state_at( + &self, + block_id: BlockId, + ) -> Result<>::State, sp_blockchain::Error> { + self.backend.state_at(block_id) + } } pub struct RelayChainDirectBuilder { @@ -294,6 +306,13 @@ impl RelayChainInterface fn get_import_lock(&self) -> &RwLock<()> { (**self).get_import_lock() } + + fn get_state_at( + &self, + block_id: BlockId, + ) -> Result<>::State, sp_blockchain::Error> { + (**self).get_state_at(block_id) + } } impl RelayChainInterface for Arc @@ -371,6 +390,13 @@ where fn get_import_lock(&self) -> &RwLock<()> { (**self).get_import_lock() } + + fn get_state_at( + &self, + block_id: BlockId, + ) -> Result<>::State, sp_blockchain::Error> { + (**self).get_state_at(block_id) + } } pub fn build_relay_chain_direct( diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index b98511cde23..b329248da6f 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -457,7 +457,6 @@ pub async fn start_parachain_node( cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_interface, - &*relay_chain_backend, &validation_data, id, ); diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 68d698a8042..cc4d1100b19 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -733,7 +733,6 @@ pub async fn start_rococo_parachain_node( cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_direct, - &*relay_chain_backend, &validation_data, id, ); @@ -851,7 +850,6 @@ pub async fn start_shell_node( cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_interface, - &*relay_chain_backend, &validation_data, id, ); @@ -1144,7 +1142,6 @@ where cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_interface, - &*relay_chain_backend, &validation_data, id, ); @@ -1210,7 +1207,6 @@ where cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_interface, - &*relay_chain_backend, &validation_data, id, ); diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 7221abaa2a2..599be8cee35 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -32,14 +32,14 @@ const LOG_TARGET: &str = "parachain-inherent"; /// Collect the relevant relay chain state in form of a proof for putting it into the validation /// data inherent. fn collect_relay_storage_proof( - polkadot_backend: &impl Backend, + relay_chain_interface: &impl RelayChainInterface, para_id: ParaId, relay_parent: PHash, ) -> Option { use relay_chain::well_known_keys as relay_well_known_keys; - let relay_parent_state_backend = polkadot_backend - .state_at(BlockId::Hash(relay_parent)) + let relay_parent_state_backend = relay_chain_interface + .get_state_at(BlockId::Hash(relay_parent)) .map_err(|e| { tracing::error!( target: LOG_TARGET, @@ -132,7 +132,6 @@ impl ParachainInherentData { pub fn create_at( relay_parent: PHash, relay_chain_interface: &T, - polkadot_backend: &impl Backend, validation_data: &PersistedValidationData, para_id: ParaId, ) -> Option @@ -140,7 +139,8 @@ impl ParachainInherentData { T: RelayChainInterface, { let relay_chain_state = - collect_relay_storage_proof(polkadot_backend, para_id, relay_parent)?; + collect_relay_storage_proof(relay_chain_interface, para_id, relay_parent)?; + let downward_messages = relay_chain_interface.retrieve_dmq_contents(para_id, relay_parent)?; let horizontal_messages = relay_chain_interface diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 863833910e6..9a52fca10c8 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -293,7 +293,6 @@ where cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_interface, - &*relay_chain_backend, &validation_data, para_id, ); From 9c865fed90f2af045a4601046da4c592fddd9f99 Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 25 Nov 2021 15:19:30 +0100 Subject: [PATCH 20/56] Remove backend references from consensus structs --- client/consensus/aura/src/lib.rs | 40 +++++-------------- client/consensus/relay-chain/src/lib.rs | 38 +++++------------- parachain-template/node/src/service.rs | 6 +-- polkadot-parachains/src/service.rs | 27 ++++--------- .../parachain-inherent/src/client_side.rs | 1 - test/service/src/lib.rs | 5 +-- 6 files changed, 32 insertions(+), 85 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index e8408627f30..2b494a2359b 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -60,10 +60,9 @@ pub use sc_consensus_slots::InherentDataProviderExt; const LOG_TARGET: &str = "aura::cumulus"; /// The implementation of the AURA consensus for parachains. -pub struct AuraConsensus { +pub struct AuraConsensus { create_inherent_data_providers: Arc, relay_chain_interface: RCInterface, - relay_chain_backend: Arc, aura_worker: Arc< Mutex< dyn sc_consensus_slots::SlotWorker::Proof> @@ -74,14 +73,13 @@ pub struct AuraConsensus { slot_duration: SlotDuration, } -impl Clone for AuraConsensus +impl Clone for AuraConsensus where RCInterface: Clone + Send + Sync, { fn clone(&self) -> Self { Self { create_inherent_data_providers: self.create_inherent_data_providers.clone(), - relay_chain_backend: self.relay_chain_backend.clone(), relay_chain_interface: self.relay_chain_interface.clone(), aura_worker: self.aura_worker.clone(), slot_duration: self.slot_duration, @@ -89,11 +87,10 @@ where } } -impl AuraConsensus +impl AuraConsensus where B: BlockT, RCInterface: RelayChainInterface + Clone + Send + Sync, - RBackend: Backend, CIDP: CreateInherentDataProviders, CIDP::InherentDataProviders: InherentDataProviderExt, { @@ -108,7 +105,6 @@ where keystore: SyncCryptoStorePtr, create_inherent_data_providers: CIDP, relay_chain_interface: RCInterface, - polkadot_backend: Arc, slot_duration: SlotDuration, telemetry: Option, block_proposal_slot_portion: SlotProportion, @@ -152,7 +148,6 @@ where Self { create_inherent_data_providers: Arc::new(create_inherent_data_providers), - relay_chain_backend: polkadot_backend, relay_chain_interface, aura_worker: Arc::new(Mutex::new(worker)), slot_duration, @@ -196,14 +191,10 @@ where } #[async_trait::async_trait] -impl ParachainConsensus - for AuraConsensus +impl ParachainConsensus for AuraConsensus where B: BlockT, RCInterface: RelayChainInterface + Clone + Send + Sync, - // RClient: ProvideRuntimeApi + Send + Sync, - // RClient::Api: ParachainHost, - RBackend: Backend, CIDP: CreateInherentDataProviders + Send + Sync, CIDP::InherentDataProviders: InherentDataProviderExt + Send, { @@ -236,12 +227,11 @@ where } /// Paramaters of [`build_aura_consensus`]. -pub struct BuildAuraConsensusParams { +pub struct BuildAuraConsensusParams { pub proposer_factory: PF, pub create_inherent_data_providers: CIDP, pub block_import: BI, pub relay_chain_interface: RCInterface, - pub relay_chain_backend: Arc, pub para_client: Arc, pub backoff_authoring_blocks: Option, pub sync_oracle: SO, @@ -256,13 +246,12 @@ pub struct BuildAuraConsensusParams( +pub fn build_aura_consensus( BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers, block_import, relay_chain_interface, - relay_chain_backend, para_client, backoff_authoring_blocks, sync_oracle, @@ -272,11 +261,10 @@ pub fn build_aura_consensus, + }: BuildAuraConsensusParams, ) -> Box> where Block: BlockT, - RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + Send + Sync @@ -310,12 +298,11 @@ where P::Signature: TryFrom> + Hash + Member + Encode + Decode, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - AuraConsensusBuilder::::new( + AuraConsensusBuilder::::new( proposer_factory, block_import, create_inherent_data_providers, relay_chain_interface, - relay_chain_backend, para_client, backoff_authoring_blocks, sync_oracle, @@ -335,12 +322,11 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instance. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct AuraConsensusBuilder { +struct AuraConsensusBuilder { _phantom: PhantomData<(Block, Error, P)>, proposer_factory: PF, create_inherent_data_providers: CIDP, block_import: BI, - relay_chain_backend: Arc, relay_chain_interface: RCInterface, para_client: Arc, backoff_authoring_blocks: Option, @@ -353,11 +339,10 @@ struct AuraConsensusBuilder, } -impl - AuraConsensusBuilder +impl + AuraConsensusBuilder where Block: BlockT, - RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + Send + Sync @@ -397,7 +382,6 @@ where block_import: BI, create_inherent_data_providers: CIDP, relay_chain_interface: RCInterface, - relay_chain_backend: Arc, para_client: Arc, backoff_authoring_blocks: Option, sync_oracle: SO, @@ -413,7 +397,6 @@ where proposer_factory, block_import, create_inherent_data_providers, - relay_chain_backend, relay_chain_interface, para_client, backoff_authoring_blocks, @@ -439,7 +422,6 @@ where self.keystore, self.create_inherent_data_providers, self.relay_chain_interface.clone(), - self.relay_chain_backend, self.slot_duration, self.telemetry, self.block_proposal_slot_portion, diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index ca1aebadb7c..88c1e8ec5ab 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -57,18 +57,16 @@ pub use import_queue::{import_queue, Verifier}; const LOG_TARGET: &str = "cumulus-consensus-relay-chain"; /// The implementation of the relay-chain provided consensus for parachains. -pub struct RelayChainConsensus { +pub struct RelayChainConsensus { para_id: ParaId, _phantom: PhantomData, proposer_factory: Arc>, create_inherent_data_providers: Arc, block_import: Arc>>, relay_chain_interface: RCInterface, - relay_chain_backend: Arc, } -impl Clone - for RelayChainConsensus +impl Clone for RelayChainConsensus where RCInterface: Clone, { @@ -79,18 +77,15 @@ where proposer_factory: self.proposer_factory.clone(), create_inherent_data_providers: self.create_inherent_data_providers.clone(), block_import: self.block_import.clone(), - relay_chain_backend: self.relay_chain_backend.clone(), relay_chain_interface: self.relay_chain_interface.clone(), } } } -impl - RelayChainConsensus +impl RelayChainConsensus where B: BlockT, RCInterface: RelayChainInterface, - RBackend: Backend, CIDP: CreateInherentDataProviders, { /// Create a new instance of relay-chain provided consensus. @@ -100,7 +95,6 @@ where create_inherent_data_providers: CIDP, block_import: BI, relay_chain_interface: RCInterface, - polkadot_backend: Arc, ) -> Self { Self { para_id, @@ -109,7 +103,6 @@ where block_import: Arc::new(futures::lock::Mutex::new(ParachainBlockImport::new( block_import, ))), - relay_chain_backend: polkadot_backend, relay_chain_interface, _phantom: PhantomData, } @@ -149,12 +142,11 @@ where } #[async_trait::async_trait] -impl ParachainConsensus - for RelayChainConsensus +impl ParachainConsensus + for RelayChainConsensus where B: BlockT, RCInterface: RelayChainInterface + Send + Sync + Clone, - RBackend: Backend, BI: BlockImport + Send + Sync, PF: Environment + Send + Sync, PF::Proposer: Proposer< @@ -229,27 +221,25 @@ where } /// Paramaters of [`build_relay_chain_consensus`]. -pub struct BuildRelayChainConsensusParams { +pub struct BuildRelayChainConsensusParams { pub para_id: ParaId, pub proposer_factory: PF, pub create_inherent_data_providers: CIDP, pub block_import: BI, pub relay_chain_interface: RCInterface, - pub relay_chain_backend: Arc, } /// Build the [`RelayChainConsensus`]. /// /// Returns a boxed [`ParachainConsensus`]. -pub fn build_relay_chain_consensus( +pub fn build_relay_chain_consensus( BuildRelayChainConsensusParams { para_id, proposer_factory, create_inherent_data_providers, block_import, relay_chain_interface, - relay_chain_backend, - }: BuildRelayChainConsensusParams, + }: BuildRelayChainConsensusParams, ) -> Box> where Block: BlockT, @@ -261,7 +251,6 @@ where Proof = ::Proof, >, BI: BlockImport + Send + Sync + 'static, - RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { @@ -271,7 +260,6 @@ where block_import, create_inherent_data_providers, relay_chain_interface, - relay_chain_backend, ) .build() } @@ -282,18 +270,16 @@ where /// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] /// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance. -struct RelayChainConsensusBuilder { +struct RelayChainConsensusBuilder { para_id: ParaId, _phantom: PhantomData, proposer_factory: PF, create_inherent_data_providers: CIDP, block_import: BI, - relay_chain_backend: Arc, relay_chain_interface: RCInterface, } -impl - RelayChainConsensusBuilder +impl RelayChainConsensusBuilder where Block: BlockT, PF: Environment + Send + Sync + 'static, @@ -304,7 +290,6 @@ where Proof = ::Proof, >, BI: BlockImport + Send + Sync + 'static, - RBackend: Backend + 'static, CIDP: CreateInherentDataProviders + 'static, RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, { @@ -315,7 +300,6 @@ where block_import: BI, create_inherent_data_providers: CIDP, relay_chain_interface: RCInterface, - relay_chain_backend: Arc, ) -> Self { Self { para_id, @@ -323,7 +307,6 @@ where proposer_factory, block_import, create_inherent_data_providers, - relay_chain_backend, relay_chain_interface, } } @@ -336,7 +319,6 @@ where self.create_inherent_data_providers, self.block_import, self.relay_chain_interface, - self.relay_chain_backend, )) } } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index b329248da6f..9e79929e93b 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -432,11 +432,11 @@ pub async fn start_parachain_node( telemetry.clone(), ); - let relay_chain_backend = relay_chain_node.backend.clone(); let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), - relay_chain_backend.clone(), + relay_chain_node.backend.clone(), ); + let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -449,7 +449,6 @@ pub async fn start_parachain_node( _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -479,7 +478,6 @@ pub async fn start_parachain_node( }, block_import: client.clone(), relay_chain_interface: relay_chain_interface2, - relay_chain_backend: relay_chain_node.backend.clone(), para_client: client, backoff_authoring_blocks: Option::<()>::None, sync_oracle, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index cc4d1100b19..adbf67fc035 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -710,8 +710,7 @@ pub async fn start_rococo_parachain_node( telemetry.clone(), ); - let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_backend.clone()); + let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone()); let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); Ok(build_aura_consensus::< @@ -725,7 +724,6 @@ pub async fn start_rococo_parachain_node( _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -755,7 +753,6 @@ pub async fn start_rococo_parachain_node( }, block_import: client.clone(), relay_chain_interface: relay_chain_direct_for_aura_consensus, - relay_chain_backend: relay_chain_node.backend.clone(), para_client: client.clone(), backoff_authoring_blocks: Option::<()>::None, sync_oracle, @@ -832,10 +829,9 @@ pub async fn start_shell_node( telemetry.clone(), ); - let relay_chain_backend = relay_chain_node.backend.clone(); let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), - relay_chain_backend.clone(), + relay_chain_node.backend.clone(), ); Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( @@ -844,7 +840,6 @@ pub async fn start_shell_node( proposer_factory, block_import: client.clone(), relay_chain_interface: relay_chain_interface.clone(), - relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( @@ -1097,13 +1092,16 @@ where keystore, force_authoring| { let client2 = client.clone(); - let relay_chain_backend = relay_chain_node.backend.clone(); - let relay_chain_client = relay_chain_node.client.clone(); let spawn_handle = task_manager.spawn_handle(); let transaction_pool2 = transaction_pool.clone(); let telemetry2 = telemetry.clone(); let prometheus_registry2 = prometheus_registry.map(|r| (*r).clone()); + let relay_chain_interface = build_relay_chain_direct( + relay_chain_node.client.clone(), + relay_chain_node.backend.clone(), + ); + let aura_consensus = BuildOnAccess::Uninitialized(Some(Box::new(move || { let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client2).unwrap(); @@ -1116,11 +1114,6 @@ where telemetry2.clone(), ); - let relay_chain_backend2 = relay_chain_backend.clone(); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_client.clone(), - relay_chain_backend2.clone(), - ); let relay_chain_interface2 = relay_chain_interface.clone(); build_aura_consensus::< @@ -1134,7 +1127,6 @@ where _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -1164,7 +1156,6 @@ where }, block_import: client2.clone(), relay_chain_interface: relay_chain_interface2, - relay_chain_backend: relay_chain_backend2, para_client: client2.clone(), backoff_authoring_blocks: Option::<()>::None, sync_oracle, @@ -1187,10 +1178,9 @@ where telemetry.clone(), ); - let relay_chain_backend = relay_chain_node.backend.clone(); let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), - relay_chain_backend.clone(), + relay_chain_node.backend.clone(), ); let relay_chain_consensus = @@ -1200,7 +1190,6 @@ where proposer_factory, block_import: client.clone(), relay_chain_interface: relay_chain_interface.clone(), - relay_chain_backend: relay_chain_node.backend.clone(), create_inherent_data_providers: move |_, (relay_parent, validation_data)| { let parachain_inherent = diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 599be8cee35..eddaa7d4b18 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -23,7 +23,6 @@ use cumulus_primitives_core::{ ParaId, PersistedValidationData, }; use cumulus_relay_chain_interface::RelayChainInterface; -use sc_client_api::Backend; use sp_runtime::generic::BlockId; use sp_state_machine::Backend as _; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 9a52fca10c8..d97fd127b8d 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -277,11 +277,9 @@ where prometheus_registry.as_ref(), None, ); - let relay_chain_backend = relay_chain_full_node.backend.clone(); - let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), - backend: relay_chain_backend.clone(), + backend: relay_chain_full_node.backend.clone(), }); let relay_chain_interface2 = relay_chain_interface.clone(); @@ -310,7 +308,6 @@ where }, client.clone(), relay_chain_interface2, - relay_chain_full_node.backend.clone(), )) }, Consensus::Null => Box::new(NullConsensus), From 054d22d047400d6462cd2b8e99dba99f8341afeb Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 25 Nov 2021 16:35:11 +0100 Subject: [PATCH 21/56] Move network to relay-chain-interface --- Cargo.lock | 2 ++ client/relay-chain-interface/Cargo.toml | 2 ++ client/relay-chain-interface/src/lib.rs | 32 ++++++++++++++++++++++--- parachain-template/node/src/service.rs | 2 ++ polkadot-parachains/src/service.rs | 7 +++++- test/service/src/lib.rs | 2 ++ 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6f4c97607b..873eeca0972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1867,8 +1867,10 @@ dependencies = [ "parking_lot 0.11.2", "polkadot-client", "sc-client-api", + "sc-network", "sp-api", "sp-blockchain", + "sp-consensus", "sp-core", "sp-runtime", "tracing", diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 904d1026fbf..66a4cb22215 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -14,7 +14,9 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.1" tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 9f8655ab048..2d02e920950 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -13,6 +13,7 @@ use cumulus_primitives_core::{ use parking_lot::RwLock; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use sc_client_api::{blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend}; +use sc_network::NetworkService; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_core::sp_std::collections::btree_map::BTreeMap; @@ -75,16 +76,23 @@ pub trait RelayChainInterface { &[(sc_client_api::StorageKey, Option>)], >, ) -> sc_client_api::blockchain::Result>; + + fn is_major_syncing(&self) -> bool; } pub struct RelayChainDirect { pub polkadot_client: Arc, pub backend: Arc, + pub network: Arc>, } impl Clone for RelayChainDirect { fn clone(&self) -> Self { - Self { polkadot_client: self.polkadot_client.clone(), backend: self.backend.clone() } + Self { + polkadot_client: self.polkadot_client.clone(), + backend: self.backend.clone(), + network: self.network.clone(), + } } } @@ -208,11 +216,16 @@ where ) -> Result<>::State, sp_blockchain::Error> { self.backend.state_at(block_id) } + + fn is_major_syncing(&self) -> bool { + self.network.is_major_syncing() + } } pub struct RelayChainDirectBuilder { polkadot_client: polkadot_client::Client, backend: Arc, + network: Arc>, } impl RelayChainDirectBuilder { @@ -229,7 +242,11 @@ impl ExecuteWithClient for RelayChainDirectBuilder { Client: ProvideRuntimeApi + BlockchainEvents + 'static + Sync + Send, Client::Api: ParachainHost, { - Arc::new(RelayChainDirect { polkadot_client: client, backend: self.backend }) + Arc::new(RelayChainDirect { + polkadot_client: client, + backend: self.backend, + network: self.network, + }) } } @@ -313,6 +330,10 @@ impl RelayChainInterface ) -> Result<>::State, sp_blockchain::Error> { (**self).get_state_at(block_id) } + + fn is_major_syncing(&self) -> bool { + (**self).is_major_syncing() + } } impl RelayChainInterface for Arc @@ -397,12 +418,17 @@ where ) -> Result<>::State, sp_blockchain::Error> { (**self).get_state_at(block_id) } + + fn is_major_syncing(&self) -> bool { + (**self).is_major_syncing() + } } pub fn build_relay_chain_direct( client: polkadot_client::Client, backend: Arc, + network: Arc>, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend }; + let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend, network }; relay_chain_builder.build() } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 9e79929e93b..d268bdd5a60 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -249,6 +249,7 @@ where let relay_chain_interface = build_relay_chain_direct( relay_chain_full_node.client.clone(), relay_chain_full_node.backend.clone(), + relay_chain_full_node.network.clone(), ); let block_announce_validator = build_block_announce_validator( @@ -435,6 +436,7 @@ pub async fn start_parachain_node( let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), relay_chain_node.backend.clone(), + relay_chain_node.network.clone(), ); let relay_chain_interface2 = relay_chain_interface.clone(); diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index adbf67fc035..ee6d481d414 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -333,6 +333,7 @@ where let relay_chain_interface = build_relay_chain_direct( relay_chain_full_node.client.clone(), relay_chain_full_node.backend.clone(), + relay_chain_full_node.network.clone(), ); let block_announce_validator = build_block_announce_validator( @@ -512,6 +513,7 @@ where let relay_chain_interface = build_relay_chain_direct( relay_chain_full_node.client.clone(), relay_chain_full_node.backend.clone(), + relay_chain_full_node.network.clone(), ); let block_announce_validator = build_block_announce_validator( @@ -710,7 +712,7 @@ pub async fn start_rococo_parachain_node( telemetry.clone(), ); - let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone()); + let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone(), relay_chain_node.network.clone()); let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); Ok(build_aura_consensus::< @@ -832,6 +834,7 @@ pub async fn start_shell_node( let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), relay_chain_node.backend.clone(), + relay_chain_node.network.clone(), ); Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( @@ -1100,6 +1103,7 @@ where let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), relay_chain_node.backend.clone(), + relay_chain_node.network.clone(), ); let aura_consensus = BuildOnAccess::Uninitialized(Some(Box::new(move || { @@ -1181,6 +1185,7 @@ where let relay_chain_interface = build_relay_chain_direct( relay_chain_node.client.clone(), relay_chain_node.backend.clone(), + relay_chain_node.network.clone(), ); let relay_chain_consensus = diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index d97fd127b8d..b30f2a33759 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -218,6 +218,7 @@ where let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), + network: relay_chain_full_node.network.clone(), }); let block_announce_validator = BlockAnnounceValidator::new( relay_chain_interface, @@ -280,6 +281,7 @@ where let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), + network: relay_chain_full_node.network.clone(), }); let relay_chain_interface2 = relay_chain_interface.clone(); From 14ec0a1f3bead7222d755b973bc75a7771342f91 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 30 Nov 2021 10:33:49 +0100 Subject: [PATCH 22/56] Move network to RelayChainInterface --- Cargo.lock | 20 +++++++++-- client/consensus/aura/src/lib.rs | 2 +- client/consensus/relay-chain/src/lib.rs | 2 +- client/network/Cargo.toml | 4 +-- client/network/src/tests.rs | 10 ++++-- .../network/src/wait_on_relay_chain_block.rs | 24 +++++++++++-- client/relay-chain-interface/Cargo.toml | 4 ++- client/relay-chain-interface/src/lib.rs | 25 +++++++++---- parachain-template/node/src/service.rs | 14 ++------ polkadot-parachains/src/service.rs | 35 +++++-------------- test/service/src/lib.rs | 6 ++-- 11 files changed, 87 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 873eeca0972..c003cafcafd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1566,7 +1566,8 @@ dependencies = [ "futures 0.3.17", "futures-timer 3.0.2", "parity-scale-codec", - "parking_lot 0.10.2", + "parking_lot 0.11.2", + "polkadot-client", "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", @@ -1864,10 +1865,13 @@ name = "cumulus-relay-chain-interface" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "enum_dispatch", "parking_lot 0.11.2", "polkadot-client", + "polkadot-service", "sc-client-api", "sc-network", + "sc-service", "sp-api", "sp-blockchain", "sp-consensus", @@ -2244,6 +2248,18 @@ dependencies = [ "syn", ] +[[package]] +name = "enum_dispatch" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "enumflags2" version = "0.6.4" @@ -11543,7 +11559,7 @@ dependencies = [ "chrono", "lazy_static", "matchers", - "parking_lot 0.10.2", + "parking_lot 0.11.2", "regex", "serde", "serde_json", diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 2b494a2359b..b5c9e5383de 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -31,7 +31,7 @@ use cumulus_primitives_core::{ PersistedValidationData, }; use futures::lock::Mutex; -use sc_client_api::{backend::AuxStore, Backend, BlockOf}; +use sc_client_api::{backend::AuxStore, BlockOf}; use sc_consensus::BlockImport; use sc_consensus_slots::{BackoffAuthoringBlocksStrategy, SlotInfo}; use sc_telemetry::TelemetryHandle; diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 88c1e8ec5ab..f2949ac31c3 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -42,7 +42,7 @@ use cumulus_primitives_core::{ }; use cumulus_relay_chain_interface::RelayChainInterface; use parking_lot::Mutex; -use sc_client_api::Backend; + use sc_consensus::{BlockImport, BlockImportParams}; use sp_consensus::{ BlockOrigin, EnableProofRecording, Environment, ProofRecording, Proposal, Proposer, diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 9d6a4dbc89a..27d4b76d826 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -26,7 +26,7 @@ codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "deriv futures = { version = "0.3.1", features = ["compat"] } futures-timer = "3.0.2" tracing = "0.1.22" -parking_lot = "0.10.2" +parking_lot = "0.11.1" derive_more = "0.99.2" [dev-dependencies] @@ -35,9 +35,9 @@ tokio = { version = "1.10", features = ["macros"] } # Cumulus deps cumulus-test-service = { path = "../../test/service" } cumulus-primitives-core = { path = "../../primitives/core" } - # Polkadot deps polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } # substrate deps sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index e14b805d474..e7d7151c182 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -40,7 +40,7 @@ use sp_core::H256; use sp_keyring::Sr25519Keyring; use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr}; use sp_runtime::{OpaqueExtrinsic, RuntimeAppPublic}; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, sync::Mutex as StdMutex}; fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) { let error = *error @@ -67,8 +67,12 @@ impl SyncOracle for DummyCollatorNetwork { fn make_validator_and_api( ) -> (BlockAnnounceValidator>, Arc) { let api = Arc::new(TestApi::new()); - let relay_chain_interface = - RelayChainDirect { polkadot_client: api.clone(), backend: api.relay_backend.clone() }; + let network: Box = Box::new(DummyCollatorNetwork {}); + let relay_chain_interface = RelayChainDirect { + polkadot_client: api.clone(), + backend: api.relay_backend.clone(), + network: Arc::new(StdMutex::new(network)), + }; ( BlockAnnounceValidator::new( relay_chain_interface, diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 3dfeb105459..33e5bc74707 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -20,6 +20,7 @@ use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; use sc_client_api::blockchain::{self, BlockStatus}; +use sp_consensus::SyncOracle; use sp_runtime::generic::BlockId; use std::time::Duration; @@ -121,9 +122,23 @@ where } } +struct DummyNetwork {} + +impl SyncOracle for DummyNetwork { + fn is_major_syncing(&mut self) -> bool { + todo!() + } + + fn is_offline(&mut self) -> bool { + todo!() + } +} + #[cfg(test)] mod tests { - use super::*; + use std::sync::Mutex; + +use super::*; use cumulus_relay_chain_interface::RelayChainDirect; use polkadot_test_client::{ @@ -145,11 +160,16 @@ mod tests { let block_builder = client.init_polkadot_block_builder(); let block = block_builder.build().expect("Finalizes the block").block; + let dummy_network: Box = Box::new(DummyNetwork {}); ( client.clone(), block, - RelayChainDirect { polkadot_client: client, backend: backend.clone() }, + RelayChainDirect { + polkadot_client: client, + backend: backend.clone(), + network: Arc::new(Mutex::new(dummy_network)), + }, ) } diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 66a4cb22215..153a75d755f 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-primitives-core = { path = "../../primitives/core" } @@ -17,6 +18,7 @@ sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "mas sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.1" - +enum_dispatch = "0.3.7" tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 2d02e920950..9275f9682a8 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -13,11 +13,12 @@ use cumulus_primitives_core::{ use parking_lot::RwLock; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use sc_client_api::{blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend}; -use sc_network::NetworkService; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; +use sp_consensus::SyncOracle; use sp_core::sp_std::collections::btree_map::BTreeMap; +use std::sync::Mutex; -const LOG_TARGET: &str = "cumulus-collator"; +const LOG_TARGET: &str = "relay-chain-interface"; pub trait RelayChainInterface { fn get_state_at( @@ -83,7 +84,7 @@ pub trait RelayChainInterface { pub struct RelayChainDirect { pub polkadot_client: Arc, pub backend: Arc, - pub network: Arc>, + pub network: Arc>>, } impl Clone for RelayChainDirect { @@ -218,14 +219,15 @@ where } fn is_major_syncing(&self) -> bool { - self.network.is_major_syncing() + let mut network = self.network.lock().unwrap(); + network.is_major_syncing() } } pub struct RelayChainDirectBuilder { polkadot_client: polkadot_client::Client, backend: Arc, - network: Arc>, + network: Arc>>, } impl RelayChainDirectBuilder { @@ -427,8 +429,19 @@ where pub fn build_relay_chain_direct( client: polkadot_client::Client, backend: Arc, - network: Arc>, + network: Arc>>, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend, network }; relay_chain_builder.build() } + +pub fn build_relay_chain_direct_from_full( + full_node: &polkadot_service::NewFull, +) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { + let client = full_node.client.clone(); + let backend = full_node.backend.clone(); + let test: Box = Box::new(full_node.network.clone()); + let network = Arc::new(Mutex::new(test)); + let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend, network }; + relay_chain_builder.build() +} diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index d268bdd5a60..86c9c8bbe04 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,7 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; // Substrate Imports -use cumulus_relay_chain_interface::build_relay_chain_direct; +use cumulus_relay_chain_interface::build_relay_chain_direct_from_full; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -246,11 +246,7 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_full_node.client.clone(), - relay_chain_full_node.backend.clone(), - relay_chain_full_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); let block_announce_validator = build_block_announce_validator( relay_chain_interface, @@ -433,11 +429,7 @@ pub async fn start_parachain_node( telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_node.client.clone(), - relay_chain_node.backend.clone(), - relay_chain_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index ee6d481d414..223c3f57c39 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -28,7 +28,7 @@ use cumulus_primitives_core::{ relay_chain::v1::{Hash as PHash, PersistedValidationData}, ParaId, }; -use cumulus_relay_chain_interface::build_relay_chain_direct; +use cumulus_relay_chain_interface::build_relay_chain_direct_from_full; use polkadot_service::NativeExecutionDispatch; use crate::rpc; @@ -330,11 +330,7 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_full_node.client.clone(), - relay_chain_full_node.backend.clone(), - relay_chain_full_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); let block_announce_validator = build_block_announce_validator( relay_chain_interface, @@ -510,11 +506,7 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_full_node.client.clone(), - relay_chain_full_node.backend.clone(), - relay_chain_full_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); let block_announce_validator = build_block_announce_validator( relay_chain_interface, @@ -712,7 +704,8 @@ pub async fn start_rococo_parachain_node( telemetry.clone(), ); - let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone(), relay_chain_node.network.clone()); + // let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone(), ); + let relay_chain_direct = build_relay_chain_direct_from_full(&relay_chain_node); let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); Ok(build_aura_consensus::< @@ -831,11 +824,7 @@ pub async fn start_shell_node( telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_node.client.clone(), - relay_chain_node.backend.clone(), - relay_chain_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { @@ -1100,11 +1089,7 @@ where let telemetry2 = telemetry.clone(); let prometheus_registry2 = prometheus_registry.map(|r| (*r).clone()); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_node.client.clone(), - relay_chain_node.backend.clone(), - relay_chain_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); let aura_consensus = BuildOnAccess::Uninitialized(Some(Box::new(move || { let slot_duration = @@ -1182,11 +1167,7 @@ where telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct( - relay_chain_node.client.clone(), - relay_chain_node.backend.clone(), - relay_chain_node.network.clone(), - ); + let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); let relay_chain_consensus = cumulus_client_consensus_relay_chain::build_relay_chain_consensus( diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index b30f2a33759..17c5e3816fd 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -50,7 +50,7 @@ use sp_keyring::Sr25519Keyring; use sp_runtime::{codec::Encode, generic, traits::BlakeTwo256}; use sp_state_machine::BasicExternalities; use sp_trie::PrefixedMemoryDB; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use substrate_test_client::{ BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput, }; @@ -218,7 +218,7 @@ where let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), - network: relay_chain_full_node.network.clone(), + network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), }); let block_announce_validator = BlockAnnounceValidator::new( relay_chain_interface, @@ -281,7 +281,7 @@ where let relay_chain_interface = Arc::new(RelayChainDirect { polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), - network: relay_chain_full_node.network.clone(), + network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), }); let relay_chain_interface2 = relay_chain_interface.clone(); From 5e42e7772d40299839ed10445433def0e43d24cc Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 30 Nov 2021 12:13:45 +0100 Subject: [PATCH 23/56] Remove TestApi and add DummyRelayChainInterface --- client/network/src/tests.rs | 324 ++++++++++++++++-------------------- 1 file changed, 145 insertions(+), 179 deletions(-) diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index e7d7151c182..364d1a4350d 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -15,32 +15,28 @@ // along with Polkadot. If not, see . use super::*; -use cumulus_primitives_core::relay_chain::BlakeTwo256; -use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; -use parking_lot::Mutex; +use parking_lot::{Mutex, RwLock}; +use polkadot_client::FullBackend; use polkadot_node_primitives::{SignedFullStatement, Statement}; use polkadot_primitives::v1::{ - Block as PBlock, BlockNumber, CandidateCommitments, CandidateDescriptor, CandidateEvent, - CommittedCandidateReceipt, CoreState, GroupRotationInfo, Hash as PHash, HeadData, Id as ParaId, - InboundDownwardMessage, InboundHrmpMessage, OccupiedCoreAssumption, ParachainHost, - PersistedValidationData, ScrapedOnChainVotes, SessionIndex, SessionInfo, SigningContext, - ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, + Block as PBlock, CandidateCommitments, CandidateDescriptor, CommittedCandidateReceipt, + Hash as PHash, HeadData, Id as ParaId, InboundDownwardMessage, InboundHrmpMessage, + OccupiedCoreAssumption, PersistedValidationData, SessionIndex, SigningContext, ValidatorId, }; use polkadot_test_client::{ Client as PClient, ClientBlockImportExt, DefaultTestClientBuilderExt, FullBackend as PBackend, InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; -use sc_client_api::BlockchainEvents; -use sp_api::{ApiRef, ProvideRuntimeApi}; +use sc_client_api::{Backend, BlockchainEvents}; use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; use sp_core::H256; use sp_keyring::Sr25519Keyring; use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr}; -use sp_runtime::{OpaqueExtrinsic, RuntimeAppPublic}; -use std::{collections::BTreeMap, sync::Mutex as StdMutex}; +use sp_runtime::RuntimeAppPublic; +use std::collections::BTreeMap; fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) { let error = *error @@ -64,22 +60,143 @@ impl SyncOracle for DummyCollatorNetwork { } } +#[derive(Clone)] +struct DummyRelayChainInterface { + data: Arc>, + relay_client: Arc, + relay_backend: Arc, +} + +impl DummyRelayChainInterface { + fn new() -> Self { + let builder = TestClientBuilder::new(); + let relay_backend = builder.backend(); + + Self { + data: Arc::new(Mutex::new(ApiData { + validators: vec![Sr25519Keyring::Alice.public().into()], + has_pending_availability: false, + })), + relay_client: Arc::new(builder.build()), + relay_backend, + } + } +} + +impl RelayChainInterface for DummyRelayChainInterface { + fn get_state_at( + &self, + _: cumulus_primitives_core::relay_chain::BlockId, + ) -> Result<>::State, sp_blockchain::Error> { + unimplemented!("Not needed for test 1") + } + + fn get_import_lock(&self) -> &RwLock<()> { + self.relay_backend.get_import_lock() + } + + fn validators( + &self, + _: &cumulus_primitives_core::relay_chain::BlockId, + ) -> Result, sp_api::ApiError> { + Ok(self.data.lock().validators.clone()) + } + + fn block_status( + &self, + block_id: cumulus_primitives_core::relay_chain::BlockId, + ) -> Result { + self.relay_backend.blockchain().status(block_id) + } + + fn best_block_hash(&self) -> PHash { + self.relay_backend.blockchain().info().best_hash + } + + fn retrieve_dmq_contents(&self, _: ParaId, _: PHash) -> Option> { + unimplemented!("Not needed for test 5") + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + _: ParaId, + _: PHash, + ) -> Option>> { + Some(BTreeMap::new()) + } + + fn persisted_validation_data( + &self, + _: &cumulus_primitives_core::relay_chain::BlockId, + _: ParaId, + _: OccupiedCoreAssumption, + ) -> Result, sp_api::ApiError> { + Ok(Some(PersistedValidationData { + parent_head: HeadData(default_header().encode()), + ..Default::default() + })) + } + + fn candidate_pending_availability( + &self, + _: &cumulus_primitives_core::relay_chain::BlockId, + _: ParaId, + ) -> Result, sp_api::ApiError> { + if self.data.lock().has_pending_availability { + Ok(Some(CommittedCandidateReceipt { + descriptor: CandidateDescriptor { + para_head: polkadot_parachain::primitives::HeadData(default_header().encode()) + .hash(), + ..Default::default() + }, + ..Default::default() + })) + } else { + Ok(None) + } + } + + fn session_index_for_child( + &self, + _: &cumulus_primitives_core::relay_chain::BlockId, + ) -> Result { + Ok(0) + } + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + self.relay_client.import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + self.relay_client.finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result> { + self.relay_client + .storage_changes_notification_stream(filter_keys, child_filter_keys) + } + + fn is_major_syncing(&self) -> bool { + false + } +} + fn make_validator_and_api( -) -> (BlockAnnounceValidator>, Arc) { - let api = Arc::new(TestApi::new()); - let network: Box = Box::new(DummyCollatorNetwork {}); - let relay_chain_interface = RelayChainDirect { - polkadot_client: api.clone(), - backend: api.relay_backend.clone(), - network: Arc::new(StdMutex::new(network)), - }; +) -> (BlockAnnounceValidator>, Arc) { + let relay_chain_interface = Arc::new(DummyRelayChainInterface::new()); ( BlockAnnounceValidator::new( - relay_chain_interface, + relay_chain_interface.clone(), ParaId::from(56), Box::new(DummyCollatorNetwork), ), - api, + relay_chain_interface, ) } @@ -95,7 +212,7 @@ fn default_header() -> Header { /// Same as [`make_gossip_message_and_header`], but using the genesis header as relay parent. async fn make_gossip_message_and_header_using_genesis( - api: Arc, + api: Arc, validator_index: u32, ) -> (CollationSecondedSignal, Header) { let relay_parent = api.relay_client.hash(0).ok().flatten().expect("Genesis hash exists"); @@ -104,7 +221,7 @@ async fn make_gossip_message_and_header_using_genesis( } async fn make_gossip_message_and_header( - api: Arc, + relay_chain_interface: Arc, relay_parent: H256, validator_index: u32, ) -> (CollationSecondedSignal, Header) { @@ -115,8 +232,9 @@ async fn make_gossip_message_and_header( Some(&Sr25519Keyring::Alice.to_seed()), ) .unwrap(); - let session_index = - api.runtime_api().session_index_for_child(&BlockId::Hash(relay_parent)).unwrap(); + let session_index = relay_chain_interface + .session_index_for_child(&BlockId::Hash(relay_parent)) + .unwrap(); let signing_context = SigningContext { parent_hash: relay_parent, session_index }; let header = default_header(); @@ -291,8 +409,7 @@ fn check_statement_seconded() { Some(&Sr25519Keyring::Alice.to_seed()), ) .unwrap(); - let session_index = - api.runtime_api().session_index_for_child(&BlockId::Hash(relay_parent)).unwrap(); + let session_index = api.session_index_for_child(&BlockId::Hash(relay_parent)).unwrap(); let signing_context = SigningContext { parent_hash: relay_parent, session_index }; let statement = Statement::Valid(Default::default()); @@ -383,154 +500,3 @@ struct ApiData { validators: Vec, has_pending_availability: bool, } - -struct TestApi { - data: Arc>, - relay_client: Arc, - relay_backend: Arc, -} - -impl TestApi { - fn new() -> Self { - let builder = TestClientBuilder::new(); - let relay_backend = builder.backend(); - - Self { - data: Arc::new(Mutex::new(ApiData { - validators: vec![Sr25519Keyring::Alice.public().into()], - has_pending_availability: false, - })), - relay_client: Arc::new(builder.build()), - relay_backend, - } - } -} - -#[derive(Default)] -struct RuntimeApi { - data: Arc>, -} - -impl ProvideRuntimeApi for TestApi { - type Api = RuntimeApi; - - fn runtime_api<'a>(&'a self) -> ApiRef<'a, Self::Api> { - RuntimeApi { data: self.data.clone() }.into() - } -} - -impl BlockchainEvents for TestApi { - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { - self.relay_client.import_notification_stream() - } - - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { - self.relay_client.finality_notification_stream() - } - - fn storage_changes_notification_stream( - &self, - filter_keys: Option<&[sc_client_api::StorageKey]>, - child_filter_keys: Option< - &[(sc_client_api::StorageKey, Option>)], - >, - ) -> sp_blockchain::Result< - sc_client_api::StorageEventStream< - , - OpaqueExtrinsic, - > as BlockT>::Hash, - >, - > { - self.relay_client - .storage_changes_notification_stream(filter_keys, child_filter_keys) - } -} - -sp_api::mock_impl_runtime_apis! { - impl ParachainHost for RuntimeApi { - fn validators(&self) -> Vec { - self.data.lock().validators.clone() - } - - fn validator_groups(&self) -> (Vec>, GroupRotationInfo) { - (Vec::new(), GroupRotationInfo { session_start_block: 0, group_rotation_frequency: 0, now: 0 }) - } - - fn availability_cores(&self) -> Vec> { - Vec::new() - } - - fn persisted_validation_data( - &self, - _: ParaId, - _: OccupiedCoreAssumption, - ) -> Option> { - Some(PersistedValidationData { - parent_head: HeadData(default_header().encode()), - ..Default::default() - }) - } - - fn session_index_for_child(&self) -> SessionIndex { - 0 - } - - fn validation_code(&self, _: ParaId, _: OccupiedCoreAssumption) -> Option { - None - } - - fn candidate_pending_availability(&self, _: ParaId) -> Option> { - if self.data.lock().has_pending_availability { - Some(CommittedCandidateReceipt { - descriptor: CandidateDescriptor { - para_head: polkadot_parachain::primitives::HeadData( - default_header().encode(), - ).hash(), - ..Default::default() - }, - ..Default::default() - }) - } else { - None - } - } - - fn candidate_events(&self) -> Vec> { - Vec::new() - } - - fn session_info(_: SessionIndex) -> Option { - None - } - - fn check_validation_outputs(_: ParaId, _: CandidateCommitments) -> bool { - false - } - - fn dmq_contents(_: ParaId) -> Vec> { - Vec::new() - } - - fn inbound_hrmp_channels_contents( - _: ParaId, - ) -> BTreeMap>> { - BTreeMap::new() - } - - fn assumed_validation_data( - _: ParaId, - _: Hash, - ) -> Option<(PersistedValidationData, ValidationCodeHash)> { - None - } - - fn validation_code_by_hash(_: ValidationCodeHash) -> Option { - None - } - - fn on_chain_votes() -> Option> { - None - } - } -} From 06815eef34b742b474cf0945cdb1c65faff01bbd Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 30 Nov 2021 13:56:31 +0100 Subject: [PATCH 24/56] Remove network from BlockAnnounceValidator --- client/network/src/lib.rs | 34 +++++-------------- client/network/src/tests.rs | 19 +---------- .../network/src/wait_on_relay_chain_block.rs | 2 +- parachain-template/node/src/service.rs | 1 - polkadot-parachains/src/service.rs | 4 +-- test/service/src/lib.rs | 6 +--- 6 files changed, 12 insertions(+), 54 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 56eb76ddbed..7c9a677abff 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -20,9 +20,8 @@ //! that use the relay chain provided consensus. See [`BlockAnnounceValidator`] //! and [`WaitToAnnounce`] for more information about this implementation. -use sp_consensus::{ - block_validation::{BlockAnnounceValidator as BlockAnnounceValidatorT, Validation}, - SyncOracle, +use sp_consensus::block_validation::{ + BlockAnnounceValidator as BlockAnnounceValidatorT, Validation, }; use sp_core::traits::SpawnNamed; use sp_runtime::{ @@ -227,7 +226,6 @@ pub struct BlockAnnounceValidator { phantom: PhantomData, relay_chain_interface: RCInterface, para_id: ParaId, - relay_chain_sync_oracle: Box, wait_on_relay_chain_block: WaitOnRelayChainBlock, } @@ -236,16 +234,11 @@ where RCInterface: Clone, { /// Create a new [`BlockAnnounceValidator`]. - pub fn new( - relay_chain_interface: RCInterface, - para_id: ParaId, - relay_chain_sync_oracle: Box, - ) -> Self { + pub fn new(relay_chain_interface: RCInterface, para_id: ParaId) -> Self { Self { phantom: Default::default(), relay_chain_interface: relay_chain_interface.clone(), para_id, - relay_chain_sync_oracle, wait_on_relay_chain_block: WaitOnRelayChainBlock::new(relay_chain_interface), } } @@ -341,7 +334,7 @@ where header: &Block::Header, mut data: &[u8], ) -> Pin> + Send>> { - if self.relay_chain_sync_oracle.is_major_syncing() { + if self.relay_chain_interface.is_major_syncing() { return ready(Ok(Validation::Success { is_new_best: false })).boxed() } @@ -391,13 +384,11 @@ where pub fn build_block_announce_validator( relay_chain_interface: RCInterface, para_id: ParaId, - relay_chain_sync_oracle: Box, ) -> Box + Send> where RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - BlockAnnounceValidatorBuilder::new(relay_chain_interface, para_id, relay_chain_sync_oracle) - .build() + BlockAnnounceValidatorBuilder::new(relay_chain_interface, para_id).build() } /// Block announce validator builder. @@ -407,7 +398,6 @@ struct BlockAnnounceValidatorBuilder { phantom: PhantomData, relay_chain_interface: RCInterface, para_id: ParaId, - relay_chain_sync_oracle: Box, } impl BlockAnnounceValidatorBuilder @@ -415,21 +405,13 @@ where RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { /// Create a new instance of the builder. - fn new( - relay_chain_interface: RCInterface, - para_id: ParaId, - relay_chain_sync_oracle: Box, - ) -> Self { - Self { relay_chain_interface, para_id, relay_chain_sync_oracle, phantom: PhantomData } + fn new(relay_chain_interface: RCInterface, para_id: ParaId) -> Self { + Self { relay_chain_interface, para_id, phantom: PhantomData } } /// Build the block announce validator. fn build(self) -> Box + Send> { - Box::new(BlockAnnounceValidator::new( - self.relay_chain_interface.clone(), - self.para_id, - self.relay_chain_sync_oracle, - )) + Box::new(BlockAnnounceValidator::new(self.relay_chain_interface.clone(), self.para_id)) } } diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 364d1a4350d..9b68d910764 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -47,19 +47,6 @@ fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceErro } } -#[derive(Clone)] -struct DummyCollatorNetwork; - -impl SyncOracle for DummyCollatorNetwork { - fn is_major_syncing(&mut self) -> bool { - false - } - - fn is_offline(&mut self) -> bool { - unimplemented!("Not required in tests") - } -} - #[derive(Clone)] struct DummyRelayChainInterface { data: Arc>, @@ -191,11 +178,7 @@ fn make_validator_and_api( ) -> (BlockAnnounceValidator>, Arc) { let relay_chain_interface = Arc::new(DummyRelayChainInterface::new()); ( - BlockAnnounceValidator::new( - relay_chain_interface.clone(), - ParaId::from(56), - Box::new(DummyCollatorNetwork), - ), + BlockAnnounceValidator::new(relay_chain_interface.clone(), ParaId::from(56)), relay_chain_interface, ) } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 33e5bc74707..dc160b8826a 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -138,7 +138,7 @@ impl SyncOracle for DummyNetwork { mod tests { use std::sync::Mutex; -use super::*; + use super::*; use cumulus_relay_chain_interface::RelayChainDirect; use polkadot_test_client::{ diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 86c9c8bbe04..7df66acca20 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -251,7 +251,6 @@ where let block_announce_validator = build_block_announce_validator( relay_chain_interface, id, - Box::new(relay_chain_full_node.network.clone()), ); let force_authoring = parachain_config.force_authoring; diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 223c3f57c39..961e9e989ec 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -334,8 +334,7 @@ where let block_announce_validator = build_block_announce_validator( relay_chain_interface, - id, - Box::new(relay_chain_full_node.network.clone()), + id ); let force_authoring = parachain_config.force_authoring; @@ -511,7 +510,6 @@ where let block_announce_validator = build_block_announce_validator( relay_chain_interface, id, - Box::new(relay_chain_full_node.network.clone()), ); let force_authoring = parachain_config.force_authoring; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 17c5e3816fd..068089f7a22 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -220,11 +220,7 @@ where backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), }); - let block_announce_validator = BlockAnnounceValidator::new( - relay_chain_interface, - para_id, - Box::new(relay_chain_full_node.network.clone()), - ); + let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface, para_id); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; let prometheus_registry = parachain_config.prometheus_registry().cloned(); From ce860cf623f31d2bdc70781d47372f861c130e63 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 30 Nov 2021 17:25:57 +0100 Subject: [PATCH 25/56] Remove references to full node from service.rs --- client/collator/src/lib.rs | 3 +- parachain-template/node/src/service.rs | 5 +--- polkadot-parachains/src/service.rs | 39 +++++++++----------------- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index cae42c4f50f..2ae43a5e05d 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -18,8 +18,7 @@ use cumulus_client_network::WaitToAnnounce; use cumulus_primitives_core::{ - relay_chain::{Hash as PHash}, - ParachainBlockData, PersistedValidationData, + relay_chain::Hash as PHash, ParachainBlockData, PersistedValidationData, }; use sc_client_api::BlockBackend; diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 7df66acca20..29d820e16f9 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -248,10 +248,7 @@ where let backend = params.backend.clone(); let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); - let block_announce_validator = build_block_announce_validator( - relay_chain_interface, - id, - ); + let block_announce_validator = build_block_announce_validator(relay_chain_interface, id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 961e9e989ec..37569139629 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -25,10 +25,10 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::{ - relay_chain::v1::{Hash as PHash, PersistedValidationData}, + relay_chain::v1::{Block as PBlock, Hash as PHash, PersistedValidationData}, ParaId, }; -use cumulus_relay_chain_interface::build_relay_chain_direct_from_full; +use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainInterface}; use polkadot_service::NativeExecutionDispatch; use crate::rpc; @@ -332,10 +332,7 @@ where let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); - let block_announce_validator = build_block_announce_validator( - relay_chain_interface, - id - ); + let block_announce_validator = build_block_announce_validator(relay_chain_interface, id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); @@ -475,7 +472,7 @@ where Option<&Registry>, Option, &TaskManager, - &polkadot_service::NewFull, + Arc + Sync + Send>, Arc< sc_transaction_pool::FullPool< Block, @@ -507,10 +504,8 @@ where let backend = params.backend.clone(); let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); - let block_announce_validator = build_block_announce_validator( - relay_chain_interface, - id, - ); + let block_announce_validator = + build_block_announce_validator(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); @@ -568,7 +563,7 @@ where prometheus_registry.as_ref(), telemetry.as_ref().map(|t| t.handle()), &task_manager, - &relay_chain_full_node, + relay_chain_interface.clone(), transaction_pool, network, params.keystore_container.sync_keystore(), @@ -687,7 +682,7 @@ pub async fn start_rococo_parachain_node( prometheus_registry, telemetry, task_manager, - relay_chain_node, + relay_chain_interface, transaction_pool, sync_oracle, keystore, @@ -702,9 +697,7 @@ pub async fn start_rococo_parachain_node( telemetry.clone(), ); - // let relay_chain_direct = build_relay_chain_direct(relay_chain_node.client.clone(), relay_chain_node.backend.clone(), ); - let relay_chain_direct = build_relay_chain_direct_from_full(&relay_chain_node); - let relay_chain_direct_for_aura_consensus = relay_chain_direct.clone(); + let relay_chain_direct_for_aura_consensus = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -723,7 +716,7 @@ pub async fn start_rococo_parachain_node( let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &relay_chain_direct, + &relay_chain_interface, &validation_data, id, ); @@ -1076,7 +1069,7 @@ where prometheus_registry, telemetry, task_manager, - relay_chain_node, + relay_chain_interface, transaction_pool, sync_oracle, keystore, @@ -1086,9 +1079,7 @@ where let transaction_pool2 = transaction_pool.clone(); let telemetry2 = telemetry.clone(); let prometheus_registry2 = prometheus_registry.map(|r| (*r).clone()); - - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); - + let relay_chain_for_aura = relay_chain_interface.clone(); let aura_consensus = BuildOnAccess::Uninitialized(Some(Box::new(move || { let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client2).unwrap(); @@ -1101,7 +1092,7 @@ where telemetry2.clone(), ); - let relay_chain_interface2 = relay_chain_interface.clone(); + let relay_chain_interface2 = relay_chain_for_aura.clone(); build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -1120,7 +1111,7 @@ where let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &relay_chain_interface, + &relay_chain_for_aura, &validation_data, id, ); @@ -1165,8 +1156,6 @@ where telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); - let relay_chain_consensus = cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { From f8c8325071bedee4faf70f3558a64405e4c7750b Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 1 Dec 2021 16:48:20 +0100 Subject: [PATCH 26/56] Move StartCollator and StartConsensus to RelayChainInterface --- Cargo.lock | 4 + client/consensus/common/Cargo.toml | 2 + .../common/src/parachain_consensus.rs | 14 +- client/pov-recovery/Cargo.toml | 1 + client/pov-recovery/src/lib.rs | 25 +-- client/relay-chain-interface/Cargo.toml | 1 + client/relay-chain-interface/src/lib.rs | 69 +++++-- client/service/Cargo.toml | 1 + client/service/src/lib.rs | 193 +++++------------- parachain-template/node/src/service.rs | 22 +- polkadot-parachains/src/service.rs | 21 +- test/service/src/lib.rs | 2 + 12 files changed, 158 insertions(+), 197 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c003cafcafd..2e3a7d675c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1515,6 +1515,7 @@ name = "cumulus-client-consensus-common" version = "0.1.0" dependencies = [ "async-trait", + "cumulus-relay-chain-interface", "cumulus-test-client", "dyn-clone", "futures 0.3.17", @@ -1592,6 +1593,7 @@ name = "cumulus-client-pov-recovery" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "cumulus-relay-chain-interface", "cumulus-test-service", "futures 0.3.17", "futures-timer 3.0.2", @@ -1622,6 +1624,7 @@ dependencies = [ "cumulus-client-consensus-common", "cumulus-client-pov-recovery", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "parity-scale-codec", "parking_lot 0.10.2", "polkadot-overseer", @@ -1870,6 +1873,7 @@ dependencies = [ "polkadot-client", "polkadot-service", "sc-client-api", + "sc-consensus-babe", "sc-network", "sc-service", "sp-api", diff --git a/client/consensus/common/Cargo.toml b/client/consensus/common/Cargo.toml index 66579668260..4d784f7f6fb 100644 --- a/client/consensus/common/Cargo.toml +++ b/client/consensus/common/Cargo.toml @@ -18,6 +18,8 @@ sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } +cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } + # Other deps futures = { version = "0.3.8", features = ["compat"] } codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } diff --git a/client/consensus/common/src/parachain_consensus.rs b/client/consensus/common/src/parachain_consensus.rs index 47a5b4dbaa8..2aa493f9a88 100644 --- a/client/consensus/common/src/parachain_consensus.rs +++ b/client/consensus/common/src/parachain_consensus.rs @@ -14,11 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . +use cumulus_relay_chain_interface::RelayChainInterface; use sc_client_api::{ Backend, BlockBackend, BlockImportNotification, BlockchainEvents, Finalizer, UsageProvider, }; use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy}; -use sp_api::ProvideRuntimeApi; use sp_blockchain::{Error as ClientError, Result as ClientResult}; use sp_consensus::{BlockOrigin, BlockStatus}; use sp_runtime::{ @@ -26,9 +26,7 @@ use sp_runtime::{ traits::{Block as BlockT, Header as HeaderT}, }; -use polkadot_primitives::v1::{ - Block as PBlock, Id as ParaId, OccupiedCoreAssumption, ParachainHost, -}; +use polkadot_primitives::v1::{Block as PBlock, Id as ParaId, OccupiedCoreAssumption}; use codec::Decode; use futures::{future, select, FutureExt, Stream, StreamExt}; @@ -370,10 +368,9 @@ where } } -impl RelaychainClient for Arc +impl RelaychainClient for T where - T: sc_client_api::BlockchainEvents + ProvideRuntimeApi + 'static + Send + Sync, - >::Api: ParachainHost, + T: RelayChainInterface + Send + Sync + Clone + 'static, { type Error = ClientError; @@ -410,8 +407,7 @@ where at: &BlockId, para_id: ParaId, ) -> ClientResult>> { - self.runtime_api() - .persisted_validation_data(at, para_id, OccupiedCoreAssumption::TimedOut) + self.persisted_validation_data(at, para_id, OccupiedCoreAssumption::TimedOut) .map(|s| s.map(|s| s.parent_head.0)) .map_err(Into::into) } diff --git a/client/pov-recovery/Cargo.toml b/client/pov-recovery/Cargo.toml index b95693b1ee9..1c2c757209e 100644 --- a/client/pov-recovery/Cargo.toml +++ b/client/pov-recovery/Cargo.toml @@ -22,6 +22,7 @@ polkadot-node-subsystem = { git = "https://github.com/paritytech/polkadot", bran # Cumulus deps cumulus-primitives-core = { path = "../../primitives/core" } +cumulus-relay-chain-interface = {path = "../relay-chain-interface"} # other deps codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } diff --git a/client/pov-recovery/src/lib.rs b/client/pov-recovery/src/lib.rs index a875a5c6eb6..c3af24cec2c 100644 --- a/client/pov-recovery/src/lib.rs +++ b/client/pov-recovery/src/lib.rs @@ -44,7 +44,6 @@ use sc_client_api::{BlockBackend, BlockchainEvents, UsageProvider}; use sc_consensus::import_queue::{ImportQueue, IncomingBlock}; -use sp_api::ProvideRuntimeApi; use sp_consensus::{BlockOrigin, BlockStatus}; use sp_runtime::{ generic::BlockId, @@ -54,11 +53,12 @@ use sp_runtime::{ use polkadot_node_primitives::{AvailableData, POV_BOMB_LIMIT}; use polkadot_overseer::Handle as OverseerHandle; use polkadot_primitives::v1::{ - Block as PBlock, CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, ParachainHost, + Block as PBlock, CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, SessionIndex, }; use cumulus_primitives_core::ParachainBlockData; +use cumulus_relay_chain_interface::RelayChainInterface; use codec::Decode; use futures::{select, stream::FuturesUnordered, Future, FutureExt, Stream, StreamExt}; @@ -102,15 +102,14 @@ pub struct PoVRecovery { relay_chain_slot_duration: Duration, parachain_client: Arc, parachain_import_queue: IQ, - relay_chain_client: Arc, + relay_chain_interface: RC, para_id: ParaId, } impl PoVRecovery where PC: BlockBackend + BlockchainEvents + UsageProvider, - RC: ProvideRuntimeApi + BlockchainEvents, - RC::Api: ParachainHost, + RC: RelayChainInterface + Send + Sync + Clone, IQ: ImportQueue, { /// Create a new instance. @@ -119,7 +118,7 @@ where relay_chain_slot_duration: Duration, parachain_client: Arc, parachain_import_queue: IQ, - relay_chain_client: Arc, + relay_chain_interface: RC, para_id: ParaId, ) -> Self { Self { @@ -130,7 +129,7 @@ where waiting_for_parent: HashMap::new(), parachain_client, parachain_import_queue, - relay_chain_client, + relay_chain_interface, para_id, } } @@ -365,7 +364,7 @@ where let mut imported_blocks = self.parachain_client.import_notification_stream().fuse(); let mut finalized_blocks = self.parachain_client.finality_notification_stream().fuse(); let pending_candidates = - pending_candidates(self.relay_chain_client.clone(), self.para_id).fuse(); + pending_candidates(self.relay_chain_interface.clone(), self.para_id).fuse(); futures::pin_mut!(pending_candidates); loop { @@ -420,19 +419,17 @@ where /// Returns a stream over pending candidates for the parachain corresponding to `para_id`. fn pending_candidates( - relay_chain_client: Arc, + relay_chain_client: RC, para_id: ParaId, ) -> impl Stream where - RC: ProvideRuntimeApi + BlockchainEvents, - RC::Api: ParachainHost, + RC: RelayChainInterface + Clone, { relay_chain_client.import_notification_stream().filter_map(move |n| { - let runtime_api = relay_chain_client.runtime_api(); - let res = runtime_api + let res = relay_chain_client .candidate_pending_availability(&BlockId::hash(n.hash), para_id) .and_then(|pa| { - runtime_api + relay_chain_client .session_index_for_child(&BlockId::hash(n.hash)) .map(|v| pa.map(|pa| (pa, v))) }) diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 153a75d755f..5aee301d8bf 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -17,6 +17,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.1" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 9275f9682a8..9edaf614961 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use cumulus_primitives_core::{ relay_chain::{ @@ -12,7 +12,10 @@ use cumulus_primitives_core::{ }; use parking_lot::RwLock; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; -use sc_client_api::{blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend}; +use polkadot_service::{AuxStore, BabeApi, Handle, TaskManager}; +use sc_client_api::{ + blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, UsageProvider, +}; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_consensus::SyncOracle; use sp_core::sp_std::collections::btree_map::BTreeMap; @@ -79,12 +82,17 @@ pub trait RelayChainInterface { ) -> sc_client_api::blockchain::Result>; fn is_major_syncing(&self) -> bool; + + fn slot_duration(&self) -> Result; + + fn overseer_handle(&self) -> Option; } pub struct RelayChainDirect { pub polkadot_client: Arc, pub backend: Arc, pub network: Arc>>, + pub overseer_handle: Option, } impl Clone for RelayChainDirect { @@ -93,14 +101,15 @@ impl Clone for RelayChainDirect { polkadot_client: self.polkadot_client.clone(), backend: self.backend.clone(), network: self.network.clone(), + overseer_handle: self.overseer_handle.clone(), } } } impl RelayChainInterface for RelayChainDirect where - Client: ProvideRuntimeApi + BlockchainEvents, - Client::Api: ParachainHost, + Client: ProvideRuntimeApi + BlockchainEvents + AuxStore + UsageProvider, + Client::Api: ParachainHost + BabeApi, Block: BlockT, { fn retrieve_dmq_contents( @@ -222,12 +231,21 @@ where let mut network = self.network.lock().unwrap(); network.is_major_syncing() } + + fn slot_duration(&self) -> Result { + Ok(sc_consensus_babe::Config::get_or_compute(&*self.polkadot_client)?.slot_duration()) + } + + fn overseer_handle(&self) -> Option { + self.overseer_handle.clone() + } } pub struct RelayChainDirectBuilder { polkadot_client: polkadot_client::Client, backend: Arc, network: Arc>>, + overseer_handle: Option, } impl RelayChainDirectBuilder { @@ -241,13 +259,20 @@ impl ExecuteWithClient for RelayChainDirectBuilder { fn execute_with_client(self, client: Arc) -> Self::Output where - Client: ProvideRuntimeApi + BlockchainEvents + 'static + Sync + Send, - Client::Api: ParachainHost, + Client: ProvideRuntimeApi + + BlockchainEvents + + AuxStore + + UsageProvider + + 'static + + Sync + + Send, + Client::Api: ParachainHost + BabeApi, { Arc::new(RelayChainDirect { polkadot_client: client, backend: self.backend, network: self.network, + overseer_handle: self.overseer_handle, }) } } @@ -336,6 +361,14 @@ impl RelayChainInterface fn is_major_syncing(&self) -> bool { (**self).is_major_syncing() } + + fn slot_duration(&self) -> Result { + (**self).slot_duration() + } + + fn overseer_handle(&self) -> Option { + (**self).overseer_handle() + } } impl RelayChainInterface for Arc @@ -424,24 +457,30 @@ where fn is_major_syncing(&self) -> bool { (**self).is_major_syncing() } -} -pub fn build_relay_chain_direct( - client: polkadot_client::Client, - backend: Arc, - network: Arc>>, -) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend, network }; - relay_chain_builder.build() + fn slot_duration(&self) -> Result { + (**self).slot_duration() + } + + fn overseer_handle(&self) -> Option { + (**self).overseer_handle() + } } pub fn build_relay_chain_direct_from_full( full_node: &polkadot_service::NewFull, + task_manager: &mut TaskManager, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let client = full_node.client.clone(); let backend = full_node.backend.clone(); let test: Box = Box::new(full_node.network.clone()); let network = Arc::new(Mutex::new(test)); - let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: client, backend, network }; + let relay_chain_builder = RelayChainDirectBuilder { + polkadot_client: client, + backend, + network, + overseer_handle: full_node.overseer_handle.clone(), + }; + task_manager.add_child(full_node.task_manager); relay_chain_builder.build() } diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index de76a0d657a..442030f1c49 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" cumulus-client-consensus-common = { path = "../consensus/common" } cumulus-client-collator = { path = "../collator" } cumulus-client-pov-recovery = { path = "../pov-recovery" } +cumulus-relay-chain-interface = { path = "../relay-chain-interface" } cumulus-primitives-core = { path = "../../primitives/core" } # Substrate dependencies diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 692d73bb909..1218eeeeb3d 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -20,9 +20,9 @@ use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; -use polkadot_overseer::Handle as OverseerHandle; +use cumulus_relay_chain_interface::RelayChainInterface; use polkadot_primitives::v1::{Block as PBlock, CollatorPair}; -use polkadot_service::{AbstractClient, Client as PClient, ClientHandle, RuntimeApiCollection}; +use polkadot_service::Client as PClient; use sc_client_api::{ Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider, }; @@ -37,10 +37,10 @@ use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; use sp_core::{traits::SpawnNamed, Pair}; use sp_runtime::{ - traits::{BlakeTwo256, Block as BlockT, NumberFor}, + traits::{Block as BlockT, NumberFor}, Justifications, }; -use std::{marker::PhantomData, ops::Deref, sync::Arc}; +use std::{ops::Deref, sync::Arc}; pub mod genesis; @@ -61,16 +61,17 @@ impl Deref for RFullNode { } /// Parameters given to [`start_collator`]. -pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, Spawner, RClient, IQ> { +pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawner, IQ> { pub block_status: Arc, pub client: Arc, pub announce_block: Arc>) + Send + Sync>, pub spawner: Spawner, pub para_id: ParaId, - pub relay_chain_full_node: RFullNode, + pub relay_chain_interface: RCInterface, pub task_manager: &'a mut TaskManager, pub parachain_consensus: Box>, pub import_queue: IQ, + pub collator_key: CollatorPair, } /// Start a collator node for a parachain. @@ -78,7 +79,7 @@ pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, Spawner, RClient, /// A collator is similar to a validator in a normal blockchain. /// It is responsible for producing blocks and sending the blocks to a /// parachain validator for validation and inclusion into the relay chain. -pub async fn start_collator<'a, Block, BS, Client, Backend, Spawner, RClient, IQ>( +pub async fn start_collator<'a, Block, BS, Client, Backend, RCInterface, Spawner, IQ>( StartCollatorParams { block_status, client, @@ -86,10 +87,11 @@ pub async fn start_collator<'a, Block, BS, Client, Backend, Spawner, RClient, IQ spawner, para_id, task_manager, - relay_chain_full_node, + relay_chain_interface, parachain_consensus, import_queue, - }: StartCollatorParams<'a, Block, BS, Client, Spawner, RClient, IQ>, + collator_key, + }: StartCollatorParams<'a, Block, BS, Client, RCInterface, Spawner, IQ>, ) -> sc_service::error::Result<()> where Block: BlockT, @@ -106,55 +108,58 @@ where Client::Api: CollectCollationInfo, for<'b> &'b Client: BlockImport, Spawner: SpawnNamed + Clone + Send + Sync + 'static, - RClient: ClientHandle, + RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, Backend: BackendT + 'static, IQ: ImportQueue + 'static, { - relay_chain_full_node.client.execute_with(StartConsensus { - para_id, - announce_block: announce_block.clone(), - client: client.clone(), - task_manager, - _phantom: PhantomData, - }); - - relay_chain_full_node.client.execute_with(StartPoVRecovery { + let consensus = cumulus_client_consensus_common::run_parachain_consensus( para_id, - client: client.clone(), - import_queue, - task_manager, - overseer_handle: relay_chain_full_node - .overseer_handle - .clone() + client.clone(), + relay_chain_interface.clone(), + announce_block.clone(), + ); + + task_manager + .spawn_essential_handle() + .spawn("cumulus-consensus", None, consensus); + + let pov_recovery = cumulus_client_pov_recovery::PoVRecovery::new( + relay_chain_interface + .overseer_handle() .ok_or_else(|| "Polkadot full node did not provide an `OverseerHandle`!")?, - _phantom: PhantomData, - })?; + relay_chain_interface.slot_duration()?, + client.clone(), + import_queue, + relay_chain_interface.clone(), + para_id, + ); + + task_manager + .spawn_essential_handle() + .spawn("cumulus-pov-recovery", None, pov_recovery.run()); cumulus_client_collator::start_collator(cumulus_client_collator::StartCollatorParams { runtime_api: client.clone(), block_status, announce_block, - overseer_handle: relay_chain_full_node - .overseer_handle - .clone() + overseer_handle: relay_chain_interface + .overseer_handle() .ok_or_else(|| "Polkadot full node did not provide an `OverseerHandle`!")?, spawner, para_id, - key: relay_chain_full_node.collator_key.clone(), + key: collator_key, parachain_consensus, }) .await; - task_manager.add_child(relay_chain_full_node.relay_chain_full_node.task_manager); - Ok(()) } /// Parameters given to [`start_full_node`]. -pub struct StartFullNodeParams<'a, Block: BlockT, Client, PClient> { +pub struct StartFullNodeParams<'a, Block: BlockT, Client, RCInterface> { pub para_id: ParaId, pub client: Arc, - pub relay_chain_full_node: RFullNode, + pub relay_chain_interface: RCInterface, pub task_manager: &'a mut TaskManager, pub announce_block: Arc>) + Send + Sync>, } @@ -163,14 +168,14 @@ pub struct StartFullNodeParams<'a, Block: BlockT, Client, PClient> { /// /// A full node will only sync the given parachain and will follow the /// tip of the chain. -pub fn start_full_node( +pub fn start_full_node( StartFullNodeParams { client, announce_block, task_manager, - relay_chain_full_node, + relay_chain_interface, para_id, - }: StartFullNodeParams, + }: StartFullNodeParams, ) -> sc_service::error::Result<()> where Block: BlockT, @@ -183,116 +188,22 @@ where + 'static, for<'a> &'a Client: BlockImport, Backend: BackendT + 'static, - PClient: ClientHandle, + RCInterface: RelayChainInterface + Sync + Send + Clone + 'static, { - relay_chain_full_node.client.execute_with(StartConsensus { - announce_block, + let consensus = cumulus_client_consensus_common::run_parachain_consensus( para_id, - client, - task_manager, - _phantom: PhantomData, - }); + client.clone(), + relay_chain_interface.clone(), + announce_block, + ); - task_manager.add_child(relay_chain_full_node.relay_chain_full_node.task_manager); + task_manager + .spawn_essential_handle() + .spawn("cumulus-consensus", None, consensus); Ok(()) } -struct StartConsensus<'a, Block: BlockT, Client, Backend> { - para_id: ParaId, - announce_block: Arc>) + Send + Sync>, - client: Arc, - task_manager: &'a mut TaskManager, - _phantom: PhantomData, -} - -impl<'a, Block, Client, Backend> polkadot_service::ExecuteWithClient - for StartConsensus<'a, Block, Client, Backend> -where - Block: BlockT, - Client: Finalizer - + UsageProvider - + Send - + Sync - + BlockBackend - + BlockchainEvents - + 'static, - for<'b> &'b Client: BlockImport, - Backend: BackendT + 'static, -{ - type Output = (); - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: sp_api::StateBackend, - PBackend: sc_client_api::Backend, - PBackend::State: sp_api::StateBackend, - Api: RuntimeApiCollection, - PClient: AbstractClient + 'static, - { - let consensus = cumulus_client_consensus_common::run_parachain_consensus( - self.para_id, - self.client.clone(), - client.clone(), - self.announce_block, - ); - - self.task_manager - .spawn_essential_handle() - .spawn("cumulus-consensus", None, consensus); - } -} - -struct StartPoVRecovery<'a, Block: BlockT, Client, IQ> { - para_id: ParaId, - client: Arc, - task_manager: &'a mut TaskManager, - overseer_handle: OverseerHandle, - import_queue: IQ, - _phantom: PhantomData, -} - -impl<'a, Block, Client, IQ> polkadot_service::ExecuteWithClient - for StartPoVRecovery<'a, Block, Client, IQ> -where - Block: BlockT, - Client: UsageProvider - + Send - + Sync - + BlockBackend - + BlockchainEvents - + 'static, - IQ: ImportQueue + 'static, -{ - type Output = sc_service::error::Result<()>; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - >::StateBackend: sp_api::StateBackend, - PBackend: sc_client_api::Backend, - PBackend::State: sp_api::StateBackend, - Api: RuntimeApiCollection, - PClient: AbstractClient + 'static, - { - let pov_recovery = cumulus_client_pov_recovery::PoVRecovery::new( - self.overseer_handle, - sc_consensus_babe::Config::get_or_compute(&*client)?.slot_duration(), - self.client, - self.import_queue, - client, - self.para_id, - ); - - self.task_manager.spawn_essential_handle().spawn( - "cumulus-pov-recovery", - None, - pov_recovery.run(), - ); - - Ok(()) - } -} - /// Prepare the parachain's node condifugration /// /// This function will disable the default announcement of Substrate for the parachain in favor diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 29d820e16f9..f1ca7a54d21 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -17,10 +17,10 @@ use cumulus_client_network::build_block_announce_validator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; -use cumulus_primitives_core::ParaId; +use cumulus_primitives_core::{relay_chain::v1::Block as PBlock, ParaId}; // Substrate Imports -use cumulus_relay_chain_interface::build_relay_chain_direct_from_full; +use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainInterface}; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -216,7 +216,7 @@ where Option<&Registry>, Option, &TaskManager, - &polkadot_service::NewFull, + Arc + Sync + Send>, Arc< sc_transaction_pool::FullPool< Block, @@ -246,15 +246,16 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + let collator_key = relay_chain_full_node.collator_key.clone(); let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); - let block_announce_validator = build_block_announce_validator(relay_chain_interface, id); + let block_announce_validator = + build_block_announce_validator(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); let prometheus_registry = parachain_config.prometheus_registry().cloned(); let transaction_pool = params.transaction_pool.clone(); - let mut task_manager = params.task_manager; let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue); let (network, system_rpc_tx, start_network) = sc_service::build_network(sc_service::BuildNetworkParams { @@ -306,7 +307,7 @@ where prometheus_registry.as_ref(), telemetry.as_ref().map(|t| t.handle()), &task_manager, - &relay_chain_full_node, + relay_chain_interface.clone(), transaction_pool, network, params.keystore_container.sync_keystore(), @@ -321,10 +322,11 @@ where announce_block, client: client.clone(), task_manager: &mut task_manager, - relay_chain_full_node, + relay_chain_interface, spawner, parachain_consensus, import_queue, + collator_key, }; start_collator(params).await?; @@ -334,7 +336,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_full_node, + relay_chain_interface: relay_chain_full_node, }; start_full_node(params)?; @@ -410,7 +412,7 @@ pub async fn start_parachain_node( prometheus_registry, telemetry, task_manager, - relay_chain_node, + relay_chain_interface, transaction_pool, sync_oracle, keystore, @@ -425,8 +427,6 @@ pub async fn start_parachain_node( telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); - let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 37569139629..d28168b52cc 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -330,15 +330,18 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); + let mut task_manager = params.task_manager; + let collator_key = relay_chain_full_node.collator_key.clone(); + let relay_chain_interface = + build_relay_chain_direct_from_full(relay_chain_full_node, &mut task_manager); - let block_announce_validator = build_block_announce_validator(relay_chain_interface, id); + let block_announce_validator = + build_block_announce_validator(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); let prometheus_registry = parachain_config.prometheus_registry().cloned(); let transaction_pool = params.transaction_pool.clone(); - let mut task_manager = params.task_manager; let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue); let (network, system_rpc_tx, start_network) = sc_service::build_network(sc_service::BuildNetworkParams { @@ -393,10 +396,11 @@ where announce_block, client: client.clone(), task_manager: &mut task_manager, - relay_chain_full_node, + relay_chain_interface, spawner, parachain_consensus, import_queue, + collator_key, }; start_collator(params).await?; @@ -406,7 +410,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_full_node, + relay_chain_interface: relay_chain_full_node, }; start_full_node(params)?; @@ -502,6 +506,8 @@ where let client = params.client.clone(); let backend = params.backend.clone(); + + let collator_key = relay_chain_full_node.collator_key.clone(); let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); let block_announce_validator = @@ -578,10 +584,11 @@ where announce_block, client: client.clone(), task_manager: &mut task_manager, - relay_chain_full_node, + relay_chain_interface: relay_chain_interface.clone(), spawner, parachain_consensus, import_queue, + collator_key, }; start_collator(params).await?; @@ -591,7 +598,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_full_node, + relay_chain_interface: relay_chain_full_node, }; start_full_node(params)?; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 068089f7a22..89aa1ce7685 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -219,6 +219,7 @@ where polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), + overseer_handle: None, }); let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface, para_id); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; @@ -278,6 +279,7 @@ where polkadot_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), + overseer_handle: None, }); let relay_chain_interface2 = relay_chain_interface.clone(); From b1b3814ebd5b55a0c8676c7d6f770890d37cfcf7 Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 1 Dec 2021 18:21:37 +0100 Subject: [PATCH 27/56] Replace more references to full_node --- client/relay-chain-interface/src/lib.rs | 2 +- parachain-template/node/src/service.rs | 8 ++++++-- polkadot-parachains/src/service.rs | 26 ++++++++++++++----------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 9edaf614961..f64b3047a9e 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -468,7 +468,7 @@ where } pub fn build_relay_chain_direct_from_full( - full_node: &polkadot_service::NewFull, + full_node: polkadot_service::NewFull, task_manager: &mut TaskManager, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { let client = full_node.client.clone(); diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index f1ca7a54d21..b6ebb86e67a 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -247,7 +247,11 @@ where let client = params.client.clone(); let backend = params.backend.clone(); let collator_key = relay_chain_full_node.collator_key.clone(); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); + let mut task_manager = params.task_manager; + let relay_chain_interface = build_relay_chain_direct_from_full( + relay_chain_full_node.relay_chain_full_node, + &mut task_manager, + ); let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); @@ -336,7 +340,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_interface: relay_chain_full_node, + relay_chain_interface, }; start_full_node(params)?; diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index d28168b52cc..7a34a23c166 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -299,7 +299,7 @@ where Option<&Registry>, Option, &TaskManager, - &polkadot_service::NewFull, + Arc + Sync + Send>, Arc< sc_transaction_pool::FullPool< Block, @@ -331,9 +331,12 @@ where let backend = params.backend.clone(); let mut task_manager = params.task_manager; + let collator_key = relay_chain_full_node.collator_key.clone(); - let relay_chain_interface = - build_relay_chain_direct_from_full(relay_chain_full_node, &mut task_manager); + let relay_chain_interface = build_relay_chain_direct_from_full( + relay_chain_full_node.relay_chain_full_node, + &mut task_manager, + ); let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); @@ -381,7 +384,7 @@ where prometheus_registry.as_ref(), telemetry.as_ref().map(|t| t.handle()), &task_manager, - &relay_chain_full_node, + relay_chain_interface.clone(), transaction_pool, network, params.keystore_container.sync_keystore(), @@ -410,7 +413,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_interface: relay_chain_full_node, + relay_chain_interface, }; start_full_node(params)?; @@ -508,7 +511,11 @@ where let backend = params.backend.clone(); let collator_key = relay_chain_full_node.collator_key.clone(); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_full_node); + let mut task_manager = params.task_manager; + let relay_chain_interface = build_relay_chain_direct_from_full( + relay_chain_full_node.relay_chain_full_node, + &mut task_manager, + ); let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); @@ -517,7 +524,6 @@ where let validator = parachain_config.role.is_authority(); let prometheus_registry = parachain_config.prometheus_registry().cloned(); let transaction_pool = params.transaction_pool.clone(); - let mut task_manager = params.task_manager; let import_queue = cumulus_client_service::SharedImportQueue::new(params.import_queue); let (network, system_rpc_tx, start_network) = sc_service::build_network(sc_service::BuildNetworkParams { @@ -598,7 +604,7 @@ where announce_block, task_manager: &mut task_manager, para_id: id, - relay_chain_interface: relay_chain_full_node, + relay_chain_interface, }; start_full_node(params)?; @@ -809,7 +815,7 @@ pub async fn start_shell_node( prometheus_registry, telemetry, task_manager, - relay_chain_node, + relay_chain_interface, transaction_pool, _, _, @@ -822,8 +828,6 @@ pub async fn start_shell_node( telemetry.clone(), ); - let relay_chain_interface = build_relay_chain_direct_from_full(&relay_chain_node); - Ok(cumulus_client_consensus_relay_chain::build_relay_chain_consensus( cumulus_client_consensus_relay_chain::BuildRelayChainConsensusParams { para_id: id, From 4b37786c0de348f6974d15ce3ff4d3fe376f6e47 Mon Sep 17 00:00:00 2001 From: skunert Date: Thu, 2 Dec 2021 14:38:01 +0100 Subject: [PATCH 28/56] Clean up and fix overseer handle --- Cargo.lock | 1 + client/network/Cargo.toml | 1 + client/network/src/tests.rs | 13 +++++-- .../network/src/wait_on_relay_chain_block.rs | 3 +- client/pov-recovery/tests/pov_recovery.rs | 2 +- client/relay-chain-interface/src/lib.rs | 34 ++++++++----------- polkadot-parachains/src/service.rs | 5 +-- test/service/src/lib.rs | 32 ++++++++--------- 8 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e3a7d675c0..2da9e61f4ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1572,6 +1572,7 @@ dependencies = [ "polkadot-node-primitives", "polkadot-parachain", "polkadot-primitives", + "polkadot-service", "polkadot-test-client", "sc-cli", "sc-client-api", diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 27d4b76d826..626a5ef3373 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -38,6 +38,7 @@ cumulus-primitives-core = { path = "../../primitives/core" } # Polkadot deps polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } # substrate deps sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 9b68d910764..9f3e3a0eda8 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -25,6 +25,7 @@ use polkadot_primitives::v1::{ Hash as PHash, HeadData, Id as ParaId, InboundDownwardMessage, InboundHrmpMessage, OccupiedCoreAssumption, PersistedValidationData, SessionIndex, SigningContext, ValidatorId, }; +use polkadot_service::Handle; use polkadot_test_client::{ Client as PClient, ClientBlockImportExt, DefaultTestClientBuilderExt, FullBackend as PBackend, InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, @@ -75,7 +76,7 @@ impl RelayChainInterface for DummyRelayChainInterface { &self, _: cumulus_primitives_core::relay_chain::BlockId, ) -> Result<>::State, sp_blockchain::Error> { - unimplemented!("Not needed for test 1") + unimplemented!("Not needed for test") } fn get_import_lock(&self) -> &RwLock<()> { @@ -101,7 +102,7 @@ impl RelayChainInterface for DummyRelayChainInterface { } fn retrieve_dmq_contents(&self, _: ParaId, _: PHash) -> Option> { - unimplemented!("Not needed for test 5") + unimplemented!("Not needed for test") } fn retrieve_all_inbound_hrmp_channel_contents( @@ -172,6 +173,14 @@ impl RelayChainInterface for DummyRelayChainInterface { fn is_major_syncing(&self) -> bool { false } + + fn slot_duration(&self) -> Result { + unimplemented!("Not needed for test") + } + + fn overseer_handle(&self) -> Option { + unimplemented!("Not needed for test") + } } fn make_validator_and_api( diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index dc160b8826a..bac77794c95 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -166,9 +166,10 @@ mod tests { client.clone(), block, RelayChainDirect { - polkadot_client: client, + full_client: client, backend: backend.clone(), network: Arc::new(Mutex::new(dummy_network)), + overseer_handle: None, }, ) } diff --git a/client/pov-recovery/tests/pov_recovery.rs b/client/pov-recovery/tests/pov_recovery.rs index c0240a48b92..65a1477b1e8 100644 --- a/client/pov-recovery/tests/pov_recovery.rs +++ b/client/pov-recovery/tests/pov_recovery.rs @@ -24,7 +24,7 @@ use std::sync::Arc; /// the parachain network, we need to recover the PoV from the relay chain. Using this PoV we can /// recover the block, import it and share it with the other nodes of the parachain network. #[substrate_test_utils::test] -#[ignore] +// #[ignore] async fn pov_recovery() { let mut builder = sc_cli::LoggerBuilder::new(""); builder.with_colors(false); diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index f64b3047a9e..6d4115e4751 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -89,7 +89,7 @@ pub trait RelayChainInterface { } pub struct RelayChainDirect { - pub polkadot_client: Arc, + pub full_client: Arc, pub backend: Arc, pub network: Arc>>, pub overseer_handle: Option, @@ -98,7 +98,7 @@ pub struct RelayChainDirect { impl Clone for RelayChainDirect { fn clone(&self) -> Self { Self { - polkadot_client: self.polkadot_client.clone(), + full_client: self.full_client.clone(), backend: self.backend.clone(), network: self.network.clone(), overseer_handle: self.overseer_handle.clone(), @@ -117,7 +117,7 @@ where para_id: ParaId, relay_parent: PHash, ) -> Option> { - self.polkadot_client + self.full_client .runtime_api() .dmq_contents_with_context( &BlockId::hash(relay_parent), @@ -140,7 +140,7 @@ where para_id: ParaId, relay_parent: PHash, ) -> Option>> { - self.polkadot_client + self.full_client .runtime_api() .inbound_hrmp_channels_contents_with_context( &BlockId::hash(relay_parent), @@ -164,7 +164,7 @@ where para_id: ParaId, occupied_core_assumption: OccupiedCoreAssumption, ) -> Result, ApiError> { - self.polkadot_client.runtime_api().persisted_validation_data( + self.full_client.runtime_api().persisted_validation_data( block_id, para_id, occupied_core_assumption, @@ -176,25 +176,23 @@ where block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError> { - self.polkadot_client - .runtime_api() - .candidate_pending_availability(block_id, para_id) + self.full_client.runtime_api().candidate_pending_availability(block_id, para_id) } fn session_index_for_child(&self, block_id: &BlockId) -> Result { - self.polkadot_client.runtime_api().session_index_for_child(block_id) + self.full_client.runtime_api().session_index_for_child(block_id) } fn validators(&self, block_id: &BlockId) -> Result, ApiError> { - self.polkadot_client.runtime_api().validators(block_id) + self.full_client.runtime_api().validators(block_id) } fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { - self.polkadot_client.import_notification_stream() + self.full_client.import_notification_stream() } fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { - self.polkadot_client.finality_notification_stream() + self.full_client.finality_notification_stream() } fn storage_changes_notification_stream( @@ -204,7 +202,7 @@ where &[(sc_client_api::StorageKey, Option>)], >, ) -> sc_client_api::blockchain::Result> { - self.polkadot_client + self.full_client .storage_changes_notification_stream(filter_keys, child_filter_keys) } @@ -233,7 +231,7 @@ where } fn slot_duration(&self) -> Result { - Ok(sc_consensus_babe::Config::get_or_compute(&*self.polkadot_client)?.slot_duration()) + Ok(sc_consensus_babe::Config::get_or_compute(&*self.full_client)?.slot_duration()) } fn overseer_handle(&self) -> Option { @@ -269,7 +267,7 @@ impl ExecuteWithClient for RelayChainDirectBuilder { Client::Api: ParachainHost + BabeApi, { Arc::new(RelayChainDirect { - polkadot_client: client, + full_client: client, backend: self.backend, network: self.network, overseer_handle: self.overseer_handle, @@ -471,13 +469,11 @@ pub fn build_relay_chain_direct_from_full( full_node: polkadot_service::NewFull, task_manager: &mut TaskManager, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let client = full_node.client.clone(); - let backend = full_node.backend.clone(); let test: Box = Box::new(full_node.network.clone()); let network = Arc::new(Mutex::new(test)); let relay_chain_builder = RelayChainDirectBuilder { - polkadot_client: client, - backend, + polkadot_client: full_node.client, + backend: full_node.backend, network, overseer_handle: full_node.overseer_handle.clone(), }; diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 7a34a23c166..4a11e8f9b09 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -710,7 +710,8 @@ pub async fn start_rococo_parachain_node( telemetry.clone(), ); - let relay_chain_direct_for_aura_consensus = relay_chain_interface.clone(); + + let relay_chain_interface_for_consensus = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, @@ -751,7 +752,7 @@ pub async fn start_rococo_parachain_node( } }, block_import: client.clone(), - relay_chain_interface: relay_chain_direct_for_aura_consensus, + relay_chain_interface: relay_chain_interface_for_consensus, para_client: client.clone(), backoff_authoring_blocks: Option::<()>::None, sync_oracle, diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 89aa1ce7685..e6135bf7a9a 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -28,7 +28,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::RelayChainDirect; +use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainDirect}; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use frame_system_rpc_runtime_api::AccountNonceApi; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; @@ -216,12 +216,13 @@ where let backend = params.backend.clone(); let relay_chain_interface = Arc::new(RelayChainDirect { - polkadot_client: relay_chain_full_node.client.clone(), + full_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), - overseer_handle: None, + overseer_handle: relay_chain_full_node.overseer_handle.clone(), }); - let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface, para_id); + let block_announce_validator = + BlockAnnounceValidator::new(relay_chain_interface.clone(), para_id); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; let prometheus_registry = parachain_config.prometheus_registry().cloned(); @@ -276,10 +277,10 @@ where None, ); let relay_chain_interface = Arc::new(RelayChainDirect { - polkadot_client: relay_chain_full_node.client.clone(), + full_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), - overseer_handle: None, + overseer_handle: relay_chain_full_node.overseer_handle.clone(), }); let relay_chain_interface2 = relay_chain_interface.clone(); @@ -313,8 +314,8 @@ where Consensus::Null => Box::new(NullConsensus), }; - let relay_chain_full_node = - relay_chain_full_node.with_client(polkadot_test_service::TestClient); + // let relay_chain_full_node = + // relay_chain_full_node.with_client(polkadot_test_service::TestClient); let params = StartCollatorParams { block_status: client.clone(), @@ -324,27 +325,22 @@ where task_manager: &mut task_manager, para_id, parachain_consensus, - relay_chain_full_node: cumulus_client_service::RFullNode { - relay_chain_full_node, - collator_key, - }, + relay_chain_interface, + collator_key, import_queue, }; start_collator(params).await?; } else { - let relay_chain_full_node = - relay_chain_full_node.with_client(polkadot_test_service::TestClient); + // let relay_chain_full_node = + // relay_chain_full_node.with_client(polkadot_test_service::TestClient); let params = StartFullNodeParams { client: client.clone(), announce_block, task_manager: &mut task_manager, para_id, - relay_chain_full_node: cumulus_client_service::RFullNode { - relay_chain_full_node, - collator_key: CollatorPair::generate().0, - }, + relay_chain_interface, }; start_full_node(params)?; From b6193978f2c6657254980c7289d143e411e8c8d2 Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 3 Dec 2021 17:11:52 +0100 Subject: [PATCH 29/56] Remove unused import, commented code --- Cargo.lock | 2 ++ test/service/src/lib.rs | 9 +++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ad9fbc0965..36aed791913 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1470,6 +1470,7 @@ dependencies = [ "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.18", "parity-scale-codec", "sc-client-api", @@ -1521,6 +1522,7 @@ dependencies = [ "async-trait", "cumulus-client-consensus-common", "cumulus-primitives-core", + "cumulus-relay-chain-interface", "futures 0.3.18", "parking_lot 0.10.2", "sc-client-api", diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index e6135bf7a9a..e101d508a28 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -28,7 +28,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainDirect}; +use cumulus_relay_chain_interface::RelayChainDirect; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use frame_system_rpc_runtime_api::AccountNonceApi; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; @@ -221,6 +221,8 @@ where network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), overseer_handle: relay_chain_full_node.overseer_handle.clone(), }); + task_manager.add_child(relay_chain_full_node.task_manager); + let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), para_id); let block_announce_validator_builder = move |_| Box::new(block_announce_validator) as Box<_>; @@ -314,9 +316,6 @@ where Consensus::Null => Box::new(NullConsensus), }; - // let relay_chain_full_node = - // relay_chain_full_node.with_client(polkadot_test_service::TestClient); - let params = StartCollatorParams { block_status: client.clone(), announce_block, @@ -332,8 +331,6 @@ where start_collator(params).await?; } else { - // let relay_chain_full_node = - // relay_chain_full_node.with_client(polkadot_test_service::TestClient); let params = StartFullNodeParams { client: client.clone(), From 84fc3c1cd565423f1d46f9b6c4da84426545c82a Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 3 Dec 2021 18:18:03 +0100 Subject: [PATCH 30/56] Move relay_chain_full instantiation to interface crate --- Cargo.lock | 3 +- client/collator/Cargo.toml | 1 - client/consensus/relay-chain/src/lib.rs | 5 +- client/relay-chain-interface/Cargo.toml | 2 + client/relay-chain-interface/src/lib.rs | 72 +++++++++++++++++++++++-- client/service/Cargo.toml | 1 - client/service/src/lib.rs | 50 ++--------------- parachain-template/node/src/service.rs | 19 +++---- polkadot-parachains/src/service.rs | 38 +++++-------- 9 files changed, 96 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36aed791913..615cf12e392 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1612,7 +1612,6 @@ dependencies = [ "parking_lot 0.10.2", "polkadot-overseer", "polkadot-primitives", - "polkadot-service", "sc-chain-spec", "sc-client-api", "sc-consensus", @@ -1859,6 +1858,8 @@ dependencies = [ "sc-consensus-babe", "sc-network", "sc-service", + "sc-telemetry", + "sc-tracing", "sp-api", "sp-blockchain", "sp-consensus", diff --git a/client/collator/Cargo.toml b/client/collator/Cargo.toml index 2fec8ddd672..d7281e87583 100644 --- a/client/collator/Cargo.toml +++ b/client/collator/Cargo.toml @@ -17,7 +17,6 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-node-subsystem = { git = "https://github.com/paritytech/polkadot", branch = "master" } -# polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } # Cumulus dependencies cumulus-client-network = { path = "../network" } diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index f2949ac31c3..27b41928671 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -266,10 +266,7 @@ where /// Relay chain consensus builder. /// -/// Builds a [`RelayChainConsensus`] for a parachain. As this requires -/// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] -/// that wraps this concrete instanace. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance. +/// Builds a [`RelayChainConsensus`] for a parachain. struct RelayChainConsensusBuilder { para_id: ParaId, _phantom: PhantomData, diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 5aee301d8bf..86a67736bd3 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -20,6 +20,8 @@ sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "mas sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.1" enum_dispatch = "0.3.7" tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 6d4115e4751..f0c1237996c 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -12,13 +12,16 @@ use cumulus_primitives_core::{ }; use parking_lot::RwLock; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; -use polkadot_service::{AuxStore, BabeApi, Handle, TaskManager}; +use polkadot_service::{ + AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, +}; use sc_client_api::{ blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, UsageProvider, }; +use sc_telemetry::TelemetryWorkerHandle; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_consensus::SyncOracle; -use sp_core::sp_std::collections::btree_map::BTreeMap; +use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; use std::sync::Mutex; const LOG_TARGET: &str = "relay-chain-interface"; @@ -239,6 +242,12 @@ where } } +/// Builder for a concrete relay chain interface, creatd from a full node. Builds +/// a [`RelayChainDirect`] to access relay chain data necessary for parachain operation. +/// +/// The builder takes a [`polkadot_client::Client`] +/// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] +/// the builder gets access to this concrete instance and instantiates a RelayChainDirect with it. pub struct RelayChainDirectBuilder { polkadot_client: polkadot_client::Client, backend: Arc, @@ -465,12 +474,67 @@ where } } +/// Build the Polkadot full node using the given `config`. +#[sc_tracing::logging::prefix_logs_with("Relaychain")] +pub fn build_polkadot_full_node( + config: Configuration, + telemetry_worker_handle: Option, +) -> Result<(NewFull, CollatorPair), polkadot_service::Error> { + let is_light = matches!(config.role, Role::Light); + if is_light { + Err(polkadot_service::Error::Sub("Light client not supported.".into())) + } else { + let collator_key = CollatorPair::generate().0; + + let relay_chain_full_node = polkadot_service::build_full( + config, + polkadot_service::IsCollator::Yes(collator_key.clone()), + None, + true, + None, + telemetry_worker_handle, + polkadot_service::RealOverseerGen, + )?; + + Ok((relay_chain_full_node, collator_key)) + } +} + +pub fn build_relay_chain_interface( + polkadot_config: Configuration, + telemetry_worker_handle: Option, + task_manager: &mut TaskManager, +) -> Result< + (Arc<(dyn RelayChainInterface + Send + Sync + 'static)>, CollatorPair), + polkadot_service::Error, +> { + let (full_node, collator_key) = + build_polkadot_full_node(polkadot_config, telemetry_worker_handle).map_err( + |e| match e { + polkadot_service::Error::Sub(x) => x, + s => format!("{}", s).into(), + }, + )?; + + let sync_oracle: Box = Box::new(full_node.network.clone()); + let network = Arc::new(Mutex::new(sync_oracle)); + let relay_chain_interface_builder = RelayChainDirectBuilder { + polkadot_client: full_node.client.clone(), + backend: full_node.backend.clone(), + network, + overseer_handle: full_node.overseer_handle.clone(), + }; + task_manager.add_child(full_node.task_manager); + + Ok((relay_chain_interface_builder.build(), collator_key)) +} + pub fn build_relay_chain_direct_from_full( full_node: polkadot_service::NewFull, task_manager: &mut TaskManager, ) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let test: Box = Box::new(full_node.network.clone()); - let network = Arc::new(Mutex::new(test)); + let sync_oracle: Box = Box::new(full_node.network.clone()); + let network = Arc::new(Mutex::new(sync_oracle)); let relay_chain_builder = RelayChainDirectBuilder { polkadot_client: full_node.client, backend: full_node.backend, diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 442030f1c49..ec2cd3ca86e 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -28,7 +28,6 @@ sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "mas # Polkadot dependencies polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } -polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } # Other deps diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 1218eeeeb3d..f95536ec7dc 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -22,7 +22,6 @@ use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; use cumulus_relay_chain_interface::RelayChainInterface; use polkadot_primitives::v1::{Block as PBlock, CollatorPair}; -use polkadot_service::Client as PClient; use sc_client_api::{ Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider, }; @@ -30,36 +29,19 @@ use sc_consensus::{ import_queue::{ImportQueue, IncomingBlock, Link, Origin}, BlockImport, }; -use sc_service::{Configuration, Role, TaskManager}; -use sc_telemetry::TelemetryWorkerHandle; +use sc_service::{Configuration, TaskManager}; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; -use sp_core::{traits::SpawnNamed, Pair}; +use sp_core::traits::SpawnNamed; use sp_runtime::{ traits::{Block as BlockT, NumberFor}, Justifications, }; -use std::{ops::Deref, sync::Arc}; +use std::sync::Arc; pub mod genesis; -/// The relay chain full node handle. -pub struct RFullNode { - /// The relay chain full node handles. - pub relay_chain_full_node: polkadot_service::NewFull, - /// The collator key used by the node. - pub collator_key: CollatorPair, -} - -impl Deref for RFullNode { - type Target = polkadot_service::NewFull; - - fn deref(&self) -> &Self::Target { - &self.relay_chain_full_node - } -} - /// Parameters given to [`start_collator`]. pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawner, IQ> { pub block_status: Arc, @@ -214,32 +196,6 @@ pub fn prepare_node_config(mut parachain_config: Configuration) -> Configuration parachain_config } -/// Build the Polkadot full node using the given `config`. -#[sc_tracing::logging::prefix_logs_with("Relaychain")] -pub fn build_polkadot_full_node( - config: Configuration, - telemetry_worker_handle: Option, -) -> Result, polkadot_service::Error> { - let is_light = matches!(config.role, Role::Light); - if is_light { - Err(polkadot_service::Error::Sub("Light client not supported.".into())) - } else { - let collator_key = CollatorPair::generate().0; - - let relay_chain_full_node = polkadot_service::build_full( - config, - polkadot_service::IsCollator::Yes(collator_key.clone()), - None, - true, - None, - telemetry_worker_handle, - polkadot_service::RealOverseerGen, - )?; - - Ok(RFullNode { relay_chain_full_node, collator_key }) - } -} - /// A shared import queue /// /// This is basically a hack until the Substrate side is implemented properly. diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index b6ebb86e67a..4b92a582c41 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -20,7 +20,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::{relay_chain::v1::Block as PBlock, ParaId}; // Substrate Imports -use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainInterface}; +use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -237,22 +237,17 @@ where let params = new_partial::(¶chain_config, build_import_queue)?; let (mut telemetry, telemetry_worker_handle) = params.other; - let relay_chain_full_node = - cumulus_client_service::build_polkadot_full_node(polkadot_config, telemetry_worker_handle) + let client = params.client.clone(); + let backend = params.backend.clone(); + let mut task_manager = params.task_manager; + + let (relay_chain_interface, collator_key) = + build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager) .map_err(|e| match e { polkadot_service::Error::Sub(x) => x, s => format!("{}", s).into(), })?; - let client = params.client.clone(); - let backend = params.backend.clone(); - let collator_key = relay_chain_full_node.collator_key.clone(); - let mut task_manager = params.task_manager; - let relay_chain_interface = build_relay_chain_direct_from_full( - relay_chain_full_node.relay_chain_full_node, - &mut task_manager, - ); - let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 4a11e8f9b09..e0d13ec6e32 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -28,7 +28,7 @@ use cumulus_primitives_core::{ relay_chain::v1::{Block as PBlock, Hash as PHash, PersistedValidationData}, ParaId, }; -use cumulus_relay_chain_interface::{build_relay_chain_direct_from_full, RelayChainInterface}; +use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; use polkadot_service::NativeExecutionDispatch; use crate::rpc; @@ -320,23 +320,17 @@ where let params = new_partial::(¶chain_config, build_import_queue)?; let (mut telemetry, telemetry_worker_handle) = params.other; - let relay_chain_full_node = - cumulus_client_service::build_polkadot_full_node(polkadot_config, telemetry_worker_handle) - .map_err(|e| match e { - polkadot_service::Error::Sub(x) => x, - s => format!("{}", s).into(), - })?; - let client = params.client.clone(); let backend = params.backend.clone(); let mut task_manager = params.task_manager; - let collator_key = relay_chain_full_node.collator_key.clone(); - let relay_chain_interface = build_relay_chain_direct_from_full( - relay_chain_full_node.relay_chain_full_node, - &mut task_manager, - ); + let (relay_chain_interface, collator_key) = + build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager) + .map_err(|e| match e { + polkadot_service::Error::Sub(x) => x, + s => format!("{}", s).into(), + })?; let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); @@ -500,22 +494,16 @@ where let params = new_partial::(¶chain_config, build_import_queue)?; let (mut telemetry, telemetry_worker_handle) = params.other; - let relay_chain_full_node = - cumulus_client_service::build_polkadot_full_node(polkadot_config, telemetry_worker_handle) - .map_err(|e| match e { - polkadot_service::Error::Sub(x) => x, - s => format!("{}", s).into(), - })?; - let client = params.client.clone(); let backend = params.backend.clone(); - let collator_key = relay_chain_full_node.collator_key.clone(); let mut task_manager = params.task_manager; - let relay_chain_interface = build_relay_chain_direct_from_full( - relay_chain_full_node.relay_chain_full_node, - &mut task_manager, - ); + let (relay_chain_interface, collator_key) = + build_relay_chain_interface(polkadot_config, telemetry_worker_handle, &mut task_manager) + .map_err(|e| match e { + polkadot_service::Error::Sub(x) => x, + s => format!("{}", s).into(), + })?; let block_announce_validator = build_block_announce_validator(relay_chain_interface.clone(), id); From 6dfa4e69f240852eee513fc96debbcee9db4ed2d Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 3 Dec 2021 18:24:44 +0100 Subject: [PATCH 31/56] Remove AuraConsensusBuilder --- client/consensus/aura/src/lib.rs | 129 ++---------------------- client/relay-chain-interface/src/lib.rs | 16 --- 2 files changed, 7 insertions(+), 138 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index b5c9e5383de..d967a5a8947 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -298,134 +298,19 @@ where P::Signature: TryFrom> + Hash + Member + Encode + Decode, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - AuraConsensusBuilder::::new( - proposer_factory, - block_import, - create_inherent_data_providers, - relay_chain_interface, + Box::new(AuraConsensus::new::( para_client, - backoff_authoring_blocks, + block_import, sync_oracle, + proposer_factory, force_authoring, + backoff_authoring_blocks, keystore, + create_inherent_data_providers, + relay_chain_interface.clone(), slot_duration, telemetry, block_proposal_slot_portion, max_block_proposal_slot_portion, - ) - .build() -} - -/// Aura consensus builder. -/// -/// Builds a [`AuraConsensus`] for a parachain. As this requires -/// a concrete relay chain client instance, the builder takes a [`polkadot_client::Client`] -/// that wraps this concrete instance. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance. -struct AuraConsensusBuilder { - _phantom: PhantomData<(Block, Error, P)>, - proposer_factory: PF, - create_inherent_data_providers: CIDP, - block_import: BI, - relay_chain_interface: RCInterface, - para_client: Arc, - backoff_authoring_blocks: Option, - sync_oracle: SO, - force_authoring: bool, - keystore: SyncCryptoStorePtr, - slot_duration: SlotDuration, - telemetry: Option, - block_proposal_slot_portion: SlotProportion, - max_block_proposal_slot_portion: Option, -} - -impl - AuraConsensusBuilder -where - Block: BlockT, - CIDP: CreateInherentDataProviders - + Send - + Sync - + 'static, - CIDP::InherentDataProviders: InherentDataProviderExt + Send, - Client: ProvideRuntimeApi - + BlockOf - + AuxStore - + HeaderBackend - + Send - + Sync - + 'static, - Client::Api: AuraApi, - BI: BlockImport> - + Send - + Sync - + 'static, - SO: SyncOracle + Send + Sync + Clone + 'static, - BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, - PF: Environment + Send + Sync + 'static, - PF::Proposer: Proposer< - Block, - Error = Error, - Transaction = sp_api::TransactionFor, - ProofRecording = EnableProofRecording, - Proof = ::Proof, - >, - Error: std::error::Error + Send + From + 'static, - P: Pair + Send + Sync, - P::Public: AppPublic + Hash + Member + Encode + Decode, - P::Signature: TryFrom> + Hash + Member + Encode + Decode, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, -{ - /// Create a new instance of the builder. - fn new( - proposer_factory: PF, - block_import: BI, - create_inherent_data_providers: CIDP, - relay_chain_interface: RCInterface, - para_client: Arc, - backoff_authoring_blocks: Option, - sync_oracle: SO, - force_authoring: bool, - keystore: SyncCryptoStorePtr, - slot_duration: SlotDuration, - telemetry: Option, - block_proposal_slot_portion: SlotProportion, - max_block_proposal_slot_portion: Option, - ) -> Self { - Self { - _phantom: PhantomData, - proposer_factory, - block_import, - create_inherent_data_providers, - relay_chain_interface, - para_client, - backoff_authoring_blocks, - sync_oracle, - force_authoring, - keystore, - slot_duration, - telemetry, - block_proposal_slot_portion, - max_block_proposal_slot_portion, - } - } - - /// Build the relay chain consensus. - fn build(self) -> Box> { - Box::new(AuraConsensus::new::( - self.para_client, - self.block_import, - self.sync_oracle, - self.proposer_factory, - self.force_authoring, - self.backoff_authoring_blocks, - self.keystore, - self.create_inherent_data_providers, - self.relay_chain_interface.clone(), - self.slot_duration, - self.telemetry, - self.block_proposal_slot_portion, - self.max_block_proposal_slot_portion, - )) - } + )) } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index f0c1237996c..4a202ec3d36 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -528,19 +528,3 @@ pub fn build_relay_chain_interface( Ok((relay_chain_interface_builder.build(), collator_key)) } - -pub fn build_relay_chain_direct_from_full( - full_node: polkadot_service::NewFull, - task_manager: &mut TaskManager, -) -> Arc<(dyn RelayChainInterface + Send + Sync + 'static)> { - let sync_oracle: Box = Box::new(full_node.network.clone()); - let network = Arc::new(Mutex::new(sync_oracle)); - let relay_chain_builder = RelayChainDirectBuilder { - polkadot_client: full_node.client, - backend: full_node.backend, - network, - overseer_handle: full_node.overseer_handle.clone(), - }; - task_manager.add_child(full_node.task_manager); - relay_chain_builder.build() -} From ada4b065cc90a1592d1adc64cd69ca2bd068ef19 Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 3 Dec 2021 19:23:52 +0100 Subject: [PATCH 32/56] Remove duplicate relay-chain-interface from test service --- client/consensus/aura/src/lib.rs | 2 +- client/consensus/relay-chain/src/lib.rs | 63 ++----------------------- test/service/src/lib.rs | 13 ++--- 3 files changed, 7 insertions(+), 71 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index d967a5a8947..6fb582606f8 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -46,7 +46,7 @@ use sp_core::crypto::Pair; use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider}; use sp_keystore::SyncCryptoStorePtr; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Member, NumberFor}; -use std::{convert::TryFrom, hash::Hash, marker::PhantomData, sync::Arc}; +use std::{convert::TryFrom, hash::Hash, sync::Arc}; mod import_queue; diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 27b41928671..1cf9249b682 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -254,68 +254,11 @@ where CIDP: CreateInherentDataProviders + 'static, RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, { - RelayChainConsensusBuilder::new( + Box::new(RelayChainConsensus::new( para_id, proposer_factory, - block_import, create_inherent_data_providers, + block_import, relay_chain_interface, - ) - .build() -} - -/// Relay chain consensus builder. -/// -/// Builds a [`RelayChainConsensus`] for a parachain. -struct RelayChainConsensusBuilder { - para_id: ParaId, - _phantom: PhantomData, - proposer_factory: PF, - create_inherent_data_providers: CIDP, - block_import: BI, - relay_chain_interface: RCInterface, -} - -impl RelayChainConsensusBuilder -where - Block: BlockT, - PF: Environment + Send + Sync + 'static, - PF::Proposer: Proposer< - Block, - Transaction = BI::Transaction, - ProofRecording = EnableProofRecording, - Proof = ::Proof, - >, - BI: BlockImport + Send + Sync + 'static, - CIDP: CreateInherentDataProviders + 'static, - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, -{ - /// Create a new instance of the builder. - fn new( - para_id: ParaId, - proposer_factory: PF, - block_import: BI, - create_inherent_data_providers: CIDP, - relay_chain_interface: RCInterface, - ) -> Self { - Self { - para_id, - _phantom: PhantomData, - proposer_factory, - block_import, - create_inherent_data_providers, - relay_chain_interface, - } - } - - /// Build the relay chain consensus. - fn build(self) -> Box> { - Box::new(RelayChainConsensus::new( - self.para_id, - self.proposer_factory, - self.create_inherent_data_providers, - self.block_import, - self.relay_chain_interface, - )) - } + )) } diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index e101d508a28..7a6860a97f9 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -268,6 +268,7 @@ where .map(|w| (w)(announce_block.clone())) .unwrap_or_else(|| announce_block); + let relay_chain_interface_for_closure = relay_chain_interface.clone(); if let Some(collator_key) = collator_key { let parachain_consensus: Box> = match consensus { Consensus::RelayChain => { @@ -278,14 +279,7 @@ where prometheus_registry.as_ref(), None, ); - let relay_chain_interface = Arc::new(RelayChainDirect { - full_client: relay_chain_full_node.client.clone(), - backend: relay_chain_full_node.backend.clone(), - network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), - overseer_handle: relay_chain_full_node.overseer_handle.clone(), - }); - - let relay_chain_interface2 = relay_chain_interface.clone(); + let relay_chain_interface2 = relay_chain_interface_for_closure.clone(); Box::new(cumulus_client_consensus_relay_chain::RelayChainConsensus::new( para_id, proposer_factory, @@ -293,7 +287,7 @@ where let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, - &relay_chain_interface, + &relay_chain_interface_for_closure, &validation_data, para_id, ); @@ -331,7 +325,6 @@ where start_collator(params).await?; } else { - let params = StartFullNodeParams { client: client.clone(), announce_block, From 6b4930c8798ea1101fd0db693175f5e52cd89679 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 6 Dec 2021 10:33:27 +0100 Subject: [PATCH 33/56] Format --- client/collator/Cargo.toml | 1 + client/pov-recovery/src/lib.rs | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/collator/Cargo.toml b/client/collator/Cargo.toml index d7281e87583..e54a04e2008 100644 --- a/client/collator/Cargo.toml +++ b/client/collator/Cargo.toml @@ -23,6 +23,7 @@ cumulus-client-network = { path = "../network" } cumulus-client-consensus-common = { path = "../consensus/common" } cumulus-primitives-core = { path = "../../primitives/core" } cumulus-relay-chain-interface = { path = "../relay-chain-interface" } + # Other dependencies codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } futures = { version = "0.3.1", features = ["compat"] } diff --git a/client/pov-recovery/src/lib.rs b/client/pov-recovery/src/lib.rs index c3af24cec2c..734bd14b466 100644 --- a/client/pov-recovery/src/lib.rs +++ b/client/pov-recovery/src/lib.rs @@ -53,8 +53,7 @@ use sp_runtime::{ use polkadot_node_primitives::{AvailableData, POV_BOMB_LIMIT}; use polkadot_overseer::Handle as OverseerHandle; use polkadot_primitives::v1::{ - Block as PBlock, CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, - SessionIndex, + Block as PBlock, CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, SessionIndex, }; use cumulus_primitives_core::ParachainBlockData; From 861402777a6cbf9d3ccba65f1f6197e981b4a85a Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 6 Dec 2021 10:57:12 +0100 Subject: [PATCH 34/56] Adjust naming and remove duplicate trait implementation --- client/network/src/lib.rs | 4 +- client/relay-chain-interface/src/lib.rs | 96 +------------------------ 2 files changed, 3 insertions(+), 97 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 7c9a677abff..5e591a29b2e 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -131,9 +131,9 @@ impl BlockAnnounceData { /// Check the signature of the statement. /// /// Returns an `Err(_)` if it failed. - fn check_signature

(self, relay_chain_client: &P) -> Result + fn check_signature(self, relay_chain_client: &RCInterface) -> Result where - P: RelayChainInterface + Send + Sync + 'static, + RCInterface: RelayChainInterface + Send + Sync + 'static, { let validator_index = self.statement.unchecked_validator_index(); diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 4a202ec3d36..5f3bba09462 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -284,103 +284,9 @@ impl ExecuteWithClient for RelayChainDirectBuilder { } } -impl RelayChainInterface - for Arc + Sync + Send> -{ - fn retrieve_dmq_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option> { - (**self).retrieve_dmq_contents(para_id, relay_parent) - } - - fn retrieve_all_inbound_hrmp_channel_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>> { - (**self).retrieve_all_inbound_hrmp_channel_contents(para_id, relay_parent) - } - - fn persisted_validation_data( - &self, - block_id: &BlockId, - para_id: ParaId, - occupied_core_assumption: OccupiedCoreAssumption, - ) -> Result, ApiError> { - (**self).persisted_validation_data(block_id, para_id, occupied_core_assumption) - } - - fn candidate_pending_availability( - &self, - block_id: &BlockId, - para_id: ParaId, - ) -> Result, ApiError> { - (**self).candidate_pending_availability(block_id, para_id) - } - - fn session_index_for_child(&self, block_id: &BlockId) -> Result { - (**self).session_index_for_child(block_id) - } - - fn validators(&self, block_id: &BlockId) -> Result, ApiError> { - (**self).validators(block_id) - } - - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { - (**self).import_notification_stream() - } - - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { - (**self).finality_notification_stream() - } - - fn storage_changes_notification_stream( - &self, - filter_keys: Option<&[sc_client_api::StorageKey]>, - child_filter_keys: Option< - &[(sc_client_api::StorageKey, Option>)], - >, - ) -> sc_client_api::blockchain::Result> { - (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) - } - - fn best_block_hash(&self) -> PHash { - (**self).best_block_hash() - } - - fn block_status(&self, block_id: BlockId) -> Result { - (**self).block_status(block_id) - } - - fn get_import_lock(&self) -> &RwLock<()> { - (**self).get_import_lock() - } - - fn get_state_at( - &self, - block_id: BlockId, - ) -> Result<>::State, sp_blockchain::Error> { - (**self).get_state_at(block_id) - } - - fn is_major_syncing(&self) -> bool { - (**self).is_major_syncing() - } - - fn slot_duration(&self) -> Result { - (**self).slot_duration() - } - - fn overseer_handle(&self) -> Option { - (**self).overseer_handle() - } -} - impl RelayChainInterface for Arc where - T: RelayChainInterface, + T: RelayChainInterface + ?Sized, Block: BlockT, { fn retrieve_dmq_contents( From 01173595ac1e09f80ef6d03f5517852926d5f4ea Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 6 Dec 2021 11:42:10 +0100 Subject: [PATCH 35/56] Improve naming --- client/network/src/lib.rs | 5 +++- .../network/src/wait_on_relay_chain_block.rs | 6 ++--- client/pov-recovery/tests/pov_recovery.rs | 2 +- client/relay-chain-interface/src/lib.rs | 25 +++++++++++-------- test/service/src/lib.rs | 4 +-- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 5e591a29b2e..c2b15d78ca1 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -131,7 +131,10 @@ impl BlockAnnounceData { /// Check the signature of the statement. /// /// Returns an `Err(_)` if it failed. - fn check_signature(self, relay_chain_client: &RCInterface) -> Result + fn check_signature( + self, + relay_chain_client: &RCInterface, + ) -> Result where RCInterface: RelayChainInterface + Send + Sync + 'static, { diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index bac77794c95..c67191ef2ff 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -140,7 +140,7 @@ mod tests { use super::*; - use cumulus_relay_chain_interface::RelayChainDirect; + use cumulus_relay_chain_interface::RelayChainLocal; use polkadot_test_client::{ construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, DefaultTestClientBuilderExt, ExecutionStrategy, InitPolkadotBlockBuilder, @@ -152,7 +152,7 @@ mod tests { use futures::{executor::block_on, poll, task::Poll}; - fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainDirect) { + fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainLocal) { let builder = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); let backend = builder.backend(); @@ -165,7 +165,7 @@ mod tests { ( client.clone(), block, - RelayChainDirect { + RelayChainLocal { full_client: client, backend: backend.clone(), network: Arc::new(Mutex::new(dummy_network)), diff --git a/client/pov-recovery/tests/pov_recovery.rs b/client/pov-recovery/tests/pov_recovery.rs index 65a1477b1e8..c0240a48b92 100644 --- a/client/pov-recovery/tests/pov_recovery.rs +++ b/client/pov-recovery/tests/pov_recovery.rs @@ -24,7 +24,7 @@ use std::sync::Arc; /// the parachain network, we need to recover the PoV from the relay chain. Using this PoV we can /// recover the block, import it and share it with the other nodes of the parachain network. #[substrate_test_utils::test] -// #[ignore] +#[ignore] async fn pov_recovery() { let mut builder = sc_cli::LoggerBuilder::new(""); builder.with_colors(false); diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 5f3bba09462..2a0c5ec2222 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -26,6 +26,7 @@ use std::sync::Mutex; const LOG_TARGET: &str = "relay-chain-interface"; +/// Should be used for all interaction with the relay chain in cumulus. pub trait RelayChainInterface { fn get_state_at( &self, @@ -39,6 +40,7 @@ pub trait RelayChainInterface { fn block_status(&self, block_id: BlockId) -> Result; fn best_block_hash(&self) -> PHash; + /// Returns the whole contents of the downward message queue for the parachain we are collating /// for. /// @@ -91,14 +93,16 @@ pub trait RelayChainInterface { fn overseer_handle(&self) -> Option; } -pub struct RelayChainDirect { +/// RelayChainLocal is used to interact with a full node that is running locally +/// in the same process. +pub struct RelayChainLocal { pub full_client: Arc, pub backend: Arc, pub network: Arc>>, pub overseer_handle: Option, } -impl Clone for RelayChainDirect { +impl Clone for RelayChainLocal { fn clone(&self) -> Self { Self { full_client: self.full_client.clone(), @@ -109,7 +113,7 @@ impl Clone for RelayChainDirect { } } -impl RelayChainInterface for RelayChainDirect +impl RelayChainInterface for RelayChainLocal where Client: ProvideRuntimeApi + BlockchainEvents + AuxStore + UsageProvider, Client::Api: ParachainHost + BabeApi, @@ -243,25 +247,25 @@ where } /// Builder for a concrete relay chain interface, creatd from a full node. Builds -/// a [`RelayChainDirect`] to access relay chain data necessary for parachain operation. +/// a [`RelayChainLocal`] to access relay chain data necessary for parachain operation. /// /// The builder takes a [`polkadot_client::Client`] /// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance and instantiates a RelayChainDirect with it. -pub struct RelayChainDirectBuilder { +/// the builder gets access to this concrete instance and instantiates a RelayChainLocal with it. +pub struct RelayChainLocalBuilder { polkadot_client: polkadot_client::Client, backend: Arc, network: Arc>>, overseer_handle: Option, } -impl RelayChainDirectBuilder { +impl RelayChainLocalBuilder { pub fn build(self) -> Arc + Sync + Send> { self.polkadot_client.clone().execute_with(self) } } -impl ExecuteWithClient for RelayChainDirectBuilder { +impl ExecuteWithClient for RelayChainLocalBuilder { type Output = Arc + Sync + Send>; fn execute_with_client(self, client: Arc) -> Self::Output @@ -275,7 +279,7 @@ impl ExecuteWithClient for RelayChainDirectBuilder { + Send, Client::Api: ParachainHost + BabeApi, { - Arc::new(RelayChainDirect { + Arc::new(RelayChainLocal { full_client: client, backend: self.backend, network: self.network, @@ -406,6 +410,7 @@ pub fn build_polkadot_full_node( } } +/// Builds a relay chain interface by constructing a full relay chain node pub fn build_relay_chain_interface( polkadot_config: Configuration, telemetry_worker_handle: Option, @@ -424,7 +429,7 @@ pub fn build_relay_chain_interface( let sync_oracle: Box = Box::new(full_node.network.clone()); let network = Arc::new(Mutex::new(sync_oracle)); - let relay_chain_interface_builder = RelayChainDirectBuilder { + let relay_chain_interface_builder = RelayChainLocalBuilder { polkadot_client: full_node.client.clone(), backend: full_node.backend.clone(), network, diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 7a6860a97f9..49a9ae6db76 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -28,7 +28,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::RelayChainDirect; +use cumulus_relay_chain_interface::RelayChainLocal; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use frame_system_rpc_runtime_api::AccountNonceApi; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; @@ -215,7 +215,7 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = Arc::new(RelayChainDirect { + let relay_chain_interface = Arc::new(RelayChainLocal { full_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), From d0bc375efff5f212e420fd504b5aae7729079166 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 6 Dec 2021 13:34:06 +0100 Subject: [PATCH 36/56] Fix type parameter name --- primitives/parachain-inherent/src/client_side.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index eddaa7d4b18..ccd32b237e8 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -128,14 +128,14 @@ impl ParachainInherentData { /// Create the [`ParachainInherentData`] at the given `relay_parent`. /// /// Returns `None` if the creation failed. - pub fn create_at( + pub fn create_at( relay_parent: PHash, - relay_chain_interface: &T, + relay_chain_interface: &RCInterface, validation_data: &PersistedValidationData, para_id: ParaId, ) -> Option where - T: RelayChainInterface, + RCInterface: RelayChainInterface, { let relay_chain_state = collect_relay_storage_proof(relay_chain_interface, para_id, relay_parent)?; From 98ad00809c3db5c75946b8bf52932c7d9b003bbe Mon Sep 17 00:00:00 2001 From: skunert Date: Wed, 8 Dec 2021 22:33:11 +0100 Subject: [PATCH 37/56] Replace todo macro by unimplemented --- client/network/src/wait_on_relay_chain_block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index c67191ef2ff..7747f65178a 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -126,11 +126,11 @@ struct DummyNetwork {} impl SyncOracle for DummyNetwork { fn is_major_syncing(&mut self) -> bool { - todo!() + unimplemented!("Not needed for test") } fn is_offline(&mut self) -> bool { - todo!() + unimplemented!("Not needed for test") } } From f9041f66596343595a4399b964664fa3a5d19f30 Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 10 Dec 2021 15:33:35 +0100 Subject: [PATCH 38/56] Remove `get_state_at` from interface --- Cargo.lock | 2 + client/network/Cargo.toml | 1 + client/network/src/tests.rs | 23 +++-- client/relay-chain-interface/Cargo.toml | 1 + client/relay-chain-interface/src/lib.rs | 99 +++++++++++++++---- .../parachain-inherent/src/client_side.rs | 54 +++------- 6 files changed, 111 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 615cf12e392..bf9857ceb78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1566,6 +1566,7 @@ dependencies = [ "sp-keyring", "sp-keystore", "sp-runtime", + "sp-state-machine", "substrate-test-utils", "tokio", "tracing", @@ -1865,6 +1866,7 @@ dependencies = [ "sp-consensus", "sp-core", "sp-runtime", + "sp-state-machine", "tracing", ] diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 626a5ef3373..37ac1aa0b00 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -13,6 +13,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" } # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 9f3e3a0eda8..f4149bc711f 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -72,13 +72,6 @@ impl DummyRelayChainInterface { } impl RelayChainInterface for DummyRelayChainInterface { - fn get_state_at( - &self, - _: cumulus_primitives_core::relay_chain::BlockId, - ) -> Result<>::State, sp_blockchain::Error> { - unimplemented!("Not needed for test") - } - fn get_import_lock(&self) -> &RwLock<()> { self.relay_backend.get_import_lock() } @@ -181,6 +174,22 @@ impl RelayChainInterface for DummyRelayChainInterface { fn overseer_handle(&self) -> Option { unimplemented!("Not needed for test") } + + fn get_storage_by_key( + &self, + _: &polkadot_service::BlockId, + _: &[u8], + ) -> Option { + unimplemented!("Not needed for test") + } + + fn prove_read( + &self, + _: &polkadot_service::BlockId, + _: &Vec>, + ) -> Result, Box> { + unimplemented!("Not needed for test") + } } fn make_validator_and_api( diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 86a67736bd3..dda57590899 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -16,6 +16,7 @@ sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 2a0c5ec2222..bd9de299df7 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -16,22 +16,20 @@ use polkadot_service::{ AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, }; use sc_client_api::{ - blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, UsageProvider, + blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, StorageProof, UsageProvider, }; use sc_telemetry::TelemetryWorkerHandle; use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; use sp_consensus::SyncOracle; use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; +use sp_state_machine::{Backend as StateBackend, StorageValue}; use std::sync::Mutex; const LOG_TARGET: &str = "relay-chain-interface"; /// Should be used for all interaction with the relay chain in cumulus. pub trait RelayChainInterface { - fn get_state_at( - &self, - block_id: BlockId, - ) -> Result<>::State, sp_blockchain::Error>; + fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option; fn get_import_lock(&self) -> &RwLock<()>; @@ -91,6 +89,12 @@ pub trait RelayChainInterface { fn slot_duration(&self) -> Result; fn overseer_handle(&self) -> Option; + + fn prove_read( + &self, + block_id: &BlockId, + relevant_keys: &Vec>, + ) -> Result, Box>; } /// RelayChainLocal is used to interact with a full node that is running locally @@ -225,13 +229,6 @@ where self.backend.get_import_lock() } - fn get_state_at( - &self, - block_id: BlockId, - ) -> Result<>::State, sp_blockchain::Error> { - self.backend.state_at(block_id) - } - fn is_major_syncing(&self) -> bool { let mut network = self.network.lock().unwrap(); network.is_major_syncing() @@ -244,6 +241,65 @@ where fn overseer_handle(&self) -> Option { self.overseer_handle.clone() } + + fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { + let state = self + .backend + .state_at(*block_id) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Cannot obtain the state of the relay chain.", + ) + }) + .ok()?; + state + .storage(key) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + error = ?e, + "Cannot decode the hrmp ingress channel index.", + ) + }) + .ok()? + } + + fn prove_read( + &self, + block_id: &BlockId, + relevant_keys: &Vec>, + ) -> Result, Box> { + let state_backend = self + .backend + .state_at(*block_id) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Cannot obtain the state of the relay chain.", + ); + }) + .ok(); + + match state_backend { + Some(state) => sp_state_machine::prove_read(state, relevant_keys) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Failed to collect required relay chain state storage proof.", + ); + e + }) + .map(|v| Some(v)), + None => Ok(None), + } + } } /// Builder for a concrete relay chain interface, creatd from a full node. Builds @@ -364,13 +420,6 @@ where (**self).get_import_lock() } - fn get_state_at( - &self, - block_id: BlockId, - ) -> Result<>::State, sp_blockchain::Error> { - (**self).get_state_at(block_id) - } - fn is_major_syncing(&self) -> bool { (**self).is_major_syncing() } @@ -382,6 +431,18 @@ where fn overseer_handle(&self) -> Option { (**self).overseer_handle() } + + fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { + (**self).get_storage_by_key(block_id, key) + } + + fn prove_read( + &self, + block_id: &BlockId, + relevant_keys: &Vec>, + ) -> Result, Box> { + (**self).prove_read(block_id, relevant_keys) + } } /// Build the Polkadot full node using the given `config`. diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index ccd32b237e8..1a4cd3fc7f7 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -24,7 +24,6 @@ use cumulus_primitives_core::{ }; use cumulus_relay_chain_interface::RelayChainInterface; use sp_runtime::generic::BlockId; -use sp_state_machine::Backend as _; const LOG_TARGET: &str = "parachain-inherent"; @@ -37,28 +36,11 @@ fn collect_relay_storage_proof( ) -> Option { use relay_chain::well_known_keys as relay_well_known_keys; - let relay_parent_state_backend = relay_chain_interface - .get_state_at(BlockId::Hash(relay_parent)) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "Cannot obtain the state of the relay chain.", - ) - }) - .ok()?; - - let ingress_channels = relay_parent_state_backend - .storage(&relay_well_known_keys::hrmp_ingress_channel_index(para_id)) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - error = ?e, - "Cannot obtain the hrmp ingress channel index." - ) - }) - .ok()?; + let relay_parent_block_id = BlockId::Hash(relay_parent); + let ingress_channels = relay_chain_interface.get_storage_by_key( + &relay_parent_block_id, + &relay_well_known_keys::hrmp_ingress_channel_index(para_id), + ); let ingress_channels = ingress_channels .map(|raw| >::decode(&mut &raw[..])) @@ -73,16 +55,11 @@ fn collect_relay_storage_proof( .ok()? .unwrap_or_default(); - let egress_channels = relay_parent_state_backend - .storage(&relay_well_known_keys::hrmp_egress_channel_index(para_id)) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - error = ?e, - "Cannot obtain the hrmp egress channel index.", - ) - }) - .ok()?; + let egress_channels = relay_chain_interface.get_storage_by_key( + &relay_parent_block_id, + &relay_well_known_keys::hrmp_egress_channel_index(para_id), + ); + let egress_channels = egress_channels .map(|raw| >::decode(&mut &raw[..])) .transpose() @@ -112,16 +89,7 @@ fn collect_relay_storage_proof( relay_well_known_keys::hrmp_channels(HrmpChannelId { sender: para_id, recipient }) })); - sp_state_machine::prove_read(relay_parent_state_backend, relevant_keys) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "Failed to collect required relay chain state storage proof.", - ) - }) - .ok() + relay_chain_interface.prove_read(&relay_parent_block_id, &relevant_keys).ok()? } impl ParachainInherentData { From 197e4001fda124c76e587610a8b9fa8d8e62fbac Mon Sep 17 00:00:00 2001 From: skunert Date: Fri, 10 Dec 2021 15:53:50 +0100 Subject: [PATCH 39/56] Remove slot_duration from interface --- client/network/src/tests.rs | 5 ----- client/relay-chain-interface/src/lib.rs | 12 +----------- client/service/src/lib.rs | 5 ++++- parachain-template/node/src/service.rs | 4 +++- polkadot-parachains/src/service.rs | 3 +++ test/service/src/lib.rs | 5 ++++- 6 files changed, 15 insertions(+), 19 deletions(-) diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index f4149bc711f..15f06f025d5 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -18,7 +18,6 @@ use super::*; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; use parking_lot::{Mutex, RwLock}; -use polkadot_client::FullBackend; use polkadot_node_primitives::{SignedFullStatement, Statement}; use polkadot_primitives::v1::{ Block as PBlock, CandidateCommitments, CandidateDescriptor, CommittedCandidateReceipt, @@ -167,10 +166,6 @@ impl RelayChainInterface for DummyRelayChainInterface { false } - fn slot_duration(&self) -> Result { - unimplemented!("Not needed for test") - } - fn overseer_handle(&self) -> Option { unimplemented!("Not needed for test") } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index bd9de299df7..1da384083ab 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; use cumulus_primitives_core::{ relay_chain::{ @@ -86,8 +86,6 @@ pub trait RelayChainInterface { fn is_major_syncing(&self) -> bool; - fn slot_duration(&self) -> Result; - fn overseer_handle(&self) -> Option; fn prove_read( @@ -234,10 +232,6 @@ where network.is_major_syncing() } - fn slot_duration(&self) -> Result { - Ok(sc_consensus_babe::Config::get_or_compute(&*self.full_client)?.slot_duration()) - } - fn overseer_handle(&self) -> Option { self.overseer_handle.clone() } @@ -424,10 +418,6 @@ where (**self).is_major_syncing() } - fn slot_duration(&self) -> Result { - (**self).slot_duration() - } - fn overseer_handle(&self) -> Option { (**self).overseer_handle() } diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index f95536ec7dc..2e60a098349 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -18,6 +18,7 @@ //! //! Provides functions for starting a collator node or a normal full node. +use core::time::Duration; use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; use cumulus_relay_chain_interface::RelayChainInterface; @@ -54,6 +55,7 @@ pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawn pub parachain_consensus: Box>, pub import_queue: IQ, pub collator_key: CollatorPair, + pub slot_duration: Duration, } /// Start a collator node for a parachain. @@ -73,6 +75,7 @@ pub async fn start_collator<'a, Block, BS, Client, Backend, RCInterface, Spawner parachain_consensus, import_queue, collator_key, + slot_duration, }: StartCollatorParams<'a, Block, BS, Client, RCInterface, Spawner, IQ>, ) -> sc_service::error::Result<()> where @@ -109,7 +112,7 @@ where relay_chain_interface .overseer_handle() .ok_or_else(|| "Polkadot full node did not provide an `OverseerHandle`!")?, - relay_chain_interface.slot_duration()?, + slot_duration, client.clone(), import_queue, relay_chain_interface.clone(), diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 4b92a582c41..43d2c4cb03c 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -18,9 +18,10 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::{relay_chain::v1::Block as PBlock, ParaId}; +use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; // Substrate Imports -use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; +use core::time::Duration; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; @@ -326,6 +327,7 @@ where parachain_consensus, import_queue, collator_key, + slot_duration: Duration::from_secs(6), }; start_collator(params).await?; diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index e0d13ec6e32..91f8da19de5 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -34,6 +34,7 @@ use polkadot_service::NativeExecutionDispatch; use crate::rpc; pub use parachains_common::{AccountId, Balance, Block, Hash, Header, Index as Nonce}; +use core::time::Duration; use cumulus_client_consensus_relay_chain::Verifier as RelayChainVerifier; use futures::lock::Mutex; use sc_client_api::ExecutorProvider; @@ -398,6 +399,7 @@ where parachain_consensus, import_queue, collator_key, + slot_duration: Duration::from_secs(6), }; start_collator(params).await?; @@ -583,6 +585,7 @@ where parachain_consensus, import_queue, collator_key, + slot_duration: Duration::from_secs(6), }; start_collator(params).await?; diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 49a9ae6db76..625d9240e1a 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -21,7 +21,8 @@ mod chain_spec; mod genesis; -use core::future::Future; +use core::{future::Future, time::Duration}; + use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ @@ -30,6 +31,7 @@ use cumulus_client_service::{ use cumulus_primitives_core::ParaId; use cumulus_relay_chain_interface::RelayChainLocal; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; + use frame_system_rpc_runtime_api::AccountNonceApi; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use polkadot_service::ProvideRuntimeApi; @@ -321,6 +323,7 @@ where relay_chain_interface, collator_key, import_queue, + slot_duration: Duration::from_secs(6), }; start_collator(params).await?; From 926d9fb8400dda484edb7db9fbd2fb49ca3a3dd1 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 13 Dec 2021 14:37:09 +0100 Subject: [PATCH 40/56] Extract checking for blocks into relay-chain-interface --- Cargo.lock | 1 + client/network/src/tests.rs | 29 ++++++++--- .../network/src/wait_on_relay_chain_block.rs | 20 +++----- client/relay-chain-interface/Cargo.toml | 2 + client/relay-chain-interface/src/lib.rs | 48 ++++++++++++++----- 5 files changed, 70 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf9857ceb78..f574162ef07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1852,6 +1852,7 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "enum_dispatch", + "futures 0.3.18", "parking_lot 0.11.2", "polkadot-client", "polkadot-service", diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 15f06f025d5..55ec710f7ae 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -17,7 +17,7 @@ use super::*; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; -use parking_lot::{Mutex, RwLock}; +use parking_lot::Mutex; use polkadot_node_primitives::{SignedFullStatement, Statement}; use polkadot_primitives::v1::{ Block as PBlock, CandidateCommitments, CandidateDescriptor, CommittedCandidateReceipt, @@ -30,7 +30,7 @@ use polkadot_test_client::{ InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; use sc_client_api::{Backend, BlockchainEvents}; -use sp_blockchain::HeaderBackend; +use sp_blockchain::{BlockStatus, HeaderBackend}; use sp_consensus::BlockOrigin; use sp_core::H256; use sp_keyring::Sr25519Keyring; @@ -71,10 +71,6 @@ impl DummyRelayChainInterface { } impl RelayChainInterface for DummyRelayChainInterface { - fn get_import_lock(&self) -> &RwLock<()> { - self.relay_backend.get_import_lock() - } - fn validators( &self, _: &cumulus_primitives_core::relay_chain::BlockId, @@ -185,6 +181,27 @@ impl RelayChainInterface for DummyRelayChainInterface { ) -> Result, Box> { unimplemented!("Not needed for test") } + + fn check_block_in_chain( + &self, + block_id: polkadot_service::BlockId, + ) -> Result>, sp_blockchain::Error> { + let _lock = self.relay_backend.get_import_lock(); + + match self.relay_backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(None), + Err(err) => return Err(err), + _ => {}, + } + + let listener = self.relay_client.import_notification_stream(); + + // Now it is safe to drop the lock, even when the block is now imported, it should show + // up in our registered listener. + drop(_lock); + + Ok(Some(listener)) + } } fn make_validator_and_api( diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 7747f65178a..46f17097172 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -19,7 +19,7 @@ use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; -use sc_client_api::blockchain::{self, BlockStatus}; +use sc_client_api::blockchain::{self}; use sp_consensus::SyncOracle; use sp_runtime::generic::BlockId; use std::time::Duration; @@ -90,18 +90,12 @@ where &self, hash: PHash, ) -> impl Future> { - let _lock = self.relay_chain_interface.get_import_lock().read(); - - match self.relay_chain_interface.block_status(BlockId::Hash(hash)) { - Ok(BlockStatus::InChain) => return ready(Ok(())).boxed(), - Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(), - _ => {}, - } - - let mut listener = self.relay_chain_interface.import_notification_stream(); - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); + let mut listener = + match self.relay_chain_interface.check_block_in_chain(BlockId::Hash(hash)) { + Ok(Some(listener)) => listener, + Ok(None) => return ready(Ok(())).boxed(), + Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(), + }; let mut timeout = futures_timer::Delay::new(Duration::from_secs(TIMEOUT_IN_SECONDS)).fuse(); diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index dda57590899..c7649e7891a 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -23,6 +23,8 @@ sc-network = { git = "https://github.com/paritytech/substrate", branch = "master sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } + +futures = { version = "0.3.1", features = ["compat"] } parking_lot = "0.11.1" enum_dispatch = "0.3.7" tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 1da384083ab..2e5bbd87e37 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -10,7 +10,6 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use parking_lot::RwLock; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use polkadot_service::{ AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, @@ -31,8 +30,6 @@ const LOG_TARGET: &str = "relay-chain-interface"; pub trait RelayChainInterface { fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option; - fn get_import_lock(&self) -> &RwLock<()>; - fn validators(&self, block_id: &BlockId) -> Result, ApiError>; fn block_status(&self, block_id: BlockId) -> Result; @@ -75,7 +72,16 @@ pub trait RelayChainInterface { fn session_index_for_child(&self, block_id: &BlockId) -> Result; fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; + + /// Check if block is in chain. If it is, we return nothing. + /// If it is not in the chain, we return a listener that can be used to wait on the block. + fn check_block_in_chain( + &self, + block_id: BlockId, + ) -> Result>, sp_blockchain::Error>; + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; + fn storage_changes_notification_stream( &self, filter_keys: Option<&[sc_client_api::StorageKey]>, @@ -223,10 +229,6 @@ where self.backend.blockchain().status(block_id) } - fn get_import_lock(&self) -> &RwLock<()> { - self.backend.get_import_lock() - } - fn is_major_syncing(&self) -> bool { let mut network = self.network.lock().unwrap(); network.is_major_syncing() @@ -294,6 +296,27 @@ where None => Ok(None), } } + + fn check_block_in_chain( + &self, + block_id: BlockId, + ) -> Result>, sp_blockchain::Error> { + let _lock = self.backend.get_import_lock(); + + match self.backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(None), + Err(err) => return Err(err), + _ => {}, + } + + let listener = self.full_client.import_notification_stream(); + + // Now it is safe to drop the lock, even when the block is now imported, it should show + // up in our registered listener. + drop(_lock); + + Ok(Some(listener)) + } } /// Builder for a concrete relay chain interface, creatd from a full node. Builds @@ -410,10 +433,6 @@ where (**self).block_status(block_id) } - fn get_import_lock(&self) -> &RwLock<()> { - (**self).get_import_lock() - } - fn is_major_syncing(&self) -> bool { (**self).is_major_syncing() } @@ -433,6 +452,13 @@ where ) -> Result, Box> { (**self).prove_read(block_id, relevant_keys) } + + fn check_block_in_chain( + &self, + block_id: BlockId, + ) -> Result>, sp_blockchain::Error> { + (**self).check_block_in_chain(block_id) + } } /// Build the Polkadot full node using the given `config`. From f18c5dad14139ea817016db4d7abbdc0c3521b04 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 13 Dec 2021 15:09:44 +0100 Subject: [PATCH 41/56] Minor adjustments suggested by the comments --- Cargo.lock | 1 - client/collator/src/lib.rs | 3 +- client/consensus/aura/Cargo.toml | 1 - client/consensus/aura/src/lib.rs | 3 +- client/consensus/common/Cargo.toml | 1 + .../network/src/wait_on_relay_chain_block.rs | 27 ++++++++--------- client/pov-recovery/src/lib.rs | 9 ++---- client/relay-chain-interface/Cargo.toml | 2 +- client/relay-chain-interface/src/lib.rs | 30 ++++++++++++++----- test/service/src/lib.rs | 2 +- 10 files changed, 45 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f574162ef07..b1030e5a809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1467,7 +1467,6 @@ name = "cumulus-client-consensus-aura" version = "0.1.0" dependencies = [ "async-trait", - "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-primitives-core", "cumulus-relay-chain-interface", diff --git a/client/collator/src/lib.rs b/client/collator/src/lib.rs index 2ae43a5e05d..ca298de2fea 100644 --- a/client/collator/src/lib.rs +++ b/client/collator/src/lib.rs @@ -18,7 +18,7 @@ use cumulus_client_network::WaitToAnnounce; use cumulus_primitives_core::{ - relay_chain::Hash as PHash, ParachainBlockData, PersistedValidationData, + relay_chain::Hash as PHash, CollectCollationInfo, ParachainBlockData, PersistedValidationData, }; use sc_client_api::BlockBackend; @@ -39,7 +39,6 @@ use polkadot_overseer::Handle as OverseerHandle; use polkadot_primitives::v1::{CollatorPair, HeadData, Id as ParaId}; use codec::{Decode, Encode}; -use cumulus_primitives_core::CollectCollationInfo; use futures::{channel::oneshot, FutureExt}; use parking_lot::Mutex; use std::sync::Arc; diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 1ce846b7d29..66e69377b26 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -26,7 +26,6 @@ substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } -cumulus-client-collator = { path = "../../../client/collator" } cumulus-primitives-core = { path = "../../../primitives/core" } cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 6fb582606f8..07b10b07857 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -30,6 +30,8 @@ use cumulus_primitives_core::{ relay_chain::v1::{Block as PBlock, Hash as PHash}, PersistedValidationData, }; +use cumulus_relay_chain_interface::RelayChainInterface; + use futures::lock::Mutex; use sc_client_api::{backend::AuxStore, BlockOf}; use sc_consensus::BlockImport; @@ -50,7 +52,6 @@ use std::{convert::TryFrom, hash::Hash, sync::Arc}; mod import_queue; -use cumulus_relay_chain_interface::RelayChainInterface; pub use import_queue::{build_verifier, import_queue, BuildVerifierParams, ImportQueueParams}; pub use sc_consensus_aura::{ slot_duration, AuraVerifier, BuildAuraWorkerParams, SlotDuration, SlotProportion, diff --git a/client/consensus/common/Cargo.toml b/client/consensus/common/Cargo.toml index 4d784f7f6fb..057f0b34966 100644 --- a/client/consensus/common/Cargo.toml +++ b/client/consensus/common/Cargo.toml @@ -18,6 +18,7 @@ sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } # Polkadot deps polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } +# Cumulus deps cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } # Other deps diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 46f17097172..b291739743f 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -20,7 +20,6 @@ use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; use sc_client_api::blockchain::{self}; -use sp_consensus::SyncOracle; use sp_runtime::generic::BlockId; use std::time::Duration; @@ -116,18 +115,6 @@ where } } -struct DummyNetwork {} - -impl SyncOracle for DummyNetwork { - fn is_major_syncing(&mut self) -> bool { - unimplemented!("Not needed for test") - } - - fn is_offline(&mut self) -> bool { - unimplemented!("Not needed for test") - } -} - #[cfg(test)] mod tests { use std::sync::Mutex; @@ -146,6 +133,18 @@ mod tests { use futures::{executor::block_on, poll, task::Poll}; + struct DummyNetwork {} + + impl SyncOracle for DummyNetwork { + fn is_major_syncing(&mut self) -> bool { + unimplemented!("Not needed for test") + } + + fn is_offline(&mut self) -> bool { + unimplemented!("Not needed for test") + } + } + fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainLocal) { let builder = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); @@ -162,7 +161,7 @@ mod tests { RelayChainLocal { full_client: client, backend: backend.clone(), - network: Arc::new(Mutex::new(dummy_network)), + sync_oracle: Arc::new(Mutex::new(dummy_network)), overseer_handle: None, }, ) diff --git a/client/pov-recovery/src/lib.rs b/client/pov-recovery/src/lib.rs index 734bd14b466..8ef68835a96 100644 --- a/client/pov-recovery/src/lib.rs +++ b/client/pov-recovery/src/lib.rs @@ -417,13 +417,10 @@ where } /// Returns a stream over pending candidates for the parachain corresponding to `para_id`. -fn pending_candidates( - relay_chain_client: RC, +fn pending_candidates( + relay_chain_client: impl RelayChainInterface, para_id: ParaId, -) -> impl Stream -where - RC: RelayChainInterface + Clone, -{ +) -> impl Stream { relay_chain_client.import_notification_stream().filter_map(move |n| { let res = relay_chain_client .candidate_pending_availability(&BlockId::hash(n.hash), para_id) diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index c7649e7891a..1d2cf0242c3 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -1,9 +1,9 @@ [package] +authors = ["Parity Technologies "] name = "cumulus-relay-chain-interface" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 2e5bbd87e37..0aa55ac5d8a 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -1,3 +1,19 @@ +// Copyright 2021 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + use std::sync::Arc; use cumulus_primitives_core::{ @@ -106,7 +122,7 @@ pub trait RelayChainInterface { pub struct RelayChainLocal { pub full_client: Arc, pub backend: Arc, - pub network: Arc>>, + pub sync_oracle: Arc>>, pub overseer_handle: Option, } @@ -115,7 +131,7 @@ impl Clone for RelayChainLocal { Self { full_client: self.full_client.clone(), backend: self.backend.clone(), - network: self.network.clone(), + sync_oracle: self.sync_oracle.clone(), overseer_handle: self.overseer_handle.clone(), } } @@ -230,7 +246,7 @@ where } fn is_major_syncing(&self) -> bool { - let mut network = self.network.lock().unwrap(); + let mut network = self.sync_oracle.lock().unwrap(); network.is_major_syncing() } @@ -328,7 +344,7 @@ where pub struct RelayChainLocalBuilder { polkadot_client: polkadot_client::Client, backend: Arc, - network: Arc>>, + sync_oracle: Arc>>, overseer_handle: Option, } @@ -355,7 +371,7 @@ impl ExecuteWithClient for RelayChainLocalBuilder { Arc::new(RelayChainLocal { full_client: client, backend: self.backend, - network: self.network, + sync_oracle: self.sync_oracle, overseer_handle: self.overseer_handle, }) } @@ -505,11 +521,11 @@ pub fn build_relay_chain_interface( )?; let sync_oracle: Box = Box::new(full_node.network.clone()); - let network = Arc::new(Mutex::new(sync_oracle)); + let sync_oracle = Arc::new(Mutex::new(sync_oracle)); let relay_chain_interface_builder = RelayChainLocalBuilder { polkadot_client: full_node.client.clone(), backend: full_node.backend.clone(), - network, + sync_oracle, overseer_handle: full_node.overseer_handle.clone(), }; task_manager.add_child(full_node.task_manager); diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 625d9240e1a..7a7b7e5ce3a 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -220,7 +220,7 @@ where let relay_chain_interface = Arc::new(RelayChainLocal { full_client: relay_chain_full_node.client.clone(), backend: relay_chain_full_node.backend.clone(), - network: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), + sync_oracle: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), overseer_handle: relay_chain_full_node.overseer_handle.clone(), }); task_manager.add_child(relay_chain_full_node.task_manager); From 94a8a50260f7225f5801d02bc0ab534ce0040323 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 13 Dec 2021 16:42:14 +0100 Subject: [PATCH 42/56] Improve naming, remove block generics --- client/consensus/aura/src/lib.rs | 8 +-- .../common/src/parachain_consensus.rs | 4 +- client/consensus/relay-chain/src/lib.rs | 8 +-- client/network/src/lib.rs | 18 +++---- client/network/src/tests.rs | 2 +- .../network/src/wait_on_relay_chain_block.rs | 9 ++-- client/pov-recovery/src/lib.rs | 10 ++-- client/relay-chain-interface/src/lib.rs | 52 +++++++++---------- client/service/src/lib.rs | 6 +-- parachain-template/node/src/service.rs | 4 +- polkadot-parachains/src/service.rs | 6 +-- .../parachain-inherent/src/client_side.rs | 13 ++--- 12 files changed, 69 insertions(+), 71 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 07b10b07857..9b85a9d9891 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -27,7 +27,7 @@ use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; use cumulus_primitives_core::{ - relay_chain::v1::{Block as PBlock, Hash as PHash}, + relay_chain::v1::Hash as PHash, PersistedValidationData, }; use cumulus_relay_chain_interface::RelayChainInterface; @@ -91,7 +91,7 @@ where impl AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone, CIDP: CreateInherentDataProviders, CIDP::InherentDataProviders: InherentDataProviderExt, { @@ -195,7 +195,7 @@ where impl ParachainConsensus for AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone + Send + Sync, + RCInterface: RelayChainInterface + Clone, CIDP: CreateInherentDataProviders + Send + Sync, CIDP::InherentDataProviders: InherentDataProviderExt + Send, { @@ -297,7 +297,7 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { Box::new(AuraConsensus::new::( para_client, diff --git a/client/consensus/common/src/parachain_consensus.rs b/client/consensus/common/src/parachain_consensus.rs index 2aa493f9a88..224e3e5fd9b 100644 --- a/client/consensus/common/src/parachain_consensus.rs +++ b/client/consensus/common/src/parachain_consensus.rs @@ -368,9 +368,9 @@ where } } -impl RelaychainClient for T +impl RelaychainClient for RCInterface where - T: RelayChainInterface + Send + Sync + Clone + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { type Error = ClientError; diff --git a/client/consensus/relay-chain/src/lib.rs b/client/consensus/relay-chain/src/lib.rs index 1cf9249b682..e7fa0d78e6f 100644 --- a/client/consensus/relay-chain/src/lib.rs +++ b/client/consensus/relay-chain/src/lib.rs @@ -37,7 +37,7 @@ use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; use cumulus_primitives_core::{ - relay_chain::v1::{Block as PBlock, Hash as PHash}, + relay_chain::v1::Hash as PHash, ParaId, PersistedValidationData, }; use cumulus_relay_chain_interface::RelayChainInterface; @@ -85,7 +85,7 @@ where impl RelayChainConsensus where B: BlockT, - RCInterface: RelayChainInterface, + RCInterface: RelayChainInterface, CIDP: CreateInherentDataProviders, { /// Create a new instance of relay-chain provided consensus. @@ -146,7 +146,7 @@ impl ParachainConsensus for RelayChainConsensus where B: BlockT, - RCInterface: RelayChainInterface + Send + Sync + Clone, + RCInterface: RelayChainInterface + Clone, BI: BlockImport + Send + Sync, PF: Environment + Send + Sync, PF::Proposer: Proposer< @@ -252,7 +252,7 @@ where >, BI: BlockImport + Send + Sync + 'static, CIDP: CreateInherentDataProviders + 'static, - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { Box::new(RelayChainConsensus::new( para_id, diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index c2b15d78ca1..5943109cf48 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -136,7 +136,7 @@ impl BlockAnnounceData { relay_chain_client: &RCInterface, ) -> Result where - RCInterface: RelayChainInterface + Send + Sync + 'static, + RCInterface: RelayChainInterface + 'static, { let validator_index = self.statement.unchecked_validator_index(); @@ -247,13 +247,13 @@ where } } -impl BlockAnnounceValidator +impl BlockAnnounceValidator where - R: RelayChainInterface + Clone, + RCInterface: RelayChainInterface + Clone, { /// Get the included block of the given parachain in the relay chain. fn included_block( - relay_chain_interface: &R, + relay_chain_interface: &RCInterface, block_id: &BlockId, para_id: ParaId, ) -> Result { @@ -275,7 +275,7 @@ where /// Get the backed block hash of the given parachain in the relay chain. fn backed_block_hash( - relay_chain_interface: &R, + relay_chain_interface: &RCInterface, block_id: &BlockId, para_id: ParaId, ) -> Result, BoxedError> { @@ -328,9 +328,9 @@ where } } -impl BlockAnnounceValidatorT for BlockAnnounceValidator +impl BlockAnnounceValidatorT for BlockAnnounceValidator where - P: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { fn validate( &mut self, @@ -389,7 +389,7 @@ pub fn build_block_announce_validator( para_id: ParaId, ) -> Box + Send> where - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { BlockAnnounceValidatorBuilder::new(relay_chain_interface, para_id).build() } @@ -405,7 +405,7 @@ struct BlockAnnounceValidatorBuilder { impl BlockAnnounceValidatorBuilder where - RCInterface: RelayChainInterface + Clone + Send + Sync + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { /// Create a new instance of the builder. fn new(relay_chain_interface: RCInterface, para_id: ParaId) -> Self { diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 55ec710f7ae..bfb5c096c36 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -70,7 +70,7 @@ impl DummyRelayChainInterface { } } -impl RelayChainInterface for DummyRelayChainInterface { +impl RelayChainInterface for DummyRelayChainInterface { fn validators( &self, _: &cumulus_primitives_core::relay_chain::BlockId, diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index b291739743f..c1443cd24db 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -18,8 +18,8 @@ use cumulus_relay_chain_interface::RelayChainInterface; use futures::{future::ready, Future, FutureExt, StreamExt}; -use polkadot_primitives::v1::{Block as PBlock, Hash as PHash}; -use sc_client_api::blockchain::{self}; +use polkadot_primitives::v1::Hash as PHash; +use sc_client_api::blockchain; use sp_runtime::generic::BlockId; use std::time::Duration; @@ -83,7 +83,7 @@ impl WaitOnRelayChainBlock { impl WaitOnRelayChainBlock where - RCInterface: RelayChainInterface, + RCInterface: RelayChainInterface, { pub fn wait_on_relay_chain_block( &self, @@ -122,13 +122,14 @@ mod tests { use super::*; use cumulus_relay_chain_interface::RelayChainLocal; + use polkadot_primitives::v1::Block as PBlock; use polkadot_test_client::{ construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, DefaultTestClientBuilderExt, ExecutionStrategy, InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; use sc_service::Arc; - use sp_consensus::BlockOrigin; + use sp_consensus::{BlockOrigin, SyncOracle}; use sp_runtime::traits::Block as BlockT; use futures::{executor::block_on, poll, task::Poll}; diff --git a/client/pov-recovery/src/lib.rs b/client/pov-recovery/src/lib.rs index 8ef68835a96..7e31f5000d6 100644 --- a/client/pov-recovery/src/lib.rs +++ b/client/pov-recovery/src/lib.rs @@ -53,7 +53,7 @@ use sp_runtime::{ use polkadot_node_primitives::{AvailableData, POV_BOMB_LIMIT}; use polkadot_overseer::Handle as OverseerHandle; use polkadot_primitives::v1::{ - Block as PBlock, CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, SessionIndex, + CandidateReceipt, CommittedCandidateReceipt, Id as ParaId, SessionIndex, }; use cumulus_primitives_core::ParachainBlockData; @@ -105,10 +105,10 @@ pub struct PoVRecovery { para_id: ParaId, } -impl PoVRecovery +impl PoVRecovery where PC: BlockBackend + BlockchainEvents + UsageProvider, - RC: RelayChainInterface + Send + Sync + Clone, + RCInterface: RelayChainInterface + Clone, IQ: ImportQueue, { /// Create a new instance. @@ -117,7 +117,7 @@ where relay_chain_slot_duration: Duration, parachain_client: Arc, parachain_import_queue: IQ, - relay_chain_interface: RC, + relay_chain_interface: RCInterface, para_id: ParaId, ) -> Self { Self { @@ -418,7 +418,7 @@ where /// Returns a stream over pending candidates for the parachain corresponding to `para_id`. fn pending_candidates( - relay_chain_client: impl RelayChainInterface, + relay_chain_client: impl RelayChainInterface, para_id: ParaId, ) -> impl Stream { relay_chain_client.import_notification_stream().filter_map(move |n| { diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 0aa55ac5d8a..d6d122ad8d0 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -34,7 +34,7 @@ use sc_client_api::{ blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, StorageProof, UsageProvider, }; use sc_telemetry::TelemetryWorkerHandle; -use sp_api::{ApiError, BlockT, ProvideRuntimeApi}; +use sp_api::{ApiError, ProvideRuntimeApi}; use sp_consensus::SyncOracle; use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; use sp_state_machine::{Backend as StateBackend, StorageValue}; @@ -43,7 +43,7 @@ use std::sync::Mutex; const LOG_TARGET: &str = "relay-chain-interface"; /// Should be used for all interaction with the relay chain in cumulus. -pub trait RelayChainInterface { +pub trait RelayChainInterface: Send + Sync { fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option; fn validators(&self, block_id: &BlockId) -> Result, ApiError>; @@ -87,16 +87,16 @@ pub trait RelayChainInterface { fn session_index_for_child(&self, block_id: &BlockId) -> Result; - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; /// Check if block is in chain. If it is, we return nothing. /// If it is not in the chain, we return a listener that can be used to wait on the block. fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error>; + ) -> Result>, sp_blockchain::Error>; - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; fn storage_changes_notification_stream( &self, @@ -104,7 +104,7 @@ pub trait RelayChainInterface { child_filter_keys: Option< &[(sc_client_api::StorageKey, Option>)], >, - ) -> sc_client_api::blockchain::Result>; + ) -> sc_client_api::blockchain::Result>; fn is_major_syncing(&self) -> bool; @@ -137,11 +137,15 @@ impl Clone for RelayChainLocal { } } -impl RelayChainInterface for RelayChainLocal +impl RelayChainInterface for RelayChainLocal where - Client: ProvideRuntimeApi + BlockchainEvents + AuxStore + UsageProvider, + Client: ProvideRuntimeApi + + BlockchainEvents + + AuxStore + + UsageProvider + + Sync + + Send, Client::Api: ParachainHost + BabeApi, - Block: BlockT, { fn retrieve_dmq_contents( &self, @@ -218,11 +222,11 @@ where self.full_client.runtime_api().validators(block_id) } - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { self.full_client.import_notification_stream() } - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { self.full_client.finality_notification_stream() } @@ -232,7 +236,7 @@ where child_filter_keys: Option< &[(sc_client_api::StorageKey, Option>)], >, - ) -> sc_client_api::blockchain::Result> { + ) -> sc_client_api::blockchain::Result> { self.full_client .storage_changes_notification_stream(filter_keys, child_filter_keys) } @@ -316,7 +320,7 @@ where fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error> { + ) -> Result>, sp_blockchain::Error> { let _lock = self.backend.get_import_lock(); match self.backend.blockchain().status(block_id) { @@ -349,13 +353,13 @@ pub struct RelayChainLocalBuilder { } impl RelayChainLocalBuilder { - pub fn build(self) -> Arc + Sync + Send> { + pub fn build(self) -> Arc { self.polkadot_client.clone().execute_with(self) } } impl ExecuteWithClient for RelayChainLocalBuilder { - type Output = Arc + Sync + Send>; + type Output = Arc; fn execute_with_client(self, client: Arc) -> Self::Output where @@ -377,10 +381,9 @@ impl ExecuteWithClient for RelayChainLocalBuilder { } } -impl RelayChainInterface for Arc +impl RelayChainInterface for Arc where - T: RelayChainInterface + ?Sized, - Block: BlockT, + T: RelayChainInterface + ?Sized, { fn retrieve_dmq_contents( &self, @@ -423,11 +426,11 @@ where (**self).validators(block_id) } - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { (**self).import_notification_stream() } - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { (**self).finality_notification_stream() } @@ -437,7 +440,7 @@ where child_filter_keys: Option< &[(sc_client_api::StorageKey, Option>)], >, - ) -> sc_client_api::blockchain::Result> { + ) -> sc_client_api::blockchain::Result> { (**self).storage_changes_notification_stream(filter_keys, child_filter_keys) } @@ -472,7 +475,7 @@ where fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error> { + ) -> Result>, sp_blockchain::Error> { (**self).check_block_in_chain(block_id) } } @@ -508,10 +511,7 @@ pub fn build_relay_chain_interface( polkadot_config: Configuration, telemetry_worker_handle: Option, task_manager: &mut TaskManager, -) -> Result< - (Arc<(dyn RelayChainInterface + Send + Sync + 'static)>, CollatorPair), - polkadot_service::Error, -> { +) -> Result<(Arc<(dyn RelayChainInterface + 'static)>, CollatorPair), polkadot_service::Error> { let (full_node, collator_key) = build_polkadot_full_node(polkadot_config, telemetry_worker_handle).map_err( |e| match e { diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 2e60a098349..30ddb2391d8 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -22,7 +22,7 @@ use core::time::Duration; use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; use cumulus_relay_chain_interface::RelayChainInterface; -use polkadot_primitives::v1::{Block as PBlock, CollatorPair}; +use polkadot_primitives::v1::CollatorPair; use sc_client_api::{ Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, UsageProvider, }; @@ -93,7 +93,7 @@ where Client::Api: CollectCollationInfo, for<'b> &'b Client: BlockImport, Spawner: SpawnNamed + Clone + Send + Sync + 'static, - RCInterface: RelayChainInterface + Send + Sync + Clone + 'static, + RCInterface: RelayChainInterface + Clone + 'static, Backend: BackendT + 'static, IQ: ImportQueue + 'static, { @@ -173,7 +173,7 @@ where + 'static, for<'a> &'a Client: BlockImport, Backend: BackendT + 'static, - RCInterface: RelayChainInterface + Sync + Send + Clone + 'static, + RCInterface: RelayChainInterface + Clone + 'static, { let consensus = cumulus_client_consensus_common::run_parachain_consensus( para_id, diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 43d2c4cb03c..78879be5f87 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -17,7 +17,7 @@ use cumulus_client_network::build_block_announce_validator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; -use cumulus_primitives_core::{relay_chain::v1::Block as PBlock, ParaId}; +use cumulus_primitives_core::ParaId; use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; // Substrate Imports @@ -217,7 +217,7 @@ where Option<&Registry>, Option, &TaskManager, - Arc + Sync + Send>, + Arc, Arc< sc_transaction_pool::FullPool< Block, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 91f8da19de5..890103a18dc 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -25,7 +25,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::{ - relay_chain::v1::{Block as PBlock, Hash as PHash, PersistedValidationData}, + relay_chain::v1::{Hash as PHash, PersistedValidationData}, ParaId, }; use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; @@ -300,7 +300,7 @@ where Option<&Registry>, Option, &TaskManager, - Arc + Sync + Send>, + Arc, Arc< sc_transaction_pool::FullPool< Block, @@ -475,7 +475,7 @@ where Option<&Registry>, Option, &TaskManager, - Arc + Sync + Send>, + Arc, Arc< sc_transaction_pool::FullPool< Block, diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 1a4cd3fc7f7..1b5a3d8e605 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -19,7 +19,7 @@ use crate::ParachainInherentData; use codec::Decode; use cumulus_primitives_core::{ - relay_chain::{self, v1::HrmpChannelId, Block as PBlock, Hash as PHash}, + relay_chain::{self, v1::HrmpChannelId, Hash as PHash}, ParaId, PersistedValidationData, }; use cumulus_relay_chain_interface::RelayChainInterface; @@ -30,7 +30,7 @@ const LOG_TARGET: &str = "parachain-inherent"; /// Collect the relevant relay chain state in form of a proof for putting it into the validation /// data inherent. fn collect_relay_storage_proof( - relay_chain_interface: &impl RelayChainInterface, + relay_chain_interface: &impl RelayChainInterface, para_id: ParaId, relay_parent: PHash, ) -> Option { @@ -96,15 +96,12 @@ impl ParachainInherentData { /// Create the [`ParachainInherentData`] at the given `relay_parent`. /// /// Returns `None` if the creation failed. - pub fn create_at( + pub fn create_at( relay_parent: PHash, - relay_chain_interface: &RCInterface, + relay_chain_interface: &impl RelayChainInterface, validation_data: &PersistedValidationData, para_id: ParaId, - ) -> Option - where - RCInterface: RelayChainInterface, - { + ) -> Option { let relay_chain_state = collect_relay_storage_proof(relay_chain_interface, para_id, relay_parent)?; From f020868ec7d17c5f21df90139cfa87be534881be Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 13 Dec 2021 18:21:00 +0100 Subject: [PATCH 43/56] Introduce constructor for RelayChainLocal --- .../network/src/wait_on_relay_chain_block.rs | 12 ++++---- client/relay-chain-interface/src/lib.rs | 30 +++++++++++-------- test/service/src/lib.rs | 12 ++++---- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index c1443cd24db..367537b5b1f 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -159,12 +159,12 @@ mod tests { ( client.clone(), block, - RelayChainLocal { - full_client: client, - backend: backend.clone(), - sync_oracle: Arc::new(Mutex::new(dummy_network)), - overseer_handle: None, - }, + RelayChainLocal::new( + client, + backend.clone(), + Arc::new(Mutex::new(dummy_network)), + None, + ), ) } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index d6d122ad8d0..6c169c2ba37 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -120,10 +120,21 @@ pub trait RelayChainInterface: Send + Sync { /// RelayChainLocal is used to interact with a full node that is running locally /// in the same process. pub struct RelayChainLocal { - pub full_client: Arc, - pub backend: Arc, - pub sync_oracle: Arc>>, - pub overseer_handle: Option, + full_client: Arc, + backend: Arc, + sync_oracle: Arc>>, + overseer_handle: Option, +} + +impl RelayChainLocal { + pub fn new( + full_client: Arc, + backend: Arc, + sync_oracle: Arc>>, + overseer_handle: Option, + ) -> Self { + Self { full_client, backend, sync_oracle, overseer_handle } + } } impl Clone for RelayChainLocal { @@ -345,7 +356,7 @@ where /// The builder takes a [`polkadot_client::Client`] /// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] /// the builder gets access to this concrete instance and instantiates a RelayChainLocal with it. -pub struct RelayChainLocalBuilder { +struct RelayChainLocalBuilder { polkadot_client: polkadot_client::Client, backend: Arc, sync_oracle: Arc>>, @@ -372,12 +383,7 @@ impl ExecuteWithClient for RelayChainLocalBuilder { + Send, Client::Api: ParachainHost + BabeApi, { - Arc::new(RelayChainLocal { - full_client: client, - backend: self.backend, - sync_oracle: self.sync_oracle, - overseer_handle: self.overseer_handle, - }) + Arc::new(RelayChainLocal::new(client, self.backend, self.sync_oracle, self.overseer_handle)) } } @@ -482,7 +488,7 @@ where /// Build the Polkadot full node using the given `config`. #[sc_tracing::logging::prefix_logs_with("Relaychain")] -pub fn build_polkadot_full_node( +fn build_polkadot_full_node( config: Configuration, telemetry_worker_handle: Option, ) -> Result<(NewFull, CollatorPair), polkadot_service::Error> { diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 3155af40761..f8975178d8c 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -218,12 +218,12 @@ where let client = params.client.clone(); let backend = params.backend.clone(); - let relay_chain_interface = Arc::new(RelayChainLocal { - full_client: relay_chain_full_node.client.clone(), - backend: relay_chain_full_node.backend.clone(), - sync_oracle: Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), - overseer_handle: relay_chain_full_node.overseer_handle.clone(), - }); + let relay_chain_interface = Arc::new(RelayChainLocal::new( + relay_chain_full_node.client.clone(), + relay_chain_full_node.backend.clone(), + Arc::new(Mutex::new(Box::new(relay_chain_full_node.network.clone()))), + relay_chain_full_node.overseer_handle.clone(), + )); task_manager.add_child(relay_chain_full_node.task_manager); let block_announce_validator = From f5967b37a6e7792a03b7717314accd8d0cafe741 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 20 Dec 2021 11:44:55 +0100 Subject: [PATCH 44/56] Extract relay-chain-local to own crate --- Cargo.lock | 21 +- client/network/Cargo.toml | 1 + .../network/src/wait_on_relay_chain_block.rs | 2 +- client/relay-chain-interface/Cargo.toml | 10 - client/relay-chain-interface/src/lib.rs | 347 +---------------- client/relay-chain-local/Cargo.toml | 31 ++ client/relay-chain-local/src/lib.rs | 366 ++++++++++++++++++ parachain-template/node/Cargo.toml | 1 + parachain-template/node/src/service.rs | 3 +- polkadot-parachains/Cargo.toml | 1 + polkadot-parachains/src/service.rs | 3 +- test/service/Cargo.toml | 2 +- test/service/src/lib.rs | 2 +- 13 files changed, 433 insertions(+), 357 deletions(-) create mode 100644 client/relay-chain-local/Cargo.toml create mode 100644 client/relay-chain-local/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e2c8fc1260d..768192e9682 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1528,6 +1528,7 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "cumulus-relay-chain-interface", + "cumulus-relay-chain-local", "cumulus-test-service", "derive_more", "futures 0.3.18", @@ -1835,6 +1836,22 @@ name = "cumulus-relay-chain-interface" version = "0.1.0" dependencies = [ "cumulus-primitives-core", + "parking_lot 0.11.2", + "polkadot-service", + "sc-client-api", + "sp-api", + "sp-blockchain", + "sp-core", + "sp-runtime", + "sp-state-machine", +] + +[[package]] +name = "cumulus-relay-chain-local" +version = "0.1.0" +dependencies = [ + "cumulus-primitives-core", + "cumulus-relay-chain-interface", "enum_dispatch", "futures 0.3.18", "parking_lot 0.11.2", @@ -1950,7 +1967,7 @@ dependencies = [ "cumulus-client-service", "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", - "cumulus-relay-chain-interface", + "cumulus-relay-chain-local", "cumulus-test-relay-validation-worker-provider", "cumulus-test-runtime", "frame-system", @@ -6054,6 +6071,7 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-relay-chain-interface", + "cumulus-relay-chain-local", "derive_more", "frame-benchmarking", "frame-benchmarking-cli", @@ -6699,6 +6717,7 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-relay-chain-interface", + "cumulus-relay-chain-local", "frame-benchmarking", "frame-benchmarking-cli", "futures 0.3.18", diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 37ac1aa0b00..59760d377cc 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -21,6 +21,7 @@ polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", bra polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-relay-chain-interface = { path = "../relay-chain-interface" } +cumulus-relay-chain-local = { path = "../relay-chain-local" } # other deps codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index 367537b5b1f..ab7fc7b8698 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -121,7 +121,7 @@ mod tests { use super::*; - use cumulus_relay_chain_interface::RelayChainLocal; + use cumulus_relay_chain_local::RelayChainLocal; use polkadot_primitives::v1::Block as PBlock; use polkadot_test_client::{ construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 1d2cf0242c3..459fcdd495d 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" [dependencies] -polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-primitives-core = { path = "../../primitives/core" } @@ -15,16 +14,7 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } -sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } -sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } -sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } -sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } -sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "master" } -sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } -futures = { version = "0.3.1", features = ["compat"] } parking_lot = "0.11.1" -enum_dispatch = "0.3.7" -tracing = "0.1.25" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 6c169c2ba37..398fbc6936a 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -18,29 +18,17 @@ use std::sync::Arc; use cumulus_primitives_core::{ relay_chain::{ - v1::{ - CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost, SessionIndex, - ValidatorId, - }, + v1::{CommittedCandidateReceipt, OccupiedCoreAssumption, SessionIndex, ValidatorId}, Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; -use polkadot_service::{ - AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, -}; -use sc_client_api::{ - blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, StorageProof, UsageProvider, -}; -use sc_telemetry::TelemetryWorkerHandle; -use sp_api::{ApiError, ProvideRuntimeApi}; -use sp_consensus::SyncOracle; -use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; -use sp_state_machine::{Backend as StateBackend, StorageValue}; -use std::sync::Mutex; +use polkadot_service::Handle; +use sc_client_api::{blockchain::BlockStatus, StorageProof}; -const LOG_TARGET: &str = "relay-chain-interface"; +use sp_api::ApiError; +use sp_core::sp_std::collections::btree_map::BTreeMap; +use sp_state_machine::StorageValue; /// Should be used for all interaction with the relay chain in cumulus. pub trait RelayChainInterface: Send + Sync { @@ -117,276 +105,6 @@ pub trait RelayChainInterface: Send + Sync { ) -> Result, Box>; } -/// RelayChainLocal is used to interact with a full node that is running locally -/// in the same process. -pub struct RelayChainLocal { - full_client: Arc, - backend: Arc, - sync_oracle: Arc>>, - overseer_handle: Option, -} - -impl RelayChainLocal { - pub fn new( - full_client: Arc, - backend: Arc, - sync_oracle: Arc>>, - overseer_handle: Option, - ) -> Self { - Self { full_client, backend, sync_oracle, overseer_handle } - } -} - -impl Clone for RelayChainLocal { - fn clone(&self) -> Self { - Self { - full_client: self.full_client.clone(), - backend: self.backend.clone(), - sync_oracle: self.sync_oracle.clone(), - overseer_handle: self.overseer_handle.clone(), - } - } -} - -impl RelayChainInterface for RelayChainLocal -where - Client: ProvideRuntimeApi - + BlockchainEvents - + AuxStore - + UsageProvider - + Sync - + Send, - Client::Api: ParachainHost + BabeApi, -{ - fn retrieve_dmq_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option> { - self.full_client - .runtime_api() - .dmq_contents_with_context( - &BlockId::hash(relay_parent), - sp_core::ExecutionContext::Importing, - para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "An error occured during requesting the downward messages.", - ); - }) - .ok() - } - - fn retrieve_all_inbound_hrmp_channel_contents( - &self, - para_id: ParaId, - relay_parent: PHash, - ) -> Option>> { - self.full_client - .runtime_api() - .inbound_hrmp_channels_contents_with_context( - &BlockId::hash(relay_parent), - sp_core::ExecutionContext::Importing, - para_id, - ) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?relay_parent, - error = ?e, - "An error occured during requesting the inbound HRMP messages.", - ); - }) - .ok() - } - - fn persisted_validation_data( - &self, - block_id: &BlockId, - para_id: ParaId, - occupied_core_assumption: OccupiedCoreAssumption, - ) -> Result, ApiError> { - self.full_client.runtime_api().persisted_validation_data( - block_id, - para_id, - occupied_core_assumption, - ) - } - - fn candidate_pending_availability( - &self, - block_id: &BlockId, - para_id: ParaId, - ) -> Result, ApiError> { - self.full_client.runtime_api().candidate_pending_availability(block_id, para_id) - } - - fn session_index_for_child(&self, block_id: &BlockId) -> Result { - self.full_client.runtime_api().session_index_for_child(block_id) - } - - fn validators(&self, block_id: &BlockId) -> Result, ApiError> { - self.full_client.runtime_api().validators(block_id) - } - - fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { - self.full_client.import_notification_stream() - } - - fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { - self.full_client.finality_notification_stream() - } - - fn storage_changes_notification_stream( - &self, - filter_keys: Option<&[sc_client_api::StorageKey]>, - child_filter_keys: Option< - &[(sc_client_api::StorageKey, Option>)], - >, - ) -> sc_client_api::blockchain::Result> { - self.full_client - .storage_changes_notification_stream(filter_keys, child_filter_keys) - } - - fn best_block_hash(&self) -> PHash { - self.backend.blockchain().info().best_hash - } - - fn block_status(&self, block_id: BlockId) -> Result { - self.backend.blockchain().status(block_id) - } - - fn is_major_syncing(&self) -> bool { - let mut network = self.sync_oracle.lock().unwrap(); - network.is_major_syncing() - } - - fn overseer_handle(&self) -> Option { - self.overseer_handle.clone() - } - - fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { - let state = self - .backend - .state_at(*block_id) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?block_id, - error = ?e, - "Cannot obtain the state of the relay chain.", - ) - }) - .ok()?; - state - .storage(key) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - error = ?e, - "Cannot decode the hrmp ingress channel index.", - ) - }) - .ok()? - } - - fn prove_read( - &self, - block_id: &BlockId, - relevant_keys: &Vec>, - ) -> Result, Box> { - let state_backend = self - .backend - .state_at(*block_id) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?block_id, - error = ?e, - "Cannot obtain the state of the relay chain.", - ); - }) - .ok(); - - match state_backend { - Some(state) => sp_state_machine::prove_read(state, relevant_keys) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?block_id, - error = ?e, - "Failed to collect required relay chain state storage proof.", - ); - e - }) - .map(|v| Some(v)), - None => Ok(None), - } - } - - fn check_block_in_chain( - &self, - block_id: BlockId, - ) -> Result>, sp_blockchain::Error> { - let _lock = self.backend.get_import_lock(); - - match self.backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(None), - Err(err) => return Err(err), - _ => {}, - } - - let listener = self.full_client.import_notification_stream(); - - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); - - Ok(Some(listener)) - } -} - -/// Builder for a concrete relay chain interface, creatd from a full node. Builds -/// a [`RelayChainLocal`] to access relay chain data necessary for parachain operation. -/// -/// The builder takes a [`polkadot_client::Client`] -/// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance and instantiates a RelayChainLocal with it. -struct RelayChainLocalBuilder { - polkadot_client: polkadot_client::Client, - backend: Arc, - sync_oracle: Arc>>, - overseer_handle: Option, -} - -impl RelayChainLocalBuilder { - pub fn build(self) -> Arc { - self.polkadot_client.clone().execute_with(self) - } -} - -impl ExecuteWithClient for RelayChainLocalBuilder { - type Output = Arc; - - fn execute_with_client(self, client: Arc) -> Self::Output - where - Client: ProvideRuntimeApi - + BlockchainEvents - + AuxStore - + UsageProvider - + 'static - + Sync - + Send, - Client::Api: ParachainHost + BabeApi, - { - Arc::new(RelayChainLocal::new(client, self.backend, self.sync_oracle, self.overseer_handle)) - } -} - impl RelayChainInterface for Arc where T: RelayChainInterface + ?Sized, @@ -485,56 +203,3 @@ where (**self).check_block_in_chain(block_id) } } - -/// Build the Polkadot full node using the given `config`. -#[sc_tracing::logging::prefix_logs_with("Relaychain")] -fn build_polkadot_full_node( - config: Configuration, - telemetry_worker_handle: Option, -) -> Result<(NewFull, CollatorPair), polkadot_service::Error> { - let is_light = matches!(config.role, Role::Light); - if is_light { - Err(polkadot_service::Error::Sub("Light client not supported.".into())) - } else { - let collator_key = CollatorPair::generate().0; - - let relay_chain_full_node = polkadot_service::build_full( - config, - polkadot_service::IsCollator::Yes(collator_key.clone()), - None, - true, - None, - telemetry_worker_handle, - polkadot_service::RealOverseerGen, - )?; - - Ok((relay_chain_full_node, collator_key)) - } -} - -/// Builds a relay chain interface by constructing a full relay chain node -pub fn build_relay_chain_interface( - polkadot_config: Configuration, - telemetry_worker_handle: Option, - task_manager: &mut TaskManager, -) -> Result<(Arc<(dyn RelayChainInterface + 'static)>, CollatorPair), polkadot_service::Error> { - let (full_node, collator_key) = - build_polkadot_full_node(polkadot_config, telemetry_worker_handle).map_err( - |e| match e { - polkadot_service::Error::Sub(x) => x, - s => format!("{}", s).into(), - }, - )?; - - let sync_oracle: Box = Box::new(full_node.network.clone()); - let sync_oracle = Arc::new(Mutex::new(sync_oracle)); - let relay_chain_interface_builder = RelayChainLocalBuilder { - polkadot_client: full_node.client.clone(), - backend: full_node.backend.clone(), - sync_oracle, - overseer_handle: full_node.overseer_handle.clone(), - }; - task_manager.add_child(full_node.task_manager); - - Ok((relay_chain_interface_builder.build(), collator_key)) -} diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml new file mode 100644 index 00000000000..099b13abde3 --- /dev/null +++ b/client/relay-chain-local/Cargo.toml @@ -0,0 +1,31 @@ +[package] +authors = ["Parity Technologies "] +name = "cumulus-relay-chain-local" +version = "0.1.0" +edition = "2021" + + +[dependencies] +polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } + +cumulus-primitives-core = { path = "../../primitives/core" } +cumulus-relay-chain-interface = { path = "../relay-chain-interface" } + +sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } +sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "master" } +sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } + +futures = { version = "0.3.1", features = ["compat"] } +parking_lot = "0.11.1" +enum_dispatch = "0.3.7" +tracing = "0.1.25" diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs new file mode 100644 index 00000000000..7020a81d640 --- /dev/null +++ b/client/relay-chain-local/src/lib.rs @@ -0,0 +1,366 @@ +// Copyright 2021 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +use std::sync::Arc; + +use cumulus_primitives_core::{ + relay_chain::{ + v1::{ + CommittedCandidateReceipt, OccupiedCoreAssumption, ParachainHost, SessionIndex, + ValidatorId, + }, + Block as PBlock, BlockId, Hash as PHash, InboundHrmpMessage, + }, + InboundDownwardMessage, ParaId, PersistedValidationData, +}; +use cumulus_relay_chain_interface::RelayChainInterface; +use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; +use polkadot_service::{ + AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, +}; +use sc_client_api::{ + blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, StorageProof, UsageProvider, +}; +use sc_telemetry::TelemetryWorkerHandle; +use sp_api::{ApiError, ProvideRuntimeApi}; +use sp_consensus::SyncOracle; +use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; +use sp_state_machine::{Backend as StateBackend, StorageValue}; +use std::sync::Mutex; +const LOG_TARGET: &str = "relay-chain-local"; + +/// RelayChainLocal is used to interact with a full node that is running locally +/// in the same process. +pub struct RelayChainLocal { + full_client: Arc, + backend: Arc, + sync_oracle: Arc>>, + overseer_handle: Option, +} + +impl RelayChainLocal { + pub fn new( + full_client: Arc, + backend: Arc, + sync_oracle: Arc>>, + overseer_handle: Option, + ) -> Self { + Self { full_client, backend, sync_oracle, overseer_handle } + } +} + +impl Clone for RelayChainLocal { + fn clone(&self) -> Self { + Self { + full_client: self.full_client.clone(), + backend: self.backend.clone(), + sync_oracle: self.sync_oracle.clone(), + overseer_handle: self.overseer_handle.clone(), + } + } +} + +impl RelayChainInterface for RelayChainLocal +where + Client: ProvideRuntimeApi + + BlockchainEvents + + AuxStore + + UsageProvider + + Sync + + Send, + Client::Api: ParachainHost + BabeApi, +{ + fn retrieve_dmq_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option> { + self.full_client + .runtime_api() + .dmq_contents_with_context( + &BlockId::hash(relay_parent), + sp_core::ExecutionContext::Importing, + para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?relay_parent, + error = ?e, + "An error occured during requesting the downward messages.", + ); + }) + .ok() + } + + fn retrieve_all_inbound_hrmp_channel_contents( + &self, + para_id: ParaId, + relay_parent: PHash, + ) -> Option>> { + self.full_client + .runtime_api() + .inbound_hrmp_channels_contents_with_context( + &BlockId::hash(relay_parent), + sp_core::ExecutionContext::Importing, + para_id, + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?relay_parent, + error = ?e, + "An error occured during requesting the inbound HRMP messages.", + ); + }) + .ok() + } + + fn persisted_validation_data( + &self, + block_id: &BlockId, + para_id: ParaId, + occupied_core_assumption: OccupiedCoreAssumption, + ) -> Result, ApiError> { + self.full_client.runtime_api().persisted_validation_data( + block_id, + para_id, + occupied_core_assumption, + ) + } + + fn candidate_pending_availability( + &self, + block_id: &BlockId, + para_id: ParaId, + ) -> Result, ApiError> { + self.full_client.runtime_api().candidate_pending_availability(block_id, para_id) + } + + fn session_index_for_child(&self, block_id: &BlockId) -> Result { + self.full_client.runtime_api().session_index_for_child(block_id) + } + + fn validators(&self, block_id: &BlockId) -> Result, ApiError> { + self.full_client.runtime_api().validators(block_id) + } + + fn import_notification_stream(&self) -> sc_client_api::ImportNotifications { + self.full_client.import_notification_stream() + } + + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications { + self.full_client.finality_notification_stream() + } + + fn storage_changes_notification_stream( + &self, + filter_keys: Option<&[sc_client_api::StorageKey]>, + child_filter_keys: Option< + &[(sc_client_api::StorageKey, Option>)], + >, + ) -> sc_client_api::blockchain::Result> { + self.full_client + .storage_changes_notification_stream(filter_keys, child_filter_keys) + } + + fn best_block_hash(&self) -> PHash { + self.backend.blockchain().info().best_hash + } + + fn block_status(&self, block_id: BlockId) -> Result { + self.backend.blockchain().status(block_id) + } + + fn is_major_syncing(&self) -> bool { + let mut network = self.sync_oracle.lock().unwrap(); + network.is_major_syncing() + } + + fn overseer_handle(&self) -> Option { + self.overseer_handle.clone() + } + + fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { + let state = self + .backend + .state_at(*block_id) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Cannot obtain the state of the relay chain.", + ) + }) + .ok()?; + state + .storage(key) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + error = ?e, + "Cannot decode the hrmp ingress channel index.", + ) + }) + .ok()? + } + + fn prove_read( + &self, + block_id: &BlockId, + relevant_keys: &Vec>, + ) -> Result, Box> { + let state_backend = self + .backend + .state_at(*block_id) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Cannot obtain the state of the relay chain.", + ); + }) + .ok(); + + match state_backend { + Some(state) => sp_state_machine::prove_read(state, relevant_keys) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?block_id, + error = ?e, + "Failed to collect required relay chain state storage proof.", + ); + e + }) + .map(|v| Some(v)), + None => Ok(None), + } + } + + fn check_block_in_chain( + &self, + block_id: BlockId, + ) -> Result>, sp_blockchain::Error> { + let _lock = self.backend.get_import_lock(); + + match self.backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(None), + Err(err) => return Err(err), + _ => {}, + } + + let listener = self.full_client.import_notification_stream(); + + // Now it is safe to drop the lock, even when the block is now imported, it should show + // up in our registered listener. + drop(_lock); + + Ok(Some(listener)) + } +} + +/// Builder for a concrete relay chain interface, creatd from a full node. Builds +/// a [`RelayChainLocal`] to access relay chain data necessary for parachain operation. +/// +/// The builder takes a [`polkadot_client::Client`] +/// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] +/// the builder gets access to this concrete instance and instantiates a RelayChainLocal with it. +struct RelayChainLocalBuilder { + polkadot_client: polkadot_client::Client, + backend: Arc, + sync_oracle: Arc>>, + overseer_handle: Option, +} + +impl RelayChainLocalBuilder { + pub fn build(self) -> Arc { + self.polkadot_client.clone().execute_with(self) + } +} + +impl ExecuteWithClient for RelayChainLocalBuilder { + type Output = Arc; + + fn execute_with_client(self, client: Arc) -> Self::Output + where + Client: ProvideRuntimeApi + + BlockchainEvents + + AuxStore + + UsageProvider + + 'static + + Sync + + Send, + Client::Api: ParachainHost + BabeApi, + { + Arc::new(RelayChainLocal::new(client, self.backend, self.sync_oracle, self.overseer_handle)) + } +} + +/// Build the Polkadot full node using the given `config`. +#[sc_tracing::logging::prefix_logs_with("Relaychain")] +fn build_polkadot_full_node( + config: Configuration, + telemetry_worker_handle: Option, +) -> Result<(NewFull, CollatorPair), polkadot_service::Error> { + let is_light = matches!(config.role, Role::Light); + if is_light { + Err(polkadot_service::Error::Sub("Light client not supported.".into())) + } else { + let collator_key = CollatorPair::generate().0; + + let relay_chain_full_node = polkadot_service::build_full( + config, + polkadot_service::IsCollator::Yes(collator_key.clone()), + None, + true, + None, + telemetry_worker_handle, + polkadot_service::RealOverseerGen, + )?; + + Ok((relay_chain_full_node, collator_key)) + } +} + +/// Builds a relay chain interface by constructing a full relay chain node +pub fn build_relay_chain_interface( + polkadot_config: Configuration, + telemetry_worker_handle: Option, + task_manager: &mut TaskManager, +) -> Result<(Arc<(dyn RelayChainInterface + 'static)>, CollatorPair), polkadot_service::Error> { + let (full_node, collator_key) = + build_polkadot_full_node(polkadot_config, telemetry_worker_handle).map_err( + |e| match e { + polkadot_service::Error::Sub(x) => x, + s => format!("{}", s).into(), + }, + )?; + + let sync_oracle: Box = Box::new(full_node.network.clone()); + let sync_oracle = Arc::new(Mutex::new(sync_oracle)); + let relay_chain_interface_builder = RelayChainLocalBuilder { + polkadot_client: full_node.client.clone(), + backend: full_node.backend.clone(), + sync_oracle, + overseer_handle: full_node.overseer_handle.clone(), + }; + task_manager.add_child(full_node.task_manager); + + Ok((relay_chain_interface_builder.build(), collator_key)) +} diff --git a/parachain-template/node/Cargo.toml b/parachain-template/node/Cargo.toml index 8cafbc5422c..144c9f811b9 100644 --- a/parachain-template/node/Cargo.toml +++ b/parachain-template/node/Cargo.toml @@ -89,6 +89,7 @@ cumulus-client-service = { path = "../../client/service" } cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface" } +cumulus-relay-chain-local = { path = "../../client/relay-chain-local" } # Polkadot dependencies polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index cd108e1d831..179550d8658 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -18,7 +18,8 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; +use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_local::build_relay_chain_interface; // Substrate Imports use core::time::Duration; diff --git a/polkadot-parachains/Cargo.toml b/polkadot-parachains/Cargo.toml index f4f30a57115..4e3a283d685 100644 --- a/polkadot-parachains/Cargo.toml +++ b/polkadot-parachains/Cargo.toml @@ -78,6 +78,7 @@ cumulus-client-collator = { path = "../client/collator" } cumulus-primitives-core = { path = "../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../primitives/parachain-inherent" } cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" } +cumulus-relay-chain-local = { path = "../client/relay-chain-local" } # Polkadot dependencies polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 2f78eb5909b..08d461d507a 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -28,7 +28,8 @@ use cumulus_primitives_core::{ relay_chain::v1::{Hash as PHash, PersistedValidationData}, ParaId, }; -use cumulus_relay_chain_interface::{build_relay_chain_interface, RelayChainInterface}; +use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_local::build_relay_chain_interface; use polkadot_service::NativeExecutionDispatch; use crate::rpc; diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index 8d4815b9ac2..3d11f0a060a 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -54,7 +54,7 @@ cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } cumulus-test-runtime = { path = "../runtime" } cumulus-test-relay-validation-worker-provider = { path = "../relay-validation-worker-provider" } -cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface" } +cumulus-relay-chain-local = { path = "../../client/relay-chain-local" } criterion = { version = "0.3.5", features = [ "async_tokio" ] } diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index f8975178d8c..fd3cf4faa13 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -29,7 +29,7 @@ use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; use cumulus_primitives_core::ParaId; -use cumulus_relay_chain_interface::RelayChainLocal; +use cumulus_relay_chain_local::RelayChainLocal; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use frame_system_rpc_runtime_api::AccountNonceApi; From 4e6fbad0a7921b23379e338c48a15cdb12cf62da Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 20 Dec 2021 12:35:11 +0100 Subject: [PATCH 45/56] Remove polkadot-service dependency from relay-chain-interface Introduce enum as return type for block checking --- Cargo.lock | 2 +- client/network/src/tests.rs | 5 +++-- .../network/src/wait_on_relay_chain_block.rs | 6 +++--- client/relay-chain-interface/Cargo.toml | 2 +- client/relay-chain-interface/src/lib.rs | 19 ++++++++++++------- client/relay-chain-local/src/lib.rs | 8 ++++---- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 768192e9682..5837dc94edc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1837,7 +1837,7 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "parking_lot 0.11.2", - "polkadot-service", + "polkadot-overseer", "sc-client-api", "sp-api", "sp-blockchain", diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 8aa493e517c..52543548f91 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -15,6 +15,7 @@ // along with Polkadot. If not, see . use super::*; +use cumulus_relay_chain_interface::BlockCheckResult; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll}; use parking_lot::Mutex; @@ -204,7 +205,7 @@ impl RelayChainInterface for DummyRelayChainInterface { let _lock = self.relay_backend.get_import_lock(); match self.relay_backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(None), + Ok(BlockStatus::InChain) => return Ok(BlockCheckResult::InChain), Err(err) => return Err(err), _ => {}, } @@ -215,7 +216,7 @@ impl RelayChainInterface for DummyRelayChainInterface { // up in our registered listener. drop(_lock); - Ok(Some(listener)) + Ok(BlockCheckResult::NotFound(listener)) } } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index ab7fc7b8698..abdf53ea6ca 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -16,7 +16,7 @@ //! Provides the [`WaitOnRelayChainBlock`] type. -use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface}; use futures::{future::ready, Future, FutureExt, StreamExt}; use polkadot_primitives::v1::Hash as PHash; use sc_client_api::blockchain; @@ -91,8 +91,8 @@ where ) -> impl Future> { let mut listener = match self.relay_chain_interface.check_block_in_chain(BlockId::Hash(hash)) { - Ok(Some(listener)) => listener, - Ok(None) => return ready(Ok(())).boxed(), + Ok(BlockCheckResult::NotFound(listener)) => listener, + Ok(BlockCheckResult::InChain) => return ready(Ok(())).boxed(), Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(), }; diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 459fcdd495d..266c36e32f9 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] -polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-primitives-core = { path = "../../primitives/core" } diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 398fbc6936a..b2cfebaa290 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -23,13 +23,18 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use polkadot_service::Handle; +use polkadot_overseer::Handle as OverseerHandle; use sc_client_api::{blockchain::BlockStatus, StorageProof}; use sp_api::ApiError; use sp_core::sp_std::collections::btree_map::BTreeMap; use sp_state_machine::StorageValue; +pub enum BlockCheckResult { + InChain, + NotFound(sc_client_api::ImportNotifications), +} + /// Should be used for all interaction with the relay chain in cumulus. pub trait RelayChainInterface: Send + Sync { fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option; @@ -77,12 +82,12 @@ pub trait RelayChainInterface: Send + Sync { fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; - /// Check if block is in chain. If it is, we return nothing. - /// If it is not in the chain, we return a listener that can be used to wait on the block. + /// Check if block is in chain. If it is, we return BlockCheckResult::InChain. + /// If it is not in the chain, we return BlockCheckResult::NotFound with a listener that can be used to wait on the block. fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error>; + ) -> Result; fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; @@ -96,7 +101,7 @@ pub trait RelayChainInterface: Send + Sync { fn is_major_syncing(&self) -> bool; - fn overseer_handle(&self) -> Option; + fn overseer_handle(&self) -> Option; fn prove_read( &self, @@ -180,7 +185,7 @@ where (**self).is_major_syncing() } - fn overseer_handle(&self) -> Option { + fn overseer_handle(&self) -> Option { (**self).overseer_handle() } @@ -199,7 +204,7 @@ where fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error> { + ) -> Result { (**self).check_block_in_chain(block_id) } } diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs index 7020a81d640..906417c025f 100644 --- a/client/relay-chain-local/src/lib.rs +++ b/client/relay-chain-local/src/lib.rs @@ -26,7 +26,7 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use cumulus_relay_chain_interface::RelayChainInterface; +use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface}; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use polkadot_service::{ AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, @@ -256,11 +256,11 @@ where fn check_block_in_chain( &self, block_id: BlockId, - ) -> Result>, sp_blockchain::Error> { + ) -> Result { let _lock = self.backend.get_import_lock(); match self.backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(None), + Ok(BlockStatus::InChain) => return Ok(BlockCheckResult::InChain), Err(err) => return Err(err), _ => {}, } @@ -271,7 +271,7 @@ where // up in our registered listener. drop(_lock); - Ok(Some(listener)) + Ok(BlockCheckResult::NotFound(listener)) } } From 4768ad9f99d6312fe4f2d12c021123baf6a6f0bb Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 20 Dec 2021 12:41:47 +0100 Subject: [PATCH 46/56] Fix test --- client/network/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 52543548f91..36cf5be4978 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -201,7 +201,7 @@ impl RelayChainInterface for DummyRelayChainInterface { fn check_block_in_chain( &self, block_id: polkadot_service::BlockId, - ) -> Result>, sp_blockchain::Error> { + ) -> Result { let _lock = self.relay_backend.get_import_lock(); match self.relay_backend.blockchain().status(block_id) { From dd64161ad232d6c930c6206fc8341305a6221fc9 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 20 Dec 2021 13:46:23 +0100 Subject: [PATCH 47/56] Move relay-chain-local to dev dependency in network --- client/network/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 59760d377cc..4d7d2c7dd4f 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -21,7 +21,6 @@ polkadot-node-primitives = { git = "https://github.com/paritytech/polkadot", bra polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" } cumulus-relay-chain-interface = { path = "../relay-chain-interface" } -cumulus-relay-chain-local = { path = "../relay-chain-local" } # other deps codec = { package = "parity-scale-codec", version = "2.3.0", features = [ "derive" ] } @@ -37,6 +36,8 @@ tokio = { version = "1.10", features = ["macros"] } # Cumulus deps cumulus-test-service = { path = "../../test/service" } cumulus-primitives-core = { path = "../../primitives/core" } +cumulus-relay-chain-local = { path = "../relay-chain-local" } + # Polkadot deps polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } From b1b07b5ebd1467bd2bf9797f2a0984aa4226dcc6 Mon Sep 17 00:00:00 2001 From: skunert Date: Mon, 20 Dec 2021 14:12:45 +0100 Subject: [PATCH 48/56] Remove unused dependency --- Cargo.lock | 13 ------------- client/relay-chain-local/Cargo.toml | 1 - 2 files changed, 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0575475dd5f..49b6054e881 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1853,7 +1853,6 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "cumulus-relay-chain-interface", - "enum_dispatch", "futures 0.3.18", "parking_lot 0.11.2", "polkadot-client", @@ -2247,18 +2246,6 @@ dependencies = [ "syn", ] -[[package]] -name = "enum_dispatch" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" -dependencies = [ - "once_cell", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "enumflags2" version = "0.6.4" diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index 099b13abde3..ed1d9f0860f 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -27,5 +27,4 @@ sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master futures = { version = "0.3.1", features = ["compat"] } parking_lot = "0.11.1" -enum_dispatch = "0.3.7" tracing = "0.1.25" From 9ccc9bf9fb197601c99bec021d97a1c18b72a0f0 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 21 Dec 2021 13:58:50 +0100 Subject: [PATCH 49/56] Remove unused relay-chain reference in AuraConsensus Improve get_storage_value method with error handling --- Cargo.lock | 3 +- client/consensus/aura/Cargo.toml | 1 - client/consensus/aura/src/lib.rs | 28 ++++----------- client/network/src/tests.rs | 3 +- .../network/src/wait_on_relay_chain_block.rs | 2 +- client/relay-chain-interface/src/lib.rs | 12 +++++-- client/relay-chain-local/Cargo.toml | 1 - client/relay-chain-local/src/lib.rs | 34 +++++-------------- parachain-template/node/src/service.rs | 3 -- polkadot-parachains/src/service.rs | 8 ----- .../parachain-inherent/src/client_side.rs | 34 ++++++++++++++----- test/service/Cargo.toml | 2 ++ test/service/src/lib.rs | 3 +- 13 files changed, 60 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49b6054e881..430e4fefd61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1454,7 +1454,6 @@ dependencies = [ "async-trait", "cumulus-client-consensus-common", "cumulus-primitives-core", - "cumulus-relay-chain-interface", "futures 0.3.18", "parity-scale-codec", "sc-client-api", @@ -1853,7 +1852,6 @@ version = "0.1.0" dependencies = [ "cumulus-primitives-core", "cumulus-relay-chain-interface", - "futures 0.3.18", "parking_lot 0.11.2", "polkadot-client", "polkadot-service", @@ -1976,6 +1974,7 @@ dependencies = [ "jsonrpc-core", "pallet-transaction-payment", "parity-scale-codec", + "parking_lot 0.11.2", "polkadot-primitives", "polkadot-service", "polkadot-test-service", diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 66e69377b26..445b42635e3 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -27,7 +27,6 @@ substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate # Cumulus dependencies cumulus-client-consensus-common = { path = "../common" } cumulus-primitives-core = { path = "../../../primitives/core" } -cumulus-relay-chain-interface = { path = "../../relay-chain-interface" } # Other deps futures = { version = "0.3.8", features = ["compat"] } diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index fd1ab3289d7..9eefa92e881 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -27,7 +27,6 @@ use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; use cumulus_primitives_core::{relay_chain::v1::Hash as PHash, PersistedValidationData}; -use cumulus_relay_chain_interface::RelayChainInterface; use futures::lock::Mutex; use sc_client_api::{backend::AuxStore, BlockOf}; @@ -58,9 +57,8 @@ pub use sc_consensus_slots::InherentDataProviderExt; const LOG_TARGET: &str = "aura::cumulus"; /// The implementation of the AURA consensus for parachains. -pub struct AuraConsensus { +pub struct AuraConsensus { create_inherent_data_providers: Arc, - relay_chain_interface: RCInterface, aura_worker: Arc< Mutex< dyn sc_consensus_slots::SlotWorker::Proof> @@ -71,24 +69,19 @@ pub struct AuraConsensus { slot_duration: SlotDuration, } -impl Clone for AuraConsensus -where - RCInterface: Clone + Send + Sync, -{ +impl Clone for AuraConsensus { fn clone(&self) -> Self { Self { create_inherent_data_providers: self.create_inherent_data_providers.clone(), - relay_chain_interface: self.relay_chain_interface.clone(), aura_worker: self.aura_worker.clone(), slot_duration: self.slot_duration, } } } -impl AuraConsensus +impl AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone, CIDP: CreateInherentDataProviders, CIDP::InherentDataProviders: InherentDataProviderExt, { @@ -102,7 +95,6 @@ where backoff_authoring_blocks: Option, keystore: SyncCryptoStorePtr, create_inherent_data_providers: CIDP, - relay_chain_interface: RCInterface, slot_duration: SlotDuration, telemetry: Option, block_proposal_slot_portion: SlotProportion, @@ -146,7 +138,6 @@ where Self { create_inherent_data_providers: Arc::new(create_inherent_data_providers), - relay_chain_interface, aura_worker: Arc::new(Mutex::new(worker)), slot_duration, } @@ -189,10 +180,9 @@ where } #[async_trait::async_trait] -impl ParachainConsensus for AuraConsensus +impl ParachainConsensus for AuraConsensus where B: BlockT, - RCInterface: RelayChainInterface + Clone, CIDP: CreateInherentDataProviders + Send + Sync, CIDP::InherentDataProviders: InherentDataProviderExt + Send, { @@ -225,11 +215,10 @@ where } /// Paramaters of [`build_aura_consensus`]. -pub struct BuildAuraConsensusParams { +pub struct BuildAuraConsensusParams { pub proposer_factory: PF, pub create_inherent_data_providers: CIDP, pub block_import: BI, - pub relay_chain_interface: RCInterface, pub para_client: Arc, pub backoff_authoring_blocks: Option, pub sync_oracle: SO, @@ -244,12 +233,11 @@ pub struct BuildAuraConsensusParams { /// Build the [`AuraConsensus`]. /// /// Returns a boxed [`ParachainConsensus`]. -pub fn build_aura_consensus( +pub fn build_aura_consensus( BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers, block_import, - relay_chain_interface, para_client, backoff_authoring_blocks, sync_oracle, @@ -259,7 +247,7 @@ pub fn build_aura_consensus, + }: BuildAuraConsensusParams, ) -> Box> where Block: BlockT, @@ -294,7 +282,6 @@ where P: Pair + Send + Sync, P::Public: AppPublic + Hash + Member + Encode + Decode, P::Signature: TryFrom> + Hash + Member + Encode + Decode, - RCInterface: RelayChainInterface + Clone + 'static, { Box::new(AuraConsensus::new::( para_client, @@ -305,7 +292,6 @@ where backoff_authoring_blocks, keystore, create_inherent_data_providers, - relay_chain_interface.clone(), slot_duration, telemetry, block_proposal_slot_portion, diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 36cf5be4978..f3c7863d512 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -38,6 +38,7 @@ use sp_core::{Pair, H256}; use sp_keyring::Sr25519Keyring; use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr}; use sp_runtime::RuntimeAppPublic; +use sp_state_machine::StorageValue; use std::collections::BTreeMap; fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) { @@ -186,7 +187,7 @@ impl RelayChainInterface for DummyRelayChainInterface { &self, _: &polkadot_service::BlockId, _: &[u8], - ) -> Option { + ) -> Result, sp_blockchain::Error> { unimplemented!("Not needed for test") } diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index abdf53ea6ca..b32868e38f3 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -117,7 +117,7 @@ where #[cfg(test)] mod tests { - use std::sync::Mutex; + use parking_lot::Mutex; use super::*; diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index b2cfebaa290..45cb673b82a 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -37,7 +37,11 @@ pub enum BlockCheckResult { /// Should be used for all interaction with the relay chain in cumulus. pub trait RelayChainInterface: Send + Sync { - fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option; + fn get_storage_by_key( + &self, + block_id: &BlockId, + key: &[u8], + ) -> Result, sp_blockchain::Error>; fn validators(&self, block_id: &BlockId) -> Result, ApiError>; @@ -189,7 +193,11 @@ where (**self).overseer_handle() } - fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { + fn get_storage_by_key( + &self, + block_id: &BlockId, + key: &[u8], + ) -> Result, sp_blockchain::Error> { (**self).get_storage_by_key(block_id, key) } diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index ed1d9f0860f..f80142522d0 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -25,6 +25,5 @@ sc-service = { git = "https://github.com/paritytech/substrate", branch = "master sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } -futures = { version = "0.3.1", features = ["compat"] } parking_lot = "0.11.1" tracing = "0.1.25" diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs index 906417c025f..6b43b22a53b 100644 --- a/client/relay-chain-local/src/lib.rs +++ b/client/relay-chain-local/src/lib.rs @@ -27,6 +27,7 @@ use cumulus_primitives_core::{ InboundDownwardMessage, ParaId, PersistedValidationData, }; use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface}; +use parking_lot::Mutex; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use polkadot_service::{ AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, @@ -39,7 +40,6 @@ use sp_api::{ApiError, ProvideRuntimeApi}; use sp_consensus::SyncOracle; use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; use sp_state_machine::{Backend as StateBackend, StorageValue}; -use std::sync::Mutex; const LOG_TARGET: &str = "relay-chain-local"; /// RelayChainLocal is used to interact with a full node that is running locally @@ -186,7 +186,7 @@ where } fn is_major_syncing(&self) -> bool { - let mut network = self.sync_oracle.lock().unwrap(); + let mut network = self.sync_oracle.lock(); network.is_major_syncing() } @@ -194,29 +194,13 @@ where self.overseer_handle.clone() } - fn get_storage_by_key(&self, block_id: &BlockId, key: &[u8]) -> Option { - let state = self - .backend - .state_at(*block_id) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - relay_parent = ?block_id, - error = ?e, - "Cannot obtain the state of the relay chain.", - ) - }) - .ok()?; - state - .storage(key) - .map_err(|e| { - tracing::error!( - target: LOG_TARGET, - error = ?e, - "Cannot decode the hrmp ingress channel index.", - ) - }) - .ok()? + fn get_storage_by_key( + &self, + block_id: &BlockId, + key: &[u8], + ) -> Result, sp_blockchain::Error> { + let state = self.backend.state_at(*block_id)?; + state.storage(key).map_err(sp_blockchain::Error::Storage) } fn prove_read( diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 179550d8658..1b2ae7369bf 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -430,7 +430,6 @@ pub async fn start_parachain_node( telemetry.clone(), ); - let relay_chain_interface2 = relay_chain_interface.clone(); Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -441,7 +440,6 @@ pub async fn start_parachain_node( _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -470,7 +468,6 @@ pub async fn start_parachain_node( } }, block_import: client.clone(), - relay_chain_interface: relay_chain_interface2, para_client: client, backoff_authoring_blocks: Option::<()>::None, sync_oracle, diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 08d461d507a..9a4d9cfb8a0 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -719,8 +719,6 @@ pub async fn start_rococo_parachain_node( ); - let relay_chain_interface_for_consensus = relay_chain_interface.clone(); - Ok(build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -731,7 +729,6 @@ pub async fn start_rococo_parachain_node( _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -760,7 +757,6 @@ pub async fn start_rococo_parachain_node( } }, block_import: client.clone(), - relay_chain_interface: relay_chain_interface_for_consensus, para_client: client.clone(), backoff_authoring_blocks: Option::<()>::None, sync_oracle, @@ -1141,8 +1137,6 @@ where telemetry2.clone(), ); - let relay_chain_interface2 = relay_chain_for_aura.clone(); - build_aura_consensus::< sp_consensus_aura::sr25519::AuthorityPair, _, @@ -1153,7 +1147,6 @@ where _, _, _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -1182,7 +1175,6 @@ where } }, block_import: client2.clone(), - relay_chain_interface: relay_chain_interface2, para_client: client2.clone(), backoff_authoring_blocks: Option::<()>::None, sync_oracle, diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index 1b5a3d8e605..b0a1e3effbc 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -37,10 +37,19 @@ fn collect_relay_storage_proof( use relay_chain::well_known_keys as relay_well_known_keys; let relay_parent_block_id = BlockId::Hash(relay_parent); - let ingress_channels = relay_chain_interface.get_storage_by_key( - &relay_parent_block_id, - &relay_well_known_keys::hrmp_ingress_channel_index(para_id), - ); + let ingress_channels = relay_chain_interface + .get_storage_by_key( + &relay_parent_block_id, + &relay_well_known_keys::hrmp_ingress_channel_index(para_id), + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + relay_parent = ?relay_parent_block_id, + error = ?e, "Cannot obtain the hrmp ingress channel." + ) + }) + .ok()?; let ingress_channels = ingress_channels .map(|raw| >::decode(&mut &raw[..])) @@ -55,10 +64,19 @@ fn collect_relay_storage_proof( .ok()? .unwrap_or_default(); - let egress_channels = relay_chain_interface.get_storage_by_key( - &relay_parent_block_id, - &relay_well_known_keys::hrmp_egress_channel_index(para_id), - ); + let egress_channels = relay_chain_interface + .get_storage_by_key( + &relay_parent_block_id, + &relay_well_known_keys::hrmp_egress_channel_index(para_id), + ) + .map_err(|e| { + tracing::error!( + target: LOG_TARGET, + error = ?e, + "Cannot obtain the hrmp egress channel.", + ) + }) + .ok()?; let egress_channels = egress_channels .map(|raw| >::decode(&mut &raw[..])) diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index 3d11f0a060a..37b22d7b31a 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -58,6 +58,8 @@ cumulus-relay-chain-local = { path = "../../client/relay-chain-local" } criterion = { version = "0.3.5", features = [ "async_tokio" ] } +parking_lot = "0.11.1" + # RPC related dependencies jsonrpc-core = "18.0.0" diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index fd3cf4faa13..66d07047b42 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -33,6 +33,7 @@ use cumulus_relay_chain_local::RelayChainLocal; use cumulus_test_runtime::{Hash, Header, NodeBlock as Block, RuntimeApi}; use frame_system_rpc_runtime_api::AccountNonceApi; +use parking_lot::Mutex; use polkadot_primitives::v1::{CollatorPair, Hash as PHash, PersistedValidationData}; use polkadot_service::ProvideRuntimeApi; use sc_client_api::execution_extensions::ExecutionStrategies; @@ -52,7 +53,7 @@ use sp_keyring::Sr25519Keyring; use sp_runtime::{codec::Encode, generic, traits::BlakeTwo256}; use sp_state_machine::BasicExternalities; use sp_trie::PrefixedMemoryDB; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use substrate_test_client::{ BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput, }; From 481d7e95763bd39e0fb97a7cd910a2d8244cec50 Mon Sep 17 00:00:00 2001 From: skunert Date: Tue, 21 Dec 2021 17:37:39 +0100 Subject: [PATCH 50/56] Remove unused builders, move wait_on_block to relaychain --- Cargo.lock | 6 + client/consensus/aura/src/lib.rs | 109 ++++-------------- client/network/Cargo.toml | 1 + client/network/src/lib.rs | 26 +---- client/network/src/tests.rs | 42 ++++++- .../network/src/wait_on_relay_chain_block.rs | 59 ++-------- client/relay-chain-interface/Cargo.toml | 2 + client/relay-chain-interface/src/lib.rs | 27 +++++ client/relay-chain-local/Cargo.toml | 3 + client/relay-chain-local/src/lib.rs | 40 ++++++- parachain-template/node/src/service.rs | 84 ++++++-------- polkadot-parachains/src/service.rs | 77 ++++++------- 12 files changed, 218 insertions(+), 258 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 430e4fefd61..ab74404805d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1525,6 +1525,7 @@ dependencies = [ name = "cumulus-client-network" version = "0.1.0" dependencies = [ + "async-trait", "cumulus-primitives-core", "cumulus-relay-chain-interface", "cumulus-relay-chain-local", @@ -1835,7 +1836,9 @@ dependencies = [ name = "cumulus-relay-chain-interface" version = "0.1.0" dependencies = [ + "async-trait", "cumulus-primitives-core", + "derive_more", "parking_lot 0.11.2", "polkadot-overseer", "sc-client-api", @@ -1850,8 +1853,11 @@ dependencies = [ name = "cumulus-relay-chain-local" version = "0.1.0" dependencies = [ + "async-trait", "cumulus-primitives-core", "cumulus-relay-chain-interface", + "futures 0.3.18", + "futures-timer 3.0.2", "parking_lot 0.11.2", "polkadot-client", "polkadot-service", diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 9eefa92e881..e65aa6ce976 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -82,24 +82,26 @@ impl Clone for AuraConsensus { impl AuraConsensus where B: BlockT, - CIDP: CreateInherentDataProviders, + CIDP: CreateInherentDataProviders + 'static, CIDP::InherentDataProviders: InherentDataProviderExt, { - /// Create a new instance of AURA consensus. - pub fn new( - para_client: Arc, - block_import: BI, - sync_oracle: SO, - proposer_factory: PF, - force_authoring: bool, - backoff_authoring_blocks: Option, - keystore: SyncCryptoStorePtr, - create_inherent_data_providers: CIDP, - slot_duration: SlotDuration, - telemetry: Option, - block_proposal_slot_portion: SlotProportion, - max_block_proposal_slot_portion: Option, - ) -> Self + /// Create a new boxed instance of AURA consensus. + pub fn build( + BuildAuraConsensusParams { + proposer_factory, + create_inherent_data_providers, + block_import, + para_client, + backoff_authoring_blocks, + sync_oracle, + keystore, + force_authoring, + slot_duration, + telemetry, + block_proposal_slot_portion, + max_block_proposal_slot_portion, + }: BuildAuraConsensusParams, + ) -> Box> where Client: ProvideRuntimeApi + BlockOf + AuxStore + HeaderBackend + Send + Sync + 'static, @@ -136,11 +138,11 @@ where }, ); - Self { + Box::new(Self { create_inherent_data_providers: Arc::new(create_inherent_data_providers), aura_worker: Arc::new(Mutex::new(worker)), slot_duration, - } + }) } /// Create the inherent data. @@ -183,7 +185,7 @@ where impl ParachainConsensus for AuraConsensus where B: BlockT, - CIDP: CreateInherentDataProviders + Send + Sync, + CIDP: CreateInherentDataProviders + Send + Sync + 'static, CIDP::InherentDataProviders: InherentDataProviderExt + Send, { async fn produce_candidate( @@ -229,72 +231,3 @@ pub struct BuildAuraConsensusParams { pub block_proposal_slot_portion: SlotProportion, pub max_block_proposal_slot_portion: Option, } - -/// Build the [`AuraConsensus`]. -/// -/// Returns a boxed [`ParachainConsensus`]. -pub fn build_aura_consensus( - BuildAuraConsensusParams { - proposer_factory, - create_inherent_data_providers, - block_import, - para_client, - backoff_authoring_blocks, - sync_oracle, - keystore, - force_authoring, - slot_duration, - telemetry, - block_proposal_slot_portion, - max_block_proposal_slot_portion, - }: BuildAuraConsensusParams, -) -> Box> -where - Block: BlockT, - CIDP: CreateInherentDataProviders - + Send - + Sync - + 'static, - CIDP::InherentDataProviders: InherentDataProviderExt + Send, - Client: ProvideRuntimeApi - + BlockOf - + AuxStore - + HeaderBackend - + Send - + Sync - + 'static, - Client::Api: AuraApi, - BI: BlockImport> - + Send - + Sync - + 'static, - SO: SyncOracle + Send + Sync + Clone + 'static, - BS: BackoffAuthoringBlocksStrategy> + Send + Sync + 'static, - PF: Environment + Send + Sync + 'static, - PF::Proposer: Proposer< - Block, - Error = Error, - Transaction = sp_api::TransactionFor, - ProofRecording = EnableProofRecording, - Proof = ::Proof, - >, - Error: std::error::Error + Send + From + 'static, - P: Pair + Send + Sync, - P::Public: AppPublic + Hash + Member + Encode + Decode, - P::Signature: TryFrom> + Hash + Member + Encode + Decode, -{ - Box::new(AuraConsensus::new::( - para_client, - block_import, - sync_oracle, - proposer_factory, - force_authoring, - backoff_authoring_blocks, - keystore, - create_inherent_data_providers, - slot_duration, - telemetry, - block_proposal_slot_portion, - max_block_proposal_slot_portion, - )) -} diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 4d7d2c7dd4f..18a4a8c3a07 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -29,6 +29,7 @@ futures-timer = "3.0.2" tracing = "0.1.22" parking_lot = "0.11.1" derive_more = "0.99.2" +async-trait = "0.1.52" [dev-dependencies] tokio = { version = "1.10", features = ["macros"] } diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 4a85e109ffa..e988ce12669 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -392,31 +392,7 @@ pub fn build_block_announce_validator( where RCInterface: RelayChainInterface + Clone + 'static, { - BlockAnnounceValidatorBuilder::new(relay_chain_interface, para_id).build() -} - -/// Block announce validator builder. -/// -/// Builds a [`BlockAnnounceValidator`] for a parachain. -struct BlockAnnounceValidatorBuilder { - phantom: PhantomData, - relay_chain_interface: RCInterface, - para_id: ParaId, -} - -impl BlockAnnounceValidatorBuilder -where - RCInterface: RelayChainInterface + Clone + 'static, -{ - /// Create a new instance of the builder. - fn new(relay_chain_interface: RCInterface, para_id: ParaId) -> Self { - Self { relay_chain_interface, para_id, phantom: PhantomData } - } - - /// Build the block announce validator. - fn build(self) -> Box + Send> { - Box::new(BlockAnnounceValidator::new(self.relay_chain_interface.clone(), self.para_id)) - } + Box::new(BlockAnnounceValidator::new(relay_chain_interface, para_id)) } /// Wait before announcing a block that a candidate message has been received for this block, then diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index f3c7863d512..b589091001c 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -15,9 +15,10 @@ // along with Polkadot. If not, see . use super::*; -use cumulus_relay_chain_interface::BlockCheckResult; +use async_trait::async_trait; +use cumulus_relay_chain_interface::{BlockCheckResult, WaitError}; use cumulus_test_service::runtime::{Block, Hash, Header}; -use futures::{executor::block_on, poll, task::Poll}; +use futures::{executor::block_on, poll, task::Poll, FutureExt, StreamExt}; use parking_lot::Mutex; use polkadot_node_primitives::{SignedFullStatement, Statement}; use polkadot_primitives::v1::{ @@ -39,7 +40,7 @@ use sp_keyring::Sr25519Keyring; use sp_keystore::{testing::KeyStore, SyncCryptoStore, SyncCryptoStorePtr}; use sp_runtime::RuntimeAppPublic; use sp_state_machine::StorageValue; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, time::Duration}; fn check_error(error: crate::BoxedError, check_error: impl Fn(&BlockAnnounceError) -> bool) { let error = *error @@ -73,6 +74,7 @@ impl DummyRelayChainInterface { } } +#[async_trait] impl RelayChainInterface for DummyRelayChainInterface { fn validators( &self, @@ -219,6 +221,40 @@ impl RelayChainInterface for DummyRelayChainInterface { Ok(BlockCheckResult::NotFound(listener)) } + + async fn wait_for_block( + &self, + hash: PHash, + ) -> Result<(), cumulus_relay_chain_interface::WaitError> { + let block_id = BlockId::Hash(hash); + let _lock = self.relay_backend.get_import_lock(); + + match self.relay_backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(()), + Err(err) => return Err(WaitError::BlockchainError(hash, err)), + _ => {}, + } + + let mut listener = self.relay_client.import_notification_stream(); + + // Now it is safe to drop the lock, even when the block is now imported, it should show + // up in our registered listener. + drop(_lock); + + let mut timeout = futures_timer::Delay::new(Duration::from_secs(10)).fuse(); + + loop { + futures::select! { + _ = timeout => return Err(WaitError::Timeout(hash)), + evt = listener.next() => match evt { + Some(evt) if evt.hash == hash => return Ok(()), + // Not the event we waited on. + Some(_) => continue, + None => return Err(WaitError::ImportListenerClosed(hash)), + } + } + } + } } fn make_validator_and_api( diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs index b32868e38f3..37c27136a7a 100644 --- a/client/network/src/wait_on_relay_chain_block.rs +++ b/client/network/src/wait_on_relay_chain_block.rs @@ -16,33 +16,9 @@ //! Provides the [`WaitOnRelayChainBlock`] type. -use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface}; -use futures::{future::ready, Future, FutureExt, StreamExt}; +use cumulus_relay_chain_interface::{RelayChainInterface, WaitError}; +use futures::Future; use polkadot_primitives::v1::Hash as PHash; -use sc_client_api::blockchain; -use sp_runtime::generic::BlockId; -use std::time::Duration; - -/// The timeout in seconds after that the waiting for a block should be aborted. -const TIMEOUT_IN_SECONDS: u64 = 6; - -/// Custom error type used by [`WaitOnRelayChainBlock`]. -#[derive(Debug, derive_more::Display)] -pub enum Error { - #[display(fmt = "Timeout while waiting for relay-chain block `{}` to be imported.", _0)] - Timeout(PHash), - #[display( - fmt = "Import listener closed while waiting for relay-chain block `{}` to be imported.", - _0 - )] - ImportListenerClosed(PHash), - #[display( - fmt = "Blockchain returned an error while waiting for relay-chain block `{}` to be imported: {:?}", - _0, - _1 - )] - BlockchainError(PHash, blockchain::Error), -} /// A helper to wait for a given relay chain block in an async way. /// @@ -88,30 +64,8 @@ where pub fn wait_on_relay_chain_block( &self, hash: PHash, - ) -> impl Future> { - let mut listener = - match self.relay_chain_interface.check_block_in_chain(BlockId::Hash(hash)) { - Ok(BlockCheckResult::NotFound(listener)) => listener, - Ok(BlockCheckResult::InChain) => return ready(Ok(())).boxed(), - Err(err) => return ready(Err(Error::BlockchainError(hash, err))).boxed(), - }; - - let mut timeout = futures_timer::Delay::new(Duration::from_secs(TIMEOUT_IN_SECONDS)).fuse(); - - async move { - loop { - futures::select! { - _ = timeout => return Err(Error::Timeout(hash)), - evt = listener.next() => match evt { - Some(evt) if evt.hash == hash => return Ok(()), - // Not the event we waited on. - Some(_) => continue, - None => return Err(Error::ImportListenerClosed(hash)), - } - } - } - } - .boxed() + ) -> impl Future> + '_ { + self.relay_chain_interface.wait_for_block(hash) } } @@ -210,7 +164,10 @@ mod tests { let wait = WaitOnRelayChainBlock::new(relay_chain_interface); - assert!(matches!(block_on(wait.wait_on_relay_chain_block(hash)), Err(Error::Timeout(_)))); + assert!(matches!( + block_on(wait.wait_on_relay_chain_block(hash)), + Err(WaitError::Timeout(_)) + )); } #[test] diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 266c36e32f9..09548687be2 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -18,3 +18,5 @@ sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = " sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.1" +derive_more = "0.99.2" +async-trait = "0.1.52" diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 45cb673b82a..72fe0c9fbac 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -30,12 +30,32 @@ use sp_api::ApiError; use sp_core::sp_std::collections::btree_map::BTreeMap; use sp_state_machine::StorageValue; +use async_trait::async_trait; + pub enum BlockCheckResult { InChain, NotFound(sc_client_api::ImportNotifications), } +#[derive(Debug, derive_more::Display)] +pub enum WaitError { + #[display(fmt = "Timeout while waiting for relay-chain block `{}` to be imported.", _0)] + Timeout(PHash), + #[display( + fmt = "Import listener closed while waiting for relay-chain block `{}` to be imported.", + _0 + )] + ImportListenerClosed(PHash), + #[display( + fmt = "Blockchain returned an error while waiting for relay-chain block `{}` to be imported: {:?}", + _0, + _1 + )] + BlockchainError(PHash, sp_blockchain::Error), +} + /// Should be used for all interaction with the relay chain in cumulus. +#[async_trait] pub trait RelayChainInterface: Send + Sync { fn get_storage_by_key( &self, @@ -93,6 +113,8 @@ pub trait RelayChainInterface: Send + Sync { block_id: BlockId, ) -> Result; + async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError>; + fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; fn storage_changes_notification_stream( @@ -114,6 +136,7 @@ pub trait RelayChainInterface: Send + Sync { ) -> Result, Box>; } +#[async_trait] impl RelayChainInterface for Arc where T: RelayChainInterface + ?Sized, @@ -215,4 +238,8 @@ where ) -> Result { (**self).check_block_in_chain(block_id) } + + async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError> { + (**self).wait_for_block(hash).await + } } diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index f80142522d0..95893b1164a 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -27,3 +27,6 @@ sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master parking_lot = "0.11.1" tracing = "0.1.25" +async-trait = "0.1.52" +futures = { version = "0.3.1", features = ["compat"] } +futures-timer = "3.0.2" diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs index 6b43b22a53b..ef13f0ba86d 100644 --- a/client/relay-chain-local/src/lib.rs +++ b/client/relay-chain-local/src/lib.rs @@ -16,6 +16,8 @@ use std::sync::Arc; +use async_trait::async_trait; +use core::time::Duration; use cumulus_primitives_core::{ relay_chain::{ v1::{ @@ -26,7 +28,8 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface}; +use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface, WaitError}; +use futures::{FutureExt, StreamExt}; use parking_lot::Mutex; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; use polkadot_service::{ @@ -40,7 +43,10 @@ use sp_api::{ApiError, ProvideRuntimeApi}; use sp_consensus::SyncOracle; use sp_core::{sp_std::collections::btree_map::BTreeMap, Pair}; use sp_state_machine::{Backend as StateBackend, StorageValue}; + const LOG_TARGET: &str = "relay-chain-local"; +/// The timeout in seconds after that the waiting for a block should be aborted. +const TIMEOUT_IN_SECONDS: u64 = 6; /// RelayChainLocal is used to interact with a full node that is running locally /// in the same process. @@ -73,6 +79,7 @@ impl Clone for RelayChainLocal { } } +#[async_trait] impl RelayChainInterface for RelayChainLocal where Client: ProvideRuntimeApi @@ -257,6 +264,37 @@ where Ok(BlockCheckResult::NotFound(listener)) } + + async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError> { + let block_id = BlockId::Hash(hash); + let _lock = self.backend.get_import_lock(); + + match self.backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(()), + Err(err) => return Err(WaitError::BlockchainError(hash, err)), + _ => {}, + } + + let mut listener = self.full_client.import_notification_stream(); + + // Now it is safe to drop the lock, even when the block is now imported, it should show + // up in our registered listener. + drop(_lock); + + let mut timeout = futures_timer::Delay::new(Duration::from_secs(TIMEOUT_IN_SECONDS)).fuse(); + + loop { + futures::select! { + _ = timeout => return Err(WaitError::Timeout(hash)), + evt = listener.next() => match evt { + Some(evt) if evt.hash == hash => return Ok(()), + // Not the event we waited on. + Some(_) => continue, + None => return Err(WaitError::ImportListenerClosed(hash)), + } + } + } + } } /// Builder for a concrete relay chain interface, creatd from a full node. Builds diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 1b2ae7369bf..bb5e053ce8f 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -9,9 +9,7 @@ use parachain_template_runtime::{ }; // Cumulus Imports -use cumulus_client_consensus_aura::{ - build_aura_consensus, BuildAuraConsensusParams, SlotProportion, -}; +use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion}; use cumulus_client_consensus_common::ParachainConsensus; use cumulus_client_network::build_block_announce_validator; use cumulus_client_service::{ @@ -430,56 +428,48 @@ pub async fn start_parachain_node( telemetry.clone(), ); - Ok(build_aura_consensus::< - sp_consensus_aura::sr25519::AuthorityPair, - _, - _, - _, - _, - _, - _, - _, - _, - >(BuildAuraConsensusParams { - proposer_factory, - create_inherent_data_providers: move |_, (relay_parent, validation_data)| { - let parachain_inherent = - cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( - relay_parent, - &relay_chain_interface, - &validation_data, - id, - ); - async move { - let time = sp_timestamp::InherentDataProvider::from_system_time(); - - let slot = + Ok(AuraConsensus::build::( + BuildAuraConsensusParams { + proposer_factory, + create_inherent_data_providers: move |_, (relay_parent, validation_data)| { + let parachain_inherent = + cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( + relay_parent, + &relay_chain_interface, + &validation_data, + id, + ); + async move { + let time = sp_timestamp::InherentDataProvider::from_system_time(); + + let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration( *time, slot_duration.slot_duration(), ); - let parachain_inherent = parachain_inherent.ok_or_else(|| { - Box::::from( - "Failed to create parachain inherent", - ) - })?; - Ok((time, slot, parachain_inherent)) - } + let parachain_inherent = parachain_inherent.ok_or_else(|| { + Box::::from( + "Failed to create parachain inherent", + ) + })?; + Ok((time, slot, parachain_inherent)) + } + }, + block_import: client.clone(), + para_client: client, + backoff_authoring_blocks: Option::<()>::None, + sync_oracle, + keystore, + force_authoring, + slot_duration, + // We got around 500ms for proposing + block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), + // And a maximum of 750ms if slots are skipped + max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), + telemetry, }, - block_import: client.clone(), - para_client: client, - backoff_authoring_blocks: Option::<()>::None, - sync_oracle, - keystore, - force_authoring, - slot_duration, - // We got around 500ms for proposing - block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), - // And a maximum of 750ms if slots are skipped - max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), - telemetry, - })) + )) }, ) .await diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 9a4d9cfb8a0..8c3dd925cc9 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -14,9 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use cumulus_client_consensus_aura::{ - build_aura_consensus, BuildAuraConsensusParams, SlotProportion, -}; +use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion}; use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; @@ -719,7 +717,7 @@ pub async fn start_rococo_parachain_node( ); - Ok(build_aura_consensus::< + Ok(AuraConsensus::build::< sp_consensus_aura::sr25519::AuthorityPair, _, _, @@ -727,8 +725,6 @@ pub async fn start_rococo_parachain_node( _, _, _, - _, - _, >(BuildAuraConsensusParams { proposer_factory, create_inherent_data_providers: move |_, (relay_parent, validation_data)| { @@ -1137,56 +1133,51 @@ where telemetry2.clone(), ); - build_aura_consensus::< - sp_consensus_aura::sr25519::AuthorityPair, - _, - _, - _, - _, - _, - _, - _, - _, - >(BuildAuraConsensusParams { - proposer_factory, - create_inherent_data_providers: move |_, (relay_parent, validation_data)| { - let parachain_inherent = + AuraConsensus::build::( + BuildAuraConsensusParams { + proposer_factory, + create_inherent_data_providers: + move |_, (relay_parent, validation_data)| { + let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at( relay_parent, &relay_chain_for_aura, &validation_data, id, ); - async move { - let time = sp_timestamp::InherentDataProvider::from_system_time(); + async move { + let time = + sp_timestamp::InherentDataProvider::from_system_time(); - let slot = + let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration( *time, slot_duration.slot_duration(), ); - let parachain_inherent = parachain_inherent.ok_or_else(|| { - Box::::from( - "Failed to create parachain inherent", - ) - })?; - Ok((time, slot, parachain_inherent)) - } + let parachain_inherent = + parachain_inherent.ok_or_else(|| { + Box::::from( + "Failed to create parachain inherent", + ) + })?; + Ok((time, slot, parachain_inherent)) + } + }, + block_import: client2.clone(), + para_client: client2.clone(), + backoff_authoring_blocks: Option::<()>::None, + sync_oracle, + keystore, + force_authoring, + slot_duration, + // We got around 500ms for proposing + block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), + // And a maximum of 750ms if slots are skipped + max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), + telemetry: telemetry2, }, - block_import: client2.clone(), - para_client: client2.clone(), - backoff_authoring_blocks: Option::<()>::None, - sync_oracle, - keystore, - force_authoring, - slot_duration, - // We got around 500ms for proposing - block_proposal_slot_portion: SlotProportion::new(1f32 / 24f32), - // And a maximum of 750ms if slots are skipped - max_block_proposal_slot_portion: Some(SlotProportion::new(1f32 / 16f32)), - telemetry: telemetry2, - }) + ) }))); let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording( From 67ce2f2b55bc300a68054acb579e06b0045d9a9a Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Tue, 21 Dec 2021 20:14:07 +0100 Subject: [PATCH 51/56] Remove WaitOnRelayChain and add docs --- Cargo.lock | 5 + client/network/src/lib.rs | 10 +- client/network/src/tests.rs | 23 +- .../network/src/wait_on_relay_chain_block.rs | 213 ------------------ client/relay-chain-interface/src/lib.rs | 44 ++-- client/relay-chain-local/Cargo.toml | 13 ++ client/relay-chain-local/src/lib.rs | 177 +++++++++++++-- 7 files changed, 200 insertions(+), 285 deletions(-) delete mode 100644 client/network/src/wait_on_relay_chain_block.rs diff --git a/Cargo.lock b/Cargo.lock index ab74404805d..af7d1194e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1856,11 +1856,14 @@ dependencies = [ "async-trait", "cumulus-primitives-core", "cumulus-relay-chain-interface", + "cumulus-test-service", "futures 0.3.18", "futures-timer 3.0.2", "parking_lot 0.11.2", "polkadot-client", + "polkadot-primitives", "polkadot-service", + "polkadot-test-client", "sc-client-api", "sc-consensus-babe", "sc-network", @@ -1871,8 +1874,10 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-core", + "sp-keyring", "sp-runtime", "sp-state-machine", + "substrate-test-utils", "tracing", ] diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index e988ce12669..45c44bda0dc 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -46,11 +46,8 @@ use futures::{ use std::{convert::TryFrom, fmt, marker::PhantomData, pin::Pin, sync::Arc}; -use wait_on_relay_chain_block::WaitOnRelayChainBlock; - #[cfg(test)] mod tests; -mod wait_on_relay_chain_block; const LOG_TARGET: &str = "sync::cumulus"; @@ -229,7 +226,6 @@ pub struct BlockAnnounceValidator { phantom: PhantomData, relay_chain_interface: RCInterface, para_id: ParaId, - wait_on_relay_chain_block: WaitOnRelayChainBlock, } impl BlockAnnounceValidator @@ -242,7 +238,6 @@ where phantom: Default::default(), relay_chain_interface: relay_chain_interface.clone(), para_id, - wait_on_relay_chain_block: WaitOnRelayChainBlock::new(relay_chain_interface), } } } @@ -360,7 +355,6 @@ where let relay_chain_interface = self.relay_chain_interface.clone(); let header_encoded = header.encode(); - let wait_on_relay_chain_block = self.wait_on_relay_chain_block.clone(); async move { if let Err(e) = block_announce_data.validate(header_encoded) { @@ -369,8 +363,8 @@ where let relay_parent = block_announce_data.receipt.descriptor.relay_parent; - wait_on_relay_chain_block - .wait_on_relay_chain_block(relay_parent) + relay_chain_interface + .wait_for_block(relay_parent) .await .map_err(|e| Box::new(BlockAnnounceError(e.to_string())) as Box<_>)?; diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index b589091001c..57509dddb26 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -16,7 +16,7 @@ use super::*; use async_trait::async_trait; -use cumulus_relay_chain_interface::{BlockCheckResult, WaitError}; +use cumulus_relay_chain_interface::WaitError; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll, FutureExt, StreamExt}; use parking_lot::Mutex; @@ -201,27 +201,6 @@ impl RelayChainInterface for DummyRelayChainInterface { unimplemented!("Not needed for test") } - fn check_block_in_chain( - &self, - block_id: polkadot_service::BlockId, - ) -> Result { - let _lock = self.relay_backend.get_import_lock(); - - match self.relay_backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(BlockCheckResult::InChain), - Err(err) => return Err(err), - _ => {}, - } - - let listener = self.relay_client.import_notification_stream(); - - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); - - Ok(BlockCheckResult::NotFound(listener)) - } - async fn wait_for_block( &self, hash: PHash, diff --git a/client/network/src/wait_on_relay_chain_block.rs b/client/network/src/wait_on_relay_chain_block.rs deleted file mode 100644 index 37c27136a7a..00000000000 --- a/client/network/src/wait_on_relay_chain_block.rs +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright 2020-2021 Parity Technologies (UK) Ltd. -// This file is part of Polkadot. - -// Polkadot is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Polkadot is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Polkadot. If not, see . - -//! Provides the [`WaitOnRelayChainBlock`] type. - -use cumulus_relay_chain_interface::{RelayChainInterface, WaitError}; -use futures::Future; -use polkadot_primitives::v1::Hash as PHash; - -/// A helper to wait for a given relay chain block in an async way. -/// -/// The caller needs to pass the hash of a block it waits for and the function will return when the -/// block is available or an error occurred. -/// -/// The waiting for the block is implemented as follows: -/// -/// 1. Get a read lock on the import lock from the backend. -/// -/// 2. Check if the block is already imported. If yes, return from the function. -/// -/// 3. If the block isn't imported yet, add an import notification listener. -/// -/// 4. Poll the import notification listener until the block is imported or the timeout is fired. -/// -/// The timeout is set to 6 seconds. This should be enough time to import the block in the current -/// round and if not, the new round of the relay chain already started anyway. -pub struct WaitOnRelayChainBlock { - relay_chain_interface: R, -} - -impl Clone for WaitOnRelayChainBlock -where - RCInterface: Clone, -{ - fn clone(&self) -> Self { - Self { relay_chain_interface: self.relay_chain_interface.clone() } - } -} - -impl WaitOnRelayChainBlock { - /// Creates a new instance of `Self`. - pub fn new(relay_chain_interface: RCInterface) -> Self { - Self { relay_chain_interface } - } -} - -impl WaitOnRelayChainBlock -where - RCInterface: RelayChainInterface, -{ - pub fn wait_on_relay_chain_block( - &self, - hash: PHash, - ) -> impl Future> + '_ { - self.relay_chain_interface.wait_for_block(hash) - } -} - -#[cfg(test)] -mod tests { - use parking_lot::Mutex; - - use super::*; - - use cumulus_relay_chain_local::RelayChainLocal; - use polkadot_primitives::v1::Block as PBlock; - use polkadot_test_client::{ - construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, - DefaultTestClientBuilderExt, ExecutionStrategy, InitPolkadotBlockBuilder, - TestClientBuilder, TestClientBuilderExt, - }; - use sc_service::Arc; - use sp_consensus::{BlockOrigin, SyncOracle}; - use sp_runtime::traits::Block as BlockT; - - use futures::{executor::block_on, poll, task::Poll}; - - struct DummyNetwork {} - - impl SyncOracle for DummyNetwork { - fn is_major_syncing(&mut self) -> bool { - unimplemented!("Not needed for test") - } - - fn is_offline(&mut self) -> bool { - unimplemented!("Not needed for test") - } - } - - fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainLocal) { - let builder = - TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); - let backend = builder.backend(); - let client = Arc::new(builder.build()); - - let block_builder = client.init_polkadot_block_builder(); - let block = block_builder.build().expect("Finalizes the block").block; - let dummy_network: Box = Box::new(DummyNetwork {}); - - ( - client.clone(), - block, - RelayChainLocal::new( - client, - backend.clone(), - Arc::new(Mutex::new(dummy_network)), - None, - ), - ) - } - - #[test] - fn returns_directly_for_available_block() { - let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); - let hash = block.hash(); - - block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block"); - - let wait = WaitOnRelayChainBlock::new(relay_chain_interface); - - block_on(async move { - // Should be ready on the first poll - assert!(matches!(poll!(wait.wait_on_relay_chain_block(hash)), Poll::Ready(Ok(())))); - }); - } - - #[test] - fn resolve_after_block_import_notification_was_received() { - let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); - let hash = block.hash(); - - let wait = WaitOnRelayChainBlock::new(relay_chain_interface); - - block_on(async move { - let mut future = wait.wait_on_relay_chain_block(hash); - // As the block is not yet imported, the first poll should return `Pending` - assert!(poll!(&mut future).is_pending()); - - // Import the block that should fire the notification - client.import(BlockOrigin::Own, block).await.expect("Imports the block"); - - // Now it should have received the notification and report that the block was imported - assert!(matches!(poll!(future), Poll::Ready(Ok(())))); - }); - } - - #[test] - fn wait_for_block_time_out_when_block_is_not_imported() { - let (_, block, relay_chain_interface) = build_client_backend_and_block(); - let hash = block.hash(); - - let wait = WaitOnRelayChainBlock::new(relay_chain_interface); - - assert!(matches!( - block_on(wait.wait_on_relay_chain_block(hash)), - Err(WaitError::Timeout(_)) - )); - } - - #[test] - fn do_not_resolve_after_different_block_import_notification_was_received() { - let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); - let hash = block.hash(); - - let ext = construct_transfer_extrinsic( - &*client, - sp_keyring::Sr25519Keyring::Alice, - sp_keyring::Sr25519Keyring::Bob, - 1000, - ); - let mut block_builder = client.init_polkadot_block_builder(); - // Push an extrinsic to get a different block hash. - block_builder.push_polkadot_extrinsic(ext).expect("Push extrinsic"); - let block2 = block_builder.build().expect("Build second block").block; - let hash2 = block2.hash(); - - let wait = WaitOnRelayChainBlock::new(relay_chain_interface); - - block_on(async move { - let mut future = wait.wait_on_relay_chain_block(hash); - let mut future2 = wait.wait_on_relay_chain_block(hash2); - // As the block is not yet imported, the first poll should return `Pending` - assert!(poll!(&mut future).is_pending()); - assert!(poll!(&mut future2).is_pending()); - - // Import the block that should fire the notification - client.import(BlockOrigin::Own, block2).await.expect("Imports the second block"); - - // The import notification of the second block should not make this one finish - assert!(poll!(&mut future).is_pending()); - // Now it should have received the notification and report that the block was imported - assert!(matches!(poll!(future2), Poll::Ready(Ok(())))); - - client.import(BlockOrigin::Own, block).await.expect("Imports the first block"); - - // Now it should be ready - assert!(matches!(poll!(future), Poll::Ready(Ok(())))); - }); - } -} diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 72fe0c9fbac..7dacea18611 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -32,11 +32,6 @@ use sp_state_machine::StorageValue; use async_trait::async_trait; -pub enum BlockCheckResult { - InChain, - NotFound(sc_client_api::ImportNotifications), -} - #[derive(Debug, derive_more::Display)] pub enum WaitError { #[display(fmt = "Timeout while waiting for relay-chain block `{}` to be imported.", _0)] @@ -54,19 +49,23 @@ pub enum WaitError { BlockchainError(PHash, sp_blockchain::Error), } -/// Should be used for all interaction with the relay chain in cumulus. +/// Trait that provides all necessary methods for interaction between collator and relay chain. #[async_trait] pub trait RelayChainInterface: Send + Sync { + /// Fetch a storage item by key. fn get_storage_by_key( &self, block_id: &BlockId, key: &[u8], ) -> Result, sp_blockchain::Error>; + /// Fetch a vector of current validators. fn validators(&self, block_id: &BlockId) -> Result, ApiError>; + /// Get the status of a given block. fn block_status(&self, block_id: BlockId) -> Result; + /// Get the hash of the current best block. fn best_block_hash(&self) -> PHash; /// Returns the whole contents of the downward message queue for the parachain we are collating @@ -89,6 +88,11 @@ pub trait RelayChainInterface: Send + Sync { relay_parent: PHash, ) -> Option>>; + /// Yields the persisted validation data for the given `ParaId` along with an assumption that + /// should be used if the para currently occupies a core. + /// + /// Returns `None` if either the para is not registered or the assumption is `Freed` + /// and the para already occupies a core. fn persisted_validation_data( &self, block_id: &BlockId, @@ -96,27 +100,30 @@ pub trait RelayChainInterface: Send + Sync { _: OccupiedCoreAssumption, ) -> Result, ApiError>; + /// Get the receipt of a candidate pending availability. This returns `Some` for any paras + /// assigned to occupied cores in `availability_cores` and `None` otherwise. fn candidate_pending_availability( &self, block_id: &BlockId, para_id: ParaId, ) -> Result, ApiError>; + /// Returns the session index expected at a child of the block. fn session_index_for_child(&self, block_id: &BlockId) -> Result; + /// Get a stream of import block notifications. fn import_notification_stream(&self) -> sc_client_api::ImportNotifications; - /// Check if block is in chain. If it is, we return BlockCheckResult::InChain. - /// If it is not in the chain, we return BlockCheckResult::NotFound with a listener that can be used to wait on the block. - fn check_block_in_chain( - &self, - block_id: BlockId, - ) -> Result; - + /// Wait for a block with a given hash in the relay chain. + /// + /// This method returns immediately on error or if the block is already + /// reported to be in chain. Otherwise, it waits for the block to arrive. async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError>; + /// Get a stream of finality notifications. fn finality_notification_stream(&self) -> sc_client_api::FinalityNotifications; + /// Get a stream of storage change notifications. fn storage_changes_notification_stream( &self, filter_keys: Option<&[sc_client_api::StorageKey]>, @@ -125,10 +132,14 @@ pub trait RelayChainInterface: Send + Sync { >, ) -> sc_client_api::blockchain::Result>; + /// Whether the synchronization service is undergoing major sync. + /// Returns true if so. fn is_major_syncing(&self) -> bool; + /// Get a handle to the overseer. fn overseer_handle(&self) -> Option; + /// Generate a storage read proof. fn prove_read( &self, block_id: &BlockId, @@ -232,13 +243,6 @@ where (**self).prove_read(block_id, relevant_keys) } - fn check_block_in_chain( - &self, - block_id: BlockId, - ) -> Result { - (**self).check_block_in_chain(block_id) - } - async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError> { (**self).wait_for_block(hash).await } diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index 95893b1164a..c2fde2b919d 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -18,6 +18,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" } + sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" } @@ -30,3 +31,15 @@ tracing = "0.1.25" async-trait = "0.1.52" futures = { version = "0.3.1", features = ["compat"] } futures-timer = "3.0.2" + +[dev-dependencies] +# Cumulus deps +cumulus-test-service = { path = "../../test/service" } + +# Polkadot deps +polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } +polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" } + +# substrate deps +sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" } +substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" } diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs index ef13f0ba86d..1f5dcad38c3 100644 --- a/client/relay-chain-local/src/lib.rs +++ b/client/relay-chain-local/src/lib.rs @@ -28,7 +28,7 @@ use cumulus_primitives_core::{ }, InboundDownwardMessage, ParaId, PersistedValidationData, }; -use cumulus_relay_chain_interface::{BlockCheckResult, RelayChainInterface, WaitError}; +use cumulus_relay_chain_interface::{RelayChainInterface, WaitError}; use futures::{FutureExt, StreamExt}; use parking_lot::Mutex; use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend}; @@ -244,27 +244,23 @@ where } } - fn check_block_in_chain( - &self, - block_id: BlockId, - ) -> Result { - let _lock = self.backend.get_import_lock(); - - match self.backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(BlockCheckResult::InChain), - Err(err) => return Err(err), - _ => {}, - } - - let listener = self.full_client.import_notification_stream(); - - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); - - Ok(BlockCheckResult::NotFound(listener)) - } - + /// Wait for a given relay chain block in an async way. + /// + /// The caller needs to pass the hash of a block it waits for and the function will return when the + /// block is available or an error occurred. + /// + /// The waiting for the block is implemented as follows: + /// + /// 1. Get a read lock on the import lock from the backend. + /// + /// 2. Check if the block is already imported. If yes, return from the function. + /// + /// 3. If the block isn't imported yet, add an import notification listener. + /// + /// 4. Poll the import notification listener until the block is imported or the timeout is fired. + /// + /// The timeout is set to 6 seconds. This should be enough time to import the block in the current + /// round and if not, the new round of the relay chain already started anyway. async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError> { let block_id = BlockId::Hash(hash); let _lock = self.backend.get_import_lock(); @@ -386,3 +382,140 @@ pub fn build_relay_chain_interface( Ok((relay_chain_interface_builder.build(), collator_key)) } + +#[cfg(test)] +mod tests { + use parking_lot::Mutex; + + use super::*; + + use polkadot_primitives::v1::Block as PBlock; + use polkadot_test_client::{ + construct_transfer_extrinsic, BlockBuilderExt, Client, ClientBlockImportExt, + DefaultTestClientBuilderExt, ExecutionStrategy, InitPolkadotBlockBuilder, + TestClientBuilder, TestClientBuilderExt, + }; + use sc_service::Arc; + use sp_consensus::{BlockOrigin, SyncOracle}; + use sp_runtime::traits::Block as BlockT; + + use futures::{executor::block_on, poll, task::Poll}; + + struct DummyNetwork {} + + impl SyncOracle for DummyNetwork { + fn is_major_syncing(&mut self) -> bool { + unimplemented!("Not needed for test") + } + + fn is_offline(&mut self) -> bool { + unimplemented!("Not needed for test") + } + } + + fn build_client_backend_and_block() -> (Arc, PBlock, RelayChainLocal) { + let builder = + TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible); + let backend = builder.backend(); + let client = Arc::new(builder.build()); + + let block_builder = client.init_polkadot_block_builder(); + let block = block_builder.build().expect("Finalizes the block").block; + let dummy_network: Box = Box::new(DummyNetwork {}); + + ( + client.clone(), + block, + RelayChainLocal::new( + client, + backend.clone(), + Arc::new(Mutex::new(dummy_network)), + None, + ), + ) + } + + #[test] + fn returns_directly_for_available_block() { + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); + let hash = block.hash(); + + block_on(client.import(BlockOrigin::Own, block)).expect("Imports the block"); + + block_on(async move { + // Should be ready on the first poll + assert!(matches!( + poll!(relay_chain_interface.wait_for_block(hash)), + Poll::Ready(Ok(())) + )); + }); + } + + #[test] + fn resolve_after_block_import_notification_was_received() { + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); + let hash = block.hash(); + + block_on(async move { + let mut future = relay_chain_interface.wait_for_block(hash); + // As the block is not yet imported, the first poll should return `Pending` + assert!(poll!(&mut future).is_pending()); + + // Import the block that should fire the notification + client.import(BlockOrigin::Own, block).await.expect("Imports the block"); + + // Now it should have received the notification and report that the block was imported + assert!(matches!(poll!(future), Poll::Ready(Ok(())))); + }); + } + + #[test] + fn wait_for_block_time_out_when_block_is_not_imported() { + let (_, block, relay_chain_interface) = build_client_backend_and_block(); + let hash = block.hash(); + + assert!(matches!( + block_on(relay_chain_interface.wait_for_block(hash)), + Err(WaitError::Timeout(_)) + )); + } + + #[test] + fn do_not_resolve_after_different_block_import_notification_was_received() { + let (mut client, block, relay_chain_interface) = build_client_backend_and_block(); + let hash = block.hash(); + + let ext = construct_transfer_extrinsic( + &*client, + sp_keyring::Sr25519Keyring::Alice, + sp_keyring::Sr25519Keyring::Bob, + 1000, + ); + let mut block_builder = client.init_polkadot_block_builder(); + // Push an extrinsic to get a different block hash. + block_builder.push_polkadot_extrinsic(ext).expect("Push extrinsic"); + let block2 = block_builder.build().expect("Build second block").block; + let hash2 = block2.hash(); + + block_on(async move { + let mut future = relay_chain_interface.wait_for_block(hash); + let mut future2 = relay_chain_interface.wait_for_block(hash2); + // As the block is not yet imported, the first poll should return `Pending` + assert!(poll!(&mut future).is_pending()); + assert!(poll!(&mut future2).is_pending()); + + // Import the block that should fire the notification + client.import(BlockOrigin::Own, block2).await.expect("Imports the second block"); + + // The import notification of the second block should not make this one finish + assert!(poll!(&mut future).is_pending()); + // Now it should have received the notification and report that the block was imported + assert!(matches!(poll!(future2), Poll::Ready(Ok(())))); + + client.import(BlockOrigin::Own, block).await.expect("Imports the first block"); + + // Now it should be ready + assert!(matches!(poll!(future), Poll::Ready(Ok(())))); + }); + } +} From 6f7094a5738ce637d92bccb5a020ebefc1865710 Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 22 Dec 2021 10:19:51 +0100 Subject: [PATCH 52/56] Remove unused dep --- Cargo.lock | 1 - client/relay-chain-local/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af7d1194e49..c420c213183 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1877,7 +1877,6 @@ dependencies = [ "sp-keyring", "sp-runtime", "sp-state-machine", - "substrate-test-utils", "tracing", ] diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index c2fde2b919d..ebcc5d30817 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -42,4 +42,3 @@ polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = # substrate deps sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" } -substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" } From 83c977fa26a0fedecb9965a0f0d40f854fa9c2b5 Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 22 Dec 2021 11:44:54 +0100 Subject: [PATCH 53/56] Format --- client/relay-chain-interface/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/client/relay-chain-interface/Cargo.toml b/client/relay-chain-interface/Cargo.toml index 09548687be2..a962155ed1e 100644 --- a/client/relay-chain-interface/Cargo.toml +++ b/client/relay-chain-interface/Cargo.toml @@ -4,7 +4,6 @@ name = "cumulus-relay-chain-interface" version = "0.1.0" edition = "2021" - [dependencies] polkadot-overseer = { git = "https://github.com/paritytech/polkadot", branch = "master" } From 2f51a3e487b6858ae85ae32086fb1c15160e4638 Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 22 Dec 2021 15:11:21 +0100 Subject: [PATCH 54/56] Remove unused dependencies, fix lock issue --- Cargo.lock | 3 -- client/network/src/tests.rs | 25 ++++------ client/relay-chain-interface/src/lib.rs | 3 +- client/relay-chain-local/Cargo.toml | 1 - client/relay-chain-local/src/lib.rs | 65 ++++++++++++++++--------- client/service/src/lib.rs | 3 +- parachain-template/node/src/service.rs | 3 +- polkadot-parachains/Cargo.toml | 1 - polkadot-parachains/src/service.rs | 7 +-- test/service/Cargo.toml | 2 - test/service/src/lib.rs | 2 +- 11 files changed, 59 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 590531520ea..a5c113b4414 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1968,7 +1968,6 @@ version = "0.1.0" dependencies = [ "async-trait", "criterion", - "cumulus-client-collator", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", "cumulus-client-network", @@ -2002,7 +2001,6 @@ dependencies = [ "sc-transaction-pool", "sc-transaction-pool-api", "serde", - "sp-api", "sp-arithmetic", "sp-blockchain", "sp-core", @@ -6724,7 +6722,6 @@ dependencies = [ "assert_cmd", "async-trait", "cumulus-client-cli", - "cumulus-client-collator", "cumulus-client-consensus-aura", "cumulus-client-consensus-common", "cumulus-client-consensus-relay-chain", diff --git a/client/network/src/tests.rs b/client/network/src/tests.rs index 57509dddb26..34584edd69d 100644 --- a/client/network/src/tests.rs +++ b/client/network/src/tests.rs @@ -17,6 +17,7 @@ use super::*; use async_trait::async_trait; use cumulus_relay_chain_interface::WaitError; +use cumulus_relay_chain_local::{check_block_in_chain, BlockCheckStatus}; use cumulus_test_service::runtime::{Block, Hash, Header}; use futures::{executor::block_on, poll, task::Poll, FutureExt, StreamExt}; use parking_lot::Mutex; @@ -33,7 +34,7 @@ use polkadot_test_client::{ InitPolkadotBlockBuilder, TestClientBuilder, TestClientBuilderExt, }; use sc_client_api::{Backend, BlockchainEvents}; -use sp_blockchain::{BlockStatus, HeaderBackend}; +use sp_blockchain::HeaderBackend; use sp_consensus::BlockOrigin; use sp_core::{Pair, H256}; use sp_keyring::Sr25519Keyring; @@ -205,20 +206,14 @@ impl RelayChainInterface for DummyRelayChainInterface { &self, hash: PHash, ) -> Result<(), cumulus_relay_chain_interface::WaitError> { - let block_id = BlockId::Hash(hash); - let _lock = self.relay_backend.get_import_lock(); - - match self.relay_backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(()), - Err(err) => return Err(WaitError::BlockchainError(hash, err)), - _ => {}, - } - - let mut listener = self.relay_client.import_notification_stream(); - - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); + let mut listener = match check_block_in_chain( + self.relay_backend.clone(), + self.relay_client.clone(), + hash, + )? { + BlockCheckStatus::InChain => return Ok(()), + BlockCheckStatus::Unknown(listener) => listener, + }; let mut timeout = futures_timer::Delay::new(Duration::from_secs(10)).fuse(); diff --git a/client/relay-chain-interface/src/lib.rs b/client/relay-chain-interface/src/lib.rs index 7dacea18611..185e9a6f0a3 100644 --- a/client/relay-chain-interface/src/lib.rs +++ b/client/relay-chain-interface/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc}; use cumulus_primitives_core::{ relay_chain::{ @@ -27,7 +27,6 @@ use polkadot_overseer::Handle as OverseerHandle; use sc_client_api::{blockchain::BlockStatus, StorageProof}; use sp_api::ApiError; -use sp_core::sp_std::collections::btree_map::BTreeMap; use sp_state_machine::StorageValue; use async_trait::async_trait; diff --git a/client/relay-chain-local/Cargo.toml b/client/relay-chain-local/Cargo.toml index ebcc5d30817..a5150afeab2 100644 --- a/client/relay-chain-local/Cargo.toml +++ b/client/relay-chain-local/Cargo.toml @@ -4,7 +4,6 @@ name = "cumulus-relay-chain-local" version = "0.1.0" edition = "2021" - [dependencies] polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" } polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" } diff --git a/client/relay-chain-local/src/lib.rs b/client/relay-chain-local/src/lib.rs index 1f5dcad38c3..9a03462345d 100644 --- a/client/relay-chain-local/src/lib.rs +++ b/client/relay-chain-local/src/lib.rs @@ -14,10 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use async_trait::async_trait; -use core::time::Duration; use cumulus_primitives_core::{ relay_chain::{ v1::{ @@ -36,7 +35,8 @@ use polkadot_service::{ AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, Role, TaskManager, }; use sc_client_api::{ - blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, StorageProof, UsageProvider, + blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, ImportNotifications, + StorageProof, UsageProvider, }; use sc_telemetry::TelemetryWorkerHandle; use sp_api::{ApiError, ProvideRuntimeApi}; @@ -48,8 +48,7 @@ const LOG_TARGET: &str = "relay-chain-local"; /// The timeout in seconds after that the waiting for a block should be aborted. const TIMEOUT_IN_SECONDS: u64 = 6; -/// RelayChainLocal is used to interact with a full node that is running locally -/// in the same process. +/// Provides an implementation of the [`RelayChainInterface`] using a local in-process relay chain node. pub struct RelayChainLocal { full_client: Arc, backend: Arc, @@ -58,6 +57,7 @@ pub struct RelayChainLocal { } impl RelayChainLocal { + /// Create a new instance of [`RelayChainLocal`] pub fn new( full_client: Arc, backend: Arc, @@ -239,7 +239,7 @@ where ); e }) - .map(|v| Some(v)), + .map(Some), None => Ok(None), } } @@ -262,20 +262,11 @@ where /// The timeout is set to 6 seconds. This should be enough time to import the block in the current /// round and if not, the new round of the relay chain already started anyway. async fn wait_for_block(&self, hash: PHash) -> Result<(), WaitError> { - let block_id = BlockId::Hash(hash); - let _lock = self.backend.get_import_lock(); - - match self.backend.blockchain().status(block_id) { - Ok(BlockStatus::InChain) => return Ok(()), - Err(err) => return Err(WaitError::BlockchainError(hash, err)), - _ => {}, - } - - let mut listener = self.full_client.import_notification_stream(); - - // Now it is safe to drop the lock, even when the block is now imported, it should show - // up in our registered listener. - drop(_lock); + let mut listener = + match check_block_in_chain(self.backend.clone(), self.full_client.clone(), hash)? { + BlockCheckStatus::InChain => return Ok(()), + BlockCheckStatus::Unknown(listener) => listener, + }; let mut timeout = futures_timer::Delay::new(Duration::from_secs(TIMEOUT_IN_SECONDS)).fuse(); @@ -293,12 +284,42 @@ where } } -/// Builder for a concrete relay chain interface, creatd from a full node. Builds +pub enum BlockCheckStatus { + /// Block is in chain + InChain, + /// Block status is unknown, listener can be used to wait for notification + Unknown(ImportNotifications), +} + +// Helper function to check if a block is in chain. +pub fn check_block_in_chain( + backend: Arc, + client: Arc, + hash: PHash, +) -> Result +where + Client: BlockchainEvents, +{ + let _lock = backend.get_import_lock().read(); + + let block_id = BlockId::Hash(hash); + match backend.blockchain().status(block_id) { + Ok(BlockStatus::InChain) => return Ok(BlockCheckStatus::InChain), + Err(err) => return Err(WaitError::BlockchainError(hash, err)), + _ => {}, + } + + let listener = client.import_notification_stream(); + + Ok(BlockCheckStatus::Unknown(listener)) +} + +/// Builder for a concrete relay chain interface, created from a full node. Builds /// a [`RelayChainLocal`] to access relay chain data necessary for parachain operation. /// /// The builder takes a [`polkadot_client::Client`] /// that wraps a concrete instance. By using [`polkadot_client::ExecuteWithClient`] -/// the builder gets access to this concrete instance and instantiates a RelayChainLocal with it. +/// the builder gets access to this concrete instance and instantiates a [`RelayChainLocal`] with it. struct RelayChainLocalBuilder { polkadot_client: polkadot_client::Client, backend: Arc, diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 30ddb2391d8..925c957c6fd 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -18,7 +18,6 @@ //! //! Provides functions for starting a collator node or a normal full node. -use core::time::Duration; use cumulus_client_consensus_common::ParachainConsensus; use cumulus_primitives_core::{CollectCollationInfo, ParaId}; use cumulus_relay_chain_interface::RelayChainInterface; @@ -39,7 +38,7 @@ use sp_runtime::{ traits::{Block as BlockT, NumberFor}, Justifications, }; -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; pub mod genesis; diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index bb5e053ce8f..7b12b6e0b97 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -1,7 +1,7 @@ //! Service and ServiceFactory implementation. Specialized wrapper over substrate service. // std -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; // Local Runtime Types use parachain_template_runtime::{ @@ -20,7 +20,6 @@ use cumulus_relay_chain_interface::RelayChainInterface; use cumulus_relay_chain_local::build_relay_chain_interface; // Substrate Imports -use core::time::Duration; use sc_client_api::ExecutorProvider; use sc_executor::NativeElseWasmExecutor; use sc_network::NetworkService; diff --git a/polkadot-parachains/Cargo.toml b/polkadot-parachains/Cargo.toml index 4412d4df252..c124283a419 100644 --- a/polkadot-parachains/Cargo.toml +++ b/polkadot-parachains/Cargo.toml @@ -74,7 +74,6 @@ cumulus-client-consensus-relay-chain = { path = "../client/consensus/relay-chain cumulus-client-consensus-common = { path = "../client/consensus/common" } cumulus-client-service = { path = "../client/service" } cumulus-client-network = { path = "../client/network" } -cumulus-client-collator = { path = "../client/collator" } cumulus-primitives-core = { path = "../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../primitives/parachain-inherent" } cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" } diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 8c3dd925cc9..542441b6437 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -33,7 +33,6 @@ use polkadot_service::NativeExecutionDispatch; use crate::rpc; pub use parachains_common::{AccountId, Balance, Block, Hash, Header, Index as Nonce}; -use core::time::Duration; use cumulus_client_consensus_relay_chain::Verifier as RelayChainVerifier; use futures::lock::Mutex; use sc_client_api::ExecutorProvider; @@ -53,7 +52,7 @@ use sp_runtime::{ generic::BlockId, traits::{BlakeTwo256, Header as HeaderT}, }; -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use substrate_prometheus_endpoint::Registry; /// Native executor instance. @@ -131,9 +130,7 @@ impl sc_executor::NativeExecutionDispatch for StatemineRuntimeExecutor { } } -/** -Native Westmint executor instance. -*/ +/// Native Westmint executor instance. pub struct WestmintRuntimeExecutor; impl sc_executor::NativeExecutionDispatch for WestmintRuntimeExecutor { diff --git a/test/service/Cargo.toml b/test/service/Cargo.toml index bacdf4ddf2e..02537138848 100644 --- a/test/service/Cargo.toml +++ b/test/service/Cargo.toml @@ -29,7 +29,6 @@ sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } -sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } @@ -48,7 +47,6 @@ polkadot-test-service = { git = "https://github.com/paritytech/polkadot", branch cumulus-client-consensus-relay-chain = { path = "../../client/consensus/relay-chain" } cumulus-client-network = { path = "../../client/network" } cumulus-client-service = { path = "../../client/service" } -cumulus-client-collator = { path = "../../client/collator" } cumulus-client-consensus-common = { path = "../../client/consensus/common" } cumulus-primitives-core = { path = "../../primitives/core" } cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" } diff --git a/test/service/src/lib.rs b/test/service/src/lib.rs index 66d07047b42..5022612d290 100644 --- a/test/service/src/lib.rs +++ b/test/service/src/lib.rs @@ -21,7 +21,7 @@ mod chain_spec; mod genesis; -use core::{future::Future, time::Duration}; +use std::{future::Future, time::Duration}; use cumulus_client_consensus_common::{ParachainCandidate, ParachainConsensus}; use cumulus_client_network::BlockAnnounceValidator; From aebfcda9b925fffa4d3d9fcbfc8b5fee5623db9d Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 22 Dec 2021 17:16:58 +0100 Subject: [PATCH 55/56] Fix log output --- primitives/parachain-inherent/src/client_side.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/primitives/parachain-inherent/src/client_side.rs b/primitives/parachain-inherent/src/client_side.rs index b0a1e3effbc..dab368dc6cd 100644 --- a/primitives/parachain-inherent/src/client_side.rs +++ b/primitives/parachain-inherent/src/client_side.rs @@ -45,8 +45,9 @@ fn collect_relay_storage_proof( .map_err(|e| { tracing::error!( target: LOG_TARGET, - relay_parent = ?relay_parent_block_id, - error = ?e, "Cannot obtain the hrmp ingress channel." + relay_parent = ?relay_parent, + error = ?e, + "Cannot obtain the hrmp ingress channel." ) }) .ok()?; From 117ba4c1bbf9106dfb46cfa816949cbc3f8d3b67 Mon Sep 17 00:00:00 2001 From: Sebastian Kunert Date: Wed, 22 Dec 2021 17:26:09 +0100 Subject: [PATCH 56/56] Remove `build_block_announce_validator` function --- client/network/src/lib.rs | 13 ------------- parachain-template/node/src/service.rs | 9 +++++---- polkadot-parachains/src/service.rs | 16 +++++++++------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/client/network/src/lib.rs b/client/network/src/lib.rs index 45c44bda0dc..79e4f7c1b79 100644 --- a/client/network/src/lib.rs +++ b/client/network/src/lib.rs @@ -376,19 +376,6 @@ where } } -/// Build a block announce validator instance. -/// -/// Returns a boxed [`BlockAnnounceValidator`]. -pub fn build_block_announce_validator( - relay_chain_interface: RCInterface, - para_id: ParaId, -) -> Box + Send> -where - RCInterface: RelayChainInterface + Clone + 'static, -{ - Box::new(BlockAnnounceValidator::new(relay_chain_interface, para_id)) -} - /// Wait before announcing a block that a candidate message has been received for this block, then /// add this message as justification for the block announcement. /// diff --git a/parachain-template/node/src/service.rs b/parachain-template/node/src/service.rs index 7b12b6e0b97..3d87547ded2 100644 --- a/parachain-template/node/src/service.rs +++ b/parachain-template/node/src/service.rs @@ -11,7 +11,7 @@ use parachain_template_runtime::{ // Cumulus Imports use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion}; use cumulus_client_consensus_common::ParachainConsensus; -use cumulus_client_network::build_block_announce_validator; +use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; @@ -248,8 +248,7 @@ where s => format!("{}", s).into(), })?; - let block_announce_validator = - build_block_announce_validator(relay_chain_interface.clone(), id); + let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); @@ -263,7 +262,9 @@ where transaction_pool: transaction_pool.clone(), spawn_handle: task_manager.spawn_handle(), import_queue: import_queue.clone(), - block_announce_validator_builder: Some(Box::new(|_| block_announce_validator)), + block_announce_validator_builder: Some(Box::new(|_| { + Box::new(block_announce_validator) + })), warp_sync: None, })?; diff --git a/polkadot-parachains/src/service.rs b/polkadot-parachains/src/service.rs index 542441b6437..d248240a5a7 100644 --- a/polkadot-parachains/src/service.rs +++ b/polkadot-parachains/src/service.rs @@ -18,7 +18,7 @@ use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, Slo use cumulus_client_consensus_common::{ ParachainBlockImport, ParachainCandidate, ParachainConsensus, }; -use cumulus_client_network::build_block_announce_validator; +use cumulus_client_network::BlockAnnounceValidator; use cumulus_client_service::{ prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams, }; @@ -345,8 +345,7 @@ where s => format!("{}", s).into(), })?; - let block_announce_validator = - build_block_announce_validator(relay_chain_interface.clone(), id); + let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); @@ -360,7 +359,9 @@ where transaction_pool: transaction_pool.clone(), spawn_handle: task_manager.spawn_handle(), import_queue: import_queue.clone(), - block_announce_validator_builder: Some(Box::new(|_| block_announce_validator)), + block_announce_validator_builder: Some(Box::new(|_| { + Box::new(block_announce_validator) + })), warp_sync: None, })?; @@ -519,8 +520,7 @@ where s => format!("{}", s).into(), })?; - let block_announce_validator = - build_block_announce_validator(relay_chain_interface.clone(), id); + let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id); let force_authoring = parachain_config.force_authoring; let validator = parachain_config.role.is_authority(); @@ -534,7 +534,9 @@ where transaction_pool: transaction_pool.clone(), spawn_handle: task_manager.spawn_handle(), import_queue: import_queue.clone(), - block_announce_validator_builder: Some(Box::new(|_| block_announce_validator)), + block_announce_validator_builder: Some(Box::new(|_| { + Box::new(block_announce_validator) + })), warp_sync: None, })?;