Skip to content

Commit

Permalink
fix: Remove unused dynamiclink features (#286)
Browse files Browse the repository at this point in the history
* fix: remove unused dynamiclink features

* fix: fix tests for callee contracts
  • Loading branch information
loloicci committed Apr 14, 2023
1 parent 6ce2d99 commit cb1a355
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 226 deletions.
27 changes: 6 additions & 21 deletions contracts/dynamic-callee-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ fn callable_point_pong_works() {
let export_index = 0;
assert_eq!("pong".to_string(), required_exports[export_index].0);
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"pong",
&[env_region_ptr.into(), param_region_ptr.into()],
)
.call_function("pong", &[env_region_ptr.into(), param_region_ptr.into()])
.unwrap();
assert_eq!(call_result.len(), 1);

Expand Down Expand Up @@ -124,8 +120,7 @@ fn callable_point_pong_with_struct_works() {
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
.call_function(
"pong_with_struct",
&[env_region_ptr.into(), param_region_ptr.into()],
)
Expand Down Expand Up @@ -155,8 +150,7 @@ fn callable_point_pong_with_tuple_works() {
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
.call_function(
"pong_with_tuple",
&[env_region_ptr.into(), param_region_ptr.into()],
)
Expand Down Expand Up @@ -189,8 +183,7 @@ fn callable_point_pong_with_tuple_takes_2_args_works() {
required_exports[export_index].0
);
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
.call_function(
"pong_with_tuple_takes_2_args",
&[
env_region_ptr.into(),
Expand Down Expand Up @@ -221,11 +214,7 @@ fn callable_point_pong_env_works() {
let export_index = 4;
assert_eq!("pong_env".to_string(), required_exports[export_index].0);
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"pong_env",
&[env_region_ptr.into()],
)
.call_function("pong_env", &[env_region_ptr.into()])
.unwrap();
assert_eq!(call_result.len(), 1);

Expand All @@ -247,11 +236,7 @@ fn callable_point_do_panic_raises_runtime_error() {
.set_serialized_env(&to_vec(&mock_env()).unwrap());
let export_index = 5;
assert_eq!("do_panic".to_string(), required_exports[export_index].0);
let call_result = instance.call_function_strict(
&required_exports[export_index].1,
"do_panic",
&[env_region_ptr.into()],
);
let call_result = instance.call_function("do_panic", &[env_region_ptr.into()]);

match call_result.unwrap_err() {
VmError::RuntimeErr { msg, .. } => {
Expand Down
20 changes: 4 additions & 16 deletions contracts/number/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ fn callable_point_add_works() {
// Before solving #213, it issues an error.
// This is because `add` panics without number in deps.storage.
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"add",
&[env_region_ptr.into(), param_region_ptr.into()],
)
.call_function("add", &[env_region_ptr.into(), param_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand All @@ -103,11 +99,7 @@ fn callable_point_sub_works() {
// Before solving #213, it issues an error.
// This is because `sub` panics without number in deps.storage.
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"sub",
&[env_region_ptr.into(), param_region_ptr.into()],
)
.call_function("sub", &[env_region_ptr.into(), param_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand All @@ -130,11 +122,7 @@ fn callable_point_mul_works() {
// Before solving #213, it issues an error.
// This is because `mul` panics without number in deps.storage.
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"mul",
&[env_region_ptr.into(), param_region_ptr.into()],
)
.call_function("mul", &[env_region_ptr.into(), param_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand All @@ -153,7 +141,7 @@ fn callable_point_number_works() {
// Before solving #213, it issues an error.
// This is because `number` panics without number in deps.storage.
let call_result = instance
.call_function_strict(&required_exports[0].1, "number", &[env_region_ptr.into()])
.call_function("number", &[env_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand Down
18 changes: 3 additions & 15 deletions contracts/simple-callee/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ fn callable_point_succeed_works() {

// check succeed
instance
.call_function_strict(
&required_exports[export_index].1,
"succeed",
&[env_region_ptr.into()],
)
.call_function("succeed", &[env_region_ptr.into()])
.unwrap();
}

Expand All @@ -95,11 +91,7 @@ fn callable_point_succeed_readonly_works() {

// check succeed_readonly
instance
.call_function_strict(
&required_exports[export_index].1,
"succeed_readonly",
&[env_region_ptr.into()],
)
.call_function("succeed_readonly", &[env_region_ptr.into()])
.unwrap();
}

Expand All @@ -115,11 +107,7 @@ fn callable_fail_fails() {

// check unreachable
let call_result = instance
.call_function_strict(
&required_exports[export_index].1,
"fail",
&[env_region_ptr.into()],
)
.call_function("fail", &[env_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand Down
17 changes: 0 additions & 17 deletions packages/vm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ use thiserror::Error;
use cosmwasm_std::{Binary, ContractResult, SystemResult};
#[cfg(feature = "iterator")]
use cosmwasm_std::{Order, Record};
use wasmer::Module;

use crate::environment::Environment;
use crate::{FunctionMetadata, WasmerVal};

/// A structure that represents gas cost to be deducted from the remaining gas.
/// This is always needed when computations are performed outside of
Expand Down Expand Up @@ -137,19 +133,6 @@ pub trait Storage {
pub trait BackendApi: Copy + Clone + Send {
fn canonical_address(&self, human: &str) -> BackendResult<Vec<u8>>;
fn human_address(&self, canonical: &[u8]) -> BackendResult<String>;
// TODO: remove contract_call and get_wasmer_module after solving #273
fn contract_call<A, S, Q>(
&self,
caller_env: &Environment<A, S, Q>,
contract_addr: &str,
target_info: &FunctionMetadata,
args: &[WasmerVal],
) -> BackendResult<Box<[WasmerVal]>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static;
fn get_wasmer_module(&self, contract_addr: &str) -> BackendResult<Module>;
fn call_callable_point(
&self,
contract_addr: &str,
Expand Down
43 changes: 0 additions & 43 deletions packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,49 +377,6 @@ impl<A: BackendApi, S: Storage, Q: Querier> Environment<A, S, Q> {
})
}

// TODO: remove it after make wasmvm not using this
pub fn remove_latest_dynamic_call_trace(&self) {
self.with_context_data_mut(|ctx| {
ctx.dynamic_callstack.pop();
})
}

// try_pass_callstack will be called through wasmvm.
// checking between the previous callers in the virtual_callstack and target.
// if it failed, it will be returned ReEntrancyErr.
// TODO: remove it after make wasmvm not using this
pub fn try_pass_callstack<A2, S2, Q2>(
&self,
target: &mut Environment<A2, S2, Q2>,
) -> VmResult<()>
where
A2: BackendApi + 'static,
S2: Storage + 'static,
Q2: Querier + 'static,
{
//TODO::need check the race condition when calling the contract oneself(recursive).
self.with_context_data_mut(|self_ctx| {
target.with_context_data_mut(|target_ctx| {
let target_contract_env: Env = match &target_ctx.serialized_env {
Some(env) => from_slice(env, DESERIALIZATION_LIMIT),
None => Err(VmError::uninitialized_context_data("serialized_env")),
}?;

match self_ctx
.dynamic_callstack
.iter()
.find(|x| **x == target_contract_env.contract.address)
{
Some(_) => Err(VmError::re_entrancy_err()),
None => {
target_ctx.dynamic_callstack = self_ctx.dynamic_callstack.clone();
Ok(())
}
}
})
})
}

/// this function sets callstack to environment and checks it is not re-entrance
pub fn set_dynamic_callstack(&self, callstack: Vec<Addr>) -> VmResult<()> {
// check callstack length
Expand Down
15 changes: 1 addition & 14 deletions packages/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::ptr::NonNull;
use std::sync::Mutex;

use wasmer::{
Exports, Function, FunctionType, ImportObject, Instance as WasmerInstance, Module, Val,
};
use wasmer::{Exports, Function, ImportObject, Instance as WasmerInstance, Module, Val};

use crate::backend::{Backend, BackendApi, Querier, Storage};
use crate::capabilities::required_capabilities_from_module;
Expand Down Expand Up @@ -406,17 +404,6 @@ where
Ok(())
}

/// Calls a function exported by the instance.
/// TODO: remove after make wasmvm not using this
pub fn call_function_strict(
&self,
_type: &FunctionType,
name: &str,
args: &[Val],
) -> VmResult<Box<[Val]>> {
self.env.call_function(name, args)
}

/// Calls a function exported by the instance.
pub fn call_function(&self, name: &str, args: &[Val]) -> VmResult<Box<[Val]>> {
self.env.call_function(name, args)
Expand Down
101 changes: 1 addition & 100 deletions packages/vm/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,13 @@ use wasmer::Module;

use super::querier::MockQuerier;
use super::storage::MockStorage;
use crate::environment::Environment;
use crate::instance::Instance;
use crate::{
read_region_vals_from_env, to_vec, write_value_to_env, Backend, BackendApi, BackendError,
BackendResult, GasInfo, Querier, Storage,
};
use crate::{FunctionMetadata, WasmerVal};
use crate::{Backend, BackendApi, BackendError, BackendResult, GasInfo};

pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
const DEFAULT_GAS_COST_HUMANIZE: u64 = 44;
const DEFAULT_GAS_COST_CANONICALIZE: u64 = 55;

// calculated by https://github.com/line/lbm-sdk/blob/6411ce42259b4774ce1864f452624651299b9683/x/wasm/types/params.go#L17-L38
const DEFAULT_GAS_COST_INSTANTIATE: u64 = 140000000 * 60000;

// comes from https://github.com/line/lbm-sdk/blob/6411ce42259b4774ce1864f452624651299b9683/x/wasm/keeper/gas_register.go#L13-L18
const DEFAULT_GAS_COST_MESSAGE: u64 = 0;
const MAX_REGIONS_LENGTH: usize = 64 * 1024 * 1024;

/// All external requirements that can be injected for unit tests.
/// It sets the given balance for the contract itself, nothing else
pub fn mock_backend(contract_balance: &[Coin]) -> Backend<MockApi, MockStorage, MockQuerier> {
Expand Down Expand Up @@ -202,93 +190,6 @@ impl BackendApi for MockApi {
(result, gas_info)
}

fn contract_call<A, S, Q>(
&self,
caller_env: &Environment<A, S, Q>,
contract_addr: &str,
func_info: &FunctionMetadata,
args: &[WasmerVal],
) -> BackendResult<Box<[WasmerVal]>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
let mut gas_info = GasInfo::new(0, 0);
INSTANCE_CACHE.with(|lock| {
let cache = lock.read().unwrap();
match cache.get(contract_addr) {
Some(callee_instance_cell) => {
let datas =
read_region_vals_from_env(caller_env, args, MAX_REGIONS_LENGTH, false)
.unwrap();
let input_length: u64 = datas
.iter()
.fold(0, |sum, x| sum + x.len())
.try_into()
.unwrap();
let instantiate_cost: u64 =
DEFAULT_GAS_COST_INSTANTIATE + DEFAULT_GAS_COST_MESSAGE * input_length;

let callee_instance = callee_instance_cell.borrow_mut();
gas_info.cost += instantiate_cost;

let mut arg_region_ptrs = Vec::<WasmerVal>::new();
let env_ptr =
write_value_to_env(&callee_instance.env, &to_vec(&mock_env()).unwrap())
.unwrap();
arg_region_ptrs.push(env_ptr);
for data in datas {
arg_region_ptrs
.push(write_value_to_env(&callee_instance.env, &data).unwrap());
}

let call_ret = match callee_instance.call_function_strict(
&func_info.signature,
&func_info.name,
&arg_region_ptrs,
) {
Ok(rets) => {
let ret_datas = read_region_vals_from_env(
&callee_instance.env,
&rets,
MAX_REGIONS_LENGTH,
true,
)
.unwrap();
let mut ret_region_ptrs = Vec::<WasmerVal>::new();
for data in ret_datas {
ret_region_ptrs.push(write_value_to_env(caller_env, &data).unwrap())
}
Ok(ret_region_ptrs.into_boxed_slice())
}
Err(e) => Err(BackendError::dynamic_link_err(e.to_string())),
};
gas_info.cost += callee_instance.create_gas_report().used_internally;
(call_ret, gas_info)
}
None => (
Err(BackendError::dynamic_link_err("cannot found contract")),
gas_info,
),
}
})
}

fn get_wasmer_module(&self, contract_addr: &str) -> BackendResult<Module> {
let gas_info = GasInfo::new(0, 0);
MODULE_CACHE.with(|lock| {
let cache = lock.read().unwrap();
match cache.get(contract_addr) {
Some(module) => (Ok(module.borrow().clone()), gas_info),
None => (
Err(BackendError::dynamic_link_err("cannot found checksum")),
gas_info,
),
}
})
}

fn call_callable_point(
&self,
_contract_addr: &str,
Expand Down

0 comments on commit cb1a355

Please sign in to comment.