From 7374392abe969be846bf386d43ba30f6fe65b55f Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 5 Dec 2024 23:39:37 +0800 Subject: [PATCH] lnrpc: sort `Invoice.HTLCs` based on `HtlcIndex` So the returned HTLCs are ordered. --- docs/release-notes/release-notes-0.19.0.md | 4 ++++ lnrpc/invoicesrpc/utils.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index db5ca738ef..ee0b0c1360 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -90,6 +90,10 @@ * [The `walletrpc.FundPsbt` method now has a new option to specify the maximum fee to output amounts ratio.](https://github.com/lightningnetwork/lnd/pull/8600) +* When returning the response from list invoices RPC, the `lnrpc.Invoice.Htlcs` + are now [sorted](https://github.com/lightningnetwork/lnd/pull/9337) based on + the `InvoiceHTLC.HtlcIndex`. + ## lncli Additions * [A pre-generated macaroon root key can now be specified in `lncli create` and diff --git a/lnrpc/invoicesrpc/utils.go b/lnrpc/invoicesrpc/utils.go index 955ba6acf2..19ade28fd8 100644 --- a/lnrpc/invoicesrpc/utils.go +++ b/lnrpc/invoicesrpc/utils.go @@ -1,8 +1,10 @@ package invoicesrpc import ( + "cmp" "encoding/hex" "fmt" + "slices" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/chaincfg" @@ -160,6 +162,11 @@ func CreateRPCInvoice(invoice *invoices.Invoice, rpcHtlcs = append(rpcHtlcs, &rpcHtlc) } + // Perform an inplace sort of the HTLCs to ensure they are ordered. + slices.SortFunc(rpcHtlcs, func(i, j *lnrpc.InvoiceHTLC) int { + return cmp.Compare(i.HtlcIndex, j.HtlcIndex) + }) + rpcInvoice := &lnrpc.Invoice{ Memo: string(invoice.Memo), RHash: rHash,