Fix incorrect use of transaction index in find-from-transaction query.

This fixes an issue where the transaction index was incorrectly being
used to filter out transactions from the contents of the
`v_transactions` view, and updates the query to account for the fact
that both the block time and the transaction index may be NULL in the
results of this view.
This commit is contained in:
Kris Nuttycombe 2023-09-19 10:16:33 -06:00
parent e40b1e6c42
commit 94a5f599ae
2 changed files with 11 additions and 8 deletions

View File

@ -88,14 +88,17 @@ class TransactionSQLDAO: TransactionRepository {
func find(from transaction: ZcashTransaction.Overview, limit: Int, kind: TransactionKind) async throws -> [ZcashTransaction.Overview] {
guard
let transactionIndex = transaction.index,
let transactionBlockTime = transaction.blockTime
let transactionBlockHeight = transaction.minedHeight
else { throw ZcashError.transactionRepositoryTransactionMissingRequiredFields }
let query = transactionsView
.order((ZcashTransaction.Overview.Column.minedHeight ?? BlockHeight.max).desc)
.filter(
Int64(transactionBlockTime) > ZcashTransaction.Overview.Column.blockTime
&& transactionIndex > ZcashTransaction.Overview.Column.index
transactionBlockHeight > ZcashTransaction.Overview.Column.minedHeight
|| (
transactionBlockHeight == ZcashTransaction.Overview.Column.minedHeight &&
transactionIndex > (ZcashTransaction.Overview.Column.index ?? -1)
)
)
.filterQueryFor(kind: kind)
.limit(limit)

View File

@ -197,21 +197,21 @@ class TransactionRepositoryTests: XCTestCase {
let transaction = try await self.transactionRepository.find(rawID: rawID)
let transactionsFrom = try await self.transactionRepository.find(from: transaction, limit: Int.max, kind: .all)
XCTAssertEqual(transactionsFrom.count, 8)
XCTAssertEqual(transactionsFrom.count, 15)
transactionsFrom.forEach { preceededTransaction in
guard let preceededTransactionIndex = preceededTransaction.index, let transactionIndex = transaction.index else {
guard let precedingHeight = preceededTransaction.minedHeight, let transactionHeight = transaction.minedHeight else {
XCTFail("Transactions are missing indexes.")
return
}
guard let preceededTransactionBlockTime = preceededTransaction.blockTime, let transactionBlockTime = transaction.blockTime else {
guard let precedingBlockTime = preceededTransaction.blockTime, let transactionBlockTime = transaction.blockTime else {
XCTFail("Transactions are missing block time.")
return
}
XCTAssertLessThan(preceededTransactionIndex, transactionIndex)
XCTAssertLessThan(preceededTransactionBlockTime, transactionBlockTime)
XCTAssertLessThanOrEqual(precedingHeight, transactionHeight)
XCTAssertLessThan(precedingBlockTime, transactionBlockTime)
}
}
}