github.com/cosmos/cosmos-sdk@v0.50.10/server/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/spf13/viper"
     8  
     9  	pruningtypes "cosmossdk.io/store/pruning/types"
    10  
    11  	"github.com/cosmos/cosmos-sdk/telemetry"
    12  	sdk "github.com/cosmos/cosmos-sdk/types"
    13  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    14  )
    15  
    16  const (
    17  	defaultMinGasPrices = ""
    18  
    19  	// DefaultAPIAddress defines the default address to bind the API server to.
    20  	DefaultAPIAddress = "tcp://localhost:1317"
    21  
    22  	// DefaultGRPCAddress defines the default address to bind the gRPC server to.
    23  	DefaultGRPCAddress = "localhost:9090"
    24  
    25  	// DefaultGRPCMaxRecvMsgSize defines the default gRPC max message size in
    26  	// bytes the server can receive.
    27  	DefaultGRPCMaxRecvMsgSize = 1024 * 1024 * 10
    28  
    29  	// DefaultGRPCMaxSendMsgSize defines the default gRPC max message size in
    30  	// bytes the server can send.
    31  	DefaultGRPCMaxSendMsgSize = math.MaxInt32
    32  )
    33  
    34  // BaseConfig defines the server's basic configuration
    35  type BaseConfig struct {
    36  	// The minimum gas prices a validator is willing to accept for processing a
    37  	// transaction. A transaction's fees must meet the minimum of any denomination
    38  	// specified in this config (e.g. 0.25token1;0.0001token2).
    39  	MinGasPrices string `mapstructure:"minimum-gas-prices"`
    40  
    41  	// The maximum amount of gas a grpc/Rest query may consume.
    42  	// If set to 0, it is unbounded.
    43  	QueryGasLimit uint64 `mapstructure:"query-gas-limit"`
    44  
    45  	Pruning           string `mapstructure:"pruning"`
    46  	PruningKeepRecent string `mapstructure:"pruning-keep-recent"`
    47  	PruningInterval   string `mapstructure:"pruning-interval"`
    48  
    49  	// HaltHeight contains a non-zero block height at which a node will gracefully
    50  	// halt and shutdown that can be used to assist upgrades and testing.
    51  	//
    52  	// Note: Commitment of state will be attempted on the corresponding block.
    53  	HaltHeight uint64 `mapstructure:"halt-height"`
    54  
    55  	// HaltTime contains a non-zero minimum block time (in Unix seconds) at which
    56  	// a node will gracefully halt and shutdown that can be used to assist
    57  	// upgrades and testing.
    58  	//
    59  	// Note: Commitment of state will be attempted on the corresponding block.
    60  	HaltTime uint64 `mapstructure:"halt-time"`
    61  
    62  	// MinRetainBlocks defines the minimum block height offset from the current
    63  	// block being committed, such that blocks past this offset may be pruned
    64  	// from CometBFT. It is used as part of the process of determining the
    65  	// ResponseCommit.RetainHeight value during ABCI Commit. A value of 0 indicates
    66  	// that no blocks should be pruned.
    67  	//
    68  	// This configuration value is only responsible for pruning CometBFT blocks.
    69  	// It has no bearing on application state pruning which is determined by the
    70  	// "pruning-*" configurations.
    71  	//
    72  	// Note: CometBFT block pruning is dependant on this parameter in conjunction
    73  	// with the unbonding (safety threshold) period, state pruning and state sync
    74  	// snapshot parameters to determine the correct minimum value of
    75  	// ResponseCommit.RetainHeight.
    76  	MinRetainBlocks uint64 `mapstructure:"min-retain-blocks"`
    77  
    78  	// InterBlockCache enables inter-block caching.
    79  	InterBlockCache bool `mapstructure:"inter-block-cache"`
    80  
    81  	// IndexEvents defines the set of events in the form {eventType}.{attributeKey},
    82  	// which informs CometBFT what to index. If empty, all events will be indexed.
    83  	IndexEvents []string `mapstructure:"index-events"`
    84  
    85  	// IavlCacheSize set the size of the iavl tree cache.
    86  	IAVLCacheSize uint64 `mapstructure:"iavl-cache-size"`
    87  
    88  	// IAVLDisableFastNode enables or disables the fast sync node.
    89  	IAVLDisableFastNode bool `mapstructure:"iavl-disable-fastnode"`
    90  
    91  	// AppDBBackend defines the type of Database to use for the application and snapshots databases.
    92  	// An empty string indicates that the CometBFT config's DBBackend value should be used.
    93  	AppDBBackend string `mapstructure:"app-db-backend"`
    94  }
    95  
    96  // APIConfig defines the API listener configuration.
    97  type APIConfig struct {
    98  	// Enable defines if the API server should be enabled.
    99  	Enable bool `mapstructure:"enable"`
   100  
   101  	// Swagger defines if swagger documentation should automatically be registered.
   102  	Swagger bool `mapstructure:"swagger"`
   103  
   104  	// EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk)
   105  	EnableUnsafeCORS bool `mapstructure:"enabled-unsafe-cors"`
   106  
   107  	// Address defines the API server to listen on
   108  	Address string `mapstructure:"address"`
   109  
   110  	// MaxOpenConnections defines the number of maximum open connections
   111  	MaxOpenConnections uint `mapstructure:"max-open-connections"`
   112  
   113  	// RPCReadTimeout defines the CometBFT RPC read timeout (in seconds)
   114  	RPCReadTimeout uint `mapstructure:"rpc-read-timeout"`
   115  
   116  	// RPCWriteTimeout defines the CometBFT RPC write timeout (in seconds)
   117  	RPCWriteTimeout uint `mapstructure:"rpc-write-timeout"`
   118  
   119  	// RPCMaxBodyBytes defines the CometBFT maximum request body (in bytes)
   120  	RPCMaxBodyBytes uint `mapstructure:"rpc-max-body-bytes"`
   121  
   122  	// TODO: TLS/Proxy configuration.
   123  	//
   124  	// Ref: https://github.com/cosmos/cosmos-sdk/issues/6420
   125  }
   126  
   127  // GRPCConfig defines configuration for the gRPC server.
   128  type GRPCConfig struct {
   129  	// Enable defines if the gRPC server should be enabled.
   130  	Enable bool `mapstructure:"enable"`
   131  
   132  	// Address defines the API server to listen on
   133  	Address string `mapstructure:"address"`
   134  
   135  	// MaxRecvMsgSize defines the max message size in bytes the server can receive.
   136  	// The default value is 10MB.
   137  	MaxRecvMsgSize int `mapstructure:"max-recv-msg-size"`
   138  
   139  	// MaxSendMsgSize defines the max message size in bytes the server can send.
   140  	// The default value is math.MaxInt32.
   141  	MaxSendMsgSize int `mapstructure:"max-send-msg-size"`
   142  }
   143  
   144  // GRPCWebConfig defines configuration for the gRPC-web server.
   145  type GRPCWebConfig struct {
   146  	// Enable defines if the gRPC-web should be enabled.
   147  	Enable bool `mapstructure:"enable"`
   148  }
   149  
   150  // StateSyncConfig defines the state sync snapshot configuration.
   151  type StateSyncConfig struct {
   152  	// SnapshotInterval sets the interval at which state sync snapshots are taken.
   153  	// 0 disables snapshots.
   154  	SnapshotInterval uint64 `mapstructure:"snapshot-interval"`
   155  
   156  	// SnapshotKeepRecent sets the number of recent state sync snapshots to keep.
   157  	// 0 keeps all snapshots.
   158  	SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"`
   159  }
   160  
   161  // MempoolConfig defines the configurations for the SDK built-in app-side mempool
   162  // implementations.
   163  type MempoolConfig struct {
   164  	// MaxTxs defines the behavior of the mempool. A negative value indicates
   165  	// the mempool is disabled entirely, zero indicates that the mempool is
   166  	// unbounded in how many txs it may contain, and a positive value indicates
   167  	// the maximum amount of txs it may contain.
   168  	MaxTxs int `mapstructure:"max-txs"`
   169  }
   170  
   171  // State Streaming configuration
   172  type (
   173  	// StreamingConfig defines application configuration for external streaming services
   174  	StreamingConfig struct {
   175  		ABCI ABCIListenerConfig `mapstructure:"abci"`
   176  	}
   177  	// ABCIListenerConfig defines application configuration for ABCIListener streaming service
   178  	ABCIListenerConfig struct {
   179  		Keys          []string `mapstructure:"keys"`
   180  		Plugin        string   `mapstructure:"plugin"`
   181  		StopNodeOnErr bool     `mapstructure:"stop-node-on-err"`
   182  	}
   183  )
   184  
   185  // Config defines the server's top level configuration
   186  type Config struct {
   187  	BaseConfig `mapstructure:",squash"`
   188  
   189  	// Telemetry defines the application telemetry configuration
   190  	Telemetry telemetry.Config `mapstructure:"telemetry"`
   191  	API       APIConfig        `mapstructure:"api"`
   192  	GRPC      GRPCConfig       `mapstructure:"grpc"`
   193  	GRPCWeb   GRPCWebConfig    `mapstructure:"grpc-web"`
   194  	StateSync StateSyncConfig  `mapstructure:"state-sync"`
   195  	Streaming StreamingConfig  `mapstructure:"streaming"`
   196  	Mempool   MempoolConfig    `mapstructure:"mempool"`
   197  }
   198  
   199  // SetMinGasPrices sets the validator's minimum gas prices.
   200  func (c *Config) SetMinGasPrices(gasPrices sdk.DecCoins) {
   201  	c.MinGasPrices = gasPrices.String()
   202  }
   203  
   204  // GetMinGasPrices returns the validator's minimum gas prices based on the set configuration.
   205  func (c *Config) GetMinGasPrices() sdk.DecCoins {
   206  	if c.MinGasPrices == "" {
   207  		return sdk.DecCoins{}
   208  	}
   209  
   210  	gasPrices, err := sdk.ParseDecCoins(c.MinGasPrices)
   211  	if err != nil {
   212  		panic(fmt.Sprintf("invalid minimum gas prices: %v", err))
   213  	}
   214  
   215  	return gasPrices
   216  }
   217  
   218  // DefaultConfig returns server's default configuration.
   219  func DefaultConfig() *Config {
   220  	return &Config{
   221  		BaseConfig: BaseConfig{
   222  			MinGasPrices:        defaultMinGasPrices,
   223  			QueryGasLimit:       0,
   224  			InterBlockCache:     true,
   225  			Pruning:             pruningtypes.PruningOptionDefault,
   226  			PruningKeepRecent:   "0",
   227  			PruningInterval:     "0",
   228  			MinRetainBlocks:     0,
   229  			IndexEvents:         make([]string, 0),
   230  			IAVLCacheSize:       781250,
   231  			IAVLDisableFastNode: false,
   232  			AppDBBackend:        "",
   233  		},
   234  		Telemetry: telemetry.Config{
   235  			Enabled:      false,
   236  			GlobalLabels: [][]string{},
   237  		},
   238  		API: APIConfig{
   239  			Enable:             false,
   240  			Swagger:            false,
   241  			Address:            DefaultAPIAddress,
   242  			MaxOpenConnections: 1000,
   243  			RPCReadTimeout:     10,
   244  			RPCMaxBodyBytes:    1000000,
   245  		},
   246  		GRPC: GRPCConfig{
   247  			Enable:         true,
   248  			Address:        DefaultGRPCAddress,
   249  			MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize,
   250  			MaxSendMsgSize: DefaultGRPCMaxSendMsgSize,
   251  		},
   252  		GRPCWeb: GRPCWebConfig{
   253  			Enable: true,
   254  		},
   255  		StateSync: StateSyncConfig{
   256  			SnapshotInterval:   0,
   257  			SnapshotKeepRecent: 2,
   258  		},
   259  		Streaming: StreamingConfig{
   260  			ABCI: ABCIListenerConfig{
   261  				Keys:          []string{},
   262  				StopNodeOnErr: true,
   263  			},
   264  		},
   265  		Mempool: MempoolConfig{
   266  			MaxTxs: -1,
   267  		},
   268  	}
   269  }
   270  
   271  // GetConfig returns a fully parsed Config object.
   272  func GetConfig(v *viper.Viper) (Config, error) {
   273  	conf := DefaultConfig()
   274  	if err := v.Unmarshal(conf); err != nil {
   275  		return Config{}, fmt.Errorf("error extracting app config: %w", err)
   276  	}
   277  	return *conf, nil
   278  }
   279  
   280  // ValidateBasic returns an error if min-gas-prices field is empty in BaseConfig. Otherwise, it returns nil.
   281  func (c Config) ValidateBasic() error {
   282  	if c.BaseConfig.MinGasPrices == "" {
   283  		return sdkerrors.ErrAppConfig.Wrap("set min gas price in app.toml or flag or env variable")
   284  	}
   285  	if c.Pruning == pruningtypes.PruningOptionEverything && c.StateSync.SnapshotInterval > 0 {
   286  		return sdkerrors.ErrAppConfig.Wrapf(
   287  			"cannot enable state sync snapshots with '%s' pruning setting", pruningtypes.PruningOptionEverything,
   288  		)
   289  	}
   290  
   291  	return nil
   292  }