|
| 1 | +// VulcanizeDB |
| 2 | +// Copyright © 2022 Vulcanize |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package statediff |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/log" |
| 25 | + |
| 26 | + "github.com/ethereum/go-ethereum/params" |
| 27 | +) |
| 28 | + |
| 29 | +// LoadConfig loads chain config from json file |
| 30 | +func LoadConfig(chainConfigPath string) (*params.ChainConfig, error) { |
| 31 | + file, err := os.Open(chainConfigPath) |
| 32 | + if err != nil { |
| 33 | + log.Error(fmt.Sprintf("Failed to read chain config file: %v", err)) |
| 34 | + |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + defer file.Close() |
| 38 | + |
| 39 | + chainConfig := new(params.ChainConfig) |
| 40 | + if err := json.NewDecoder(file).Decode(chainConfig); err != nil { |
| 41 | + log.Error(fmt.Sprintf("invalid chain config file: %v", err)) |
| 42 | + |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + log.Info(fmt.Sprintf("Using chain config from %s file. Content %+v", chainConfigPath, chainConfig)) |
| 47 | + |
| 48 | + return chainConfig, nil |
| 49 | +} |
| 50 | + |
| 51 | +// ChainConfig returns the appropriate ethereum chain config for the provided chain id |
| 52 | +func ChainConfig(chainID uint64) (*params.ChainConfig, error) { |
| 53 | + switch chainID { |
| 54 | + case 1: |
| 55 | + return params.MainnetChainConfig, nil |
| 56 | + case 3: |
| 57 | + return params.RopstenChainConfig, nil |
| 58 | + case 4: |
| 59 | + return params.RinkebyChainConfig, nil |
| 60 | + case 5: |
| 61 | + return params.GoerliChainConfig, nil |
| 62 | + default: |
| 63 | + return nil, fmt.Errorf("chain config for chainid %d not available", chainID) |
| 64 | + } |
| 65 | +} |
0 commit comments