2019-01-16 22:03:59 +03:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-10-29 19:23:08 +03:00
|
|
|
"fmt"
|
2019-01-16 22:03:59 +03:00
|
|
|
"io"
|
2019-10-29 19:23:08 +03:00
|
|
|
"sync"
|
2019-01-17 15:11:04 +03:00
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2019-10-29 19:23:08 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2019-05-02 02:06:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2019-01-16 22:03:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-05-02 02:06:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// commitOutputConfTarget is the default confirmation target we'll use
|
|
|
|
// for sweeps of commit outputs that belong to us.
|
|
|
|
commitOutputConfTarget = 6
|
2019-01-16 22:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// commitSweepResolver is a resolver that will attempt to sweep the commitment
|
|
|
|
// output paying to us, in the case that the remote party broadcasts their
|
|
|
|
// version of the commitment transaction. We can sweep this output immediately,
|
|
|
|
// as it doesn't have a time-lock delay.
|
|
|
|
type commitSweepResolver struct {
|
|
|
|
// commitResolution contains all data required to successfully sweep
|
|
|
|
// this HTLC on-chain.
|
|
|
|
commitResolution lnwallet.CommitOutputResolution
|
|
|
|
|
|
|
|
// resolved reflects if the contract has been fully resolved or not.
|
|
|
|
resolved bool
|
|
|
|
|
|
|
|
// broadcastHeight is the height that the original contract was
|
|
|
|
// broadcast to the main-chain at. We'll use this value to bound any
|
|
|
|
// historical queries to the chain for spends/confirmations.
|
|
|
|
broadcastHeight uint32
|
|
|
|
|
|
|
|
// chanPoint is the channel point of the original contract.
|
|
|
|
chanPoint wire.OutPoint
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// currentReport stores the current state of the resolver for reporting
|
|
|
|
// over the rpc interface.
|
|
|
|
currentReport ContractReport
|
|
|
|
|
|
|
|
// reportLock prevents concurrent access to the resolver report.
|
|
|
|
reportLock sync.Mutex
|
|
|
|
|
2019-11-06 15:16:50 +03:00
|
|
|
contractResolverKit
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 13:04:28 +03:00
|
|
|
// newCommitSweepResolver instantiates a new direct commit output resolver.
|
|
|
|
func newCommitSweepResolver(res lnwallet.CommitOutputResolution,
|
|
|
|
broadcastHeight uint32,
|
|
|
|
chanPoint wire.OutPoint, resCfg ResolverConfig) *commitSweepResolver {
|
|
|
|
|
2019-10-30 13:32:27 +03:00
|
|
|
r := &commitSweepResolver{
|
2019-11-01 13:04:28 +03:00
|
|
|
contractResolverKit: *newContractResolverKit(resCfg),
|
|
|
|
commitResolution: res,
|
|
|
|
broadcastHeight: broadcastHeight,
|
|
|
|
chanPoint: chanPoint,
|
|
|
|
}
|
2019-10-30 13:32:27 +03:00
|
|
|
|
|
|
|
r.initLogger(r)
|
2019-10-29 19:23:08 +03:00
|
|
|
r.initReport()
|
2019-10-30 13:32:27 +03:00
|
|
|
|
|
|
|
return r
|
2019-11-01 13:04:28 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// ResolverKey returns an identifier which should be globally unique for this
|
|
|
|
// particular resolver within the chain the original contract resides within.
|
|
|
|
func (c *commitSweepResolver) ResolverKey() []byte {
|
|
|
|
key := newResolverID(c.commitResolution.SelfOutPoint)
|
|
|
|
return key[:]
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// waitForHeight registers for block notifications and waits for the provided
|
|
|
|
// block height to be reached.
|
|
|
|
func (c *commitSweepResolver) waitForHeight(waitHeight uint32) error {
|
|
|
|
// Register for block epochs. After registration, the current height
|
|
|
|
// will be sent on the channel immediately.
|
|
|
|
blockEpochs, err := c.Notifier.RegisterBlockEpochNtfn(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer blockEpochs.Cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case newBlock, ok := <-blockEpochs.Epochs:
|
|
|
|
if !ok {
|
|
|
|
return errResolverShuttingDown
|
|
|
|
}
|
|
|
|
height := newBlock.Height
|
|
|
|
if height >= int32(waitHeight) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-c.quit:
|
|
|
|
return errResolverShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCommitTxConfHeight waits for confirmation of the commitment tx and returns
|
|
|
|
// the confirmation height.
|
|
|
|
func (c *commitSweepResolver) getCommitTxConfHeight() (uint32, error) {
|
|
|
|
txID := c.commitResolution.SelfOutPoint.Hash
|
|
|
|
signDesc := c.commitResolution.SelfOutputSignDesc
|
|
|
|
pkScript := signDesc.Output.PkScript
|
|
|
|
const confDepth = 1
|
|
|
|
confChan, err := c.Notifier.RegisterConfirmationsNtfn(
|
|
|
|
&txID, pkScript, confDepth, c.broadcastHeight,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer confChan.Cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case txConfirmation, ok := <-confChan.Confirmed:
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("cannot get confirmation "+
|
|
|
|
"for commit tx %v", txID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txConfirmation.BlockHeight, nil
|
|
|
|
|
|
|
|
case <-c.quit:
|
|
|
|
return 0, errResolverShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// Resolve instructs the contract resolver to resolve the output on-chain. Once
|
|
|
|
// the output has been *fully* resolved, the function should return immediately
|
|
|
|
// with a nil ContractResolver value for the first return value. In the case
|
|
|
|
// that the contract requires further resolution, then another resolve is
|
|
|
|
// returned.
|
|
|
|
//
|
|
|
|
// NOTE: This function MUST be run as a goroutine.
|
|
|
|
func (c *commitSweepResolver) Resolve() (ContractResolver, error) {
|
|
|
|
// If we're already resolved, then we can exit early.
|
|
|
|
if c.resolved {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
confHeight, err := c.getCommitTxConfHeight()
|
2019-01-16 22:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
unlockHeight := confHeight + c.commitResolution.MaturityDelay
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
c.log.Debugf("commit conf_height=%v, unlock_height=%v",
|
|
|
|
confHeight, unlockHeight)
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// Update report now that we learned the confirmation height.
|
|
|
|
c.reportLock.Lock()
|
|
|
|
c.currentReport.MaturityHeight = unlockHeight
|
|
|
|
c.reportLock.Unlock()
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// If there is a csv delay, we'll wait for that.
|
|
|
|
if c.commitResolution.MaturityDelay > 0 {
|
|
|
|
c.log.Debugf("waiting for csv lock to expire at height %v",
|
|
|
|
unlockHeight)
|
2019-08-01 06:21:30 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// We only need to wait for the block before the block that
|
|
|
|
// unlocks the spend path.
|
|
|
|
err := c.waitForHeight(unlockHeight - 1)
|
2019-01-16 22:03:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 19:23:08 +03:00
|
|
|
}
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// We're dealing with our commitment transaction if the delay on the
|
|
|
|
// resolution isn't zero.
|
|
|
|
isLocalCommitTx := c.commitResolution.MaturityDelay != 0
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// There're two types of commitments, those that have tweaks
|
|
|
|
// for the remote key (us in this case), and those that don't.
|
|
|
|
// We'll rely on the presence of the commitment tweak to to
|
|
|
|
// discern which type of commitment this is.
|
|
|
|
var witnessType input.WitnessType
|
|
|
|
switch {
|
|
|
|
case isLocalCommitTx:
|
|
|
|
witnessType = input.CommitmentTimeLock
|
|
|
|
case c.commitResolution.SelfOutputSignDesc.SingleTweak == nil:
|
|
|
|
witnessType = input.CommitSpendNoDelayTweakless
|
|
|
|
default:
|
|
|
|
witnessType = input.CommitmentNoDelay
|
2019-01-17 15:11:04 +03:00
|
|
|
}
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// We'll craft an input with all the information required for
|
|
|
|
// the sweeper to create a fully valid sweeping transaction to
|
|
|
|
// recover these coins.
|
|
|
|
inp := input.NewCsvInput(
|
2019-01-17 15:11:04 +03:00
|
|
|
&c.commitResolution.SelfOutPoint,
|
2019-10-29 19:23:08 +03:00
|
|
|
witnessType,
|
|
|
|
&c.commitResolution.SelfOutputSignDesc,
|
2019-01-17 15:11:04 +03:00
|
|
|
c.broadcastHeight,
|
2019-10-29 19:23:08 +03:00
|
|
|
c.commitResolution.MaturityDelay,
|
2019-01-17 15:11:04 +03:00
|
|
|
)
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// With our input constructed, we'll now offer it to the
|
|
|
|
// sweeper.
|
|
|
|
c.log.Infof("sweeping commit output")
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
feePref := sweep.FeePreference{ConfTarget: commitOutputConfTarget}
|
|
|
|
resultChan, err := c.Sweeper.SweepInput(inp, feePref)
|
2019-01-16 22:03:59 +03:00
|
|
|
if err != nil {
|
2019-10-29 19:23:08 +03:00
|
|
|
c.log.Errorf("unable to sweep input: %v", err)
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 19:23:08 +03:00
|
|
|
|
|
|
|
// Sweeper is going to join this input with other inputs if
|
|
|
|
// possible and publish the sweep tx. When the sweep tx
|
|
|
|
// confirms, it signals us through the result channel with the
|
|
|
|
// outcome. Wait for this to happen.
|
2019-01-16 22:03:59 +03:00
|
|
|
select {
|
2019-10-29 19:23:08 +03:00
|
|
|
case sweepResult := <-resultChan:
|
|
|
|
if sweepResult.Err != nil {
|
|
|
|
c.log.Errorf("unable to sweep input: %v",
|
|
|
|
sweepResult.Err)
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
return nil, sweepResult.Err
|
|
|
|
}
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
c.log.Infof("commit tx fully resolved by sweep tx: %v",
|
|
|
|
sweepResult.Tx.TxHash())
|
2019-11-06 15:31:13 +03:00
|
|
|
case <-c.quit:
|
2019-09-11 16:53:00 +03:00
|
|
|
return nil, errResolverShuttingDown
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// Funds have been swept and balance is no longer in limbo.
|
|
|
|
c.reportLock.Lock()
|
|
|
|
c.currentReport.RecoveredBalance = c.currentReport.LimboBalance
|
|
|
|
c.currentReport.LimboBalance = 0
|
|
|
|
c.reportLock.Unlock()
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
c.resolved = true
|
|
|
|
return nil, c.Checkpoint(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the resolver to cancel any current resolution processes, and
|
|
|
|
// suspend.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (c *commitSweepResolver) Stop() {
|
2019-11-06 15:31:13 +03:00
|
|
|
close(c.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 (c *commitSweepResolver) IsResolved() bool {
|
|
|
|
return c.resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode writes an encoded version of the ContractResolver into the passed
|
|
|
|
// Writer.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (c *commitSweepResolver) Encode(w io.Writer) error {
|
|
|
|
if err := encodeCommitResolution(w, &c.commitResolution); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(w, endian, c.resolved); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, endian, c.broadcastHeight); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.Write(c.chanPoint.Hash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := binary.Write(w, endian, c.chanPoint.Index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-17 15:11:04 +03:00
|
|
|
// Previously a sweep tx was serialized at this point. Refactoring
|
|
|
|
// removed this, but keep in mind that this data may still be present in
|
|
|
|
// the database.
|
2019-01-16 22:03:59 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:24:33 +03:00
|
|
|
// newCommitSweepResolverFromReader attempts to decode an encoded
|
|
|
|
// ContractResolver from the passed Reader instance, returning an active
|
|
|
|
// ContractResolver instance.
|
|
|
|
func newCommitSweepResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
|
|
|
*commitSweepResolver, error) {
|
|
|
|
|
|
|
|
c := &commitSweepResolver{
|
|
|
|
contractResolverKit: *newContractResolverKit(resCfg),
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
if err := decodeCommitResolution(r, &c.commitResolution); err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(r, endian, &c.resolved); err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
if err := binary.Read(r, endian, &c.broadcastHeight); err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
_, err := io.ReadFull(r, c.chanPoint.Hash[:])
|
|
|
|
if err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
err = binary.Read(r, endian, &c.chanPoint.Index)
|
|
|
|
if err != nil {
|
2019-11-01 12:24:33 +03:00
|
|
|
return nil, err
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:11:04 +03:00
|
|
|
// Previously a sweep tx was deserialized at this point. Refactoring
|
|
|
|
// removed this, but keep in mind that this data may still be present in
|
|
|
|
// the database.
|
2019-01-16 22:03:59 +03:00
|
|
|
|
2019-10-30 13:32:27 +03:00
|
|
|
c.initLogger(c)
|
2019-10-29 19:23:08 +03:00
|
|
|
c.initReport()
|
2019-10-30 13:32:27 +03:00
|
|
|
|
2019-11-01 12:24:33 +03:00
|
|
|
return c, nil
|
2019-01-16 22:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 19:23:08 +03:00
|
|
|
// report returns a report on the resolution state of the contract.
|
|
|
|
func (c *commitSweepResolver) report() *ContractReport {
|
|
|
|
c.reportLock.Lock()
|
|
|
|
defer c.reportLock.Unlock()
|
|
|
|
|
|
|
|
copy := c.currentReport
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
// initReport initializes the pending channels report for this resolver.
|
|
|
|
func (c *commitSweepResolver) initReport() {
|
|
|
|
amt := btcutil.Amount(
|
|
|
|
c.commitResolution.SelfOutputSignDesc.Output.Value,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set the initial report. All fields are filled in, except for the
|
|
|
|
// maturity height which remains 0 until Resolve() is executed.
|
|
|
|
//
|
|
|
|
// TODO(joostjager): Resolvers only activate after the commit tx
|
|
|
|
// confirms. With more refactoring in channel arbitrator, it would be
|
|
|
|
// possible to make the confirmation height part of ResolverConfig and
|
|
|
|
// populate MaturityHeight here.
|
|
|
|
c.currentReport = ContractReport{
|
|
|
|
Outpoint: c.commitResolution.SelfOutPoint,
|
|
|
|
Type: ReportOutputUnencumbered,
|
|
|
|
Amount: amt,
|
|
|
|
LimboBalance: amt,
|
|
|
|
RecoveredBalance: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:03:59 +03:00
|
|
|
// A compile time assertion to ensure commitSweepResolver meets the
|
|
|
|
// ContractResolver interface.
|
2019-10-29 19:23:08 +03:00
|
|
|
var _ reportingContractResolver = (*commitSweepResolver)(nil)
|