Skip to content

Commit 00e2962

Browse files
committed
blocktime arg
1 parent 2254202 commit 00e2962

File tree

5 files changed

+58
-77
lines changed

5 files changed

+58
-77
lines changed

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1313

1414
[dependencies]
1515
# Crates.io
16+
tokio = { workspace = true }
1617
async-trait = { workspace = true }
1718
array-bytes = { workspace = true }
1819
polkadot-primitives = { workspace = true }
@@ -64,7 +65,6 @@ similar-asserts = "1.5.0"
6465
assert_cmd = { workspace = true }
6566
regex = { workspace = true }
6667
tempfile = { workspace = true }
67-
tokio = { workspace = true }
6868

6969
sc-service = { workspace = true }
7070
substrate-cli-test-utils = { workspace = true, features = ["try-runtime"] }

core/src/commands/fast_forward.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
use std::{
19-
fmt::Debug,
20-
str::FromStr,
21-
sync::{Arc, Mutex},
22-
};
18+
use std::{fmt::Debug, str::FromStr, sync::Arc, time::Duration};
2319

2420
use parity_scale_codec::Encode;
2521
use sc_cli::Result;
2622
use sc_executor::sp_wasm_interface::HostFunctions;
2723
use serde::de::DeserializeOwned;
2824
use sp_core::H256;
2925
use sp_runtime::traits::NumberFor;
26+
use tokio::sync::Mutex;
3027

