Updated file check

This commit is contained in:
Krishna Upadhyaya 2022-01-21 19:28:07 +05:30 committed by Victor Castell
parent 1ef2726beb
commit b3984096e2
2 changed files with 22 additions and 12 deletions

View file

@ -2,7 +2,10 @@ package chains
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"os"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
@ -21,13 +24,23 @@ var chains = map[string]*Chain{
"mumbai": mumbaiTestnet, "mumbai": mumbaiTestnet,
} }
func GetChain(name string) (*Chain, bool) { func GetChain(name string) (*Chain, error) {
chain, err := ImportFromFile(name) var chain *Chain
if err != nil { var err error
chain, ok := chains[name] if _, fileErr := os.Stat(name); fileErr == nil {
return chain, ok if chain, err = ImportFromFile(name); err != nil {
return nil, fmt.Errorf("error importing chain from file: %v", err)
}
return chain, nil
} else if errors.Is(fileErr, os.ErrNotExist) {
var ok bool
if chain, ok = chains[name]; !ok {
return nil, fmt.Errorf("chain %s not found", name)
}
return chain, nil
} else {
return nil, fileErr
} }
return chain, true
} }
func ImportFromFile(filename string) (*Chain, error) { func ImportFromFile(filename string) (*Chain, error) {

View file

@ -591,12 +591,9 @@ func readConfigFile(path string) (*Config, error) {
} }
func (c *Config) loadChain() error { func (c *Config) loadChain() error {
if c.Developer.Enabled { chain, err := chains.GetChain(c.Chain)
return nil if err != nil {
} return err
chain, ok := chains.GetChain(c.Chain)
if !ok {
return fmt.Errorf("chain '%s' not found", c.Chain)
} }
c.chain = chain c.chain = chain