Small code cleanup and typo fixes#13325
Conversation
|
automerge label removed due to a CI failure |
|
If you rebase this PR, it should get past |
4a366d4 to
76a7f8f
Compare
| // if stakers do not claim before the epoch goes away they lose the | ||
| // credits... |
There was a problem hiding this comment.
claim-redeem is past thing.
| pub const INITIAL_LOCKOUT: usize = 2; | ||
|
|
||
| // Maximum number of credits history to keep around | ||
| // smaller numbers makes |
There was a problem hiding this comment.
- incomplete comment
pubisn't needed (at least for now)
| ) -> (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 { |
There was a problem hiding this comment.
Added comment, and swapped the condition operands to match with the comment and read fluently (IMO).
| let mut points = 0; | ||
| let mut new_credits_observed = self.credits_observed; |
There was a problem hiding this comment.
this is subtle, but let's match with the rhythm here: we're using (point, credits) tuple across this fn
There was a problem hiding this comment.
also, the intention of point (without new_) and new_credits_observed (with new_) is that point is isolated and independent for each inflation distribution. So, it's stateless. But credits_observed is continuously updated thing in stake accounts. So, it's state-full. So, only credits_observed can be said there is newer version and (current/older) version.
| let mut new_credits_observed = self.credits_observed; | ||
|
|
||
| for (epoch, final_epoch_credits, initial_epoch_credits) in | ||
| new_vote_state.epoch_credits().iter().copied() |
There was a problem hiding this comment.
copied() is to remove *s prefixed credits and prev_credits.
| new_credits_observed = new_credits_observed.max(final_epoch_credits); | ||
|
|
||
| // finally calculate points for this epoch | ||
| points += stake * earned_credits; |
There was a problem hiding this comment.
the main dish of this fn is put at the end, partly preparing for the next actual functional change pr.
There was a problem hiding this comment.
yeah, I know this is against https://github.com/solana-labs/solana/pull/13325/files#r515579041 (credits => points here) and the general rule of putting looping-state-related code (maintaining max of new_credits_observed while looping) at the end to avoid intermixed state inside loop body.
| 0 | ||
| }; | ||
|
|
||
| points += u128::from(self.delegation.stake(*epoch, stake_history)) |
There was a problem hiding this comment.
factor out u128::froms and stake calculation. let's make important definition (point calculation formula) shine by its own: https://github.com/solana-labs/solana/pull/13325/files#r515579738
| let mut points = 0; | ||
| let mut new_credits_observed = self.credits_observed; | ||
|
|
||
| for (epoch, final_epoch_credits, initial_epoch_credits) in |
There was a problem hiding this comment.
the naming of credits and prev_credits here was a bit unhelpful in this context, imo.
what's important here is that they're together forming an interval because we're calculating overlaps between it and self.credit_observed.
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). final_ and initial_ is chosen over last_/first_ and end_/start_:
- these epoch_credit interval's bounds are rather more rigid, not arbitrary, like they generally must not overlap with each other and properly ordered, etc. For example, this is not like lockout_intervals. So let's use somewhat formal-sounding wording here.
- it's less odd to see (final, initial) order than
(end, start)because thefinalwording has more semantic significance thaninitial; usuallyendandstarthave equal balance. Otherwise, we have to change ABI to makefirst, lastto happen...
Codecov Report
@@ Coverage Diff @@
## master #13325 +/- ##
=========================================
- Coverage 82.2% 82.2% -0.1%
=========================================
Files 375 375
Lines 87307 87310 +3
=========================================
+ Hits 71789 71791 +2
- Misses 15518 15519 +1 |
mvines
left a comment
There was a problem hiding this comment.
Nice readability improvement!
* Small code cleanup and typo fixes * Clean up calculate_points_and_credits (cherry picked from commit 0e4509c)
* Small code cleanup and typo fixes * Clean up calculate_points_and_credits (cherry picked from commit 0e4509c)
| 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); |
There was a problem hiding this comment.
Problem
There is some outdated code, typo, confusing variable names, spotted by me or audit report information suggestions.
Summary of Changes
Clarify them all. There should be absolutely no functional changes.
These code are extremely important. Let's squeeze out every part of noise and make it beautifully-coded.
Context #13233