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

Mock epoch index and randomness in SproofBuilder (for testing) #1594

Merged
merged 18 commits into from
Sep 8, 2022
Merged
8 changes: 8 additions & 0 deletions primitives/parachain-inherent/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ impl InherentDataProvider for MockValidationDataInherentDataProvider {
sproof_builder.upsert_inbound_channel(*para_id).mqc_head = Some(channel_mqc.head());
}

// Epoch is set equal to current para block
sproof_builder.current_epoch = self.current_para_block.into();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not make this configurable to the outside? I find it weird to use the current para block for this.

Copy link
Contributor Author

@4meta5 4meta5 Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the epoch configurable in terms of the number of parachain blocks per relay chain epoch. Let me know if it works.

// Randomness is set equal to current relay block
// could improve mock randomness by XOR with [u8::MAX; 32]
let mut mock_randomness: [u8; 32] = [0u8; 32];
mock_randomness[..4].copy_from_slice(&relay_parent_number.to_be_bytes());
sproof_builder.randomness = mock_randomness.into();

let (relay_parent_storage_root, proof) = sproof_builder.into_state_root_and_proof();

inherent_data.put_data(
Expand Down
6 changes: 6 additions & 0 deletions test/relay-sproof-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct RelayStateSproofBuilder {
pub hrmp_channels: BTreeMap<relay_chain::v2::HrmpChannelId, AbridgedHrmpChannel>,
pub current_slot: relay_chain::v2::Slot,
pub current_epoch: u64,
pub randomness: relay_chain::Hash,
}

impl Default for RelayStateSproofBuilder {
Expand All @@ -69,6 +70,7 @@ impl Default for RelayStateSproofBuilder {
hrmp_channels: BTreeMap::new(),
current_slot: 0.into(),
current_epoch: 0u64,
randomness: relay_chain::Hash::default(),
}
}
}
Expand Down Expand Up @@ -156,6 +158,10 @@ impl RelayStateSproofBuilder {
insert(relay_chain::well_known_keys::hrmp_channels(channel), metadata.encode());
}
insert(relay_chain::well_known_keys::EPOCH_INDEX.to_vec(), self.current_epoch.encode());
insert(
relay_chain::well_known_keys::ONE_EPOCH_AGO_RANDOMNESS.to_vec(),
self.randomness.encode(),
);
insert(relay_chain::well_known_keys::CURRENT_SLOT.to_vec(), self.current_slot.encode());
}

Expand Down