Final attempt: Don't div by 0; print out debug info (#2746)

* Revert "Revert "fix earned-reward by writing the stats only once, remove from UpdateValdiatorVotingPower (#2737)" (#2742)"

This reverts commit 832b01dfff.

* Don't div by 0; print out debug info
pull/2747/head
Rongjian Lan 5 years ago committed by GitHub
parent 832b01dfff
commit 191f1d5ee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      core/blockchain.go
  2. 44
      core/offchain.go
  3. 8
      staking/availability/measure.go

@ -2287,9 +2287,9 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
block *types.Block,
newEpochSuperCommittee, currentEpochSuperCommittee *shard.State,
state *state.DB,
) error {
) (map[common.Address]*staking.ValidatorStats, error) {
if newEpochSuperCommittee == nil {
return shard.ErrSuperCommitteeNil
return nil, shard.ErrSuperCommitteeNil
}
rosters, bootedFromSuperCommittee :=
@ -2319,7 +2319,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
for i := range newEpochSuperCommittee.Shards {
subCommittee := &newEpochSuperCommittee.Shards[i]
if newEpochSuperCommittee.Epoch == nil {
return errors.Wrapf(
return nil, errors.Wrapf(
errNilEpoch,
"block epoch %v current-committee-epoch %v",
block.Epoch(),
@ -2328,13 +2328,13 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
}
roster, err := votepower.Compute(subCommittee, newEpochSuperCommittee.Epoch)
if err != nil {
return err
return nil, 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 {
@ -2358,7 +2358,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
if currentEpochSuperCommittee.Epoch != nil {
wrapper, err := state.ValidatorWrapper(key)
if err != nil {
return err
return nil, err
}
aprComputed, err := apr.ComputeForValidator(
@ -2382,7 +2382,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
)
if err != nil {
return err
return nil, err
}
computed := availability.ComputeCurrentSigning(snapshot, wrapper)
@ -2399,15 +2399,10 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
stats.BootedStatus = effective.BannedForDoubleSigning
}
}
if err := rawdb.WriteValidatorStats(
batch, key, stats,
); err != nil {
return err
}
validatorStats[key] = stats
}
return nil
return validatorStats, nil
}
// deleteValidatorSnapshots deletes the snapshot staking information of given validator address

@ -135,26 +135,6 @@ 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()) &&
@ -214,6 +194,29 @@ 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 {
@ -223,7 +226,6 @@ func (bc *BlockChain) CommitOffChainData(
); err != nil {
return NonStatTy, err
}
tempValidatorStats := map[common.Address]*staking.ValidatorStats{}
for _, paid := range [...][]reward.Payout{
roundResult.BeaconchainAward, roundResult.ShardChainAward,
} {

@ -184,8 +184,12 @@ func ComputeCurrentSigning(
utils.Logger().Error().Msg("negative number of blocks to sign")
}
s1, s2 :=
numeric.NewDecFromBigInt(signed), numeric.NewDecFromBigInt(toSign)
s1, s2 := numeric.NewDecFromBigInt(signed), numeric.NewDecFromBigInt(toSign)
if s2.IsZero() || s2.IsNil() {
utils.Logger().Debug().Interface("s2", s2).
Msg("s2 is 0 or nil")
return computed
}
computed.Percentage = s1.Quo(s2)
computed.IsBelowThreshold = IsBelowSigningThreshold(computed.Percentage)
return computed

Loading…
Cancel
Save