@@ -4,7 +4,7 @@ use std::io::{Read, Write};
4
4
use std:: marker:: PhantomData ;
5
5
use std:: path:: { Path , PathBuf } ;
6
6
use std:: sync:: Mutex ;
7
- use wasmer:: { Engine , Store } ;
7
+ use wasmer:: { Module , Store } ;
8
8
9
9
use crate :: backend:: { Backend , BackendApi , Querier , Storage } ;
10
10
use crate :: capabilities:: required_capabilities_from_module;
@@ -17,7 +17,7 @@ use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryC
17
17
use crate :: parsed_wasm:: ParsedWasm ;
18
18
use crate :: size:: Size ;
19
19
use crate :: static_analysis:: has_ibc_entry_points;
20
- use crate :: wasm_backend:: { compile, make_compiling_engine, make_runtime_engine } ;
20
+ use crate :: wasm_backend:: { compile, make_compiling_engine} ;
21
21
22
22
const STATE_DIR : & str = "state" ;
23
23
// Things related to the state of the blockchain.
@@ -88,24 +88,14 @@ pub struct CacheInner {
88
88
memory_cache : InMemoryCache ,
89
89
fs_cache : FileSystemCache ,
90
90
stats : Stats ,
91
- /// A single engine to execute all contracts in this cache instance (usually
92
- /// this means all contracts in the process).
93
- ///
94
- /// This engine is headless, i.e. does not contain a Singlepass compiler.
95
- /// It only executes modules compiled with other engines.
96
- ///
97
- /// The engine has one memory limit set which is the same for all contracts
98
- /// running with it. If different memory limits would be needed for different
99
- /// contracts at some point, we'd need multiple engines. This is because the tunables
100
- /// that control the limit are attached to the engine.
101
- runtime_engine : Engine ,
102
91
}
103
92
104
93
pub struct Cache < A : BackendApi , S : Storage , Q : Querier > {
105
94
/// Available capabilities are immutable for the lifetime of the cache,
106
95
/// i.e. any number of read-only references is allowed to access it concurrently.
107
96
available_capabilities : HashSet < String > ,
108
97
inner : Mutex < CacheInner > ,
98
+ instance_memory_limit : Size ,
109
99
// Those two don't store data but only fix type information
110
100
type_api : PhantomData < A > ,
111
101
type_storage : PhantomData < S > ,
@@ -161,8 +151,8 @@ where
161
151
memory_cache : InMemoryCache :: new ( memory_cache_size) ,
162
152
fs_cache,
163
153
stats : Stats :: default ( ) ,
164
- runtime_engine : make_runtime_engine ( Some ( instance_memory_limit) ) ,
165
154
} ) ,
155
+ instance_memory_limit,
166
156
type_storage : PhantomData :: < S > ,
167
157
type_api : PhantomData :: < A > ,
168
158
type_querier : PhantomData :: < Q > ,
@@ -298,11 +288,12 @@ where
298
288
// for a not-so-relevant use case.
299
289
300
290
// Try to get module from file system cache
301
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
291
+ if let Some ( cached_module) = cache
292
+ . fs_cache
293
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
294
+ {
302
295
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
303
- return cache
304
- . pinned_memory_cache
305
- . store ( checksum, module, module_size) ;
296
+ return cache. pinned_memory_cache . store ( checksum, cached_module) ;
306
297
}
307
298
308
299
// Re-compile from original Wasm bytecode
@@ -317,16 +308,16 @@ where
317
308
}
318
309
319
310
// This time we'll hit the file-system cache.
320
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
311
+ let Some ( cached_module) = cache
312
+ . fs_cache
313
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
321
314
else {
322
315
return Err ( VmError :: generic_err (
323
316
"Can't load module from file system cache after storing it to file system cache (pin)" ,
324
317
) ) ;
325
318
} ;
326
319
327
- cache
328
- . pinned_memory_cache
329
- . store ( checksum, module, module_size)
320
+ cache. pinned_memory_cache . store ( checksum, cached_module)
330
321
}
331
322
332
323
/// Unpins a Module, i.e. removes it from the pinned memory cache.
@@ -350,10 +341,10 @@ where
350
341
backend : Backend < A , S , Q > ,
351
342
options : InstanceOptions ,
352
343
) -> VmResult < Instance < A , S , Q > > {
353
- let ( cached , store) = self . get_module ( checksum) ?;
344
+ let ( module , store) = self . get_module ( checksum) ?;
354
345
let instance = Instance :: from_module (
355
346
store,
356
- & cached . module ,
347
+ & module,
357
348
backend,
358
349
options. gas_limit ,
359
350
options. print_debug ,
@@ -366,36 +357,49 @@ where
366
357
/// Returns a module tied to a previously saved Wasm.
367
358
/// Depending on availability, this is either generated from a memory cache, file system cache or Wasm code.
368
359
/// This is part of `get_instance` but pulled out to reduce the locking time.
369
- fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( CachedModule , Store ) > {
360
+ fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( Module , Store ) > {
370
361
let mut cache = self . inner . lock ( ) . unwrap ( ) ;
371
362
// Try to get module from the pinned memory cache
372
363
if let Some ( element) = cache. pinned_memory_cache . load ( checksum) ? {
373
364
cache. stats . hits_pinned_memory_cache =
374
365
cache. stats . hits_pinned_memory_cache . saturating_add ( 1 ) ;
375
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
376
- return Ok ( ( element, store) ) ;
366
+ let CachedModule {
367
+ module,
368
+ engine,
369
+ size_estimate : _,
370
+ } = element;
371
+ let store = Store :: new ( engine) ;
372
+ return Ok ( ( module, store) ) ;
377
373
}
378
374
379
375
// Get module from memory cache
380
376
if let Some ( element) = cache. memory_cache . load ( checksum) ? {
381
377
cache. stats . hits_memory_cache = cache. stats . hits_memory_cache . saturating_add ( 1 ) ;
382
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
383
- return Ok ( ( element, store) ) ;
378
+ let CachedModule {
379
+ module,
380
+ engine,
381
+ size_estimate : _,
382
+ } = element;
383
+ let store = Store :: new ( engine) ;
384
+ return Ok ( ( module, store) ) ;
384
385
}
385
386
386
387
// Get module from file system cache
387
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
388
+ if let Some ( cached_module) = cache
389
+ . fs_cache
390
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
391
+ {
388
392
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
389
393
390
- cache
391
- . memory_cache
392
- . store ( checksum, module. clone ( ) , module_size) ?;
393
- let cached = CachedModule {
394
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
395
+
396
+ let CachedModule {
394
397
module,
395
- size_estimate : module_size,
396
- } ;
397
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
398
- return Ok ( ( cached, store) ) ;
398
+ engine,
399
+ size_estimate : _,
400
+ } = cached_module;
401
+ let store = Store :: new ( engine) ;
402
+ return Ok ( ( module, store) ) ;
399
403
}
400
404
401
405
// Re-compile module from wasm
@@ -414,21 +418,23 @@ where
414
418
}
415
419
416
420
// This time we'll hit the file-system cache.
417
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
421
+ let Some ( cached_module) = cache
422
+ . fs_cache
423
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
418
424
else {
419
425
return Err ( VmError :: generic_err (
420
426
"Can't load module from file system cache after storing it to file system cache (get_module)" ,
421
427
) ) ;
422
428
} ;
423
- cache
424
- . memory_cache
425
- . store ( checksum, module. clone ( ) , module_size) ?;
426
- let cached = CachedModule {
429
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
430
+
431
+ let CachedModule {
427
432
module,
428
- size_estimate : module_size,
429
- } ;
430
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
431
- Ok ( ( cached, store) )
433
+ engine,
434
+ size_estimate : _,
435
+ } = cached_module;
436
+ let store = Store :: new ( engine) ;
437
+ Ok ( ( module, store) )
432
438
}
433
439
}
434
440
0 commit comments