diff --git a/gno.land/cmd/gnoland/start.go b/gno.land/cmd/gnoland/start.go index cc1030a33b9..42bd2145ab0 100644 --- a/gno.land/cmd/gnoland/start.go +++ b/gno.land/cmd/gnoland/start.go @@ -243,7 +243,7 @@ func execStart(c *startCfg, io commands.IO) error { // 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) + genesisFilePath := filepath.Join(nodeDir, "../", "genesis.json") if !osm.FileExists(genesisFilePath) { // Create priv validator first. @@ -277,7 +277,7 @@ func execStart(c *startCfg, io commands.IO) error { io.Println(startGraphic) } - gnoNode, err := node.DefaultNewNode(cfg, logger) + gnoNode, err := node.DefaultNewNode(cfg, genesisFilePath, logger) if err != nil { return fmt.Errorf("error in creating node: %w", err) } diff --git a/tm2/pkg/bft/blockchain/reactor_test.go b/tm2/pkg/bft/blockchain/reactor_test.go index 7f96cdefc97..a40dbc6376b 100644 --- a/tm2/pkg/bft/blockchain/reactor_test.go +++ b/tm2/pkg/bft/blockchain/reactor_test.go @@ -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) @@ -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) diff --git a/tm2/pkg/bft/config/config.go b/tm2/pkg/bft/config/config.go index 8e1c0883cc7..8668dde5003 100644 --- a/tm2/pkg/bft/config/config.go +++ b/tm2/pkg/bft/config/config.go @@ -330,11 +330,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) diff --git a/tm2/pkg/bft/config/toml.go b/tm2/pkg/bft/config/toml.go index 825a0885ad8..474fce0e8a3 100644 --- a/tm2/pkg/bft/config/toml.go +++ b/tm2/pkg/bft/config/toml.go @@ -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) (*Config, string) { chainID := "test-chain" // create a unique, concurrency-safe test directory under os.TempDir() @@ -81,7 +81,7 @@ func ResetTestRoot(testName string) *Config { baseConfig := DefaultBaseConfig() configFilePath := filepath.Join(rootDir, defaultConfigPath) - genesisFilePath := filepath.Join(rootDir, "../", baseConfig.Genesis) + genesisFilePath := filepath.Join(rootDir, defaultGenesisJSONName) privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey) privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState) @@ -101,7 +101,8 @@ func ResetTestRoot(testName string) *Config { osm.MustWriteFile(privStateFilePath, []byte(testPrivValidatorState), 0o644) config := TestConfig().SetRootDir(rootDir) - return config + + return config, genesisFilePath } var testGenesisFmt = `{ diff --git a/tm2/pkg/bft/config/toml_test.go b/tm2/pkg/bft/config/toml_test.go index 864a57248ac..53dea1f2dfe 100644 --- a/tm2/pkg/bft/config/toml_test.go +++ b/tm2/pkg/bft/config/toml_test.go @@ -53,7 +53,7 @@ func TestEnsureTestRoot(t *testing.T) { testName := "ensureTestRoot" // create root dir - cfg := ResetTestRoot(testName) + cfg, _ := ResetTestRoot(testName) defer os.RemoveAll(cfg.RootDir) rootDir := cfg.RootDir @@ -69,15 +69,10 @@ func TestEnsureTestRoot(t *testing.T) { t, rootDir, DefaultDBDir, + baseConfig.Genesis, baseConfig.PrivValidatorKey, baseConfig.PrivValidatorState, ) - - ensureFiles( - t, - filepath.Join(rootDir, ".."), - baseConfig.Genesis, - ) } func checkConfig(configFile string) bool { diff --git a/tm2/pkg/bft/consensus/common_test.go b/tm2/pkg/bft/consensus/common_test.go index cace1aac43a..d4c572c6bda 100644 --- a/tm2/pkg/bft/consensus/common_test.go +++ b/tm2/pkg/bft/consensus/common_test.go @@ -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) } @@ -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) } @@ -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) @@ -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 { diff --git a/tm2/pkg/bft/consensus/mempool_test.go b/tm2/pkg/bft/consensus/mempool_test.go index f425a161e90..b61f0e44245 100644 --- a/tm2/pkg/bft/consensus/mempool_test.go +++ b/tm2/pkg/bft/consensus/mempool_test.go @@ -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) @@ -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) @@ -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) diff --git a/tm2/pkg/bft/consensus/replay_file.go b/tm2/pkg/bft/consensus/replay_file.go index bddc6b429be..03ddb2d2722 100644 --- a/tm2/pkg/bft/consensus/replay_file.go +++ b/tm2/pkg/bft/consensus/replay_file.go @@ -9,19 +9,11 @@ import ( "strconv" "strings" - "github.com/gnolang/gno/tm2/pkg/bft/appconn" - cfg "github.com/gnolang/gno/tm2/pkg/bft/config" - cnscfg "github.com/gnolang/gno/tm2/pkg/bft/consensus/config" cstypes "github.com/gnolang/gno/tm2/pkg/bft/consensus/types" - "github.com/gnolang/gno/tm2/pkg/bft/mempool/mock" - "github.com/gnolang/gno/tm2/pkg/bft/proxy" sm "github.com/gnolang/gno/tm2/pkg/bft/state" - "github.com/gnolang/gno/tm2/pkg/bft/store" walm "github.com/gnolang/gno/tm2/pkg/bft/wal" - dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/errors" "github.com/gnolang/gno/tm2/pkg/events" - "github.com/gnolang/gno/tm2/pkg/log" osm "github.com/gnolang/gno/tm2/pkg/os" ) @@ -33,15 +25,6 @@ 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) - - if err := consensusState.ReplayFile(csConfig.WalFile(), console); err != nil { - osm.Exit(fmt.Sprintf("Error during consensus replay: %v", err)) - } -} - // Replay msgs in file or start the console func (cs *ConsensusState) ReplayFile(file string, console bool) error { if cs.IsRunning() { @@ -268,64 +251,3 @@ func (pb *playback) replayConsoleLoop() int { } // -------------------------------------------------------------------------------- - -// convenience for replay mode -func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig) *ConsensusState { - dbType := dbm.BackendType(config.DBBackend) - // Get BlockStore - blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir()) - if err != nil { - osm.Exit(err.Error()) - } - - blockStore := store.NewBlockStore(blockStoreDB) - - // Get State - stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) - if err != nil { - osm.Exit(err.Error()) - } - - gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile()) - if err != nil { - osm.Exit(err.Error()) - } - state, err := sm.MakeGenesisState(gdoc) - if err != nil { - osm.Exit(err.Error()) - } - - // Create proxyAppConn connection (consensus, mempool, query) - clientCreator := proxy.DefaultClientCreator( - config.LocalApp, - config.ProxyApp, - config.ABCI, - config.DBDir(), - ) - proxyApp := appconn.NewAppConns(clientCreator) - err = proxyApp.Start() - if err != nil { - osm.Exit(fmt.Sprintf("Error starting proxy app conns: %v", err)) - } - - evsw := events.NewEventSwitch() - if err := evsw.Start(); err != nil { - osm.Exit(fmt.Sprintf("Failed to start event bus: %v", err)) - } - - handshaker := NewHandshaker(stateDB, state, blockStore, gdoc) - handshaker.SetEventSwitch(evsw) - err = handshaker.Handshake(proxyApp) - if err != nil { - osm.Exit(fmt.Sprintf("Error on handshake: %v", err)) - } - - mempool := mock.Mempool{} - blockExec := sm.NewBlockExecutor(stateDB, log.NewNoopLogger(), proxyApp.Consensus(), mempool) - - consensusState := NewConsensusState(csConfig, state.Copy(), blockExec, - blockStore, mempool) - - consensusState.SetEventSwitch(evsw) - return consensusState -} diff --git a/tm2/pkg/bft/consensus/replay_test.go b/tm2/pkg/bft/consensus/replay_test.go index 6f138cd03e1..4ba091346f0 100644 --- a/tm2/pkg/bft/consensus/replay_test.go +++ b/tm2/pkg/bft/consensus/replay_test.go @@ -29,7 +29,6 @@ import ( sm "github.com/gnolang/gno/tm2/pkg/bft/state" "github.com/gnolang/gno/tm2/pkg/bft/types" walm "github.com/gnolang/gno/tm2/pkg/bft/wal" - "github.com/gnolang/gno/tm2/pkg/crypto" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/db/memdb" "github.com/gnolang/gno/tm2/pkg/events" @@ -39,11 +38,11 @@ import ( ) func TestMain(m *testing.M) { - config = ResetConfig("consensus_reactor_test") - consensusReplayConfig = ResetConfig("consensus_replay_test") - configStateTest := ResetConfig("consensus_state_test") - configMempoolTest := ResetConfig("consensus_mempool_test") - configByzantineTest := ResetConfig("consensus_byzantine_test") + config, _ = ResetConfig("consensus_reactor_test") + consensusReplayConfig, _ = ResetConfig("consensus_replay_test") + configStateTest, _ := ResetConfig("consensus_state_test") + configMempoolTest, _ := ResetConfig("consensus_mempool_test") + configByzantineTest, _ := ResetConfig("consensus_byzantine_test") code := m.Run() os.RemoveAll(config.RootDir) os.RemoveAll(consensusReplayConfig.RootDir) @@ -68,13 +67,18 @@ func TestMain(m *testing.M) { // and which ones we need the wal for - then we'd also be able to only flush the // wal writer when we need to, instead of with every message. -func startNewConsensusStateAndWaitForBlock(t *testing.T, consensusReplayConfig *cfg.Config, - lastBlockHeight int64, blockDB dbm.DB, stateDB dbm.DB, +func startNewConsensusStateAndWaitForBlock( + t *testing.T, + consensusReplayConfig *cfg.Config, + consensusReplayGenesisFile string, + lastBlockHeight int64, + blockDB dbm.DB, + stateDB dbm.DB, ) { t.Helper() logger := log.NewTestingLogger(t) - state, _ := sm.LoadStateFromDBOrGenesisFile(stateDB, consensusReplayConfig.GenesisFile()) + state, _ := sm.LoadStateFromDBOrGenesisFile(stateDB, consensusReplayGenesisFile) privValidator := loadPrivValidator(consensusReplayConfig) cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB) cs.SetLogger(logger) @@ -149,17 +153,27 @@ func TestWALCrash(t *testing.T) { for i, tc := range testCases { tc := tc - consensusReplayConfig := ResetConfig(fmt.Sprintf("%s_%d", t.Name(), i)) + consensusReplayConfig, genesisFile := ResetConfig(fmt.Sprintf("%s_%d", t.Name(), i)) t.Run(tc.name, func(t *testing.T) { t.Parallel() - crashWALandCheckLiveness(t, consensusReplayConfig, tc.initFn, tc.lastBlockHeight) + crashWALandCheckLiveness( + t, + consensusReplayConfig, + genesisFile, + tc.initFn, + tc.lastBlockHeight, + ) }) } } -func crashWALandCheckLiveness(t *testing.T, consensusReplayConfig *cfg.Config, - initFn func(dbm.DB, *ConsensusState, context.Context), lastBlockHeight int64, +func crashWALandCheckLiveness( + t *testing.T, + consensusReplayConfig *cfg.Config, + genesisFile string, + initFn func(dbm.DB, *ConsensusState, context.Context), + lastBlockHeight int64, ) { t.Helper() @@ -175,7 +189,7 @@ LOOP: logger := log.NewTestingLogger(t) blockDB := memdb.NewMemDB() stateDB := blockDB - state, _ := sm.MakeGenesisStateFromFile(consensusReplayConfig.GenesisFile()) + state, _ := sm.MakeGenesisStateFromFile(genesisFile) privValidator := loadPrivValidator(consensusReplayConfig) cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB) cs.SetLogger(logger) @@ -207,7 +221,7 @@ LOOP: t.Logf("WAL crashed: %v", err) // make sure we can make blocks after a crash - startNewConsensusStateAndWaitForBlock(t, consensusReplayConfig, cs.Height, blockDB, stateDB) + startNewConsensusStateAndWaitForBlock(t, consensusReplayConfig, genesisFile, cs.Height, blockDB, stateDB) // stop consensus state and transactions sender (initFn) cs.Stop() @@ -638,13 +652,18 @@ func tempWALWithData(data []byte) string { func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uint, sim *testSim) { t.Helper() - var chain []*types.Block - var commits []*types.Commit - var store *mockBlockStore - var stateDB dbm.DB - var genesisState sm.State + var ( + chain []*types.Block + commits []*types.Commit + store *mockBlockStore + stateDB dbm.DB + genesisState sm.State + + genesisFile string + ) + if sim != nil { - testConfig := ResetConfig(fmt.Sprintf("%s_%v_m", t.Name(), mode)) + testConfig, gf := ResetConfig(fmt.Sprintf("%s_%v_m", t.Name(), mode)) defer os.RemoveAll(testConfig.RootDir) stateDB = memdb.NewMemDB() defer stateDB.Close() @@ -653,16 +672,15 @@ func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uin chain = sim.Chain commits = sim.Commits store = newMockBlockStore(config, genesisState.ConsensusParams) + genesisFile = gf } else { // test single node - testConfig := ResetConfig(fmt.Sprintf("%s_%v_s", t.Name(), mode)) + testConfig, gf := ResetConfig(fmt.Sprintf("%s_%v_s", t.Name(), mode)) defer os.RemoveAll(testConfig.RootDir) walBody, err := WALWithNBlocks(t, numBlocks) require.NoError(t, err) walFile := tempWALWithData(walBody) config.Consensus.SetWalFile(walFile) - privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) - wal, err := walm.NewWAL(walFile, maxMsgSize) require.NoError(t, err) wal.SetLogger(log.NewTestingLogger(t)) @@ -672,8 +690,9 @@ func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uin chain, commits, err = makeBlockchainFromWAL(wal) require.NoError(t, err) - stateDB, genesisState, store = makeStateAndStore(config, privVal.GetPubKey(), kvstore.AppVersion) + stateDB, genesisState, store = makeStateAndStore(config, gf, kvstore.AppVersion) defer stateDB.Close() + genesisFile = gf } store.chain = chain store.commits = commits @@ -699,7 +718,7 @@ func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uin // now start the app using the handshake - it should sync evsw := events.NewEventSwitch() - genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile()) + genDoc, _ := sm.MakeGenesisDocFromFile(genesisFile) handshaker := NewHandshaker(stateDB, state, store, genDoc) handshaker.SetEventSwitch(evsw) proxyApp := appconn.NewAppConns(clientCreator2) @@ -833,12 +852,12 @@ func TestHandshakePanicsIfAppReturnsWrongAppHash(t *testing.T) { // - 0x01 // - 0x02 // - 0x03 - config := ResetConfig("handshake_test_") + config, genesisFile := ResetConfig("handshake_test_") defer os.RemoveAll(config.RootDir) privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) const appVersion = "v0.0.0-test" - stateDB, state, store := makeStateAndStore(config, privVal.GetPubKey(), appVersion) - genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile()) + stateDB, state, store := makeStateAndStore(config, genesisFile, appVersion) + genDoc, _ := sm.MakeGenesisDocFromFile(genesisFile) state.LastValidators = state.Validators.Copy() // mode = 0 for committing all the blocks blocks := makeBlocks(3, &state, privVal) @@ -1059,9 +1078,9 @@ func readPieceFromWAL(msg *walm.TimedWALMessage) interface{} { } // fresh state and mock store -func makeStateAndStore(config *cfg.Config, pubKey crypto.PubKey, appVersion string) (dbm.DB, sm.State, *mockBlockStore) { +func makeStateAndStore(config *cfg.Config, genesisFile string, appVersion string) (dbm.DB, sm.State, *mockBlockStore) { stateDB := memdb.NewMemDB() - state, _ := sm.MakeGenesisStateFromFile(config.GenesisFile()) + state, _ := sm.MakeGenesisStateFromFile(genesisFile) state.AppVersion = appVersion store := newMockBlockStore(config, state.ConsensusParams) sm.SaveState(stateDB, state) @@ -1115,15 +1134,14 @@ func TestHandshakeUpdatesValidators(t *testing.T) { app := &initChainApp{vals: vals.ABCIValidatorUpdates()} clientCreator := proxy.NewLocalClientCreator(app) - config := ResetConfig("handshake_test_") + config, genesisFile := ResetConfig("handshake_test_") defer os.RemoveAll(config.RootDir) - privVal := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) - stateDB, state, store := makeStateAndStore(config, privVal.GetPubKey(), "v0.0.0-test") + stateDB, state, store := makeStateAndStore(config, genesisFile, "v0.0.0-test") oldValAddr := state.Validators.Validators[0].Address // now start the app using the handshake - it should sync - genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile()) + genDoc, _ := sm.MakeGenesisDocFromFile(genesisFile) handshaker := NewHandshaker(stateDB, state, store, genDoc) proxyApp := appconn.NewAppConns(clientCreator) if err := proxyApp.Start(); err != nil { diff --git a/tm2/pkg/bft/consensus/types/height_vote_set_test.go b/tm2/pkg/bft/consensus/types/height_vote_set_test.go index 4c315585daa..60cac5831a6 100644 --- a/tm2/pkg/bft/consensus/types/height_vote_set_test.go +++ b/tm2/pkg/bft/consensus/types/height_vote_set_test.go @@ -14,7 +14,7 @@ import ( var config *cfg.Config // NOTE: must be reset for each _test.go file func TestMain(m *testing.M) { - config = cfg.ResetTestRoot("consensus_height_vote_set_test") + config, _ = cfg.ResetTestRoot("consensus_height_vote_set_test") code := m.Run() os.RemoveAll(config.RootDir) os.Exit(code) diff --git a/tm2/pkg/bft/consensus/wal_generator.go b/tm2/pkg/bft/consensus/wal_generator.go index 175771f50ed..fddcb4231e5 100644 --- a/tm2/pkg/bft/consensus/wal_generator.go +++ b/tm2/pkg/bft/consensus/wal_generator.go @@ -25,18 +25,19 @@ func makeAddrs() (string, string, string) { fmt.Sprintf("tcp://0.0.0.0:%d", start+2) } -// getConfig returns a config for test cases -func getConfig(t *testing.T) *cfg.Config { +// getConfig returns a config and genesis file for test cases +func getConfig(t *testing.T) (*cfg.Config, string) { t.Helper() - c := cfg.ResetTestRoot(t.Name()) + c, genesisFile := cfg.ResetTestRoot(t.Name()) // and we use random ports to run in parallel tm, rpc, grpc := makeAddrs() c.P2P.ListenAddress = tm c.RPC.ListenAddress = rpc c.RPC.GRPCListenAddress = grpc - return c + + return c, genesisFile } // heightStopWAL is a WAL which writes all msgs to underlying WALWriter. diff --git a/tm2/pkg/bft/consensus/wal_test.go b/tm2/pkg/bft/consensus/wal_test.go index 42651c0727d..b3ed662b968 100644 --- a/tm2/pkg/bft/consensus/wal_test.go +++ b/tm2/pkg/bft/consensus/wal_test.go @@ -111,7 +111,7 @@ func TestWALTruncate(t *testing.T) { func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) { t.Helper() - config := getConfig(t) + config, genesisFile := getConfig(t) app := kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), "wal_generator")) defer app.Close() @@ -126,7 +126,7 @@ func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) { privValidatorKeyFile := config.PrivValidatorKeyFile() privValidatorStateFile := config.PrivValidatorStateFile() privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile) - genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) + genDoc, err := types.GenesisDocFromFile(genesisFile) if err != nil { return errors.Wrap(err, "failed to read genesis file") } diff --git a/tm2/pkg/bft/node/node.go b/tm2/pkg/bft/node/node.go index 5fe47bb8893..9dd63464310 100644 --- a/tm2/pkg/bft/node/node.go +++ b/tm2/pkg/bft/node/node.go @@ -67,10 +67,10 @@ func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) { type GenesisDocProvider func() (*types.GenesisDoc, error) // DefaultGenesisDocProviderFunc returns a GenesisDocProvider that loads -// the GenesisDoc from the config.GenesisFile() on the filesystem. -func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider { +// the GenesisDoc from the genesis path on the filesystem. +func DefaultGenesisDocProviderFunc(genesisFile string) GenesisDocProvider { return func() (*types.GenesisDoc, error) { - return types.GenesisDocFromFile(config.GenesisFile()) + return types.GenesisDocFromFile(genesisFile) } } @@ -80,7 +80,7 @@ type NodeProvider func(*cfg.Config, *slog.Logger) (*Node, error) // DefaultNewNode returns a Tendermint node with default settings for the // PrivValidator, ClientCreator, GenesisDoc, and DBProvider. // It implements NodeProvider. -func DefaultNewNode(config *cfg.Config, logger *slog.Logger) (*Node, error) { +func DefaultNewNode(config *cfg.Config, genesisFile string, logger *slog.Logger) (*Node, error) { // Generate node PrivKey nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { @@ -103,7 +103,7 @@ func DefaultNewNode(config *cfg.Config, logger *slog.Logger) (*Node, error) { privval.LoadOrGenFilePV(newPrivValKey, newPrivValState), nodeKey, appClientCreator, - DefaultGenesisDocProviderFunc(config), + DefaultGenesisDocProviderFunc(genesisFile), DefaultDBProvider, logger, ) diff --git a/tm2/pkg/bft/node/node_test.go b/tm2/pkg/bft/node/node_test.go index c8e83b4963a..28ab95e0064 100644 --- a/tm2/pkg/bft/node/node_test.go +++ b/tm2/pkg/bft/node/node_test.go @@ -31,11 +31,11 @@ import ( ) func TestNodeStartStop(t *testing.T) { - config := cfg.ResetTestRoot("node_node_test") + config, genesisFile := cfg.ResetTestRoot("node_node_test") defer os.RemoveAll(config.RootDir) // create & start node - n, err := DefaultNewNode(config, log.NewNoopLogger()) + n, err := DefaultNewNode(config, genesisFile, log.NewNoopLogger()) require.NoError(t, err) err = n.Start() require.NoError(t, err) @@ -93,12 +93,12 @@ func TestSplitAndTrimEmpty(t *testing.T) { } func TestNodeDelayedStart(t *testing.T) { - config := cfg.ResetTestRoot("node_delayed_start_test") + config, genesisFile := cfg.ResetTestRoot("node_delayed_start_test") defer os.RemoveAll(config.RootDir) now := tmtime.Now() // create & start node - n, err := DefaultNewNode(config, log.NewTestingLogger(t)) + n, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) n.GenesisDoc().GenesisTime = now.Add(2 * time.Second) require.NoError(t, err) @@ -111,11 +111,11 @@ func TestNodeDelayedStart(t *testing.T) { } func TestNodeReady(t *testing.T) { - config := cfg.ResetTestRoot("node_node_test") + config, genesisFile := cfg.ResetTestRoot("node_node_test") defer os.RemoveAll(config.RootDir) // Create & start node - n, err := DefaultNewNode(config, log.NewTestingLogger(t)) + n, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) require.NoError(t, err) // Assert that blockstore has zero block before waiting for the first block @@ -144,11 +144,11 @@ func TestNodeReady(t *testing.T) { } func TestNodeSetAppVersion(t *testing.T) { - config := cfg.ResetTestRoot("node_app_version_test") + config, genesisFile := cfg.ResetTestRoot("node_app_version_test") defer os.RemoveAll(config.RootDir) // create & start node - n, err := DefaultNewNode(config, log.NewTestingLogger(t)) + n, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) require.NoError(t, err) // default config uses the kvstore app @@ -167,7 +167,7 @@ func TestNodeSetAppVersion(t *testing.T) { func TestNodeSetPrivValTCP(t *testing.T) { addr := "tcp://" + testFreeAddr(t) - config := cfg.ResetTestRoot("node_priv_val_tcp_test") + config, genesisFile := cfg.ResetTestRoot("node_priv_val_tcp_test") defer os.RemoveAll(config.RootDir) config.BaseConfig.PrivValidatorListenAddr = addr @@ -192,7 +192,7 @@ func TestNodeSetPrivValTCP(t *testing.T) { }() defer signerServer.Stop() - n, err := DefaultNewNode(config, log.NewTestingLogger(t)) + n, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) require.NoError(t, err) assert.IsType(t, &privval.SignerClient{}, n.PrivValidator()) } @@ -201,11 +201,11 @@ func TestNodeSetPrivValTCP(t *testing.T) { func TestPrivValidatorListenAddrNoProtocol(t *testing.T) { addrNoPrefix := testFreeAddr(t) - config := cfg.ResetTestRoot("node_priv_val_tcp_test") + config, genesisFile := cfg.ResetTestRoot("node_priv_val_tcp_test") defer os.RemoveAll(config.RootDir) config.BaseConfig.PrivValidatorListenAddr = addrNoPrefix - _, err := DefaultNewNode(config, log.NewTestingLogger(t)) + _, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) assert.Error(t, err) } @@ -213,7 +213,7 @@ func TestNodeSetPrivValIPC(t *testing.T) { tmpfile := "/tmp/kms." + random.RandStr(6) + ".sock" defer os.Remove(tmpfile) // clean up - config := cfg.ResetTestRoot("node_priv_val_tcp_test") + config, genesisFile := cfg.ResetTestRoot("node_priv_val_tcp_test") defer os.RemoveAll(config.RootDir) config.BaseConfig.PrivValidatorListenAddr = "unix://" + tmpfile @@ -236,7 +236,7 @@ func TestNodeSetPrivValIPC(t *testing.T) { }() defer pvsc.Stop() - n, err := DefaultNewNode(config, log.NewTestingLogger(t)) + n, err := DefaultNewNode(config, genesisFile, log.NewTestingLogger(t)) require.NoError(t, err) assert.IsType(t, &privval.SignerClient{}, n.PrivValidator()) } @@ -255,7 +255,7 @@ func testFreeAddr(t *testing.T) string { // create a proposal block using real and full // mempool pool and validate it. func TestCreateProposalBlock(t *testing.T) { - config := cfg.ResetTestRoot("node_create_proposal") + config, _ := cfg.ResetTestRoot("node_create_proposal") defer os.RemoveAll(config.RootDir) cc := proxy.NewLocalClientCreator(kvstore.NewKVStoreApplication()) proxyApp := appconn.NewAppConns(cc) @@ -309,7 +309,7 @@ func TestCreateProposalBlock(t *testing.T) { } func TestNodeNewNodeCustomReactors(t *testing.T) { - config := cfg.ResetTestRoot("node_new_node_custom_reactors_test") + config, genesisFile := cfg.ResetTestRoot("node_new_node_custom_reactors_test") defer os.RemoveAll(config.RootDir) cr := p2pmock.NewReactor() @@ -322,7 +322,7 @@ func TestNodeNewNodeCustomReactors(t *testing.T) { privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()), nodeKey, proxy.DefaultClientCreator(nil, config.ProxyApp, config.ABCI, config.DBDir()), - DefaultGenesisDocProviderFunc(config), + DefaultGenesisDocProviderFunc(genesisFile), DefaultDBProvider, log.NewTestingLogger(t), CustomReactors(map[string]p2p.Reactor{"FOO": cr, "BLOCKCHAIN": customBlockchainReactor}), diff --git a/tm2/pkg/bft/rpc/client/examples_test.go b/tm2/pkg/bft/rpc/client/examples_test.go index bad20cc9b4f..287a63164d2 100644 --- a/tm2/pkg/bft/rpc/client/examples_test.go +++ b/tm2/pkg/bft/rpc/client/examples_test.go @@ -17,7 +17,8 @@ func ExampleHTTP_simple() { defer rpctest.StopTendermint(node) // Create our RPC client - rpcAddr := rpctest.GetConfig().RPC.ListenAddress + cfg, _ := rpctest.GetConfig() + rpcAddr := cfg.RPC.ListenAddress c := client.NewHTTP(rpcAddr, "/websocket") // Create a transaction @@ -63,11 +64,12 @@ func ExampleHTTP_simple() { func ExampleHTTP_batching() { // Start a tendermint node (and kvstore) in the background to test against app := kvstore.NewKVStoreApplication() - node := rpctest.StartTendermint(app, rpctest.SuppressStdout, rpctest.RecreateConfig) + node := rpctest.StartTendermint(app, rpctest.RecreateConfig) defer rpctest.StopTendermint(node) // Create our RPC client - rpcAddr := rpctest.GetConfig().RPC.ListenAddress + cfg, _ := rpctest.GetConfig() + rpcAddr := cfg.RPC.ListenAddress c := client.NewHTTP(rpcAddr, "/websocket") // Create our two transactions diff --git a/tm2/pkg/bft/rpc/client/rpc_test.go b/tm2/pkg/bft/rpc/client/rpc_test.go index 3c93829dcc7..49640a394fb 100644 --- a/tm2/pkg/bft/rpc/client/rpc_test.go +++ b/tm2/pkg/bft/rpc/client/rpc_test.go @@ -17,7 +17,8 @@ import ( ) func getHTTPClient() *client.HTTP { - rpcAddr := rpctest.GetConfig().RPC.ListenAddress + cfg, _ := rpctest.GetConfig() + rpcAddr := cfg.RPC.ListenAddress return client.NewHTTP(rpcAddr, "/websocket") } @@ -47,7 +48,8 @@ func TestNilCustomHTTPClient(t *testing.T) { func TestCustomHTTPClient(t *testing.T) { t.Parallel() - remote := rpctest.GetConfig().RPC.ListenAddress + cfg, _ := rpctest.GetConfig() + remote := cfg.RPC.ListenAddress c := client.NewHTTPWithClient(remote, "/websocket", http.DefaultClient) status, err := c.Status() require.NoError(t, err) @@ -57,8 +59,9 @@ func TestCustomHTTPClient(t *testing.T) { func TestCorsEnabled(t *testing.T) { t.Parallel() - origin := rpctest.GetConfig().RPC.CORSAllowedOrigins[0] - remote := strings.Replace(rpctest.GetConfig().RPC.ListenAddress, "tcp", "http", -1) + cfg, _ := rpctest.GetConfig() + origin := cfg.RPC.CORSAllowedOrigins[0] + remote := strings.Replace(cfg.RPC.ListenAddress, "tcp", "http", -1) req, err := http.NewRequest("GET", remote, nil) require.Nil(t, err, "%+v", err) @@ -76,7 +79,8 @@ func TestStatus(t *testing.T) { t.Parallel() for i, c := range GetClients() { - moniker := rpctest.GetConfig().Moniker + cfg, _ := rpctest.GetConfig() + moniker := cfg.Moniker status, err := c.Status() require.Nil(t, err, "%d: %+v", i, err) assert.Equal(t, moniker, status.NodeInfo.Moniker) diff --git a/tm2/pkg/bft/rpc/test/helpers.go b/tm2/pkg/bft/rpc/test/helpers.go index 0b6d39c6839..39078eaaf7f 100644 --- a/tm2/pkg/bft/rpc/test/helpers.go +++ b/tm2/pkg/bft/rpc/test/helpers.go @@ -23,17 +23,24 @@ import ( type Options struct { suppressStdout bool recreateConfig bool + genesisPath string } var ( - globalConfig *cfg.Config + // This entire testing insanity is removed in: + // https://github.com/gnolang/gno/pull/1498 + globalConfig *cfg.Config + globalGenesis string + defaultOptions = Options{ recreateConfig: false, + genesisPath: "genesis.json", } ) func waitForRPC() { - laddr := GetConfig().RPC.ListenAddress + cfg, _ := GetConfig() + laddr := cfg.RPC.ListenAddress client := rpcclient.NewJSONRPCClient(laddr) result := new(ctypes.ResultStatus) for { @@ -59,24 +66,24 @@ func makePathname() string { return strings.Replace(p, sep, "_", -1) } -func createConfig() *cfg.Config { +func createConfig() (*cfg.Config, string) { pathname := makePathname() - c := cfg.ResetTestRoot(pathname) + c, genesisFile := cfg.ResetTestRoot(pathname) // and we use random ports to run in parallel c.P2P.ListenAddress = "tcp://127.0.0.1:0" c.RPC.ListenAddress = "tcp://127.0.0.1:0" c.RPC.CORSAllowedOrigins = []string{"https://tendermint.com/"} // c.TxIndex.IndexTags = "app.creator,tx.height" // see kvstore application - return c + return c, genesisFile } // GetConfig returns a config for the test cases as a singleton -func GetConfig(forceCreate ...bool) *cfg.Config { - if globalConfig == nil || (len(forceCreate) > 0 && forceCreate[0]) { - globalConfig = createConfig() +func GetConfig(forceCreate ...bool) (*cfg.Config, string) { + if globalConfig == nil || globalGenesis == "" || (len(forceCreate) > 0 && forceCreate[0]) { + globalConfig, globalGenesis = createConfig() } - return globalConfig + return globalConfig, globalGenesis } // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized @@ -94,10 +101,6 @@ func StartTendermint(app abci.Application, opts ...func(*Options)) *nm.Node { // wait for rpc waitForRPC() - if !nodeOpts.suppressStdout { - fmt.Println("Tendermint running!") - } - return node } @@ -112,7 +115,7 @@ func StopTendermint(node *nm.Node) { // NewTendermint creates a new tendermint server and sleeps forever func NewTendermint(app abci.Application, opts *Options) *nm.Node { // Create & start node - config := GetConfig(opts.recreateConfig) + config, genesisFile := GetConfig(opts.recreateConfig) pvKeyFile := config.PrivValidatorKeyFile() pvKeyStateFile := config.PrivValidatorStateFile() @@ -123,7 +126,7 @@ func NewTendermint(app abci.Application, opts *Options) *nm.Node { panic(err) } node, err := nm.NewNode(config, pv, nodeKey, papp, - nm.DefaultGenesisDocProviderFunc(config), + nm.DefaultGenesisDocProviderFunc(genesisFile), nm.DefaultDBProvider, log.NewNoopLogger()) if err != nil { diff --git a/tm2/pkg/bft/state/state_test.go b/tm2/pkg/bft/state/state_test.go index b7627cbb59f..15c87a41589 100644 --- a/tm2/pkg/bft/state/state_test.go +++ b/tm2/pkg/bft/state/state_test.go @@ -26,12 +26,12 @@ import ( func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) { t.Helper() - config := cfg.ResetTestRoot("state_") + config, genesisFile := cfg.ResetTestRoot("state_") dbType := dbm.BackendType(config.DBBackend) stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) require.NoError(t, err) - state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile()) + state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, genesisFile) assert.NoError(t, err, "expected no error on LoadStateFromDBOrGenesisFile") tearDown := func(t *testing.T) { diff --git a/tm2/pkg/bft/state/store_test.go b/tm2/pkg/bft/state/store_test.go index add5d7e34d2..e2e86b76c16 100644 --- a/tm2/pkg/bft/state/store_test.go +++ b/tm2/pkg/bft/state/store_test.go @@ -60,13 +60,13 @@ func TestStoreLoadValidators(t *testing.T) { func BenchmarkLoadValidators(b *testing.B) { const valSetSize = 100 - config := cfg.ResetTestRoot("state_") + config, genesisFile := cfg.ResetTestRoot("state_") defer os.RemoveAll(config.RootDir) dbType := dbm.BackendType(config.DBBackend) stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) require.NoError(b, err) - state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile()) + state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, genesisFile) require.NoError(b, err) state.Validators = genValSet(valSetSize) state.NextValidators = state.Validators.CopyIncrementProposerPriority(1) diff --git a/tm2/pkg/bft/store/store_test.go b/tm2/pkg/bft/store/store_test.go index e20db43c2d6..6ea85592ac9 100644 --- a/tm2/pkg/bft/store/store_test.go +++ b/tm2/pkg/bft/store/store_test.go @@ -46,12 +46,12 @@ func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Bl } func makeStateAndBlockStore(logger *slog.Logger) (sm.State, *BlockStore, cleanupFunc) { - config := cfg.ResetTestRoot("blockchain_reactor_test") + config, genesisFile := cfg.ResetTestRoot("blockchain_reactor_test") // blockDB := dbm.NewDebugDB("blockDB", memdb.NewMemDB()) // stateDB := dbm.NewDebugDB("stateDB", memdb.NewMemDB()) blockDB := memdb.NewMemDB() stateDB := memdb.NewMemDB() - state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile()) + state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, genesisFile) if err != nil { panic(errors.Wrap(err, "error constructing state from genesis file")) }