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,36 +155,19 @@ 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)
var err error
genesis := new(core.Genesis)
if ctx.Bool(utils.MainnetFlag.Name) {
if ctx.Args().Len() > 0 {
utils.Fatalf("The mainnet flag and genesis file can't be used at the same time")
}
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis)
} else if ctx.Bool(utils.TestnetFlag.Name) {
if ctx.Args().Len() > 0 {
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 {
if ctx.Args().Len() != 1 { if ctx.Args().Len() != 1 {
utils.Fatalf("need the genesis.json file or the network name [ mainnet | testnet | devnet ] as the only argument") utils.Fatalf("need the genesis.json file or the network name [ mainnet | testnet | devnet ] as the only argument")
} }
var err error
genesis := new(core.Genesis)
genesisPath := ctx.Args().First() genesisPath := ctx.Args().First()
if genesisPath == "mainnet" || genesisPath == "xinfin" { if genesisPath == "mainnet" || genesisPath == "xinfin" {
err = json.Unmarshal(xdc_genesis.MainnetGenesis, &genesis) err = json.Unmarshal(xdc_genesis.MainnetGenesis, genesis)
} else if genesisPath == "testnet" || genesisPath == "apothem" { } else if genesisPath == "testnet" || genesisPath == "apothem" {
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis) err = json.Unmarshal(xdc_genesis.TestnetGenesis, genesis)
} else if genesisPath == "devnet" { } else if genesisPath == "devnet" {
err = json.Unmarshal(xdc_genesis.DevnetGenesis, &genesis) err = json.Unmarshal(xdc_genesis.DevnetGenesis, genesis)
} else { } else {
if len(genesisPath) == 0 { if len(genesisPath) == 0 {
utils.Fatalf("invalid path to genesis file") utils.Fatalf("invalid path to genesis file")
@ -197,7 +180,6 @@ func initGenesis(ctx *cli.Context) error {
defer file.Close() defer file.Close()
err = json.NewDecoder(file).Decode(genesis) 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)
} }