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

feat: Blockhash returning expected value #480

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion crates/script/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ impl PreSimulationState {
.into_iter()
.map(|btx| {
let mut tx = TransactionWithMetadata::from_tx_request(btx.transaction);
tx.zk = btx.zk_tx.map(|metadata| ZkTransaction { factory_deps: metadata.factory_deps });
tx.zk =
btx.zk_tx.map(|metadata| ZkTransaction { factory_deps: metadata.factory_deps });
tx.rpc = btx.rpc.expect("missing broadcastable tx rpc url");
tx
})
Expand Down
48 changes: 48 additions & 0 deletions crates/zksync/core/src/vm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,29 @@ where
PaymasterParams::default(),
);

let current_block_number = ecx.env.block.number;

let blockhashes = (0..=255)
.map(|i| current_block_number - alloy_primitives::U256::from(i))
nbaztec marked this conversation as resolved.
Show resolved Hide resolved
.map(|index| {
let block_hash = ecx.block_hash(index);
block_hash.map(|block_hash| (index, block_hash))
})
.take_while(|result| match result {
Ok((_, block_hash)) => !block_hash.is_zero(),
Err(_) => false,
})
.filter_map(Result::ok)
.collect();

let call_ctx = CallContext {
tx_caller: env.tx.caller,
msg_sender: env.tx.caller,
contract: transact_to.to_address(),
delegate_as: None,
block_number: env.block.number,
block_timestamp: env.block.timestamp,
blockhashes,
block_basefee: min(max_fee_per_gas.to_ru256(), env.block.basefee),
is_create,
is_static: false,
Expand Down Expand Up @@ -151,6 +167,21 @@ where
PaymasterParams::default(),
);

let current_block_number = ecx.env.block.number;

let blockhashes = (0..=255)
.map(|i| current_block_number - alloy_primitives::U256::from(i))
.map(|index| {
let block_hash = ecx.block_hash(index);
block_hash.map(|block_hash| (index, block_hash))
})
.take_while(|result| match result {
Ok((_, block_hash)) => !block_hash.is_zero(),
Err(_) => false,
})
.filter_map(Result::ok)
.collect();

let call_ctx = CallContext {
tx_caller: ecx.env.tx.caller,
msg_sender: call.caller,
Expand All @@ -159,6 +190,7 @@ where
block_number: ecx.env.block.number,
block_timestamp: ecx.env.block.timestamp,
block_basefee: min(max_fee_per_gas.to_ru256(), ecx.env.block.basefee),
blockhashes,
is_create: true,
is_static: false,
};
Expand Down Expand Up @@ -201,6 +233,21 @@ where
PaymasterParams::default(),
);

let current_block_number = ecx.env.block.number;

let blockhashes = (0..=255)
.map(|i| current_block_number - alloy_primitives::U256::from(i))
.map(|index| {
let block_hash = ecx.block_hash(index);
block_hash.map(|block_hash| (index, block_hash))
})
.take_while(|result| match result {
Ok((_, block_hash)) => !block_hash.is_zero(),
Err(_) => false,
})
.filter_map(Result::ok)
.collect();

// address and caller are specific to the type of call:
// Call | StaticCall => { address: to, caller: contract.address }
// CallCode => { address: contract.address, caller: contract.address }
Expand All @@ -215,6 +262,7 @@ where
},
block_number: ecx.env.block.number,
block_timestamp: ecx.env.block.timestamp,
blockhashes,
block_basefee: min(max_fee_per_gas.to_ru256(), ecx.env.block.basefee),
is_create: false,
is_static: call.is_static,
Expand Down
10 changes: 8 additions & 2 deletions crates/zksync/core/src/vm/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ pub struct CallContext {
pub is_create: bool,
/// Whether the current call is a static call.
pub is_static: bool,
/// L1 blockhashes to ensure consistency when returning environment data in L2, similar to
/// other cases (e.g., Block number, Block timestamp)
pub blockhashes: HashMap<alloy_primitives::U256, alloy_primitives::FixedBytes<32>>,
}

/// A tracer to allow for foundry-specific functionality.
Expand Down Expand Up @@ -302,8 +305,11 @@ impl<S: Send, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for CheatcodeTracer
if current.code_address == SYSTEM_CONTEXT_ADDRESS &&
calldata.starts_with(&SELECTOR_BLOCK_HASH)
{
self.farcall_handler.set_immediate_return(rU256::ZERO.to_be_bytes_vec());
return
let index = U256::from_big_endian(&calldata[calldata.len() - 4..]);
if let Some(blockhash) = self.call_context.blockhashes.get(&index.to_ru256()) {
self.farcall_handler.set_immediate_return(blockhash.to_vec());
return;
}
}
}

Expand Down
Loading