Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom chain config #36

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
}
26 changes: 26 additions & 0 deletions pkg/eth/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion pkg/serve/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down