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

anvil_dumpState is not deterministic in the output #3891

Closed
1 of 2 tasks
KholdStare opened this issue Dec 14, 2022 · 1 comment · Fixed by #3896
Closed
1 of 2 tasks

anvil_dumpState is not deterministic in the output #3891

KholdStare opened this issue Dec 14, 2022 · 1 comment · Fixed by #3896
Labels
T-bug Type: bug

Comments

@KholdStare
Copy link
Contributor

KholdStare commented Dec 14, 2022

Component

Anvil

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (d896050 2022-12-01T21:20:45.538002477Z)

What command(s) is the bug in?

anvil_dumpState

Operating System

Linux

Describe the bug

A useful feature was introduced by @dbeal-eth in #2256 for dumping the current state of the evm and loading it back in. However, the returned hex string is not deterministic. Running it on the same evm state, calling anvil_dumpState back to back results in different strings.

I suspect this is due to the fact that the resulting bytes are serialized from a rust HashMap:

pub struct SerializableState {
    pub accounts: HashMap<Address, SerializableAccountRecord>,
}

And according to https://doc.rust-lang.org/std/collections/struct.HashMap.html, the hashmap is randomly seeded. Seems like the solution is either to sort the keys before serializing, or initialize the hashmap with a deterministic seed with e.g. https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.with_hasher . What do you think?

Why is this even an issue?

Even though the end-result of loading the dumped state into anvil results in the same state no matter the order of the keys, it's not easy to verify whether two dumped states are identical by just looking at the hex strings. We want to use this as an extra sanity check on our deployments - we deploy our contracts on anvil using forge script, then run anvil_dumpState and see if it matches the expected result. Right now it is not possible.

@KholdStare KholdStare added the T-bug Type: bug label Dec 14, 2022
@mattsse
Copy link
Member

mattsse commented Dec 16, 2022

good find, ty.

you're totally right, will change to BTreeMap which is commonly used to sort json objects

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

Successfully merging a pull request may close this issue.

2 participants