Skip to content

Commit

Permalink
Migrate to latest revision of librustzcash crates
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Aug 13, 2024
1 parent 02857a5 commit 6c12d04
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 49 deletions.
22 changes: 11 additions & 11 deletions backend-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions backend-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ crate-type = ["staticlib", "cdylib"]
[patch.crates-io]
incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree", rev = "337f59179eda51261e9ddfc6b18e8fb84ea277c9" }
shardtree = { git = "https://github.com/zcash/incrementalmerkletree", rev = "337f59179eda51261e9ddfc6b18e8fb84ea277c9" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_protocol = { git = "https://github.com/zcash/librustzcash.git", rev = "7f7b685b99132505a0fe4ba588f329e3516e9669" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_client_backend = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_client_sqlite = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
zcash_protocol = { git = "https://github.com/zcash/librustzcash.git", rev = "9cfce187f0ad93869fa20ada5f011bab64ccc9c5" }
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ interface Backend {
unifiedSpendingKey: ByteArray
): List<ByteArray>

suspend fun decryptAndStoreTransaction(tx: ByteArray)
/**
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun decryptAndStoreTransaction(
tx: ByteArray,
minedHeight: Long?
)

/**
* Sets up the internal structure of the data database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,17 @@ class RustBackend private constructor(
}
}

override suspend fun decryptAndStoreTransaction(tx: ByteArray) =
withContext(SdkDispatchers.DATABASE_IO) {
decryptAndStoreTransaction(
dataDbFile.absolutePath,
tx,
networkId = networkId
)
}
override suspend fun decryptAndStoreTransaction(
tx: ByteArray,
minedHeight: Long?
) = withContext(SdkDispatchers.DATABASE_IO) {
decryptAndStoreTransaction(
dataDbFile.absolutePath,
tx,
minedHeight = minedHeight ?: -1,
networkId = networkId
)
}

override suspend fun proposeTransfer(
account: Int,
Expand Down Expand Up @@ -616,6 +619,7 @@ class RustBackend private constructor(
private external fun decryptAndStoreTransaction(
dbDataPath: String,
tx: ByteArray,
minedHeight: Long,
networkId: Int
)

Expand Down
10 changes: 7 additions & 3 deletions backend-lib/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_createAcc
let account = db_data.get_account(account_id)?.expect("just created");
let account_index = match account.source() {
AccountSource::Derived { account_index, .. } => account_index,
AccountSource::Imported => unreachable!("just created"),
AccountSource::Imported { .. } => unreachable!("just created"),
};

Ok(encode_usk(env, account_index, usk)?.into_raw())
Expand Down Expand Up @@ -1327,7 +1327,9 @@ fn encode_wallet_summary<'a, P: Parameters>(
.source()
{
AccountSource::Derived { account_index, .. } => account_index,
AccountSource::Imported => unreachable!("Imported accounts are unimplemented"),
AccountSource::Imported { .. } => {
unreachable!("Imported accounts are unimplemented")
}
};
Ok::<_, anyhow::Error>((account_index, balance))
})
Expand Down Expand Up @@ -1556,6 +1558,7 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_decryptAn
_: JClass<'local>,
db_data: JString<'local>,
tx: JByteArray<'local>,
mined_height: jlong,
network_id: jint,
) -> jboolean {
let res = catch_unwind(&mut env, |env| {
Expand All @@ -1570,8 +1573,9 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_decryptAn
// - v5 and above transactions ignore the argument, and parse the correct value
// from their encoding.
let tx = Transaction::read(&tx_bytes[..], BranchId::Sapling)?;
let mined_height = BlockHeight::try_from(mined_height).ok();

match decrypt_and_store_transaction(&network, &mut db_data, &tx) {
match decrypt_and_store_transaction(&network, &mut db_data, &tx, mined_height) {
Ok(()) => Ok(JNI_TRUE),
Err(e) => Err(anyhow!("Error while decrypting transaction: {}", e)),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ internal class FakeRustBackend(
TODO("Not yet implemented")
}

override suspend fun decryptAndStoreTransaction(tx: ByteArray) =
error("Intentionally not implemented in mocked FakeRustBackend implementation.")
override suspend fun decryptAndStoreTransaction(
tx: ByteArray,
minedHeight: Long?
) = error("Intentionally not implemented in mocked FakeRustBackend implementation.")

override suspend fun initDataDb(seed: ByteArray?): Int =
error("Intentionally not implemented in mocked FakeRustBackend implementation.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,8 @@ class CompactBlockProcessor internal constructor(
range = currentEnhancingRange,
repository = repository,
backend = backend,
downloader = downloader
downloader = downloader,
network = network
).collect { enhancingResult ->
Twig.info { "Enhancing result: $enhancingResult" }
resultState =
Expand Down Expand Up @@ -1838,7 +1839,8 @@ class CompactBlockProcessor internal constructor(
range: ClosedRange<BlockHeight>,
repository: DerivedDataRepository,
backend: TypesafeBackend,
downloader: CompactBlockDownloader
downloader: CompactBlockDownloader,
network: ZcashNetwork
): Flow<SyncingResult> =
flow {
Twig.debug { "Enhancing transaction details for blocks $range" }
Expand All @@ -1856,7 +1858,7 @@ class CompactBlockProcessor internal constructor(
}

newTxs.filter { it.minedHeight != null }.onEach { newTransaction ->
val trEnhanceResult = enhanceTransaction(newTransaction, backend, downloader)
val trEnhanceResult = enhanceTransaction(newTransaction, backend, downloader, network)
if (trEnhanceResult is SyncingResult.EnhanceFailed) {
Twig.error { "Encountered transaction enhancing error: ${trEnhanceResult.exception}" }
emit(trEnhanceResult)
Expand All @@ -1872,7 +1874,8 @@ class CompactBlockProcessor internal constructor(
private suspend fun enhanceTransaction(
transaction: DbTransactionOverview,
backend: TypesafeBackend,
downloader: CompactBlockDownloader
downloader: CompactBlockDownloader,
network: ZcashNetwork
): SyncingResult {
Twig.debug {
"Starting enhancing transaction (txid:${transaction.txIdString()} block:${transaction
Expand All @@ -1895,7 +1898,8 @@ class CompactBlockProcessor internal constructor(
transactionId = transaction.txIdString(),
rawTransactionId = transaction.rawId.byteArray,
minedHeight = transaction.minedHeight,
downloader = downloader
downloader = downloader,
network = network
)

// Decrypting and storing transaction is run just once, since we consider it more stable
Expand All @@ -1904,8 +1908,8 @@ class CompactBlockProcessor internal constructor(
"(txid:${transaction.txIdString()} block:${transaction.minedHeight})"
}
decryptTransaction(
transactionData = transactionData,
minedHeight = transaction.minedHeight,
transactionData = transactionData.first,
minedHeight = transactionData.second,
backend = backend
)

Expand All @@ -1931,10 +1935,11 @@ class CompactBlockProcessor internal constructor(
transactionId: String,
rawTransactionId: ByteArray,
minedHeight: BlockHeight,
downloader: CompactBlockDownloader
): ByteArray {
downloader: CompactBlockDownloader,
network: ZcashNetwork
): Pair<ByteArray, BlockHeight?> {
val traceScope = TraceScope("CompactBlockProcessor.fetchTransaction")
var transactionDataResult: ByteArray? = null
var transactionDataResult: Pair<ByteArray, BlockHeight?>? = null
retryUpToAndThrow(TRANSACTION_FETCH_RETRIES) { failedAttempts ->
if (failedAttempts == 0) {
Twig.debug { "Starting to fetch transaction (txid:$transactionId, block:$minedHeight)" }
Expand All @@ -1946,7 +1951,10 @@ class CompactBlockProcessor internal constructor(
}
when (val response = downloader.fetchTransaction(rawTransactionId)) {
is Response.Success -> {
transactionDataResult = response.result.data
val currentMinedHeight =
runCatching { response.result.height.toBlockHeight(network) }
.getOrNull()
transactionDataResult = Pair(response.result.data, currentMinedHeight)
}
is Response.Failure -> {
throw EnhanceTxDownloadError(minedHeight, response.toThrowable())
Expand All @@ -1961,12 +1969,12 @@ class CompactBlockProcessor internal constructor(
@Throws(EnhanceTxDecryptError::class)
private suspend fun decryptTransaction(
transactionData: ByteArray,
minedHeight: BlockHeight,
minedHeight: BlockHeight?,
backend: TypesafeBackend,
) {
val traceScope = TraceScope("CompactBlockProcessor.decryptTransaction")
runCatching {
backend.decryptAndStoreTransaction(transactionData)
backend.decryptAndStoreTransaction(transactionData, minedHeight)
}.onFailure {
traceScope.end()
throw EnhanceTxDecryptError(minedHeight, it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sealed class CompactBlockProcessorException(message: String, cause: Throwable? =

open class EnhanceTransactionError(
message: String,
val height: BlockHeight,
val height: BlockHeight?,
cause: Throwable
) : CompactBlockProcessorException(message, cause) {
class EnhanceTxDownloadError(
Expand All @@ -111,7 +111,7 @@ sealed class CompactBlockProcessorException(message: String, cause: Throwable? =
)

class EnhanceTxDecryptError(
height: BlockHeight,
height: BlockHeight?,
cause: Throwable
) : EnhanceTransactionError(
"Error while attempting to decrypt and store a transaction to enhance",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ internal interface TypesafeBackend {
@Throws(RuntimeException::class)
suspend fun suggestScanRanges(): List<ScanRange>

suspend fun decryptAndStoreTransaction(tx: ByteArray)
suspend fun decryptAndStoreTransaction(
tx: ByteArray,
minedHeight: BlockHeight?
)

fun getSaplingReceiver(ua: String): String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ internal class TypesafeBackendImpl(private val backend: Backend) : TypesafeBacke
)
}

override suspend fun decryptAndStoreTransaction(tx: ByteArray) = backend.decryptAndStoreTransaction(tx)
override suspend fun decryptAndStoreTransaction(
tx: ByteArray,
minedHeight: BlockHeight?
) = backend
.decryptAndStoreTransaction(tx, minedHeight?.value)

override fun getSaplingReceiver(ua: String): String? = backend.getSaplingReceiver(ua)

Expand Down

0 comments on commit 6c12d04

Please sign in to comment.