1use crate::{host::PrecheckResultSender, worker_interface::WORKER_DIR_PREFIX};
58use always_assert::always;
59use polkadot_node_core_pvf_common::{error::PrepareError, pvf::PvfPrepData, ArtifactChecksum};
60use polkadot_parachain_primitives::primitives::ValidationCodeHash;
61use polkadot_primitives::ExecutorParamsPrepHash;
62use std::{
63 collections::HashMap,
64 fs,
65 path::{Path, PathBuf},
66 time::{Duration, SystemTime},
67};
68
69const ARTIFACT_EXTENSION: &str = "pvf";
71
72const ARTIFACT_OLD_PREFIX: &str = "wasmtime_";
74
75pub fn generate_artifact_path(cache_path: &Path) -> PathBuf {
76 let file_name = {
77 use array_bytes::Hex;
78 use rand::RngCore;
79 let mut bytes = [0u8; 64];
80 rand::thread_rng().fill_bytes(&mut bytes);
81 bytes.hex("0x")
82 };
83 let mut artifact_path = cache_path.join(file_name);
84 artifact_path.set_extension(ARTIFACT_EXTENSION);
85 artifact_path
86}
87
88#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
91pub struct ArtifactId {
92 pub(crate) code_hash: ValidationCodeHash,
93 pub(crate) executor_params_prep_hash: ExecutorParamsPrepHash,
94}
95
96impl ArtifactId {
97 pub fn new(
99 code_hash: ValidationCodeHash,
100 executor_params_prep_hash: ExecutorParamsPrepHash,
101 ) -> Self {
102 Self { code_hash, executor_params_prep_hash }
103 }
104
105 pub fn from_pvf_prep_data(pvf: &PvfPrepData) -> Self {
108 Self::new(pvf.code_hash(), pvf.executor_params().prep_hash())
109 }
110}
111
112#[derive(Debug, Clone)]
120pub struct ArtifactPathId {
121 pub(crate) id: ArtifactId,
122 pub(crate) path: PathBuf,
123 pub(crate) checksum: ArtifactChecksum,
124}
125
126impl ArtifactPathId {
127 pub(crate) fn new(artifact_id: ArtifactId, path: &Path, checksum: ArtifactChecksum) -> Self {
128 Self { id: artifact_id, path: path.to_owned(), checksum }
129 }
130}
131
132#[derive(Debug)]
133pub enum ArtifactState {
134 Prepared {
139 checksum: ArtifactChecksum,
141 path: PathBuf,
143 last_time_needed: SystemTime,
148 size: u64,
150 },
151 Preparing {
153 waiting_for_response: Vec<PrecheckResultSender>,
155 num_failures: u32,
157 },
158 FailedToProcess {
161 last_time_failed: SystemTime,
163 num_failures: u32,
165 error: PrepareError,
167 },
168}
169
170pub struct Artifacts {
172 inner: HashMap<ArtifactId, ArtifactState>,
173}
174
175#[derive(Debug)]
179pub struct ArtifactsCleanupConfig {
180 cache_limit: u64,
182 min_stale_time: Duration,
184}
185
186impl Default for ArtifactsCleanupConfig {
187 fn default() -> Self {
188 Self {
189 cache_limit: 10 * 1024 * 1024 * 1024, min_stale_time: Duration::from_secs(24 * 60 * 60), }
192 }
193}
194
195#[cfg(test)]
196impl ArtifactsCleanupConfig {
197 pub fn new(cache_limit: u64, min_stale_time: Duration) -> Self {
198 Self { cache_limit, min_stale_time }
199 }
200}
201
202impl Artifacts {
203 #[cfg(test)]
204 pub(crate) fn empty() -> Self {
205 Self { inner: HashMap::new() }
206 }
207
208 #[cfg(test)]
209 fn len(&self) -> usize {
210 self.inner.len()
211 }
212
213 #[cfg(test)]
214 fn artifact_ids(&self) -> Vec<ArtifactId> {
215 self.inner.keys().cloned().collect()
216 }
217
218 #[cfg(feature = "test-utils")]
219 pub fn replace_artifact_checksum(
220 &mut self,
221 checksum: ArtifactChecksum,
222 new_checksum: ArtifactChecksum,
223 ) {
224 for artifact in self.inner.values_mut() {
225 if let ArtifactState::Prepared { checksum: c, .. } = artifact {
226 if *c == checksum {
227 *c = new_checksum;
228 }
229 }
230 }
231 }
232
233 pub async fn new(cache_path: &Path) -> Self {
235 let _ = tokio::fs::create_dir_all(cache_path).await;
237
238 for entry in fs::read_dir(cache_path).into_iter().flatten().flatten() {
242 let path = entry.path();
243 let Some(file_name) = path.file_name().and_then(|f| f.to_str()) else { continue };
244 if path.is_dir() && file_name.starts_with(WORKER_DIR_PREFIX) {
245 let _ = fs::remove_dir_all(path);
246 } else if path.extension().map_or(false, |ext| ext == ARTIFACT_EXTENSION) ||
247 file_name.starts_with(ARTIFACT_OLD_PREFIX)
248 {
249 let _ = fs::remove_file(path);
250 }
251 }
252
253 Self { inner: HashMap::new() }
254 }
255
256 pub fn artifact_state_mut(&mut self, artifact_id: &ArtifactId) -> Option<&mut ArtifactState> {
258 self.inner.get_mut(artifact_id)
259 }
260
261 pub fn insert_preparing(
266 &mut self,
267 artifact_id: ArtifactId,
268 waiting_for_response: Vec<PrecheckResultSender>,
269 ) {
270 always!(self
272 .inner
273 .insert(artifact_id, ArtifactState::Preparing { waiting_for_response, num_failures: 0 })
274 .is_none());
275 }
276
277 #[cfg(test)]
282 pub(crate) fn insert_prepared(
283 &mut self,
284 artifact_id: ArtifactId,
285 path: PathBuf,
286 checksum: ArtifactChecksum,
287 last_time_needed: SystemTime,
288 size: u64,
289 ) {
290 always!(self
292 .inner
293 .insert(artifact_id, ArtifactState::Prepared { path, checksum, last_time_needed, size })
294 .is_none());
295 }
296
297 pub fn remove(&mut self, artifact_id: ArtifactId) -> Option<(ArtifactId, PathBuf)> {
299 self.inner.remove(&artifact_id).and_then(|state| match state {
300 ArtifactState::Prepared { path, .. } => Some((artifact_id, path)),
301 _ => None,
302 })
303 }
304
305 pub fn prune(&mut self, cleanup_config: &ArtifactsCleanupConfig) -> Vec<(ArtifactId, PathBuf)> {
308 let mut to_remove = vec![];
309 let now = SystemTime::now();
310
311 let mut total_size = 0;
312 let mut artifact_sizes = vec![];
313
314 for (k, v) in self.inner.iter() {
315 if let ArtifactState::Prepared { ref path, last_time_needed, size, .. } = *v {
316 total_size += size;
317 artifact_sizes.push((k.clone(), path.clone(), size, last_time_needed));
318 }
319 }
320 artifact_sizes
321 .sort_by_key(|&(_, _, _, last_time_needed)| std::cmp::Reverse(last_time_needed));
322
323 while total_size > cleanup_config.cache_limit {
324 let Some((artifact_id, path, size, last_time_needed)) = artifact_sizes.pop() else {
325 break
326 };
327
328 let used_recently = now
329 .duration_since(last_time_needed)
330 .map(|stale_time| stale_time < cleanup_config.min_stale_time)
331 .unwrap_or(true);
332 if used_recently {
333 break;
334 }
335
336 self.inner.remove(&artifact_id);
337 to_remove.push((artifact_id, path));
338 total_size -= size;
339 }
340
341 to_remove
342 }
343}
344
345#[cfg(test)]
346mod tests {
347 use crate::testing::artifact_id;
348
349 use super::*;
350
351 #[tokio::test]
352 async fn cache_cleared_on_startup() {
353 let tempdir = tempfile::tempdir().unwrap();
354 let cache_path = tempdir.path();
355
356 fs::write(cache_path.join("abcd.pvf"), "test").unwrap();
358 fs::write(cache_path.join("wasmtime_..."), "test").unwrap();
359 fs::create_dir(cache_path.join("worker-dir-prepare-test")).unwrap();
360
361 fs::write(cache_path.join("abcd.pvfartifact"), "test").unwrap();
363 fs::write(cache_path.join("polkadot_..."), "test").unwrap();
364 fs::create_dir(cache_path.join("worker-prepare-test")).unwrap();
365
366 let artifacts = Artifacts::new(cache_path).await;
367
368 let entries: Vec<String> = fs::read_dir(&cache_path)
369 .unwrap()
370 .map(|entry| entry.unwrap().file_name().into_string().unwrap())
371 .collect();
372 assert_eq!(entries.len(), 3);
373 assert!(entries.contains(&String::from("abcd.pvfartifact")));
374 assert!(entries.contains(&String::from("polkadot_...")));
375 assert!(entries.contains(&String::from("worker-prepare-test")));
376 assert_eq!(artifacts.len(), 0);
377 }
378
379 #[tokio::test]
380 async fn test_pruned_by_cache_size() {
381 let mock_now = SystemTime::now();
382 let tempdir = tempfile::tempdir().unwrap();
383 let cache_path = tempdir.path();
384
385 let path1 = generate_artifact_path(cache_path);
386 let path2 = generate_artifact_path(cache_path);
387 let path3 = generate_artifact_path(cache_path);
388 let artifact_id1 = artifact_id(1);
389 let artifact_id2 = artifact_id(2);
390 let artifact_id3 = artifact_id(3);
391
392 let mut artifacts = Artifacts::new(cache_path).await;
393 let cleanup_config = ArtifactsCleanupConfig::new(1500, Duration::from_secs(0));
394
395 artifacts.insert_prepared(
396 artifact_id1.clone(),
397 path1.clone(),
398 Default::default(),
399 mock_now - Duration::from_secs(5),
400 1024,
401 );
402 artifacts.insert_prepared(
403 artifact_id2.clone(),
404 path2.clone(),
405 Default::default(),
406 mock_now - Duration::from_secs(10),
407 1024,
408 );
409 artifacts.insert_prepared(
410 artifact_id3.clone(),
411 path3.clone(),
412 Default::default(),
413 mock_now - Duration::from_secs(15),
414 1024,
415 );
416
417 let pruned = artifacts.prune(&cleanup_config);
418
419 assert!(artifacts.artifact_ids().contains(&artifact_id1));
420 assert!(!pruned.contains(&(artifact_id1, path1)));
421 assert!(!artifacts.artifact_ids().contains(&artifact_id2));
422 assert!(pruned.contains(&(artifact_id2, path2)));
423 assert!(!artifacts.artifact_ids().contains(&artifact_id3));
424 assert!(pruned.contains(&(artifact_id3, path3)));
425 }
426
427 #[tokio::test]
428 async fn test_did_not_prune_by_cache_size_because_of_stale_time() {
429 let mock_now = SystemTime::now();
430 let tempdir = tempfile::tempdir().unwrap();
431 let cache_path = tempdir.path();
432
433 let path1 = generate_artifact_path(cache_path);
434 let path2 = generate_artifact_path(cache_path);
435 let path3 = generate_artifact_path(cache_path);
436 let artifact_id1 = artifact_id(1);
437 let artifact_id2 = artifact_id(2);
438 let artifact_id3 = artifact_id(3);
439
440 let mut artifacts = Artifacts::new(cache_path).await;
441 let cleanup_config = ArtifactsCleanupConfig::new(1500, Duration::from_secs(12));
442
443 artifacts.insert_prepared(
444 artifact_id1.clone(),
445 path1.clone(),
446 Default::default(),
447 mock_now - Duration::from_secs(5),
448 1024,
449 );
450 artifacts.insert_prepared(
451 artifact_id2.clone(),
452 path2.clone(),
453 Default::default(),
454 mock_now - Duration::from_secs(10),
455 1024,
456 );
457 artifacts.insert_prepared(
458 artifact_id3.clone(),
459 path3.clone(),
460 Default::default(),
461 mock_now - Duration::from_secs(15),
462 1024,
463 );
464
465 let pruned = artifacts.prune(&cleanup_config);
466
467 assert!(artifacts.artifact_ids().contains(&artifact_id1));
468 assert!(!pruned.contains(&(artifact_id1, path1)));
469 assert!(artifacts.artifact_ids().contains(&artifact_id2));
470 assert!(!pruned.contains(&(artifact_id2, path2)));
471 assert!(!artifacts.artifact_ids().contains(&artifact_id3));
472 assert!(pruned.contains(&(artifact_id3, path3)));
473 }
474}