integration: instantiate the module in prepare

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-04-16 20:06:34 +02:00
parent 8a6d43bcd3
commit 796d0c19bb
3 changed files with 261 additions and 21 deletions
+15 -15
View File
@@ -60,8 +60,8 @@ mod tests {
let input = Fibonacci::fib3Call::new((parameter,)).abi_encode();
let state = State::new(input);
let (instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &instance, export);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
@@ -75,13 +75,13 @@ mod tests {
fn flipper() {
let code = crate::compile_blob("Flipper", include_str!("../contracts/flipper.sol"));
let state = State::new(0xcde4efa9u32.to_be_bytes().to_vec());
let (instance, export) = mock_runtime::prepare(&code, None);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &instance, export);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
assert_eq!(state.storage[&U256::ZERO], U256::try_from(1).unwrap());
let state = crate::mock_runtime::call(state, &instance, export);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
assert_eq!(state.storage[&U256::ZERO], U256::ZERO);
}
@@ -105,8 +105,8 @@ mod tests {
let input = TestSha3::testCall::new((param.to_string(),)).abi_encode();
let state = State::new(input);
let (instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &instance, export);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
@@ -133,8 +133,8 @@ mod tests {
input.extend_from_slice(&param.to_be_bytes::<32>());
let state = State::new(input);
let (instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &instance, export);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
@@ -153,8 +153,8 @@ mod tests {
input.extend_from_slice(&param.to_be_bytes::<32>());
let state = State::new(input);
let (instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &instance, export);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let state = crate::mock_runtime::call(state, &mut instance, export);
assert_eq!(state.output.flags, 0);
@@ -175,10 +175,10 @@ mod tests {
include_str!("../contracts/MSize.sol"),
false,
);
let (instance, export) = mock_runtime::prepare(&code, None);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let input = MSize::mSizeCall::new(()).abi_encode();
let state = crate::mock_runtime::call(State::new(input), &instance, export);
let state = crate::mock_runtime::call(State::new(input), &mut instance, export);
assert_eq!(state.output.flags, 0);
@@ -201,10 +201,10 @@ mod tests {
include_str!("../contracts/MSize.sol"),
false,
);
let (instance, export) = mock_runtime::prepare(&code, None);
let (mut instance, export) = mock_runtime::prepare(&code, None);
let input = MSize::mStore100Call::new(()).abi_encode();
let state = crate::mock_runtime::call(State::new(input), &instance, export);
let state = crate::mock_runtime::call(State::new(input), &mut instance, export);
assert_eq!(state.output.flags, 0);
+7 -6
View File
@@ -5,8 +5,8 @@ use std::collections::HashMap;
use alloy_primitives::{Keccak256, U256};
use parity_scale_codec::Encode;
use polkavm::{
Caller, Config, Engine, ExportIndex, GasMeteringKind, InstancePre, Linker, Module,
ModuleConfig, ProgramBlob, Trap,
Caller, Config, Engine, ExportIndex, GasMeteringKind, Instance, Linker, Module, ModuleConfig,
ProgramBlob, Trap,
};
#[derive(Default, Clone, Debug)]
@@ -192,7 +192,7 @@ fn link_host_functions(engine: &Engine) -> Linker<State> {
linker
}
pub fn prepare(code: &[u8], config: Option<Config>) -> (InstancePre<State>, ExportIndex) {
pub fn prepare(code: &[u8], config: Option<Config>) -> (Instance<State>, ExportIndex) {
let blob = ProgramBlob::parse(code).unwrap();
let engine = Engine::new(&config.unwrap_or_default()).unwrap();
@@ -205,11 +205,12 @@ pub fn prepare(code: &[u8], config: Option<Config>) -> (InstancePre<State>, Expo
let func = link_host_functions(&engine)
.instantiate_pre(&module)
.unwrap();
let instance = func.instantiate().unwrap();
(func, export)
(instance, export)
}
pub fn call(mut state: State, on: &InstancePre<State>, export: ExportIndex) -> State {
pub fn call(mut state: State, on: &mut Instance<State>, export: ExportIndex) -> State {
state.reset_output();
let mut state_args = polkavm::StateArgs::default();
@@ -219,7 +220,7 @@ pub fn call(mut state: State, on: &InstancePre<State>, export: ExportIndex) -> S
init_logs();
match on.instantiate().unwrap().call(state_args, call_args) {
match on.call(state_args, call_args) {
Err(polkavm::ExecutionError::Trap(_)) => state,
Err(other) => panic!("unexpected error: {other}"),
Ok(_) => panic!("unexpected return"),