From d77b2f9c26f5c800896381e345ca2b8c7c0b2ebd Mon Sep 17 00:00:00 2001 From: ziggie Date: Wed, 13 Aug 2025 09:48:09 +0200 Subject: [PATCH] mulit: move payment query code to separate file We also rename the struct since it is now in its own package there is no need to prefix it. --- payments/db/kv_store.go | 6 ++-- payments/db/payment.go | 68 ------------------------------------ payments/db/payment_test.go | 38 ++++++++++---------- payments/db/query.go | 69 +++++++++++++++++++++++++++++++++++++ rpcserver.go | 2 +- 5 files changed, 92 insertions(+), 91 deletions(-) create mode 100644 payments/db/query.go diff --git a/payments/db/kv_store.go b/payments/db/kv_store.go index 0bd6576d0..02cce77a9 100644 --- a/payments/db/kv_store.go +++ b/payments/db/kv_store.go @@ -1131,9 +1131,9 @@ func fetchFailedHtlcKeys(bucket kvdb.RBucket) ([][]byte, error) { // to a subset of payments by the payments query, containing an offset // index and a maximum number of returned payments. func (p *KVPaymentsDB) QueryPayments(_ context.Context, - query PaymentsQuery) (PaymentsResponse, error) { + query Query) (Response, error) { - var resp PaymentsResponse + var resp Response if err := kvdb.View(p.db, func(tx kvdb.RTx) error { // Get the root payments bucket. @@ -1247,7 +1247,7 @@ func (p *KVPaymentsDB) QueryPayments(_ context.Context, return nil }, func() { - resp = PaymentsResponse{} + resp = Response{} }); err != nil { return resp, err } diff --git a/payments/db/payment.go b/payments/db/payment.go index a43e87144..38948b167 100644 --- a/payments/db/payment.go +++ b/payments/db/payment.go @@ -19,74 +19,6 @@ import ( "github.com/lightningnetwork/lnd/routing/route" ) -// PaymentsQuery represents a query to the payments database starting or ending -// at a certain offset index. The number of retrieved records can be limited. -type PaymentsQuery struct { - // IndexOffset determines the starting point of the payments query and - // is always exclusive. In normal order, the query starts at the next - // higher (available) index compared to IndexOffset. In reversed order, - // the query ends at the next lower (available) index compared to the - // IndexOffset. In the case of a zero index_offset, the query will start - // with the oldest payment when paginating forwards, or will end with - // the most recent payment when paginating backwards. - IndexOffset uint64 - - // MaxPayments is the maximal number of payments returned in the - // payments query. - MaxPayments uint64 - - // Reversed gives a meaning to the IndexOffset. If reversed is set to - // true, the query will fetch payments with indices lower than the - // IndexOffset, otherwise, it will return payments with indices greater - // than the IndexOffset. - Reversed bool - - // If IncludeIncomplete is true, then return payments that have not yet - // fully completed. This means that pending payments, as well as failed - // payments will show up if this field is set to true. - IncludeIncomplete bool - - // CountTotal indicates that all payments currently present in the - // payment index (complete and incomplete) should be counted. - CountTotal bool - - // CreationDateStart, expressed in Unix seconds, if set, filters out - // all payments with a creation date greater than or equal to it. - CreationDateStart int64 - - // CreationDateEnd, expressed in Unix seconds, if set, filters out all - // payments with a creation date less than or equal to it. - CreationDateEnd int64 -} - -// PaymentsResponse contains the result of a query to the payments database. -// It includes the set of payments that match the query and integers which -// represent the index of the first and last item returned in the series of -// payments. These integers allow callers to resume their query in the event -// that the query's response exceeds the max number of returnable events. -type PaymentsResponse struct { - // Payments is the set of payments returned from the database for the - // PaymentsQuery. - Payments []*MPPayment - - // FirstIndexOffset is the index of the first element in the set of - // returned MPPayments. Callers can use this to resume their query - // in the event that the slice has too many events to fit into a single - // response. The offset can be used to continue reverse pagination. - FirstIndexOffset uint64 - - // LastIndexOffset is the index of the last element in the set of - // returned MPPayments. Callers can use this to resume their query - // in the event that the slice has too many events to fit into a single - // response. The offset can be used to continue forward pagination. - LastIndexOffset uint64 - - // TotalCount represents the total number of payments that are currently - // stored in the payment database. This will only be set if the - // CountTotal field in the query was set to true. - TotalCount uint64 -} - // HTLCAttemptInfo contains static information about a specific HTLC attempt // for a payment. This information is used by the router to handle any errors // coming back after an attempt is made, and to query the switch about the diff --git a/payments/db/payment_test.go b/payments/db/payment_test.go index 897f570f8..91cbdcaa4 100644 --- a/payments/db/payment_test.go +++ b/payments/db/payment_test.go @@ -134,7 +134,7 @@ func TestQueryPayments(t *testing.T) { // of legacy payments. tests := []struct { name string - query PaymentsQuery + query Query firstIndex uint64 lastIndex uint64 @@ -144,7 +144,7 @@ func TestQueryPayments(t *testing.T) { }{ { name: "IndexOffset at the end of the payments range", - query: PaymentsQuery{ + query: Query{ IndexOffset: 7, MaxPayments: 7, Reversed: false, @@ -156,7 +156,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query in forwards order, start at beginning", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 2, Reversed: false, @@ -168,7 +168,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query in forwards order, start at end, overflow", - query: PaymentsQuery{ + query: Query{ IndexOffset: 6, MaxPayments: 2, Reversed: false, @@ -180,7 +180,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "start at offset index outside of payments", - query: PaymentsQuery{ + query: Query{ IndexOffset: 20, MaxPayments: 2, Reversed: false, @@ -192,7 +192,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "overflow in forwards order", - query: PaymentsQuery{ + query: Query{ IndexOffset: 4, MaxPayments: math.MaxUint64, Reversed: false, @@ -205,7 +205,7 @@ func TestQueryPayments(t *testing.T) { { name: "start at offset index outside of payments, " + "reversed order", - query: PaymentsQuery{ + query: Query{ IndexOffset: 9, MaxPayments: 2, Reversed: true, @@ -217,7 +217,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query in reverse order, start at end", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 2, Reversed: true, @@ -229,7 +229,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query in reverse order, starting in middle", - query: PaymentsQuery{ + query: Query{ IndexOffset: 4, MaxPayments: 2, Reversed: true, @@ -242,7 +242,7 @@ func TestQueryPayments(t *testing.T) { { name: "query in reverse order, starting in middle, " + "with underflow", - query: PaymentsQuery{ + query: Query{ IndexOffset: 4, MaxPayments: 5, Reversed: true, @@ -254,7 +254,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "all payments in reverse, order maintained", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 7, Reversed: true, @@ -266,7 +266,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "exclude incomplete payments", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 7, Reversed: false, @@ -278,7 +278,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query payments at index gap", - query: PaymentsQuery{ + query: Query{ IndexOffset: 1, MaxPayments: 7, Reversed: false, @@ -290,7 +290,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query payments reverse before index gap", - query: PaymentsQuery{ + query: Query{ IndexOffset: 3, MaxPayments: 7, Reversed: true, @@ -302,7 +302,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query payments reverse on index gap", - query: PaymentsQuery{ + query: Query{ IndexOffset: 2, MaxPayments: 7, Reversed: true, @@ -314,7 +314,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query payments forward on index gap", - query: PaymentsQuery{ + query: Query{ IndexOffset: 2, MaxPayments: 2, Reversed: false, @@ -327,7 +327,7 @@ func TestQueryPayments(t *testing.T) { { name: "query in forwards order, with start creation " + "time", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 2, Reversed: false, @@ -341,7 +341,7 @@ func TestQueryPayments(t *testing.T) { { name: "query in forwards order, with start creation " + "time at end, overflow", - query: PaymentsQuery{ + query: Query{ IndexOffset: 0, MaxPayments: 2, Reversed: false, @@ -354,7 +354,7 @@ func TestQueryPayments(t *testing.T) { }, { name: "query with start and end creation time", - query: PaymentsQuery{ + query: Query{ IndexOffset: 9, MaxPayments: math.MaxUint64, Reversed: true, diff --git a/payments/db/query.go b/payments/db/query.go new file mode 100644 index 000000000..5904ab56a --- /dev/null +++ b/payments/db/query.go @@ -0,0 +1,69 @@ +package paymentsdb + +// Query represents a query to the payments database starting or ending +// at a certain offset index. The number of retrieved records can be limited. +type Query struct { + // IndexOffset determines the starting point of the payments query and + // is always exclusive. In normal order, the query starts at the next + // higher (available) index compared to IndexOffset. In reversed order, + // the query ends at the next lower (available) index compared to the + // IndexOffset. In the case of a zero index_offset, the query will start + // with the oldest payment when paginating forwards, or will end with + // the most recent payment when paginating backwards. + IndexOffset uint64 + + // MaxPayments is the maximal number of payments returned in the + // payments query. + MaxPayments uint64 + + // Reversed gives a meaning to the IndexOffset. If reversed is set to + // true, the query will fetch payments with indices lower than the + // IndexOffset, otherwise, it will return payments with indices greater + // than the IndexOffset. + Reversed bool + + // If IncludeIncomplete is true, then return payments that have not yet + // fully completed. This means that pending payments, as well as failed + // payments will show up if this field is set to true. + IncludeIncomplete bool + + // CountTotal indicates that all payments currently present in the + // payment index (complete and incomplete) should be counted. + CountTotal bool + + // CreationDateStart, expressed in Unix seconds, if set, filters out + // all payments with a creation date greater than or equal to it. + CreationDateStart int64 + + // CreationDateEnd, expressed in Unix seconds, if set, filters out all + // payments with a creation date less than or equal to it. + CreationDateEnd int64 +} + +// Response contains the result of a query to the payments database. +// It includes the set of payments that match the query and integers which +// represent the index of the first and last item returned in the series of +// payments. These integers allow callers to resume their query in the event +// that the query's response exceeds the max number of returnable events. +type Response struct { + // Payments is the set of payments returned from the database for the + // Query. + Payments []*MPPayment + + // FirstIndexOffset is the index of the first element in the set of + // returned MPPayments. Callers can use this to resume their query + // in the event that the slice has too many events to fit into a single + // response. The offset can be used to continue reverse pagination. + FirstIndexOffset uint64 + + // LastIndexOffset is the index of the last element in the set of + // returned MPPayments. Callers can use this to resume their query + // in the event that the slice has too many events to fit into a single + // response. The offset can be used to continue forward pagination. + LastIndexOffset uint64 + + // TotalCount represents the total number of payments that are currently + // stored in the payment database. This will only be set if the + // CountTotal field in the query was set to true. + TotalCount uint64 +} diff --git a/rpcserver.go b/rpcserver.go index d26e4b889..8b4c1b1b5 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -7513,7 +7513,7 @@ func (r *rpcServer) ListPayments(ctx context.Context, } } - query := paymentsdb.PaymentsQuery{ + query := paymentsdb.Query{ IndexOffset: req.IndexOffset, MaxPayments: req.MaxPayments, Reversed: req.Reversed,