2022-01-20 07:34:50 +00:00
|
|
|
package app
|
2021-12-27 07:59:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
2022-11-14 16:33:57 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2021-12-27 07:59:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/node"
|
|
|
|
)
|
|
|
|
|
|
|
|
var initCommand = cli.Command{
|
2022-11-15 20:38:31 +00:00
|
|
|
Action: MigrateFlags(initGenesis),
|
2021-12-27 07:59:29 +00:00
|
|
|
Name: "init",
|
|
|
|
Usage: "Bootstrap and initialize a new genesis block",
|
|
|
|
ArgsUsage: "<genesisPath>",
|
|
|
|
Flags: []cli.Flag{
|
2022-11-14 16:33:57 +00:00
|
|
|
&utils.DataDirFlag,
|
2021-12-27 07:59:29 +00:00
|
|
|
},
|
|
|
|
Category: "BLOCKCHAIN COMMANDS",
|
|
|
|
Description: `
|
|
|
|
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.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// Make sure we have a valid genesis JSON
|
|
|
|
genesisPath := ctx.Args().First()
|
|
|
|
if len(genesisPath) == 0 {
|
|
|
|
utils.Fatalf("Must supply path to genesis JSON file")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(genesisPath)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to read genesis file: %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
genesis := new(core.Genesis)
|
|
|
|
if err := json.NewDecoder(file).Decode(genesis); err != nil {
|
|
|
|
utils.Fatalf("invalid genesis file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open and initialise both full and light databases
|
|
|
|
stack := MakeConfigNodeDefault(ctx)
|
|
|
|
defer stack.Close()
|
|
|
|
|
|
|
|
chaindb, err := node.OpenDatabase(stack.Config(), log.New(ctx), kv.ChainDB)
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to open database: %v", err)
|
|
|
|
}
|
2023-02-11 20:44:51 +00:00
|
|
|
_, hash, err := core.CommitGenesisBlock(chaindb, genesis, "")
|
2021-12-27 07:59:29 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to write genesis block: %v", err)
|
|
|
|
}
|
|
|
|
chaindb.Close()
|
|
|
|
log.Info("Successfully wrote genesis state", "hash", hash.Hash())
|
|
|
|
return nil
|
|
|
|
}
|