Skip to content

Commit

Permalink
Merge pull request #674 from galacticcouncil/feat/rotate-collators
Browse files Browse the repository at this point in the history
feat: rotate collator order each session
  • Loading branch information
mrq1911 authored Dec 5, 2024
2 parents 8ccc36a + 381577d commit afed6e3
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.0.5"
version = "1.0.6"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
32 changes: 32 additions & 0 deletions integration-tests/src/kusama_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,38 @@ pub mod basilisk {
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_from_seed::<AuraId>("Bob"),
),
(
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_from_seed::<AuraId>("Charlie"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator4"),
get_from_seed::<AuraId>("Collator4"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator5"),
get_from_seed::<AuraId>("Collator5"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator6"),
get_from_seed::<AuraId>("Collator6"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator7"),
get_from_seed::<AuraId>("Collator7"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator8"),
get_from_seed::<AuraId>("Collator8"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator9"),
get_from_seed::<AuraId>("Collator9"),
),
(
get_account_id_from_seed::<sr25519::Public>("Collator10"),
get_from_seed::<AuraId>("Collator10"),
),
]
}

Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod nft_marketplace;
mod non_native_fee;
mod oracle;
mod router;
mod sessions;
mod transact_call_filter;
mod vesting;
mod xyk;
95 changes: 95 additions & 0 deletions integration-tests/src/sessions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#![cfg(test)]
use crate::kusama_test_net::*;
use basilisk_runtime::CollatorRewards;
use basilisk_runtime::Runtime;
use frame_support::traits::Contains;
use pallet_session::SessionManager;
use polkadot_xcm::v3::prelude::*;
use polkadot_xcm::VersionedXcm;
use pretty_assertions::assert_eq;
use xcm_emulator::TestExt;
#[test]
fn new_session_should_rotate_collators_list() {
TestNet::reset();

Basilisk::execute_with(|| {
let collator1 = basilisk::invulnerables()[0].0.clone(); //d435...
let collator2 = basilisk::invulnerables()[1].0.clone(); //8eaf...
let collator3 = basilisk::invulnerables()[2].0.clone(); //90b5...
let collator4 = basilisk::invulnerables()[3].0.clone(); //6ebe...
let collator5 = basilisk::invulnerables()[4].0.clone(); //ec5e...
let collator6 = basilisk::invulnerables()[5].0.clone(); //9c78...
let collator7 = basilisk::invulnerables()[6].0.clone(); //a678...
let collator8 = basilisk::invulnerables()[7].0.clone(); //2433...
let collator9 = basilisk::invulnerables()[8].0.clone(); //ee28...
let collator10 = basilisk::invulnerables()[9].0.clone(); //da53...

let collators = CollatorRewards::new_session(0).unwrap();
assert_eq!(
collators,
vec![
collator8.clone(),
collator4.clone(),
collator2.clone(),
collator3.clone(),
collator6.clone(),
collator7.clone(),
collator1.clone(),
collator10.clone(),
collator5.clone(),
collator9.clone()
]
);

let collators = CollatorRewards::new_session(1).unwrap();
assert_eq!(
collators,
vec![
collator4.clone(),
collator2.clone(),
collator3.clone(),
collator6.clone(),
collator7.clone(),
collator1.clone(),
collator10.clone(),
collator5.clone(),
collator9.clone(),
collator8.clone(),
]
);

let collators = CollatorRewards::new_session(2).unwrap();
assert_eq!(
collators,
vec![
collator2.clone(),
collator3.clone(),
collator6.clone(),
collator7.clone(),
collator1.clone(),
collator10.clone(),
collator5.clone(),
collator9.clone(),
collator8.clone(),
collator4.clone(),
]
);

let collators = CollatorRewards::new_session(3).unwrap();
assert_eq!(
collators,
vec![
collator3.clone(),
collator6.clone(),
collator7.clone(),
collator1.clone(),
collator10.clone(),
collator5.clone(),
collator9.clone(),
collator8.clone(),
collator4.clone(),
collator2.clone(),
]
);
});
}
2 changes: 1 addition & 1 deletion runtime/basilisk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basilisk-runtime"
version = "122.0.0"
version = "123.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
homepage = "https://github.com/galacticcouncil/Basilisk-node"
Expand Down
2 changes: 1 addition & 1 deletion runtime/basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("basilisk"),
impl_name: create_runtime_str!("basilisk"),
authoring_version: 1,
spec_version: 122,
spec_version: 123,
impl_version: 0,
apis: apis::RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
30 changes: 27 additions & 3 deletions runtime/basilisk/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,34 @@ impl pallet_collator_rewards::Config for Runtime {
type RewardPerCollator = RewardPerCollator;
type RewardCurrencyId = NativeAssetId;
type ExcludedCollators = ExcludedCollators;
// We wrap the ` SessionManager` implementation of `CollatorSelection` to get the collatrs that
// we hand out rewards to.
type SessionManager = CollatorSelection;
type SessionManager = RotatingCollatorManager;
type MaxCandidates = MaxInvulnerables;
}

pub struct RotatingCollatorManager;
impl SessionManager<AccountId> for RotatingCollatorManager {
fn new_session(new_index: SessionIndex) -> Option<Vec<AccountId>> {
// We wrap the ` SessionManager` implementation of `CollatorSelection` to get the collators that
// we hand out rewards to,
// then we rotate the collators to have a regular distribution for block production
let mut collators = CollatorSelection::new_session(new_index)?;

if let Some(rotation_amount) = (new_index as usize).checked_rem(collators.len()) {
collators.rotate_left(rotation_amount);
}

Some(collators)
}

fn end_session(end_index: SessionIndex) {
CollatorSelection::end_session(end_index)
}

fn start_session(start_index: SessionIndex) {
CollatorSelection::start_session(start_index)
}
}

parameter_types! {
pub const BasicDeposit: Balance = 5 * DOLLARS;
pub const ByteDeposit: Balance = DOLLARS / 10;
Expand Down Expand Up @@ -614,6 +636,8 @@ use frame_system::EnsureSigned;

#[cfg(not(feature = "runtime-benchmarks"))]
use frame_system::EnsureSignedBy;
use pallet_session::SessionManager;
use sp_staking::SessionIndex;

impl pallet_state_trie_migration::Config for Runtime {
type ControlOrigin = EnsureRoot<Self::AccountId>;
Expand Down

0 comments on commit afed6e3

Please sign in to comment.