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: add do_panic as callable_point #236

Merged
merged 7 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions contracts/dynamic-callee-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ fn pong_env() -> Env {
GlobalApi::env()
}

#[callable_point]
fn callee_panic() {
loloicci marked this conversation as resolved.
Show resolved Hide resolved
panic!();
}

#[derive(Contract)]
struct Me {
address: Addr,
Expand Down
26 changes: 25 additions & 1 deletion contracts/dynamic-callee-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_vm::testing::{
mock_backend, mock_env, read_data_from_mock_env, write_data_to_mock_env, Contract, MockApi,
MockInstanceOptions, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR,
};
use cosmwasm_vm::Instance;
use cosmwasm_vm::{Instance, VmError};
use dynamic_callee_contract::contract::ExampleStruct;
use std::collections::HashMap;
use wasmer_types::{FunctionType, Type};
Expand All @@ -27,6 +27,7 @@ fn required_exports() -> Vec<(String, FunctionType)> {
([Type::I32, Type::I32], [Type::I32]).into(),
),
(String::from("pong_env"), ([], [Type::I32]).into()),
(String::from("callee_panic"), ([], []).into()),
]
}

Expand Down Expand Up @@ -207,3 +208,26 @@ fn callable_point_pong_env_works() {
let result: Env = from_slice(&serialized_return).unwrap();
assert_eq!(result.contract.address, Addr::unchecked(MOCK_CONTRACT_ADDR));
}

#[test]
fn callable_point_callee_panic_works() {
loloicci marked this conversation as resolved.
Show resolved Hide resolved
let instance = make_callee_instance();

let required_exports = required_exports();
instance
.env
.set_serialized_env(&to_vec(&mock_env()).unwrap());
let export_index = 5;
assert_eq!("callee_panic".to_string(), required_exports[export_index].0);
let call_result =
instance.call_function_strict(&required_exports[export_index].1, "callee_panic", &[]);

match call_result.unwrap_err() {
VmError::RuntimeErr { msg, .. } => {
// Because content in the latter part depends on the environment,
// comparing whether the error begins with panic error or not.
assert!(msg.starts_with("Wasmer runtime error: RuntimeError: unreachable"))
}
e => panic!("Unexpected error: {:?}", e),
}
}
12 changes: 12 additions & 0 deletions contracts/dynamic-caller-contract/schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"callee_panic"
],
"properties": {
"callee_panic": {
"type": "object"
}
},
"additionalProperties": false
}
],
"definitions": {
Expand Down
13 changes: 13 additions & 0 deletions contracts/dynamic-caller-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait Callee: Contract {
fn pong_with_tuple_takes_2_args(&self, input1: String, input2: i32) -> (String, i32);
fn pong_env(&self) -> Env;
fn reentrancy(&self, addr: Addr);
fn callee_panic(&self);
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -62,6 +63,10 @@ impl Callee for CalleeContract {
fn reentrancy(&self, _addr: Addr) {
panic!()
}

fn callee_panic(&self) {
panic!()
}
}

// Note, you can use StdResult in some functions where you do not
Expand Down Expand Up @@ -90,6 +95,7 @@ pub fn execute(
match msg {
ExecuteMsg::Ping { ping_num } => try_ping(deps, ping_num),
ExecuteMsg::TryReEntrancy {} => try_re_entrancy(deps, env),
ExecuteMsg::CalleePanic {} => try_callee_panic(deps, env),
}
}

Expand Down Expand Up @@ -131,6 +137,13 @@ pub fn try_re_entrancy(deps: DepsMut, env: Env) -> Result<Response, ContractErro
Ok(Response::default())
}

pub fn try_callee_panic(deps: DepsMut, _env: Env) -> Result<Response, ContractError> {
let address = from_slice(&deps.storage.get(b"dynamic_callee_contract").unwrap())?;
let contract = CalleeContract { address };
contract.callee_panic();
Ok(Response::default())
}

#[callable_point]
fn should_never_be_called() {}

Expand Down
1 change: 1 addition & 0 deletions contracts/dynamic-caller-contract/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct InstantiateMsg {
pub enum ExecuteMsg {
Ping { ping_num: Uint128 },
TryReEntrancy {},
CalleePanic {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down
5 changes: 5 additions & 0 deletions contracts/dynamic-caller-contract/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ fn required_imports() -> Vec<(String, String, FunctionType)> {
String::from("CalleeContract"),
([Type::I32], [Type::I32]).into(),
),
(
String::from("callee_panic"),
String::from("CalleeContract"),
([Type::I32], []).into(),
),
]
}

Expand Down