Skip to content

Commit

Permalink
Enable async backing (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Aug 9, 2024
1 parent 647d457 commit e619d26
Show file tree
Hide file tree
Showing 30 changed files with 70 additions and 206 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

/// 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 = 6_000;

/// Slot duration.
pub const SLOT_DURATION: u64 = 12_000;
pub const SLOT_DURATION: u64 = 6_000;

// Time is measured by number of blocks.
/// 10 blocks.
Expand Down
1 change: 1 addition & 0 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]
#![allow(clippy::needless_borrows_for_generic_args)]

pub mod migration;

Expand Down
2 changes: 0 additions & 2 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ std = [
"pallet-treasury?/std",
]

non-async = []

test = []

runtime-benchmarks = [
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 @@ -175,9 +175,9 @@ impl frame_support::weights::WeightToFeePolynomial for RefTimeToFee {
type Balance = Balance;

fn polynomial() -> frame_support::weights::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
let q = 800
* Balance::from(
frame_support::weights::constants::ExtrinsicBaseWeight::get().ref_time(),
);
Expand Down
4 changes: 2 additions & 2 deletions runtime/common/src/pallet_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const AVERAGE_ON_INITIALIZE_RATIO: sp_runtime::Perbill = sp_runtime::Perbill::fr
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used by
/// `Operational` extrinsics.
pub const NORMAL_DISPATCH_RATIO: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(75);
const WEIGHT_MILLISECS_PER_BLOCK: u64 = 500;
const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2_000;
pub const MAXIMUM_BLOCK_WEIGHT: frame_support::weights::Weight =
frame_support::weights::Weight::from_parts(
frame_support::weights::constants::WEIGHT_REF_TIME_PER_MILLIS * WEIGHT_MILLISECS_PER_BLOCK,
frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2),
cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64,
);

Expand Down
51 changes: 27 additions & 24 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ macro_rules! impl_account_migration_tests {

#[macro_export]
macro_rules! impl_evm_tests {
($non_async:expr) => {
() => {
mod evm {
// darwinia
use super::mock::*;
Expand All @@ -351,10 +351,7 @@ macro_rules! impl_evm_tests {
#[test]
fn evm_constants_are_correctly() {
assert_eq!(pallet_config::BlockGasLimit::get(), U256::from(20_000_000));
assert_eq!(
pallet_config::WeightPerGas::get().ref_time(),
if $non_async { 18750 } else { 75000 }
);
assert_eq!(pallet_config::WeightPerGas::get().ref_time(), 75000);
}

#[test]
Expand Down Expand Up @@ -620,7 +617,7 @@ macro_rules! impl_fee_tests {
assert_eq!(TransactionPayment::next_fee_multiplier(), Multiplier::from(1u128));
assert_eq!(
TransactionPaymentGasPrice::min_gas_price().0,
U256::from(753_532_560_644u128)
U256::from(753_532_560_644_u128)
);
})
}
Expand All @@ -640,70 +637,76 @@ macro_rules! impl_fee_tests {
TransactionPaymentGasPrice::min_gas_price().0
};

assert_eq!(sim(Perbill::from_percent(0), 1), U256::from(753_518_432_040u128));
assert_eq!(sim(Perbill::from_percent(25), 1), U256::from(753_518_432_040u128));
assert_eq!(sim(Perbill::from_percent(50), 1), U256::from(753_532_560_644u128));
assert_eq!(sim(Perbill::from_percent(100), 1), U256::from(753_574_948_042u128));
assert_eq!(sim(Perbill::from_percent(0), 1), U256::from(753_518_432_040_u128));
assert_eq!(sim(Perbill::from_percent(25), 1), U256::from(753_518_432_040_u128));
assert_eq!(sim(Perbill::from_percent(50), 1), U256::from(753_532_560_644_u128));
assert_eq!(
sim(Perbill::from_percent(100), 1),
U256::from(753_574_948_042_u128)
);

// 1 "real" hour (at 12-second blocks)
assert_eq!(sim(Perbill::from_percent(0), 300), U256::from(749_347_988_429u128));
assert_eq!(
sim(Perbill::from_percent(0), 300),
U256::from(749_347_988_429_u128)
);
assert_eq!(
sim(Perbill::from_percent(25), 300),
U256::from(749_347_988_429u128)
U256::from(749_347_988_429_u128)
);
assert_eq!(
sim(Perbill::from_percent(50), 300),
U256::from(753_574_948_042u128)
U256::from(753_574_948_042_u128)
);
assert_eq!(
sim(Perbill::from_percent(100), 300),
U256::from(766_399_427_500u128)
U256::from(766_399_427_500_u128)
);

// 1 "real" day (at 12-second blocks)
assert_eq!(
sim(Perbill::from_percent(0), 7200),
U256::from(669_615_374_520u128)
U256::from(669_615_374_520_u128)
);
assert_eq!(
sim(Perbill::from_percent(25), 7200),
U256::from(669_615_374_520u128)
U256::from(669_615_374_520_u128)
);
assert_eq!(
sim(Perbill::from_percent(50), 7200),
U256::from(766_399_427_500u128)
U256::from(766_399_427_500_u128)
);
assert_eq!(
sim(Perbill::from_percent(100), 7200),
U256::from(1149_064_577_447u128)
U256::from(1_149_064_577_447_u128)
);

// 7 "real" day (at 12-second blocks)
assert_eq!(
sim(Perbill::from_percent(0), 50400),
U256::from(446_617_926_925u128)
U256::from(446_617_926_925_u128)
);
assert_eq!(
sim(Perbill::from_percent(25), 50400),
U256::from(446_617_926_925u128)
U256::from(446_617_926_925_u128)
);
assert_eq!(
sim(Perbill::from_percent(50), 50400),
U256::from(1_149_064_577_447u128)
U256::from(1_149_064_577_447_u128)
);
assert_eq!(
sim(Perbill::from_percent(100), 50400),
U256::from(19_569_029_695_681u128)
U256::from(19_569_029_695_681_u128)
);

// 30 "real" day (at 12-second blocks)
assert_eq!(
sim(Perbill::from_percent(0), 259200),
U256::from(151_669_449_464u128)
U256::from(151_669_449_464_u128)
);
assert_eq!(
sim(Perbill::from_percent(25), 259200),
U256::from(151_669_449_464u128)
U256::from(151_669_449_464_u128)
);
});
}
Expand Down
6 changes: 1 addition & 5 deletions runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(
migration::CustomOnRuntimeUpgrade,
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
),
migration::CustomOnRuntimeUpgrade,
>;

/// Runtime version.
Expand Down
2 changes: 1 addition & 1 deletion runtime/crab/src/pallets/aura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
>;

impl pallet_aura::Config for Runtime {
type AllowMultipleBlocksPerSlot = ConstBool<false>;
type AllowMultipleBlocksPerSlot = ConstBool<true>;
type AuthorityId = sp_consensus_aura::sr25519::AuthorityId;
type DisabledValidators = ();
type MaxAuthorities = ConstU32<100_000>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/crab/src/pallets/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::*;

impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
type MinimumPeriod = ();
/// A timestamp: milliseconds since the unix epoch.
type Moment = u64;
#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down
2 changes: 1 addition & 1 deletion runtime/crab/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub mod mock;

darwinia_common_runtime::impl_weight_tests! {}
darwinia_common_runtime::impl_fee_tests! {}
darwinia_common_runtime::impl_evm_tests! { true }
darwinia_common_runtime::impl_evm_tests! {}
darwinia_common_runtime::impl_account_migration_tests! {}
darwinia_common_runtime::impl_maintenance_tests! {}
6 changes: 1 addition & 5 deletions runtime/darwinia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(
migration::CustomOnRuntimeUpgrade,
cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4<Runtime>,
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
),
migration::CustomOnRuntimeUpgrade,
>;

/// Runtime version.
Expand Down
2 changes: 1 addition & 1 deletion runtime/darwinia/src/pallets/aura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
>;

impl pallet_aura::Config for Runtime {
type AllowMultipleBlocksPerSlot = ConstBool<false>;
type AllowMultipleBlocksPerSlot = ConstBool<true>;
type AuthorityId = sp_consensus_aura::sr25519::AuthorityId;
type DisabledValidators = ();
type MaxAuthorities = ConstU32<100_000>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/darwinia/src/pallets/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::*;

impl pallet_timestamp::Config for Runtime {
type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
type MinimumPeriod = ();
/// A timestamp: milliseconds since the unix epoch.
type Moment = u64;
#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down
2 changes: 1 addition & 1 deletion runtime/darwinia/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub mod mock;

darwinia_common_runtime::impl_weight_tests! {}
darwinia_common_runtime::impl_fee_tests! {}
darwinia_common_runtime::impl_evm_tests! { true }
darwinia_common_runtime::impl_evm_tests! {}
darwinia_common_runtime::impl_account_migration_tests! {}
// darwinia_common_runtime::impl_maintenance_tests! {}
1 change: 0 additions & 1 deletion runtime/koi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ array-bytes = { workspace = true }
codec = { workspace = true, package = "parity-scale-codec" }
log = { workspace = true }
scale-info = { workspace = true }
smallvec = { version = "1.10" }
strum = { workspace = true, features = ["derive"] }

# darwinia
Expand Down
50 changes: 3 additions & 47 deletions runtime/koi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,14 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3;
pub const SLOT_DURATION: u64 = 6_000;
pub const MINUTES: BlockNumber = 60_000 / (SLOT_DURATION as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
pub struct WeightToFee;
impl frame_support::weights::WeightToFee for WeightToFee {
type Balance = Balance;

fn weight_to_fee(weight: &frame_support::weights::Weight) -> Self::Balance {
// polkadot-sdk
use frame_support::weights::WeightToFeePolynomial;

let time_poly: frame_support::weights::FeePolynomial<Balance> =
RefTimeToFee::polynomial().into();
let proof_poly: frame_support::weights::FeePolynomial<Balance> =
darwinia_common_runtime::ProofSizeToFee::polynomial().into();

// Take the maximum instead of the sum to charge by the more scarce resource.
time_poly.eval(weight.ref_time()).max(proof_poly.eval(weight.proof_size()))
}
}
pub struct RefTimeToFee;
impl frame_support::weights::WeightToFeePolynomial for RefTimeToFee {
type Balance = Balance;

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

smallvec::smallvec![frame_support::weights::WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: sp_runtime::Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
pub mod pallet_config;

pub mod weights;

mod pallets;
pub use pallets::*;

mod migration;

use darwinia_common_runtime::*;
pub use darwinia_common_runtime::*;
pub use dc_primitives::*;

// crates.io
Expand Down Expand Up @@ -950,7 +906,7 @@ sp_api::impl_runtime_apis! {

let weight = Executive::try_runtime_upgrade(checks).unwrap();

(weight, crate::pallet_config::RuntimeBlockWeights::get().max_block)
(weight, pallet_config::RuntimeBlockWeights::get().max_block)
}

fn execute_block(
Expand Down Expand Up @@ -978,7 +934,7 @@ fn replay_on_idle() {
use frame_system::pallet_prelude::BlockNumberFor;

let weight = <frame_system::Pallet<Runtime>>::block_weight();
let max_weight = crate::pallet_config::RuntimeBlockWeights::get().max_block;
let max_weight = pallet_config::RuntimeBlockWeights::get().max_block;
let remaining_weight = max_weight.saturating_sub(weight.total());
if remaining_weight.all_gt(frame_support::weights::Weight::zero()) {
let _ = <AllPalletsWithSystem as OnIdle<BlockNumberFor<Runtime>>>::on_idle(
Expand Down
Loading

0 comments on commit e619d26

Please sign in to comment.