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(test): support *OriginCall funcs in _test files #703

Merged
merged 7 commits into from
Apr 12, 2023

Conversation

tbruyelle
Copy link
Contributor

@tbruyelle tbruyelle commented Apr 5, 2023

Fix #481

Description

AssertOriginCall and IsOriginCall functions can be used inside contracts to determine if the call comes directly from a tx (which is an origin call) or from an other contract (not an origin call).

To do this those functions count the number of frames. In production environment, if there's exactly 2 frames, it is considered as an origin call. In test environment, the number of frames was 3, but it was working only for _filetest, and not for _test, where the setup is different and so the number of frames (7 instead of 3).

Change

To support _test, the change brings a way to identify if a test is a _test or _filetest, by checking the function name of the first frame.

There's currently a pending discussion (#683) about how we should deal with frames, that's why I tried to keep this change minimal, but at least it fixes something and we can move forward.

This replaces the previous attempt #495 which was more complicated and actually was not correct.

Next steps

Ideally, *OriginCall functions should rely on something stronger that the number of frames, because for instance they could consider that a call is not an origin call, just because std.IsOriginCall is called by a sub-function of the same contract. But that's something we'll probably tackle with #683.

How has this been tested?

Some new tests have been added to r/demo/tests. Note that because we need to catch the panic inside the gno test, panic has been replaced by m.Panic (as explained in #663).

gno test -verbose examples/gno.land/r/demo/tests/

Fix gnolang#481

`AssertOriginCall` and `IsOriginCall` functions can be used inside
contracts to determine if the call comes directly from a tx (which is
an *origin call*) or from an other contract (not an *origin call*).

To do this those functions count the number of frames. In production
environment, if there's exactly 2 frames, it is considered as an *origin
call*. In test environment, the number of frames was 3, but it was
working only for `_filetest`, and not for `_test`, where the setup is
different and so the number of frames (7 instead of 3).

To support `_test`, the change brings a way to identify if a test is
a `_test` or `_filetest`, by checking the function name of the first
frame.

There's currently a pending discussion (gnolang#683) about how we should deal
with frames, that's why I tried to keep this change minimal, but at
least it fixes something and we can move forward.

Ideally, `*OriginCall` functions should rely on something stronger that
the number of frames, because for instance they could consider that a call
is not an *origin call*, just because `std.IsOriginCall` is called by a
sub-function of the same contract. But that's something we'll probably
tackle with gnolang#683.
Copy link
Member

@moul moul left a comment

Choose a reason for hiding this comment

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

LGTM. Agreed, let's proceed with the small updates as you suggested and focus on defining a more permanent solution in #683.

@thehowl thehowl changed the title chore(test): support *OriginCall funcs in _test files feat(test): support *OriginCall funcs in _test files Apr 5, 2023
@tbruyelle tbruyelle requested a review from a team as a code owner April 5, 2023 16:17
@tbruyelle
Copy link
Contributor Author

tbruyelle commented Apr 5, 2023

@moul I've added testing in the package whitelist. CI should be happy now. d85f462

EDIT: it's not, working on it...

Not usable right now because it imports `sort.Sort` which is disabled in
tests.
Since p/demo/tests source code has been changed, some golden outputs
have to be updated.
@tbruyelle
Copy link
Contributor Author

Finally fixed the tests, I had to remove the testutils.RequirePanics added in this PR because it adds dependencies like testing which itself depends on sort.Sort which isn't defined in the test context (not sure to understand exactly why at the moment, but I'll try to understand later).

@moul moul merged commit 5b275ff into gnolang:master Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

test: unable to use std.AssertOriginCall in _test files
2 participants