From 9877ba77e38ac555a02ee136de9c35281658af72 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Sat, 3 Aug 2024 14:13:47 -0500 Subject: [PATCH] validate chain ID against config, or defer to config --- cmd/serve.go | 4 ++-- pkg/serve/config.go | 39 +++++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 7d94cbf3..cc3345b2 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -277,10 +277,10 @@ func init() { serveCmd.PersistentFlags().String("eth-client-name", "Geth", "eth client name") serveCmd.PersistentFlags().String("eth-genesis-block", "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "eth genesis block hash") serveCmd.PersistentFlags().String("eth-network-id", "1", "eth network id") - serveCmd.PersistentFlags().String("eth-chain-id", "1", "eth chain id") + serveCmd.PersistentFlags().String("eth-chain-id", "", "eth chain id") + serveCmd.PersistentFlags().String("eth-chain-config", "", "json chain config file location") 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") serveCmd.PersistentFlags().Bool("eth-supports-state-diff", false, "whether the proxy ethereum client supports statediffing endpoints") serveCmd.PersistentFlags().Bool("eth-forward-eth-calls", false, "whether to immediately forward eth_calls to proxy client") serveCmd.PersistentFlags().Bool("eth-proxy-on-error", true, "whether to forward all failed calls to proxy client") diff --git a/pkg/serve/config.go b/pkg/serve/config.go index 7c12b3bb..cbc12a3f 100644 --- a/pkg/serve/config.go +++ b/pkg/serve/config.go @@ -128,9 +128,10 @@ func NewConfig() (*Config, error) { viper.BindEnv("server.ipcPath", SERVER_IPC_PATH) viper.BindEnv("server.graphqlPath", SERVER_GRAPHQL_PATH) + viper.BindEnv("ethereum.chainID", ETH_CHAIN_ID) + viper.BindEnv("ethereum.chainConfig", ETH_CHAIN_CONFIG) viper.BindEnv("ethereum.httpPath", ETH_HTTP_PATH) viper.BindEnv("ethereum.rpcGasCap", ETH_RPC_GAS_CAP) - viper.BindEnv("ethereum.chainConfig", ETH_CHAIN_CONFIG) viper.BindEnv("ethereum.supportsStateDiff", ETH_SUPPORTS_STATEDIFF) viper.BindEnv("ethereum.stateDiffTimeout", ETH_STATEDIFF_TIMEOUT) viper.BindEnv("ethereum.forwardEthCalls", ETH_FORWARD_ETH_CALLS) @@ -143,11 +144,32 @@ func NewConfig() (*Config, error) { c.dbInit() ethHTTP := viper.GetString("ethereum.httpPath") ethHTTPEndpoint := fmt.Sprintf("http://%s", ethHTTP) - nodeInfo, cli, err := getEthNodeAndClient(ethHTTPEndpoint) - c.NodeNetworkID = nodeInfo.NetworkID + + // At least one of chain ID and chain config must be passed. + // If both are passed, the chain ID must match the config. + chainID := viper.GetUint64("ethereum.chainID") + chainConfigPath := viper.GetString("ethereum.chainConfig") + var err error + if chainConfigPath != "" { + if c.ChainConfig, err = utils.LoadConfig(chainConfigPath); err != nil { + return nil, err + } + // Only validate the chain ID if it was actually passed + if viper.GetString("ethereum.chainID") != "" && c.ChainConfig.ChainID.Uint64() != chainID { + return nil, fmt.Errorf("passed chain ID %d does not match chain config chain ID %d", + chainID, c.ChainConfig.ChainID.Uint64()) + } + } else { + if c.ChainConfig, err = utils.ChainConfig(chainID); err != nil { + return nil, err + } + } + + nodeInfo, cli, err := getEthNodeAndClient(ethHTTPEndpoint, chainID) if err != nil { return nil, err } + c.NodeNetworkID = nodeInfo.NetworkID c.Client = cli c.SupportStateDiff = viper.GetBool("ethereum.supportsStateDiff") c.ForwardEthCalls = viper.GetBool("ethereum.forwardEthCalls") @@ -237,12 +259,6 @@ func NewConfig() (*Config, error) { if c.StateDiffTimeout < 0 { return nil, errors.New("ethereum.stateDiffTimeout < 0") } - chainConfigPath := viper.GetString("ethereum.chainConfig") - if chainConfigPath != "" { - c.ChainConfig, err = utils.LoadConfig(chainConfigPath) - } else { - c.ChainConfig, err = utils.ChainConfig(nodeInfo.ChainID) - } c.loadGroupCacheConfig() @@ -312,12 +328,11 @@ func (c *Config) loadValidatorConfig() { } // GetEthNodeAndClient returns eth node info and client from path url -func getEthNodeAndClient(path string) (node.Info, *rpc.Client, error) { +func getEthNodeAndClient(path string, chainid uint64) (node.Info, *rpc.Client, error) { viper.BindEnv("ethereum.nodeID", ETH_NODE_ID) viper.BindEnv("ethereum.clientName", ETH_CLIENT_NAME) viper.BindEnv("ethereum.genesisBlock", ETH_GENESIS_BLOCK) viper.BindEnv("ethereum.networkID", ETH_NETWORK_ID) - viper.BindEnv("ethereum.chainID", ETH_CHAIN_ID) rpcClient, err := rpc.Dial(path) if err != nil { @@ -328,6 +343,6 @@ func getEthNodeAndClient(path string) (node.Info, *rpc.Client, error) { ClientName: viper.GetString("ethereum.clientName"), GenesisBlock: viper.GetString("ethereum.genesisBlock"), NetworkID: viper.GetString("ethereum.networkID"), - ChainID: viper.GetUint64("ethereum.chainID"), + ChainID: chainid, }, rpcClient, nil }