Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/native-preimage-lookup-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@parity/truapi": patch
---

On native hosts, a preimage lookup that misses keeps calling the app's `lookup_preimage` until it returns the value, so a lookup whose first fetch failed still resolves without the app calling `notifyPreimageChanged`. Retries run every 6 seconds for the first 30 seconds, then every 30 seconds. They stop when the app returns the value or an error, or when the product unsubscribes. On every host, a key that is not 32 bytes gets a miss without the host being asked. Retries log `truapi.native.preimage.retrying` and `truapi.native.preimage.found`.
3 changes: 2 additions & 1 deletion rust/crates/truapi-codegen/tests/golden/host-callbacks.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/crates/truapi-codegen/tests/golden/wasm_bridge.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions rust/crates/truapi-host-cli/src/bulletin_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,8 @@ impl<S: BlobSource + 'static> BulletinLookup<S> {
/// then the value once it can be read, then the end.
pub fn subscribe(
self: &Arc<Self>,
key: Vec<u8>,
key: [u8; 32],
) -> BoxStream<'static, Result<Option<Vec<u8>>, api::GenericError>> {
let Ok(key) = <[u8; 32]>::try_from(key.as_slice()) else {
// Not a blake2b-256 digest, so nothing can ever hash to it.
return stream::once(future::ready(Ok(None))).boxed();
};
let lookup = Arc::clone(self);
async move {
let cid = preimage_cid(&key);
Expand Down Expand Up @@ -264,10 +260,10 @@ mod tests {
/// Every item of the subscription, errors by their reason.
async fn events(
lookup: &Arc<BulletinLookup<Scripted>>,
key: &[u8],
key: &[u8; 32],
) -> Vec<Result<Option<Vec<u8>>, String>> {
lookup
.subscribe(key.to_vec())
.subscribe(*key)
.map(|item| item.map_err(|error| error.reason))
.collect()
.await
Expand Down Expand Up @@ -330,12 +326,6 @@ mod tests {
assert_eq!(calls(&lookup), 2);
}

#[tokio::test]
async fn a_key_that_is_not_a_digest_is_a_miss_and_the_end() {
let lookup = scripted(vec![]);
assert_eq!(events(&lookup, &[9]).await, vec![Ok(None)]);
}

/// The bitswap spec makes only the code stable, and `Fail` covers data that is not held yet,
/// so every `Fail` is a miss to poll on, whatever its message or data.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/truapi-host-cli/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ impl LocaleHost for CliPlatform {
impl PreimageHost for CliPlatform {
fn lookup_preimage(
&self,
key: Vec<u8>,
key: [u8; 32],
) -> BoxStream<'static, Result<Option<Vec<u8>>, api::GenericError>> {
self.bulletin.subscribe(key)
}
Expand Down
3 changes: 2 additions & 1 deletion rust/crates/truapi/src/native/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ pub trait HostCallbacks: Send + Sync {
) -> Result<PermissionDecision, HostRejection>;

/// Look up one preimage value by key. The native shim emits this as the
/// current item in its subscription stream.
/// current item in its subscription stream and calls again after a miss.
/// Return `None` if the fetch fails. An error ends the lookup.
async fn lookup_preimage(&self, key: Vec<u8>) -> Result<Option<Vec<u8>>, HostRejection>;

/// Current host theme, named variant included. The native shim emits this
Expand Down
74 changes: 68 additions & 6 deletions rust/crates/truapi/src/native/platform.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::time::Duration;
use std::sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
Expand All @@ -10,14 +11,17 @@ use crate::platform::{
ThemeHost, UserConfirmation, UserConfirmationReview, async_trait,
};
use futures::{
FutureExt,
channel::mpsc,
future,
stream::{self, BoxStream, StreamExt},
};
use parity_scale_codec::Encode;
use truapi::v01;

use crate::{
DevicePairingObserver, PairedSsoPeer,
host_internal::bulletin::preimage_key,
host_logic::worker::WorkerTransition,
installation::{InstallationId, InstallationIdProvider},
};
Expand Down Expand Up @@ -405,7 +409,7 @@ impl Drop for NativeJsonRpcConnection {
}
}

/// Lowercase hex for a genesis hash in a log line.
/// Lowercase hex for a hash in a log line.
fn hex_lower(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
Expand Down Expand Up @@ -500,18 +504,76 @@ impl LocaleHost for CallbackPlatform {
}
}

// Pauses between retries of a preimage lookup that missed. The first retries
// use the CLI host's poll interval and cover the 30 seconds the product SDK's
// lookup helpers wait. Later retries are slower, to limit requests for lookups
// a product keeps open.
#[cfg(not(test))]
const PREIMAGE_RETRY_PAUSE_FAST: Duration = Duration::from_secs(6);
#[cfg(not(test))]
const PREIMAGE_RETRY_PAUSE_SLOW: Duration = Duration::from_secs(30);
#[cfg(test)]
const PREIMAGE_RETRY_PAUSE_FAST: Duration = Duration::from_millis(1);
#[cfg(test)]
const PREIMAGE_RETRY_PAUSE_SLOW: Duration = Duration::from_millis(4);
const PREIMAGE_FAST_RETRIES: u32 = 5;

impl PreimageHost for CallbackPlatform {
fn lookup_preimage(
&self,
key: Vec<u8>,
key: [u8; 32],
) -> BoxStream<'static, Result<Option<Vec<u8>>, v01::GenericError>> {
// Register the change receiver first so no event between the lookup
// and the subscription is lost, then await the current value lazily.
let rx = self.events.subscribe_preimage_changes(key.clone());
let rx = self.events.subscribe_preimage_changes(key.to_vec());
let callbacks = self.callbacks.clone();
let current =
async move { callbacks.lookup_preimage(key).await.map_err(v01::GenericError::from) };
stream::once(current).chain(rx).boxed()
async move {
let current = callbacks.lookup_preimage(key.to_vec()).await;
// A miss can mean the fetch failed, and hosts are not required to
// notify the value later, so retry until the bytes hash to the key.
// An error is final: apps answer a failed fetch with `None`, and
// `HostRejection` has no case for a failure worth retrying.
let retry = match &current {
Ok(Some(value)) => preimage_key(value) != key,
Ok(None) => true,
Err(HostRejection::Rejected { .. }) => false,
};
let found = stream::iter(retry.then_some(key))
.then(move |key| lookup_until_found(callbacks.clone(), key))
.map(|found| found.map(Some).map_err(v01::GenericError::from));
let current = current.map_err(v01::GenericError::from);
stream::once(future::ready(current)).chain(stream::select(rx, found))
}
.flatten_stream()
.boxed()
}
}

/// Retry the host lookup until it returns bytes that hash to `digest` or an
/// error.
async fn lookup_until_found(
callbacks: Arc<dyn HostCallbacks>,
digest: [u8; 32],
) -> Result<Vec<u8>, HostRejection> {
let key_hex = hex_lower(&digest);
callbacks.on_core_log("truapi.native.preimage.retrying".to_string(), key_hex.clone());
let mut retries = 0u32;
loop {
let pause = if retries < PREIMAGE_FAST_RETRIES {
PREIMAGE_RETRY_PAUSE_FAST
} else {
PREIMAGE_RETRY_PAUSE_SLOW
};
Comment thread
Mikelle marked this conversation as resolved.
Resolved
futures_timer::Delay::new(pause).await;
retries += 1;
let value = callbacks.lookup_preimage(digest.to_vec()).await?;
if let Some(value) = value.filter(|bytes| preimage_key(bytes) == digest) {
callbacks.on_core_log(
"truapi.native.preimage.found".to_string(),
format!("{key_hex} on retry {retries}"),
);
return Ok(value);
}
}
}

Expand Down
Loading
Loading