Fix: Return proper numeric type for consensus branch Id.

Closes https://github.com/zcash/zcash-android-wallet-sdk/issues/224
Related to https://github.com/nighthawk-apps/nighthawk-wallet-android/pull/35
This commit is contained in:
Kevin Gorham 2021-04-28 10:12:41 -04:00
parent c6d69b8266
commit 028bb95d5d
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
2 changed files with 20 additions and 13 deletions

View File

@ -16,15 +16,22 @@ import org.junit.runners.Parameterized
class BranchIdTest(
private val networkName: String,
private val height: Int,
private val branchId: Long,
private val branchHex: String,
private val rustBackend: RustBackendWelding
) {
@Test
fun testBranchId() {
fun testBranchId_Hex() {
val branchId = rustBackend.getBranchIdForHeight(height)
val clientBranch = "%x".format(branchId)
assertEquals("Invalid branch ID for $networkName at height $height on ${rustBackend.network.networkName}", branchHex, clientBranch)
assertEquals("Invalid branch Id Hex value for $networkName at height $height on ${rustBackend.network.networkName}", branchHex, clientBranch)
}
@Test
fun testBranchId_Numeric() {
val actual = rustBackend.getBranchIdForHeight(height)
assertEquals("Invalid branch ID for $networkName at height $height on ${rustBackend.network.networkName}", branchId, actual)
}
companion object {
@ -40,16 +47,16 @@ class BranchIdTest(
val mainnetBackend = RustBackend.init("", "", "", ZcashNetwork.Mainnet)
return listOf(
// Mainnet Cases
arrayOf("Sapling", 419_200, "76b809bb", mainnetBackend),
arrayOf("Blossom", 653_600, "2bb40e60", mainnetBackend),
arrayOf("Heartwood", 903_000, "f5b9230b", mainnetBackend),
arrayOf("Canopy", 1_046_400, "e9ff75a6", mainnetBackend),
arrayOf("Sapling", 419_200, 1991772603L, "76b809bb", mainnetBackend),
arrayOf("Blossom", 653_600, 733220448L, "2bb40e60", mainnetBackend),
arrayOf("Heartwood", 903_000, 4122551051L, "f5b9230b", mainnetBackend),
arrayOf("Canopy", 1_046_400, 3925833126L, "e9ff75a6", mainnetBackend),
// Testnet Cases
arrayOf("Sapling", 280_000, "76b809bb", testnetBackend),
arrayOf("Blossom", 584_000, "2bb40e60", testnetBackend),
arrayOf("Heartwood", 903_800, "f5b9230b", testnetBackend),
arrayOf("Canopy", 1_028_500, "e9ff75a6", testnetBackend),
arrayOf("Sapling", 280_000, 1991772603L, "76b809bb", testnetBackend),
arrayOf("Blossom", 584_000, 733220448L, "2bb40e60", testnetBackend),
arrayOf("Heartwood", 903_800, 4122551051L, "f5b9230b", testnetBackend),
arrayOf("Canopy", 1_028_500, 3925833126L, "e9ff75a6", testnetBackend),
)
}
}

View File

@ -1158,13 +1158,13 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_branchIdFor
_: JClass<'_>,
height: jint,
network_id: jint,
) -> jint {
) -> jlong {
let res = panic::catch_unwind(|| {
let network = parse_network(network_id as u32)?;
let branch: BranchId = BranchId::for_height(&network, BlockHeight::from(height as u32));
let branch_id: u32 = u32::from(branch);
debug!("For height {} found consensus branch {:?}", height, branch);
Ok(branch_id as i32)
debug!("For height {} found consensus branch {:?} with id {}", height, branch, branch_id);
Ok(branch_id.into())
});
unwrap_exc_or(&env, res, -1)
}