Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merget3 2 #2747

Merged
merged 13 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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