This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
distribute leftover lamports #7396
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1259,8 +1259,9 @@ impl Bank { | |
rent_to_be_distributed: u64, | ||
) { | ||
let mut total_staked = 0; | ||
let mut rent_distributed_in_initial_round = 0; | ||
|
||
let node_stakes = vote_account_hashmap | ||
let mut node_stakes = vote_account_hashmap | ||
.iter() | ||
.filter_map(|(_vote_pubkey, (staked, account))| { | ||
total_staked += *staked; | ||
|
@@ -1271,13 +1272,41 @@ impl Bank { | |
}) | ||
.collect::<Vec<(Pubkey, u64)>>(); | ||
|
||
node_stakes.iter().for_each(|(pubkey, staked)| { | ||
let rent_to_be_paid = | ||
(((staked * rent_to_be_distributed) as f64) / (total_staked as f64)) as u64; | ||
let mut account = self.get_account(pubkey).unwrap_or_default(); | ||
account.lamports += rent_to_be_paid; | ||
self.store_account(pubkey, &account); | ||
}); | ||
// Sort first by stake and then by pubkey for determinism | ||
node_stakes.sort_by( | ||
|(pubkey1, staked1), (pubkey2, staked2)| match staked2.cmp(staked1) { | ||
std::cmp::Ordering::Equal => pubkey2.cmp(pubkey1), | ||
other => other, | ||
}, | ||
); | ||
|
||
let node_stakes_and_rent = node_stakes | ||
.iter() | ||
.map(|(pubkey, staked)| { | ||
let rent_in_initial_round = | ||
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. rent_to_be_paid was just as good a name 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. could also be |
||
(((*staked * rent_to_be_distributed) as f64) / (total_staked as f64)) as u64; | ||
rent_distributed_in_initial_round += rent_in_initial_round; | ||
(*pubkey, *staked, rent_in_initial_round) | ||
}) | ||
.collect::<Vec<(Pubkey, u64, u64)>>(); | ||
|
||
// Leftover lamports after fraction calculation, will be paid to validators starting from highest stake | ||
// holder | ||
let mut leftover_lamports = rent_to_be_distributed - rent_distributed_in_initial_round; | ||
|
||
node_stakes_and_rent | ||
.iter() | ||
.for_each(|(pubkey, _staked, rent)| { | ||
let rent_to_be_paid = if leftover_lamports > 0 { | ||
leftover_lamports -= 1; | ||
*rent + 1 | ||
} else { | ||
*rent | ||
}; | ||
let mut account = self.get_account(pubkey).unwrap_or_default(); | ||
account.lamports += rent_to_be_paid; | ||
self.store_account(pubkey, &account); | ||
}); | ||
} | ||
|
||
fn distribute_rent(&self) { | ||
|
@@ -2321,19 +2350,20 @@ mod tests { | |
let rent_to_be_distributed = total_rent_deducted - burned_portion; | ||
|
||
let bootstrap_leader_portion = | ||
((bootstrap_leader_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64; | ||
((bootstrap_leader_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64 + 1; // Leftover lamport | ||
assert_eq!( | ||
bank.get_balance(&bootstrap_leader_pubkey), | ||
bootstrap_leader_portion + bootstrap_leader_initial_balance | ||
); | ||
|
||
let validator_1_portion = | ||
((validator_1_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64; | ||
((validator_1_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64 + 1; | ||
assert_eq!( | ||
bank.get_balance(&validator_1_pubkey), | ||
validator_1_portion + 42 | ||
); | ||
|
||
// No leftover lamport for you, as you have smallest of stake | ||
let validator_2_portion = | ||
((validator_2_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64; | ||
assert_eq!( | ||
|
@@ -2342,7 +2372,7 @@ mod tests { | |
); | ||
|
||
let validator_3_portion = | ||
((validator_3_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64; | ||
((validator_3_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64 + 1; | ||
assert_eq!( | ||
bank.get_balance(&validator_3_pubkey), | ||
validator_3_portion + 42 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this sort doesn't need to be stable, does it? you should be safe sorting merely by stake...
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.
@rob-solana If I sort by stake only, it could lead to indeterminism, as the order might not be guaranteed for node with same stake amount. Which could result in rare cases, different distribution of leftover lamports.
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.
ah, good point.
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.
are you testing for that?
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.
@rob-solana not specifically. Let me add a separate test scenario for this, just for my assurance.
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.
@rob-solana Added a scenario in test-case.