2018-09-26 07:03:16 +02:00
|
|
|
package sweep
|
|
|
|
|
|
|
|
import (
|
2018-10-23 12:05:48 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2020-05-05 21:10:11 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-09-26 07:03:16 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-10-23 12:05:48 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2023-10-26 08:27:46 +02:00
|
|
|
"github.com/lightningnetwork/lnd/fn"
|
2019-01-16 15:47:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2024-07-24 13:31:21 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnutils"
|
2018-09-26 07:03:16 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-10-31 03:43:05 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2018-09-26 07:03:16 +02:00
|
|
|
)
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
var (
|
|
|
|
// ErrRemoteSpend is returned in case an output that we try to sweep is
|
|
|
|
// confirmed in a tx of the remote party.
|
|
|
|
ErrRemoteSpend = errors.New("remote party swept utxo")
|
|
|
|
|
2023-07-12 10:23:29 +02:00
|
|
|
// ErrFeePreferenceTooLow is returned when the fee preference gives a
|
|
|
|
// fee rate that's below the relay fee rate.
|
|
|
|
ErrFeePreferenceTooLow = errors.New("fee preference too low")
|
|
|
|
|
2019-12-09 11:51:13 +01:00
|
|
|
// ErrExclusiveGroupSpend is returned in case a different input of the
|
|
|
|
// same exclusive group was spent.
|
|
|
|
ErrExclusiveGroupSpend = errors.New("other member of exclusive group " +
|
|
|
|
"was spent")
|
|
|
|
|
2019-05-17 04:52:46 +02:00
|
|
|
// ErrSweeperShuttingDown is an error returned when a client attempts to
|
|
|
|
// make a request to the UtxoSweeper, but it is unable to handle it as
|
2020-03-19 05:43:49 +01:00
|
|
|
// it is/has already been stopped.
|
2019-05-17 04:52:46 +02:00
|
|
|
ErrSweeperShuttingDown = errors.New("utxo sweeper shutting down")
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// DefaultDeadlineDelta defines a default deadline delta (1 week) to be
|
|
|
|
// used when sweeping inputs with no deadline pressure.
|
|
|
|
DefaultDeadlineDelta = int32(1008)
|
2018-10-23 12:05:48 +02:00
|
|
|
)
|
|
|
|
|
2019-12-09 11:42:58 +01:00
|
|
|
// Params contains the parameters that control the sweeping process.
|
|
|
|
type Params struct {
|
2019-12-09 11:51:13 +01:00
|
|
|
// ExclusiveGroup is an identifier that, if set, prevents other inputs
|
|
|
|
// with the same identifier from being batched together.
|
|
|
|
ExclusiveGroup *uint64
|
2024-02-27 10:52:47 +01:00
|
|
|
|
|
|
|
// DeadlineHeight specifies an absolute block height that this input
|
|
|
|
// should be confirmed by. This value is used by the fee bumper to
|
|
|
|
// decide its urgency and adjust its feerate used.
|
|
|
|
DeadlineHeight fn.Option[int32]
|
|
|
|
|
|
|
|
// Budget specifies the maximum amount of satoshis that can be spent on
|
|
|
|
// fees for this sweep.
|
|
|
|
Budget btcutil.Amount
|
2024-04-02 19:09:14 +02:00
|
|
|
|
|
|
|
// Immediate indicates that the input should be swept immediately
|
|
|
|
// without waiting for blocks to come to trigger the sweeping of
|
|
|
|
// inputs.
|
|
|
|
Immediate bool
|
2024-04-11 11:08:36 +02:00
|
|
|
|
|
|
|
// StartingFeeRate is an optional parameter that can be used to specify
|
|
|
|
// the initial fee rate to use for the fee function.
|
|
|
|
StartingFeeRate fn.Option[chainfee.SatPerKWeight]
|
2019-12-09 11:42:58 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// String returns a human readable interpretation of the sweep parameters.
|
|
|
|
func (p Params) String() string {
|
2024-03-21 08:24:02 +01:00
|
|
|
deadline := "none"
|
|
|
|
p.DeadlineHeight.WhenSome(func(d int32) {
|
|
|
|
deadline = fmt.Sprintf("%d", d)
|
|
|
|
})
|
|
|
|
|
|
|
|
exclusiveGroup := "none"
|
2023-08-09 11:30:52 +02:00
|
|
|
if p.ExclusiveGroup != nil {
|
2024-03-21 08:24:02 +01:00
|
|
|
exclusiveGroup = fmt.Sprintf("%d", *p.ExclusiveGroup)
|
2023-08-09 11:30:52 +02:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:08:36 +02:00
|
|
|
return fmt.Sprintf("startingFeeRate=%v, immediate=%v, "+
|
|
|
|
"exclusive_group=%v, budget=%v, deadline=%v", p.StartingFeeRate,
|
|
|
|
p.Immediate, exclusiveGroup, p.Budget, deadline)
|
2020-01-09 14:41:28 +01:00
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// SweepState represents the current state of a pending input.
|
|
|
|
//
|
|
|
|
//nolint:revive
|
|
|
|
type SweepState uint8
|
|
|
|
|
|
|
|
const (
|
2024-03-17 19:49:58 +01:00
|
|
|
// Init is the initial state of a pending input. This is set when a new
|
|
|
|
// sweeping request for a given input is made.
|
|
|
|
Init SweepState = iota
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
// PendingPublish specifies an input's state where it's already been
|
|
|
|
// included in a sweeping tx but the tx is not published yet. Inputs
|
|
|
|
// in this state should not be used for grouping again.
|
|
|
|
PendingPublish
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
// Published is the state where the input's sweeping tx has
|
2023-10-24 02:14:52 +02:00
|
|
|
// successfully been published. Inputs in this state can only be
|
|
|
|
// updated via RBF.
|
2024-03-17 19:49:58 +01:00
|
|
|
Published
|
|
|
|
|
|
|
|
// PublishFailed is the state when an error is returned from publishing
|
|
|
|
// the sweeping tx. Inputs in this state can be re-grouped in to a new
|
|
|
|
// sweeping tx.
|
|
|
|
PublishFailed
|
|
|
|
|
|
|
|
// Swept is the final state of a pending input. This is set when the
|
|
|
|
// input has been successfully swept.
|
|
|
|
Swept
|
|
|
|
|
|
|
|
// Excluded is the state of a pending input that has been excluded and
|
|
|
|
// can no longer be swept. For instance, when one of the three anchor
|
|
|
|
// sweeping transactions confirmed, the remaining two will be excluded.
|
|
|
|
Excluded
|
|
|
|
|
|
|
|
// Failed is the state when a pending input has too many failed publish
|
|
|
|
// atttempts or unknown broadcast error is returned.
|
|
|
|
Failed
|
2023-10-24 02:14:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// String gives a human readable text for the sweep states.
|
|
|
|
func (s SweepState) String() string {
|
|
|
|
switch s {
|
2024-03-17 19:49:58 +01:00
|
|
|
case Init:
|
2023-10-24 02:14:52 +02:00
|
|
|
return "Init"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case PendingPublish:
|
2023-10-24 02:14:52 +02:00
|
|
|
return "PendingPublish"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case Published:
|
2023-10-24 02:14:52 +02:00
|
|
|
return "Published"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case PublishFailed:
|
2023-10-24 02:14:52 +02:00
|
|
|
return "PublishFailed"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case Swept:
|
2023-10-24 02:14:52 +02:00
|
|
|
return "Swept"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case Excluded:
|
2023-10-24 07:47:14 +02:00
|
|
|
return "Excluded"
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
case Failed:
|
2023-10-24 07:47:14 +02:00
|
|
|
return "Failed"
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 08:27:46 +02:00
|
|
|
// RBFInfo stores the information required to perform a RBF bump on a pending
|
|
|
|
// sweeping tx.
|
|
|
|
type RBFInfo struct {
|
|
|
|
// Txid is the txid of the sweeping tx.
|
|
|
|
Txid chainhash.Hash
|
|
|
|
|
|
|
|
// FeeRate is the fee rate of the sweeping tx.
|
|
|
|
FeeRate chainfee.SatPerKWeight
|
|
|
|
|
|
|
|
// Fee is the total fee of the sweeping tx.
|
|
|
|
Fee btcutil.Amount
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
// SweeperInput is created when an input reaches the main loop for the first
|
2019-12-09 15:26:05 +01:00
|
|
|
// time. It wraps the input and tracks all relevant state that is needed for
|
|
|
|
// sweeping.
|
2024-03-17 20:20:37 +01:00
|
|
|
type SweeperInput struct {
|
2019-12-09 15:26:05 +01:00
|
|
|
input.Input
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// state tracks the current state of the input.
|
|
|
|
state SweepState
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// listeners is a list of channels over which the final outcome of the
|
|
|
|
// sweep needs to be broadcasted.
|
|
|
|
listeners []chan Result
|
|
|
|
|
|
|
|
// ntfnRegCancel is populated with a function that cancels the chain
|
|
|
|
// notifier spend registration.
|
|
|
|
ntfnRegCancel func()
|
|
|
|
|
|
|
|
// publishAttempts records the number of attempts that have already been
|
|
|
|
// made to sweep this tx.
|
|
|
|
publishAttempts int
|
2019-05-02 01:06:19 +02:00
|
|
|
|
2019-12-09 11:42:58 +01:00
|
|
|
// params contains the parameters that control the sweeping process.
|
|
|
|
params Params
|
2019-05-28 01:14:50 +02:00
|
|
|
|
|
|
|
// lastFeeRate is the most recent fee rate used for this input within a
|
|
|
|
// transaction broadcast to the network.
|
2019-10-31 03:43:05 +01:00
|
|
|
lastFeeRate chainfee.SatPerKWeight
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// rbf records the RBF constraints.
|
|
|
|
rbf fn.Option[RBFInfo]
|
2024-04-11 11:01:31 +02:00
|
|
|
|
2024-04-15 21:15:34 +02:00
|
|
|
// DeadlineHeight is the deadline height for this input. This is
|
2024-04-11 11:01:31 +02:00
|
|
|
// different from the DeadlineHeight in its params as it's an actual
|
|
|
|
// value than an option.
|
2024-04-15 21:15:34 +02:00
|
|
|
DeadlineHeight int32
|
2019-05-02 01:06:19 +02:00
|
|
|
}
|
|
|
|
|
2024-02-27 10:52:47 +01:00
|
|
|
// String returns a human readable interpretation of the pending input.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (p *SweeperInput) String() string {
|
2024-02-27 10:52:47 +01:00
|
|
|
return fmt.Sprintf("%v (%v)", p.Input.OutPoint(), p.Input.WitnessType())
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// terminated returns a boolean indicating whether the input has reached a
|
|
|
|
// final state.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (p *SweeperInput) terminated() bool {
|
2023-10-24 07:47:14 +02:00
|
|
|
switch p.state {
|
|
|
|
// If the input has reached a final state, that it's either
|
|
|
|
// been swept, or failed, or excluded, we will remove it from
|
|
|
|
// our sweeper.
|
2024-03-17 19:49:58 +01:00
|
|
|
case Failed, Swept, Excluded:
|
2023-10-24 07:47:14 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
// InputsMap is a type alias for a set of pending inputs.
|
|
|
|
type InputsMap = map[wire.OutPoint]*SweeperInput
|
2019-05-02 01:06:19 +02:00
|
|
|
|
2019-05-17 04:52:46 +02:00
|
|
|
// pendingSweepsReq is an internal message we'll use to represent an external
|
|
|
|
// caller's intent to retrieve all of the pending inputs the UtxoSweeper is
|
|
|
|
// attempting to sweep.
|
|
|
|
type pendingSweepsReq struct {
|
2024-03-17 20:20:37 +01:00
|
|
|
respChan chan map[wire.OutPoint]*PendingInputResponse
|
2020-12-09 12:24:01 +01:00
|
|
|
errChan chan error
|
2019-05-17 04:52:46 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
// PendingInputResponse contains information about an input that is currently
|
|
|
|
// being swept by the UtxoSweeper.
|
|
|
|
type PendingInputResponse struct {
|
2019-05-17 04:52:46 +02:00
|
|
|
// OutPoint is the identify outpoint of the input being swept.
|
|
|
|
OutPoint wire.OutPoint
|
|
|
|
|
|
|
|
// WitnessType is the witness type of the input being swept.
|
|
|
|
WitnessType input.WitnessType
|
|
|
|
|
|
|
|
// Amount is the amount of the input being swept.
|
|
|
|
Amount btcutil.Amount
|
|
|
|
|
|
|
|
// LastFeeRate is the most recent fee rate used for the input being
|
|
|
|
// swept within a transaction broadcast to the network.
|
2019-10-31 03:43:05 +01:00
|
|
|
LastFeeRate chainfee.SatPerKWeight
|
2019-05-17 04:52:46 +02:00
|
|
|
|
|
|
|
// BroadcastAttempts is the number of attempts we've made to sweept the
|
|
|
|
// input.
|
|
|
|
BroadcastAttempts int
|
|
|
|
|
2020-01-09 13:53:25 +01:00
|
|
|
// Params contains the sweep parameters for this pending request.
|
|
|
|
Params Params
|
2024-04-11 11:01:31 +02:00
|
|
|
|
|
|
|
// DeadlineHeight records the deadline height of this input.
|
|
|
|
DeadlineHeight uint32
|
2019-05-17 04:52:46 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// updateReq is an internal message we'll use to represent an external caller's
|
|
|
|
// intent to update the sweep parameters of a given input.
|
|
|
|
type updateReq struct {
|
|
|
|
input wire.OutPoint
|
2024-04-09 14:05:00 +02:00
|
|
|
params Params
|
2020-01-09 14:41:28 +01:00
|
|
|
responseChan chan *updateResp
|
2019-05-29 23:00:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// updateResp is an internal message we'll use to hand off the response of a
|
|
|
|
// updateReq from the UtxoSweeper's main event loop back to the caller.
|
|
|
|
type updateResp struct {
|
2019-05-29 23:00:14 +02:00
|
|
|
resultChan chan Result
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// UtxoSweeper is responsible for sweeping outputs back into the wallet
|
2018-09-26 07:03:16 +02:00
|
|
|
type UtxoSweeper struct {
|
2018-10-23 12:05:48 +02:00
|
|
|
started uint32 // To be used atomically.
|
|
|
|
stopped uint32 // To be used atomically.
|
|
|
|
|
2018-09-26 07:03:16 +02:00
|
|
|
cfg *UtxoSweeperConfig
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
newInputs chan *sweepInputMessage
|
|
|
|
spendChan chan *chainntnfs.SpendDetail
|
|
|
|
|
2019-05-17 04:52:46 +02:00
|
|
|
// pendingSweepsReq is a channel that will be sent requests by external
|
|
|
|
// callers in order to retrieve the set of pending inputs the
|
|
|
|
// UtxoSweeper is attempting to sweep.
|
|
|
|
pendingSweepsReqs chan *pendingSweepsReq
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// updateReqs is a channel that will be sent requests by external
|
2019-05-29 23:00:14 +02:00
|
|
|
// callers who wish to bump the fee rate of a given input.
|
2020-01-09 14:41:28 +01:00
|
|
|
updateReqs chan *updateReq
|
2019-05-29 23:00:14 +02:00
|
|
|
|
2024-03-17 20:00:58 +01:00
|
|
|
// inputs is the total set of inputs the UtxoSweeper has been requested
|
|
|
|
// to sweep.
|
2024-03-17 20:20:37 +01:00
|
|
|
inputs InputsMap
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2024-06-04 08:05:19 +02:00
|
|
|
currentOutputScript fn.Option[lnwallet.AddrWithKey]
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2019-10-31 03:43:05 +01:00
|
|
|
relayFeeRate chainfee.SatPerKWeight
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
2023-10-24 01:14:55 +02:00
|
|
|
|
|
|
|
// currentHeight is the best known height of the main chain. This is
|
|
|
|
// updated whenever a new block epoch is received.
|
|
|
|
currentHeight int32
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// bumpResultChan is a channel that receives broadcast results from the
|
|
|
|
// TxPublisher.
|
|
|
|
bumpResultChan chan *BumpResult
|
2018-09-26 07:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UtxoSweeperConfig contains dependencies of UtxoSweeper.
|
|
|
|
type UtxoSweeperConfig struct {
|
2018-10-23 12:05:48 +02:00
|
|
|
// GenSweepScript generates a P2WKH script belonging to the wallet where
|
|
|
|
// funds can be swept.
|
2024-06-04 08:00:38 +02:00
|
|
|
GenSweepScript func() fn.Result[lnwallet.AddrWithKey]
|
2018-09-26 07:03:16 +02:00
|
|
|
|
2018-11-18 05:57:30 +01:00
|
|
|
// FeeEstimator is used when crafting sweep transactions to estimate
|
|
|
|
// the necessary fee relative to the expected size of the sweep
|
2018-10-18 01:27:11 +02:00
|
|
|
// transaction.
|
2019-10-31 03:43:05 +01:00
|
|
|
FeeEstimator chainfee.Estimator
|
2018-09-26 07:03:16 +02:00
|
|
|
|
2019-12-10 15:32:57 +01:00
|
|
|
// Wallet contains the wallet functions that sweeper requires.
|
|
|
|
Wallet Wallet
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
// Notifier is an instance of a chain notifier we'll use to watch for
|
|
|
|
// certain on-chain events.
|
|
|
|
Notifier chainntnfs.ChainNotifier
|
|
|
|
|
2023-10-24 07:14:21 +02:00
|
|
|
// Mempool is the mempool watcher that will be used to query whether a
|
|
|
|
// given input is already being spent by a transaction in the mempool.
|
|
|
|
Mempool chainntnfs.MempoolWatcher
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// Store stores the published sweeper txes.
|
|
|
|
Store SweeperStore
|
|
|
|
|
2018-09-26 07:03:16 +02:00
|
|
|
// Signer is used by the sweeper to generate valid witnesses at the
|
|
|
|
// time the incubated outputs need to be spent.
|
2019-01-16 15:47:43 +01:00
|
|
|
Signer input.Signer
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
// MaxInputsPerTx specifies the default maximum number of inputs allowed
|
|
|
|
// in a single sweep tx. If more need to be swept, multiple txes are
|
|
|
|
// created and published.
|
2024-03-19 22:56:56 +01:00
|
|
|
MaxInputsPerTx uint32
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2024-01-11 03:38:04 +01:00
|
|
|
// MaxFeeRate is the maximum fee rate allowed within the UtxoSweeper.
|
2023-07-26 11:04:04 +02:00
|
|
|
MaxFeeRate chainfee.SatPerVByte
|
2019-05-02 01:06:19 +02:00
|
|
|
|
2023-10-24 06:32:17 +02:00
|
|
|
// Aggregator is used to group inputs into clusters based on its
|
|
|
|
// implemention-specific strategy.
|
|
|
|
Aggregator UtxoAggregator
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// Publisher is used to publish the sweep tx crafted here and monitors
|
|
|
|
// it for potential fee bumps.
|
|
|
|
Publisher Bumper
|
2024-03-18 21:45:05 +01:00
|
|
|
|
|
|
|
// NoDeadlineConfTarget is the conf target to use when sweeping
|
|
|
|
// non-time-sensitive outputs.
|
|
|
|
NoDeadlineConfTarget uint32
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result is the struct that is pushed through the result channel. Callers can
|
|
|
|
// use this to be informed of the final sweep result. In case of a remote
|
|
|
|
// spend, Err will be ErrRemoteSpend.
|
|
|
|
type Result struct {
|
|
|
|
// Err is the final result of the sweep. It is nil when the input is
|
|
|
|
// swept successfully by us. ErrRemoteSpend is returned when another
|
|
|
|
// party took the input.
|
|
|
|
Err error
|
|
|
|
|
|
|
|
// Tx is the transaction that spent the input.
|
|
|
|
Tx *wire.MsgTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweepInputMessage structs are used in the internal channel between the
|
|
|
|
// SweepInput call and the sweeper main loop.
|
|
|
|
type sweepInputMessage struct {
|
2019-12-09 11:42:58 +01:00
|
|
|
input input.Input
|
|
|
|
params Params
|
|
|
|
resultChan chan Result
|
2018-09-26 07:03:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// New returns a new Sweeper instance.
|
2018-09-26 07:03:16 +02:00
|
|
|
func New(cfg *UtxoSweeperConfig) *UtxoSweeper {
|
|
|
|
return &UtxoSweeper{
|
2019-05-17 04:52:46 +02:00
|
|
|
cfg: cfg,
|
|
|
|
newInputs: make(chan *sweepInputMessage),
|
|
|
|
spendChan: make(chan *chainntnfs.SpendDetail),
|
2020-01-09 14:41:28 +01:00
|
|
|
updateReqs: make(chan *updateReq),
|
2019-05-17 04:52:46 +02:00
|
|
|
pendingSweepsReqs: make(chan *pendingSweepsReq),
|
|
|
|
quit: make(chan struct{}),
|
2024-03-17 20:20:37 +01:00
|
|
|
inputs: make(InputsMap),
|
2024-01-17 10:21:09 +01:00
|
|
|
bumpResultChan: make(chan *BumpResult, 100),
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the process of constructing and publish sweep txes.
|
|
|
|
func (s *UtxoSweeper) Start() error {
|
|
|
|
if !atomic.CompareAndSwapUint32(&s.started, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 15:47:50 +01:00
|
|
|
log.Info("Sweeper starting")
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
// Retrieve relay fee for dust limit calculation. Assume that this will
|
|
|
|
// not change from here on.
|
2019-05-02 01:06:19 +02:00
|
|
|
s.relayFeeRate = s.cfg.FeeEstimator.RelayFeePerKW()
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2019-10-07 13:21:25 +02:00
|
|
|
// We need to register for block epochs and retry sweeping every block.
|
|
|
|
// We should get a notification with the current best block immediately
|
|
|
|
// if we don't provide any epoch. We'll wait for that in the collector.
|
|
|
|
blockEpochs, err := s.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
2018-10-23 12:05:48 +02:00
|
|
|
if err != nil {
|
2024-02-26 12:19:38 +01:00
|
|
|
return fmt.Errorf("register block epoch ntfn: %w", err)
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start sweeper main loop.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer blockEpochs.Cancel()
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2019-10-07 13:21:25 +02:00
|
|
|
s.collector(blockEpochs.Epochs)
|
2020-12-09 12:24:01 +01:00
|
|
|
|
|
|
|
// The collector exited and won't longer handle incoming
|
|
|
|
// requests. This can happen on shutdown, when the block
|
|
|
|
// notifier shuts down before the sweeper and its clients. In
|
|
|
|
// order to not deadlock the clients waiting for their requests
|
|
|
|
// being handled, we handle them here and immediately return an
|
|
|
|
// error. When the sweeper finally is shut down we can exit as
|
|
|
|
// the clients will be notified.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case inp := <-s.newInputs:
|
|
|
|
inp.resultChan <- Result{
|
|
|
|
Err: ErrSweeperShuttingDown,
|
|
|
|
}
|
|
|
|
|
|
|
|
case req := <-s.pendingSweepsReqs:
|
|
|
|
req.errChan <- ErrSweeperShuttingDown
|
|
|
|
|
|
|
|
case req := <-s.updateReqs:
|
|
|
|
req.responseChan <- &updateResp{
|
|
|
|
err: ErrSweeperShuttingDown,
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 12:05:48 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 08:52:20 +01:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
func (s *UtxoSweeper) RelayFeePerKW() chainfee.SatPerKWeight {
|
|
|
|
return s.relayFeeRate
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// Stop stops sweeper from listening to block epochs and constructing sweep
|
|
|
|
// txes.
|
|
|
|
func (s *UtxoSweeper) Stop() error {
|
|
|
|
if !atomic.CompareAndSwapUint32(&s.stopped, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 20:16:42 +02:00
|
|
|
log.Info("Sweeper shutting down...")
|
|
|
|
defer log.Debug("Sweeper shutdown complete")
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
close(s.quit)
|
|
|
|
s.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SweepInput sweeps inputs back into the wallet. The inputs will be batched and
|
2019-05-02 01:06:19 +02:00
|
|
|
// swept after the batch time window ends. A custom fee preference can be
|
2019-05-29 23:00:14 +02:00
|
|
|
// provided to determine what fee rate should be used for the input. Note that
|
|
|
|
// the input may not always be swept with this exact value, as its possible for
|
|
|
|
// it to be batched under the same transaction with other similar fee rate
|
|
|
|
// inputs.
|
2018-10-23 12:05:48 +02:00
|
|
|
//
|
|
|
|
// NOTE: Extreme care needs to be taken that input isn't changed externally.
|
|
|
|
// Because it is an interface and we don't know what is exactly behind it, we
|
|
|
|
// cannot make a local copy in sweeper.
|
2023-10-24 02:14:52 +02:00
|
|
|
//
|
|
|
|
// TODO(yy): make sure the caller is using the Result chan.
|
2024-03-27 10:07:48 +01:00
|
|
|
func (s *UtxoSweeper) SweepInput(inp input.Input,
|
2019-12-09 11:42:58 +01:00
|
|
|
params Params) (chan Result, error) {
|
2019-05-02 01:06:19 +02:00
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
if inp == nil || inp.OutPoint() == input.EmptyOutPoint ||
|
|
|
|
inp.SignDesc() == nil {
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
return nil, errors.New("nil input received")
|
|
|
|
}
|
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
absoluteTimeLock, _ := inp.RequiredLockTime()
|
2018-10-23 12:05:48 +02:00
|
|
|
log.Infof("Sweep request received: out_point=%v, witness_type=%v, "+
|
2021-07-15 02:19:22 +02:00
|
|
|
"relative_time_lock=%v, absolute_time_lock=%v, amount=%v, "+
|
2024-03-27 10:07:48 +01:00
|
|
|
"parent=(%v), params=(%v)", inp.OutPoint(), inp.WitnessType(),
|
|
|
|
inp.BlocksToMaturity(), absoluteTimeLock,
|
|
|
|
btcutil.Amount(inp.SignDesc().Output.Value),
|
|
|
|
inp.UnconfParent(), params)
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
sweeperInput := &sweepInputMessage{
|
2024-03-27 10:07:48 +01:00
|
|
|
input: inp,
|
2019-12-09 11:42:58 +01:00
|
|
|
params: params,
|
|
|
|
resultChan: make(chan Result, 1),
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 05:43:49 +01:00
|
|
|
// Deliver input to the main event loop.
|
2018-10-23 12:05:48 +02:00
|
|
|
select {
|
|
|
|
case s.newInputs <- sweeperInput:
|
|
|
|
case <-s.quit:
|
2019-05-17 04:52:46 +02:00
|
|
|
return nil, ErrSweeperShuttingDown
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sweeperInput.resultChan, nil
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:27:42 +02:00
|
|
|
// removeConflictSweepDescendants removes any transactions from the wallet that
|
|
|
|
// spend outputs included in the passed outpoint set. This needs to be done in
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
// cases where we're not the only ones that can sweep an output, but there may
|
|
|
|
// exist unconfirmed spends that spend outputs created by a sweep transaction.
|
|
|
|
// The most common case for this is when someone sweeps our anchor outputs
|
2023-07-01 12:27:42 +02:00
|
|
|
// after 16 blocks. Moreover this is also needed for wallets which use neutrino
|
|
|
|
// as a backend when a channel is force closed and anchor cpfp txns are
|
|
|
|
// created to bump the initial commitment transaction. In this case an anchor
|
|
|
|
// cpfp is broadcasted for up to 3 commitment transactions (local,
|
|
|
|
// remote-dangling, remote). Using neutrino all of those transactions will be
|
|
|
|
// accepted (the commitment tx will be different in all of those cases) and have
|
|
|
|
// to be removed as soon as one of them confirmes (they do have the same
|
|
|
|
// ExclusiveGroup). For neutrino backends the corresponding BIP 157 serving full
|
|
|
|
// nodes do not signal invalid transactions anymore.
|
|
|
|
func (s *UtxoSweeper) removeConflictSweepDescendants(
|
|
|
|
outpoints map[wire.OutPoint]struct{}) error {
|
|
|
|
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
// Obtain all the past sweeps that we've done so far. We'll need these
|
|
|
|
// to ensure that if the spendingTx spends any of the same inputs, then
|
|
|
|
// we remove any transaction that may be spending those inputs from the
|
|
|
|
// wallet.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): can be last sweep here if we remove anything confirmed
|
|
|
|
// from the store?
|
|
|
|
pastSweepHashes, err := s.cfg.Store.ListSweeps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now go through each past transaction we published during this
|
|
|
|
// epoch and cross reference the spent inputs. If there're any inputs
|
|
|
|
// in common with the inputs the spendingTx spent, then we'll remove
|
|
|
|
// those.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): need to start to remove all transaction hashes after
|
|
|
|
// every N blocks (assumed point of no return)
|
|
|
|
for _, sweepHash := range pastSweepHashes {
|
|
|
|
sweepTx, err := s.cfg.Wallet.FetchTx(sweepHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction wasn't found in the wallet, may have already
|
|
|
|
// been replaced/removed.
|
|
|
|
if sweepTx == nil {
|
2023-04-14 23:58:13 +02:00
|
|
|
// If it was removed, then we'll play it safe and mark
|
|
|
|
// it as no longer need to be rebroadcasted.
|
|
|
|
s.cfg.Wallet.CancelRebroadcast(sweepHash)
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if this past sweep transaction spent any of the
|
|
|
|
// same inputs as spendingTx.
|
|
|
|
var isConflicting bool
|
|
|
|
for _, txIn := range sweepTx.TxIn {
|
2023-07-01 12:27:42 +02:00
|
|
|
if _, ok := outpoints[txIn.PreviousOutPoint]; ok {
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
isConflicting = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:27:42 +02:00
|
|
|
if !isConflicting {
|
|
|
|
continue
|
|
|
|
}
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
|
2023-07-01 12:27:42 +02:00
|
|
|
// If it is conflicting, then we'll signal the wallet to remove
|
|
|
|
// all the transactions that are descendants of outputs created
|
|
|
|
// by the sweepTx and the sweepTx itself.
|
|
|
|
log.Debugf("Removing sweep txid=%v from wallet: %v",
|
|
|
|
sweepTx.TxHash(), spew.Sdump(sweepTx))
|
2023-04-14 23:58:13 +02:00
|
|
|
|
2023-07-01 12:27:42 +02:00
|
|
|
err = s.cfg.Wallet.RemoveDescendants(sweepTx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to remove descendants: %v", err)
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
}
|
2023-07-01 12:27:42 +02:00
|
|
|
|
|
|
|
// If this transaction was conflicting, then we'll stop
|
|
|
|
// rebroadcasting it in the background.
|
|
|
|
s.cfg.Wallet.CancelRebroadcast(sweepHash)
|
sweep: remove all unconfirmed descendant transactions when a sweep conflicts
Before this commit, we we were trying to sweep an anchor output, and
that output was spent by someone else (not the sweeper), then we would
report this back to the original resolver (allowing it to be cleaned
up), and also remove the set of inputs spent by that transaction from
the set we need to sweep.
However, it's possible that if a user is spending unconfirmed outputs,
then the wallet is holding onto an invalid transaction, as the outputs
that were used as inputs have been double spent elsewhere.
In this commit, we fix this issue by recursively removing all descendant
transactions of our past sweeps that have an intersecting input set as
the spending transaction. In cases where a user spent an unconfirmed
output to funding a channel, and that output was a descendant of the now
swept anchor output, the funds will now properly be marked as available.
Fixes #6241
2022-02-18 03:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// collector is the sweeper main loop. It processes new inputs, spend
|
|
|
|
// notifications and counts down to publication of the sweep tx.
|
2019-10-07 13:21:25 +02:00
|
|
|
func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
|
|
|
|
// We registered for the block epochs with a nil request. The notifier
|
|
|
|
// should send us the current best block immediately. So we need to wait
|
|
|
|
// for it here because we need to know the current best height.
|
|
|
|
select {
|
|
|
|
case bestBlock := <-blockEpochs:
|
2023-10-24 01:14:55 +02:00
|
|
|
s.currentHeight = bestBlock.Height
|
2019-10-07 13:21:25 +02:00
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
for {
|
2023-10-24 07:47:14 +02:00
|
|
|
// Clean inputs, which will remove inputs that are swept,
|
|
|
|
// failed, or excluded from the sweeper and return inputs that
|
|
|
|
// are either new or has been published but failed back, which
|
|
|
|
// will be retried again here.
|
2024-04-08 09:45:03 +02:00
|
|
|
s.updateSweeperInputs()
|
2023-10-24 07:47:14 +02:00
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
select {
|
2023-08-08 08:04:22 +02:00
|
|
|
// A new inputs is offered to the sweeper. We check to see if
|
|
|
|
// we are already trying to sweep this input and if not, set up
|
|
|
|
// a listener to spend and schedule a sweep.
|
2018-10-23 12:05:48 +02:00
|
|
|
case input := <-s.newInputs:
|
2024-03-27 10:51:45 +01:00
|
|
|
err := s.handleNewInput(input)
|
|
|
|
if err != nil {
|
|
|
|
log.Criticalf("Unable to handle new input: %v",
|
|
|
|
err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2018-10-23 12:05:48 +02:00
|
|
|
|
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.
At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.
Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2023-10-26 10:00:13 +02:00
|
|
|
// If this input is forced, we perform an sweep
|
|
|
|
// immediately.
|
2024-07-30 22:28:43 +02:00
|
|
|
//
|
|
|
|
// TODO(ziggie): Make sure when `immediate` is selected
|
|
|
|
// as a parameter that we only trigger the sweeping of
|
|
|
|
// this specific input rather than triggering the sweeps
|
|
|
|
// of all current pending inputs registered with the
|
|
|
|
// sweeper.
|
2024-04-02 19:09:14 +02:00
|
|
|
if input.params.Immediate {
|
2024-04-08 09:45:03 +02:00
|
|
|
inputs := s.updateSweeperInputs()
|
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.
At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.
Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2023-10-26 10:00:13 +02:00
|
|
|
s.sweepPendingInputs(inputs)
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// A spend of one of our inputs is detected. Signal sweep
|
|
|
|
// results to the caller(s).
|
|
|
|
case spend := <-s.spendChan:
|
sweep: simplify polling logic in sweeper
This commit attempts to make the polling logic in sweeper more linear.
Previously, the sweep's timer is reset/restarted in multiple places,
such as when a new input comes in, or a new block comes in, or a
previous input being spent, making it difficult to follow. We now remove
the old timer and replaces it with a simple polling logic - we will
schedule sweeps every 5s(default), and if there's no input to be swept,
we'd skip, just like the previous `scheduleSweep` does.
It's also worthy noting that, although `scheduleSweep` triggers the
timer to tick, by the time we do the actual sweep in `sweepCluster`,
conditions may have changed. This is now also fixed because we only have
one place to create the clusters and sweeps.
2023-10-12 12:04:14 +02:00
|
|
|
s.handleInputSpent(spend)
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2019-05-17 04:52:46 +02:00
|
|
|
// A new external request has been received to retrieve all of
|
|
|
|
// the inputs we're currently attempting to sweep.
|
|
|
|
case req := <-s.pendingSweepsReqs:
|
2024-03-20 08:05:25 +01:00
|
|
|
s.handlePendingSweepsReq(req)
|
2019-05-17 04:52:46 +02:00
|
|
|
|
2019-05-29 23:00:14 +02:00
|
|
|
// A new external request has been received to bump the fee rate
|
|
|
|
// of a given input.
|
2020-01-09 14:41:28 +01:00
|
|
|
case req := <-s.updateReqs:
|
2023-10-24 01:14:55 +02:00
|
|
|
resultChan, err := s.handleUpdateReq(req)
|
2020-01-09 14:41:28 +01:00
|
|
|
req.responseChan <- &updateResp{
|
2019-05-29 23:00:14 +02:00
|
|
|
resultChan: resultChan,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
|
2024-04-09 14:05:00 +02:00
|
|
|
// Perform an sweep immediately if asked.
|
|
|
|
if req.params.Immediate {
|
|
|
|
inputs := s.updateSweeperInputs()
|
|
|
|
s.sweepPendingInputs(inputs)
|
|
|
|
}
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
case result := <-s.bumpResultChan:
|
|
|
|
// Handle the bump event.
|
|
|
|
err := s.handleBumpEvent(result)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to handle bump event: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A new block comes in, update the bestHeight, perform a check
|
|
|
|
// over all pending inputs and publish sweeping txns if needed.
|
2018-10-23 12:05:48 +02:00
|
|
|
case epoch, ok := <-blockEpochs:
|
|
|
|
if !ok {
|
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.
At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.
Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2023-10-26 10:00:13 +02:00
|
|
|
// We should stop the sweeper before stopping
|
|
|
|
// the chain service. Otherwise it indicates an
|
|
|
|
// error.
|
|
|
|
log.Error("Block epoch channel closed")
|
|
|
|
|
2019-10-07 13:21:25 +02:00
|
|
|
return
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.
At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.
Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2023-10-26 10:00:13 +02:00
|
|
|
// Update the sweeper to the best height.
|
2023-10-24 01:14:55 +02:00
|
|
|
s.currentHeight = epoch.Height
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2024-04-08 09:45:03 +02:00
|
|
|
// Update the inputs with the latest height.
|
|
|
|
inputs := s.updateSweeperInputs()
|
|
|
|
|
sweep: make sweeper block-driven instead of time-driven
This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.
At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.
Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
2023-10-26 10:00:13 +02:00
|
|
|
log.Debugf("Received new block: height=%v, attempt "+
|
|
|
|
"sweeping %d inputs", epoch.Height, len(inputs))
|
|
|
|
|
|
|
|
// Attempt to sweep any pending inputs.
|
|
|
|
s.sweepPendingInputs(inputs)
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
case <-s.quit:
|
2019-10-07 13:21:25 +02:00
|
|
|
return
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:51:13 +01:00
|
|
|
// removeExclusiveGroup removes all inputs in the given exclusive group. This
|
|
|
|
// function is called when one of the exclusive group inputs has been spent. The
|
|
|
|
// other inputs won't ever be spendable and can be removed. This also prevents
|
2023-07-01 12:27:42 +02:00
|
|
|
// them from being part of future sweep transactions that would fail. In
|
|
|
|
// addition sweep transactions of those inputs will be removed from the wallet.
|
2019-12-09 11:51:13 +01:00
|
|
|
func (s *UtxoSweeper) removeExclusiveGroup(group uint64) {
|
2024-03-17 20:00:58 +01:00
|
|
|
for outpoint, input := range s.inputs {
|
2019-12-09 11:51:13 +01:00
|
|
|
outpoint := outpoint
|
|
|
|
|
|
|
|
// Skip inputs that aren't exclusive.
|
|
|
|
if input.params.ExclusiveGroup == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip inputs from other exclusive groups.
|
|
|
|
if *input.params.ExclusiveGroup != group {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// Skip inputs that are already terminated.
|
|
|
|
if input.terminated() {
|
|
|
|
log.Tracef("Skipped sending error result for "+
|
|
|
|
"input %v, state=%v", outpoint, input.state)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:51:13 +01:00
|
|
|
// Signal result channels.
|
2023-10-24 07:47:14 +02:00
|
|
|
s.signalResult(input, Result{
|
2019-12-09 11:51:13 +01:00
|
|
|
Err: ErrExclusiveGroupSpend,
|
|
|
|
})
|
2023-07-01 12:27:42 +02:00
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// Update the input's state as it can no longer be swept.
|
2024-03-17 19:49:58 +01:00
|
|
|
input.state = Excluded
|
2023-10-24 07:47:14 +02:00
|
|
|
|
2023-07-01 12:27:42 +02:00
|
|
|
// Remove all unconfirmed transactions from the wallet which
|
|
|
|
// spend the passed outpoint of the same exclusive group.
|
|
|
|
outpoints := map[wire.OutPoint]struct{}{
|
|
|
|
outpoint: {},
|
|
|
|
}
|
|
|
|
err := s.removeConflictSweepDescendants(outpoints)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to remove conflicting sweep tx from "+
|
|
|
|
"wallet for outpoint %v : %v", outpoint, err)
|
|
|
|
}
|
2019-12-09 11:51:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// signalResult notifies the listeners of the final result of the input sweep.
|
|
|
|
// It also cancels any pending spend notification.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (s *UtxoSweeper) signalResult(pi *SweeperInput, result Result) {
|
2023-10-24 07:47:14 +02:00
|
|
|
op := pi.OutPoint()
|
|
|
|
listeners := pi.listeners
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
if result.Err == nil {
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Dispatching sweep success for %v to %v listeners",
|
2023-10-24 07:47:14 +02:00
|
|
|
op, len(listeners),
|
2018-10-23 12:05:48 +02:00
|
|
|
)
|
|
|
|
} else {
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Dispatching sweep error for %v to %v listeners: %v",
|
2023-10-24 07:47:14 +02:00
|
|
|
op, len(listeners), result.Err,
|
2018-10-23 12:05:48 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal all listeners. Channel is buffered. Because we only send once
|
|
|
|
// on every channel, it should never block.
|
|
|
|
for _, resultChan := range listeners {
|
|
|
|
resultChan <- result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel spend notification with chain notifier. This is not necessary
|
|
|
|
// in case of a success, except for that a reorg could still happen.
|
2023-10-24 07:47:14 +02:00
|
|
|
if pi.ntfnRegCancel != nil {
|
|
|
|
log.Debugf("Canceling spend ntfn for %v", op)
|
2018-10-23 12:05:48 +02:00
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
pi.ntfnRegCancel()
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// sweep takes a set of preselected inputs, creates a sweep tx and publishes
|
|
|
|
// the tx. The output address is only marked as used if the publish succeeds.
|
2023-10-30 13:43:33 +01:00
|
|
|
func (s *UtxoSweeper) sweep(set InputSet) error {
|
2019-05-02 01:06:19 +02:00
|
|
|
// Generate an output script if there isn't an unused script available.
|
2024-06-04 08:05:19 +02:00
|
|
|
if s.currentOutputScript.IsNone() {
|
|
|
|
addr, err := s.cfg.GenSweepScript().Unpack()
|
2018-10-23 12:05:48 +02:00
|
|
|
if err != nil {
|
2024-02-26 12:19:38 +01:00
|
|
|
return fmt.Errorf("gen sweep script: %w", err)
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
2024-06-04 08:05:19 +02:00
|
|
|
s.currentOutputScript = fn.Some(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepAddr, err := s.currentOutputScript.UnwrapOrErr(
|
|
|
|
fmt.Errorf("none sweep script"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Create a fee bump request and ask the publisher to broadcast it. The
|
|
|
|
// publisher will then take over and start monitoring the tx for
|
|
|
|
// potential fee bump.
|
|
|
|
req := &BumpRequest{
|
|
|
|
Inputs: set.Inputs(),
|
|
|
|
Budget: set.Budget(),
|
2024-03-27 12:22:02 +01:00
|
|
|
DeadlineHeight: set.DeadlineHeight(),
|
2024-06-04 08:06:17 +02:00
|
|
|
DeliveryAddress: sweepAddr,
|
2024-01-17 10:21:09 +01:00
|
|
|
MaxFeeRate: s.cfg.MaxFeeRate.FeePerKWeight(),
|
2024-04-11 11:08:36 +02:00
|
|
|
StartingFeeRate: set.StartingFeeRate(),
|
2024-01-17 10:21:09 +01:00
|
|
|
// TODO(yy): pass the strategy here.
|
2023-10-25 12:45:25 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 04:44:46 +02:00
|
|
|
// Reschedule the inputs that we just tried to sweep. This is done in
|
|
|
|
// case the following publish fails, we'd like to update the inputs'
|
|
|
|
// publish attempts and rescue them in the next sweep.
|
2024-02-21 09:00:05 +01:00
|
|
|
s.markInputsPendingPublish(set)
|
2023-10-23 04:44:46 +02:00
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Broadcast will return a read-only chan that we will listen to for
|
|
|
|
// this publish result and future RBF attempt.
|
|
|
|
resp, err := s.cfg.Publisher.Broadcast(req)
|
2022-08-29 03:27:12 +02:00
|
|
|
if err != nil {
|
2024-01-16 17:20:24 +01:00
|
|
|
outpoints := make([]wire.OutPoint, len(set.Inputs()))
|
|
|
|
for i, inp := range set.Inputs() {
|
2024-03-27 10:07:48 +01:00
|
|
|
outpoints[i] = inp.OutPoint()
|
2024-01-16 17:20:24 +01:00
|
|
|
}
|
|
|
|
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Errorf("Initial broadcast failed: %v, inputs=\n%v", err,
|
|
|
|
inputTypeSummary(set.Inputs()))
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// TODO(yy): find out which input is causing the failure.
|
2024-01-16 17:20:24 +01:00
|
|
|
s.markInputsPublishFailed(outpoints)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2023-10-23 04:44:46 +02:00
|
|
|
return err
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
// Successfully sent the broadcast attempt, we now handle the result by
|
|
|
|
// subscribing to the result chan and listen for future updates about
|
|
|
|
// this tx.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.monitorFeeBumpResult(resp)
|
2023-10-23 04:44:46 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:00:05 +01:00
|
|
|
// markInputsPendingPublish updates the pending inputs with the given tx
|
|
|
|
// inputs. It also increments the `publishAttempts`.
|
|
|
|
func (s *UtxoSweeper) markInputsPendingPublish(set InputSet) {
|
2018-10-23 12:05:48 +02:00
|
|
|
// Reschedule sweep.
|
2024-02-21 09:00:05 +01:00
|
|
|
for _, input := range set.Inputs() {
|
2024-03-27 10:07:48 +01:00
|
|
|
pi, ok := s.inputs[input.OutPoint()]
|
2018-10-23 12:05:48 +02:00
|
|
|
if !ok {
|
2024-01-11 00:54:32 +01:00
|
|
|
// It could be that this input is an additional wallet
|
|
|
|
// input that was attached. In that case there also
|
|
|
|
// isn't a pending input to update.
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Skipped marking input as pending "+
|
2024-01-11 00:54:32 +01:00
|
|
|
"published: %v not found in pending inputs",
|
2024-02-21 09:00:05 +01:00
|
|
|
input.OutPoint())
|
2024-01-11 00:54:32 +01:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this input has already terminated, there's clearly
|
|
|
|
// something wrong as it would have been removed. In this case
|
|
|
|
// we log an error and skip marking this input as pending
|
|
|
|
// publish.
|
|
|
|
if pi.terminated() {
|
|
|
|
log.Errorf("Expect input %v to not have terminated "+
|
|
|
|
"state, instead it has %v",
|
2024-02-21 09:00:05 +01:00
|
|
|
input.OutPoint, pi.state)
|
2024-01-11 00:54:32 +01:00
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// Update the input's state.
|
2024-03-17 19:49:58 +01:00
|
|
|
pi.state = PendingPublish
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
// Record another publish attempt.
|
|
|
|
pi.publishAttempts++
|
|
|
|
}
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// markInputsPublished updates the sweeping tx in db and marks the list of
|
|
|
|
// inputs as published.
|
|
|
|
func (s *UtxoSweeper) markInputsPublished(tr *TxRecord,
|
|
|
|
inputs []*wire.TxIn) error {
|
|
|
|
|
|
|
|
// Mark this tx in db once successfully published.
|
|
|
|
//
|
|
|
|
// NOTE: this will behave as an overwrite, which is fine as the record
|
|
|
|
// is small.
|
|
|
|
tr.Published = true
|
|
|
|
err := s.cfg.Store.StoreTx(tr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("store tx: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reschedule sweep.
|
|
|
|
for _, input := range inputs {
|
2024-03-17 20:00:58 +01:00
|
|
|
pi, ok := s.inputs[input.PreviousOutPoint]
|
2023-10-24 02:14:52 +02:00
|
|
|
if !ok {
|
2024-01-11 00:54:32 +01:00
|
|
|
// It could be that this input is an additional wallet
|
|
|
|
// input that was attached. In that case there also
|
|
|
|
// isn't a pending input to update.
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Skipped marking input as published: %v "+
|
2023-10-24 02:14:52 +02:00
|
|
|
"not found in pending inputs",
|
|
|
|
input.PreviousOutPoint)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valdiate that the input is in an expected state.
|
2024-03-17 19:49:58 +01:00
|
|
|
if pi.state != PendingPublish {
|
2024-04-01 15:28:54 +02:00
|
|
|
// We may get a Published if this is a replacement tx.
|
|
|
|
log.Debugf("Expect input %v to have %v, instead it "+
|
2023-10-24 02:14:52 +02:00
|
|
|
"has %v", input.PreviousOutPoint,
|
2024-03-17 19:49:58 +01:00
|
|
|
PendingPublish, pi.state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the input's state.
|
2024-03-17 19:49:58 +01:00
|
|
|
pi.state = Published
|
2024-04-11 11:01:31 +02:00
|
|
|
|
|
|
|
// Update the input's latest fee rate.
|
|
|
|
pi.lastFeeRate = chainfee.SatPerKWeight(tr.FeeRate)
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// markInputsPublishFailed marks the list of inputs as failed to be published.
|
2024-01-16 17:20:24 +01:00
|
|
|
func (s *UtxoSweeper) markInputsPublishFailed(outpoints []wire.OutPoint) {
|
2023-10-24 02:14:52 +02:00
|
|
|
// Reschedule sweep.
|
2024-01-16 17:20:24 +01:00
|
|
|
for _, op := range outpoints {
|
2024-03-17 20:00:58 +01:00
|
|
|
pi, ok := s.inputs[op]
|
2023-10-24 02:14:52 +02:00
|
|
|
if !ok {
|
2024-01-11 00:54:32 +01:00
|
|
|
// It could be that this input is an additional wallet
|
|
|
|
// input that was attached. In that case there also
|
|
|
|
// isn't a pending input to update.
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Skipped marking input as publish failed: "+
|
2024-01-16 17:20:24 +01:00
|
|
|
"%v not found in pending inputs", op)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valdiate that the input is in an expected state.
|
2024-04-08 11:46:18 +02:00
|
|
|
if pi.state != PendingPublish && pi.state != Published {
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Debugf("Expect input %v to have %v, instead it "+
|
2024-03-17 19:49:58 +01:00
|
|
|
"has %v", op, PendingPublish, pi.state)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-16 17:20:24 +01:00
|
|
|
log.Warnf("Failed to publish input %v", op)
|
2023-10-24 02:14:52 +02:00
|
|
|
|
|
|
|
// Update the input's state.
|
2024-03-17 19:49:58 +01:00
|
|
|
pi.state = PublishFailed
|
2023-10-24 02:14:52 +02:00
|
|
|
}
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 08:09:57 +02:00
|
|
|
// monitorSpend registers a spend notification with the chain notifier. It
|
2018-10-23 12:05:48 +02:00
|
|
|
// returns a cancel function that can be used to cancel the registration.
|
2023-08-08 08:09:57 +02:00
|
|
|
func (s *UtxoSweeper) monitorSpend(outpoint wire.OutPoint,
|
2018-10-23 12:05:48 +02:00
|
|
|
script []byte, heightHint uint32) (func(), error) {
|
|
|
|
|
2022-08-29 03:27:12 +02:00
|
|
|
log.Tracef("Wait for spend of %v at heightHint=%v",
|
|
|
|
outpoint, heightHint)
|
2018-10-23 12:05:48 +02:00
|
|
|
|
|
|
|
spendEvent, err := s.cfg.Notifier.RegisterSpendNtfn(
|
|
|
|
&outpoint, script, heightHint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-02-26 12:19:38 +01:00
|
|
|
return nil, fmt.Errorf("register spend ntfn: %w", err)
|
2018-10-23 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.wg.Done()
|
2022-11-21 13:07:56 +01:00
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
select {
|
|
|
|
case spend, ok := <-spendEvent.Spend:
|
|
|
|
if !ok {
|
|
|
|
log.Debugf("Spend ntfn for %v canceled",
|
|
|
|
outpoint)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
log.Debugf("Delivering spend ntfn for %v", outpoint)
|
|
|
|
|
2018-10-23 12:05:48 +02:00
|
|
|
select {
|
|
|
|
case s.spendChan <- spend:
|
|
|
|
log.Debugf("Delivered spend ntfn for %v",
|
|
|
|
outpoint)
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
}
|
|
|
|
case <-s.quit:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return spendEvent.Cancel, nil
|
2018-09-26 07:03:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-17 04:52:46 +02:00
|
|
|
// PendingInputs returns the set of inputs that the UtxoSweeper is currently
|
|
|
|
// attempting to sweep.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (s *UtxoSweeper) PendingInputs() (
|
|
|
|
map[wire.OutPoint]*PendingInputResponse, error) {
|
|
|
|
|
|
|
|
respChan := make(chan map[wire.OutPoint]*PendingInputResponse, 1)
|
2020-12-09 12:24:01 +01:00
|
|
|
errChan := make(chan error, 1)
|
2019-05-17 04:52:46 +02:00
|
|
|
select {
|
|
|
|
case s.pendingSweepsReqs <- &pendingSweepsReq{
|
|
|
|
respChan: respChan,
|
2020-12-09 12:24:01 +01:00
|
|
|
errChan: errChan,
|
2019-05-17 04:52:46 +02:00
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
return nil, ErrSweeperShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case pendingSweeps := <-respChan:
|
|
|
|
return pendingSweeps, nil
|
2020-12-09 12:24:01 +01:00
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
2019-05-17 04:52:46 +02:00
|
|
|
case <-s.quit:
|
|
|
|
return nil, ErrSweeperShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePendingSweepsReq handles a request to retrieve all pending inputs the
|
|
|
|
// UtxoSweeper is attempting to sweep.
|
|
|
|
func (s *UtxoSweeper) handlePendingSweepsReq(
|
2024-03-17 20:20:37 +01:00
|
|
|
req *pendingSweepsReq) map[wire.OutPoint]*PendingInputResponse {
|
2019-05-17 04:52:46 +02:00
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
resps := make(map[wire.OutPoint]*PendingInputResponse, len(s.inputs))
|
|
|
|
for _, inp := range s.inputs {
|
2019-05-17 04:52:46 +02:00
|
|
|
// Only the exported fields are set, as we expect the response
|
|
|
|
// to only be consumed externally.
|
2024-03-27 10:07:48 +01:00
|
|
|
op := inp.OutPoint()
|
2024-03-17 20:20:37 +01:00
|
|
|
resps[op] = &PendingInputResponse{
|
2019-05-17 04:52:46 +02:00
|
|
|
OutPoint: op,
|
2024-03-17 20:20:37 +01:00
|
|
|
WitnessType: inp.WitnessType(),
|
2019-05-17 04:52:46 +02:00
|
|
|
Amount: btcutil.Amount(
|
2024-03-17 20:20:37 +01:00
|
|
|
inp.SignDesc().Output.Value,
|
2019-05-17 04:52:46 +02:00
|
|
|
),
|
2024-03-17 20:20:37 +01:00
|
|
|
LastFeeRate: inp.lastFeeRate,
|
|
|
|
BroadcastAttempts: inp.publishAttempts,
|
|
|
|
Params: inp.params,
|
2024-04-15 21:15:34 +02:00
|
|
|
DeadlineHeight: uint32(inp.DeadlineHeight),
|
2019-05-17 04:52:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:05:25 +01:00
|
|
|
select {
|
|
|
|
case req.respChan <- resps:
|
|
|
|
case <-s.quit:
|
|
|
|
log.Debug("Skipped sending pending sweep response due to " +
|
|
|
|
"UtxoSweeper shutting down")
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
return resps
|
2019-05-17 04:52:46 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// UpdateParams allows updating the sweep parameters of a pending input in the
|
|
|
|
// UtxoSweeper. This function can be used to provide an updated fee preference
|
2020-09-04 11:53:24 +02:00
|
|
|
// and force flag that will be used for a new sweep transaction of the input
|
|
|
|
// that will act as a replacement transaction (RBF) of the original sweeping
|
|
|
|
// transaction, if any. The exclusive group is left unchanged.
|
2019-05-29 23:00:14 +02:00
|
|
|
//
|
|
|
|
// NOTE: This currently doesn't do any fee rate validation to ensure that a bump
|
|
|
|
// is actually successful. The responsibility of doing so should be handled by
|
|
|
|
// the caller.
|
2020-01-09 14:41:28 +01:00
|
|
|
func (s *UtxoSweeper) UpdateParams(input wire.OutPoint,
|
2024-04-09 14:05:00 +02:00
|
|
|
params Params) (chan Result, error) {
|
2019-05-29 23:00:14 +02:00
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
responseChan := make(chan *updateResp, 1)
|
2019-05-29 23:00:14 +02:00
|
|
|
select {
|
2020-01-09 14:41:28 +01:00
|
|
|
case s.updateReqs <- &updateReq{
|
|
|
|
input: input,
|
|
|
|
params: params,
|
|
|
|
responseChan: responseChan,
|
2019-05-29 23:00:14 +02:00
|
|
|
}:
|
|
|
|
case <-s.quit:
|
|
|
|
return nil, ErrSweeperShuttingDown
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case response := <-responseChan:
|
|
|
|
return response.resultChan, response.err
|
|
|
|
case <-s.quit:
|
|
|
|
return nil, ErrSweeperShuttingDown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:41:28 +01:00
|
|
|
// handleUpdateReq handles an update request by simply updating the sweep
|
|
|
|
// parameters of the pending input. Currently, no validation is done on the new
|
|
|
|
// fee preference to ensure it will properly create a replacement transaction.
|
2019-05-29 23:00:14 +02:00
|
|
|
//
|
|
|
|
// TODO(wilmer):
|
2022-08-22 20:58:42 +02:00
|
|
|
// - Validate fee preference to ensure we'll create a valid replacement
|
2019-05-29 23:00:14 +02:00
|
|
|
// transaction to allow the new fee rate to propagate throughout the
|
|
|
|
// network.
|
2022-08-22 20:58:42 +02:00
|
|
|
// - Ensure we don't combine this input with any other unconfirmed inputs that
|
2019-05-29 23:00:14 +02:00
|
|
|
// did not exist in the original sweep transaction, resulting in an invalid
|
|
|
|
// replacement transaction.
|
2023-10-24 01:14:55 +02:00
|
|
|
func (s *UtxoSweeper) handleUpdateReq(req *updateReq) (
|
2020-01-09 14:41:28 +01:00
|
|
|
chan Result, error) {
|
2019-05-29 23:00:14 +02:00
|
|
|
|
|
|
|
// If the UtxoSweeper is already trying to sweep this input, then we can
|
|
|
|
// simply just increase its fee rate. This will allow the input to be
|
|
|
|
// batched with others which also have a similar fee rate, creating a
|
|
|
|
// higher fee rate transaction that replaces the original input's
|
|
|
|
// sweeping transaction.
|
2024-03-17 20:20:37 +01:00
|
|
|
sweeperInput, ok := s.inputs[req.input]
|
2019-05-29 23:00:14 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, lnwallet.ErrNotMine
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:14:26 +01:00
|
|
|
// Create the updated parameters struct. Leave the exclusive group
|
|
|
|
// unchanged.
|
2024-04-11 11:08:50 +02:00
|
|
|
newParams := Params{
|
|
|
|
StartingFeeRate: req.params.StartingFeeRate,
|
|
|
|
Immediate: req.params.Immediate,
|
|
|
|
Budget: req.params.Budget,
|
|
|
|
DeadlineHeight: req.params.DeadlineHeight,
|
|
|
|
ExclusiveGroup: sweeperInput.params.ExclusiveGroup,
|
|
|
|
}
|
2020-03-11 18:14:26 +01:00
|
|
|
|
2023-10-26 09:13:23 +02:00
|
|
|
log.Debugf("Updating parameters for %v(state=%v) from (%v) to (%v)",
|
2024-03-17 20:20:37 +01:00
|
|
|
req.input, sweeperInput.state, sweeperInput.params, newParams)
|
2019-05-29 23:00:14 +02:00
|
|
|
|
2024-03-17 20:20:37 +01:00
|
|
|
sweeperInput.params = newParams
|
2019-05-29 23:00:14 +02:00
|
|
|
|
2023-10-26 09:13:23 +02:00
|
|
|
// We need to reset the state so this input will be attempted again by
|
|
|
|
// our sweeper.
|
2019-05-29 23:00:14 +02:00
|
|
|
//
|
2023-10-26 09:13:23 +02:00
|
|
|
// TODO(yy): a dedicated state?
|
2024-03-17 20:20:37 +01:00
|
|
|
sweeperInput.state = Init
|
2019-05-29 23:00:14 +02:00
|
|
|
|
2024-04-15 21:15:34 +02:00
|
|
|
// If the new input specifies a deadline, update the deadline height.
|
|
|
|
sweeperInput.DeadlineHeight = req.params.DeadlineHeight.UnwrapOr(
|
|
|
|
sweeperInput.DeadlineHeight,
|
|
|
|
)
|
|
|
|
|
2019-05-29 23:00:14 +02:00
|
|
|
resultChan := make(chan Result, 1)
|
2024-03-17 20:20:37 +01:00
|
|
|
sweeperInput.listeners = append(sweeperInput.listeners, resultChan)
|
2019-05-29 23:00:14 +02:00
|
|
|
|
|
|
|
return resultChan, nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 07:05:47 +01:00
|
|
|
// ListSweeps returns a list of the sweeps recorded by the sweep store.
|
2020-05-05 21:10:11 +02:00
|
|
|
func (s *UtxoSweeper) ListSweeps() ([]chainhash.Hash, error) {
|
|
|
|
return s.cfg.Store.ListSweeps()
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:14:21 +02:00
|
|
|
// mempoolLookup takes an input's outpoint and queries the mempool to see
|
|
|
|
// whether it's already been spent in a transaction found in the mempool.
|
|
|
|
// Returns the transaction if found.
|
2024-02-24 05:21:54 +01:00
|
|
|
func (s *UtxoSweeper) mempoolLookup(op wire.OutPoint) fn.Option[wire.MsgTx] {
|
2023-10-24 07:14:21 +02:00
|
|
|
// For neutrino backend, there's no mempool available, so we exit
|
|
|
|
// early.
|
|
|
|
if s.cfg.Mempool == nil {
|
|
|
|
log.Debugf("Skipping mempool lookup for %v, no mempool ", op)
|
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
return fn.None[wire.MsgTx]()
|
2023-10-24 07:14:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-24 05:21:54 +01:00
|
|
|
// Query this input in the mempool. If this outpoint is already spent
|
|
|
|
// in mempool, we should get a spending event back immediately.
|
|
|
|
return s.cfg.Mempool.LookupInputMempoolSpend(op)
|
2023-10-24 07:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 08:04:22 +02:00
|
|
|
// handleNewInput processes a new input by registering spend notification and
|
|
|
|
// scheduling sweeping for it.
|
2024-03-27 10:51:45 +01:00
|
|
|
func (s *UtxoSweeper) handleNewInput(input *sweepInputMessage) error {
|
2024-04-15 21:15:34 +02:00
|
|
|
// Create a default deadline height, which will be used when there's no
|
|
|
|
// DeadlineHeight specified for a given input.
|
|
|
|
defaultDeadline := s.currentHeight + int32(s.cfg.NoDeadlineConfTarget)
|
|
|
|
|
2024-03-27 10:07:48 +01:00
|
|
|
outpoint := input.input.OutPoint()
|
2024-03-17 20:00:58 +01:00
|
|
|
pi, pending := s.inputs[outpoint]
|
2023-08-08 08:04:22 +02:00
|
|
|
if pending {
|
2024-01-09 07:06:43 +01:00
|
|
|
log.Debugf("Already has pending input %v received", outpoint)
|
2023-08-08 08:04:22 +02:00
|
|
|
|
2023-10-24 07:14:21 +02:00
|
|
|
s.handleExistingInput(input, pi)
|
2023-08-08 08:04:22 +02:00
|
|
|
|
2024-03-27 10:51:45 +01:00
|
|
|
return nil
|
2023-08-08 08:04:22 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// This is a new input, and we want to query the mempool to see if this
|
|
|
|
// input has already been spent. If so, we'll start the input with
|
|
|
|
// state Published and attach the RBFInfo.
|
2024-03-27 10:07:48 +01:00
|
|
|
state, rbfInfo := s.decideStateAndRBFInfo(input.input.OutPoint())
|
2024-03-20 00:31:41 +01:00
|
|
|
|
2023-08-08 08:04:22 +02:00
|
|
|
// Create a new pendingInput and initialize the listeners slice with
|
|
|
|
// the passed in result channel. If this input is offered for sweep
|
|
|
|
// again, the result channel will be appended to this slice.
|
2024-03-17 20:20:37 +01:00
|
|
|
pi = &SweeperInput{
|
2024-03-20 00:31:41 +01:00
|
|
|
state: state,
|
2023-10-26 09:13:23 +02:00
|
|
|
listeners: []chan Result{input.resultChan},
|
|
|
|
Input: input.input,
|
|
|
|
params: input.params,
|
2024-03-20 00:31:41 +01:00
|
|
|
rbf: rbfInfo,
|
2024-04-15 21:15:34 +02:00
|
|
|
// Set the acutal deadline height.
|
|
|
|
DeadlineHeight: input.params.DeadlineHeight.UnwrapOr(
|
|
|
|
defaultDeadline,
|
|
|
|
),
|
2023-08-08 08:04:22 +02:00
|
|
|
}
|
2023-10-24 07:14:21 +02:00
|
|
|
|
2024-03-17 20:00:58 +01:00
|
|
|
s.inputs[outpoint] = pi
|
|
|
|
log.Tracef("input %v, state=%v, added to inputs", outpoint, pi.state)
|
2023-08-08 08:04:22 +02:00
|
|
|
|
|
|
|
// Start watching for spend of this input, either by us or the remote
|
|
|
|
// party.
|
2023-08-08 08:09:57 +02:00
|
|
|
cancel, err := s.monitorSpend(
|
2023-08-08 08:04:22 +02:00
|
|
|
outpoint, input.input.SignDesc().Output.PkScript,
|
|
|
|
input.input.HeightHint(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("wait for spend: %w", err)
|
2024-02-27 20:10:49 +01:00
|
|
|
s.markInputFailed(pi, err)
|
2023-08-08 08:04:22 +02:00
|
|
|
|
2024-03-27 10:51:45 +01:00
|
|
|
return err
|
2023-08-08 08:04:22 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 07:14:21 +02:00
|
|
|
pi.ntfnRegCancel = cancel
|
2024-03-27 10:51:45 +01:00
|
|
|
|
|
|
|
return nil
|
2023-08-08 08:04:22 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// decideStateAndRBFInfo queries the mempool to see whether the given input has
|
|
|
|
// already been spent. If so, the state Published will be returned, otherwise
|
|
|
|
// state Init. When spent, it will query the sweeper store to fetch the fee
|
|
|
|
// info of the spending transction, and construct an RBFInfo based on it.
|
|
|
|
// Suppose an error occurs, fn.None is returned.
|
|
|
|
func (s *UtxoSweeper) decideStateAndRBFInfo(op wire.OutPoint) (
|
|
|
|
SweepState, fn.Option[RBFInfo]) {
|
|
|
|
|
2023-10-26 08:27:46 +02:00
|
|
|
// Check if we can find the spending tx of this input in mempool.
|
2024-03-20 00:31:41 +01:00
|
|
|
txOption := s.mempoolLookup(op)
|
|
|
|
|
|
|
|
// Extract the spending tx from the option.
|
|
|
|
var tx *wire.MsgTx
|
|
|
|
txOption.WhenSome(func(t wire.MsgTx) {
|
|
|
|
tx = &t
|
|
|
|
})
|
2023-10-26 08:27:46 +02:00
|
|
|
|
|
|
|
// Exit early if it's not found.
|
|
|
|
//
|
|
|
|
// NOTE: this is not accurate for backends that don't support mempool
|
|
|
|
// lookup:
|
|
|
|
// - for neutrino we don't have a mempool.
|
|
|
|
// - for btcd below v0.24.1 we don't have `gettxspendingprevout`.
|
2024-03-20 00:31:41 +01:00
|
|
|
if tx == nil {
|
2024-03-17 19:49:58 +01:00
|
|
|
return Init, fn.None[RBFInfo]()
|
2023-10-26 08:27:46 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Otherwise the input is already spent in the mempool, so eventually
|
2024-03-17 19:49:58 +01:00
|
|
|
// we will return Published.
|
2024-03-20 00:31:41 +01:00
|
|
|
//
|
2023-10-26 08:27:46 +02:00
|
|
|
// We also need to update the RBF info for this input. If the sweeping
|
|
|
|
// transaction is broadcast by us, we can find the fee info in the
|
|
|
|
// sweeper store.
|
|
|
|
txid := tx.TxHash()
|
|
|
|
tr, err := s.cfg.Store.GetTx(txid)
|
|
|
|
|
|
|
|
// If the tx is not found in the store, it means it's not broadcast by
|
|
|
|
// us, hence we can't find the fee info. This is fine as, later on when
|
2024-03-17 20:20:37 +01:00
|
|
|
// this tx is confirmed, we will remove the input from our inputs.
|
2023-10-26 08:27:46 +02:00
|
|
|
if errors.Is(err, ErrTxNotFound) {
|
|
|
|
log.Warnf("Spending tx %v not found in sweeper store", txid)
|
2024-03-17 19:49:58 +01:00
|
|
|
return Published, fn.None[RBFInfo]()
|
2023-10-26 08:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exit if we get an db error.
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to get tx %v from sweeper store: %v",
|
|
|
|
txid, err)
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
return Published, fn.None[RBFInfo]()
|
2023-10-26 08:27:46 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 00:31:41 +01:00
|
|
|
// Prepare the fee info and return it.
|
|
|
|
rbf := fn.Some(RBFInfo{
|
2023-10-26 08:27:46 +02:00
|
|
|
Txid: txid,
|
|
|
|
Fee: btcutil.Amount(tr.Fee),
|
|
|
|
FeeRate: chainfee.SatPerKWeight(tr.FeeRate),
|
|
|
|
})
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
return Published, rbf
|
2023-10-26 08:27:46 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 08:04:22 +02:00
|
|
|
// handleExistingInput processes an input that is already known to the sweeper.
|
|
|
|
// It will overwrite the params of the old input with the new ones.
|
|
|
|
func (s *UtxoSweeper) handleExistingInput(input *sweepInputMessage,
|
2024-03-17 20:20:37 +01:00
|
|
|
oldInput *SweeperInput) {
|
2023-08-08 08:04:22 +02:00
|
|
|
|
|
|
|
// Before updating the input details, check if an exclusive group was
|
|
|
|
// set. In case the same input is registered again without an exclusive
|
|
|
|
// group set, the previous input and its sweep parameters are outdated
|
|
|
|
// hence need to be replaced. This scenario currently only happens for
|
|
|
|
// anchor outputs. When a channel is force closed, in the worst case 3
|
|
|
|
// different sweeps with the same exclusive group are registered with
|
|
|
|
// the sweeper to bump the closing transaction (cpfp) when its time
|
|
|
|
// critical. Receiving an input which was already registered with the
|
|
|
|
// sweeper but now without an exclusive group means non of the previous
|
|
|
|
// inputs were used as CPFP, so we need to make sure we update the
|
|
|
|
// sweep parameters but also remove all inputs with the same exclusive
|
|
|
|
// group because the are outdated too.
|
|
|
|
var prevExclGroup *uint64
|
|
|
|
if oldInput.params.ExclusiveGroup != nil &&
|
|
|
|
input.params.ExclusiveGroup == nil {
|
|
|
|
|
|
|
|
prevExclGroup = new(uint64)
|
|
|
|
*prevExclGroup = *oldInput.params.ExclusiveGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update input details and sweep parameters. The re-offered input
|
|
|
|
// details may contain a change to the unconfirmed parent tx info.
|
|
|
|
oldInput.params = input.params
|
|
|
|
oldInput.Input = input.input
|
|
|
|
|
2024-04-15 21:15:34 +02:00
|
|
|
// If the new input specifies a deadline, update the deadline height.
|
|
|
|
oldInput.DeadlineHeight = input.params.DeadlineHeight.UnwrapOr(
|
|
|
|
oldInput.DeadlineHeight,
|
|
|
|
)
|
|
|
|
|
2023-08-08 08:04:22 +02:00
|
|
|
// Add additional result channel to signal spend of this input.
|
|
|
|
oldInput.listeners = append(oldInput.listeners, input.resultChan)
|
|
|
|
|
|
|
|
if prevExclGroup != nil {
|
|
|
|
s.removeExclusiveGroup(*prevExclGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 10:54:52 +02:00
|
|
|
// handleInputSpent takes a spend event of our input and updates the sweeper's
|
|
|
|
// internal state to remove the input.
|
sweep: simplify polling logic in sweeper
This commit attempts to make the polling logic in sweeper more linear.
Previously, the sweep's timer is reset/restarted in multiple places,
such as when a new input comes in, or a new block comes in, or a
previous input being spent, making it difficult to follow. We now remove
the old timer and replaces it with a simple polling logic - we will
schedule sweeps every 5s(default), and if there's no input to be swept,
we'd skip, just like the previous `scheduleSweep` does.
It's also worthy noting that, although `scheduleSweep` triggers the
timer to tick, by the time we do the actual sweep in `sweepCluster`,
conditions may have changed. This is now also fixed because we only have
one place to create the clusters and sweeps.
2023-10-12 12:04:14 +02:00
|
|
|
func (s *UtxoSweeper) handleInputSpent(spend *chainntnfs.SpendDetail) {
|
2023-10-12 10:54:52 +02:00
|
|
|
// Query store to find out if we ever published this tx.
|
|
|
|
spendHash := *spend.SpenderTxHash
|
|
|
|
isOurTx, err := s.cfg.Store.IsOurTx(spendHash)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot determine if tx %v is ours: %v",
|
|
|
|
spendHash, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't our transaction, it means someone else swept outputs
|
|
|
|
// that we were attempting to sweep. This can happen for anchor outputs
|
|
|
|
// as well as justice transactions. In this case, we'll notify the
|
|
|
|
// wallet to remove any spends that descent from this output.
|
|
|
|
if !isOurTx {
|
2023-07-01 12:27:42 +02:00
|
|
|
// Construct a map of the inputs this transaction spends.
|
|
|
|
spendingTx := spend.SpendingTx
|
|
|
|
inputsSpent := make(
|
|
|
|
map[wire.OutPoint]struct{}, len(spendingTx.TxIn),
|
|
|
|
)
|
|
|
|
for _, txIn := range spendingTx.TxIn {
|
|
|
|
inputsSpent[txIn.PreviousOutPoint] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Attempting to remove descendant txns invalidated "+
|
|
|
|
"by (txid=%v): %v", spendingTx.TxHash(),
|
|
|
|
spew.Sdump(spendingTx))
|
|
|
|
|
|
|
|
err := s.removeConflictSweepDescendants(inputsSpent)
|
2023-10-12 10:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to remove descendant transactions "+
|
|
|
|
"due to tx %v: ", spendHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Detected third party spend related to in flight "+
|
2023-07-01 12:27:42 +02:00
|
|
|
"inputs (is_ours=%v): %v", isOurTx,
|
2024-07-25 16:18:00 +02:00
|
|
|
lnutils.SpewLogClosure(spend.SpendingTx))
|
2023-10-12 10:54:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// We now use the spending tx to update the state of the inputs.
|
|
|
|
s.markInputsSwept(spend.SpendingTx, isOurTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// markInputsSwept marks all inputs swept by the spending transaction as swept.
|
|
|
|
// It will also notify all the subscribers of this input.
|
2024-01-11 00:54:32 +01:00
|
|
|
func (s *UtxoSweeper) markInputsSwept(tx *wire.MsgTx, isOurTx bool) {
|
2023-10-24 02:14:52 +02:00
|
|
|
for _, txIn := range tx.TxIn {
|
2023-10-12 10:54:52 +02:00
|
|
|
outpoint := txIn.PreviousOutPoint
|
|
|
|
|
|
|
|
// Check if this input is known to us. It could probably be
|
2024-03-17 20:20:37 +01:00
|
|
|
// unknown if we canceled the registration, deleted from inputs
|
|
|
|
// map but the ntfn was in-flight already. Or this could be not
|
|
|
|
// one of our inputs.
|
2024-03-17 20:00:58 +01:00
|
|
|
input, ok := s.inputs[outpoint]
|
2023-10-12 10:54:52 +02:00
|
|
|
if !ok {
|
2024-01-11 00:54:32 +01:00
|
|
|
// It's very likely that a spending tx contains inputs
|
|
|
|
// that we don't know.
|
2024-05-20 16:02:13 +02:00
|
|
|
log.Tracef("Skipped marking input as swept: %v not "+
|
2024-01-11 00:54:32 +01:00
|
|
|
"found in pending inputs", outpoint)
|
|
|
|
|
2023-10-12 10:54:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
// This input may already been marked as swept by a previous
|
|
|
|
// spend notification, which is likely to happen as one sweep
|
|
|
|
// transaction usually sweeps multiple inputs.
|
2023-10-24 07:47:14 +02:00
|
|
|
if input.terminated() {
|
2024-01-11 00:54:32 +01:00
|
|
|
log.Debugf("Skipped marking input as swept: %v "+
|
|
|
|
"state=%v", outpoint, input.state)
|
2023-10-24 07:47:14 +02:00
|
|
|
|
2023-10-24 02:14:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
input.state = Swept
|
2023-10-24 02:14:52 +02:00
|
|
|
|
2023-10-12 10:54:52 +02:00
|
|
|
// Return either a nil or a remote spend result.
|
|
|
|
var err error
|
|
|
|
if !isOurTx {
|
2023-10-24 07:47:14 +02:00
|
|
|
log.Warnf("Input=%v was spent by remote or third "+
|
|
|
|
"party in tx=%v", outpoint, tx.TxHash())
|
2023-10-12 10:54:52 +02:00
|
|
|
err = ErrRemoteSpend
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal result channels.
|
2023-10-24 07:47:14 +02:00
|
|
|
s.signalResult(input, Result{
|
2023-10-24 02:14:52 +02:00
|
|
|
Tx: tx,
|
2023-10-12 10:54:52 +02:00
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Remove all other inputs in this exclusive group.
|
|
|
|
if input.params.ExclusiveGroup != nil {
|
|
|
|
s.removeExclusiveGroup(*input.params.ExclusiveGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 20:10:49 +01:00
|
|
|
// markInputFailed marks the given input as failed and won't be retried. It
|
|
|
|
// will also notify all the subscribers of this input.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (s *UtxoSweeper) markInputFailed(pi *SweeperInput, err error) {
|
2024-02-27 20:10:49 +01:00
|
|
|
log.Errorf("Failed to sweep input: %v, error: %v", pi, err)
|
|
|
|
|
2024-03-17 19:49:58 +01:00
|
|
|
pi.state = Failed
|
2024-02-27 20:10:49 +01:00
|
|
|
|
|
|
|
// Remove all other inputs in this exclusive group.
|
|
|
|
if pi.params.ExclusiveGroup != nil {
|
|
|
|
s.removeExclusiveGroup(*pi.params.ExclusiveGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.signalResult(pi, Result{Err: err})
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// updateSweeperInputs updates the sweeper's internal state and returns a map
|
|
|
|
// of inputs to be swept. It will remove the inputs that are in final states,
|
2024-03-17 19:49:58 +01:00
|
|
|
// and returns a map of inputs that have either state Init or PublishFailed.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (s *UtxoSweeper) updateSweeperInputs() InputsMap {
|
2023-10-24 07:47:14 +02:00
|
|
|
// Create a map of inputs to be swept.
|
2024-03-17 20:20:37 +01:00
|
|
|
inputs := make(InputsMap)
|
2023-10-24 07:47:14 +02:00
|
|
|
|
|
|
|
// Iterate the pending inputs and update the sweeper's state.
|
|
|
|
//
|
|
|
|
// TODO(yy): sweeper is made to communicate via go channels, so no
|
|
|
|
// locks are needed to access the map. However, it'd be safer if we
|
2024-03-17 20:20:37 +01:00
|
|
|
// turn this inputs map into a SyncMap in case we wanna add concurrent
|
|
|
|
// access to the map in the future.
|
2024-03-17 20:00:58 +01:00
|
|
|
for op, input := range s.inputs {
|
2023-10-24 07:47:14 +02:00
|
|
|
// If the input has reached a final state, that it's either
|
|
|
|
// been swept, or failed, or excluded, we will remove it from
|
|
|
|
// our sweeper.
|
|
|
|
if input.terminated() {
|
|
|
|
log.Debugf("Removing input(State=%v) %v from sweeper",
|
|
|
|
input.state, op)
|
|
|
|
|
2024-03-17 20:00:58 +01:00
|
|
|
delete(s.inputs, op)
|
2023-10-24 07:47:14 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this input has been included in a sweep tx that's not
|
|
|
|
// published yet, we'd skip this input and wait for the sweep
|
|
|
|
// tx to be published.
|
2024-03-17 19:49:58 +01:00
|
|
|
if input.state == PendingPublish {
|
2023-10-24 07:47:14 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this input has already been published, we will need to
|
|
|
|
// check the RBF condition before attempting another sweeping.
|
2024-03-17 19:49:58 +01:00
|
|
|
if input.state == Published {
|
2023-10-24 07:47:14 +02:00
|
|
|
continue
|
2024-03-20 09:48:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the input has a locktime that's not yet reached, we will
|
|
|
|
// skip this input and wait for the locktime to be reached.
|
|
|
|
locktime, _ := input.RequiredLockTime()
|
|
|
|
if uint32(s.currentHeight) < locktime {
|
|
|
|
log.Warnf("Skipping input %v due to locktime=%v not "+
|
|
|
|
"reached, current height is %v", op, locktime,
|
|
|
|
s.currentHeight)
|
|
|
|
|
|
|
|
continue
|
2023-10-24 07:47:14 +02:00
|
|
|
}
|
|
|
|
|
2024-04-08 09:45:03 +02:00
|
|
|
// If the input has a CSV that's not yet reached, we will skip
|
|
|
|
// this input and wait for the expiry.
|
|
|
|
locktime = input.BlocksToMaturity() + input.HeightHint()
|
|
|
|
if s.currentHeight < int32(locktime)-1 {
|
|
|
|
log.Infof("Skipping input %v due to CSV expiry=%v not "+
|
|
|
|
"reached, current height is %v", op, locktime,
|
|
|
|
s.currentHeight)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-24 07:47:14 +02:00
|
|
|
// If this input is new or has been failed to be published,
|
|
|
|
// we'd retry it. The assumption here is that when an error is
|
|
|
|
// returned from `PublishTransaction`, it means the tx has
|
|
|
|
// failed to meet the policy, hence it's not in the mempool.
|
|
|
|
inputs[op] = input
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweepPendingInputs is called when the ticker fires. It will create clusters
|
|
|
|
// and attempt to create and publish the sweeping transactions.
|
2024-03-17 20:20:37 +01:00
|
|
|
func (s *UtxoSweeper) sweepPendingInputs(inputs InputsMap) {
|
2023-11-01 10:52:53 +01:00
|
|
|
// Cluster all of our inputs based on the specific Aggregator.
|
2024-04-15 21:15:34 +02:00
|
|
|
sets := s.cfg.Aggregator.ClusterInputs(inputs)
|
2023-11-01 10:52:53 +01:00
|
|
|
|
|
|
|
// sweepWithLock is a helper closure that executes the sweep within a
|
|
|
|
// coin select lock to prevent the coins being selected for other
|
|
|
|
// transactions like funding of a channel.
|
|
|
|
sweepWithLock := func(set InputSet) error {
|
|
|
|
return s.cfg.Wallet.WithCoinSelectLock(func() error {
|
|
|
|
// Try to add inputs from our wallet.
|
|
|
|
err := set.AddWalletInputs(s.cfg.Wallet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-30 14:32:46 +01:00
|
|
|
|
2023-11-01 10:52:53 +01:00
|
|
|
// Create sweeping transaction for each set.
|
|
|
|
err = s.sweep(set)
|
2023-10-30 14:32:46 +01:00
|
|
|
if err != nil {
|
2023-11-01 10:52:53 +01:00
|
|
|
return err
|
2023-10-30 14:32:46 +01:00
|
|
|
}
|
2023-11-01 10:52:53 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, set := range sets {
|
|
|
|
var err error
|
|
|
|
if set.NeedWalletInput() {
|
|
|
|
// Sweep the set of inputs that need the wallet inputs.
|
|
|
|
err = sweepWithLock(set)
|
|
|
|
} else {
|
|
|
|
// Sweep the set of inputs that don't need the wallet
|
|
|
|
// inputs.
|
|
|
|
err = s.sweep(set)
|
2023-10-12 11:02:40 +02:00
|
|
|
}
|
2023-10-30 14:32:46 +01:00
|
|
|
|
2023-11-01 10:52:53 +01:00
|
|
|
if err != nil {
|
2024-03-18 18:14:37 +01:00
|
|
|
log.Errorf("Failed to sweep %v: %v", set, err)
|
2023-11-01 10:52:53 +01:00
|
|
|
}
|
2023-10-12 11:02:40 +02:00
|
|
|
}
|
|
|
|
}
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// monitorFeeBumpResult subscribes to the passed result chan to listen for
|
|
|
|
// future updates about the sweeping tx.
|
|
|
|
//
|
|
|
|
// NOTE: must run as a goroutine.
|
|
|
|
func (s *UtxoSweeper) monitorFeeBumpResult(resultChan <-chan *BumpResult) {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-resultChan:
|
|
|
|
// Validate the result is valid.
|
|
|
|
if err := r.Validate(); err != nil {
|
|
|
|
log.Errorf("Received invalid result: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the result back to the main event loop.
|
|
|
|
select {
|
|
|
|
case s.bumpResultChan <- r:
|
|
|
|
case <-s.quit:
|
|
|
|
log.Debug("Sweeper shutting down, skip " +
|
|
|
|
"sending bump result")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sweeping tx has been confirmed, we can exit the
|
|
|
|
// monitor now.
|
|
|
|
//
|
|
|
|
// TODO(yy): can instead remove the spend subscription
|
|
|
|
// in sweeper and rely solely on this event to mark
|
|
|
|
// inputs as Swept?
|
|
|
|
if r.Event == TxConfirmed || r.Event == TxFailed {
|
|
|
|
log.Debugf("Received %v for sweep tx %v, exit "+
|
|
|
|
"fee bump monitor", r.Event,
|
|
|
|
r.Tx.TxHash())
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// Cancel the rebroadcasting of the failed tx.
|
|
|
|
s.cfg.Wallet.CancelRebroadcast(r.Tx.TxHash())
|
|
|
|
|
2024-01-17 10:21:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
log.Debugf("Sweeper shutting down, exit fee " +
|
|
|
|
"bump handler")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleBumpEventTxFailed handles the case where the tx has been failed to
|
|
|
|
// publish.
|
|
|
|
func (s *UtxoSweeper) handleBumpEventTxFailed(r *BumpResult) error {
|
|
|
|
tx, err := r.Tx, r.Err
|
|
|
|
|
|
|
|
log.Errorf("Fee bump attempt failed for tx=%v: %v", tx.TxHash(), err)
|
|
|
|
|
|
|
|
outpoints := make([]wire.OutPoint, 0, len(tx.TxIn))
|
|
|
|
for _, inp := range tx.TxIn {
|
|
|
|
outpoints = append(outpoints, inp.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(yy): should we also remove the failed tx from db?
|
|
|
|
s.markInputsPublishFailed(outpoints)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleBumpEventTxReplaced handles the case where the sweeping tx has been
|
|
|
|
// replaced by a new one.
|
|
|
|
func (s *UtxoSweeper) handleBumpEventTxReplaced(r *BumpResult) error {
|
|
|
|
oldTx := r.ReplacedTx
|
|
|
|
newTx := r.Tx
|
|
|
|
|
|
|
|
// Prepare a new record to replace the old one.
|
|
|
|
tr := &TxRecord{
|
|
|
|
Txid: newTx.TxHash(),
|
|
|
|
FeeRate: uint64(r.FeeRate),
|
|
|
|
Fee: uint64(r.Fee),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the old record for logging purpose.
|
|
|
|
oldTxid := oldTx.TxHash()
|
|
|
|
record, err := s.cfg.Store.GetTx(oldTxid)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Fetch tx record for %v: %v", oldTxid, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-21 13:34:43 +01:00
|
|
|
// Cancel the rebroadcasting of the replaced tx.
|
|
|
|
s.cfg.Wallet.CancelRebroadcast(oldTxid)
|
|
|
|
|
2024-03-21 08:24:02 +01:00
|
|
|
log.Infof("RBFed tx=%v(fee=%v sats, feerate=%v sats/kw) with new "+
|
|
|
|
"tx=%v(fee=%v, "+"feerate=%v)", record.Txid, record.Fee,
|
|
|
|
record.FeeRate, tr.Txid, tr.Fee, tr.FeeRate)
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
// The old sweeping tx has been replaced by a new one, we will update
|
|
|
|
// the tx record in the sweeper db.
|
|
|
|
//
|
|
|
|
// TODO(yy): we may also need to update the inputs in this tx to a new
|
|
|
|
// state. Suppose a replacing tx only spends a subset of the inputs
|
2024-03-17 19:49:58 +01:00
|
|
|
// here, we'd end up with the rest being marked as `Published` and
|
2024-01-17 10:21:09 +01:00
|
|
|
// won't be aggregated in the next sweep. Atm it's fine as we always
|
|
|
|
// RBF the same input set.
|
|
|
|
if err := s.cfg.Store.DeleteTx(oldTxid); err != nil {
|
|
|
|
log.Errorf("Delete tx record for %v: %v", oldTxid, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the inputs as published using the replacing tx.
|
|
|
|
return s.markInputsPublished(tr, r.Tx.TxIn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleBumpEventTxPublished handles the case where the sweeping tx has been
|
|
|
|
// successfully published.
|
|
|
|
func (s *UtxoSweeper) handleBumpEventTxPublished(r *BumpResult) error {
|
|
|
|
tx := r.Tx
|
|
|
|
tr := &TxRecord{
|
|
|
|
Txid: tx.TxHash(),
|
|
|
|
FeeRate: uint64(r.FeeRate),
|
|
|
|
Fee: uint64(r.Fee),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs have been successfully published so we update their
|
|
|
|
// states.
|
|
|
|
err := s.markInputsPublished(tr, tx.TxIn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Published sweep tx %v, num_inputs=%v, height=%v",
|
|
|
|
tx.TxHash(), len(tx.TxIn), s.currentHeight)
|
|
|
|
|
2024-06-04 08:05:19 +02:00
|
|
|
// If there's no error, remove the output script. Otherwise keep it so
|
|
|
|
// that it can be reused for the next transaction and causes no address
|
|
|
|
// inflation.
|
|
|
|
s.currentOutputScript = fn.None[lnwallet.AddrWithKey]()
|
2024-01-17 10:21:09 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleBumpEvent handles the result sent from the bumper based on its event
|
|
|
|
// type.
|
|
|
|
//
|
|
|
|
// NOTE: TxConfirmed event is not handled, since we already subscribe to the
|
|
|
|
// input's spending event, we don't need to do anything here.
|
|
|
|
func (s *UtxoSweeper) handleBumpEvent(r *BumpResult) error {
|
|
|
|
log.Debugf("Received bump event [%v] for tx %v", r.Event, r.Tx.TxHash())
|
|
|
|
|
|
|
|
switch r.Event {
|
|
|
|
// The tx has been published, we update the inputs' state and create a
|
|
|
|
// record to be stored in the sweeper db.
|
|
|
|
case TxPublished:
|
|
|
|
return s.handleBumpEventTxPublished(r)
|
|
|
|
|
|
|
|
// The tx has failed, we update the inputs' state.
|
|
|
|
case TxFailed:
|
|
|
|
return s.handleBumpEventTxFailed(r)
|
|
|
|
|
|
|
|
// The tx has been replaced, we will remove the old tx and replace it
|
|
|
|
// with the new one.
|
|
|
|
case TxReplaced:
|
|
|
|
return s.handleBumpEventTxReplaced(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-30 17:55:25 +01:00
|
|
|
|
|
|
|
// IsSweeperOutpoint determines whether the outpoint was created by the sweeper.
|
|
|
|
//
|
|
|
|
// NOTE: It is enough to check the txid because the sweeper will create
|
|
|
|
// outpoints which solely belong to the internal LND wallet.
|
|
|
|
func (s *UtxoSweeper) IsSweeperOutpoint(op wire.OutPoint) bool {
|
|
|
|
found, err := s.cfg.Store.IsOurTx(op.Hash)
|
|
|
|
// In case there is an error fetching the transaction details from the
|
|
|
|
// sweeper store we assume the outpoint is still used by the sweeper
|
|
|
|
// (worst case scenario).
|
|
|
|
//
|
|
|
|
// TODO(ziggie): Ensure that confirmed outpoints are deleted from the
|
|
|
|
// bucket.
|
|
|
|
if err != nil && !errors.Is(err, errNoTxHashesBucket) {
|
|
|
|
log.Errorf("failed to fetch info for outpoint(%v:%d) "+
|
|
|
|
"with: %v, we assume it is still in use by the sweeper",
|
|
|
|
op.Hash, op.Index, err)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|