Merge pull request #734 from zcash/more-rust-updates

More Rust updates
This commit is contained in:
str4d 2022-10-15 00:57:01 +01:00 committed by GitHub
commit b7bb468907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 242 additions and 367 deletions

461
sdk-lib/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,12 +10,12 @@ publish = false
edition = "2018"
[dependencies]
android_logger = "0.9"
android_logger = "0.11"
failure = "0.1"
hdwallet = "0.3.1"
hdwallet-bitcoin = "0.3"
hex = "0.4"
jni = { version = "0.17", default-features = false }
jni = { version = "0.19", default-features = false }
log = "0.4"
log-panics = "2.0.0"
schemer = "0.2"
@ -29,14 +29,14 @@ zcash_proofs = "0.7"
# Revision corresponds to the pending zcash_primitives 0.8.0.
[patch.crates-io]
group = { git = "https://github.com/zkcrypto/group.git", rev = "a7f3ceb2373e9fe536996f7b4d55c797f3e667f0" }
orchard = { git = 'https://github.com/zcash/orchard.git', rev='f206b3f5d4e31bba75d03d9d03d5fa25825a9384' }
zcash_address = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
zcash_client_backend = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
zcash_client_sqlite = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
zcash_note_encryption = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
zcash_primitives = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
zcash_proofs = { git = 'https://github.com/zcash/librustzcash.git', rev='774ffadf5a0120a74d70d281974d079ccd58c600' }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "4035a97d178f99bb889f4d7ed26c59378fa88961" }
schemer = { git = "https://github.com/aschampion/schemer.git", rev = "6726b60f43f72c6e24a18d31be0ec7d42829e5e1" }
schemer-rusqlite = { git = "https://github.com/aschampion/schemer.git", rev = "6726b60f43f72c6e24a18d31be0ec7d42829e5e1" }
zcash_address = { git = 'https://github.com/zcash/librustzcash.git', rev='b3dc323876e07d990392855c30a2958d37750e24' }
zcash_client_backend = { git = 'https://github.com/zcash/librustzcash.git', rev='b3dc323876e07d990392855c30a2958d37750e24' }
zcash_client_sqlite = { git = 'https://github.com/zcash/librustzcash.git', rev='b3dc323876e07d990392855c30a2958d37750e24' }
zcash_primitives = { git = 'https://github.com/zcash/librustzcash.git', rev='b3dc323876e07d990392855c30a2958d37750e24' }
zcash_proofs = { git = 'https://github.com/zcash/librustzcash.git', rev='b3dc323876e07d990392855c30a2958d37750e24' }
## Uncomment this to test librustzcash changes locally
#[patch.crates-io]

View File

