contractcourt: unify the lease specific HTLC sweeping logic

In this commit, we consolidate the _lease specific_ logic for the
success and timeout HTLC resolvers. We do this with the addition of a
new struct which is then composed via struct embedding with the two
existing structs. This fixes a flake in the integration tests by
ensuring the height is set up front, rather than eventually once the
height matches the lock time.
This commit is contained in:
Olaoluwa Osuntokun
2022-05-31 16:53:06 -07:00
parent 199f9d1139
commit 9b0718ae35
7 changed files with 127 additions and 95 deletions

View File

@@ -67,6 +67,8 @@ type htlcSuccessResolver struct {
reportLock sync.Mutex
contractResolverKit
htlcLeaseResolver
}
// newSuccessResolver instanties a new htlc success resolver.
@@ -292,9 +294,12 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
}
}
// The HTLC success tx has a CSV lock that we must wait for.
waitHeight := uint32(commitSpend.SpendingHeight) +
h.htlcResolution.CsvDelay - 1
// The HTLC success tx has a CSV lock that we must wait for, and if
// this is a lease enforced channel and we're the imitator, we may need
// to wait for longer.
waitHeight := h.deriveWaitHeight(
h.htlcResolution.CsvDelay, commitSpend,
)
// Now that the sweeper has broadcasted the second-level transaction,
// it has confirmed, and we have checkpointed our state, we'll sweep
@@ -305,8 +310,14 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
h.currentReport.MaturityHeight = waitHeight
h.reportLock.Unlock()
log.Infof("%T(%x): waiting for CSV lock to expire at height %v",
h, h.htlc.RHash[:], waitHeight)
if h.hasCLTV() {
log.Infof("%T(%x): waiting for CSV and CLTV lock to "+
"expire at height %v", h, h.htlc.RHash[:],
waitHeight)
} else {
log.Infof("%T(%x): waiting for CSV lock to expire at "+
"height %v", h, h.htlc.RHash[:], waitHeight)
}
err := waitForHeight(waitHeight, h.Notifier, h.quit)
if err != nil {
@@ -327,10 +338,14 @@ func (h *htlcSuccessResolver) broadcastReSignedSuccessTx() (
log.Infof("%T(%x): CSV lock expired, offering second-layer "+
"output to sweeper: %v", h, h.htlc.RHash[:], op)
inp := input.NewCsvInput(
// Let the sweeper sweep the second-level output now that the
// CSV/CLTV locks have expired.
inp := h.makeSweepInput(
op, input.HtlcAcceptedSuccessSecondLevel,
&h.htlcResolution.SweepSignDesc, h.broadcastHeight,
h.htlcResolution.CsvDelay,
input.LeaseHtlcAcceptedSuccessSecondLevel,
&h.htlcResolution.SweepSignDesc,
h.htlcResolution.CsvDelay, h.broadcastHeight,
h.htlc.RHash,
)
_, err = h.Sweeper.SweepInput(
inp,
@@ -626,13 +641,6 @@ func (h *htlcSuccessResolver) Supplement(htlc channeldb.HTLC) {
h.htlc = htlc
}
// SupplementState allows the user of a ContractResolver to supplement it with
// state required for the proper resolution of a contract.
//
// NOTE: Part of the ContractResolver interface.
func (h *htlcSuccessResolver) SupplementState(_ *channeldb.OpenChannel) {
}
// HtlcPoint returns the htlc's outpoint on the commitment tx.
//
// NOTE: Part of the htlcContractResolver interface.