Skip to content

Commit

Permalink
Convert parachain-staking for use with cumulus and pallet-session (#1)
Browse files Browse the repository at this point in the history
Converts parachain-staking to use default polkadot branches for substrate pallets
Implements the required traits for use with the session pallet and cumulus concensus
  • Loading branch information
nvengal authored Apr 22, 2022
1 parent 0eb967e commit 20f6664
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 23 deletions.
26 changes: 14 additions & 12 deletions pallets/parachain-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,39 @@ log = "0.4"
serde = { version = "1.0.101", optional = true }

# Substrate
frame-benchmarking = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18", optional = true, default-features = false }
frame-support = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18", default-features = false }
frame-system = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", optional = true, default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false }
parity-scale-codec = { version = "3.0.0", default-features = false, features = [ "derive" ] }
scale-info = { version = "2.0", default-features = false, features = [ "derive" ] }
sp-runtime = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18", default-features = false }
sp-std = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false }
sp-staking = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false}
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false }
substrate-fixed = { git = "https://github.com/encointer/substrate-fixed", default-features = false }

# Nimbus
nimbus-primitives = { git = "https://github.com/purestake/nimbus", branch = "moonbeam-polkadot-v0.9.18", default-features = false }
pallet-authorship = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false}
pallet-session = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18", default-features = false}

[dev-dependencies]
similar-asserts = "1.1.0"

pallet-balances = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18" }
sp-core = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18" }
sp-io = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.18" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }

[features]
default = [ "std" ]
std = [
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"nimbus-primitives/std",
"parity-scale-codec/std",
"scale-info/std",
"serde",
"sp-runtime/std",
"sp-std/std",
"sp-staking/std",
"pallet-authorship/std",
"pallet-session/std",
]
runtime-benchmarks = [ "frame-benchmarking" ]
try-runtime = [ "frame-support/try-runtime" ]
2 changes: 1 addition & 1 deletion pallets/parachain-staking/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::traits::{Currency, Get, OnFinalize, OnInitialize, ReservableCurrency};
use frame_system::RawOrigin;
use nimbus_primitives::EventHandler;
use pallet_authorship::EventHandler;
use sp_runtime::{Perbill, Percent};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

Expand Down
81 changes: 71 additions & 10 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub mod pallet {
use parity_scale_codec::Decode;
use sp_runtime::{
traits::{Saturating, Zero},
Perbill, Percent,
Perbill, Percent, Permill,
};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

Expand Down Expand Up @@ -1652,26 +1652,87 @@ pub mod pallet {
}
}

/// Add reward points to block authors:
/// * 20 points to the block producer for producing a block in the chain
impl<T: Config> nimbus_primitives::EventHandler<T::AccountId> for Pallet<T> {
impl<T: Config> Get<Vec<T::AccountId>> for Pallet<T> {
fn get() -> Vec<T::AccountId> {
Self::selected_candidates()
}
}

impl<T: Config> pallet_authorship::EventHandler<T::AccountId, T::BlockNumber> for Pallet<T> {
// Add reward points to block authors:
// * 20 points to the block producer for producing a block in the chain
fn note_author(author: T::AccountId) {
let now = <Round<T>>::get().current;
let score_plus_20 = <AwardedPts<T>>::get(now, &author).saturating_add(20);
<AwardedPts<T>>::insert(now, author, score_plus_20);
<Points<T>>::mutate(now, |x| *x = x.saturating_add(20));
}

fn note_uncle(_author: T::AccountId, _age: T::BlockNumber) {}
}

impl<T: Config> nimbus_primitives::CanAuthor<T::AccountId> for Pallet<T> {
fn can_author(account: &T::AccountId, _slot: &u32) -> bool {
Self::is_selected_candidate(account)
impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> {
fn new_session(new_index: sp_staking::SessionIndex) -> Option<Vec<T::AccountId>> {
log::debug!(
"assembling new collators for new session {} at #{:?}",
new_index,
<frame_system::Pallet<T>>::block_number(),
);

let collators = Pallet::<T>::selected_candidates().to_vec();
if collators.is_empty() {
// We never want to pass an empty set of collators. This would brick the chain.
// Sessions 0, 1 are configured on genesis and use SessionConfig keys
if new_index > 1 {
log::error!("💥 keeping old session because of empty collator set!");
}
None
} else {
Some(collators)
}
}

fn end_session(_end_index: sp_staking::SessionIndex) {}

fn start_session(_start_index: sp_staking::SessionIndex) {}
}

impl<T: Config> Get<Vec<T::AccountId>> for Pallet<T> {
fn get() -> Vec<T::AccountId> {
Self::selected_candidates()
impl<T: Config> pallet_session::ShouldEndSession<T::BlockNumber> for Pallet<T> {
fn should_end_session(now: T::BlockNumber) -> bool {
let round = <Round<T>>::get();
return round.should_update(now);
}
}

impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumber> for Pallet<T> {
fn average_session_length() -> T::BlockNumber {
T::BlockNumber::from(<Round<T>>::get().length)
}

fn estimate_current_session_progress(now: T::BlockNumber) -> (Option<Permill>, Weight) {
let round = <Round<T>>::get();
let passed_blocks = now.saturating_sub(round.first);

(
Some(Permill::from_rational(
passed_blocks,
T::BlockNumber::from(round.length),
)),
// One read for the round info, blocknumber is read free
T::DbWeight::get().reads(1),
)
}

fn estimate_next_session_rotation(
_now: T::BlockNumber,
) -> (Option<T::BlockNumber>, Weight) {
let round = <Round<T>>::get();

(
Some(round.first + round.length.into()),
// One read for the round info, blocknumber is read free
T::DbWeight::get().reads(1),
)
}
}
}

0 comments on commit 20f6664

Please sign in to comment.