Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
blindchaser committed Oct 8, 2024
1 parent c5890f3 commit 434a5f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion x/evm/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (fc EVMFeeCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
return ctx, sdkerrors.ErrInsufficientFee
}
if txData.GetGasTipCap().Sign() < 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "gas tip cap cannot be negative: %s", txData.GetGasTipCap())
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "gas fee cap cannot be negative")
}

// if EVM version is Cancun or later, and the transaction contains at least one blob, we need to
Expand Down
37 changes: 37 additions & 0 deletions x/evm/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ func TestEVMFeeCheckDecorator(t *testing.T) {
return ctx, nil
})
require.NotNil(t, err)

// should fail because of negative gas tip cap
txData.GasTipCap = big.NewInt(-1)
txData.GasFeeCap = big.NewInt(10000000000000)
tx, err = ethtypes.SignTx(ethtypes.NewTx(&txData), signer, key)
require.Nil(t, err)
typedTx = newDynamicFeeTxWithoutValidation(tx)
msg, err = types.NewMsgEVMTransaction(typedTx)
require.Nil(t, err)
ctx, err = preprocessor.AnteHandle(ctx, mockTx{msgs: []sdk.Msg{msg}}, false, func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
})
require.Nil(t, err)
_, err = handler.AnteHandle(ctx, mockTx{msgs: []sdk.Msg{msg}}, false, func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
})
require.NotNil(t, err)
require.Contains(t, err.Error(), "gas fee cap cannot be negative")
}

func TestCalculatePriorityScenarios(t *testing.T) {
Expand Down Expand Up @@ -227,3 +245,22 @@ func TestCalculatePriorityScenarios(t *testing.T) {
})
}
}

func newDynamicFeeTxWithoutValidation(tx *ethtypes.Transaction) *ethtx.DynamicFeeTx {
txData := &ethtx.DynamicFeeTx{
Nonce: tx.Nonce(),
Data: tx.Data(),
GasLimit: tx.Gas(),
}

v, r, s := tx.RawSignatureValues()
ethtx.SetConvertIfPresent(tx.To(), func(to *common.Address) string { return to.Hex() }, txData.SetTo)
ethtx.SetConvertIfPresent(tx.Value(), sdk.NewIntFromBigInt, txData.SetAmount)
ethtx.SetConvertIfPresent(tx.GasFeeCap(), sdk.NewIntFromBigInt, txData.SetGasFeeCap)
ethtx.SetConvertIfPresent(tx.GasTipCap(), sdk.NewIntFromBigInt, txData.SetGasTipCap)
al := tx.AccessList()
ethtx.SetConvertIfPresent(&al, ethtx.NewAccessList, txData.SetAccesses)

txData.SetSignatureValues(tx.ChainId(), v, r, s)
return txData
}
4 changes: 0 additions & 4 deletions x/evm/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ func UnpackTxData(any *codectypes.Any) (ethtx.TxData, error) {
dtx := ethtx.DynamicFeeTx{}
if proto.Unmarshal(any.Value, &dtx) == nil {
// value is a dynamic fee tx
// check gas tip cap non-negative
if dtx.GasTipCap.Sign() < 0 {
return nil, fmt.Errorf("gas tip cap cannot be negative: %s", dtx.GasTipCap)
}
return &dtx, nil
}
btx := ethtx.BlobTx{}
Expand Down

0 comments on commit 434a5f6

Please sign in to comment.