Skip to content

Commit 6a6add5

Browse files
committed
start with main changes
1 parent b064f1a commit 6a6add5

File tree

6 files changed

+124
-30
lines changed

6 files changed

+124
-30
lines changed

Cargo.lock

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

runtime/moonbeam/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ parachains-common = { workspace = true }
161161
async-backing-primitives = { workspace = true }
162162
moonkit-xcm-primitives = { workspace = true }
163163
nimbus-primitives = { workspace = true }
164+
pallet-async-backing = { workspace = true }
164165
pallet-author-inherent = { workspace = true }
165166
pallet-author-slot-filter = { workspace = true }
166167
pallet-relay-storage-roots = { workspace = true }
@@ -179,6 +180,7 @@ sha3 = { workspace = true, features = ["std"] }
179180
cumulus-primitives-parachain-inherent = { workspace = true }
180181
cumulus-test-relay-sproof-builder = { workspace = true }
181182

183+
sp-timestamp = { workspace = true }
182184
polkadot-runtime-parachains = { workspace = true }
183185
xcm-simulator = { workspace = true }
184186

@@ -220,6 +222,7 @@ std = [
220222
"orml-xtokens/std",
221223
"pallet-asset-manager/std",
222224
"pallet-assets/std",
225+
"pallet-async-backing/std",
223226
"pallet-author-inherent/std",
224227
"pallet-author-mapping/std",
225228
"pallet-author-slot-filter/std",
@@ -370,6 +373,7 @@ try-runtime = [
370373
"frame-try-runtime",
371374
"moonbeam-runtime-common/try-runtime",
372375
"pallet-asset-manager/try-runtime",
376+
"pallet-async-backing/try-runtime",
373377
"pallet-author-mapping/try-runtime",
374378
"pallet-author-slot-filter/try-runtime",
375379
"pallet-balances/try-runtime",

runtime/moonbeam/src/lib.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use sp_version::RuntimeVersion;
101101

102102
use nimbus_primitives::CanAuthor;
103103

104+
mod migrations;
104105
mod precompiles;
105106
pub use precompiles::{
106107
MoonbeamPrecompiles, PrecompileName, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
@@ -146,7 +147,7 @@ pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_
146147
.saturating_div(2)
147148
.set_proof_size(relay_chain::MAX_POV_SIZE as u64);
148149

149-
pub const MILLISECS_PER_BLOCK: u64 = 12000;
150+
pub const MILLISECS_PER_BLOCK: u64 = 6_000;
150151
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
151152
pub const HOURS: BlockNumber = MINUTES * 60;
152153
pub const DAYS: BlockNumber = HOURS * 24;
@@ -668,6 +669,19 @@ impl pallet_ethereum::Config for Runtime {
668669
type ExtraDataLength = ConstU32<30>;
669670
}
670671

672+
/// Maximum number of blocks simultaneously accepted by the Runtime, not yet included
673+
/// into the relay chain.
674+
const UNINCLUDED_SEGMENT_CAPACITY: u32 = 3;
675+
/// How many parachain blocks are processed by the relay chain per parent. Limits the
676+
/// number of blocks authored per slot.
677+
const BLOCK_PROCESSING_VELOCITY: u32 = 1;
678+
679+
type ConsensusHook = pallet_async_backing::consensus_hook::FixedVelocityConsensusHook<
680+
Runtime,
681+
BLOCK_PROCESSING_VELOCITY,
682+
UNINCLUDED_SEGMENT_CAPACITY,
683+
>;
684+
671685
impl cumulus_pallet_parachain_system::Config for Runtime {
672686
type RuntimeEvent = RuntimeEvent;
673687
type OnSystemEvent = ();
@@ -676,8 +690,10 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
676690
type OutboundXcmpMessageSource = XcmpQueue;
677691
type XcmpMessageHandler = XcmpQueue;
678692
type ReservedXcmpWeight = ReservedXcmpWeight;
679-
type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
680-
type ConsensusHook = cumulus_pallet_parachain_system::ExpectParentIncluded;
693+
type CheckAssociatedRelayNumber =
694+
cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
695+
//TODO: replace by ConsensusHookWrapperForRelayTimestamp
696+
type ConsensusHook = ConsensusHook;
681697
type DmpQueue = frame_support::traits::EnqueueWithOrigin<MessageQueue, RelayOrigin>;
682698
type WeightInfo = cumulus_pallet_parachain_system::weights::SubstrateWeight<Runtime>;
683699
}
@@ -768,13 +784,11 @@ impl pallet_parachain_staking::OnInactiveCollator<Runtime> for OnInactiveCollato
768784
type MonetaryGovernanceOrigin =
769785
EitherOfDiverse<EnsureRoot<AccountId>, governance::custom_origins::GeneralAdmin>;
770786

771-
/// TODO:
772-
/// Temporary type that we should replace by RelayChainSlotProvider once async backing is enabled.
773-
pub struct StakingRoundSlotProvider;
774-
impl Get<Slot> for StakingRoundSlotProvider {
787+
pub struct RelayChainSlotProvider;
788+
impl Get<Slot> for RelayChainSlotProvider {
775789
fn get() -> Slot {
776-
let block_number: u64 = frame_system::pallet::Pallet::<Runtime>::block_number().into();
777-
Slot::from(block_number)
790+
let slot_info = pallet_async_backing::pallet::Pallet::<Runtime>::slot_info();
791+
slot_info.unwrap_or_default().0
778792
}
779793
}
780794

@@ -815,7 +829,7 @@ impl pallet_parachain_staking::Config for Runtime {
815829
type PayoutCollatorReward = PayoutCollatorOrOrbiterReward;
816830
type OnInactiveCollator = OnInactiveCollator;
817831
type OnNewRound = OnNewRound;
818-
type SlotProvider = StakingRoundSlotProvider;
832+
type SlotProvider = RelayChainSlotProvider;
819833
type WeightInfo = moonbeam_weights::pallet_parachain_staking::WeightInfo<Runtime>;
820834
type MaxCandidates = ConstU32<200>;
821835
type SlotDuration = ConstU64<12_000>;
@@ -837,6 +851,12 @@ impl pallet_author_slot_filter::Config for Runtime {
837851
type WeightInfo = moonbeam_weights::pallet_author_slot_filter::WeightInfo<Runtime>;
838852
}
839853

854+
impl pallet_async_backing::Config for Runtime {
855+
type AllowMultipleBlocksPerSlot = ConstBool<true>;
856+
type GetAndVerifySlot = pallet_async_backing::RelaySlot;
857+
type ExpectedBlockTime = ConstU64<6000>;
858+
}
859+
840860
parameter_types! {
841861
pub const InitializationPayment: Perbill = Perbill::from_percent(30);
842862
pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
@@ -1103,7 +1123,10 @@ impl pallet_proxy::Config for Runtime {
11031123

11041124
impl pallet_migrations::Config for Runtime {
11051125
type RuntimeEvent = RuntimeEvent;
1106-
type MigrationsList = (moonbeam_runtime_common::migrations::CommonMigrations<Runtime>,);
1126+
type MigrationsList = (
1127+
moonbeam_runtime_common::migrations::CommonMigrations<Runtime>,
1128+
migrations::MoonbeamMigrations,
1129+
);
11071130
type XcmExecutionManager = XcmExecutionManager;
11081131
}
11091132

@@ -1347,6 +1370,7 @@ construct_runtime! {
13471370
AuthorFilter: pallet_author_slot_filter::{Pallet, Call, Storage, Event, Config<T>} = 22,
13481371
AuthorMapping: pallet_author_mapping::{Pallet, Call, Config<T>, Storage, Event<T>} = 23,
13491372
MoonbeamOrbiters: pallet_moonbeam_orbiters::{Pallet, Call, Storage, Event<T>} = 24,
1373+
AsyncBacking: pallet_async_backing::{Pallet, Storage} = 25,
13501374

13511375
// Handy utilities.
13521376
Utility: pallet_utility::{Pallet, Call, Event} = 30,
@@ -1572,12 +1596,10 @@ moonbeam_runtime_common::impl_runtime_apis_plus_common! {
15721596

15731597
impl async_backing_primitives::UnincludedSegmentApi<Block> for Runtime {
15741598
fn can_build_upon(
1575-
_included_hash: <Block as BlockT>::Hash,
1576-
_slot: async_backing_primitives::Slot,
1599+
included_hash: <Block as BlockT>::Hash,
1600+
slot: async_backing_primitives::Slot,
15771601
) -> bool {
1578-
// This runtime API can be called only when asynchronous backing is enabled client-side
1579-
// We return false here to force the client to not use async backing in moonbeam.
1580-
false
1602+
ConsensusHook::can_build_upon(included_hash, slot)
15811603
}
15821604
}
15831605
}

runtime/moonbeam/src/migrations.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2024 Moonbeam Foundation Inc.
2+
// This file is part of Moonbeam.
3+
4+
// Moonbeam is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Moonbeam is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! # Moonbeam specific Migrations
18+
19+
use crate::Runtime;
20+
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
21+
use pallet_migrations::{GetMigrations, Migration};
22+
use pallet_parachain_staking::migrations::MultiplyRoundLenBy2;
23+
use sp_std::{prelude::*, vec};
24+
25+
pub struct MoonbeamMigrations;
26+
27+
impl GetMigrations for MoonbeamMigrations {
28+
fn get_migrations() -> Vec<Box<dyn Migration>> {
29+
vec![Box::new(PalletStakingMultiplyRoundLenBy2)]
30+
}
31+
}
32+
33+
// This migration should only be applied to runtimes with async backing enabled
34+
pub struct PalletStakingMultiplyRoundLenBy2;
35+
impl Migration for PalletStakingMultiplyRoundLenBy2 {
36+
fn friendly_name(&self) -> &str {
37+
"MM_MultiplyRoundLenBy2"
38+
}
39+
40+
fn migrate(&self, _available_weight: Weight) -> Weight {
41+
MultiplyRoundLenBy2::<Runtime>::on_runtime_upgrade()
42+
}
43+
}

runtime/moonbeam/tests/common/mod.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ pub use moonbeam_runtime::{
2626
asset_config::AssetRegistrarMetadata,
2727
currency::{GIGAWEI, GLMR, SUPPLY_FACTOR, WEI},
2828
xcm_config::AssetType,
29-
AccountId, AssetId, AssetManager, AuthorInherent, Balance, Balances, CrowdloanRewards,
30-
Ethereum, Executive, Header, InflationInfo, ParachainStaking, Range, Runtime, RuntimeCall,
31-
RuntimeEvent, System, TransactionConverter, TransactionPaymentAsGasPrice, UncheckedExtrinsic,
32-
HOURS, WEEKS,
29+
AccountId, AssetId, AssetManager, AsyncBacking, AuthorInherent, Balance, Balances,
30+
CrowdloanRewards, Ethereum, Executive, Header, InflationInfo, ParachainStaking, Range, Runtime,
31+
RuntimeCall, RuntimeEvent, System, TransactionConverter, TransactionPaymentAsGasPrice,
32+
UncheckedExtrinsic, HOURS, WEEKS,
3333
};
3434
use nimbus_primitives::{NimbusId, NIMBUS_ENGINE_ID};
35+
use polkadot_parachain::primitives::HeadData;
36+
use sp_consensus_slots::Slot;
3537
use sp_core::{Encode, H160};
3638
use sp_runtime::{traits::Dispatchable, BuildStorage, Digest, DigestItem, Perbill, Percent};
3739

@@ -83,6 +85,8 @@ pub fn run_to_block(n: u32, author: Option<NimbusId>) {
8385
}
8486
}
8587

88+
increase_last_relay_slot_number(1);
89+
8690
// Initialize the new block
8791
AuthorInherent::on_initialize(System::block_number());
8892
ParachainStaking::on_initialize(System::block_number());
@@ -339,8 +343,20 @@ pub fn root_origin() -> <Runtime as frame_system::Config>::RuntimeOrigin {
339343
pub fn set_parachain_inherent_data() {
340344
use cumulus_primitives_core::PersistedValidationData;
341345
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
342-
let (relay_parent_storage_root, relay_chain_state) =
343-
RelayStateSproofBuilder::default().into_state_root_and_proof();
346+
347+
let mut relay_sproof = RelayStateSproofBuilder::default();
348+
relay_sproof.para_id = 100u32.into();
349+
relay_sproof.included_para_head = Some(HeadData(vec![1, 2, 3]));
350+
351+
let additional_key_values = vec![(
352+
moonbeam_core_primitives::well_known_relay_keys::TIMESTAMP_NOW.to_vec(),
353+
sp_timestamp::Timestamp::default().encode(),
354+
)];
355+
356+
relay_sproof.additional_key_values = additional_key_values;
357+
358+
let (relay_parent_storage_root, relay_chain_state) = relay_sproof.into_state_root_and_proof();
359+
344360
let vfp = PersistedValidationData {
345361
relay_parent_number: 1u32,
346362
relay_parent_storage_root,
@@ -371,3 +387,11 @@ pub fn ethereum_transaction(raw_hex_tx: &str) -> pallet_ethereum::Transaction {
371387
assert!(transaction.is_ok());
372388
transaction.unwrap()
373389
}
390+
391+
pub(crate) fn increase_last_relay_slot_number(amount: u64) {
392+
let last_relay_slot = u64::from(AsyncBacking::slot_info().unwrap_or_default().0);
393+
frame_support::storage::unhashed::put(
394+
&frame_support::storage::storage_prefix(b"AsyncBacking", b"SlotInfo"),
395+
&((Slot::from(last_relay_slot + amount), 0)),
396+
);
397+
}

runtime/moonbeam/tests/integration_test.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,9 @@ fn reward_block_authors() {
625625
)])
626626
.build()
627627
.execute_with(|| {
628-
set_parachain_inherent_data();
629-
for x in 2..3599 {
630-
run_to_block(x, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
631-
}
628+
increase_last_relay_slot_number(1);
629+
// Just before round 3
630+
run_to_block(7199, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
632631
// no rewards doled out yet
633632
assert_eq!(
634633
Balances::usable_balance(AccountId::from(ALICE)),
@@ -638,7 +637,7 @@ fn reward_block_authors() {
638637
Balances::usable_balance(AccountId::from(BOB)),
639638
9_950_000 * GLMR,
640639
);
641-
run_to_block(3601, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
640+
run_to_block(7201, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
642641
// rewards minted and distributed
643642
assert_eq!(
644643
Balances::usable_balance(AccountId::from(ALICE)),
@@ -672,14 +671,14 @@ fn reward_block_authors_with_parachain_bond_reserved() {
672671
)])
673672
.build()
674673
.execute_with(|| {
675-
set_parachain_inherent_data();
674+
increase_last_relay_slot_number(1);
676675
assert_ok!(ParachainStaking::set_parachain_bond_account(
677676
root_origin(),
678677
AccountId::from(CHARLIE),
679678
),);
680679

681680
// Stop just before round 3
682-
run_to_block(3599, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
681+
run_to_block(7199, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
683682
// no collators rewards doled out yet
684683
assert_eq!(
685684
Balances::usable_balance(AccountId::from(ALICE)),
@@ -696,7 +695,7 @@ fn reward_block_authors_with_parachain_bond_reserved() {
696695
);
697696

698697
// Go to round 3
699-
run_to_block(3601, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
698+
run_to_block(7201, Some(NimbusId::from_slice(&ALICE_NIMBUS).unwrap()));
700699

701700
// collators rewards minted and distributed
702701
assert_eq!(

0 commit comments

Comments
 (0)