2019-11-25 13:46:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-03-30 18:56:53 +00:00
|
|
|
"encoding/json"
|
2019-11-25 13:46:36 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-02-12 13:33:09 +00:00
|
|
|
"path/filepath"
|
2019-11-25 13:46:36 +00:00
|
|
|
|
2022-01-19 03:49:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/internal/debug"
|
2020-01-07 02:27:19 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-11-25 13:46:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-12 18:36:14 +00:00
|
|
|
genesisPath string
|
|
|
|
genesis *core.Genesis
|
2019-11-25 13:46:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-08-20 03:52:27 +00:00
|
|
|
utils.CobraFlags(rootCmd, append(debug.Flags, utils.MetricFlags...))
|
2020-03-30 18:56:53 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&genesisPath, "genesis", "", "path to genesis.json file")
|
2019-11-25 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "state",
|
|
|
|
Short: "state is a utility for Stateless ethereum clients",
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2020-04-12 18:36:14 +00:00
|
|
|
if err := debug.SetupCobra(cmd); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:56:53 +00:00
|
|
|
genesis = core.DefaultGenesisBlock()
|
|
|
|
if genesisPath != "" {
|
|
|
|
genesis = genesisFromFile(genesisPath)
|
|
|
|
}
|
2021-04-19 07:25:26 +00:00
|
|
|
if chaindata == "" {
|
2022-02-12 13:33:09 +00:00
|
|
|
chaindata = filepath.Join(datadir, "chaindata")
|
2021-04-19 07:25:26 +00:00
|
|
|
}
|
2019-11-25 13:46:36 +00:00
|
|
|
},
|
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
2020-04-12 18:36:14 +00:00
|
|
|
debug.Exit()
|
2019-11-25 13:46:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:56:53 +00:00
|
|
|
func genesisFromFile(genesisPath string) *core.Genesis {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return genesis
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:46:36 +00:00
|
|
|
func Execute() {
|
2022-01-19 03:49:07 +00:00
|
|
|
ctx, cancel := common.RootContext()
|
|
|
|
defer cancel()
|
|
|
|
if err := rootCmd.ExecuteContext(ctx); err != nil {
|
2019-11-25 13:46:36 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|