namada_vm 0.48.3

The Namada VM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! WASM compilation cache.
//!
//! The cache is backed by in-memory LRU cache with configurable size
//! limit and a file system cache of serialized modules.

use std::collections::hash_map::RandomState;
use std::fs;
use std::marker::PhantomData;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::thread::sleep;
use std::time::Duration;

use clru::{CLruCache, CLruCacheConfig, WeightScale};
use namada_core::collections::HashMap;
use namada_core::control_flow::time::{ExponentialBackoff, SleepStrategy};
use namada_core::hash::Hash;
use wasmer::{Module, Store};
use wasmer_cache::{FileSystemCache, Hash as CacheHash};

use crate::wasm::run::untrusted_wasm_store;
use crate::wasm::{self, memory};
use crate::{WasmCacheAccess, WasmCacheRoAccess};

/// Cache handle. Thread-safe.
#[derive(Debug, Clone)]
pub struct Cache<N, A> {
    /// Cached files directory
    dir: PathBuf,
    /// Compilation progress
    progress: Arc<RwLock<HashMap<Hash, Compilation>>>,
    /// In-memory LRU cache of compiled modules
    in_memory: Arc<RwLock<MemoryCache>>,
    /// The cache's name
    name: PhantomData<N>,
    /// Cache access level
    access: PhantomData<A>,
    /// Wasmer store - The store that's used to compile the modules.
    // N.B.: This has to be kept alive to avoid segfaults when running them
    // from cache (otherwise `wasmer_compiler::FRAME_INFO` gets cleared out
    // when the `Store` is dropped and the next run of this `Module`
    // doesn't re-instantiate it and crashes when it tries to access it).
    // Related issues:
    // - https://github.com/wasmerio/wasmer/issues/4377
    // - https://github.com/wasmerio/wasmer/issues/4949
    store: Arc<Store>,
}

/// This trait is used to give names to different caches
pub trait CacheName: Clone + std::fmt::Debug {
    /// Get the name of the cache
    fn name() -> &'static str;
}

/// In-memory LRU cache of compiled modules
type MemoryCache = CLruCache<Hash, Module, RandomState, ModuleCacheScale>;

/// Compilation progress
#[derive(Debug)]
enum Compilation {
    Compiling,
    Done,
}

/// Configures the cache scale of modules that limits the maximum capacity
/// of the cache (CLruCache::len + CLruCache::weight <= CLruCache::capacity).
#[derive(Debug)]
struct ModuleCacheScale;

impl WeightScale<Hash, Module> for ModuleCacheScale {
    fn weight(&self, _key: &Hash, _value: &Module) -> usize {
        1
    }
}

impl<N: CacheName, A: WasmCacheAccess> Cache<N, A> {
    /// Create a wasm in-memory cache with a given size limit and a file
    /// system cache.
    ///
    /// # Panics
    /// The `max_bytes` must be non-zero.
    pub fn new(dir: impl Into<PathBuf>, max_bytes: usize) -> Self {
        let cache = CLruCache::with_config(
            CLruCacheConfig::new(NonZeroUsize::new(max_bytes).unwrap())
                .with_scale(ModuleCacheScale),
        );
        let in_memory = Arc::new(RwLock::new(cache));

        let target_hash = {
            use std::hash::{Hash, Hasher};
            let mut hasher = std::hash::DefaultHasher::new();
            wasmer::Target::default().hash(&mut hasher);
            hasher.finish()
        };
        let version = format!(
            "{}_{:x}",
            concat!(env!("CARGO_PKG_VERSION"), "_", env!("RUSTUP_TOOLCHAIN")),
            target_hash,
        );
        let dir = dir.into().join(version);

        fs::create_dir_all(&dir)
            .expect("Couldn't create the wasm cache directory");

        Self {
            dir,
            progress: Default::default(),
            in_memory,
            name: Default::default(),
            access: Default::default(),
            store: Arc::new(store()),
        }
    }

    /// Get a WASM module from LRU cache, from a file or compile it and cache
    /// it. If the cache access is set to [`crate::WasmCacheRwAccess`], it
    /// updates the position in the LRU cache. Otherwise, the compiled
    /// module will not be cached, if it's not already.
    pub fn fetch(
        &mut self,
        code_hash: &Hash,
    ) -> Result<Option<(Module, Store)>, wasm::run::Error> {
        if A::is_read_write() {
            let module = self.get(code_hash)?;
            Ok(module.map(|module| (module, store())))
        } else {
            let store = store();
            let module = self.peek(code_hash, &store)?;
            Ok(module.map(|module| (module, store)))
        }
    }

