Skip to content

Commit

Permalink
Rename + brush up StoredL2BlockEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Aug 16, 2024
1 parent 09d4175 commit 83249e5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion core/lib/vm_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use crate::{
VmRevertReasonParsingError,
},
inputs::{
L1BatchEnv, L2BlockEnv, OneshotEnv, PendingL2BlockEnv, SystemEnv, TxExecutionMode,
L1BatchEnv, L2BlockEnv, OneshotEnv, StoredL2BlockEnv, SystemEnv, TxExecutionMode,
VmExecutionMode,
},
outputs::{
Expand Down
16 changes: 8 additions & 8 deletions core/lib/vm_interface/src/types/inputs/l2_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ pub struct L2BlockEnv {
}

impl L2BlockEnv {
pub fn from_l2_block_data(miniblock_execution_data: &L2BlockExecutionData) -> Self {
pub fn from_l2_block_data(execution_data: &L2BlockExecutionData) -> Self {
Self {
number: miniblock_execution_data.number.0,
timestamp: miniblock_execution_data.timestamp,
prev_block_hash: miniblock_execution_data.prev_block_hash,
max_virtual_blocks_to_create: miniblock_execution_data.virtual_blocks,
number: execution_data.number.0,
timestamp: execution_data.timestamp,
prev_block_hash: execution_data.prev_block_hash,
max_virtual_blocks_to_create: execution_data.virtual_blocks,
}
}
}

/// Pending block information used in oneshot transaction / call execution.
// FIXME: come up with a better name
/// Current block information stored in the system context contract. Can be used to set up
/// oneshot transaction / call execution.
#[derive(Debug, Clone, Copy)]
pub struct PendingL2BlockEnv {
pub struct StoredL2BlockEnv {
pub number: u32,
pub timestamp: u64,
pub txs_rolling_hash: H256,
Expand Down
7 changes: 4 additions & 3 deletions core/lib/vm_interface/src/types/inputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use self::{
execution_mode::VmExecutionMode,
l1_batch_env::L1BatchEnv,
l2_block::{L2BlockEnv, PendingL2BlockEnv},
l2_block::{L2BlockEnv, StoredL2BlockEnv},
system_env::{SystemEnv, TxExecutionMode},
};

Expand All @@ -17,6 +17,7 @@ pub struct OneshotEnv {
pub system: SystemEnv,
/// Part of the environment specific to an L1 batch.
pub l1_batch: L1BatchEnv,
/// Part of the environment representing a pending L2 block.
pub pending_block: Option<PendingL2BlockEnv>,
/// Part of the environment representing the current L2 block. Can be used to override storage slots
/// in the system context contract, which are set from `L1BatchEnv.first_l2_block` by default.
pub current_block: Option<StoredL2BlockEnv>,
}
59 changes: 28 additions & 31 deletions core/node/api_server/src/execution_sandbox/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use zksync_dal::{Connection, Core, CoreDal, DalError};
use zksync_multivm::{
interface::{
storage::{ReadStorage, StoragePtr, StorageView, WriteStorage},
BytecodeCompressionError, L1BatchEnv, L2BlockEnv, OneshotEnv, PendingL2BlockEnv, SystemEnv,
BytecodeCompressionError, L1BatchEnv, L2BlockEnv, OneshotEnv, StoredL2BlockEnv, SystemEnv,
TxExecutionMode, VmExecutionMode, VmExecutionResultAndLogs, VmInterface,
},
tracers::StorageInvocations,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub(super) async fn prepare_env_and_storage(
.schedule_values_update(resolved_block_info.state_l2_block_number);
}

let (next_l2_block_info, l2_block_info_to_reset) = load_l2_block_info(
let (next_block, current_block) = load_l2_block_info(
&mut connection,
block_args.is_pending_l2_block(),
&resolved_block_info,
Expand All @@ -85,12 +85,12 @@ pub(super) async fn prepare_env_and_storage(
.context("cannot create `PostgresStorage`")?
.with_caches(setup_args.caches.clone());

let (system, l1_batch) = prepare_env(setup_args, &resolved_block_info, next_l2_block_info);
let (system, l1_batch) = prepare_env(setup_args, &resolved_block_info, next_block);

let env = OneshotEnv {
system,
l1_batch,
pending_block: l2_block_info_to_reset.map(Into::into),
current_block,
};
initialization_stage.observe();
Ok((env, storage))
Expand All @@ -100,23 +100,22 @@ async fn load_l2_block_info(
connection: &mut Connection<'_, Core>,
is_pending_block: bool,
resolved_block_info: &ResolvedBlockInfo,
) -> anyhow::Result<(L2BlockEnv, Option<PendingL2BlockEnv>)> {
let mut l2_block_info_to_reset = None;
let current_l2_block_info =
read_stored_l2_block_info(connection, resolved_block_info.state_l2_block_number)
.await
.context("failed reading L2 block info")?;
) -> anyhow::Result<(L2BlockEnv, Option<StoredL2BlockEnv>)> {
let mut current_block = None;
let next_block = read_stored_l2_block(connection, resolved_block_info.state_l2_block_number)
.await
.context("failed reading L2 block info")?;

let next_l2_block_info = if is_pending_block {
let next_block = if is_pending_block {
L2BlockEnv {
number: current_l2_block_info.number + 1,
number: next_block.number + 1,
timestamp: resolved_block_info.l1_batch_timestamp,
prev_block_hash: resolved_block_info.state_l2_block_hash,
// For simplicity, we assume each L2 block create one virtual block.
// This may be wrong only during transition period.
max_virtual_blocks_to_create: 1,
}
} else if current_l2_block_info.number == 0 {
} else if next_block.number == 0 {
// Special case:
// - For environments, where genesis block was created before virtual block upgrade it doesn't matter what we put here.
// - Otherwise, we need to put actual values here. We cannot create next L2 block with block_number=0 and `max_virtual_blocks_to_create=0`
Expand All @@ -131,7 +130,7 @@ async fn load_l2_block_info(
// We need to reset L2 block info in storage to process transaction in the current block context.
// Actual resetting will be done after `storage_view` is created.
let prev_block_number = resolved_block_info.state_l2_block_number - 1;
let prev_l2_block_info = read_stored_l2_block_info(connection, prev_block_number)
let prev_l2_block = read_stored_l2_block(connection, prev_block_number)
.await
.context("failed reading previous L2 block info")?;

Expand All @@ -152,24 +151,24 @@ async fn load_l2_block_info(
});
}

l2_block_info_to_reset = Some(prev_l2_block_info);
current_block = Some(prev_l2_block);
L2BlockEnv {
number: current_l2_block_info.number,
timestamp: current_l2_block_info.timestamp,
number: next_block.number,
timestamp: next_block.timestamp,
prev_block_hash: prev_block_hash.with_context(|| {
format!("missing hash for previous L2 block #{prev_block_number}")
})?,
max_virtual_blocks_to_create: 1,
}
};

Ok((next_l2_block_info, l2_block_info_to_reset))
Ok((next_block, current_block))
}

fn prepare_env(
setup_args: TxSetupArgs,
resolved_block_info: &ResolvedBlockInfo,
next_l2_block_info: L2BlockEnv,
next_block: L2BlockEnv,
) -> (SystemEnv, L1BatchEnv) {
let TxSetupArgs {
execution_mode,
Expand Down Expand Up @@ -203,7 +202,7 @@ fn prepare_env(
fee_input,
fee_account: *operator_account.address(),
enforced_base_fee,
first_l2_block: next_l2_block_info,
first_l2_block: next_block,
};
(system_env, l1_batch_env)
}
Expand All @@ -220,7 +219,7 @@ impl<S: ReadStorage> VmSandbox<S> {
/// This method is blocking.
pub fn new(storage: S, mut env: OneshotEnv, execution_args: TxExecutionArgs) -> Self {
let mut storage_view = StorageView::new(storage);
Self::setup_storage_view(&mut storage_view, &execution_args, env.pending_block);
Self::setup_storage_view(&mut storage_view, &execution_args, env.current_block);

let protocol_version = env.system.version;
if execution_args.adjust_pubdata_price {
Expand Down Expand Up @@ -251,7 +250,7 @@ impl<S: ReadStorage> VmSandbox<S> {
fn setup_storage_view(
storage_view: &mut StorageView<S>,
execution_args: &TxExecutionArgs,
l2_block_info_to_reset: Option<PendingL2BlockEnv>,
current_block: Option<StoredL2BlockEnv>,
) {
let storage_view_setup_started_at = Instant::now();
if let Some(nonce) = execution_args.enforced_nonce {
Expand All @@ -269,15 +268,13 @@ impl<S: ReadStorage> VmSandbox<S> {
storage_view.set_value(balance_key, u256_to_h256(current_balance));

// Reset L2 block info if necessary.
if let Some(l2_block_info_to_reset) = l2_block_info_to_reset {
if let Some(current_block) = current_block {
let l2_block_info_key = StorageKey::new(
AccountTreeId::new(SYSTEM_CONTEXT_ADDRESS),
SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION,
);
let l2_block_info = pack_block_info(
l2_block_info_to_reset.number as u64,
l2_block_info_to_reset.timestamp,
);
let l2_block_info =
pack_block_info(current_block.number.into(), current_block.timestamp);
storage_view.set_value(l2_block_info_key, u256_to_h256(l2_block_info));

let l2_block_txs_rolling_hash_key = StorageKey::new(
Expand All @@ -286,7 +283,7 @@ impl<S: ReadStorage> VmSandbox<S> {
);
storage_view.set_value(
l2_block_txs_rolling_hash_key,
l2_block_info_to_reset.txs_rolling_hash,
current_block.txs_rolling_hash,
);
}

Expand Down Expand Up @@ -416,10 +413,10 @@ where
}
}

async fn read_stored_l2_block_info(
async fn read_stored_l2_block(
connection: &mut Connection<'_, Core>,
l2_block_number: L2BlockNumber,
) -> anyhow::Result<PendingL2BlockEnv> {
) -> anyhow::Result<StoredL2BlockEnv> {
let l2_block_info_key = StorageKey::new(
AccountTreeId::new(SYSTEM_CONTEXT_ADDRESS),
SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION,
Expand All @@ -439,7 +436,7 @@ async fn read_stored_l2_block_info(
.get_historical_value_unchecked(l2_block_txs_rolling_hash_key.hashed_key(), l2_block_number)
.await?;

Ok(PendingL2BlockEnv {
Ok(StoredL2BlockEnv {
number: l2_block_number_from_state as u32,
timestamp,
txs_rolling_hash,
Expand Down

0 comments on commit 83249e5

Please sign in to comment.