3128
use crate::{
3229
common::{
@@ -43,11 +40,9 @@ pub struct Command {
4340
#[arg(long)]
4441
pub n_blocks: u64,
4542

46-
/// Which inherent provider variant to use. In most cases "smart" should be used, which
47-
/// attempts to support all chains.
48-
#[arg(long, default_value = "smart")]
49-
#[clap(value_enum)]
50-
pub provider_variant: ProviderVariant,
43+
/// The chain blocktime in milliseconds.
44+
#[arg(long, default_value = "6000")]
45+
pub blocktime: u64,
5146

5247
/// Which try-state targets to execute when running this command.
5348
///
@@ -108,14 +103,15 @@ where
108103
let inner_ext = Arc::new(Mutex::new(ext.inner_ext));
109104
let mut parent_header = ext.header.clone();
110105
let mut parent_block_building_info = None;
106+
let provider_variant = ProviderVariant::Smart(Duration::from_millis(command.blocktime));
111107

112108
for _ in 1..=command.n_blocks {
113109
let (next_block_building_info, next_header) = mine_block::<Block, HostFns>(
114110
inner_ext.clone(),
115111
&executor,
116112
parent_block_building_info,
117113
parent_header.clone(),
118-
command.provider_variant,
114+
provider_variant,
119115
command.try_state.clone(),
120116
)
121117
.await?;

core/src/common/empty_block/inherents/custom_idps/para_parachain.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Inherent data provider for the [cumulus parachin inherents](https://github.com/paritytech/polkadot-sdk/blob/master/cumulus/primitives/parachain-inherent/src/lib.rs)
1919
//! for empty block production on top of an existing externalities.
2020
21-
use std::sync::{Arc, Mutex};
21+
use std::{ops::DerefMut, sync::Arc};
2222

2323
use parity_scale_codec::{Decode, Encode};
2424
use polkadot_primitives::{BlockNumber, HeadData};
@@ -27,6 +27,7 @@ use sp_core::twox_128;
2727
use sp_inherents::InherentIdentifier;
2828
use sp_runtime::traits::{Block as BlockT, HashingFor, NumberFor};
2929
use sp_state_machine::TestExternalities;
30+
use tokio::sync::Mutex;
3031

3132
/// Get the para id if it exists
3233
pub fn get_para_id<B: BlockT>(ext: &mut TestExternalities<HashingFor<B>>) -> Option<u32> {
@@ -61,7 +62,7 @@ pub struct InherentDataProvider<B: BlockT> {
6162
pub timestamp: sp_timestamp::Timestamp,
6263
pub blocktime_millis: u64,
6364
pub parent_header: B::Header,
64-
pub ext: Arc<Mutex<TestExternalities<HashingFor<B>>>>,
65+
pub ext_mutex: Arc<Mutex<TestExternalities<HashingFor<B>>>>,
6566
}
6667

6768
#[async_trait::async_trait]
@@ -70,9 +71,10 @@ impl<B: BlockT> sp_inherents::InherentDataProvider for InherentDataProvider<B> {
7071
&self,
7172
inherent_data: &mut sp_inherents::InherentData,
7273
) -> Result<(), sp_inherents::Error> {
73-
let maybe_last_relay_chain_block_number =
74-
get_last_relay_chain_block_number::<B>(&mut self.ext.lock().unwrap());
75-
let maybe_para_id = get_para_id::<B>(&mut self.ext.lock().unwrap());
74+
let mut ext_guard = self.ext_mutex.lock().await;
75+
let ext = ext_guard.deref_mut();
76+
let maybe_last_relay_chain_block_number = get_last_relay_chain_block_number::<B>(ext);
77+
let maybe_para_id = get_para_id::<B>(ext);
7678
let (last_relay_chain_block_number, para_id) =
7779
match (maybe_last_relay_chain_block_number, maybe_para_id) {
7880
(Some(last_relay_chain_block_number), Some(para_id)) => {

core/src/common/empty_block/inherents/providers.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717

1818
//! Contains providers for inherents required for empty block production.
1919
20-
use std::{
21-
sync::{Arc, Mutex},
22-
time::Duration,
23-
};
20+
use std::{sync::Arc, time::Duration};
2421

25-
use clap::ValueEnum;
2622
use parity_scale_codec::Encode;
2723
use sp_consensus_aura::{Slot, SlotDuration, AURA_ENGINE_ID};
2824
use sp_consensus_babe::{
@@ -37,6 +33,7 @@ use sp_runtime::{
3733
use sp_state_machine::TestExternalities;
3834
use sp_std::prelude::*;
3935
use strum_macros::{Display, EnumIter};
36+
use tokio::sync::Mutex;
4037

4138
use crate::common::empty_block::inherents::custom_idps;
4239

@@ -60,12 +57,13 @@ type InherentProviderResult<Err> =
6057
///
6158
/// Currently only Smart is implemented. New implementations may be added if Smart is not suitable
6259
/// for some edge cases.
63-
#[derive(Debug, Clone, clap::Parser, EnumIter, Display, Copy, ValueEnum)]
64-
#[clap(rename_all = "snake_case")]
60+
#[derive(Debug, Clone, EnumIter, Display, Copy)]
6561
pub enum ProviderVariant {
6662
/// Smart chain varient will automatically adjust provided inherents based on the given
6763
/// externalities.
68-
Smart,
64+
///
65+
/// The blocktime is provided in milliseconds.
66+
Smart(core::time::Duration),
6967
}
7068

7169
impl<B: BlockT> InherentProvider<B> for ProviderVariant {
@@ -78,9 +76,9 @@ impl<B: BlockT> InherentProvider<B> for ProviderVariant {
7876
ext: Arc<Mutex<TestExternalities<HashingFor<B>>>>,
7977
) -> InherentProviderResult<Self::Err> {
8078
match *self {
81-
ProviderVariant::Smart => {
82-
<SmartInherentProvider as InherentProvider<B>>::get_inherent_providers_and_pre_digest(&SmartInherentProvider {
83-
blocktime: Duration::from_secs(6),
79+
ProviderVariant::Smart(blocktime) => {
80+
<SmartInherentProvider as InherentProvider<B>>::get_inherent_providers_and_pre_digest(&SmartInherentProvider {
81+
blocktime,
8482
}, maybe_parent_info, parent_header, ext)
8583
}
8684
}
@@ -108,24 +106,22 @@ impl<B: BlockT> InherentProvider<B> for SmartInherentProvider {
108106
parent_header: B::Header,
109107
ext: Arc<Mutex<TestExternalities<HashingFor<B>>>>,
110108
) -> InherentProviderResult<Self::Err> {
111-
let blocktime_millis = self.blocktime.as_millis() as u64;
112-
113109
let timestamp_idp = custom_idps::timestamp::InherentDataProvider {
114-
blocktime_millis,
110+
blocktime_millis: self.blocktime.as_millis() as u64,
115111
maybe_parent_info,
116112
};
117113
let para_parachain_idp = custom_idps::para_parachain::InherentDataProvider::<B> {
118-
blocktime_millis,
114+
blocktime_millis: self.blocktime.as_millis() as u64,
119115
parent_header: parent_header.clone(),
120116
timestamp: timestamp_idp.timestamp(),
121-
ext,
117+
ext_mutex: ext,
122118
};
123119
let relay_parachain_data_idp =
124120
custom_idps::relay_parachains::InherentDataProvider::<B>::new(parent_header);
125121

126122
let slot = Slot::from_timestamp(
127123
timestamp_idp.timestamp(),
128-
SlotDuration::from_millis(blocktime_millis),
124+
SlotDuration::from_millis(self.blocktime.as_millis() as u64),
129125
);
130126
let digest = vec![
131127
DigestItem::PreRuntime(

core/src/common/empty_block/production.rs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use std::{
2-
ops::DerefMut,
3-
str::FromStr,
4-
sync::{Arc, Mutex},
5-
};
1+
use std::{ops::DerefMut, str::FromStr, sync::Arc};
62

73
use parity_scale_codec::{Decode, Encode};
84
use sc_cli::Result;
@@ -15,6 +11,7 @@ use sp_runtime::{
1511
};
1612
use sp_state_machine::TestExternalities;
1713
use sp_std::fmt::Debug;
14+
use tokio::sync::Mutex;
1815

1916
use super::inherents::{pre_apply::pre_apply_inherents, providers::InherentProvider};
2017
use crate::{
@@ -23,7 +20,7 @@ use crate::{
2320
};
2421

2522
pub async fn mine_block<Block: BlockT, HostFns: HostFunctions>(
26-
ext: Arc<Mutex<TestExternalities<HashingFor<Block>>>>,
23+
ext_mutex: Arc<Mutex<TestExternalities<HashingFor<Block>>>>,
2724
executor: &WasmExecutor<HostFns>,
2825
previous_block_building_info: Option<(InherentData, Digest)>,
2926
parent_header: Block::Header,
@@ -38,15 +35,18 @@ where
3835
<NumberFor<Block> as FromStr>::Err: Debug,
3936
{
4037
// We are saving state before we overwrite it while producing new block.
41-
let backend = ext.lock().unwrap().deref_mut().as_backend();
38+
let mut ext_guard = ext_mutex.lock().await;
39+
let ext = ext_guard.deref_mut();
40+
let backend = ext.as_backend();
41+
drop(ext_guard);
4242

4343
log::info!(
4444
"Producing new empty block at height {:?}",
4545
*parent_header.number() + One::one()
4646
);
4747

4848
let (next_block, new_block_building_info) = produce_next_block::<Block, HostFns>(
49-
ext.clone(),
49+
ext_mutex.clone(),
5050
executor,
5151
parent_header.clone(),
5252
provider_variant,
@@ -59,10 +59,13 @@ where
5959
array_bytes::bytes2hex("0x", next_block.header().hash())
6060
);
6161

62+
let mut ext_guard = ext_mutex.lock().await;
63+
let ext = ext_guard.deref_mut();
64+
6265
// And now we restore previous state.
63-
ext.lock().unwrap().deref_mut().backend = backend;
66+
ext.backend = backend;
6467

65-
pre_apply_inherents::<Block>(ext.lock().unwrap().deref_mut());
68+
pre_apply_inherents::<Block>(ext);
6669
let state_root_check = true;
6770
let signature_check = true;
6871
let payload = (
@@ -72,13 +75,7 @@ where
7275
try_state,
7376
)
7477
.encode();
75-
call::<Block, _>(
76-
ext.lock().unwrap().deref_mut(),
77-
executor,
78-
"TryRuntime_execute_block",
79-
&payload,
80-
)
81-
.await?;
78+
call::<Block, _>(ext, executor, "TryRuntime_execute_block", &payload).await?;
8279

8380
log::info!("Executed the new block");
8481

@@ -87,7 +84,7 @@ where
8784

8885
/// Produces next block containing only inherents.
8986
pub async fn produce_next_block<Block: BlockT, HostFns: HostFunctions>(
90-
externalities: Arc<Mutex<TestExternalities<HashingFor<Block>>>>,
87+
ext_mutex: Arc<Mutex<TestExternalities<HashingFor<Block>>>>,
9188
executor: &WasmExecutor<HostFns>,
9289
parent_header: Block::Header,
9390
chain: ProviderVariant,
@@ -105,10 +102,14 @@ where
105102
&chain,
106103
previous_block_building_info,
107104
parent_header.clone(),
108-
externalities.clone(),
105+
ext_mutex.clone(),
109106
)?;
110107

111-
pre_apply_inherents::<Block>(externalities.clone().lock().unwrap().deref_mut());
108+
let mut ext_guard = ext_mutex.lock().await;
109+
let ext = ext_guard.deref_mut();
110+
111+
pre_apply_inherents::<Block>(ext);
112+
drop(ext_guard);
112113
let inherent_data = inherent_data_provider
113114
.create_inherent_data()
114115
.await
@@ -123,45 +124,31 @@ where
123124
digest.clone(),
124125
);
125126

126-
call::<Block, _>(
127-
externalities.lock().unwrap().deref_mut(),
128-
executor,
129-
"Core_initialize_block",
130-
&header.encode(),
131-
)
132-
.await?;
127+
let mut ext_guard = ext_mutex.lock().await;
128+
let ext = ext_guard.deref_mut();
129+
call::<Block, _>(ext, executor, "Core_initialize_block", &header.encode()).await?;
133130

134131
let extrinsics = dry_call::<Vec<Block::Extrinsic>, Block, _>(
135-
externalities.lock().unwrap().deref_mut(),
132+
ext,
136133
executor,
137134
"BlockBuilder_inherent_extrinsics",
138135
&inherent_data.encode(),
139136
)?;
140137

141138
for xt in &extrinsics {
142-
call::<Block, _>(
143-
externalities.lock().unwrap().deref_mut(),
144-
executor,
145-
"BlockBuilder_apply_extrinsic",
146-
&xt.encode(),
147-
)
148-
.await?;
139+
call::<Block, _>(ext, executor, "BlockBuilder_apply_extrinsic", &xt.encode()).await?;
149140
}
150141

151142
let header = dry_call::<Block::Header, Block, _>(
152-
externalities.lock().unwrap().deref_mut(),
143+
ext,
153144
executor,
154145
"BlockBuilder_finalize_block",
155146
&[0u8; 0],
156147
)?;
157148

158-
call::<Block, _>(
159-
externalities.lock().unwrap().deref_mut(),
160-
executor,
161-
"BlockBuilder_finalize_block",
162-
&[0u8; 0],
163-
)
164-
.await?;
149+
call::<Block, _>(ext, executor, "BlockBuilder_finalize_block", &[0u8; 0]).await?;
150+
151+
drop(ext_guard);
165152

166153
Ok((Block::new(header, extrinsics), (inherent_data, digest)))
167154
}

0 commit comments

Comments
 (0)