Skip to content

Commit f4a9361

Browse files
committed
Add lightweight nonce check before proposing block
1 parent 86cbc5f commit f4a9361

File tree

22 files changed

+411
-27
lines changed

22 files changed

+411
-27
lines changed

app/abci.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package app
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
sdk "github.com/cosmos/cosmos-sdk/types"
9+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
810
"github.com/sei-protocol/sei-chain/utils/metrics"
11+
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
912
abci "github.com/tendermint/tendermint/abci/types"
1013
"go.opentelemetry.io/otel/attribute"
1114
)
@@ -38,6 +41,26 @@ func (app *App) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abci.Re
3841
return app.BaseApp.CheckTx(ctx, req)
3942
}
4043

44+
func (app *App) CheckTxWrapped(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTxV2, any, error) {
45+
_, span := app.GetBaseApp().TracingInfo.Start("CheckTxWrapped")
46+
defer span.End()
47+
tx, err := app.txDecoder(req.Tx)
48+
if err != nil {
49+
res := sdkerrors.ResponseCheckTx(err, 0, 0, false)
50+
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, nil, err
51+
}
52+
res, err := app.BaseApp.CheckTxDecoded(tx, req)
53+
if err != nil {
54+
return res, nil, err
55+
}
56+
if tx != nil && len(tx.GetMsgs()) > 0 {
57+
if evmMsg, ok := tx.GetMsgs()[0].(*evmtypes.MsgEVMTransaction); ok {
58+
return res, evmMsg, nil
59+
}
60+
}
61+
return res, nil, nil
62+
}
63+
4164
func (app *App) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, tx sdk.Tx, checksum [32]byte) abci.ResponseDeliverTx {
4265
defer metrics.MeasureDeliverTxDuration(time.Now())
4366
// ensure we carry the initial context from tracer here
@@ -80,3 +103,12 @@ func (app *App) LoadLatest(ctx context.Context, req *abci.RequestLoadLatest) (*a
80103
app.mounter()
81104
return app.BaseApp.LoadLatest(ctx, req)
82105
}
106+
107+
func (app *App) CheckNonce(ctx context.Context, req any, index int) (bool, error) {
108+
sdkCtx := app.GetCheckCtx()
109+
msg, ok := req.(*evmtypes.MsgEVMTransaction)
110+
if !ok {
111+
return false, fmt.Errorf("invalid request type: %T", req)
112+
}
113+
return app.EvmKeeper.CheckNonce(sdkCtx, msg, index)
114+
}

sei-cosmos/baseapp/abci.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,15 @@ func (app *BaseApp) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) (res abc
208208
// the ResponseCheckTx will contain relevant gas execution context.
209209
func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTxV2, error) {
210210
defer telemetry.MeasureSince(time.Now(), "abci", "check_tx")
211+
tx, err := app.txDecoder(req.Tx)
212+
if err != nil {
213+
res := sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
214+
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err
215+
}
216+
return app.CheckTxDecoded(tx, req)
217+
}
211218

