Skip to content

Commit

Permalink
rotate collators based on session index and collators size
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoka committed Dec 4, 2024
1 parent d85bb68 commit 8a86988
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
4 changes: 4 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,10 @@ 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"),
),
]
}

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;
53 changes: 53 additions & 0 deletions integration-tests/src/sessions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![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 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 collators = CollatorRewards::new_session(0).unwrap();
assert_eq!(collators, vec![collator2.clone(), collator3.clone(), collator1.clone()]);

let collators = CollatorRewards::new_session(1).unwrap();
assert_eq!(collators, vec![collator2.clone(), collator3.clone(), collator1.clone()]);

let collators = CollatorRewards::new_session(2).unwrap();
assert_eq!(collators, vec![collator2.clone(), collator3.clone(), collator1.clone()]);

let collators = CollatorRewards::new_session(3).unwrap();
assert_eq!(collators, vec![collator3.clone(), collator1.clone(), collator2.clone()]);

let collators = CollatorRewards::new_session(4).unwrap();
assert_eq!(collators, vec![collator3.clone(), collator1.clone(), collator2.clone()]);

let collators = CollatorRewards::new_session(5).unwrap();
assert_eq!(collators, vec![collator3.clone(), collator1.clone(), collator2.clone()]);

let collators = CollatorRewards::new_session(6).unwrap();
assert_eq!(collators, vec![collator1.clone(), collator2.clone(), collator3.clone()]);

let collators = CollatorRewards::new_session(7).unwrap();
assert_eq!(collators, vec![collator1.clone(), collator2.clone(), collator3.clone()]);

let collators = CollatorRewards::new_session(8).unwrap();
assert_eq!(collators, vec![collator1.clone(), collator2.clone(), collator3.clone()]);

let collators = CollatorRewards::new_session(9).unwrap();
assert_eq!(collators, vec![collator2.clone(), collator3.clone(), collator1.clone()]);

let collators = CollatorRewards::new_session(10).unwrap();
assert_eq!(collators, vec![collator2.clone(), collator3.clone(), collator1.clone()]);
});
}
6 changes: 2 additions & 4 deletions runtime/basilisk/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use super::*;
use crate::governance::{origins::GeneralAdmin, TechCommitteeMajority, TechnicalCollective, TreasuryAccount};
use sp_io::transaction_index::index;

use pallet_transaction_multi_payment::{DepositAll, TransferFees};
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
Expand Down Expand Up @@ -551,11 +550,10 @@ impl SessionManager<AccountId> for RotatingCollatorManager {
// 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 !collators.is_empty() {
collators.rotate_left(1);
if let Some(amount) = (new_index as usize).checked_div(collators.len()) {
collators.rotate_left(amount);
}

Some(collators)
Expand Down

0 comments on commit 8a86988

Please sign in to comment.