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
2024-01-26 11:11:37 +08:00
parent 221e7ff898
commit 18333e8c7d
4 changed files with 35 additions and 48 deletions

View File

@@ -1407,7 +1407,7 @@ func TestQueryInvoices(t *testing.T) {
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(25, 0),
CreationDateEnd: 25,
},
expected: invoices[:25],
},
@@ -1415,7 +1415,7 @@ func TestQueryInvoices(t *testing.T) {
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(26, 0),
CreationDateStart: 26,
},
expected: invoices[25:],
},
@@ -1424,7 +1424,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{
PendingOnly: true,
NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(25, 0),
CreationDateEnd: 25,
},
expected: pendingInvoices[:13],
},
@@ -1433,7 +1433,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{
PendingOnly: true,
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(26, 0),
CreationDateStart: 26,
},
expected: pendingInvoices[13:],
},
@@ -1442,7 +1442,7 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{
IndexOffset: 20,
NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(30, 0),
CreationDateEnd: 30,
},
// Since we're skipping to invoice 20 and iterating
// to invoice 30, we'll expect those invoices.
@@ -1455,7 +1455,7 @@ func TestQueryInvoices(t *testing.T) {
IndexOffset: 21,
Reversed: true,
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0),
CreationDateStart: 11,
},
// Since we're skipping to invoice 20 and iterating
// backward to invoice 10, we'll expect those invoices.
@@ -1465,8 +1465,8 @@ func TestQueryInvoices(t *testing.T) {
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0),
CreationDateEnd: time.Unix(20, 0),
CreationDateStart: 11,
CreationDateEnd: 20,
},
expected: invoices[10:20],
},
@@ -1475,8 +1475,8 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{
PendingOnly: true,
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0),
CreationDateEnd: time.Unix(20, 0),
CreationDateStart: 11,
CreationDateEnd: 20,
},
expected: pendingInvoices[5:10],
},
@@ -1486,8 +1486,8 @@ func TestQueryInvoices(t *testing.T) {
query: invpkg.InvoiceQuery{
Reversed: true,
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0),
CreationDateEnd: time.Unix(20, 0),
CreationDateStart: 11,
CreationDateEnd: 20,
},
expected: invoices[10:20],
},
@@ -1498,8 +1498,8 @@ func TestQueryInvoices(t *testing.T) {
PendingOnly: true,
Reversed: true,
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(11, 0),
CreationDateEnd: time.Unix(20, 0),
CreationDateStart: 11,
CreationDateEnd: 20,
},
expected: pendingInvoices[5:10],
},
@@ -1508,8 +1508,8 @@ func TestQueryInvoices(t *testing.T) {
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(20, 0),
CreationDateEnd: time.Unix(11, 0),
CreationDateStart: 20,
CreationDateEnd: 11,
},
expected: nil,
},

View File

@@ -497,11 +497,7 @@ func (d *DB) FetchPendingInvoices(_ context.Context) (
func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
invpkg.InvoiceSlice, error) {
var (
resp invpkg.InvoiceSlice
startDateSet = !q.CreationDateStart.IsZero()
endDateSet = !q.CreationDateEnd.IsZero()
)
var resp invpkg.InvoiceSlice
err := kvdb.View(d, func(tx kvdb.RTx) error {
// 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
}
// 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
// specified time.
if startDateSet && invoice.CreationDate.Before(
q.CreationDateStart,
) {
if createTime < q.CreationDateStart {
return false, nil
}
// Skip any invoices that were created after the
// specified time.
if endDateSet && invoice.CreationDate.After(
q.CreationDateEnd,
) {
if q.CreationDateEnd != 0 &&
createTime > q.CreationDateEnd {
return false, nil
}

View File

@@ -2,7 +2,6 @@ package invoices
import (
"context"
"time"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lntypes"
@@ -126,13 +125,13 @@ type InvoiceQuery struct {
// from the IndexOffset and go backwards.
Reversed bool
// CreationDateStart, if set, filters out all invoices with a creation
// date greater than or euqal to it.
CreationDateStart time.Time
// CreationDateStart, expressed in Unix seconds, if set, filters out
// all invoices with a creation date greater than or equal to it.
CreationDateStart int64
// CreationDateEnd, if set, filters out all invoices with a creation
// date less than or euqal to it.
CreationDateEnd time.Time
// date less than or equal to it.
CreationDateEnd int64
}
// InvoiceSlice is the response to a invoice query. It includes the original

View File

@@ -5800,16 +5800,8 @@ func (r *rpcServer) ListInvoices(ctx context.Context,
NumMaxInvoices: req.NumMaxInvoices,
PendingOnly: req.PendingOnly,
Reversed: req.Reversed,
}
// 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)
CreationDateStart: int64(req.CreationDateStart),
CreationDateEnd: int64(req.CreationDateEnd),
}
invoiceSlice, err := r.server.miscDB.QueryInvoices(ctx, q)