multi: rename chan DB Open method to OpenForTesting

This commit is contained in:
Elle Mouton 2024-11-26 12:31:38 +02:00
parent 4089fbcb44
commit 439a6c7d6c
No known key found for this signature in database
GPG Key ID: D7D916376026F177
24 changed files with 101 additions and 331 deletions

View File

@ -37,11 +37,7 @@ var (
func initHintCache(t *testing.T) *channeldb.HeightHintCache { func initHintCache(t *testing.T) *channeldb.HeightHintCache {
t.Helper() t.Helper()
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
testCfg := channeldb.CacheConfig{ testCfg := channeldb.CacheConfig{
QueryDisable: false, QueryDisable: false,

View File

@ -33,11 +33,7 @@ var (
func initHintCache(t *testing.T) *channeldb.HeightHintCache { func initHintCache(t *testing.T) *channeldb.HeightHintCache {
t.Helper() t.Helper()
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
require.NoError(t, err, "unable to create db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
testCfg := channeldb.CacheConfig{ testCfg := channeldb.CacheConfig{
QueryDisable: false, QueryDisable: false,

View File

@ -1906,10 +1906,8 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
// Initialize a height hint cache for each notifier. // Initialize a height hint cache for each notifier.
tempDir := t.TempDir() tempDir := t.TempDir()
db, err := channeldb.Open(tempDir) db := channeldb.OpenForTesting(t, tempDir)
if err != nil {
t.Fatalf("unable to create db: %v", err)
}
testCfg := channeldb.CacheConfig{ testCfg := channeldb.CacheConfig{
QueryDisable: false, QueryDisable: false,
} }

View File

@ -34,6 +34,7 @@ import (
"github.com/lightningnetwork/lnd/invoices" "github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -345,10 +346,11 @@ type DB struct {
noRevLogAmtData bool noRevLogAmtData bool
} }
// Open opens or creates channeldb. Any necessary schemas migrations due // OpenForTesting opens or creates a channeldb to be used for tests. Any
// to updates will take place as necessary. // necessary schemas migrations due to updates will take place as necessary.
// TODO(bhandras): deprecate this function. func OpenForTesting(t testing.TB, dbPath string,
func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) { modifiers ...OptionModifier) *DB {
backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{ backend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
DBPath: dbPath, DBPath: dbPath,
DBFileName: dbName, DBFileName: dbName,
@ -357,16 +359,18 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) {
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge, AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout, DBTimeout: kvdb.DefaultDBTimeout,
}) })
if err != nil { require.NoError(t, err)
return nil, err
}
db, err := CreateWithBackend(backend, modifiers...) db, err := CreateWithBackend(backend, modifiers...)
if err == nil { require.NoError(t, err)
db.dbPath = dbPath
}
return db, err db.dbPath = dbPath
t.Cleanup(func() {
require.NoError(t, db.Close())
})
return db
} }
// CreateWithBackend creates channeldb instance using the passed kvdb.Backend. // CreateWithBackend creates channeldb instance using the passed kvdb.Backend.

View File

@ -65,11 +65,7 @@ func TestOpenWithCreate(t *testing.T) {
// Now, reopen the same db in dry run migration mode. Since we have not // Now, reopen the same db in dry run migration mode. Since we have not
// applied any migrations, this should ignore the flag and not fail. // applied any migrations, this should ignore the flag and not fail.
cdb, err = Open(dbPath, OptionDryRunMigration(true)) OpenForTesting(t, dbPath, OptionDryRunMigration(true))
require.NoError(t, err, "unable to create channeldb")
if err := cdb.Close(); err != nil {
t.Fatalf("unable to close channeldb: %v", err)
}
} }
// TestWipe tests that the database wipe operation completes successfully // TestWipe tests that the database wipe operation completes successfully

View File

@ -23,15 +23,11 @@ func initHintCache(t *testing.T) *HeightHintCache {
func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache { func initHintCacheWithConfig(t *testing.T, cfg CacheConfig) *HeightHintCache {
t.Helper() t.Helper()
db, err := Open(t.TempDir()) db := OpenForTesting(t, t.TempDir())
require.NoError(t, err, "unable to create db")
hintCache, err := NewHeightHintCache(cfg, db.Backend) hintCache, err := NewHeightHintCache(cfg, db.Backend)
require.NoError(t, err, "unable to create hint cache") require.NoError(t, err, "unable to create hint cache")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
return hintCache return hintCache
} }

View File

@ -635,15 +635,6 @@ func TestMockRetributionStore(t *testing.T) {
} }
} }
func makeTestChannelDB(t *testing.T) (*channeldb.DB, error) {
db, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, err
}
return db, nil
}
// TestChannelDBRetributionStore instantiates a retributionStore backed by a // TestChannelDBRetributionStore instantiates a retributionStore backed by a
// channeldb.DB, and tests its behavior using the general RetributionStore test // channeldb.DB, and tests its behavior using the general RetributionStore test
// suite. // suite.
@ -654,25 +645,19 @@ func TestChannelDBRetributionStore(t *testing.T) {
t.Run( t.Run(
"channeldbDBRetributionStore."+test.name, "channeldbDBRetributionStore."+test.name,
func(tt *testing.T) { func(tt *testing.T) {
db, err := makeTestChannelDB(t) db := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
t.Fatalf("unable to open channeldb: %v", err)
}
defer db.Close()
restartDb := func() RetributionStorer { restartDb := func() RetributionStorer {
// Close and reopen channeldb // Close and reopen channeldb
if err = db.Close(); err != nil { if err := db.Close(); err != nil {
t.Fatalf("unable to close "+ t.Fatalf("unable to close "+
"channeldb during "+ "channeldb during "+
"restart: %v", "restart: %v",
err) err)
} }
db, err = channeldb.Open(db.Path()) db = channeldb.OpenForTesting(
if err != nil { t, db.Path(),
t.Fatalf("unable to open "+ )
"channeldb: %v", err)
}
return NewRetributionStore(db) return NewRetributionStore(db)
} }
@ -2279,21 +2264,8 @@ func createInitChannels(t *testing.T) (
return nil, nil, err return nil, nil, err
} }
dbAlice, err := channeldb.Open(t.TempDir()) dbAlice := channeldb.OpenForTesting(t, t.TempDir())
if err != nil { dbBob := channeldb.OpenForTesting(t, t.TempDir())
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbAlice.Close())
})
dbBob, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbBob.Close())
})
estimator := chainfee.NewStaticEstimator(12500, 0) estimator := chainfee.NewStaticEstimator(12500, 0)
feePerKw, err := estimator.EstimateFeePerKW(1) feePerKw, err := estimator.EstimateFeePerKW(1)

