@@ -229,13 +229,30 @@ where
229229 /// This does the same as [`save_wasm_unchecked`] plus the static checks.
230230 /// When a Wasm blob is stored the first time, use this function.
231231 pub fn save_wasm ( & self , wasm : & [ u8 ] ) -> VmResult < Checksum > {
232+ self . store_code ( wasm, true )
233+ }
234+
235+ /// Takes a Wasm bytecode and stores it to the cache.
236+ ///
237+ /// This performs static checks, compiles the bytescode to a module and
238+ /// stores the Wasm file on disk if `persist` is `true`.
239+ ///
240+ /// This does the same as [`save_wasm`] if `persist` is `true`.
241+ pub fn store_code ( & self , wasm : & [ u8 ] , persist : bool ) -> VmResult < Checksum > {
232242 check_wasm (
233243 wasm,
234244 & self . available_capabilities ,
235245 & self . wasm_limits ,
236246 crate :: internals:: Logger :: Off ,
237247 ) ?;
238- self . save_wasm_unchecked ( wasm)
248+
249+ let module = compile_module ( wasm) ?;
250+
251+ if persist {
252+ self . save_to_disk ( wasm, & module)
253+ } else {
254+ Ok ( Checksum :: generate ( wasm) )
255+ }
239256 }
240257
241258 /// Takes a Wasm bytecode and stores it to the cache.
@@ -247,14 +264,14 @@ where
247264 /// When a Wasm blob is stored which was previously checked (e.g. as part of state sync),
248265 /// use this function.
249266 pub fn save_wasm_unchecked ( & self , wasm : & [ u8 ] ) -> VmResult < Checksum > {
250- // We need a new engine for each Wasm -> module compilation due to the metering middleware.
251- let compiling_engine = make_compiling_engine ( None ) ;
252- // This module cannot be executed directly as it was not created with the runtime engine
253- let module = compile ( & compiling_engine, wasm) ?;
267+ let module = compile_module ( wasm) ?;
268+ self . save_to_disk ( wasm, & module)
269+ }
254270
271+ fn save_to_disk ( & self , wasm : & [ u8 ] , module : & Module ) -> VmResult < Checksum > {
255272 let mut cache = self . inner . lock ( ) . unwrap ( ) ;
256273 let checksum = save_wasm_to_disk ( & cache. wasm_path , wasm) ?;
257- cache. fs_cache . store ( & checksum, & module) ?;
274+ cache. fs_cache . store ( & checksum, module) ?;
258275 Ok ( checksum)
259276 }
260277
@@ -491,6 +508,12 @@ where
491508 }
492509}
493510
511+ fn compile_module ( wasm : & [ u8 ] ) -> Result < Module , VmError > {
512+ let compiling_engine = make_compiling_engine ( None ) ;
513+ let module = compile ( & compiling_engine, wasm) ?;
514+ Ok ( module)
515+ }
516+
494517unsafe impl < A , S , Q > Sync for Cache < A , S , Q >
495518where
496519 A : BackendApi + ' static ,
0 commit comments