Skip to content

Commit

Permalink
Check TX nonce before registering hook to bump nonce for failed tx (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Aug 28, 2024
1 parent 1e6db8b commit a8ea0f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion x/evm/ante/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func (gl BasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, n

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() {
txNonce := etx.Nonce()
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() && startingNonce == txNonce {
ctx = ctx.WithDeliverTxCallback(func(callCtx sdk.Context) {
// bump nonce if it is for some reason not incremented (e.g. ante failure)
if gl.k.GetNonce(callCtx, msg.Derived.SenderEVMAddr) == startingNonce {
Expand Down
2 changes: 1 addition & 1 deletion x/evm/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (fc EVMFeeCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
// we don't want to run nonce check here for CheckTx because we have special
// logic for pending nonce during CheckTx in sig.go
if err := st.StatelessChecks(); err != nil {
return ctx, err
return ctx, sdkerrors.Wrap(sdkerrors.ErrWrongSequence, err.Error())
}
}
if err := st.BuyGas(); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions x/evm/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ func TestNonceIncrementsForInsufficientFunds(t *testing.T) {
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

// ensure that old txs cannot be used by malicious party to bump nonces
res = testkeeper.EVMTestApp.DeliverTx(ctx, abci.RequestDeliverTx{Tx: txbz}, cosmosTx, sha256.Sum256(txbz))
require.Equal(t, uint32(32), res.Code) // wrong nonce has error code 32
require.Equal(t, uint64(1), k.GetNonce(ctx, evmAddr)) // nonce should not be incremented this time because the tx is an old one
}

func TestInvalidAssociateMsg(t *testing.T) {
Expand Down

0 comments on commit a8ea0f9

Please sign in to comment.