Skip to content

Commit

Permalink
cmd: add genesis flag to solo, allow specifying a custom genesis file… (
Browse files Browse the repository at this point in the history
#682)

* cmd: add genesis flag to solo, allow specifying a custom genesis file to solo

* improve the code
  • Loading branch information
libotony authored Apr 23, 2024
1 parent 3ad407a commit 351f86c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
39 changes: 22 additions & 17 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -80,18 +79,10 @@ var (
Value: "any",
Usage: "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
}
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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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",
}
)
21 changes: 18 additions & 3 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func main() {
Name: "solo",
Usage: "client runs in solo mode for test & dev",
Flags: []cli.Flag{
genesisFlag,
dataDirFlag,
cacheFlag,
apiAddrFlag,
Expand Down Expand Up @@ -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
Expand Down
52 changes: 31 additions & 21 deletions cmd/thor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
}

Expand Down

0 comments on commit 351f86c

Please sign in to comment.