Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Companion for Polkadot#7451 #2807

Merged
merged 8 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
794 changes: 454 additions & 340 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "maste
substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" }

# Polkadot
polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" }
polkadot-test-client = { git = "https://github.com/paritytech/polkadot", branch = "master" }

# Cumulus
Expand Down
3 changes: 1 addition & 2 deletions client/relay-chain-inprocess-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }

# Polkadot
polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false, features = ["cli"] }
polkadot-client = { git = "https://github.com/paritytech/polkadot", branch = "master" }
polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false, features = ["cli", "polkadot-native"] }
polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "master" }

# Cumulus
Expand Down
108 changes: 20 additions & 88 deletions client/relay-chain-inprocess-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ use cumulus_primitives_core::{
};
use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface, RelayChainResult};
use futures::{FutureExt, Stream, StreamExt};
use polkadot_client::{ClientHandle, ExecuteWithClient, FullBackend};
use polkadot_service::{
AuxStore, BabeApi, CollatorPair, Configuration, Handle, NewFull, TaskManager,
CollatorPair, Configuration, FullBackend, FullClient, Handle, NewFull, TaskManager,
};
use sc_cli::SubstrateCli;
use sc_client_api::{
blockchain::BlockStatus, Backend, BlockchainEvents, HeaderBackend, ImportNotifications,
StorageProof, UsageProvider,
StorageProof,
};
use sc_telemetry::TelemetryWorkerHandle;
use sp_api::ProvideRuntimeApi;
Expand All @@ -46,17 +45,18 @@ use sp_state_machine::{Backend as StateBackend, StorageValue};
const TIMEOUT_IN_SECONDS: u64 = 6;

