Skip to content

Commit ef45c6b

Browse files
committed
sweep: ensure we factor in extra change addrs in MaxFeeRateAllowed
1 parent a55905c commit ef45c6b

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

sweep/fee_bumper.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sweep
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"sync"
@@ -129,12 +130,29 @@ type BumpRequest struct {
129130
// compares it with the specified MaxFeeRate, and returns the smaller of the
130131
// two.
131132
func (r *BumpRequest) MaxFeeRateAllowed() (chainfee.SatPerKWeight, error) {
133+
// We'll want to know if we have any blobs, as we need to factor this
134+
// into the max fee rate for this bump request.
135+
hasBlobs := fn.Any(func(i input.Input) bool {
136+
return i.ResolutionBlob().IsSome()
137+
}, r.Inputs)
138+
139+
sweepAddrs := [][]byte{
140+
r.DeliveryAddress.DeliveryAddress,
141+
}
142+
143+
// If we have blobs, then we'll add an extra sweep addr for the size
144+
// estimate below. WE know that these blobs will also always be based on
145+
// p2tr addrs.
146+
if hasBlobs {
147+
sweepAddrs = append(
148+
sweepAddrs, bytes.Repeat([]byte{0}, input.P2TRSize),
149+
)
150+
}
151+
132152
// Get the size of the sweep tx, which will be used to calculate the
133153
// budget fee rate.
134-
//
135-
// TODO(roasbeef): also wants the extra change output?
136154
size, err := calcSweepTxWeight(
137-
r.Inputs, r.DeliveryAddress.DeliveryAddress,
155+
r.Inputs, sweepAddrs,
138156
)
139157
if err != nil {
140158
return 0, err
@@ -163,7 +181,7 @@ func (r *BumpRequest) MaxFeeRateAllowed() (chainfee.SatPerKWeight, error) {
163181
// calcSweepTxWeight calculates the weight of the sweep tx. It assumes a
164182
// sweeping tx always has a single output(change).
165183
func calcSweepTxWeight(inputs []input.Input,
166-
outputPkScript []byte) (lntypes.WeightUnit, error) {
184+
outputPkScript [][]byte) (lntypes.WeightUnit, error) {
167185

168186
// Use a const fee rate as we only use the weight estimator to
169187
// calculate the size.
@@ -177,7 +195,7 @@ func calcSweepTxWeight(inputs []input.Input,
177195
// TODO(yy): we should refactor the weight estimator to not require a
178196
// fee rate and max fee rate and make it a pure tx weight calculator.
179197
_, estimator, err := getWeightEstimate(
180-
inputs, nil, feeRate, 0, [][]byte{outputPkScript},
198+
inputs, nil, feeRate, 0, outputPkScript,
181199
)
182200
if err != nil {
183201
return 0, err

sweep/fee_bumper_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ func TestCalcSweepTxWeight(t *testing.T) {
115115
inp := createTestInput(100, input.WitnessKeyHash)
116116

117117
// Use a wrong change script to test the error case.
118-
weight, err := calcSweepTxWeight([]input.Input{&inp}, []byte{0})
118+
weight, err := calcSweepTxWeight(
119+
[]input.Input{&inp}, [][]byte{{0x00}},
120+
)
119121
require.Error(t, err)
120122
require.Zero(t, weight)
121123

122124
// Use a correct change script to test the success case.
123125
weight, err = calcSweepTxWeight(
124-
[]input.Input{&inp}, changePkScript.DeliveryAddress,
126+
[]input.Input{&inp}, [][]byte{changePkScript.DeliveryAddress},
125127
)
126128
require.NoError(t, err)
127129

@@ -143,7 +145,7 @@ func TestBumpRequestMaxFeeRateAllowed(t *testing.T) {
143145

144146
// The weight is 487.
145147
weight, err := calcSweepTxWeight(
146-
[]input.Input{&inp}, changePkScript.DeliveryAddress,
148+
[]input.Input{&inp}, [][]byte{changePkScript.DeliveryAddress},
147149
)
148150
require.NoError(t, err)
149151

sweep/tx_input_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestNewBudgetInputSet(t *testing.T) {
9292
rt.ErrorContains(err, "duplicate inputs")
9393
rt.Nil(set)
9494

95-
// Pass a slice of inputs that only one input has the deadline height,
95+
// Pass a slice of inputs that only one input has the deadline height.
9696
set, err = NewBudgetInputSet(
9797
[]SweeperInput{input0, input3}, testHeight,
9898
fn.None[AuxSweeper](),

0 commit comments

Comments
 (0)