near / nearcore

Reference client for NEAR Protocol
https://near.org
GNU General Public License v3.0
2.32k stars 622 forks source link

Make `CompiledContractCache` non-optional #7278

Open matklad opened 2 years ago

matklad commented 2 years ago

At the moment, CompiledContractCache is optional:

https://github.com/near/nearcore/blob/e3a9b1ea3f9665339732208a391376f12c1522bf/core/primitives/src/runtime/apply_state.rs#L36-L38

However, our current execution semantics actually requires us to have the cache. We should:

mina86 commented 2 years ago

this requires finding all the places where it is optional in the first place

That’s simple:

$ gg -C10 'match cache '
runtime/near-vm-runner/src/cache.rs-213-            }
runtime/near-vm-runner/src/cache.rs-214-        }
runtime/near-vm-runner/src/cache.rs-215-    }
runtime/near-vm-runner/src/cache.rs-216-
runtime/near-vm-runner/src/cache.rs-217-    fn compile_module_cached_wasmer_impl(
runtime/near-vm-runner/src/cache.rs-218-        key: CryptoHash,
runtime/near-vm-runner/src/cache.rs-219-        wasm_code: &[u8],
runtime/near-vm-runner/src/cache.rs-220-        config: &VMConfig,
runtime/near-vm-runner/src/cache.rs-221-        cache: Option<&dyn CompiledContractCache>,
runtime/near-vm-runner/src/cache.rs-222-    ) -> Result<Result<wasmer_runtime::Module, CompilationError>, CacheError> {
runtime/near-vm-runner/src/cache.rs:223:        match cache {
runtime/near-vm-runner/src/cache.rs-224-            None => Ok(compile_module(wasm_code, config)),
runtime/near-vm-runner/src/cache.rs-225-            Some(cache) => {
runtime/near-vm-runner/src/cache.rs-226-                let serialized = cache.get(&key.0).map_err(|_io_err| CacheError::ReadError)?;
runtime/near-vm-runner/src/cache.rs-227-                match serialized {
runtime/near-vm-runner/src/cache.rs-228-                    Some(serialized) => deserialize_wasmer(serialized.as_slice()),
runtime/near-vm-runner/src/cache.rs-229-                    None => compile_and_serialize_wasmer(wasm_code, config, &key, cache),
runtime/near-vm-runner/src/cache.rs-230-                }
runtime/near-vm-runner/src/cache.rs-231-            }
runtime/near-vm-runner/src/cache.rs-232-        }
runtime/near-vm-runner/src/cache.rs-233-    }
--
runtime/near-vm-runner/src/cache.rs-326-        }
runtime/near-vm-runner/src/cache.rs-327-    }
runtime/near-vm-runner/src/cache.rs-328-
runtime/near-vm-runner/src/cache.rs-329-    fn compile_module_cached_wasmer2_impl(
runtime/near-vm-runner/src/cache.rs-330-        key: CryptoHash,
runtime/near-vm-runner/src/cache.rs-331-        code: &ContractCode,
runtime/near-vm-runner/src/cache.rs-332-        config: &VMConfig,
runtime/near-vm-runner/src/cache.rs-333-        cache: Option<&dyn CompiledContractCache>,
runtime/near-vm-runner/src/cache.rs-334-    ) -> Result<Result<VMArtifact, CompilationError>, CacheError> {
runtime/near-vm-runner/src/cache.rs-335-        let vm = Wasmer2VM::new(config.clone());
runtime/near-vm-runner/src/cache.rs:336:        match cache {
runtime/near-vm-runner/src/cache.rs-337-            None => Ok(compile_module_wasmer2(&vm, code.code(), config).and_then(|executable| {
runtime/near-vm-runner/src/cache.rs-338-                vm.engine
runtime/near-vm-runner/src/cache.rs-339-                    .load_universal_executable(&executable)
runtime/near-vm-runner/src/cache.rs-340-                    .map(|v| Arc::new(v) as _)
runtime/near-vm-runner/src/cache.rs-341-                    .map_err(|err| panic!("could not load the executable: {}", err.to_string()))
runtime/near-vm-runner/src/cache.rs-342-            })),
runtime/near-vm-runner/src/cache.rs-343-            Some(cache) => {
runtime/near-vm-runner/src/cache.rs-344-                let serialized = cache.get(&key.0).map_err(|_io_err| CacheError::ReadError)?;
runtime/near-vm-runner/src/cache.rs-345-                match serialized {
runtime/near-vm-runner/src/cache.rs-346-                    Some(serialized) => deserialize_wasmer2(serialized.as_slice(), config),
--
runtime/near-vm-runner/src/cache.rs-366-        return compile_module_cached_wasmer2_impl(key, code, config, cache);
runtime/near-vm-runner/src/cache.rs-367-    }
runtime/near-vm-runner/src/cache.rs-368-}
runtime/near-vm-runner/src/cache.rs-369-
runtime/near-vm-runner/src/cache.rs-370-pub fn precompile_contract_vm(
runtime/near-vm-runner/src/cache.rs-371-    vm_kind: VMKind,
runtime/near-vm-runner/src/cache.rs-372-    wasm_code: &ContractCode,
runtime/near-vm-runner/src/cache.rs-373-    config: &VMConfig,
runtime/near-vm-runner/src/cache.rs-374-    cache: Option<&dyn CompiledContractCache>,
runtime/near-vm-runner/src/cache.rs-375-) -> Result<Result<ContractPrecompilatonResult, CompilationError>, CacheError> {
runtime/near-vm-runner/src/cache.rs:376:    let cache = match cache {
runtime/near-vm-runner/src/cache.rs-377-        None => return Ok(Ok(ContractPrecompilatonResult::CacheNotAvailable)),
runtime/near-vm-runner/src/cache.rs-378-        Some(it) => it,
runtime/near-vm-runner/src/cache.rs-379-    };
runtime/near-vm-runner/src/cache.rs-380-    let key = get_contract_cache_key(wasm_code, vm_kind, config);
runtime/near-vm-runner/src/cache.rs-381-    // Check if we already cached with such a key.
runtime/near-vm-runner/src/cache.rs-382-    match cache.get(&key.0).map_err(|_io_error| CacheError::ReadError)? {
runtime/near-vm-runner/src/cache.rs-383-        // If so - do not override.
runtime/near-vm-runner/src/cache.rs-384-        Some(_) => return Ok(Ok(ContractPrecompilatonResult::ContractAlreadyInCache)),
runtime/near-vm-runner/src/cache.rs-385-        None => {}
runtime/near-vm-runner/src/cache.rs-386-    };