erigon-pulse/cmd/state/commands/root.go
ledgerwatch fdd385cef1
[Devnet tool] Side-quest to improve logging - part 1 (#7445)
This is the beginning of the series of changes to make it possible to
run multiple instances of erigon inside a single process (as devnet tool
does), with the logging from these processes going to respective log
files correctly.
This is the first part where the initial infrastructure is being
established

---------

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro-2.local>
2023-05-07 07:28:15 +01:00

86 lines
2.2 KiB
Go

package commands
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
chain2 "github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/spf13/cobra"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/erigon/turbo/logging"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/params"
)
var (
genesisPath string
genesis *types.Genesis
chainConfig *chain2.Config
)
func init() {
utils.CobraFlags(rootCmd, debug.Flags, utils.MetricFlags, logging.Flags)
rootCmd.PersistentFlags().StringVar(&genesisPath, "genesis", "", "path to genesis.json file")
}
var rootCmd = &cobra.Command{
Use: "state",
Short: "state is a utility for Stateless ethereum clients",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
genesis, chainConfig = getChainGenesisAndConfig()
if genesisPath != "" {
genesis = genesisFromFile(genesisPath)
}
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())
}
if chaindata == "" {
chaindata = filepath.Join(datadirCli, "chaindata")
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
debug.Exit()
},
}
func genesisFromFile(genesisPath string) *types.Genesis {
file, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("Failed to read genesis file: %v", err)
}
defer file.Close()
genesis := new(types.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
utils.Fatalf("invalid genesis file: %v", err)
}
return genesis
}
func getChainGenesisAndConfig() (genesis *types.Genesis, chainConfig *chain2.Config) {
if chain == "" {
genesis, chainConfig = core.MainnetGenesisBlock(), params.MainnetChainConfig
} else {
genesis, chainConfig = core.GenesisBlockByChainName(chain), params.ChainConfigByChainName(chain)
}
return genesis, chainConfig
}
func Execute() {
ctx, cancel := common.RootContext()
defer cancel()
if err := rootCmd.ExecuteContext(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
}
}