2019-11-25 13:46:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-12-06 07:18:26 +00:00
|
|
|
"context"
|
2020-03-30 18:56:53 +00:00
|
|
|
"encoding/json"
|
2019-11-25 13:46:36 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-12-06 07:18:26 +00:00
|
|
|
"os/signal"
|
2021-04-19 07:25:26 +00:00
|
|
|
"path"
|
2019-12-06 07:18:26 +00:00
|
|
|
"syscall"
|
2019-11-25 13:46:36 +00:00
|
|
|
|
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"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
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
|
|
|
}
|
|
|
|
|
2020-04-04 07:18:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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 == "" {
|
2021-05-26 10:35:39 +00:00
|
|
|
chaindata = path.Join(datadir, "erigon", "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() {
|
2020-04-04 07:18:10 +00:00
|
|
|
if err := rootCmd.ExecuteContext(rootContext()); err != nil {
|
2019-11-25 13:46:36 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|