    /// Get the current number of items in the cache
    pub fn get_size(&self) -> usize {
        self.in_memory.read().unwrap().len()
    }

    /// Get the current weight of the cache
    pub fn get_cache_size(&self) -> usize {
        self.in_memory.read().unwrap().weight()
    }

    /// Get a WASM module from LRU cache, from a file or compile it and cache
    /// it. Updates the position in the LRU cache.
    fn get(&mut self, hash: &Hash) -> Result<Option<Module>, wasm::run::Error> {
        let mut in_memory = self.in_memory.write().unwrap();
        if let Some(module) = in_memory.get(hash) {
            tracing::trace!(
                "{} found {} in cache.",
                N::name(),
                hash.to_string()
            );
            return Ok(Some(module.clone()));
        }
        drop(in_memory);

        let mut iter = 0;
        let exponential_backoff = ExponentialBackoff {
            base: 2,
            as_duration: |backoff: u64| {
                Duration::from_millis(backoff.saturating_mul(10))
            },
        };
        loop {
            let progress = self.progress.read().unwrap();
            match progress.get(hash) {
                Some(Compilation::Done) => {
                    drop(progress);
                    let mut in_memory = self.in_memory.write().unwrap();
                    if let Some(module) = in_memory.get(hash) {
                        tracing::info!(
                            "{} found {} in memory cache.",
                            N::name(),
                            hash.to_string()
                        );
                        return Ok(Some(module.clone()));
                    }

                    if let Ok(module) =
                        file_load_module(&self.dir, hash, &self.store)
                    {
                        tracing::info!(
                            "{} found {} in file cache.",
                            N::name(),
                            hash.to_string()
                        );
                        // Put into cache, ignore result if it's full
                        let _ =
                            in_memory.put_with_weight(*hash, module.clone());

                        return Ok(Some(module));
                    } else {
                        return Ok(None);
                    }
                }
                Some(Compilation::Compiling) => {
                    drop(progress);
                    tracing::info!(
                        "Waiting for {} {} ...",
                        N::name(),
                        hash.to_string()
                    );
                    sleep(exponential_backoff.backoff(&iter));
                    // Cannot overflow
                    #[allow(clippy::arithmetic_side_effects)]
                    {
                        iter += 1;
                    }
                    continue;
                }
                None => {
                    drop(progress);
                    let module = if module_file_exists(&self.dir, hash) {
                        tracing::info!(
                            "Trying to load {} {} from file.",
                            N::name(),
                            hash.to_string()
                        );
                        if let Ok(module) =
                            file_load_module(&self.dir, hash, &self.store)
                        {
                            module
                        } else {
                            return Ok(None);
                        }
                    } else {
                        return Ok(None);
                    };

                    // Update progress
                    let mut progress = self.progress.write().unwrap();
                    progress.insert(*hash, Compilation::Done);

                    // Put into cache, ignore the result (fails if the module
                    // cannot fit into the cache)
                    let mut in_memory = self.in_memory.write().unwrap();
                    let _ = in_memory.put_with_weight(*hash, module.clone());

                    return Ok(Some(module));
                }
            }
        }
    }

