mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-30 23:53:41 +02:00
sweep: add new interface Cluster
to manage grouping inputs
This commit adds a new interface `Cluster` to manage cluster-level inputs grouping. This new interface replaces the `inputCluster` and will be futher refactored so the sweeper can use a much smaller coin selection lock.
This commit is contained in:
@@ -37,112 +37,6 @@ type txInput interface {
|
||||
parameters() Params
|
||||
}
|
||||
|
||||
// inputSet is a set of inputs that can be used as the basis to generate a tx
|
||||
// on.
|
||||
type inputSet []input.Input
|
||||
|
||||
// generateInputPartitionings goes through all given inputs and constructs sets
|
||||
// of inputs that can be used to generate a sensible transaction. Each set
|
||||
// contains up to the configured maximum number of inputs. Negative yield
|
||||
// inputs are skipped. No input sets with a total value after fees below the
|
||||
// dust limit are returned.
|
||||
func generateInputPartitionings(sweepableInputs []txInput,
|
||||
feePerKW, maxFeeRate chainfee.SatPerKWeight, maxInputsPerTx int,
|
||||
wallet Wallet) ([]inputSet, error) {
|
||||
|
||||
// Sort input by yield. We will start constructing input sets starting
|
||||
// with the highest yield inputs. This is to prevent the construction
|
||||
// of a set with an output below the dust limit, causing the sweep
|
||||
// process to stop, while there are still higher value inputs
|
||||
// available. It also allows us to stop evaluating more inputs when the
|
||||
// first input in this ordering is encountered with a negative yield.
|
||||
//
|
||||
// Yield is calculated as the difference between value and added fee
|
||||
// for this input. The fee calculation excludes fee components that are
|
||||
// common to all inputs, as those wouldn't influence the order. The
|
||||
// single component that is differentiating is witness size.
|
||||
//
|
||||
// For witness size, the upper limit is taken. The actual size depends
|
||||
// on the signature length, which is not known yet at this point.
|
||||
yields := make(map[wire.OutPoint]int64)
|
||||
for _, input := range sweepableInputs {
|
||||
size, _, err := input.WitnessType().SizeUpperBound()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"failed adding input weight: %v", err)
|
||||
}
|
||||
|
||||
yields[*input.OutPoint()] = input.SignDesc().Output.Value -
|
||||
int64(feePerKW.FeeForWeight(int64(size)))
|
||||
}
|
||||
|
||||
sort.Slice(sweepableInputs, func(i, j int) bool {
|
||||
// Because of the specific ordering and termination condition
|
||||
// that is described above, we place force sweeps at the start
|
||||
// of the list. Otherwise we can't be sure that they will be
|
||||
// included in an input set.
|
||||
if sweepableInputs[i].parameters().Force {
|
||||
return true
|
||||
}
|
||||
|
||||
return yields[*sweepableInputs[i].OutPoint()] >
|
||||
yields[*sweepableInputs[j].OutPoint()]
|
||||
})
|
||||
|
||||
// Select blocks of inputs up to the configured maximum number.
|
||||
var sets []inputSet
|
||||
for len(sweepableInputs) > 0 {
|
||||
// Start building a set of positive-yield tx inputs under the
|
||||
// condition that the tx will be published with the specified
|
||||
// fee rate.
|
||||
txInputs := newTxInputSet(
|
||||
wallet, feePerKW, maxFeeRate, maxInputsPerTx,
|
||||
)
|
||||
|
||||
// From the set of sweepable inputs, keep adding inputs to the
|
||||
// input set until the tx output value no longer goes up or the
|
||||
// maximum number of inputs is reached.
|
||||
txInputs.addPositiveYieldInputs(sweepableInputs)
|
||||
|
||||
// If there are no positive yield inputs, we can stop here.
|
||||
inputCount := len(txInputs.inputs)
|
||||
if inputCount == 0 {
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
// Check the current output value and add wallet utxos if
|
||||
// needed to push the output value to the lower limit.
|
||||
if err := txInputs.tryAddWalletInputsIfNeeded(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If the output value of this block of inputs does not reach
|
||||
// the dust limit, stop sweeping. Because of the sorting,
|
||||
// continuing with the remaining inputs will only lead to sets
|
||||
// with an even lower output value.
|
||||
if !txInputs.enoughInput() {
|
||||
// The change output is always a p2tr here.
|
||||
dl := lnwallet.DustLimitForSize(input.P2TRSize)
|
||||
log.Debugf("Input set value %v (required=%v, "+
|
||||
"change=%v) below dust limit of %v",
|
||||
txInputs.totalOutput(), txInputs.requiredOutput,
|
||||
txInputs.changeOutput, dl)
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
log.Infof("Candidate sweep set of size=%v (+%v wallet inputs), "+
|
||||
"has yield=%v, weight=%v",
|
||||
inputCount, len(txInputs.inputs)-inputCount,
|
||||
txInputs.totalOutput()-txInputs.walletInputTotal,
|
||||
txInputs.weightEstimate(true).weight())
|
||||
|
||||
sets = append(sets, txInputs.inputs)
|
||||
sweepableInputs = sweepableInputs[inputCount:]
|
||||
}
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
// createSweepTx builds a signed tx spending the inputs to the given outputs,
|
||||
// sending any leftover change to the change script.
|
||||
func createSweepTx(inputs []input.Input, outputs []*wire.TxOut,
|
||||
|
Reference in New Issue
Block a user