Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass env as the second arg of dynamic linked callee function #234

Merged
merged 7 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions contracts/dynamic-callee-contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
callable_point, dynamic_link, entry_point, Addr, Contract, Deps, DepsMut, Env, GlobalApi,
MessageInfo, Response,
callable_point, dynamic_link, entry_point, Addr, Contract, Deps, DepsMut, Env, MessageInfo,
Response,
};
use serde::{Deserialize, Serialize};

Expand All @@ -20,7 +20,7 @@ pub fn instantiate(
}

#[callable_point]
fn pong(_deps: Deps, x: u64) -> u64 {
fn pong(_deps: Deps, _env: Env, x: u64) -> u64 {
x + 1
}

Expand All @@ -29,49 +29,53 @@ pub struct ExampleStruct {
pub str_field: String,
pub u64_field: u64,
}

#[callable_point]
fn pong_with_struct(_deps: Deps, example: ExampleStruct) -> ExampleStruct {
fn pong_with_struct(_deps: Deps, _env: Env, example: ExampleStruct) -> ExampleStruct {
ExampleStruct {
str_field: example.str_field + " world",
u64_field: example.u64_field + 1,
}
}

#[callable_point]
fn pong_with_tuple(_deps: Deps, input: (String, i32)) -> (String, i32) {
fn pong_with_tuple(_deps: Deps, _env: Env, input: (String, i32)) -> (String, i32) {
(input.0 + " world", input.1 + 1)
}

#[callable_point]
fn pong_with_tuple_takes_2_args(_deps: Deps, input1: String, input2: i32) -> (String, i32) {
fn pong_with_tuple_takes_2_args(
_deps: Deps,
_env: Env,
input1: String,
input2: i32,
) -> (String, i32) {
(input1 + " world", input2 + 1)
}

#[callable_point]
fn pong_env(_deps: Deps) -> Env {
GlobalApi::env()
fn pong_env(_deps: Deps, env: Env) -> Env {
env
}

#[callable_point]
fn do_panic(_deps: Deps) {
fn do_panic(_deps: Deps, _env: Env) {
panic!();
}

#[derive(Contract)]
struct Me {
struct Caller {
address: Addr,
}

#[dynamic_link(Me)]
#[dynamic_link(Caller)]
trait ReEntrance: Contract {
fn should_never_be_called(&self);
}

#[callable_point]
fn reentrancy(_deps: Deps, address: Addr) {
let me = Me { address };
me.should_never_be_called()
fn reentrancy(_deps: Deps, _env: Env, address: Addr) {
let caller = Caller { address };
caller.should_never_be_called()
}

// And declare a custom Error variant for the ones where you will want to make use of it
Expand Down
53 changes: 40 additions & 13 deletions contracts/dynamic-callee-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ static CONTRACT_CALLEE: &[u8] =

fn required_exports() -> Vec<(String, FunctionType)> {
vec![
(String::from("pong"), ([Type::I32], [Type::I32]).into()),
(
String::from("pong"),
([Type::I32, Type::I32], [Type::I32]).into(),
),
(
String::from("pong_with_struct"),
([Type::I32], [Type::I32]).into(),
([Type::I32, Type::I32], [Type::I32]).into(),
),
(
String::from("pong_with_tuple"),
([Type::I32], [Type::I32]).into(),
([Type::I32, Type::I32], [Type::I32]).into(),
),
(
String::from("pong_with_tuple_takes_2_args"),
([Type::I32, Type::I32], [Type::I32]).into(),
([Type::I32, Type::I32, Type::I32], [Type::I32]).into(),
),
(String::from("pong_env"), ([], [Type::I32]).into()),
(String::from("do_panic"), ([], []).into()),
(String::from("pong_env"), ([Type::I32], [Type::I32]).into()),
(String::from("do_panic"), ([Type::I32], []).into()),
]
}

Expand Down Expand Up @@ -72,6 +75,8 @@ fn callable_point_export_works() {
#[test]
fn callable_point_pong_works() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&10u64).unwrap();
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();
Expand All @@ -83,7 +88,7 @@ fn callable_point_pong_works() {
.call_function_strict(
&required_exports[export_index].1,
"pong",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap();
assert_eq!(call_result.len(), 1);
Expand All @@ -97,12 +102,15 @@ fn callable_point_pong_works() {
#[test]
fn callable_point_pong_with_struct_works() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&ExampleStruct {
str_field: String::from("hello"),
u64_field: 100u64,
})
.unwrap();

let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();

let required_exports = required_exports();
Expand All @@ -115,7 +123,7 @@ fn callable_point_pong_with_struct_works() {
.call_function_strict(
&required_exports[export_index].1,
"pong_with_struct",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap();
assert_eq!(call_result.len(), 1);
Expand All @@ -130,6 +138,8 @@ fn callable_point_pong_with_struct_works() {
#[test]
fn callable_point_pong_with_tuple_works() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&(String::from("hello"), 41i32)).unwrap();
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();
Expand All @@ -144,7 +154,7 @@ fn callable_point_pong_with_tuple_works() {
.call_function_strict(
&required_exports[export_index].1,
"pong_with_tuple",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap();
assert_eq!(call_result.len(), 1);
Expand All @@ -159,6 +169,8 @@ fn callable_point_pong_with_tuple_works() {
#[test]
fn callable_point_pong_with_tuple_takes_2_args_works() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param1 = to_vec(&String::from("hello")).unwrap();
let param_region_ptr1 = write_data_to_mock_env(&instance.env, &serialized_param1).unwrap();
Expand All @@ -176,7 +188,11 @@ fn callable_point_pong_with_tuple_takes_2_args_works() {
.call_function_strict(
&required_exports[export_index].1,
"pong_with_tuple_takes_2_args",
&[param_region_ptr1.into(), param_region_ptr2.into()],
&[
env_region_ptr.into(),
param_region_ptr1.into(),
param_region_ptr2.into(),
],
)
.unwrap();
assert_eq!(call_result.len(), 1);
Expand All @@ -191,6 +207,8 @@ fn callable_point_pong_with_tuple_takes_2_args_works() {
#[test]
fn callable_point_pong_env_works() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let required_exports = required_exports();
instance
Expand All @@ -199,7 +217,11 @@ 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", &[])
.call_function_strict(
&required_exports[export_index].1,
"pong_env",
&[env_region_ptr.into()],
)
.unwrap();
assert_eq!(call_result.len(), 1);

Expand All @@ -212,15 +234,20 @@ fn callable_point_pong_env_works() {
#[test]
fn callable_point_do_panic_raises_runtime_error() {
let instance = make_callee_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let required_exports = required_exports();
instance
.env
.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", &[]);
let call_result = instance.call_function_strict(
&required_exports[export_index].1,
"do_panic",
&[env_region_ptr.into()],
);

match call_result.unwrap_err() {
VmError::RuntimeErr { msg, .. } => {
Expand Down
2 changes: 1 addition & 1 deletion contracts/dynamic-caller-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn try_do_panic(deps: DepsMut, _env: Env) -> Result<Response, ContractError>
}

#[callable_point]
fn should_never_be_called(_deps: Deps) {}
fn should_never_be_called(_deps: Deps, _env: Env) {}

#[cfg(test)]
mod tests {
Expand Down
8 changes: 4 additions & 4 deletions contracts/number/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ fn query_number(deps: Deps) -> Result<NumberResponse, ContractError> {
}

#[callable_point]
fn add(deps: DepsMut, by: i32) {
fn add(deps: DepsMut, _env: Env, by: i32) {
handle_add(deps, by).unwrap();
}

#[callable_point]
fn sub(deps: DepsMut, by: i32) {
fn sub(deps: DepsMut, _env: Env, by: i32) {
handle_sub(deps, by).unwrap();
}

#[callable_point]
fn mul(deps: DepsMut, by: i32) {
fn mul(deps: DepsMut, _env: Env, by: i32) {
handle_mul(deps, by).unwrap();
}

#[callable_point]
fn number(deps: Deps) -> i32 {
fn number(deps: Deps, _env: Env) -> i32 {
read(deps.storage).unwrap()
}
24 changes: 16 additions & 8 deletions contracts/number/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ static CONTRACT: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/releas

fn required_exports() -> Vec<(String, FunctionType)> {
vec![
(String::from("add"), ([Type::I32], []).into()),
(String::from("sub"), ([Type::I32], []).into()),
(String::from("mul"), ([Type::I32], []).into()),
(String::from("number"), ([], [Type::I32]).into()),
(String::from("add"), ([Type::I32, Type::I32], []).into()),
(String::from("sub"), ([Type::I32, Type::I32], []).into()),
(String::from("mul"), ([Type::I32, Type::I32], []).into()),
(String::from("number"), ([Type::I32], [Type::I32]).into()),
]
}

Expand Down Expand Up @@ -59,6 +59,8 @@ fn callable_point_export_works() {
#[test]
fn callable_point_add_works() {
let instance = make_number_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&10i32).unwrap();
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();
Expand All @@ -73,7 +75,7 @@ fn callable_point_add_works() {
.call_function_strict(
&required_exports[export_index].1,
"add",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap_err();
assert!(call_result
Expand All @@ -84,6 +86,8 @@ fn callable_point_add_works() {
#[test]
fn callable_point_sub_works() {
let instance = make_number_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&10i32).unwrap();
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();
Expand All @@ -98,7 +102,7 @@ fn callable_point_sub_works() {
.call_function_strict(
&required_exports[export_index].1,
"sub",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap_err();
assert!(call_result
Expand All @@ -109,6 +113,8 @@ fn callable_point_sub_works() {
#[test]
fn callable_point_mul_works() {
let instance = make_number_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let serialized_param = to_vec(&10i32).unwrap();
let param_region_ptr = write_data_to_mock_env(&instance.env, &serialized_param).unwrap();
Expand All @@ -123,7 +129,7 @@ fn callable_point_mul_works() {
.call_function_strict(
&required_exports[export_index].1,
"mul",
&[param_region_ptr.into()],
&[env_region_ptr.into(), param_region_ptr.into()],
)
.unwrap_err();
assert!(call_result
Expand All @@ -134,14 +140,16 @@ fn callable_point_mul_works() {
#[test]
fn callable_point_number_works() {
let instance = make_number_instance();
let env = to_vec(&mock_env()).unwrap();
let env_region_ptr = write_data_to_mock_env(&instance.env, &env).unwrap();

let required_exports = required_exports();
let export_index = 3;
assert_eq!("number".to_string(), required_exports[export_index].0);
// 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", &[])
.call_function_strict(&required_exports[0].1, "number", &[env_region_ptr.into()])
.unwrap_err();
assert!(call_result
.to_string()
Expand Down
Loading