Skip to content
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
6 changes: 6 additions & 0 deletions .github/workflows/check-runtime-migration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ jobs:
- name: Run Check
id: required2
run: |
# Disable the check for asset-hub-westend temporarily due to frequent failures.
if [ "${{ matrix.network }}" = "asset-hub-westend" ]; then
echo "Skipping runtime migration check for asset-hub-westend due to frequent failures."
exit 0
fi

echo "Running ${{ matrix.network }} runtime migration check"
export RUST_LOG=remote-ext=debug,runtime=debug

Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions cumulus/client/parachain-inherent/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ impl<R: Send + Sync + GenerateRandomness<u64>> InherentDataProvider

// Inject current para block head, if any
sproof_builder.included_para_head = self.current_para_block_head.clone();

let (relay_parent_storage_root, proof) = sproof_builder.into_state_root_and_proof();
sproof_builder.num_authorities = 2;
let (relay_parent_storage_root, proof, relay_parent_descendants) =
sproof_builder.into_state_root_proof_and_descendants(1);
let parachain_inherent_data = ParachainInherentData {
validation_data: PersistedValidationData {
parent_head: Default::default(),
Expand All @@ -240,7 +241,7 @@ impl<R: Send + Sync + GenerateRandomness<u64>> InherentDataProvider
downward_messages,
horizontal_messages,
relay_chain_state: proof,
relay_parent_descendants: Default::default(),
relay_parent_descendants,
collator_peer_id: None,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ mod tests {
next_authorities: Option<Vec<(AuthorityId, BabeAuthorityWeight)>>,
) -> RelayChainStateProof {
// Create a mock implementation or structure, adjust this to match the proof's definition
let mut proof_builder = RelayStateSproofBuilder::default();
let mut proof_builder =
RelayStateSproofBuilder { num_authorities: 0, ..Default::default() };
if let Some(authorities) = authorities {
proof_builder
.additional_key_values
Expand Down
21 changes: 16 additions & 5 deletions cumulus/parachains/runtimes/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,20 @@ where
AllPalletsWithoutSystem::on_initialize(next_block_number);

let parent_head = HeadData(header.encode());

// Get RelayParentOffset from the parachain system pallet config.
let relay_parent_offset =
<Runtime as cumulus_pallet_parachain_system::Config>::RelayParentOffset::get()
.saturated_into::<u64>();

let sproof_builder = RelayStateSproofBuilder {
para_id: <Runtime>::SelfParaId::get(),
included_para_head: parent_head.clone().into(),
..Default::default()
};

let (relay_parent_storage_root, relay_chain_state) =
sproof_builder.into_state_root_and_proof();
let (relay_parent_storage_root, relay_chain_state, relay_parent_descendants) =
sproof_builder.into_state_root_proof_and_descendants(relay_parent_offset);
let inherent_data = ParachainInherentData {
validation_data: PersistedValidationData {
parent_head,
Expand All @@ -349,7 +355,7 @@ where
relay_chain_state,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
relay_parent_descendants: Default::default(),
relay_parent_descendants,
collator_peer_id: None,
};

Expand Down Expand Up @@ -689,6 +695,9 @@ pub fn mock_open_hrmp_channel<
let timestamp = slot.saturating_mul(slot_durations.para.as_millis());
let relay_slot = Slot::from_timestamp(timestamp.into(), slot_durations.relay);

// Get RelayParentOffset from the parachain system pallet config.
let relay_parent_offset = C::RelayParentOffset::get().saturated_into::<u64>();

let n = 1_u32;
let mut sproof_builder = RelayStateSproofBuilder {
para_id: sender,
Expand All @@ -709,7 +718,9 @@ pub fn mock_open_hrmp_channel<
},
);

let (relay_parent_storage_root, relay_chain_state) = sproof_builder.into_state_root_and_proof();
let (relay_parent_storage_root, relay_chain_state, relay_parent_descendants) =
sproof_builder.into_state_root_proof_and_descendants(relay_parent_offset);

let vfp = PersistedValidationData {
relay_parent_number: n as RelayChainBlockNumber,
relay_parent_storage_root,
Expand All @@ -724,7 +735,7 @@ pub fn mock_open_hrmp_channel<
relay_chain_state,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
relay_parent_descendants: Default::default(),
relay_parent_descendants,
collator_peer_id: None,
};
inherent_data
Expand Down
4 changes: 4 additions & 0 deletions cumulus/test/relay-sproof-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ workspace = true
codec = { features = ["derive"], workspace = true }

# Substrate
sp-consensus-babe = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-state-machine = { workspace = true }
sp-trie = { workspace = true }
Expand All @@ -31,6 +33,8 @@ std = [
"codec/std",
"cumulus-primitives-core/std",
"polkadot-primitives/std",
"sp-consensus-babe/std",
"sp-core/std",
"sp-runtime/std",
"sp-state-machine/std",
"sp-trie/std",
Expand Down
122 changes: 119 additions & 3 deletions cumulus/test/relay-sproof-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@
extern crate alloc;

use alloc::collections::btree_map::BTreeMap;
use codec::{Decode, Encode};
use cumulus_primitives_core::{
relay_chain, AbridgedHostConfiguration, AbridgedHrmpChannel, ParaId,
};
use polkadot_primitives::UpgradeGoAhead;
use sp_runtime::traits::HashingFor;
use polkadot_primitives::{Header, UpgradeGoAhead};
use sp_consensus_babe::{
digests::{CompatibleDigestItem, PreDigest, PrimaryPreDigest},
AuthorityId, AuthorityPair, BabeAuthorityWeight,
};
use sp_core::{
sr25519::vrf::{VrfPreOutput, VrfProof, VrfSignature},
Pair, H256,
};
use sp_runtime::{
traits::{HashingFor, Header as HeaderT},
Digest, DigestItem,
};
use sp_trie::PrefixedMemoryDB;

/// Builds a sproof (portmanteau of 'spoof' and 'proof') of the relay chain state.
Expand Down Expand Up @@ -49,6 +61,7 @@ pub struct RelayStateSproofBuilder {
pub randomness: relay_chain::Hash,
pub additional_key_values: Vec<(Vec<u8>, Vec<u8>)>,
pub included_para_head: Option<relay_chain::HeadData>,
pub num_authorities: u64,
}

impl Default for RelayStateSproofBuilder {
Expand Down Expand Up @@ -81,6 +94,7 @@ impl Default for RelayStateSproofBuilder {
randomness: relay_chain::Hash::default(),
additional_key_values: vec![],
included_para_head: None,
num_authorities: 1,
}
}
}
Expand Down Expand Up @@ -129,9 +143,39 @@ impl RelayStateSproofBuilder {
})
}

pub fn into_state_root_and_proof(
/// Build sproof and generate relay parent descendants with the configured authorities.
///
/// Returns a tuple of (state_root, storage_proof, relay_parent_descendants).
pub fn into_state_root_proof_and_descendants(
self,
relay_parent_offset: u64,
) -> (polkadot_primitives::Hash, sp_state_machine::StorageProof, Vec<Header>) {
let authorities = generate_authority_pairs(self.num_authorities);
let (state_root, proof) = self.into_state_root_and_proof();
let descendants =
build_relay_parent_descendants(relay_parent_offset + 1, state_root.into(), authorities);
(state_root, proof, descendants)
}

pub fn into_state_root_and_proof(
mut self,
) -> (polkadot_primitives::Hash, sp_state_machine::StorageProof) {
// Generate and add authorities if num_authorities is set
if self.num_authorities > 0 {
let authorities = generate_authority_pairs(self.num_authorities);
let auth_pair = convert_to_authority_weight_pair(&authorities);

// Add authorities to the sproof builder
self.additional_key_values.push((
relay_chain::well_known_keys::AUTHORITIES.to_vec(),
auth_pair.clone().encode(),
));
self.additional_key_values.push((
relay_chain::well_known_keys::NEXT_AUTHORITIES.to_vec(),
auth_pair.encode(),
));
}

let (db, root) =
PrefixedMemoryDB::<HashingFor<polkadot_primitives::Block>>::default_with_root();
let state_version = Default::default(); // for test using default.
Expand Down Expand Up @@ -213,3 +257,75 @@ impl RelayStateSproofBuilder {
(root, proof)
}
}

/// Generate a vector of AuthorityPairs
pub fn generate_authority_pairs(num_authorities: u64) -> Vec<AuthorityPair> {
(0..num_authorities).map(|i| AuthorityPair::from_seed(&[i as u8; 32])).collect()
}

/// Convert AuthorityPair to (AuthorityId, BabeAuthorityWeight)
fn convert_to_authority_weight_pair(
authorities: &[AuthorityPair],
) -> Vec<(AuthorityId, BabeAuthorityWeight)> {
authorities
.iter()
.map(|auth| (auth.public().into(), Default::default()))
.collect()
}

/// Add a BABE pre-digest to a generic header
fn add_babe_pre_digest(header: &mut Header, authority_index: u32, block_number: u64) {
/// This method generates some vrf data, but only to make the compiler happy
fn generate_testing_vrf() -> VrfSignature {
let vrf_proof_bytes = [0u8; 64];
let proof: VrfProof = VrfProof::decode(&mut vrf_proof_bytes.as_slice()).unwrap();
let vrf_pre_out_bytes = [0u8; 32];
let pre_output: VrfPreOutput =
VrfPreOutput::decode(&mut vrf_pre_out_bytes.as_slice()).unwrap();
VrfSignature { pre_output, proof }
}

let pre_digest = PrimaryPreDigest {
authority_index,
slot: block_number.into(),
vrf_signature: generate_testing_vrf(),
};

header
.digest_mut()
.push(DigestItem::babe_pre_digest(PreDigest::Primary(pre_digest)));
}

/// Create a mock chain of relay headers as descendants of the relay parent
pub fn build_relay_parent_descendants(
num_headers: u64,
state_root: H256,
authorities: Vec<AuthorityPair>,
) -> Vec<Header> {
let mut headers = Vec::with_capacity(num_headers as usize);

let mut previous_hash = None;

for block_number in 0..=num_headers as u32 {
let mut header = Header {
number: block_number,
parent_hash: previous_hash.unwrap_or_default(),
state_root,
extrinsics_root: H256::default(),
digest: Digest::default(),
};
let authority_index = block_number % (authorities.len() as u32);

// Add pre-digest
add_babe_pre_digest(&mut header, authority_index, block_number as u64);

// Sign and seal the header
let signature = authorities[authority_index as usize].sign(header.hash().as_bytes());
header.digest_mut().push(DigestItem::babe_seal(signature.into()));

previous_hash = Some(header.hash());
headers.push(header);
}

headers
}
5 changes: 3 additions & 2 deletions cumulus/test/service/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub fn extrinsic_set_validation_data(
..Default::default()
};

let (relay_parent_storage_root, relay_chain_state) = sproof_builder.into_state_root_and_proof();
let (relay_parent_storage_root, relay_chain_state, relay_parent_descendants) =
sproof_builder.into_state_root_proof_and_descendants(1);
let data = BasicParachainInherentData {
validation_data: PersistedValidationData {
parent_head,
Expand All @@ -98,7 +99,7 @@ pub fn extrinsic_set_validation_data(
max_pov_size: 10000,
},
relay_chain_state,
relay_parent_descendants: Default::default(),
relay_parent_descendants,
collator_peer_id: None,
};

Expand Down
15 changes: 11 additions & 4 deletions cumulus/xcm/xcm-emulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub use sp_tracing;
// Cumulus
pub use cumulus_pallet_parachain_system::{
parachain_inherent::{deconstruct_parachain_inherent_data, InboundMessagesData},
Call as ParachainSystemCall, Pallet as ParachainSystemPallet,
Call as ParachainSystemCall, Config as ParachainSystemConfig, Pallet as ParachainSystemPallet,
};
pub use cumulus_primitives_core::{
relay_chain::{BlockNumber as RelayBlockNumber, HeadData, HrmpChannelId},
Expand Down Expand Up @@ -205,6 +205,7 @@ pub trait Network {
para_id: u32,
relay_parent_number: u32,
parent_head_data: HeadData,
relay_parent_offset: u64,
) -> ParachainInherentData;
fn send_horizontal_messages<I: Iterator<Item = (ParaId, RelayBlockNumber, Vec<u8>)>>(
to_para_id: u32,
Expand Down Expand Up @@ -731,8 +732,11 @@ macro_rules! decl_test_parachains {
timestamp_set.dispatch(<Self as Chain>::RuntimeOrigin::none())
);

// Get RelayParentOffset from the runtime
let relay_parent_offset = <<<Self as $crate::Chain>::Runtime as $crate::ParachainSystemConfig>::RelayParentOffset as $crate::Get<u32>>::get();

// 2. inherent: cumulus_pallet_parachain_system::Call::set_validation_data
let data = N::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data);
let data = N::hrmp_channel_parachain_inherent_data(para_id, relay_block_number, parent_head_data, relay_parent_offset as u64);
let (data, mut downward_messages, mut horizontal_messages) =
$crate::deconstruct_parachain_inherent_data(data);
let inbound_messages_data = $crate::InboundMessagesData::new(
Expand Down Expand Up @@ -1195,11 +1199,13 @@ macro_rules! decl_test_networks {
para_id: u32,
relay_parent_number: u32,
parent_head_data: $crate::HeadData,
relay_parent_offset: u64,
) -> $crate::ParachainInherentData {
let mut sproof = $crate::RelayStateSproofBuilder::default();
sproof.para_id = para_id.into();
sproof.current_slot = $crate::polkadot_primitives::Slot::from(relay_parent_number as u64);
sproof.host_config.max_upward_message_size = 1024 * 1024;
sproof.num_authorities = relay_parent_offset + 1;

// egress channel
let e_index = sproof.hrmp_egress_channel_index.get_or_insert_with(Vec::new);
Expand Down Expand Up @@ -1227,7 +1233,8 @@ macro_rules! decl_test_networks {
});
}

let (relay_storage_root, proof) = sproof.into_state_root_and_proof();
let (relay_storage_root, proof, relay_parent_descendants) =
sproof.into_state_root_proof_and_descendants(relay_parent_offset);

$crate::ParachainInherentData {
validation_data: $crate::PersistedValidationData {
Expand All @@ -1239,7 +1246,7 @@ macro_rules! decl_test_networks {
relay_chain_state: proof,
downward_messages: Default::default(),
horizontal_messages: Default::default(),
relay_parent_descendants: Default::default(),
relay_parent_descendants,
collator_peer_id: None,
}
}
Expand Down
Loading
Loading