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 4 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
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 parachaint hat 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 @@ -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