@@ -5,7 +5,7 @@ use std::marker::PhantomData;
5
5
use std:: path:: { Path , PathBuf } ;
6
6
use std:: str:: FromStr ;
7
7
use std:: sync:: Mutex ;
8
- use wasmer:: { Engine , Store } ;
8
+ use wasmer:: { Module , Store } ;
9
9
10
10
use cosmwasm_std:: Checksum ;
11
11
@@ -19,7 +19,7 @@ use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryC
19
19
use crate :: parsed_wasm:: ParsedWasm ;
20
20
use crate :: size:: Size ;
21
21
use crate :: static_analysis:: { Entrypoint , ExportInfo , REQUIRED_IBC_EXPORTS } ;
22
- use crate :: wasm_backend:: { compile, make_compiling_engine, make_runtime_engine } ;
22
+ use crate :: wasm_backend:: { compile, make_compiling_engine} ;
23
23
24
24
const STATE_DIR : & str = "state" ;
25
25
// Things related to the state of the blockchain.
@@ -91,24 +91,14 @@ pub struct CacheInner {
91
91
memory_cache : InMemoryCache ,
92
92
fs_cache : FileSystemCache ,
93
93
stats : Stats ,
94
- /// A single engine to execute all contracts in this cache instance (usually
95
- /// this means all contracts in the process).
96
- ///
97
- /// This engine is headless, i.e. does not contain a Singlepass compiler.
98
- /// It only executes modules compiled with other engines.
99
- ///
100
- /// The engine has one memory limit set which is the same for all contracts
101
- /// running with it. If different memory limits would be needed for different
102
- /// contracts at some point, we'd need multiple engines. This is because the tunables
103
- /// that control the limit are attached to the engine.
104
- runtime_engine : Engine ,
105
94
}
106
95
107
96
pub struct Cache < A : BackendApi , S : Storage , Q : Querier > {
108
97
/// Available capabilities are immutable for the lifetime of the cache,
109
98
/// i.e. any number of read-only references is allowed to access it concurrently.
110
99
available_capabilities : HashSet < String > ,
111
100
inner : Mutex < CacheInner > ,
101
+ instance_memory_limit : Size ,
112
102
// Those two don't store data but only fix type information
113
103
type_api : PhantomData < A > ,
114
104
type_storage : PhantomData < S > ,
@@ -169,8 +159,8 @@ where
169
159
memory_cache : InMemoryCache :: new ( memory_cache_size) ,
170
160
fs_cache,
171
161
stats : Stats :: default ( ) ,
172
- runtime_engine : make_runtime_engine ( Some ( instance_memory_limit) ) ,
173
162
} ) ,
163
+ instance_memory_limit,
174
164
type_storage : PhantomData :: < S > ,
175
165
type_api : PhantomData :: < A > ,
176
166
type_querier : PhantomData :: < Q > ,
@@ -318,11 +308,12 @@ where
318
308
// for a not-so-relevant use case.
319
309
320
310
// Try to get module from file system cache
321
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
311
+ if let Some ( cached_module) = cache
312
+ . fs_cache
313
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
314
+ {
322
315
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
323
- return cache
324
- . pinned_memory_cache
325
- . store ( checksum, module, module_size) ;
316
+ return cache. pinned_memory_cache . store ( checksum, cached_module) ;
326
317
}
327
318
328
319
// Re-compile from original Wasm bytecode
@@ -337,16 +328,16 @@ where
337
328
}
338
329
339
330
// This time we'll hit the file-system cache.
340
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
331
+ let Some ( cached_module) = cache
332
+ . fs_cache
333
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
341
334
else {
342
335
return Err ( VmError :: generic_err (
343
336
"Can't load module from file system cache after storing it to file system cache (pin)" ,
344
337
) ) ;
345
338
} ;
346
339
347
- cache
348
- . pinned_memory_cache
349
- . store ( checksum, module, module_size)
340
+ cache. pinned_memory_cache . store ( checksum, cached_module)
350
341
}
351
342
352
343
/// Unpins a Module, i.e. removes it from the pinned memory cache.
@@ -370,10 +361,10 @@ where
370
361
backend : Backend < A , S , Q > ,
371
362
options : InstanceOptions ,
372
363
) -> VmResult < Instance < A , S , Q > > {
373
- let ( cached , store) = self . get_module ( checksum) ?;
364
+ let ( module , store) = self . get_module ( checksum) ?;
374
365
let instance = Instance :: from_module (
375
366
store,
376
- & cached . module ,
367
+ & module,
377
368
backend,
378
369
options. gas_limit ,
379
370
None ,
@@ -385,36 +376,49 @@ where
385
376
/// Returns a module tied to a previously saved Wasm.
386
377
/// Depending on availability, this is either generated from a memory cache, file system cache or Wasm code.
387
378
/// This is part of `get_instance` but pulled out to reduce the locking time.
388
- fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( CachedModule , Store ) > {
379
+ fn get_module ( & self , checksum : & Checksum ) -> VmResult < ( Module , Store ) > {
389
380
let mut cache = self . inner . lock ( ) . unwrap ( ) ;
390
381
// Try to get module from the pinned memory cache
391
382
if let Some ( element) = cache. pinned_memory_cache . load ( checksum) ? {
392
383
cache. stats . hits_pinned_memory_cache =
393
384
cache. stats . hits_pinned_memory_cache . saturating_add ( 1 ) ;
394
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
395
- return Ok ( ( element, store) ) ;
385
+ let CachedModule {
386
+ module,
387
+ engine,
388
+ size_estimate : _,
389
+ } = element;
390
+ let store = Store :: new ( engine) ;
391
+ return Ok ( ( module, store) ) ;
396
392
}
397
393
398
394
// Get module from memory cache
399
395
if let Some ( element) = cache. memory_cache . load ( checksum) ? {
400
396
cache. stats . hits_memory_cache = cache. stats . hits_memory_cache . saturating_add ( 1 ) ;
401
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
402
- return Ok ( ( element, store) ) ;
397
+ let CachedModule {
398
+ module,
399
+ engine,
400
+ size_estimate : _,
401
+ } = element;
402
+ let store = Store :: new ( engine) ;
403
+ return Ok ( ( module, store) ) ;
403
404
}
404
405
405
406
// Get module from file system cache
406
- if let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ? {
407
+ if let Some ( cached_module) = cache
408
+ . fs_cache
409
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
410
+ {
407
411
cache. stats . hits_fs_cache = cache. stats . hits_fs_cache . saturating_add ( 1 ) ;
408
412
409
- cache
410
- . memory_cache
411
- . store ( checksum, module. clone ( ) , module_size) ?;
412
- let cached = CachedModule {
413
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
414
+
415
+ let CachedModule {
413
416
module,
414
- size_estimate : module_size,
415
- } ;
416
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
417
- return Ok ( ( cached, store) ) ;
417
+ engine,
418
+ size_estimate : _,
419
+ } = cached_module;
420
+ let store = Store :: new ( engine) ;
421
+ return Ok ( ( module, store) ) ;
418
422
}
419
423
420
424
// Re-compile module from wasm
@@ -433,21 +437,23 @@ where
433
437
}
434
438
435
439
// This time we'll hit the file-system cache.
436
- let Some ( ( module, module_size) ) = cache. fs_cache . load ( checksum, & cache. runtime_engine ) ?
440
+ let Some ( cached_module) = cache
441
+ . fs_cache
442
+ . load ( checksum, Some ( self . instance_memory_limit ) ) ?
437
443
else {
438
444
return Err ( VmError :: generic_err (
439
445
"Can't load module from file system cache after storing it to file system cache (get_module)" ,
440
446
) ) ;
441
447
} ;
442
- cache
443
- . memory_cache
444
- . store ( checksum, module. clone ( ) , module_size) ?;
445
- let cached = CachedModule {
448
+ cache. memory_cache . store ( checksum, cached_module. clone ( ) ) ?;
449
+
450
+ let CachedModule {
446
451
module,
447
- size_estimate : module_size,
448
- } ;
449
- let store = Store :: new ( cache. runtime_engine . clone ( ) ) ;
450
- Ok ( ( cached, store) )
452
+ engine,
453
+ size_estimate : _,
454
+ } = cached_module;
455
+ let store = Store :: new ( engine) ;
456
+ Ok ( ( module, store) )
451
457
}
452
458
}
453
459
0 commit comments