Skip to content

Support multiple balance formats in targetContractsBalances#580

Merged
anishnaik merged 15 commits intocrytic:masterfrom
0xZRA:dev/set-initial-balance-in-base-10-strings
Mar 3, 2025
Merged

Support multiple balance formats in targetContractsBalances#580
anishnaik merged 15 commits intocrytic:masterfrom
0xZRA:dev/set-initial-balance-in-base-10-strings

Conversation

@0xZRA
Copy link
Copy Markdown
Contributor

@0xZRA 0xZRA commented Feb 14, 2025

This is to address issue #306
Relies on running go generate command to automatically generate code generate JSON marshaling code from template for FuzzingConfig

@anishnaik

I'm running into an issue where tests are failing locally on my machine, even on the master branch. I wanted to flag this early - I'm using Sequoia version 15.1.1, so it's unclear if my changes are implemented correctly.

go test -v ./fuzzing

FAIL
FAIL    github.com/crytic/medusa/fuzzing        240.868s
FAIL

At the same time, when running following tests individually thru VSCode, both are passing:

Running tool: /opt/homebrew/bin/go test -timeout 30s -run ^TestDeploymentsWithPayableConstructors$ github.com/crytic/medusa/fuzzing

ok  	github.com/crytic/medusa/fuzzing	6.693s
Running tool: /opt/homebrew/bin/go test -timeout 30s -run ^TestDeploymentsWithPredeploy$ github.com/crytic/medusa/fuzzing

ok  	github.com/crytic/medusa/fuzzing	1.280s

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 14, 2025

CLA assistant check
All committers have signed the CLA.

@0xZRA 0xZRA marked this pull request as ready for review February 14, 2025 15:38
@anishnaik
Copy link
Copy Markdown
Collaborator

@0xZRA provided some feedback. Lmk if you have any questions. I'll run the CI when you make those changes and I can investigate if/why the tests are failing.

@0xZRA 0xZRA requested a review from anishnaik February 17, 2025 21:12
@anishnaik
Copy link
Copy Markdown
Collaborator

Hey @0xZRA, will provide some more feedback soon. Sorry for the delay.

@anishnaik
Copy link
Copy Markdown
Collaborator

anishnaik commented Feb 20, 2025

Hey @0xZRA okay so I think we need to change our approach. Tbh I didn't expect go generate to complain about this (sorry about that). So, going back to the original problem: we need to (de)serialize the big.Ints. Another way to approach this is to create a custom type (type ContractBalance *big.Int). Thus, the TargetContractBalances will become of type []ContractBalance. Now, we can write our own custom MarshalJSON and UnmarshalJSON that will marshal the ContractBalance to a string and unmarshal from a string to a *big.Int. Here is a stack overflow post that should get you in the right direction: https://stackoverflow.com/questions/53991835/how-to-marshal-and-unmarshal-big-int-in-json

Doing it this way will remove any requirement for go generate so you can remove the directive and the associated file as well.

I would like to take it one more step than what is mentioned in the stack overflow post. I would like to be able to unmarshal any three of these string possibilities:

  1. Base-10 string: "1234" or "45678"
  2. Hex string: "0xabcdef"
  3. Scientific notation for base-10 strings (optional if it is easy): "1.2e18"

Let me know if you have any questions.

Also, about the failing tests: We have some challenging unit tests that sometimes takes medusa a while to solve. That's why sometimes the tests may fail to run. Usually, re-running it does the trick but I do need to investigate why it is happening more frequently nowadays.

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Feb 21, 2025

Hey @0xZRA okay so I think we need to change our approach. Tbh I didn't expect go generate to complain about this (sorry about that). So, going back to the original problem: we need to (de)serialize the big.Ints. Another way to approach this is to create a custom type (type ContractBalance *big.Int). Thus, the TargetContractBalances will become of type []ContractBalance. Now, we can write our own custom MarshalJSON and UnmarshalJSON that will marshal the ContractBalance to a string and unmarshal from a string to a *big.Int. Here is a stack overflow post that should get you in the right direction: https://stackoverflow.com/questions/53991835/how-to-marshal-and-unmarshal-big-int-in-json

Doing it this way will remove any requirement for go generate so you can remove the directive and the associated file as well.

I would like to take it one more step than what is mentioned in the stack overflow post. I would like to be able to unmarshal any three of these string possibilities:

  1. Base-10 string: "1234" or "45678"
  2. Hex string: "0xabcdef"
  3. Scientific notation for base-10 strings (optional if it is easy): "1.2e18"

Let me know if you have any questions.

Also, about the failing tests: We have some challenging unit tests that sometimes takes medusa a while to solve. That's why sometimes the tests may fail to run. Usually, re-running it does the trick but I do need to investigate why it is happening more frequently nowadays.

@anishnaik I appreciate your response and I think it makes sense so far. Just wanted to clarify one point in your approach regarding the unmarshalling of the 3 string possibilities. Does the proposed approach still involve maintaining the current hexadecimal notation for setting initial balances, while adding the option to use base-10 strings, including scientific notation?

So if we take it straight to the docs, the new version will look like:

targetContractsBalances

  • Type: [Base-16 Strings] (e.g. [0x123, 0x456, 0x789])
  • Type: [Base-10 Strings] (e.g. ["123", "1000000", "115792089237316195423570985008687907853269984665640564039457584007913129639935"])
  • Type: [Base-10 Strings Scientific Notation] (e.g. ["1.23e2", "1.0e6", "1.15792089237316195423570985008687907853269984665640564039457584007913129639935e77"])

