From cc815a6002dfdc475206cf7014cd720b718b32c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Tue, 26 Apr 2022 16:24:32 +0200 Subject: [PATCH] fix: update BaseFee JSON-RPC (#1059) * release: v0.14.0 changelog (#1057) * fix: update BaseFee JSON-RPC * typo * changelog --- CHANGELOG.md | 4 ++++ rpc/ethereum/backend/backend.go | 35 ++++++++++----------------------- x/feemarket/keeper/eip1559.go | 10 ++++++++-- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f0432ef57..2752d30cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased +### Bug Fixes + +* (rpc) [tharsis#1059](https://github.com/tharsis/ethermint/pull/1059) Remove unnecessary event filtering logic on the `eth_baseFee` JSON-RPC endpoint. + ### Improvements - (rpc) [tharsis#979](https://github.com/tharsis/ethermint/pull/979) Add configurable timeouts to http server diff --git a/rpc/ethereum/backend/backend.go b/rpc/ethereum/backend/backend.go index 51565b28f0..8057e8f313 100644 --- a/rpc/ethereum/backend/backend.go +++ b/rpc/ethereum/backend/backend.go @@ -960,7 +960,7 @@ func (e *EVMBackend) ChainConfig() *params.ChainConfig { // mitigate the base fee changes. func (e *EVMBackend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) { if baseFee == nil { - // london hardfork not enabled or feemarket not enabeld + // london hardfork not enabled or feemarket not enabled return big.NewInt(0), nil } @@ -986,42 +986,27 @@ func (e *EVMBackend) SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) { return big.NewInt(maxDelta), nil } -// BaseFee returns the base fee tracked by the Fee Market module. If the base fee is not enabled, -// it returns the initial base fee amount. Return nil if London is not activated. +// BaseFee returns the base fee tracked by the Fee Market module. +// If the base fee is not enabled globally, the query returns nil. +// If the London hard fork is not activated at the current height, the query will +// return nil. func (e *EVMBackend) BaseFee(height int64) (*big.Int, error) { cfg := e.ChainConfig() if !cfg.IsLondon(new(big.Int).SetInt64(height)) { return nil, nil } - // Checks the feemarket param NoBaseFee settings, return 0 if it is enabled. - resParams, err := e.queryClient.FeeMarket.Params(types.ContextWithHeight(height), &feemarkettypes.QueryParamsRequest{}) - if err != nil { - return nil, err - } - - if resParams.Params.NoBaseFee { - return big.NewInt(0), nil - } - - blockRes, err := e.clientCtx.Client.BlockResults(e.ctx, &height) + // return BaseFee if London hard fork is activated and feemarket is not enabled + res, err := e.queryClient.FeeMarket.BaseFee(types.ContextWithHeight(height), &feemarkettypes.QueryBaseFeeRequest{}) if err != nil { return nil, err } - baseFee := types.BaseFeeFromEvents(blockRes.BeginBlockEvents) - if baseFee != nil { - return baseFee, nil - } - - // If we cannot find in events, we tried to get it from the state. - // It will return feemarket.baseFee if london is activated but feemarket is not enable - res, err := e.queryClient.FeeMarket.BaseFee(types.ContextWithHeight(height), &feemarkettypes.QueryBaseFeeRequest{}) - if err == nil && res.BaseFee != nil { - return res.BaseFee.BigInt(), nil + if res.BaseFee == nil { + return nil, nil } - return nil, nil + return res.BaseFee.BigInt(), nil } // GetEthereumMsgsFromTendermintBlock returns all real MsgEthereumTxs from a Tendermint block. diff --git a/x/feemarket/keeper/eip1559.go b/x/feemarket/keeper/eip1559.go index 93fd055aa8..30073230d1 100644 --- a/x/feemarket/keeper/eip1559.go +++ b/x/feemarket/keeper/eip1559.go @@ -16,14 +16,16 @@ import ( func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int { params := k.GetParams(ctx) - // Ignore the calculation if not enable + // Ignore the calculation if not enabled if !params.IsBaseFeeEnabled(ctx.BlockHeight()) { return nil } consParams := ctx.ConsensusParams() - // If the current block is the first EIP-1559 block, return the InitialBaseFee. + // If the current block is the first EIP-1559 block, return the base fee + // defined in the parameters (DefaultBaseFee if it hasn't been changed by + // governance). if ctx.BlockHeight() == params.EnableHeight { return new(big.Int).SetInt64(params.InitialBaseFee) } @@ -37,10 +39,14 @@ func (k Keeper) CalculateBaseFee(ctx sdk.Context) *big.Int { parentGasUsed := k.GetBlockGasUsed(ctx) gasLimit := new(big.Int).SetUint64(math.MaxUint64) + + // NOTE: a MaxGas equal to -1 means that block gas is unlimited if consParams != nil && consParams.Block.MaxGas > -1 { gasLimit = big.NewInt(consParams.Block.MaxGas) } + // CONTRACT: ElasticityMultiplier cannot be 0 as it's checked in the params + // validation parentGasTargetBig := new(big.Int).Div(gasLimit, new(big.Int).SetUint64(uint64(params.ElasticityMultiplier))) if !parentGasTargetBig.IsUint64() { return new(big.Int).SetInt64(params.InitialBaseFee)