Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -116,6 +116,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
15 changes: 13 additions & 2 deletions cumulus/test/relay-sproof-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ workspace = true

[dependencies]
codec = { features = ["derive"], workspace = true }
cumulus-primitives-core.workspace = true
polkadot-primitives.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

# Polkadot
polkadot-primitives.workspace = true

# Cumulus
cumulus-primitives-core.workspace = true


[features]
default = ["std"]
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
Loading
Loading