From 351f86c796825ab4a08bc1074b61ca8278127a31 Mon Sep 17 00:00:00 2001 From: libotony Date: Tue, 23 Apr 2024 11:16:55 +0100 Subject: [PATCH] =?UTF-8?q?cmd:=20add=20genesis=20flag=20to=20solo,=20allo?= =?UTF-8?q?w=20specifying=20a=20custom=20genesis=20file=E2=80=A6=20(#682)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cmd: add genesis flag to solo, allow specifying a custom genesis file to solo * improve the code --- cmd/thor/flags.go | 39 +++++++++++++++++++---------------- cmd/thor/main.go | 21 ++++++++++++++++--- cmd/thor/utils.go | 52 ++++++++++++++++++++++++++++------------------- 3 files changed, 71 insertions(+), 41 deletions(-) diff --git a/cmd/thor/flags.go b/cmd/thor/flags.go index 3f04d83a0..94fc157b9 100644 --- a/cmd/thor/flags.go +++ b/cmd/thor/flags.go @@ -64,7 +64,6 @@ var ( Value: int(log15.LvlInfo), Usage: "log verbosity (0-9)", } - maxPeersFlag = cli.IntFlag{ Name: "max-peers", Usage: "maximum number of P2P network peers (P2P network disabled if set to 0)", @@ -80,18 +79,10 @@ var ( Value: "any", Usage: "port mapping mechanism (any|none|upnp|pmp|extip:)", } - onDemandFlag = cli.BoolFlag{ - Name: "on-demand", - Usage: "create new block when there is pending transaction", - } - persistFlag = cli.BoolFlag{ - Name: "persist", - Usage: "blockchain data storage option, if set data will be saved to disk", - } - gasLimitFlag = cli.IntFlag{ - Name: "gas-limit", - Value: 10000000, - Usage: "block gas limit(adaptive if set to 0)", + + bootNodeFlag = cli.StringFlag{ + Name: "bootnode", + Usage: "comma separated list of bootnode IDs", } importMasterKeyFlag = cli.BoolFlag{ Name: "import", @@ -106,10 +97,6 @@ var ( Value: 0, Usage: "target block gas limit (adaptive if set to 0)", } - bootNodeFlag = cli.StringFlag{ - Name: "bootnode", - Usage: "comma separated list of bootnode IDs", - } pprofFlag = cli.BoolFlag{ Name: "pprof", Usage: "turn on go-pprof", @@ -132,6 +119,20 @@ var ( Name: "disable-pruner", Usage: "disable state pruner to keep all history", } + // solo mode only flags + onDemandFlag = cli.BoolFlag{ + Name: "on-demand", + Usage: "create new block when there is pending transaction", + } + persistFlag = cli.BoolFlag{ + Name: "persist", + Usage: "blockchain data storage option, if set data will be saved to disk", + } + gasLimitFlag = cli.IntFlag{ + Name: "gas-limit", + Value: 10000000, + Usage: "block gas limit(adaptive if set to 0)", + } txPoolLimitFlag = cli.IntFlag{ Name: "txpool-limit", Value: 10000, @@ -142,4 +143,8 @@ var ( Value: 16, Usage: "set tx limit per account in pool", } + genesisFlag = cli.StringFlag{ + Name: "genesis", + Usage: "path to genesis file, if not set, the default devnet genesis will be used", + } ) diff --git a/cmd/thor/main.go b/cmd/thor/main.go index 3b393a6c0..b08d3fda2 100644 --- a/cmd/thor/main.go +++ b/cmd/thor/main.go @@ -94,6 +94,7 @@ func main() { Name: "solo", Usage: "client runs in solo mode for test & dev", Flags: []cli.Flag{ + genesisFlag, dataDirFlag, cacheFlag, apiAddrFlag, @@ -246,9 +247,23 @@ func soloAction(ctx *cli.Context) error { defer func() { log.Info("exited") }() initLogger(ctx) - gene := genesis.NewDevnet() - // Solo forks from the start - forkConfig := thor.ForkConfig{} + + var ( + gene *genesis.Genesis + forkConfig thor.ForkConfig + ) + + flagGenesis := ctx.String(genesisFlag.Name) + if flagGenesis == "" { + gene = genesis.NewDevnet() + forkConfig = thor.ForkConfig{} // Devnet forks from the start + } else { + var err error + gene, forkConfig, err = parseGenesisFile(flagGenesis) + if err != nil { + return err + } + } var mainDB *muxdb.MuxDB var logDB *logdb.LogDB diff --git a/cmd/thor/utils.go b/cmd/thor/utils.go index be6a62b45..9b81d0d65 100644 --- a/cmd/thor/utils.go +++ b/cmd/thor/utils.go @@ -51,6 +51,8 @@ import ( "gopkg.in/urfave/cli.v1" ) +var devNetGenesisID = genesis.NewDevnet().ID() + func initLogger(ctx *cli.Context) { logLevel := ctx.Int(verbosityFlag.Name) log15.Root().SetHandler(log15.LvlFilterHandler(log15.Lvl(logLevel), log15.StderrHandler)) @@ -202,30 +204,34 @@ func selectGenesis(ctx *cli.Context) (*genesis.Genesis, thor.ForkConfig, error) gene := genesis.NewMainnet() return gene, thor.GetForkConfig(gene.ID()), nil default: - file, err := os.Open(network) - if err != nil { - return nil, thor.ForkConfig{}, errors.Wrap(err, "open genesis file") - } - defer file.Close() + return parseGenesisFile(network) + } +} - decoder := json.NewDecoder(file) - decoder.DisallowUnknownFields() +func parseGenesisFile(filePath string) (*genesis.Genesis, thor.ForkConfig, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, thor.ForkConfig{}, errors.Wrap(err, "open genesis file") + } + defer file.Close() - var forkConfig = thor.NoFork - var gen genesis.CustomGenesis - gen.ForkConfig = &forkConfig + decoder := json.NewDecoder(file) + decoder.DisallowUnknownFields() - if err := decoder.Decode(&gen); err != nil { - return nil, thor.ForkConfig{}, errors.Wrap(err, "decode genesis file") - } + var forkConfig = thor.NoFork + var gen genesis.CustomGenesis + gen.ForkConfig = &forkConfig - customGen, err := genesis.NewCustomNet(&gen) - if err != nil { - return nil, thor.ForkConfig{}, errors.Wrap(err, "build genesis") - } + if err := decoder.Decode(&gen); err != nil { + return nil, thor.ForkConfig{}, errors.Wrap(err, "decode genesis file") + } - return customGen, forkConfig, nil + customGen, err := genesis.NewCustomNet(&gen) + if err != nil { + return nil, thor.ForkConfig{}, errors.Wrap(err, "build genesis") } + + return customGen, forkConfig, nil } func makeConfigDir(ctx *cli.Context) (string, error) { @@ -597,9 +603,6 @@ func printSoloStartupMessage( Forks [ %v ] Data dir [ %v ] API portal [ %v ] -┌──────────────────┬───────────────────────────────────────────────────────────────────────────────┐ -│ Mnemonic Words │ denial kitchen pet squirrel other broom bar gas better priority spoil cross │ -└──────────────────┴───────────────────────────────────────────────────────────────────────────────┘ `, common.MakeName("Thor solo", fullVersion()), gene.ID(), gene.Name(), @@ -608,6 +611,13 @@ func printSoloStartupMessage( dataDir, apiURL) + if gene.ID() == devNetGenesisID { + info += `┌──────────────────┬───────────────────────────────────────────────────────────────────────────────┐ +│ Mnemonic Words │ denial kitchen pet squirrel other broom bar gas better priority spoil cross │ +└──────────────────┴───────────────────────────────────────────────────────────────────────────────┘ +` + } + fmt.Print(info) }