@anishnaik
Copy link
Copy Markdown
Collaborator

anishnaik commented Feb 21, 2025

Technically, you can also mix and match: ["1e12", "0x1234", "5678"]. Since you are evaluating each string individually, and not the whole array, the logic shouldn't change depending on what the array looks like.

Like the UnmarshalJSON function you write will take a []byte and you need to convert it into a ContractBalance (aka *big.Int). This []byte could be any of the three string possibilities (or invalid).

Also, for MarshalJSON, always just serialize it as a base-10 string.

This PR became more complicated than I expected :)

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Feb 21, 2025

Technically, you can also mix and match: ["1e12", "0x1234", "5678"]. Since you are evaluating each string individually, and not the whole array, the logic shouldn't change depending on what the array looks like.

Like the UnmarshalJSON function you write will take a []byte and you need to convert it into a ContractBalance (aka *big.Int). This []byte could be any of the three string possibilities (or invalid).

Also, for MarshalJSON, always just serialize it as a base-10 string.

This PR became more complicated than I expected :)

I hadn't thought of mix and match, you're right. I'll take a shot at it.

@0xZRA 0xZRA marked this pull request as draft February 21, 2025 13:55
@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Feb 23, 2025

This is still WIP, as I haven't tested the actual runs with different balance formats. @anishnaik let me know if I captured the requirements correctly

@0xZRA 0xZRA changed the title WIP: set initial balance in base 10 strings WIP: support multiple balance formats in targetContractsBalances Feb 23, 2025
Copy link
Copy Markdown
Collaborator

@anishnaik anishnaik left a comment

Choose a reason for hiding this comment

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

Hey @0xZRA, great job! Provided some feedback. lmk if you have any questions.

@anishnaik anishnaik marked this pull request as ready for review February 27, 2025 01:13
@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Feb 28, 2025

@anishnaik I hope I addressed all issues except these 2:
#580 (comment)
#580 (comment)
Let me know whenever you get a chance

Copy link
Copy Markdown
Collaborator

@anishnaik anishnaik left a comment

Choose a reason for hiding this comment

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

Okay one more round of final nitpicks and review :)

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Feb 28, 2025

Okay one more round of final nitpicks and review :)

No problem, will do

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Mar 2, 2025

@anishnaik Thank you for your comments, I hope I addressed latest round of feedback.

@0xZRA 0xZRA requested a review from anishnaik March 2, 2025 15:52
@0xZRA 0xZRA changed the title WIP: support multiple balance formats in targetContractsBalances Support multiple balance formats in targetContractsBalances Mar 2, 2025
@anishnaik
Copy link
Copy Markdown
Collaborator

Hey @0xZRA looks great! Can you run npm prettier . --write in the docs/ folder? It will help the linter pass in the CI. PS you will probably have to download prettier which you can do via npm -g install prettier.

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Mar 2, 2025

@anishnaik just pushed the change after running prettier

@anishnaik
Copy link
Copy Markdown
Collaborator

@0xZRA, it seems like docs/theme/highlight.js has been added to this PR which we don't want. Can you remove it from this PR?

@anishnaik
Copy link
Copy Markdown
Collaborator

Another linter is complaining about this line in MarshalJSON:
return []byte(fmt.Sprintf("%s", cb.Int.String()))

You can change it to
return []byte(cb.Int.String())

and that should be sufficient.

@anishnaik
Copy link
Copy Markdown
Collaborator

@0xZRA, I think there was a misunderstanding on my part around highlight.js (my mistake). What I meant was to revert the changes that prettier made to that .js file. But honestly it doesn't matter. Can you add the .js file back to this PR?

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Mar 3, 2025

@0xZRA, I think there was a misunderstanding on my part around highlight.js (my mistake). What I meant was to revert the changes that prettier made to that .js file. But honestly it doesn't matter. Can you add the .js file back to this PR?

I feel like all these things could be caught and understood by me better before pushing to origin ;( Thanks for your patience

@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Mar 3, 2025

@anishnaik I'm able to reproduce the test(s) failure locally but I'm not sure why sequence of 71 and 7 is triggering violated state (I'm not talking about how x becomes 10 and y becomes 80), I'm rather trying to understand under which condition the failure became possible.

1) InheritedFirstContract.setY(uint256)(71) (block=4, time=165981, gas=12500000, gasprice=1, value=0, sender=0x30000)
2) InheritedFirstContract.setX(uint256)(7) (block=24092, time=602323, gas=12500000, gasprice=1, value=0, sender=0x10000)
[Execution Trace]
 => [call] InheritedFirstContract.setX(uint256)(7) (addr=0xA647ff3c36cFab592509E13860ab8c4F28781a66, value=0, sender=0x10000)
         => [return ()]

[Property Test Execution Trace]
[Execution Trace]
 => [call] InheritedFirstContract.property_never_specific_values()() (addr=0xA647ff3c36cFab592509E13860ab8c4F28781a66, value=0, sender=0x10000)
         => [return (false)]

⇾ Test summary: 1 test(s) passed, 1 test(s) failed

@anishnaik
Copy link
Copy Markdown
Collaborator

Hey @0xZRA, honestly that specific test has been failing more frequently these days, I am unsure why. But luckily doesn't have anything to do with this PR. Going to merge now, thank you so much for the help and great job!

@anishnaik anishnaik merged commit 4f7a62e into crytic:master Mar 3, 2025
13 checks passed
@0xZRA
Copy link
Copy Markdown
Contributor Author

0xZRA commented Mar 3, 2025

@anishnaik Thanks for your guidance and opportunity to help!

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.

3 participants