Skip to content

Commit

Permalink
refactor(db): use pebbledb as the default db in integration tests (#1979
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Unique-Divine authored Jul 30, 2024
1 parent d78385b commit c0053ef
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions app/appconst/appconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/big"
"runtime"

db "github.com/cometbft/cometbft-db"
)

const (
Expand All @@ -14,6 +16,11 @@ const (
AccountAddressPrefix = "nibi"
)

var (
DefaultDBBackend db.BackendType = db.PebbleDBBackend
HavePebbleDBBuildTag bool
)

// Runtime version vars
var (
AppVersion = ""
Expand Down
30 changes: 30 additions & 0 deletions app/appconst/consensus_config.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions app/appconst/pebbledb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build pebbledb
// +build pebbledb

package appconst

func init() {
HavePebbleDBBuildTag = true
}
3 changes: 0 additions & 3 deletions cmd/ethclient/const.go

This file was deleted.

25 changes: 3 additions & 22 deletions cmd/nibid/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/nibid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewRootCmd() (*cobra.Command, app.EncodingConfig) {
}

customAppTemplate, customAppConfig := srvconfig.AppConfig("unibi")
tmCfg := customTendermintConfig()
tmCfg := appconst.NewDefaultTendermintConfig()

return sdkserver.InterceptConfigsPreRunHandler(
cmd,
Expand Down
5 changes: 3 additions & 2 deletions contrib/make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test-unit:
.PHONY: test-coverage-unit
test-coverage-unit:
go test ./... -short \
-tags=pebbledb \
-coverprofile=coverage.txt \
-covermode=atomic \
-race
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions eth/eip712/eip712_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(
Expand Down
36 changes: 25 additions & 11 deletions x/common/testutil/testnetwork/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand All @@ -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())

Expand Down Expand Up @@ -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)
Expand All @@ -291,21 +305,21 @@ 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
valPubKeys[valIdx] = pubKey

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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/common/testutil/testnetwork/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c0053ef

Please sign in to comment.