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

fix: block overrides inconsistencies #4746

Merged
merged 1 commit into from
Sep 24, 2023
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
27 changes: 24 additions & 3 deletions crates/rpc/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,37 @@ impl<T: Serialize> Serialize for Rich<T> {
/// BlockOverrides is a set of header fields to override.
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
#[allow(missing_docs)]
pub struct BlockOverrides {
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Overrides the block number.
///
/// For `eth_callMany` this will be the block number of the first simulated block. Each
/// following block increments its block number by 1
// Note: geth uses `number`, erigon uses `blockNumber`
#[serde(default, skip_serializing_if = "Option::is_none", alias = "blockNumber")]
pub number: Option<U256>,
/// Overrides the difficulty of the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub difficulty: Option<U256>,
#[serde(default, skip_serializing_if = "Option::is_none")]
/// Overrides the timestamp of the block.
// Note: geth uses `time`, erigon uses `timestamp`
#[serde(default, skip_serializing_if = "Option::is_none", alias = "timestamp")]
pub time: Option<U64>,
/// Overrides the gas limit of the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gas_limit: Option<U64>,
/// Overrides the coinbase address of the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub coinbase: Option<Address>,
/// Overrides the prevrandao of the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub random: Option<H256>,
/// Overrides the basefee of the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub base_fee: Option<U256>,
/// A dictionary that maps blockNumber to a user-defined hash. It could be queried from the
/// solidity opcode BLOCKHASH.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_hash: Option<BTreeMap<u64, H256>>,
}

#[cfg(test)]
Expand Down Expand Up @@ -380,4 +395,10 @@ mod tests {
let deserialized: Block = serde_json::from_str(&serialized).unwrap();
assert_eq!(block, deserialized);
}

#[test]
fn block_overrides() {
let s = r#"{"blockNumber": "0xe39dd0"}"#;
let _overrides = serde_json::from_str::<BlockOverrides>(s).unwrap();
}
}
19 changes: 16 additions & 3 deletions crates/rpc/rpc/src/eth/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ where
}

// apply block overrides
if let Some(block_overrides) = overrides.block {
if let Some(mut block_overrides) = overrides.block {
if let Some(block_hashes) = block_overrides.block_hash.take() {
// override block hashes
db.block_hashes
.extend(block_hashes.into_iter().map(|(num, hash)| (U256::from(num), hash)))
}
apply_block_overrides(*block_overrides, &mut env.block);
}

Expand Down Expand Up @@ -400,8 +405,16 @@ impl CallFees {

/// Applies the given block overrides to the env
fn apply_block_overrides(overrides: BlockOverrides, env: &mut BlockEnv) {
let BlockOverrides { number, difficulty, time, gas_limit, coinbase, random, base_fee } =
overrides;
let BlockOverrides {
number,
difficulty,
time,
gas_limit,
coinbase,
random,
base_fee,
block_hash: _,
} = overrides;

if let Some(number) = number {
env.number = number;
Expand Down
Loading