Skip to content

Commit 521d5da

Browse files
committed
feat(client-lib+client-wasm): certificate verifier cache duration can now be set at the client level
With a default duration of one week
1 parent 0775a9e commit 521d5da

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

mithril-client-wasm/src/client_wasm.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ impl MithrilClient {
9191
.map_err(|err| format!("Failed to parse options: {err:?}"))
9292
.unwrap()
9393
};
94-
let unstable = client_options.unstable;
95-
let enable_certificate_chain_verification_cache =
96-
client_options.enable_certificate_chain_verification_cache;
9794
let mut client_builder =
9895
ClientBuilder::aggregator(aggregator_endpoint, genesis_verification_key)
9996
.add_feedback_receiver(feedback_receiver)
100-
.with_options(client_options);
97+
.with_options(client_options.clone());
10198

102-
let certificate_verifier_cache = if unstable && enable_certificate_chain_verification_cache
99+
let certificate_verifier_cache = if client_options.unstable
100+
&& client_options.enable_certificate_chain_verification_cache
103101
{
104-
let cache = Self::build_certifier_cache(aggregator_endpoint, TimeDelta::weeks(1));
102+
let cache = Self::build_certifier_cache(
103+
aggregator_endpoint,
104+
TimeDelta::seconds(
105+
client_options.certificate_chain_verification_cache_duration_in_seconds as i64,
106+
),
107+
);
105108
if let Some(cache) = &cache {
106109
client_builder = client_builder.with_certificate_verifier_cache(cache.clone());
107110
}
@@ -118,7 +121,7 @@ impl MithrilClient {
118121
MithrilClient {
119122
client,
120123
certificate_verifier_cache,
121-
unstable,
124+
unstable: client_options.unstable,
122125
}
123126
}
124127

@@ -461,7 +464,7 @@ mod tests {
461464

462465
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
463466
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
464-
const FAKE_AGGREGATOR_PORT: &str = "8000";
467+
const FAKE_AGGREGATOR_PORT: &str = "8001";
465468

466469
fn get_mithril_client(options: ClientOptions) -> MithrilClient {
467470
let options_js_value = serde_wasm_bindgen::to_value(&options).unwrap();

mithril-client/src/client.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ use crate::snapshot_client::SnapshotClient;
2222
use crate::snapshot_downloader::{HttpSnapshotDownloader, SnapshotDownloader};
2323
use crate::MithrilResult;
2424

25+
#[cfg(target_family = "wasm")]
26+
const fn certificate_chain_verification_cache_duration_in_seconds_default() -> u32 {
27+
604800
28+
}
29+
2530
/// Options that can be used to configure the client.
26-
#[derive(Debug, Default, Serialize, Deserialize)]
31+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2732
pub struct ClientOptions {
2833
/// HTTP headers to include in the client requests.
2934
pub http_headers: Option<HashMap<String, String>>,
@@ -42,6 +47,19 @@ pub struct ClientOptions {
4247
#[cfg(target_family = "wasm")]
4348
#[cfg_attr(target_family = "wasm", serde(default))]
4449
pub enable_certificate_chain_verification_cache: bool,
50+
51+
/// Duration in seconds of certificate chain verification cache in the WASM client.
52+
///
53+
/// Default to one week (604800 seconds).
54+
///
55+
/// `enable_certificate_chain_verification_cache` and `unstable` must both be set to `true`
56+
/// for this option to have any effect.
57+
#[cfg(target_family = "wasm")]
58+
#[cfg_attr(
59+
target_family = "wasm",
60+
serde(default = "certificate_chain_verification_cache_duration_in_seconds_default")
61+
)]
62+
pub certificate_chain_verification_cache_duration_in_seconds: u32,
4563
}
4664

4765
impl ClientOptions {
@@ -53,6 +71,9 @@ impl ClientOptions {
5371
unstable: false,
5472
#[cfg(target_family = "wasm")]
5573
enable_certificate_chain_verification_cache: false,
74+
#[cfg(target_family = "wasm")]
75+
certificate_chain_verification_cache_duration_in_seconds:
76+
certificate_chain_verification_cache_duration_in_seconds_default(),
5677
}
5778
}
5879

0 commit comments

Comments
 (0)