Skip to content

Commit 547d263

Browse files
committed
fee: round up weight to the nearest satoshi
this commit fixes cases where a rounded down transaction weight leads to undershooting of the minimum relay fee.
1 parent 6b71582 commit 547d263

File tree

13 files changed

+21
-21
lines changed

13 files changed

+21
-21
lines changed

instantout/instantout.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (i *InstantOut) createHtlcTransaction(network *chaincfg.Params) (
169169

170170
// Estimate the fee
171171
weight := htlcWeight(len(inputReservations))
172-
fee := i.htlcFeeRate.FeeForWeight(weight)
172+
fee := i.htlcFeeRate.FeeForWeightRoundUp(weight)
173173
if fee > i.Value/5 {
174174
return nil, errors.New("fee is higher than 20% of " +
175175
"sweep value")
@@ -213,7 +213,7 @@ func (i *InstantOut) createSweeplessSweepTx(feerate chainfee.SatPerKWeight) (
213213

214214
// Estimate the fee
215215
weight := sweeplessSweepWeight(len(inputReservations))
216-
fee := feerate.FeeForWeight(weight)
216+
fee := feerate.FeeForWeightRoundUp(weight)
217217
if fee > i.Value/5 {
218218
return nil, errors.New("fee is higher than 20% of " +
219219
"sweep value")
@@ -381,7 +381,7 @@ func (i *InstantOut) generateHtlcSweepTx(ctx context.Context,
381381
return nil, err
382382
}
383383

384-
fee := feeRate.FeeForWeight(weightEstimator.Weight())
384+
fee := feeRate.FeeForWeightRoundUp(weightEstimator.Weight())
385385

386386
htlcOutValue := i.finalizedHtlcTx.TxOut[0].Value
387387
output := &wire.TxOut{

instantout/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (m *Manager) GetInstantOutQuote(ctx context.Context,
248248

249249
// The on chain chainFee is the chainFee rate times the estimated
250250
// sweepless sweep transaction size.
251-
chainFee := feeRate.FeeForWeight(sweeplessSweepWeight(len(reservationIDs)))
251+
chainFee := feeRate.FeeForWeightRoundUp(sweeplessSweepWeight(len(reservationIDs)))
252252

253253
return Quote{
254254
ServiceFee: btcutil.Amount(quoteRes.SwapFee),

liquidity/loopin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ func loopInSweepFee(fee chainfee.SatPerKWeight) btcutil.Amount {
7878

7979
estimator.AddWitnessInput(maxSize)
8080

81-
return fee.FeeForWeight(estimator.Weight())
81+
return fee.FeeForWeightRoundUp(estimator.Weight())
8282
}

loopout_feerate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s testSweeper) GetSweepFeeDetails(ctx context.Context,
7676
// Find weight.
7777
weight := weightEstimate.Weight()
7878

79-
return feeRate.FeeForWeight(weight), feeRate, weight, nil
79+
return feeRate.FeeForWeightRoundUp(weight), feeRate, weight, nil
8080
}
8181

8282
// TestLoopOutSweepFeerateProvider tests that loopOutSweepFeerateProvider

loopout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func testCustomSweepConfTarget(t *testing.T) {
436436
)
437437
require.NoError(t, err, "unable to retrieve fee estimate")
438438

439-
minFee := feeRate.FeeForWeight(lntypes.WeightUnit(weight))
439+
minFee := feeRate.FeeForWeightRoundUp(lntypes.WeightUnit(weight))
440440
// Just an estimate that works to sanity check fee upper bound.
441441
maxFee := btcutil.Amount(float64(minFee) * 1.5)
442442

staticaddr/deposit/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (f *FSM) PublishDepositExpirySweepAction(ctx context.Context,
4848

4949
weight := script.ExpirySpendWeight()
5050

51-
fee := feeRateEstimator.FeeForWeight(lntypes.WeightUnit(weight))
51+
fee := feeRateEstimator.FeeForWeightRoundUp(lntypes.WeightUnit(weight))
5252

5353
// We cap the fee at 20% of the deposit value.
5454
if fee > f.deposit.Value/5 {

staticaddr/loopin/actions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
212212
f.cfg.MaxStaticAddrHtlcBackupFeePercentage)
213213

214214
feeRate := chainfee.SatPerKWeight(loopInResp.StandardHtlcInfo.FeeRate)
215-
fee := feeRate.FeeForWeight(f.loopIn.htlcWeight())
215+
fee := feeRate.FeeForWeightRoundUp(f.loopIn.htlcWeight())
216216
if fee > maxHtlcTxFee {
217217
// Abort the swap by pushing empty sigs to the server.
218218
pushEmptySigs()
@@ -225,7 +225,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
225225
f.loopIn.HtlcTxFeeRate = feeRate
226226

227227
highFeeRate := chainfee.SatPerKWeight(loopInResp.HighFeeHtlcInfo.FeeRate)
228-
fee = highFeeRate.FeeForWeight(f.loopIn.htlcWeight())
228+
fee = highFeeRate.FeeForWeightRoundUp(f.loopIn.htlcWeight())
229229
if fee > maxHtlcTxBackupFee {
230230
// Abort the swap by pushing empty sigs to the server.
231231
pushEmptySigs()
@@ -241,7 +241,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
241241
extremelyHighFeeRate := chainfee.SatPerKWeight(
242242
loopInResp.ExtremeFeeHtlcInfo.FeeRate,
243243
)
244-
fee = extremelyHighFeeRate.FeeForWeight(f.loopIn.htlcWeight())
244+
fee = extremelyHighFeeRate.FeeForWeightRoundUp(f.loopIn.htlcWeight())
245245
if fee > maxHtlcTxBackupFee {
246246
// Abort the swap by pushing empty sigs to the server.
247247
pushEmptySigs()

staticaddr/loopin/loopin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (l *StaticAddressLoopIn) createHtlcTx(chainParams *chaincfg.Params,
285285

286286
// Calculate htlc tx fee for server provided fee rate.
287287
weight := l.htlcWeight()
288-
fee := feeRate.FeeForWeight(weight)
288+
fee := feeRate.FeeForWeightRoundUp(weight)
289289

290290
// Check if the server breaches our fee limits.
291291
amt := float64(l.TotalDepositAmount())
@@ -389,7 +389,7 @@ func (l *StaticAddressLoopIn) createHtlcSweepTx(ctx context.Context,
389389
return nil, err
390390
}
391391

392-
fee := feeRate.FeeForWeight(weightEstimator.Weight())
392+
fee := feeRate.FeeForWeightRoundUp(weightEstimator.Weight())
393393

394394
htlcOutValue := htlcTx.TxOut[0].Value
395395
output := &wire.TxOut{

staticaddr/withdraw/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ func (m *Manager) createWithdrawalTx(ctx context.Context,
826826
if err != nil {
827827
return nil, 0, 0, err
828828
}
829-
feeWithoutChange := feeRate.FeeForWeight(weight)
829+
feeWithoutChange := feeRate.FeeForWeightRoundUp(weight)
830830

831831
// If the user selected a fraction of the sum of the selected deposits
832832
// to withdraw, check if a change output is needed.
@@ -839,7 +839,7 @@ func (m *Manager) createWithdrawalTx(ctx context.Context,
839839
if err != nil {
840840
return nil, 0, 0, err
841841
}
842-
feeWithChange := feeRate.FeeForWeight(weight)
842+
feeWithChange := feeRate.FeeForWeightRoundUp(weight)
843843

844844
// The available change that can cover fees is the total
845845
// selected deposit amount minus the selected withdrawal amount.

sweep/sweeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
226226
weight := weightEstimate.Weight()
227227

228228
// Find fee.
229-
fee := feeRate.FeeForWeight(weight)
229+
fee := feeRate.FeeForWeightRoundUp(weight)
230230

231231
log.Debugf("Estimations for a tx (label=%s): weight=%v, fee=%v, "+
232232
"feerate=%v, sweepConfTarget=%d.", label, weight, fee, feeRate,

0 commit comments

Comments
 (0)