//! JNI bridge for the platform-wallet-ffi `EventHandlerCallbacks` vtable //! and its size/version-tagged additive event extension. //! //! Kotlin counterpart: `org.dashfoundation.dashsdk.ffi.NativeWalletEventBridge`, //! reached from the manager built in [`crate::wallet_manager`]. //! //! ## Trampoline contract (same discipline as `persistence.rs`) //! //! 1. **Attach the thread.** Completion / progress callbacks fire on Tokio //! worker threads ART knows nothing about, so each trampoline attaches //! via `JavaVM::attach_current_thread_as_daemon()` (daemon = never //! detach; ART reaps the attachment on thread death). //! 2. **Never unwind.** The whole body runs under `catch_unwind`; a panic is //! swallowed rather than unwinding across the C ABI (UB). Events are //! fire-and-forget (`void` return), so there is no error code to report — //! a dropped event just means the host's poll fallback picks up liveness. //! 3. **Copy before returning.** Every Rust-owned payload (`[u8; 32]` ids, //! `*const c_char` error messages, the result slice itself) is copied //! into JVM objects before the trampoline returns — the FFI pointers are //! valid only for the callback window. //! 4. **Fan out per entry.** The completion callbacks hand a `results` //! array; we make one flat `call_method` per entry (mirroring how //! `persistence.rs` walks its changeset slices), plus a single boundary //! call marking the pass boundary with its unix timestamp. Kotlin never //! touches native memory. #![allow(clippy::missing_safety_doc)] use crate::support::JVM; use jni::objects::{GlobalRef, JByteArray, JObject, JValue}; use jni::JNIEnv; use platform_wallet_ffi::event_handler::{ DpnsSyncWalletResultFFI, EventHandlerCallbacks, EventHandlerCallbacksExtension, }; use platform_wallet_ffi::platform_address_sync::PlatformAddressSyncWalletResultFFI; use platform_wallet_ffi::shielded_types::{ ShieldedBalanceAccountFFI, ShieldedBalanceUpdateFFI, ShieldedSyncWalletResultFFI, }; use std::ffi::{c_void, CStr}; use std::os::raw::c_char; use std::panic::{catch_unwind, AssertUnwindSafe}; // ── Context ─────────────────────────────────────────────────────────── /// Boxed context for the event-handler vtable. Holds the Kotlin /// `NativeWalletEventBridge` as a `GlobalRef` so it survives across the /// vtable's lifetime and across threads. Ownership transfers to the /// native manager at create (the vtable's `release_fn` is /// [`release_event_ctx`]): Rust frees the box — and with it the /// `GlobalRef` — exactly once, when the manager and every worker that /// could still dispatch an event have dropped their references. pub(crate) struct KotlinEventCtx { pub(crate) bridge: GlobalRef, } impl KotlinEventCtx { pub(crate) fn new(bridge: GlobalRef) -> Self { Self { bridge } } } // SAFETY: `GlobalRef` is valid from any attached thread; trampolines // re-attach per call. unsafe impl Send for KotlinEventCtx {} unsafe impl Sync for KotlinEventCtx {} // ── Attach helper ───────────────────────────────────────────────────── /// Attach the current (Tokio) thread and hand `f` the env + the bridge /// object. Any pending exception left by `f` (or an attach failure) is /// cleared; the whole thing runs under `catch_unwind`. Fire-and-forget — /// no value is returned to Rust. /// /// # Safety /// `context` must be a live `KotlinEventCtx` pointer. unsafe fn with_bridge(context: *mut c_void, f: F) where F: FnOnce(&mut JNIEnv, &JObject) -> Result<(), jni::errors::Error>, { let _ = catch_unwind(AssertUnwindSafe(|| { if context.is_null() { return; } let Some(vm) = JVM.get() else { return }; let Ok(mut env) = vm.attach_current_thread_as_daemon() else { return; }; let ctx = &*(context as *const KotlinEventCtx); let bridge = ctx.bridge.as_obj(); let env: &mut JNIEnv = &mut env; if env.with_local_frame(32, |env| f(env, bridge)).is_err() { let _ = env.exception_clear(); } })); } /// `count` items behind `ptr`, or an empty slice when null / zero. /// /// # Safety /// `ptr` must point to `count` valid `T`s (or be null with `count == 0`). unsafe fn slice_or_empty<'a, T>(ptr: *const T, count: usize) -> &'a [T] { if ptr.is_null() || count == 0 { &[] } else { std::slice::from_raw_parts(ptr, count) } } /// Copy a 32-byte id into a JVM `byte[]`. fn id32<'l>(env: &JNIEnv<'l>, id: &[u8; 32]) -> Result, jni::errors::Error> { env.byte_array_from_slice(id) } /// Copy a NUL-terminated C string into an optional JVM `String`; null → JVM null. fn cstr_opt<'l>(env: &JNIEnv<'l>, ptr: *const c_char) -> Result, jni::errors::Error> { if ptr.is_null() { return Ok(JObject::null()); } // SAFETY: the FFI guarantees the message is a valid CString for the // callback window. let s = unsafe { CStr::from_ptr(ptr) }.to_string_lossy(); Ok(env.new_string(s)?.into()) } // ── on_wallet_event / on_error (ABI-simple) ─────────────────────────── /// `on_wallet_event_fn`: Debug-formatted UTF-8 bytes (not necessarily /// NUL-terminated) → `NativeWalletEventBridge.onWalletEvent(String)`. unsafe extern "C" fn tramp_wallet_event( context: *mut c_void, event_json: *const u8, event_json_len: usize, ) { with_bridge(context, |env, bridge| { let text = if event_json.is_null() { String::new() } else { let bytes = std::slice::from_raw_parts(event_json, event_json_len); String::from_utf8_lossy(bytes).into_owned() }; let jtext = env.new_string(text)?; env.call_method( bridge, "onWalletEvent", "(Ljava/lang/String;)V", &[(&jtext).into()], )?; Ok(()) }); } /// `on_error_fn`: NUL-terminated C string → `NativeWalletEventBridge.onError(String)`. unsafe extern "C" fn tramp_error(context: *mut c_void, error_msg: *const c_char) { with_bridge(context, |env, bridge| { let text = if error_msg.is_null() { String::new() } else { CStr::from_ptr(error_msg).to_string_lossy().into_owned() }; let jtext = env.new_string(text)?; env.call_method( bridge, "onError", "(Ljava/lang/String;)V", &[(&jtext).into()], )?; Ok(()) }); } // ── on_platform_address_sync_completed ──────────────────────────────── /// One flat call per wallet result, then a boundary call marking the pass /// with its unix timestamp + the total wallet count. unsafe extern "C" fn tramp_platform_address_sync_completed( context: *mut c_void, results: *const PlatformAddressSyncWalletResultFFI, count: usize, sync_unix_seconds: u64, ) { with_bridge(context, |env, bridge| { for r in slice_or_empty(results, count) { env.with_local_frame(16, |env| -> Result<(), jni::errors::Error> { let wid = id32(env, &r.wallet_id)?; let err = cstr_opt(env, r.error_message)?; env.call_method( bridge, "onPlatformAddressSyncCompleted", "([BZJJJJJJLjava/lang/String;)V", &[ (&wid).into(), JValue::Bool(r.success as u8), JValue::Long(r.found_count as i64), JValue::Long(r.absent_count as i64), JValue::Long(r.checkpoint_height as i64), JValue::Long(r.new_sync_height as i64), JValue::Long(r.new_sync_timestamp as i64), JValue::Long(r.last_known_recent_block as i64), (&err).into(), ], )?; Ok(()) })?; } env.call_method( bridge, "onPlatformAddressSyncPassCompleted", "(JI)V", &[ JValue::Long(sync_unix_seconds as i64), JValue::Int(count as i32), ], )?; Ok(()) }); } // ── on_dpns_marketplace_sync_completed (versioned extension) ───────── unsafe extern "C" fn tramp_dpns_marketplace_sync_completed( context: *mut c_void, results: *const DpnsSyncWalletResultFFI, count: usize, sync_unix_seconds: u64, ) { with_bridge(context, |env, bridge| { for result in slice_or_empty(results, count) { env.with_local_frame(16, |env| -> Result<(), jni::errors::Error> { let wallet_id = id32(env, &result.wallet_id)?; let error = cstr_opt(env, result.error_message)?; env.call_method( bridge, "onDpnsMarketplaceSyncCompleted", "([BZIIIILjava/lang/String;)V", &[ (&wallet_id).into(), JValue::Bool(result.success as u8), JValue::Int(result.names_tracked as i32), JValue::Int(result.names_added as i32), JValue::Int(result.names_departed as i32), JValue::Int(result.prices_changed as i32), (&error).into(), ], )?; Ok(()) })?; } env.call_method( bridge, "onDpnsMarketplaceSyncPassCompleted", "(JI)V", &[ JValue::Long(sync_unix_seconds as i64), JValue::Int(count as i32), ], )?; Ok(()) }); } // ── on_shielded_sync_completed ──────────────────────────────────────── unsafe extern "C" fn tramp_shielded_sync_completed( context: *mut c_void, results: *const ShieldedSyncWalletResultFFI, count: usize, sync_unix_seconds: u64, ) { with_bridge(context, |env, bridge| { for r in slice_or_empty(results, count) { env.with_local_frame(16, |env| -> Result<(), jni::errors::Error> { let wid = id32(env, &r.wallet_id)?; let err = cstr_opt(env, r.error_message)?; env.call_method( bridge, "onShieldedSyncCompleted", "([BZZZIJIJLjava/lang/String;)V", &[ (&wid).into(), JValue::Bool(r.success as u8), JValue::Bool(r.skipped as u8), JValue::Bool(r.cooldown_skip as u8), JValue::Int(r.new_notes as i32), JValue::Long(r.total_scanned as i64), JValue::Int(r.newly_spent as i32), JValue::Long(r.balance as i64), (&err).into(), ], )?; Ok(()) })?; } env.call_method( bridge, "onShieldedSyncPassCompleted", "(JI)V", &[ JValue::Long(sync_unix_seconds as i64), JValue::Int(count as i32), ], )?; Ok(()) }); } // ── on_shielded_sync_progress / on_shielded_tree_progress ───────────── unsafe extern "C" fn tramp_shielded_sync_progress( context: *mut c_void, cumulative_scanned: u64, block_height: u64, ) { with_bridge(context, |env, bridge| { env.call_method( bridge, "onShieldedSyncProgress", "(JJ)V", &[ JValue::Long(cumulative_scanned as i64), JValue::Long(block_height as i64), ], )?; Ok(()) }); } unsafe extern "C" fn tramp_shielded_tree_progress( context: *mut c_void, leaves_committed: u64, total_target: u64, ) { with_bridge(context, |env, bridge| { env.call_method( bridge, "onShieldedTreeProgress", "(JJ)V", &[ JValue::Long(leaves_committed as i64), JValue::Long(total_target as i64), ], )?; Ok(()) }); } // ── on_shielded_balance_changed ─────────────────────────────────────── /// Number of `long` values per account row handed to /// `onShieldedBalanceChanged`: account index, spendable credits, established /// (0 or 1), has last scanned index (0 or 1), last scanned index, source. const SHIELDED_BALANCE_ACCOUNT_LONGS: usize = 6; /// JNI descriptor of `NativeWalletEventBridge.onShieldedBalanceChanged`: /// wallet id, revision, status, ordinary spendable credits, ordinary balance /// established, account rows. The Kotlin unit test /// `NativeWalletEventBridgeTest` pins the same signature on its side. const SHIELDED_BALANCE_CHANGED_DESCRIPTOR: &str = "([BJIJZ[J)V"; /// One wallet's shielded balance state after it changed. The update and its /// account array are only valid during this call, so the accounts are copied /// into a `long[]` (rows of [`SHIELDED_BALANCE_ACCOUNT_LONGS`]) before the /// bridge is called. Indices are `u64` bit patterns; amounts go through /// [`kotlin_amount`]. unsafe extern "C" fn tramp_shielded_balance_changed( context: *mut c_void, update: *const ShieldedBalanceUpdateFFI, ) { // SAFETY: the FFI passes a pointer valid for the callback window, or null. let Some(update) = update.as_ref() else { return; }; // Kotlin orders updates by a signed revision. One past `i64::MAX` has no // place in that order, so it is not delivered; the host keeps its last. let Ok(revision) = i64::try_from(update.revision) else { return; }; let rows = shielded_balance_account_longs(slice_or_empty(update.accounts, update.accounts_count)); let (ordinary_credits, ordinary_established) = kotlin_amount( update.ordinary_spendable_credits, update.ordinary_balance_established, ); with_bridge(context, |env, bridge| { env.with_local_frame(16, |env| -> Result<(), jni::errors::Error> { let wid = id32(env, &update.wallet_id)?; let Ok(length) = jni::sys::jsize::try_from(rows.len()) else { // More account values than a JVM array holds: nothing a // real wallet reaches. The host keeps its last update. return Ok(()); }; let accounts = env.new_long_array(length)?; env.set_long_array_region(&accounts, 0, &rows)?; env.call_method( bridge, "onShieldedBalanceChanged", SHIELDED_BALANCE_CHANGED_DESCRIPTOR, &[ (&wid).into(), JValue::Long(revision), JValue::Int(update.status as i32), JValue::Long(ordinary_credits), JValue::Bool(ordinary_established as u8), (&accounts).into(), ], )?; Ok(()) }) }); } /// An amount as Kotlin receives it. Kotlin's `Long` is signed, so credits /// past `i64::MAX` cannot be passed on as they are: such an amount goes over /// as zero and not established, which a host shows as unknown, in place of a /// negative number flagged as a known balance. fn kotlin_amount(credits: u64, established: bool) -> (i64, bool) { match i64::try_from(credits) { Ok(credits) => (credits, established), Err(_) => (0, false), } } /// A scan index as Kotlin receives it: one past `i64::MAX` goes over as "no /// index known", in place of a negative watermark. fn kotlin_scan_index(has_index: bool, index: u64) -> (bool, i64) { match i64::try_from(index) { Ok(index) if has_index => (true, index), _ => (false, 0), } } /// Lay `accounts` out as the flat rows `onShieldedBalanceChanged` documents. fn shielded_balance_account_longs(accounts: &[ShieldedBalanceAccountFFI]) -> Vec { let mut values = Vec::with_capacity(accounts.len() * SHIELDED_BALANCE_ACCOUNT_LONGS); for account in accounts { values.push(i64::from(account.account_index)); let (credits, established) = kotlin_amount(account.spendable_credits, account.established); values.push(credits); values.push(i64::from(established)); let (has_last_scanned_index, last_scanned_index) = kotlin_scan_index(account.has_last_scanned_index, account.last_scanned_index); values.push(i64::from(has_last_scanned_index)); values.push(last_scanned_index); values.push(account.source as i64); } values } // ── Vtable ──────────────────────────────────────────────────────────── /// Build the full event vtable pointing at `context` (a boxed /// [`KotlinEventCtx`]). Every slot is wired: the two ABI-simple event / /// error slots, plus the platform-address + shielded completion / progress /// slots that marshal their payload arrays into per-entry flat calls on the /// Kotlin bridge. DPNS completion lives in [`build_event_extension`] so the /// legacy by-value vtable never grows. pub(crate) fn build_event_vtable(context: *mut c_void) -> EventHandlerCallbacks { EventHandlerCallbacks { context, on_wallet_event_fn: Some(tramp_wallet_event), on_error_fn: Some(tramp_error), on_platform_address_sync_completed_fn: Some(tramp_platform_address_sync_completed), on_shielded_sync_completed_fn: Some(tramp_shielded_sync_completed), on_shielded_sync_progress_fn: Some(tramp_shielded_sync_progress), on_shielded_tree_progress_fn: Some(tramp_shielded_tree_progress), release_fn: Some(release_event_ctx), } } /// Build the size/version-tagged additive event extension. It shares the /// legacy vtable's context and destructor and is copied during manager /// creation. pub(crate) fn build_event_extension() -> EventHandlerCallbacksExtension { EventHandlerCallbacksExtension { on_dpns_marketplace_sync_completed_fn: Some(tramp_dpns_marketplace_sync_completed), on_shielded_balance_changed_fn: Some(tramp_shielded_balance_changed), ..EventHandlerCallbacksExtension::default() } } /// `release_fn` for the event vtable: frees the boxed [`KotlinEventCtx`] /// when the native manager's last event-handler reference drops. The FFI /// guarantees exactly one call, which may land on any Rust thread — /// `GlobalRef`'s own `Drop` attaches that thread to the JVM before /// deleting the reference, so no manual attach is needed here. /// /// # Safety /// `context` must be the live boxed [`KotlinEventCtx`] this vtable was /// built around, never freed elsewhere. unsafe extern "C" fn release_event_ctx(context: *mut c_void) { if !context.is_null() { drop(Box::from_raw(context as *mut KotlinEventCtx)); } } #[cfg(test)] mod shielded_balance_tests { use super::*; use platform_wallet_ffi::shielded_types::ShieldedBalanceSourceFFI; #[test] fn should_lay_out_one_row_per_account() { let accounts = [ ShieldedBalanceAccountFFI { account_index: 0, spendable_credits: 100, established: false, last_scanned_index: 0, has_last_scanned_index: false, source: ShieldedBalanceSourceFFI::NoHistory, }, ShieldedBalanceAccountFFI { account_index: 0x4000_0000, spendable_credits: u64::MAX, established: true, last_scanned_index: 42, has_last_scanned_index: true, source: ShieldedBalanceSourceFFI::ScannedThisSession, }, ]; assert_eq!( shielded_balance_account_longs(&accounts), vec![0, 100, 0, 0, 0, 0, 0x4000_0000, 0, 0, 1, 42, 2] ); assert!(shielded_balance_account_longs(&[]).is_empty()); } #[test] fn should_pass_an_amount_past_a_kotlin_long_as_unknown() { assert_eq!(kotlin_amount(0, true), (0, true)); assert_eq!(kotlin_amount(i64::MAX as u64, true), (i64::MAX, true)); assert_eq!(kotlin_amount(i64::MAX as u64 + 1, true), (0, false)); assert_eq!(kotlin_amount(u64::MAX, true), (0, false)); // An amount Rust did not establish stays not established. assert_eq!(kotlin_amount(7, false), (7, false)); } #[test] fn should_pass_a_scan_index_past_a_kotlin_long_as_not_known() { assert_eq!(kotlin_scan_index(true, 0), (true, 0)); assert_eq!(kotlin_scan_index(true, i64::MAX as u64), (true, i64::MAX)); assert_eq!(kotlin_scan_index(true, i64::MAX as u64 + 1), (false, 0)); // An index that is not set stays not set, whatever the field holds. assert_eq!(kotlin_scan_index(false, 42), (false, 0)); } #[test] fn should_call_the_bridge_with_the_signature_kotlin_declares() { // byte[] walletId, long revision, int status, long credits, // boolean established, long[] accounts, returning void. assert_eq!(SHIELDED_BALANCE_CHANGED_DESCRIPTOR, "([BJIJZ[J)V"); } }