diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index 3f189c8fa..d7bbc5b5c 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -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, }, diff --git a/channeldb/invoices.go b/channeldb/invoices.go index f163f40b8..9ffc6aa27 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -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 } diff --git a/invoices/interface.go b/invoices/interface.go index 0da031842..73274dfb5 100644 --- a/invoices/interface.go +++ b/invoices/interface.go @@ -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 diff --git a/rpcserver.go b/rpcserver.go index 370dfd992..eb79e7c7e 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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 // the database. q := invoices.InvoiceQuery{ - IndexOffset: req.IndexOffset, - 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) + IndexOffset: req.IndexOffset, + NumMaxInvoices: req.NumMaxInvoices, + PendingOnly: req.PendingOnly, + Reversed: req.Reversed, + CreationDateStart: int64(req.CreationDateStart), + CreationDateEnd: int64(req.CreationDateEnd), } invoiceSlice, err := r.server.miscDB.QueryInvoices(ctx, q)