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

Add map integrity tests #1227

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add map integrity tests #1227

wants to merge 3 commits into from

Conversation

sisuresh
Copy link
Contributor

What

Resolves #1148.

@sisuresh sisuresh marked this pull request as ready for review November 20, 2023 19:12
@sisuresh sisuresh requested review from graydon, dmkozh and a team as code owners November 20, 2023 19:12
let keys = ["a", "b", "c", "d", "e"];

let res = host.map_new_from_slices(&keys, &vals.as_slice())?;
assert!(host.from_host_val(res.to_val()).is_err());
Copy link
Contributor

Choose a reason for hiding this comment

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

If you expect budget error, please add the specific assert for that. It would make test intent more clear.

}

// Same but with out of order keys
pub fn map_mem_bad(e: Env) -> Result<(), Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this return Result?

// Same but with out of order keys
pub fn map_mem_bad(e: Env) -> Result<(), Error> {
// out of order
assert!(e.map_new_from_slices(&["b", "a"], &[1u32.into(), 2u32.into()]).is_err());
Copy link
Contributor

@dmkozh dmkozh Nov 20, 2023

Choose a reason for hiding this comment

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

Seems like almost every function in this Wasm is set up incorrectly. Host functions don't return errors in guest environment, they cause the contract to trap. Functions like map_new_from_slices should be changed in SDK to be infallible (probably open an issue for that?).
Besides that, functions that perform assert! check can only be meaningfully tested when the expected invocation result is Ok (e.g. map_mem_bad expects an error, so even if you could assert on host fns, your test would still pass if either first or second assertion fails).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the assert part. The contract would've already trapped and the assert here does nothing (and the line after this will never get executed).
But I'm not sure about making map_new_from_slices and other functions infallible in the sdk. They are part of the EnvBase crate, and simply calls the host functions from the guest env. So I guess it doesn't really matter? If you have an opinion of what to do better in the sdk, can you create an issue there?

Copy link
Contributor

Choose a reason for hiding this comment

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

But I'm not sure about making map_new_from_slices and other functions infallible in the sdk. They are part of the EnvBase crate, and simply calls the host functions from the guest env. So I guess it doesn't really matter?

It does matter, because it creates a false impression that these functions can return an Err result. They cannot though, as they either cause a trap, or return Ok. This test is a live evidence of how misleading this is.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand the issue with EnvBase though, so I'm not sure how exactly we should address this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not totally familiar with the structuring of the sdk, maybe @graydon or @leighmcculloch can chime in here.

The EnvBase has same as the guest::Env here, where all functions have fallible interface but Infallible error. The Env functions are not directly exposed to the outside (unless via one of the public modules), but the EnvBase functions are. So maybe the answer here is not to expose the EnvBase methods, or expose only selected ones in other modules (and giving them infallible signatures)?

Copy link
Contributor

Choose a reason for hiding this comment

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

So maybe the answer here is not to expose the EnvBase methods, or expose only selected ones in other modules (and giving them infallible signatures)?

Yes, probably the least confusing thing to do would be to add a public wrapper over all the EnvBase functions and hide the fallible implementation, so that it can't be used by accident.

Copy link
Contributor

Choose a reason for hiding this comment

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

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.

[test] map & vec integrity/invariant checks
3 participants