@@ -596,33 +596,34 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
596
596
return nil
597
597
}
598
598
599
- // Try to spend htlc and continue (rbf) until a spend has confirmed.
600
- spend , err := s .waitForHtlcSpendConfirmedV2 (
599
+ // Try to spend htlc and continue (rbf) until a spend has fully
600
+ // confirmed.
601
+ conf , err := s .waitForHtlcSpendConfirmedV2 (
601
602
globalCtx , * htlcOutpoint , htlcValue ,
602
603
)
603
604
if err != nil {
604
605
return err
605
606
}
606
607
607
- // If spend details are nil, we resolved the swap without waiting for
608
- // its spend , so we can exit.
609
- if spend == nil {
608
+ // If conf details are nil, we resolved the swap without waiting for
609
+ // its confirmation , so we can exit.
610
+ if conf == nil {
610
611
return nil
611
612
}
612
613
613
614
// Inspect witness stack to see if it is a success transaction. We
614
615
// don't just try to match with the hash of our sweep tx, because it
615
616
// may be swept by a different (fee) sweep tx from a previous run.
616
617
htlcInput , err := swap .GetTxInputByOutpoint (
617
- spend .Tx , htlcOutpoint ,
618
+ conf .Tx , htlcOutpoint ,
618
619
)
619
620
if err != nil {
620
621
return err
621
622
}
622
623
623
624
sweepSuccessful := s .htlc .IsSuccessWitness (htlcInput .Witness )
624
625
if sweepSuccessful {
625
- s .cost .Onchain = spend .OnChainFeePortion
626
+ s .cost .Onchain = conf .OnChainFeePortion
626
627
s .state = loopdb .StateSuccess
627
628
} else {
628
629
s .state = loopdb .StateFailSweepTimeout
@@ -1140,10 +1141,12 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
1140
1141
// sweep or a server revocation tx.
1141
1142
func (s * loopOutSwap ) waitForHtlcSpendConfirmedV2 (globalCtx context.Context ,
1142
1143
htlcOutpoint wire.OutPoint , htlcValue btcutil.Amount ) (
1143
- * sweepbatcher.SpendDetail , error ) {
1144
+ * sweepbatcher.ConfDetail , error ) {
1144
1145
1145
1146
spendChan := make (chan * sweepbatcher.SpendDetail )
1146
1147
spendErrChan := make (chan error , 1 )
1148
+ confChan := make (chan * sweepbatcher.ConfDetail , 1 )
1149
+ confErrChan := make (chan error , 1 )
1147
1150
quitChan := make (chan bool , 1 )
1148
1151
1149
1152
defer func () {
@@ -1153,6 +1156,8 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
1153
1156
notifier := sweepbatcher.SpendNotifier {
1154
1157
SpendChan : spendChan ,
1155
1158
SpendErrChan : spendErrChan ,
1159
+ ConfChan : confChan ,
1160
+ ConfErrChan : confErrChan ,
1156
1161
QuitChan : quitChan ,
1157
1162
}
1158
1163
@@ -1192,15 +1197,28 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
1192
1197
1193
1198
for {
1194
1199
select {
1195
- // Htlc spend, break loop.
1200
+ // Htlc spend, but waiting for more confirmations in case of
1201
+ // a reorg.
1196
1202
case spend := <- spendChan :
1197
1203
s .log .Infof ("Htlc spend by tx: %v" , spend .Tx .TxHash ())
1198
1204
1199
- return spend , nil
1200
-
1201
1205
// Spend notification error.
1202
1206
case err := <- spendErrChan :
1203
- return nil , err
1207
+ return nil , fmt .Errorf ("spend notification error: %w" ,
1208
+ err )
1209
+
1210
+ // Htlc was spent and the sweep transaction is fully confirmed.
1211
+ // Break from loop.
1212
+ case conf := <- confChan :
1213
+ s .log .Infof ("Sweep tx %v fully confirmed in block %d" ,
1214
+ conf .Tx .TxHash (), conf .BlockHeight )
1215
+
1216
+ return conf , nil
1217
+
1218
+ // Conf notification error.
1219
+ case err := <- confErrChan :
1220
+ return nil , fmt .Errorf ("conf notification error: %w" ,
1221
+ err )
1204
1222
1205
1223
// Receive status updates for our payment so that we can detect
1206
1224
// whether we've successfully pushed our preimage.
0 commit comments