Skip to content

Commit 48f10ed

Browse files
Async backing (#1492)
* Async backing * Move to aura * Fix tests * Code clean * Hack * Try * Apply hack * Fix compile * Fix broken ts test --------- Co-authored-by: bear <[email protected]>
1 parent 1eb277e commit 48f10ed

37 files changed

+1244
-902
lines changed

Cargo.lock

Lines changed: 953 additions & 737 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 139 additions & 32 deletions
Large diffs are not rendered by default.

core/primitives/src/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,23 @@ pub type Header = sp_runtime::generic::Header<BlockNumber, Hashing>;
6060
/// Block type.
6161
pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
6262

63-
/// This determines the average expected block time that we are targeting.
64-
/// Blocks will be produced at a minimum duration defined by `SLOT_DURATION`.
65-
/// `SLOT_DURATION` is picked up by `pallet_timestamp` which is in turn picked
66-
/// up by `pallet_aura` to implement `fn slot_duration()`.
67-
///
68-
/// Change this to adjust the block time.
69-
pub const MILLISECS_PER_BLOCK: u64 = 12_000;
7063
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
7164
/// into the relay chain.
72-
pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
65+
pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3;
7366
/// How many parachain blocks are processed by the relay chain per parent. Limits the
7467
/// number of blocks authored per slot.
7568
pub const BLOCK_PROCESSING_VELOCITY: u32 = 1;
7669
/// Relay chain slot duration, in milliseconds.
77-
pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6000;
70+
pub const RELAY_CHAIN_SLOT_DURATION_MILLIS: u32 = 6_000;
7871

7972
// NOTE: Currently it is not possible to change the slot duration after the chain has started.
8073
// Attempting to do so will brick block production.
8174
/// Slot duration.
82-
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
75+
pub const SLOT_DURATION: u64 = 6_000;
8376

8477
// Time is measured by number of blocks.
8578
/// 10 blocks.
86-
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
79+
pub const MINUTES: BlockNumber = 60_000 / (SLOT_DURATION as BlockNumber);
8780
/// 600 blocks.
8881
pub const HOURS: BlockNumber = MINUTES * 60;
8982
/// 14,400 blocks.

node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cumulus-client-consensus-aura = { workspace = true }
2828
cumulus-client-consensus-common = { workspace = true }
2929
cumulus-client-consensus-proposer = { workspace = true }
3030
cumulus-client-service = { workspace = true }
31+
cumulus-primitives-aura = { workspace = true, features = ["std"] }
3132
cumulus-primitives-core = { workspace = true, features = ["std"] }
3233
cumulus-primitives-parachain-inherent = { workspace = true, features = ["std"] }
3334
cumulus-relay-chain-interface = { workspace = true }

node/src/command.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ pub fn run() -> Result<()> {
506506
if chain_spec.is_crab() {
507507
return service::start_dev_node::<CrabRuntimeApi, CrabRuntimeExecutor>(
508508
config,
509+
id,
509510
&eth_rpc_config,
510511
)
511512
.map_err(Into::into);
@@ -515,6 +516,7 @@ pub fn run() -> Result<()> {
515516
if chain_spec.is_darwinia() {
516517
return service::start_dev_node::<DarwiniaRuntimeApi, DarwiniaRuntimeExecutor>(
517518
config,
519+
id,
518520
&eth_rpc_config,
519521
)
520522
.map_err(Into::into)
@@ -524,6 +526,7 @@ pub fn run() -> Result<()> {
524526
if chain_spec.is_pangolin() {
525527
return service::start_dev_node::<PangolinRuntimeApi, PangolinRuntimeExecutor>(
526528
config,
529+
id,
527530
&eth_rpc_config,
528531
)
529532
.map_err(Into::into)

node/src/service/mod.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ use futures::FutureExt;
4343
// darwinia
4444
use dc_primitives::*;
4545
// substrate
46-
use sc_client_api::Backend;
46+
use sc_client_api::{Backend, HeaderBackend};
4747
use sc_consensus::ImportQueue;
4848
use sc_network::NetworkBlock;
49+
use sp_core::Encode;
4950

5051
/// Full client backend type.
5152
type FullBackend = sc_service::TFullBackend<Block>;
@@ -94,7 +95,8 @@ impl IdentifyVariant for Box<dyn sc_service::ChainSpec> {
9495

9596
/// A set of APIs that darwinia-like runtimes must implement.
9697
pub trait RuntimeApiCollection:
97-
cumulus_primitives_core::CollectCollationInfo<Block>
98+
cumulus_primitives_aura::AuraUnincludedSegmentApi<Block>
99+
+ cumulus_primitives_core::CollectCollationInfo<Block>
98100
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
99101
+ fp_rpc::EthereumRuntimeRPCApi<Block>
100102
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>
@@ -110,7 +112,8 @@ pub trait RuntimeApiCollection:
110112
{
111113
}
112114
impl<Api> RuntimeApiCollection for Api where
113-
Api: cumulus_primitives_core::CollectCollationInfo<Block>
115+
Api: cumulus_primitives_aura::AuraUnincludedSegmentApi<Block>
116+
+ cumulus_primitives_core::CollectCollationInfo<Block>
114117
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
115118
+ fp_rpc::EthereumRuntimeRPCApi<Block>
116119
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>
@@ -261,6 +264,7 @@ where
261264
Executor: 'static + sc_executor::NativeExecutionDispatch,
262265
SC: FnOnce(
263266
Arc<FullClient<RuntimeApi, Executor>>,
267+
Arc<FullBackend>,
264268
ParachainBlockImport<RuntimeApi, Executor>,
265269
Option<&substrate_prometheus_endpoint::Registry>,
266270
Option<sc_telemetry::TelemetryHandle>,
@@ -296,7 +300,6 @@ where
296300
telemetry_worker_handle,
297301
),
298302
} = new_partial::<RuntimeApi, Executor>(&parachain_config, eth_rpc_config)?;
299-
300303
let (relay_chain_interface, collator_key) =
301304
cumulus_client_service::build_relay_chain_interface(
302305
polkadot_config,
@@ -308,12 +311,10 @@ where
308311
)
309312
.await
310313
.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;
311-
312314
let validator = parachain_config.role.is_authority();
313315
let prometheus_registry = parachain_config.prometheus_registry().cloned();
314316
let import_queue_service = import_queue.service();
315317
let net_config = sc_network::config::FullNetworkConfiguration::new(&parachain_config.network);
316-
317318
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
318319
cumulus_client_service::build_network(cumulus_client_service::BuildNetworkParams {
319320
parachain_config: &parachain_config,
@@ -521,6 +522,7 @@ where
521522
if validator {
522523
start_consensus(
523524
client.clone(),
525+
backend.clone(),
524526
block_import,
525527
prometheus_registry.as_ref(),
526528
telemetry.as_ref().map(|t| t.handle()),
@@ -614,6 +616,7 @@ where
614616
cumulus_client_service::CollatorSybilResistance::Resistant, // Aura
615617
para_id,
616618
|client,
619+
backend,
617620
block_import,
618621
prometheus_registry,
619622
telemetry,
@@ -642,11 +645,17 @@ where
642645
announce_block,
643646
client.clone(),
644647
);
645-
let params = cumulus_client_consensus_aura::collators::basic::Params {
648+
let params = cumulus_client_consensus_aura::collators::lookahead::Params {
646649
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
647650
block_import,
648-
para_client: client,
651+
para_client: client.clone(),
652+
para_backend: backend.clone(),
649653
relay_client: relay_chain_interface,
654+
code_hash_provider: move |block_hash| {
655+
client.code_at(block_hash).ok().map(|c| {
656+
cumulus_primitives_core::relay_chain::ValidationCode::from(c).hash()
657+
})
658+
},
650659
sync_oracle,
651660
keystore,
652661
collator_key,
@@ -657,9 +666,9 @@ where
657666
proposer,
658667
collator_service,
659668
// Very limited proposal time.
660-
authoring_duration: Duration::from_millis(500),
669+
authoring_duration: Duration::from_millis(1_500),
661670
};
662-
let fut = cumulus_client_consensus_aura::collators::basic::run::<
671+
let fut = cumulus_client_consensus_aura::collators::lookahead::run::<
663672
Block,
664673
sp_consensus_aura::sr25519::AuthorityPair,
665674
_,
@@ -669,6 +678,8 @@ where
669678
_,
670679
_,
671680
_,
681+
_,
682+
_,
672683
>(params);
673684

674685
task_manager.spawn_essential_handle().spawn("aura", None, fut);
@@ -685,21 +696,19 @@ where
685696
/// !!! WARNING: DO NOT USE ELSEWHERE
686697
pub fn start_dev_node<RuntimeApi, Executor>(
687698
mut config: sc_service::Configuration,
699+
para_id: cumulus_primitives_core::ParaId,
688700
eth_rpc_config: &crate::cli::EthRpcConfig,
689701
) -> Result<sc_service::TaskManager, sc_service::error::Error>
690702
where
691-
RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>
703+
RuntimeApi: 'static
692704
+ Send
693705
+ Sync
694-
+ 'static,
706+
+ sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>,
695707
RuntimeApi::RuntimeApi: RuntimeApiCollection,
696708
RuntimeApi::RuntimeApi:
697709
sp_consensus_aura::AuraApi<Block, sp_consensus_aura::sr25519::AuthorityId>,
698710
Executor: 'static + sc_executor::NativeExecutionDispatch,
699711
{
700-
// substrate
701-
use sc_client_api::HeaderBackend;
702-
703712
let sc_service::PartialComponents {
704713
client,
705714
backend,
@@ -720,7 +729,6 @@ where
720729
),
721730
} = new_partial::<RuntimeApi, Executor>(&config, eth_rpc_config)?;
722731
let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
723-
724732
let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
725733
sc_service::build_network(sc_service::BuildNetworkParams {
726734
config: &config,
@@ -757,17 +765,17 @@ where
757765
}
758766

759767
let force_authoring = config.force_authoring;
760-
let backoff_authoring_blocks: Option<()> = None;
768+
let backoff_authoring_blocks = <Option<()>>::None;
769+
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
761770
let proposer_factory = sc_basic_authorship::ProposerFactory::new(
762771
task_manager.spawn_handle(),
763772
client.clone(),
764773
transaction_pool.clone(),
765774
None,
766775
None,
767776
);
768-
769-
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
770777
let client_for_cidp = client.clone();
778+
771779
if config.role.is_authority() {
772780
let aura = sc_consensus_aura::start_aura::<
773781
sp_consensus_aura::sr25519::AuthorityPair,
@@ -782,41 +790,54 @@ where
782790
_,
783791
_,
784792
>(sc_consensus_aura::StartAuraParams {
785-
slot_duration: sc_consensus_aura::slot_duration(&*client)?,
793+
slot_duration,
786794
client: client.clone(),
787795
select_chain,
788796
block_import: instant_finalize::InstantFinalizeBlockImport::new(client.clone()),
789797
proposer_factory,
790798
create_inherent_data_providers: move |block: Hash, ()| {
791-
let current_para_block = client_for_cidp
792-
.number(block)
793-
.expect("Header lookup should succeed")
794-
.expect("Header passed in as parent should be present in backend.");
799+
let maybe_current_para_block = client_for_cidp.number(block);
800+
let maybe_current_block_head = client_for_cidp.expect_header(block);
795801
let client_for_xcm = client_for_cidp.clone();
802+
// TODO: hack for now.
803+
let additional_key_values = Some(vec![(
804+
array_bytes::hex2bytes_unchecked(
805+
"1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed",
806+
),
807+
cumulus_primitives_aura::Slot::from_timestamp(
808+
sp_timestamp::Timestamp::current(),
809+
slot_duration,
810+
)
811+
.encode(),
812+
)]);
796813

797814
async move {
815+
let current_para_block = maybe_current_para_block?
816+
.ok_or(sp_blockchain::Error::UnknownBlock(block.to_string()))?;
817+
let current_para_block_head =
818+
Some(polkadot_primitives::HeadData(maybe_current_block_head?.encode()));
798819
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();
799-
800820
let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
801821
*timestamp,
802822
slot_duration,
803823
);
804-
805824
let mocked_parachain =
806825
cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {
807826
current_para_block,
827+
current_para_block_head,
808828
relay_offset: 1000,
809829
relay_blocks_per_para_block: 2,
810830
para_blocks_per_relay_epoch: 0,
811831
relay_randomness_config: (),
812832
xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(
813833
&*client_for_xcm,
814834
block,
815-
Default::default(),
835+
para_id,
816836
Default::default(),
817837
),
818838
raw_downward_messages: Vec::new(),
819839
raw_horizontal_messages: Vec::new(),
840+
additional_key_values,
820841
};
821842

822843
Ok((slot, timestamp, mocked_parachain))
@@ -887,8 +908,6 @@ where
887908
let collator = config.role.is_authority();
888909
let eth_rpc_config = eth_rpc_config.clone();
889910
let sync_service = sync_service.clone();
890-
891-
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
892911
let pending_create_inherent_data_providers = move |_, ()| async move {
893912
let current = sp_timestamp::InherentDataProvider::from_system_time();
894913
let next_slot = current.timestamp().as_millis() + slot_duration.as_millis();

pallet/staking/src/weights.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
134134
// Proof Size summary in bytes:
135135
// Measured: `574`
136136
// Estimated: `4543`
137-
// Minimum execution time: 12_000 nanoseconds.
137+
// Minimum execution time: 6_000 nanoseconds.
138138
Weight::from_parts(12_000_000, 0)
139139
.saturating_add(Weight::from_parts(4543, 0))
140140
.saturating_add(T::DbWeight::get().reads(3_u64))
@@ -265,7 +265,7 @@ impl WeightInfo for () {
265265
// Proof Size summary in bytes:
266266
// Measured: `574`
267267
// Estimated: `4543`
268-
// Minimum execution time: 12_000 nanoseconds.
268+
// Minimum execution time: 6_000 nanoseconds.
269269
Weight::from_parts(12_000_000, 0)
270270
.saturating_add(Weight::from_parts(4543, 0))
271271
.saturating_add(RocksDbWeight::get().reads(3_u64))

runtime/common/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ impl WeightToFeePolynomial for RefTimeToFee {
174174
type Balance = Balance;
175175

176176
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
177-
// Map base extrinsic weight to 1/200 UNIT.
177+
// Map base extrinsic weight to 1/800 UNIT.
178178
let p = UNIT;
179-
let q = 200 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
179+
let q = 800 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
180180

181181
smallvec::smallvec![WeightToFeeCoefficient {
182182
degree: 1,

runtime/common/src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ macro_rules! impl_evm_tests {
421421
#[test]
422422
fn evm_constants_are_correctly() {
423423
assert_eq!(BlockGasLimit::get(), U256::from(20_000_000));
424-
assert_eq!(WeightPerGas::get().ref_time(), 18750);
424+
assert_eq!(WeightPerGas::get().ref_time(), 75000);
425425
}
426426

427427
#[test]

runtime/crab/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cumulus-pallet-dmp-queue = { workspace = true }
2727
cumulus-pallet-parachain-system = { workspace = true }
2828
cumulus-pallet-xcm = { workspace = true }
2929
cumulus-pallet-xcmp-queue = { workspace = true }
30+
cumulus-primitives-aura = { workspace = true }
3031
cumulus-primitives-core = { workspace = true }
3132
cumulus-primitives-utility = { workspace = true }
3233
parachain-info = { workspace = true }
@@ -141,6 +142,7 @@ std = [
141142
"cumulus-pallet-parachain-system/std",
142143
"cumulus-pallet-xcm/std",
143144
"cumulus-pallet-xcmp-queue/std",
145+
"cumulus-primitives-aura/std",
144146
"cumulus-primitives-core/std",
145147
"cumulus-primitives-utility/std",
146148
"parachain-info/std",

0 commit comments

Comments
 (0)