Refactor distribute_rent_to_validators() for clarity
This commit is contained in:
parent
90c9462dd4
commit
bb92184085
|
@ -1513,42 +1513,48 @@ impl Bank {
|
||||||
let mut total_staked = 0;
|
let mut total_staked = 0;
|
||||||
let mut rent_distributed_in_initial_round = 0;
|
let mut rent_distributed_in_initial_round = 0;
|
||||||
|
|
||||||
let mut node_stakes = vote_account_hashmap
|
// Collect the stake associated with each validator.
|
||||||
|
// Note that a validator may be present in this vector multiple times if it happens to have
|
||||||
|
// more than one staked vote account somehow
|
||||||
|
let mut validator_stakes = vote_account_hashmap
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(_vote_pubkey, (staked, account))| {
|
.filter_map(|(_vote_pubkey, (staked, account))| {
|
||||||
total_staked += *staked;
|
if *staked == 0 {
|
||||||
VoteState::deserialize(&account.data)
|
None
|
||||||
.ok()
|
} else {
|
||||||
.map(|vote_state| (vote_state.node_pubkey, *staked))
|
total_staked += *staked;
|
||||||
.filter(|(_pubkey, staked)| *staked != 0)
|
VoteState::deserialize(&account.data)
|
||||||
|
.ok()
|
||||||
|
.map(|vote_state| (vote_state.node_pubkey, *staked))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<(Pubkey, u64)>>();
|
.collect::<Vec<(Pubkey, u64)>>();
|
||||||
|
|
||||||
// Sort first by stake and then by pubkey for determinism
|
// Sort first by stake and then by validator identity pubkey for determinism
|
||||||
node_stakes.sort_by(
|
validator_stakes.sort_by(|(pubkey1, staked1), (pubkey2, staked2)| {
|
||||||
|(pubkey1, staked1), (pubkey2, staked2)| match staked2.cmp(staked1) {
|
match staked2.cmp(staked1) {
|
||||||
std::cmp::Ordering::Equal => pubkey2.cmp(pubkey1),
|
std::cmp::Ordering::Equal => pubkey2.cmp(pubkey1),
|
||||||
other => other,
|
other => other,
|
||||||
},
|
}
|
||||||
);
|
});
|
||||||
|
|
||||||
let node_stakes_and_rent = node_stakes
|
let validator_rent_shares = validator_stakes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(pubkey, staked)| {
|
.map(|(pubkey, staked)| {
|
||||||
let rent_share =
|
let rent_share =
|
||||||
(((*staked * rent_to_be_distributed) as f64) / (total_staked as f64)) as u64;
|
(((*staked * rent_to_be_distributed) as f64) / (total_staked as f64)) as u64;
|
||||||
rent_distributed_in_initial_round += rent_share;
|
rent_distributed_in_initial_round += rent_share;
|
||||||
(*pubkey, *staked, rent_share)
|
(*pubkey, rent_share)
|
||||||
})
|
})
|
||||||
.collect::<Vec<(Pubkey, u64, u64)>>();
|
.collect::<Vec<(Pubkey, u64)>>();
|
||||||
|
|
||||||
// Leftover lamports after fraction calculation, will be paid to validators starting from highest stake
|
// Leftover lamports after fraction calculation, will be paid to validators starting from highest stake
|
||||||
// holder
|
// holder
|
||||||
let mut leftover_lamports = rent_to_be_distributed - rent_distributed_in_initial_round;
|
let mut leftover_lamports = rent_to_be_distributed - rent_distributed_in_initial_round;
|
||||||
|
|
||||||
node_stakes_and_rent
|
validator_rent_shares
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|(pubkey, _staked, rent_share)| {
|
.for_each(|(pubkey, rent_share)| {
|
||||||
let rent_to_be_paid = if leftover_lamports > 0 {
|
let rent_to_be_paid = if leftover_lamports > 0 {
|
||||||
leftover_lamports -= 1;
|
leftover_lamports -= 1;
|
||||||
*rent_share + 1
|
*rent_share + 1
|
||||||
|
|
Loading…
Reference in New Issue