Comment Stakes::clone_with_epoch (#13388) (#13389)

(cherry picked from commit b0d1ae1d8b)

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
This commit is contained in:
mergify[bot]
2020-11-04 13:49:48 +00:00
committed by GitHub
parent 64c76c2f4b
commit f697a86d1e

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 {
), ),
); );
Stakes { // refresh the stake distribution of vote accounts for the next epoch, using new stake history
stake_delegations: self.stake_delegations.clone(), let vote_accounts_for_next_epoch = self
unused: self.unused,
epoch,
vote_accounts: self
.vote_accounts .vote_accounts
.iter() .iter()
.map(|(pubkey, (_stake, account))| { .map(|(pubkey, (_stake, account))| {
( (
*pubkey, *pubkey,
( (
self.calculate_stake(pubkey, epoch, Some(&stake_history)), self.calculate_stake(
pubkey,
next_epoch,
Some(&stake_history_upto_prev_epoch),
),
account.clone(), account.clone(),
), ),
) )
}) })
.collect(), .collect();
stake_history,
Stakes {
stake_delegations: self.stake_delegations.clone(),
unused: self.unused,
epoch: next_epoch,
stake_history: stake_history_upto_prev_epoch,
vote_accounts: vote_accounts_for_next_epoch,
} }
} }
} }