From 6b4a3435051cefdceff4c8e777014ebe35faf5c6 Mon Sep 17 00:00:00 2001 From: ramil Date: Mon, 8 Mar 2021 16:25:46 +0300 Subject: [PATCH 1/2] support custom chain config --- cmd/serve.go | 2 ++ pkg/eth/helpers.go | 27 ++++++++++++++++++++++++++- pkg/serve/config.go | 4 +++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index d32b142d..6c4dfdf6 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -137,6 +137,7 @@ func init() { serveCmd.PersistentFlags().String("eth-chain-id", "1", "eth chain id") serveCmd.PersistentFlags().String("eth-default-sender", "", "default sender address") serveCmd.PersistentFlags().String("eth-rpc-gas-cap", "", "rpc gas cap (for eth_Call execution)") + serveCmd.PersistentFlags().String("eth-chain-config", "", "json chain config file location") // and their bindings viper.BindPFlag("server.graphql", serveCmd.PersistentFlags().Lookup("server-graphql")) @@ -153,4 +154,5 @@ func init() { viper.BindPFlag("ethereum.chainID", serveCmd.PersistentFlags().Lookup("eth-chain-id")) viper.BindPFlag("ethereum.defaultSender", serveCmd.PersistentFlags().Lookup("eth-default-sender")) viper.BindPFlag("ethereum.rpcGasCap", serveCmd.PersistentFlags().Lookup("eth-rpc-gas-cap")) + viper.BindPFlag("ethereum.chainConfig", serveCmd.PersistentFlags().Lookup("eth-chain-config")) } diff --git a/pkg/eth/helpers.go b/pkg/eth/helpers.go index def2a461..fb072cae 100644 --- a/pkg/eth/helpers.go +++ b/pkg/eth/helpers.go @@ -17,7 +17,11 @@ package eth import ( + "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/cmd/utils" + log "github.com/sirupsen/logrus" + "os" sdtypes "github.com/ethereum/go-ethereum/statediff/types" @@ -40,7 +44,28 @@ func ResolveToNodeType(nodeType int) sdtypes.NodeType { } // ChainConfig returns the appropriate ethereum chain config for the provided chain id -func ChainConfig(chainID uint64) (*params.ChainConfig, error) { +func ChainConfig(chainID uint64, chainConfigPath string) (*params.ChainConfig, error) { + if chainConfigPath != "" { + file, err := os.Open(chainConfigPath) + if err != nil { + utils.Fatalf("Failed to read chain config file: %v", err) + + return nil, err + } + defer file.Close() + + chainConfig := new(params.ChainConfig) + if err := json.NewDecoder(file).Decode(chainConfig); err != nil { + utils.Fatalf("invalid chain config file: %v", err) + + return nil, err + } + + log.Infof("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig) + + return chainConfig, nil + } + switch chainID { case 1: return params.MainnetChainConfig, nil diff --git a/pkg/serve/config.go b/pkg/serve/config.go index 10f5e580..e02b32e9 100644 --- a/pkg/serve/config.go +++ b/pkg/serve/config.go @@ -48,6 +48,7 @@ const ( ETH_DEFAULT_SENDER_ADDR = "ETH_DEFAULT_SENDER_ADDR" ETH_RPC_GAS_CAP = "ETH_RPC_GAS_CAP" + ETH_CHAIN_CONFIG = "ETH_CHAIN_CONFIG" ETH_SUPPORTS_STATEDIFF = "ETH_SUPPORTS_STATEDIFF" ) @@ -76,6 +77,7 @@ func NewConfig() (*Config, error) { viper.BindEnv("ethereum.httpPath", shared.ETH_HTTP_PATH) viper.BindEnv("ethereum.defaultSender", ETH_DEFAULT_SENDER_ADDR) viper.BindEnv("ethereum.rpcGasCap", ETH_RPC_GAS_CAP) + viper.BindEnv("ethereum.chainConfig", ETH_CHAIN_CONFIG) viper.BindEnv("ethereum.supportsStateDiff", ETH_SUPPORTS_STATEDIFF) c.DBConfig.Init() @@ -123,7 +125,7 @@ func NewConfig() (*Config, error) { c.RPCGasCap = rpcGasCap } } - c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID) + c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID, viper.GetString("ethereum.chainConfig")) return c, err } -- 2.45.2 From 510c29c14eedd1cec8b0945be33995c1edb9f0f3 Mon Sep 17 00:00:00 2001 From: ramil Date: Wed, 10 Mar 2021 19:09:33 +0300 Subject: [PATCH 2/2] move loading config logic to a separate helper function --- pkg/eth/helpers.go | 37 +++++++++++++++++++------------------ pkg/serve/config.go | 7 ++++++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pkg/eth/helpers.go b/pkg/eth/helpers.go index fb072cae..fc09d13c 100644 --- a/pkg/eth/helpers.go +++ b/pkg/eth/helpers.go @@ -43,29 +43,30 @@ func ResolveToNodeType(nodeType int) sdtypes.NodeType { } } -// ChainConfig returns the appropriate ethereum chain config for the provided chain id -func ChainConfig(chainID uint64, chainConfigPath string) (*params.ChainConfig, error) { - if chainConfigPath != "" { - file, err := os.Open(chainConfigPath) - if err != nil { - utils.Fatalf("Failed to read chain config file: %v", err) +// LoadConfig loads chain config from json file +func LoadConfig(chainConfigPath string) (*params.ChainConfig, error) { + file, err := os.Open(chainConfigPath) + if err != nil { + utils.Fatalf("Failed to read chain config file: %v", err) - return nil, err - } - defer file.Close() + return nil, err + } + defer file.Close() - chainConfig := new(params.ChainConfig) - if err := json.NewDecoder(file).Decode(chainConfig); err != nil { - utils.Fatalf("invalid chain config file: %v", err) + chainConfig := new(params.ChainConfig) + if err := json.NewDecoder(file).Decode(chainConfig); err != nil { + utils.Fatalf("invalid chain config file: %v", err) - return nil, err - } - - log.Infof("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig) - - return chainConfig, nil + return nil, err } + log.Infof("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig) + + return chainConfig, nil +} + +// ChainConfig returns the appropriate ethereum chain config for the provided chain id +func ChainConfig(chainID uint64) (*params.ChainConfig, error) { switch chainID { case 1: return params.MainnetChainConfig, nil diff --git a/pkg/serve/config.go b/pkg/serve/config.go index e02b32e9..7786974c 100644 --- a/pkg/serve/config.go +++ b/pkg/serve/config.go @@ -125,7 +125,12 @@ func NewConfig() (*Config, error) { c.RPCGasCap = rpcGasCap } } - c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID, viper.GetString("ethereum.chainConfig")) + chainConfigPath := viper.GetString("ethereum.chainConfig") + if chainConfigPath != "" { + c.ChainConfig, err = eth.LoadConfig(chainConfigPath) + } else { + c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID) + } return c, err } -- 2.45.2