Skip to content

Commit 353f208

Browse files
committed
sweep: refactor IsOurTx to not return an error
Before this commit, the only error returned from `IsOurTx` is when the root bucket was not created. In that case, we should consider the tx to be not found in our db, since technically our db is empty. A future PR may consider treating our wallet as the single source of truth and query the wallet instead to check for past sweeping txns.
1 parent 8d49246 commit 353f208

File tree

5 files changed

+22
-47
lines changed

5 files changed

+22
-47
lines changed

sweep/mock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func NewMockSweeperStore() *MockSweeperStore {
2424
}
2525

2626
// IsOurTx determines whether a tx is published by us, based on its hash.
27-
func (s *MockSweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
27+
func (s *MockSweeperStore) IsOurTx(hash chainhash.Hash) bool {
2828
args := s.Called(hash)
2929

30-
return args.Bool(0), args.Error(1)
30+
return args.Bool(0)
3131
}
3232

3333
// StoreTx stores a tx we are about to publish.

sweep/store.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func deserializeTxRecord(r io.Reader) (*TxRecord, error) {
121121
type SweeperStore interface {
122122
// IsOurTx determines whether a tx is published by us, based on its
123123
// hash.
124-
IsOurTx(hash chainhash.Hash) (bool, error)
124+
IsOurTx(hash chainhash.Hash) bool
125125

126126
// StoreTx stores a tx hash we are about to publish.
127127
StoreTx(*TxRecord) error
@@ -276,15 +276,17 @@ func (s *sweeperStore) StoreTx(tr *TxRecord) error {
276276
}, func() {})
277277
}
278278

279-
// IsOurTx determines whether a tx is published by us, based on its
280-
// hash.
281-
func (s *sweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
279+
// IsOurTx determines whether a tx is published by us, based on its hash.
280+
func (s *sweeperStore) IsOurTx(hash chainhash.Hash) bool {
282281
var ours bool
283282

284283
err := kvdb.View(s.db, func(tx kvdb.RTx) error {
285284
txHashesBucket := tx.ReadBucket(txHashesBucketKey)
285+
// If the root bucket cannot be found, we consider the tx to be
286+
// not found in our db.
286287
if txHashesBucket == nil {
287-
return errNoTxHashesBucket
288+
log.Error("Tx hashes bucket not found in sweeper store")
289+
return nil
288290
}
289291

290292
ours = txHashesBucket.Get(hash[:]) != nil
@@ -294,10 +296,10 @@ func (s *sweeperStore) IsOurTx(hash chainhash.Hash) (bool, error) {
294296
ours = false
295297
})
296298
if err != nil {
297-
return false, err
299+
return false
298300
}
299301

300-
return ours, nil
302+
return ours
301303
}
302304

303305
// ListSweeps lists all the sweep transactions we have in the sweeper store.

sweep/store_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,15 @@ func TestStore(t *testing.T) {
5757
require.NoError(t, err)
5858

5959
// Assert that both txes are recognized as our own.
60-
ours, err := store.IsOurTx(tx1.TxHash())
61-
require.NoError(t, err)
60+
ours := store.IsOurTx(tx1.TxHash())
6261
require.True(t, ours, "expected tx to be ours")
6362

64-
ours, err = store.IsOurTx(tx2.TxHash())
65-
require.NoError(t, err)
63+
ours = store.IsOurTx(tx2.TxHash())
6664
require.True(t, ours, "expected tx to be ours")
6765

6866
// An different hash should be reported as not being ours.
6967
var unknownHash chainhash.Hash
70-
ours, err = store.IsOurTx(unknownHash)
71-
require.NoError(t, err)
68+
ours = store.IsOurTx(unknownHash)
7269
require.False(t, ours, "expected tx to not be ours")
7370

7471
txns, err := store.ListSweeps()

sweep/sweeper.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,12 +1394,7 @@ func (s *UtxoSweeper) handleExistingInput(input *sweepInputMessage,
13941394
func (s *UtxoSweeper) handleInputSpent(spend *chainntnfs.SpendDetail) {
13951395
// Query store to find out if we ever published this tx.
13961396
spendHash := *spend.SpenderTxHash
1397-
isOurTx, err := s.cfg.Store.IsOurTx(spendHash)
1398-
if err != nil {
1399-
log.Errorf("cannot determine if tx %v is ours: %v",
1400-
spendHash, err)
1401-
return
1402-
}
1397+
isOurTx := s.cfg.Store.IsOurTx(spendHash)
14031398

14041399
// If this isn't our transaction, it means someone else swept outputs
14051400
// that we were attempting to sweep. This can happen for anchor outputs
@@ -1879,22 +1874,7 @@ func (s *UtxoSweeper) handleBumpEvent(r *bumpResp) error {
18791874
// NOTE: It is enough to check the txid because the sweeper will create
18801875
// outpoints which solely belong to the internal LND wallet.
18811876
func (s *UtxoSweeper) IsSweeperOutpoint(op wire.OutPoint) bool {
1882-
found, err := s.cfg.Store.IsOurTx(op.Hash)
1883-
// In case there is an error fetching the transaction details from the
1884-
// sweeper store we assume the outpoint is still used by the sweeper
1885-
// (worst case scenario).
1886-
//
1887-
// TODO(ziggie): Ensure that confirmed outpoints are deleted from the
1888-
// bucket.
1889-
if err != nil && !errors.Is(err, errNoTxHashesBucket) {
1890-
log.Errorf("failed to fetch info for outpoint(%v:%d) "+
1891-
"with: %v, we assume it is still in use by the sweeper",
1892-
op.Hash, op.Index, err)
1893-
1894-
return true
1895-
}
1896-
1897-
return found
1877+
return s.cfg.Store.IsOurTx(op.Hash)
18981878
}
18991879

19001880
// markInputSwept marks the given input as swept by the tx. It will also notify
@@ -1923,11 +1903,7 @@ func (s *UtxoSweeper) handleUnknownSpendTx(inp *SweeperInput, tx *wire.MsgTx) {
19231903
op := inp.OutPoint()
19241904
txid := tx.TxHash()
19251905

1926-
isOurTx, err := s.cfg.Store.IsOurTx(txid)
1927-
if err != nil {
1928-
log.Errorf("Cannot determine if tx %v is ours: %v", txid, err)
1929-
return
1930-
}
1906+
isOurTx := s.cfg.Store.IsOurTx(txid)
19311907

19321908
// If this is our tx, it means it's a previous sweeping tx that got
19331909
// confirmed, which could happen when a restart happens during the
@@ -1955,7 +1931,7 @@ func (s *UtxoSweeper) handleUnknownSpendTx(inp *SweeperInput, tx *wire.MsgTx) {
19551931
spentInputs[txIn.PreviousOutPoint] = struct{}{}
19561932
}
19571933

1958-
err = s.removeConflictSweepDescendants(spentInputs)
1934+
err := s.removeConflictSweepDescendants(spentInputs)
19591935
if err != nil {
19601936
log.Warnf("unable to remove descendant transactions "+
19611937
"due to tx %v: ", txid)

sweep/sweeper_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ func TestHandleUnknownSpendTxOurs(t *testing.T) {
12271227
txid := tx.TxHash()
12281228

12291229
// Mock the store to return true when calling IsOurTx.
1230-
store.On("IsOurTx", txid).Return(true, nil).Once()
1230+
store.On("IsOurTx", txid).Return(true).Once()
12311231

12321232
// Call the method under test.
12331233
s.handleUnknownSpendTx(si, tx)
@@ -1271,7 +1271,7 @@ func TestHandleInputSpendTxThirdParty(t *testing.T) {
12711271
txid := tx.TxHash()
12721272

12731273
// Mock the store to return false when calling IsOurTx.
1274-
store.On("IsOurTx", txid).Return(false, nil).Once()
1274+
store.On("IsOurTx", txid).Return(false).Once()
12751275

12761276
// Mock `ListSweeps` to return an empty slice as we are testing the
12771277
// workflow here, not the method `removeConflictSweepDescendants`.
@@ -1333,7 +1333,7 @@ func TestHandleBumpEventTxUnknownSpendNoRetry(t *testing.T) {
13331333
}
13341334

13351335
// Mock the store to return true when calling IsOurTx.
1336-
store.On("IsOurTx", txid).Return(true, nil).Once()
1336+
store.On("IsOurTx", txid).Return(true).Once()
13371337

13381338
// Call the method under test.
13391339
s.handleBumpEventTxUnknownSpend(resp)
@@ -1419,7 +1419,7 @@ func TestHandleBumpEventTxUnknownSpendWithRetry(t *testing.T) {
14191419
}
14201420

14211421
// Mock the store to return true when calling IsOurTx.
1422-
store.On("IsOurTx", txid).Return(true, nil).Once()
1422+
store.On("IsOurTx", txid).Return(true).Once()
14231423

14241424
// Mock the aggregator to return an empty slice as we are not testing
14251425
// the actual sweeping behavior.

0 commit comments

Comments
 (0)