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

refactor(mempool)!: match server/v2/cometbft and sdk mempool interface #21744

Merged
merged 13 commits into from
Sep 18, 2024
Next Next commit
feat(server/v2/cometbft): wire mempool config
  • Loading branch information
julienrbrt committed Sep 16, 2024
commit 741842f6ad9f036a8e64e0d797139c1cd7b6b743
6 changes: 6 additions & 0 deletions server/v2/cometbft/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cometbft

import (
"cosmossdk.io/server/v2/cometbft/mempool"

cmtcfg "github.com/cometbft/cometbft/config"
)

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.
// Options are func that are able to take CometBFT app.toml section config and config.toml config.
type ServerOptions[T transaction.Tx] struct {
Mempool mempool.Mempool[T]
Mempool func(cfg map[string]any) mempool.Mempool[T]
PrepareProposalHandler handlers.PrepareHandler[T]
ProcessProposalHandler handlers.ProcessHandler[T]
VerifyVoteExtensionHandler handlers.VerifyVoteExtensionhandler
ExtendVoteHandler handlers.ExtendVoteHandler

SnapshotOptions snapshots.SnapshotOptions
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]{},
Mempool: func(cfg map[string]any) mempool.Mempool[T] { return mempool.NoOpMempool[T]{} },
PrepareProposalHandler: handlers.NoOpPrepareProposal[T](),
ProcessProposalHandler: handlers.NoOpProcessProposal[T](),
VerifyVoteExtensionHandler: handlers.NoOpVerifyVoteExtensionHandler(),
ExtendVoteHandler: handlers.NoOpExtendVote(),
SnapshotOptions: snapshots.NewSnapshotOptions(0, 0),
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
34 changes: 34 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,32 @@ 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]()

// TOOD mempool interface doesn't match!

// overwrite app mempool, using max-txs option
// if maxTxs := cast.ToInt(cfg.Get(cometbft.FlagMempoolMaxTxs)); maxTxs >= 0 {
// serverOptions.Mempool = mempool.NewSenderNonceMempool(
// mempool.SenderNonceMaxTxOpt(maxTxs),
// )
// }

return serverOptions
}
Loading