Skip to content

Fee calculation rounding in wallet anchor #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions itest/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,40 @@ func testMinRelayFeeBump(t *harnessTest) {
SetNodeUTXOs(t, t.lndHarness.Alice, btcutil.Amount(1), initialUTXOs)
defer ResetNodeWallet(t, t.lndHarness.Alice)

// Set the min relay fee to a higher value than the fee rate that will
// be returned by the fee estimation.
lowFeeRate := chainfee.SatPerVByte(1).FeePerKWeight()
highMinRelayFeeRate := chainfee.SatPerVByte(2).FeePerKVByte()
defaultMinRelayFeeRate := chainfee.SatPerVByte(1).FeePerKVByte()
// Set the variables for the fee rates we'll use in this test.
belowFloorFeeRate := chainfee.SatPerVByte(1).FeePerKWeight()
belowMinRelayFeeRate := chainfee.SatPerKVByte(1500).FeePerKWeight()
realWorldMinRelayFeeRate := chainfee.SatPerKVByte(1952)
harnessMinRelayFeeRate := chainfee.SatPerKVByte(1000)
defaultFeeRate := chainfee.SatPerKWeight(3125)
t.lndHarness.SetFeeEstimateWithConf(lowFeeRate, 6)
t.lndHarness.SetMinRelayFeerate(highMinRelayFeeRate)

// roundUpMinRelayFeeRate is the converted fee rate to sat/vbyte as
// that's what the FundPsbt expects. We add 999 to round up to the
// nearest 1000. We'll use this with `AssertFeerate`.
roundUpMinRelayFeeRate := chainfee.SatPerVByte(
(realWorldMinRelayFeeRate + 999) / 1000,
)

t.lndHarness.SetFeeEstimateWithConf(belowFloorFeeRate, 6)
t.lndHarness.SetMinRelayFeerate(realWorldMinRelayFeeRate)

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

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

MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner().Client, t.tapd,
[]*mintrpc.MintAssetRequest{issuableAssets[0]},
WithFeeRate(uint32(lowFeeRate)+10),
WithFeeRate(uint32(belowMinRelayFeeRate)),
WithError("feerate does not meet minrelayfee"),
)

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

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

sendAsset(
t, t.tapd, withReceiverAddresses(bobAddr),
withFeeRate(uint32(lowFeeRate)),
withFeeRate(uint32(belowFloorFeeRate)),
withError("manual fee rate below floor"),
)

sendAsset(
t, t.tapd, withReceiverAddresses(bobAddr),
withFeeRate(uint32(lowFeeRate)+10),
withFeeRate(uint32(belowMinRelayFeeRate)),
withError("feerate does not meet minrelayfee"),
)

Expand All @@ -271,7 +279,7 @@ func testMinRelayFeeBump(t *harnessTest) {
sendInputAmt := initialUTXOs[1].Amount + 1000
AssertTransferFeeRate(
t.t, t.lndHarness.Miner().Client, sendResp, sendInputAmt,
highMinRelayFeeRate.FeePerKWeight(),
roundUpMinRelayFeeRate.FeePerKWeight(),
)

AssertNonInteractiveRecvComplete(t.t, secondTapd, 1)
Expand Down
8 changes: 7 additions & 1 deletion wallet_anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ func (l *LndRpcWalletAnchor) FundPsbt(ctx context.Context, packet *psbt.Packet,
}
}

// We'll convert the fee rate to sat/vbyte as that's what the FundPsbt
// expects. We round up to the nearest whole unit to prevent issues
// where the fee doesn't meet the min_relay_fee because of rounding
// down.
satPerVByte := uint64(math.Ceil(float64(feeRate.FeePerKVByte()) / 1000))

pkt, changeIndex, leasedUtxos, err := l.lnd.WalletKit.FundPsbt(
ctx, &walletrpc.FundPsbtRequest{
Template: fundTemplate,
Fees: &walletrpc.FundPsbtRequest_SatPerVbyte{
SatPerVbyte: uint64(feeRate.FeePerKVByte()) / 1000,
SatPerVbyte: satPerVByte,
},
MinConfs: int32(minConfs),
ChangeType: defaultChangeType,
Expand Down