sweep: wrap weight estimator

Preparation for a cpfp-aware weight estimator. For cpfp, a regular
weight estimator isn't sufficient, because it needs to take into account
the weight of the parent transaction(s) as well.
This commit is contained in:
Joost Jager
2020-09-04 14:03:14 +02:00
parent 2ebfb64b9b
commit 3e3d8487fb
3 changed files with 61 additions and 24 deletions

View File

@@ -33,7 +33,7 @@ const (
type txInputSetState struct {
// weightEstimate is the (worst case) tx weight with the current set of
// inputs.
weightEstimate input.TxWeightEstimator
weightEstimate *weightEstimator
// inputTotal is the total value of all inputs.
inputTotal btcutil.Amount
@@ -54,7 +54,7 @@ type txInputSetState struct {
func (t *txInputSetState) clone() txInputSetState {
s := txInputSetState{
weightEstimate: t.weightEstimate,
weightEstimate: t.weightEstimate.clone(),
inputTotal: t.inputTotal,
outputValue: t.outputValue,
walletInputTotal: t.walletInputTotal,
@@ -95,15 +95,20 @@ func newTxInputSet(wallet Wallet, feePerKW,
btcutil.Amount(relayFee.FeePerKVByte()),
)
state := txInputSetState{
weightEstimate: newWeightEstimator(),
}
b := txInputSet{
feePerKW: feePerKW,
dustLimit: dustLimit,
maxInputs: maxInputs,
wallet: wallet,
feePerKW: feePerKW,
dustLimit: dustLimit,
maxInputs: maxInputs,
wallet: wallet,
txInputSetState: state,
}
// Add the sweep tx output to the weight estimate.
b.weightEstimate.AddP2WKHOutput()
b.weightEstimate.addP2WKHOutput()
return &b
}
@@ -126,29 +131,22 @@ func (t *txInputSet) addToState(inp input.Input, constraints addConstraints) *tx
return nil
}
// Can ignore error, because it has already been checked when
// calculating the yields.
size, isNestedP2SH, _ := inp.WitnessType().SizeUpperBound()
// Clone the current set state.
s := t.clone()
// Add the new input.
s.inputs = append(s.inputs, inp)
// Add weight of the new input.
if isNestedP2SH {
s.weightEstimate.AddNestedP2WSHInput(size)
} else {
s.weightEstimate.AddWitnessInput(size)
}
// Can ignore error, because it has already been checked when
// calculating the yields.
_ = s.weightEstimate.add(inp)
// Add the value of the new input.
value := btcutil.Amount(inp.SignDesc().Output.Value)
s.inputTotal += value
// Recalculate the tx fee.
weight := s.weightEstimate.Weight()
weight := s.weightEstimate.weight()
fee := t.feePerKW.FeeForWeight(int64(weight))
// Calculate the new output value.