-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Fix truncated hex in anvil dump_state #8216
Fix truncated hex in anvil dump_state #8216
Conversation
crates/anvil/src/eth/backend/db.rs
Outdated
@@ -348,7 +348,7 @@ pub struct SerializableAccountRecord { | |||
pub nonce: u64, | |||
pub balance: U256, | |||
pub code: Bytes, | |||
pub storage: BTreeMap<U256, U256>, | |||
pub storage: BTreeMap<U256, B256>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be flipped, the key should always be b256
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I change also the key type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's good, but the value should stay U256, BTreeMap<B256, U256>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I the value is U256 the bug is not corrected:
#8179
{
"0x0000000000000000000000000000000000000000000000000000000000000000":"0x1",
"0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc":"0x68e44eb31e111028c41598e4535be7468674d0a", // <--- The truncated zero
"0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103":"0x9e2803ad9ecc0a848c8a045329524d295f6ee44f",
"0xd30e835d3f35624761057ff5b27d558f97bd5be034621e62240e5c0b784abe68":"0x9965507d1a55bcc2695c58ba16fb37d819b0a4dc"}},
@@ -348,7 +348,7 @@ pub struct SerializableAccountRecord { | |||
pub nonce: u64, | |||
pub balance: U256, | |||
pub code: Bytes, | |||
pub storage: BTreeMap<U256, U256>, | |||
pub storage: BTreeMap<B256, B256>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is also how the regular genesis account is formatted with hash -> hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see an example of genesis.json storage field:
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x02",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x04",
"0x29ecdbdf95c7f6ceec92d6150c697aa14abeb0f8595dd58d808842ea237d8494": "0x01",
"0x6aa118c6537572d8b515a9f9154be55a3377a8de7991cd23bf6e5ceb368688e3": "0x01",
...
}
The type is pub storage: BTreeMap<B256, U256>
, but it doesnt resolve the issue:
#8179
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please cargo +nightly fmt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good
@@ -564,7 +564,7 @@ impl Backend { | |||
slot: U256, | |||
val: B256, | |||
) -> DatabaseResult<()> { | |||
self.db.write().await.set_storage_at(address, slot, U256::from_be_bytes(val.0)) | |||
self.db.write().await.set_storage_at(address, B256::from(slot), B256::from(val.0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
B256::from(val.0)
is not needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something went wrong here, why are all lines changed?
line endings?
Hi @AxelAramburu why did you close your PR? Do you need any help / pointers? Would be great to get this fix in |
@AxelAramburu Running into this issue. Can you re-open? |
What remains to be done here? I am also having issues decoding the state dump due to odd-length fields which are expected to be hex encoded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actual changes look good, can we undo the changes in backend/mem/mod.rs @zerosnacks then this should be good
Maybe this is wrong, but I think there should be a "round-trip" test in general for the dump state functionality. Given a state, does dump_state -> load_state -> dump_state produce the same result as the first dump_state? It should catch any random regressions that may happen in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, we should be able to easily add one
@yash-atreya could you please add such a test? but we can also do this as a followup
* Fix truncated hex in anvil dump_state * Change type of key * fixs * rustfmt * fix --------- Co-authored-by: unknown <[email protected]> Co-authored-by: Matthias Seitz <[email protected]> Co-authored-by: Yash Atreya <[email protected]>
Motivation
Fixes #8179
An issue reported is leading zeros in hex can be truncated and leading to deserialize failure with
anvil_dumpState
Solution
Like @mattsse recommend in the issue post, I change the type of storage from
U256
toB256
.foundry/crates/anvil/src/eth/backend/db.rs
Line 345 in f673066
The modifications don't break the actual implemented tests:
Before
After
Let me know if I can improve my contribution