    /// Peak-only is used for dry-ran txs (and VPs that the tx triggers).
    /// It doesn't update the in-memory cache.
    fn peek(
        &self,
        hash: &Hash,
        store: &Store,
    ) -> Result<Option<Module>, wasm::run::Error> {
        let in_memory = self.in_memory.read().unwrap();
        if let Some(module) = in_memory.peek(hash) {
            tracing::info!(
                "{} found {} in cache.",
                N::name(),
                hash.to_string()
            );
            return Ok(Some(module.clone()));
        }
        drop(in_memory);

        let mut iter = 0;
        let exponential_backoff = ExponentialBackoff {
            base: 2,
            as_duration: |backoff: u64| {
                Duration::from_millis(backoff.saturating_mul(10))
            },
        };
        loop {
            let progress = self.progress.read().unwrap();
            match progress.get(hash) {
                Some(Compilation::Done) => {
                    drop(progress);
                    let in_memory = self.in_memory.read().unwrap();
                    if let Some(module) = in_memory.peek(hash) {
                        tracing::info!(
                            "{} found {} in memory cache.",
                            N::name(),
                            hash.to_string()
                        );
                        return Ok(Some(module.clone()));
                    }

                    if let Ok(module) = file_load_module(&self.dir, hash, store)
                    {
                        tracing::info!(
                            "{} found {} in file cache.",
                            N::name(),
                            hash.to_string()
                        );
                        return Ok(Some(module));
                    } else {
                        return Ok(None);
                    }
                }
                Some(Compilation::Compiling) => {
                    drop(progress);
                    tracing::info!(
                        "Waiting for {} {} ...",
                        N::name(),
                        hash.to_string()
                    );
                    sleep(exponential_backoff.backoff(&iter));
                    // Cannot overflow
                    #[allow(clippy::arithmetic_side_effects)]
                    {
                        iter += 1;
                    }
                    continue;
                }
                None => {
                    drop(progress);

                    return if module_file_exists(&self.dir, hash) {
                        tracing::info!(
                            "Trying to load {} {} from file.",
                            N::name(),
                            hash.to_string()
                        );
                        if let Ok(module) =
                            file_load_module(&self.dir, hash, store)
                        {
                            return Ok(Some(module));
                        } else {
                            return Ok(None);
                        }
                    } else {
                        Ok(None)
                    };
                }
            }
        }
    }

    /// Compile a WASM module and persist the compiled modules to files.
    pub fn compile_or_fetch(
        &mut self,
        code: impl AsRef<[u8]>,
    ) -> Result<Option<(Module, Store)>, wasm::run::Error> {
        let hash = hash_of_code(&code);

        if !A::is_read_write() {
            // It doesn't update the cache and files
            let progress = self.progress.read().unwrap();
            match progress.get(&hash) {
                Some(_) => {
                    let store = store();
                    let module = self.peek(&hash, &store)?;
                    return Ok(module.map(|module| (module, store)));
                }
                None => {
                    let code = wasm::run::prepare_wasm_code(code)?;
                    let store = store();
                    let module = compile(code, &store)?;
                    return Ok(Some((module, store)));
                }
            }
        }

        let mut progress = self.progress.write().unwrap();
        if progress.get(&hash).is_some() {
            drop(progress);
            return self.fetch(&hash);
        }
        progress.insert(hash, Compilation::Compiling);
        drop(progress);

        tracing::info!("Compiling {} {}.", N::name(), hash.to_string());

        match wasm::run::prepare_wasm_code(code) {
            Ok(code) => match compile(code, &self.store) {
                Ok(module) => {
                    // Write the file
                    file_write_module(&self.dir, &module, &hash);

                    // Update progress
                    let mut progress = self.progress.write().unwrap();
                    progress.insert(hash, Compilation::Done);

                    // Put into cache, ignore result if it's full
                    let mut in_memory = self.in_memory.write().unwrap();
                    let _ = in_memory.put_with_weight(hash, module.clone());

                    Ok(Some((module, store())))
                }
                Err(err) => {
                    tracing::info!(
                        "Failed to compile WASM {} with {}",
                        hash.to_string(),
                        err
                    );
                    let mut progress = self.progress.write().unwrap();
                    progress.swap_remove(&hash);
                    Err(err)
                }
            },
            Err(err) => {
                tracing::info!(
                    "Failed to prepare WASM {} with {}",
                    hash.to_string(),
                    err
                );
                let mut progress = self.progress.write().unwrap();
                progress.swap_remove(&hash);
                Err(err)
            }
        }
    }

