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

distribute leftover lamports #7396

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Copy link
Contributor

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

Copy link
Contributor Author

@ParthDesai ParthDesai Dec 10, 2019

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, good point.

Copy link
Contributor

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?

Copy link
Contributor Author

@ParthDesai ParthDesai Dec 10, 2019

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.

Copy link
Contributor Author

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.

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 =
Copy link
Contributor

Choose a reason for hiding this comment

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

rent_to_be_paid was just as good a name

Copy link
Contributor

Choose a reason for hiding this comment

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

could also be rent_share

(((*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) {
Expand Down Expand Up @@ -2173,7 +2202,7 @@ mod tests {
let rent = Rent::free();

let validator_1_pubkey = Pubkey::new_rand();
let validator_1_stake_lamports = 23;
let validator_1_stake_lamports = 20;
let validator_1_staking_keypair = Keypair::new();
let validator_1_voting_keypair = Keypair::new();

Expand Down Expand Up @@ -2206,7 +2235,7 @@ mod tests {
);

let validator_2_pubkey = Pubkey::new_rand();
let validator_2_stake_lamports = 22;
let validator_2_stake_lamports = 20;
let validator_2_staking_keypair = Keypair::new();
let validator_2_voting_keypair = Keypair::new();

Expand Down Expand Up @@ -2239,7 +2268,7 @@ mod tests {
);

let validator_3_pubkey = Pubkey::new_rand();
let validator_3_stake_lamports = 25;
let validator_3_stake_lamports = 30;
let validator_3_staking_keypair = Keypair::new();
let validator_3_voting_keypair = Keypair::new();

Expand Down Expand Up @@ -2321,28 +2350,38 @@ 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 =
// Since, validator 1 and validator 2 has equal smallest stake, it comes down to comparison
// between their pubkey.
let mut validator_1_portion =
((validator_1_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64;
if validator_1_pubkey > validator_2_pubkey {
validator_1_portion += 1;
}
assert_eq!(
bank.get_balance(&validator_1_pubkey),
validator_1_portion + 42
);

let validator_2_portion =
// Since, validator 1 and validator 2 has equal smallest stake, it comes down to comparison
// between their pubkey.
let mut validator_2_portion =
((validator_2_stake_lamports * rent_to_be_distributed) as f64 / 100.0) as u64;
if validator_2_pubkey > validator_1_pubkey {
validator_2_portion += 1;
}
assert_eq!(
bank.get_balance(&validator_2_pubkey),
validator_2_portion + 42
);

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
Expand Down