* Small code cleanup and typo fixes
* Clean up calculate_points_and_credits
(cherry picked from commit 0e4509c497
)
Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
This commit is contained in:
@ -406,37 +406,45 @@ impl Stake {
|
|||||||
/// for credits_observed were the points paid
|
/// for credits_observed were the points paid
|
||||||
pub fn calculate_points_and_credits(
|
pub fn calculate_points_and_credits(
|
||||||
&self,
|
&self,
|
||||||
vote_state: &VoteState,
|
new_vote_state: &VoteState,
|
||||||
stake_history: Option<&StakeHistory>,
|
stake_history: Option<&StakeHistory>,
|
||||||
) -> (u128, u64) {
|
) -> (u128, u64) {
|
||||||
if self.credits_observed >= vote_state.credits() {
|
// if there is no newer credits since observed, return no point
|
||||||
|
if new_vote_state.credits() <= self.credits_observed {
|
||||||
return (0, 0);
|
return (0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut credits_observed = self.credits_observed;
|
let mut points = 0;
|
||||||
let mut points = 0u128;
|
let mut new_credits_observed = self.credits_observed;
|
||||||
for (epoch, credits, prev_credits) in vote_state.epoch_credits() {
|
|
||||||
|
for (epoch, final_epoch_credits, initial_epoch_credits) in
|
||||||
|
new_vote_state.epoch_credits().iter().copied()
|
||||||
|
{
|
||||||
|
let stake = u128::from(self.delegation.stake(epoch, stake_history));
|
||||||
|
|
||||||
// figure out how much this stake has seen that
|
// figure out how much this stake has seen that
|
||||||
// for which the vote account has a record
|
// for which the vote account has a record
|
||||||
let epoch_credits = if self.credits_observed < *prev_credits {
|
let earned_credits = if self.credits_observed < initial_epoch_credits {
|
||||||
// the staker observed the entire epoch
|
// the staker observed the entire epoch
|
||||||
credits - prev_credits
|
final_epoch_credits - initial_epoch_credits
|
||||||
} else if self.credits_observed < *credits {
|
} else if self.credits_observed < final_epoch_credits {
|
||||||
// the staker registered sometime during the epoch, partial credit
|
// the staker registered sometime during the epoch, partial credit
|
||||||
credits - credits_observed
|
final_epoch_credits - new_credits_observed
|
||||||
} else {
|
} else {
|
||||||
// the staker has already observed or been redeemed this epoch
|
// the staker has already observed or been redeemed this epoch
|
||||||
// or was activated after this epoch
|
// or was activated after this epoch
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
let earned_credits = u128::from(earned_credits);
|
||||||
points += u128::from(self.delegation.stake(*epoch, stake_history))
|
|
||||||
* u128::from(epoch_credits);
|
|
||||||
|
|
||||||
// don't want to assume anything about order of the iterator...
|
// don't want to assume anything about order of the iterator...
|
||||||
credits_observed = credits_observed.max(*credits);
|
new_credits_observed = new_credits_observed.max(final_epoch_credits);
|
||||||
|
|
||||||
|
// finally calculate points for this epoch
|
||||||
|
points += stake * earned_credits;
|
||||||
}
|
}
|
||||||
(points, credits_observed)
|
|
||||||
|
(points, new_credits_observed)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// for a given stake and vote_state, calculate what distributions and what updates should be made
|
/// for a given stake and vote_state, calculate what distributions and what updates should be made
|
||||||
|
@ -29,8 +29,7 @@ pub const MAX_LOCKOUT_HISTORY: usize = 31;
|
|||||||
pub const INITIAL_LOCKOUT: usize = 2;
|
pub const INITIAL_LOCKOUT: usize = 2;
|
||||||
|
|
||||||
// Maximum number of credits history to keep around
|
// Maximum number of credits history to keep around
|
||||||
// smaller numbers makes
|
const MAX_EPOCH_CREDITS_HISTORY: usize = 64;
|
||||||
pub const MAX_EPOCH_CREDITS_HISTORY: usize = 64;
|
|
||||||
|
|
||||||
#[frozen_abi(digest = "69hYtmmcuqPbhpc64ZaNJDidaUcg66CW6wzPFiuYZ3To")]
|
#[frozen_abi(digest = "69hYtmmcuqPbhpc64ZaNJDidaUcg66CW6wzPFiuYZ3To")]
|
||||||
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone, AbiExample)]
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone, AbiExample)]
|
||||||
@ -388,8 +387,7 @@ impl VoteState {
|
|||||||
self.epoch_credits.last_mut().unwrap().0 = epoch;
|
self.epoch_credits.last_mut().unwrap().0 = epoch;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if stakers do not claim before the epoch goes away they lose the
|
// Remove too old epoch_credits
|
||||||
// credits...
|
|
||||||
if self.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY {
|
if self.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY {
|
||||||
self.epoch_credits.remove(0);
|
self.epoch_credits.remove(0);
|
||||||
}
|
}
|
||||||
|
@ -1224,7 +1224,7 @@ impl Bank {
|
|||||||
let validator_rewards =
|
let validator_rewards =
|
||||||
(validator_rate * capitalization as f64 * epoch_duration_in_years) as u64;
|
(validator_rate * capitalization as f64 * epoch_duration_in_years) as u64;
|
||||||
|
|
||||||
let vote_balance_and_staked = self.stakes.read().unwrap().vote_balance_and_staked();
|
let old_vote_balance_and_staked = self.stakes.read().unwrap().vote_balance_and_staked();
|
||||||
|
|
||||||
let validator_point_value = self.pay_validator_rewards(validator_rewards);
|
let validator_point_value = self.pay_validator_rewards(validator_rewards);
|
||||||
|
|
||||||
@ -1241,8 +1241,8 @@ impl Bank {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let validator_rewards_paid =
|
let new_vote_balance_and_staked = self.stakes.read().unwrap().vote_balance_and_staked();
|
||||||
self.stakes.read().unwrap().vote_balance_and_staked() - vote_balance_and_staked;
|
let validator_rewards_paid = new_vote_balance_and_staked - old_vote_balance_and_staked;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
validator_rewards_paid,
|
validator_rewards_paid,
|
||||||
u64::try_from(
|
u64::try_from(
|
||||||
@ -5938,7 +5938,7 @@ mod tests {
|
|||||||
// The same reward should be distributed given same credits
|
// The same reward should be distributed given same credits
|
||||||
let expected_capitalization = do_test_bank_update_rewards_determinism();
|
let expected_capitalization = do_test_bank_update_rewards_determinism();
|
||||||
// Repeat somewhat large number of iterations to expose possible different behavior
|
// Repeat somewhat large number of iterations to expose possible different behavior
|
||||||
// depending on the randamly-seeded HashMap ordering
|
// depending on the randomly-seeded HashMap ordering
|
||||||
for _ in 0..30 {
|
for _ in 0..30 {
|
||||||
let actual_capitalization = do_test_bank_update_rewards_determinism();
|
let actual_capitalization = do_test_bank_update_rewards_determinism();
|
||||||
assert_eq!(actual_capitalization, expected_capitalization);
|
assert_eq!(actual_capitalization, expected_capitalization);
|
||||||
|
Reference in New Issue
Block a user