Skip to content

Commit

Permalink
[EVM] Add pending nonce support (#390)
Browse files Browse the repository at this point in the history
- Adding an expiration handler callback that lets the mempool

- sei-protocol/sei-tendermint#179

- e2e testing with hardhat tests
  • Loading branch information
stevenlanders authored and codchen committed Jan 8, 2024
1 parent 1b95f79 commit 33c829f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
5 changes: 3 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc
res := sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err
}

Check warning on line 228 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L226-L228

Added lines #L226 - L228 were not covered by tests
gInfo, result, _, priority, pendingTxChecker, err := app.runTx(sdkCtx, mode, tx, sha256.Sum256(req.Tx))
gInfo, result, _, priority, pendingTxChecker, expireTxHandler, err := app.runTx(sdkCtx, mode, tx, sha256.Sum256(req.Tx))
if err != nil {
res := sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err

Check warning on line 232 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L232

Added line #L232 was not covered by tests
Expand All @@ -241,6 +241,7 @@ func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abc
res.IsPendingTransaction = true
res.Checker = pendingTxChecker
}

Check warning on line 243 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L241-L243

Added lines #L241 - L243 were not covered by tests
res.ExpireTxHandler = expireTxHandler

return res, nil
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func (app *BaseApp) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, tx sdk
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
}()

