Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ pub struct Config {
/// the initial balance of each deployed test contract
pub initial_balance: U256,
/// the block.number value during EVM execution
#[serde(deserialize_with = "crate::deser_u64_to_u256")]
#[serde(
deserialize_with = "crate::deserialize_u64_to_u256",
serialize_with = "crate::serialize_u64_or_u256"
)]
pub block_number: U256,
/// pins the block number for the state fork
pub fork_block_number: Option<u64>,
Expand All @@ -367,7 +370,10 @@ pub struct Config {
/// The `block.coinbase` value during EVM execution.
pub block_coinbase: Address,
/// The `block.timestamp` value during EVM execution.
#[serde(deserialize_with = "crate::deser_u64_to_u256")]
#[serde(
deserialize_with = "crate::deserialize_u64_to_u256",
serialize_with = "crate::serialize_u64_or_u256"
)]
pub block_timestamp: U256,
/// The `block.difficulty` value during EVM execution.
pub block_difficulty: u64,
Expand Down Expand Up @@ -3737,8 +3743,8 @@ mod tests {
block_coinbase = '0x0000000000000000000000000000000000000000'
block_difficulty = 0
block_prevrandao = '0x0000000000000000000000000000000000000000000000000000000000000000'
block_number = "0x1"
block_timestamp = "0x1"
block_number = 1
block_timestamp = 1
use_literal_content = false
bytecode_hash = 'ipfs'
cbor_metadata = true
Expand Down
24 changes: 22 additions & 2 deletions crates/config/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use foundry_compilers::artifacts::{
remappings::{Remapping, RemappingError},
};
use revm::primitives::hardfork::SpecId;
use serde::{Deserialize, Deserializer, de::Error};
use serde::{Deserialize, Deserializer, Serializer, de::Error};
use std::{
io,
path::{Path, PathBuf},
Expand Down Expand Up @@ -214,7 +214,7 @@ where
}

/// Deserialize into `U256` from either a `u64` or a `U256` hex string.
pub fn deser_u64_to_u256<'de, D>(deserializer: D) -> Result<U256, D::Error>
pub fn deserialize_u64_to_u256<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
D: Deserializer<'de>,
{
Expand All @@ -231,6 +231,26 @@ where
}
}

/// Serialize `U256` as `u64` if it fits, otherwise as a hex string.
/// If the number fits into a i64, serialize it as number without quotation marks.
/// If the number fits into a u64, serialize it as a stringified number with quotation marks.
/// Otherwise, serialize it as a hex string with quotation marks.
pub fn serialize_u64_or_u256<S>(n: &U256, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// The TOML specification handles integers as i64 so the number representation is limited to
// i64. If the number is larger than `i64::MAX` and up to `u64::MAX`, we serialize it as a
// string to avoid losing precision.
if let Ok(n_i64) = i64::try_from(*n) {
serializer.serialize_i64(n_i64)
} else if let Ok(n_u64) = u64::try_from(*n) {
serializer.serialize_str(&n_u64.to_string())
} else {
serializer.serialize_str(&format!("{n:#x}"))
}
}

/// Helper type to parse both `u64` and `U256`
#[derive(Clone, Copy, Deserialize)]
#[serde(untagged)]
Expand Down
10 changes: 8 additions & 2 deletions crates/evm/core/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,17 @@ pub struct Env {
pub block_coinbase: Address,

/// the block.timestamp value during EVM execution
#[serde(deserialize_with = "foundry_config::deser_u64_to_u256")]
#[serde(
deserialize_with = "foundry_config::deserialize_u64_to_u256",
serialize_with = "foundry_config::serialize_u64_or_u256"
)]
pub block_timestamp: U256,

/// the block.number value during EVM execution"
#[serde(deserialize_with = "foundry_config::deser_u64_to_u256")]
#[serde(
deserialize_with = "foundry_config::deserialize_u64_to_u256",
serialize_with = "foundry_config::serialize_u64_or_u256"
)]
pub block_number: U256,

/// the block.difficulty value during EVM execution
Expand Down
8 changes: 4 additions & 4 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,11 +1016,11 @@ prompt_timeout = 120
sender = "0x1804c8ab1f12e6bbf3894d4083f33e07309d1f38"
tx_origin = "0x1804c8ab1f12e6bbf3894d4083f33e07309d1f38"
initial_balance = "0xffffffffffffffffffffffff"
block_number = "0x1"
block_number = 1
gas_limit = 1073741824
block_base_fee_per_gas = 0
block_coinbase = "0x0000000000000000000000000000000000000000"
block_timestamp = "0x1"
block_timestamp = 1
block_difficulty = 0
block_prevrandao = "0x0000000000000000000000000000000000000000000000000000000000000000"
memory_limit = 134217728
Expand Down Expand Up @@ -1242,15 +1242,15 @@ exclude = []
"sender": "0x1804c8ab1f12e6bbf3894d4083f33e07309d1f38",
"tx_origin": "0x1804c8ab1f12e6bbf3894d4083f33e07309d1f38",
"initial_balance": "0xffffffffffffffffffffffff",
"block_number": "0x1",
"block_number": 1,
"fork_block_number": null,
"chain_id": null,
"gas_limit": 1073741824,
"code_size_limit": null,
"gas_price": null,
"block_base_fee_per_gas": 0,
"block_coinbase": "0x0000000000000000000000000000000000000000",
"block_timestamp": "0x1",
"block_timestamp": 1,
"block_difficulty": 0,
"block_prevrandao": "0x0000000000000000000000000000000000000000000000000000000000000000",
"block_gas_limit": null,
Expand Down