Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d1bce56
inherent overhaul
liamaharon May 7, 2024
20878ee
bump version
liamaharon May 7, 2024
d58941c
clean up custom idps
liamaharon May 8, 2024
ff30415
smart inherent provider
liamaharon May 8, 2024
5883f46
licence and basic docs
liamaharon May 9, 2024
b27fd10
clippy
liamaharon May 9, 2024
62d9696
remove node-executor node-primitive deps
liamaharon May 9, 2024
9c5fcbe
fmt
liamaharon May 9, 2024
ac898c2
update cargo.lock
liamaharon May 9, 2024
36a8cf1
move shared params to common
liamaharon May 10, 2024
4424f61
move misc logging to common
liamaharon May 10, 2024
a0a5485
move parse to common
liamaharon May 10, 2024
16c5b39
move state to common
liamaharon May 10, 2024
9632a14
move inherents to empty block production dir
liamaharon May 10, 2024
4f7e04e
move block creation out of fast forward
liamaharon May 10, 2024
b728086
improve log
liamaharon May 10, 2024
039cdaf
clippy
liamaharon May 10, 2024
bcdd083
cargo fmt
liamaharon May 13, 2024
4a406c6
update test runtimes and snapshots
liamaharon May 14, 2024
fd0092e
use stable for clippy
liamaharon May 14, 2024
4b4648b
fix typos
liamaharon May 14, 2024
527c857
Update core/src/common/empty_block/inherents/pre_apply.rs
liamaharon May 15, 2024
c7dbbfe
address comments
liamaharon May 16, 2024
8b465e2
doc comment
liamaharon May 16, 2024
828a357
doc
liamaharon May 16, 2024
5980993
use valueenum
liamaharon May 16, 2024
aee8086
rename execute_next_block to mine_block
liamaharon May 16, 2024
2254202
bump relay_offset +1
liamaharon May 17, 2024
00e2962
blocktime arg
liamaharon May 20, 2024
e80f524
make blocktime a required value
liamaharon May 20, 2024
47a76ca
hardcode relaychain blocktime
liamaharon May 20, 2024
c6a143b
clippy
liamaharon May 26, 2024
5fdd6d5
free disk space on test runner
liamaharon May 26, 2024
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
Prev Previous commit
Next Next commit
smart inherent provider
  • Loading branch information
liamaharon committed May 8, 2024
commit ff30415853a19d3f9b73752e5801518740ff9ab9
21 changes: 12 additions & 9 deletions core/src/commands/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

use std::{fmt::Debug, str::FromStr};
use crate::inherents::providers::pre_apply_inherents;

use parity_scale_codec::{Decode, Encode};
use sc_cli::Result;
use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutor};
Expand All @@ -31,7 +31,10 @@ use sp_state_machine::TestExternalities;

