From 52ba264c80afe77bce848b0629601d3cc8fd7cfa Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 16 Sep 2024 20:45:21 +0200 Subject: [PATCH] feat(server/v2/cometbft): wire mempool config (#21741) --- server/v2/cometbft/config.go | 6 +++++ server/v2/cometbft/flags.go | 13 +++++----- server/v2/cometbft/mempool/config.go | 19 +++++++++----- server/v2/cometbft/mempool/doc.go | 6 +---- server/v2/cometbft/options.go | 9 ++++--- server/v2/cometbft/server.go | 6 +++-- server/v2/commands.go | 4 +-- simapp/v2/simdv2/cmd/commands.go | 6 ++++- simapp/v2/simdv2/cmd/config.go | 38 ++++++++++++++++++++++++++++ 9 files changed, 80 insertions(+), 27 deletions(-) diff --git a/server/v2/cometbft/config.go b/server/v2/cometbft/config.go index 81f3aeb33354..4525e7563c27 100644 --- a/server/v2/cometbft/config.go +++ b/server/v2/cometbft/config.go @@ -2,6 +2,8 @@ package cometbft import ( cmtcfg "github.com/cometbft/cometbft/config" + + "cosmossdk.io/server/v2/cometbft/mempool" ) // Config is the configuration for the CometBFT application @@ -20,6 +22,7 @@ func DefaultAppTomlConfig() *AppTomlConfig { Transport: "socket", Trace: false, Standalone: false, + Mempool: mempool.DefaultConfig(), } } @@ -32,6 +35,9 @@ type AppTomlConfig struct { Transport string `mapstructure:"transport" toml:"transport" comment:"transport defines the CometBFT RPC server transport protocol: socket, grpc"` Trace bool `mapstructure:"trace" toml:"trace" comment:"trace enables the CometBFT RPC server to output trace information about its internal operations."` Standalone bool `mapstructure:"standalone" toml:"standalone" comment:"standalone starts the application without the CometBFT node. The node should be started separately."` + + // Sub configs + Mempool mempool.Config `mapstructure:"mempool" toml:"mempool" comment:"mempool defines the configuration for the SDK built-in app-side mempool implementations."` } // CfgOption is a function that allows to overwrite the default server configuration. diff --git a/server/v2/cometbft/flags.go b/server/v2/cometbft/flags.go index 55fa0a14e771..216755f9885b 100644 --- a/server/v2/cometbft/flags.go +++ b/server/v2/cometbft/flags.go @@ -51,10 +51,11 @@ func prefix(f string) string { // Server flags var ( - Standalone = prefix("standalone") - FlagAddress = prefix("address") - FlagTransport = prefix("transport") - FlagHaltHeight = prefix("halt-height") - FlagHaltTime = prefix("halt-time") - FlagTrace = prefix("trace") + Standalone = prefix("standalone") + FlagAddress = prefix("address") + FlagTransport = prefix("transport") + FlagHaltHeight = prefix("halt-height") + FlagHaltTime = prefix("halt-time") + FlagTrace = prefix("trace") + FlagMempoolMaxTxs = prefix("mempool.max-txs") ) diff --git a/server/v2/cometbft/mempool/config.go b/server/v2/cometbft/mempool/config.go index 38de18844107..e07d6a6e7167 100644 --- a/server/v2/cometbft/mempool/config.go +++ b/server/v2/cometbft/mempool/config.go @@ -1,11 +1,16 @@ package mempool -// Config defines the configurations for the SDK built-in app-side mempool -// implementations. +var DefaultMaxTx = -1 + +// Config defines the configurations for the SDK built-in app-side mempool implementations. type Config struct { - // MaxTxs defines the behavior of the mempool. A negative value indicates - // the mempool is disabled entirely, zero indicates that the mempool is - // unbounded in how many txs it may contain, and a positive value indicates - // the maximum amount of txs it may contain. - MaxTxs int `mapstructure:"max-txs"` + // MaxTxs defines the maximum number of transactions that can be in the mempool. + MaxTxs int `mapstructure:"max-txs" toml:"max-txs" comment:"max-txs defines the maximum number of transactions that can be in the mempool. A value of 0 indicates an unbounded mempool, a negative value disables the app-side mempool."` +} + +// DefaultConfig returns a default configuration for the SDK built-in app-side mempool implementations. +func DefaultConfig() Config { + return Config{ + MaxTxs: DefaultMaxTx, + } } diff --git a/server/v2/cometbft/mempool/doc.go b/server/v2/cometbft/mempool/doc.go index f9857f3a9fae..eb6c72aced18 100644 --- a/server/v2/cometbft/mempool/doc.go +++ b/server/v2/cometbft/mempool/doc.go @@ -1,6 +1,2 @@ -/* -The mempool package defines a few mempool services which can be used in conjunction with your consensus implementation - -*/ - +// Package mempool defines a few mempool services which can be used in conjunction with your consensus implementation. package mempool diff --git a/server/v2/cometbft/options.go b/server/v2/cometbft/options.go index 1e0a389882e0..ba65f3084dc4 100644 --- a/server/v2/cometbft/options.go +++ b/server/v2/cometbft/options.go @@ -9,14 +9,15 @@ import ( ) // ServerOptions defines the options for the CometBFT server. +// When an option takes a map[string]any, it can access the app.tom's cometbft section and the config.toml config. type ServerOptions[T transaction.Tx] struct { - Mempool mempool.Mempool[T] PrepareProposalHandler handlers.PrepareHandler[T] ProcessProposalHandler handlers.ProcessHandler[T] VerifyVoteExtensionHandler handlers.VerifyVoteExtensionhandler ExtendVoteHandler handlers.ExtendVoteHandler - SnapshotOptions snapshots.SnapshotOptions + Mempool func(cfg map[string]any) mempool.Mempool[T] + SnapshotOptions func(cfg map[string]any) snapshots.SnapshotOptions AddrPeerFilter types.PeerFilter // filter peers by address and port IdPeerFilter types.PeerFilter // filter peers by node ID @@ -26,12 +27,12 @@ type ServerOptions[T transaction.Tx] struct { // It defaults to a NoOpMempool and NoOp handlers. func DefaultServerOptions[T transaction.Tx]() ServerOptions[T] { return ServerOptions[T]{ - Mempool: mempool.NoOpMempool[T]{}, PrepareProposalHandler: handlers.NoOpPrepareProposal[T](), ProcessProposalHandler: handlers.NoOpProcessProposal[T](), VerifyVoteExtensionHandler: handlers.NoOpVerifyVoteExtensionHandler(), ExtendVoteHandler: handlers.NoOpExtendVote(), - SnapshotOptions: snapshots.NewSnapshotOptions(0, 0), + Mempool: func(cfg map[string]any) mempool.Mempool[T] { return mempool.NoOpMempool[T]{} }, + SnapshotOptions: func(cfg map[string]any) snapshots.SnapshotOptions { return snapshots.NewSnapshotOptions(0, 0) }, AddrPeerFilter: nil, IdPeerFilter: nil, } diff --git a/server/v2/cometbft/server.go b/server/v2/cometbft/server.go index 26cd99ff97cd..717a3dfe7629 100644 --- a/server/v2/cometbft/server.go +++ b/server/v2/cometbft/server.go @@ -24,6 +24,7 @@ import ( "cosmossdk.io/log" serverv2 "cosmossdk.io/server/v2" cometlog "cosmossdk.io/server/v2/cometbft/log" + "cosmossdk.io/server/v2/cometbft/mempool" "cosmossdk.io/server/v2/cometbft/types" "cosmossdk.io/store/v2/snapshots" @@ -105,7 +106,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg appI.Name(), appI.GetConsensusAuthority(), appI.GetAppManager(), - s.serverOptions.Mempool, + s.serverOptions.Mempool(cfg), indexEvents, appI.GetGPRCMethodsToMessageMap(), store, @@ -127,7 +128,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg if err != nil { return err } - consensus.snapshotManager = snapshots.NewManager(snapshotStore, s.serverOptions.SnapshotOptions, sc, ss, nil, s.logger) + consensus.snapshotManager = snapshots.NewManager(snapshotStore, s.serverOptions.SnapshotOptions(cfg), sc, ss, nil, s.logger) s.Consensus = consensus @@ -240,6 +241,7 @@ func (s *CometBFTServer[T]) StartCmdFlags() *pflag.FlagSet { flags.Uint64(FlagHaltTime, 0, "Minimum block time (in Unix seconds) at which to gracefully halt the chain and shutdown the node") flags.Bool(FlagTrace, false, "Provide full stack traces for errors in ABCI Log") flags.Bool(Standalone, false, "Run app without CometBFT") + flags.Int(FlagMempoolMaxTxs, mempool.DefaultMaxTx, "Sets MaxTx value for the app-side mempool") // add comet flags, we use an empty command to avoid duplicating CometBFT's AddNodeFlags. // we can then merge the flag sets. diff --git a/server/v2/commands.go b/server/v2/commands.go index 8651074f406f..da8e0ec6e537 100644 --- a/server/v2/commands.go +++ b/server/v2/commands.go @@ -38,14 +38,14 @@ func AddCommands[T transaction.Tx]( rootCmd *cobra.Command, newApp AppCreator[T], logger log.Logger, - serverCfg ServerConfig, + globalServerCfg ServerConfig, components ...ServerComponent[T], ) error { if len(components) == 0 { return errors.New("no components provided") } - server := NewServer(logger, serverCfg, components...) + server := NewServer(logger, globalServerCfg, components...) originalPersistentPreRunE := rootCmd.PersistentPreRunE rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { // set the default command outputs diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index d58619170e0d..916f4b2be9da 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -74,7 +74,11 @@ func initRootCmd[T transaction.Tx]( newApp, logger, initServerConfig(), - cometbft.New(&genericTxDecoder[T]{txConfig}, cometbft.DefaultServerOptions[T]()), + cometbft.New( + &genericTxDecoder[T]{txConfig}, + initCometOptions[T](), + initCometConfig(), + ), grpc.New[T](), store.New[T](newApp), ); err != nil { diff --git a/simapp/v2/simdv2/cmd/config.go b/simapp/v2/simdv2/cmd/config.go index c7f6b707c89c..ec222cf8846f 100644 --- a/simapp/v2/simdv2/cmd/config.go +++ b/simapp/v2/simdv2/cmd/config.go @@ -2,8 +2,13 @@ package cmd import ( "strings" + "time" + cmtcfg "github.com/cometbft/cometbft/config" + + "cosmossdk.io/core/transaction" serverv2 "cosmossdk.io/server/v2" + "cosmossdk.io/server/v2/cometbft" clientconfig "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -68,3 +73,36 @@ func initServerConfig() serverv2.ServerConfig { return serverCfg } + +// initCometConfig helps to override default comet config template and configs. +func initCometConfig() cometbft.CfgOption { + cfg := cmtcfg.DefaultConfig() + + // display only warn logs by default except for p2p and state + cfg.LogLevel = "*:warn,p2p:info,state:info" + // increase block timeout + cfg.Consensus.TimeoutCommit = 5 * time.Second + // overwrite default pprof listen address + cfg.RPC.PprofListenAddress = "localhost:6060" + + return cometbft.OverwriteDefaultConfigTomlConfig(cfg) +} + +func initCometOptions[T transaction.Tx]() cometbft.ServerOptions[T] { + serverOptions := cometbft.DefaultServerOptions[T]() + + // TODO mempool interface doesn't match! + + // overwrite app mempool, using max-txs option + // serverOptions.Mempool = func(cfg map[string]any) mempool.Mempool[T] { + // if maxTxs := cast.ToInt(cfg[cometbft.FlagMempoolMaxTxs]); maxTxs >= 0 { + // return mempool.NewSenderNonceMempool( + // mempool.SenderNonceMaxTxOpt(maxTxs), + // ) + // } + + // return mempool.NoOpMempool[T]{} + // } + + return serverOptions +}