diff --git a/libs/db/badgerdb/db.go b/libs/db/badgerdb/db.go index 94a727dc46..122ec117e8 100644 --- a/libs/db/badgerdb/db.go +++ b/libs/db/badgerdb/db.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/dgraph-io/badger/v3" + "github.com/dgraph-io/badger/v3/options" "github.com/lazyledger/lazyledger-core/libs/db" ) @@ -22,7 +23,8 @@ func NewDB(dbName, dir string) (*BadgerDB, error) { } opts := badger.DefaultOptions(path) opts.SyncWrites = false // note that we have Sync methods - opts.Logger = nil // badger is too chatty by default + // TODO(ismail): investigate if we don't want a logger here at least for errors though: + opts.Logger = nil // badger is too chatty by default return NewDBWithOptions(opts) } @@ -37,6 +39,22 @@ func NewDBWithOptions(opts badger.Options) (*BadgerDB, error) { return &BadgerDB{db: db}, nil } +// NewInMemoryDB creates a light weight in-memory BadgerDB. +// Mainly useful for unit-tests. +func NewInMemoryDB() (*BadgerDB, error) { + opts := badger.DefaultOptions("") + opts.InMemory = true + opts.NumCompactors = 2 // minimize number of go-routines + opts.Compression = options.None // this is supposed to be short-lived + opts.ZSTDCompressionLevel = 0 // this is supposed to be short-lived + opts.Logger = nil // there is not much that can go wrong in-memory + db, err := badger.Open(opts) + if err != nil { + return nil, err + } + return &BadgerDB{db: db}, nil +} + type BadgerDB struct { db *badger.DB } diff --git a/node/node.go b/node/node.go index f3391f92e9..1235c65b2d 100644 --- a/node/node.go +++ b/node/node.go @@ -67,6 +67,11 @@ func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) { return badgerdb.NewDB(ctx.ID, ctx.Config.DBDir()) } +// InMemDBProvider provides an in-memory DB. +func InMemDBProvider(ctx *DBContext) (dbm.DB, error) { + return badgerdb.NewInMemoryDB() +} + // GenesisDocProvider returns a GenesisDoc. // It allows the GenesisDoc to be pulled from sources other than the // filesystem, for instance from a distributed key-value store cluster. diff --git a/node/node_test.go b/node/node_test.go index b8b448871c..cb5a351814 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -35,12 +35,35 @@ import ( tmtime "github.com/lazyledger/lazyledger-core/types/time" ) +func defaultNewTestNode(config *cfg.Config, logger log.Logger) (*Node, error) { + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err) + } + + pval, err := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) + if err != nil { + return nil, err + } + + return NewNode(config, + pval, + nodeKey, + proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), + DefaultGenesisDocProviderFunc(config), + InMemDBProvider, + ipfs.Mock(), + DefaultMetricsProvider(config.Instrumentation), + logger, + ) +} + func TestNodeStartStop(t *testing.T) { config := cfg.ResetTestRoot("node_node_test") defer os.RemoveAll(config.RootDir) // create & start node - n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger()) + n, err := defaultNewTestNode(config, log.TestingLogger()) require.NoError(t, err) err = n.Start() require.NoError(t, err) @@ -103,7 +126,7 @@ func TestNodeDelayedStart(t *testing.T) { now := tmtime.Now() // create & start node - n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger()) + n, err := defaultNewTestNode(config, log.TestingLogger()) n.GenesisDoc().GenesisTime = now.Add(2 * time.Second) require.NoError(t, err) @@ -120,7 +143,7 @@ func TestNodeSetAppVersion(t *testing.T) { defer os.RemoveAll(config.RootDir) // create & start node - n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger()) + n, err := defaultNewTestNode(config, log.TestingLogger()) require.NoError(t, err) // default config uses the kvstore app @@ -136,6 +159,7 @@ func TestNodeSetAppVersion(t *testing.T) { } func TestNodeSetPrivValTCP(t *testing.T) { + t.Skip("TODO(ismail): Mock these conns using net.Pipe instead") addr := "tcp://" + testFreeAddr(t) config := cfg.ResetTestRoot("node_priv_val_tcp_test") @@ -164,7 +188,7 @@ func TestNodeSetPrivValTCP(t *testing.T) { defer signerServer.Stop() //nolint:errcheck // ignore for tests logger := log.TestingLogger() - n, err := DefaultNewNode(config, ipfs.Mock(), logger) + n, err := defaultNewTestNode(config, logger) require.NoError(t, err) assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator()) } @@ -177,7 +201,7 @@ func TestPrivValidatorListenAddrNoProtocol(t *testing.T) { defer os.RemoveAll(config.RootDir) config.BaseConfig.PrivValidatorListenAddr = addrNoPrefix - _, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger()) + _, err := defaultNewTestNode(config, log.TestingLogger()) assert.Error(t, err) } @@ -209,7 +233,7 @@ func TestNodeSetPrivValIPC(t *testing.T) { defer pvsc.Stop() //nolint:errcheck // ignore for tests logger := log.TestingLogger() - n, err := DefaultNewNode(config, ipfs.Mock(), logger) + n, err := defaultNewTestNode(config, logger) require.NoError(t, err) assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator()) } @@ -514,7 +538,7 @@ func TestNodeNewNodeCustomReactors(t *testing.T) { nodeKey, proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), DefaultGenesisDocProviderFunc(config), - DefaultDBProvider, + InMemDBProvider, ipfs.Mock(), DefaultMetricsProvider(config.Instrumentation), log.TestingLogger(), diff --git a/p2p/transport_test.go b/p2p/transport_test.go index e13c81534c..2e01462e16 100644 --- a/p2p/transport_test.go +++ b/p2p/transport_test.go @@ -615,7 +615,7 @@ func TestTransportHandshake(t *testing.T) { t.Fatal(err) } - ni, err := handshake(c, 20*time.Millisecond, emptyNodeInfo()) + ni, err := handshake(c, 100*time.Millisecond, emptyNodeInfo()) if err != nil { t.Fatal(err) } @@ -651,7 +651,7 @@ func testSetupMultiplexTransport(t *testing.T) *MultiplexTransport { } // give the listener some time to get ready - time.Sleep(20 * time.Millisecond) + time.Sleep(100 * time.Millisecond) return mt } diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index de6d79825d..5a8b8e747a 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -175,7 +175,7 @@ func NewTendermint(app abci.Application, opts *Options) *nm.Node { node, err := nm.NewNode(config, pv, nodeKey, papp, nm.DefaultGenesisDocProviderFunc(config), - nm.DefaultDBProvider, + nm.InMemDBProvider, ipfs.Mock(), nm.DefaultMetricsProvider(config.Instrumentation), logger,