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 f0bce72
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 117 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
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 f0bce72

Please sign in to comment.