Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to jni 0.21 #1357

Merged
merged 1 commit into from
Jan 22, 2024
Merged

Migrate to jni 0.21 #1357

merged 1 commit into from
Jan 22, 2024

Conversation

str4d
Copy link
Contributor

@str4d str4d commented Jan 19, 2024

In addition to the necessary breaking changes, we also clean up the style to match what is used in the jni crate examples.

Note
This code review checklist is intended to serve as a starting point for the author and reviewer, although it may not be appropriate for all types of changes (e.g. fixing a spelling typo in documentation). For more in-depth discussion of how we think about code review, please see Code Review Guidelines.

Author

  • Self-review your own code in GitHub's web interface1
  • Add automated tests as appropriate
  • Update the manual tests2 as appropriate
  • Check the code coverage3 report for the automated tests
  • Update documentation as appropriate (e.g README.md, Architecture.md, etc.)
  • Run the demo app and try the changes
  • Pull in the latest changes from the main branch and squash your commits before assigning a reviewer4

Reviewer

  • Check the code with the Code Review Guidelines checklist
  • Perform an ad hoc review5
  • Review the automated tests
  • Review the manual tests
  • Review the documentation, README.md, Architecture.md, etc. as appropriate
  • Run the demo app and try the changes6

Footnotes

  1. Code often looks different when reviewing the diff in a browser, making it easier to spot potential bugs.

  2. While we aim for automated testing of the SDK, some aspects require manual testing. If you had to manually test
    something during development of this pull request, write those steps down.

  3. While we are not looking for perfect coverage, the tool can point out potential cases that have been missed. Code coverage can be generated with: ./gradlew check for Kotlin modules and ./gradlew connectedCheck -PIS_ANDROID_INSTRUMENTATION_TEST_COVERAGE_ENABLED=true for Android modules.

  4. Having your code up to date and squashed will make it easier for others to review. Use best judgement when squashing commits, as some changes (such as refactoring) might be easier to review as a separate commit.

  5. In addition to a first pass using the code review guidelines, do a second pass using your best judgement and experience which may identify additional questions or comments. Research shows that code review is most effective when done in multiple passes, where reviewers look for different things through each pass.

  6. While the CI server runs the demo app to look for build failures or crashes, humans running the demo app are
    more likely to notice unexpected log messages, UI inconsistencies, or bad output data. Perform this step last, after verifying the code changes are safe to run locally.

In addition to the necessary breaking changes, we also clean up the
style to match what is used in the `jni` crate examples.
@@ -80,16 +80,16 @@ fn print_debug_state() {
}

fn wallet_db<P: Parameters>(
env: &JNIEnv<'_>,
env: &mut JNIEnv,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this wasn't &mut already, given that a JNIEnv can only be used by a single thread.

Copy link
Contributor

@daira daira Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading jni-rs/jni-rs#392 (comment) I see the issue now; prior to implementing per-frame JNIEnv wrappers with local lifetime, it would have been too restrictive.

_: JClass<'_>,
fsblockdb_root: JString<'_>,
pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_initBlockMetaDb<'local>(
mut env: JNIEnv<'local>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused by this being mut env rather than &mut env. I guess this section of the jni crate docs partially explains it: if this parameter were &mut env then it would need to be last. But it is not clear to me why that is; or what the trade-offs are between making it mut env, and making it &mut env with reordered parameters.

Copy link
Contributor

@daira daira Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is the extern function implementing a method, not an independent function that happens to take a Rust JNIEnv as in the section I linked. So the env has to be first, and the jni crate will handle creating a new Rust JNIEnv for the frame. (This actually wraps the same underlying C JNIEnv, which is part of what was confusing me: there are two separate concepts of a JNIEnv at the C and Rust layers.) Got it.

I might submit a PR to the jni crate docs to clarify this, for people who find the description of how JNIEnv is used and then are confused in a similar way.

}

fn decode_usk(env: &JNIEnv<'_>, usk: jbyteArray) -> Result<UnifiedSpendingKey, failure::Error> {
fn decode_usk(env: &JNIEnv, usk: JByteArray) -> Result<UnifiedSpendingKey, failure::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind-of an implementation detail that this doesn't need to call JNIEnv methods that take &mut self. But since it's not public, that doesn't matter I guess.

@@ -52,7 +52,7 @@ pub fn unwrap_exc_or<T>(env: &JNIEnv, res: ExceptionResult<T>, error_val: T) ->

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the commented-out code or change it to use env: &mut JNIEnv so that it would compile. (Probably delete it: writing unwrap_exc_or_default is not much more concise than just passing the default, and I prefer the explicitness of the latter.)

) -> Result<CommitmentTreeRoot<sapling::Node>, failure::Error> {
let long_as_u32 = |name| -> Result<u32, failure::Error> {
fn long_as_u32(env: &mut JNIEnv, obj: &JObject, name: &str) -> Result<u32, failure::Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps these helpers should be in utils, since they're used more than once?

Copy link
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK with comments. If the calling convention isn't fixed in this PR, please open an issue to do so.

Copy link
Contributor

@HonzaR HonzaR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just tested these changes with the basic demo app use cases. All worked as expected.

@str4d str4d merged commit e2f1e92 into feature-2.1.0 Jan 22, 2024
11 of 12 checks passed
@str4d str4d deleted the jni-0.21 branch January 22, 2024 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants