Skip to content

Commit

Permalink
Mock relay block number in dev service. (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshOrndorff committed May 28, 2021
1 parent 7fd4ec9 commit a3680ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
22 changes: 20 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 parachain that starts in relay block 1000 and gets a block in every other relay
/// 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 @@ -91,11 +105,15 @@ impl InherentDataProvider for MockValidationDataInherentDataProvider {
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
36 changes: 25 additions & 11 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use sc_service::{
TFullClient, TaskManager,
};
use sp_api::ConstructRuntimeApi;
use sp_blockchain::HeaderBackend;
use std::sync::Arc;

pub use client::*;
Expand Down Expand Up @@ -851,6 +852,8 @@ pub fn new_dev(
Therefore, a `LongestChainRule` is present. qed.",
);

let client_set_aside_for_cidp = client.clone();

task_manager.spawn_essential_handle().spawn_blocking(
"authorship_task",
run_manual_seal(ManualSealParams {
Expand All @@ -861,17 +864,28 @@ pub fn new_dev(
commands_stream,
select_chain,
consensus_data_provider: None,
create_inherent_data_providers: move |_, _| async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();

let mocked_parachain = inherents::MockValidationDataInherentDataProvider;

//TODO I want to use the value from a variable above.
let author = nimbus_primitives::InherentDataProvider::<NimbusId>(
chain_spec::get_from_seed::<NimbusId>("Alice"),
);

Ok((time, mocked_parachain, author))
create_inherent_data_providers: move |block: H256, ()| {
let current_para_block = client_set_aside_for_cidp
.number(block)
.expect("Header lookup should succeed")
.expect("Header passed in as parent should be present in backend.");

async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();

let mocked_parachain = inherents::MockValidationDataInherentDataProvider {
current_para_block,
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>(
chain_spec::get_from_seed::<NimbusId>("Alice"),
);

Ok((time, mocked_parachain, author))
}
},
}),
);
Expand Down

0 comments on commit a3680ad

Please sign in to comment.