Skip to content

Conversation

zerosnacks
Copy link
Member

@zerosnacks zerosnacks commented Aug 1, 2025

Motivation

Optimistically closes: #11176

Implements backwards compatibility for loading Anvil state dumps generated with v1.2

Solution

Previously:

error: invalid value '<old state file>' for '--load-state <PATH>': failed to parse json file: "<old state file>": invalid type: string "0x1c9c380", expected u64 at line 1 column 129

now succeeds loading

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@zerosnacks zerosnacks marked this pull request as ready for review August 1, 2025 12:17
@zerosnacks zerosnacks moved this to Ready For Review in Foundry Aug 1, 2025
Comment on lines +442 to +458
fn try_from(legacy: LegacyBlockEnv) -> Result<Self, Self::Error> {
Ok(Self {
number: legacy.number.and_then(|v| v.to_u256()).unwrap_or(U256::ZERO),
beneficiary: legacy.beneficiary.unwrap_or(Address::ZERO),
timestamp: legacy.timestamp.and_then(|v| v.to_u256()).unwrap_or(U256::ONE),
gas_limit: legacy.gas_limit.and_then(|v| v.to_u64()).unwrap_or(u64::MAX),
basefee: legacy.basefee.and_then(|v| v.to_u64()).unwrap_or(0),
difficulty: legacy.difficulty.and_then(|v| v.to_u256()).unwrap_or(U256::ZERO),
prevrandao: legacy.prevrandao.or(Some(B256::ZERO)),
blob_excess_gas_and_price: legacy
.blob_excess_gas_and_price
.map(|v| BlobExcessGasAndPrice::new(v.excess_blob_gas, v.blob_gasprice))
.or_else(|| {
Some(BlobExcessGasAndPrice::new(0, BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE))
}),
})
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@zerosnacks zerosnacks changed the title fix(anvil): v1.2 state dump compatibility fix(anvil): v1.2 state load compatibility Aug 1, 2025
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

lgtm!

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

nice, no we finally have some tests to catch this

@mattsse mattsse merged commit dd317d3 into master Aug 2, 2025
22 checks passed
@mattsse mattsse deleted the zerosnacks/anvil-compatibility branch August 2, 2025 08:46
@github-project-automation github-project-automation bot moved this from Ready For Review to Done in Foundry Aug 2, 2025
MerkleBoy pushed a commit to MerkleBoy/foundry that referenced this pull request Sep 17, 2025
* add backwards compat for v1.2 anvil state dumps

* nits

* rename to _compat

* nits

* clean up

* clean up

* clean up, introduce LegacyBlockEnv

* fix tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug(anvil): --load-state does not appear to work

3 participants