cmd/XDC: remove genesis file dependency for XDC init command

This commit is contained in:
Daniel Liu 2025-02-13 12:27:29 +08:00
parent 47543e8b1c
commit d304a24edc
3 changed files with 39 additions and 161 deletions

File diff suppressed because one or more lines are too long

View file

@ -30,6 +30,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
xdc_genesis "github.com/XinFinOrg/XDPoSChain/genesis"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/urfave/cli/v2"
)
@ -51,7 +52,7 @@ The init command initializes a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
participating.
It expects the genesis file as argument.`,
It expects the genesis file or the network name [ mainnet | testnet | devnet ] as argument.`,
}
importCommand = &cli.Command{
Action: importChain,
@ -153,34 +154,40 @@ Use "ethereum dump 0" to dump the genesis block.`,
// 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.
func initGenesis(ctx *cli.Context) error {
var err error
genesis := new(core.Genesis)
if ctx.Bool(utils.XDCTestnetFlag.Name) {
if ctx.Args().Len() > 0 {
utils.Fatalf("Flags --apothem and genesis file can't be used at the same time")
}
err := json.Unmarshal(apothemGenesis, &genesis)
if err != nil {
utils.Fatalf("invalid genesis json: %v", err)
}
err = json.Unmarshal(xdc_genesis.TestnetGenesis, &genesis)
} else {
if ctx.Args().Len() != 1 {
utils.Fatalf("need genesis.json file as the only argument")
utils.Fatalf("need the genesis.json file or the network name [ mainnet | testnet | devnet ] as the only argument")
}
genesisPath := ctx.Args().First()
if len(genesisPath) == 0 {
utils.Fatalf("invalid path to genesis file")
}
file, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("Failed to read genesis file: %v", err)
}
defer file.Close()
if err := json.NewDecoder(file).Decode(genesis); err != nil {
utils.Fatalf("invalid genesis file: %v", err)
if genesisPath == "mainnet" {
err = json.Unmarshal(xdc_genesis.MainnetGenesis, &genesis)
} else if genesisPath == "testnet" {
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)
}
}
if err != nil {
utils.Fatalf("invalid genesis json: %v", err)
}
// Open an initialise both full and light databases
stack, _ := makeFullNode(ctx)

14
genesis/genesis.go Normal file
View file

@ -0,0 +1,14 @@
package genesis
import (
_ "embed"
)
//go:embed mainnet.json
var MainnetGenesis []byte
//go:embed testnet.json
var TestnetGenesis []byte
//go:embed devnet.json
var DevnetGenesis []byte