From a2dfc0f2e2750c2c69a66f279533862ce6d79963 Mon Sep 17 00:00:00 2001 From: Victor Graf Date: Thu, 30 Jan 2020 16:20:01 -0800 Subject: [PATCH 1/2] check if rewards are frozen and add a log message --- consensus/istanbul/backend/engine.go | 1 + consensus/istanbul/backend/pos.go | 13 ++++++-- contract_comm/epoch_rewards/epoch_rewards.go | 23 +++++++++++++++ params/protocol_params.go | 31 ++++++++++---------- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/consensus/istanbul/backend/engine.go b/consensus/istanbul/backend/engine.go index 942be28479..888780302b 100644 --- a/consensus/istanbul/backend/engine.go +++ b/consensus/istanbul/backend/engine.go @@ -466,6 +466,7 @@ func (sb *Backend) Finalize(chain consensus.ChainReader, header *types.Header, s snapshot = state.Snapshot() err = sb.distributeEpochPaymentsAndRewards(header, state) if err != nil { + sb.logger.Error("Failed to distribute epoch rewards", "blockNumber", header.Number, "err", err) state.RevertToSnapshot(snapshot) } } diff --git a/consensus/istanbul/backend/pos.go b/consensus/istanbul/backend/pos.go index 8a0f3674d6..3b2039c97f 100644 --- a/consensus/istanbul/backend/pos.go +++ b/consensus/istanbul/backend/pos.go @@ -41,6 +41,15 @@ import ( func (sb *Backend) distributeEpochPaymentsAndRewards(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 { @@ -50,14 +59,14 @@ func (sb *Backend) distributeEpochPaymentsAndRewards(header *types.Header, state if err != nil { return err } - log.Debug("Calculated target epoch payment and rewards", "validatorEpochPayment", validatorEpochPayment, "totalVoterRewards", totalVoterRewards) + logger.Debug("Calculated target epoch payment and rewards", "validatorEpochPayment", validatorEpochPayment, "totalVoterRewards", totalVoterRewards) // 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 payments and rewards") - sb.logger.Error(err.Error()) + logger.Error(err.Error()) return err } diff --git a/contract_comm/epoch_rewards/epoch_rewards.go b/contract_comm/epoch_rewards/epoch_rewards.go index b65419c3a5..dc45c657bc 100644 --- a/contract_comm/epoch_rewards/epoch_rewards.go +++ b/contract_comm/epoch_rewards/epoch_rewards.go @@ -55,6 +55,20 @@ const epochRewardsABIString string = `[ "payable": false, "stateMutability": "nonpayable", "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "frozen", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" } ] ` @@ -75,3 +89,12 @@ func CalculateTargetEpochPaymentAndRewards(header *types.Header, state vm.StateD } return validatorEpochPayment, totalVoterRewards, 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.MaxGasForEpochRewardsFrozen, header, state) + if err != nil { + return false, err + } + return frozen, nil +} diff --git a/params/protocol_params.go b/params/protocol_params.go index 25e6db2444..73cd6d44bb 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -154,30 +154,31 @@ const ( IntrinsicGasForAlternativeFeeCurrency uint64 = 50000 // Contract communication gas limits - MaxGasForCalculateTargetEpochPaymentAndRewards uint64 = 2000000 - MaxGasForCommitments uint64 = 2000000 - MaxGasForComputeCommitment uint64 = 2000000 - MaxGasForCreditToTransactions uint64 = 100000 - MaxGasForDebitFromTransactions uint64 = 100000 + MaxGasForCalculateTargetEpochPaymentAndRewards uint64 = 2 * 1000000 + MaxGasForCommitments uint64 = 2 * 1000000 + MaxGasForComputeCommitment uint64 = 2 * 1000000 + MaxGasForCreditToTransactions uint64 = 100 * 1000 + MaxGasForDebitFromTransactions uint64 = 100 * 1000 MaxGasForDistributeEpochPayment uint64 = 1 * 1000000 MaxGasForDistributeEpochRewards uint64 = 1 * 1000000 MaxGasForElectValidators uint64 = 50 * 1000000 + MaxGasForEpochRewardsFrozen uint64 = 20 * 1000 MaxGasForGetAddressFor uint64 = 1 * 100000 MaxGasForGetEligibleValidatorGroupsVoteTotals uint64 = 1 * 1000000 - MaxGasForGetGasPriceMinimum uint64 = 2000000 + MaxGasForGetGasPriceMinimum uint64 = 2 * 1000000 MaxGasForGetGroupEpochRewards uint64 = 500 * 1000 MaxGasForGetMembershipInLastEpoch uint64 = 1 * 1000000 - MaxGasForGetOrComputeTobinTax uint64 = 1000000 - MaxGasForGetRegisteredValidators uint64 = 2000000 + MaxGasForGetOrComputeTobinTax uint64 = 1 * 1000000 + MaxGasForGetRegisteredValidators uint64 = 2 * 1000000 MaxGasForGetValidator uint64 = 100 * 1000 - MaxGasForGetWhiteList uint64 = 20000 + MaxGasForGetWhiteList uint64 = 20 * 1000 MaxGasForIncreaseSupply uint64 = 50 * 1000 - MaxGasForMedianRate uint64 = 20000 - MaxGasForReadBlockchainParameter uint64 = 20000 - MaxGasForRevealAndCommit uint64 = 2000000 - MaxGasForUpdateGasPriceMinimum uint64 = 2000000 - MaxGasForUpdateTargetVotingYield uint64 = 2000000 + MaxGasForMedianRate uint64 = 20 * 1000 + MaxGasForReadBlockchainParameter uint64 = 20 * 1000 + MaxGasForRevealAndCommit uint64 = 2 * 1000000 + MaxGasForUpdateGasPriceMinimum uint64 = 2 * 1000000 + MaxGasForUpdateTargetVotingYield uint64 = 2 * 1000000 MaxGasForUpdateValidatorScore uint64 = 1 * 1000000 MaxGasForTotalSupply uint64 = 50 * 1000 - MaxGasToReadErc20Balance uint64 = 100000 + MaxGasToReadErc20Balance uint64 = 100 * 1000 ) From e5848db0041b433aa796dd6f21fbe2be9faaaaa5 Mon Sep 17 00:00:00 2001 From: Victor Graf Date: Thu, 30 Jan 2020 16:33:29 -0800 Subject: [PATCH 2/2] tab to space --- contract_comm/epoch_rewards/epoch_rewards.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contract_comm/epoch_rewards/epoch_rewards.go b/contract_comm/epoch_rewards/epoch_rewards.go index dc45c657bc..347d5a22ee 100644 --- a/contract_comm/epoch_rewards/epoch_rewards.go +++ b/contract_comm/epoch_rewards/epoch_rewards.go @@ -56,7 +56,7 @@ const epochRewardsABIString string = `[ "stateMutability": "nonpayable", "type": "function" }, - { + { "constant": true, "inputs": [], "name": "frozen",