mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 23:31:07 +00:00
Adds support for checking the timestamp inherent while validating a block (#494)
* Adds support for checking the timestamp inherent while validating a block This adds support for checking the timestamp inherent while validating a block. This will use the relay chain slot number * relay chain slot duration to calculate a timestamp. This timestamp is used to check the timestamp in the timestamp inherent. * Update polkadot-parachains/rococo-runtime/src/lib.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Update polkadot-parachains/statemine-runtime/src/lib.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Update primitives/timestamp/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Fix warnings Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,8 @@ edition = "2018"
|
||||
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sc-block-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sc-executor-common = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
@@ -16,6 +18,7 @@ sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master
|
||||
sp-test-primitives = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
@@ -31,6 +34,7 @@ cumulus-primitives-parachain-inherent = { path = "../../primitives/parachain-inh
|
||||
|
||||
# Polkadot deps
|
||||
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" }
|
||||
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "master" }
|
||||
|
||||
# Other deps
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [ "derive" ] }
|
||||
|
||||
@@ -15,14 +15,17 @@
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Backend, Client};
|
||||
use cumulus_primitives_core::PersistedValidationData;
|
||||
use cumulus_primitives_core::{ParachainBlockData, PersistedValidationData};
|
||||
use cumulus_primitives_parachain_inherent::{ParachainInherentData, INHERENT_IDENTIFIER};
|
||||
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
|
||||
use cumulus_test_runtime::{Block, GetLastTimestamp};
|
||||
use cumulus_test_runtime::{Block, GetLastTimestamp, Hash, Header};
|
||||
use polkadot_primitives::v1::{BlockNumber as PBlockNumber, Hash as PHash};
|
||||
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, Header as HeaderT},
|
||||
};
|
||||
|
||||
/// An extension for the Cumulus test client to init a block builder.
|
||||
pub trait InitBlockBuilder {
|
||||
@@ -49,6 +52,70 @@ pub trait InitBlockBuilder {
|
||||
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
|
||||
relay_sproof_builder: RelayStateSproofBuilder,
|
||||
) -> sc_block_builder::BlockBuilder<Block, Client, Backend>;
|
||||
|
||||
/// Init a specific block builder that works for the test runtime.
|
||||
///
|
||||
/// Same as [`InitBlockBuilder::init_block_builder`] besides that it takes a
|
||||
/// [`BlockId`] to say which should be the parent block of the block that is being build and
|
||||
/// it will use the given `timestamp` as input for the timestamp inherent.
|
||||
fn init_block_builder_with_timestamp(
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
|
||||
relay_sproof_builder: RelayStateSproofBuilder,
|
||||
timestamp: u64,
|
||||
) -> sc_block_builder::BlockBuilder<Block, Client, Backend>;
|
||||
}
|
||||
|
||||
fn init_block_builder<'a>(
|
||||
client: &'a Client,
|
||||
at: &BlockId<Block>,
|
||||
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
|
||||
relay_sproof_builder: RelayStateSproofBuilder,
|
||||
timestamp: u64,
|
||||
) -> BlockBuilder<'a, Block, Client, Backend> {
|
||||
let mut block_builder = client
|
||||
.new_block_at(at, Default::default(), true)
|
||||
.expect("Creates new block builder for test runtime");
|
||||
|
||||
let mut inherent_data = sp_inherents::InherentData::new();
|
||||
|
||||
inherent_data
|
||||
.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
.expect("Put timestamp failed");
|
||||
|
||||
let (relay_parent_storage_root, relay_chain_state) =
|
||||
relay_sproof_builder.into_state_root_and_proof();
|
||||
|
||||
let mut validation_data = validation_data.unwrap_or_default();
|
||||
assert_eq!(
|
||||
validation_data.relay_parent_storage_root,
|
||||
Default::default(),
|
||||
"Overriding the relay storage root is not implemented",
|
||||
);
|
||||
validation_data.relay_parent_storage_root = relay_parent_storage_root;
|
||||
|
||||
inherent_data
|
||||
.put_data(
|
||||
INHERENT_IDENTIFIER,
|
||||
&ParachainInherentData {
|
||||
validation_data,
|
||||
relay_chain_state,
|
||||
downward_messages: Default::default(),
|
||||
horizontal_messages: Default::default(),
|
||||
},
|
||||
)
|
||||
.expect("Put validation function params failed");
|
||||
|
||||
let inherents = block_builder
|
||||
.create_inherents(inherent_data)
|
||||
.expect("Creates inherents");
|
||||
|
||||
inherents
|
||||
.into_iter()
|
||||
.for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
|
||||
|
||||
block_builder
|
||||
}
|
||||
|
||||
impl InitBlockBuilder for Client {
|
||||
@@ -71,11 +138,6 @@ impl InitBlockBuilder for Client {
|
||||
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
|
||||
relay_sproof_builder: RelayStateSproofBuilder,
|
||||
) -> BlockBuilder<Block, Client, Backend> {
|
||||
let mut block_builder = self
|
||||
.new_block_at(at, Default::default(), true)
|
||||
.expect("Creates new block builder for test runtime");
|
||||
|
||||
let mut inherent_data = sp_inherents::InherentData::new();
|
||||
let last_timestamp = self
|
||||
.runtime_api()
|
||||
.get_last_timestamp(&at)
|
||||
@@ -83,41 +145,38 @@ impl InitBlockBuilder for Client {
|
||||
|
||||
let timestamp = last_timestamp + cumulus_test_runtime::MinimumPeriod::get();
|
||||
|
||||
inherent_data
|
||||
.put_data(sp_timestamp::INHERENT_IDENTIFIER, ×tamp)
|
||||
.expect("Put timestamp failed");
|
||||
init_block_builder(self, at, validation_data, relay_sproof_builder, timestamp)
|
||||
}
|
||||
|
||||
let (relay_parent_storage_root, relay_chain_state) =
|
||||
relay_sproof_builder.into_state_root_and_proof();
|
||||
|
||||
let mut validation_data = validation_data.unwrap_or_default();
|
||||
assert_eq!(
|
||||
validation_data.relay_parent_storage_root,
|
||||
Default::default(),
|
||||
"Overriding the relay storage root is not implemented",
|
||||
);
|
||||
validation_data.relay_parent_storage_root = relay_parent_storage_root;
|
||||
|
||||
inherent_data
|
||||
.put_data(
|
||||
INHERENT_IDENTIFIER,
|
||||
&ParachainInherentData {
|
||||
validation_data,
|
||||
relay_chain_state,
|
||||
downward_messages: Default::default(),
|
||||
horizontal_messages: Default::default(),
|
||||
},
|
||||
)
|
||||
.expect("Put validation function params failed");
|
||||
|
||||
let inherents = block_builder
|
||||
.create_inherents(inherent_data)
|
||||
.expect("Creates inherents");
|
||||
|
||||
inherents
|
||||
.into_iter()
|
||||
.for_each(|ext| block_builder.push(ext).expect("Pushes inherent"));
|
||||
|
||||
block_builder
|
||||
fn init_block_builder_with_timestamp(
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
validation_data: Option<PersistedValidationData<PHash, PBlockNumber>>,
|
||||
relay_sproof_builder: RelayStateSproofBuilder,
|
||||
timestamp: u64,
|
||||
) -> sc_block_builder::BlockBuilder<Block, Client, Backend> {
|
||||
init_block_builder(self, at, validation_data, relay_sproof_builder, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for the [`BlockBuilder`](sc_block_builder::BlockBuilder) to build directly a
|
||||
/// [`ParachainBlockData`].
|
||||
pub trait BuildParachainBlockData {
|
||||
/// Directly build the [`ParachainBlockData`] from the block that comes out of the block builder.
|
||||
fn build_parachain_block(self, parent_state_root: Hash) -> ParachainBlockData<Block>;
|
||||
}
|
||||
|
||||
impl<'a> BuildParachainBlockData for sc_block_builder::BlockBuilder<'a, Block, Client, Backend> {
|
||||
fn build_parachain_block(self, parent_state_root: Hash) -> ParachainBlockData<Block> {
|
||||
let built_block = self.build().expect("Builds the block");
|
||||
|
||||
let storage_proof = built_block
|
||||
.proof
|
||||
.expect("We enabled proof recording before.")
|
||||
.into_compact_proof::<<Header as HeaderT>::Hashing>(parent_state_root)
|
||||
.expect("Creates the compact proof");
|
||||
|
||||
let (header, extrinsics) = built_block.block.deconstruct();
|
||||
ParachainBlockData::new(header, extrinsics, storage_proof)
|
||||
}
|
||||
}
|
||||
|
||||
+41
-6
@@ -1,36 +1,43 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
// This file is part of Cumulus.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// Cumulus is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// Cumulus is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A Polkadot test client.
|
||||
//! A Cumulus test client.
|
||||
|
||||
mod block_builder;
|
||||
use codec::Encode;
|
||||
use codec::{Encode, Decode};
|
||||
use runtime::{
|
||||
Balance, Block, BlockHashCount, Call, GenesisConfig, Runtime, Signature, SignedExtra,
|
||||
SignedPayload, UncheckedExtrinsic, VERSION,
|
||||
};
|
||||
use sc_executor::{sp_wasm_interface::HostFunctions, WasmExecutionMethod, WasmExecutor};
|
||||
use sc_executor_common::runtime_blob::RuntimeBlob;
|
||||
use sc_service::client;
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_core::storage::Storage;
|
||||
use sp_io::TestExternalities;
|
||||
use sp_runtime::{generic::Era, BuildStorage, SaturatedConversion};
|
||||
|
||||
pub use block_builder::*;
|
||||
pub use cumulus_test_runtime as runtime;
|
||||
pub use polkadot_parachain::primitives::{BlockData, HeadData, ValidationParams, ValidationResult};
|
||||
pub use sc_executor::error::Result as ExecutorResult;
|
||||
pub use substrate_test_client::*;
|
||||
|
||||
pub type ParachainBlockData = cumulus_primitives_core::ParachainBlockData<Block>;
|
||||
|
||||
mod local_executor {
|
||||
use substrate_test_client::sc_executor::native_executor_instance;
|
||||
native_executor_instance!(
|
||||
@@ -159,3 +166,31 @@ pub fn transfer(
|
||||
|
||||
generate_extrinsic(client, origin, function)
|
||||
}
|
||||
|
||||
/// Call `validate_block` in the given `wasm_blob`.
|
||||
pub fn validate_block(
|
||||
validation_params: ValidationParams,
|
||||
wasm_blob: &[u8],
|
||||
) -> ExecutorResult<ValidationResult> {
|
||||
let mut ext = TestExternalities::default();
|
||||
let mut ext_ext = ext.ext();
|
||||
|
||||
let executor = WasmExecutor::new(
|
||||
WasmExecutionMethod::Interpreted,
|
||||
Some(1024),
|
||||
sp_io::SubstrateHostFunctions::host_functions(),
|
||||
1,
|
||||
None,
|
||||
);
|
||||
|
||||
executor
|
||||
.uncached_call(
|
||||
RuntimeBlob::uncompress_if_needed(wasm_blob).expect("RuntimeBlob uncompress & parse"),
|
||||
&mut ext_ext,
|
||||
false,
|
||||
"validate_block",
|
||||
&validation_params.encode(),
|
||||
)
|
||||
.map(|v| ValidationResult::decode(&mut &v[..]).expect("Decode `ValidationResult`."))
|
||||
.map_err(|err| err.into())
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ sp-version = { git = "https://github.com/paritytech/substrate", default-features
|
||||
# Cumulus dependencies
|
||||
cumulus-pallet-parachain-system = { path = "../../pallets/parachain-system", default-features = false }
|
||||
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
|
||||
cumulus-primitives-timestamp = { path = "../../primitives/timestamp", default-features = false }
|
||||
|
||||
# Polkadot dependencies
|
||||
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
@@ -45,6 +46,7 @@ std = [
|
||||
"codec/std",
|
||||
"cumulus-pallet-parachain-system/std",
|
||||
"cumulus-primitives-core/std",
|
||||
"cumulus-primitives-timestamp/std",
|
||||
"frame-executive/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
|
||||
@@ -32,6 +32,7 @@ sp-version = { git = "https://github.com/paritytech/substrate", default-features
|
||||
# Cumulus dependencies
|
||||
cumulus-pallet-parachain-system = { path = "../../pallets/parachain-system", default-features = false }
|
||||
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
|
||||
cumulus-primitives-timestamp = { path = "../../primitives/timestamp", default-features = false }
|
||||
|
||||
# Polkadot dependencies
|
||||
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||
@@ -45,6 +46,7 @@ std = [
|
||||
"codec/std",
|
||||
"cumulus-pallet-parachain-system/std",
|
||||
"cumulus-primitives-core/std",
|
||||
"cumulus-primitives-timestamp/std",
|
||||
"frame-executive/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
|
||||
+13
-3
@@ -96,7 +96,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
transaction_version: 1,
|
||||
};
|
||||
|
||||
pub const MILLISECS_PER_BLOCK: u64 = 1000;
|
||||
pub const MILLISECS_PER_BLOCK: u64 = 6000;
|
||||
|
||||
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
|
||||
|
||||
@@ -431,7 +431,7 @@ struct CheckInherents;
|
||||
|
||||
impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {
|
||||
fn check_inherents(
|
||||
_: &[UncheckedExtrinsic],
|
||||
block: &Block,
|
||||
relay_state_proof: &cumulus_pallet_parachain_system::RelayChainStateProof,
|
||||
) -> sp_inherents::CheckInherentsResult {
|
||||
if relay_state_proof.read_slot().expect("Reads slot") == 1337u64 {
|
||||
@@ -443,7 +443,17 @@ impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {
|
||||
.expect("Puts error");
|
||||
res
|
||||
} else {
|
||||
sp_inherents::CheckInherentsResult::new()
|
||||
let relay_chain_slot = relay_state_proof
|
||||
.read_slot()
|
||||
.expect("Could not read the relay chain slot from the proof");
|
||||
|
||||
let inherent_data =
|
||||
cumulus_primitives_timestamp::InherentDataProvider::from_relay_chain_slot_and_duration(
|
||||
relay_chain_slot,
|
||||
sp_std::time::Duration::from_secs(6),
|
||||
).create_inherent_data().expect("Could not create the timestamp inherent data");
|
||||
|
||||
inherent_data.check_extrinsics(&block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ cumulus-test-relay-validation-worker-provider = { path = "../relay-validation-wo
|
||||
jsonrpc-core = "15.1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
futures = { version = "0.3.5" }
|
||||
futures = "0.3.5"
|
||||
tokio = { version = "0.2.21", features = ["macros"] }
|
||||
|
||||
# Polkadot dependencies
|
||||
|
||||
Reference in New Issue
Block a user