Skip to content

Commit

Permalink
fix(evm): Fix DynamicFeeTx gas cap parameters (#2017)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Aug 30, 2024
1 parent 3e9bda9 commit 9c46873
Show file tree
Hide file tree
Showing 52 changed files with 2,689 additions and 1,612 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2008](https://github.com/NibiruChain/nibiru/pull/2008) - refactor(evm): clean up precompile setups
- [#2013](https://github.com/NibiruChain/nibiru/pull/2013) - chore(evm): Set appropriate gas value for the required gas of the "IFunToken.sol" precompile.
- [#2014](https://github.com/NibiruChain/nibiru/pull/2014) - feat(evm): Emit block bloom event in EndBlock hook.
- [#2017](https://github.com/NibiruChain/nibiru/pull/2017) - fix(evm): Fix DynamicFeeTx gas cap parameters
- [#2019](https://github.com/NibiruChain/nibiru/pull/2019) - chore(evm): enabled debug rpc api on localnet.
- [#2020](https://github.com/NibiruChain/nibiru/pull/2020) - test(evm): e2e tests for debug namespace

Expand Down
8 changes: 4 additions & 4 deletions app/evmante/evmante_can_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"

Expand Down Expand Up @@ -40,7 +40,7 @@ func (ctd CanTransferDecorator) AnteHandle(
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}
Expand All @@ -62,7 +62,7 @@ func (ctd CanTransferDecorator) AnteHandle(
}
if coreMsg.GasFeeCap().Cmp(baseFee) < 0 {
return ctx, errors.Wrapf(
errortypes.ErrInsufficientFee,
sdkerrors.ErrInsufficientFee,
"max fee per gas less than block base fee (%s < %s)",
coreMsg.GasFeeCap(), baseFee,
)
Expand All @@ -89,7 +89,7 @@ func (ctd CanTransferDecorator) AnteHandle(
!evmInstance.Context.CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
balanceWei := stateDB.GetBalance(coreMsg.From())
return ctx, errors.Wrapf(
errortypes.ErrInsufficientFunds,
sdkerrors.ErrInsufficientFunds,
"failed to transfer %s wei (balance=%s) from address %s using the EVM block context transfer function",
coreMsg.Value(),
balanceWei,
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_emit_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/NibiruChain/nibiru/v2/x/evm"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func (eeed EthEmitEventDecorator) AnteHandle(
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errorsmod.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
)
Expand Down
19 changes: 10 additions & 9 deletions app/evmante/evmante_gas_consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/NibiruChain/nibiru/v2/eth"
Expand Down Expand Up @@ -34,8 +34,7 @@ func NewAnteDecEthGasConsume(

// AnteHandle validates that the Ethereum tx message has enough to cover
// intrinsic gas (during CheckTx only) and that the sender has enough balance to
// pay for the gas cost. If the balance is not sufficient, it will be attempted
// to withdraw enough staking rewards for the payment.
// pay for the gas cost.
//
// Intrinsic gas for a transaction is the amount of gas that the transaction uses
// before the transaction is executed. The gas is a constant value plus any cost
Expand Down Expand Up @@ -72,13 +71,13 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(

// Use the lowest priority of all the messages as the final one.
minPriority := int64(math.MaxInt64)
baseFee := anteDec.evmKeeper.GetBaseFee(ctx)
baseFeeMicronibiPerGas := anteDec.evmKeeper.GetBaseFee(ctx)

for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
)
Expand All @@ -101,7 +100,7 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(
gasWanted += txData.GetGas()
}

fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, ctx.IsCheckTx())
fees, err := keeper.VerifyFee(txData, evmDenom, baseFeeMicronibiPerGas, ctx.IsCheckTx())
if err != nil {
return ctx, errors.Wrapf(err, "failed to verify the fees")
}
Expand All @@ -117,7 +116,7 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(
),
)

priority := evm.GetTxPriority(txData, baseFee)
priority := evm.GetTxPriority(txData, baseFeeMicronibiPerGas)

if priority < minPriority {
minPriority = priority
Expand All @@ -135,7 +134,7 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(
// EthSetupContextDecorator, so it will never exceed the block gas limit.
if gasWanted > blockGasLimit {
return ctx, errors.Wrapf(
errortypes.ErrOutOfGas,
sdkerrors.ErrOutOfGas,
"tx gas (%d) exceeds block gas limit (%d)",
gasWanted,
blockGasLimit,
Expand All @@ -158,7 +157,9 @@ func (anteDec AnteDecEthGasConsume) AnteHandle(

// deductFee checks if the fee payer has enough funds to pay for the fees and deducts them.
// If the spendable balance is not enough, it tries to claim enough staking rewards to cover the fees.
func (anteDec AnteDecEthGasConsume) deductFee(ctx sdk.Context, fees sdk.Coins, feePayer sdk.AccAddress) error {
func (anteDec AnteDecEthGasConsume) deductFee(
ctx sdk.Context, fees sdk.Coins, feePayer sdk.AccAddress,
) error {
if fees.IsZero() {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions app/evmante/evmante_increment_sender_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package evmante
import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
gethcommon "github.com/ethereum/go-ethereum/common"

Expand Down Expand Up @@ -38,7 +38,7 @@ func (issd AnteDecEthIncrementSenderSequence) AnteHandle(
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}
Expand All @@ -52,7 +52,7 @@ func (issd AnteDecEthIncrementSenderSequence) AnteHandle(
acc := issd.accountKeeper.GetAccount(ctx, msgEthTx.GetFrom())
if acc == nil {
return ctx, errors.Wrapf(
errortypes.ErrUnknownAddress,
sdkerrors.ErrUnknownAddress,
"account %s is nil", gethcommon.BytesToAddress(msgEthTx.GetFrom().Bytes()),
)
}
Expand All @@ -62,7 +62,7 @@ func (issd AnteDecEthIncrementSenderSequence) AnteHandle(
// with same sender, they'll be accepted.
if txData.GetNonce() != nonce {
return ctx, errors.Wrapf(
errortypes.ErrInvalidSequence,
sdkerrors.ErrInvalidSequence,
"invalid nonce; got %d, expected %d", txData.GetNonce(), nonce,
)
}
Expand Down
25 changes: 16 additions & 9 deletions app/evmante/evmante_mempool_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package evmante

import (
"cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/NibiruChain/nibiru/v2/x/evm"
)
Expand Down Expand Up @@ -38,32 +39,38 @@ func (d MempoolGasPriceDecorator) AnteHandle(
}

minGasPrice := ctx.MinGasPrices().AmountOf(d.evmKeeper.GetParams(ctx).EvmDenom)
baseFeeMicronibi := d.evmKeeper.GetBaseFee(ctx)
baseFeeDec := math.LegacyNewDecFromBigInt(baseFeeMicronibi)

// if MinGasPrices is not set, skip the check
if minGasPrice.IsZero() {
return next(ctx, tx, simulate)
} else if minGasPrice.LT(baseFeeDec) {
minGasPrice = baseFeeDec
}

baseFee := d.evmKeeper.GetBaseFee(ctx)

for _, msg := range tx.GetMsgs() {
ethTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T",
msg, (*evm.MsgEthereumTx)(nil),
)
}

effectiveGasPrice := ethTx.GetEffectiveGasPrice(baseFee)

if sdk.NewDecFromBigInt(effectiveGasPrice).LT(minGasPrice) {
baseFeeWei := evm.NativeToWei(baseFeeMicronibi)
effectiveGasPriceDec := math.LegacyNewDecFromBigInt(
evm.WeiToNative(ethTx.GetEffectiveGasPrice(baseFeeWei)),
)
if effectiveGasPriceDec.LT(minGasPrice) {
// if sdk.NewDecFromBigInt(effectiveGasPrice).LT(minGasPrice) {
return ctx, errors.Wrapf(
errortypes.ErrInsufficientFee,
sdkerrors.ErrInsufficientFee,
"provided gas price < minimum local gas price (%s < %s). "+
"Please increase the priority tip (for EIP-1559 txs) or the gas prices "+
"(for access list or legacy txs)",
effectiveGasPrice.String(), minGasPrice.String(),
effectiveGasPriceDec, minGasPrice,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_setup_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
errorsmod "cosmossdk.io/errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
)

Expand All @@ -31,7 +31,7 @@ func (esc EthSetupContextDecorator) AnteHandle(
_, ok := tx.(authante.GasTx)
if !ok {
return ctx, errorsmod.Wrapf(
errortypes.ErrInvalidType,
sdkerrors.ErrInvalidType,
"invalid transaction type %T, expected GasTx", tx,
)
}
Expand Down
8 changes: 4 additions & 4 deletions app/evmante/evmante_sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/x/evm"
Expand Down Expand Up @@ -43,7 +43,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errors.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}
Expand All @@ -52,7 +52,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(
ethTx := msgEthTx.AsTransaction()
if !allowUnprotectedTxs && !ethTx.Protected() {
return ctx, errors.Wrapf(
errortypes.ErrNotSupported,
sdkerrors.ErrNotSupported,
"rejected unprotected Ethereum transaction. "+
"Please EIP155 sign your transaction to protect it against replay-attacks",
)
Expand All @@ -61,7 +61,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(
sender, err := signer.Sender(ethTx)
if err != nil {
return ctx, errors.Wrapf(
errortypes.ErrorInvalidSigner,
sdkerrors.ErrorInvalidSigner,
"couldn't retrieve sender address from the ethereum transaction: %s",
err.Error(),
)
Expand Down
24 changes: 12 additions & 12 deletions app/evmante/evmante_validate_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/x/evm"
Expand All @@ -34,7 +34,7 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu

err := tx.ValidateBasic()
// ErrNoSignatures is fine with eth tx
if err != nil && !errors.Is(err, errortypes.ErrNoSignatures) {
if err != nil && !errors.Is(err, sdkerrors.ErrNoSignatures) {
return ctx, errorsmod.Wrap(err, "tx basic validation failed")
}

Expand All @@ -43,7 +43,7 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
wrapperTx, ok := tx.(protoTxProvider)
if !ok {
return ctx, errorsmod.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid tx type %T, didn't implement interface protoTxProvider",
tx,
)
Expand All @@ -52,36 +52,36 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
protoTx := wrapperTx.GetProtoTx()
body := protoTx.Body
if body.Memo != "" || body.TimeoutHeight != uint64(0) || len(body.NonCriticalExtensionOptions) > 0 {
return ctx, errorsmod.Wrap(errortypes.ErrInvalidRequest,
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest,
"for eth tx body Memo TimeoutHeight NonCriticalExtensionOptions should be empty")
}

if len(body.ExtensionOptions) != 1 {
return ctx, errorsmod.Wrap(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"for eth tx length of ExtensionOptions should be 1",
)
}

authInfo := protoTx.AuthInfo
if len(authInfo.SignerInfos) > 0 {
return ctx, errorsmod.Wrap(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"for eth tx AuthInfo SignerInfos should be empty",
)
}

if authInfo.Fee.Payer != "" || authInfo.Fee.Granter != "" {
return ctx, errorsmod.Wrap(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"for eth tx AuthInfo Fee payer and granter should be empty",
)
}

sigs := protoTx.Signatures
if len(sigs) > 0 {
return ctx, errorsmod.Wrap(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"for eth tx Signatures should be empty",
)
}
Expand All @@ -97,15 +97,15 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
if !ok {
return ctx, errorsmod.Wrapf(
errortypes.ErrUnknownRequest,
sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}

// Validate `From` field
if msgEthTx.From != "" {
return ctx, errorsmod.Wrapf(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"invalid From %s, expect empty string", msgEthTx.From,
)
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu

if !authInfo.Fee.Amount.IsEqual(txFee) {
return ctx, errorsmod.Wrapf(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"invalid AuthInfo Fee Amount (%s != %s)",
authInfo.Fee.Amount,
txFee,
Expand All @@ -143,7 +143,7 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu

if authInfo.Fee.GasLimit != txGasLimit {
return ctx, errorsmod.Wrapf(
errortypes.ErrInvalidRequest,
sdkerrors.ErrInvalidRequest,
"invalid AuthInfo Fee GasLimit (%d != %d)",
authInfo.Fee.GasLimit,
txGasLimit,
Expand Down
Loading

0 comments on commit 9c46873

Please sign in to comment.