-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(trimming): implement pre-trimming #687
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
daa210f
refactor(trimming): implement pre-trimming
niklasad1 729fa5a
cargo fmt
niklasad1 130d558
cargo fmt
niklasad1 3b7e090
update to polkadot-sdk
niklasad1 4987cbc
cleanup the pre-trimming code
niklasad1 57513ef
remove checking solution score
niklasad1 e56c755
update polkadot-sdk
niklasad1 235c42c
cleaup
niklasad1 871593a
Update src/epm.rs
niklasad1 1130dfc
add promethous counter for trimming
niklasad1 274e7f2
Merge remote-tracking branch 'origin/main' into na-refactor-trimming
niklasad1 81608fb
cargo fmt
niklasad1 3fd404d
fix nits
niklasad1 3322085
pre-trimming: impl bsearch to find pre-trim
niklasad1 aee2163
fix nit in weight trimmming
niklasad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,24 +21,34 @@ use crate::{ | |
helpers::{storage_at, RuntimeDispatchInfo}, | ||
opt::{BalanceIterations, Balancing, Solver}, | ||
prelude::*, | ||
static_types, | ||
prometheus, | ||
static_types::{self}, | ||
}; | ||
|
||
use std::{ | ||
collections::{BTreeMap, BTreeSet}, | ||
marker::PhantomData, | ||
}; | ||
|
||
use codec::{Decode, Encode}; | ||
use frame_election_provider_support::{NposSolution, PhragMMS, SequentialPhragmen}; | ||
use frame_support::weights::Weight; | ||
use pallet_election_provider_multi_phase::{RawSolution, ReadySolution, SolutionOrSnapshotSize}; | ||
use frame_election_provider_support::{Get, NposSolution, PhragMMS, SequentialPhragmen}; | ||
use frame_support::{weights::Weight, BoundedVec}; | ||
use pallet_election_provider_multi_phase::{ | ||
unsigned::TrimmingStatus, RawSolution, ReadySolution, SolutionOf, SolutionOrSnapshotSize, | ||
}; | ||
use scale_info::{PortableRegistry, TypeInfo}; | ||
use scale_value::scale::{decode_as_type, TypeId}; | ||
use sp_core::Bytes; | ||
use sp_npos_elections::ElectionScore; | ||
use sp_npos_elections::{ElectionScore, VoteWeight}; | ||
use subxt::{dynamic::Value, rpc::rpc_params, tx::DynamicPayload}; | ||
|
||
const EPM_PALLET_NAME: &str = "ElectionProviderMultiPhase"; | ||
|
||
type MinerVoterOf = | ||
frame_election_provider_support::Voter<AccountId, crate::static_types::MaxVotesPerVoter>; | ||
|
||
type RoundSnapshot = pallet_election_provider_multi_phase::RoundSnapshot<AccountId, MinerVoterOf>; | ||
type Voters = | ||
Vec<(AccountId, VoteWeight, BoundedVec<AccountId, crate::static_types::MaxVotesPerVoter>)>; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
struct EpmConstant { | ||
|
@@ -62,6 +72,113 @@ impl std::fmt::Display for EpmConstant { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct State { | ||
voters: Voters, | ||
voters_by_stake: BTreeMap<VoteWeight, usize>, | ||
} | ||
|
||
impl State { | ||
fn len(&self) -> usize { | ||
self.voters_by_stake.len() | ||
} | ||
|
||
fn to_voters(&self) -> Voters { | ||
self.voters.clone() | ||
} | ||
} | ||
|
||
/// Represent voters that may be trimmed | ||
/// | ||
/// The trimming works by removing the voter with the least amount of stake. | ||
/// | ||
/// It's using an internal `BTreeMap` to determine which voter to remove next | ||
/// and the voters Vec can't be sorted because the EPM pallet will index into it | ||
/// when checking the solution. | ||
#[derive(Debug)] | ||
pub struct TrimmedVoters<T> { | ||
state: State, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> TrimmedVoters<T> | ||
where | ||
T: MinerConfig<AccountId = AccountId, MaxVotesPerVoter = static_types::MaxVotesPerVoter> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
T::Solution: Send, | ||
{ | ||
/// Create a new `TrimmedVotes`. | ||
pub async fn new(mut voters: Voters, desired_targets: u32) -> Result<Self, Error> { | ||
let mut voters_by_stake = BTreeMap::new(); | ||
let mut targets = BTreeSet::new(); | ||
|
||
for (idx, (_voter, stake, supports)) in voters.iter().enumerate() { | ||
voters_by_stake.insert(*stake, idx); | ||
targets.extend(supports.iter().cloned()); | ||
} | ||
|
||
loop { | ||
let targets_len = targets.len() as u32; | ||
let active_voters = voters_by_stake.len() as u32; | ||
|
||
let est_weight: Weight = tokio::task::spawn_blocking(move || { | ||
T::solution_weight(active_voters, targets_len, active_voters, desired_targets) | ||
}) | ||
.await?; | ||
|
||
let max_weight: Weight = T::MaxWeight::get(); | ||
log::trace!(target: "staking-miner", "trimming weight: est_weight={est_weight} / max_weight={max_weight}"); | ||
|
||
if est_weight.all_lt(max_weight) { | ||
return Ok(Self { state: State { voters, voters_by_stake }, _marker: PhantomData }) | ||
} | ||
|
||
let Some((_, idx)) = voters_by_stake.pop_first() else { break }; | ||
|
||
let rm = voters[idx].0.clone(); | ||
|
||
// Remove votes for an account. | ||
for (_voter, _stake, supports) in &mut voters { | ||
supports.retain(|a| a != &rm); | ||
} | ||
|
||
targets.remove(&rm); | ||
} | ||
|
||
return Err(Error::Feasibility("Failed to pre-trim weight < T::MaxLength".to_string())) | ||
} | ||
|
||
/// Clone the state and trim it, so it get can be reverted. | ||
pub fn trim(&mut self, n: usize) -> Result<State, Error> { | ||
let mut voters = self.state.voters.clone(); | ||
let mut voters_by_stake = self.state.voters_by_stake.clone(); | ||
|
||
for _ in 0..n { | ||
let Some((_, idx)) = voters_by_stake.pop_first() else { | ||
return Err(Error::Feasibility("Failed to pre-trim len".to_string())) | ||
}; | ||
let rm = voters[idx].0.clone(); | ||
|
||
// Remove votes for an account. | ||
for (_voter, _stake, supports) in &mut voters { | ||
supports.retain(|a| a != &rm); | ||
} | ||
} | ||
|
||
Ok(State { voters, voters_by_stake }) | ||
} | ||
|
||
pub fn to_voters(&self) -> Voters { | ||
self.state.voters.clone() | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.state.len() | ||
} | ||
} | ||
|
||
/// Read the constants from the metadata and updates the static types. | ||
pub(crate) async fn update_metadata_constants(api: &SubxtClient) -> Result<(), Error> { | ||
const SIGNED_MAX_WEIGHT: EpmConstant = EpmConstant::new("SignedMaxWeight"); | ||
|
@@ -186,8 +303,44 @@ pub async fn snapshot_at( | |
} | ||
} | ||
|
||
/// Helper to fetch snapshot data via RPC | ||
pub async fn mine_solution<T>( | ||
solver: Solver, | ||
targets: Vec<AccountId>, | ||
voters: Voters, | ||
desired_targets: u32, | ||
) -> Result<(SolutionOf<T>, ElectionScore, SolutionOrSnapshotSize, TrimmingStatus), Error> | ||
where | ||
T: MinerConfig<AccountId = AccountId, MaxVotesPerVoter = static_types::MaxVotesPerVoter> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
T::Solution: Send, | ||
{ | ||
match tokio::task::spawn_blocking(move || match solver { | ||
Solver::SeqPhragmen { iterations } => { | ||
BalanceIterations::set(iterations); | ||
Miner::<T>::mine_solution_with_snapshot::< | ||
SequentialPhragmen<AccountId, Accuracy, Balancing>, | ||
>(voters, targets, desired_targets) | ||
}, | ||
Solver::PhragMMS { iterations } => { | ||
BalanceIterations::set(iterations); | ||
Miner::<T>::mine_solution_with_snapshot::<PhragMMS<AccountId, Accuracy, Balancing>>( | ||
voters, | ||
targets, | ||
desired_targets, | ||
) | ||
}, | ||
}) | ||
.await | ||
{ | ||
Ok(Ok(s)) => Ok(s), | ||
Err(e) => Err(e.into()), | ||
Ok(Err(e)) => Err(Error::Other(format!("{:?}", e))), | ||
} | ||
} | ||
|
||
/// Helper to fetch snapshot data via RPC | ||
/// and compute an NPos solution via [`pallet_election_provider_multi_phase`]. | ||
pub async fn fetch_snapshot_and_mine_solution<T>( | ||
api: &SubxtClient, | ||
|
@@ -219,47 +372,69 @@ where | |
.await? | ||
.map(|score| score.0); | ||
|
||
let voters = snapshot.voters.clone(); | ||
let targets = snapshot.targets.clone(); | ||
let mut voters = TrimmedVoters::<T>::new(snapshot.voters.clone(), desired_targets).await?; | ||
|
||
log::trace!( | ||
target: LOG_TARGET, | ||
"mine solution: desired_targets={}, voters={}, targets={}", | ||
let (solution, score, solution_or_snapshot_size, trim_status) = mine_solution::<T>( | ||
solver.clone(), | ||
snapshot.targets.clone(), | ||
voters.to_voters(), | ||
desired_targets, | ||
voters.len(), | ||
targets.len() | ||
); | ||
) | ||
.await?; | ||
|
||
let blocking_task = tokio::task::spawn_blocking(move || match solver { | ||
Solver::SeqPhragmen { iterations } => { | ||
BalanceIterations::set(iterations); | ||
Miner::<T>::mine_solution_with_snapshot::< | ||
SequentialPhragmen<AccountId, Accuracy, Balancing>, | ||
>(voters, targets, desired_targets) | ||
}, | ||
Solver::PhragMMS { iterations } => { | ||
BalanceIterations::set(iterations); | ||
Miner::<T>::mine_solution_with_snapshot::<PhragMMS<AccountId, Accuracy, Balancing>>( | ||
voters, | ||
targets, | ||
desired_targets, | ||
) | ||
}, | ||
}) | ||
.await; | ||
if !trim_status.is_trimmed() { | ||
return Ok(MinedSolution { | ||
round, | ||
desired_targets, | ||
snapshot, | ||
minimum_untrusted_score, | ||
solution, | ||
score, | ||
solution_or_snapshot_size, | ||
}) | ||
} | ||
|
||
prometheus::on_trim_attempt(); | ||
|
||
let mut l = 1; | ||
let mut h = voters.len(); | ||
let mut best_solution = None; | ||
|
||
while l <= h { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. binary search to find the best "pre-trim" |
||
let mid = ((h - l) / 2) + l; | ||
|
||
let next_state = voters.trim(mid)?; | ||
|
||
match blocking_task { | ||
Ok(Ok((solution, score, solution_or_snapshot_size))) => Ok(MinedSolution { | ||
let (solution, score, solution_or_snapshot_size, trim_status) = mine_solution::<T>( | ||
solver.clone(), | ||
snapshot.targets.clone(), | ||
next_state.to_voters(), | ||
desired_targets, | ||
) | ||
.await?; | ||
|
||
if !trim_status.is_trimmed() { | ||
best_solution = Some((solution, score, solution_or_snapshot_size)); | ||
h = mid - 1; | ||
} else { | ||
l = mid + 1; | ||
} | ||
} | ||
|
||
if let Some((solution, score, solution_or_snapshot_size)) = best_solution { | ||
prometheus::on_trim_success(); | ||
|
||
Ok(MinedSolution { | ||
round, | ||
desired_targets, | ||
snapshot, | ||
minimum_untrusted_score, | ||
solution, | ||
score, | ||
solution_or_snapshot_size, | ||
}), | ||
Ok(Err(err)) => Err(Error::Other(format!("{:?}", err))), | ||
Err(err) => Err(err.into()), | ||
}) | ||
} else { | ||
Err(Error::Feasibility("Failed pre-trim length".to_string())) | ||
} | ||
} | ||
|
||
|
@@ -315,6 +490,16 @@ where | |
} | ||
} | ||
|
||
impl<T: MinerConfig> std::fmt::Debug for MinedSolution<T> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("MinedSolution") | ||
.field("round", &self.round) | ||
.field("desired_targets", &self.desired_targets) | ||
.field("score", &self.score) | ||
.finish() | ||
} | ||
} | ||
|
||
fn make_type<T: scale_info::TypeInfo + 'static>() -> (TypeId, PortableRegistry) { | ||
let m = scale_info::MetaType::new::<T>(); | ||
let mut types = scale_info::Registry::new(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decided to add this in the constructor/new because it should only be needed to do once