Comment Stakes::clone_with_epoch (#13388)

This commit is contained in:
Ryo Onodera 2020-11-04 20:18:05 +09:00 committed by GitHub
parent f349def5d4
commit b0d1ae1d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 22 deletions

View File

@ -29,16 +29,17 @@ impl Stakes {
pub fn history(&self) -> &StakeHistory { pub fn history(&self) -> &StakeHistory {
&self.stake_history &self.stake_history
} }
pub fn clone_with_epoch(&self, epoch: Epoch) -> Self { pub fn clone_with_epoch(&self, next_epoch: Epoch) -> Self {
if self.epoch == epoch { let prev_epoch = self.epoch;
if prev_epoch == next_epoch {
self.clone() self.clone()
} else { } else {
let mut stake_history = self.stake_history.clone(); // wrap up the prev epoch by adding new stake history entry for the prev epoch
let mut stake_history_upto_prev_epoch = self.stake_history.clone();
stake_history.add( stake_history_upto_prev_epoch.add(
self.epoch, prev_epoch,
new_stake_history_entry( new_stake_history_entry(
self.epoch, prev_epoch,
self.stake_delegations self.stake_delegations
.iter() .iter()
.map(|(_pubkey, stake_delegation)| stake_delegation), .map(|(_pubkey, stake_delegation)| stake_delegation),
@ -46,24 +47,31 @@ impl Stakes {
), ),
); );
// refresh the stake distribution of vote accounts for the next epoch, using new stake history
let vote_accounts_for_next_epoch = self
.vote_accounts
.iter()
.map(|(pubkey, (_stake, account))| {
(
*pubkey,
(
self.calculate_stake(
pubkey,
next_epoch,
Some(&stake_history_upto_prev_epoch),
),
account.clone(),
),
)
})
.collect();
Stakes { Stakes {
stake_delegations: self.stake_delegations.clone(), stake_delegations: self.stake_delegations.clone(),
unused: self.unused, unused: self.unused,
epoch, epoch: next_epoch,
vote_accounts: self stake_history: stake_history_upto_prev_epoch,
.vote_accounts vote_accounts: vote_accounts_for_next_epoch,
.iter()
.map(|(pubkey, (_stake, account))| {
(
*pubkey,
(
self.calculate_stake(pubkey, epoch, Some(&stake_history)),
account.clone(),
),
)
})
.collect(),
stake_history,
} }
} }
} }