Skip to content

Commit

Permalink
different types for system and user logs
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Nov 6, 2023
1 parent 155dea7 commit 3ca96fc
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 44 deletions.
32 changes: 17 additions & 15 deletions core/lib/dal/src/events_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::fmt;

use crate::{models::storage_event::StorageL2ToL1Log, SqlxError, StorageProcessor};
use zksync_types::{
l2_to_l1_log::L2ToL1Log, tx::IncludedTxLocation, MiniblockNumber, VmEvent, H256,
l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log},
tx::IncludedTxLocation,
MiniblockNumber, VmEvent, H256,
};

/// Wrapper around an optional event topic allowing to hex-format it for `COPY` instructions.
Expand Down Expand Up @@ -99,12 +101,12 @@ impl EventsDal<'_, '_> {
.unwrap();
}

/// Saves L2-to-L1 logs from a miniblock. Logs must be ordered by transaction location
/// Saves user L2-to-L1 logs from a miniblock. Logs must be ordered by transaction location
/// and within each transaction.
pub async fn save_l2_to_l1_logs(
pub async fn save_user_l2_to_l1_logs(
&mut self,
block_number: MiniblockNumber,
all_block_l2_to_l1_logs: &[(IncludedTxLocation, Vec<&L2ToL1Log>)],
all_block_l2_to_l1_logs: &[(IncludedTxLocation, Vec<&UserL2ToL1Log>)],
) {
let mut copy = self
.storage
Expand Down Expand Up @@ -139,7 +141,7 @@ impl EventsDal<'_, '_> {
sender,
key,
value,
} = log;
} = log.0;

write_str!(
&mut buffer,
Expand Down Expand Up @@ -273,15 +275,15 @@ mod tests {
}
}

fn create_l2_to_l1_log(tx_number_in_block: u16, index: u8) -> L2ToL1Log {
L2ToL1Log {
fn create_l2_to_l1_log(tx_number_in_block: u16, index: u8) -> UserL2ToL1Log {
UserL2ToL1Log(L2ToL1Log {
shard_id: 0,
is_service: false,
tx_number_in_block,
sender: Address::repeat_byte(index),
key: H256::from_low_u64_be(u64::from(index)),
value: H256::repeat_byte(index),
}
})
}

#[tokio::test]
Expand Down Expand Up @@ -324,7 +326,7 @@ mod tests {
(second_location, second_logs.iter().collect()),
];
conn.events_dal()
.save_l2_to_l1_logs(MiniblockNumber(1), &all_logs)
.save_user_l2_to_l1_logs(MiniblockNumber(1), &all_logs)
.await;

let logs = conn
Expand All @@ -338,9 +340,9 @@ mod tests {
assert_eq!(log.log_index_in_tx as usize, i);
}
for (log, expected_log) in logs.iter().zip(&first_logs) {
assert_eq!(log.key, expected_log.key.as_bytes());
assert_eq!(log.value, expected_log.value.as_bytes());
assert_eq!(log.sender, expected_log.sender.as_bytes());
assert_eq!(log.key, expected_log.0.key.as_bytes());
assert_eq!(log.value, expected_log.0.value.as_bytes());
assert_eq!(log.sender, expected_log.0.sender.as_bytes());
}

let logs = conn
Expand All @@ -354,9 +356,9 @@ mod tests {
assert_eq!(log.log_index_in_tx as usize, i);
}
for (log, expected_log) in logs.iter().zip(&second_logs) {
assert_eq!(log.key, expected_log.key.as_bytes());
assert_eq!(log.value, expected_log.value.as_bytes());
assert_eq!(log.sender, expected_log.sender.as_bytes());
assert_eq!(log.key, expected_log.0.key.as_bytes());
assert_eq!(log.value, expected_log.0.value.as_bytes());
assert_eq!(log.sender, expected_log.0.sender.as_bytes());
}
}
}
16 changes: 14 additions & 2 deletions core/lib/multivm/src/glue/types/vm/vm_block_result.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use zksync_types::l2_to_l1_log::UserL2ToL1Log;

