watchtower: use bbolt db instead of mock DB for client tests

The watchtower client test framework currently uses a mock version of
the tower client DB. This can lead to bugs if the mock DB works slightly
differently to the actual bbolt DB. So this commit ensures that we only
use the bbolt db for the tower client tests. We also increment the
`waitTime` used in the tests a bit to account for the slightly longer DB
read and write times. Doing this switch resulted in one bug being
caught: we were not removing sessions from the in-memory set on deletion
of the session and so that is fixed here too.
This commit is contained in:
Elle Mouton committed 2023-09-13 12:13:22 +02:00
1 parent fd58cbfe6b
commit f889c9b1cc
2 files changed
+36 -5

No files matched your search

+13
View File
@@ -977,6 +977,19 @@ func (c *TowerClient) handleClosableSessions(
// and handle it. // and handle it.
c.closableSessionQueue.Pop() c.closableSessionQueue.Pop()
// Stop the session and remove it from the
// in-memory set.
err := c.activeSessions.StopAndRemove(
item.sessionID,
)
if err != nil {
c.log.Errorf("could not remove "+
"session(%s) from in-memory "+
"set: %v", item.sessionID, err)
return
}
// Fetch the session from the DB so that we can // Fetch the session from the DB so that we can
// extract the Tower info. // extract the Tower info.
sess, err := c.cfg.DB.GetClientSession( sess, err := c.cfg.DB.GetClientSession(
+23 -5
View File
@@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/channelnotifier" "github.com/lightningnetwork/lnd/channelnotifier"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -72,7 +73,7 @@ var (
addrScript, _ = txscript.PayToAddrScript(addr) addrScript, _ = txscript.PayToAddrScript(addr)
waitTime = 5 * time.Second waitTime = 15 * time.Second
defaultTxPolicy = wtpolicy.TxPolicy{ defaultTxPolicy = wtpolicy.TxPolicy{
BlobType: blob.TypeAltruistCommit, BlobType: blob.TypeAltruistCommit,
@@ -398,7 +399,7 @@ type testHarness struct {
cfg harnessCfg cfg harnessCfg
signer *wtmock.MockSigner signer *wtmock.MockSigner
capacity lnwire.MilliSatoshi capacity lnwire.MilliSatoshi
clientDB *wtmock.ClientDB clientDB *wtdb.ClientDB
clientCfg *wtclient.Config clientCfg *wtclient.Config
client wtclient.Client client wtclient.Client
server *serverHarness server *serverHarness
@@ -426,10 +427,26 @@ type harnessCfg struct {
noServerStart bool noServerStart bool
} }
func newClientDB(t *testing.T) *wtdb.ClientDB {
dbCfg := &kvdb.BoltConfig{
DBTimeout: kvdb.DefaultDBTimeout,
}
// Construct the ClientDB.
dir := t.TempDir()
bdb, err := wtdb.NewBoltBackendCreator(true, dir, "wtclient.db")(dbCfg)
require.NoError(t, err)
clientDB, err := wtdb.OpenClientDB(bdb)
require.NoError(t, err)
return clientDB
}
func newHarness(t *testing.T, cfg harnessCfg) *testHarness { func newHarness(t *testing.T, cfg harnessCfg) *testHarness {
signer := wtmock.NewMockSigner() signer := wtmock.NewMockSigner()
mockNet := newMockNet() mockNet := newMockNet()
clientDB := wtmock.NewClientDB() clientDB := newClientDB(t)
server := newServerHarness( server := newServerHarness(
t, mockNet, towerAddrStr, func(serverCfg *wtserver.Config) { t, mockNet, towerAddrStr, func(serverCfg *wtserver.Config) {
@@ -509,6 +526,7 @@ func newHarness(t *testing.T, cfg harnessCfg) *testHarness {
h.startClient() h.startClient()
t.Cleanup(func() { t.Cleanup(func() {
require.NoError(t, h.client.Stop()) require.NoError(t, h.client.Stop())
require.NoError(t, h.clientDB.Close())
}) })
h.makeChannel(0, h.cfg.localBalance, h.cfg.remoteBalance) h.makeChannel(0, h.cfg.localBalance, h.cfg.remoteBalance)
@@ -1342,7 +1360,7 @@ var clientTests = []clientTest{
// Wait for all the updates to be populated in the // Wait for all the updates to be populated in the
// server's database. // server's database.
h.server.waitForUpdates(hints, 3*time.Second) h.server.waitForUpdates(hints, waitTime)
}, },
}, },
{ {
@@ -2053,7 +2071,7 @@ var clientTests = []clientTest{
// Now stop the client and reset its database. // Now stop the client and reset its database.
require.NoError(h.t, h.client.Stop()) require.NoError(h.t, h.client.Stop())
db := wtmock.NewClientDB() db := newClientDB(h.t)
h.clientDB = db h.clientDB = db
h.clientCfg.DB = db h.clientCfg.DB = db