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
clean up custom idps
  • Loading branch information
liamaharon committed May 8, 2024
commit d58941c6ac6d1802a4ab2da8ee2f096985499373
4 changes: 2 additions & 2 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,7 @@ use sp_state_machine::TestExternalities;

use crate::{
build_executor, full_extensions,
inherents::providers::{pre_apply_inherents, ChainVariant, InherentProvider},
inherents::providers::{ChainVariant, InherentProvider},
state::{RuntimeChecks, State},
state_machine_call, state_machine_call_with_proof, BlockT, SharedParams,
};
Expand Down
3 changes: 3 additions & 0 deletions core/src/inherents/custom_idps/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod para_parachain;
pub mod relay_parachains;
pub mod timestamp;
106 changes: 106 additions & 0 deletions core/src/inherents/custom_idps/para_parachain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use parity_scale_codec::{Decode, Encode};
use polkadot_primitives::{BlockNumber, HeadData};
use sp_consensus_babe::SlotDuration;
use sp_core::twox_128;
use sp_inherents::InherentIdentifier;
use sp_runtime::traits::{Block as BlockT, HashingFor, NumberFor};
use sp_state_machine::TestExternalities;

/// 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 {
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")
}

/// 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 {
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))
.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")
}
}
}

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 parent_header: B::Header,
}

#[async_trait::async_trait]
impl<B: BlockT> sp_inherents::InherentDataProvider for InherentDataProvider<B> {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
let relay_chain_slot = cumulus_primitives_core::relay_chain::Slot::from_timestamp(
self.timestamp,
SlotDuration::from_millis(self.blocktime_millis),
)
.encode();

let additional_key_values: Vec<(Vec<u8>, Vec<u8>)> = vec![
// Insert relay chain slot to pass Aura check
// https://github.com/paritytech/polkadot-sdk/blob/ef114a422291b44f8973739ab7858a29a523e6a2/cumulus/pallets/aura-ext/src/consensus_hook.rs#L69
(
cumulus_primitives_core::relay_chain::well_known_keys::CURRENT_SLOT.to_vec(),
relay_chain_slot,
),
// 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(),
),
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_blocks_per_para_block: Default::default(),
para_blocks_per_relay_epoch: Default::default(),
relay_randomness_config: (),
xcm_config: cumulus_client_parachain_inherent::MockXcmConfig::default(),
raw_downward_messages: Default::default(),
raw_horizontal_messages: Default::default(),
additional_key_values: Some(additional_key_values),
}
.provide_inherent_data(inherent_data)
.await
.expect("Failed to provide Para Parachain inherent data.");

Ok(())
}

async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Custom InherentDataProviders.
//!
//! Useful to create custom inherent data providers for testing, when the one provided by
//! Substrate is unnecessarily too complex for try-runtime-cli purposes.
//! Relay chain parachains inherent.

use sp_inherents::InherentIdentifier;
use sp_runtime::traits::Block as BlockT;

pub struct ParaInherentDataProvider<B: BlockT> {
pub struct InherentDataProvider<B: BlockT> {
parent_header: B::Header,
}

impl<B: BlockT> ParaInherentDataProvider<B> {
impl<B: BlockT> InherentDataProvider<B> {
pub fn new(parent_header: B::Header) -> Self {
Self { parent_header }
}
}

/// Auxiliary trait to extract para inherent data.
pub trait ParaInherentData<B: BlockT> {
/// Get para inherent data.
fn para_inherent_data(&self) -> Result<Option<B::Header>, sp_inherents::Error>;
}

impl<B: BlockT> ParaInherentData<B> for sp_inherents::InherentData {
fn para_inherent_data(&self) -> Result<Option<B::Header>, sp_inherents::Error> {
self.get_data(&polkadot_primitives::PARACHAINS_INHERENT_IDENTIFIER)
}
}

#[async_trait::async_trait]
impl<B: BlockT> sp_inherents::InherentDataProvider for ParaInherentDataProvider<B> {
impl<B: BlockT> sp_inherents::InherentDataProvider for InherentDataProvider<B> {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
Expand Down
66 changes: 66 additions & 0 deletions core/src/inherents/custom_idps/timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use sp_inherents::{InherentData, InherentIdentifier};
use sp_runtime::Digest;
use sp_timestamp::{Timestamp, TimestampInherentData};

pub struct InherentDataProvider {
pub blocktime_millis: u64,
pub maybe_parent_info: Option<(InherentData, Digest)>,
}

impl InherentDataProvider {
pub fn timestamp(&self) -> Timestamp {
match &self.maybe_parent_info {
Some((prev_inherent_data, _)) => sp_timestamp::InherentDataProvider::new(
prev_inherent_data
.timestamp_inherent_data()
.unwrap()
.unwrap()
+ self.blocktime_millis,
)
.timestamp()
.into(),
None => sp_timestamp::InherentDataProvider::from_system_time()
.timestamp()
.into(),
}
}
}

#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
match &self.maybe_parent_info {
Some((prev_inherent_data, _)) => {
let idp = sp_timestamp::InherentDataProvider::new(
prev_inherent_data
.timestamp_inherent_data()
.unwrap()
.unwrap()
+ self.blocktime_millis,
);
idp.provide_inherent_data(inherent_data)
.await
.expect("Failed to provide timestamp inherent");
}
None => {
let idp = sp_timestamp::InherentDataProvider::from_system_time();
idp.provide_inherent_data(inherent_data)
.await
.expect("Failed to provide timestamp inherent");
}
};

Ok(())
}

async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}
Loading