multi: fix timestamp filters for payment query

This commit fixes the timestamp precision to always compare the dates
using unix seconds for payments.
This commit is contained in:
yyforyongyu committed 2024-02-05 15:26:49 +08:00
1 parent 18333e8c7d
commit 605211823d
3 files changed
+21 -37

No files matched your search

+14 -18
View File
@@ -471,13 +471,13 @@ type PaymentsQuery struct {
// payment index (complete and incomplete) should be counted. // payment index (complete and incomplete) should be counted.
CountTotal bool CountTotal bool
// CreationDateStart, if set, filters out all payments with a creation // CreationDateStart, expressed in Unix seconds, if set, filters out
// date greater than or euqal to it. // all payments with a creation date greater than or equal to it.
CreationDateStart time.Time CreationDateStart int64
// CreationDateEnd, if set, filters out all payments with a creation // CreationDateEnd, expressed in Unix seconds, if set, filters out all
// date less than or euqal to it. // payments with a creation date less than or equal to it.
CreationDateEnd time.Time CreationDateEnd int64
} }
// PaymentsResponse contains the result of a query to the payments database. // PaymentsResponse contains the result of a query to the payments database.
@@ -512,11 +512,7 @@ type PaymentsResponse struct {
// to a subset of payments by the payments query, containing an offset // to a subset of payments by the payments query, containing an offset
// index and a maximum number of returned payments. // index and a maximum number of returned payments.
func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) { func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
var ( var resp PaymentsResponse
resp PaymentsResponse
startDateSet = !query.CreationDateStart.IsZero()
endDateSet = !query.CreationDateEnd.IsZero()
)
if err := kvdb.View(d, func(tx kvdb.RTx) error { if err := kvdb.View(d, func(tx kvdb.RTx) error {
// Get the root payments bucket. // Get the root payments bucket.
@@ -561,20 +557,20 @@ func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
return false, err 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 // Skip any payments that were created before the
// specified time. // specified time.
if startDateSet && payment.Info.CreationTime.Before( if createTime < query.CreationDateStart {
query.CreationDateStart,
) {
return false, nil return false, nil
} }
// Skip any payments that were created after the // Skip any payments that were created after the
// specified time. // specified time.
if endDateSet && payment.Info.CreationTime.After( if query.CreationDateEnd != 0 &&
query.CreationDateEnd, createTime > query.CreationDateEnd {
) {
return false, nil return false, nil
} }
+5 -5
View File
@@ -438,7 +438,7 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: 2, MaxPayments: 2,
Reversed: false, Reversed: false,
IncludeIncomplete: true, IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 5), CreationDateStart: 5,
}, },
firstIndex: 5, firstIndex: 5,
lastIndex: 6, lastIndex: 6,
@@ -452,7 +452,7 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: 2, MaxPayments: 2,
Reversed: false, Reversed: false,
IncludeIncomplete: true, IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 7), CreationDateStart: 7,
}, },
firstIndex: 7, firstIndex: 7,
lastIndex: 7, lastIndex: 7,
@@ -465,8 +465,8 @@ func TestQueryPayments(t *testing.T) {
MaxPayments: math.MaxUint64, MaxPayments: math.MaxUint64,
Reversed: true, Reversed: true,
IncludeIncomplete: true, IncludeIncomplete: true,
CreationDateStart: time.Unix(0, 3), CreationDateStart: 3,
CreationDateEnd: time.Unix(0, 5), CreationDateEnd: 5,
}, },
firstIndex: 3, firstIndex: 3,
lastIndex: 5, lastIndex: 5,
@@ -509,7 +509,7 @@ func TestQueryPayments(t *testing.T) {
} }
// Override creation time to allow for testing // Override creation time to allow for testing
// of CreationDateStart and CreationDateEnd. // 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. // Create a new payment entry in the database.
err = pControl.InitPayment(info.PaymentIdentifier, info) err = pControl.InitPayment(info.PaymentIdentifier, info)
+2 -14
View File
@@ -6637,20 +6637,8 @@ func (r *rpcServer) ListPayments(ctx context.Context,
Reversed: req.Reversed, Reversed: req.Reversed,
IncludeIncomplete: req.IncludeIncomplete, IncludeIncomplete: req.IncludeIncomplete,
CountTotal: req.CountTotalPayments, CountTotal: req.CountTotalPayments,
} CreationDateStart: int64(req.CreationDateStart),
CreationDateEnd: int64(req.CreationDateEnd),
// Attach the start date if set.
if req.CreationDateStart != 0 {
query.CreationDateStart = time.Unix(
int64(req.CreationDateStart), 0,
)
}
// Attach the end date if set.
if req.CreationDateEnd != 0 {
query.CreationDateEnd = time.Unix(
int64(req.CreationDateEnd), 0,
)
} }
// If the maximum number of payments wasn't specified, then we'll // If the maximum number of payments wasn't specified, then we'll