Skip to content

Commit

Permalink
sweep: rename methods for clarity
Browse files Browse the repository at this point in the history
We now rename "third party" to "unknown" as the inputs can be spent via
an older sweeping tx, a third party (anchor), or a remote party (pin).
In fee bumper we don't have the info to distinguish the above cases, and
leave them to be further handled by the sweeper as it has more context.
  • Loading branch information
yyforyongyu committed Feb 7, 2025
1 parent bce9814 commit cb67094
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
41 changes: 19 additions & 22 deletions sweep/fee_bumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ var (
// preparation, usually due to the output being dust.
ErrTxNoOutput = errors.New("tx has no output")

// ErrThirdPartySpent is returned when a third party has spent the
// input in the sweeping tx.
ErrThirdPartySpent = errors.New("third party spent the output")
// ErrUnknownSpent is returned when an unknown tx has spent an input in
// the sweeping tx.
ErrUnknownSpent = errors.New("unknown spend of input")
)

var (
Expand Down Expand Up @@ -81,10 +81,6 @@ const (
// bumper. In either case the inputs in this tx should be retried with
// either a different grouping strategy or an increased budget.
//
// NOTE: We also send this event when there's a third party spend
// event, and the sweeper will handle cleaning this up once it's
// confirmed.
//
// TODO(yy): Remove the above usage once we remove sweeping non-CPFP
// anchors.
TxFailed
Expand Down Expand Up @@ -929,7 +925,7 @@ func (t *TxPublisher) processRecords() {

// Check whether the inputs has been spent by a unknown
// tx.
if t.isThirdPartySpent(r, spends) {
if t.isUnknownSpent(r, spends) {
failedRecords[requestID] = r

// Move to the next record.
Expand Down Expand Up @@ -989,7 +985,7 @@ func (t *TxPublisher) processRecords() {
// result.
for _, r := range failedRecords {
t.wg.Add(1)
go t.handleThirdPartySpent(r)
go t.handleUnknownSpent(r)
}
}

Expand Down Expand Up @@ -1151,12 +1147,12 @@ func (t *TxPublisher) handleFeeBumpTx(r *monitorRecord, currentHeight int32) {
})
}

// handleThirdPartySpent is called when the inputs in an unconfirmed tx is
// spent. It will notify the subscriber then remove the record from the maps
// and send a TxFailed event to the subscriber.
// handleUnknownSpent is called when the inputs are spent by a unknown tx. It
// will notify the subscriber then remove the record from the maps and send a
// TxUnknownSpend event to the subscriber.
//
// NOTE: Must be run as a goroutine to avoid blocking on sending the result.
func (t *TxPublisher) handleThirdPartySpent(r *monitorRecord) {
func (t *TxPublisher) handleUnknownSpent(r *monitorRecord) {
defer t.wg.Done()

log.Debugf("Record %v has inputs spent by a tx unknown to the fee "+
Expand All @@ -1169,7 +1165,7 @@ func (t *TxPublisher) handleThirdPartySpent(r *monitorRecord) {
Event: TxUnknownSpend,
Tx: r.tx,
requestID: r.requestID,
Err: ErrThirdPartySpent,
Err: ErrUnknownSpent,
}

// Notify that this tx is confirmed and remove the record from the map.
Expand Down Expand Up @@ -1277,10 +1273,11 @@ func (t *TxPublisher) createAndPublishTx(
return fn.Some(*result)
}

// isThirdPartySpent checks whether the inputs of the tx has already been spent
// by a third party. When a tx is not confirmed, yet its inputs has been spent,
// then it must be spent by a different tx other than the sweeping tx here.
func (t *TxPublisher) isThirdPartySpent(r *monitorRecord,
// isUnknownSpent checks whether the inputs of the tx has already been spent by
// a tx not known to us. When a tx is not confirmed, yet its inputs has been
// spent, then it must be spent by a different tx other than the sweeping tx
// here.
func (t *TxPublisher) isUnknownSpent(r *monitorRecord,
spends map[wire.OutPoint]*wire.MsgTx) bool {

txid := r.tx.TxHash()
Expand All @@ -1290,14 +1287,14 @@ func (t *TxPublisher) isThirdPartySpent(r *monitorRecord,
for op, spendingTx := range spends {
spendingTxID := spendingTx.TxHash()

// If the spending tx is the same as the sweeping tx
// then we are good.
// If the spending tx is the same as the sweeping tx then we are
// good.
if spendingTxID == txid {
continue
}

log.Warnf("Detected third party spent of output=%v "+
"in tx=%v", op, spendingTx.TxHash())
log.Warnf("Detected unknown spend of input=%v in tx=%v", op,
spendingTx.TxHash())

return true
}
Expand Down
4 changes: 2 additions & 2 deletions sweep/fee_bumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ func TestProcessRecordsInitialSpent(t *testing.T) {
require.Nil(t, result.ReplacedTx)

// The error should be set.
require.ErrorIs(t, result.Err, ErrThirdPartySpent)
require.ErrorIs(t, result.Err, ErrUnknownSpent)
require.Equal(t, requestID, result.requestID)
}
}
Expand Down Expand Up @@ -1786,7 +1786,7 @@ func TestProcessRecordsSpent(t *testing.T) {
require.Equal(t, tx, result.Tx)

// No error should be set.
require.ErrorIs(t, result.Err, ErrThirdPartySpent)
require.ErrorIs(t, result.Err, ErrUnknownSpent)
require.Equal(t, requestID, result.requestID)
}
}
Expand Down

0 comments on commit cb67094

Please sign in to comment.