Skip to content

Commit

Permalink
feat(server/v2/cometbft): wire mempool config (#21741)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Sep 16, 2024
1 parent f348d84 commit 52ba264
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 27 deletions.
6 changes: 6 additions & 0 deletions server/v2/cometbft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +22,7 @@ func DefaultAppTomlConfig() *AppTomlConfig {
Transport: "socket",
Trace: false,
Standalone: false,
Mempool: mempool.DefaultConfig(),
}
}

Expand All @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions server/v2/cometbft/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
19 changes: 12 additions & 7 deletions server/v2/cometbft/mempool/config.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
6 changes: 1 addition & 5 deletions server/v2/cometbft/mempool/doc.go
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions server/v2/cometbft/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions server/v2/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions simapp/v2/simdv2/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 52ba264

Please sign in to comment.