219+
func (app *BaseApp) CheckTxDecoded(tx sdk.Tx, req *abci.RequestCheckTx) (*abci.ResponseCheckTxV2, error) {
212220
var mode runTxMode
213221

214222
switch {
@@ -223,11 +231,6 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc
223231
}
224232

225233
sdkCtx := app.getContextForTx(mode, req.Tx)
226-
tx, err := app.txDecoder(req.Tx)
227-
if err != nil {
228-
res := sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
229-
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err
230-
}
231234
gInfo, result, _, priority, pendingTxChecker, expireTxHandler, txCtx, err := app.runTx(sdkCtx, mode, tx, sha256.Sum256(req.Tx))
232235
if err != nil {
233236
res := sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
@@ -1258,3 +1261,15 @@ func (app *BaseApp) GetTxPriorityHint(_ context.Context, req *abci.RequestGetTxP
12581261
Priority: priority,
12591262
}, nil
12601263
}
1264+
1265+
func (app *BaseApp) CheckTxWrapped(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTxV2, any, error) {
1266+
res, err := app.CheckTx(ctx, req)
1267+
if err != nil {
1268+
return res, nil, err
1269+
}
1270+
return res, nil, nil
1271+
}
1272+
1273+
func (app *BaseApp) CheckNonce(ctx context.Context, req any, index int) (bool, error) {
1274+
return true, nil
1275+
}

sei-tendermint/abci/client/grpc_client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,12 @@ func (cli *grpcClient) LoadLatest(ctx context.Context, params *types.RequestLoad
190190
func (cli *grpcClient) GetTxPriorityHint(ctx context.Context, req *types.RequestGetTxPriorityHint) (*types.ResponseGetTxPriorityHint, error) {
191191
return cli.client.GetTxPriorityHint(ctx, types.ToRequestGetTxPriorityHint(req).GetGetTxPriorityHint(), grpc.WaitForReady(true))
192192
}
193+
194+
func (cli *grpcClient) CheckNonce(ctx context.Context, req any, index int) (bool, error) {
195+
return false, errors.New("not implemented")
196+
}
197+
198+
func (cli *grpcClient) CheckTxWrapped(ctx context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTxV2, any, error) {
199+
res, err := cli.CheckTx(ctx, req)
200+
return res, nil, err
201+
}

sei-tendermint/abci/client/mocks/client.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sei-tendermint/abci/client/socket_client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ func (cli *socketClient) GetTxPriorityHint(ctx context.Context, req *types.Reque
377377
return res.GetGetTxPriorityHint(), nil
378378
}
379379

380+
func (cli *socketClient) CheckNonce(ctx context.Context, req any, index int) (bool, error) {
381+
return false, errors.New("not implemented")
382+
}
383+
384+
func (cli *socketClient) CheckTxWrapped(ctx context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTxV2, any, error) {
385+
res, err := cli.CheckTx(ctx, req)
386+
return res, nil, err
387+
}
388+
380389
//----------------------------------------
381390

382391
func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {

sei-tendermint/abci/example/kvstore/kvstore.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ func (*Application) CheckTx(_ context.Context, req *types.RequestCheckTx) (*type
209209
return &types.ResponseCheckTxV2{ResponseCheckTx: &types.ResponseCheckTx{Code: code.CodeTypeOK, GasWanted: 1}}, nil
210210
}
211211

212+
func (*Application) CheckTxWrapped(_ context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTxV2, any, error) {
213+
return &types.ResponseCheckTxV2{ResponseCheckTx: &types.ResponseCheckTx{Code: code.CodeTypeOK, GasWanted: 1}}, nil, nil
214+
}
215+
212216
func (app *Application) Commit(_ context.Context) (*types.ResponseCommit, error) {
213217
app.mu.Lock()
214218
defer app.mu.Unlock()

sei-tendermint/abci/types/application.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type Application interface {
1212
Query(context.Context, *RequestQuery) (*ResponseQuery, error) // Query for state
1313

1414
// Mempool Connection
15-
CheckTx(context.Context, *RequestCheckTx) (*ResponseCheckTxV2, error) // Validate a tx for the mempool
15+
CheckTx(context.Context, *RequestCheckTx) (*ResponseCheckTxV2, error) // Validate a tx for the mempool; deprecated
16+
CheckTxWrapped(context.Context, *RequestCheckTx) (*ResponseCheckTxV2, any, error) // Validate a tx for the mempool
17+
CheckNonce(context.Context, any, int) (bool, error)
1618

1719
// Consensus Connection
1820
InitChain(context.Context, *RequestInitChain) (*ResponseInitChain, error) // Initialize blockchain w validators/other info from TendermintCore
@@ -56,6 +58,14 @@ func (BaseApplication) CheckTx(_ context.Context, req *RequestCheckTx) (*Respons
5658
return &ResponseCheckTxV2{ResponseCheckTx: &ResponseCheckTx{Code: CodeTypeOK}}, nil
5759
}
5860

61+
func (BaseApplication) CheckTxWrapped(_ context.Context, req *RequestCheckTx) (*ResponseCheckTxV2, any, error) {
62+
return &ResponseCheckTxV2{ResponseCheckTx: &ResponseCheckTx{Code: CodeTypeOK}}, nil, nil
63+
}
64+
65+
func (BaseApplication) CheckNonce(_ context.Context, req any, index int) (bool, error) {
66+
return true, nil
67+
}
68+
5969
func (BaseApplication) Commit(_ context.Context) (*ResponseCommit, error) {
6070
return &ResponseCommit{}, nil
6171
}

sei-tendermint/abci/types/mocks/application.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sei-tendermint/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ type MempoolConfig struct {
862862
//
863863
// See DropUtilisationThreshold and DropPriorityThreshold.
864864
DropPriorityReservoirSize int `mapstructure:"drop-priority-reservoir-size"`
865+
866+
// Do a JIT check of nonce before proposing a block to make sure no transaction
867+
// with invalid nonce is included in the block.
868+
CheckNonceBeforePropose bool `mapstructure:"check-nonce-before-propose"`
865869
}
866870

867871
// DefaultMempoolConfig returns a default configuration for the Tendermint mempool.
@@ -888,6 +892,7 @@ func DefaultMempoolConfig() *MempoolConfig {
888892
DropPriorityThreshold: 0.1,
889893
DropUtilisationThreshold: 1.0,
890894
DropPriorityReservoirSize: 10_240,
895+
CheckNonceBeforePropose: false,
891896
}
892897
}
893898

sei-tendermint/config/toml.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ drop-utilisation-threshold = {{ .Mempool.DropUtilisationThreshold }}
457457
# See DropUtilisationThreshold and DropPriorityThreshold.
458458
drop-priority-reservoir-size = {{ .Mempool.DropPriorityReservoirSize }}
459459
460+
check-nonce-before-propose = {{ .Mempool.CheckNonceBeforePropose }}
461+
460462
#######################################################
461463
### State Sync Configuration Options ###
462464
#######################################################

0 commit comments

Comments
 (0)