From e1c269c7edf0580ff2b93e8c747c359bdb38d6a4 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 14 Jul 2021 17:19:22 -0700 Subject: [PATCH] sweep: remove previous exclusive group upon re-offered inputs This aims to cover an edge case and also serves as an optimization of what happens when an input that was offered to the Sweeper with an exclusive group is re-offered without one. This happens every time we attempt to sweep the different possible anchors of a channel at the time of broadcast, as we don't know which commitment transaction will end up confirming in the chain. Once the commitment transaction confirms however, we know which anchor output to act upon and re-offer it to the Sweeper without an exclusive group. At this point, the Sweeper will continue to attempt sweeping the other anchor output versions even know we know they are not valid. --- sweep/sweeper.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sweep/sweeper.go b/sweep/sweeper.go index 1219a2919..2530306fe 100644 --- a/sweep/sweeper.go +++ b/sweep/sweeper.go @@ -459,9 +459,11 @@ func (s *UtxoSweeper) SweepInput(input input.Input, return nil, err } + absoluteTimeLock, _ := input.RequiredLockTime() log.Infof("Sweep request received: out_point=%v, witness_type=%v, "+ - "time_lock=%v, amount=%v, params=(%v)", - input.OutPoint(), input.WitnessType(), input.BlocksToMaturity(), + "relative_time_lock=%v, absolute_time_lock=%v, amount=%v, "+ + "params=(%v)", input.OutPoint(), input.WitnessType(), + input.BlocksToMaturity(), absoluteTimeLock, btcutil.Amount(input.SignDesc().Output.Value), params) sweeperInput := &sweepInputMessage{ @@ -534,6 +536,17 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) { log.Debugf("Already pending input %v received", outpoint) + // Before updating the input details, check if + // an exclusive group was set, and if so, assume + // this input as finalized and remove all other + // inputs belonging to the same exclusive group. + var prevExclGroup *uint64 + if pendInput.params.ExclusiveGroup != nil && + input.params.ExclusiveGroup == nil { + prevExclGroup = new(uint64) + *prevExclGroup = *pendInput.params.ExclusiveGroup + } + // Update input details and sweep parameters. // The re-offered input details may contain a // change to the unconfirmed parent tx info. @@ -545,6 +558,11 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) { pendInput.listeners = append( pendInput.listeners, input.resultChan, ) + + if prevExclGroup != nil { + s.removeExclusiveGroup(*prevExclGroup) + } + continue }