Skip to content

Commit

Permalink
Updated file check
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKrishna authored and Victor Castell committed May 9, 2022
1 parent 1ef2726 commit b398409
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
25 changes: 19 additions & 6 deletions internal/cli/server/chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package chains

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
Expand All @@ -21,13 +24,23 @@ var chains = map[string]*Chain{
"mumbai": mumbaiTestnet,
}

func GetChain(name string) (*Chain, bool) {
chain, err := ImportFromFile(name)
if err != nil {
chain, ok := chains[name]
return chain, ok
func GetChain(name string) (*Chain, error) {
var chain *Chain
var err error
if _, fileErr := os.Stat(name); fileErr == nil {
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) {
Expand Down
9 changes: 3 additions & 6 deletions internal/cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,9 @@ func readConfigFile(path string) (*Config, error) {
}

func (c *Config) loadChain() error {
if c.Developer.Enabled {
return nil
}
chain, ok := chains.GetChain(c.Chain)
if !ok {
return fmt.Errorf("chain '%s' not found", c.Chain)
chain, err := chains.GetChain(c.Chain)
if err != nil {
return err
}
c.chain = chain

Expand Down

0 comments on commit b398409

Please sign in to comment.