use crate::glue::{GlueFrom, GlueInto};
use crate::interface::{
types::outputs::VmExecutionLogs, CurrentExecutionState, ExecutionResult, Refunds,
Expand Down Expand Up @@ -127,7 +129,12 @@ impl GlueFrom<crate::vm_1_3_2::vm::VmBlockResult> for crate::interface::VmExecut
result,
logs: VmExecutionLogs {
events: value.full_result.events,
user_l2_to_l1_logs: value.full_result.l2_to_l1_logs,
user_l2_to_l1_logs: value
.full_result
.l2_to_l1_logs
.into_iter()
.map(UserL2ToL1Log)
.collect(),
system_l2_to_l1_logs: vec![],
storage_logs: value.full_result.storage_log_queries,
total_log_queries_count: value.full_result.total_log_queries,
Expand Down Expand Up @@ -189,7 +196,12 @@ impl GlueFrom<crate::vm_m6::vm::VmBlockResult> for crate::interface::VmExecution
result,
logs: VmExecutionLogs {
events: value.full_result.events,
user_l2_to_l1_logs: value.full_result.l2_to_l1_logs,
user_l2_to_l1_logs: value
.full_result
.l2_to_l1_logs
.into_iter()
.map(UserL2ToL1Log)
.collect(),
system_l2_to_l1_logs: vec![],
storage_logs: value.full_result.storage_log_queries,
total_log_queries_count: value.full_result.total_log_queries,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::interface::{Halt, VmExecutionStatistics, VmRevertReason};
use zksync_system_constants::PUBLISH_BYTECODE_OVERHEAD;
use zksync_types::event::{extract_long_l2_to_l1_messages, extract_published_bytecodes};
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, SystemL2ToL1Log, UserL2ToL1Log};
use zksync_types::tx::ExecutionMetrics;
use zksync_types::{StorageLogQuery, Transaction, VmEvent};
use zksync_utils::bytecode::bytecode_len_in_bytes;
Expand All @@ -20,8 +20,8 @@ pub struct VmExecutionLogs {
pub events: Vec<VmEvent>,
// For pre-boojum VMs, there was no distinction between user logs and system
// logs and so all the outputted logs were treated as user_l2_to_l1_logs.
pub user_l2_to_l1_logs: Vec<L2ToL1Log>,
pub system_l2_to_l1_logs: Vec<L2ToL1Log>,
pub user_l2_to_l1_logs: Vec<UserL2ToL1Log>,
pub system_l2_to_l1_logs: Vec<SystemL2ToL1Log>,
// This field moved to statistics, but we need to keep it for backward compatibility
pub total_log_queries_count: usize,
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_1_3_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zk_evm_1_3_3::zkevm_opcode_defs::decoding::{
use zk_evm_1_3_3::zkevm_opcode_defs::definitions::RET_IMPLICIT_RETURNDATA_PARAMS_REGISTER;
use zksync_state::WriteStorage;
use zksync_system_constants::MAX_TXS_IN_BLOCK;
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::tx::tx_execution_info::TxExecutionStatus;
use zksync_types::vm_trace::{Call, VmExecutionTrace, VmTrace};
use zksync_types::{L1BatchNumber, StorageLogQuery, VmEvent, U256};
Expand Down Expand Up @@ -428,7 +428,7 @@ impl<H: HistoryMode, S: WriteStorage> VmInstance<S, H> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: l2_to_l1_logs,
user_l2_to_l1_logs: l2_to_l1_logs.into_iter().map(UserL2ToL1Log).collect(),
total_log_queries_count: storage_logs_count
+ log_queries.len()
+ precompile_calls_count,
Expand Down
12 changes: 9 additions & 3 deletions core/lib/multivm/src/versions/vm_latest/implementation/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use zk_evm_1_4_0::aux_structures::Timestamp;
use zksync_state::WriteStorage;

use zksync_types::event::extract_l2tol1logs_from_l1_messenger;
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, SystemL2ToL1Log, UserL2ToL1Log};
use zksync_types::VmEvent;

use crate::interface::types::outputs::VmExecutionLogs;
Expand Down Expand Up @@ -46,8 +46,14 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: user_logs.into_iter().map(|log| log.into()).collect(),
system_l2_to_l1_logs,
user_l2_to_l1_logs: user_logs
.into_iter()
.map(|log| UserL2ToL1Log(log.into()))
.collect(),
system_l2_to_l1_logs: system_l2_to_l1_logs
.into_iter()
.map(SystemL2ToL1Log)
.collect(),
total_log_queries_count,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use zksync_system_constants::BOOTLOADER_ADDRESS;
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::storage_writes_deduplicator::StorageWritesDeduplicator;
use zksync_types::{get_code_key, get_known_code_key, U256};
use zksync_utils::u256_to_h256;
Expand Down Expand Up @@ -42,14 +42,17 @@ fn test_l1_tx_execution() {
let deploy_tx = account.get_deploy_tx(&contract_code, None, TxType::L1 { serial_id: 1 });
let tx_data: TransactionData = deploy_tx.tx.clone().into();

let required_l2_to_l1_logs = vec![L2ToL1Log {
let required_l2_to_l1_logs: Vec<_> = vec![L2ToL1Log {
shard_id: 0,
is_service: true,
tx_number_in_block: 0,
sender: BOOTLOADER_ADDRESS,
key: tx_data.tx_hash(0.into()),
value: u256_to_h256(U256::from(1u32)),
}];
}]
.into_iter()
.map(UserL2ToL1Log)
.collect();

vm.vm.push_transaction(deploy_tx.tx.clone());

Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_m5/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zk_evm_1_3_1::zkevm_opcode_defs::decoding::{
use zk_evm_1_3_1::zkevm_opcode_defs::definitions::RET_IMPLICIT_RETURNDATA_PARAMS_REGISTER;
use zksync_system_constants::MAX_TXS_IN_BLOCK;

use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::tx::tx_execution_info::TxExecutionStatus;
use zksync_types::vm_trace::VmExecutionTrace;
use zksync_types::{L1BatchNumber, StorageLogQuery, VmEvent, U256};
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<S: Storage> VmInstance<S> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: l2_to_l1_logs,
user_l2_to_l1_logs: l2_to_l1_logs.into_iter().map(UserL2ToL1Log).collect(),
system_l2_to_l1_logs: vec![],
total_log_queries_count: storage_logs_count
+ log_queries.len()
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_m6/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zk_evm_1_3_1::zkevm_opcode_defs::decoding::{
};
use zk_evm_1_3_1::zkevm_opcode_defs::definitions::RET_IMPLICIT_RETURNDATA_PARAMS_REGISTER;
use zksync_system_constants::MAX_TXS_IN_BLOCK;
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::tx::tx_execution_info::TxExecutionStatus;
use zksync_types::vm_trace::{Call, VmExecutionTrace, VmTrace};
use zksync_types::{L1BatchNumber, StorageLogQuery, VmEvent, U256};
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<H: HistoryMode, S: Storage> VmInstance<S, H> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: l2_to_l1_logs,
user_l2_to_l1_logs: l2_to_l1_logs.into_iter().map(UserL2ToL1Log).collect(),
system_l2_to_l1_logs: vec![],
total_log_queries_count: storage_logs_count
+ log_queries.len()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use zk_evm_1_3_3::aux_structures::Timestamp;
use zksync_state::WriteStorage;

use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::VmEvent;

use crate::interface::types::outputs::VmExecutionLogs;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: l2_to_l1_logs,
user_l2_to_l1_logs: l2_to_l1_logs.into_iter().map(UserL2ToL1Log).collect(),
system_l2_to_l1_logs: vec![],
total_log_queries_count,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use zk_evm_1_3_3::aux_structures::Timestamp;
use zksync_state::WriteStorage;

use crate::interface::types::outputs::VmExecutionLogs;
use zksync_types::l2_to_l1_log::L2ToL1Log;
use zksync_types::l2_to_l1_log::{L2ToL1Log, UserL2ToL1Log};
use zksync_types::VmEvent;

use crate::vm_virtual_blocks::old_vm::events::merge_events;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl<S: WriteStorage, H: HistoryMode> Vm<S, H> {
VmExecutionLogs {
storage_logs,
events,
user_l2_to_l1_logs: l2_to_l1_logs,
user_l2_to_l1_logs: l2_to_l1_logs.into_iter().map(UserL2ToL1Log).collect(),
system_l2_to_l1_logs: vec![],
total_log_queries_count,
}
Expand Down
11 changes: 11 additions & 0 deletions core/lib/types/src/l2_to_l1_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ pub struct L2ToL1Log {
pub value: H256,
}

/// A struct representing a "user" L2->L1 log, i.e. the one that has been emitted by using the L1Messenger.
/// It is identical to the SystemL2ToL1Log struct, but
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Eq)]
pub struct UserL2ToL1Log(pub L2ToL1Log);

/// A struct representing a "user" L2->L1 log, i.e. the one that has been emitted by using the L1Messenger.
/// It is identical to the SystemL2ToL1Log struct, but
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, Eq)]
pub struct SystemL2ToL1Log(pub L2ToL1Log);

impl L2ToL1Log {
/// Legacy upper bound of L2-to-L1 logs per single L1 batch. This is not used as a limit now,
/// but still determines the minimum number of items in the Merkle tree built from L2-to-L1 logs
Expand Down
14 changes: 8 additions & 6 deletions core/lib/zksync_core/src/state_keeper/io/seal_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use multivm::interface::{FinishedL1Batch, L1BatchEnv};
use zksync_dal::StorageProcessor;
use zksync_system_constants::ACCOUNT_CODE_STORAGE_ADDRESS;
use zksync_types::{
block::unpack_block_info, CURRENT_VIRTUAL_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_ADDRESS,
block::unpack_block_info,
l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log},
CURRENT_VIRTUAL_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_ADDRESS,
};
use zksync_types::{
block::{L1BatchHeader, MiniblockHeader},
Expand Down Expand Up @@ -399,7 +401,7 @@ impl MiniblockSealCommand {
let progress = MINIBLOCK_METRICS.start(MiniblockSealStage::InsertL2ToL1Logs, is_fictive);
transaction
.events_dal()
.save_l2_to_l1_logs(miniblock_number, &user_l2_to_l1_logs)
.save_user_l2_to_l1_logs(miniblock_number, &user_l2_to_l1_logs)
.await;
progress.observe(user_l2_to_l1_log_count);

Expand Down Expand Up @@ -540,18 +542,18 @@ impl MiniblockSealCommand {
fn extract_system_l2_to_l1_logs(
&self,
is_fictive: bool,
) -> Vec<(IncludedTxLocation, Vec<&L2ToL1Log>)> {
) -> Vec<(IncludedTxLocation, Vec<&SystemL2ToL1Log>)> {
self.group_by_tx_location(&self.miniblock.system_l2_to_l1_logs, is_fictive, |log| {
u32::from(log.tx_number_in_block)
u32::from(log.0.tx_number_in_block)
})
}

fn extract_user_l2_to_l1_logs(
&self,
is_fictive: bool,
) -> Vec<(IncludedTxLocation, Vec<&L2ToL1Log>)> {
) -> Vec<(IncludedTxLocation, Vec<&UserL2ToL1Log>)> {
self.group_by_tx_location(&self.miniblock.user_l2_to_l1_logs, is_fictive, |log| {
u32::from(log.tx_number_in_block)
u32::from(log.0.tx_number_in_block)
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use multivm::interface::{ExecutionResult, L2BlockEnv, VmExecutionResultAndLogs};
use multivm::vm_latest::TransactionVmExt;
use std::collections::HashMap;
use zksync_types::l2_to_l1_log::{SystemL2ToL1Log, UserL2ToL1Log};

use zksync_types::{
block::{legacy_miniblock_hash, miniblock_hash, BlockGasCount},
Expand All @@ -19,8 +20,8 @@ pub struct MiniblockUpdates {
pub executed_transactions: Vec<TransactionExecutionResult>,
pub events: Vec<VmEvent>,
pub storage_logs: Vec<StorageLogQuery>,
pub user_l2_to_l1_logs: Vec<L2ToL1Log>,
pub system_l2_to_l1_logs: Vec<L2ToL1Log>,
pub user_l2_to_l1_logs: Vec<UserL2ToL1Log>,
pub system_l2_to_l1_logs: Vec<SystemL2ToL1Log>,
pub new_factory_deps: HashMap<H256, Vec<u8>>,
/// How much L1 gas will it take to submit this block?
pub l1_gas_count: BlockGasCount,
Expand Down

0 comments on commit 3ca96fc

Please sign in to comment.