    /// Pre-compile a WASM module to a file. The compilation runs in a new OS
    /// thread and the function returns immediately.
    pub fn pre_compile(&mut self, code: impl AsRef<[u8]>) {
        if A::is_read_write() {
            let hash = hash_of_code(&code);
            let mut progress = self.progress.write().unwrap();
            match progress.get(&hash) {
                Some(_) => {
                    // Already known, do nothing
                }
                None => {
                    if module_file_exists(&self.dir, &hash) {
                        progress.insert(hash, Compilation::Done);
                        return;
                    }
                    progress.insert(hash, Compilation::Compiling);
                    drop(progress);
                    let progress = self.progress.clone();
                    let code = code.as_ref().to_vec();
                    let dir = self.dir.clone();
                    let store = self.store.clone();
                    std::thread::spawn(move || {
                        tracing::info!("Compiling WASM {}.", hash.to_string());

                        let _module = match wasm::run::prepare_wasm_code(code) {
                            Ok(code) => {
                                match compile(code, &store) {
                                    Ok(module) => {
                                        // Write the file
                                        file_write_module(&dir, &module, &hash);

                                        // Update progress
                                        let mut progress =
                                            progress.write().unwrap();
                                        progress
                                            .insert(hash, Compilation::Done);
                                        tracing::info!(
                                            "Finished compiling WASM {hash}."
                                        );
                                        if progress.values().all(
                                            |compilation| {
                                                matches!(
                                                    compilation,
                                                    Compilation::Done
                                                )
                                            },
                                        ) {
                                            tracing::info!(
                                                "Finished compiling all {}.",
                                                N::name()
                                            )
                                        }
                                        module
                                    }
                                    Err(err) => {
                                        let mut progress =
                                            progress.write().unwrap();
                                        tracing::info!(
                                            "Failed to compile WASM {} with {}",
                                            hash.to_string(),
                                            err
                                        );
                                        progress.swap_remove(&hash);
                                        return Err(err);
                                    }
                                }
                            }
                            Err(err) => {
                                let mut progress = progress.write().unwrap();
                                tracing::info!(
                                    "Failed to prepare WASM {} with {}",
                                    hash.to_string(),
                                    err
                                );
                                progress.swap_remove(&hash);
                                return Err(err);
                            }
                        };

                        let res: Result<(), wasm::run::Error> = Ok(());
                        res
                    });
                }
            }
        }
    }

    /// Get a read-only cache handle.
    pub fn read_only(&self) -> Cache<N, WasmCacheRoAccess> {
        Cache {
            dir: self.dir.clone(),
            progress: self.progress.clone(),
            in_memory: self.in_memory.clone(),
            name: Default::default(),
            access: Default::default(),
            store: self.store.clone(),
        }
    }
}

fn hash_of_code(code: impl AsRef<[u8]>) -> Hash {
    Hash::sha256(code.as_ref())
}

fn compile(
    code: impl AsRef<[u8]>,
    store: &Store,
) -> Result<Module, wasm::run::Error> {
    universal::compile(code, store).map_err(wasm::run::Error::CompileError)
}

fn file_ext() -> &'static str {
    // This has to be using the file_ext matching the compilation method in the
    // `fn compile`
    universal::FILE_EXT
}

pub(crate) fn store() -> Store {
    // This has to be using the store matching the compilation method in the
    // `fn compile`
    universal::store()
}

fn file_write_module(dir: impl AsRef<Path>, module: &Module, hash: &Hash) {
    use wasmer_cache::Cache;
    let mut fs_cache = fs_cache(dir, hash);
    fs_cache.store(CacheHash::new(hash.0), module).unwrap();
}

fn file_load_module(
    dir: impl AsRef<Path>,
    hash: &Hash,
    store: &Store,
) -> Result<Module, wasmer::DeserializeError> {
    use wasmer_cache::Cache;
    let fs_cache = fs_cache(dir, hash);
    let hash = CacheHash::new(hash.0);
    let module = unsafe { fs_cache.load(store, hash) };
    if let Err(err) = module.as_ref() {
        tracing::error!(
            "Error loading cached wasm {}: {err}.",
            hash.to_string()
        );
    }
    module
}

fn fs_cache(dir: impl AsRef<Path>, hash: &Hash) -> FileSystemCache {
    let path = dir.as_ref().join(hash.to_string().to_lowercase());
    let mut fs_cache = FileSystemCache::new(path).unwrap();
    fs_cache.set_cache_extension(Some(file_ext()));
    fs_cache
}

fn module_file_exists(dir: impl AsRef<Path>, hash: &Hash) -> bool {
    let file =
        dir.as_ref()
            .join(hash.to_string().to_lowercase())
            .join(format!(
                "{}.{}",
                hash.to_string().to_lowercase(),
                file_ext()
            ));
    file.exists()
}

/// A universal engine compilation. The module can be serialized to/from bytes.
mod universal {
    use super::*;

    #[allow(dead_code)]
    pub const FILE_EXT: &str = "bin";

    /// Compile wasm with a universal engine.
    #[allow(dead_code)]
    pub fn compile(
        code: impl AsRef<[u8]>,
        store: &Store,
    ) -> Result<Module, wasmer::CompileError> {
        Module::new(store, code.as_ref())
    }

