2021-04-18 16:39:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-08-18 14:11:51 +00:00
|
|
|
"errors"
|
2021-08-16 09:45:10 +00:00
|
|
|
"fmt"
|
2021-09-28 11:33:54 +00:00
|
|
|
"path"
|
2021-10-25 15:01:04 +00:00
|
|
|
"time"
|
2021-08-16 09:45:10 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-08-16 09:45:10 +00:00
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/strings"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/config"
|
|
|
|
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultGRPCAddress is the default address the gRPC server binds to.
|
|
|
|
DefaultGRPCAddress = "0.0.0.0:9900"
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
// DefaultJSONRPCAddress is the default address the JSON-RPC server binds to.
|
|
|
|
DefaultJSONRPCAddress = "0.0.0.0:8545"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
// DefaultJSONRPCWsAddress is the default address the JSON-RPC WebSocket server binds to.
|
|
|
|
DefaultJSONRPCWsAddress = "0.0.0.0:8546"
|
|
|
|
|
|
|
|
// DefaultEVMTracer is the default vm.Tracer type
|
2021-11-16 08:57:03 +00:00
|
|
|
DefaultEVMTracer = ""
|
2021-08-18 14:11:51 +00:00
|
|
|
|
2022-07-28 19:14:46 +00:00
|
|
|
DefaultMaxTxGasWanted = 0
|
2022-03-21 09:01:21 +00:00
|
|
|
|
2021-08-18 14:11:51 +00:00
|
|
|
DefaultGasCap uint64 = 25000000
|
2021-10-13 11:03:49 +00:00
|
|
|
|
|
|
|
DefaultFilterCap int32 = 200
|
2021-10-20 19:14:39 +00:00
|
|
|
|
2021-11-17 11:58:52 +00:00
|
|
|
DefaultFeeHistoryCap int32 = 100
|
|
|
|
|
2021-12-29 21:47:25 +00:00
|
|
|
DefaultLogsCap int32 = 10000
|
|
|
|
|
|
|
|
DefaultBlockRangeCap int32 = 10000
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
DefaultEVMTimeout = 5 * time.Second
|
2021-10-20 19:14:39 +00:00
|
|
|
// default 1.0 eth
|
|
|
|
DefaultTxFeeCap float64 = 1.0
|
2022-03-09 07:31:51 +00:00
|
|
|
|
|
|
|
DefaultHTTPTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
DefaultHTTPIdleTimeout = 120 * time.Second
|
2022-06-22 10:50:36 +00:00
|
|
|
// DefaultAllowUnprotectedTxs value is false
|
|
|
|
DefaultAllowUnprotectedTxs = false
|
2022-08-10 12:26:51 +00:00
|
|
|
// DefaultMaxOpenConnections represents the amount of open connections (unlimited = 0)
|
|
|
|
DefaultMaxOpenConnections = 0
|
2021-08-16 09:45:10 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 08:57:03 +00:00
|
|
|
var evmTracers = []string{"json", "markdown", "struct", "access_list"}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
// Config defines the server's top level configuration. It includes the default app config
|
|
|
|
// from the SDK as well as the EVM configuration to enable the JSON-RPC APIs.
|
|
|
|
type Config struct {
|
|
|
|
config.Config
|
|
|
|
|
|
|
|
EVM EVMConfig `mapstructure:"evm"`
|
|
|
|
JSONRPC JSONRPCConfig `mapstructure:"json-rpc"`
|
|
|
|
TLS TLSConfig `mapstructure:"tls"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EVMConfig defines the application configuration values for the EVM.
|
|
|
|
type EVMConfig struct {
|
|
|
|
// Tracer defines vm.Tracer type that the EVM will use if the node is run in
|
|
|
|
// trace mode. Default: 'json'.
|
|
|
|
Tracer string `mapstructure:"tracer"`
|
2022-03-21 09:01:21 +00:00
|
|
|
// MaxTxGasWanted defines the gas wanted for each eth tx returned in ante handler in check tx mode.
|
|
|
|
MaxTxGasWanted uint64 `mapstructure:"max-tx-gas-wanted"`
|
2021-09-28 11:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSONRPCConfig defines configuration for the EVM RPC server.
|
|
|
|
type JSONRPCConfig struct {
|
2021-10-13 11:03:49 +00:00
|
|
|
// API defines a list of JSON-RPC namespaces that should be enabled
|
|
|
|
API []string `mapstructure:"api"`
|
2021-09-28 11:33:54 +00:00
|
|
|
// Address defines the HTTP server to listen on
|
|
|
|
Address string `mapstructure:"address"`
|
|
|
|
// WsAddress defines the WebSocket server to listen on
|
|
|
|
WsAddress string `mapstructure:"ws-address"`
|
|
|
|
// GasCap is the global gas cap for eth-call variants.
|
|
|
|
GasCap uint64 `mapstructure:"gas-cap"`
|
2021-10-25 15:01:04 +00:00
|
|
|
// EVMTimeout is the global timeout for eth-call.
|
|
|
|
EVMTimeout time.Duration `mapstructure:"evm-timeout"`
|
2021-10-20 19:14:39 +00:00
|
|
|
// TxFeeCap is the global tx-fee cap for send transaction
|
|
|
|
TxFeeCap float64 `mapstructure:"txfee-cap"`
|
2021-10-13 11:03:49 +00:00
|
|
|
// FilterCap is the global cap for total number of filters that can be created.
|
|
|
|
FilterCap int32 `mapstructure:"filter-cap"`
|
2021-11-17 11:58:52 +00:00
|
|
|
// FeeHistoryCap is the global cap for total number of blocks that can be fetched
|
|
|
|
FeeHistoryCap int32 `mapstructure:"feehistory-cap"`
|
2021-10-13 11:03:49 +00:00
|
|
|
// Enable defines if the EVM RPC server should be enabled.
|
|
|
|
Enable bool `mapstructure:"enable"`
|
2021-12-29 21:47:25 +00:00
|
|
|
// LogsCap defines the max number of results can be returned from single `eth_getLogs` query.
|
|
|
|
LogsCap int32 `mapstructure:"logs-cap"`
|
|
|
|
// BlockRangeCap defines the max block range allowed for `eth_getLogs` query.
|
|
|
|
BlockRangeCap int32 `mapstructure:"block-range-cap"`
|
2022-03-09 07:31:51 +00:00
|
|
|
// HTTPTimeout is the read/write timeout of http json-rpc server.
|
|
|
|
HTTPTimeout time.Duration `mapstructure:"http-timeout"`
|
|
|
|
// HTTPIdleTimeout is the idle timeout of http json-rpc server.
|
|
|
|
HTTPIdleTimeout time.Duration `mapstructure:"http-idle-timeout"`
|
2022-06-22 10:50:36 +00:00
|
|
|
// AllowUnprotectedTxs restricts unprotected (non EIP155 signed) transactions to be submitted via
|
|
|
|
// the node's RPC when global parameter is disabled.
|
|
|
|
AllowUnprotectedTxs bool `mapstructure:"allow-unprotected-txs"`
|
2022-08-10 12:26:51 +00:00
|
|
|
// MaxOpenConnections sets the maximum number of simultaneous connections
|
|
|
|
// for the server listener.
|
|
|
|
MaxOpenConnections int `mapstructure:"max-open-connections"`
|
2021-09-28 11:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig defines the certificate and matching private key for the server.
|
|
|
|
type TLSConfig struct {
|
|
|
|
// CertificatePath the file path for the certificate .pem file
|
|
|
|
CertificatePath string `mapstructure:"certificate-path"`
|
|
|
|
// KeyPath the file path for the key .pem file
|
|
|
|
KeyPath string `mapstructure:"key-path"`
|
2021-07-26 11:15:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
// AppConfig helps to override default appConfig template and configs.
|
|
|
|
// return "", nil if no custom configuration is required for the application.
|
2021-07-28 09:47:29 +00:00
|
|
|
func AppConfig(denom string) (string, interface{}) {
|
2021-06-29 17:02:21 +00:00
|
|
|
// Optionally allow the chain developer to overwrite the SDK's default
|
|
|
|
// server config.
|
|
|
|
srvCfg := config.DefaultConfig()
|
|
|
|
|
|
|
|
// The SDK's default minimum gas price is set to "" (empty value) inside
|
|
|
|
// app.toml. If left empty by validators, the node will halt on startup.
|
|
|
|
// However, the chain developer can set a default app.toml value for their
|
|
|
|
// validators here.
|
|
|
|
//
|
|
|
|
// In summary:
|
|
|
|
// - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their
|
|
|
|
// own app.toml config,
|
|
|
|
// - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their
|
|
|
|
// own app.toml to override, or use this default value.
|
|
|
|
//
|
|
|
|
// In ethermint, we set the min gas prices to 0.
|
2021-07-28 09:47:29 +00:00
|
|
|
if denom != "" {
|
|
|
|
srvCfg.MinGasPrices = "0" + denom
|
|
|
|
}
|
2021-06-29 17:02:21 +00:00
|
|
|
|
|
|
|
customAppConfig := Config{
|
2021-08-16 09:45:10 +00:00
|
|
|
Config: *srvCfg,
|
|
|
|
EVM: *DefaultEVMConfig(),
|
|
|
|
JSONRPC: *DefaultJSONRPCConfig(),
|
2021-09-28 11:33:54 +00:00
|
|
|
TLS: *DefaultTLSConfig(),
|
2021-06-29 17:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
customAppTemplate := config.DefaultConfigTemplate + DefaultConfigTemplate
|
|
|
|
|
|
|
|
return customAppTemplate, customAppConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig returns server's default configuration.
|
|
|
|
func DefaultConfig() *Config {
|
|
|
|
return &Config{
|
2021-08-16 09:45:10 +00:00
|
|
|
Config: *config.DefaultConfig(),
|
|
|
|
EVM: *DefaultEVMConfig(),
|
|
|
|
JSONRPC: *DefaultJSONRPCConfig(),
|
2021-09-28 11:33:54 +00:00
|
|
|
TLS: *DefaultTLSConfig(),
|
2021-08-16 09:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultEVMConfig returns the default EVM configuration
|
|
|
|
func DefaultEVMConfig() *EVMConfig {
|
|
|
|
return &EVMConfig{
|
2022-03-21 09:01:21 +00:00
|
|
|
Tracer: DefaultEVMTracer,
|
|
|
|
MaxTxGasWanted: DefaultMaxTxGasWanted,
|
2021-08-16 09:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the tracer type is invalid.
|
|
|
|
func (c EVMConfig) Validate() error {
|
2021-11-16 08:57:03 +00:00
|
|
|
if c.Tracer != "" && !strings.StringInSlice(c.Tracer, evmTracers) {
|
2021-08-16 09:45:10 +00:00
|
|
|
return fmt.Errorf("invalid tracer type %s, available types: %v", c.Tracer, evmTracers)
|
2021-06-29 17:02:21 +00:00
|
|
|
}
|
2021-08-16 09:45:10 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-29 17:02:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
// GetDefaultAPINamespaces returns the default list of JSON-RPC namespaces that should be enabled
|
|
|
|
func GetDefaultAPINamespaces() []string {
|
|
|
|
return []string{"eth", "net", "web3"}
|
|
|
|
}
|
|
|
|
|
2021-11-30 09:16:40 +00:00
|
|
|
// GetAPINamespaces returns the all the available JSON-RPC API namespaces.
|
|
|
|
func GetAPINamespaces() []string {
|
|
|
|
return []string{"web3", "eth", "personal", "net", "txpool", "debug", "miner"}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
// DefaultJSONRPCConfig returns an EVM config with the JSON-RPC API enabled by default
|
|
|
|
func DefaultJSONRPCConfig() *JSONRPCConfig {
|
|
|
|
return &JSONRPCConfig{
|
2022-06-22 10:50:36 +00:00
|
|
|
Enable: true,
|
|
|
|
API: GetDefaultAPINamespaces(),
|
|
|
|
Address: DefaultJSONRPCAddress,
|
|
|
|
WsAddress: DefaultJSONRPCWsAddress,
|
|
|
|
GasCap: DefaultGasCap,
|
|
|
|
EVMTimeout: DefaultEVMTimeout,
|
|
|
|
TxFeeCap: DefaultTxFeeCap,
|
|
|
|
FilterCap: DefaultFilterCap,
|
|
|
|
FeeHistoryCap: DefaultFeeHistoryCap,
|
|
|
|
BlockRangeCap: DefaultBlockRangeCap,
|
|
|
|
LogsCap: DefaultLogsCap,
|
|
|
|
HTTPTimeout: DefaultHTTPTimeout,
|
|
|
|
HTTPIdleTimeout: DefaultHTTPIdleTimeout,
|
|
|
|
AllowUnprotectedTxs: DefaultAllowUnprotectedTxs,
|
2022-08-10 12:26:51 +00:00
|
|
|
MaxOpenConnections: DefaultMaxOpenConnections,
|
2021-09-28 11:33:54 +00:00
|
|
|
}
|
2021-08-18 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the JSON-RPC configuration fields are invalid.
|
|
|
|
func (c JSONRPCConfig) Validate() error {
|
|
|
|
if c.Enable && len(c.API) == 0 {
|
|
|
|
return errors.New("cannot enable JSON-RPC without defining any API namespace")
|
|
|
|
}
|
|
|
|
|
2021-10-13 11:03:49 +00:00
|
|
|
if c.FilterCap < 0 {
|
|
|
|
return errors.New("JSON-RPC filter-cap cannot be negative")
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:58:52 +00:00
|
|
|
if c.FeeHistoryCap <= 0 {
|
|
|
|
return errors.New("JSON-RPC feehistory-cap cannot be negative or 0")
|
|
|
|
}
|
|
|
|
|
2021-10-25 15:01:04 +00:00
|
|
|
if c.TxFeeCap < 0 {
|
|
|
|
return errors.New("JSON-RPC tx fee cap cannot be negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.EVMTimeout < 0 {
|
|
|
|
return errors.New("JSON-RPC EVM timeout duration cannot be negative")
|
|
|
|
}
|
|
|
|
|
2021-12-29 21:47:25 +00:00
|
|
|
if c.LogsCap < 0 {
|
|
|
|
return errors.New("JSON-RPC logs cap cannot be negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.BlockRangeCap < 0 {
|
|
|
|
return errors.New("JSON-RPC block range cap cannot be negative")
|
|
|
|
}
|
|
|
|
|
2022-03-09 07:31:51 +00:00
|
|
|
if c.HTTPTimeout < 0 {
|
|
|
|
return errors.New("JSON-RPC HTTP timeout duration cannot be negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HTTPIdleTimeout < 0 {
|
|
|
|
return errors.New("JSON-RPC HTTP idle timeout duration cannot be negative")
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:49:05 +00:00
|
|
|
// check for duplicates
|
2021-08-18 14:11:51 +00:00
|
|
|
seenAPIs := make(map[string]bool)
|
|
|
|
for _, api := range c.API {
|
|
|
|
if seenAPIs[api] {
|
|
|
|
return fmt.Errorf("repeated API namespace '%s'", api)
|
|
|
|
}
|
|
|
|
|
|
|
|
seenAPIs[api] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
// DefaultTLSConfig returns the default TLS configuration
|
|
|
|
func DefaultTLSConfig() *TLSConfig {
|
|
|
|
return &TLSConfig{
|
|
|
|
CertificatePath: "",
|
|
|
|
KeyPath: "",
|
2021-07-26 11:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
// Validate returns an error if the TLS certificate and key file extensions are invalid.
|
|
|
|
func (c TLSConfig) Validate() error {
|
|
|
|
certExt := path.Ext(c.CertificatePath)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
if c.CertificatePath != "" && certExt != ".pem" {
|
|
|
|
return fmt.Errorf("invalid extension %s for certificate path %s, expected '.pem'", certExt, c.CertificatePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyExt := path.Ext(c.KeyPath)
|
|
|
|
|
|
|
|
if c.KeyPath != "" && keyExt != ".pem" {
|
|
|
|
return fmt.Errorf("invalid extension %s for key path %s, expected '.pem'", keyExt, c.KeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns a fully parsed Config object.
|
|
|
|
func GetConfig(v *viper.Viper) Config {
|
2021-06-25 09:18:37 +00:00
|
|
|
cfg := config.GetConfig(v)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
return Config{
|
2021-06-29 17:02:21 +00:00
|
|
|
Config: cfg,
|
2021-08-16 09:45:10 +00:00
|
|
|
EVM: EVMConfig{
|
2022-03-21 09:01:21 +00:00
|
|
|
Tracer: v.GetString("evm.tracer"),
|
|
|
|
MaxTxGasWanted: v.GetUint64("evm.max-tx-gas-wanted"),
|
2021-04-18 16:39:15 +00:00
|
|
|
},
|
2021-08-16 09:45:10 +00:00
|
|
|
JSONRPC: JSONRPCConfig{
|
2022-08-10 12:26:51 +00:00
|
|
|
Enable: v.GetBool("json-rpc.enable"),
|
|
|
|
API: v.GetStringSlice("json-rpc.api"),
|
|
|
|
Address: v.GetString("json-rpc.address"),
|
|
|
|
WsAddress: v.GetString("json-rpc.ws-address"),
|
|
|
|
GasCap: v.GetUint64("json-rpc.gas-cap"),
|
|
|
|
FilterCap: v.GetInt32("json-rpc.filter-cap"),
|
|
|
|
FeeHistoryCap: v.GetInt32("json-rpc.feehistory-cap"),
|
|
|
|
TxFeeCap: v.GetFloat64("json-rpc.txfee-cap"),
|
|
|
|
EVMTimeout: v.GetDuration("json-rpc.evm-timeout"),
|
|
|
|
LogsCap: v.GetInt32("json-rpc.logs-cap"),
|
|
|
|
BlockRangeCap: v.GetInt32("json-rpc.block-range-cap"),
|
|
|
|
HTTPTimeout: v.GetDuration("json-rpc.http-timeout"),
|
|
|
|
HTTPIdleTimeout: v.GetDuration("json-rpc.http-idle-timeout"),
|
|
|
|
MaxOpenConnections: v.GetInt("json-rpc.max-open-connections"),
|
2021-08-16 09:45:10 +00:00
|
|
|
},
|
2021-09-28 11:33:54 +00:00
|
|
|
TLS: TLSConfig{
|
|
|
|
CertificatePath: v.GetString("tls.certificate-path"),
|
|
|
|
KeyPath: v.GetString("tls.key-path"),
|
|
|
|
},
|
2021-08-16 09:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:11:51 +00:00
|
|
|
// ParseConfig retrieves the default environment configuration for the
|
|
|
|
// application.
|
|
|
|
func ParseConfig(v *viper.Viper) (*Config, error) {
|
|
|
|
conf := DefaultConfig()
|
|
|
|
err := v.Unmarshal(conf)
|
|
|
|
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
// ValidateBasic returns an error any of the application configuration fields are invalid
|
|
|
|
func (c Config) ValidateBasic() error {
|
|
|
|
if err := c.EVM.Validate(); err != nil {
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrAppConfig, "invalid evm config value: %s", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2021-08-16 09:45:10 +00:00
|
|
|
|
2021-08-18 14:11:51 +00:00
|
|
|
if err := c.JSONRPC.Validate(); err != nil {
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrAppConfig, "invalid json-rpc config value: %s", err.Error())
|
|
|
|
}
|
2021-08-16 09:45:10 +00:00
|
|
|
|
2021-09-28 11:33:54 +00:00
|
|
|
if err := c.TLS.Validate(); err != nil {
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrAppConfig, "invalid tls config value: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:45:10 +00:00
|
|
|
return c.Config.ValidateBasic()
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|