Skip to content

Commit c891674

Browse files
committed
sweepbatcher: fix rounding in constructUnsignedTx
Previously it may round one satoshi towards zero resulting in fee-rate slightly lower than requested.
1 parent 9dcb6ca commit c891674

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,13 @@ func constructUnsignedTx(sweeps []sweep, address btcutil.Address,
11601160
weight := weightEstimate.Weight()
11611161
feeForWeight := feeRate.FeeForWeight(weight)
11621162

1163+
// Fee can be rounded towards zero, leading to actual feeRate being
1164+
// slightly lower than the requested value. Increase the fee if this is
1165+
// the case.
1166+
if chainfee.NewSatPerKWeight(feeForWeight, weight) < feeRate {
1167+
feeForWeight++
1168+
}
1169+
11631170
// Clamp the calculated fee to the max allowed fee amount for the batch.
11641171
fee := clampBatchFee(feeForWeight, batchAmt)
11651172

sweepbatcher/sweep_batch_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,44 @@ func TestConstructUnsignedTx(t *testing.T) {
295295
wantErr: "sweep.htlcSuccessEstimator failed: " +
296296
"weight estimator test failure",
297297
},
298+
299+
{
300+
name: "fix fee rounding",
301+
sweeps: []sweep{
302+
{
303+
outpoint: op1,
304+
value: 1_000_000,
305+
},
306+
{
307+
outpoint: op2,
308+
value: 2_000_000,
309+
},
310+
},
311+
address: destAddr,
312+
currentHeight: 800_000,
313+
feeRate: 253,
314+
wantTx: &wire.MsgTx{
315+
Version: 2,
316+
LockTime: 800_000,
317+
TxIn: []*wire.TxIn{
318+
{
319+
PreviousOutPoint: op1,
320+
},
321+
{
322+
PreviousOutPoint: op2,
323+
},
324+
},
325+
TxOut: []*wire.TxOut{
326+
{
327+
Value: 2999841,
328+
PkScript: batchPkScript,
329+
},
330+
},
331+
},
332+
wantWeight: 626,
333+
wantFeeForWeight: 159,
334+
wantFee: 159,
335+
},
298336
}
299337

300338
for _, tc := range cases {

0 commit comments

Comments
 (0)