use crate::{
build_executor, full_extensions,
inherents::providers::{ChainVariant, InherentProvider},
inherents::{
pre_apply::pre_apply_inherents,
providers::{InherentProvider, ProviderVariant},
},
state::{RuntimeChecks, State},
state_machine_call, state_machine_call_with_proof, BlockT, SharedParams,
};
Expand All @@ -44,8 +47,8 @@ pub struct Command {
pub n_blocks: u64,

/// ChainVariant
#[arg(long)]
pub chain: ChainVariant,
#[arg(long, default_value = "smart")]
pub provider_variant: ProviderVariant,

/// Which try-state targets to execute when running this command.
///
Expand Down Expand Up @@ -117,7 +120,7 @@ async fn produce_next_block<Block: BlockT, HostFns: HostFunctions>(
externalities: &mut TestExternalities<HashingFor<Block>>,
executor: &WasmExecutor<HostFns>,
parent_header: Block::Header,
chain: ChainVariant,
chain: ProviderVariant,
previous_block_building_info: Option<(InherentData, Digest)>,
) -> Result<(Block, Option<(InherentData, Digest)>)>
where
Expand All @@ -128,14 +131,14 @@ where
<NumberFor<Block> as FromStr>::Err: Debug,
{
let (inherent_data_provider, pre_digest) =
<ChainVariant as InherentProvider<Block>>::get_inherent_providers_and_pre_digest(
<ProviderVariant as InherentProvider<Block>>::get_inherent_providers_and_pre_digest(
&chain,
previous_block_building_info,
parent_header.clone(),
externalities,
)?;

pre_apply_inherents::<Block>(chain, externalities);
pre_apply_inherents::<Block>(externalities);
let inherent_data = inherent_data_provider
.create_inherent_data()
.await
Expand Down Expand Up @@ -248,7 +251,7 @@ where
&mut inner_ext,
&executor,
parent_header.clone(),
command.chain,
command.provider_variant,
parent_block_building_info,
)
.await?;
Expand All @@ -258,7 +261,7 @@ where
// And now we restore previous state.
inner_ext.backend = backend;

pre_apply_inherents::<Block>(command.chain, &mut inner_ext);
pre_apply_inherents::<Block>(&mut inner_ext);
let state_root_check = true;
let signature_check = true;
let payload = (
Expand Down
56 changes: 35 additions & 21 deletions core/src/inherents/custom_idps/para_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,51 @@ use sp_inherents::InherentIdentifier;
use sp_runtime::traits::{Block as BlockT, HashingFor, NumberFor};
use sp_state_machine::TestExternalities;

pub fn is_parachain<B: BlockT>(ext: &mut TestExternalities<HashingFor<B>>) -> bool {
let para_id_key = [twox_128(b"ParachainInfo"), twox_128(b"ParachainId")].concat();

ext.execute_with(|| sp_io::storage::get(&para_id_key))
.map(|b| -> Option<u32> { Decode::decode(&mut &b[..]).ok() })
.is_some()
}

/// ext cannot be part of the InherentDataProvider (thread safety), so we need to make storage
/// queries seperately
pub fn get_para_id<B: BlockT>(ext: &mut TestExternalities<HashingFor<B>>) -> u32 {
pub fn get_para_id<B: BlockT>(ext: &mut TestExternalities<HashingFor<B>>) -> Option<u32> {
let para_id_key = [twox_128(b"ParachainInfo"), twox_128(b"ParachainId")].concat();

ext.execute_with(|| sp_io::storage::get(&para_id_key))
.map(|b| -> u32 { Decode::decode(&mut &b[..]).unwrap() })
.expect("Parachain must have an ID")
.map(|b| -> Option<u32> { Decode::decode(&mut &b[..]).ok() })
.flatten()
}

/// ext cannot be part of the InherentDataProvider (thread safety), so we need to make storage
/// queries seperately
pub fn get_last_relay_chain_block_number<B: BlockT>(
ext: &mut TestExternalities<HashingFor<B>>,
) -> BlockNumber {
) -> Option<BlockNumber> {
let last_relay_chain_block_number_key = [
twox_128(b"ParachainSystem"),
twox_128(b"LastRelayChainBlockNumber"),
]
.concat();

match ext
.execute_with(|| sp_io::storage::get(&last_relay_chain_block_number_key))
ext.execute_with(|| sp_io::storage::get(&last_relay_chain_block_number_key))
.map(|b| -> Option<NumberFor<B>> { Decode::decode(&mut &b[..]).ok() })
.unwrap()
.expect("Parachain must provide last relay chain block number")
.try_into()
{
Ok(block_number) => block_number,
Err(_) => {
panic!("Failed to convert relay chain block number")
}
}
.flatten()
.map(|n| match n.try_into() {
Ok(block_number) => block_number,
Err(_) => {
panic!("Failed to convert relay chain block number")
}
})
}

pub struct InherentDataProvider<B: BlockT> {
pub timestamp: sp_timestamp::Timestamp,
pub blocktime_millis: u64,
pub last_relay_chain_block_number: BlockNumber,
pub para_id: u32,
pub maybe_last_relay_chain_block_number: Option<BlockNumber>,
pub maybe_para_id: Option<u32>,
pub parent_header: B::Header,
}

Expand All @@ -55,6 +60,17 @@ impl<B: BlockT> sp_inherents::InherentDataProvider for InherentDataProvider<B> {
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
let (last_relay_chain_block_number, para_id) =
match (self.maybe_last_relay_chain_block_number, self.maybe_para_id) {
(Some(last_relay_chain_block_number), Some(para_id)) => {
(last_relay_chain_block_number, para_id)
}
_ => {
log::debug!("Unable to provide para parachains inherent for this chain.");
return Ok(());
}
};

let relay_chain_slot = cumulus_primitives_core::relay_chain::Slot::from_timestamp(
self.timestamp,
SlotDuration::from_millis(self.blocktime_millis),
Expand All @@ -71,16 +87,14 @@ impl<B: BlockT> sp_inherents::InherentDataProvider for InherentDataProvider<B> {
// Insert para header info to pass para inherent check
// https://github.com/paritytech/polkadot-sdk/blob/17b56fae2d976a3df87f34076875de8c26da0355/cumulus/pallets/parachain-system/src/lib.rs#L1296
(
cumulus_primitives_core::relay_chain::well_known_keys::para_head(
self.para_id.into(),
),
cumulus_primitives_core::relay_chain::well_known_keys::para_head(para_id.into()),
HeadData(self.parent_header.encode()).encode(),
),
];

cumulus_client_parachain_inherent::MockValidationDataInherentDataProvider {
current_para_block: Default::default(),
relay_offset: self.last_relay_chain_block_number,
relay_offset: last_relay_chain_block_number,
relay_blocks_per_para_block: Default::default(),
para_blocks_per_relay_epoch: Default::default(),
relay_randomness_config: (),
Expand Down
1 change: 1 addition & 0 deletions core/src/inherents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod custom_idps;
pub mod pre_apply;
pub mod providers;
21 changes: 21 additions & 0 deletions core/src/inherents/pre_apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use cumulus_primitives_parachain_inherent::MessageQueueChain;
use parity_scale_codec::Encode;
use sp_core::{twox_128, H256};
use sp_runtime::traits::{Block as BlockT, HashingFor};
use sp_state_machine::TestExternalities;

/// Some operations must be to performed prior to inherents being applied.
pub fn pre_apply_inherents<B: BlockT>(ext: &mut TestExternalities<HashingFor<B>>) {
// set the last dmq mcq head value to zero to pass this check
// https://github.com/paritytech/polkadot-sdk/blob/ef114a422291b44f8973739ab7858a29a523e6a2/cumulus/pallets/parachain-system/src/lib.rs#L1162
//
// it would have been preferable to set it to the real value in the mock inherent
// provider for parachain system, but that would require the paraid which we cannot
// derive from the externalities.
let last_dmq_mqc_head_key =
[twox_128(b"ParachainSystem"), twox_128(b"LastDmqMqcHead")].concat();
ext.insert(
last_dmq_mqc_head_key.to_vec(),
MessageQueueChain::new(H256::zero()).encode(),
);
}
Loading