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.
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.
@@ -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.
@@ -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
}
+5 -5
View File
@@ -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,
@@ -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,
@@ -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,
@@ -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)
+2 -14
View File
@@ -6637,20 +6637,8 @@ func (r *rpcServer) ListPayments(ctx context.Context,
Reversed: req.Reversed,
IncludeIncomplete: req.IncludeIncomplete,
CountTotal: req.CountTotalPayments,
}
// 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,
)
CreationDateStart: int64(req.CreationDateStart),
CreationDateEnd: int64(req.CreationDateEnd),
}
// If the maximum number of payments wasn't specified, then we'll