From 191f1d5ee266d32cf08f243f953925718fdb61c1 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sun, 5 Apr 2020 21:11:21 -0700 Subject: [PATCH] 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 832b01dfff0b2fec3cc647f507a8d105a8dbf4a7. * Don't div by 0; print out debug info --- core/blockchain.go | 23 +++++++---------- core/offchain.go | 44 +++++++++++++++++---------------- staking/availability/measure.go | 8 ++++-- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 6d54f2a1c..7def6bed7 100644 --- a/core/blockchain.go +++ b/core/blockchain.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 diff --git a/core/offchain.go b/core/offchain.go index 9f0355d9c..48bcae675 100644 --- a/core/offchain.go +++ b/core/offchain.go @@ -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, } { diff --git a/staking/availability/measure.go b/staking/availability/measure.go index b73bac020..75ad45616 100644 --- a/staking/availability/measure.go +++ b/staking/availability/measure.go @@ -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