    /// Universal WASM store
    #[allow(dead_code)]
    pub fn store() -> Store {
        untrusted_wasm_store(memory::vp_limit())
    }
}

/// Testing helpers
#[cfg(any(test, feature = "testing"))]
pub mod testing {
    use tempfile::{tempdir, TempDir};

    use super::*;
    use crate::wasm::{TxCache, VpCache};
    use crate::WasmCacheRwAccess;

    /// Instantiate the default wasmer store.
    pub fn store() -> Store {
        super::store()
    }

    /// VP Cache with a temp dir for testing
    pub fn vp_cache() -> (VpCache<WasmCacheRwAccess>, TempDir) {
        cache::<super::super::vp::Name>()
    }

    /// Tx Cache with a temp dir for testing
    pub fn tx_cache() -> (TxCache<WasmCacheRwAccess>, TempDir) {
        cache::<super::super::tx::Name>()
    }

    /// Generic Cache with a temp dir for testing
    pub fn cache<N: CacheName>() -> (Cache<N, WasmCacheRwAccess>, TempDir) {
        let dir = tempdir().unwrap();
        let cache = Cache::new(
            dir.path(),
            50 * 1024 * 1024, // 50 MiB
        );
        (cache, dir)
    }
}

#[allow(clippy::arithmetic_side_effects)]
#[cfg(test)]
mod test {
    use std::cmp::max;

    use assert_matches::assert_matches;
    use byte_unit::{Byte, UnitType};
    use namada_test_utils::TestWasms;
    use tempfile::{tempdir, TempDir};
    use test_log::test;

    use super::*;
    use crate::WasmCacheRwAccess;

    #[test]
    fn test_fetch_or_compile_valid_wasm() {
        // Load some WASMs and find their hashes and in-memory size
        let tx_read_storage_key = load_wasm(TestWasms::TxReadStorageKey.path());
        let tx_no_op = load_wasm(TestWasms::TxNoOp.path());

        // Create a new cache with the limit set to
        // `max(tx_read_storage_key.size, tx_no_op.size) + 1`
        {
            let max_bytes = max(tx_read_storage_key.size, tx_no_op.size) + 1;
            println!(
                "Using cache with max_bytes {} ({})",
                Byte::from_u128(max_bytes as u128)
                    .unwrap()
                    .get_appropriate_unit(UnitType::Binary),
                max_bytes
            );
            let (mut cache, _tmp_dir) = cache(max_bytes);

            // Fetch `tx_read_storage_key`
            {
                let fetched = cache.fetch(&tx_read_storage_key.hash).unwrap();
                assert_matches!(
                    fetched,
                    None,
                    "The module should not be in cache"
                );

                let fetched =
                    cache.compile_or_fetch(&tx_read_storage_key.code).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The code should be compiled"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_read_storage_key.hash),
                    Some(_),
                    "The module must be in memory"
                );

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&tx_read_storage_key.hash),
                    Some(Compilation::Done),
                    "The progress must be updated"
                );

