Skip to content

Commit

Permalink
feat(client-lib+client-wasm): certificate verifier cache duration can…
Browse files Browse the repository at this point in the history
… now be set at the client level

With a default duration of one week
  • Loading branch information
Alenar committed Dec 10, 2024
1 parent 0775a9e commit eac7a7f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 10 additions & 7 deletions mithril-client-wasm/src/client_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ impl MithrilClient {
.map_err(|err| format!("Failed to parse options: {err:?}"))
.unwrap()
};
let unstable = client_options.unstable;
let enable_certificate_chain_verification_cache =
client_options.enable_certificate_chain_verification_cache;
let mut client_builder =
ClientBuilder::aggregator(aggregator_endpoint, genesis_verification_key)
.add_feedback_receiver(feedback_receiver)
.with_options(client_options);
.with_options(client_options.clone());

let certificate_verifier_cache = if unstable && enable_certificate_chain_verification_cache
let certificate_verifier_cache = if client_options.unstable
&& client_options.enable_certificate_chain_verification_cache
{
let cache = Self::build_certifier_cache(aggregator_endpoint, TimeDelta::weeks(1));
let cache = Self::build_certifier_cache(
aggregator_endpoint,
TimeDelta::seconds(
client_options.certificate_chain_verification_cache_duration_in_seconds as i64,
),
);
if let Some(cache) = &cache {
client_builder = client_builder.with_certificate_verifier_cache(cache.clone());
}
Expand All @@ -118,7 +121,7 @@ impl MithrilClient {
MithrilClient {
client,
certificate_verifier_cache,
unstable,
unstable: client_options.unstable,
}
}

Expand Down
23 changes: 22 additions & 1 deletion mithril-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ use crate::snapshot_client::SnapshotClient;
use crate::snapshot_downloader::{HttpSnapshotDownloader, SnapshotDownloader};
use crate::MithrilResult;

#[cfg(target_family = "wasm")]
const fn certificate_chain_verification_cache_duration_in_seconds_default() -> u32 {
604800
}

/// Options that can be used to configure the client.
#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ClientOptions {
/// HTTP headers to include in the client requests.
pub http_headers: Option<HashMap<String, String>>,
Expand All @@ -42,6 +47,19 @@ pub struct ClientOptions {
#[cfg(target_family = "wasm")]
#[cfg_attr(target_family = "wasm", serde(default))]
pub enable_certificate_chain_verification_cache: bool,

/// Duration in seconds of certificate chain verification cache in the WASM client.
///
/// Default to one week (604800 seconds).
///
/// `enable_certificate_chain_verification_cache` and `unstable` must both be set to `true`
/// for this option to have any effect.
#[cfg(target_family = "wasm")]
#[cfg_attr(
target_family = "wasm",
serde(default = "certificate_chain_verification_cache_duration_in_seconds_default")
)]
pub certificate_chain_verification_cache_duration_in_seconds: u32,
}

impl ClientOptions {
Expand All @@ -53,6 +71,9 @@ impl ClientOptions {
unstable: false,
#[cfg(target_family = "wasm")]
enable_certificate_chain_verification_cache: false,
#[cfg(target_family = "wasm")]
certificate_chain_verification_cache_duration_in_seconds:
certificate_chain_verification_cache_duration_in_seconds_default(),
}
}

Expand Down

0 comments on commit eac7a7f

Please sign in to comment.