Skip to content

Commit ca1557c

Browse files
committed
feat(explorer): add reset of the certificate verifier
1 parent 1197393 commit ca1557c

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

mithril-client-wasm/src/client_wasm.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl From<MithrilEvent> for MithrilEventWasm {
6969
#[wasm_bindgen(getter_with_clone)]
7070
pub struct MithrilClient {
7171
client: Client,
72-
72+
certificate_verifier_cache: Option<Arc<dyn CertificateVerifierCache>>,
7373
unstable: bool,
7474
}
7575

@@ -99,20 +99,27 @@ impl MithrilClient {
9999
.add_feedback_receiver(feedback_receiver)
100100
.with_options(client_options);
101101

102-
if unstable && enable_certificate_chain_verification_cache {
103-
if let Some(cache) =
104-
Self::build_certifier_cache(aggregator_endpoint, TimeDelta::weeks(1))
105-
{
106-
client_builder = client_builder.with_certificate_verifier_cache(cache);
102+
let certificate_verifier_cache = if unstable && enable_certificate_chain_verification_cache
103+
{
104+
let cache = Self::build_certifier_cache(aggregator_endpoint, TimeDelta::weeks(1));
105+
if let Some(cache) = &cache {
106+
client_builder = client_builder.with_certificate_verifier_cache(cache.clone());
107107
}
108-
}
108+
cache
109+
} else {
110+
None
111+
};
109112

110113
let client = client_builder
111114
.build()
112115
.map_err(|err| format!("{err:?}"))
113116
.unwrap();
114117

115-
MithrilClient { client, unstable }
118+
MithrilClient {
119+
client,
120+
certificate_verifier_cache,
121+
unstable,
122+
}
116123
}
117124

118125
fn build_certifier_cache(
@@ -409,6 +416,18 @@ impl MithrilClient {
409416

410417
Ok(serde_wasm_bindgen::to_value(&result)?)
411418
}
419+
420+
/// `unstable` Reset the certificate verifier cache if enabled
421+
#[wasm_bindgen]
422+
pub async fn reset_certificate_verifier_cache(&self) -> Result<(), JsValue> {
423+
self.guard_unstable()?;
424+
425+
if let Some(cache) = self.certificate_verifier_cache.as_ref() {
426+
cache.reset().await.map_err(|err| format!("{err:?}"))?;
427+
}
428+
429+
Ok(())
430+
}
412431
}
413432

414433
allow_unstable_dead_code! {
@@ -444,8 +463,7 @@ mod tests {
444463
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
445464
const FAKE_AGGREGATOR_PORT: &str = "8000";
446465

447-
fn get_mithril_client(unstable: bool) -> MithrilClient {
448-
let options = ClientOptions::new(None).with_unstable_features(unstable);
466+
fn get_mithril_client(options: ClientOptions) -> MithrilClient {
449467
let options_js_value = serde_wasm_bindgen::to_value(&options).unwrap();
450468
MithrilClient::new(
451469
&format!(
@@ -458,7 +476,8 @@ mod tests {
458476
}
459477

460478
fn get_mithril_client_stable() -> MithrilClient {
461-
get_mithril_client(false)
479+
let options = ClientOptions::new(None).with_unstable_features(false);
480+
get_mithril_client(options)
462481
}
463482

464483
wasm_bindgen_test_configure!(run_in_browser);

mithril-explorer/src/components/VerifyCertificate/CertificateVerifier.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default function CertificateVerifier({
5252
certificate,
5353
hideSpinner = false,
5454
showCertificateLinks = false,
55+
certificateChainVerificationCacheEnabled = true,
5556
onStepChange = (step) => {},
5657
onChainValidationError = (error) => {},
5758
onCertificateClick = (hash) => {},
@@ -138,6 +139,10 @@ export default function CertificateVerifier({
138139
]);
139140
}
140141

142+
async function onCacheResetClick() {
143+
await client.reset_certificate_verifier_cache();
144+
}
145+
141146
return (
142147
<>
143148
{Object.entries(certificate).length > 0 && (
@@ -212,6 +217,15 @@ export default function CertificateVerifier({
212217
.map((evt) => (
213218
<div key={evt.id}>{evt.message}</div>
214219
))}
220+
{certificateChainVerificationCacheEnabled &&
221+
currentStep === certificateValidationSteps.done && (
222+
<>
223+
Cache enabled:{" "}
224+
<a href="#" onClick={onCacheResetClick}>
225+
reset cache
226+
</a>
227+
</>
228+
)}
215229
{validationError !== undefined && (
216230
<Alert variant="danger" className="mt-2">
217231
<Alert.Heading>

mithril-explorer/src/components/VerifyCertificate/VerifyCertificateModal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function VerifyCertificateModal({ show, onClose, certificateHash
1111
const [showLoadingWarning, setShowLoadingWarning] = useState(false);
1212
const [client, setClient] = useState(undefined);
1313
const [certificate, setCertificate] = useState(undefined);
14+
const enableCertificateChainVerificationCache = true;
1415

1516
useEffect(() => {
1617
if (show) {
@@ -32,7 +33,7 @@ export default function VerifyCertificateModal({ show, onClose, certificateHash
3233
const genesisVerificationKey = await fetchGenesisVerificationKey(aggregator);
3334
const client = new MithrilClient(aggregator, genesisVerificationKey, {
3435
unstable: true,
35-
enable_certificate_chain_verification_cache: true,
36+
enable_certificate_chain_verification_cache: enableCertificateChainVerificationCache,
3637
});
3738
const certificate = await client.get_mithril_certificate(certificateHash);
3839

@@ -72,6 +73,7 @@ export default function VerifyCertificateModal({ show, onClose, certificateHash
7273
<CertificateVerifier
7374
client={client}
7475
certificate={certificate}
76+
certificateChainVerificationCacheEnabled={enableCertificateChainVerificationCache}
7577
onStepChange={(step) =>
7678
setLoading(step === certificateValidationSteps.validationInProgress)
7779
}

0 commit comments

Comments
 (0)