From 0b84d5f976bc8cec8f009b8a4d626fd78e5cb6b0 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 9 Dec 2020 12:24:02 +0100 Subject: [PATCH] contractcourt/success_resolver: extract HTLC success handling into broadcastSuccessTx --- contractcourt/htlc_success_resolver.go | 51 +++++++++++++++++--------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/contractcourt/htlc_success_resolver.go b/contractcourt/htlc_success_resolver.go index 5fb034c8..9e48a351 100644 --- a/contractcourt/htlc_success_resolver.go +++ b/contractcourt/htlc_success_resolver.go @@ -108,6 +108,38 @@ func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) { return h.resolveRemoteCommitOutput() } + // Otherwise this an output on our own commitment, and we must start by + // broadcasting the second-level success transaction. + secondLevelOutpoint, err := h.broadcastSuccessTx() + if err != nil { + return nil, err + } + + // To wrap this up, we'll wait until the second-level transaction has + // been spent, then fully resolve the contract. + log.Infof("%T(%x): waiting for second-level HTLC output to be spent "+ + "after csv_delay=%v", h, h.htlc.RHash[:], h.htlcResolution.CsvDelay) + + spend, err := waitForSpend( + secondLevelOutpoint, + h.htlcResolution.SweepSignDesc.Output.PkScript, + h.broadcastHeight, h.Notifier, h.quit, + ) + if err != nil { + return nil, err + } + + h.resolved = true + return nil, h.checkpointClaim( + spend.SpenderTxHash, channeldb.ResolverOutcomeClaimed, + ) +} + +// broadcastSuccessTx handles an HTLC output on our local commitment by +// broadcasting the second-level success transaction. It returns the ultimate +// outpoint of the second-level tx, that we must wait to be spent for the +// resolver to be fully resolved. +func (h *htlcSuccessResolver) broadcastSuccessTx() (*wire.OutPoint, error) { log.Infof("%T(%x): broadcasting second-layer transition tx: %v", h, h.htlc.RHash[:], spew.Sdump(h.htlcResolution.SignedSuccessTx)) @@ -146,24 +178,7 @@ func (h *htlcSuccessResolver) Resolve() (ContractResolver, error) { } } - // To wrap this up, we'll wait until the second-level transaction has - // been spent, then fully resolve the contract. - log.Infof("%T(%x): waiting for second-level HTLC output to be spent "+ - "after csv_delay=%v", h, h.htlc.RHash[:], h.htlcResolution.CsvDelay) - - spend, err := waitForSpend( - &h.htlcResolution.ClaimOutpoint, - h.htlcResolution.SweepSignDesc.Output.PkScript, - h.broadcastHeight, h.Notifier, h.quit, - ) - if err != nil { - return nil, err - } - - h.resolved = true - return nil, h.checkpointClaim( - spend.SpenderTxHash, channeldb.ResolverOutcomeClaimed, - ) + return &h.htlcResolution.ClaimOutpoint, nil } // resolveRemoteCommitOutput handles sweeping an HTLC output on the remote