Skip to content

Commit

Permalink
Create and submit PCZT
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKorba committed Dec 12, 2024
1 parent ee32938 commit f499898
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
func extractAndStoreTxFromPCZT(
pcztWithProofs: Data,
pcztWithSigs: Data
) async throws -> Data {
) async throws -> [Data] {
let pcztPtr: UnsafeMutablePointer<FfiBoxedSlice>? = pcztWithProofs.withUnsafeBytes { pcztWithProofsBuffer in
guard let pcztWithProofsBufferPtr = pcztWithProofsBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
return nil
Expand Down Expand Up @@ -394,10 +394,10 @@ struct ZcashRustBackend: ZcashRustBackendWelding {

defer { zcashlc_free_boxed_slice(pcztPtr) }

return Data(
return [Data(
bytes: pcztPtr.pointee.ptr,
count: Int(pcztPtr.pointee.len)
)
)]
}

@DBActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ protocol ZcashRustBackendWelding {
func extractAndStoreTxFromPCZT(
pcztWithProofs: Data,
pcztWithSigs: Data
) async throws -> Data
) async throws -> [Data]

/// Gets the consensus branch id for the given height
/// - Parameter height: the height you what to know the branch id for
Expand Down
4 changes: 2 additions & 2 deletions Sources/ZcashLightClientKit/Synchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ public protocol Synchronizer: AnyObject {
pczt: Data
) async throws -> Data

func extractAndStoreTxFromPCZT(
func createTransactionFromPCZT(
pcztWithProofs: Data,
pcztWithSigs: Data
) async throws -> Data
) async throws -> AsyncThrowingStream<TransactionSubmitResult, Error>

/// all the transactions that are on the blockchain
var transactions: [ZcashTransaction.Overview] { get async }
Expand Down
25 changes: 22 additions & 3 deletions Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ public class SDKSynchronizer: Synchronizer {
proposal: proposal,
spendingKey: spendingKey
)

return submitTransactions(transactions)
}

func submitTransactions(_ transactions: [ZcashTransaction.Overview]) -> AsyncThrowingStream<TransactionSubmitResult, Error> {
var iterator = transactions.makeIterator()
var submitFailed = false

Expand Down Expand Up @@ -419,14 +424,28 @@ public class SDKSynchronizer: Synchronizer {
)
}

public func extractAndStoreTxFromPCZT(
public func createTransactionFromPCZT(
pcztWithProofs: Data,
pcztWithSigs: Data
) async throws -> Data {
try await initializer.rustBackend.extractAndStoreTxFromPCZT(
) async throws -> AsyncThrowingStream<TransactionSubmitResult, Error> {
try throwIfUnprepared()

try await SaplingParameterDownloader.downloadParamsIfnotPresent(
spendURL: initializer.spendParamsURL,
spendSourceURL: initializer.saplingParamsSourceURL.spendParamFileURL,
outputURL: initializer.outputParamsURL,
outputSourceURL: initializer.saplingParamsSourceURL.outputParamFileURL,
logger: logger
)

let txIds = try await initializer.rustBackend.extractAndStoreTxFromPCZT(
pcztWithProofs: pcztWithProofs,
pcztWithSigs: pcztWithSigs
)

let transactions = try await transactionEncoder.createTransactionsFromTxIds(txIds)

return submitTransactions(transactions)
}

public func allReceivedTransactions() async throws -> [ZcashTransaction.Overview] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ protocol TransactionEncoder {
/// - Parameter transaction: a transaction overview
func submit(transaction: EncodedTransaction) async throws

func createTransactionsFromTxIds(_ txIds: [Data]) async throws -> [ZcashTransaction.Overview]

func closeDBConnection()
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ class WalletTransactionEncoder: TransactionEncoder {
usk: spendingKey
)

return try await createTransactionsFromTxIds(txIds)

// logger.debug("transaction ids: \(txIds)")
//
// var txs: [ZcashTransaction.Overview] = []
//
// for txId in txIds {
// txs.append(try await repository.find(rawID: txId))
// }
//
// return txs
}

func createTransactionsFromTxIds(_ txIds: [Data]) async throws -> [ZcashTransaction.Overview] {
guard ensureParams(spend: self.spendParamsURL, output: self.outputParamsURL) else {
throw ZcashError.walletTransEncoderCreateTransactionMissingSaplingParams
}

logger.debug("transaction ids: \(txIds)")

var txs: [ZcashTransaction.Overview] = []
Expand All @@ -121,7 +139,7 @@ class WalletTransactionEncoder: TransactionEncoder {

return txs
}

func submit(
transaction: EncodedTransaction
) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,27 +1620,27 @@ class SynchronizerMock: Synchronizer {
}
}

// MARK: - extractAndStoreTxFromPCZT
// MARK: - createTransactionFromPCZT

var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError: Error?
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount = 0
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCalled: Bool {
return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount > 0
var createTransactionFromPCZTPcztWithProofsPcztWithSigsThrowableError: Error?
var createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount = 0
var createTransactionFromPCZTPcztWithProofsPcztWithSigsCalled: Bool {
return createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount > 0
}
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments: (pcztWithProofs: Data, pcztWithSigs: Data)?
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue: Data!
var extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure: ((Data, Data) async throws -> Data)?
var createTransactionFromPCZTPcztWithProofsPcztWithSigsReceivedArguments: (pcztWithProofs: Data, pcztWithSigs: Data)?
var createTransactionFromPCZTPcztWithProofsPcztWithSigsReturnValue: AsyncThrowingStream<TransactionSubmitResult, Error>!
var createTransactionFromPCZTPcztWithProofsPcztWithSigsClosure: ((Data, Data) async throws -> AsyncThrowingStream<TransactionSubmitResult, Error>)?

func extractAndStoreTxFromPCZT(pcztWithProofs: Data, pcztWithSigs: Data) async throws -> Data {
if let error = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsThrowableError {
func createTransactionFromPCZT(pcztWithProofs: Data, pcztWithSigs: Data) async throws -> AsyncThrowingStream<TransactionSubmitResult, Error> {
if let error = createTransactionFromPCZTPcztWithProofsPcztWithSigsThrowableError {
throw error
}
extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsCallsCount += 1
extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReceivedArguments = (pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs)
if let closure = extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsClosure {
createTransactionFromPCZTPcztWithProofsPcztWithSigsCallsCount += 1
createTransactionFromPCZTPcztWithProofsPcztWithSigsReceivedArguments = (pcztWithProofs: pcztWithProofs, pcztWithSigs: pcztWithSigs)
if let closure = createTransactionFromPCZTPcztWithProofsPcztWithSigsClosure {
return try await closure(pcztWithProofs, pcztWithSigs)
} else {
return extractAndStoreTxFromPCZTPcztWithProofsPcztWithSigsReturnValue
return createTransactionFromPCZTPcztWithProofsPcztWithSigsReturnValue
}
}

Expand Down

0 comments on commit f499898

Please sign in to comment.