erigon-pulse/cmd/state/commands/root.go

88 lines
1.8 KiB
Go
Raw Normal View History

package commands
import (
2019-12-06 07:18:26 +00:00
"context"
"encoding/json"
"fmt"
"os"
2019-12-06 07:18:26 +00:00
"os/signal"
"path"
2019-12-06 07:18:26 +00:00
"syscall"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/internal/debug"
2021-07-29 10:23:23 +00:00
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
)
var (
genesisPath string
genesis *core.Genesis
)
func init() {
2020-08-20 03:52:27 +00:00
utils.CobraFlags(rootCmd, append(debug.Flags, utils.MetricFlags...))
rootCmd.PersistentFlags().StringVar(&genesisPath, "genesis", "", "path to genesis.json file")
}
func rootContext() context.Context {
2019-12-06 07:18:26 +00:00
ctx, cancel := context.WithCancel(context.Background())
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(ch)
select {
case <-ch:
log.Info("Got interrupt, shutting down...")
case <-ctx.Done():
}
cancel()
}()
2019-12-06 07:36:21 +00:00
return ctx
2019-12-06 07:18:26 +00:00
}
var rootCmd = &cobra.Command{
Use: "state",
Short: "state is a utility for Stateless ethereum clients",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := debug.SetupCobra(cmd); err != nil {
panic(err)
}
genesis = core.DefaultGenesisBlock()
if genesisPath != "" {
genesis = genesisFromFile(genesisPath)
}
if chaindata == "" {
chaindata = path.Join(datadir, "erigon", "chaindata")
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
debug.Exit()
},
}
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
}
func Execute() {
if err := rootCmd.ExecuteContext(rootContext()); err != nil {
fmt.Println(err)
os.Exit(1)
}
}