Skip to content

Commit

Permalink
do snapshot for validators at last block of epoch (harmony-one#2736)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlan35 authored Apr 6, 2020
1 parent f2524a8 commit 66f26e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ func (bc *BlockChain) writeDelegationsByDelegator(
// Note: this should only be called within the blockchain insert process.
func (bc *BlockChain) UpdateStakingMetaData(
batch rawdb.DatabaseWriter, txns staking.StakingTransactions,
state *state.DB, epoch *big.Int,
state *state.DB, epoch *big.Int, isNewEpoch bool,
) (newValidators []common.Address, err error) {
newValidators, newDelegations, err := bc.prepareStakingMetaData(txns, state)
if err != nil {
Expand Down Expand Up @@ -2574,6 +2574,14 @@ func (bc *BlockChain) UpdateStakingMetaData(
if err := rawdb.WriteValidatorSnapshot(batch, validator, epoch); err != nil {
return newValidators, err
}
// For validator created at exactly the last block of an epoch, we should create the snapshot
// for next epoch too.
if isNewEpoch {
newEpoch := new(big.Int).Add(epoch, common.Big1)
if err := rawdb.WriteValidatorSnapshot(batch, validator, newEpoch); err != nil {
return newValidators, err
}
}
}

// Update validator list
Expand Down
2 changes: 1 addition & 1 deletion core/offchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (bc *BlockChain) CommitOffChainData(

// Do bookkeeping for new staking txns
newVals, err := bc.UpdateStakingMetaData(
batch, block.StakingTransactions(), state, epoch,
batch, block.StakingTransactions(), state, epoch, isNewEpoch,
)
if err != nil {
utils.Logger().Err(err).Msg("UpdateStakingMetaData failed")
Expand Down

0 comments on commit 66f26e8

Please sign in to comment.