View File

@ -22,13 +22,7 @@ import (
func TestChainArbitratorRepublishCloses(t *testing.T) { func TestChainArbitratorRepublishCloses(t *testing.T) {
t.Parallel() t.Parallel()
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
require.NoError(t, db.Close())
})
// Create 10 test channels and sync them to the database. // Create 10 test channels and sync them to the database.
const numChans = 10 const numChans = 10
@ -139,11 +133,7 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
func TestResolveContract(t *testing.T) { func TestResolveContract(t *testing.T) {
t.Parallel() t.Parallel()
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
require.NoError(t, err, "unable to open db")
t.Cleanup(func() {
require.NoError(t, db.Close())
})
// With the DB created, we'll make a new channel, and mark it as // With the DB created, we'll make a new channel, and mark it as
// pending open within the database. // pending open within the database.

View File

@ -65,12 +65,9 @@ func copyChannelState(t *testing.T, state *channeldb.OpenChannel) (
return nil, err return nil, err
} }
newDb, err := channeldb.Open(tempDbPath) newDB := channeldb.OpenForTesting(t, tempDbPath)
if err != nil {
return nil, err
}
chans, err := newDb.ChannelStateDB().FetchAllChannels() chans, err := newDB.ChannelStateDB().FetchAllChannels()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -73,21 +73,6 @@ var (
rebroadcastInterval = time.Hour * 1000000 rebroadcastInterval = time.Hour * 1000000
) )
// makeTestDB creates a new instance of the ChannelDB for testing purposes.
func makeTestDB(t *testing.T) (*channeldb.DB, error) {
// Create channeldb for the first time.
cdb, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, err
}
t.Cleanup(func() {
cdb.Close()
})
return cdb, nil
}
type mockGraphSource struct { type mockGraphSource struct {
bestHeight uint32 bestHeight uint32
@ -734,10 +719,7 @@ func createTestCtx(t *testing.T, startHeight uint32, isChanPeer bool) (
notifier := newMockNotifier() notifier := newMockNotifier()
router := newMockRouter(startHeight) router := newMockRouter(startHeight)
db, err := makeTestDB(t) db := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
return nil, err
}
waitingProofStore, err := channeldb.NewWaitingProofStore(db) waitingProofStore, err := channeldb.NewWaitingProofStore(db)
if err != nil { if err != nil {

View File

@ -17,19 +17,10 @@ import (
func createTestMessageStore(t *testing.T) *MessageStore { func createTestMessageStore(t *testing.T) *MessageStore {
t.Helper() t.Helper()
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
t.Fatalf("unable to open db: %v", err)
}
t.Cleanup(func() {
db.Close()
})
store, err := NewMessageStore(db) store, err := NewMessageStore(db)
if err != nil { require.NoError(t, err)
t.Fatalf("unable to initialize message store: %v", err)
}
return store return store
} }

View File

@ -427,10 +427,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
} }
dbDir := filepath.Join(tempTestDir, "cdb") dbDir := filepath.Join(tempTestDir, "cdb")
fullDB, err := channeldb.Open(dbDir) fullDB := channeldb.OpenForTesting(t, dbDir)
if err != nil {
return nil, err
}
cdb := fullDB.ChannelStateDB() cdb := fullDB.ChannelStateDB()

View File

@ -625,9 +625,7 @@ func makeCircuitDB(t *testing.T, path string) *channeldb.DB {
path = t.TempDir() path = t.TempDir()
} }
db, err := channeldb.Open(path) db := channeldb.OpenForTesting(t, path)
require.NoError(t, err, "unable to open channel db")
t.Cleanup(func() { db.Close() })
return db return db
} }

