Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: comet mempool lanes #21960

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 143 additions & 68 deletions api/cosmos/base/abci/v1beta1/abci.pulsar.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ func (app *BaseApp) Info(_ *abci.InfoRequest) (*abci.InfoResponse, error) {
}
}

lanes, defaultLane := app.laneHandler.GetLanes()

return &abci.InfoResponse{
Data: app.name,
Version: app.version,
AppVersion: appVersion,
LastBlockHeight: lastCommitID.Version,
LastBlockAppHash: lastCommitID.Hash,
LanePriorities: lanes,
DefaultLane: defaultLane,
}, nil
}

Expand Down Expand Up @@ -377,6 +381,7 @@ func (app *BaseApp) CheckTx(req *abci.CheckTxRequest) (*abci.CheckTxResponse, er
Log: result.Log,
Data: result.Data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
LaneId: result.LaneId,
}, nil
}

Expand Down
14 changes: 14 additions & 0 deletions baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,17 @@ func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes,
// check if we've reached capacity; if so, we cannot select any more transactions
return ts.totalTxBytes >= maxTxBytes || (maxBlockGas > 0 && (ts.totalTxGas >= maxBlockGas))
}

var _ mempool.Lanes = NoopLaneHandler{}

// NoopLaneHandler is a LaneHandler that does not return any lanes.
// All transactions are considered to be in the default lane.
type NoopLaneHandler struct{}

func (NoopLaneHandler) GetLanes() (map[string]uint32, string) {
return map[string]uint32{}, ""
}

func (NoopLaneHandler) GetTxLane(_ context.Context, _ sdk.Tx) (string, error) {
return "", nil
}
12 changes: 12 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type BaseApp struct {
prepareCheckStater sdk.PrepareCheckStater // logic to run during commit using the checkState
precommiter sdk.Precommiter // logic to run during commit using the deliverState
versionModifier server.VersionModifier // interface to get and set the app version
laneHandler mempool.Lanes // lane handler for the comet mempool

addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
Expand Down Expand Up @@ -938,6 +939,15 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
if err != nil {
return gInfo, nil, anteEvents, err
}
if app.laneHandler != nil {
// if the laneHandler is set, we set the laneId in the result
// this takes advantage of https://github.com/cometbft/cometbft/blob/main/docs/references/architecture/adr-118-mempool-lanes.md
lane, err := app.laneHandler.GetTxLane(ctx, tx)
if err != nil {
return gInfo, nil, anteEvents, err
}
result.LaneId = lane
}
} else if mode == execModeFinalize {
err = app.mempool.Remove(tx)
if err != nil && !errors.Is(err, mempool.ErrTxNotFound) {
Expand Down Expand Up @@ -1003,6 +1013,8 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
}
}

// handleLaned mempool

return gInfo, result, anteEvents, err
}

