From 92671ad250566c9bcd1f47b864c06b46777ed4ca Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 9 Aug 2023 22:34:38 +0800 Subject: [PATCH] contractcourt: register conf notification before publish tx This commit moves the confirmation registration before publishing the tx to make sure the conf event won't be missed. --- contractcourt/htlc_success_resolver.go | 34 ++++++++++++++------------ 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/contractcourt/htlc_success_resolver.go b/contractcourt/htlc_success_resolver.go index 5d30b365a..26a906ce4 100644 --- a/contractcourt/htlc_success_resolver.go +++ b/contractcourt/htlc_success_resolver.go @@ -417,20 +417,8 @@ func (h *htlcSuccessResolver) resolveRemoteCommitOutput() ( // with the published one. } - // Regardless of whether an existing transaction was found or newly - // constructed, we'll broadcast the sweep transaction to the network. - label := labels.MakeLabel( - labels.LabelTypeChannelClose, &h.ShortChanID, - ) - err := h.PublishTx(h.sweepTx, label) - if err != nil { - log.Infof("%T(%x): unable to publish tx: %v", - h, h.htlc.RHash[:], err) - return nil, err - } - - // With the sweep transaction broadcast, we'll wait for its - // confirmation. + // Register the confirmation notification before broadcasting the sweep + // transaction. sweepTXID := h.sweepTx.TxHash() sweepScript := h.sweepTx.TxOut[0].PkScript confNtfn, err := h.Notifier.RegisterConfirmationsNtfn( @@ -440,8 +428,22 @@ func (h *htlcSuccessResolver) resolveRemoteCommitOutput() ( return nil, err } - log.Infof("%T(%x): waiting for sweep tx (txid=%v) to be "+ - "confirmed", h, h.htlc.RHash[:], sweepTXID) + // Regardless of whether an existing transaction was found or newly + // constructed, we'll broadcast the sweep transaction to the network. + label := labels.MakeLabel( + labels.LabelTypeChannelClose, &h.ShortChanID, + ) + err = h.PublishTx(h.sweepTx, label) + if err != nil { + log.Infof("%T(%x): unable to publish tx: %v", + h, h.htlc.RHash[:], err) + confNtfn.Cancel() + + return nil, err + } + + log.Infof("%T(%x): waiting for sweep tx (txid=%v) to be confirmed", h, + h.htlc.RHash[:], sweepTXID) select { case _, ok := <-confNtfn.Confirmed: