Skip to content

Commit

Permalink
fix: remove unused dynamiclink features
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed Apr 12, 2023
1 parent 6ce2d99 commit 0c2320c
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 174 deletions.
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 0c2320c

Please sign in to comment.