From f889c9b1cca3d006b8514dd1f3c21c2f6d75b27f Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 12 Sep 2023 19:53:16 +0200 Subject: [PATCH] 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. --- watchtower/wtclient/client.go | 13 +++++++++++++ watchtower/wtclient/client_test.go | 28 +++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/watchtower/wtclient/client.go b/watchtower/wtclient/client.go index f3b8d3307..412412c1e 100644 --- a/watchtower/wtclient/client.go +++ b/watchtower/wtclient/client.go @@ -977,6 +977,19 @@ func (c *TowerClient) handleClosableSessions( // and handle it. 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 // extract the Tower info. sess, err := c.cfg.DB.GetClientSession( diff --git a/watchtower/wtclient/client_test.go b/watchtower/wtclient/client_test.go index 5cb998c9a..a5b774c10 100644 --- a/watchtower/wtclient/client_test.go +++ b/watchtower/wtclient/client_test.go @@ -21,6 +21,7 @@ import ( "github.com/lightningnetwork/lnd/channelnotifier" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/keychain" + "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" @@ -72,7 +73,7 @@ var ( addrScript, _ = txscript.PayToAddrScript(addr) - waitTime = 5 * time.Second + waitTime = 15 * time.Second defaultTxPolicy = wtpolicy.TxPolicy{ BlobType: blob.TypeAltruistCommit, @@ -398,7 +399,7 @@ type testHarness struct { cfg harnessCfg signer *wtmock.MockSigner capacity lnwire.MilliSatoshi - clientDB *wtmock.ClientDB + clientDB *wtdb.ClientDB clientCfg *wtclient.Config client wtclient.Client server *serverHarness @@ -426,10 +427,26 @@ type harnessCfg struct { 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 { signer := wtmock.NewMockSigner() mockNet := newMockNet() - clientDB := wtmock.NewClientDB() + clientDB := newClientDB(t) server := newServerHarness( t, mockNet, towerAddrStr, func(serverCfg *wtserver.Config) { @@ -509,6 +526,7 @@ func newHarness(t *testing.T, cfg harnessCfg) *testHarness { h.startClient() t.Cleanup(func() { require.NoError(t, h.client.Stop()) + require.NoError(t, h.clientDB.Close()) }) 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 // 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. require.NoError(h.t, h.client.Stop()) - db := wtmock.NewClientDB() + db := newClientDB(h.t) h.clientDB = db h.clientCfg.DB = db