Skip to content

Commit

Permalink
Enable async backing moonbase (#2623)
Browse files Browse the repository at this point in the history
* use relay block as clocktime in parachain staking

* add RelayChainBlockNumberProvider trait

* adapt benchmarks

* fix benchmarks

* fix warning in benchmarks

* fix staking TS tests

* upgrade to polkadot v1.3.0

* Temporarily disable kusama and polkadot runtime dependencies

* Remove support for Kusama Native Runtime

* fix delegation tests

* Update evm pin

* update deposit_asset function signature

* change clocktime to a diff of 2

* fix integration tests

* add last_relay_block to can_author runtime api

* fmt and fix some TS tests

* remove default impl for RelayChainBlockNumberProvider and add test comment

* Add Hasher generic parameter to Bounded

* Rename InvalidEvmTransactionError to TransactionValidationError

* Use transfer_allow_death instead of deprecated transfer

* fix build issue

* Use pallet_xcm::RuntimeCall

* BackendReader renamed to Backend in Frontier

* Update frontier pin

* Temporarily update new missing weight functions

* Adapt to new return type of ValidatedTransaction::apply

* IdentityInfo in pallet_identity is now generic

* Associated type Hasher for QueryPreimage, StorePreimage and Bounded

* Update Frontier Pin

* fix runtime compilation errors

* Rename BackendReader to Backend

* Remove Send and Sync Trait Bounds for Frontier Backend

* Implement closest_merkle_tree and child_closest_merkle_tree for StorageProvider

* Set block_relay to none

* Use polkadot_primitives v6

* Annotate EC for Eth::new

* Update polkadot-sdk pin

* Use transferAllowDeath instead of transfer in tests

* start migration

* Update polkadot-sdk pin

* Use transferAllowDeath instead of transfer

* add pre and post checks

* fix typescript test

* add logs for testing

* fix typescript tests

* Fix zombie tests

* finish migration

* clippy

* Fix test balance Transferable

* Temporarily disabled relay-encoder rust tests

* Fix Rust Tests build

* Fix build with runtime-benchmarks

* fmt

* Typescript Fmt

* Set MaxHolds to 1

* comply editorconfig

* fix democracy TS tests

* fix Eslint warning

* Fix typescript test

* fix txpool TS test

* fix precompile identity test

* remove unused import

* Fix rust tests

* Fix rust tests (no reserve anymore with preimage)

* Fix xcm rust tests

* Fix build error

* Update polkadot-sdk pin

* fix collective test

* Add Evm Fix

* use relay slot instead of relay block number

* add TODO in migration

* fix warning

* clippy

* use proper RelayChainSlot type

* fix compilation

* fix types in benchmarks

* new migration logic

* remove parachain_system bound

* add ConsensusHookWrapperForMigration

* remove ConsensusHookWrapper and restore previous migration

* work with the previous round length value

* fix staking TS test

* Fully enable async-backing on moonbase

* adapt GasLimitStorageGrowthRatio to new block gas limit

* make SlotProvider more generic

* minor fixes

* cleanup

* remove unnecessary dependencies

* fix rust compilation

* moonbase rounds are 120 parablocks now

* recompute gas limit pov ratio

* Revert "recompute gas limit pov ratio"

This reverts commit bca701f.

* Revert "adapt GasLimitStorageGrowthRatio to new block gas limit"

This reverts commit c3efa38.

* revert block size changes and min period changes

* revert unneeded tests changes from Agustin's PR

* opengov periods are double in parablocks now

* comment gov v1 tests

* palelt staking should support several "slot times"

* compile rust tests

* remove gov v1 tests

* compile rust tests

* missing import

* try to enable async backing on zombi tests

* inflation info per round are half now

* enable async backing in rpc zombi tests

* force moonbase collators to use async backing

* adapt rust staking tests

* async backing is not optional

* Update pallets/parachain-staking/src/lib.rs

Co-authored-by: Agustín Rodriguez <agus@moonsonglabs.com>

* remove constants no longer used

* fix client compilation

* fmt

* increase capacity and velocity

* use fixed ports for zombi relay

* enable async backing on zombi relay

* add json fields instead of replace

* use a ts script to modify rococo plain specs

* add json fields instead of modify

* use snake case

* use a custom polkadot binary for zombi tests

* update moonkit pin (improve async backing logs)

* not use raw chain spec for rococo

* not call author inherent on_finalize

* Revert "use a custom polkadot binary for zombi tests"

This reverts commit eba6d2f.

* zombinet require plain spec for the relay

* not call AuthorInherent::on_finalize

* no modify rococo specs

* use forked polkadot bins

* properly use forked polkadot bins

* explicitly list all polkadot bins

* fix path location on CI

* Fix zombienet timeouts for async branch (#2644)

* timeout

* fix

* relaxed the test

* lint

* rm broken rust toolchain

* force addition of wasm32 target

---------

Co-authored-by: Agusrodri <agusrodriguez2456@gmail.com>
Co-authored-by: Ahmad Kaouk <ahmadkaouk.93@gmail.com>
Co-authored-by: Agustín Rodriguez <agus@moonsonglabs.com>
Co-authored-by: Tim B <79199034+timbrinded@users.noreply.github.com>
  • Loading branch information
5 people authored Feb 14, 2024
1 parent 5ba55f0 commit 74ab3a3
Show file tree
Hide file tree
Showing 29 changed files with 108 additions and 287 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ jobs:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
fi
rustup show
rustup target add wasm32-unknown-unknown
# Checks are run after uploading artifacts since they are modified by the tests
- name: Unit tests
run: |
Expand Down Expand Up @@ -613,7 +614,18 @@ jobs:
docker create --name moonbeam_container $DOCKER_TAG bash
docker cp moonbeam_container:moonbeam/moonbeam test/tmp/moonbeam_rt
docker rm -f moonbeam_container
- name: Setup chainspecs
- name: "TEMPORARY: Download forked polkadot-bins"
run: |
# When we remove this step, re-add "runScripts": ["download-polkadot.sh"]," to moonwall config
cd test/tmp
wget https://opslayer-dev-artifacts.s3.us-east-2.amazonaws.com/bins/moonbeam/polkadot/1.3.0/polkadot
wget https://opslayer-dev-artifacts.s3.us-east-2.amazonaws.com/bins/moonbeam/polkadot/1.3.0/polkadot-execute-worker
wget https://opslayer-dev-artifacts.s3.us-east-2.amazonaws.com/bins/moonbeam/polkadot/1.3.0/polkadot-prepare-worker
chmod +x polkadot
chmod +x polkadot-execute-worker
chmod +x polkadot-prepare-worker
- name: Prepare Chainspecs
run: |
cd test
pnpm install
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/subxt-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ jobs:
uses: pnpm/action-setup@v2
with:
version: 8.6.12
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.74.0
override: true
- name: Install Subxt-cli
run: |
cargo install subxt-cli
Expand Down
24 changes: 12 additions & 12 deletions Cargo.lock

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

5 changes: 0 additions & 5 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,6 @@ pub struct RunCmd {
/// Removes moonbeam prefix from Prometheus metrics
#[clap(long)]
pub no_prometheus_prefix: bool,

/// Enable asynchronous backing (for collators only)
/// This feature is still experimental, please don't use it in production.
#[clap(hide = true, long = "experimental-async-backing")]
pub async_backing: bool,
}

impl RunCmd {
Expand Down
6 changes: 3 additions & 3 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ pub fn run() -> Result<()> {
collator_options,
id,
rpc_config,
cli.run.async_backing,
false,
hwbench,
)
.await
Expand All @@ -807,7 +807,7 @@ pub fn run() -> Result<()> {
collator_options,
id,
rpc_config,
cli.run.async_backing,
false,
hwbench,
)
.await
Expand All @@ -823,7 +823,7 @@ pub fn run() -> Result<()> {
collator_options,
id,
rpc_config,
cli.run.async_backing,
true,
hwbench,
)
.await
Expand Down
5 changes: 2 additions & 3 deletions node/service/src/chain_spec/moonbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,11 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
const BLOCKS_PER_ROUND: u32 = 2 * HOURS;
const BLOCKS_PER_YEAR: u32 = 31_557_600 / 6;
const NUM_SELECTED_CANDIDATES: u32 = 8;
pub fn moonbase_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use pallet_parachain_staking::inflation::{
perbill_annual_to_perbill_round, BLOCKS_PER_YEAR,
};
use pallet_parachain_staking::inflation::perbill_annual_to_perbill_round;
perbill_annual_to_perbill_round(
annual,
// rounds per year
Expand Down
5 changes: 2 additions & 3 deletions node/service/src/chain_spec/moonbeam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,11 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
const BLOCKS_PER_ROUND: u32 = 6 * HOURS;
const BLOCKS_PER_YEAR: u32 = 31_557_600 / 12;
const NUM_SELECTED_CANDIDATES: u32 = 8;
pub fn moonbeam_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use pallet_parachain_staking::inflation::{
perbill_annual_to_perbill_round, BLOCKS_PER_YEAR,
};
use pallet_parachain_staking::inflation::perbill_annual_to_perbill_round;
perbill_annual_to_perbill_round(
annual,
// rounds per year
Expand Down
5 changes: 2 additions & 3 deletions node/service/src/chain_spec/moonriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,11 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
const COLLATOR_COMMISSION: Perbill = Perbill::from_percent(20);
const PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(30);
const BLOCKS_PER_ROUND: u32 = 2 * HOURS;
const BLOCKS_PER_YEAR: u32 = 31_557_600 / 12;
const NUM_SELECTED_CANDIDATES: u32 = 8;
pub fn moonriver_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use pallet_parachain_staking::inflation::{
perbill_annual_to_perbill_round, BLOCKS_PER_YEAR,
};
use pallet_parachain_staking::inflation::perbill_annual_to_perbill_round;
perbill_annual_to_perbill_round(
annual,
// rounds per year
Expand Down
2 changes: 2 additions & 0 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ where
};

if async_backing {
log::info!("Collator started with asynchronous backing.");
let client_clone = client.clone();
let code_hash_provider = move |block_hash| {
client_clone
Expand Down Expand Up @@ -1093,6 +1094,7 @@ where
}),
);
} else {
log::info!("Collator started without asynchronous backing.");
task_manager.spawn_essential_handle().spawn(
"nimbus",
None,
Expand Down
12 changes: 4 additions & 8 deletions pallets/parachain-staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Helper methods for computing issuance based on inflation
use crate::pallet::{BalanceOf, Config, Pallet};
use frame_support::traits::Currency;
use frame_support::traits::{Currency, Get};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
Expand All @@ -25,13 +25,9 @@ use sp_runtime::{Perbill, RuntimeDebug};
use substrate_fixed::transcendental::pow as floatpow;
use substrate_fixed::types::I64F64;

const SECONDS_PER_YEAR: u32 = 31557600;
const SECONDS_PER_BLOCK: u32 = 12;
pub const BLOCKS_PER_YEAR: u32 = SECONDS_PER_YEAR / SECONDS_PER_BLOCK;

fn rounds_per_year<T: Config>() -> u32 {
let blocks_per_round = <Pallet<T>>::round().length;
BLOCKS_PER_YEAR / blocks_per_round
T::SlotsPerYear::get() / blocks_per_round
}

#[derive(
Expand Down Expand Up @@ -136,8 +132,8 @@ impl<Balance> InflationInfo<Balance> {
self.round = annual_to_round::<T>(new);
}
/// Reset round inflation rate based on changes to round length
pub fn reset_round(&mut self, new_length: u32) {
let periods = BLOCKS_PER_YEAR / new_length;
pub fn reset_round<T: Config>(&mut self, new_length: u32) {
let periods = T::SlotsPerYear::get() / new_length;
self.round = perbill_annual_to_perbill_round(self.annual, periods);
}
/// Set staking expectations
Expand Down
5 changes: 4 additions & 1 deletion pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ pub mod pallet {
/// Maximum candidates
#[pallet::constant]
type MaxCandidates: Get<u32>;
/// Average number of slots per year
/// A slot here is the unit of time for staking rounds (provided by SlotProvider)
type SlotsPerYear: Get<u32>;
}

#[pallet::error]
Expand Down Expand Up @@ -973,7 +976,7 @@ pub mod pallet {
round.length = new;
// update per-round inflation given new rounds per year
let mut inflation_config = <InflationConfig<T>>::get();
inflation_config.reset_round(new);
inflation_config.reset_round::<T>(new);
<Round<T>>::put(round);
Self::deposit_event(Event::BlocksPerRoundSet {
current_round: now,
Expand Down
1 change: 1 addition & 0 deletions pallets/parachain-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl Config for Test {
type SlotProvider = StakingRoundSlotProvider;
type WeightInfo = ();
type MaxCandidates = MaxCandidates;
type SlotsPerYear = frame_support::traits::ConstU32<{ 31_557_600 / 6 }>;
}

pub(crate) struct ExtBuilder {
Expand Down
18 changes: 9 additions & 9 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ fn set_blocks_per_round_event_emits_correctly() {
first_block: 0,
old: 5,
new: 6,
new_per_round_inflation_min: Perbill::from_parts(926),
new_per_round_inflation_ideal: Perbill::from_parts(926),
new_per_round_inflation_max: Perbill::from_parts(926),
new_per_round_inflation_min: Perbill::from_parts(463),
new_per_round_inflation_ideal: Perbill::from_parts(463),
new_per_round_inflation_max: Perbill::from_parts(463),
});
});
}
Expand Down Expand Up @@ -518,9 +518,9 @@ fn set_inflation_event_emits_correctly() {
annual_min: min,
annual_ideal: ideal,
annual_max: max,
round_min: Perbill::from_parts(57),
round_ideal: Perbill::from_parts(75),
round_max: Perbill::from_parts(93),
round_min: Perbill::from_parts(29),
round_ideal: Perbill::from_parts(38),
round_max: Perbill::from_parts(47),
});
});
}
Expand Down Expand Up @@ -560,9 +560,9 @@ fn set_inflation_storage_updates_correctly() {
assert_eq!(
ParachainStaking::inflation_config().round,
Range {
min: Perbill::from_parts(57),
ideal: Perbill::from_parts(75),
max: Perbill::from_parts(93)
min: Perbill::from_parts(29),
ideal: Perbill::from_parts(38),
max: Perbill::from_parts(47)
}
);
});
Expand Down
1 change: 1 addition & 0 deletions precompiles/parachain-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl pallet_parachain_staking::Config for Runtime {
type SlotProvider = StakingRoundSlotProvider;
type WeightInfo = ();
type MaxCandidates = MaxCandidates;
type SlotsPerYear = frame_support::traits::ConstU32<{ 31_557_600 / 6 }>;
}

pub(crate) struct ExtBuilder {
Expand Down
12 changes: 8 additions & 4 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_
.saturating_div(2)
.set_proof_size(cumulus_primitives_core::relay_chain::MAX_POV_SIZE as u64);

// TODO: Set MILLISECS_PER_BLOCK to 6000 when async backing will be definitly enabled
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const MILLISECS_PER_BLOCK: u64 = 6_000;
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
Expand Down Expand Up @@ -709,8 +708,12 @@ parameter_types! {
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
}

pub const UNINCLUDED_SEGMENT_CAPACITY: u32 = 1;
pub const BLOCK_PROCESSING_VELOCITY: u32 = 1;
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
/// into the relay chain.
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.
const BLOCK_PROCESSING_VELOCITY: u32 = 2;

type ConsensusHook = pallet_async_backing::consensus_hook::FixedVelocityConsensusHook<
Runtime,
Expand Down Expand Up @@ -830,6 +833,7 @@ impl pallet_parachain_staking::Config for Runtime {
type SlotProvider = RelayChainSlotProvider;
type WeightInfo = moonbeam_weights::pallet_parachain_staking::WeightInfo<Runtime>;
type MaxCandidates = ConstU32<200>;
type SlotsPerYear = ConstU32<{ 31_557_600 / 6 }>;
}

impl pallet_author_inherent::Config for Runtime {
Expand Down
Loading

0 comments on commit 74ab3a3

Please sign in to comment.