                assert!(
                    module_file_exists(&cache.dir, &tx_read_storage_key.hash),
                    "The file must be written"
                );
            }

            // Fetch `tx_no_op`. Fetching another module should get us over the
            // limit, so the previous one should be popped from the cache
            {
                let fetched = cache.fetch(&tx_no_op.hash).unwrap();
                assert_matches!(
                    fetched,
                    None,
                    "The module must not be in cache"
                );

                let fetched = cache.compile_or_fetch(&tx_no_op.code).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The code should be compiled"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_no_op.hash),
                    Some(_),
                    "The module must be in memory"
                );

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&tx_no_op.hash),
                    Some(Compilation::Done),
                    "The progress must be updated"
                );

                assert!(
                    module_file_exists(&cache.dir, &tx_no_op.hash),
                    "The file must be written"
                );

                // The previous module's file should still exist
                assert!(
                    module_file_exists(&cache.dir, &tx_read_storage_key.hash),
                    "The file must be written"
                );
                // But it should not be in-memory
                assert_matches!(
                    in_memory.peek(&tx_read_storage_key.hash),
                    None,
                    "The module should have been popped from memory"
                );
            }

            // Reset the in-memory cache and progress and fetch
            // `tx_read_storage_key` again, this time it should get loaded
            // from file
            let in_memory_cache = CLruCache::with_config(
                CLruCacheConfig::new(NonZeroUsize::new(max_bytes).unwrap())
                    .with_scale(ModuleCacheScale),
            );
            let in_memory = Arc::new(RwLock::new(in_memory_cache));
            cache.in_memory = in_memory;
            cache.progress = Default::default();
            {
                let fetched = cache.fetch(&tx_read_storage_key.hash).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module must be in file cache"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_read_storage_key.hash),
                    Some(_),
                    "The module must be in memory"
                );

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&tx_read_storage_key.hash),
                    Some(Compilation::Done),
                    "The progress must be updated"
                );

                assert!(
                    module_file_exists(&cache.dir, &tx_read_storage_key.hash),
                    "The file must be written"
                );

                // The previous module's file should still exist
                assert!(
                    module_file_exists(&cache.dir, &tx_no_op.hash),
                    "The file must be written"
                );
                // But it should not be in-memory
                assert_matches!(
                    in_memory.peek(&tx_no_op.hash),
                    None,
                    "The module should have been popped from memory"
                );
            }

            // Fetch `tx_read_storage_key` again, now it should be in-memory
            {
                let fetched = cache.fetch(&tx_read_storage_key.hash).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module must be in memory"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_read_storage_key.hash),
                    Some(_),
                    "The module must be in memory"
                );

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&tx_read_storage_key.hash),
                    Some(Compilation::Done),
                    "The progress must be updated"
                );

                assert!(
                    module_file_exists(&cache.dir, &tx_read_storage_key.hash),
                    "The file must be written"
                );

                // The previous module's file should still exist
                assert!(
                    module_file_exists(&cache.dir, &tx_no_op.hash),
                    "The file must be written"
                );
                // But it should not be in-memory
                assert_matches!(
                    in_memory.peek(&tx_no_op.hash),
                    None,
                    "The module should have been popped from memory"
                );
            }

            // Fetch `tx_no_op` with read/only access
            {
                let mut cache = cache.read_only();

                let fetched = cache.fetch(&tx_no_op.hash).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module must be in cache"
                );

                // Fetching with read-only should not modify the in-memory cache
                let fetched = cache.compile_or_fetch(&tx_no_op.code).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module should be compiled"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_no_op.hash),
                    None,
                    "The module should not be added back to in-memory cache"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&tx_read_storage_key.hash),
                    Some(_),
                    "The previous module must still be in memory"
                );
            }
        }
    }

    #[test]
    fn test_fetch_or_compile_invalid_wasm() {
        // Some random bytes
        let invalid_wasm = vec![1_u8, 0, 8, 10, 6, 1];
        let hash = hash_of_code(&invalid_wasm);
        let (mut cache, _) = testing::cache::<TestCache>();

        // Try to compile it
        let error = cache
            .compile_or_fetch(&invalid_wasm)
            .expect_err("Compilation should fail");
        println!("Error: {}", error);

        let in_memory = cache.in_memory.read().unwrap();
        assert_matches!(
            in_memory.peek(&hash),
            None,
            "There should be no entry for this hash in memory"
        );

        let progress = cache.progress.read().unwrap();
        assert_matches!(progress.get(&hash), None, "Any progress is removed");

        assert!(
            !module_file_exists(&cache.dir, &hash),
            "The file must not be written"
        );
    }

    #[test]
    fn test_pre_compile_valid_wasm() {
        // Load some WASMs and find their hashes and in-memory size
        let vp_always_true = load_wasm(TestWasms::VpAlwaysTrue.path());
        let vp_eval = load_wasm(TestWasms::VpEval.path());

        // Create a new cache with the limit set to
        // `max(vp_always_true.size, vp_eval.size) + 1 + extra_bytes`
        {
            let max_bytes = max(vp_always_true.size, vp_eval.size) + 1;
            println!(
                "Using cache with max_bytes {} ({})",
                Byte::from_u128(max_bytes as u128)
                    .unwrap()
                    .get_appropriate_unit(UnitType::Binary),
                max_bytes
            );
            let (mut cache, _tmp_dir) = cache(max_bytes);

            // Pre-compile `vp_always_true`
            {
                cache.pre_compile(&vp_always_true.code);

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&vp_always_true.hash),
                    Some(Compilation::Done | Compilation::Compiling),
                    "The progress must be updated"
                );
            }

            // Now fetch it to wait for it finish compilation
            {
                let fetched = cache.fetch(&vp_always_true.hash).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module must be in cache"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&vp_always_true.hash),
                    Some(_),
                    "The module must be in memory"
                );

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&vp_always_true.hash),
                    Some(Compilation::Done),
                    "The progress must be updated"
                );

                assert!(
                    module_file_exists(&cache.dir, &vp_always_true.hash),
                    "The file must be written"
                );
            }

            // Pre-compile `vp_eval`. Pre-compiling another module should get us
            // over the limit, so the previous one should be popped
            // from the cache
            {
                cache.pre_compile(&vp_eval.code);

                let progress = cache.progress.read().unwrap();
                assert_matches!(
                    progress.get(&vp_eval.hash),
                    Some(Compilation::Done | Compilation::Compiling),
                    "The progress must be updated"
                );
            }

            // Now fetch it to wait for it finish compilation
            {
                let fetched = cache.fetch(&vp_eval.hash).unwrap();
                assert_matches!(
                    fetched,
                    Some(_),
                    "The module must be in cache"
                );

                let in_memory = cache.in_memory.read().unwrap();
                assert_matches!(
                    in_memory.peek(&vp_eval.hash),
                    Some(_),
                    "The module must be in memory"
                );

                assert!(
                    module_file_exists(&cache.dir, &vp_eval.hash),
                    "The file must be written"
                );

                // The previous module's file should still exist
                assert!(
                    module_file_exists(&cache.dir, &vp_always_true.hash),
                    "The file must be written"
                );
                // But it should not be in-memory
                assert_matches!(
                    in_memory.peek(&vp_always_true.hash),
                    None,
                    "The module should have been popped from memory"
                );
            }
        }
    }

    #[test]
    fn test_pre_compile_invalid_wasm() {
        // Some random bytes
        let invalid_wasm = vec![1_u8];
        let hash = hash_of_code(&invalid_wasm);
        let (mut cache, _) = testing::cache::<TestCache>();

        // Try to pre-compile it
        {
            cache.pre_compile(&invalid_wasm);
            let progress = cache.progress.read().unwrap();
            assert_matches!(
                progress.get(&hash),
                Some(Compilation::Done | Compilation::Compiling) | None,
                "The progress must be updated"
            );
        }

        // Now fetch it to wait for it finish compilation
        {
            let fetched = cache.fetch(&hash).unwrap();
            assert_matches!(
                fetched,
                None,
                "There should be no entry for this hash in cache"
            );

            let in_memory = cache.in_memory.read().unwrap();
            assert_matches!(
                in_memory.peek(&hash),
                None,
                "There should be no entry for this hash in memory"
            );

            let progress = cache.progress.read().unwrap();
            assert_matches!(
                progress.get(&hash),
                None,
                "Any progress is removed"
            );

            assert!(
                !module_file_exists(&cache.dir, &hash),
                "The file must not be written"
            );
        }
    }

    /// Get the WASM code bytes, its hash and find the compiled module's size
    fn load_wasm(file: impl AsRef<Path>) -> WasmWithMeta {
        let file = file.as_ref();
        let code = fs::read(file).unwrap();
        let hash = hash_of_code(&code);
        // Find the size of the compiled module
        let size = {
            let (mut cache, _tmp_dir) = cache(
                // No in-memory cache needed, but must be non-zero
                1,
            );
            let (_module, _store) =
                cache.compile_or_fetch(&code).unwrap().unwrap();
            1
        };
        println!(
            "Compiled module {} size including the hash: {} ({})",
            file.to_string_lossy(),
            Byte::from_u128(size as u128)
                .unwrap()
                .get_appropriate_unit(UnitType::Binary),
            size,
        );
        WasmWithMeta { code, hash, size }
    }

    /// A test helper for loading WASM and finding its hash and size
    #[derive(Clone, Debug)]
    struct WasmWithMeta {
        code: Vec<u8>,
        hash: Hash,
        /// Compiled module's in-memory size
        size: usize,
    }

    /// A `CacheName` implementation for unit tests
    #[derive(Clone, Debug)]
    struct TestCache;
    impl CacheName for TestCache {
        fn name() -> &'static str {
            "test"
        }
    }

    /// A cache with a temp dir for unit tests
    fn cache(
        max_bytes: usize,
    ) -> (Cache<TestCache, WasmCacheRwAccess>, TempDir) {
        let dir = tempdir().unwrap();
        let cache = Cache::new(dir.path(), max_bytes);
        (cache, dir)
    }
}