Skip to content

Commit

Permalink
Merge pull request ethereum#419 from cffls/v0.3.0-dev-flags
Browse files Browse the repository at this point in the history
CLI improvements
  • Loading branch information
cffls authored Jun 7, 2022
2 parents 0d2b1d0 + 96ac8d1 commit 8808c23
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 298 deletions.
15 changes: 15 additions & 0 deletions internal/cli/server/chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
)

type Chain struct {
Expand Down Expand Up @@ -59,5 +60,19 @@ func importChain(content []byte) (*Chain, error) {
return nil, err
}

if chain.Genesis == nil {
log.Info("Try reading as legacy genesis")
var genesis core.Genesis
if err := json.Unmarshal(content, &genesis); err != nil {
return nil, err
}
if genesis.Config != nil {
chain.Genesis = &genesis
chain.NetworkId = genesis.Config.ChainID.Uint64()
} else {
return nil, fmt.Errorf("unable to parse chain config")
}
}

return chain, nil
}
10 changes: 10 additions & 0 deletions internal/cli/server/chains/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ func TestChain_ImportFromFile(t *testing.T) {
args: args{filename: "test_files/chain_test.json"},
wantErr: false,
},
{
name: "ImportFromFile correct legacy json file",
args: args{filename: "test_files/chain_legacy_test.json"},
wantErr: false,
},
{
name: "ImportFromFile wrong json file",
args: args{filename: "test_files/wrong_chain.json"},
wantErr: true,
},
{
name: "ImportFromFile nonexistent json file",
args: args{filename: "test_files/chain_test_nonexistent.json"},
Expand Down
83 changes: 83 additions & 0 deletions internal/cli/server/chains/test_files/chain_legacy_test.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/cli/server/chains/test_files/wrong_chain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
13 changes: 7 additions & 6 deletions internal/cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,15 @@ func (c *Config) buildNode() (*node.Config, error) {
if cfg.P2P.StaticNodes, err = parseBootnodes(c.P2P.Discovery.StaticNodes); err != nil {
return nil, err
}
if len(cfg.P2P.StaticNodes) == 0 {
cfg.P2P.StaticNodes = cfg.StaticNodes()
}
if cfg.P2P.TrustedNodes, err = parseBootnodes(c.P2P.Discovery.TrustedNodes); err != nil {
return nil, err
}
if len(cfg.P2P.TrustedNodes) == 0 {
cfg.P2P.TrustedNodes = cfg.TrustedNodes()
}
}

if c.P2P.NoDiscover {
Expand All @@ -950,14 +956,9 @@ func (c *Config) buildNode() (*node.Config, error) {

func (c *Config) Merge(cc ...*Config) error {
for _, elem := range cc {
if err := mergo.Merge(c, elem, mergo.WithOverride, mergo.WithAppendSlice); err != nil {
if err := mergo.Merge(c, elem, mergo.WithOverwriteWithEmptyValue, mergo.WithAppendSlice); err != nil {
return fmt.Errorf("failed to merge configurations: %v", err)
}

// override max peers
if elem.P2P.MaxPeers == 0 {
c.P2P.MaxPeers = 0
}
}
return nil
}
Expand Down
5 changes: 1 addition & 4 deletions internal/cli/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ func TestConfigMerge(t *testing.T) {
}
expected := &Config{
Chain: "1",
NoSnapshot: true,
NoSnapshot: false,
RequiredBlocks: map[string]string{
"a": "b",
"b": "c",
},
TxPool: &TxPoolConfig{
LifeTime: 5 * time.Second,
},
P2P: &P2PConfig{
MaxPeers: 10,
Discovery: &P2PDiscovery{
Expand Down
Loading

0 comments on commit 8808c23

Please sign in to comment.