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"
|
2022-10-25 02:58:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/debug"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/logging"
|
2022-03-10 14:01:37 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
2022-03-10 14:01:37 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2019-11-25 13:46:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-12 18:36:14 +00:00
|
|
|
genesisPath string
|
|
|
|
genesis *core.Genesis
|
2022-03-10 14:01:37 +00:00
|
|
|
chainConfig *params.ChainConfig
|
2019-11-25 13:46:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-10-21 05:36:17 +00:00
|
|
|
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging.Flags)
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:01:37 +00:00
|
|
|
genesis, chainConfig = getChainGenesisAndConfig()
|
2020-03-30 18:56:53 +00:00
|
|
|
if genesisPath != "" {
|
|
|
|
genesis = genesisFromFile(genesisPath)
|
|
|
|
}
|
2022-03-10 14:01:37 +00:00
|
|
|
if genesis.Config != nil && genesis.Config.ChainID.Cmp(chainConfig.ChainID) != 0 {
|
|
|
|
utils.Fatalf("provided genesis.json chain configuration is invalid: expected chainId to be %v, got %v",
|
|
|
|
chainConfig.ChainID.String(), genesis.Config.ChainID.String())
|
|
|
|
}
|
|
|
|
// Apply special hacks for BSC params
|
|
|
|
if chainConfig.Parlia != nil {
|
|
|
|
params.ApplyBinanceSmartChainParams()
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:25:26 +00:00
|
|
|
if chaindata == "" {
|
2022-10-21 06:45:20 +00:00
|
|
|
chaindata = filepath.Join(datadirCli, "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
|
|
|
|
}
|
|
|
|
|
2022-03-10 14:01:37 +00:00
|
|
|
func getChainGenesisAndConfig() (genesis *core.Genesis, chainConfig *params.ChainConfig) {
|
|
|
|
if chain == "" {
|
|
|
|
genesis, chainConfig = core.DefaultGenesisBlock(), params.MainnetChainConfig
|
|
|
|
} else {
|
|
|
|
genesis, chainConfig = core.DefaultGenesisBlockByChainName(chain), params.ChainConfigByChainName(chain)
|
|
|
|
}
|
|
|
|
return genesis, chainConfig
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|