View File

@ -2169,7 +2169,7 @@ func newSingleLinkTestHarness(t *testing.T, chanAmt,
BaseFee: lnwire.NewMSatFromSatoshis(1), BaseFee: lnwire.NewMSatFromSatoshis(1),
TimeLockDelta: 6, TimeLockDelta: 6,
} }
invoiceRegistry = newMockRegistry(globalPolicy.TimeLockDelta) invoiceRegistry = newMockRegistry(t)
) )
pCache := newMockPreimageCache() pCache := newMockPreimageCache()
@ -2267,7 +2267,6 @@ func newSingleLinkTestHarness(t *testing.T, chanAmt,
t.Cleanup(func() { t.Cleanup(func() {
close(alicePeer.quit) close(alicePeer.quit)
invoiceRegistry.cleanup()
}) })
harness := singleLinkTestHarness{ harness := singleLinkTestHarness{

View File

@ -8,7 +8,6 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"os"
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -216,11 +215,7 @@ func initSwitchWithTempDB(t testing.TB, startingHeight uint32) (*Switch,
error) { error) {
tempPath := filepath.Join(t.TempDir(), "switchdb") tempPath := filepath.Join(t.TempDir(), "switchdb")
db, err := channeldb.Open(tempPath) db := channeldb.OpenForTesting(t, tempPath)
if err != nil {
return nil, err
}
t.Cleanup(func() { db.Close() })
s, err := initSwitchWithDB(startingHeight, db) s, err := initSwitchWithDB(startingHeight, db)
if err != nil { if err != nil {
@ -254,9 +249,7 @@ func newMockServer(t testing.TB, name string, startingHeight uint32,
t.Cleanup(func() { _ = htlcSwitch.Stop() }) t.Cleanup(func() { _ = htlcSwitch.Stop() })
registry := newMockRegistry(defaultDelta) registry := newMockRegistry(t)
t.Cleanup(func() { registry.cleanup() })
return &mockServer{ return &mockServer{
t: t, t: t,
@ -977,37 +970,12 @@ func (f *mockChannelLink) CommitmentCustomBlob() fn.Option[tlv.Blob] {
var _ ChannelLink = (*mockChannelLink)(nil) var _ ChannelLink = (*mockChannelLink)(nil)
func newDB() (*channeldb.DB, func(), error) {
// First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := os.MkdirTemp("", "channeldb")
if err != nil {
return nil, nil, err
}
// Next, create channeldb for the first time.
cdb, err := channeldb.Open(tempDirName)
if err != nil {
os.RemoveAll(tempDirName)
return nil, nil, err
}
cleanUp := func() {
cdb.Close()
os.RemoveAll(tempDirName)
}
return cdb, cleanUp, nil
}
const testInvoiceCltvExpiry = 6 const testInvoiceCltvExpiry = 6
type mockInvoiceRegistry struct { type mockInvoiceRegistry struct {
settleChan chan lntypes.Hash settleChan chan lntypes.Hash
registry *invoices.InvoiceRegistry registry *invoices.InvoiceRegistry
cleanup func()
} }
type mockChainNotifier struct { type mockChainNotifier struct {
@ -1024,11 +992,8 @@ func (m *mockChainNotifier) RegisterBlockEpochNtfn(*chainntnfs.BlockEpoch) (
}, nil }, nil
} }
func newMockRegistry(minDelta uint32) *mockInvoiceRegistry { func newMockRegistry(t testing.TB) *mockInvoiceRegistry {
cdb, cleanup, err := newDB() cdb := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
panic(err)
}
modifierMock := &invoices.MockHtlcModifier{} modifierMock := &invoices.MockHtlcModifier{}
registry := invoices.NewRegistry( registry := invoices.NewRegistry(
@ -1046,7 +1011,6 @@ func newMockRegistry(minDelta uint32) *mockInvoiceRegistry {
return &mockInvoiceRegistry{ return &mockInvoiceRegistry{
registry: registry, registry: registry,
cleanup: cleanup,
} }
} }

View File

@ -101,11 +101,7 @@ func TestNetworkResultStore(t *testing.T) {
const numResults = 4 const numResults = 4
db, err := channeldb.Open(t.TempDir()) db := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
store := newNetworkResultStore(db) store := newNetworkResultStore(db)

View File

@ -1002,9 +1002,7 @@ func TestSwitchForwardFailAfterFullAdd(t *testing.T) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -1096,9 +1094,7 @@ func TestSwitchForwardFailAfterFullAdd(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -1192,9 +1188,7 @@ func TestSwitchForwardSettleAfterFullAdd(t *testing.T) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -1286,9 +1280,7 @@ func TestSwitchForwardSettleAfterFullAdd(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -1385,9 +1377,7 @@ func TestSwitchForwardDropAfterFullAdd(t *testing.T) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -1471,9 +1461,7 @@ func TestSwitchForwardDropAfterFullAdd(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -1541,9 +1529,7 @@ func TestSwitchForwardFailAfterHalfAdd(t *testing.T) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -1622,9 +1608,7 @@ func TestSwitchForwardFailAfterHalfAdd(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -1698,9 +1682,7 @@ func TestSwitchForwardCircuitPersistence(t *testing.T) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -1778,9 +1760,7 @@ func TestSwitchForwardCircuitPersistence(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -1870,9 +1850,7 @@ func TestSwitchForwardCircuitPersistence(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
cdb3, err := channeldb.Open(tempPath) cdb3 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to reopen channeldb")
t.Cleanup(func() { cdb3.Close() })
s3, err := initSwitchWithDB(testStartingHeight, cdb3) s3, err := initSwitchWithDB(testStartingHeight, cdb3)
require.NoError(t, err, "unable reinit switch") require.NoError(t, err, "unable reinit switch")
@ -3827,9 +3805,7 @@ func newInterceptableSwitchTestContext(
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err, "unable to open channeldb")
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err, "unable to init switch") require.NoError(t, err, "unable to init switch")
@ -4914,9 +4890,7 @@ func testSwitchForwardFailAlias(t *testing.T, zeroConf bool) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err)
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err) require.NoError(t, err)
@ -4990,9 +4964,7 @@ func testSwitchForwardFailAlias(t *testing.T, zeroConf bool) {
err = cdb.Close() err = cdb.Close()
require.NoError(t, err) require.NoError(t, err)
cdb2, err := channeldb.Open(tempPath) cdb2 := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err)
t.Cleanup(func() { cdb2.Close() })
s2, err := initSwitchWithDB(testStartingHeight, cdb2) s2, err := initSwitchWithDB(testStartingHeight, cdb2)
require.NoError(t, err) require.NoError(t, err)
@ -5130,9 +5102,7 @@ func testSwitchAliasFailAdd(t *testing.T, zeroConf, private, useAlias bool) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err)
defer cdb.Close()
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err) require.NoError(t, err)
@ -5471,9 +5441,7 @@ func testSwitchAliasInterceptFail(t *testing.T, zeroConf bool) {
tempPath := t.TempDir() tempPath := t.TempDir()
cdb, err := channeldb.Open(tempPath) cdb := channeldb.OpenForTesting(t, tempPath)
require.NoError(t, err)
t.Cleanup(func() { cdb.Close() })
s, err := initSwitchWithDB(testStartingHeight, cdb) s, err := initSwitchWithDB(testStartingHeight, cdb)
require.NoError(t, err) require.NoError(t, err)

View File

@ -251,21 +251,8 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte,
return nil, nil, err return nil, nil, err
} }
dbAlice, err := channeldb.Open(t.TempDir()) dbAlice := channeldb.OpenForTesting(t, t.TempDir())
if err != nil { dbBob := channeldb.OpenForTesting(t, t.TempDir())
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbAlice.Close())
})
dbBob, err := channeldb.Open(t.TempDir())
if err != nil {
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbBob.Close())
})
estimator := chainfee.NewStaticEstimator(6000, 0) estimator := chainfee.NewStaticEstimator(6000, 0)
feePerKw, err := estimator.EstimateFeePerKW(1) feePerKw, err := estimator.EstimateFeePerKW(1)
@ -403,11 +390,7 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte,
switch err { switch err {
case nil: case nil:
case kvdb.ErrDatabaseNotOpen: case kvdb.ErrDatabaseNotOpen:
dbAlice, err = channeldb.Open(dbAlice.Path()) dbAlice = channeldb.OpenForTesting(t, dbAlice.Path())
if err != nil {
return nil, errors.Errorf("unable to reopen alice "+
"db: %v", err)
}
aliceStoredChannels, err = dbAlice.ChannelStateDB(). aliceStoredChannels, err = dbAlice.ChannelStateDB().
FetchOpenChannels(aliceKeyPub) FetchOpenChannels(aliceKeyPub)
@ -451,7 +434,7 @@ func createTestChannel(t *testing.T, alicePrivKey, bobPrivKey []byte,
switch err { switch err {
case nil: case nil:
case kvdb.ErrDatabaseNotOpen: case kvdb.ErrDatabaseNotOpen:
dbBob, err = channeldb.Open(dbBob.Path()) dbBob = channeldb.OpenForTesting(t, dbBob.Path())
if err != nil { if err != nil {
return nil, errors.Errorf("unable to reopen bob "+ return nil, errors.Errorf("unable to reopen bob "+
"db: %v", err) "db: %v", err)

View File

@ -311,16 +311,14 @@ func loadTestCredits(miner *rpctest.Harness, w *lnwallet.LightningWallet,
// createTestWallet creates a test LightningWallet will a total of 20BTC // createTestWallet creates a test LightningWallet will a total of 20BTC
// available for funding channels. // available for funding channels.
func createTestWallet(tempTestDir string, miningNode *rpctest.Harness, func createTestWallet(t *testing.T, tempTestDir string,
netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier, miningNode *rpctest.Harness, netParams *chaincfg.Params,
wc lnwallet.WalletController, keyRing keychain.SecretKeyRing, notifier chainntnfs.ChainNotifier, wc lnwallet.WalletController,
signer input.Signer, bio lnwallet.BlockChainIO) (*lnwallet.LightningWallet, error) { keyRing keychain.SecretKeyRing, signer input.Signer,
bio lnwallet.BlockChainIO) *lnwallet.LightningWallet {
dbDir := filepath.Join(tempTestDir, "cdb") dbDir := filepath.Join(tempTestDir, "cdb")
fullDB, err := channeldb.Open(dbDir) fullDB := channeldb.OpenForTesting(t, dbDir)
if err != nil {
return nil, err
}
cfg := lnwallet.Config{ cfg := lnwallet.Config{
Database: fullDB.ChannelStateDB(), Database: fullDB.ChannelStateDB(),
@ -335,20 +333,18 @@ func createTestWallet(tempTestDir string, miningNode *rpctest.Harness,
} }
wallet, err := lnwallet.NewLightningWallet(cfg) wallet, err := lnwallet.NewLightningWallet(cfg)
if err != nil { require.NoError(t, err)
return nil, err
}
if err := wallet.Startup(); err != nil { require.NoError(t, wallet.Startup())
return nil, err
} t.Cleanup(func() {
require.NoError(t, wallet.Shutdown())
})
// Load our test wallet with 20 outputs each holding 4BTC. // Load our test wallet with 20 outputs each holding 4BTC.
if err := loadTestCredits(miningNode, wallet, 20, 4); err != nil { require.NoError(t, loadTestCredits(miningNode, wallet, 20, 4))
return nil, err
}
return wallet, nil return wallet
} }
func testGetRecoveryInfo(miner *rpctest.Harness, func testGetRecoveryInfo(miner *rpctest.Harness,
@ -3206,9 +3202,7 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) {
rpcConfig := miningNode.RPCConfig() rpcConfig := miningNode.RPCConfig()
tempDir := t.TempDir() db := channeldb.OpenForTesting(t, t.TempDir())
db, err := channeldb.Open(tempDir)
require.NoError(t, err, "unable to create db")
testCfg := channeldb.CacheConfig{ testCfg := channeldb.CacheConfig{
QueryDisable: false, QueryDisable: false,
} }
@ -3450,20 +3444,16 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
} }
// Funding via 20 outputs with 4BTC each. // Funding via 20 outputs with 4BTC each.
alice, err := createTestWallet( alice := createTestWallet(
tempTestDirAlice, miningNode, netParams, t, tempTestDirAlice, miningNode, netParams,
chainNotifier, aliceWalletController, aliceKeyRing, chainNotifier, aliceWalletController, aliceKeyRing,
aliceSigner, bio, aliceSigner, bio,
) )
require.NoError(t, err, "unable to create test ln wallet")
defer alice.Shutdown()
bob, err := createTestWallet( bob := createTestWallet(
tempTestDirBob, miningNode, netParams, t, tempTestDirBob, miningNode, netParams,
chainNotifier, bobWalletController, bobKeyRing, bobSigner, bio, chainNotifier, bobWalletController, bobKeyRing, bobSigner, bio,
) )
require.NoError(t, err, "unable to create test ln wallet")
defer bob.Shutdown()
// Both wallets should now have 80BTC available for // Both wallets should now have 80BTC available for
// spending. // spending.

View File

@ -243,21 +243,8 @@ func CreateTestChannels(t *testing.T, chanType channeldb.ChannelType,
return nil, nil, err return nil, nil, err
} }
dbAlice, err := channeldb.Open(t.TempDir(), dbModifiers...) dbAlice := channeldb.OpenForTesting(t, t.TempDir(), dbModifiers...)
if err != nil { dbBob := channeldb.OpenForTesting(t, t.TempDir(), dbModifiers...)
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbAlice.Close())
})
dbBob, err := channeldb.Open(t.TempDir(), dbModifiers...)
if err != nil {
return nil, nil, err
}
t.Cleanup(func() {
require.NoError(t, dbBob.Close())
})
estimator := chainfee.NewStaticEstimator(6000, 0) estimator := chainfee.NewStaticEstimator(6000, 0)
feePerKw, err := estimator.EstimateFeePerKW(1) feePerKw, err := estimator.EstimateFeePerKW(1)

View File

@ -914,11 +914,8 @@ func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelTyp
) )
// Create temporary databases. // Create temporary databases.
dbRemote, err := channeldb.Open(t.TempDir()) dbRemote := channeldb.OpenForTesting(t, t.TempDir())
require.NoError(t, err) dbLocal := channeldb.OpenForTesting(t, t.TempDir())
dbLocal, err := channeldb.Open(t.TempDir())
require.NoError(t, err)
// Create the initial commitment transactions for the channel. // Create the initial commitment transactions for the channel.
feePerKw := chainfee.SatPerKWeight(feeRate) feePerKw := chainfee.SatPerKWeight(feeRate)

View File

@ -203,13 +203,7 @@ func createTestPeerWithChannel(t *testing.T, updateChan func(a,
return nil, err return nil, err
} }
dbBob, err := channeldb.Open(t.TempDir()) dbBob := channeldb.OpenForTesting(t, t.TempDir())
if err != nil {
return nil, err
}
t.Cleanup(func() {
require.NoError(t, dbBob.Close())
})
feePerKw, err := estimator.EstimateFeePerKW(1) feePerKw, err := estimator.EstimateFeePerKW(1)
if err != nil { if err != nil {
@ -624,11 +618,7 @@ func createTestPeer(t *testing.T) *peerTestCtx {
dbAliceGraph, err := graphdb.NewChannelGraph(graphBackend) dbAliceGraph, err := graphdb.NewChannelGraph(graphBackend)
require.NoError(t, err) require.NoError(t, err)
dbAliceChannel, err := channeldb.Open(dbPath) dbAliceChannel := channeldb.OpenForTesting(t, dbPath)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, dbAliceChannel.Close())
})
nodeSignerAlice := netann.NewNodeSigner(aliceKeySigner) nodeSignerAlice := netann.NewNodeSigner(aliceKeySigner)

View File

@ -47,16 +47,13 @@ var (
func TestControlTowerSubscribeUnknown(t *testing.T) { func TestControlTowerSubscribeUnknown(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB(t, false) db := initDB(t, false)
require.NoError(t, err, "unable to init db")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
// Subscription should fail when the payment is not known. // Subscription should fail when the payment is not known.
_, err = pControl.SubscribePayment(lntypes.Hash{1}) _, err := pControl.SubscribePayment(lntypes.Hash{1})
if err != channeldb.ErrPaymentNotInitiated { require.ErrorIs(t, err, channeldb.ErrPaymentNotInitiated)
t.Fatal("expected subscribe to fail for unknown payment")
}
} }
// TestControlTowerSubscribeSuccess tests that payment updates for a // TestControlTowerSubscribeSuccess tests that payment updates for a
@ -64,8 +61,7 @@ func TestControlTowerSubscribeUnknown(t *testing.T) {
func TestControlTowerSubscribeSuccess(t *testing.T) { func TestControlTowerSubscribeSuccess(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB(t, false) db := initDB(t, false)
require.NoError(t, err, "unable to init db")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
@ -184,8 +180,7 @@ func TestPaymentControlSubscribeFail(t *testing.T) {
func TestPaymentControlSubscribeAllSuccess(t *testing.T) { func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB(t, true) db := initDB(t, true)
require.NoError(t, err, "unable to init db: %v")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
@ -298,8 +293,7 @@ func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
func TestPaymentControlSubscribeAllImmediate(t *testing.T) { func TestPaymentControlSubscribeAllImmediate(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB(t, true) db := initDB(t, true)
require.NoError(t, err, "unable to init db: %v")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
@ -336,8 +330,7 @@ func TestPaymentControlSubscribeAllImmediate(t *testing.T) {
func TestPaymentControlUnsubscribeSuccess(t *testing.T) { func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
t.Parallel() t.Parallel()
db, err := initDB(t, true) db := initDB(t, true)
require.NoError(t, err, "unable to init db: %v")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
@ -406,8 +399,7 @@ func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
func testPaymentControlSubscribeFail(t *testing.T, registerAttempt, func testPaymentControlSubscribeFail(t *testing.T, registerAttempt,
keepFailedPaymentAttempts bool) { keepFailedPaymentAttempts bool) {
db, err := initDB(t, keepFailedPaymentAttempts) db := initDB(t, keepFailedPaymentAttempts)
require.NoError(t, err, "unable to init db")
pControl := NewControlTower(channeldb.NewPaymentControl(db)) pControl := NewControlTower(channeldb.NewPaymentControl(db))
@ -525,17 +517,12 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt,
} }
} }
func initDB(t *testing.T, keepFailedPaymentAttempts bool) (*channeldb.DB, error) { func initDB(t *testing.T, keepFailedPaymentAttempts bool) *channeldb.DB {
db, err := channeldb.Open( return channeldb.OpenForTesting(
t.TempDir(), channeldb.OptionKeepFailedPaymentAttempts( t, t.TempDir(), channeldb.OptionKeepFailedPaymentAttempts(
keepFailedPaymentAttempts, keepFailedPaymentAttempts,
), ),
) )
if err != nil {
return nil, err
}
return db, err
} }
func genInfo() (*channeldb.PaymentCreationInfo, *channeldb.HTLCAttemptInfo, func genInfo() (*channeldb.PaymentCreationInfo, *channeldb.HTLCAttemptInfo,

View File

@ -41,11 +41,7 @@ func (m *mockDataParser) InlineParseCustomData(msg proto.Message) error {
func TestAuxDataParser(t *testing.T) { func TestAuxDataParser(t *testing.T) {
// We create an empty channeldb, so we can fetch some channels. // We create an empty channeldb, so we can fetch some channels.
cdb, err := channeldb.Open(t.TempDir()) cdb := channeldb.OpenForTesting(t, t.TempDir())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, cdb.Close())
})
r := &rpcServer{ r := &rpcServer{
server: &server{ server: &server{