Skip to content

Commit

Permalink
Add counter to wasmv1 and test interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon committed Jul 16, 2024
1 parent 3cb1caf commit ce91892
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ impl InterfaceClone for TestInterface {
}

impl Interface for TestInterface {
fn increment_recursion_counter(&self) -> Result<()> {
println!("Increment recursion counter");
Ok(())
}

fn decrement_recursion_counter(&self) -> Result<()> {
println!("Decrement recursion counter");
Ok(())
}

fn init_call(&self, address: &str, raw_coins: u64) -> Result<Vec<u8>> {
println!("Init call to {}, with {} coins", address, raw_coins);
Ok(vec![])
Expand Down
22 changes: 21 additions & 1 deletion src/wasmv1_execution/abi/abis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ fn abi_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<i32, W
let remaining_gas = handler.get_remaining_gas();
let interface = handler.exec_env.get_interface();
let module = helper_get_module(interface, bytecode, remaining_gas)?;
interface.increment_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e))
})?;
let response = crate::execution::run_function(
interface,
module,
Expand All @@ -161,6 +164,9 @@ fn abi_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<i32, W
handler.get_gas_costs().clone(),
)
.map_err(|err| WasmV1Error::RuntimeError(format!("Could not run function: {}", err)))?;
interface.decrement_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!("Could not decrement recursion counter: {}", e))
})?;
handler.set_remaining_gas(response.remaining_gas);
let interface = handler.exec_env.get_interface();
interface.finish_call().map_err(|err| {
Expand Down Expand Up @@ -201,7 +207,9 @@ fn abi_local_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<
let remaining_gas = handler.get_remaining_gas();
let interface = handler.exec_env.get_interface();
let module = helper_get_module(interface, bytecode.clone(), remaining_gas)?;

interface.increment_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e))
})?;
let response = crate::execution::run_function(
interface,
module,
Expand All @@ -211,6 +219,9 @@ fn abi_local_call(store_env: FunctionEnvMut<ABIEnv>, arg_offset: i32) -> Result<
handler.get_gas_costs().clone(),
)
.map_err(|err| WasmV1Error::RuntimeError(format!("Could not run function: {}", err)))?;
interface.decrement_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!("Could not decrement recursion counter: {}", e))
})?;
handler.set_remaining_gas(response.remaining_gas);

#[cfg(feature = "execution-trace")]
Expand Down Expand Up @@ -1022,6 +1033,9 @@ fn abi_local_execution(
let module = helper_get_tmp_module(handler, req.bytecode.clone(), remaining_gas)?;

let interface = handler.exec_env.get_interface();
interface.increment_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!("Could not increment recursion counter: {}", e))
})?;
match crate::execution::run_function(
interface,
module,
Expand All @@ -1031,6 +1045,12 @@ fn abi_local_execution(
handler.get_gas_costs().clone(),
) {
Ok(response) => {
interface.decrement_recursion_counter().map_err(|e| {
WasmV1Error::RuntimeError(format!(
"Could not decrement recursion counter: {}",
e
))
})?;
handler.set_remaining_gas(response.remaining_gas);

#[cfg(feature = "execution-trace")]
Expand Down

0 comments on commit ce91892

Please sign in to comment.