Skip to content

Commit

Permalink
Check if epoch rewards are frozen and avoid putting errors in the logs (
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor "Nate" Graf authored Mar 7, 2020
1 parent dccce21 commit ffa8d5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func (sb *Backend) Finalize(chain consensus.ChainReader, header *types.Header, s
snapshot = state.Snapshot()
err = sb.distributeEpochRewards(header, state)
if err != nil {
sb.logger.Error("Failed to distribute epoch rewards", "blockNumber", header.Number, "err", err)
state.RevertToSnapshot(snapshot)
}
}
Expand Down
15 changes: 13 additions & 2 deletions consensus/istanbul/backend/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ import (
func (sb *Backend) distributeEpochRewards(header *types.Header, state *state.StateDB) error {
start := time.Now()
defer sb.rewardDistributionTimer.UpdateSince(start)
logger := sb.logger.New("func", "Backend.distributeEpochPaymentsAndRewards", "blocknum", header.Number.Uint64())

// Check if reward distribution has been frozen and return early without error if it is.
if frozen, err := epoch_rewards.EpochRewardsIsFrozen(header, state); err != nil {
logger.Warn("Failed to determine if epoch rewards are frozen", "err", err)
} else if frozen {
logger.Debug("Epoch rewards are frozen, skipping distribution")
return nil
}

err := epoch_rewards.UpdateTargetVotingYield(header, state)
if err != nil {
Expand All @@ -50,14 +59,16 @@ func (sb *Backend) distributeEpochRewards(header *types.Header, state *state.Sta
if err != nil {
return err
}
log.Debug("Calculated target rewards", "validatorReward", validatorReward, "totalVoterRewards", totalVoterRewards, "communityReward", communityReward)

logger.Debug("Calculated target rewards", "validatorReward", validatorReward, "totalVoterRewards", totalVoterRewards, "communityReward", communityReward)

// The validator set that signs off on the last block of the epoch is the one that we need to
// iterate over.
valSet := sb.GetValidators(big.NewInt(header.Number.Int64()-1), header.ParentHash)
if len(valSet) == 0 {

err := errors.New("Unable to fetch validator set to update scores and distribute rewards")
sb.logger.Error(err.Error())
logger.Error(err.Error())
return err
}

Expand Down
23 changes: 23 additions & 0 deletions contract_comm/epoch_rewards/epoch_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ const epochRewardsABIString string = `[
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "frozen",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
`
Expand Down Expand Up @@ -135,3 +149,12 @@ func GetCarbonOffsettingPartnerAddress(header *types.Header, state vm.StateDB) (
}
return carbonOffsettingPartner, nil
}

func EpochRewardsIsFrozen(header *types.Header, state vm.StateDB) (bool, error) {
var frozen bool
_, err := contract_comm.MakeStaticCall(params.EpochRewardsRegistryId, epochRewardsABI, "frozen", []interface{}{}, &[]interface{}{&frozen}, params.MaxGasForIsFrozen, header, state)
if err != nil {
return false, err
}
return frozen, nil
}

0 comments on commit ffa8d5e

Please sign in to comment.