From 5c31e58ef537cc2bd3731014dd62f357353c921b Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 3 Dec 2024 20:39:13 +0100 Subject: [PATCH 1/5] add wrapper for session manager --- runtime/basilisk/src/system.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/runtime/basilisk/src/system.rs b/runtime/basilisk/src/system.rs index e62c09abb5..8ad4f0a06c 100644 --- a/runtime/basilisk/src/system.rs +++ b/runtime/basilisk/src/system.rs @@ -17,6 +17,7 @@ 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}; @@ -540,12 +541,36 @@ 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 for RotatingCollatorManager { + fn new_session(new_index: SessionIndex) -> Option> { + // 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); + } + + 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; @@ -614,6 +639,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; From d85bb684919a3035523ae7895ce57ca88eec8d45 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 3 Dec 2024 21:18:07 +0100 Subject: [PATCH 2/5] bump version --- Cargo.lock | 2 +- runtime/basilisk/Cargo.toml | 2 +- runtime/basilisk/src/lib.rs | 2 +- runtime/basilisk/src/system.rs | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04b678e4f8..e93ee1a8e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -973,7 +973,7 @@ dependencies = [ [[package]] name = "basilisk-runtime" -version = "122.0.0" +version = "123.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", diff --git a/runtime/basilisk/Cargo.toml b/runtime/basilisk/Cargo.toml index aa3b2d79ab..c620927474 100644 --- a/runtime/basilisk/Cargo.toml +++ b/runtime/basilisk/Cargo.toml @@ -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" diff --git a/runtime/basilisk/src/lib.rs b/runtime/basilisk/src/lib.rs index c7e2413505..5d02c03caf 100644 --- a/runtime/basilisk/src/lib.rs +++ b/runtime/basilisk/src/lib.rs @@ -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, diff --git a/runtime/basilisk/src/system.rs b/runtime/basilisk/src/system.rs index 8ad4f0a06c..87ded44ea3 100644 --- a/runtime/basilisk/src/system.rs +++ b/runtime/basilisk/src/system.rs @@ -541,7 +541,6 @@ impl pallet_collator_rewards::Config for Runtime { type RewardPerCollator = RewardPerCollator; type RewardCurrencyId = NativeAssetId; type ExcludedCollators = ExcludedCollators; - type SessionManager = RotatingCollatorManager; type MaxCandidates = MaxInvulnerables; } From 8a8698856fe285086f9ed097a7948a4863d01d43 Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 4 Dec 2024 10:25:51 +0100 Subject: [PATCH 3/5] rotate collators based on session index and collators size --- integration-tests/src/kusama_test_net.rs | 4 ++ integration-tests/src/lib.rs | 1 + integration-tests/src/sessions.rs | 53 ++++++++++++++++++++++++ runtime/basilisk/src/system.rs | 6 +-- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 integration-tests/src/sessions.rs diff --git a/integration-tests/src/kusama_test_net.rs b/integration-tests/src/kusama_test_net.rs index 35111d42b0..d2a3b3bd73 100644 --- a/integration-tests/src/kusama_test_net.rs +++ b/integration-tests/src/kusama_test_net.rs @@ -307,6 +307,10 @@ pub mod basilisk { get_account_id_from_seed::("Bob"), get_from_seed::("Bob"), ), + ( + get_account_id_from_seed::("Charlie"), + get_from_seed::("Charlie"), + ), ] } diff --git a/integration-tests/src/lib.rs b/integration-tests/src/lib.rs index 7ae33ebae0..cec3dbd118 100644 --- a/integration-tests/src/lib.rs +++ b/integration-tests/src/lib.rs @@ -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; diff --git a/integration-tests/src/sessions.rs b/integration-tests/src/sessions.rs new file mode 100644 index 0000000000..d078598450 --- /dev/null +++ b/integration-tests/src/sessions.rs @@ -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()]); + }); +} diff --git a/runtime/basilisk/src/system.rs b/runtime/basilisk/src/system.rs index 87ded44ea3..e6f9d7fb03 100644 --- a/runtime/basilisk/src/system.rs +++ b/runtime/basilisk/src/system.rs @@ -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}; @@ -551,11 +550,10 @@ impl SessionManager 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) From c156c484cde9c3b5876d36ecc864a2b501e47db3 Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 4 Dec 2024 18:21:19 +0100 Subject: [PATCH 4/5] change rotation to use module and add test with 10 collators --- integration-tests/src/kusama_test_net.rs | 28 +++++++ integration-tests/src/sessions.rs | 94 +++++++++++++++++------- runtime/basilisk/src/system.rs | 4 +- 3 files changed, 98 insertions(+), 28 deletions(-) diff --git a/integration-tests/src/kusama_test_net.rs b/integration-tests/src/kusama_test_net.rs index d2a3b3bd73..117beebb05 100644 --- a/integration-tests/src/kusama_test_net.rs +++ b/integration-tests/src/kusama_test_net.rs @@ -311,6 +311,34 @@ pub mod basilisk { get_account_id_from_seed::("Charlie"), get_from_seed::("Charlie"), ), + ( + get_account_id_from_seed::("Collator4"), + get_from_seed::("Collator4"), + ), + ( + get_account_id_from_seed::("Collator5"), + get_from_seed::("Collator5"), + ), + ( + get_account_id_from_seed::("Collator6"), + get_from_seed::("Collator6"), + ), + ( + get_account_id_from_seed::("Collator7"), + get_from_seed::("Collator7"), + ), + ( + get_account_id_from_seed::("Collator8"), + get_from_seed::("Collator8"), + ), + ( + get_account_id_from_seed::("Collator9"), + get_from_seed::("Collator9"), + ), + ( + get_account_id_from_seed::("Collator10"), + get_from_seed::("Collator10"), + ), ] } diff --git a/integration-tests/src/sessions.rs b/integration-tests/src/sessions.rs index d078598450..b436ab392f 100644 --- a/integration-tests/src/sessions.rs +++ b/integration-tests/src/sessions.rs @@ -6,8 +6,8 @@ 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(); @@ -16,38 +16,80 @@ fn new_session_should_rotate_collators_list() { 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![collator2.clone(), collator3.clone(), collator1.clone()]); + 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![collator2.clone(), collator3.clone(), collator1.clone()]); + 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(), collator1.clone()]); + 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(), 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()]); + assert_eq!( + collators, + vec![ + collator3.clone(), + collator6.clone(), + collator7.clone(), + collator1.clone(), + collator10.clone(), + collator5.clone(), + collator9.clone(), + collator8.clone(), + collator4.clone(), + collator2.clone(), + ] + ); }); } diff --git a/runtime/basilisk/src/system.rs b/runtime/basilisk/src/system.rs index e6f9d7fb03..4c2ad35485 100644 --- a/runtime/basilisk/src/system.rs +++ b/runtime/basilisk/src/system.rs @@ -552,8 +552,8 @@ impl SessionManager for RotatingCollatorManager { // then we rotate the collators to have a regular distribution for block production let mut collators = CollatorSelection::new_session(new_index)?; - if let Some(amount) = (new_index as usize).checked_div(collators.len()) { - collators.rotate_left(amount); + if let Some(rotation_amount) = (new_index as usize).checked_rem(collators.len()) { + collators.rotate_left(rotation_amount); } Some(collators) From 381577d6e00783fd1a3ccdae61455b410c22d312 Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 4 Dec 2024 18:53:27 +0100 Subject: [PATCH 5/5] bump versions --- Cargo.lock | 2 +- integration-tests/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e93ee1a8e9..f91b2f1022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11179,7 +11179,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.0.5" +version = "1.0.6" dependencies = [ "basilisk-runtime", "cumulus-pallet-aura-ext", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index ae74465358..70bf3d7d04 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.0.5" +version = "1.0.6" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021"