Skip to content

Commit 149bffc

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 1983cc2 commit 149bffc

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

301339
for _, tc := range cases {

0 commit comments

Comments
 (0)