Skip to content

Commit

Permalink
Use the reply id to indicate Instantiate vs. Execute messages (echo c…
Browse files Browse the repository at this point in the history
…ontract)
  • Loading branch information
maurolacy committed Oct 28, 2021
1 parent c20f112 commit c0d4c01
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 26 deletions.
76 changes: 62 additions & 14 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,8 @@ mod test {
mod reply_data_overwrite {
use super::*;

use echo::EXECUTE_REPLY_BASE_ID;

fn make_echo_submsg(
contract: Addr,
data: impl Into<Option<&'static str>>,
Expand Down Expand Up @@ -1907,7 +1909,12 @@ mod test {
contract.clone(),
&echo::Message {
data: Some("First".to_owned()),
sub_msg: vec![make_echo_submsg(contract, "Second", vec![], 1)],
sub_msg: vec![make_echo_submsg(
contract,
"Second",
vec![],
EXECUTE_REPLY_BASE_ID,
)],
..echo::Message::default()
},
&[],
Expand Down Expand Up @@ -1987,7 +1994,12 @@ mod test {
owner,
contract.clone(),
&echo::Message {
sub_msg: vec![make_echo_submsg(contract, "Second", vec![], 1)],
sub_msg: vec![make_echo_submsg(
contract,
"Second",
vec![],
EXECUTE_REPLY_BASE_ID,
)],
..echo::Message::default()
},
&[],
Expand Down Expand Up @@ -2076,10 +2088,25 @@ mod test {
&echo::Message {
data: Some("Orig".to_owned()),
sub_msg: vec![
make_echo_submsg(contract.clone(), None, vec![], 1),
make_echo_submsg(contract.clone(), "First", vec![], 2),
make_echo_submsg(contract.clone(), "Second", vec![], 3),
make_echo_submsg(contract, None, vec![], 4),
make_echo_submsg(
contract.clone(),
None,
vec![],
EXECUTE_REPLY_BASE_ID + 1,
),
make_echo_submsg(
contract.clone(),
"First",
vec![],
EXECUTE_REPLY_BASE_ID + 2,
),
make_echo_submsg(
contract.clone(),
"Second",
vec![],
EXECUTE_REPLY_BASE_ID + 3,
),
make_echo_submsg(contract, None, vec![], EXECUTE_REPLY_BASE_ID + 4),
],
..echo::Message::default()
},
Expand Down Expand Up @@ -2139,10 +2166,25 @@ mod test {
contract.clone(),
&echo::Message {
sub_msg: vec![
make_echo_submsg(contract.clone(), None, vec![], 1),
make_echo_submsg(
contract.clone(),
None,
vec![],
EXECUTE_REPLY_BASE_ID + 1,
),
make_echo_submsg_no_reply(contract.clone(), "Hidden", vec![]),
make_echo_submsg(contract.clone(), "Shown", vec![], 2),
make_echo_submsg(contract.clone(), None, vec![], 3),
make_echo_submsg(
contract.clone(),
"Shown",
vec![],
EXECUTE_REPLY_BASE_ID + 2,
),
make_echo_submsg(
contract.clone(),
None,
vec![],
EXECUTE_REPLY_BASE_ID + 3,
),
make_echo_submsg_no_reply(contract, "Lost", vec![]),
],
..echo::Message::default()
Expand Down Expand Up @@ -2180,12 +2222,17 @@ mod test {
vec![make_echo_submsg(
contract.clone(),
"Second",
vec![make_echo_submsg(contract, None, vec![], 4)],
3,
vec![make_echo_submsg(
contract,
None,
vec![],
EXECUTE_REPLY_BASE_ID + 4,
)],
EXECUTE_REPLY_BASE_ID + 3,
)],
2,
EXECUTE_REPLY_BASE_ID + 2,
)],
1,
EXECUTE_REPLY_BASE_ID + 1,
)],
..echo::Message::default()
},
Expand Down Expand Up @@ -2393,6 +2440,7 @@ mod test {

mod protobuf_wrapped_data {
use super::*;
use crate::test_helpers::contracts::echo::EXECUTE_REPLY_BASE_ID;
use cw0::parse_instantiate_response_data;

#[test]
Expand Down Expand Up @@ -2486,7 +2534,7 @@ mod test {
msg: to_binary(&msg).unwrap(),
funds: vec![],
},
1234,
EXECUTE_REPLY_BASE_ID,
);
let init_msg = echo::InitMessage::<Empty> {
data: Some("Overwrite me".into()),
Expand Down
36 changes: 24 additions & 12 deletions packages/multi-test/src/test_helpers/contracts/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ use crate::{test_helpers::EmptyMsg, Contract, ContractWrapper};
use schemars::JsonSchema;
use std::fmt::Debug;

use cw0::parse_execute_response_data;
use cw0::{parse_execute_response_data, parse_instantiate_response_data};
use derivative::Derivative;

// Choosing a reply id less than ECHO_EXECUTE_BASE_ID indicates an Instantiate message reply by convention.
// An Execute message reply otherwise.
pub(crate) const EXECUTE_REPLY_BASE_ID: u64 = i64::MAX as u64;

#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
#[derivative(Default(bound = "", new = "true"))]
pub struct Message<ExecC>
Expand Down Expand Up @@ -90,27 +94,35 @@ fn reply<ExecC>(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response<ExecC>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
{
let res = Response::new();
if let Reply {
id,
result:
ContractResult::Ok(SubMsgExecutionResponse {
data: Some(data), ..
}),
..
} = msg
{
// we parse out the WasmMsg::Execute wrapper...
// TODO: this is not fully correct... we need to handle execute, instantiate, and bankmsg differently
// that will require using the Reply id somehow to signal what type
let parsed = parse_execute_response_data(data.as_slice())
.map_err(|e| StdError::generic_err(e.to_string()))?
.data;
if let Some(d) = parsed {
Ok(Response::new().set_data(d))
// We parse out the WasmMsg::Execute wrapper...
// TODO: Handle all of Execute, Instantiate, and BankMsg replies differently.
let parsed_data;
if id < EXECUTE_REPLY_BASE_ID {
parsed_data = parse_instantiate_response_data(data.as_slice())
.map_err(|e| StdError::generic_err(e.to_string()))?
.data;
} else {
parsed_data = parse_execute_response_data(data.as_slice())
.map_err(|e| StdError::generic_err(e.to_string()))?
.data;
}

if let Some(data) = parsed_data {
Ok(res.set_data(data))
} else {
Ok(Response::new())
Ok(res)
}
} else {
Ok(Response::new())
Ok(res)
}
}

Expand Down

0 comments on commit c0d4c01

Please sign in to comment.