From 5b1e64abdef432098f9e9ecf4eddfaf87e294dbb Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 5 Jul 2021 08:56:23 +0200 Subject: [PATCH] wtxmgr: filter out expired leases Expired leases are kept in the database until the next cleaning round. This commit makes sure that expired leases look like they do not exist to outside callers. --- wtxmgr/tx.go | 6 ++++++ wtxmgr/tx_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/wtxmgr/tx.go b/wtxmgr/tx.go index 19c5fc2..bea60e1 100644 --- a/wtxmgr/tx.go +++ b/wtxmgr/tx.go @@ -1240,6 +1240,12 @@ func (s *Store) ListLockedOutputs(ns walletdb.ReadBucket) ([]*LockedOutput, var outputs []*LockedOutput err := forEachLockedOutput( ns, func(op wire.OutPoint, id LockID, expiration time.Time) { + // Skip expired leases. They will be cleaned up with the + // next call to DeleteExpiredLockedOutputs. + if !s.clock.Now().Before(expiration) { + return + } + outputs = append(outputs, &LockedOutput{ Outpoint: op, LockID: id, diff --git a/wtxmgr/tx_test.go b/wtxmgr/tx_test.go index 855dc3b..967b6cb 100644 --- a/wtxmgr/tx_test.go +++ b/wtxmgr/tx_test.go @@ -2780,6 +2780,16 @@ func TestOutputLocks(t *testing.T) { // Let the output lock expired. s.clock.(*clock.TestClock).SetTime(expiry) + // Lock should not longer be listed. + assertOutputLocksExist(t, s, ns) + + // But the lock should still exist and active + // when time is turned back. + assertLocked( + t, ns, confirmedOutPoint, time.Time{}, + true, + ) + // Delete all expired locked outputs. We should // no longer see any locked outputs. err = s.DeleteExpiredLockedOutputs(ns) @@ -2788,6 +2798,10 @@ func TestOutputLocks(t *testing.T) { "locked outputs: %v", err) } assertOutputLocksExist(t, s, ns) + assertLocked( + t, ns, confirmedOutPoint, time.Time{}, + false, + ) }, }, }