Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnoland): pass genesis file as a flag #1972

Merged
merged 19 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions gno.land/cmd/gnoland/config_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ func TestConfig_Get_Base(t *testing.T) {
assert.Equal(t, loadedCfg.DBPath, value)
},
},
{
"genesis path fetched",
"genesis_file",
func(loadedCfg *config.Config, value string) {
assert.Equal(t, loadedCfg.Genesis, value)
},
},
{
"validator key fetched",
"priv_validator_key_file",
Expand Down
10 changes: 0 additions & 10 deletions gno.land/cmd/gnoland/config_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,6 @@ func TestConfig_Set_Base(t *testing.T) {
assert.Equal(t, value, loadedCfg.DBPath)
},
},
{
"genesis path updated",
[]string{
"genesis_file",
"example path",
},
func(loadedCfg *config.Config, value string) {
assert.Equal(t, value, loadedCfg.Genesis)
},
},
{
"validator key updated",
[]string{
Expand Down
20 changes: 13 additions & 7 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type startCfg struct {
skipStart bool
genesisBalancesFile string
genesisTxsFile string
genesisFile string
chainID string
genesisRemote string
dataDir string
Expand Down Expand Up @@ -106,6 +107,13 @@ func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
"initial txs to replay",
)

fs.StringVar(
&c.genesisFile,
"genesis",
"genesis.json",
"path to genesis file",
)

fs.StringVar(
&c.chainID,
"chainid",
Expand Down Expand Up @@ -241,11 +249,9 @@ func execStart(c *startCfg, io commands.IO) error {
logger := log.ZapLoggerToSlog(zapLogger)

// Write genesis file if missing.
// NOTE: this will be dropped in a PR that resolves issue #1883:
// https://github.com/gnolang/gno/issues/1883
genesisFilePath := filepath.Join(nodeDir, "../", "genesis.json")

if !osm.FileExists(genesisFilePath) {
// NOTE: this will be dropped in a PR that resolves issue #1886:
// https://github.com/gnolang/gno/issues/1886
if !osm.FileExists(c.genesisFile) {
// Create priv validator first.
// Need it to generate genesis.json
newPrivValKey := cfg.PrivValidatorKeyFile()
Expand All @@ -254,7 +260,7 @@ func execStart(c *startCfg, io commands.IO) error {
pk := priv.GetPubKey()

// Generate genesis.json file
if err := generateGenesisFile(genesisFilePath, pk, c); err != nil {
if err := generateGenesisFile(c.genesisFile, pk, c); err != nil {
return fmt.Errorf("unable to generate genesis file: %w", err)
}
}
Expand All @@ -277,7 +283,7 @@ func execStart(c *startCfg, io commands.IO) error {
io.Println(startGraphic)
}

gnoNode, err := node.DefaultNewNode(cfg, genesisFilePath, logger)
gnoNode, err := node.DefaultNewNode(cfg, c.genesisFile, logger)
if err != nil {
return fmt.Errorf("error in creating node: %w", err)
}
Expand Down
13 changes: 12 additions & 1 deletion gno.land/cmd/gnoland/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"path/filepath"
"testing"
"time"

Expand All @@ -14,15 +15,24 @@ import (
func TestStartInitialize(t *testing.T) {
t.Parallel()

// NOTE: cannot be txtar tests as they use their own parsing for the
// "gnoland" command line. See pkg/integration.

var (
nodeDir = t.TempDir()
nodeDir = t.TempDir()
genesisFile = filepath.Join(nodeDir, "test_genesis.json")

args = []string{
"start",
"--skip-start",
"--skip-failing-genesis-txs",

// These two flags are tested together as they would otherwise
// pollute this directory (cmd/gnoland) if not set.
"--data-dir",
nodeDir,
"--genesis",
genesisFile,
}
)

Expand All @@ -42,4 +52,5 @@ func TestStartInitialize(t *testing.T) {

// Make sure the directory is created
assert.DirExists(t, nodeDir)
assert.FileExists(t, genesisFile)
}
11 changes: 0 additions & 11 deletions tm2/pkg/bft/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
errInvalidMoniker = errors.New("moniker not set")
errInvalidDBBackend = errors.New("invalid DB backend")
errInvalidDBPath = errors.New("invalid DB path")
errInvalidGenesisPath = errors.New("invalid genesis path")
errInvalidPrivValidatorKeyPath = errors.New("invalid private validator key path")
errInvalidPrivValidatorStatePath = errors.New("invalid private validator state file path")
errInvalidABCIMechanism = errors.New("invalid ABCI mechanism")
Expand Down Expand Up @@ -205,7 +204,6 @@ var (
defaultSecretsDir = "secrets"

defaultConfigFileName = "config.toml"
defaultGenesisJSONName = "genesis.json"
defaultNodeKeyName = "node_key.json"
defaultPrivValKeyName = "priv_validator_key.json"
defaultPrivValStateName = "priv_validator_state.json"
Expand Down Expand Up @@ -271,9 +269,6 @@ type BaseConfig struct {
// Database directory
DBPath string `toml:"db_dir" comment:"Database directory"`

// Path to the JSON file containing the initial validator set and other meta data
Genesis string `toml:"genesis_file" comment:"Path to the JSON file containing the initial validator set and other meta data"`

// Path to the JSON file containing the private key to use as a validator in the consensus protocol
PrivValidatorKey string `toml:"priv_validator_key_file" comment:"Path to the JSON file containing the private key to use as a validator in the consensus protocol"`

Expand Down Expand Up @@ -301,7 +296,6 @@ type BaseConfig struct {
// DefaultBaseConfig returns a default base configuration for a Tendermint node
func DefaultBaseConfig() BaseConfig {
return BaseConfig{
Genesis: defaultGenesisJSONName,
PrivValidatorKey: defaultPrivValKeyPath,
PrivValidatorState: defaultPrivValStatePath,
NodeKey: defaultNodeKeyPath,
Expand Down Expand Up @@ -382,11 +376,6 @@ func (cfg BaseConfig) ValidateBasic() error {
return errInvalidDBPath
}

// Verify the genesis path is set
if cfg.Genesis == "" {
return errInvalidGenesisPath
}

// Verify the validator private key path is set
if cfg.PrivValidatorKey == "" {
return errInvalidPrivValidatorKeyPath
Expand Down
9 changes: 0 additions & 9 deletions tm2/pkg/bft/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ func TestConfig_ValidateBaseConfig(t *testing.T) {
assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidDBPath)
})

t.Run("genesis path not set", func(t *testing.T) {
t.Parallel()

c := DefaultConfig()
c.Genesis = ""

assert.ErrorIs(t, c.BaseConfig.ValidateBasic(), errInvalidGenesisPath)
})

t.Run("priv validator key path not set", func(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 5 additions & 1 deletion tm2/pkg/bft/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func ResetTestRoot(testName string) (*Config, string) {

baseConfig := DefaultBaseConfig()
configFilePath := filepath.Join(rootDir, defaultConfigPath)
genesisFilePath := filepath.Join(rootDir, defaultGenesisJSONName)
// NOTE: this does not match the behaviour of the Gno.land node.
// However, many tests rely on the fact that they can cleanup the directory
// by doing RemoveAll on the rootDir; so to keep compatibility with that
// behaviour, we place genesis.json in the rootDir.
genesisFilePath := filepath.Join(rootDir, "genesis.json")
privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey)
privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState)

Expand Down
3 changes: 1 addition & 2 deletions tm2/pkg/bft/config/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestEnsureTestRoot(t *testing.T) {
ensureFiles(
t,
rootDir,
"genesis.json",
DefaultDBDir,
baseConfig.Genesis,
baseConfig.PrivValidatorKey,
baseConfig.PrivValidatorState,
)
Expand All @@ -93,7 +93,6 @@ func checkConfig(configFile string) bool {
"wal",
"propose",
"max",
"genesis",
}
for _, e := range elems {
if !strings.Contains(configFile, e) {
Expand Down
2 changes: 0 additions & 2 deletions tm2/pkg/bft/consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,3 @@ func (pb *playback) replayConsoleLoop() int {
}
}
}

// --------------------------------------------------------------------------------
6 changes: 3 additions & 3 deletions tm2/pkg/bft/rpc/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func StartTendermint(app abci.Application, opts ...func(*Options)) *nm.Node {
for _, opt := range opts {
opt(&nodeOpts)
}
node := NewTendermint(app, &nodeOpts)
node := newTendermint(app, &nodeOpts)
err := node.Start()
if err != nil {
panic(err)
Expand All @@ -112,8 +112,8 @@ func StopTendermint(node *nm.Node) {
os.RemoveAll(node.Config().RootDir)
}

// NewTendermint creates a new tendermint server and sleeps forever
func NewTendermint(app abci.Application, opts *Options) *nm.Node {
// newTendermint creates a new tendermint server and sleeps forever
func newTendermint(app abci.Application, opts *Options) *nm.Node {
// Create & start node
config, genesisFile := GetConfig(opts.recreateConfig)

Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/bft/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Bl
return block
}

func makeStateAndBlockStore(logger *slog.Logger) (sm.State, *BlockStore, cleanupFunc) {
func makeStateAndBlockStore(_ *slog.Logger) (sm.State, *BlockStore, cleanupFunc) {
config, genesisFile := cfg.ResetTestRoot("blockchain_reactor_test")
// blockDB := dbm.NewDebugDB("blockDB", memdb.NewMemDB())
// stateDB := dbm.NewDebugDB("stateDB", memdb.NewMemDB())
Expand Down
Loading