Skip to content
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

ForkStats: compute and wire up fork_weight #34623

Merged
merged 8 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ pub type PubkeyVotes = Vec<(Pubkey, Slot)>;
pub(crate) struct ComputedBankState {
pub voted_stakes: VotedStakes,
pub total_stake: Stake,
#[allow(dead_code)]
bank_weight: u128,
pub bank_stake: Stake,
// Tree of intervals of lockouts of the form [slot, slot + slot.lockout],
// keyed by end of the range
pub lockout_intervals: LockoutIntervals,
Expand Down Expand Up @@ -319,7 +318,7 @@ impl Tower {
let mut vote_slots = HashSet::new();
let mut voted_stakes = HashMap::new();
let mut total_stake = 0;
let mut bank_weight = 0;
let mut bank_stake = 0;
// Tree of intervals of lockouts of the form [slot, slot + slot.lockout],
// keyed by end of the range
let mut lockout_intervals = LockoutIntervals::new();
Expand Down Expand Up @@ -390,7 +389,7 @@ impl Tower {
process_slot_vote_unchecked(&mut vote_state, bank_slot);

for vote in &vote_state.votes {
bank_weight += vote.lockout.lockout() as u128 * voted_stake as u128;
bank_stake += voted_stake;
vote_slots.insert(vote.slot());
}

Expand All @@ -399,13 +398,12 @@ impl Tower {
let vote =
Lockout::new_with_confirmation_count(root, MAX_LOCKOUT_HISTORY as u32);
trace!("ROOT: {}", vote.slot());
bank_weight += vote.lockout() as u128 * voted_stake as u128;
vote_slots.insert(vote.slot());
}
}
if let Some(root) = vote_state.root_slot {
let vote = Lockout::new_with_confirmation_count(root, MAX_LOCKOUT_HISTORY as u32);
bank_weight += vote.lockout() as u128 * voted_stake as u128;
bank_stake += voted_stake;
vote_slots.insert(vote.slot());
}

Expand Down Expand Up @@ -440,7 +438,7 @@ impl Tower {
ComputedBankState {
voted_stakes,
total_stake,
bank_weight,
bank_stake,
lockout_intervals,
my_latest_landed_vote,
}
Expand Down Expand Up @@ -2271,7 +2269,6 @@ pub mod test {
let ComputedBankState {
voted_stakes,
total_stake,
bank_weight,
..
} = Tower::collect_vote_lockouts(
&Pubkey::default(),
Expand All @@ -2286,10 +2283,6 @@ pub mod test {
let mut new_votes = latest_validator_votes_for_frozen_banks.take_votes_dirty_set(0);
new_votes.sort();
assert_eq!(new_votes, account_latest_votes);

Copy link
Contributor Author

@HaoranYi HaoranYi Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

voted_stake*lock_out is no longer used for choosing the forks.
We no longer computes this. Removed the assertion in the tests.

// Each account has 1 vote in it. After simulating a vote in collect_vote_lockouts,
// the account will have 2 votes, with lockout 2 + 4 = 6. So expected weight for
assert_eq!(bank_weight, 12)
}

#[test]
Expand Down Expand Up @@ -2326,11 +2319,7 @@ pub mod test {
assert_eq!(tower.vote_state.root_slot, Some(0));
let mut latest_validator_votes_for_frozen_banks =
LatestValidatorVotesForFrozenBanks::default();
let ComputedBankState {
voted_stakes,
bank_weight,
..
} = Tower::collect_vote_lockouts(
let ComputedBankState { voted_stakes, .. } = Tower::collect_vote_lockouts(
&Pubkey::default(),
MAX_LOCKOUT_HISTORY as u64,
&accounts,
Expand Down
10 changes: 8 additions & 2 deletions core/src/consensus/progress_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ impl ForkProgress {

#[derive(Debug, Clone, Default)]
pub struct ForkStats {
pub weight: u128,
pub fork_weight: u128,
pub bank_stake: Stake,
pub total_stake: Stake,
pub block_height: u64,
pub has_voted: bool,
Expand All @@ -310,6 +309,13 @@ pub struct ForkStats {
pub my_latest_landed_vote: Option<Slot>,
}

impl ForkStats {
/// Return the percentage of bank_stake over total_stake.
pub fn bank_weight(&self) -> u64 {
100 * self.bank_stake / self.total_stake
}
}

#[derive(Clone, Default)]
pub struct PropagatedStats {
pub propagated_validators: HashSet<Pubkey>,
Expand Down
18 changes: 10 additions & 8 deletions core/src/replay_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3295,13 +3295,15 @@ impl ReplayStage {
let ComputedBankState {
voted_stakes,
total_stake,
bank_stake,
lockout_intervals,
my_latest_landed_vote,
..
} = computed_bank_state;
let stats = progress
.get_fork_stats_mut(bank_slot)
.expect("All frozen banks must exist in the Progress map");
stats.bank_stake = bank_stake;
stats.total_stake = total_stake;
stats.voted_stakes = voted_stakes;
stats.lockout_intervals = lockout_intervals;
Expand All @@ -3312,15 +3314,15 @@ impl ReplayStage {
datapoint_info!(
"bank_weight",
("slot", bank_slot, i64),
// u128 too large for influx, convert to hex
("weight", format!("{:X}", stats.weight), String),
("bank_stake", stats.bank_stake, i64),
("bank_weight", stats.bank_weight(), i64),
);

info!(
"{} slot_weight: {} {} {} {}",
"{} slot_weight: {} {:2}% {}%",
my_vote_pubkey,
bank_slot,
stats.weight,
stats.fork_weight,
stats.bank_weight(), // percentage bank stake in total_stake
bank.parent().map(|b| b.slot()).unwrap_or(0)
);
}
Expand Down Expand Up @@ -3664,7 +3666,7 @@ impl ReplayStage {
vote_threshold,
propagated_stake,
is_leader_slot,
fork_weight,
bank_weight,
total_threshold_stake,
total_epoch_stake,
) = {
Expand All @@ -3677,7 +3679,7 @@ impl ReplayStage {
fork_stats.vote_threshold,
propagated_stats.propagated_validators_stake,
propagated_stats.is_leader_slot,
fork_stats.weight,
fork_stats.bank_weight(),
fork_stats.total_stake,
propagated_stats.total_epoch_stake,
)
Expand Down Expand Up @@ -3712,7 +3714,7 @@ impl ReplayStage {
&& propagation_confirmed
&& switch_fork_decision.can_vote()
{
info!("voting: {} {}", candidate_vote_bank.slot(), fork_weight);
info!("voting: {} {}", candidate_vote_bank.slot(), bank_weight);
SelectVoteAndResetForkResult {
vote_bank: Some((candidate_vote_bank.clone(), switch_fork_decision)),
reset_bank: Some(candidate_vote_bank.clone()),
Expand Down
Loading