/// Provides an implementation of the [`RelayChainInterface`] using a local in-process relay chain node.
pub struct RelayChainInProcessInterface<Client> {
full_client: Arc<Client>,
#[derive(Clone)]
pub struct RelayChainInProcessInterface {
full_client: Arc<FullClient>,
backend: Arc<FullBackend>,
sync_oracle: Arc<dyn SyncOracle + Send + Sync>,
overseer_handle: Handle,
}

impl<Client> RelayChainInProcessInterface<Client> {
impl RelayChainInProcessInterface {
/// Create a new instance of [`RelayChainInProcessInterface`]
pub fn new(
full_client: Arc<Client>,
full_client: Arc<FullClient>,
backend: Arc<FullBackend>,
sync_oracle: Arc<dyn SyncOracle + Send + Sync>,
overseer_handle: Handle,
Expand All @@ -65,29 +65,8 @@ impl<Client> RelayChainInProcessInterface<Client> {
}
}

impl<T> Clone for RelayChainInProcessInterface<T> {
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(),
}
}
}

#[async_trait]
impl<Client> RelayChainInterface for RelayChainInProcessInterface<Client>
where
Client: ProvideRuntimeApi<PBlock>
+ HeaderBackend<PBlock>
+ BlockchainEvents<PBlock>
+ AuxStore
+ UsageProvider<PBlock>
+ Sync
+ Send,
Client::Api: ParachainHost<PBlock> + BabeApi<PBlock>,
{
impl RelayChainInterface for RelayChainInProcessInterface {
async fn retrieve_dmq_contents(
&self,
para_id: ParaId,
Expand Down Expand Up @@ -269,14 +248,11 @@ pub enum BlockCheckStatus {
}

// Helper function to check if a block is in chain.
pub fn check_block_in_chain<Client>(
pub fn check_block_in_chain(
backend: Arc<FullBackend>,
client: Arc<Client>,
client: Arc<FullClient>,
hash: PHash,
) -> RelayChainResult<BlockCheckStatus>
where
Client: BlockchainEvents<PBlock>,
{
) -> RelayChainResult<BlockCheckStatus> {
let _lock = backend.get_import_lock().read();

if backend.blockchain().status(hash)? == BlockStatus::InChain {
Expand All @@ -288,57 +264,14 @@ where
Ok(BlockCheckStatus::Unknown(listener))
}

/// Builder for a concrete relay chain interface, created from a full node. Builds
/// a [`RelayChainInProcessInterface`] 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 [`RelayChainInProcessInterface`] with it.
struct RelayChainInProcessInterfaceBuilder {
polkadot_client: polkadot_client::Client,
backend: Arc<FullBackend>,
sync_oracle: Arc<dyn SyncOracle + Send + Sync>,
overseer_handle: Handle,
}

impl RelayChainInProcessInterfaceBuilder {
pub fn build(self) -> Arc<dyn RelayChainInterface> {
self.polkadot_client.clone().execute_with(self)
}
}

impl ExecuteWithClient for RelayChainInProcessInterfaceBuilder {
type Output = Arc<dyn RelayChainInterface>;

fn execute_with_client<Client, Api, Backend>(self, client: Arc<Client>) -> Self::Output
where
Client: ProvideRuntimeApi<PBlock>
+ HeaderBackend<PBlock>
+ BlockchainEvents<PBlock>
+ AuxStore
+ UsageProvider<PBlock>
+ 'static
+ Sync
+ Send,
Client::Api: ParachainHost<PBlock> + BabeApi<PBlock>,
{
Arc::new(RelayChainInProcessInterface::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,
parachain_config: &Configuration,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
hwbench: Option<sc_sysinfo::HwBench>,
) -> Result<(NewFull<polkadot_client::Client>, Option<CollatorPair>), polkadot_service::Error> {
) -> Result<(NewFull, Option<CollatorPair>), polkadot_service::Error> {
let (is_collator, maybe_collator_key) = if parachain_config.role.is_authority() {
let collator_key = CollatorPair::generate().0;
(polkadot_service::IsCollator::Yes(collator_key.clone()), Some(collator_key))
Expand Down Expand Up @@ -385,18 +318,18 @@ pub fn build_inprocess_relay_chain(
)
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;

let relay_chain_interface_builder = RelayChainInProcessInterfaceBuilder {
polkadot_client: full_node.client.clone(),
backend: full_node.backend.clone(),
sync_oracle: full_node.sync_service.clone(),
overseer_handle: full_node.overseer_handle.clone().ok_or(RelayChainError::GenericError(
let relay_chain_interface = Arc::new(RelayChainInProcessInterface::new(
full_node.client,
full_node.backend,
full_node.sync_service,
full_node.overseer_handle.clone().ok_or(RelayChainError::GenericError(
"Overseer not running in full node.".to_string(),
))?,
};
));

task_manager.add_child(full_node.task_manager);

Ok((relay_chain_interface_builder.build(), collator_key))
Ok((relay_chain_interface, collator_key))
}

#[cfg(test)]
Expand Down Expand Up @@ -427,8 +360,7 @@ mod tests {
}
}

fn build_client_backend_and_block(
) -> (Arc<Client>, PBlock, RelayChainInProcessInterface<Client>) {
fn build_client_backend_and_block() -> (Arc<Client>, PBlock, RelayChainInProcessInterface) {
let builder =
TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible);
let backend = builder.backend();
Expand Down
2 changes: 1 addition & 1 deletion polkadot-parachain/tests/polkadot_argument_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn polkadot_argument_parsing() {

let args = &[
"--",
"--dev",
"--chain=rococo-local",
"--bootnodes",
"/ip4/127.0.0.1/tcp/30333/p2p/Qmbx43psh7LVkrYTRXisUpzCubbgYojkejzAgj5mteDnxy",
"--bootnodes",
Expand Down
2 changes: 1 addition & 1 deletion polkadot-parachain/tests/polkadot_mdns_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn interrupt_polkadot_mdns_issue_test() {

let base_dir = tempdir().expect("could not create a temp dir");

let args = &["--", "--dev"];
let args = &["--", "--chain=rococo-local"];

common::run_node_for_a_while(base_dir.path(), args, SIGINT).await;
common::run_node_for_a_while(base_dir.path(), args, SIGTERM).await;
Expand Down
8 changes: 4 additions & 4 deletions polkadot-parachain/tests/purge_chain_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ async fn purge_chain_works() {
let base_dir = tempdir().expect("could not create a temp dir");
let base_dir_path = format!("{}/polkadot", base_dir.path().display());

let args = &["--", "--dev", "-d", &base_dir_path];
let args = &["--", "-d", &base_dir_path, "--chain=rococo-local"];

common::run_node_for_a_while(base_dir.path(), args, SIGINT).await;

assert!(base_dir.path().join("chains/local_testnet/db/full").exists());
assert!(base_dir.path().join("polkadot/chains/dev/db/full").exists());
assert!(base_dir.path().join("polkadot/chains/rococo_local_testnet/db/full").exists());

let status = Command::new(cargo_bin("polkadot-parachain"))
.args(["purge-chain", "-d"])
Expand All @@ -48,6 +48,6 @@ async fn purge_chain_works() {
// Make sure that the `parachain_local_testnet` chain folder exists, but the `db` is deleted.
assert!(base_dir.path().join("chains/local_testnet").exists());
assert!(!base_dir.path().join("chains/local_testnet/db/full").exists());
// assert!(base_path.path().join("polkadot/chains/dev").exists());
// assert!(!base_path.path().join("polkadot/chains/dev/db").exists());
assert!(base_dir.path().join("polkadot/chains/rococo_local_testnet").exists());
assert!(!base_dir.path().join("polkadot/chains/rococo_local_testnet/db/full").exists());
}
2 changes: 1 addition & 1 deletion polkadot-parachain/tests/running_the_node_and_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn running_the_node_works_and_can_be_interrupted() {

let base_dir = tempdir().expect("could not create a temp dir");

let args = &["--", "--dev"];
let args = &["--", "--chain=rococo-local"];

common::run_node_for_a_while(base_dir.path(), args, SIGINT).await;
common::run_node_for_a_while(base_dir.path(), args, SIGTERM).await;
Expand Down