Skip to content

Commit

Permalink
Revert "fix earned-reward by writing the stats only once, remove from…
Browse files Browse the repository at this point in the history
… UpdateValdiatorVotingPower (#2737)" (#2745)

This reverts commit dc036e6.
  • Loading branch information
rlan35 authored Apr 6, 2020
1 parent 2c114f8 commit 5b2e041
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
23 changes: 14 additions & 9 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2287,9 +2287,9 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
block *types.Block,
newEpochSuperCommittee, currentEpochSuperCommittee *shard.State,
state *state.DB,
) (map[common.Address]*staking.ValidatorStats, error) {
) error {
if newEpochSuperCommittee == nil {
return nil, shard.ErrSuperCommitteeNil
return shard.ErrSuperCommitteeNil
}

rosters, bootedFromSuperCommittee :=
Expand Down Expand Up @@ -2319,7 +2319,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
for i := range newEpochSuperCommittee.Shards {
subCommittee := &newEpochSuperCommittee.Shards[i]
if newEpochSuperCommittee.Epoch == nil {
return nil, errors.Wrapf(
return errors.Wrapf(
errNilEpoch,
"block epoch %v current-committee-epoch %v",
block.Epoch(),
Expand All @@ -2328,13 +2328,13 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
}
roster, err := votepower.Compute(subCommittee, newEpochSuperCommittee.Epoch)
if err != nil {
return nil, err
return err
}
rosters[i] = roster
}

validatorStats := map[common.Address]*staking.ValidatorStats{}
networkWide := votepower.AggregateRosters(rosters)

for key, value := range networkWide {
stats, err := rawdb.ReadValidatorStats(bc.db, key)
if err != nil {
Expand All @@ -2358,7 +2358,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
if currentEpochSuperCommittee.Epoch != nil {
wrapper, err := state.ValidatorWrapper(key)
if err != nil {
return nil, err
return err
}

aprComputed, err := apr.ComputeForValidator(
Expand All @@ -2382,7 +2382,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
)

if err != nil {
return nil, err
return err
}

computed := availability.ComputeCurrentSigning(snapshot, wrapper)
Expand All @@ -2399,10 +2399,15 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
stats.BootedStatus = effective.BannedForDoubleSigning
}
}
validatorStats[key] = stats

if err := rawdb.WriteValidatorStats(
batch, key, stats,
); err != nil {
return err
}
}

return validatorStats, nil
return nil
}

// deleteValidatorSnapshots deletes the snapshot staking information of given validator address
Expand Down
44 changes: 21 additions & 23 deletions core/offchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ func (bc *BlockChain) CommitOffChainData(
}
}

// Update voting power of validators for all shards
if isNewEpoch && isBeaconChain {
currentSuperCommittee, _ := bc.ReadShardState(bc.CurrentHeader().Epoch())
if shardState, err := shard.DecodeWrapper(
header.ShardState(),
); err == nil {
if err := bc.UpdateValidatorVotingPower(
batch, block, shardState, currentSuperCommittee, state,
); err != nil {
utils.Logger().
Err(err).
Msg("[UpdateValidatorVotingPower] Failed to update voting power")
}
} else {
utils.Logger().
Err(err).
Msg("[UpdateValidatorVotingPower] Failed to decode shard state")
}
}

// Writing beacon chain cross links
if isBeaconChain &&
bc.chainConfig.IsCrossLink(block.Epoch()) &&
Expand Down Expand Up @@ -194,29 +214,6 @@ func (bc *BlockChain) CommitOffChainData(
}
}

// Update voting power of validators for all shards
tempValidatorStats := map[common.Address]*staking.ValidatorStats{}
if isNewEpoch && isBeaconChain {
currentSuperCommittee, _ := bc.ReadShardState(bc.CurrentHeader().Epoch())
if shardState, err := shard.DecodeWrapper(
header.ShardState(),
); err == nil {
if stats, err := bc.UpdateValidatorVotingPower(
batch, block, shardState, currentSuperCommittee, state,
); err != nil {
utils.Logger().
Err(err).
Msg("[UpdateValidatorVotingPower] Failed to update voting power")
} else {
tempValidatorStats = stats
}
} else {
utils.Logger().
Err(err).
Msg("[UpdateValidatorVotingPower] Failed to decode shard state")
}
}

// Update block reward accumulator and slashes
if isBeaconChain {
if isStaking {
Expand All @@ -226,6 +223,7 @@ func (bc *BlockChain) CommitOffChainData(
); err != nil {
return NonStatTy, err
}
tempValidatorStats := map[common.Address]*staking.ValidatorStats{}
for _, paid := range [...][]reward.Payout{
roundResult.BeaconchainAward, roundResult.ShardChainAward,
} {
Expand Down

0 comments on commit 5b2e041

Please sign in to comment.