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

Cheatcodes as per new std-forge library #567

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

Conversation

msooseth
Copy link
Collaborator

@msooseth msooseth commented Sep 23, 2024

Goal

The way forge-std is doing things has changed. It no longer sets a value in ds-test, but instead Revert-s with a specific message. So we have to match:

forge test --match-test "test_set_get()" -vvvv
[...]
  [31205] MateFailContractTest::test_set_get()
    ├─ [22216] MateFailContract::set_mate()
    │   └─ ← [Stop]
    ├─ [268] MateFailContract::get_mate() [staticcall]
    │   └─ ← [Return] 3
    ├─ [0] VM::assertEq(2, 3) [staticcall]
    │   └─ ← [Revert] assertion failed: 3 != 2
    └─ ← [Revert] assertion failed: 3 != 2

Instead of the current:

Traces:
  [42786] MateFailContractTest::test_set_get()
    ├─ [22216] MateFailContract::set_mate()
    │   └─ ← [Stop]
    ├─ [268] MateFailContract::get_mate() [staticcall]
    │   └─ ← [Return] 3
    ├─ emit log(val: "Error: a == b not satisfied [uint]")
    ├─ emit log_named_uint(key: "      Left", val: 3)
    ├─ emit log_named_uint(key: "     Right", val: 2)
    ├─ [0] VM::store(VM: [0x7109709ECfa91a80626fF3989D68f67F5b1DD12D], 0x6661696c65640000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000001)
    │   └─ ← [Return]
    └─ ← [Stop]

The most prominent part of this change is the disappearance of line 202 from UnitTest.hs, where we'd check for failure bit being set in the cheat contract (i.e. 0x7109709EC...)

Implementation

  • Emilio has changed things around already in EVM.hs to have a separate frame, which allows this to be coded in a relatively compact way. I have now added a number of required (but not all) cheatcodes.
  • Moved all relevant test cases to std-forge/Test.hs and {Test} from ds-test/test.hs and {DSTest}
  • Added most of the required assert... methods, though some are still missing. The ones still missing would require dynamic ABI handling, such as assertTrue(uint,string), where string is dynamic.
  • Fixed the function signature generation for types such as tuples. Now prove_tuple_fail1(uint256[5] memory amounts) actually works
  • Better debug output. Cleaner and more thorough. It should be a lot easier to debug things.

Future work

I haven't added:

  • assertEq(T[], T[]) type asserts
  • assertNotEq(T[], T[]) type asserts
  • assertXXDecimal
  • `assertApproxXX
  • assertEqCall`

Mostly due to the above mentioned issue with dynamic ABI type issue.

TODO in a future PR

  • Move cheatcodes into a new file
  • Would be nice to accumulate errors when e.g. we have trouble with cheatcodes

Checklist

  • tested locally
  • added automated tests
  • updated the docs
  • updated the changelog

@msooseth msooseth force-pushed the implement_new_cheatcodes branch 2 times, most recently from e78213a to c0cca47 Compare October 28, 2024 11:21
@msooseth msooseth changed the title [TODO] Cheatcodes TODOs Cheatcodes as per new std-forge library Oct 29, 2024
@msooseth msooseth changed the title Cheatcodes as per new std-forge library [DRAFT] Cheatcodes as per new std-forge library Oct 29, 2024
@msooseth msooseth force-pushed the implement_new_cheatcodes branch 7 times, most recently from fd7745f to 527025f Compare October 29, 2024 17:46
Comment on lines -290 to -297
checkSymFailures :: VMOps t => UnitTestOptions RealWorld -> Stepper t RealWorld (VM t RealWorld)
checkSymFailures UnitTestOptions { .. } = do
-- Ask whether any assertions failed
Stepper.evm $ do
popTrace
abiCall testParams (Left ("failed()", emptyAbi))
Stepper.runFully

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not used anywhere, so I'm removing it

src/EVM.hs Outdated Show resolved Hide resolved
src/EVM.hs Outdated Show resolved Hide resolved
src/EVM/UnitTest.hs Outdated Show resolved Hide resolved
@msooseth msooseth force-pushed the implement_new_cheatcodes branch 2 times, most recently from e138b11 to 0edfa94 Compare November 26, 2024 16:20
@msooseth msooseth changed the title [DRAFT] Cheatcodes as per new std-forge library Cheatcodes as per new std-forge library Nov 26, 2024
@msooseth msooseth marked this pull request as ready for review November 26, 2024 17:09
@msooseth
Copy link
Collaborator Author

This is now ready to re-review, @d-xo and maybe @blishko in case you want to have a go at it :)

Comment on lines -202 to -204
Just cheatContract -> readStorage' (Lit 0x6661696c65640000000000000000000000000000000000000000000000000000) cheatContract.storage .== Lit 1
Nothing -> And (readStorage' (Lit 0) (testContract store).storage) (Lit 2) .== Lit 2
postcondition = curry $ case shouldFail of
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't store failures here any more in forge-std, so no need to check.

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.

2 participants