Skip to content

Commit 66783c6

Browse files
Syzygy106mattsse
andauthored
fix(anvil): set evm block number to input value (#12490)
* fix(anvil): added REVM blocknumber sync with RPC * test(anvil): replicate REVM unsync issue case --------- Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
1 parent f94f589 commit 66783c6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ impl Backend {
286286
)
287287
};
288288

289+
// Sync EVM block.number with genesis for non-fork mode.
290+
// Fork mode syncs in setup_fork_db_config() instead.
291+
if fork.read().is_none() {
292+
let mut write_env = env.write();
293+
write_env.evm_env.block_env.number = U256::from(genesis.number);
294+
}
295+
289296
let start_timestamp = if let Some(fork) = fork.read().as_ref() {
290297
fork.timestamp()
291298
} else {

crates/anvil/tests/it/anvil.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloy_consensus::EMPTY_ROOT_HASH;
44
use alloy_eips::BlockNumberOrTag;
55
use alloy_hardforks::EthereumHardfork;
66
use alloy_network::{ReceiptResponse, TransactionBuilder};
7-
use alloy_primitives::{Address, B256, hex};
7+
use alloy_primitives::{Address, B256, U256, bytes, hex};
88
use alloy_provider::Provider;
99
use alloy_rpc_types::TransactionRequest;
1010
use alloy_sol_types::SolCall;
@@ -139,6 +139,30 @@ async fn test_can_use_default_genesis_block_number() {
139139
assert_eq!(0, provider.get_block(0.into()).await.unwrap().unwrap().header.number);
140140
}
141141

142+
/// Verify that genesis block number affects both RPC and EVM execution layer.
143+
#[tokio::test(flavor = "multi_thread")]
144+
async fn test_number_opcode_reflects_genesis_block_number() {
145+
let genesis_number: u64 = 4242;
146+
let (api, handle) =
147+
spawn(NodeConfig::test().with_genesis_block_number(Some(genesis_number))).await;
148+
let provider = handle.http_provider();
149+
150+
// RPC layer should return configured genesis number
151+
let bn = provider.get_block_number().await.unwrap();
152+
assert_eq!(bn, genesis_number);
153+
154+
// Deploy bytecode that returns block.number
155+
// 0x43 (NUMBER) 0x5f (PUSH0) 0x52 (MSTORE) 0x60 0x20 (PUSH1 0x20) 0x5f (PUSH0) 0xf3 (RETURN)
156+
let target = Address::random();
157+
api.anvil_set_code(target, bytes!("435f5260205ff3")).await.unwrap();
158+
159+
// EVM execution should reflect genesis number (+ 1 for pending block)
160+
let tx = alloy_rpc_types::TransactionRequest::default().with_to(target);
161+
let out = provider.call(tx.into()).await.unwrap();
162+
let returned = U256::from_be_slice(out.as_ref());
163+
assert_eq!(returned, U256::from(genesis_number + 1));
164+
}
165+
142166
#[tokio::test(flavor = "multi_thread")]
143167
async fn test_anvil_recover_signature() {
144168
let (api, handle) = spawn(NodeConfig::test()).await;

0 commit comments

Comments
 (0)