Make validator snapshot on new validator too (#2733)

pull/2736/head
Rongjian Lan 5 years ago committed by GitHub
parent 350b7a0bb4
commit f2524a888d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      core/blockchain.go
  2. 7
      core/offchain.go

@ -2430,7 +2430,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
// UpdateValidatorSnapshots updates the content snapshot of all validators // UpdateValidatorSnapshots updates the content snapshot of all validators
// Note: this should only be called within the blockchain insert process. // Note: this should only be called within the blockchain insert process.
func (bc *BlockChain) UpdateValidatorSnapshots( func (bc *BlockChain) UpdateValidatorSnapshots(
batch rawdb.DatabaseWriter, epoch *big.Int, state *state.DB, batch rawdb.DatabaseWriter, epoch *big.Int, state *state.DB, newValidators []common.Address,
) error { ) error {
// Note this is reading the validator list from last block. // Note this is reading the validator list from last block.
// It's fine since the new validators from this block is already snapshot when created. // It's fine since the new validators from this block is already snapshot when created.
@ -2439,6 +2439,7 @@ func (bc *BlockChain) UpdateValidatorSnapshots(
return err return err
} }
allValidators = append(allValidators, newValidators...)
// TODO: enable this once we allow validator to delete itself. // TODO: enable this once we allow validator to delete itself.
//err = bc.deleteValidatorSnapshots(allValidators) //err = bc.deleteValidatorSnapshots(allValidators)
//if err != nil { //if err != nil {
@ -2544,49 +2545,49 @@ func (bc *BlockChain) writeDelegationsByDelegator(
func (bc *BlockChain) UpdateStakingMetaData( func (bc *BlockChain) UpdateStakingMetaData(
batch rawdb.DatabaseWriter, txns staking.StakingTransactions, batch rawdb.DatabaseWriter, txns staking.StakingTransactions,
state *state.DB, epoch *big.Int, state *state.DB, epoch *big.Int,
) error { ) (newValidators []common.Address, err error) {
newValidators, newDelegations, err := bc.prepareStakingMetaData(txns, state) newValidators, newDelegations, err := bc.prepareStakingMetaData(txns, state)
if err != nil { if err != nil {
utils.Logger().Warn().Msgf("oops, prepareStakingMetaData failed, err: %+v", err) utils.Logger().Warn().Msgf("oops, prepareStakingMetaData failed, err: %+v", err)
return err return newValidators, err
} }
if len(newValidators) > 0 { if len(newValidators) > 0 {
list, err := bc.ReadValidatorList() list, err := bc.ReadValidatorList()
if err != nil { if err != nil {
return err return newValidators, err
} }
for _, addr := range newValidators { for _, addr := range newValidators {
newList, appended := utils.AppendIfMissing(list, addr) newList, appended := utils.AppendIfMissing(list, addr)
if !appended { if !appended {
return errValidatorExist return newValidators, errValidatorExist
} }
list = newList list = newList
// Update validator snapshot for the new validator // Update validator snapshot for the new validator
validator, err := state.ValidatorWrapper(addr) validator, err := state.ValidatorWrapper(addr)
if err != nil { if err != nil {
return err return newValidators, err
} }
if err := rawdb.WriteValidatorSnapshot(batch, validator, epoch); err != nil { if err := rawdb.WriteValidatorSnapshot(batch, validator, epoch); err != nil {
return err return newValidators, err
} }
} }
// Update validator list // Update validator list
if err = bc.WriteValidatorList(batch, list); err != nil { if err = bc.WriteValidatorList(batch, list); err != nil {
return err return newValidators, err
} }
} }
for addr, delegations := range newDelegations { for addr, delegations := range newDelegations {
if err := bc.writeDelegationsByDelegator(batch, addr, delegations); err != nil { if err := bc.writeDelegationsByDelegator(batch, addr, delegations); err != nil {
return err return newValidators, err
} }
} }
return nil return newValidators, nil
} }
// prepareStakingMetaData prepare the updates of validator's // prepareStakingMetaData prepare the updates of validator's

@ -93,9 +93,10 @@ func (bc *BlockChain) CommitOffChainData(
//} //}
// Do bookkeeping for new staking txns // Do bookkeeping for new staking txns
if err := bc.UpdateStakingMetaData( newVals, err := bc.UpdateStakingMetaData(
batch, block.StakingTransactions(), state, epoch, batch, block.StakingTransactions(), state, epoch,
); err != nil { )
if err != nil {
utils.Logger().Err(err).Msg("UpdateStakingMetaData failed") utils.Logger().Err(err).Msg("UpdateStakingMetaData failed")
return NonStatTy, err return NonStatTy, err
} }
@ -129,7 +130,7 @@ func (bc *BlockChain) CommitOffChainData(
if isBeaconChain && shard.Schedule.IsLastBlock(header.Number().Uint64()+1) { if isBeaconChain && shard.Schedule.IsLastBlock(header.Number().Uint64()+1) {
// Update snapshots for all validators // Update snapshots for all validators
epoch := new(big.Int).Add(header.Epoch(), common.Big1) epoch := new(big.Int).Add(header.Epoch(), common.Big1)
if err := bc.UpdateValidatorSnapshots(batch, epoch, state); err != nil { if err := bc.UpdateValidatorSnapshots(batch, epoch, state, newVals); err != nil {
return NonStatTy, err return NonStatTy, err
} }
} }

Loading…
Cancel
Save