Skip to content

Commit 63e698e

Browse files
authored
Merge pull request #8432 from yyforyongyu/fix-timestamp-precision
invoice+payment: fix timestamp precision for queries
2 parents 4584f7e + 4da9582 commit 63e698e

File tree

9 files changed

+154
-144
lines changed

9 files changed

+154
-144
lines changed

channeldb/invoice_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,15 +1407,15 @@ func TestQueryInvoices(t *testing.T) {
14071407
{
14081408
query: invpkg.InvoiceQuery{
14091409
NumMaxInvoices: numInvoices,
1410-
CreationDateEnd: time.Unix(25, 0),
1410+
CreationDateEnd: 25,
14111411
},
14121412
expected: invoices[:25],
14131413
},
14141414
// Fetch invoices >= 26 creation date.
14151415
{
14161416
query: invpkg.InvoiceQuery{
14171417
NumMaxInvoices: numInvoices,
1418-
CreationDateStart: time.Unix(26, 0),
1418+
CreationDateStart: 26,
14191419
},
14201420
expected: invoices[25:],
14211421
},
@@ -1424,7 +1424,7 @@ func TestQueryInvoices(t *testing.T) {
14241424
query: invpkg.InvoiceQuery{
14251425
PendingOnly: true,
14261426
NumMaxInvoices: numInvoices,
1427-
CreationDateEnd: time.Unix(25, 0),
1427+
CreationDateEnd: 25,
14281428
},
14291429
expected: pendingInvoices[:13],
14301430
},
@@ -1433,7 +1433,7 @@ func TestQueryInvoices(t *testing.T) {
14331433
query: invpkg.InvoiceQuery{
14341434
PendingOnly: true,
14351435
NumMaxInvoices: numInvoices,
1436-
CreationDateStart: time.Unix(26, 0),
1436+
CreationDateStart: 26,
14371437
},
14381438
expected: pendingInvoices[13:],
14391439
},
@@ -1442,7 +1442,7 @@ func TestQueryInvoices(t *testing.T) {
14421442
query: invpkg.InvoiceQuery{
14431443
IndexOffset: 20,
14441444
NumMaxInvoices: numInvoices,
1445-
CreationDateEnd: time.Unix(30, 0),
1445+
CreationDateEnd: 30,
14461446
},
14471447
// Since we're skipping to invoice 20 and iterating
14481448
// to invoice 30, we'll expect those invoices.
@@ -1455,7 +1455,7 @@ func TestQueryInvoices(t *testing.T) {
14551455
IndexOffset: 21,
14561456
Reversed: true,
14571457
NumMaxInvoices: numInvoices,
1458-
CreationDateStart: time.Unix(11, 0),
1458+
CreationDateStart: 11,
14591459
},
14601460
// Since we're skipping to invoice 20 and iterating
14611461
// backward to invoice 10, we'll expect those invoices.
@@ -1465,8 +1465,8 @@ func TestQueryInvoices(t *testing.T) {
14651465
{
14661466
query: invpkg.InvoiceQuery{
14671467
NumMaxInvoices: numInvoices,
1468-
CreationDateStart: time.Unix(11, 0),
1469-
CreationDateEnd: time.Unix(20, 0),
1468+
CreationDateStart: 11,
1469+
CreationDateEnd: 20,
14701470
},
14711471
expected: invoices[10:20],
14721472
},
@@ -1475,8 +1475,8 @@ func TestQueryInvoices(t *testing.T) {
14751475
query: invpkg.InvoiceQuery{
14761476
PendingOnly: true,
14771477
NumMaxInvoices: numInvoices,
1478-
CreationDateStart: time.Unix(11, 0),
1479-
CreationDateEnd: time.Unix(20, 0),
1478+
CreationDateStart: 11,
1479+
CreationDateEnd: 20,
14801480
},
14811481
expected: pendingInvoices[5:10],
14821482
},
@@ -1486,8 +1486,8 @@ func TestQueryInvoices(t *testing.T) {
14861486
query: invpkg.InvoiceQuery{
14871487
Reversed: true,
14881488
NumMaxInvoices: numInvoices,
1489-
CreationDateStart: time.Unix(11, 0),
1490-
CreationDateEnd: time.Unix(20, 0),
1489+
CreationDateStart: 11,
1490+
CreationDateEnd: 20,
14911491
},
14921492
expected: invoices[10:20],
14931493
},
@@ -1498,8 +1498,8 @@ func TestQueryInvoices(t *testing.T) {
14981498
PendingOnly: true,
14991499
Reversed: true,
15001500
NumMaxInvoices: numInvoices,
1501-
CreationDateStart: time.Unix(11, 0),
1502-
CreationDateEnd: time.Unix(20, 0),
1501+
CreationDateStart: 11,
1502+
CreationDateEnd: 20,
15031503
},
15041504
expected: pendingInvoices[5:10],
15051505
},
@@ -1508,8 +1508,8 @@ func TestQueryInvoices(t *testing.T) {
15081508
{
15091509
query: invpkg.InvoiceQuery{
15101510
NumMaxInvoices: numInvoices,
1511-
CreationDateStart: time.Unix(20, 0),
1512-
CreationDateEnd: time.Unix(11, 0),
1511+
CreationDateStart: 20,
1512+
CreationDateEnd: 11,
15131513
},
15141514
expected: nil,
15151515
},

channeldb/invoices.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,7 @@ func (d *DB) FetchPendingInvoices(_ context.Context) (
497497
func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
498498
invpkg.InvoiceSlice, error) {
499499

500-
var (
501-
resp invpkg.InvoiceSlice
502-
startDateSet = !q.CreationDateStart.IsZero()
503-
endDateSet = !q.CreationDateEnd.IsZero()
504-
)
500+
var resp invpkg.InvoiceSlice
505501

506502
err := kvdb.View(d, func(tx kvdb.RTx) error {
507503
// 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) (
541537
return false, nil
542538
}
543539

540+
// Get the creation time in Unix seconds, this always
541+
// rounds down the nanoseconds to full seconds.
542+
createTime := invoice.CreationDate.Unix()
543+
544544
// Skip any invoices that were created before the
545545
// specified time.
546-
if startDateSet && invoice.CreationDate.Before(
547-
q.CreationDateStart,
548-
) {
549-
546+
if createTime < q.CreationDateStart {
550547
return false, nil
551548
}
552549

553550
// Skip any invoices that were created after the
554551
// specified time.
555-
if endDateSet && invoice.CreationDate.After(
556-
q.CreationDateEnd,
557-
) {
552+
if q.CreationDateEnd != 0 &&
553+
createTime > q.CreationDateEnd {
558554

559555
return false, nil
560556
}

channeldb/payments.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,13 @@ type PaymentsQuery struct {
471471
// payment index (complete and incomplete) should be counted.
472472
CountTotal bool
473473

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

478-
// CreationDateEnd, if set, filters out all payments with a creation
479-
// date less than or euqal to it.
480-
CreationDateEnd time.Time
478+
// CreationDateEnd, expressed in Unix seconds, if set, filters out all
479+
// payments with a creation date less than or equal to it.
480+
CreationDateEnd int64
481481
}
482482

483483
// PaymentsResponse contains the result of a query to the payments database.
@@ -512,11 +512,7 @@ type PaymentsResponse struct {
512512
// to a subset of payments by the payments query, containing an offset
513513
// index and a maximum number of returned payments.
514514
func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
515-
var (
516-
resp PaymentsResponse
517-
startDateSet = !query.CreationDateStart.IsZero()
518-
endDateSet = !query.CreationDateEnd.IsZero()
519-
)
515+
var resp PaymentsResponse
520516

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

560+
// Get the creation time in Unix seconds, this always
561+
// rounds down the nanoseconds to full seconds.
562+
createTime := payment.Info.CreationTime.Unix()
563+
564564
// Skip any payments that were created before the
565565
// specified time.
566-
if startDateSet && payment.Info.CreationTime.Before(
567-
query.CreationDateStart,
568-
) {
569-
566+
if createTime < query.CreationDateStart {
570567
return false, nil
571568
}
572569

573570
// Skip any payments that were created after the
574571
// specified time.
575-
if endDateSet && payment.Info.CreationTime.After(
576-
query.CreationDateEnd,
577-
) {
572+
if query.CreationDateEnd != 0 &&
573+
createTime > query.CreationDateEnd {
578574

579575
return false, nil
580576
}

channeldb/payments_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func TestQueryPayments(t *testing.T) {
438438
MaxPayments: 2,
439439
Reversed: false,
440440
IncludeIncomplete: true,
441-
CreationDateStart: time.Unix(0, 5),
441+
CreationDateStart: 5,
442442
},
443443
firstIndex: 5,
444444
lastIndex: 6,
@@ -452,7 +452,7 @@ func TestQueryPayments(t *testing.T) {
452452
MaxPayments: 2,
453453
Reversed: false,
454454
IncludeIncomplete: true,
455-
CreationDateStart: time.Unix(0, 7),
455+
CreationDateStart: 7,
456456
},
457457
firstIndex: 7,
458458
lastIndex: 7,
@@ -465,8 +465,8 @@ func TestQueryPayments(t *testing.T) {
465465
MaxPayments: math.MaxUint64,
466466
Reversed: true,
467467
IncludeIncomplete: true,
468-
CreationDateStart: time.Unix(0, 3),
469-
CreationDateEnd: time.Unix(0, 5),
468+
CreationDateStart: 3,
469+
CreationDateEnd: 5,
470470
},
471471
firstIndex: 3,
472472
lastIndex: 5,
@@ -509,7 +509,7 @@ func TestQueryPayments(t *testing.T) {
509509
}
510510
// Override creation time to allow for testing
511511
// of CreationDateStart and CreationDateEnd.
512-
info.CreationTime = time.Unix(0, int64(i+1))
512+
info.CreationTime = time.Unix(int64(i+1), 0)
513513

514514
// Create a new payment entry in the database.
515515
err = pControl.InitPayment(info.PaymentIdentifier, info)

docs/release-notes/release-notes-0.18.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
* [Fixed](https://github.com/lightningnetwork/lnd/pull/7852) the payload size
8282
calculation in our pathfinder because blinded hops introduced new tlv records.
8383

84+
* [Fixed](https://github.com/lightningnetwork/lnd/pull/8432) a timestamp
85+
precision issue when querying payments and invoices using the start and end
86+
date filters.
87+
8488
# New Features
8589
## Functional Enhancements
8690

invoices/interface.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package invoices
22

33
import (
44
"context"
5-
"time"
65

76
"github.com/lightningnetwork/lnd/channeldb/models"
87
"github.com/lightningnetwork/lnd/lntypes"
@@ -126,13 +125,13 @@ type InvoiceQuery struct {
126125
// from the IndexOffset and go backwards.
127126
Reversed bool
128127

129-
// CreationDateStart, if set, filters out all invoices with a creation
130-
// date greater than or euqal to it.
131-
CreationDateStart time.Time
128+
// CreationDateStart, expressed in Unix seconds, if set, filters out
129+
// all invoices with a creation date greater than or equal to it.
130+
CreationDateStart int64
132131

133132
// CreationDateEnd, if set, filters out all invoices with a creation
134-
// date less than or euqal to it.
135-
CreationDateEnd time.Time
133+
// date less than or equal to it.
134+
CreationDateEnd int64
136135
}
137136

138137
// InvoiceSlice is the response to a invoice query. It includes the original

itest/list_on_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var allTestCases = []*lntest.TestCase{
174174
TestFunc: testUpdateNodeAnnouncement,
175175
},
176176
{
177-
Name: "list outgoing payments",
177+
Name: "list payments",
178178
TestFunc: testListPayments,
179179
},
180180
{

0 commit comments

Comments
 (0)