Skip to content

Commit

Permalink
A0-0000: Choose a producer for every block in session (#1929)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Swietek <[email protected]>
  • Loading branch information
mike1729 and mike1729 authored Feb 5, 2025
1 parent 4bc5d3e commit 35181e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
20 changes: 2 additions & 18 deletions pallets/committee-management/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use primitives::{
AbftScoresProvider, BanHandler, BanInfo, BanReason, BannedValidators, CommitteeSeats,
EraValidators, SessionCommittee, SessionValidatorError, SessionValidators, ValidatorProvider,
};
use rand::{seq::SliceRandom, SeedableRng};
use rand_pcg::Pcg32;
use sp_runtime::{traits::Get, Perbill, Perquintill};
use sp_staking::{EraIndex, SessionIndex};
use sp_std::{
Expand Down Expand Up @@ -57,17 +55,6 @@ fn choose_for_session<T: Clone>(validators: &[T], count: usize, session: usize)
Some(chosen)
}

fn shuffle_order_for_session<T>(
producers: &mut Vec<T>,
validators: &mut Vec<T>,
session: SessionIndex,
) {
let mut rng = Pcg32::seed_from_u64(session as u64);

producers.shuffle(&mut rng);
validators.shuffle(&mut rng);
}

/// Choose all items from `reserved` if present and extend it by #`non_reserved_seats` from
/// `non_reserved` if present.
fn choose_finality_committee<T: Clone>(
Expand Down Expand Up @@ -105,23 +92,20 @@ fn select_committee_inner<AccountId: Clone + PartialEq>(
let non_reserved_committee =
choose_for_session(non_reserved, non_reserved_seats, current_session as usize);

let mut finalizers = choose_finality_committee(
let finalizers = choose_finality_committee(
&reserved_committee,
&non_reserved_committee,
non_reserved_finality_seats,
current_session as usize,
);

let mut producers = match (reserved_committee, non_reserved_committee) {
let producers = match (reserved_committee, non_reserved_committee) {
(Some(rc), Some(nrc)) => Some(rc.into_iter().chain(nrc.into_iter()).collect()),
(Some(rc), _) => Some(rc),
(_, Some(nrc)) => Some(nrc),
_ => None,
}?;

// randomize order of the producers and committee
shuffle_order_for_session(&mut producers, &mut finalizers, current_session);

Some(SessionCommittee {
producers,
finalizers,
Expand Down
12 changes: 11 additions & 1 deletion pallets/committee-management/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use frame_system::pallet_prelude::BlockNumberFor;
use log::debug;
use pallet_session::SessionManager;
use primitives::{AbftScoresProvider, EraManager, FinalityCommitteeManager, SessionCommittee};
use rand::{prelude::SliceRandom, SeedableRng};
use rand_pcg::Pcg32;
use sp_runtime::traits::Get;
use sp_staking::{EraIndex, SessionIndex};
use sp_std::{marker::PhantomData, vec::Vec};

Expand Down Expand Up @@ -127,7 +130,14 @@ where
// Notify about elected next session finality committee
C::FinalityCommitteeManager::on_next_session_finality_committee(finalizers);

Some(producers)
// Prepare a list of all block authors for the next session. We shuffle to minimize
// the impact of slow producer on his followers.
let full_size = C::SessionPeriod::get() as usize;
let mut full_aura: Vec<_> = producers.into_iter().cycle().take(full_size).collect();
let mut rng = Pcg32::seed_from_u64(new_index as u64);
full_aura.shuffle(&mut rng);

Some(full_aura)
}

fn end_session(end_index: SessionIndex) {
Expand Down

0 comments on commit 35181e3

Please sign in to comment.