Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async backing #1492

Merged
merged 9 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1,690 changes: 953 additions & 737 deletions Cargo.lock

Large diffs are not rendered by default.

171 changes: 139 additions & 32 deletions Cargo.toml

Large diffs are not rendered by default.

15 changes: 4 additions & 11 deletions core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,23 @@ pub type Header = sp_runtime::generic::Header<BlockNumber, Hashing>;
/// Block type.
pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;

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

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

// Time is measured by number of blocks.
/// 10 blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const MINUTES: BlockNumber = 60_000 / (SLOT_DURATION as BlockNumber);
/// 600 blocks.
pub const HOURS: BlockNumber = MINUTES * 60;
/// 14,400 blocks.
Expand Down
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cumulus-client-consensus-aura = { workspace = true }
cumulus-client-consensus-common = { workspace = true }
cumulus-client-consensus-proposer = { workspace = true }
cumulus-client-service = { workspace = true }
cumulus-primitives-aura = { workspace = true, features = ["std"] }
cumulus-primitives-core = { workspace = true, features = ["std"] }
cumulus-primitives-parachain-inherent = { workspace = true, features = ["std"] }
cumulus-relay-chain-interface = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ pub fn run() -> Result<()> {
if chain_spec.is_crab() {
return service::start_dev_node::<CrabRuntimeApi, CrabRuntimeExecutor>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into);
Expand All @@ -515,6 +516,7 @@ pub fn run() -> Result<()> {
if chain_spec.is_darwinia() {
return service::start_dev_node::<DarwiniaRuntimeApi, DarwiniaRuntimeExecutor>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into)
Expand All @@ -524,6 +526,7 @@ pub fn run() -> Result<()> {
if chain_spec.is_pangolin() {
return service::start_dev_node::<PangolinRuntimeApi, PangolinRuntimeExecutor>(
config,
id,
&eth_rpc_config,
)
.map_err(Into::into)
Expand Down
77 changes: 48 additions & 29 deletions node/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ use futures::FutureExt;
// darwinia
use dc_primitives::*;
// substrate
use sc_client_api::Backend;
use sc_client_api::{Backend, HeaderBackend};
use sc_consensus::ImportQueue;
use sc_network::NetworkBlock;
use sp_core::Encode;

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

/// A set of APIs that darwinia-like runtimes must implement.
pub trait RuntimeApiCollection:
cumulus_primitives_core::CollectCollationInfo<Block>
cumulus_primitives_aura::AuraUnincludedSegmentApi<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>
Expand All @@ -110,7 +112,8 @@ pub trait RuntimeApiCollection:
{
}
impl<Api> RuntimeApiCollection for Api where
Api: cumulus_primitives_core::CollectCollationInfo<Block>
Api: cumulus_primitives_aura::AuraUnincludedSegmentApi<Block>
+ cumulus_primitives_core::CollectCollationInfo<Block>
+ fp_rpc::ConvertTransactionRuntimeApi<Block>
+ fp_rpc::EthereumRuntimeRPCApi<Block>
+ moonbeam_rpc_primitives_debug::DebugRuntimeApi<Block>
Expand Down Expand Up @@ -261,6 +264,7 @@ where
Executor: 'static + sc_executor::NativeExecutionDispatch,
SC: FnOnce(
Arc<FullClient<RuntimeApi, Executor>>,
Arc<FullBackend>,
ParachainBlockImport<RuntimeApi, Executor>,
Option<&substrate_prometheus_endpoint::Registry>,
Option<sc_telemetry::TelemetryHandle>,
Expand Down Expand Up @@ -296,7 +300,6 @@ where
telemetry_worker_handle,
),
} = new_partial::<RuntimeApi, Executor>(&parachain_config, eth_rpc_config)?;

let (relay_chain_interface, collator_key) =
cumulus_client_service::build_relay_chain_interface(
polkadot_config,
Expand All @@ -308,12 +311,10 @@ where
)
.await
.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;

let validator = parachain_config.role.is_authority();
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let import_queue_service = import_queue.service();
let net_config = sc_network::config::FullNetworkConfiguration::new(&parachain_config.network);

let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
cumulus_client_service::build_network(cumulus_client_service::BuildNetworkParams {
parachain_config: &parachain_config,
Expand Down Expand Up @@ -521,6 +522,7 @@ where
if validator {
start_consensus(
client.clone(),
backend.clone(),
block_import,
prometheus_registry.as_ref(),
telemetry.as_ref().map(|t| t.handle()),
Expand Down Expand Up @@ -614,6 +616,7 @@ where
cumulus_client_service::CollatorSybilResistance::Resistant, // Aura
para_id,
|client,
backend,
block_import,
prometheus_registry,
telemetry,
Expand Down Expand Up @@ -642,11 +645,17 @@ where
announce_block,
client.clone(),
);
let params = cumulus_client_consensus_aura::collators::basic::Params {
let params = cumulus_client_consensus_aura::collators::lookahead::Params {
create_inherent_data_providers: move |_, ()| async move { Ok(()) },
block_import,
para_client: client,
para_client: client.clone(),
para_backend: backend.clone(),
relay_client: relay_chain_interface,
code_hash_provider: move |block_hash| {
client.code_at(block_hash).ok().map(|c| {
cumulus_primitives_core::relay_chain::ValidationCode::from(c).hash()
})
},
sync_oracle,
keystore,
collator_key,
Expand All @@ -657,9 +666,9 @@ where
proposer,
collator_service,
// Very limited proposal time.
authoring_duration: Duration::from_millis(500),
authoring_duration: Duration::from_millis(1_500),
};
let fut = cumulus_client_consensus_aura::collators::basic::run::<
let fut = cumulus_client_consensus_aura::collators::lookahead::run::<
Block,
sp_consensus_aura::sr25519::AuthorityPair,
_,
Expand All @@ -669,6 +678,8 @@ where
_,
_,
_,
_,
_,
>(params);

task_manager.spawn_essential_handle().spawn("aura", None, fut);
Expand All @@ -685,21 +696,19 @@ where
/// !!! WARNING: DO NOT USE ELSEWHERE
pub fn start_dev_node<RuntimeApi, Executor>(
mut config: sc_service::Configuration,
para_id: cumulus_primitives_core::ParaId,
eth_rpc_config: &crate::cli::EthRpcConfig,
) -> Result<sc_service::TaskManager, sc_service::error::Error>
where
RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>
RuntimeApi: 'static
+ Send
+ Sync
+ 'static,
+ sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>>,
RuntimeApi::RuntimeApi: RuntimeApiCollection,
RuntimeApi::RuntimeApi:
sp_consensus_aura::AuraApi<Block, sp_consensus_aura::sr25519::AuthorityId>,
Executor: 'static + sc_executor::NativeExecutionDispatch,
{
// substrate
use sc_client_api::HeaderBackend;

let sc_service::PartialComponents {
client,
backend,
Expand All @@ -720,7 +729,6 @@ where
),
} = new_partial::<RuntimeApi, Executor>(&config, eth_rpc_config)?;
let net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);

let (network, system_rpc_tx, tx_handler_controller, start_network, sync_service) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
Expand Down Expand Up @@ -757,17 +765,17 @@ where
}

let force_authoring = config.force_authoring;
let backoff_authoring_blocks: Option<()> = None;
let backoff_authoring_blocks = <Option<()>>::None;
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
let proposer_factory = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
transaction_pool.clone(),
None,
None,
);

let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
let client_for_cidp = client.clone();

if config.role.is_authority() {
let aura = sc_consensus_aura::start_aura::<
sp_consensus_aura::sr25519::AuthorityPair,
Expand All @@ -782,41 +790,54 @@ where
_,
_,
>(sc_consensus_aura::StartAuraParams {
slot_duration: sc_consensus_aura::slot_duration(&*client)?,
slot_duration,
client: client.clone(),
select_chain,
block_import: instant_finalize::InstantFinalizeBlockImport::new(client.clone()),
proposer_factory,
create_inherent_data_providers: move |block: Hash, ()| {
let current_para_block = client_for_cidp
.number(block)
.expect("Header lookup should succeed")
.expect("Header passed in as parent should be present in backend.");
let maybe_current_para_block = client_for_cidp.number(block);
let maybe_current_block_head = client_for_cidp.expect_header(block);
let client_for_xcm = client_for_cidp.clone();
// TODO: hack for now.
let additional_key_values = Some(vec![(
array_bytes::hex2bytes_unchecked(
"1cb6f36e027abb2091cfb5110ab5087f06155b3cd9a8c9e5e9a23fd5dc13a5ed",
),
cumulus_primitives_aura::Slot::from_timestamp(
sp_timestamp::Timestamp::current(),
slot_duration,
)
.encode(),
)]);

async move {
let current_para_block = maybe_current_para_block?
.ok_or(sp_blockchain::Error::UnknownBlock(block.to_string()))?;
let current_para_block_head =
Some(polkadot_primitives::HeadData(maybe_current_block_head?.encode()));
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

let slot = sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*timestamp,
slot_duration,
);

let mocked_parachain =
cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {
current_para_block,
current_para_block_head,
relay_offset: 1000,
relay_blocks_per_para_block: 2,
para_blocks_per_relay_epoch: 0,
relay_randomness_config: (),
xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(
&*client_for_xcm,
block,
Default::default(),
para_id,
Default::default(),
),
raw_downward_messages: Vec::new(),
raw_horizontal_messages: Vec::new(),
additional_key_values,
};

Ok((slot, timestamp, mocked_parachain))
Expand Down Expand Up @@ -887,8 +908,6 @@ where
let collator = config.role.is_authority();
let eth_rpc_config = eth_rpc_config.clone();
let sync_service = sync_service.clone();

let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
let pending_create_inherent_data_providers = move |_, ()| async move {
let current = sp_timestamp::InherentDataProvider::from_system_time();
let next_slot = current.timestamp().as_millis() + slot_duration.as_millis();
Expand Down
4 changes: 2 additions & 2 deletions pallet/staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `574`
// Estimated: `4543`
// Minimum execution time: 12_000 nanoseconds.
// Minimum execution time: 6_000 nanoseconds.
Weight::from_parts(12_000_000, 0)
.saturating_add(Weight::from_parts(4543, 0))
.saturating_add(T::DbWeight::get().reads(3_u64))
Expand Down Expand Up @@ -265,7 +265,7 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `574`
// Estimated: `4543`
// Minimum execution time: 12_000 nanoseconds.
// Minimum execution time: 6_000 nanoseconds.
Weight::from_parts(12_000_000, 0)
.saturating_add(Weight::from_parts(4543, 0))
.saturating_add(RocksDbWeight::get().reads(3_u64))
Expand Down
4 changes: 2 additions & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ impl WeightToFeePolynomial for RefTimeToFee {
type Balance = Balance;

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

smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ macro_rules! impl_evm_tests {
#[test]
fn evm_constants_are_correctly() {
assert_eq!(BlockGasLimit::get(), U256::from(20_000_000));
assert_eq!(WeightPerGas::get().ref_time(), 18750);
assert_eq!(WeightPerGas::get().ref_time(), 75000);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions runtime/crab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cumulus-pallet-dmp-queue = { workspace = true }
cumulus-pallet-parachain-system = { workspace = true }
cumulus-pallet-xcm = { workspace = true }
cumulus-pallet-xcmp-queue = { workspace = true }
cumulus-primitives-aura = { workspace = true }
cumulus-primitives-core = { workspace = true }
cumulus-primitives-utility = { workspace = true }
parachain-info = { workspace = true }
Expand Down Expand Up @@ -141,6 +142,7 @@ std = [
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-xcm/std",
"cumulus-pallet-xcmp-queue/std",
"cumulus-primitives-aura/std",
"cumulus-primitives-core/std",
"cumulus-primitives-utility/std",
"parachain-info/std",
Expand Down
9 changes: 9 additions & 0 deletions runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ sp_api::impl_runtime_apis! {
}
}

impl cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> for Runtime {
fn can_build_upon(
included_hash: <Block as sp_runtime::traits::Block>::Hash,
slot: cumulus_primitives_aura::Slot,
) -> bool {
ConsensusHook::can_build_upon(included_hash, slot)
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
fn collect_collation_info(header: &<Block as sp_runtime::traits::Block>::Header) -> cumulus_primitives_core::CollationInfo {
ParachainSystem::collect_collation_info(header)
Expand Down
3 changes: 1 addition & 2 deletions runtime/crab/src/pallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ mod session;
pub use session::*;

mod aura;

mod aura_ext;
pub use aura::*;

// Governance stuff.
mod governance;
Expand Down
Loading