From d3f30b621bcee0a4d5bcfdb791da52c563239584 Mon Sep 17 00:00:00 2001 From: Ramil Amerzyanov Date: Wed, 10 Mar 2021 19:18:32 +0300 Subject: [PATCH] support custom chain config (#36) * support custom chain config * move loading config logic to a separate helper function --- cmd/serve.go | 2 ++ pkg/eth/helpers.go | 26 ++++++++++++++++++++++++++ pkg/serve/config.go | 9 ++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) 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..fc09d13c 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" @@ -39,6 +43,28 @@ func ResolveToNodeType(nodeType int) sdtypes.NodeType { } } +// 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() + + 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 +} + // ChainConfig returns the appropriate ethereum chain config for the provided chain id func ChainConfig(chainID uint64) (*params.ChainConfig, error) { switch chainID { diff --git a/pkg/serve/config.go b/pkg/serve/config.go index 3f246800..4578fddf 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,12 @@ func NewConfig() (*Config, error) { c.RPCGasCap = rpcGasCap } } - c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID) + chainConfigPath := viper.GetString("ethereum.chainConfig") + if chainConfigPath != "" { + c.ChainConfig, err = eth.LoadConfig(chainConfigPath) + } else { + c.ChainConfig, err = eth.ChainConfig(nodeInfo.ChainID) + } return c, err }