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

Properly handling data in submessages in multi-test #369

Merged
merged 2 commits into from
Aug 3, 2021

Conversation

hashedone
Copy link
Contributor

@hashedone hashedone commented Aug 2, 2021

Closes #352

I am not entirely sure - the issue mentions only data, but I am pretty sure that events from sub messages should also be merged (however I can misunderstand something) - as this would be similar and simple change, I think I can add it to the same PR.

@hashedone hashedone force-pushed the 352-repsonse-data-handling branch from a289ed3 to 2eedb20 Compare August 2, 2021 19:33
@ethanfrey
Copy link
Member

ethanfrey commented Aug 2, 2021

Events get appended (they should already!).

Data may get overwritten if set to Some(_). Only the Response of reply can overwrite data. All events from all executions are emitted and appended. (I actually need to clarify this with Alex this week, but for now, just throw them all together). The last time I looked at events in tests, I think they already worked like that?

I would merge this (once linting fixed), and I'd be happy to see your echo contract used to test all events (+attributed) being properly propagated up. If they don't, then yes, please fix that as well. (That can be another PR - test and possibly fix events)

Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff.

I'd just ask you to remove the unneeded Binary in Response<Binary>, SubMsg<Binary> for echo. We may need better docs to explain when you need that T.

contract: Addr,
data: impl Into<Option<&'static str>>,
sub_msg: Vec<SubMsg<Binary>>,
) -> SubMsg<Binary> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need SubMsg<Binary>?

They type T here just controls the type of CosmosMsg::Custom variant, so we can compile with code that uses it. We return an error if anyone actually returns those variants in multi-test (this is a future TODO to support it)

_env: Env,
_info: MessageInfo,
_msg: EmptyMsg,
) -> Result<Response<Binary>, StdError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented above, but I am quite sure there is no need for Response<Binary> unless you do something like:

let msg = CosmosMsg::Custom(Binary::from(b"foo"));
res.add_message(msg);

The type here is unused unless you use some blockchain-specific custom return types. Which is only the case for tgrade-specific contracts (PoE), terra-specific contracts, etc. Not for any tests. Multi-test does not support those messages when returned

msg: Message,
) -> Result<Response<Binary>, StdError> {
let mut resp = Response::new();
if let Some(data) = msg.data {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice logic

to_binary(&msg)
}

fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response<Binary>, StdError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this will re-play whatever reply data came from the submsg. Nice for tests

let data = response
.messages
.into_iter()
.try_fold(response.data, |data, resend| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting rework of loop to functional, but looks good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that or introduce mutable variable which is updated. I find folding way more readable that mutating, as if you understand folding, nothing surprises you. Analyze mutation always requires you to carefully read the loop. Otherwise I won't bother changing it.

)
.unwrap();

assert_eq!(response.data, Some("Data".to_owned().into_bytes().into()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is some chain... Doesn't it work without the to_owned?

Or "Data".to_bytes().into()?

)
.unwrap();

assert_eq!(response.data, Some("Second".to_owned().into_bytes().into()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test case. Very correct

)
.unwrap();

assert_eq!(response.data, Some("First".to_owned().into_bytes().into()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No overwrite on None. 👍

&echo::Message {
data: Some("Orig".to_owned()),
sub_msg: vec![
make_echo_submsg(contract.clone(), None, vec![]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool test case for the fold

contract.clone(),
&echo::Message {
data: Some("Orig".to_owned()),
sub_msg: vec![make_echo_submsg(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another great test case.
I had to read it twice to ensure the final result made sense, but yes, very correct

@hashedone hashedone mentioned this pull request Aug 3, 2021
@hashedone hashedone force-pushed the 352-repsonse-data-handling branch from 2eedb20 to 6b52132 Compare August 3, 2021 07:54
@hashedone hashedone merged commit 8501678 into main Aug 3, 2021
@hashedone hashedone deleted the 352-repsonse-data-handling branch August 3, 2021 08:42
@ethanfrey
Copy link
Member

Nice updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

multi-test: data handling with replies
2 participants