Contracts upload with Determinism::Enforced when possible (#3540)

Co-authored-by: Cyrill Leutwiler <cyrill@parity.io>
Co-authored-by: command-bot <>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
This commit is contained in:
PG Herveou
2024-03-06 17:35:55 +01:00
committed by GitHub
parent f2f4b154d7
commit 9952786e1b
8 changed files with 756 additions and 636 deletions
@@ -313,6 +313,11 @@ impl<T: Config> CodeInfo<T> {
}
}
/// Returns the determinism of the module.
pub fn determinism(&self) -> Determinism {
self.determinism
}
/// Returns reference count of the module.
pub fn refcount(&self) -> u64 {
self.refcount
+19 -6
View File
@@ -79,7 +79,10 @@ impl LoadedModule {
}
let engine = Engine::new(&config);
let module = Module::new(&engine, code).map_err(|_| "Can't load the module into wasmi!")?;
let module = Module::new(&engine, code).map_err(|err| {
log::debug!(target: LOG_TARGET, "Module creation failed: {:?}", err);
"Can't load the module into wasmi!"
})?;
// Return a `LoadedModule` instance with
// __valid__ module.
@@ -220,7 +223,7 @@ impl LoadedModule {
fn validate<E, T>(
code: &[u8],
schedule: &Schedule<T>,
determinism: Determinism,
determinism: &mut Determinism,
) -> Result<(), (DispatchError, &'static str)>
where
E: Environment<()>,
@@ -229,7 +232,17 @@ where
(|| {
// We check that the module is generally valid,
// and does not have restricted WebAssembly features, here.
let contract_module = LoadedModule::new::<T>(code, determinism, None)?;
let contract_module = match *determinism {
Determinism::Relaxed =>
if let Ok(module) = LoadedModule::new::<T>(code, Determinism::Enforced, None) {
*determinism = Determinism::Enforced;
module
} else {
LoadedModule::new::<T>(code, Determinism::Relaxed, None)?
},
Determinism::Enforced => LoadedModule::new::<T>(code, Determinism::Enforced, None)?,
};
// The we check that module satisfies constraints the pallet puts on contracts.
contract_module.scan_exports()?;
contract_module.scan_imports::<T>(schedule)?;
@@ -252,7 +265,7 @@ where
&code,
(),
schedule,
determinism,
*determinism,
stack_limits,
AllowDeprecatedInterface::No,
)
@@ -276,13 +289,13 @@ pub fn prepare<E, T>(
code: CodeVec<T>,
schedule: &Schedule<T>,
owner: AccountIdOf<T>,
determinism: Determinism,
mut determinism: Determinism,
) -> Result<WasmBlob<T>, (DispatchError, &'static str)>
where
E: Environment<()>,
T: Config,
{
validate::<E, T>(code.as_ref(), schedule, determinism)?;
validate::<E, T>(code.as_ref(), schedule, &mut determinism)?;
// Calculate deposit for storing contract code and `code_info` in two different storage items.
let code_len = code.len() as u32;