Expand Down
6 changes: 6 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,9 @@ func (app *BaseApp) SetMsgServiceRouter(msgServiceRouter *MsgServiceRouter) {
func (app *BaseApp) SetGRPCQueryRouter(grpcQueryRouter *GRPCQueryRouter) {
app.grpcQueryRouter = grpcQueryRouter
}

// SetLaneHandler provides baseapp the LaneHandler
// 0 is a reserved lane, order of the lanes does not matter
func (app *BaseApp) SetLaneHandler(laneHandler mempool.Lanes) {
app.laneHandler = laneHandler
}
77 changes: 77 additions & 0 deletions docs/build/abci/04-checktx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# CheckTx

CheckTx is called by the `BaseApp` when comet receives a transaction from a client, over the p2p network or RPC. The CheckTx method is responsible for validating the transaction and returning an error if the transaction is invalid.

```mermaid
graph TD
subgraph SDK[Cosmos SDK]
B[Baseapp]
A[AnteHandlers]
B <-->|Validate TX| A
end
C[CometBFT] <-->|CheckTx|SDK
U((User)) -->|Submit TX| C
N[P2P] -->|Receive TX| C
```

```go
// CheckTx implements the ABCI interface.
func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
var mode execMode

switch {
case req.Type == abci.CHECK_TX_TYPE_CHECK:
mode = execModeCheck

case req.Type == abci.CHECK_TX_TYPE_RECHECK:
mode = execModeReCheck

default:
return nil, fmt.Errorf("unknown RequestCheckTx type: %s", req.Type)
}

gInfo, result, anteEvents, err := app.runTx(mode, req.Tx, nil)
if err != nil {
return responseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace), nil
}

return &abci.CheckTxResponse{
GasWanted: int64(gInfo.GasWanted),
GasUsed: int64(gInfo.GasUsed),
Log: result.Log,
Data: result.Data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
}, nil
}
```

## CheckTx Lane Handler

Comet provides the optionality for application developers to set a lane for a specific type of transaction. This allows the application to set a form of priority for transaction dissemination.

The `LaneHandler` interface is defined as follows:

```go
type Lanes interface {
GetLanes() (lanes map[string]uint32, defaultLane string)
GetTxLane(context.Context, sdk.Tx) (string, error)
}
```

The `LaneHandler` is set on the `BaseApp` using the `SetLaneHandler` method.

```go
func NewSimApp(
logger log.Logger,
db corestore.KVStoreWithBatch,
traceStore io.Writer,
loadLatest bool,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
...
laneHandler := mempool.CustomLaneHandler(map[string]uint32{}, "default")
app.SetLaneHandler(laneHandler)
...
}
```
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ require (
cosmossdk.io/x/tx v0.13.3
github.com/99designs/keyring v1.2.2
github.com/bgentry/speakeasy v0.2.0
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f
github.com/cometbft/cometbft/api v1.0.0-rc.1
github.com/cometbft/cometbft v1.0.0-rc1.0.20240925131815-d341b313d7e2
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240925131815-d341b313d7e2
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22
github.com/cosmos/cosmos-proto v1.0.0-beta.5
Expand Down Expand Up @@ -82,7 +82,7 @@ require (
github.com/cockroachdb/pebble v1.1.2 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v0.15.0 // indirect
github.com/cometbft/cometbft-db v1.0.0 // indirect
github.com/cosmos/iavl v1.3.0 // indirect
github.com/cosmos/ics23/go v0.11.0 // indirect
github.com/danieljoos/wincred v1.2.1 // indirect
Expand All @@ -96,7 +96,6 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-kit/kit v0.13.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
Expand Down Expand Up @@ -144,7 +143,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.11.0 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.5 // indirect
Expand Down
20 changes: 8 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/adlio/schema v1.3.6 h1:k1/zc2jNfeiZBA5aFTRy37jlBIuCkXCm0XmvpzCKI9I=
github.com/adlio/schema v1.3.6/go.mod h1:qkxwLgPBd1FgLRHYVCmQT/rrBr3JH38J9LjmVzWNudg=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -90,12 +88,12 @@ github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwP
github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f h1:rPWKqyc+CeuddICqlqptf+3NPE8exbC9SOGuDPTEN3k=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f/go.mod h1:MqZ5E5jLU1JdP10FSRXhItpm+GdHMbW7Myv3UARLxqg=
github.com/cometbft/cometbft-db v0.15.0 h1:VLtsRt8udD4jHCyjvrsTBpgz83qne5hnL245AcPJVRk=
github.com/cometbft/cometbft-db v0.15.0/go.mod h1:EBrFs1GDRiTqrWXYi4v90Awf/gcdD5ExzdPbg4X8+mk=
github.com/cometbft/cometbft/api v1.0.0-rc.1 h1:GtdXwDGlqwHYs16A4egjwylfYOMYyEacLBrs3Zvpt7g=
github.com/cometbft/cometbft/api v1.0.0-rc.1/go.mod h1:NDFKiBBD8HJC6QQLAoUI99YhsiRZtg2+FJWfk6A6m6o=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240925131815-d341b313d7e2 h1:zMiCop6ZA3mqjhCjI61uMKbIbJuumIwA9PGPkkOTHC4=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240925131815-d341b313d7e2/go.mod h1:vNUsiRz1tV9U0Opnvpw/WxPgvvFIi3Gdm+rkpxwnkas=
github.com/cometbft/cometbft-db v1.0.0 h1:AK805KzaWCzBYnWaUvMRiV4+uoZJIPRYn/VpDfke2ic=
github.com/cometbft/cometbft-db v1.0.0/go.mod h1:EBrFs1GDRiTqrWXYi4v90Awf/gcdD5ExzdPbg4X8+mk=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240925131815-d341b313d7e2 h1:fex0Z7zPKZMxHVSdWchKqkL/Ag9AuLDq+/kzDJNTOIQ=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240925131815-d341b313d7e2/go.mod h1:EkQiqVSu/p2ebrZEnB2z6Re7r8XNe//M7ylR0qEwWm0=
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
Expand Down Expand Up @@ -177,8 +175,6 @@ github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxI
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.13.0 h1:OoneCcHKHQ03LfBpoQCUfCluwd2Vt3ohz+kvbJneZAU=
github.com/go-kit/kit v0.13.0/go.mod h1:phqEHMMUbyrCFCTgH48JueqrM3md2HcAZ8N3XE4FKDg=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
Expand Down Expand Up @@ -426,8 +422,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand Down
2 changes: 2 additions & 0 deletions proto/cosmos/base/abci/v1beta1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@

// msg_responses contains the Msg handler responses type packed in Anys.
repeated google.protobuf.Any msg_responses = 4 [(cosmos_proto.field_added_in) = "cosmos-sdk 0.46"];
// lane_id is used by the comet mempool to identify the lane that the transaction should be broadcast in.
uint32`` lane_id = 5;

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / lint

invalid character

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected error

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / lint

invalid character

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / break-check

invalid character

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / break-check

syntax error: unexpected error

Check failure on line 109 in proto/cosmos/base/abci/v1beta1/abci.proto

View workflow job for this annotation

GitHub Actions / break-check

invalid character
}

// SimulationResponse defines the response generated when a transaction is
Expand Down
Loading
Loading