From 0fde5b83dfba30ed2eeaf23b08573be69ca90f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20=C5=BDivkovi=C4=87?= Date: Thu, 25 Apr 2024 11:38:31 +0200 Subject: [PATCH] feat: standardize node directory structure (#1944) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Closes #1882 This PR standardizes the node's directory structure, and does not assume that the `genesis.json` is contained within the node directory (further tackled in #1883). The biggest change introduced in this PR is that the node's directory path is treated as absolute, so there is no funny business with path concatenation when the node is running. This is best described with the desired structure: ``` . ├── genesis.json └── my-node-dir/ ├── db/ │ ├── blockstore.db (folder) │ ├── gnolang.db (folder) │ ├── state.db (folder) │ └── cs.wal ├── secrets/ │ ├── priv_validator_state.json │ ├── node_key.json │ └── priv_validator_key.json └── config/ └── config.toml ``` **BREAKING CHANGES**: - the `genesis.json` file is no longer contained inside the node's working directory, but outside it by default (this is dropped entirely in #1883) - the configuration is moved top-level to the node's directroy - the secrets are no longer contained in `/config`, but in `/secrets` - the `priv_validator_state.json` is moved to `/secrets` by default If you have an old Gno chain, and what to keep backwards compatibility, move the `genesis.json` to just outside the node directory (to prevent it from being regenerated), and update the relevant `config.toml` path values (`genesis_file` and `home` for the `BaseConfig`). Additionally, update all `home` paths in the `config.toml` to be absolute paths (this is dropped in #1884).
Contributors' checklist... - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
--- .dockerignore | 34 ++++--- .gitignore | 23 +++-- contribs/gnokeykc/go.mod | 1 + .../local-setup/premining-balances.md | 2 +- .../local-setup/setting-up-a-local-chain.md | 4 +- docs/gno-tooling/cli/gnoland.md | 4 +- gno.land/Makefile | 2 +- gno.land/cmd/gnoland/start.go | 29 +++--- gno.land/pkg/gnoland/app.go | 3 +- gno.land/pkg/gnoland/node_inmemory.go | 28 ------ gnovm/pkg/doc/dirs_test.go | 2 +- misc/deployments/staging.gno.land/Makefile | 2 +- .../staging.gno.land/docker-compose.yml | 2 +- misc/docker-compose/docker-compose.yml | 2 +- misc/loop/cmd/snapshotter.go | 2 +- misc/loop/scripts/start.sh | 10 +-- tm2/pkg/bft/blockchain/reactor_test.go | 4 +- tm2/pkg/bft/config/config.go | 61 +++++++------ tm2/pkg/bft/config/config_test.go | 7 +- tm2/pkg/bft/config/toml.go | 26 ++++-- tm2/pkg/bft/config/toml_test.go | 35 ++++---- tm2/pkg/bft/config/utils.go | 11 --- tm2/pkg/bft/consensus/common_test.go | 8 +- tm2/pkg/bft/consensus/config/config.go | 7 +- tm2/pkg/bft/consensus/config/utils.go | 11 --- tm2/pkg/bft/consensus/mempool_test.go | 6 +- tm2/pkg/bft/consensus/replay_file.go | 78 ---------------- tm2/pkg/bft/consensus/replay_test.go | 88 +++++++++++-------- .../consensus/types/height_vote_set_test.go | 2 +- tm2/pkg/bft/consensus/wal_generator.go | 9 +- tm2/pkg/bft/consensus/wal_test.go | 4 +- tm2/pkg/bft/node/node.go | 10 +-- tm2/pkg/bft/node/node_test.go | 34 +++---- tm2/pkg/bft/rpc/client/examples_test.go | 8 +- tm2/pkg/bft/rpc/client/rpc_test.go | 14 +-- tm2/pkg/bft/rpc/test/helpers.go | 33 +++---- tm2/pkg/bft/state/state_test.go | 4 +- tm2/pkg/bft/state/store_test.go | 4 +- tm2/pkg/bft/store/store_test.go | 4 +- tm2/pkg/crypto/keys/utils.go | 4 +- 40 files changed, 285 insertions(+), 337 deletions(-) delete mode 100644 tm2/pkg/bft/config/utils.go delete mode 100644 tm2/pkg/bft/consensus/config/utils.go diff --git a/.dockerignore b/.dockerignore index caecb619cd6..a45b7bafa98 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,22 +5,36 @@ misc/ docker-compose.yml tests/docker-integration/ -# gitignore -*.sw[pon] -cmd/foo.go -unsorted.* +# Copied from .gitignore + +# Editor Leftovers .DS_Store +.vscode +.idea + +# Default node data +gnoland-data +genesis.json + +# Build leftovers +build + +# Legacy .gitignore data/* -proto/* -testdir/ -pkgs/sdk/vm/_testdata -build/* +testdir + +# Log + generation leftovers *.tx *.log.* *.log *.gno.gen.go *.gno.gen_test.go -.vscode -.idea *.pb.go pbbindings.go +*~ +.#* +*# + +# Test coverage leftovers +cover.out +coverage.out diff --git a/.gitignore b/.gitignore index d7e6d7eb9a4..0a06c2cf055 100644 --- a/.gitignore +++ b/.gitignore @@ -1,22 +1,31 @@ -*.sw[pon] -cmd/foo.go -unsorted.* +# Editor Leftovers .DS_Store +.vscode +.idea + +# Default node data +gnoland-data +genesis.json + +# Build leftovers +build + +# Legacy .gitignore data/* testdir -pkgs/sdk/vm/_testdata -build + +# Log + generation leftovers *.tx *.log.* *.log *.gno.gen.go *.gno.gen_test.go -.vscode -.idea *.pb.go pbbindings.go *~ .#* *# + +# Test coverage leftovers cover.out coverage.out diff --git a/contribs/gnokeykc/go.mod b/contribs/gnokeykc/go.mod index c0b4a874576..06e4db7a355 100644 --- a/contribs/gnokeykc/go.mod +++ b/contribs/gnokeykc/go.mod @@ -10,6 +10,7 @@ require ( ) require ( + dario.cat/mergo v1.0.0 // indirect github.com/alessio/shellescape v1.4.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect github.com/btcsuite/btcd/btcutil v1.1.5 // indirect diff --git a/docs/getting-started/local-setup/premining-balances.md b/docs/getting-started/local-setup/premining-balances.md index e7c0d6606ec..7117f8d5cdd 100644 --- a/docs/getting-started/local-setup/premining-balances.md +++ b/docs/getting-started/local-setup/premining-balances.md @@ -24,7 +24,7 @@ In order for us to premine funds on a fresh chain, we need to make sure we do no from previous chain runs. The blockchain node, when it runs, works with an embedded DB locally on disk to store execution data (such as -configuration files, or the state DB). For Gno blockchain nodes, this working directory is labeled as `testdir` by +configuration files, or the state DB). For Gno blockchain nodes, this working directory is labeled as `gnoland-data` by default. To clean out old blockchain data, navigate to the `gno.land` folder and run the appropriate make command: diff --git a/docs/getting-started/local-setup/setting-up-a-local-chain.md b/docs/getting-started/local-setup/setting-up-a-local-chain.md index e999071a589..23b4e110820 100644 --- a/docs/getting-started/local-setup/setting-up-a-local-chain.md +++ b/docs/getting-started/local-setup/setting-up-a-local-chain.md @@ -42,11 +42,11 @@ Let's break down the most important default settings: - `genesis-balances-file` - the initial premine balances file, which contains initial native currency allocations for the chain. By default, the genesis balances file is located in `gno.land/genesis/genesis_balances.txt`, this is also the reason why we need to navigate to the `gno.land` sub-folder to run the command with default settings -- `root-dir` - the working directory for the node configuration and node data (state DB) +- `data-dir` - the working directory for the node configuration and node data (state DB) :::info Resetting the chain -As mentioned, the working directory for the node is located in `root-dir`. To reset the chain, you need +As mentioned, the working directory for the node is located in `data-dir`. To reset the chain, you need to delete this directory and start the node up again. If you are using the default node configuration, you can run `make fclean` from the `gno.land` sub-folder to delete the `tempdir` working directory. diff --git a/docs/gno-tooling/cli/gnoland.md b/docs/gno-tooling/cli/gnoland.md index 9dbb22cd9c5..9bd722b0f2c 100644 --- a/docs/gno-tooling/cli/gnoland.md +++ b/docs/gno-tooling/cli/gnoland.md @@ -21,11 +21,11 @@ gnoland ### **Options** | Name | Type | Description | -| -------------------------- | ------- | --------------------------------------------------------------------------------------- | +|----------------------------| ------- | --------------------------------------------------------------------------------------- | | `chainid` | String | The id of the chain (default: `dev`). | | `genesis-balances-file` | String | The initial GNOT distribution file (default: `./gnoland/genesis/genesis_balances.txt`). | | `genesis-remote` | String | Replacement '%%REMOTE%%' in genesis (default: `"localhost:26657"`). | | `genesis-txs-file` | String | Initial txs to be executed (default: `"./gnoland/genesis/genesis_txs.jsonl"`). | -| `root-dir` | String | directory for config and data (default: `testdir`). | +| `data-dir` | String | directory for config and data (default: `gnoland-data`). | | `skip-failing-genesis-txs` | Boolean | Skips transactions that fail from the `genesis-txs-file` | | `skip-start` | Boolean | Quits after initialization without starting the node. | diff --git a/gno.land/Makefile b/gno.land/Makefile index e82a90efb2f..88fe3b396c0 100644 --- a/gno.land/Makefile +++ b/gno.land/Makefile @@ -49,7 +49,7 @@ install.gnokey:; go install ./cmd/gnokey .PHONY: fclean fclean: clean - rm -rf testdir + rm -rf gnoland-data genesis.json .PHONY: clean clean: diff --git a/gno.land/cmd/gnoland/start.go b/gno.land/cmd/gnoland/start.go index 4eaf9618648..41f4e7c2f44 100644 --- a/gno.land/cmd/gnoland/start.go +++ b/gno.land/cmd/gnoland/start.go @@ -120,12 +120,11 @@ func (c *startCfg) RegisterFlags(fs *flag.FlagSet) { "the root directory of the gno repository", ) - // XXX: Use home directory for this fs.StringVar( &c.dataDir, "data-dir", - "testdir", - "directory for config and data", + "gnoland-data", + "the path to the node's data directory", ) fs.StringVar( @@ -192,18 +191,14 @@ func (c *startCfg) RegisterFlags(fs *flag.FlagSet) { log.ConsoleFormat.String(), "log format for the gnoland node", ) - - // XXX(deprecated): use data-dir instead - fs.StringVar( - &c.dataDir, - "root-dir", - "testdir", - "deprecated: use data-dir instead - directory for config and data", - ) } func execStart(c *startCfg, io commands.IO) error { - dataDir := c.dataDir + // 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) + } var ( cfg *config.Config @@ -223,7 +218,7 @@ func execStart(c *startCfg, io commands.IO) error { cfg, loadCfgErr = config.LoadConfigFile(c.nodeConfigPath) } else { // Load the default node configuration - cfg, loadCfgErr = config.LoadOrMakeConfigWithOptions(dataDir) + cfg, loadCfgErr = config.LoadOrMakeConfigWithOptions(nodeDir) } if loadCfgErr != nil { @@ -246,7 +241,9 @@ func execStart(c *startCfg, io commands.IO) error { logger := log.ZapLoggerToSlog(zapLogger) // Write genesis file if missing. - genesisFilePath := filepath.Join(dataDir, cfg.Genesis) + // 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) { // Create priv validator first. @@ -270,7 +267,7 @@ func execStart(c *startCfg, io commands.IO) error { cfg.TxEventStore = txEventStoreCfg // Create application and node. - gnoApp, err := gnoland.NewApp(dataDir, c.skipFailingGenesisTxs, logger, c.genesisMaxVMCycles) + gnoApp, err := gnoland.NewApp(nodeDir, c.skipFailingGenesisTxs, logger, c.genesisMaxVMCycles) if err != nil { return fmt.Errorf("error in creating new app: %w", err) } @@ -280,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/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index d1978e76563..ee007c058a1 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -8,6 +8,7 @@ import ( "github.com/gnolang/gno/gno.land/pkg/sdk/vm" "github.com/gnolang/gno/gnovm/pkg/gnoenv" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" + "github.com/gnolang/gno/tm2/pkg/bft/config" dbm "github.com/gnolang/gno/tm2/pkg/db" "github.com/gnolang/gno/tm2/pkg/log" "github.com/gnolang/gno/tm2/pkg/sdk" @@ -132,7 +133,7 @@ func NewApp(dataRootDir string, skipFailingGenesisTxs bool, logger *slog.Logger, } // Get main DB. - cfg.DB, err = dbm.NewDB("gnolang", dbm.GoLevelDBBackend, filepath.Join(dataRootDir, "data")) + cfg.DB, err = dbm.NewDB("gnolang", dbm.GoLevelDBBackend, filepath.Join(dataRootDir, config.DefaultDBDir)) if err != nil { return nil, fmt.Errorf("error initializing database %q using path %q: %w", dbm.GoLevelDBBackend, dataRootDir, err) } diff --git a/gno.land/pkg/gnoland/node_inmemory.go b/gno.land/pkg/gnoland/node_inmemory.go index d48d0e2ab47..05ed59ee491 100644 --- a/gno.land/pkg/gnoland/node_inmemory.go +++ b/gno.land/pkg/gnoland/node_inmemory.go @@ -59,34 +59,6 @@ func NewDefaultTMConfig(rootdir string) *tmcfg.Config { return tmcfg.TestConfig().SetRootDir(rootdir) } -// NewInMemoryNodeConfig creates a default configuration for an in-memory node. -func NewDefaultInMemoryNodeConfig(rootdir string) *InMemoryNodeConfig { - tm := NewDefaultTMConfig(rootdir) - - // Create Mocked Identity - pv := NewMockedPrivValidator() - genesis := NewDefaultGenesisConfig(pv.GetPubKey(), tm.ChainID()) - - // Add self as validator - self := pv.GetPubKey() - genesis.Validators = []bft.GenesisValidator{ - { - Address: self.Address(), - PubKey: self, - Power: 10, - Name: "self", - }, - } - - return &InMemoryNodeConfig{ - PrivValidator: pv, - TMConfig: tm, - Genesis: genesis, - GenesisTxHandler: PanicOnFailingTxHandler, - GenesisMaxVMCycles: 10_000_000, - } -} - func (cfg *InMemoryNodeConfig) validate() error { if cfg.PrivValidator == nil { return fmt.Errorf("`PrivValidator` is required but not provided") diff --git a/gnovm/pkg/doc/dirs_test.go b/gnovm/pkg/doc/dirs_test.go index 41f3409a08b..8659f3cbfcb 100644 --- a/gnovm/pkg/doc/dirs_test.go +++ b/gnovm/pkg/doc/dirs_test.go @@ -100,7 +100,7 @@ func TestDirs_findPackage(t *testing.T) { }}, {"alpha", []bfsDir{ {importPath: "dirs.mod/dep/alpha", dir: filepath.Join(td, "dirsdep/pkg/mod/dirs.mod/dep/alpha")}, - // no testdir/module/alpha as it is inside a module + // no gnoland-data/module/alpha as it is inside a module }}, {"math", []bfsDir{ {importPath: "math", dir: filepath.Join(td, "dirs/math")}, diff --git a/misc/deployments/staging.gno.land/Makefile b/misc/deployments/staging.gno.land/Makefile index 516be6dfc0e..6ad76a0480a 100644 --- a/misc/deployments/staging.gno.land/Makefile +++ b/misc/deployments/staging.gno.land/Makefile @@ -11,7 +11,7 @@ logs: down: docker compose down docker volume rm -f staginggnoland_gnonode - docker compose run gnoland rm -rf /opt/gno/src/gno.land/testdir/data /opt/gno/src/gno.land/testdir/config + docker compose run gnoland rm -rf /opt/gno/src/gno.land/gnoland-data/data /opt/gno/src/gno.land/gnoland-data/config pull: git pull diff --git a/misc/deployments/staging.gno.land/docker-compose.yml b/misc/deployments/staging.gno.land/docker-compose.yml index bf825b54536..991a102b691 100644 --- a/misc/deployments/staging.gno.land/docker-compose.yml +++ b/misc/deployments/staging.gno.land/docker-compose.yml @@ -17,7 +17,7 @@ services: - --chainid=staging - --genesis-remote=staging.gno.land:36657 volumes: - - "./data/gnoland:/opt/gno/src/gno.land/testdir" + - "./data/gnoland:/opt/gno/src/gno.land/gnoland-data" ports: - 36656:36656 - 36657:36657 diff --git a/misc/docker-compose/docker-compose.yml b/misc/docker-compose/docker-compose.yml index 26ef50913ff..470aeaf3127 100644 --- a/misc/docker-compose/docker-compose.yml +++ b/misc/docker-compose/docker-compose.yml @@ -9,7 +9,7 @@ services: - LOG_LEVEL=4 command: [ "gnoland", "start" ] volumes: - - "gnonode:/opt/gno/src/testdir" + - "gnonode:/opt/gno/src/gnoland-data" networks: - gnonode restart: on-failure diff --git a/misc/loop/cmd/snapshotter.go b/misc/loop/cmd/snapshotter.go index 2de28906e7e..b501a3116f1 100644 --- a/misc/loop/cmd/snapshotter.go +++ b/misc/loop/cmd/snapshotter.go @@ -162,7 +162,7 @@ func (s snapshotter) startPortalLoopContainer(ctx context.Context) (*types.Conta Binds: []string{ fmt.Sprintf("%s/scripts:/scripts", s.cfg.hostPWD), fmt.Sprintf("%s/backups:/backups", s.cfg.hostPWD), - fmt.Sprintf("%s:/opt/gno/src/testdir", s.containerName), + fmt.Sprintf("%s:/opt/gno/src/gnoland-data", s.containerName), }, }, nil, nil, s.containerName) if err != nil { diff --git a/misc/loop/scripts/start.sh b/misc/loop/scripts/start.sh index d50b3175932..1b52f6f03f5 100755 --- a/misc/loop/scripts/start.sh +++ b/misc/loop/scripts/start.sh @@ -19,11 +19,11 @@ gnoland start \ --skip-start=true \ --skip-failing-genesis-txs -sed -i "s#^moniker = \".*\"#moniker = \"${MONIKER}\"#" ./testdir/config/config.toml -sed -i "s#laddr = \".*:26656\"#laddr = \"${P2P_LADDR}\"#" ./testdir/config/config.toml -sed -i "s#laddr = \".*:26657\"#laddr = \"${RPC_LADDR}\"#" ./testdir/config/config.toml +sed -i "s#^moniker = \".*\"#moniker = \"${MONIKER}\"#" ./gnoland-data/config/config.toml +sed -i "s#laddr = \".*:26656\"#laddr = \"${P2P_LADDR}\"#" ./gnoland-data/config/config.toml +sed -i "s#laddr = \".*:26657\"#laddr = \"${RPC_LADDR}\"#" ./gnoland-data/config/config.toml -sed -i "s#seeds = \".*\"#seeds = \"${SEEDS}\"#" ./testdir/config/config.toml -sed -i "s#persistent_peers = \".*\"#persistent_peers = \"${PERSISTENT_PEERS}\"#" ./testdir/config/config.toml +sed -i "s#seeds = \".*\"#seeds = \"${SEEDS}\"#" ./gnoland-data/config/config.toml +sed -i "s#persistent_peers = \".*\"#persistent_peers = \"${PERSISTENT_PEERS}\"#" ./gnoland-data/config/config.toml exec gnoland start --skip-failing-genesis-txs 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 85db5ecc3ae..8668dde5003 100644 --- a/tm2/pkg/bft/config/config.go +++ b/tm2/pkg/bft/config/config.go @@ -77,7 +77,7 @@ func LoadOrMakeConfigWithOptions(root string, opts ...Option) (*Config, error) { // Initialize the config as default var ( cfg = DefaultConfig() - configPath = join(root, defaultConfigFilePath) + configPath = filepath.Join(root, defaultConfigPath) ) // Config doesn't exist, create it @@ -165,8 +165,12 @@ func (cfg *Config) EnsureDirs() error { return fmt.Errorf("no config directory, %w", err) } - if err := osm.EnsureDir(filepath.Join(rootDir, defaultDataDir), DefaultDirPerm); err != nil { - return fmt.Errorf("no data directory, %w", err) + if err := osm.EnsureDir(filepath.Join(rootDir, defaultSecretsDir), DefaultDirPerm); err != nil { + return fmt.Errorf("no secrets directory, %w", err) + } + + if err := osm.EnsureDir(filepath.Join(rootDir, DefaultDBDir), DefaultDirPerm); err != nil { + return fmt.Errorf("no DB directory, %w", err) } return nil @@ -194,11 +198,11 @@ func (cfg *Config) ValidateBasic() error { } // ----------------------------------------------------------------------------- -// BaseConfig var ( - defaultConfigDir = "config" - defaultDataDir = "data" + DefaultDBDir = "db" + defaultConfigDir = "config" + defaultSecretsDir = "secrets" defaultConfigFileName = "config.toml" defaultGenesisJSONName = "genesis.json" @@ -206,20 +210,32 @@ var ( defaultPrivValKeyName = "priv_validator_key.json" defaultPrivValStateName = "priv_validator_state.json" - defaultConfigFilePath = filepath.Join(defaultConfigDir, defaultConfigFileName) - defaultGenesisJSONPath = filepath.Join(defaultConfigDir, defaultGenesisJSONName) - defaultPrivValKeyPath = filepath.Join(defaultConfigDir, defaultPrivValKeyName) - defaultPrivValStatePath = filepath.Join(defaultDataDir, defaultPrivValStateName) - defaultNodeKeyPath = filepath.Join(defaultConfigDir, defaultNodeKeyName) + defaultConfigPath = filepath.Join(defaultConfigDir, defaultConfigFileName) + defaultPrivValKeyPath = filepath.Join(defaultSecretsDir, defaultPrivValKeyName) + defaultPrivValStatePath = filepath.Join(defaultSecretsDir, defaultPrivValStateName) + defaultNodeKeyPath = filepath.Join(defaultSecretsDir, defaultNodeKeyName) ) -// BaseConfig defines the base configuration for a Tendermint node +// BaseConfig defines the base configuration for a Tendermint node. type BaseConfig struct { // chainID is unexposed and immutable but here for convenience chainID string // The root directory for all data. - // This should be set in viper so it can unmarshal into this struct + // The node directory contains: + // + // ┌── db/ + // │ ├── blockstore.db (folder) + // │ ├── gnolang.db (folder) + // │ └── state.db (folder) + // ├── wal/ + // │ └── cs.wal (folder) + // ├── secrets/ + // │ ├── priv_validator_state.json + // │ ├── node_key.json + // │ └── priv_validator_key.json + // └── config/ + // └── config.toml (optional) RootDir string `toml:"home"` // TCP or UNIX socket address of the ABCI application, @@ -285,7 +301,7 @@ type BaseConfig struct { // DefaultBaseConfig returns a default base configuration for a Tendermint node func DefaultBaseConfig() BaseConfig { return BaseConfig{ - Genesis: defaultGenesisJSONPath, + Genesis: defaultGenesisJSONName, PrivValidatorKey: defaultPrivValKeyPath, PrivValidatorState: defaultPrivValStatePath, NodeKey: defaultNodeKeyPath, @@ -296,7 +312,7 @@ func DefaultBaseConfig() BaseConfig { FastSyncMode: true, FilterPeers: false, DBBackend: db.GoLevelDBBackend.String(), - DBPath: "data", + DBPath: DefaultDBDir, } } @@ -314,29 +330,24 @@ func (cfg BaseConfig) ChainID() string { return cfg.chainID } -// GenesisFile returns the full path to the genesis.json file -func (cfg BaseConfig) GenesisFile() string { - return join(cfg.RootDir, cfg.Genesis) -} - // PrivValidatorKeyFile returns the full path to the priv_validator_key.json file func (cfg BaseConfig) PrivValidatorKeyFile() string { - return join(cfg.RootDir, cfg.PrivValidatorKey) + return filepath.Join(cfg.RootDir, cfg.PrivValidatorKey) } -// PrivValidatorFile returns the full path to the priv_validator_state.json file +// PrivValidatorStateFile returns the full path to the priv_validator_state.json file func (cfg BaseConfig) PrivValidatorStateFile() string { - return join(cfg.RootDir, cfg.PrivValidatorState) + return filepath.Join(cfg.RootDir, cfg.PrivValidatorState) } // NodeKeyFile returns the full path to the node_key.json file func (cfg BaseConfig) NodeKeyFile() string { - return join(cfg.RootDir, cfg.NodeKey) + return filepath.Join(cfg.RootDir, cfg.NodeKey) } // DBDir returns the full path to the database directory func (cfg BaseConfig) DBDir() string { - return join(cfg.RootDir, cfg.DBPath) + return filepath.Join(cfg.RootDir, cfg.DBPath) } var defaultMoniker = getDefaultMoniker() diff --git a/tm2/pkg/bft/config/config_test.go b/tm2/pkg/bft/config/config_test.go index adeeade26b3..e1e439ac9c0 100644 --- a/tm2/pkg/bft/config/config_test.go +++ b/tm2/pkg/bft/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -15,7 +16,7 @@ func TestConfig_LoadOrMakeConfigWithOptions(t *testing.T) { // Provide an empty directory cfgDir := t.TempDir() - cfgPath := join(cfgDir, defaultConfigFilePath) + cfgPath := filepath.Join(cfgDir, defaultConfigPath) // Create a default config cfg := DefaultConfig() @@ -42,7 +43,7 @@ func TestConfig_LoadOrMakeConfigWithOptions(t *testing.T) { // Provide an empty directory cfgDir := t.TempDir() - cfgPath := join(cfgDir, defaultConfigFilePath) + cfgPath := filepath.Join(cfgDir, defaultConfigPath) cfg, err := LoadOrMakeConfigWithOptions(cfgDir) require.NoError(t, err) @@ -69,7 +70,7 @@ func TestConfig_LoadOrMakeConfigWithOptions(t *testing.T) { // Provide an empty directory cfgDir := t.TempDir() - cfgPath := join(cfgDir, defaultConfigFilePath) + cfgPath := filepath.Join(cfgDir, defaultConfigPath) cfg, err := LoadOrMakeConfigWithOptions( cfgDir, diff --git a/tm2/pkg/bft/config/toml.go b/tm2/pkg/bft/config/toml.go index f813d23b8fb..474fce0e8a3 100644 --- a/tm2/pkg/bft/config/toml.go +++ b/tm2/pkg/bft/config/toml.go @@ -54,27 +54,34 @@ func WriteConfigFile(configFilePath string, config *Config) error { /****** these are for test settings ***********/ -func ResetTestRoot(testName string) *Config { - return ResetTestRootWithChainID(testName, "") -} +func ResetTestRoot(testName string) (*Config, string) { + chainID := "test-chain" -func ResetTestRootWithChainID(testName string, chainID string) *Config { // create a unique, concurrency-safe test directory under os.TempDir() - rootDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s_", chainID, testName)) + testDir, err := os.MkdirTemp("", "") + if err != nil { + panic(err) + } + + rootDir, err := os.MkdirTemp(testDir, fmt.Sprintf("%s-%s_", chainID, testName)) if err != nil { panic(err) } + // ensure config and data subdirs are created if err := osm.EnsureDir(filepath.Join(rootDir, defaultConfigDir), DefaultDirPerm); err != nil { panic(err) } - if err := osm.EnsureDir(filepath.Join(rootDir, defaultDataDir), DefaultDirPerm); err != nil { + if err := osm.EnsureDir(filepath.Join(rootDir, defaultSecretsDir), DefaultDirPerm); err != nil { + panic(err) + } + if err := osm.EnsureDir(filepath.Join(rootDir, DefaultDBDir), DefaultDirPerm); err != nil { panic(err) } baseConfig := DefaultBaseConfig() - configFilePath := filepath.Join(rootDir, defaultConfigFilePath) - genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis) + configFilePath := filepath.Join(rootDir, defaultConfigPath) + genesisFilePath := filepath.Join(rootDir, defaultGenesisJSONName) privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey) privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState) @@ -94,7 +101,8 @@ func ResetTestRootWithChainID(testName string, chainID 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 b75057a52dd..53dea1f2dfe 100644 --- a/tm2/pkg/bft/config/toml_test.go +++ b/tm2/pkg/bft/config/toml_test.go @@ -3,6 +3,7 @@ package config import ( "io" "os" + "path/filepath" "reflect" "strings" "testing" @@ -17,7 +18,7 @@ func ensureFiles(t *testing.T, rootDir string, files ...string) { t.Helper() for _, f := range files { - p := join(f, rootDir) + p := filepath.Join(rootDir, f) _, err := os.Stat(p) assert.Nil(t, err, p) } @@ -33,43 +34,45 @@ func TestEnsureRoot(t *testing.T) { throwaway := DefaultConfig() throwaway.SetRootDir(tmpDir) require.NoError(t, throwaway.EnsureDirs()) - configPath := join(tmpDir, defaultConfigFilePath) + + configPath := filepath.Join(tmpDir, defaultConfigPath) require.NoError(t, WriteConfigFile(configPath, throwaway)) // make sure config is set properly - data, err := os.ReadFile(join(tmpDir, defaultConfigFilePath)) + data, err := os.ReadFile(filepath.Join(tmpDir, defaultConfigPath)) require.Nil(t, err) - if !checkConfig(string(data)) { - t.Fatalf("config file missing some information") - } + require.True(t, checkConfig(string(data))) - ensureFiles(t, tmpDir, "data") + ensureFiles(t, tmpDir, DefaultDBDir) } func TestEnsureTestRoot(t *testing.T) { t.Parallel() - require := require.New(t) - testName := "ensureTestRoot" // create root dir - cfg := ResetTestRoot(testName) + cfg, _ := ResetTestRoot(testName) defer os.RemoveAll(cfg.RootDir) rootDir := cfg.RootDir // make sure config is set properly - data, err := os.ReadFile(join(rootDir, defaultConfigFilePath)) - require.Nil(err) + data, err := os.ReadFile(filepath.Join(rootDir, defaultConfigPath)) + require.Nil(t, err) - if !checkConfig(string(data)) { - t.Fatalf("config file missing some information") - } + require.True(t, checkConfig(string(data))) // TODO: make sure the cfg returned and testconfig are the same! baseConfig := DefaultBaseConfig() - ensureFiles(t, rootDir, defaultDataDir, baseConfig.Genesis, baseConfig.PrivValidatorKey, baseConfig.PrivValidatorState) + ensureFiles( + t, + rootDir, + DefaultDBDir, + baseConfig.Genesis, + baseConfig.PrivValidatorKey, + baseConfig.PrivValidatorState, + ) } func checkConfig(configFile string) bool { diff --git a/tm2/pkg/bft/config/utils.go b/tm2/pkg/bft/config/utils.go deleted file mode 100644 index 5a6eec09e43..00000000000 --- a/tm2/pkg/bft/config/utils.go +++ /dev/null @@ -1,11 +0,0 @@ -package config - -import "path/filepath" - -// helper function to make config creation independent of root dir -func join(root, path string) string { - if filepath.IsAbs(path) { - return path - } - return filepath.Join(root, path) -} 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/config/config.go b/tm2/pkg/bft/consensus/config/config.go index cda4c63ae28..4a350ff3976 100644 --- a/tm2/pkg/bft/consensus/config/config.go +++ b/tm2/pkg/bft/consensus/config/config.go @@ -10,7 +10,7 @@ import ( // ConsensusConfig const ( - defaultDataDir = "data" + defaultWALDir = "wal" ) // ConsensusConfig defines the configuration for the Tendermint consensus service, @@ -44,7 +44,7 @@ type ConsensusConfig struct { // DefaultConsensusConfig returns a default configuration for the consensus service func DefaultConsensusConfig() *ConsensusConfig { return &ConsensusConfig{ - WALPath: filepath.Join(defaultDataDir, "cs.wal", "wal"), + WALPath: filepath.Join(defaultWALDir, "cs.wal", "wal"), TimeoutPropose: 3000 * time.Millisecond, TimeoutProposeDelta: 500 * time.Millisecond, TimeoutPrevote: 1000 * time.Millisecond, @@ -112,7 +112,8 @@ func (cfg *ConsensusConfig) WalFile() string { if cfg.walFile != "" { return cfg.walFile } - return join(cfg.RootDir, cfg.WALPath) + + return filepath.Join(cfg.RootDir, cfg.WALPath) } // SetWalFile sets the path to the write-ahead log file diff --git a/tm2/pkg/bft/consensus/config/utils.go b/tm2/pkg/bft/consensus/config/utils.go deleted file mode 100644 index 5ba8c40b082..00000000000 --- a/tm2/pkg/bft/consensus/config/utils.go +++ /dev/null @@ -1,11 +0,0 @@ -package config - -import "path/filepath" - -// join path to root unless path is already absolute. -func join(root, path string) string { - if filepath.IsAbs(path) { - return path - } - return filepath.Join(root, path) -} 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")) } diff --git a/tm2/pkg/crypto/keys/utils.go b/tm2/pkg/crypto/keys/utils.go index 90e4e6c446d..c848fe574bd 100644 --- a/tm2/pkg/crypto/keys/utils.go +++ b/tm2/pkg/crypto/keys/utils.go @@ -3,13 +3,15 @@ package keys import ( "fmt" "path/filepath" + + "github.com/gnolang/gno/tm2/pkg/bft/config" ) const defaultKeyDBName = "keys" // NewKeyBaseFromDir initializes a keybase at a particular dir. func NewKeyBaseFromDir(rootDir string) (Keybase, error) { - return NewLazyDBKeybase(defaultKeyDBName, filepath.Join(rootDir, "data")), nil + return NewLazyDBKeybase(defaultKeyDBName, filepath.Join(rootDir, config.DefaultDBDir)), nil } func ValidateMultisigThreshold(k, nKeys int) error {