@ -559,8 +559,15 @@ class CompactBlockProcessor internal constructor(
): Int = withContext(IO) {
var skipped = 0
val aboveHeight = startHeight
twig("Clearing utxos above height $aboveHeight", -1)
rustBackend.clearUtxos(tAddress, aboveHeight)
// TODO(str4d): We no longer clear UTXOs here, as rustBackend.putUtxo now uses an upsert instead of an insert.
// This means that now-spent UTXOs would previously have been deleted, but now are left in the database (like
// shielded notes). Due to the fact that the lightwalletd query only returns _current_ UTXOs, we don't learn
// about recently-spent UTXOs here, so the transparent balance does not get updated here. Instead, when a
// received shielded note is "enhanced" by downloading the full transaction, we mark any UTXOs spent in that
// transaction as spent in the database. This relies on two current properties: UTXOs are only ever spent in
// shielding transactions, and at least one shielded note from each shielding transaction is always enhanced.
// However, for greater reliability, we may want to alter the Data Access API to support "inferring spentness"
// from what is _not_ returned as a UTXO, or alternatively fetch TXOs from lightwalletd instead of just UTXOs.
twig("Checking for UTXOs above height $aboveHeight")
result.forEach { utxo: Service.GetAddressUtxosReply ->
twig("Found UTXO at height ${utxo.height.toInt()} with ${utxo.valueZat} zatoshi")

View File

@ -232,7 +232,6 @@ internal class RustBackend private constructor(
): Long = withContext(SdkDispatchers.DATABASE_IO) {
createToAddress(
dataDbFile.absolutePath,
usk.account.value,
usk.copyBytes(),
to,
value,
@ -251,7 +250,6 @@ internal class RustBackend private constructor(
return withContext(SdkDispatchers.DATABASE_IO) {
shieldToAddress(
dataDbFile.absolutePath,
usk.account.value,
usk.copyBytes(),
memo ?: ByteArray(0),
"$pathParamsDir/$SPEND_PARAM_FILE_NAME",
@ -281,20 +279,6 @@ internal class RustBackend private constructor(
)
}
override suspend fun clearUtxos(
tAddress: String,
aboveHeightInclusive: BlockHeight
): Boolean = withContext(SdkDispatchers.DATABASE_IO) {
clearUtxos(
dataDbFile.absolutePath,
tAddress,
// The Kotlin API is inclusive, but the Rust API is exclusive.
// This can create invalid BlockHeights if the height is saplingActivationHeight.
aboveHeightInclusive.value - 1,
networkId = network.id
)
}
override suspend fun getDownloadedUtxoBalance(address: String): WalletBalance {
val verified = withContext(SdkDispatchers.DATABASE_IO) {
getVerifiedTransparentBalance(
@ -514,7 +498,6 @@ internal class RustBackend private constructor(
@Suppress("LongParameterList")
private external fun createToAddress(
dbDataPath: String,
account: Int,
usk: ByteArray,
to: String,
value: Long,
@ -528,7 +511,6 @@ internal class RustBackend private constructor(
@Suppress("LongParameterList")
private external fun shieldToAddress(
dbDataPath: String,
account: Int,
usk: ByteArray,
memo: ByteArray,
spendParamsPath: String,
@ -555,14 +537,6 @@ internal class RustBackend private constructor(
networkId: Int
): Boolean
@JvmStatic
private external fun clearUtxos(
dbDataPath: String,
tAddress: String,
aboveHeight: Long,
networkId: Int
): Boolean
@JvmStatic
private external fun getVerifiedTransparentBalance(
pathDataDb: String,

View File

@ -90,11 +90,6 @@ internal interface RustBackendWelding {
height: BlockHeight
): Boolean
suspend fun clearUtxos(
tAddress: String,
aboveHeightInclusive: BlockHeight = BlockHeight(network.saplingActivationHeight.value)
): Boolean
suspend fun getDownloadedUtxoBalance(address: String): WalletBalance
// Implemented by `DerivationTool`

View File

@ -31,18 +31,17 @@ use zcash_client_backend::{
wallet::{
create_spend_to_address, decrypt_and_store_transaction, shield_transparent_funds,
},
WalletRead, WalletReadTransparent, WalletWrite, WalletWriteTransparent,
WalletRead, WalletWrite,
},
encoding::AddressCodec,
keys::{Era, UnifiedFullViewingKey},
wallet::{OvkPolicy, WalletTransparentOutput},
};
use zcash_client_sqlite::wallet::init::WalletMigrationError;
#[allow(deprecated)]
use zcash_client_sqlite::wallet::{delete_utxos_above, get_rewind_height};
use zcash_client_sqlite::wallet::get_rewind_height;
use zcash_client_sqlite::{
error::SqliteClientError,
wallet::init::{init_accounts_table, init_blocks_table, init_wallet_db},
wallet::init::{init_accounts_table, init_blocks_table, init_wallet_db, WalletMigrationError},
BlockDb, NoteId, WalletDb,
};
use zcash_primitives::consensus::Network::{MainNetwork, TestNetwork};
@ -714,7 +713,7 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_getVerified
.map_err(|e| format_err!("Error while fetching verified balance: {}", e))
})?
.iter()
.map(|utxo| utxo.txout.value)
.map(|utxo| utxo.txout().value)
.sum::<Option<Amount>>()
.ok_or_else(|| format_err!("Balance overflowed MAX_MONEY."))?;
@ -752,7 +751,7 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_getTotalTra
.map_err(|e| format_err!("Error while fetching verified balance: {}", e))
})?
.iter()
.map(|utxo| utxo.txout.value)
.map(|utxo| utxo.txout().value)
.sum::<Option<Amount>>()
.ok_or_else(|| format_err!("Balance overflowed MAX_MONEY"))?;
@ -994,14 +993,15 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_putUtxo(
let addr = utils::java_string_to_rust(&env, address);
let _address = TransparentAddress::decode(&network, &addr).unwrap();
let output = WalletTransparentOutput {
outpoint: OutPoint::new(txid, index as u32),
txout: TxOut {
let output = WalletTransparentOutput::from_parts(
OutPoint::new(txid, index as u32),
TxOut {
value: Amount::from_i64(value).unwrap(),
script_pubkey,
},
height: BlockHeight::from(height as u32),
};
BlockHeight::from(height as u32),
)
.ok_or_else(|| format_err!("UTXO is not P2PKH or P2SH"))?;
debug!("Storing UTXO in db_data");
match db_data.put_received_transparent_utxo(&output) {
@ -1012,36 +1012,6 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_putUtxo(
unwrap_exc_or(&env, res, JNI_FALSE)
}
#[no_mangle]
pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_clearUtxos(
env: JNIEnv<'_>,
_: JClass<'_>,
db_data: JString<'_>,
taddress: JString<'_>,
above_height: jlong,
network_id: jint,
) -> jint {
#[allow(deprecated)]
let res = panic::catch_unwind(|| {
let network = parse_network(network_id as u32)?;
let db_data = wallet_db(&env, network, db_data)?;
let mut db_data = db_data.get_update_ops()?;
let addr = utils::java_string_to_rust(&env, taddress);
let taddress = TransparentAddress::decode(&network, &addr).unwrap();
let height = BlockHeight::from(above_height as u32);
debug!(
"clearing UTXOs that were found above height: {}",
above_height
);
match delete_utxos_above(&mut db_data, &taddress, height) {
Ok(rows) => Ok(rows as i32),
Err(e) => Err(format_err!("Error while clearing UTXOs: {}", e)),
}
});
unwrap_exc_or(&env, res, -1)
}
// ADDED BY ANDROID
#[no_mangle]
pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_scanBlockBatch(
@ -1234,7 +1204,6 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_createToAdd
env: JNIEnv<'_>,
_: JClass<'_>,
db_data: JString<'_>,
account: jint,
usk: jbyteArray,
to: JString<'_>,
value: jlong,
@ -1247,11 +1216,6 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_createToAdd
let network = parse_network(network_id as u32)?;
let db_data = wallet_db(&env, network, db_data)?;
let mut db_data = db_data.get_update_ops()?;
let account = if account >= 0 {
account as u32
} else {
return Err(format_err!("account argument must be nonnegative"));
};
let usk = decode_usk(&env, usk)?;
let to = utils::java_string_to_rust(&env, to);
let value =
@ -1282,13 +1246,11 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_createToAdd
let prover = LocalTxProver::new(Path::new(&spend_params), Path::new(&output_params));
// let branch = if
create_spend_to_address(
&mut db_data,
&network,
prover,
AccountId::from(account),
usk.sapling(),
&usk,
&to,
value,
memo,
@ -1305,7 +1267,6 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_shieldToAdd
env: JNIEnv<'_>,
_: JClass<'_>,
db_data: JString<'_>,
account: jint,
usk: jbyteArray,
memo: jbyteArray,
spend_params: JString<'_>,
@ -1316,11 +1277,6 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_shieldToAdd
let network = parse_network(network_id as u32)?;
let db_data = wallet_db(&env, network, db_data)?;
let mut db_data = db_data.get_update_ops()?;
let account = if account >= 0 {
account as u32
} else {
return Err(format_err!("account argument must be nonnegative"));
};
let usk = decode_usk(&env, usk)?;
let memo_bytes = env.convert_byte_array(memo).unwrap();
let spend_params = utils::java_string_to_rust(&env, spend_params);
@ -1334,8 +1290,7 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_shieldToAdd
&mut db_data,
&network,
prover,
usk.transparent(),
AccountId::from(account),
&usk,
&MemoBytes::from(&memo),
0,
)

View File

@ -56,18 +56,12 @@ fn throw(env: &JNIEnv, description: &str) {
let exception = match env.find_class("java/lang/RuntimeException") {
Ok(val) => val,
Err(e) => {
error!(
"Unable to find 'RuntimeException' class: {}",
e.description()
);
error!("Unable to find 'RuntimeException' class: {}", e.to_string());
return;
}
};
if let Err(e) = env.throw_new(exception, description) {
error!(
"Unable to find 'RuntimeException' class: {}",
e.description()
);
error!("Unable to find 'RuntimeException' class: {}", e.to_string());
}
}
@ -88,6 +82,7 @@ pub fn any_to_string(any: &Box<dyn Any + Send>) -> String {
mod tests {
use super::*;
use std::error::Error;
use std::fmt;
use std::panic;
#[test]
@ -118,7 +113,7 @@ mod tests {
assert_eq!("Unknown error occurred", any_to_string(&error));
}
fn panic_error<T: Send + 'static>(val: T) -> Box<dyn Any + Send> {
panic::catch_unwind(panic::AssertUnwindSafe(|| panic!(val))).unwrap_err()
fn panic_error<T: fmt::Display + Send + 'static>(val: T) -> Box<dyn Any + Send> {
panic::catch_unwind(panic::AssertUnwindSafe(|| panic!("{}", val))).unwrap_err()
}
}