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

Mock relay block number in dev service. #453

Merged
merged 5 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions node/service/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
///
/// This is useful when running a node that is not actually backed by any relay chain.
/// For example when running a local node, or running integration tests.
pub struct MockValidationDataInherentDataProvider;
///
/// We mock a relay chain block number as follows:
/// relay_block_number = offset + relay_blocks_per_para_block * current_para_block
/// To simulate a parachai nthat starts in relay block 1000 and gets a block in every other relay
JoshOrndorff marked this conversation as resolved.
Show resolved Hide resolved
/// block, use 1000 and 2
pub struct MockValidationDataInherentDataProvider {
/// The current block number of the local block chain (the parachain)
pub current_para_block: u32,
/// The relay block in which this parachain appeared to start. This will be the relay block
/// number in para block #P1
pub relay_offset: u32,
/// The number of relay blocks that elapses between each parablock. Probably set this to 1 or 2
/// to simulate optimistic or realistic relay chain behavior.
pub relay_blocks_per_para_block: u32,
}

#[async_trait::async_trait]
impl InherentDataProvider for MockValidationDataInherentDataProvider {
Expand All @@ -90,12 +104,15 @@ impl InherentDataProvider for MockValidationDataInherentDataProvider {
// Use the "sproof" (spoof proof) builder to build valid mock state root and proof.
let (relay_storage_root, proof) =
RelayStateSproofBuilder::default().into_state_root_and_proof();

// Calculate the mocked relay block based on the current para block
let relay_parent_number = self.relay_offset + self.relay_blocks_per_para_block * self.current_para_block;

let data = ParachainInherentData {
validation_data: PersistedValidationData {
parent_head: Default::default(),
relay_parent_storage_root: relay_storage_root,
relay_parent_number: Default::default(),
relay_parent_number,
max_pov_size: Default::default(),
},
downward_messages: Default::default(),
Expand Down
9 changes: 7 additions & 2 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,15 @@ pub fn new_dev(
commands_stream,
select_chain,
consensus_data_provider: None,
create_inherent_data_providers: move |_, _| async move {
//TODO I need a type annotation on the block type here. What type should I use?
create_inherent_data_providers: move |block, ()| async move {
JoshOrndorff marked this conversation as resolved.
Show resolved Hide resolved
let time = sp_timestamp::InherentDataProvider::from_system_time();

let mocked_parachain = inherents::MockValidationDataInherentDataProvider;
let mocked_parachain = inherents::MockValidationDataInherentDataProvider{
current_para_block: block.header().number().into(),
relay_offset: 1000,
relay_blocks_per_para_block: 2,
};

//TODO I want to use the value from a variable above.
let author = nimbus_primitives::InherentDataProvider::<NimbusId>(
Expand Down