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

Handling msg_responses #213

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b6dc293
Added msg_responses field to AppResponse.
DariuszDepta Sep 16, 2024
03b57fa
Updates.
DariuszDepta Sep 18, 2024
2f5d90c
Merge remote-tracking branch 'origin' into features/206-uses-of-depre…
DariuszDepta Sep 18, 2024
b842669
Updates.
DariuszDepta Sep 18, 2024
a5dc605
Removed the dependency to derivative crate.
DariuszDepta Sep 18, 2024
ad31183
Updates.
DariuszDepta Sep 18, 2024
aa6ecf7
Updates.
DariuszDepta Sep 18, 2024
2b066a0
Updates.
DariuszDepta Sep 18, 2024
3ac69d9
Updates.
DariuszDepta Sep 18, 2024
a18cd7a
Updates.
DariuszDepta Sep 18, 2024
e283e41
Updates.
DariuszDepta Sep 18, 2024
8f3f38e
Updates.
DariuszDepta Sep 18, 2024
b6f3d9b
Updates.
DariuszDepta Sep 18, 2024
eb921dd
Updates.
DariuszDepta Sep 18, 2024
7fdf0a2
Updates.
DariuszDepta Sep 18, 2024
6d880bd
Updates.
DariuszDepta Sep 18, 2024
1ebcf33
Updates.
DariuszDepta Sep 18, 2024
03e4680
Updates.
DariuszDepta Sep 18, 2024
7861865
Updates.
DariuszDepta Sep 19, 2024
a929b4e
Updates.
DariuszDepta Sep 19, 2024
c4a0627
Updates.
DariuszDepta Sep 19, 2024
48635a3
Updates.
DariuszDepta Sep 24, 2024
e36d9c9
Merged changes from main.
DariuszDepta Sep 24, 2024
f0bedc3
Merged changes from main.
DariuszDepta Sep 24, 2024
48fdcfb
Merge remote-tracking branch 'origin' into features/206-uses-of-depre…
DariuszDepta Oct 10, 2024
c31fd7f
Merge remote-tracking branch 'origin' into features/206-uses-of-depre…
DariuszDepta Oct 10, 2024
22a93eb
Merged changes from main.
DariuszDepta Oct 10, 2024
828a2c2
Merged changes from main.
DariuszDepta Oct 11, 2024
3bd6bc2
Updates.
DariuszDepta Oct 11, 2024
f225922
Merged from main.
DariuszDepta Oct 11, 2024
1312b62
Updates.
DariuszDepta Oct 12, 2024
d44c5a4
Merged changes from main.
DariuszDepta Oct 12, 2024
cdeb487
Added msg_response to Bank::Send message.
DariuszDepta Oct 14, 2024
6e8cde1
Added tests.
DariuszDepta Oct 15, 2024
c872e8e
Merge remote-tracking branch 'origin' into features/206-uses-of-depre…
DariuszDepta Oct 15, 2024
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
11 changes: 9 additions & 2 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::module::Module;
use crate::prefixed_storage::{prefixed, prefixed_read};
use cosmwasm_std::{
coin, to_json_binary, Addr, AllBalanceResponse, Api, BalanceResponse, BankMsg, BankQuery,
Binary, BlockInfo, Coin, DenomMetadata, Event, Querier, Storage,
Binary, BlockInfo, Coin, DenomMetadata, Event, MsgResponse, Querier, Storage,
};
#[cfg(feature = "cosmwasm_1_3")]
use cosmwasm_std::{AllDenomMetadataResponse, DenomMetadataResponse};
Expand Down Expand Up @@ -203,7 +203,14 @@ impl Module for BankKeeper {
Addr::unchecked(to_address),
amount,
)?;
Ok(AppResponse { events, data: None })
Ok(AppResponse {
events,
data: None,
msg_responses: vec![MsgResponse {
type_url: "/cosmos.bank.v1beta1.MsgSendResponse".to_string(),
value: Default::default(),
}],
})
}
BankMsg::Burn { amount } => {
// burn doesn't seem to emit any events
Expand Down
7 changes: 5 additions & 2 deletions src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::AnyResult;
use cosmwasm_std::{
to_json_binary, Addr, Attribute, BankMsg, Binary, Coin, CosmosMsg, CustomMsg, Event,
SubMsgResponse, WasmMsg,
MsgResponse, SubMsgResponse, WasmMsg,
};
use cw_utils::{parse_execute_response_data, parse_instantiate_response_data};
use serde::Serialize;
Expand All @@ -15,6 +15,8 @@ pub struct AppResponse {
pub events: Vec<Event>,
/// Response data.
pub data: Option<Binary>,
/// Responses from the messages emitted by the submessage.
pub msg_responses: Vec<MsgResponse>,
}

impl AppResponse {
Expand Down Expand Up @@ -58,9 +60,10 @@ impl AppResponse {
impl From<SubMsgResponse> for AppResponse {
fn from(reply: SubMsgResponse) -> Self {
AppResponse {
events: reply.events,
#[allow(deprecated)]
data: reply.data,
events: reply.events,
msg_responses: reply.msg_responses,
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ impl Module for StakeKeeper {
}
.into(),
)?;
Ok(AppResponse { events, data: None })
Ok(AppResponse {
events,
..Default::default()
})
}
StakingMsg::Undelegate { validator, amount } => {
self.validate_denom(&staking_storage, &amount)?;
Expand Down Expand Up @@ -714,7 +717,10 @@ impl Module for StakeKeeper {
payout_at: block.time.plus_seconds(staking_info.unbonding_time),
});
UNBONDING_QUEUE.save(&mut staking_storage, &unbonding_queue)?;
Ok(AppResponse { events, data: None })
Ok(AppResponse {
events,
..Default::default()
})
}
StakingMsg::Redelegate {
src_validator,
Expand Down Expand Up @@ -744,7 +750,10 @@ impl Module for StakeKeeper {
amount,
)?;

Ok(AppResponse { events, data: None })
Ok(AppResponse {
events,
..Default::default()
})
}
m => bail!("Unsupported staking message: {:?}", m),
}
Expand Down Expand Up @@ -977,18 +986,21 @@ impl Module for DistributionKeeper {
"amount",
format!("{}{}", rewards, staking_info.bonded_denom),
)];
Ok(AppResponse { events, data: None })
Ok(AppResponse {
events,
..Default::default()
})
}
DistributionMsg::SetWithdrawAddress { address } => {
let address = api.addr_validate(&address)?;
// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/keeper/msg_server.go#L38
let storage = &mut prefixed(storage, NAMESPACE_DISTRIBUTION);
Self::set_withdraw_address(storage, &sender, &address)?;
Ok(AppResponse {
data: None,
// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/keeper/keeper.go#L74
events: vec![Event::new("set_withdraw_address")
.add_attribute("withdraw_address", address)],
..Default::default()
})
}
m => bail!("Unsupported distribution message: {:?}", m),
Expand Down
18 changes: 9 additions & 9 deletions src/test_helpers/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ fn reply<C>(_deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Response<C>>
where
C: CustomMsg + 'static,
{
let response = Response::new();
let mut response = Response::default();
#[allow(deprecated)]
if let Reply {
id,
result: SubMsgResult::Ok(SubMsgResponse {
data: Some(data), ..
}),
result:
SubMsgResult::Ok(SubMsgResponse {
events: _,
data: Some(data),
msg_responses: _,
}),
..
} = msg
{
Expand All @@ -108,13 +111,10 @@ where
.data
};
if let Some(data) = parsed_data {
Ok(response.set_data(data))
} else {
Ok(response)
response = response.set_data(data);
}
} else {
Ok(response)
}
Ok(response)
}

