Skip to content

Commit 412115d

Browse files
authored
Problem: panic on invalid elasticity_multiplier (#472)
* Problem: panic on invalid elasticity_multiplier * Problem: codecov action v3 is pinging v4 (#458)
1 parent 27de4ed commit 412115d

File tree

8 files changed

+67
-11
lines changed

8 files changed

+67
-11
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: |
3535
make test-unit-cover
3636
if: env.GIT_DIFF
37-
- uses: codecov/codecov-action@v3
37+
- uses: codecov/codecov-action@v4
3838
with:
3939
file: ./coverage.txt
4040
fail_ci_if_error: true

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444
### Bug Fixes
4545

4646
* (rpc) [#457](https://github.com/crypto-org-chain/ethermint/pull/457) Add param keytable in evm for old upgrade.
47+
* (rpc) [#473](https://github.com/crypto-org-chain/ethermint/pull/473) Avoid panic on invalid elasticity_multiplier.
4748

4849
## v0.21.x-cronos
4950

rpc/backend/chain_info.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"strconv"
2222
"sync"
2323

24+
errorsmod "cosmossdk.io/errors"
2425
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
2526
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
2627
sdk "github.com/cosmos/cosmos-sdk/types"
28+
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
2729
"github.com/ethereum/go-ethereum/common/hexutil"
2830
"github.com/ethereum/go-ethereum/common/math"
2931
ethtypes "github.com/ethereum/go-ethereum/core/types"
@@ -226,7 +228,14 @@ func (b *Backend) FeeHistory(
226228
}
227229
wg.Add(1)
228230
go func(index int32) {
229-
defer wg.Done()
231+
defer func() {
232+
if r := recover(); r != nil {
233+
err = errorsmod.Wrapf(errortypes.ErrPanic, "%v", r)
234+
b.logger.Error("FeeHistory panicked", "error", err)
235+
chanErr <- err
236+
}
237+
wg.Done()
238+
}()
230239
// fetch block
231240
// tendermint block
232241
blockNum := rpctypes.BlockNumber(blockStart + int64(index))

rpc/backend/chain_info_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,23 @@ func (suite *BackendTestSuite) TestFeeHistory() {
377377
false,
378378
nil,
379379
},
380+
{
381+
"fail - Tendermint block fetching panic",
382+
func(validator sdk.AccAddress) {
383+
client := suite.backend.clientCtx.Client.(*mocks.Client)
384+
suite.backend.cfg.JSONRPC.FeeHistoryCap = 2
385+
var header metadata.MD
386+
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
387+
RegisterParams(queryClient, &header, 1)
388+
RegisterBlockPanic(client, ethrpc.BlockNumber(1).Int64())
389+
},
390+
1,
391+
1,
392+
nil,
393+
nil,
394+
false,
395+
nil,
396+
},
380397
{
381398
"fail - Eth block fetching error",
382399
func(validator sdk.AccAddress) {

rpc/backend/client_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ func RegisterBlockNotFound(
140140
return &tmrpctypes.ResultBlock{Block: nil}, nil
141141
}
142142

143+
// Block panic
144+
func RegisterBlockPanic(client *mocks.Client, height int64) {
145+
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
146+
Return(func(context.Context, *int64) *tmrpctypes.ResultBlock {
147+
panic("Block call panic")
148+
}, nil)
149+
}
150+
143151
func TestRegisterBlock(t *testing.T) {
144152
client := mocks.NewClient(t)
145153
height := rpc.BlockNumber(1).Int64()

rpc/backend/utils.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
sdk "github.com/cosmos/cosmos-sdk/types"
2525
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
26+
"github.com/pkg/errors"
2627
"google.golang.org/grpc/codes"
2728
"google.golang.org/grpc/status"
2829

@@ -118,16 +119,18 @@ func (b *Backend) getAccountNonce(accAddr common.Address, pending bool, height i
118119
}
119120

120121
// CalcBaseFee calculates the basefee of the header.
121-
func CalcBaseFee(config *params.ChainConfig, parent *ethtypes.Header, p feemarkettypes.Params) *big.Int {
122+
func CalcBaseFee(config *params.ChainConfig, parent *ethtypes.Header, p feemarkettypes.Params) (*big.Int, error) {
122123
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
123124
if !config.IsLondon(parent.Number) {
124-
return new(big.Int).SetUint64(params.InitialBaseFee)
125+
return new(big.Int).SetUint64(params.InitialBaseFee), nil
126+
}
127+
if p.ElasticityMultiplier == 0 {
128+
return nil, errors.New("ElasticityMultiplier cannot be 0 as it's checked in the params validation")
125129
}
126-
127130
parentGasTarget := parent.GasLimit / uint64(p.ElasticityMultiplier)
128131
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
129132
if parent.GasUsed == parentGasTarget {
130-
return new(big.Int).Set(parent.BaseFee)
133+
return new(big.Int).Set(parent.BaseFee), nil
131134
}
132135

133136
var (
@@ -144,7 +147,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *ethtypes.Header, p feemarke
144147
num.Div(num, denom.SetUint64(uint64(p.BaseFeeChangeDenominator)))
145148
baseFeeDelta := math.BigMax(num, common.Big1)
146149

147-
return num.Add(parent.BaseFee, baseFeeDelta)
150+
return num.Add(parent.BaseFee, baseFeeDelta), nil
148151
}
149152

150153
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
@@ -155,7 +158,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *ethtypes.Header, p feemarke
155158
num.Div(num, denom.SetUint64(uint64(p.BaseFeeChangeDenominator)))
156159
baseFee := num.Sub(parent.BaseFee, num)
157160
minGasPrice := p.MinGasPrice.TruncateInt().BigInt()
158-
return math.BigMax(baseFee, minGasPrice)
161+
return math.BigMax(baseFee, minGasPrice), nil
159162
}
160163

161164
// output: targetOneFeeHistory
@@ -201,7 +204,11 @@ func (b *Backend) processBlock(
201204
if err != nil {
202205
return err
203206
}
204-
targetOneFeeHistory.NextBaseFee = CalcBaseFee(cfg, &header, params.Params)
207+
nextBaseFee, err := CalcBaseFee(cfg, &header, params.Params)
208+
if err != nil {
209+
return err
210+
}
211+
targetOneFeeHistory.NextBaseFee = nextBaseFee
205212
} else {
206213
targetOneFeeHistory.NextBaseFee = new(big.Int)
207214
}

x/feemarket/migrations/v4/types/params.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func (p Params) Validate() error {
104104
return err
105105
}
106106

107+
if p.ElasticityMultiplier == 0 {
108+
return fmt.Errorf("elasticity multiplier cannot be 0")
109+
}
110+
107111
return validateMinGasPrice(p.MinGasPrice)
108112
}
109113

@@ -129,10 +133,13 @@ func validateBaseFeeChangeDenominator(i interface{}) error {
129133
}
130134

131135
func validateElasticityMultiplier(i interface{}) error {
132-
_, ok := i.(uint32)
136+
value, ok := i.(uint32)
133137
if !ok {
134138
return fmt.Errorf("invalid parameter type: %T", i)
135139
}
140+
if value == 0 {
141+
return fmt.Errorf("elasticity multiplier cannot be 0")
142+
}
136143
return nil
137144
}
138145

x/feemarket/types/params.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (p Params) Validate() error {
117117
return err
118118
}
119119

120+
if p.ElasticityMultiplier == 0 {
121+
return fmt.Errorf("elasticity multiplier cannot be 0")
122+
}
123+
120124
return validateMinGasPrice(p.MinGasPrice)
121125
}
122126

@@ -164,10 +168,13 @@ func validateBaseFeeChangeDenominator(i interface{}) error {
164168
}
165169

166170
func validateElasticityMultiplier(i interface{}) error {
167-
_, ok := i.(uint32)
171+
value, ok := i.(uint32)
168172
if !ok {
169173
return fmt.Errorf("invalid parameter type: %T", i)
170174
}
175+
if value == 0 {
176+
return fmt.Errorf("elasticity multiplier cannot be 0")
177+
}
171178
return nil
172179
}
173180

0 commit comments

Comments
 (0)