multi: fix timestamp filters for invoice query

This commit fixes the timestamp precision to always compare the dates
using unix seconds for invoices.
This commit is contained in:
yyforyongyu committed 2024-02-05 15:26:43 +08:00
1 parent 221e7ff898
commit 18333e8c7d
4 files changed
+35 -48

No files matched your search

+16 -16
View File
@@ -1407,7 +1407,7 @@ func TestQueryInvoices(t *testing.T) {
{ {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(25, 0), CreationDateEnd: 25,
}, },
expected: invoices[:25], expected: invoices[:25],
}, },
@@ -1415,7 +1415,7 @@ func TestQueryInvoices(t *testing.T) {
{ {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(26, 0), CreationDateStart: 26,
}, },
expected: invoices[25:], expected: invoices[25:],
}, },
@@ -1424,7 +1424,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
PendingOnly: true, PendingOnly: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(25, 0), CreationDateEnd: 25,
}, },
expected: pendingInvoices[:13], expected: pendingInvoices[:13],
}, },
@@ -1433,7 +1433,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
PendingOnly: true, PendingOnly: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(26, 0), CreationDateStart: 26,
}, },
expected: pendingInvoices[13:], expected: pendingInvoices[13:],
}, },
@@ -1442,7 +1442,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
IndexOffset: 20, IndexOffset: 20,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(30, 0), CreationDateEnd: 30,
}, },
// Since we're skipping to invoice 20 and iterating // Since we're skipping to invoice 20 and iterating
// to invoice 30, we'll expect those invoices. // to invoice 30, we'll expect those invoices.
@@ -1455,7 +1455,7 @@ func TestQueryInvoices(t *testing.T) {
IndexOffset: 21, IndexOffset: 21,
Reversed: true, Reversed: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0), CreationDateStart: 11,
}, },
// Since we're skipping to invoice 20 and iterating // Since we're skipping to invoice 20 and iterating
// backward to invoice 10, we'll expect those invoices. // backward to invoice 10, we'll expect those invoices.
@@ -1465,8 +1465,8 @@ func TestQueryInvoices(t *testing.T) {
{ {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0), CreationDateStart: 11,
CreationDateEnd: time.Unix(20, 0), CreationDateEnd: 20,
}, },
expected: invoices[10:20], expected: invoices[10:20],
}, },
@@ -1475,8 +1475,8 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
PendingOnly: true, PendingOnly: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0), CreationDateStart: 11,
CreationDateEnd: time.Unix(20, 0), CreationDateEnd: 20,
}, },
expected: pendingInvoices[5:10], expected: pendingInvoices[5:10],
}, },
@@ -1486,8 +1486,8 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
Reversed: true, Reversed: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0), CreationDateStart: 11,
CreationDateEnd: time.Unix(20, 0), CreationDateEnd: 20,
}, },
expected: invoices[10:20], expected: invoices[10:20],
}, },
@@ -1498,8 +1498,8 @@ func TestQueryInvoices(t *testing.T) {
PendingOnly: true, PendingOnly: true,
Reversed: true, Reversed: true,
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0), CreationDateStart: 11,
CreationDateEnd: time.Unix(20, 0), CreationDateEnd: 20,
}, },
expected: pendingInvoices[5:10], expected: pendingInvoices[5:10],
}, },
@@ -1508,8 +1508,8 @@ func TestQueryInvoices(t *testing.T) {
{ {
query: invpkg.InvoiceQuery{ query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices, NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(20, 0), CreationDateStart: 20,
CreationDateEnd: time.Unix(11, 0), CreationDateEnd: 11,
}, },
expected: nil, expected: nil,
}, },
+8 -12
View File
@@ -497,11 +497,7 @@ func (d *DB) FetchPendingInvoices(_ context.Context) (
func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) ( func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
invpkg.InvoiceSlice, error) { invpkg.InvoiceSlice, error) {
var ( var resp invpkg.InvoiceSlice
resp invpkg.InvoiceSlice
startDateSet = !q.CreationDateStart.IsZero()
endDateSet = !q.CreationDateEnd.IsZero()
)
err := kvdb.View(d, func(tx kvdb.RTx) error { err := kvdb.View(d, func(tx kvdb.RTx) error {
// If the bucket wasn't found, then there aren't any invoices // If the bucket wasn't found, then there aren't any invoices
@@ -541,20 +537,20 @@ func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
return false, nil return false, nil
} }
// Get the creation time in Unix seconds, this always
// rounds down the nanoseconds to full seconds.
createTime := invoice.CreationDate.Unix()
// Skip any invoices that were created before the // Skip any invoices that were created before the
// specified time. // specified time.
if startDateSet && invoice.CreationDate.Before( if createTime < q.CreationDateStart {
q.CreationDateStart,
) {
return false, nil return false, nil
} }
// Skip any invoices that were created after the // Skip any invoices that were created after the
// specified time. // specified time.
if endDateSet && invoice.CreationDate.After( if q.CreationDateEnd != 0 &&
q.CreationDateEnd, createTime > q.CreationDateEnd {
) {
return false, nil return false, nil
} }
+5 -6
View File
@@ -2,7 +2,6 @@ package invoices
import ( import (
"context" "context"
"time"
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
@@ -126,13 +125,13 @@ type InvoiceQuery struct {
// from the IndexOffset and go backwards. // from the IndexOffset and go backwards.
Reversed bool Reversed bool
// CreationDateStart, if set, filters out all invoices with a creation // CreationDateStart, expressed in Unix seconds, if set, filters out
// date greater than or euqal to it. // all invoices with a creation date greater than or equal to it.
CreationDateStart time.Time CreationDateStart int64
// CreationDateEnd, if set, filters out all invoices with a creation // CreationDateEnd, if set, filters out all invoices with a creation
// date less than or euqal to it. // date less than or equal to it.
CreationDateEnd time.Time CreationDateEnd int64
} }
// InvoiceSlice is the response to a invoice query. It includes the original // InvoiceSlice is the response to a invoice query. It includes the original
+6 -14
View File
@@ -5796,20 +5796,12 @@ func (r *rpcServer) ListInvoices(ctx context.Context,
// Next, we'll map the proto request into a format that is understood by // Next, we'll map the proto request into a format that is understood by
// the database. // the database.
q := invoices.InvoiceQuery{ q := invoices.InvoiceQuery{
IndexOffset: req.IndexOffset, IndexOffset: req.IndexOffset,
NumMaxInvoices: req.NumMaxInvoices, NumMaxInvoices: req.NumMaxInvoices,
PendingOnly: req.PendingOnly, PendingOnly: req.PendingOnly,
Reversed: req.Reversed, Reversed: req.Reversed,
} CreationDateStart: int64(req.CreationDateStart),
CreationDateEnd: int64(req.CreationDateEnd),
// Attach the start date if set.
if req.CreationDateStart != 0 {
q.CreationDateStart = time.Unix(int64(req.CreationDateStart), 0)
}
// Attach the end date if set.
if req.CreationDateEnd != 0 {
q.CreationDateEnd = time.Unix(int64(req.CreationDateEnd), 0)
} }
invoiceSlice, err := r.server.miscDB.QueryInvoices(ctx, q) invoiceSlice, err := r.server.miscDB.QueryInvoices(ctx, q)