cmd/XDC: improve init genesis

This commit is contained in:
Daniel Liu 2025-03-12 23:22:35 +08:00
parent 8706e1aa7d
commit 8efa0c0a29

View file

@ -155,48 +155,30 @@ Use "ethereum dump 0" to dump the genesis block.`,
// initGenesis will initialise the given JSON format genesis file and writes it as // initGenesis will initialise the given JSON format genesis file and writes it as
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed. // the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
func initGenesis(ctx *cli.Context) error { func initGenesis(ctx *cli.Context) error {
utils.CheckExclusive(ctx, utils.MainnetFlag, utils.TestnetFlag, utils.DevnetFlag) if ctx.Args().Len() != 1 {
utils.Fatalf("need the genesis.json file or the network name [ mainnet | testnet | devnet ] as the only argument")
}
var err error var err error
genesis := new(core.Genesis) genesis := new(core.Genesis)
if ctx.Bool(utils.MainnetFlag.Name) { genesisPath := ctx.Args().First()
if ctx.Args().Len() > 0 { if genesisPath == "mainnet" || genesisPath == "xinfin" {
utils.Fatalf("The mainnet flag and genesis file can't be used at the same time") err = json.Unmarshal(xdc_genesis.MainnetGenesis, genesis)
} } else if genesisPath == "testnet" || genesisPath == "apothem" {
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis) err = json.Unmarshal(xdc_genesis.TestnetGenesis, genesis)
} else if ctx.Bool(utils.TestnetFlag.Name) { } else if genesisPath == "devnet" {
if ctx.Args().Len() > 0 { err = json.Unmarshal(xdc_genesis.DevnetGenesis, genesis)
utils.Fatalf("The testnet flag and genesis file can't be used at the same time")
}
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis)
} else if ctx.Bool(utils.DevnetFlag.Name) {
if ctx.Args().Len() > 0 {
utils.Fatalf("The devnet flag and genesis file can't be used at the same time")
}
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis)
} else { } else {
if ctx.Args().Len() != 1 { if len(genesisPath) == 0 {
utils.Fatalf("need the genesis.json file or the network name [ mainnet | testnet | devnet ] as the only argument") utils.Fatalf("invalid path to genesis file")
} }
genesisPath := ctx.Args().First() var file *os.File
if genesisPath == "mainnet" || genesisPath == "xinfin" { file, err = os.Open(genesisPath)
err = json.Unmarshal(xdc_genesis.MainnetGenesis, &genesis) if err != nil {
} else if genesisPath == "testnet" || genesisPath == "apothem" { utils.Fatalf("Failed to read genesis file: %v", err)
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis)
} else if genesisPath == "devnet" {
err = json.Unmarshal(xdc_genesis.DevnetGenesis, &genesis)
} else {
if len(genesisPath) == 0 {
utils.Fatalf("invalid path to genesis file")
}
var file *os.File
file, err = os.Open(genesisPath)
if err != nil {
utils.Fatalf("Failed to read genesis file: %v", err)
}
defer file.Close()
err = json.NewDecoder(file).Decode(genesis)
} }
defer file.Close()
err = json.NewDecoder(file).Decode(genesis)
} }
if err != nil { if err != nil {
utils.Fatalf("invalid genesis json: %v", err) utils.Fatalf("invalid genesis json: %v", err)