Skip to content

Commit

Permalink
return L1 blockhashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrigada committed Jul 18, 2024
1 parent 1eca012 commit 49786ad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
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))
.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
27 changes: 16 additions & 11 deletions crates/zksync/core/src/vm/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub struct CallContext {
pub is_create: bool,
/// Whether the current call is a static call.
pub is_static: bool,
/// Blockhashes
pub blockhashes: HashMap<alloy_primitives::U256, alloy_primitives::FixedBytes<32>>,
}

/// A tracer to allow for foundry-specific functionality.
Expand Down Expand Up @@ -295,17 +297,20 @@ impl<S: Send, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for CheatcodeTracer
}

// Override blockhash
// if let Opcode::FarCall(_call) = data.opcode.variant.opcode {
// let calldata = get_calldata(&state, memory);
// let current = state.vm_local_state.callstack.current;

// 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
// }
// }
if let Opcode::FarCall(_call) = data.opcode.variant.opcode {
let calldata = get_calldata(&state, memory);
let current = state.vm_local_state.callstack.current;

if current.code_address == SYSTEM_CONTEXT_ADDRESS &&
calldata.starts_with(&SELECTOR_BLOCK_HASH)
{
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;
}
}
}

if let Some(delegate_as) = self.call_context.delegate_as {
if let Opcode::FarCall(_call) = data.opcode.variant.opcode {
Expand Down

0 comments on commit 49786ad

Please sign in to comment.