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 2023-09-12 19:53:16 +02:00
parent fd58cbfe6b
commit f889c9b1cc
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 36 additions and 5 deletions

View File

@ -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(

View File

@ -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