Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cumulus-primitives-parachain-inherent: Split into two crates #2803

Merged
merged 8 commits into from
Jan 5, 2024
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
35 changes: 26 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ members = [
"cumulus/client/consensus/proposer",
"cumulus/client/consensus/relay-chain",
"cumulus/client/network",
"cumulus/client/parachain-inherent",
"cumulus/client/pov-recovery",
"cumulus/client/relay-chain-inprocess-interface",
"cumulus/client/relay-chain-interface",
Expand Down
2 changes: 1 addition & 1 deletion cumulus/client/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ substrate-prometheus-endpoint = { path = "../../../../substrate/utils/prometheus
cumulus-client-consensus-common = { path = "../common" }
cumulus-relay-chain-interface = { path = "../../relay-chain-interface" }
cumulus-client-consensus-proposer = { path = "../proposer" }
cumulus-client-parachain-inherent = { path = "../../../client/parachain-inherent" }
cumulus-primitives-aura = { path = "../../../primitives/aura" }
cumulus-primitives-core = { path = "../../../primitives/core" }
cumulus-primitives-parachain-inherent = { path = "../../../primitives/parachain-inherent" }
cumulus-client-collator = { path = "../../collator" }

# Polkadot
Expand Down
4 changes: 2 additions & 2 deletions cumulus/client/consensus/aura/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ use cumulus_client_consensus_common::{
self as consensus_common, ParachainBlockImportMarker, ParachainCandidate,
};
use cumulus_client_consensus_proposer::ProposerInterface;
use cumulus_client_parachain_inherent::{ParachainInherentData, ParachainInherentDataProvider};
use cumulus_primitives_core::{
relay_chain::Hash as PHash, DigestItem, ParachainBlockData, PersistedValidationData,
};
use cumulus_primitives_parachain_inherent::ParachainInherentData;
use cumulus_relay_chain_interface::RelayChainInterface;

use polkadot_node_primitives::{Collation, MaybeCompressedPoV};
Expand Down Expand Up @@ -124,7 +124,7 @@ where
parent_hash: Block::Hash,
timestamp: impl Into<Option<Timestamp>>,
) -> Result<(ParachainInherentData, InherentData), Box<dyn Error + Send + Sync + 'static>> {
let paras_inherent_data = ParachainInherentData::create_at(
let paras_inherent_data = ParachainInherentDataProvider::create_at(
relay_parent,
&self.relay_client,
validation_data,
Expand Down
30 changes: 30 additions & 0 deletions cumulus/client/parachain-inherent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "cumulus-client-parachain-inherent"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
description = "Inherent that needs to be present in every parachain block. Contains messages and a relay chain storage-proof."
license = "Apache-2.0"

[dependencies]
async-trait = "0.1.73"
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
scale-info = { version = "2.10.0", features = ["derive"] }
tracing = { version = "0.1.37" }

# Substrate
sc-client-api = { path = "../../../substrate/client/api" }
sp-api = { path = "../../../substrate/primitives/api" }
sp-core = { path = "../../../substrate/primitives/core" }
sp-inherents = { path = "../../../substrate/primitives/inherents" }
sp-runtime = { path = "../../../substrate/primitives/runtime" }
sp-state-machine = { path = "../../../substrate/primitives/state-machine" }
sp-std = { path = "../../../substrate/primitives/std" }
sp-storage = { path = "../../../substrate/primitives/storage" }
sp-trie = { path = "../../../substrate/primitives/trie" }

# Cumulus
cumulus-primitives-core = { path = "../../primitives/core" }
cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" }
cumulus-relay-chain-interface = { path = "../relay-chain-interface" }
cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder" }
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

//! Client side code for generating the parachain inherent.

use crate::ParachainInherentData;
use codec::Decode;
use cumulus_primitives_core::{
relay_chain::{self, Hash as PHash, HrmpChannelId},
ParaId, PersistedValidationData,
};
use cumulus_relay_chain_interface::RelayChainInterface;

mod mock;

pub use cumulus_primitives_parachain_inherent::{ParachainInherentData, INHERENT_IDENTIFIER};
pub use mock::{MockValidationDataInherentDataProvider, MockXcmConfig};

const LOG_TARGET: &str = "parachain-inherent";

/// Collect the relevant relay chain state in form of a proof for putting it into the validation
Expand Down Expand Up @@ -132,7 +136,9 @@ async fn collect_relay_storage_proof(
.ok()
}

impl ParachainInherentData {
pub struct ParachainInherentDataProvider;

impl ParachainInherentDataProvider {
/// Create the [`ParachainInherentData`] at the given `relay_parent`.
///
/// Returns `None` if the creation failed.
Expand Down Expand Up @@ -178,21 +184,3 @@ impl ParachainInherentData {
})
}
}

#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for ParachainInherentData {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
inherent_data.put_data(crate::INHERENT_IDENTIFIER, &self)
}

async fn try_handle_error(
&self,
_: &sp_inherents::InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use codec::Decode;
use cumulus_primitives_core::{
relay_chain, InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData,
};
use cumulus_primitives_parachain_inherent::MessageQueueChain;
use sc_client_api::{Backend, StorageProvider};
use sp_core::twox_128;
use sp_inherents::{InherentData, InherentDataProvider};
Expand Down Expand Up @@ -168,7 +169,7 @@ impl<R: Send + Sync + GenerateRandomness<u64>> InherentDataProvider

// Process the downward messages and set up the correct head
let mut downward_messages = Vec::new();
let mut dmq_mqc = crate::MessageQueueChain(self.xcm_config.starting_dmq_mqc_head);
let mut dmq_mqc = MessageQueueChain::new(self.xcm_config.starting_dmq_mqc_head);
for msg in &self.raw_downward_messages {
let wrapped = InboundDownwardMessage { sent_at: relay_parent_number, msg: msg.clone() };

Expand All @@ -188,7 +189,7 @@ impl<R: Send + Sync + GenerateRandomness<u64>> InherentDataProvider

// Now iterate again, updating the heads as we go
for (para_id, messages) in &horizontal_messages {
let mut channel_mqc = crate::MessageQueueChain(
let mut channel_mqc = MessageQueueChain::new(
*self
.xcm_config
.starting_hrmp_mqc_heads
Expand Down
2 changes: 1 addition & 1 deletion cumulus/polkadot-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ cumulus-client-consensus-aura = { path = "../client/consensus/aura" }
cumulus-client-consensus-relay-chain = { path = "../client/consensus/relay-chain" }
cumulus-client-consensus-common = { path = "../client/consensus/common" }
cumulus-client-consensus-proposer = { path = "../client/consensus/proposer" }
cumulus-client-parachain-inherent = { path = "../client/parachain-inherent" }
cumulus-client-service = { path = "../client/service" }
cumulus-primitives-aura = { path = "../primitives/aura" }
cumulus-primitives-core = { path = "../primitives/core" }
cumulus-primitives-parachain-inherent = { path = "../primitives/parachain-inherent" }
cumulus-relay-chain-interface = { path = "../client/relay-chain-interface" }
color-print = "0.3.4"

Expand Down
2 changes: 1 addition & 1 deletion cumulus/polkadot-parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ where
let relay_chain_interface = relay_chain_interface.clone();
async move {
let parachain_inherent =
cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(
cumulus_client_parachain_inherent::ParachainInherentDataProvider::create_at(
relay_parent,
&relay_chain_interface,
&validation_data,
Expand Down
14 changes: 0 additions & 14 deletions cumulus/primitives/parachain-inherent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,27 @@ workspace = true
async-trait = { version = "0.1.74", optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
tracing = { version = "0.1.37", optional = true }

# Substrate
sc-client-api = { path = "../../../substrate/client/api", optional = true }
sp-api = { path = "../../../substrate/primitives/api", optional = true }
sp-core = { path = "../../../substrate/primitives/core", default-features = false }
sp-inherents = { path = "../../../substrate/primitives/inherents", default-features = false }
sp-runtime = { path = "../../../substrate/primitives/runtime", optional = true }
sp-state-machine = { path = "../../../substrate/primitives/state-machine", optional = true }
sp-std = { path = "../../../substrate/primitives/std", default-features = false }
sp-storage = { path = "../../../substrate/primitives/storage", optional = true }
sp-trie = { path = "../../../substrate/primitives/trie", default-features = false }

# Cumulus
cumulus-primitives-core = { path = "../core", default-features = false }
cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface", optional = true }
cumulus-test-relay-sproof-builder = { path = "../../test/relay-sproof-builder", optional = true }

[features]
default = ["std"]
std = [
"async-trait",
"codec/std",
"cumulus-primitives-core/std",
"cumulus-relay-chain-interface",
"cumulus-test-relay-sproof-builder",
"sc-client-api",
"scale-info/std",
"sp-api",
"sp-core/std",
"sp-inherents/std",
"sp-runtime",
"sp-state-machine",
"sp-std/std",
"sp-storage",
"sp-trie/std",
"tracing",
]
37 changes: 27 additions & 10 deletions cumulus/primitives/parachain-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
//! The [`ParachainInherentData`] is the data that is passed by the collator to the parachain
//! runtime. The runtime will use this data to execute messages from other parachains/the relay
//! chain or to read data from the relay chain state. When the parachain is validated by a parachain
//! validator on the relay chain, this data is checked for correctnes. If the data passed by the
//! validator on the relay chain, this data is checked for correctness. If the data passed by the
//! collator to the runtime isn't correct, the parachain candidate is considered invalid.
//!
//! Use [`ParachainInherentData::create_at`] to create the [`ParachainInherentData`] at a given
//! relay chain block to include it in a parachain block.
//! To create a [`ParachainInherentData`] for a specific relay chain block, there exists the
//! `ParachainInherentDataExt` trait in `cumulus-client-parachain-inherent` that helps with this.

#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -36,13 +36,6 @@ use scale_info::TypeInfo;
use sp_inherents::InherentIdentifier;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

#[cfg(feature = "std")]
mod client_side;
#[cfg(feature = "std")]
mod mock;
#[cfg(feature = "std")]
pub use mock::{MockValidationDataInherentDataProvider, MockXcmConfig};

/// The identifier for the parachain inherent.
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"sysi1337";

Expand All @@ -68,6 +61,25 @@ pub struct ParachainInherentData {
pub horizontal_messages: BTreeMap<ParaId, Vec<InboundHrmpMessage>>,
}

#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for ParachainInherentData {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self)
}

async fn try_handle_error(
&self,
_: &sp_inherents::InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}

/// This struct provides ability to extend a message queue chain (MQC) and compute a new head.
///
/// MQC is an instance of a [hash chain] applied to a message queue. Using a hash chain it's
Expand All @@ -84,6 +96,11 @@ pub struct ParachainInherentData {
pub struct MessageQueueChain(RelayHash);

impl MessageQueueChain {
/// Create a new instance initialized to `hash`.
pub fn new(hash: RelayHash) -> Self {
Self(hash)
}

/// Extend the hash chain with an HRMP message. This method should be used only when
/// this chain is tracking HRMP.
pub fn extend_hrmp(&mut self, horizontal_message: &InboundHrmpMessage) -> &mut Self {
Expand Down
2 changes: 1 addition & 1 deletion cumulus/test/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ cumulus-client-cli = { path = "../../client/cli" }
parachains-common = { path = "../../parachains/common" }
cumulus-client-consensus-common = { path = "../../client/consensus/common" }
cumulus-client-consensus-relay-chain = { path = "../../client/consensus/relay-chain" }
cumulus-client-parachain-inherent = { path = "../../client/parachain-inherent" }
cumulus-client-service = { path = "../../client/service" }
cumulus-primitives-core = { path = "../../primitives/core" }
cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inherent" }
cumulus-relay-chain-inprocess-interface = { path = "../../client/relay-chain-inprocess-interface" }
cumulus-relay-chain-interface = { path = "../../client/relay-chain-interface" }
cumulus-test-runtime = { path = "../runtime" }
Expand Down
2 changes: 1 addition & 1 deletion cumulus/test/service/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use codec::Encode;
use sc_block_builder::BlockBuilderBuilder;

use crate::{construct_extrinsic, Client as TestClient};
use cumulus_client_parachain_inherent::ParachainInherentData;
use cumulus_primitives_core::{relay_chain::AccountId, PersistedValidationData};
use cumulus_primitives_parachain_inherent::ParachainInherentData;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use cumulus_test_runtime::{
BalancesCall, GluttonCall, NodeBlock, SudoCall, UncheckedExtrinsic, WASM_BINARY,
Expand Down
2 changes: 1 addition & 1 deletion cumulus/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ where
let relay_chain_interface = relay_chain_interface_for_closure.clone();
async move {
let parachain_inherent =
cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(
cumulus_client_parachain_inherent::ParachainInherentDataProvider::create_at(
relay_parent,
&relay_chain_interface,
&validation_data,
Expand Down
Loading