From c0053efe3320b9481c741d3db452e6f54bb36467 Mon Sep 17 00:00:00 2001 From: Unique Divine <51418232+Unique-Divine@users.noreply.github.com> Date: Tue, 30 Jul 2024 13:46:31 +0100 Subject: [PATCH] refactor(db): use pebbledb as the default db in integration tests (#1979) --- CHANGELOG.md | 1 + app/appconst/appconst.go | 7 +++++ app/appconst/consensus_config.go | 30 ++++++++++++++++++++ app/appconst/pebbledb.go | 8 ++++++ cmd/ethclient/const.go | 3 -- cmd/nibid/cmd/init.go | 25 ++-------------- cmd/nibid/cmd/root.go | 2 +- contrib/make/test.mk | 5 ++-- eth/eip712/eip712_test.go | 6 ++-- x/common/testutil/testnetwork/network.go | 36 ++++++++++++++++-------- x/common/testutil/testnetwork/util.go | 4 +-- 11 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 app/appconst/consensus_config.go create mode 100644 app/appconst/pebbledb.go delete mode 100644 cmd/ethclient/const.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 49719139f..87cb72ff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1973](https://github.com/NibiruChain/nibiru/pull/1973) - chore(appconst): Add chain IDs ending in "3" to the "knownEthChainIDMap". This makes it possible to use devnet 3 and testnet 3. - [#1976](https://github.com/NibiruChain/nibiru/pull/1976) - refactor(evm): unique chain ids for all networks - [#1977](https://github.com/NibiruChain/nibiru/pull/1977) - fix(localnet): rolled back change of evm validator address with cosmos derivation path +- [#1979](https://github.com/NibiruChain/nibiru/pull/1979) -refactor(db): use pebbledb as the default db in integration tests #### Dapp modules: perp, spot, oracle, etc diff --git a/app/appconst/appconst.go b/app/appconst/appconst.go index ec1562634..cbf5d8909 100644 --- a/app/appconst/appconst.go +++ b/app/appconst/appconst.go @@ -5,6 +5,8 @@ import ( "fmt" "math/big" "runtime" + + db "github.com/cometbft/cometbft-db" ) const ( @@ -14,6 +16,11 @@ const ( AccountAddressPrefix = "nibi" ) +var ( + DefaultDBBackend db.BackendType = db.PebbleDBBackend + HavePebbleDBBuildTag bool +) + // Runtime version vars var ( AppVersion = "" diff --git a/app/appconst/consensus_config.go b/app/appconst/consensus_config.go new file mode 100644 index 000000000..4291d2672 --- /dev/null +++ b/app/appconst/consensus_config.go @@ -0,0 +1,30 @@ +package appconst + +import ( + "time" + + tmcfg "github.com/cometbft/cometbft/config" +) + +// NewDefaultTendermintConfig returns a consensus "Config" (CometBFT) with new +// default values for the "consensus" and "db_backend" fields to be enforced upon +// node initialization. See the "nibiru/cmd/nibid/cmd/InitCmd" function for more +// information. +func NewDefaultTendermintConfig() *tmcfg.Config { + cfg := tmcfg.DefaultConfig() + + // Overwrite consensus config + ms := func(n time.Duration) time.Duration { + return n * time.Millisecond + } + cfg.Consensus.TimeoutPropose = ms(3_000) + cfg.Consensus.TimeoutProposeDelta = ms(500) + cfg.Consensus.TimeoutPrevote = ms(1_000) + cfg.Consensus.TimeoutPrevoteDelta = ms(500) + cfg.Consensus.TimeoutPrecommit = ms(1_000) + cfg.Consensus.TimeoutPrecommitDelta = ms(500) + cfg.Consensus.TimeoutCommit = ms(1_000) + + cfg.DBBackend = string(DefaultDBBackend) + return cfg +} diff --git a/app/appconst/pebbledb.go b/app/appconst/pebbledb.go new file mode 100644 index 000000000..1a0bc1164 --- /dev/null +++ b/app/appconst/pebbledb.go @@ -0,0 +1,8 @@ +//go:build pebbledb +// +build pebbledb + +package appconst + +func init() { + HavePebbleDBBuildTag = true +} diff --git a/cmd/ethclient/const.go b/cmd/ethclient/const.go deleted file mode 100644 index b3d7a3038..000000000 --- a/cmd/ethclient/const.go +++ /dev/null @@ -1,3 +0,0 @@ -package ethclient - -const Bech32Prefix = "nibi" diff --git a/cmd/nibid/cmd/init.go b/cmd/nibid/cmd/init.go index cc8295bcf..0f84ec8a4 100644 --- a/cmd/nibid/cmd/init.go +++ b/cmd/nibid/cmd/init.go @@ -6,11 +6,11 @@ import ( "fmt" "os" "path/filepath" - "time" - db "github.com/cometbft/cometbft-db" tmcfg "github.com/cometbft/cometbft/config" + "github.com/NibiruChain/nibiru/app/appconst" + tmcli "github.com/cometbft/cometbft/libs/cli" tmrand "github.com/cometbft/cometbft/libs/rand" tmtypes "github.com/cometbft/cometbft/types" @@ -38,25 +38,6 @@ const ( FlagDefaultBondDenom = "default-denom" ) -func customTendermintConfig() *tmcfg.Config { - cfg := tmcfg.DefaultConfig() - - // Overwrite consensus config - ms := func(n time.Duration) time.Duration { - return n * time.Millisecond - } - cfg.Consensus.TimeoutPropose = ms(3_000) - cfg.Consensus.TimeoutProposeDelta = ms(500) - cfg.Consensus.TimeoutPrevote = ms(1_000) - cfg.Consensus.TimeoutPrevoteDelta = ms(500) - cfg.Consensus.TimeoutPrecommit = ms(1_000) - cfg.Consensus.TimeoutPrecommitDelta = ms(500) - cfg.Consensus.TimeoutCommit = ms(1_000) - - cfg.DBBackend = string(db.PebbleDBBackend) - return cfg -} - /* InitCmd is a stand-in replacement for genutilcli.InitCmd that overwrites the consensus configutation in the `config.toml` prior to writing it to the disk. @@ -176,7 +157,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command { toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState) - customCfg := customTendermintConfig() + customCfg := appconst.NewDefaultTendermintConfig() config.Consensus = customCfg.Consensus config.DBBackend = customCfg.DBBackend tmcfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) diff --git a/cmd/nibid/cmd/root.go b/cmd/nibid/cmd/root.go index 5bc620516..f4ca78e60 100644 --- a/cmd/nibid/cmd/root.go +++ b/cmd/nibid/cmd/root.go @@ -76,7 +76,7 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) { } customAppTemplate, customAppConfig := srvconfig.AppConfig("unibi") - tmCfg := customTendermintConfig() + tmCfg := appconst.NewDefaultTendermintConfig() return sdkserver.InterceptConfigsPreRunHandler( cmd, diff --git a/contrib/make/test.mk b/contrib/make/test.mk index 84e839a4e..529220445 100644 --- a/contrib/make/test.mk +++ b/contrib/make/test.mk @@ -9,6 +9,7 @@ test-unit: .PHONY: test-coverage-unit test-coverage-unit: go test ./... -short \ + -tags=pebbledb \ -coverprofile=coverage.txt \ -covermode=atomic \ -race @@ -18,10 +19,10 @@ test-coverage-unit: .PHONY: test-coverage-integration test-coverage-integration: go test ./... \ + -tags=pebbledb \ -coverprofile=coverage.txt \ -covermode=atomic \ - -race \ - -v + -race # Require Python3 .PHONY: test-create-test-cases diff --git a/eth/eip712/eip712_test.go b/eth/eip712/eip712_test.go index 885f292a5..dd8a44595 100644 --- a/eth/eip712/eip712_test.go +++ b/eth/eip712/eip712_test.go @@ -17,6 +17,7 @@ import ( "github.com/tidwall/gjson" "github.com/tidwall/sjson" + "github.com/NibiruChain/nibiru/app/appconst" "github.com/NibiruChain/nibiru/eth/eip712" "github.com/NibiruChain/nibiru/x/common/testutil" "github.com/NibiruChain/nibiru/x/evm" @@ -27,7 +28,6 @@ import ( "github.com/NibiruChain/nibiru/eth/crypto/ethsecp256k1" "github.com/NibiruChain/nibiru/app" - "github.com/NibiruChain/nibiru/cmd/ethclient" sdktx "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" @@ -82,7 +82,7 @@ func (suite *EIP712TestSuite) SetupTest() { suite.clientCtx = client.Context{}.WithTxConfig(suite.config.TxConfig) suite.denom = evm.DefaultEVMDenom - sdk.GetConfig().SetBech32PrefixForAccount(ethclient.Bech32Prefix, "") + sdk.GetConfig().SetBech32PrefixForAccount(appconst.AccountAddressPrefix, "") eip712.SetEncodingConfig(suite.config) } @@ -369,7 +369,7 @@ func (suite *EIP712TestSuite) TestEIP712() { AccountNumber: params.accountNumber, Sequence: params.sequence, PubKey: pubKey, - Address: sdk.MustBech32ifyAddressBytes(ethclient.Bech32Prefix, pubKey.Bytes()), + Address: sdk.MustBech32ifyAddressBytes(appconst.AccountAddressPrefix, pubKey.Bytes()), } bz, err := suite.clientCtx.TxConfig.SignModeHandler().GetSignBytes( diff --git a/x/common/testutil/testnetwork/network.go b/x/common/testutil/testnetwork/network.go index 44c1f6ac2..44a14cc59 100644 --- a/x/common/testutil/testnetwork/network.go +++ b/x/common/testutil/testnetwork/network.go @@ -15,6 +15,7 @@ import ( srvconfig "github.com/cosmos/cosmos-sdk/server/config" "github.com/ethereum/go-ethereum/common" + "github.com/NibiruChain/nibiru/app/appconst" serverconfig "github.com/NibiruChain/nibiru/app/server/config" "github.com/cometbft/cometbft/libs/log" @@ -143,12 +144,20 @@ Example: network, err := testnetwork.New(s.T(), s.T().TempDir(), cfg) s.Require().NoError(err) */ -func New(logger Logger, baseDir string, cfg Config) (*Network, error) { +func New(logger Logger, baseDir string, cfg Config) (network *Network, err error) { // only one caller/test can create and use a network at a time logger.Log("acquiring test network lock") lock.Lock() - network := &Network{ + // This is a `defer` pattern to add behavior that runs in the case that the error is + // non-nil, creating a concise way to add extra information. + defer func() { + if err != nil { + err = fmt.Errorf("error starting test network: %w", err) + } + }() + + network = &Network{ Logger: logger, BaseDir: baseDir, Validators: make([]*Validator, cfg.NumValidators), @@ -180,7 +189,12 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { ctx := server.NewDefaultContext() tmCfg := ctx.Config + tmCfg.Consensus.TimeoutCommit = cfg.TimeoutCommit + if appconst.HavePebbleDBBuildTag { + defaultTmCfg := appconst.NewDefaultTendermintConfig() + tmCfg.DBBackend = defaultTmCfg.DBBackend + } // Only allow the first validator to expose an RPC, API and gRPC // server/client due to Tendermint in-process constraints. @@ -196,14 +210,14 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { var err error apiListenAddr, _, err = server.FreeTCPAddr() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get free TCP address for API: %w", err) } } appCfg.API.Address = apiListenAddr apiURL, err := url.Parse(apiListenAddr) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse API listen address: %w", err) } apiAddr = fmt.Sprintf("http://%s:%s", apiURL.Hostname(), apiURL.Port()) @@ -262,12 +276,12 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { err := os.MkdirAll(filepath.Join(nodeDir, "config"), 0o755) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create node configuration directory: %w", err) } err = os.MkdirAll(clientDir, 0o755) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create node client directory: %w", err) } tmCfg.SetRoot(nodeDir) @@ -291,7 +305,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { nodeID, pubKey, err := genutil.InitializeNodeValidatorFiles(tmCfg) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to initialize node validator files: %w", err) } nodeIDs[valIdx] = nodeID @@ -299,13 +313,13 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, clientDir, buf, cfg.Codec, cfg.KeyringOptions...) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create new keyring: %w", err) } keyringAlgos, _ := kb.SupportedAlgorithms() algo, err := keyring.NewSigningAlgoFromString(cfg.SigningAlgo, keyringAlgos) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse signing algorithm: %w", err) } var mnemonic string @@ -429,7 +443,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { } } - err := initGenFiles(cfg, genAccounts, genBalances, genFiles) + err = initGenFiles(cfg, genAccounts, genBalances, genFiles) if err != nil { return nil, err } @@ -442,7 +456,7 @@ func New(logger Logger, baseDir string, cfg Config) (*Network, error) { for idx, v := range network.Validators { err := startInProcess(cfg, v) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to start node: %w", err) } logger.Log("started validator", idx) } diff --git a/x/common/testutil/testnetwork/util.go b/x/common/testutil/testnetwork/util.go index 150f623e8..0a092d918 100644 --- a/x/common/testutil/testnetwork/util.go +++ b/x/common/testutil/testnetwork/util.go @@ -69,11 +69,11 @@ func startInProcess(cfg Config, val *Validator) error { logger.With("module", val.Moniker), ) if err != nil { - return err + return fmt.Errorf("failed to construct Node: %w", err) } if err := tmNode.Start(); err != nil { - return err + return fmt.Errorf("failed Node.Start(): %w", err) } val.tmNode = tmNode