Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Small code cleanup and typo fixes#13325

Merged
ryoqun merged 2 commits intosolana-labs:masterfrom
ryoqun:small-cleanup-type-inflation
Nov 2, 2020
Merged

Small code cleanup and typo fixes#13325
ryoqun merged 2 commits intosolana-labs:masterfrom
ryoqun:small-cleanup-type-inflation

Conversation

@ryoqun
Copy link
Copy Markdown
Contributor

@ryoqun ryoqun commented Oct 31, 2020

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

@ryoqun ryoqun added automerge Merge this Pull Request automatically once CI passes v1.3 labels Oct 31, 2020
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Oct 31, 2020

automerge label removed due to a CI failure

@mergify mergify Bot removed the automerge Merge this Pull Request automatically once CI passes label Oct 31, 2020
@mvines
Copy link
Copy Markdown
Contributor

mvines commented Oct 31, 2020

If you rebase this PR, it should get past cargo audit

@ryoqun ryoqun force-pushed the small-cleanup-type-inflation branch from 4a366d4 to 76a7f8f Compare November 1, 2020 05:44
Comment on lines -395 to -396
// if stakers do not claim before the epoch goes away they lose the
// credits...
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

claim-redeem is past thing.

pub const INITIAL_LOCKOUT: usize = 2;

// Maximum number of credits history to keep around
// smaller numbers makes
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  • incomplete comment
  • pub isn'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 {
Copy link
Copy Markdown
Contributor Author

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).

Comment on lines +418 to +419
let mut points = 0;
let mut new_credits_observed = self.credits_observed;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 (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))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 the final wording has more semantic significance than initial; usually end and start have equal balance. Otherwise, we have to change ABI to make first, last to happen...

@ryoqun
Copy link
Copy Markdown
Contributor Author

ryoqun commented Nov 1, 2020

@mvines Could you review this? Now enginner talks about artistic aesthetic. xD These should be fairly safu. but I just want some sanity check since I've added another cleaning: 76a7f8f

@ryoqun ryoqun requested a review from mvines November 1, 2020 06:23
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 1, 2020

Codecov Report

Merging #13325 into master will decrease coverage by 0.0%.
The diff coverage is 100.0%.

@@            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     

Copy link
Copy Markdown
Contributor

@mvines mvines left a comment

Choose a reason for hiding this comment

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

Nice readability improvement!

@ryoqun ryoqun merged commit 0e4509c into solana-labs:master Nov 2, 2020
mergify Bot pushed a commit that referenced this pull request Nov 2, 2020
* Small code cleanup and typo fixes

* Clean up calculate_points_and_credits

(cherry picked from commit 0e4509c)
mergify Bot pushed a commit that referenced this pull request Nov 2, 2020
* Small code cleanup and typo fixes

* Clean up calculate_points_and_credits

(cherry picked from commit 0e4509c)
mergify Bot added a commit that referenced this pull request Nov 2, 2020
* Small code cleanup and typo fixes

* Clean up calculate_points_and_credits

(cherry picked from commit 0e4509c)

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
mergify Bot added a commit that referenced this pull request Nov 2, 2020
* Small code cleanup and typo fixes

* Clean up calculate_points_and_credits

(cherry picked from commit 0e4509c)

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
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);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants