Skip to content

Commit 4a6c7ab

Browse files
authored
Merge pull request #10292 from ziggie1984/introduce-sql-schema-payments-part-4
[Part 4|*] Add unit tests for the previous introduced SQL Backend methods
2 parents fbfb184 + cfc0dc0 commit 4a6c7ab

File tree

12 files changed

+1190
-378
lines changed

12 files changed

+1190
-378
lines changed

docs/release-notes/release-notes-0.21.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
SQL Backend](https://github.com/lightningnetwork/lnd/pull/10291)
8282
* Implement third(final) Part of SQL backend [payment
8383
functions](https://github.com/lightningnetwork/lnd/pull/10368)
84-
84+
* Finalize SQL payments implementation [enabling unit and itests
85+
for SQL backend](https://github.com/lightningnetwork/lnd/pull/10292)
8586

8687
## Code Health
8788

itest/lnd_payment_test.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,61 +504,86 @@ func testListPayments(ht *lntest.HarnessTest) {
504504
expected bool
505505
}
506506

507-
// Create test cases to check the timestamp filters.
508-
createCases := func(createTimeSeconds uint64) []testCase {
507+
// Create test cases with proper rounding for start and end dates.
508+
createCases := func(startTimeSeconds,
509+
endTimeSeconds uint64) []testCase {
510+
509511
return []testCase{
510512
{
511513
// Use a start date same as the creation date
512-
// should return us the item.
514+
// (truncated) should return us the item.
513515
name: "exact start date",
514-
startDate: createTimeSeconds,
516+
startDate: startTimeSeconds,
515517
expected: true,
516518
},
517519
{
518520
// Use an earlier start date should return us
519521
// the item.
520522
name: "earlier start date",
521-
startDate: createTimeSeconds - 1,
523+
startDate: startTimeSeconds - 1,
522524
expected: true,
523525
},
524526
{
525527
// Use a future start date should return us
526528
// nothing.
527529
name: "future start date",
528-
startDate: createTimeSeconds + 1,
530+
startDate: startTimeSeconds + 1,
529531
expected: false,
530532
},
531533
{
532534
// Use an end date same as the creation date
533-
// should return us the item.
535+
// (ceiling) should return us the item.
534536
name: "exact end date",
535-
endDate: createTimeSeconds,
537+
endDate: endTimeSeconds,
536538
expected: true,
537539
},
538540
{
539541
// Use an end date in the future should return
540542
// us the item.
541543
name: "future end date",
542-
endDate: createTimeSeconds + 1,
544+
endDate: endTimeSeconds + 1,
543545
expected: true,
544546
},
545547
{
546548
// Use an earlier end date should return us
547549
// nothing.
548-
name: "earlier end date",
549-
endDate: createTimeSeconds - 1,
550+
name: "earlier end date",
551+
// The native sql backend has a higher
552+
// precision than the kv backend, the native sql
553+
// backend uses microseconds, the kv backend
554+
// when filtering uses seconds so we need to
555+
// subtract 2 seconds to ensure the payment is
556+
// not included.
557+
// We could also truncate before inserting
558+
// into the sql db but I rather relax this test
559+
// here.
560+
endDate: endTimeSeconds - 2,
550561
expected: false,
551562
},
552563
}
553564
}
554565

555-
// Get the payment creation time in seconds.
556-
paymentCreateSeconds := uint64(
557-
p.CreationTimeNs / time.Second.Nanoseconds(),
566+
// Get the payment creation time in seconds, using different approaches
567+
// for start and end date comparisons to avoid rounding issues.
568+
creationTime := time.Unix(0, p.CreationTimeNs)
569+
570+
// For start date comparisons: use truncation (floor) to include
571+
// payments from the beginning of that second.
572+
paymentCreateSecondsStart := uint64(
573+
creationTime.Truncate(time.Second).Unix(),
574+
)
575+
576+
// For end date comparisons: use ceiling to include payments up to the
577+
// end of that second.
578+
paymentCreateSecondsEnd := uint64(
579+
(p.CreationTimeNs + time.Second.Nanoseconds() - 1) /
580+
time.Second.Nanoseconds(),
558581
)
559582

560583
// Create test cases from the payment creation time.
561-
testCases := createCases(paymentCreateSeconds)
584+
testCases := createCases(
585+
paymentCreateSecondsStart, paymentCreateSecondsEnd,
586+
)
562587

563588
// We now check the timestamp filters in `ListPayments`.
564589
for _, tc := range testCases {
@@ -578,7 +603,9 @@ func testListPayments(ht *lntest.HarnessTest) {
578603
}
579604

580605
// Create test cases from the invoice creation time.
581-
testCases = createCases(uint64(invoice.CreationDate))
606+
testCases = createCases(
607+
uint64(invoice.CreationDate), uint64(invoice.CreationDate),
608+
)
582609

583610
// We now do the same check for `ListInvoices`.
584611
for _, tc := range testCases {

lnrpc/routerrpc/router_backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,10 @@ func (r *RouterBackend) MarshallPayment(payment *paymentsdb.MPPayment) (
17621762
// If any of the htlcs have settled, extract a valid
17631763
// preimage.
17641764
if htlc.Settle != nil {
1765+
// For AMP payments all hashes will be different so we
1766+
// will only show the last htlc preimage, this is a
1767+
// current limitation for AMP payments because for
1768+
// MPP payments all hashes are the same.
17651769
preimage = htlc.Settle.Preimage
17661770
fee += htlc.Route.TotalFees()
17671771
}

0 commit comments

Comments
 (0)