Skip to content

Commit

Permalink
Add genesis generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Oct 18, 2023
1 parent e144d26 commit 5033232
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 6 deletions.
6 changes: 4 additions & 2 deletions gno.land/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ help:
rundep=go run -modfile ../misc/devdeps/go.mod

.PHONY: build
build: build.gnoland build.gnokey build.gnoweb build.gnofaucet build.gnotxsync
build: build.gnoland build.gnokey build.gnoweb build.gnofaucet build.gnotxsync build.genesis

build.gnoland:; go build -o build/gnoland ./cmd/gnoland
build.gnoweb:; go build -o build/gnoweb ./cmd/gnoweb
build.gnofaucet:; go build -o build/gnofaucet ./cmd/gnofaucet
build.gnokey:; go build -o build/gnokey ./cmd/gnokey
build.gnotxsync:; go build -o build/gnotxsync ./cmd/gnotxsync
build.genesis:; go build -o build/genesis ./cmd/genesis

.PHONY: install
install: install.gnoland install.gnoweb install.gnofaucet install.gnokey install.gnotxsync
install: install.gnoland install.gnoweb install.gnofaucet install.gnokey install.gnotxsync install.genesis

install.gnoland:; go install ./cmd/gnoland
install.gnoweb:; go install ./cmd/gnoweb
install.gnofaucet:; go install ./cmd/gnofaucet
install.gnokey:; go install ./cmd/gnokey
install.gnotxsync:; go install ./cmd/gnotxsync
install.genesis:; go install ./cmd/genesis

.PHONY: fclean
fclean: clean
Expand Down
149 changes: 149 additions & 0 deletions gno.land/cmd/genesis/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package main

import (
"context"
"flag"
"fmt"
"time"

"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
)

var defaultChainID = "dev"

type generateCfg struct {
outputPath string
chainID string
genesisTime int64
blockMaxTxBytes int64
blockMaxDataBytes int64
blockMaxGas int64
blockTimeIota int64
}

// newGenerateCmd creates the genesis generate subcommand
func newGenerateCmd(io *commands.IO) *commands.Command {
cfg := &generateCfg{}

return commands.NewCommand(
commands.Metadata{
Name: "generate",
ShortUsage: "generate [flags]",
LongHelp: "Generates a node's genesis.json",
},
cfg,
func(_ context.Context, args []string) error {
return execGenerate(cfg, io)
},
)
}

func (c *generateCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.outputPath,
"output-path",
"./genesis.json",
"the output path for the genesis.json",
)

fs.Int64Var(
&c.genesisTime,
"genesis-time",
time.Now().Unix(),
"the genesis creation time. Defaults to current time",
)

fs.StringVar(
&c.chainID,
"chain-id",
defaultChainID,
"the ID of the chain",
)

fs.Int64Var(
&c.blockMaxTxBytes,
"block-max-tx-bytes",
types.MaxBlockTxBytes,
"the max size of the block transaction",
)

fs.Int64Var(
&c.blockMaxDataBytes,
"block-max-data-bytes",
types.MaxBlockDataBytes,
"the max size of the block data",
)

fs.Int64Var(
&c.blockMaxGas,
"block-max-gas",
types.MaxBlockMaxGas,
"the max gas limit for the block",
)

fs.Int64Var(
&c.blockTimeIota,
"block-time-iota",
types.BlockTimeIotaMS,
"the block time iota (in ms)",
)
}

func execGenerate(cfg *generateCfg, io *commands.IO) error {
// Start with the default configuration
genesis := getDefaultGenesis()

// Set the genesis time
if cfg.genesisTime > 0 {
genesis.GenesisTime = time.Unix(cfg.genesisTime, 0)
}

// Set the chain ID
if cfg.chainID != "" {
genesis.ChainID = cfg.chainID
}

// Set the max tx bytes
if cfg.blockMaxTxBytes > 0 {
genesis.ConsensusParams.Block.MaxTxBytes = cfg.blockMaxTxBytes
}

// Set the max data bytes
if cfg.blockMaxDataBytes > 0 {
genesis.ConsensusParams.Block.MaxDataBytes = cfg.blockMaxDataBytes
}

// Set the max block gas
if cfg.blockMaxGas > 0 {
genesis.ConsensusParams.Block.MaxGas = cfg.blockMaxGas
}

// Set the block time IOTA
if cfg.blockTimeIota > 0 {
genesis.ConsensusParams.Block.TimeIotaMS = cfg.blockTimeIota
}

// Validate the genesis
if validateErr := genesis.ValidateAndComplete(); validateErr != nil {
return fmt.Errorf("unable to validate genesis, %w", validateErr)
}

// Save the genesis file to disk
if saveErr := genesis.SaveAs(cfg.outputPath); saveErr != nil {
return fmt.Errorf("unable to save genesis, %w", saveErr)
}

io.Printfln("Genesis successfully generated at %s", cfg.outputPath)

return nil
}

// getDefaultGenesis returns the default genesis config
func getDefaultGenesis() *types.GenesisDoc {
return &types.GenesisDoc{
GenesisTime: time.Now(),
ChainID: defaultChainID,
ConsensusParams: types.DefaultConsensusParams(),
}
}
Loading

0 comments on commit 5033232

Please sign in to comment.