gInfo, result, anteEvents, _, _, err := app.runTx(ctx.WithTxBytes(req.Tx).WithVoteInfos(app.voteInfos), runTxModeDeliver, tx, checksum)
gInfo, result, anteEvents, _, _, _, err := app.runTx(ctx.WithTxBytes(req.Tx).WithVoteInfos(app.voteInfos), runTxModeDeliver, tx, checksum)
if err != nil {
resultStr = "failed"
// if we have a result, use those events instead of just the anteEvents
Expand Down
14 changes: 8 additions & 6 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [
anteEvents []abci.Event,
priority int64,
pendingTxChecker abci.PendingTxChecker,
expireHandler abci.ExpireTxHandler,
err error,
) {
defer telemetry.MeasureThroughputSinceWithLabels(
Expand Down Expand Up @@ -863,7 +864,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
return gInfo, nil, nil, -1, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
return gInfo, nil, nil, -1, nil, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
}

defer func() {
Expand Down Expand Up @@ -900,13 +901,13 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [
}

if tx == nil {
return sdk.GasInfo{}, nil, nil, 0, nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx decode error")
return sdk.GasInfo{}, nil, nil, 0, nil, nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx decode error")

Check warning on line 904 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L904

Added line #L904 was not covered by tests
}

msgs := tx.GetMsgs()

if err := validateBasicTxMsgs(msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, nil, err
return sdk.GasInfo{}, nil, nil, 0, nil, nil, err
}

if app.anteHandler != nil {
Expand Down Expand Up @@ -948,7 +949,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [
// GasMeter expected to be set in AnteHandler
gasWanted = ctx.GasMeter().Limit()
if err != nil {
return gInfo, nil, nil, 0, nil, err
return gInfo, nil, nil, 0, nil, nil, err
}

// Dont need to validate in checkTx mode
Expand All @@ -963,12 +964,13 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [
op.EmitValidationFailMetrics()
}
errMessage := fmt.Sprintf("Invalid Concurrent Execution antehandler missing %d access operations", len(missingAccessOps))
return gInfo, nil, nil, 0, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidConcurrencyExecution, errMessage)
return gInfo, nil, nil, 0, nil, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidConcurrencyExecution, errMessage)

Check warning on line 967 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L967

Added line #L967 was not covered by tests
}
}

priority = ctx.Priority()
pendingTxChecker = ctx.PendingTxChecker()
expireHandler = ctx.ExpireTxHandler()
msCache.Write()
anteEvents = events.ToABCIEvents()
anteSpan.End()
Expand Down Expand Up @@ -998,7 +1000,7 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [
if ctx.CheckTxCallback() != nil {
ctx.CheckTxCallback()(err)
}

Check warning on line 1002 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L1001-L1002

Added lines #L1001 - L1002 were not covered by tests
return gInfo, result, anteEvents, priority, pendingTxChecker, err
return gInfo, result, anteEvents, priority, pendingTxChecker, expireHandler, err
}

// runMsgs iterates through a list of messages and executes them with the provided
Expand Down
6 changes: 3 additions & 3 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (app *BaseApp) Check(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *sdk
return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
ctx := app.checkState.ctx.WithTxBytes(bz).WithVoteInfos(app.voteInfos).WithConsensusParams(app.GetConsensusParams(app.checkState.ctx))
gasInfo, result, _, _, _, err := app.runTx(ctx, runTxModeCheck, tx, sha256.Sum256(bz))
gasInfo, result, _, _, _, _, err := app.runTx(ctx, runTxModeCheck, tx, sha256.Sum256(bz))
if len(ctx.MultiStore().GetEvents()) > 0 {
panic("Expected checkTx events to be empty")
}
Expand All @@ -31,7 +31,7 @@ func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) {
if err != nil {
return sdk.GasInfo{}, nil, err
}

Check warning on line 33 in baseapp/test_helpers.go

View check run for this annotation

Codecov / codecov/patch

baseapp/test_helpers.go#L32-L33

Added lines #L32 - L33 were not covered by tests
gasInfo, result, _, _, _, err := app.runTx(ctx, runTxModeSimulate, tx, sha256.Sum256(txBytes))
gasInfo, result, _, _, _, _, err := app.runTx(ctx, runTxModeSimulate, tx, sha256.Sum256(txBytes))
if len(ctx.MultiStore().GetEvents()) > 0 {
panic("Expected simulate events to be empty")
}
Expand All @@ -49,7 +49,7 @@ func (app *BaseApp) Deliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *s
if err != nil {
return sdk.GasInfo{}, &sdk.Result{}, err
}

Check warning on line 51 in baseapp/test_helpers.go

View check run for this annotation

Codecov / codecov/patch

baseapp/test_helpers.go#L50-L51

Added lines #L50 - L51 were not covered by tests
gasInfo, result, _, _, _, err := app.runTx(ctx, runTxModeDeliver, decoded, sha256.Sum256(bz))
gasInfo, result, _, _, _, _, err := app.runTx(ctx, runTxModeDeliver, decoded, sha256.Sum256(bz))
return gasInfo, result, err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ replace (
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.24
// Latest goleveldb is broken, we have to stick to this version
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.2.30-evm-2
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.2.35-evm-rebase
// latest grpc doesn't work with with our modified proto compiler, so we need to enforce
// the following version across all dependencies.
google.golang.org/grpc => google.golang.org/grpc v1.33.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,8 @@ github.com/sei-protocol/sei-db v0.0.24 h1:rSidZZ4GNEJRY+0gm5+RioNqmYiOiPaZuDQ+vI
github.com/sei-protocol/sei-db v0.0.24/go.mod h1:F/ZKZA8HJPcUzSZPA8yt6pfwlGriJ4RDR4eHKSGLStI=
github.com/sei-protocol/sei-iavl v0.1.7-seidb-1 h1:Acsjnanr8vhRZ5Dbv5sYnxDa8ibheXrlqLtbsUmLGEo=
github.com/sei-protocol/sei-iavl v0.1.7-seidb-1/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
github.com/sei-protocol/sei-tendermint v0.2.30-evm-2 h1:ibEV/DNT/5zO+A1+pP0Di96raadNv33ieOtMAn/iEr8=
github.com/sei-protocol/sei-tendermint v0.2.30-evm-2/go.mod h1:+BtDvAwTkE64BlxzpH9ZP7S6vUYT9wRXiZa/WW8/o4g=
github.com/sei-protocol/sei-tendermint v0.2.35-evm-rebase h1:qcYyQoa56KaMfH3EH3eiICxq9LgPVETBw3gzBehdKIA=
github.com/sei-protocol/sei-tendermint v0.2.35-evm-rebase/go.mod h1:4LSlJdhl3nf3OmohliwRNUFLOB1XWlrmSodrIP7fLh4=
github.com/sei-protocol/sei-tm-db v0.0.5 h1:3WONKdSXEqdZZeLuWYfK5hP37TJpfaUa13vAyAlvaQY=
github.com/sei-protocol/sei-tm-db v0.0.5/go.mod h1:Cpa6rGyczgthq7/0pI31jys2Fw0Nfrc+/jKdP1prVqY=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
1 change: 1 addition & 0 deletions types/accesscontrol/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ResourceTree = map[ResourceType]TreeNode{
ResourceType_KV_FEEGRANT,
ResourceType_KV_SLASHING,
ResourceType_KV_BANK_DEFERRED,
ResourceType_KV_EVM,
}},
ResourceType_Mem: {ResourceType_ANY, []ResourceType{
ResourceType_DexMem,
Expand Down
10 changes: 10 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Context struct {
priority int64 // The tx priority, only relevant in CheckTx
pendingTxChecker abci.PendingTxChecker // Checker for pending transaction, only relevant in CheckTx
checkTxCallback func(error) // callback to make at the end of CheckTx. Input param is the error (nil-able) of `runMsgs`
expireTxHandler func() // callback that the mempool invokes when a tx is expired

txBlockingChannels acltypes.MessageAccessOpsChannelMapping
txCompletionChannels acltypes.MessageAccessOpsChannelMapping
Expand Down Expand Up @@ -118,6 +119,10 @@ func (c Context) Priority() int64 {
return c.priority
}

func (c Context) ExpireTxHandler() abci.ExpireTxHandler {
return c.expireTxHandler

Check warning on line 123 in types/context.go

View check run for this annotation

Codecov / codecov/patch

types/context.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}

func (c Context) PendingTxChecker() abci.PendingTxChecker {
return c.pendingTxChecker

Check warning on line 127 in types/context.go

View check run for this annotation

Codecov / codecov/patch

types/context.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}
Expand Down Expand Up @@ -369,6 +374,11 @@ func (c Context) WithCheckTxCallback(checkTxCallback func(error)) Context {
return c

Check warning on line 374 in types/context.go

View check run for this annotation

Codecov / codecov/patch

types/context.go#L372-L374

Added lines #L372 - L374 were not covered by tests
}

func (c Context) WithExpireTxHandler(expireTxHandler func()) Context {
c.expireTxHandler = expireTxHandler
return c

Check warning on line 379 in types/context.go

View check run for this annotation

Codecov / codecov/patch

types/context.go#L377-L379

Added lines #L377 - L379 were not covered by tests
}

// TODO: remove???
func (c Context) IsZero() bool {
return c.ms == nil
Expand Down

0 comments on commit 33c829f

Please sign in to comment.