2019-01-16 20:03:59 +01:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2018-09-07 16:05:57 +02:00
|
|
|
"github.com/btcsuite/btcutil"
|
2019-11-05 14:23:15 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-11-01 11:04:28 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-01-16 20:03:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// htlcOutgoingContestResolver is a ContractResolver that's able to resolve an
|
|
|
|
// outgoing HTLC that is still contested. An HTLC is still contested, if at the
|
|
|
|
// time that we broadcast the commitment transaction, it isn't able to be fully
|
|
|
|
// resolved. In this case, we'll either wait for the HTLC to timeout, or for
|
|
|
|
// us to learn of the preimage.
|
|
|
|
type htlcOutgoingContestResolver struct {
|
|
|
|
// htlcTimeoutResolver is the inner solver that this resolver may turn
|
|
|
|
// into. This only happens if the HTLC expires on-chain.
|
|
|
|
htlcTimeoutResolver
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:04:28 +01:00
|
|
|
// newOutgoingContestResolver instantiates a new outgoing contested htlc
|
|
|
|
// resolver.
|
|
|
|
func newOutgoingContestResolver(res lnwallet.OutgoingHtlcResolution,
|
2019-11-05 14:23:15 +01:00
|
|
|
broadcastHeight uint32, htlc channeldb.HTLC,
|
2019-11-01 11:04:28 +01:00
|
|
|
resCfg ResolverConfig) *htlcOutgoingContestResolver {
|
|
|
|
|
|
|
|
timeout := newTimeoutResolver(
|
2019-11-05 14:23:15 +01:00
|
|
|
res, broadcastHeight, htlc, resCfg,
|
2019-11-01 11:04:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return &htlcOutgoingContestResolver{
|
|
|
|
htlcTimeoutResolver: *timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:03:59 +01:00
|
|
|
// Resolve commences the resolution of this contract. As this contract hasn't
|
|
|
|
// yet timed out, we'll wait for one of two things to happen
|
|
|
|
//
|
|
|
|
// 1. The HTLC expires. In this case, we'll sweep the funds and send a clean
|
|
|
|
// up cancel message to outside sub-systems.
|
|
|
|
//
|
|
|
|
// 2. The remote party sweeps this HTLC on-chain, in which case we'll add the
|
|
|
|
// pre-image to our global cache, then send a clean up settle message
|
|
|
|
// backwards.
|
|
|
|
//
|
|
|
|
// When either of these two things happens, we'll create a new resolver which
|
|
|
|
// is able to handle the final resolution of the contract. We're only the pivot
|
|
|
|
// point.
|
|
|
|
func (h *htlcOutgoingContestResolver) Resolve() (ContractResolver, error) {
|
|
|
|
// If we're already full resolved, then we don't have anything further
|
|
|
|
// to do.
|
|
|
|
if h.resolved {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll watch for two external signals to decide if we'll
|
|
|
|
// morph into another resolver, or fully resolve the contract.
|
2019-03-19 01:00:43 +01:00
|
|
|
//
|
2019-01-16 20:03:59 +01:00
|
|
|
// The output we'll be watching for is the *direct* spend from the HTLC
|
|
|
|
// output. If this isn't our commitment transaction, it'll be right on
|
|
|
|
// the resolution. Otherwise, we fetch this pointer from the input of
|
|
|
|
// the time out transaction.
|
2019-03-19 01:00:43 +01:00
|
|
|
outPointToWatch, scriptToWatch, err := h.chainDetailsToWatch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// First, we'll register for a spend notification for this output. If
|
|
|
|
// the remote party sweeps with the pre-image, we'll be notified.
|
|
|
|
spendNtfn, err := h.Notifier.RegisterSpendNtfn(
|
2019-03-19 01:00:43 +01:00
|
|
|
outPointToWatch, scriptToWatch, h.broadcastHeight,
|
2019-01-16 20:03:59 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll quickly check to see if the output has already been spent.
|
|
|
|
select {
|
|
|
|
// If the output has already been spent, then we can stop early and
|
|
|
|
// sweep the pre-image from the output.
|
|
|
|
case commitSpend, ok := <-spendNtfn.Spend:
|
|
|
|
if !ok {
|
2019-09-11 15:53:00 +02:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): Checkpoint?
|
2019-03-19 00:59:48 +01:00
|
|
|
return h.claimCleanUp(commitSpend)
|
2019-01-16 20:03:59 +01:00
|
|
|
|
|
|
|
// If it hasn't, then we'll watch for both the expiration, and the
|
|
|
|
// sweeping out this output.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach this point, then we can't fully act yet, so we'll await
|
|
|
|
// either of our signals triggering: the HTLC expires, or we learn of
|
|
|
|
// the preimage.
|
|
|
|
blockEpochs, err := h.Notifier.RegisterBlockEpochNtfn(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer blockEpochs.Cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
// A new block has arrived, we'll check to see if this leads to
|
|
|
|
// HTLC expiration.
|
|
|
|
case newBlock, ok := <-blockEpochs.Epochs:
|
|
|
|
if !ok {
|
2019-09-11 15:53:00 +02:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-01 11:40:47 +01:00
|
|
|
// If the current height is >= expiry-1, then a timeout
|
|
|
|
// path spend will be valid to be included in the next
|
|
|
|
// block, and we can immediately return the resolver.
|
|
|
|
//
|
|
|
|
// TODO(joostjager): Statement above may not be valid.
|
|
|
|
// For CLTV locks, the expiry value is the last
|
|
|
|
// _invalid_ block. The likely reason that this does not
|
|
|
|
// create a problem, is that utxonursery is checking the
|
|
|
|
// expiry again (in the proper way).
|
|
|
|
//
|
|
|
|
// Source:
|
|
|
|
// https://github.com/btcsuite/btcd/blob/991d32e72fe84d5fbf9c47cd604d793a0cd3a072/blockchain/validate.go#L154
|
2019-01-16 20:03:59 +01:00
|
|
|
newHeight := uint32(newBlock.Height)
|
|
|
|
if newHeight >= h.htlcResolution.Expiry-1 {
|
|
|
|
log.Infof("%T(%v): HTLC has expired "+
|
|
|
|
"(height=%v, expiry=%v), transforming "+
|
|
|
|
"into timeout resolver", h,
|
|
|
|
h.htlcResolution.ClaimOutpoint,
|
|
|
|
newHeight, h.htlcResolution.Expiry)
|
|
|
|
return &h.htlcTimeoutResolver, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The output has been spent! This means the preimage has been
|
|
|
|
// revealed on-chain.
|
|
|
|
case commitSpend, ok := <-spendNtfn.Spend:
|
|
|
|
if !ok {
|
2019-09-11 15:53:00 +02:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The only way this output can be spent by the remote
|
|
|
|
// party is by revealing the preimage. So we'll perform
|
|
|
|
// our duties to clean up the contract once it has been
|
|
|
|
// claimed.
|
2019-03-19 00:59:48 +01:00
|
|
|
return h.claimCleanUp(commitSpend)
|
2019-01-16 20:03:59 +01:00
|
|
|
|
2019-11-06 13:31:13 +01:00
|
|
|
case <-h.quit:
|
2019-10-03 17:22:43 +02:00
|
|
|
return nil, fmt.Errorf("resolver canceled")
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:05:57 +02:00
|
|
|
// report returns a report on the resolution state of the contract.
|
|
|
|
func (h *htlcOutgoingContestResolver) report() *ContractReport {
|
|
|
|
// No locking needed as these values are read-only.
|
|
|
|
|
2019-11-05 14:23:15 +01:00
|
|
|
finalAmt := h.htlc.Amt.ToSatoshis()
|
2018-09-07 16:05:57 +02:00
|
|
|
if h.htlcResolution.SignedTimeoutTx != nil {
|
|
|
|
finalAmt = btcutil.Amount(
|
|
|
|
h.htlcResolution.SignedTimeoutTx.TxOut[0].Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ContractReport{
|
|
|
|
Outpoint: h.htlcResolution.ClaimOutpoint,
|
2019-10-30 10:31:01 +01:00
|
|
|
Type: ReportOutputOutgoingHtlc,
|
2018-09-07 16:05:57 +02:00
|
|
|
Amount: finalAmt,
|
|
|
|
MaturityHeight: h.htlcResolution.Expiry,
|
|
|
|
LimboBalance: finalAmt,
|
|
|
|
Stage: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:03:59 +01:00
|
|
|
// Stop signals the resolver to cancel any current resolution processes, and
|
|
|
|
// suspend.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (h *htlcOutgoingContestResolver) Stop() {
|
2019-11-06 13:31:13 +01:00
|
|
|
close(h.quit)
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsResolved returns true if the stored state in the resolve is fully
|
|
|
|
// resolved. In this case the target output can be forgotten.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (h *htlcOutgoingContestResolver) IsResolved() bool {
|
|
|
|
return h.resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode writes an encoded version of the ContractResolver into the passed
|
|
|
|
// Writer.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (h *htlcOutgoingContestResolver) Encode(w io.Writer) error {
|
|
|
|
return h.htlcTimeoutResolver.Encode(w)
|
|
|
|
}
|
|
|
|
|
2019-11-01 10:24:33 +01:00
|
|
|
// newOutgoingContestResolverFromReader attempts to decode an encoded ContractResolver
|
|
|
|
// from the passed Reader instance, returning an active ContractResolver
|
|
|
|
// instance.
|
|
|
|
func newOutgoingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
|
|
|
*htlcOutgoingContestResolver, error) {
|
|
|
|
|
|
|
|
h := &htlcOutgoingContestResolver{}
|
|
|
|
timeoutResolver, err := newTimeoutResolverFromReader(r, resCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.htlcTimeoutResolver = *timeoutResolver
|
|
|
|
return h, nil
|
2019-01-16 20:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time assertion to ensure htlcOutgoingContestResolver meets the
|
|
|
|
// ContractResolver interface.
|
2019-11-01 12:23:48 +01:00
|
|
|
var _ htlcContractResolver = (*htlcOutgoingContestResolver)(nil)
|