2019-11-25 13:17:44 +01:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2020-03-10 13:39:01 +01:00
|
|
|
"sync"
|
2019-11-25 13:17:44 +01:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2020-07-07 19:49:53 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2019-11-25 13:17:44 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-07-07 19:49:53 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-11-25 13:17:44 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// anchorResolver is a resolver that will attempt to sweep our anchor output.
|
|
|
|
type anchorResolver struct {
|
|
|
|
// anchorSignDescriptor contains the information that is required to
|
|
|
|
// sweep the anchor.
|
|
|
|
anchorSignDescriptor input.SignDescriptor
|
|
|
|
|
|
|
|
// anchor is the outpoint on the commitment transaction.
|
|
|
|
anchor wire.OutPoint
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2023-03-02 07:14:12 +01:00
|
|
|
// chanType denotes the type of channel the contract belongs to.
|
|
|
|
chanType channeldb.ChannelType
|
|
|
|
|
2020-03-10 13:39:01 +01: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-25 13:17:44 +01:00
|
|
|
contractResolverKit
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAnchorResolver instantiates a new anchor resolver.
|
|
|
|
func newAnchorResolver(anchorSignDescriptor input.SignDescriptor,
|
|
|
|
anchor wire.OutPoint, broadcastHeight uint32,
|
|
|
|
chanPoint wire.OutPoint, resCfg ResolverConfig) *anchorResolver {
|
|
|
|
|
2020-03-10 13:39:01 +01:00
|
|
|
amt := btcutil.Amount(anchorSignDescriptor.Output.Value)
|
|
|
|
|
|
|
|
report := ContractReport{
|
|
|
|
Outpoint: anchor,
|
|
|
|
Type: ReportOutputAnchor,
|
|
|
|
Amount: amt,
|
|
|
|
LimboBalance: amt,
|
|
|
|
RecoveredBalance: 0,
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
r := &anchorResolver{
|
|
|
|
contractResolverKit: *newContractResolverKit(resCfg),
|
|
|
|
anchorSignDescriptor: anchorSignDescriptor,
|
|
|
|
anchor: anchor,
|
|
|
|
broadcastHeight: broadcastHeight,
|
|
|
|
chanPoint: chanPoint,
|
2020-03-10 13:39:01 +01:00
|
|
|
currentReport: report,
|
2019-11-25 13:17:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r.initLogger(r)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolverKey returns an identifier which should be globally unique for this
|
|
|
|
// particular resolver within the chain the original contract resides within.
|
|
|
|
func (c *anchorResolver) ResolverKey() []byte {
|
|
|
|
// The anchor resolver is stateless and doesn't need a database key.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve offers the anchor output to the sweeper and waits for it to be swept.
|
|
|
|
func (c *anchorResolver) Resolve() (ContractResolver, error) {
|
|
|
|
// Attempt to update the sweep parameters to the post-confirmation
|
|
|
|
// situation. We don't want to force sweep anymore, because the anchor
|
|
|
|
// lost its special purpose to get the commitment confirmed. It is just
|
|
|
|
// an output that we want to sweep only if it is economical to do so.
|
2020-09-04 11:53:24 +02:00
|
|
|
//
|
|
|
|
// An exclusive group is not necessary anymore, because we know that
|
|
|
|
// this is the only anchor that can be swept.
|
|
|
|
//
|
2020-09-04 11:19:27 +02:00
|
|
|
// We also clear the parent tx information for cpfp, because the
|
|
|
|
// commitment tx is confirmed.
|
|
|
|
//
|
2020-09-04 11:53:24 +02:00
|
|
|
// After a restart or when the remote force closes, the sweeper is not
|
|
|
|
// yet aware of the anchor. In that case, it will be added as new input
|
|
|
|
// to the sweeper.
|
2019-11-25 13:17:44 +01:00
|
|
|
relayFeeRate := c.Sweeper.RelayFeePerKW()
|
|
|
|
|
2023-03-02 07:14:12 +01:00
|
|
|
witnessType := input.CommitmentAnchor
|
|
|
|
|
|
|
|
// For taproot channels, we need to use the proper witness type.
|
|
|
|
if c.chanType.IsTaproot() {
|
|
|
|
witnessType = input.TaprootAnchorSweepSpend
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:53:24 +02:00
|
|
|
anchorInput := input.MakeBaseInput(
|
2023-03-02 07:14:12 +01:00
|
|
|
&c.anchor, witnessType, &c.anchorSignDescriptor,
|
|
|
|
c.broadcastHeight, nil,
|
2020-09-04 11:53:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
resultChan, err := c.Sweeper.SweepInput(
|
|
|
|
&anchorInput,
|
|
|
|
sweep.Params{
|
2019-11-25 13:17:44 +01:00
|
|
|
Fee: sweep.FeePreference{
|
|
|
|
FeeRate: relayFeeRate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-09-04 11:53:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-25 13:17:44 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:49:53 +02:00
|
|
|
var (
|
|
|
|
outcome channeldb.ResolverOutcome
|
|
|
|
spendTx *chainhash.Hash
|
|
|
|
)
|
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
select {
|
|
|
|
case sweepRes := <-resultChan:
|
|
|
|
switch sweepRes.Err {
|
|
|
|
// Anchor was swept successfully.
|
|
|
|
case nil:
|
2020-07-07 19:49:53 +02:00
|
|
|
sweepTxID := sweepRes.Tx.TxHash()
|
2019-11-25 13:17:44 +01:00
|
|
|
|
2020-07-07 19:49:53 +02:00
|
|
|
spendTx = &sweepTxID
|
|
|
|
outcome = channeldb.ResolverOutcomeClaimed
|
2020-03-10 13:39:01 +01:00
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
// Anchor was swept by someone else. This is possible after the
|
|
|
|
// 16 block csv lock.
|
|
|
|
case sweep.ErrRemoteSpend:
|
|
|
|
c.log.Warnf("our anchor spent by someone else")
|
2020-07-07 19:49:53 +02:00
|
|
|
outcome = channeldb.ResolverOutcomeUnclaimed
|
2019-11-25 13:17:44 +01:00
|
|
|
|
|
|
|
// The sweeper gave up on sweeping the anchor. This happens
|
|
|
|
// after the maximum number of sweep attempts has been reached.
|
|
|
|
// See sweep.DefaultMaxSweepAttempts. Sweep attempts are
|
|
|
|
// interspaced with random delays picked from a range that
|
|
|
|
// increases exponentially.
|
|
|
|
//
|
|
|
|
// We consider the anchor as being lost.
|
|
|
|
case sweep.ErrTooManyAttempts:
|
|
|
|
c.log.Warnf("anchor sweep abandoned")
|
2020-07-07 19:49:53 +02:00
|
|
|
outcome = channeldb.ResolverOutcomeUnclaimed
|
2019-11-25 13:17:44 +01:00
|
|
|
|
|
|
|
// An unexpected error occurred.
|
|
|
|
default:
|
|
|
|
c.log.Errorf("unable to sweep anchor: %v", sweepRes.Err)
|
|
|
|
|
|
|
|
return nil, sweepRes.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-c.quit:
|
|
|
|
return nil, errResolverShuttingDown
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:39:01 +01:00
|
|
|
// Update report to reflect that funds are no longer in limbo.
|
|
|
|
c.reportLock.Lock()
|
2020-07-07 19:49:53 +02:00
|
|
|
if outcome == channeldb.ResolverOutcomeClaimed {
|
2020-03-10 13:39:01 +01:00
|
|
|
c.currentReport.RecoveredBalance = c.currentReport.LimboBalance
|
|
|
|
}
|
|
|
|
c.currentReport.LimboBalance = 0
|
2020-07-07 19:49:53 +02:00
|
|
|
report := c.currentReport.resolverReport(
|
|
|
|
spendTx, channeldb.ResolverTypeAnchor, outcome,
|
|
|
|
)
|
2020-03-10 13:39:01 +01:00
|
|
|
c.reportLock.Unlock()
|
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
c.resolved = true
|
2020-07-07 19:49:53 +02:00
|
|
|
return nil, c.PutResolverReport(nil, report)
|
2019-11-25 13:17:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals the resolver to cancel any current resolution processes, and
|
|
|
|
// suspend.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the ContractResolver interface.
|
|
|
|
func (c *anchorResolver) Stop() {
|
|
|
|
close(c.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 *anchorResolver) IsResolved() bool {
|
|
|
|
return c.resolved
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:07:36 +02:00
|
|
|
// 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.
|
2023-03-02 07:14:12 +01:00
|
|
|
func (c *anchorResolver) SupplementState(state *channeldb.OpenChannel) {
|
|
|
|
c.chanType = state.ChanType
|
2021-07-15 01:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:39:01 +01:00
|
|
|
// report returns a report on the resolution state of the contract.
|
|
|
|
func (c *anchorResolver) report() *ContractReport {
|
|
|
|
c.reportLock.Lock()
|
|
|
|
defer c.reportLock.Unlock()
|
|
|
|
|
|
|
|
reportCopy := c.currentReport
|
|
|
|
return &reportCopy
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
func (c *anchorResolver) Encode(w io.Writer) error {
|
|
|
|
return errors.New("serialization not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time assertion to ensure anchorResolver meets the
|
|
|
|
// ContractResolver interface.
|
|
|
|
var _ ContractResolver = (*anchorResolver)(nil)
|