pub fn contract<C>() -> Box<dyn Contract<C>>
Expand Down
20 changes: 10 additions & 10 deletions src/tests/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ mod reply_data_overwrite {

// the returned data should be the same as the one being previously sent
assert_eq!(response.data, Some(b"PAYLOAD".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1116,7 +1116,7 @@ mod reply_data_overwrite {

// the returned data should be the data payload of the submessage
assert_eq!(response.data, Some(b"SECOND".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1153,7 +1153,7 @@ mod reply_data_overwrite {

// the returned data should be the data payload of the original message
assert_eq!(response.data, Some(b"FIRST".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1187,7 +1187,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"FIRST".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1219,7 +1219,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"SECOND".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1279,7 +1279,7 @@ mod reply_data_overwrite {

// ensure the data in response is empty
assert_eq!(response.data, None);
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
// ensure expected events are returned
assert_eq!(response.events.len(), 2);
let make_event = |contract_addr: &Addr| {
Expand Down Expand Up @@ -1342,7 +1342,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"SECOND".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1376,7 +1376,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"Orig".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1425,7 +1425,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"SECOND".into()));
//TODO assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}

#[test]
Expand Down Expand Up @@ -1474,7 +1474,7 @@ mod reply_data_overwrite {
.unwrap();

assert_eq!(response.data, Some(b"SECOND".into()));
//assert_eq!(response.msg_responses, vec![]);
assert_eq!(response.msg_responses, vec![]);
}
}

Expand Down
77 changes: 51 additions & 26 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,19 @@
let admin = new_admin.map(|a| api.addr_validate(&a)).transpose()?;

// check admin status
let mut data = self.contract_data(storage, &contract_addr)?;
if data.admin != Some(sender) {
bail!("Only admin can update the contract admin: {:?}", data.admin);
let mut contract_data = self.contract_data(storage, &contract_addr)?;
if contract_data.admin != Some(sender) {

Check warning on line 583 in src/wasm.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm.rs#L583

Added line #L583 was not covered by tests
bail!(
"Only admin can update the contract admin: {:?}",
contract_data.admin
);
}
// update admin field
data.admin = admin;
self.save_contract(storage, &contract_addr, &data)?;
contract_data.admin = admin;
self.save_contract(storage, &contract_addr, &contract_data)?;

// no custom event here
Ok(AppResponse {
data: None,
events: vec![],
})
Ok(AppResponse::default())
}

// this returns the contract address as well, so we can properly resend the data
Expand Down Expand Up @@ -835,7 +835,7 @@
SubMsgResponse {
events: r.events.clone(),
data: r.data,
msg_responses: vec![],
msg_responses: r.msg_responses.clone(),
},
),
};
Expand Down Expand Up @@ -890,8 +890,9 @@
self.process_response(api, router, storage, block, contract, res, msgs)
}

// this captures all the events and data from the contract call.
// it does not handle the messages
/// Captures all the events, data and sub messages from the contract call.
///
/// This function does not handle the messages.
fn build_app_response(
&self,
contract: &Addr,
Expand Down Expand Up @@ -929,11 +930,12 @@
});
app_events.extend(wasm_events);

let app = AppResponse {
let app_response = AppResponse {
events: app_events,
data,
..Default::default()
};
(app, messages)
(app_response, messages)
}

fn process_response(
Expand All @@ -944,19 +946,42 @@
block: &BlockInfo,
contract: Addr,
response: AppResponse,
messages: Vec<SubMsg<ExecC>>,
sub_messages: Vec<SubMsg<ExecC>>,
) -> AnyResult<AppResponse> {
let AppResponse { mut events, data } = response;

// recurse in all messages
let data = messages.into_iter().try_fold(data, |data, resend| {
let sub_res =
self.execute_submsg(api, router, storage, block, contract.clone(), resend)?;
events.extend_from_slice(&sub_res.events);
Ok::<_, AnyError>(sub_res.data.or(data))
})?;

Ok(AppResponse { events, data })
// Unpack the provided response.
let AppResponse {
mut events,
data,
mut msg_responses,
} = response;
// Recurse in all submessages.
let data = sub_messages
.into_iter()
.try_fold(data, |data, sub_message| {
// Execute the submessage.
let sub_response = self.execute_submsg(
api,
router,
storage,
block,
contract.clone(),
sub_message,
)?;
// COLLECT and append all events from the processed submessage.
events.extend_from_slice(&sub_response.events);

Check warning on line 971 in src/wasm.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm.rs#L971

Added line #L971 was not covered by tests
// COLLECT and append all message responses from the processed submessage.
msg_responses.extend_from_slice(&sub_response.msg_responses);

Check warning on line 973 in src/wasm.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm.rs#L973

Added line #L973 was not covered by tests
// REPLACE the data with value from the processes submessage (if not empty).
Ok::<_, AnyError>(sub_response.data.or(data))

Check warning on line 975 in src/wasm.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm.rs#L975

Added line #L975 was not covered by tests
})?;
// Return the response with updated data, events and message responses taken from
// all processed sub messages. Note that events and message responses are collected,
// but the data is replaced with the data from the last processes submessage.
Ok(AppResponse {
events,
data,
msg_responses,

Check warning on line 983 in src/wasm.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm.rs#L980-L983

Added lines #L980 - L983 were not covered by tests
})
}

/// Creates a contract address and empty storage instance.
Expand Down
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod test_contract_storage;
mod test_module;
mod test_payload;
mod test_prefixed_storage;
mod test_responses;
#[cfg(feature = "staking")]
mod test_staking;
mod test_wasm;
Expand Down
Loading