Skip to content

Commit

Permalink
rename to BlockContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Sep 28, 2023
1 parent 911c378 commit 3d75095
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
41 changes: 21 additions & 20 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,35 @@ type L2TxResult = (
VmExecutionResultAndLogs,
Block<TransactionVariant>,
HashMap<U256, Vec<U256>>,
NextBlock,
BlockContext,
);

impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
pub fn create_l1_batch_env<ST: ReadStorage>(
&self,
storage: StoragePtr<ST>,
) -> (L1BatchEnv, NextBlock) {
) -> (L1BatchEnv, BlockContext) {
let last_l2_block = load_last_l2_block(storage);
let next_block = NextBlock::from_current(
let block_ctx = BlockContext::from_current(
self.current_batch,
self.current_miniblock,
self.current_timestamp,
);
let next_block = next_block.new_batch();
let block_ctx = block_ctx.new_batch();
let batch_env = L1BatchEnv {
// TODO: set the previous batch hash properly (take from fork, when forking, and from local storage, when this is not the first block).
previous_batch_hash: None,
number: L1BatchNumber::from(next_block.batch),
timestamp: next_block.timestamp,
number: L1BatchNumber::from(block_ctx.batch),
timestamp: block_ctx.timestamp,
l1_gas_price: self.l1_gas_price,
fair_l2_gas_price: L2_GAS_PRICE,
fee_account: H160::zero(),
enforced_base_fee: None,
first_l2_block: vm::L2BlockEnv {
// the 'current_miniblock' contains the block that was already produced.
// So the next one should be one higher.
number: next_block.miniblock as u32,
timestamp: next_block.timestamp,
number: block_ctx.miniblock as u32,
timestamp: block_ctx.timestamp,
prev_block_hash: last_l2_block.hash,
// This is only used during zksyncEra block timestamp/number transition.
// In case of starting a new network, it doesn't matter.
Expand All @@ -320,7 +320,7 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
},
};

(batch_env, next_block)
(batch_env, block_ctx)
}

pub fn create_system_env(
Expand Down Expand Up @@ -1043,7 +1043,7 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {

let storage = StorageView::new(&inner.fork_storage).to_rc_ptr();

let (batch_env, next_block) = inner.create_l1_batch_env(storage.clone());
let (batch_env, block_ctx) = inner.create_l1_batch_env(storage.clone());

// if we are impersonating an account, we need to use non-verifying system contracts
let nonverifying_contracts;
Expand Down Expand Up @@ -1192,7 +1192,7 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
}

// The computed block hash here will be different than that in production.
let hash = compute_hash(next_block.miniblock, l2_tx.hash());
let hash = compute_hash(block_ctx.miniblock, l2_tx.hash());

let mut transaction = zksync_types::api::Transaction::from(l2_tx);
let block_hash = inner
Expand All @@ -1207,7 +1207,7 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {

let block = Block {
hash,
number: U64::from(next_block.miniblock),
number: U64::from(block_ctx.miniblock),
timestamp: U256::from(batch_env.timestamp),
l1_batch_number: Some(U64::from(batch_env.number.0)),
transactions: vec![TransactionVariant::Full(transaction)],
Expand All @@ -1228,7 +1228,7 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
vm.execute(vm::VmExecutionMode::Bootloader);

let modified_keys = storage.borrow().modified_storage_keys().clone();
Ok((modified_keys, tx_result, block, bytecodes, next_block))
Ok((modified_keys, tx_result, block, bytecodes, block_ctx))
}

/// Runs L2 transaction and commits it to a new block.
Expand All @@ -1245,7 +1245,7 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
inner.filters.notify_new_pending_transaction(tx_hash);
}

let (keys, result, block, bytecodes, next_block) =
let (keys, result, block, bytecodes, block_ctx) =
self.run_l2_tx_inner(l2_tx.clone(), execution_mode)?;

if let ExecutionResult::Halt { reason } = result.result {
Expand Down Expand Up @@ -1353,9 +1353,9 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
// With the introduction of 'l2 blocks' (and virtual blocks),
// we are adding one l2 block at the end of each batch (to handle things like remaining events etc).
// You can look at insert_fictive_l2_block function in VM to see how this fake block is inserted.
let next_block = next_block.new_block();
let block_ctx = block_ctx.new_block();
let empty_block_at_end_of_batch =
create_empty_block(next_block.miniblock, next_block.timestamp, next_block.batch);
create_empty_block(block_ctx.miniblock, block_ctx.timestamp, block_ctx.batch);

let new_batch = inner.current_batch.saturating_add(1);
let mut current_miniblock = inner.current_miniblock;
Expand Down Expand Up @@ -1406,14 +1406,15 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
}
}

/// Keeps track of next block's batch number, miniblock number and timestamp.
pub struct NextBlock {
/// Keeps track of a block's batch number, miniblock number and timestamp.
/// Useful for keeping track of the current context when creating multiple blocks.
pub struct BlockContext {
pub batch: u32,
pub miniblock: u64,
pub timestamp: u64,
}

impl NextBlock {
impl BlockContext {
/// Create the current instance that represents the latest block.
pub fn from_current(batch: u32, miniblock: u64, timestamp: u64) -> Self {
Self {
Expand All @@ -1433,7 +1434,7 @@ impl NextBlock {
}

/// Create the next batch instance that uses the same batch number, and has all other parameters incremented by `1`.
pub fn new_block(&self) -> NextBlock {
pub fn new_block(&self) -> BlockContext {
Self {
batch: self.batch,
miniblock: self.miniblock.saturating_add(1),
Expand Down
17 changes: 8 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
// build and insert new blocks
for i in 0..num_blocks {
// roll the vm
let (keys, bytecodes, next_block) = {
let (keys, bytecodes, block_ctx) = {
let storage = StorageView::new(&node.fork_storage).to_rc_ptr();

// system_contract.contacts_for_l2_call() will give playground contracts
// we need these to use the unsafeOverrideBlock method in SystemContext.sol
let bootloader_code = node.system_contracts.contacts_for_l2_call();
let (batch_env, mut next_block) = node.create_l1_batch_env(storage.clone());
let (batch_env, mut block_ctx) = node.create_l1_batch_env(storage.clone());
// override the next block's timestamp to match up with interval for subsequent blocks
if i != 0 {
next_block.timestamp = node.current_timestamp.saturating_add(interval_ms);
block_ctx.timestamp = node.current_timestamp.saturating_add(interval_ms);
}

// init vm
Expand All @@ -126,7 +126,7 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
.map(|b| bytecode_to_factory_dep(b.original.clone()))
.collect();
let modified_keys = storage.borrow().modified_storage_keys().clone();
(modified_keys, bytecodes, next_block)
(modified_keys, bytecodes, block_ctx)
};

for (key, value) in keys.iter() {
Expand All @@ -147,16 +147,15 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
)
}

let block =
create_empty_block(next_block.miniblock, next_block.timestamp, next_block.batch);
let block = create_empty_block(block_ctx.miniblock, block_ctx.timestamp, block_ctx.batch);

node.block_hashes.insert(block.number.as_u64(), block.hash);
node.blocks.insert(block.hash, block);

// leave node state ready for next interaction
node.current_batch = next_block.batch;
node.current_miniblock = next_block.miniblock;
node.current_timestamp = next_block.timestamp;
node.current_batch = block_ctx.batch;
node.current_miniblock = block_ctx.miniblock;
node.current_timestamp = block_ctx.timestamp;
}
}

Expand Down

0 comments on commit 3d75095

Please sign in to comment.