@@ -38,27 +38,33 @@ const (
3838 // We'll try to sweep with MuSig2 at most 10 times. If that fails we'll
3939 // fail back to using standard scriptspend sweep.
4040 maxMusigSweepRetries = 10
41- )
4241
43- var (
4442 // MinLoopOutPreimageRevealDelta configures the minimum number of
4543 // remaining blocks before htlc expiry required to reveal preimage.
46- MinLoopOutPreimageRevealDelta int32 = 20
44+ MinLoopOutPreimageRevealDelta = 20
4745
4846 // DefaultSweepConfTarget is the default confirmation target we'll use
4947 // when sweeping on-chain HTLCs.
50- DefaultSweepConfTarget int32 = 9
48+ DefaultSweepConfTarget = 9
5149
5250 // DefaultHtlcConfTarget is the default confirmation target we'll use
5351 // for on-chain htlcs published by the swap client for Loop In.
54- DefaultHtlcConfTarget int32 = 6
52+ DefaultHtlcConfTarget = 6
5553
5654 // DefaultSweepConfTargetDelta is the delta of blocks from a Loop Out
57- // swap's expiration height at which we begin to use the default sweep
58- // confirmation target.
59- //
60- // TODO(wilmer): tune?
61- DefaultSweepConfTargetDelta = DefaultSweepConfTarget * 2
55+ // swap's expiration height at which we begin to cap the sweep
56+ // confirmation target with urgentSweepConfTarget and multiply feerate
57+ // by factor urgentSweepConfTargetFactor.
58+ DefaultSweepConfTargetDelta = 10
59+
60+ // urgentSweepConfTarget is the confirmation target we'll use when the
61+ // loop-out swap is about to expire (<= DefaultSweepConfTargetDelta
62+ // blocks to expire).
63+ urgentSweepConfTarget = 3
64+
65+ // urgentSweepConfTargetFactor is the factor we apply to feerate of
66+ // loop-out sweep if it is about to expire.
67+ urgentSweepConfTargetFactor = 1.1
6268)
6369
6470// loopOutSwap contains all the in-memory state related to a pending loop out
@@ -1169,12 +1175,12 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
11691175 timerChan = s .timerFactory (repushDelay )
11701176
11711177 case <- timerChan :
1172- // sweepConfTarget will return false if the preimage is
1178+ // canSweep will return false if the preimage is
11731179 // not revealed yet but the conf target is closer than
11741180 // 20 blocks. In this case to be sure we won't attempt
11751181 // to sweep at all and we won't reveal the preimage
11761182 // either.
1177- _ , canSweep := s .sweepConfTarget ()
1183+ canSweep := s .canSweep ()
11781184 if ! canSweep {
11791185 s .log .Infof ("Aborting swap, timed " +
11801186 "out on-chain" )
@@ -1375,9 +1381,9 @@ func validateLoopOutContract(lnd *lndclient.LndServices, request *OutRequest,
13751381 return nil
13761382}
13771383
1378- // sweepConfTarget returns the confirmation target for the htlc sweep or false
1379- // if we're too late.
1380- func (s * loopOutSwap ) sweepConfTarget () ( int32 , bool ) {
1384+ // canSweep will return false if the preimage is not revealed yet but the conf
1385+ // target is closer than 20 blocks (i.e. it is too late to reveal the preimage) .
1386+ func (s * loopOutSwap ) canSweep () bool {
13811387 remainingBlocks := s .CltvExpiry - s .height
13821388 blocksToLastReveal := remainingBlocks - MinLoopOutPreimageRevealDelta
13831389 preimageRevealed := s .state == loopdb .StatePreimageRevealed
@@ -1393,20 +1399,8 @@ func (s *loopOutSwap) sweepConfTarget() (int32, bool) {
13931399 s .height )
13941400
13951401 s .state = loopdb .StateFailTimeout
1396- return 0 , false
1397- }
1398-
1399- // Calculate the transaction fee based on the confirmation target
1400- // required to sweep the HTLC before the timeout. We'll use the
1401- // confirmation target provided by the client unless we've come too
1402- // close to the expiration height, in which case we'll use the default
1403- // if it is better than what the client provided.
1404- confTarget := s .SweepConfTarget
1405- if remainingBlocks <= DefaultSweepConfTargetDelta &&
1406- confTarget > DefaultSweepConfTarget {
1407-
1408- confTarget = DefaultSweepConfTarget
1402+ return false
14091403 }
14101404
1411- return confTarget , true
1405+ return true
14121406}
0 commit comments