* Output more inflation calc details in ledger-tool (#13345)
* Output more inflation calc details in ledger-tool
* Fix broken ci...
* Rename confusing variables
* Fix panic by wrapping PointValue with Opiton...
* Minor modifications
* Remove explict needless flush; Drop already does
* Yet another csv field adjustments
* Add data_size and rename epochs to earned_epochs
* Introduce null_tracer
* Unwrap Option in new_from_parent_with_tracer
* Don't shorten identifiers
* Allow irrefutable_let_patterns temporalily
* More null_tracer
* More field adjustments
(cherry picked from commit a81e7e7749
)
# Conflicts:
# runtime/src/bank.rs
* Fix conflict
Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
This commit is contained in:
@@ -30,6 +30,19 @@ pub enum StakeState {
|
||||
RewardsPool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InflationPointCalculationEvent {
|
||||
CalculatedPoints(u128, u128, u128),
|
||||
SplitRewards(u64, u64, u64, PointValue),
|
||||
RentExemptReserve(u64),
|
||||
Delegation(Delegation),
|
||||
Commission(u8),
|
||||
}
|
||||
|
||||
fn null_tracer() -> Option<impl FnMut(&InflationPointCalculationEvent)> {
|
||||
None::<fn(&_)>
|
||||
}
|
||||
|
||||
impl Default for StakeState {
|
||||
fn default() -> Self {
|
||||
StakeState::Uninitialized
|
||||
@@ -368,6 +381,7 @@ impl Authorized {
|
||||
/// and the total points over which those lamports
|
||||
/// are to be distributed
|
||||
// basically read as rewards/points, but in integers instead of as an f64
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PointValue {
|
||||
pub rewards: u64, // lamports to split
|
||||
pub points: u128, // over these points
|
||||
@@ -383,21 +397,28 @@ impl Stake {
|
||||
point_value: &PointValue,
|
||||
vote_state: &VoteState,
|
||||
stake_history: Option<&StakeHistory>,
|
||||
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
|
||||
) -> Option<(u64, u64)> {
|
||||
self.calculate_rewards(point_value, vote_state, stake_history)
|
||||
.map(|(stakers_reward, voters_reward, credits_observed)| {
|
||||
self.credits_observed = credits_observed;
|
||||
self.delegation.stake += stakers_reward;
|
||||
(stakers_reward, voters_reward)
|
||||
})
|
||||
self.calculate_rewards(
|
||||
point_value,
|
||||
vote_state,
|
||||
stake_history,
|
||||
inflation_point_calc_tracer,
|
||||
)
|
||||
.map(|(stakers_reward, voters_reward, credits_observed)| {
|
||||
self.credits_observed = credits_observed;
|
||||
self.delegation.stake += stakers_reward;
|
||||
(stakers_reward, voters_reward)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn calculate_points(
|
||||
&self,
|
||||
vote_state: &VoteState,
|
||||
stake_history: Option<&StakeHistory>,
|
||||
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
|
||||
) -> u128 {
|
||||
self.calculate_points_and_credits(vote_state, stake_history)
|
||||
self.calculate_points_and_credits(vote_state, stake_history, inflation_point_calc_tracer)
|
||||
.0
|
||||
}
|
||||
|
||||
@@ -408,6 +429,7 @@ impl Stake {
|
||||
&self,
|
||||
new_vote_state: &VoteState,
|
||||
stake_history: Option<&StakeHistory>,
|
||||
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
|
||||
) -> (u128, u64) {
|
||||
// if there is no newer credits since observed, return no point
|
||||
if new_vote_state.credits() <= self.credits_observed {
|
||||
@@ -442,6 +464,14 @@ impl Stake {
|
||||
|
||||
// finally calculate points for this epoch
|
||||
points += stake * earned_credits;
|
||||
|
||||
if let Some(inflation_point_calc_tracer) = inflation_point_calc_tracer {
|
||||
inflation_point_calc_tracer(&InflationPointCalculationEvent::CalculatedPoints(
|
||||
points,
|
||||
stake,
|
||||
earned_credits,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
(points, new_credits_observed)
|
||||
@@ -458,9 +488,13 @@ impl Stake {
|
||||
point_value: &PointValue,
|
||||
vote_state: &VoteState,
|
||||
stake_history: Option<&StakeHistory>,
|
||||
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
|
||||
) -> Option<(u64, u64, u64)> {
|
||||
let (points, credits_observed) =
|
||||
self.calculate_points_and_credits(vote_state, stake_history);
|
||||
let (points, credits_observed) = self.calculate_points_and_credits(
|
||||
vote_state,
|
||||
stake_history,
|
||||
inflation_point_calc_tracer,
|
||||
);
|
||||
|
||||
if points == 0 || point_value.points == 0 {
|
||||
return None;
|
||||
@@ -479,6 +513,14 @@ impl Stake {
|
||||
return None;
|
||||
}
|
||||
let (voter_rewards, staker_rewards, is_split) = vote_state.commission_split(rewards);
|
||||
if let Some(inflation_point_calc_tracer) = inflation_point_calc_tracer {
|
||||
inflation_point_calc_tracer(&InflationPointCalculationEvent::SplitRewards(
|
||||
rewards,
|
||||
voter_rewards,
|
||||
staker_rewards,
|
||||
(*point_value).clone(),
|
||||
));
|
||||
}
|
||||
|
||||
if (voter_rewards == 0 || staker_rewards == 0) && is_split {
|
||||
// don't collect if we lose a whole lamport somewhere
|
||||
@@ -971,14 +1013,26 @@ pub fn redeem_rewards(
|
||||
vote_account: &mut Account,
|
||||
point_value: &PointValue,
|
||||
stake_history: Option<&StakeHistory>,
|
||||
inflation_point_calc_tracer: &mut Option<impl FnMut(&InflationPointCalculationEvent)>,
|
||||
) -> Result<(u64, u64), InstructionError> {
|
||||
if let StakeState::Stake(meta, mut stake) = stake_account.state()? {
|
||||
let vote_state: VoteState =
|
||||
StateMut::<VoteStateVersions>::state(vote_account)?.convert_to_current();
|
||||
if let Some(inflation_point_calc_tracer) = inflation_point_calc_tracer {
|
||||
inflation_point_calc_tracer(&InflationPointCalculationEvent::RentExemptReserve(
|
||||
meta.rent_exempt_reserve,
|
||||
));
|
||||
inflation_point_calc_tracer(&InflationPointCalculationEvent::Commission(
|
||||
vote_state.commission,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some((stakers_reward, voters_reward)) =
|
||||
stake.redeem_rewards(point_value, &vote_state, stake_history)
|
||||
{
|
||||
if let Some((stakers_reward, voters_reward)) = stake.redeem_rewards(
|
||||
point_value,
|
||||
&vote_state,
|
||||
stake_history,
|
||||
inflation_point_calc_tracer,
|
||||
) {
|
||||
stake_account.lamports += stakers_reward;
|
||||
vote_account.lamports += voters_reward;
|
||||
|
||||
@@ -1003,7 +1057,7 @@ pub fn calculate_points(
|
||||
let vote_state: VoteState =
|
||||
StateMut::<VoteStateVersions>::state(vote_account)?.convert_to_current();
|
||||
|
||||
Ok(stake.calculate_points(&vote_state, stake_history))
|
||||
Ok(stake.calculate_points(&vote_state, stake_history, &mut null_tracer()))
|
||||
} else {
|
||||
Err(InstructionError::InvalidAccountData)
|
||||
}
|
||||
@@ -2439,7 +2493,8 @@ mod tests {
|
||||
points: 1
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2456,7 +2511,8 @@ mod tests {
|
||||
points: 1
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2490,7 +2546,8 @@ mod tests {
|
||||
points: 1
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2504,7 +2561,7 @@ mod tests {
|
||||
// no overflow on points
|
||||
assert_eq!(
|
||||
u128::from(stake.delegation.stake) * epoch_slots,
|
||||
stake.calculate_points(&vote_state, None)
|
||||
stake.calculate_points(&vote_state, None, &mut null_tracer())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2530,7 +2587,8 @@ mod tests {
|
||||
points: 1
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2547,7 +2605,8 @@ mod tests {
|
||||
points: 2 // all his
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2561,7 +2620,8 @@ mod tests {
|
||||
points: 1
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2578,7 +2638,8 @@ mod tests {
|
||||
points: 2
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2593,7 +2654,8 @@ mod tests {
|
||||
points: 2
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2614,7 +2676,8 @@ mod tests {
|
||||
points: 4
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2629,7 +2692,8 @@ mod tests {
|
||||
points: 4
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
vote_state.commission = 99;
|
||||
@@ -2641,7 +2705,8 @@ mod tests {
|
||||
points: 4
|
||||
},
|
||||
&vote_state,
|
||||
None
|
||||
None,
|
||||
&mut null_tracer(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user