Repository navigation
perf(platform-wallet)!: reuse a recent recorded-anchor set on the shielded spend path #5280
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
Open
PastaPastaPasta
wants to merge
9
commits into
v5.1-dev
Choose a base branch
from
perf/shielded-anchor-cache
base: v5.1-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,416
−72
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9bc2414
perf(platform-wallet): reuse a recent recorded-anchor set on the shie…
PastaPastaPasta e31b385
feat(platform-wallet-ffi): prefetch shielded spend anchors when a sen…
PastaPastaPasta 199e882
fix(swift-example-app): prefetch spend anchors when shielded notes ar…
PastaPastaPasta f0702eb
fix(platform-wallet): drop anchor-set fetches sent before the cache w…
PastaPastaPasta 973915f
test(platform-wallet-ffi): cover spend-anchor prefetch without shield…
PastaPastaPasta 528c2be
fix(platform-wallet): run one spend-anchor prefetch at a time
PastaPastaPasta 3ebc73e
fix(platform-wallet-ffi): let the spend-anchor prefetch run from an e…
PastaPastaPasta 9a35f88
fix(platform-wallet-ffi): don't wedge a prefetch called from a sync-c…
PastaPastaPasta e56af70
docs(platform-wallet): say a contended fallback fetch leaves the anch…
PastaPastaPasta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
234 changes: 234 additions & 0 deletions
234
packages/rs-platform-wallet-ffi/src/shielded_spend_prefetch.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| //! FFI entry point that prepares an upcoming shielded spend. | ||
| //! | ||
| //! Every shielded spend (transfer, unshield, withdraw, identity top-up / | ||
| //! create from the pool) must pick an anchor Platform has recorded, which | ||
| //! takes a round trip that downloads and proof-verifies Platform's recorded | ||
| //! anchor set. Hosts call | ||
| //! [`platform_wallet_manager_shielded_prefetch_spend_anchors`] when a send | ||
| //! screen opens so that round trip overlaps the user filling in the form | ||
| //! instead of delaying the send. Feature-gated behind `shielded`. | ||
|
|
||
| use crate::error::*; | ||
| use crate::handle::*; | ||
| use crate::runtime::{run_on_big_stack_thread, runtime}; | ||
|
|
||
| /// Prefetch Platform's recorded shielded-anchor set for the next spend on | ||
| /// `handle`'s network, in the background. | ||
| /// | ||
| /// Returns as soon as the fetch is queued; it runs on the FFI runtime. A | ||
| /// shielded send started within the next ~30 s, before any shielded sync | ||
| /// grows the commitment tree, reuses the prefetched set and skips that round | ||
| /// trip; otherwise the send fetches for itself exactly as without this call. | ||
| /// Cheap to call repeatedly (a no-op while a usable set is cached), and a | ||
| /// failed fetch is only logged — it never affects a later send. Safe to call | ||
| /// from any thread, including from inside an event callback. | ||
| /// | ||
| /// Returns `ErrorInvalidHandle` for an unknown `handle` and | ||
| /// `ErrorWalletOperation` when shielded support isn't configured on the | ||
| /// manager. | ||
| #[no_mangle] | ||
| pub extern "C" fn platform_wallet_manager_shielded_prefetch_spend_anchors( | ||
| handle: Handle, | ||
| ) -> PlatformWalletFFIResult { | ||
| // Called from an event callback, this can run while the export that | ||
| // dispatched the event holds the registry, hence the recursive read. | ||
| let lookup = PLATFORM_WALLET_MANAGER_STORAGE.with_item_recursive(handle, |manager| { | ||
| let resolve = || runtime().block_on(manager.shielded_coordinator()); | ||
| // Event callbacks fire inside a runtime context (the sync loop | ||
| // drives its passes with `Handle::block_on`), where a nested | ||
| // `block_on` panics; resolve on a thread outside it instead. | ||
| if tokio::runtime::Handle::try_current().is_ok() { | ||
| run_on_big_stack_thread(resolve) | ||
| } else { | ||
| Ok(resolve()) | ||
| } | ||
| }); | ||
| let coordinator = match lookup { | ||
| None => { | ||
| return PlatformWalletFFIResult::err( | ||
| PlatformWalletFFIResultCode::ErrorInvalidHandle, | ||
| format!("invalid manager handle: {handle}"), | ||
| ); | ||
| } | ||
| Some(Err(e)) => { | ||
| return PlatformWalletFFIResult::err( | ||
| PlatformWalletFFIResultCode::ErrorWalletOperation, | ||
| format!("could not start the shielded spend-anchor prefetch: {e}"), | ||
| ); | ||
| } | ||
| Some(Ok(None)) => { | ||
| return PlatformWalletFFIResult::err( | ||
| PlatformWalletFFIResultCode::ErrorWalletOperation, | ||
| "shielded support not configured — call platform_wallet_manager_configure_shielded first", | ||
| ); | ||
| } | ||
| Some(Ok(Some(coordinator))) => coordinator, | ||
| }; | ||
| runtime().spawn(async move { | ||
| if let Err(e) = coordinator.prefetch_recorded_anchors().await { | ||
| tracing::debug!( | ||
| error = %e, | ||
| "shielded spend-anchor prefetch failed; the next spend fetches for itself" | ||
| ); | ||
| } | ||
| }); | ||
| PlatformWalletFFIResult::ok() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::event_handler::EventHandlerCallbacks; | ||
| use crate::shielded_sync::platform_wallet_manager_shielded_sync_sync_now; | ||
| use crate::shielded_types::ShieldedSyncWalletResultFFI; | ||
| use crate::test_fixtures::{ | ||
| assert_scenario_passes_in_child, mock_manager, mock_manager_with_events, | ||
| }; | ||
| use std::sync::atomic::{AtomicU64, Ordering}; | ||
| use std::sync::{mpsc, Mutex}; | ||
| use std::time::Duration; | ||
|
|
||
| const WAIT: Duration = Duration::from_secs(10); | ||
|
|
||
| fn code_of(mut result: PlatformWalletFFIResult) -> PlatformWalletFFIResultCode { | ||
| let code = result.code; | ||
| unsafe { crate::platform_wallet_ffi_result_free(&mut result) }; | ||
| code | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_reject_an_unknown_manager_handle() { | ||
| assert_eq!( | ||
| code_of(platform_wallet_manager_shielded_prefetch_spend_anchors( | ||
| NULL_HANDLE | ||
| )), | ||
| PlatformWalletFFIResultCode::ErrorInvalidHandle | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_reject_a_manager_without_shielded_support() { | ||
| let handle = PLATFORM_WALLET_MANAGER_STORAGE.insert(mock_manager()); | ||
| assert_eq!( | ||
| code_of(platform_wallet_manager_shielded_prefetch_spend_anchors( | ||
| handle | ||
| )), | ||
| PlatformWalletFFIResultCode::ErrorWalletOperation | ||
| ); | ||
| drop(PLATFORM_WALLET_MANAGER_STORAGE.remove(handle)); | ||
| } | ||
|
|
||
| /// Event callbacks run inside a runtime context; the lookup must not | ||
| /// nest `block_on` there. | ||
| #[test] | ||
| fn should_resolve_the_coordinator_from_inside_a_runtime_context() { | ||
| let handle = PLATFORM_WALLET_MANAGER_STORAGE.insert(mock_manager()); | ||
| let code = runtime().block_on(async { | ||
| code_of(platform_wallet_manager_shielded_prefetch_spend_anchors( | ||
| handle, | ||
| )) | ||
| }); | ||
| assert_eq!(code, PlatformWalletFFIResultCode::ErrorWalletOperation); | ||
| drop(PLATFORM_WALLET_MANAGER_STORAGE.remove(handle)); | ||
| } | ||
|
|
||
| /// Set in the child process that runs [`completion_callback_scenario`]. | ||
| const COMPLETION_CALLBACK_CHILD: &str = | ||
| "PLATFORM_WALLET_FFI_PREFETCH_FROM_COMPLETION_CALLBACK_CHILD"; | ||
|
|
||
| /// The lock cycle a call from a sync-completion callback must not form: | ||
| /// `platform_wallet_manager_shielded_sync_sync_now` holds the registry's | ||
| /// read guard until the pass, completion callback included, returns. A | ||
| /// registry writer queued behind that guard makes parking_lot park every | ||
| /// new plain reader, so a lookup that read the registry normally would | ||
| /// wait on the writer, the writer on the sync, the sync on the callback. | ||
| /// | ||
| /// The scenario depends on the sync export holding that guard across the | ||
| /// pass; if that changes, it no longer reproduces the cycle. It runs in a | ||
| /// child process, as a regression is a real deadlock (see | ||
| /// [`assert_scenario_passes_in_child`]). | ||
| #[test] | ||
| fn should_not_wedge_a_prefetch_from_a_sync_completion_callback_behind_a_queued_registry_writer() | ||
| { | ||
| if std::env::var_os(COMPLETION_CALLBACK_CHILD).is_some() { | ||
| completion_callback_scenario(); | ||
| return; | ||
| } | ||
| assert_scenario_passes_in_child( | ||
| module_path!(), | ||
| "should_not_wedge_a_prefetch_from_a_sync_completion_callback_behind_a_queued_registry_writer", | ||
| COMPLETION_CALLBACK_CHILD, | ||
| WAIT, | ||
| ); | ||
| } | ||
|
|
||
| static SYNCING_HANDLE: AtomicU64 = AtomicU64::new(NULL_HANDLE); | ||
| type CallbackReport = (PlatformWalletFFIResultCode, std::thread::JoinHandle<Handle>); | ||
| static CALLBACK_REPORT: Mutex<Option<mpsc::Sender<CallbackReport>>> = Mutex::new(None); | ||
|
|
||
| /// Queues a registry writer behind the dispatching sync's read guard, | ||
| /// then calls the prefetch, as a host's completion handler would. | ||
| unsafe extern "C" fn prefetch_on_sync_completed( | ||
| _context: *mut std::ffi::c_void, | ||
| _results: *const ShieldedSyncWalletResultFFI, | ||
| _count: usize, | ||
| _sync_unix_seconds: u64, | ||
| ) { | ||
| let other = mock_manager(); | ||
| let writer = std::thread::spawn(move || PLATFORM_WALLET_MANAGER_STORAGE.insert(other)); | ||
| // Well inside the parent's budget, so a writer that never queued is | ||
| // reported as that rather than as a deadlock. | ||
| let deadline = std::time::Instant::now() + WAIT / 2; | ||
| while !PLATFORM_WALLET_MANAGER_STORAGE.try_read_refused() { | ||
| assert!( | ||
| std::time::Instant::now() < deadline, | ||
| "the writer never queued behind the sync's guard" | ||
| ); | ||
| std::thread::sleep(Duration::from_millis(5)); | ||
| } | ||
|
|
||
| let code = code_of(platform_wallet_manager_shielded_prefetch_spend_anchors( | ||
| SYNCING_HANDLE.load(Ordering::SeqCst), | ||
| )); | ||
| if let Some(report) = CALLBACK_REPORT.lock().unwrap().as_ref() { | ||
| report | ||
| .send((code, writer)) | ||
| .expect("test observes the callback"); | ||
| } | ||
| } | ||
|
|
||
| fn completion_callback_scenario() { | ||
| let (report_tx, report_rx) = mpsc::channel(); | ||
| *CALLBACK_REPORT.lock().unwrap() = Some(report_tx); | ||
| let handle = PLATFORM_WALLET_MANAGER_STORAGE.insert(mock_manager_with_events( | ||
| EventHandlerCallbacks { | ||
| context: std::ptr::null_mut(), | ||
| on_wallet_event_fn: None, | ||
| on_error_fn: None, | ||
| on_platform_address_sync_completed_fn: None, | ||
| on_shielded_sync_completed_fn: Some(prefetch_on_sync_completed), | ||
| on_shielded_sync_progress_fn: None, | ||
| on_shielded_tree_progress_fn: None, | ||
| release_fn: None, | ||
| }, | ||
| )); | ||
| SYNCING_HANDLE.store(handle, Ordering::SeqCst); | ||
|
|
||
| assert_eq!( | ||
| code_of(unsafe { platform_wallet_manager_shielded_sync_sync_now(handle) }), | ||
| PlatformWalletFFIResultCode::Success | ||
| ); | ||
| let (code, writer) = report_rx | ||
| .recv_timeout(WAIT) | ||
| .expect("the pass dispatched its completion callback"); | ||
| assert_eq!( | ||
| code, | ||
| PlatformWalletFFIResultCode::ErrorWalletOperation, | ||
| "the prefetch looked the manager up and found no shielded support" | ||
| ); | ||
| let other = writer.join().expect("writer thread"); | ||
|
|
||
| drop(PLATFORM_WALLET_MANAGER_STORAGE.remove(other)); | ||
| drop(PLATFORM_WALLET_MANAGER_STORAGE.remove(handle)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.