2019-01-16 22:03:59 +03:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
2019-08-26 15:06:57 +03:00
|
|
|
"bytes"
|
2019-01-16 22:03:59 +03:00
|
|
|
"encoding/binary"
|
2019-04-16 12:52:42 +03:00
|
|
|
"errors"
|
2019-01-16 22:03:59 +03:00
|
|
|
"io"
|
2018-09-07 17:05:57 +03:00
|
|
|
|
2019-11-01 13:04:28 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2019-02-11 14:01:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-08-26 15:06:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2019-02-11 14:01:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2019-02-20 04:06:00 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-11-01 13:04:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-01-16 22:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// htlcIncomingContestResolver is a ContractResolver that's able to resolve an
|
|
|
|
// incoming HTLC that is still contested. An HTLC is still contested, if at the
|
|
|
|
// time of commitment broadcast, we don't know of the preimage for it yet, and
|
|
|
|
// it hasn't expired. In this case, we can resolve the HTLC if we learn of the
|
|
|
|
// preimage, otherwise the remote party will sweep it after it expires.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): just embed the other resolver?
|
|
|
|
type htlcIncomingContestResolver struct {
|
|
|
|
// htlcExpiry is the absolute expiry of this incoming HTLC. We use this
|
|
|
|
// value to determine if we can exit early as if the HTLC times out,
|
|
|
|
// before we learn of the preimage then we can't claim it on chain
|
|
|
|
// successfully.
|
|
|
|
htlcExpiry uint32
|
|
|
|
|
|
|
|
// htlcSuccessResolver is the inner resolver that may be utilized if we
|
|
|
|
// learn of the preimage.
|
|
|
|
htlcSuccessResolver
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:04:28 +03:00
|
|
|
// newIncomingContestResolver instantiates a new incoming htlc contest resolver.
|
2019-11-05 16:23:15 +03:00
|
|
|
func newIncomingContestResolver(
|
|
|
|
res lnwallet.IncomingHtlcResolution, broadcastHeight uint32,
|
|
|
|
htlc channeldb.HTLC, resCfg ResolverConfig) *htlcIncomingContestResolver {
|
2019-11-01 13:04:28 +03:00
|
|
|
|
|
|
|
success := newSuccessResolver(
|
2019-11-05 16:23:15 +03:00
|
|
|
res, broadcastHeight, htlc, resCfg,
|
2019-11-01 13:04:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return &htlcIncomingContestResolver{
|
2019-11-05 16:23:15 +03:00
|
|
|
htlcExpiry: htlc.RefundTimeout,
|
2019-11-01 13:04:28 +03:00
|
|
|
htlcSuccessResolver: *success,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// Resolve attempts to resolve this contract. As we don't yet know of the
|
|
|
|
// preimage for the contract, we'll wait for one of two things to happen:
|
|
|
|
//
|
|
|
|
// 1. We learn of the preimage! In this case, we can sweep the HTLC incoming
|
|
|
|
// and ensure that if this was a multi-hop HTLC we are made whole. In this
|
|
|
|
// case, an additional ContractResolver will be returned to finish the
|
|
|
|
// job.
|
|
|
|
//
|
|
|
|
// 2. The HTLC expires. If this happens, then the contract is fully resolved
|
|
|
|
// as we have no remaining actions left at our disposal.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) {
|
|
|
|
// If we're already full resolved, then we don't have anything further
|
|
|
|
// to do.
|
|
|
|
if h.resolved {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:06:57 +03:00
|
|
|
// First try to parse the payload. If that fails, we can stop resolution
|
|
|
|
// now.
|
|
|
|
payload, err := h.decodePayload()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("ChannelArbitrator(%v): cannot decode payload of "+
|
|
|
|
"htlc %v", h.ChanPoint, h.HtlcPoint())
|
|
|
|
|
|
|
|
// If we've locked in an htlc with an invalid payload on our
|
|
|
|
// commitment tx, we don't need to resolve it. The other party
|
|
|
|
// will time it out and get their funds back. This situation can
|
|
|
|
// present itself when we crash before processRemoteAdds in the
|
|
|
|
// link has ran.
|
|
|
|
h.resolved = true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:05:10 +03:00
|
|
|
// Register for block epochs. After registration, the current height
|
|
|
|
// will be sent on the channel immediately.
|
|
|
|
blockEpochs, err := h.Notifier.RegisterBlockEpochNtfn(nil)
|
2019-01-16 22:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 20:05:10 +03:00
|
|
|
defer blockEpochs.Cancel()
|
|
|
|
|
|
|
|
var currentHeight int32
|
|
|
|
select {
|
|
|
|
case newBlock, ok := <-blockEpochs.Epochs:
|
|
|
|
if !ok {
|
2019-09-11 16:53:00 +03:00
|
|
|
return nil, errResolverShuttingDown
|
2019-04-15 20:05:10 +03:00
|
|
|
}
|
|
|
|
currentHeight = newBlock.Height
|
2019-11-06 15:31:13 +03:00
|
|
|
case <-h.quit:
|
2019-09-11 16:53:00 +03:00
|
|
|
return nil, errResolverShuttingDown
|
2019-04-15 20:05:10 +03:00
|
|
|
}
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-04-15 20:05:10 +03:00
|
|
|
// We'll first check if this HTLC has been timed out, if so, we can
|
|
|
|
// return now and mark ourselves as resolved. If we're past the point of
|
|
|
|
// expiry of the HTLC, then at this point the sender can sweep it, so
|
|
|
|
// we'll end our lifetime. Here we deliberately forego the chance that
|
|
|
|
// the sender doesn't sweep and we already have or will learn the
|
|
|
|
// preimage. Otherwise the resolver could potentially stay active
|
|
|
|
// indefinitely and the channel will never close properly.
|
2019-01-16 22:03:59 +03:00
|
|
|
if uint32(currentHeight) >= h.htlcExpiry {
|
|
|
|
// TODO(roasbeef): should also somehow check if outgoing is
|
|
|
|
// resolved or not
|
|
|
|
// * may need to hook into the circuit map
|
|
|
|
// * can't timeout before the outgoing has been
|
|
|
|
|
|
|
|
log.Infof("%T(%v): HTLC has timed out (expiry=%v, height=%v), "+
|
|
|
|
"abandoning", h, h.htlcResolution.ClaimOutpoint,
|
|
|
|
h.htlcExpiry, currentHeight)
|
|
|
|
h.resolved = true
|
|
|
|
return nil, h.Checkpoint(h)
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
// tryApplyPreimage is a helper function that will populate our internal
|
2019-01-16 22:03:59 +03:00
|
|
|
// resolver with the preimage we learn of. This should be called once
|
|
|
|
// the preimage is revealed so the inner resolver can properly complete
|
2019-02-11 14:01:05 +03:00
|
|
|
// its duties. The boolean return value indicates whether the preimage
|
|
|
|
// was properly applied.
|
2019-04-16 12:52:42 +03:00
|
|
|
applyPreimage := func(preimage lntypes.Preimage) error {
|
|
|
|
// Sanity check to see if this preimage matches our htlc. At
|
|
|
|
// this point it should never happen that it does not match.
|
2019-11-05 16:23:15 +03:00
|
|
|
if !preimage.Matches(h.htlc.RHash) {
|
2019-04-16 12:52:42 +03:00
|
|
|
return errors.New("preimage does not match hash")
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update htlcResolution with the matching preimage.
|
2019-02-20 04:06:00 +03:00
|
|
|
h.htlcResolution.Preimage = preimage
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-02-20 04:06:00 +03:00
|
|
|
log.Infof("%T(%v): extracted preimage=%v from beacon!", h,
|
|
|
|
h.htlcResolution.ClaimOutpoint, preimage)
|
2019-01-16 22:03:59 +03:00
|
|
|
|
|
|
|
// If this our commitment transaction, then we'll need to
|
|
|
|
// populate the witness for the second-level HTLC transaction.
|
|
|
|
if h.htlcResolution.SignedSuccessTx != nil {
|
|
|
|
// Within the witness for the success transaction, the
|
|
|
|
// preimage is the 4th element as it looks like:
|
|
|
|
//
|
|
|
|
// * <sender sig> <recvr sig> <preimage> <witness script>
|
|
|
|
//
|
|
|
|
// We'll populate it within the witness, as since this
|
|
|
|
// was a "contest" resolver, we didn't yet know of the
|
|
|
|
// preimage.
|
|
|
|
h.htlcResolution.SignedSuccessTx.TxIn[0].Witness[3] = preimage[:]
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-04-16 12:52:42 +03:00
|
|
|
return nil
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the HTLC hasn't expired yet, then we may still be able to claim
|
|
|
|
// it if we learn of the pre-image, so we'll subscribe to the preimage
|
|
|
|
// database to see if it turns up, or the HTLC times out.
|
|
|
|
//
|
|
|
|
// NOTE: This is done BEFORE opportunistically querying the db, to
|
|
|
|
// ensure the preimage can't be delivered between querying and
|
|
|
|
// registering for the preimage subscription.
|
|
|
|
preimageSubscription := h.PreimageDB.SubscribeUpdates()
|
2019-04-15 20:05:10 +03:00
|
|
|
defer preimageSubscription.CancelSubscription()
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// Define closure to process htlc resolutions either direct or triggered by
|
|
|
|
// later notification.
|
2019-12-20 13:25:07 +03:00
|
|
|
processHtlcResolution := func(e invoices.HtlcResolution) (
|
|
|
|
ContractResolver, error) {
|
2019-04-16 13:11:20 +03:00
|
|
|
|
|
|
|
if e.Preimage == nil {
|
|
|
|
log.Infof("%T(%v): Exit hop HTLC canceled "+
|
|
|
|
"(expiry=%v, height=%v), abandoning", h,
|
|
|
|
h.htlcResolution.ClaimOutpoint,
|
|
|
|
h.htlcExpiry, currentHeight)
|
|
|
|
|
|
|
|
h.resolved = true
|
|
|
|
return nil, h.Checkpoint(h)
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
if err := applyPreimage(*e.Preimage); err != nil {
|
2019-04-16 12:52:42 +03:00
|
|
|
return nil, err
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
2019-04-16 12:52:42 +03:00
|
|
|
|
|
|
|
return &h.htlcSuccessResolver, nil
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// Create a buffered hodl chan to prevent deadlock.
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
// Notify registry that we are potentially resolving as an exit hop
|
|
|
|
// on-chain. If this HTLC indeed pays to an existing invoice, the
|
|
|
|
// invoice registry will tell us what to do with the HTLC. This is
|
|
|
|
// identical to HTLC resolution in the link.
|
2019-11-05 16:23:15 +03:00
|
|
|
circuitKey := channeldb.CircuitKey{
|
|
|
|
ChanID: h.ShortChanID,
|
|
|
|
HtlcID: h.htlc.HtlcIndex,
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:08 +03:00
|
|
|
resolution, err := h.Registry.NotifyExitHopHtlc(
|
2019-11-05 16:23:15 +03:00
|
|
|
h.htlc.RHash, h.htlc.Amt, h.htlcExpiry, currentHeight,
|
2019-08-26 15:06:57 +03:00
|
|
|
circuitKey, hodlChan, payload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-12-20 13:25:08 +03:00
|
|
|
if err != nil {
|
2019-04-16 13:11:20 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:08 +03:00
|
|
|
defer h.Registry.HodlUnsubscribeAll(hodlChan)
|
|
|
|
|
|
|
|
// If the resolution is non-nil (indicating that a settle or cancel has
|
|
|
|
// occurred), and the invoice is known to the registry (indicating that
|
|
|
|
// the htlc is paying one of our invoices and is not a forward), try to
|
|
|
|
// resolve it directly.
|
|
|
|
if resolution != nil &&
|
|
|
|
resolution.Outcome != invoices.ResultInvoiceNotFound {
|
|
|
|
|
|
|
|
return processHtlcResolution(*resolution)
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// With the epochs and preimage subscriptions initialized, we'll query
|
|
|
|
// to see if we already know the preimage.
|
2019-11-05 16:23:15 +03:00
|
|
|
preimage, ok := h.PreimageDB.LookupPreimage(h.htlc.RHash)
|
2019-01-16 22:03:59 +03:00
|
|
|
if ok {
|
|
|
|
// If we do, then this means we can claim the HTLC! However,
|
|
|
|
// we don't know how to ourselves, so we'll return our inner
|
|
|
|
// resolver which has the knowledge to do so.
|
2019-04-16 12:52:42 +03:00
|
|
|
if err := applyPreimage(preimage); err != nil {
|
|
|
|
return nil, err
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
2019-04-16 12:52:42 +03:00
|
|
|
|
|
|
|
return &h.htlcSuccessResolver, nil
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case preimage := <-preimageSubscription.WitnessUpdates:
|
2019-04-16 12:52:42 +03:00
|
|
|
// We receive all new preimages, so we need to ignore
|
|
|
|
// all except the preimage we are waiting for.
|
2019-11-05 16:23:15 +03:00
|
|
|
if !preimage.Matches(h.htlc.RHash) {
|
2019-01-16 22:03:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-16 12:52:42 +03:00
|
|
|
if err := applyPreimage(preimage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
// We've learned of the preimage and this information
|
|
|
|
// has been added to our inner resolver. We return it so
|
|
|
|
// it can continue contract resolution.
|
|
|
|
return &h.htlcSuccessResolver, nil
|
|
|
|
|
|
|
|
case hodlItem := <-hodlChan:
|
2019-12-20 13:25:07 +03:00
|
|
|
htlcResolution := hodlItem.(invoices.HtlcResolution)
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
return processHtlcResolution(htlcResolution)
|
2019-01-16 22:03:59 +03:00
|
|
|
|
|
|
|
case newBlock, ok := <-blockEpochs.Epochs:
|
|
|
|
if !ok {
|
2019-09-11 16:53:00 +03:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this new height expires the HTLC, then this means
|
|
|
|
// we never found out the preimage, so we can mark
|
2019-04-16 13:11:20 +03:00
|
|
|
// resolved and exit.
|
2019-01-16 22:03:59 +03:00
|
|
|
newHeight := uint32(newBlock.Height)
|
|
|
|
if newHeight >= h.htlcExpiry {
|
|
|
|
log.Infof("%T(%v): HTLC has timed out "+
|
|
|
|
"(expiry=%v, height=%v), abandoning", h,
|
|
|
|
h.htlcResolution.ClaimOutpoint,
|
|
|
|
h.htlcExpiry, currentHeight)
|
|
|
|
h.resolved = true
|
|
|
|
return nil, h.Checkpoint(h)
|
|
|
|
}
|
|
|
|
|
2019-11-06 15:31:13 +03:00
|
|
|
case <-h.quit:
|
2019-09-11 16:53:00 +03:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 17:05:57 +03:00
|
|
|
// report returns a report on the resolution state of the contract.
|
|
|
|
func (h *htlcIncomingContestResolver) report() *ContractReport {
|
|
|
|
// No locking needed as these values are read-only.
|
|
|
|
|
2019-11-05 16:23:15 +03:00
|
|
|
finalAmt := h.htlc.Amt.ToSatoshis()
|
2018-09-07 17:05:57 +03:00
|
|
|
if h.htlcResolution.SignedSuccessTx != nil {
|
|
|
|
finalAmt = btcutil.Amount(
|
|
|
|
h.htlcResolution.SignedSuccessTx.TxOut[0].Value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ContractReport{
|
|
|
|
Outpoint: h.htlcResolution.ClaimOutpoint,
|
2019-10-30 12:31:01 +03:00
|
|
|
Type: ReportOutputIncomingHtlc,
|
2018-09-07 17:05:57 +03:00
|
|
|
Amount: finalAmt,
|
|
|
|
MaturityHeight: h.htlcExpiry,
|
|
|
|
LimboBalance: finalAmt,
|
|
|
|
Stage: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// Stop signals the resolver to cancel any current resolution processes, and
|
|
|
|
// suspend.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (h *htlcIncomingContestResolver) Stop() {
|
2019-11-06 15:31:13 +03:00
|
|
|
close(h.quit)
|
2019-01-16 22:03:59 +03: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 *htlcIncomingContestResolver) 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 *htlcIncomingContestResolver) Encode(w io.Writer) error {
|
|
|
|
// We'll first write out the one field unique to this resolver.
|
|
|
|
if err := binary.Write(w, endian, h.htlcExpiry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we'll write out our internal resolver.
|
|
|
|
return h.htlcSuccessResolver.Encode(w)
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:24:33 +03:00
|
|
|
// newIncomingContestResolverFromReader attempts to decode an encoded ContractResolver
|
|
|
|
// from the passed Reader instance, returning an active ContractResolver
|
|
|
|
// instance.
|
|
|
|
func newIncomingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
|
|
|
*htlcIncomingContestResolver, error) {
|
|
|
|
|
|
|
|
h := &htlcIncomingContestResolver{}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// We'll first read the one field unique to this resolver.
|
|
|
|
if err := binary.Read(r, endian, &h.htlcExpiry); err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then we'll decode our internal resolver.
|
2019-11-01 12:24:33 +03:00
|
|
|
successResolver, err := newSuccessResolverFromReader(r, resCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.htlcSuccessResolver = *successResolver
|
|
|
|
|
|
|
|
return h, nil
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:23:48 +03:00
|
|
|
// Supplement adds additional information to the resolver that is required
|
|
|
|
// before Resolve() is called.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the htlcContractResolver interface.
|
|
|
|
func (h *htlcIncomingContestResolver) Supplement(htlc channeldb.HTLC) {
|
2019-11-05 16:23:15 +03:00
|
|
|
h.htlc = htlc
|
2019-11-01 14:23:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 15:06:57 +03:00
|
|
|
// decodePayload (re)decodes the hop payload of a received htlc.
|
|
|
|
func (h *htlcIncomingContestResolver) decodePayload() (*hop.Payload, error) {
|
|
|
|
|
|
|
|
onionReader := bytes.NewReader(h.htlc.OnionBlob)
|
|
|
|
iterator, err := h.OnionProcessor.ReconstructHopIterator(
|
|
|
|
onionReader, h.htlc.RHash[:],
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterator.HopPayload()
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// A compile time assertion to ensure htlcIncomingContestResolver meets the
|
|
|
|
// ContractResolver interface.
|
2019-11-01 14:23:48 +03:00
|
|
|
var _ htlcContractResolver = (*htlcIncomingContestResolver)(nil)
|