Skip to content

Commit 9202f08

Browse files
author
ffranr
authored
Merge pull request #1354 from lightninglabs/fee-rounding
Fee calculation rounding in wallet anchor
2 parents 3a0556c + f1ec4b8 commit 9202f08

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

itest/send_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,40 @@ func testMinRelayFeeBump(t *harnessTest) {
173173
SetNodeUTXOs(t, t.lndHarness.Alice, btcutil.Amount(1), initialUTXOs)
174174
defer ResetNodeWallet(t, t.lndHarness.Alice)
175175

176-
// Set the min relay fee to a higher value than the fee rate that will
177-
// be returned by the fee estimation.
178-
lowFeeRate := chainfee.SatPerVByte(1).FeePerKWeight()
179-
highMinRelayFeeRate := chainfee.SatPerVByte(2).FeePerKVByte()
180-
defaultMinRelayFeeRate := chainfee.SatPerVByte(1).FeePerKVByte()
176+
// Set the variables for the fee rates we'll use in this test.
177+
belowFloorFeeRate := chainfee.SatPerVByte(1).FeePerKWeight()
178+
belowMinRelayFeeRate := chainfee.SatPerKVByte(1500).FeePerKWeight()
179+
realWorldMinRelayFeeRate := chainfee.SatPerKVByte(1952)
180+
harnessMinRelayFeeRate := chainfee.SatPerKVByte(1000)
181181
defaultFeeRate := chainfee.SatPerKWeight(3125)
182-
t.lndHarness.SetFeeEstimateWithConf(lowFeeRate, 6)
183-
t.lndHarness.SetMinRelayFeerate(highMinRelayFeeRate)
182+
183+
// roundUpMinRelayFeeRate is the converted fee rate to sat/vbyte as
184+
// that's what the FundPsbt expects. We add 999 to round up to the
185+
// nearest 1000. We'll use this with `AssertFeerate`.
186+
roundUpMinRelayFeeRate := chainfee.SatPerVByte(
187+
(realWorldMinRelayFeeRate + 999) / 1000,
188+
)
189+
190+
t.lndHarness.SetFeeEstimateWithConf(belowFloorFeeRate, 6)
191+
t.lndHarness.SetMinRelayFeerate(realWorldMinRelayFeeRate)
184192

185193
// Reset all fee rates to their default value at the end of this test.
186-
defer t.lndHarness.SetMinRelayFeerate(defaultMinRelayFeeRate)
194+
defer t.lndHarness.SetMinRelayFeerate(harnessMinRelayFeeRate)
187195
defer t.lndHarness.SetFeeEstimateWithConf(defaultFeeRate, 6)
188196

189197
// First, we'll make a normal assets with enough units to allow us to
190198
// send it around a few times.
191199
MintAssetsConfirmBatch(
192200
t.t, t.lndHarness.Miner().Client, t.tapd,
193201
[]*mintrpc.MintAssetRequest{issuableAssets[0]},
194-
WithFeeRate(uint32(lowFeeRate)),
202+
WithFeeRate(uint32(belowFloorFeeRate)),
195203
WithError("manual fee rate below floor"),
196204
)
197205

198206
MintAssetsConfirmBatch(
199207
t.t, t.lndHarness.Miner().Client, t.tapd,
200208
[]*mintrpc.MintAssetRequest{issuableAssets[0]},
201-
WithFeeRate(uint32(lowFeeRate)+10),
209+
WithFeeRate(uint32(belowMinRelayFeeRate)),
202210
WithError("feerate does not meet minrelayfee"),
203211
)
204212

@@ -217,7 +225,7 @@ func testMinRelayFeeBump(t *harnessTest) {
217225
// We check whether the minting TX is bumped to the min relay fee.
218226
AssertFeeRate(
219227
t.t, t.lndHarness.Miner().Client, initialUTXOs[0].Amount,
220-
&mintOutpoint.Hash, highMinRelayFeeRate.FeePerKWeight(),
228+
&mintOutpoint.Hash, roundUpMinRelayFeeRate.FeePerKWeight(),
221229
)
222230

223231
// Now that we have the asset created, we'll make a new node that'll
@@ -250,13 +258,13 @@ func testMinRelayFeeBump(t *harnessTest) {
250258

251259
sendAsset(
252260
t, t.tapd, withReceiverAddresses(bobAddr),
253-
withFeeRate(uint32(lowFeeRate)),
261+
withFeeRate(uint32(belowFloorFeeRate)),
254262
withError("manual fee rate below floor"),
255263
)
256264

257265
sendAsset(
258266
t, t.tapd, withReceiverAddresses(bobAddr),
259-
withFeeRate(uint32(lowFeeRate)+10),
267+
withFeeRate(uint32(belowMinRelayFeeRate)),
260268
withError("feerate does not meet minrelayfee"),
261269
)
262270

@@ -271,7 +279,7 @@ func testMinRelayFeeBump(t *harnessTest) {
271279
sendInputAmt := initialUTXOs[1].Amount + 1000
272280
AssertTransferFeeRate(
273281
t.t, t.lndHarness.Miner().Client, sendResp, sendInputAmt,
274-
highMinRelayFeeRate.FeePerKWeight(),
282+
roundUpMinRelayFeeRate.FeePerKWeight(),
275283
)
276284

277285
AssertNonInteractiveRecvComplete(t.t, secondTapd, 1)

wallet_anchor.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,17 @@ func (l *LndRpcWalletAnchor) FundPsbt(ctx context.Context, packet *psbt.Packet,
7777
}
7878
}
7979

80+
// We'll convert the fee rate to sat/vbyte as that's what the FundPsbt
81+
// expects. We round up to the nearest whole unit to prevent issues
82+
// where the fee doesn't meet the min_relay_fee because of rounding
83+
// down.
84+
satPerVByte := uint64(math.Ceil(float64(feeRate.FeePerKVByte()) / 1000))
85+
8086
pkt, changeIndex, leasedUtxos, err := l.lnd.WalletKit.FundPsbt(
8187
ctx, &walletrpc.FundPsbtRequest{
8288
Template: fundTemplate,
8389
Fees: &walletrpc.FundPsbtRequest_SatPerVbyte{
84-
SatPerVbyte: uint64(feeRate.FeePerKVByte()) / 1000,
90+
SatPerVbyte: satPerVByte,
8591
},
8692
MinConfs: int32(minConfs),
8793
ChangeType: defaultChangeType,

0 commit comments

Comments
 (0)