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

@@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"sync"
"github.com/btcsuite/btcd/btcutil"
@@ -48,19 +47,6 @@ type htlcTimeoutResolver struct {
// htlc contains information on the htlc that we are resolving on-chain.
htlc channeldb.HTLC
// channelInitiator denotes whether the party responsible for resolving
// the contract initiated the channel.
channelInitiator bool
// leaseExpiry denotes the additional waiting period the contract must
// hold until it can be resolved. This waiting period is known as the
// expiration of a script-enforced leased channel and only applies to
// the channel initiator.
//
// NOTE: This value should only be set when the contract belongs to a
// leased channel.
leaseExpiry uint32
// currentReport stores the current state of the resolver for reporting
// over the rpc interface. This should only be reported in case we have
// a non-nil SignDetails on the htlcResolution, otherwise the nursery
@@ -71,6 +57,8 @@ type htlcTimeoutResolver struct {
reportLock sync.Mutex
contractResolverKit
htlcLeaseResolver
}
// newTimeoutResolver instantiates a new timeout htlc resolver.
@@ -444,13 +432,9 @@ func (h *htlcTimeoutResolver) handleCommitSpend(
// the CSV and possible CLTV lock to expire, before sweeping the output
// on the second-level.
case h.htlcResolution.SignDetails != nil:
waitHeight := uint32(commitSpend.SpendingHeight) +
h.htlcResolution.CsvDelay - 1
if h.hasCLTV() {
waitHeight = uint32(math.Max(
float64(waitHeight), float64(h.leaseExpiry),
))
}
waitHeight := h.deriveWaitHeight(
h.htlcResolution.CsvDelay, commitSpend,
)
h.reportLock.Lock()
h.currentReport.Stage = 2
@@ -483,27 +467,13 @@ func (h *htlcTimeoutResolver) handleCommitSpend(
// Let the sweeper sweep the second-level output now that the
// CSV/CLTV locks have expired.
var inp *input.BaseInput
if h.hasCLTV() {
log.Infof("%T(%x): CSV and CLTV locks expired, offering "+
"second-layer output to sweeper: %v", h,
h.htlc.RHash[:], op)
inp = input.NewCsvInputWithCltv(
op, input.LeaseHtlcOfferedTimeoutSecondLevel,
&h.htlcResolution.SweepSignDesc,
h.broadcastHeight, h.htlcResolution.CsvDelay,
h.leaseExpiry,
)
} else {
log.Infof("%T(%x): CSV lock expired, offering "+
"second-layer output to sweeper: %v", h,
h.htlc.RHash[:], op)
inp = input.NewCsvInput(
op, input.HtlcOfferedTimeoutSecondLevel,
&h.htlcResolution.SweepSignDesc,
h.broadcastHeight, h.htlcResolution.CsvDelay,
)
}
inp := h.makeSweepInput(
op, input.HtlcOfferedTimeoutSecondLevel,
input.LeaseHtlcOfferedTimeoutSecondLevel,
&h.htlcResolution.SweepSignDesc,
h.htlcResolution.CsvDelay, h.broadcastHeight,
h.htlc.RHash,
)
_, err = h.Sweeper.SweepInput(
inp,
sweep.Params{
@@ -716,23 +686,6 @@ func (h *htlcTimeoutResolver) 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 *htlcTimeoutResolver) SupplementState(state *channeldb.OpenChannel) {
if state.ChanType.HasLeaseExpiration() {
h.leaseExpiry = state.ThawHeight
}
h.channelInitiator = state.IsInitiator
}
// hasCLTV denotes whether the resolver must wait for an additional CLTV to
// expire before resolving the contract.
func (h *htlcTimeoutResolver) hasCLTV() bool {
return h.channelInitiator && h.leaseExpiry > 0
}
// HtlcPoint returns the htlc's outpoint on the commitment tx.
//
// NOTE: Part of the htlcContractResolver interface.