contracts: Upgrade to wasmi 0.28 (#13312)

* Upgrade to wasmi 0.28

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* Update stale comment

* Renamed variants of `Determinism`

* Compile fix

---------

Co-authored-by: command-bot <>
This commit is contained in:
Alexander Theißen
2023-03-21 00:09:22 +01:00
committed by GitHub
parent d5650ba07b
commit 82cb69922f
15 changed files with 1338 additions and 1393 deletions
@@ -512,7 +512,7 @@ pub fn max_pages<T: Config>() -> u32 {
fn inject_gas_metering<T: Config>(module: Module) -> Module {
let schedule = T::Schedule::get();
let gas_rules = schedule.rules(Determinism::Deterministic);
let gas_rules = schedule.rules(Determinism::Enforced);
let backend = gas_metering::host_function::Injector::new("seal0", "gas");
gas_metering::inject(module, backend, &gas_rules).unwrap()
}
@@ -381,7 +381,7 @@ benchmarks! {
T::Currency::make_free_balance_be(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::sized(c, Location::Call);
let origin = RawOrigin::Signed(caller.clone());
}: _(origin, code, None, Determinism::Deterministic)
}: _(origin, code, None, Determinism::Enforced)
verify {
// uploading the code reserves some balance in the callers account
assert!(T::Currency::reserved_balance(&caller) > 0u32.into());
@@ -397,7 +397,7 @@ benchmarks! {
T::Currency::make_free_balance_be(&caller, caller_funding::<T>());
let WasmModule { code, hash, .. } = WasmModule::<T>::dummy();
let origin = RawOrigin::Signed(caller.clone());
let uploaded = <Contracts<T>>::bare_upload_code(caller.clone(), code, None, Determinism::Deterministic)?;
let uploaded = <Contracts<T>>::bare_upload_code(caller.clone(), code, None, Determinism::Enforced)?;
assert_eq!(uploaded.code_hash, hash);
assert_eq!(uploaded.deposit, T::Currency::reserved_balance(&caller));
assert!(<Contract<T>>::code_exists(&hash));
@@ -941,7 +941,7 @@ benchmarks! {
None,
vec![],
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result?;
}
@@ -990,7 +990,7 @@ benchmarks! {
None,
vec![],
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result?;
}
@@ -3063,7 +3063,7 @@ benchmarks! {
None,
data,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result?;
}
@@ -3112,7 +3112,7 @@ benchmarks! {
None,
data,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result?;
}
+37 -37
View File
@@ -665,7 +665,7 @@ where
schedule,
value,
debug_message,
Determinism::Deterministic,
Determinism::Enforced,
)?;
let account_id = stack.top_frame().account_id.clone();
stack.run(executable, input_data).map(|ret| (account_id, ret))
@@ -759,10 +759,10 @@ where
},
};
// `AllowIndeterminism` will only be ever set in case of off-chain execution.
// `Relaxed` will only be ever set in case of off-chain execution.
// Instantiations are never allowed even when executing off-chain.
if !(executable.is_deterministic() ||
(matches!(determinism, Determinism::AllowIndeterminism) &&
(matches!(determinism, Determinism::Relaxed) &&
matches!(entry_point, ExportedFunction::Call)))
{
return Err(Error::<T>::Indeterministic.into())
@@ -1607,7 +1607,7 @@ mod tests {
value,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
),
Ok(_)
);
@@ -1661,7 +1661,7 @@ mod tests {
value,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
@@ -1703,7 +1703,7 @@ mod tests {
value,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
@@ -1739,7 +1739,7 @@ mod tests {
55,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
@@ -1791,7 +1791,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
let output = result.unwrap();
@@ -1824,7 +1824,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
let output = result.unwrap();
@@ -1855,7 +1855,7 @@ mod tests {
0,
vec![1, 2, 3, 4],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -1935,7 +1935,7 @@ mod tests {
value,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
@@ -1981,7 +1981,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
@@ -2015,7 +2015,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -2045,7 +2045,7 @@ mod tests {
0,
vec![0],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -2073,7 +2073,7 @@ mod tests {
0,
vec![0],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -2109,7 +2109,7 @@ mod tests {
0,
vec![0],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -2145,7 +2145,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
@@ -2304,7 +2304,7 @@ mod tests {
min_balance * 10,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
),
Ok(_)
);
@@ -2369,7 +2369,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
),
Ok(_)
);
@@ -2455,7 +2455,7 @@ mod tests {
0,
vec![0],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -2521,7 +2521,7 @@ mod tests {
0,
vec![],
Some(&mut debug_buffer),
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
});
@@ -2555,7 +2555,7 @@ mod tests {
0,
vec![],
Some(&mut debug_buffer),
Determinism::Deterministic,
Determinism::Enforced,
);
assert!(result.is_err());
});
@@ -2592,7 +2592,7 @@ mod tests {
0,
vec![],
Some(&mut debug_buf_after),
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
assert_eq!(debug_buf_before, debug_buf_after);
@@ -2625,7 +2625,7 @@ mod tests {
0,
CHARLIE.encode(),
None,
Determinism::Deterministic
Determinism::Enforced
));
// Calling into oneself fails
@@ -2639,7 +2639,7 @@ mod tests {
0,
BOB.encode(),
None,
Determinism::Deterministic
Determinism::Enforced
)
.map_err(|e| e.error),
<Error<Test>>::ReentranceDenied,
@@ -2678,7 +2678,7 @@ mod tests {
0,
vec![0],
None,
Determinism::Deterministic
Determinism::Enforced
)
.map_err(|e| e.error),
<Error<Test>>::ReentranceDenied,
@@ -2713,7 +2713,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
@@ -2798,7 +2798,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
)
.unwrap();
@@ -3007,7 +3007,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3134,7 +3134,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3173,7 +3173,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3212,7 +3212,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3268,7 +3268,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3324,7 +3324,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3356,7 +3356,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
@@ -3402,7 +3402,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic
Determinism::Enforced
));
});
}
@@ -3432,7 +3432,7 @@ mod tests {
0,
vec![],
None,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result, Ok(_));
});
+6 -6
View File
@@ -509,9 +509,9 @@ pub mod pallet {
/// the in storage version to the current
/// [`InstructionWeights::version`](InstructionWeights).
///
/// - `determinism`: If this is set to any other value but [`Determinism::Deterministic`]
/// then the only way to use this code is to delegate call into it from an offchain
/// execution. Set to [`Determinism::Deterministic`] if in doubt.
/// - `determinism`: If this is set to any other value but [`Determinism::Enforced`] then
/// the only way to use this code is to delegate call into it from an offchain execution.
/// Set to [`Determinism::Enforced`] if in doubt.
///
/// # Note
///
@@ -625,8 +625,8 @@ pub mod pallet {
storage_deposit_limit: storage_deposit_limit.map(Into::into),
debug_message: None,
};
let mut output = CallInput::<T> { dest, determinism: Determinism::Deterministic }
.run_guarded(common);
let mut output =
CallInput::<T> { dest, determinism: Determinism::Enforced }.run_guarded(common);
if let Ok(retval) = &output.result {
if retval.did_revert() {
output.result = Err(<Error<T>>::ContractReverted.into());
@@ -1096,7 +1096,7 @@ impl<T: Config> Invokable<T> for InstantiateInput<T> {
binary.clone(),
&schedule,
common.origin.clone(),
Determinism::Deterministic,
Determinism::Enforced,
TryInstantiate::Skip,
)
.map_err(|(err, msg)| {
+2 -2
View File
@@ -394,7 +394,7 @@ mod v9 {
initial: old.initial,
maximum: old.maximum,
code: old.code,
determinism: Determinism::Deterministic,
determinism: Determinism::Enforced,
})
});
}
@@ -464,7 +464,7 @@ mod post_checks {
fn v9<T: Config>() -> Result<(), &'static str> {
for value in CodeStorage::<T>::iter_values() {
ensure!(
value.determinism == Determinism::Deterministic,
value.determinism == Determinism::Enforced,
"All pre-existing codes need to be deterministic."
);
}
+3 -4
View File
@@ -175,9 +175,9 @@ pub struct InstructionWeights<T: Config> {
pub version: u32,
/// Weight to be used for instructions which don't have benchmarks assigned.
///
/// This weight is used whenever a code is uploaded with [`Determinism::AllowIndeterminism`]
/// This weight is used whenever a code is uploaded with [`Determinism::Relaxed`]
/// and an instruction (usually a float instruction) is encountered. This weight is **not**
/// used if a contract is uploaded with [`Determinism::Deterministic`]. If this field is set to
/// used if a contract is uploaded with [`Determinism::Enforced`]. If this field is set to
/// `0` (the default) only deterministic codes are allowed to be uploaded.
pub fallback: u32,
pub i64const: u32,
@@ -715,8 +715,7 @@ impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> {
// Returning None makes the gas instrumentation fail which we intend for
// unsupported or unknown instructions. Offchain we might allow indeterminism and hence
// use the fallback weight for those instructions.
_ if matches!(self.determinism, Determinism::AllowIndeterminism) && w.fallback > 0 =>
w.fallback,
_ if matches!(self.determinism, Determinism::Relaxed) && w.fallback > 0 => w.fallback,
_ => return None,
};
Some(weight)
+62 -67
View File
@@ -529,7 +529,7 @@ fn instantiate_and_call_and_deposit_event() {
RuntimeOrigin::signed(ALICE),
wasm,
None,
Determinism::Deterministic
Determinism::Enforced
));
// Drop previous events
@@ -726,13 +726,8 @@ fn instantiate_unique_trie_id() {
ExtBuilder::default().existential_deposit(500).build().execute_with(|| {
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
Contracts::upload_code(
RuntimeOrigin::signed(ALICE),
wasm,
None,
Determinism::Deterministic,
)
.unwrap();
Contracts::upload_code(RuntimeOrigin::signed(ALICE), wasm, None, Determinism::Enforced)
.unwrap();
// Instantiate the contract and store its trie id for later comparison.
let addr = Contracts::bare_instantiate(
@@ -860,7 +855,7 @@ fn deploy_and_call_other_contract() {
.result
.unwrap()
.account_id;
Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Deterministic).unwrap();
Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Enforced).unwrap();
let callee_addr = Contracts::contract_address(
&caller_addr,
@@ -1011,7 +1006,7 @@ fn delegate_call() {
RuntimeOrigin::signed(ALICE),
callee_wasm,
Some(codec::Compact(100_000)),
Determinism::Deterministic,
Determinism::Enforced,
));
assert_ok!(Contracts::call(
@@ -1321,7 +1316,7 @@ fn destroy_contract_and_transfer_funds() {
ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
// Create code hash for bob to instantiate
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Deterministic).unwrap();
Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Enforced).unwrap();
// This deploys the BOB contract, which in turn deploys the CHARLIE contract during
// construction.
@@ -1431,7 +1426,7 @@ fn crypto_hashes() {
None,
params,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1473,7 +1468,7 @@ fn transfer_return_code() {
None,
vec![],
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1514,7 +1509,7 @@ fn call_return_code() {
None,
AsRef::<[u8]>::as_ref(&DJANGO).to_vec(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1548,7 +1543,7 @@ fn call_return_code() {
.cloned()
.collect(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1568,7 +1563,7 @@ fn call_return_code() {
.cloned()
.collect(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1587,7 +1582,7 @@ fn call_return_code() {
.cloned()
.collect(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1639,7 +1634,7 @@ fn instantiate_return_code() {
None,
callee_hash.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1655,7 +1650,7 @@ fn instantiate_return_code() {
None,
vec![0; 33],
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1670,7 +1665,7 @@ fn instantiate_return_code() {
None,
callee_hash.iter().chain(&1u32.to_le_bytes()).cloned().collect(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1685,7 +1680,7 @@ fn instantiate_return_code() {
None,
callee_hash.iter().chain(&2u32.to_le_bytes()).cloned().collect(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1772,7 +1767,7 @@ fn chain_extension_works() {
None,
input.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_eq!(TestExtension::last_seen_buffer(), input);
assert_eq!(result.result.unwrap().data, input);
@@ -1786,7 +1781,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 0, func_id: 1, extra: &[] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1802,7 +1797,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[0] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_ok!(result.result);
let gas_consumed = result.gas_consumed;
@@ -1814,7 +1809,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[42] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_ok!(result.result);
assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 42);
@@ -1826,7 +1821,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 0, func_id: 2, extra: &[95] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_ok!(result.result);
assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 95);
@@ -1840,7 +1835,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 0, func_id: 3, extra: &[] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1858,7 +1853,7 @@ fn chain_extension_works() {
None,
ExtensionInput { extension_id: 1, func_id: 0, extra: &[] }.into(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -1919,7 +1914,7 @@ fn chain_extension_temp_storage_works() {
None,
input.clone(),
false,
Determinism::Deterministic
Determinism::Enforced
)
.result
);
@@ -2480,7 +2475,7 @@ fn reinstrument_does_charge() {
None,
zero.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert!(!result0.result.unwrap().did_revert());
@@ -2492,7 +2487,7 @@ fn reinstrument_does_charge() {
None,
zero.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert!(!result1.result.unwrap().did_revert());
@@ -2514,7 +2509,7 @@ fn reinstrument_does_charge() {
None,
zero.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert!(!result2.result.unwrap().did_revert());
assert!(result2.gas_consumed.ref_time() > result1.gas_consumed.ref_time());
@@ -2553,7 +2548,7 @@ fn debug_message_works() {
None,
vec![],
true,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result.result, Ok(_));
@@ -2589,7 +2584,7 @@ fn debug_message_logging_disabled() {
None,
vec![],
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_matches!(result.result, Ok(_));
// the dispatchables always run without debugging
@@ -2625,7 +2620,7 @@ fn debug_message_invalid_utf8() {
None,
vec![],
true,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_ok!(result.result);
assert!(result.debug_message.is_empty());
@@ -2684,7 +2679,7 @@ fn gas_estimation_nested_call_fixed_limit() {
None,
input.clone(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
assert_ok!(&result.result);
@@ -2701,7 +2696,7 @@ fn gas_estimation_nested_call_fixed_limit() {
Some(result.storage_deposit.charge_or_zero()),
input,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
);
@@ -2760,7 +2755,7 @@ fn gas_estimation_call_runtime() {
None,
call.encode(),
false,
Determinism::Deterministic,
Determinism::Enforced,
);
// contract encodes the result of the dispatch runtime
let outcome = u32::decode(&mut result.result.unwrap().data.as_ref()).unwrap();
@@ -2777,7 +2772,7 @@ fn gas_estimation_call_runtime() {
None,
call.encode(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
);
@@ -2840,7 +2835,7 @@ fn gas_call_runtime_reentrancy_guarded() {
None,
call.encode(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -2902,7 +2897,7 @@ fn ecdsa_recover() {
None,
params,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -2926,7 +2921,7 @@ fn upload_code_works() {
RuntimeOrigin::signed(ALICE),
wasm,
Some(codec::Compact(1_000)),
Determinism::Deterministic,
Determinism::Enforced,
));
assert!(<CodeStorage<Test>>::contains_key(code_hash));
@@ -2966,7 +2961,7 @@ fn upload_code_limit_too_low() {
RuntimeOrigin::signed(ALICE),
wasm,
Some(codec::Compact(100)),
Determinism::Deterministic
Determinism::Enforced
),
<Error<Test>>::StorageDepositLimitExhausted,
);
@@ -2990,7 +2985,7 @@ fn upload_code_not_enough_balance() {
RuntimeOrigin::signed(ALICE),
wasm,
Some(codec::Compact(1_000)),
Determinism::Deterministic
Determinism::Enforced
),
<Error<Test>>::StorageDepositNotEnoughFunds,
);
@@ -3013,7 +3008,7 @@ fn remove_code_works() {
RuntimeOrigin::signed(ALICE),
wasm,
Some(codec::Compact(1_000)),
Determinism::Deterministic,
Determinism::Enforced,
));
assert!(<CodeStorage<Test>>::contains_key(code_hash));
@@ -3068,7 +3063,7 @@ fn remove_code_wrong_origin() {
RuntimeOrigin::signed(ALICE),
wasm,
Some(codec::Compact(1_000)),
Determinism::Deterministic,
Determinism::Enforced,
));
assert_noop!(
@@ -3538,7 +3533,7 @@ fn set_code_extrinsic() {
RuntimeOrigin::signed(ALICE),
new_wasm,
None,
Determinism::Deterministic
Determinism::Enforced
));
// Drop previous events
@@ -3658,7 +3653,7 @@ fn contract_reverted() {
RuntimeOrigin::signed(ALICE),
wasm.clone(),
None,
Determinism::Deterministic
Determinism::Enforced
));
// Calling extrinsic: revert leads to an error
@@ -3746,7 +3741,7 @@ fn contract_reverted() {
None,
input,
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -3766,7 +3761,7 @@ fn code_rejected_error_works() {
RuntimeOrigin::signed(ALICE),
wasm.clone(),
None,
Determinism::Deterministic
Determinism::Enforced
),
<Error<Test>>::CodeRejected,
);
@@ -3792,7 +3787,7 @@ fn code_rejected_error_works() {
RuntimeOrigin::signed(ALICE),
wasm.clone(),
None,
Determinism::Deterministic
Determinism::Enforced
),
<Error<Test>>::CodeRejected,
);
@@ -3842,7 +3837,7 @@ fn set_code_hash() {
RuntimeOrigin::signed(ALICE),
new_wasm.clone(),
None,
Determinism::Deterministic
Determinism::Enforced
));
System::reset_events();
@@ -3856,7 +3851,7 @@ fn set_code_hash() {
None,
new_code_hash.as_ref().to_vec(),
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -3871,7 +3866,7 @@ fn set_code_hash() {
None,
vec![],
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -4268,7 +4263,7 @@ fn cannot_instantiate_indeterministic_code() {
RuntimeOrigin::signed(ALICE),
wasm.clone(),
None,
Determinism::Deterministic
Determinism::Enforced
),
<Error<Test>>::CodeRejected,
);
@@ -4278,7 +4273,7 @@ fn cannot_instantiate_indeterministic_code() {
RuntimeOrigin::signed(ALICE),
wasm,
None,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
));
assert_err_ignore_postinfo!(
Contracts::instantiate(
@@ -4332,7 +4327,7 @@ fn cannot_instantiate_indeterministic_code() {
None,
code_hash.encode(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result,
<Error<Test>>::Indeterministic,
@@ -4348,7 +4343,7 @@ fn cannot_instantiate_indeterministic_code() {
None,
code_hash.encode(),
false,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
)
.result,
<Error<Test>>::Indeterministic,
@@ -4368,7 +4363,7 @@ fn cannot_set_code_indeterministic_code() {
RuntimeOrigin::signed(ALICE),
wasm,
None,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
));
// Create the contract that will call `seal_set_code_hash`
@@ -4396,7 +4391,7 @@ fn cannot_set_code_indeterministic_code() {
None,
code_hash.encode(),
false,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
)
.result,
<Error<Test>>::Indeterministic,
@@ -4416,7 +4411,7 @@ fn delegate_call_indeterministic_code() {
RuntimeOrigin::signed(ALICE),
wasm,
None,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
));
// Create the contract that will call `seal_delegate_call`
@@ -4444,7 +4439,7 @@ fn delegate_call_indeterministic_code() {
None,
code_hash.encode(),
false,
Determinism::Deterministic,
Determinism::Enforced,
)
.result,
<Error<Test>>::Indeterministic,
@@ -4460,7 +4455,7 @@ fn delegate_call_indeterministic_code() {
None,
code_hash.encode(),
false,
Determinism::AllowIndeterminism,
Determinism::Relaxed,
)
.result
);
@@ -4499,7 +4494,7 @@ fn reentrance_count_works_with_call() {
None,
input,
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -4538,7 +4533,7 @@ fn reentrance_count_works_with_delegated_call() {
None,
input,
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -4590,7 +4585,7 @@ fn account_reentrance_count_works() {
None,
contract_addr.encode(),
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
@@ -4603,7 +4598,7 @@ fn account_reentrance_count_works() {
None,
another_contract_addr.encode(),
true,
Determinism::Deterministic,
Determinism::Enforced,
)
.result
.unwrap();
+9 -13
View File
@@ -127,7 +127,7 @@ pub enum Determinism {
/// allowed.
///
/// Dispatchables always use this mode in order to make on-chain execution deterministic.
Deterministic,
Enforced,
/// Allow calling or uploading an indeterministic code.
///
/// This is only possible when calling into `pallet-contracts` directly via
@@ -136,7 +136,7 @@ pub enum Determinism {
/// # Note
///
/// **Never** use this mode for on-chain execution.
AllowIndeterminism,
Relaxed,
}
impl ExportedFunction {
@@ -225,7 +225,7 @@ impl<T: Config> PrefabWasmModule<T> {
let engine = Engine::new(&config);
let module = Module::new(&engine, code)?;
let mut store = Store::new(&engine, host_state);
let mut linker = Linker::new();
let mut linker = Linker::new(&engine);
E::define(
&mut store,
&mut linker,
@@ -329,7 +329,7 @@ impl<T: Config> Executable<T> for PrefabWasmModule<T> {
log::debug!(target: "runtime::contracts", "failed to instantiate code: {}", msg);
Error::<T>::CodeRejected
})?;
store.state_mut().set_memory(memory);
store.data_mut().set_memory(memory);
let exported_func = instance
.get_export(&store, function.identifier())
@@ -346,7 +346,7 @@ impl<T: Config> Executable<T> for PrefabWasmModule<T> {
let result = exported_func.call(&mut store, &[], &mut []);
store.into_state().to_execution_result(result)
store.into_data().to_execution_result(result)
}
fn code_hash(&self) -> &CodeHash<T> {
@@ -358,7 +358,7 @@ impl<T: Config> Executable<T> for PrefabWasmModule<T> {
}
fn is_deterministic(&self) -> bool {
matches!(self.determinism, Determinism::Deterministic)
matches!(self.determinism, Determinism::Enforced)
}
}
@@ -653,7 +653,7 @@ mod tests {
wasm,
&schedule,
ALICE,
Determinism::Deterministic,
Determinism::Enforced,
TryInstantiate::Instantiate,
)
.map_err(|err| err.0)?
@@ -3058,12 +3058,8 @@ mod tests {
let schedule = crate::Schedule::<Test>::default();
#[cfg(not(feature = "runtime-benchmarks"))]
assert_err!(execute(CODE_RANDOM, vec![], MockExt::default()), <Error<Test>>::CodeRejected);
self::prepare::reinstrument::<runtime::Env, Test>(
&wasm,
&schedule,
Determinism::Deterministic,
)
.unwrap();
self::prepare::reinstrument::<runtime::Env, Test>(&wasm, &schedule, Determinism::Enforced)
.unwrap();
}
/// This test check that an unstable interface cannot be deployed. In case of runtime
+10 -60
View File
@@ -155,52 +155,6 @@ impl<'a, T: Config> ContractModule<'a, T> {
Ok(())
}
/// Ensures that no floating point types are in use.
fn ensure_no_floating_types(&self) -> Result<(), &'static str> {
if let Some(global_section) = self.module.global_section() {
for global in global_section.entries() {
match global.global_type().content_type() {
ValueType::F32 | ValueType::F64 =>
return Err("use of floating point type in globals is forbidden"),
_ => {},
}
}
}
if let Some(code_section) = self.module.code_section() {
for func_body in code_section.bodies() {
for local in func_body.locals() {
match local.value_type() {
ValueType::F32 | ValueType::F64 =>
return Err("use of floating point type in locals is forbidden"),
_ => {},
}
}
}
}
if let Some(type_section) = self.module.type_section() {
for wasm_type in type_section.types() {
match wasm_type {
Type::Function(func_type) => {
let return_type = func_type.results().get(0);
for value_type in func_type.params().iter().chain(return_type) {
match value_type {
ValueType::F32 | ValueType::F64 =>
return Err(
"use of floating point type in function types is forbidden",
),
_ => {},
}
}
},
}
}
}
Ok(())
}
/// Ensure that no function exists that has more parameters than allowed.
fn ensure_parameter_limit(&self, limit: u32) -> Result<(), &'static str> {
let type_section = if let Some(type_section) = self.module.type_section() {
@@ -411,10 +365,9 @@ where
memory64: false,
extended_const: false,
component_model: false,
// This is not our only defense: We check for float types later in the preparation
// process. Additionally, all instructions explicitly need to have weights assigned
// This is not our only defense: All instructions explicitly need to have weights assigned
// or the deployment will fail. We have none assigned for float instructions.
deterministic_only: matches!(determinism, Determinism::Deterministic),
floats: matches!(determinism, Determinism::Relaxed),
mutable_global: false,
saturating_float_to_int: false,
sign_extension: false,
@@ -422,6 +375,7 @@ where
multi_value: false,
reference_types: false,
simd: false,
memory_control: false,
})
.validate_all(original_code)
.map_err(|err| {
@@ -439,10 +393,6 @@ where
contract_module.ensure_parameter_limit(schedule.limits.parameters)?;
contract_module.ensure_br_table_size_limit(schedule.limits.br_table_size)?;
if matches!(determinism, Determinism::Deterministic) {
contract_module.ensure_no_floating_types()?;
}
// We disallow importing `gas` function here since it is treated as implementation detail.
let disallowed_imports = [b"gas".as_ref()];
let memory_limits =
@@ -608,7 +558,7 @@ pub mod benchmarking {
deposit: Default::default(),
refcount: 0,
}),
determinism: Determinism::Deterministic,
determinism: Determinism::Enforced,
})
}
}
@@ -682,7 +632,7 @@ mod tests {
wasm,
&schedule,
ALICE,
Determinism::Deterministic,
Determinism::Enforced,
TryInstantiate::Instantiate,
);
assert_matches::assert_matches!(r.map_err(|(_, msg)| msg), $($expected)*);
@@ -704,7 +654,7 @@ mod tests {
)
(func (export "deploy"))
)"#,
Err("gas instrumentation failed")
Err("validation of new code failed")
);
mod functions {
@@ -1214,7 +1164,7 @@ mod tests {
(func (export "deploy"))
)
"#,
Err("use of floating point type in globals is forbidden")
Err("validation of new code failed")
);
prepare_test!(
@@ -1226,7 +1176,7 @@ mod tests {
(func (export "deploy"))
)
"#,
Err("use of floating point type in locals is forbidden")
Err("validation of new code failed")
);
prepare_test!(
@@ -1238,7 +1188,7 @@ mod tests {
(func (export "deploy"))
)
"#,
Err("use of floating point type in function types is forbidden")
Err("validation of new code failed")
);
prepare_test!(
@@ -1250,7 +1200,7 @@ mod tests {
(func (export "deploy"))
)
"#,
Err("use of floating point type in function types is forbidden")
Err("validation of new code failed")
);
}
}
@@ -482,11 +482,7 @@ impl<'a, E: Ext + 'a> Runtime<'a, E> {
Err(wasmi::Error::Trap(trap)) => {
// If we encoded a reason then it is some abort generated by a host function.
// Otherwise the trap came from the contract.
let reason: TrapReason = *trap
.into_host()
.ok_or(Error::<E::T>::ContractTrapped)?
.downcast()
.expect("`TrapReason` is the only type we use to encode host errors; qed");
let reason: TrapReason = trap.downcast().ok_or(Error::<E::T>::ContractTrapped)?;
match reason {
Return(ReturnData { flags, data }) => {
let flags =
+1185 -1177
View File
File diff suppressed because it is too large Load Diff