Skip to content

Commit

Permalink
Merget3 2 (#2747)
Browse files Browse the repository at this point in the history
* [validator] Hide one field from JSON (#2705)

* [rpc] Show both latest header of beacon chain and shard chain (#2714)

* change explorer node storage folder (#2720)

* [state] ValidatorWrapper div zero panic fix -  (#2724)

* [state] Error on div zero

* [state] Do not pay out commit when commit is 0%

* [state] Put whole commission in guarded block

* [core] Reward cannot be zero

* [state] return nil, not hard error

* make validator snapshot consistent with election (#2726)

* make validator snapshot consistent with election

* fix condition with only beacon chain

* Fix epoch number

* Fix stats update nil epoch issue

* [node.sh] use rclone to fast sync harmony_db_0 (#2709)

Signed-off-by: Leo Chen <[email protected]>

* fix validator snapshot cache

* Make validator snapshot on new validator too (#2733)

* do snapshot for validators at last block of epoch (#2736)

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

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

This reverts commit dc036e6.

* 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 832b01d.

* Don't div by 0; print out debug info

Co-authored-by: Edgar Aroutiounian <[email protected]>
Co-authored-by: Minh Doan <[email protected]>
Co-authored-by: Leo Chen <[email protected]>
Co-authored-by: Ganesha Upadhyaya <[email protected]>
  • Loading branch information
5 people authored Apr 6, 2020
1 parent 5b2e041 commit 0c46e08
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
23 changes: 9 additions & 14 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,
) error {
) (map[common.Address]*staking.ValidatorStats, error) {
if newEpochSuperCommittee == nil {
return shard.ErrSuperCommitteeNil
return nil, 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 errors.Wrapf(
return nil, 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 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 {
Expand All @@ -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(
Expand All @@ -2382,7 +2382,7 @@ func (bc *BlockChain) UpdateValidatorVotingPower(
)

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

computed := availability.ComputeCurrentSigning(snapshot, wrapper)
Expand All @@ -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
Expand Down
44 changes: 23 additions & 21 deletions core/offchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) &&
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
} {
Expand Down
8 changes: 6 additions & 2 deletions staking/availability/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c46e08

Please sign in to comment.