-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Small code cleanup and typo fixes #13325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -407,37 +407,45 @@ impl Stake { | |
| /// for credits_observed were the points paid | ||
| pub fn calculate_points_and_credits( | ||
| &self, | ||
| vote_state: &VoteState, | ||
| new_vote_state: &VoteState, | ||
| stake_history: Option<&StakeHistory>, | ||
| ) -> (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); | ||
|
Contributor
Author
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. |
||
| } | ||
|
|
||
| let mut credits_observed = self.credits_observed; | ||
| let mut points = 0u128; | ||
| for (epoch, credits, prev_credits) in vote_state.epoch_credits() { | ||
| let mut points = 0; | ||
| let mut new_credits_observed = self.credits_observed; | ||
|
Comment on lines
+418
to
+419
Contributor
Author
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. this is subtle, but let's match with the rhythm here: we're using (point, credits) tuple across this fn
Contributor
Author
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. also, the intention of |
||
|
|
||
| for (epoch, final_epoch_credits, initial_epoch_credits) in | ||
|
Contributor
Author
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. the naming of what's important here is that they're together forming an interval because we're calculating overlaps between it and So, I prefixed both of them with prefixes. (they used symmetrically in the logic but the one has a prefix and the other not; that's another point of confution, imo).
|
||
| new_vote_state.epoch_credits().iter().copied() | ||
|
Contributor
Author
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.
|
||
| { | ||
| let stake = u128::from(self.delegation.stake(epoch, stake_history)); | ||
|
|
||
| // figure out how much this stake has seen that | ||
| // 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 | ||
| credits - prev_credits | ||
| } else if self.credits_observed < *credits { | ||
| final_epoch_credits - initial_epoch_credits | ||
| } else if self.credits_observed < final_epoch_credits { | ||
| // the staker registered sometime during the epoch, partial credit | ||
| credits - credits_observed | ||
| final_epoch_credits - new_credits_observed | ||
| } else { | ||
| // the staker has already observed or been redeemed this epoch | ||
| // or was activated after this epoch | ||
| 0 | ||
| }; | ||
|
|
||
| points += u128::from(self.delegation.stake(*epoch, stake_history)) | ||
|
Contributor
Author
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. factor out |
||
| * u128::from(epoch_credits); | ||
| let earned_credits = u128::from(earned_credits); | ||
|
|
||
| // 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; | ||
|
Contributor
Author
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. the main dish of this fn is put at the end, partly preparing for the next actual functional change pr.
Contributor
Author
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. yeah, I know this is against https://github.com/solana-labs/solana/pull/13325/files#r515579041 ( |
||
| } | ||
| (points, credits_observed) | ||
|
|
||
| (points, new_credits_observed) | ||
| } | ||
|
|
||
| /// for a given stake and vote_state, calculate what distributions and what updates should be made | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,7 @@ pub const MAX_LOCKOUT_HISTORY: usize = 31; | |
| pub const INITIAL_LOCKOUT: usize = 2; | ||
|
|
||
| // Maximum number of credits history to keep around | ||
| // smaller numbers makes | ||
|
Contributor
Author
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.
|
||
| pub const MAX_EPOCH_CREDITS_HISTORY: usize = 64; | ||
| const MAX_EPOCH_CREDITS_HISTORY: usize = 64; | ||
|
|
||
| #[frozen_abi(digest = "Ch2vVEwos2EjAVqSHCyJjnN2MNX1yrpapZTGhMSCjWUH")] | ||
| #[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone, AbiExample)] | ||
|
|
@@ -392,8 +391,7 @@ impl VoteState { | |
| self.epoch_credits.last_mut().unwrap().0 = epoch; | ||
| } | ||
|
|
||
| // if stakers do not claim before the epoch goes away they lose the | ||
| // credits... | ||
|
Comment on lines
-395
to
-396
Contributor
Author
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.
|
||
| // Remove too old epoch_credits | ||
| if self.epoch_credits.len() > MAX_EPOCH_CREDITS_HISTORY { | ||
| self.epoch_credits.remove(0); | ||
| } | ||
|
|
||
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.
Added comment, and swapped the condition operands to match with the comment and read fluently (IMO).