evm, server: configurable tracer (#434)

* server: update server and enable configurable tracer

* config validation

* fix import cycle

* fix start

* update fields

* c++

* c++
This commit is contained in:
Federico Kunze Küllmer
2021-08-16 09:45:10 +00:00
committed by GitHub
parent 353455def9
commit cc3b2ff8e9
26 changed files with 292 additions and 168 deletions
+79 -25
View File
@@ -1,19 +1,33 @@
package config
import (
"github.com/cosmos/cosmos-sdk/server/config"
"fmt"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/strings"
"github.com/cosmos/cosmos-sdk/server/config"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// DefaultGRPCAddress is the default address the gRPC server binds to.
DefaultGRPCAddress = "0.0.0.0:9900"
// DefaultEVMAddress is the default address the EVM JSON-RPC server binds to.
DefaultEVMAddress = "0.0.0.0:8545"
// DefaultJSONRPCAddress is the default address the JSON-RPC server binds to.
DefaultJSONRPCAddress = "0.0.0.0:8545"
// DefaultEVMWSAddress is the default address the EVM WebSocket server binds to.
DefaultEVMWSAddress = "0.0.0.0:8546"
// 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
DefaultEVMTracer = "json"
)
var (
evmTracers = []string{DefaultEVMTracer, "markdown", "struct", "access_list"}
)
// GetDefaultAPINamespaces returns the default list of JSON-RPC namespaces that should be enabled
@@ -45,8 +59,9 @@ func AppConfig(denom string) (string, interface{}) {
}
customAppConfig := Config{
Config: *srvCfg,
EVMRPC: *DefaultEVMConfig(),
Config: *srvCfg,
EVM: *DefaultEVMConfig(),
JSONRPC: *DefaultJSONRPCConfig(),
}
customAppTemplate := config.DefaultConfigTemplate + DefaultConfigTemplate
@@ -57,15 +72,39 @@ func AppConfig(denom string) (string, interface{}) {
// DefaultConfig returns server's default configuration.
func DefaultConfig() *Config {
return &Config{
Config: *config.DefaultConfig(),
EVMRPC: *DefaultEVMConfig(),
Config: *config.DefaultConfig(),
EVM: *DefaultEVMConfig(),
JSONRPC: *DefaultJSONRPCConfig(),
}
}
// EVMRPCConfig defines configuration for the EVM RPC server.
type EVMRPCConfig struct {
// RPCAddress defines the HTTP server to listen on
RPCAddress string `mapstructure:"address"`
// 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"`
}
// DefaultEVMConfig returns the default EVM configuration
func DefaultEVMConfig() *EVMConfig {
return &EVMConfig{
Tracer: DefaultEVMTracer,
}
}
// Validate returns an error if the tracer type is invalid.
func (c EVMConfig) Validate() error {
if !strings.StringInSlice(c.Tracer, evmTracers) {
return fmt.Errorf("invalid tracer type %s, available types: %v", c.Tracer, evmTracers)
}
return nil
}
// JSONRPCConfig defines configuration for the EVM RPC server.
type JSONRPCConfig struct {
// 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"`
// API defines a list of JSON-RPC namespaces that should be enabled
@@ -76,13 +115,13 @@ type EVMRPCConfig struct {
EnableUnsafeCORS bool `mapstructure:"enable-unsafe-cors"`
}
// DefaultEVMConfig returns an EVM config with the JSON-RPC API enabled by default
func DefaultEVMConfig() *EVMRPCConfig {
return &EVMRPCConfig{
// DefaultJSONRPCConfig returns an EVM config with the JSON-RPC API enabled by default
func DefaultJSONRPCConfig() *JSONRPCConfig {
return &JSONRPCConfig{
Enable: true,
API: GetDefaultAPINamespaces(),
RPCAddress: DefaultEVMAddress,
WsAddress: DefaultEVMWSAddress,
Address: DefaultJSONRPCAddress,
WsAddress: DefaultJSONRPCWsAddress,
EnableUnsafeCORS: false,
}
}
@@ -92,7 +131,8 @@ func DefaultEVMConfig() *EVMRPCConfig {
type Config struct {
config.Config
EVMRPC EVMRPCConfig `mapstructure:"evm-rpc"`
EVM EVMConfig `mapstructure:"evm"`
JSONRPC JSONRPCConfig `mapstructure:"json-rpc"`
}
// GetConfig returns a fully parsed Config object.
@@ -101,12 +141,26 @@ func GetConfig(v *viper.Viper) Config {
return Config{
Config: cfg,
EVMRPC: EVMRPCConfig{
Enable: v.GetBool("evm-rpc.enable"),
API: v.GetStringSlice("evm-rpc.api"),
RPCAddress: v.GetString("evm-rpc.address"),
WsAddress: v.GetString("evm-rpc.ws-address"),
EnableUnsafeCORS: v.GetBool("evm-rpc.enable-unsafe-cors"),
EVM: EVMConfig{
Tracer: v.GetString("evm.tracer"),
},
JSONRPC: JSONRPCConfig{
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"),
EnableUnsafeCORS: v.GetBool("json-rpc.enable-unsafe-cors"),
},
}
}
// 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())
}
// TODO: validate JSON-RPC APIs
return c.Config.ValidateBasic()
}
+4 -4
View File
@@ -7,8 +7,8 @@ import (
)
func TestDefaultConfig(t *testing.T) {
cfg := DefaultEVMConfig()
require.True(t, cfg.Enable)
require.Equal(t, cfg.RPCAddress, DefaultEVMAddress)
require.Equal(t, cfg.WsAddress, DefaultEVMWSAddress)
cfg := DefaultConfig()
require.True(t, cfg.JSONRPC.Enable)
require.Equal(t, cfg.JSONRPC.Address, DefaultJSONRPCAddress)
require.Equal(t, cfg.JSONRPC.WsAddress, DefaultJSONRPCWsAddress)
}
+18 -7
View File
@@ -3,24 +3,35 @@ package config
// DefaultConfigTemplate defines the configuration template for the EVM RPC configuration
const DefaultConfigTemplate = `
###############################################################################
### EVM RPC Configuration ###
### EVM Configuration ###
###############################################################################
[evm-rpc]
[evm]
# Tracer defines the 'vm.Tracer' type that the EVM will use when the node is run in
# debug mode. To enable tracing use the '--trace' flag when starting your node.
# Valid types are: json|struct|access_list|markdown
tracer = "{{ .EVM.Tracer }}"
###############################################################################
### JSON RPC Configuration ###
###############################################################################
[json-rpc]
# Enable defines if the gRPC server should be enabled.
enable = {{ .EVMRPC.Enable }}
enable = {{ .JSONRPC.Enable }}
# Address defines the EVM RPC HTTP server address to bind to.
address = "{{ .EVMRPC.RPCAddress }}"
address = "{{ .JSONRPC.Address }}"
# Address defines the EVM WebSocket server address to bind to.
ws-address = "{{ .EVMRPC.WsAddress }}"
ws-address = "{{ .JSONRPC.WsAddress }}"
# API defines a list of JSON-RPC namespaces that should be enabled
# Example: "eth,txpool,personal,net,debug,web3"
api = "{{range $index, $elmt := .EVMRPC.API}}{{if $index}},{{$elmt}}{{else}}{{$elmt}}{{end}}{{end}}"
api = "{{range $index, $elmt := .JSONRPC.API}}{{if $index}},{{$elmt}}{{else}}{{$elmt}}{{end}}{{end}}"
# EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk)
enable-unsafe-cors = "{{ .EVMRPC.EnableUnsafeCORS }}"
enable-unsafe-cors = "{{ .JSONRPC.EnableUnsafeCORS }}"
`