Skip to content

Commit

Permalink
Bump nonce even if tx fails
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jul 24, 2024
1 parent aefdcf8 commit 7e4862f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewAnteHandlerAndDepGenerator(options HandlerOptions) (sdk.AnteHandler, sdk

evmAnteDecorators := []sdk.AnteFullDecorator{
evmante.NewEVMPreprocessDecorator(options.EVMKeeper, options.EVMKeeper.AccountKeeper()),
sdk.DefaultWrappedAnteDecorator(evmante.NewBasicDecorator()),
sdk.DefaultWrappedAnteDecorator(evmante.NewBasicDecorator(options.EVMKeeper)),
sdk.DefaultWrappedAnteDecorator(evmante.NewEVMFeeCheckDecorator(options.EVMKeeper)),
sdk.DefaultWrappedAnteDecorator(evmante.NewEVMSigVerifyDecorator(options.EVMKeeper, options.LatestCtxGetter)),
sdk.DefaultWrappedAnteDecorator(evmante.NewGasLimitDecorator(options.EVMKeeper)),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ require (
replace (
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.2.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.26
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.28-0.20240724053150-ee91f65da49d
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.9
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.1
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-22
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,8 @@ github.com/sei-protocol/go-ethereum v1.13.5-sei-22 h1:t/m1qXER+DEMrcpqgoYmUxifkA
github.com/sei-protocol/go-ethereum v1.13.5-sei-22/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
github.com/sei-protocol/sei-cosmos v0.3.26 h1:brYESCnbYI7gr/O8QEHxIyntZrIZp4ONA7m0xfDn6eI=
github.com/sei-protocol/sei-cosmos v0.3.26/go.mod h1:og/KbejR/zSQ8otapODEDU9zYNhFSUDbq9+tgeYePyU=
github.com/sei-protocol/sei-cosmos v0.3.28-0.20240724053150-ee91f65da49d h1:5DCUZvrpj+naJ8GozMKxlruNGdtkwohmlVNjGlF+p5w=
github.com/sei-protocol/sei-cosmos v0.3.28-0.20240724053150-ee91f65da49d/go.mod h1:og/KbejR/zSQ8otapODEDU9zYNhFSUDbq9+tgeYePyU=
github.com/sei-protocol/sei-db v0.0.39 h1:/XwwlObPhWnX8zH8GDbDvyn+a/K2911HZlmlBZzN+gQ=
github.com/sei-protocol/sei-db v0.0.39/go.mod h1:F/ZKZA8HJPcUzSZPA8yt6pfwlGriJ4RDR4eHKSGLStI=
github.com/sei-protocol/sei-iavl v0.1.9 h1:y4mVYftxLNRs6533zl7N0/Ch+CzRQc04JDfHolIxgBE=
Expand Down
18 changes: 16 additions & 2 deletions x/evm/ante/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,35 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"

"github.com/sei-protocol/sei-chain/x/evm/keeper"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
)

type BasicDecorator struct {
k *keeper.Keeper
}

func NewBasicDecorator() *BasicDecorator {
return &BasicDecorator{}
func NewBasicDecorator(k *keeper.Keeper) *BasicDecorator {
return &BasicDecorator{k}
}

// cherrypicked from go-ethereum:txpool:ValidateTransaction
func (gl BasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
msg := evmtypes.MustGetEVMTransactionMessage(tx)
etx, _ := msg.AsTransaction()

if msg.Derived != nil && !gl.k.EthReplayConfig.Enabled && !gl.k.EthBlockTestConfig.Enabled {
startingNonce := gl.k.GetNonce(ctx, msg.Derived.SenderEVMAddr)
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
ctx = ctx.WithDeliverTxCallback(func(callCtx sdk.Context) {

Check failure on line 32 in x/evm/ante/basic.go

View workflow job for this annotation

GitHub Actions / forward-compatibility

ctx.WithDeliverTxCallback undefined (type "github.com/cosmos/cosmos-sdk/types".Context has no field or method WithDeliverTxCallback)
// bump nonce if it is for some reason not incremented (e.g. ante failure)
if gl.k.GetNonce(callCtx, msg.Derived.SenderEVMAddr) == startingNonce {
gl.k.SetNonce(callCtx, msg.Derived.SenderEVMAddr, startingNonce+1)
}

Check warning on line 36 in x/evm/ante/basic.go

View check run for this annotation

Codecov / codecov/patch

x/evm/ante/basic.go#L30-L36

Added lines #L30 - L36 were not covered by tests
})
}
}

if etx.To() == nil && len(etx.Data()) > params.MaxInitCodeSize {
return ctx, fmt.Errorf("%w: code size %v, limit %v", core.ErrMaxInitCodeSizeExceeded, len(etx.Data()), params.MaxInitCodeSize)
}
Expand Down
4 changes: 2 additions & 2 deletions x/evm/ante/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

func TestBasicDecorator(t *testing.T) {
_, ctx := testkeeper.MockEVMKeeper()
a := ante.NewBasicDecorator()
k, ctx := testkeeper.MockEVMKeeper()
a := ante.NewBasicDecorator(k)
msg, _ := types.NewMsgEVMTransaction(&ethtx.LegacyTx{})
ctx, err := a.AnteHandle(ctx, &mockTx{msgs: []sdk.Msg{msg}}, false, func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
Expand Down
36 changes: 36 additions & 0 deletions x/evm/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,39 @@ func TestCW2981PointerToERC2981(t *testing.T) {
require.Nil(t, err)
require.Equal(t, fmt.Sprintf("{\"address\":\"%s\",\"royalty_amount\":\"1000\"}", seiAddr.String()), string(ret))
}

func TestNonceIncrementsForInsufficientFunds(t *testing.T) {
k := testkeeper.EVMTestApp.EvmKeeper
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{}).WithBlockTime(time.Now())
privKey := testkeeper.MockPrivateKey()
seiAddr, evmAddr := testkeeper.PrivateKeyToAddresses(privKey)
k.SetAddressMapping(ctx, seiAddr, evmAddr)
testPrivHex := hex.EncodeToString(privKey.Bytes())
key, _ := crypto.HexToECDSA(testPrivHex)
txData := ethtypes.LegacyTx{
Nonce: 0,
GasPrice: big.NewInt(1000000000),
Gas: 5000000,
To: nil,
Data: []byte{},
}
chainID := k.ChainID(ctx)
chainCfg := types.DefaultChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
blockNum := big.NewInt(ctx.BlockHeight())
signer := ethtypes.MakeSigner(ethCfg, blockNum, uint64(ctx.BlockTime().Unix()))
tx, err := ethtypes.SignTx(ethtypes.NewTx(&txData), signer, key)
require.Nil(t, err)
typedTx, err := ethtx.NewLegacyTx(tx)
require.Nil(t, err)
msg, err := types.NewMsgEVMTransaction(typedTx)
require.Nil(t, err)
txBuilder := testkeeper.EVMTestApp.GetTxConfig().NewTxBuilder()
txBuilder.SetMsgs(msg)
cosmosTx := txBuilder.GetTx()
txbz, err := testkeeper.EVMTestApp.GetTxConfig().TxEncoder()(cosmosTx)
require.Nil(t, err)
res := testkeeper.EVMTestApp.DeliverTx(ctx, abci.RequestDeliverTx{Tx: txbz}, cosmosTx, sha256.Sum256(txbz))
require.Equal(t, uint32(5), res.Code) // insufficient funds has error code 5
require.Equal(t, uint64(1), k.GetNonce(ctx, evmAddr)) // make sure nonce is incremented regardless
}

0 comments on commit 7e4862f

Please sign in to comment.