From caa1c5a6c6b7ed8ba4aaf6e0b0848f6be5ba6668 Mon Sep 17 00:00:00 2001 From: Loredana Cirstea Date: Fri, 4 Mar 2022 01:55:09 +0200 Subject: [PATCH] fix: `eth_feeHistory` reward values cannot be nil (#970) * eth_feeHistory reward values cannot be nil * removed reward when rewardPercentiles is not specified * fix lint * refactor and fix typos * add changelog Co-authored-by: Freddy Caceres --- CHANGELOG.md | 3 ++- rpc/ethereum/backend/feebackend.go | 31 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e532e2..30bfa135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes -* (evm) [\#529](https://github.com/tharsis/ethermint/issues/529) support return value on trace tx response. +* (evm) [\#529](https://github.com/tharsis/ethermint/issues/529) Add support return value on trace tx response. +* (rpc) [#970] (https://github.com/tharsis/ethermint/pull/970) Fix unexpected nil reward values on `eth_feeHistory` response ## Improvements diff --git a/rpc/ethereum/backend/feebackend.go b/rpc/ethereum/backend/feebackend.go index a02cc580..b09b5d42 100644 --- a/rpc/ethereum/backend/feebackend.go +++ b/rpc/ethereum/backend/feebackend.go @@ -115,6 +115,7 @@ func (e *EVMBackend) processBlock( return nil } +// FeeHistory returns data relevant for fee estimation based on the specified range of blocks. func (e *EVMBackend) FeeHistory( userBlockCount rpc.DecimalOrHex, // number blocks to fetch, maximum is 100 lastBlock rpc.BlockNumber, // the block to start search , to oldest @@ -145,13 +146,16 @@ func (e *EVMBackend) FeeHistory( // prepare space reward := make([][]*hexutil.Big, blockCount) - rewardcount := len(rewardPercentiles) + rewardCount := len(rewardPercentiles) for i := 0; i < int(blockCount); i++ { - reward[i] = make([]*hexutil.Big, rewardcount) + reward[i] = make([]*hexutil.Big, rewardCount) } thisBaseFee := make([]*hexutil.Big, blockCount) thisGasUsedRatio := make([]float64, blockCount) + // rewards should only be calculated if reward percentiles were included + calculateRewards := rewardCount != 0 + // fetch block for blockID := blockStart; blockID < blockEnd; blockID++ { index := int32(blockID - blockStart) @@ -174,25 +178,34 @@ func (e *EVMBackend) FeeHistory( return nil, err } - onefeehistory := rpctypes.OneFeeHistory{} - err = e.processBlock(tendermintblock, ðBlock, rewardPercentiles, tendermintBlockResult, &onefeehistory) + oneFeeHistory := rpctypes.OneFeeHistory{} + err = e.processBlock(tendermintblock, ðBlock, rewardPercentiles, tendermintBlockResult, &oneFeeHistory) if err != nil { return nil, err } // copy - thisBaseFee[index] = (*hexutil.Big)(onefeehistory.BaseFee) - thisGasUsedRatio[index] = onefeehistory.GasUsedRatio - for j := 0; j < rewardcount; j++ { - reward[index][j] = (*hexutil.Big)(onefeehistory.Reward[j]) + thisBaseFee[index] = (*hexutil.Big)(oneFeeHistory.BaseFee) + thisGasUsedRatio[index] = oneFeeHistory.GasUsedRatio + if calculateRewards { + for j := 0; j < rewardCount; j++ { + reward[index][j] = (*hexutil.Big)(oneFeeHistory.Reward[j]) + if reward[index][j] == nil { + reward[index][j] = (*hexutil.Big)(big.NewInt(0)) + } + } } } feeHistory := rpctypes.FeeHistoryResult{ OldestBlock: oldestBlock, - Reward: reward, BaseFee: thisBaseFee, GasUsedRatio: thisGasUsedRatio, } + + if calculateRewards { + feeHistory.Reward = reward + } + return &feeHistory, nil }