Skip to content

Commit

Permalink
[#1475] Adopt transaction data requests
Browse files Browse the repository at this point in the history
- Refactor of the logic based on the review, the determination of the calls can't be based on block height but actual response from grpc

[#1475] Adopt transaction data requests

- fixes

[#1475] Adopt transaction data requests

- Changes made on pairing call

[#1475] Adopt transaction data requests

- fixes

[#1475] Adopt transaction data requests

- code cleanup

[#1475] Adopt transaction data requests

- FFI bumped to the latest version with fixes

[#1475] Adopt transaction data requests

- comments removed

[#1475] Adopt transaction data requests

- endHeight for spendsFromAddress reduced by 1
  • Loading branch information
LukasKorba committed Aug 19, 2024
1 parent 35c31a4 commit e5e1fdf
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state" : {
"revision" : "3ccafcddfe51918239d157fd839476959413840f"
"revision" : "36cb5d9a0ac2812cbc6ada0a33ed9dff90a10b1f"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
// .package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", exact: "0.8.1")
// Compiled from 2516a94f8bdc540d951c38b66e9c07e2b8c29cb4
// .package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", branch: "ffi_transaction_requests_preview")
.package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", revision: "3ccafcddfe51918239d157fd839476959413840f")
.package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", revision: "36cb5d9a0ac2812cbc6ada0a33ed9dff90a10b1f")
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protocol BlockDownloaderService {
Gets the transaction for the Id given
- Parameter txId: Data representing the transaction Id
*/
func fetchTransaction(txId: Data) async throws -> ZcashTransaction.Fetched
func fetchTransaction(txId: Data) async throws -> (tx: ZcashTransaction.Fetched?, status: TransactionStatus)

func fetchUnspentTransactionOutputs(tAddress: String, startHeight: BlockHeight) -> AsyncThrowingStream<UnspentTransactionOutputEntity, Error>

Expand Down Expand Up @@ -111,7 +111,7 @@ extension BlockDownloaderServiceImpl: BlockDownloaderService {
try await self.storage.latestHeight()
}

func fetchTransaction(txId: Data) async throws -> ZcashTransaction.Fetched {
func fetchTransaction(txId: Data) async throws -> (tx: ZcashTransaction.Fetched?, status: TransactionStatus) {
try await lightwalletService.fetchTransaction(txId: txId)
}
}
67 changes: 24 additions & 43 deletions Sources/ZcashLightClientKit/Block/Enhance/BlockEnhancer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,6 @@ struct BlockEnhancerImpl {
let metrics: SDKMetrics
let service: LightWalletService
let logger: Logger

private func enhance(txId: Data) async throws -> ZcashTransaction.Overview {
logger.debug("Zoom.... Enhance... Tx: \(txId.toHexStringTxId())")

let fetchedTransaction = try await blockDownloaderService.fetchTransaction(txId: txId)

if fetchedTransaction.minedHeight == -1 {
try await rustBackend.setTransactionStatus(txId: fetchedTransaction.rawID, status: .txidNotRecognized)
} else if fetchedTransaction.minedHeight == 0 {
try await rustBackend.setTransactionStatus(txId: fetchedTransaction.rawID, status: .notInMainChain)
} else if fetchedTransaction.minedHeight > 0 {
try await rustBackend.decryptAndStoreTransaction(
txBytes: fetchedTransaction.raw.bytes,
minedHeight: Int32(fetchedTransaction.minedHeight)
)
}

return try await transactionRepository.find(rawID: fetchedTransaction.rawID)
}
}

extension BlockEnhancerImpl: BlockEnhancer {
Expand All @@ -94,7 +75,6 @@ extension BlockEnhancerImpl: BlockEnhancer {

// fetch transactions
do {
let startTime = Date()
let transactionDataRequests = try await rustBackend.transactionDataRequests()

guard !transactionDataRequests.isEmpty else {
Expand All @@ -103,10 +83,6 @@ extension BlockEnhancerImpl: BlockEnhancer {
return nil
}

let chainTipHeight = try await blockDownloaderService.latestBlockHeight()
let newlyMinedLowerBound = chainTipHeight - ZcashSDK.expiryOffset
let newlyMinedRange = newlyMinedLowerBound...chainTipHeight

for index in 0 ..< transactionDataRequests.count {
let transactionDataRequest = transactionDataRequests[index]
var retry = true
Expand All @@ -115,29 +91,40 @@ extension BlockEnhancerImpl: BlockEnhancer {
try Task.checkCancellation()
do {
switch transactionDataRequest {
case .getStatus(let txId), .enhancement(let txId):
let confirmedTx = try await enhance(txId: txId.data)
case .getStatus(let txId):
let response = try await blockDownloaderService.fetchTransaction(txId: txId.data)
retry = false

if let fetchedTransaction = response.tx {
try await rustBackend.setTransactionStatus(txId: fetchedTransaction.rawID, status: response.status)
}

let progress = EnhancementProgress(
totalTransactions: transactionDataRequests.count,
enhancedTransactions: index + 1,
lastFoundTransaction: confirmedTx,
range: range,
newlyMined: confirmedTx.isSentTransaction && newlyMinedRange.contains(confirmedTx.minedHeight ?? 0)
)

await didEnhance(progress)
case .enhancement(let txId):
let response = try await blockDownloaderService.fetchTransaction(txId: txId.data)
retry = false

if response.status == .txidNotRecognized {
try await rustBackend.setTransactionStatus(txId: txId.data, status: .txidNotRecognized)
} else if let fetchedTransaction = response.tx {
try await rustBackend.decryptAndStoreTransaction(
txBytes: fetchedTransaction.raw.bytes,
minedHeight: fetchedTransaction.minedHeight
)
}

case .spendsFromAddress(let sfa):
var filter = TransparentAddressBlockFilter()
filter.address = sfa.address
filter.range = BlockRange(startHeight: Int(sfa.blockRangeStart), endHeight: Int(sfa.blockRangeEnd))
filter.range = BlockRange(startHeight: Int(sfa.blockRangeStart), endHeight: Int(sfa.blockRangeEnd - 1))
let stream = service.getTaddressTxids(filter)

for try await rawTransaction in stream {
let minedHeight = (rawTransaction.height == 0 || rawTransaction.height > UInt32.max)

Check warning on line 122 in Sources/ZcashLightClientKit/Block/Enhance/BlockEnhancer.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
? nil : UInt32(rawTransaction.height)

try await rustBackend.decryptAndStoreTransaction(
txBytes: rawTransaction.data.bytes,
minedHeight: Int32(rawTransaction.height)
minedHeight: minedHeight
)
}
retry = false
Expand All @@ -151,12 +138,6 @@ extension BlockEnhancerImpl: BlockEnhancer {
}
}
}

let endTime = Date()
let diff = endTime.timeIntervalSince1970 - startTime.timeIntervalSince1970
let logMsg = "Enhanced \(transactionDataRequests.count) transaction data requests in \(diff)"
logger.sync(logMsg)
metrics.actionDetail(logMsg, for: .enhance)
} catch {
logger.error("error enhancing transactions! \(error)")
throw error
Expand Down
2 changes: 1 addition & 1 deletion Sources/ZcashLightClientKit/Entity/TransactionEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public enum ZcashTransaction {
/// Used when fetching blocks from the lightwalletd
struct Fetched {
public let rawID: Data
public let minedHeight: BlockHeight
public let minedHeight: UInt32?
public let raw: Data
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,31 @@ extension LightWalletGRPCService: LightWalletService {
}
}

func fetchTransaction(txId: Data) async throws -> ZcashTransaction.Fetched {
func fetchTransaction(txId: Data) async throws -> (tx: ZcashTransaction.Fetched?, status: TransactionStatus) {
var txFilter = TxFilter()
txFilter.hash = txId

do {
let rawTx = try await compactTxStreamer.getTransaction(txFilter)
return ZcashTransaction.Fetched(
rawID: txId,
minedHeight: rawTx.height == UInt64.max ? BlockHeight(-1) : BlockHeight(rawTx.height),
raw: rawTx.data

let isNotMined = rawTx.height == 0 || rawTx.height > UInt32.max

return (
tx:
ZcashTransaction.Fetched(
rawID: txId,
minedHeight: isNotMined ? nil : UInt32(rawTx.height),
raw: rawTx.data
),
status: isNotMined ? .notInMainChain : .mined(Int(rawTx.height))
)
} catch let error as GRPCStatus {
if error.makeGRPCStatus().code == .notFound {
return (tx: nil, .txidNotRecognized)
} else {
let serviceError = error.mapToServiceError()
throw ZcashError.serviceFetchTransactionFailed(serviceError)
}
} catch {
let serviceError = error.mapToServiceError()
throw ZcashError.serviceFetchTransactionFailed(serviceError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protocol LightWalletService: AnyObject {
/// - Throws: LightWalletServiceError
/// - Returns: LightWalletServiceResponse
/// - Throws: `serviceFetchTransactionFailed` when GRPC call fails.
func fetchTransaction(txId: Data) async throws -> ZcashTransaction.Fetched
func fetchTransaction(txId: Data) async throws -> (tx: ZcashTransaction.Fetched?, status: TransactionStatus)

/// - Throws: `serviceFetchUTXOsFailed` when GRPC call fails.
// sourcery: mockedName="fetchUTXOsSingle"
Expand Down
4 changes: 2 additions & 2 deletions Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
}

@DBActor
func decryptAndStoreTransaction(txBytes: [UInt8], minedHeight: Int32) async throws {
func decryptAndStoreTransaction(txBytes: [UInt8], minedHeight: UInt32?) async throws {
let result = zcashlc_decrypt_and_store_transaction(
dbData.0,
dbData.1,
txBytes,
UInt(txBytes.count),
Int64(minedHeight),
Int64(minedHeight ?? 0),
networkType.networkId
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protocol ZcashRustBackendWelding {
/// - parameter tx: the transaction to decrypt
/// - parameter minedHeight: height on which this transaction was mined. this is used to fetch the consensus branch ID.
/// - Throws: `rustDecryptAndStoreTransaction`.
func decryptAndStoreTransaction(txBytes: [UInt8], minedHeight: Int32) async throws
func decryptAndStoreTransaction(txBytes: [UInt8], minedHeight: UInt32?) async throws

/// Returns the most-recently-generated unified payment address for the specified account.
/// - parameter account: index of the given account
Expand Down

0 comments on commit e5e1fdf

Please sign in to comment.