Skip to content

Commit

Permalink
Add ability to parse legacy genesis file
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Jun 1, 2022
1 parent 3ae87c0 commit 4cec5f0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 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 @@
{}

0 comments on commit 4cec5f0

Please sign in to comment.