Add ability to parse legacy genesis file

This commit is contained in:
Jerry 2022-05-26 15:36:38 -07:00
parent 3ae87c0d46
commit 4cec5f01c3
4 changed files with 109 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
) )
type Chain struct { type Chain struct {
@ -59,5 +60,19 @@ func importChain(content []byte) (*Chain, error) {
return nil, err 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 return chain, nil
} }

View file

@ -18,6 +18,16 @@ func TestChain_ImportFromFile(t *testing.T) {
args: args{filename: "test_files/chain_test.json"}, args: args{filename: "test_files/chain_test.json"},
wantErr: false, 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", name: "ImportFromFile nonexistent json file",
args: args{filename: "test_files/chain_test_nonexistent.json"}, args: args{filename: "test_files/chain_test_nonexistent.json"},

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
{}