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 5 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 @@
skipStart bool
genesisBalancesFile string
genesisTxsFile string
genesisFile string
chainID string
genesisRemote string
dataDir string
Expand Down Expand Up @@ -106,6 +107,13 @@
"initial txs to replay",
)

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

fs.StringVar(
&c.chainID,
"chainid",
Expand Down Expand Up @@ -197,7 +205,7 @@
// Get the absolute path to the node's data directory
nodeDir, err := filepath.Abs(c.dataDir)
if err != nil {
return fmt.Errorf("unable to get absolute path for data directory, %w", err)

Check warning on line 208 in gno.land/cmd/gnoland/start.go

View check run for this annotation

Codecov / codecov/patch

gno.land/cmd/gnoland/start.go#L208

Added line #L208 was not covered by tests
}

var (
Expand Down Expand Up @@ -241,11 +249,9 @@
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, "../", cfg.Genesis)

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 @@
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 @@
io.Println(startGraphic)
}

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

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/stretchr/testify/assert"
Expand All @@ -14,15 +14,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 @@ -33,13 +42,10 @@ func TestStartInitialize(t *testing.T) {
io.SetOut(commands.WriteNopCloser(mockOut))
io.SetErr(commands.WriteNopCloser(mockErr))

// Create and run the command
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()

cmd := newRootCmd(io)
require.NoError(t, cmd.ParseAndRun(ctx, args))
require.NoError(t, cmd.ParseAndRun(context.Background(), args))

// Make sure the directory is created
assert.DirExists(t, nodeDir)
assert.FileExists(t, genesisFile)
}
4 changes: 2 additions & 2 deletions tm2/pkg/bft/blockchain/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func newBlockchainReactor(logger *slog.Logger, genDoc *types.GenesisDoc, privVal
func TestNoBlockResponse(t *testing.T) {
t.Parallel()

config = cfg.ResetTestRoot("blockchain_reactor_test")
config, _ = cfg.ResetTestRoot("blockchain_reactor_test")
defer os.RemoveAll(config.RootDir)
genDoc, privVals := randGenesisDoc(1, false, 30)

Expand Down Expand Up @@ -182,7 +182,7 @@ func TestFlappyBadBlockStopsPeer(t *testing.T) {

testutils.FilterStability(t, testutils.Flappy)

config = cfg.ResetTestRoot("blockchain_reactor_test")
config, _ = cfg.ResetTestRoot("blockchain_reactor_test")
defer os.RemoveAll(config.RootDir)
genDoc, privVals := randGenesisDoc(1, false, 30)

Expand Down
16 changes: 0 additions & 16 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 @@ -200,7 +199,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 @@ -264,9 +262,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 @@ -294,7 +289,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 @@ -323,11 +317,6 @@ func (cfg BaseConfig) ChainID() string {
return cfg.chainID
}

// GenesisFile returns the full path to the genesis.json file
func (cfg BaseConfig) GenesisFile() string {
return filepath.Join(cfg.RootDir, "../", defaultGenesisJSONName)
}

// PrivValidatorKeyFile returns the full path to the priv_validator_key.json file
func (cfg BaseConfig) PrivValidatorKeyFile() string {
return filepath.Join(cfg.RootDir, cfg.PrivValidatorKey)
Expand Down Expand Up @@ -380,11 +369,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
10 changes: 7 additions & 3 deletions tm2/pkg/bft/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func WriteConfigFile(configFilePath string, config *Config) error {

/****** these are for test settings ***********/

func ResetTestRoot(testName string) *Config {
func ResetTestRoot(testName string) (cfg *Config, genesisFile string) {
chainID := "test-chain"

// create a unique, concurrency-safe test directory under os.TempDir()
Expand All @@ -78,7 +78,11 @@ func ResetTestRoot(testName string) *Config {

baseConfig := DefaultBaseConfig()
configFilePath := filepath.Join(rootDir, defaultConfigFileName)
genesisFilePath := filepath.Join(rootDir, "../", baseConfig.Genesis)
// 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 All @@ -98,7 +102,7 @@ func ResetTestRoot(testName string) *Config {
osm.MustWriteFile(privStateFilePath, []byte(testPrivValidatorState), 0o644)

config := TestConfig().SetRootDir(rootDir)
return config
return config, genesisFilePath
}

var testGenesisFmt = `{
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/bft/config/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestEnsureTestRoot(t *testing.T) {
testName := "ensureTestRoot"

// create root dir
cfg := ResetTestRoot(testName)
cfg, genesisFile := ResetTestRoot(testName)
defer os.RemoveAll(cfg.RootDir)
rootDir := cfg.RootDir

Expand All @@ -76,7 +76,7 @@ func TestEnsureTestRoot(t *testing.T) {
ensureFiles(
t,
filepath.Join(rootDir, ".."),
baseConfig.Genesis,
genesisFile,
)
}

Expand Down
8 changes: 4 additions & 4 deletions tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ensureDir(dir string, mode os.FileMode) {
}
}

func ResetConfig(name string) *cfg.Config {
func ResetConfig(name string) (*cfg.Config, string) {
return cfg.ResetTestRoot(name)
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func subscribeToVoter(cs *ConsensusState, addr crypto.Address) <-chan events.Eve
// consensus states

func newConsensusState(state sm.State, pv types.PrivValidator, app abci.Application) *ConsensusState {
config := cfg.ResetTestRoot("consensus_state_test")
config, _ := cfg.ResetTestRoot("consensus_state_test")
return newConsensusStateWithConfig(config, state, pv, app)
}

Expand Down Expand Up @@ -578,7 +578,7 @@ func randConsensusNet(nValidators int, testName string, tickerFunc func() Timeou
for i := 0; i < nValidators; i++ {
stateDB := memdb.NewMemDB() // each state needs its own db
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
thisConfig, _ := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
configRootDirs = append(configRootDirs, thisConfig.RootDir)
for _, opt := range configOpts {
opt(thisConfig)
Expand Down Expand Up @@ -618,7 +618,7 @@ func randConsensusNetWithPeers(nValidators, nPeers int, testName string, tickerF
for i := 0; i < nPeers; i++ {
stateDB := memdb.NewMemDB() // each state needs its own db
state, _ := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
thisConfig := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
thisConfig, _ := ResetConfig(fmt.Sprintf("%s_%d", testName, i))
configRootDirs = append(configRootDirs, thisConfig.RootDir)
ensureDir(filepath.Dir(thisConfig.Consensus.WalFile()), 0o700) // dir for wal
if i == 0 {
Expand Down
6 changes: 3 additions & 3 deletions tm2/pkg/bft/consensus/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func assertMempool(txn txNotifier) mempl.Mempool {
func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
t.Parallel()

config := ResetConfig("consensus_mempool_no_progress_until_txs_available")
config, _ := ResetConfig("consensus_mempool_no_progress_until_txs_available")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(1, false, 10)
Expand All @@ -51,7 +51,7 @@ func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
}

func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
config := ResetConfig("consensus_mempool_progress_after_create_empty_blocks_interval")
config, _ := ResetConfig("consensus_mempool_progress_after_create_empty_blocks_interval")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
state, privVals := randGenesisState(1, false, 10)
Expand All @@ -75,7 +75,7 @@ func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
func TestMempoolProgressInHigherRound(t *testing.T) {
t.Parallel()

config := ResetConfig("consensus_mempool_progress_in_higher_round")
config, _ := ResetConfig("consensus_mempool_progress_in_higher_round")
defer os.RemoveAll(config.RootDir)
config.Consensus.CreateEmptyBlocks = false
state, privVals := randGenesisState(1, false, 10)
Expand Down
8 changes: 4 additions & 4 deletions tm2/pkg/bft/consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
// replay messages interactively or all at once

// replay the wal file
func RunReplayFile(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig, console bool) {
consensusState := newConsensusStateForReplay(config, csConfig)
func RunReplayFile(config cfg.BaseConfig, genesisFile string, csConfig *cnscfg.ConsensusConfig, console bool) {
consensusState := newConsensusStateForReplay(config, genesisFile, csConfig)

if err := consensusState.ReplayFile(csConfig.WalFile(), console); err != nil {
osm.Exit(fmt.Sprintf("Error during consensus replay: %v", err))
Expand Down Expand Up @@ -270,7 +270,7 @@ func (pb *playback) replayConsoleLoop() int {
// --------------------------------------------------------------------------------

// convenience for replay mode
func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig) *ConsensusState {
func newConsensusStateForReplay(config cfg.BaseConfig, genesisFile string, csConfig *cnscfg.ConsensusConfig) *ConsensusState {
dbType := dbm.BackendType(config.DBBackend)
// Get BlockStore
blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir())
Expand All @@ -286,7 +286,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cnscfg.Consensu
osm.Exit(err.Error())
}

gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile())
gdoc, err := sm.MakeGenesisDocFromFile(genesisFile)
if err != nil {
osm.Exit(err.Error())
}
Expand Down
Loading
Loading