@@ -507,9 +507,11 @@ func TestQueryPayments(t *testing.T) {
507
507
t .Fatalf ("unable to init db: %v" , err )
508
508
}
509
509
510
+ paymentDB := NewKVPaymentsDB (db )
511
+
510
512
// Make a preliminary query to make sure it's ok to
511
513
// query when we have no payments.
512
- resp , err := db .QueryPayments (tt .query )
514
+ resp , err := paymentDB .QueryPayments (tt .query )
513
515
require .NoError (t , err )
514
516
require .Len (t , resp .Payments , 0 )
515
517
@@ -521,7 +523,6 @@ func TestQueryPayments(t *testing.T) {
521
523
// where we have duplicates in the nested duplicates
522
524
// bucket.
523
525
nonDuplicatePayments := 6
524
- paymentDB := NewKVPaymentsDB (db )
525
526
526
527
for i := 0 ; i < nonDuplicatePayments ; i ++ {
527
528
// Generate a test payment.
@@ -574,7 +575,7 @@ func TestQueryPayments(t *testing.T) {
574
575
}
575
576
576
577
// Fetch all payments in the database.
577
- allPayments , err := db .FetchPayments ()
578
+ allPayments , err := paymentDB .FetchPayments ()
578
579
if err != nil {
579
580
t .Fatalf ("payments could not be fetched from " +
580
581
"database: %v" , err )
@@ -586,7 +587,7 @@ func TestQueryPayments(t *testing.T) {
586
587
len (allPayments ), 6 )
587
588
}
588
589
589
- querySlice , err := db .QueryPayments (tt .query )
590
+ querySlice , err := paymentDB .QueryPayments (tt .query )
590
591
if err != nil {
591
592
t .Fatalf ("unexpected error: %v" , err )
592
593
}
@@ -1277,12 +1278,12 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
1277
1278
}
1278
1279
1279
1280
// Delete all failed payments.
1280
- numPayments , err := db .DeletePayments (true , false )
1281
+ numPayments , err := paymentDB .DeletePayments (true , false )
1281
1282
require .NoError (t , err )
1282
1283
require .EqualValues (t , 1 , numPayments )
1283
1284
1284
1285
// This should leave the succeeded and in-flight payments.
1285
- dbPayments , err := db .FetchPayments ()
1286
+ dbPayments , err := paymentDB .FetchPayments ()
1286
1287
if err != nil {
1287
1288
t .Fatal (err )
1288
1289
}
@@ -1313,12 +1314,12 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
1313
1314
}
1314
1315
1315
1316
// Now delete all payments except in-flight.
1316
- numPayments , err = db .DeletePayments (false , false )
1317
+ numPayments , err = paymentDB .DeletePayments (false , false )
1317
1318
require .NoError (t , err )
1318
1319
require .EqualValues (t , 2 , numPayments )
1319
1320
1320
1321
// This should leave the in-flight payment.
1321
- dbPayments , err = db .FetchPayments ()
1322
+ dbPayments , err = paymentDB .FetchPayments ()
1322
1323
if err != nil {
1323
1324
t .Fatal (err )
1324
1325
}
@@ -1375,40 +1376,40 @@ func TestKVPaymentsDBDeletePayments(t *testing.T) {
1375
1376
createTestPayments (t , paymentDB , payments )
1376
1377
1377
1378
// Check that all payments are there as we added them.
1378
- assertPayments (t , db , payments )
1379
+ assertPayments (t , paymentDB , payments )
1379
1380
1380
1381
// Delete HTLC attempts for failed payments only.
1381
- numPayments , err := db .DeletePayments (true , true )
1382
+ numPayments , err := paymentDB .DeletePayments (true , true )
1382
1383
require .NoError (t , err )
1383
1384
require .EqualValues (t , 0 , numPayments )
1384
1385
1385
1386
// The failed payment is the only altered one.
1386
1387
payments [0 ].htlcs = 0
1387
- assertPayments (t , db , payments )
1388
+ assertPayments (t , paymentDB , payments )
1388
1389
1389
1390
// Delete failed attempts for all payments.
1390
- numPayments , err = db .DeletePayments (false , true )
1391
+ numPayments , err = paymentDB .DeletePayments (false , true )
1391
1392
require .NoError (t , err )
1392
1393
require .EqualValues (t , 0 , numPayments )
1393
1394
1394
1395
// The failed attempts should be deleted, except for the in-flight
1395
1396
// payment, that shouldn't be altered until it has completed.
1396
1397
payments [1 ].htlcs = 1
1397
- assertPayments (t , db , payments )
1398
+ assertPayments (t , paymentDB , payments )
1398
1399
1399
1400
// Now delete all failed payments.
1400
- numPayments , err = db .DeletePayments (true , false )
1401
+ numPayments , err = paymentDB .DeletePayments (true , false )
1401
1402
require .NoError (t , err )
1402
1403
require .EqualValues (t , 1 , numPayments )
1403
1404
1404
- assertPayments (t , db , payments [1 :])
1405
+ assertPayments (t , paymentDB , payments [1 :])
1405
1406
1406
1407
// Finally delete all completed payments.
1407
- numPayments , err = db .DeletePayments (false , false )
1408
+ numPayments , err = paymentDB .DeletePayments (false , false )
1408
1409
require .NoError (t , err )
1409
1410
require .EqualValues (t , 1 , numPayments )
1410
1411
1411
- assertPayments (t , db , payments [2 :])
1412
+ assertPayments (t , paymentDB , payments [2 :])
1412
1413
}
1413
1414
1414
1415
// TestKVPaymentsDBDeleteSinglePayment tests that DeletePayment correctly
@@ -1447,55 +1448,55 @@ func TestKVPaymentsDBDeleteSinglePayment(t *testing.T) {
1447
1448
createTestPayments (t , paymentDB , payments )
1448
1449
1449
1450
// Check that all payments are there as we added them.
1450
- assertPayments (t , db , payments )
1451
+ assertPayments (t , paymentDB , payments )
1451
1452
1452
1453
// Delete HTLC attempts for first payment only.
1453
- require .NoError (t , db .DeletePayment (payments [0 ].id , true ))
1454
+ require .NoError (t , paymentDB .DeletePayment (payments [0 ].id , true ))
1454
1455
1455
1456
// The first payment is the only altered one as its failed HTLC should
1456
1457
// have been removed but is still present as payment.
1457
1458
payments [0 ].htlcs = 0
1458
- assertPayments (t , db , payments )
1459
+ assertPayments (t , paymentDB , payments )
1459
1460
1460
1461
// Delete the first payment completely.
1461
- require .NoError (t , db .DeletePayment (payments [0 ].id , false ))
1462
+ require .NoError (t , paymentDB .DeletePayment (payments [0 ].id , false ))
1462
1463
1463
1464
// The first payment should have been deleted.
1464
- assertPayments (t , db , payments [1 :])
1465
+ assertPayments (t , paymentDB , payments [1 :])
1465
1466
1466
1467
// Now delete the second payment completely.
1467
- require .NoError (t , db .DeletePayment (payments [1 ].id , false ))
1468
+ require .NoError (t , paymentDB .DeletePayment (payments [1 ].id , false ))
1468
1469
1469
1470
// The Second payment should have been deleted.
1470
- assertPayments (t , db , payments [2 :])
1471
+ assertPayments (t , paymentDB , payments [2 :])
1471
1472
1472
1473
// Delete failed HTLC attempts for the third payment.
1473
- require .NoError (t , db .DeletePayment (payments [2 ].id , true ))
1474
+ require .NoError (t , paymentDB .DeletePayment (payments [2 ].id , true ))
1474
1475
1475
1476
// Only the successful HTLC attempt should be left for the third
1476
1477
// payment.
1477
1478
payments [2 ].htlcs = 1
1478
- assertPayments (t , db , payments [2 :])
1479
+ assertPayments (t , paymentDB , payments [2 :])
1479
1480
1480
1481
// Now delete the third payment completely.
1481
- require .NoError (t , db .DeletePayment (payments [2 ].id , false ))
1482
+ require .NoError (t , paymentDB .DeletePayment (payments [2 ].id , false ))
1482
1483
1483
1484
// Only the last payment should be left.
1484
- assertPayments (t , db , payments [3 :])
1485
+ assertPayments (t , paymentDB , payments [3 :])
1485
1486
1486
1487
// Deleting HTLC attempts from InFlight payments should not work and an
1487
1488
// error returned.
1488
- require .Error (t , db .DeletePayment (payments [3 ].id , true ))
1489
+ require .Error (t , paymentDB .DeletePayment (payments [3 ].id , true ))
1489
1490
1490
1491
// The payment is InFlight and therefore should not have been altered.
1491
- assertPayments (t , db , payments [3 :])
1492
+ assertPayments (t , paymentDB , payments [3 :])
1492
1493
1493
1494
// Finally deleting the InFlight payment should also not work and an
1494
1495
// error returned.
1495
- require .Error (t , db .DeletePayment (payments [3 ].id , false ))
1496
+ require .Error (t , paymentDB .DeletePayment (payments [3 ].id , false ))
1496
1497
1497
1498
// The payment is InFlight and therefore should not have been altered.
1498
- assertPayments (t , db , payments [3 :])
1499
+ assertPayments (t , paymentDB , payments [3 :])
1499
1500
}
1500
1501
1501
1502
// TestKVPaymentsDBMultiShard checks the ability of payment control to
@@ -1921,7 +1922,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
1921
1922
createTestPayments (t , paymentDB , payments )
1922
1923
1923
1924
// Check that all payments are there as we added them.
1924
- assertPayments (t , db , payments )
1925
+ assertPayments (t , paymentDB , payments )
1925
1926
1926
1927
// Calling DeleteFailedAttempts on a failed payment should delete all
1927
1928
// HTLCs.
@@ -1931,7 +1932,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
1931
1932
if ! keepFailedPaymentAttempts {
1932
1933
payments [0 ].htlcs = 0
1933
1934
}
1934
- assertPayments (t , db , payments )
1935
+ assertPayments (t , paymentDB , payments )
1935
1936
1936
1937
// Calling DeleteFailedAttempts on an in-flight payment should return
1937
1938
// an error.
@@ -1945,7 +1946,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
1945
1946
1946
1947
// Since DeleteFailedAttempts returned an error, we should expect the
1947
1948
// payment to be unchanged.
1948
- assertPayments (t , db , payments )
1949
+ assertPayments (t , paymentDB , payments )
1949
1950
1950
1951
// Cleaning up a successful payment should remove failed htlcs.
1951
1952
require .NoError (t , paymentDB .DeleteFailedAttempts (payments [2 ].id ))
@@ -1954,7 +1955,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
1954
1955
if ! keepFailedPaymentAttempts {
1955
1956
payments [2 ].htlcs = 1
1956
1957
}
1957
- assertPayments (t , db , payments )
1958
+ assertPayments (t , paymentDB , payments )
1958
1959
1959
1960
if keepFailedPaymentAttempts {
1960
1961
// DeleteFailedAttempts is ignored, even for non-existent
@@ -2216,10 +2217,12 @@ func createTestPayments(t *testing.T, p *KVPaymentsDB, payments []*payment) {
2216
2217
// indices for the slice asserts that exactly the same payments in the
2217
2218
// slice for the provided indices exist when fetching payments from the
2218
2219
// database.
2219
- func assertPayments (t * testing.T , db * DB , payments []* payment ) {
2220
+ func assertPayments (t * testing.T , paymentDB * KVPaymentsDB ,
2221
+ payments []* payment ) {
2222
+
2220
2223
t .Helper ()
2221
2224
2222
- dbPayments , err := db .FetchPayments ()
2225
+ dbPayments , err := paymentDB .FetchPayments ()
2223
2226
require .NoError (t , err , "could not fetch payments from db" )
2224
2227
2225
2228
// Make sure that the number of fetched payments is the same
0 commit comments