Skip to content

Commit

Permalink
Merge pull request #8432 from yyforyongyu/fix-timestamp-precision
Browse files Browse the repository at this point in the history
invoice+payment: fix timestamp precision for queries
  • Loading branch information
yyforyongyu authored Feb 5, 2024
2 parents 4584f7e + 4da9582 commit 63e698e
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 144 deletions.
32 changes: 16 additions & 16 deletions channeldb/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,15 +1407,15 @@ func TestQueryInvoices(t *testing.T) {
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateEnd: time.Unix(25, 0),
CreationDateEnd: 25,
},
expected: invoices[:25],
},
// Fetch invoices >= 26 creation date.
{
query: invpkg.InvoiceQuery{
NumMaxInvoices: numInvoices,
CreationDateStart: time.Unix(26, 0),
CreationDateStart: 26,
},
expected: invoices[25:],
},
Expand All @@ -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],
},
Expand All @@ -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:],
},
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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],
},
Expand All @@ -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],
},
Expand All @@ -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],
},
Expand All @@ -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],
},
Expand All @@ -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,
},
Expand Down
20 changes: 8 additions & 12 deletions channeldb/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 14 additions & 18 deletions channeldb/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,13 @@ type PaymentsQuery struct {
// payment index (complete and incomplete) should be counted.
CountTotal bool

// CreationDateStart, if set, filters out all payments with a creation
// date greater than or euqal to it.
CreationDateStart time.Time
// CreationDateStart, expressed in Unix seconds, if set, filters out
// all payments with a creation date greater than or equal to it.
CreationDateStart int64

// CreationDateEnd, if set, filters out all payments with a creation
// date less than or euqal to it.
CreationDateEnd time.Time
// 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.
Expand Down Expand Up @@ -512,11 +512,7 @@ type PaymentsResponse struct {
// to a subset of payments by the payments query, containing an offset
// index and a maximum number of returned payments.
func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
var (
resp PaymentsResponse
startDateSet = !query.CreationDateStart.IsZero()
endDateSet = !query.CreationDateEnd.IsZero()
)
var resp PaymentsResponse

if err := kvdb.View(d, func(tx kvdb.RTx) error {
// Get the root payments bucket.
Expand Down Expand Up @@ -561,20 +557,20 @@ func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
return false, err
}

// Get the creation time in Unix seconds, this always
// rounds down the nanoseconds to full seconds.
createTime := payment.Info.CreationTime.Unix()

// Skip any payments that were created before the
// specified time.
if startDateSet && payment.Info.CreationTime.Before(
query.CreationDateStart,
) {

if createTime < query.CreationDateStart {
return false, nil
}

// Skip any payments that were created after the
// specified time.
if endDateSet && payment.Info.CreationTime.After(
query.CreationDateEnd,
) {
if query.CreationDateEnd != 0 &&
createTime > query.CreationDateEnd {

return false, nil
}
Expand Down
10 changes: 5 additions & 5 deletions channeldb/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: 2,
Reversed: false,
IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 5),
CreationDateStart: 5,
},
firstIndex: 5,
lastIndex: 6,
Expand All @@ -452,7 +452,7 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: 2,
Reversed: false,
IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 7),
CreationDateStart: 7,
},
firstIndex: 7,
lastIndex: 7,
Expand All @@ -465,8 +465,8 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: math.MaxUint64,
Reversed: true,
IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 3),
CreationDateEnd: time.Unix(0, 5),
CreationDateStart: 3,
CreationDateEnd: 5,
},
firstIndex: 3,
lastIndex: 5,
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestQueryPayments(t *testing.T) {
}
// Override creation time to allow for testing
// of CreationDateStart and CreationDateEnd.
info.CreationTime = time.Unix(0, int64(i+1))
info.CreationTime = time.Unix(int64(i+1), 0)

// Create a new payment entry in the database.
err = pControl.InitPayment(info.PaymentIdentifier, info)
Expand Down
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.18.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
* [Fixed](https://github.com/lightningnetwork/lnd/pull/7852) the payload size
calculation in our pathfinder because blinded hops introduced new tlv records.

* [Fixed](https://github.com/lightningnetwork/lnd/pull/8432) a timestamp
precision issue when querying payments and invoices using the start and end
date filters.

# New Features
## Functional Enhancements

Expand Down
11 changes: 5 additions & 6 deletions invoices/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package invoices

import (
"context"
"time"

"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/lntypes"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion itest/list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ var allTestCases = []*lntest.TestCase{
TestFunc: testUpdateNodeAnnouncement,
},
{
Name: "list outgoing payments",
Name: "list payments",
TestFunc: testListPayments,
},
{
Expand Down
Loading

0 comments on commit 63e698e

Please sign in to comment.