Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
chore: use simple StorageLog struct instead of LogQuery when possible (
Browse files Browse the repository at this point in the history
…matter-labs#2252)

There are many fields in LogQuery on the core side (it also exists in
zk_evm) that are unused and often just set to some garbage value but
that is very confusing.

This PR changes the uses of LogQuery in the VmInterface to StorageLog,
which only has the necessary fields.

---------

Co-authored-by: perekopskiy <[email protected]>
  • Loading branch information
2 people authored and gabrieldemian committed Jun 21, 2024
1 parent 84b331d commit 437cfed
Show file tree
Hide file tree
Showing 81 changed files with 607 additions and 714 deletions.
2 changes: 1 addition & 1 deletion core/bin/snapshots_creator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async fn create_l2_block(
.await
.unwrap();
conn.storage_logs_dal()
.insert_storage_logs(l2_block_number, &[(H256::zero(), block_logs)])
.insert_storage_logs(l2_block_number, &block_logs)
.await
.unwrap();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE storage_logs ALTER COLUMN tx_hash SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE storage_logs ALTER COLUMN tx_hash DROP NOT NULL;
37 changes: 23 additions & 14 deletions core/lib/dal/src/contract_verification_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use zksync_types::{
DeployContractCalldata, VerificationIncomingRequest, VerificationInfo, VerificationRequest,
VerificationRequestStatus,
},
get_code_key, Address, CONTRACT_DEPLOYER_ADDRESS, FAILED_CONTRACT_DEPLOYMENT_BYTECODE_HASH,
event::DEPLOY_EVENT_SIGNATURE,
Address, CONTRACT_DEPLOYER_ADDRESS,
};
use zksync_utils::address_to_h256;

use crate::{models::storage_verification_request::StorageVerificationRequest, Core};

Expand Down Expand Up @@ -288,7 +290,7 @@ impl ContractVerificationDal<'_, '_> {
&mut self,
address: Address,
) -> anyhow::Result<Option<(Vec<u8>, DeployContractCalldata)>> {
let hashed_key = get_code_key(&address).hashed_key();
let address_h256 = address_to_h256(&address);
let Some(row) = sqlx::query!(
r#"
SELECT
Expand All @@ -298,24 +300,31 @@ impl ContractVerificationDal<'_, '_> {
FROM
(
SELECT
*
miniblock_number,
tx_hash,
topic3
FROM
storage_logs
events
WHERE
storage_logs.hashed_key = $1
ORDER BY
miniblock_number DESC,
operation_number DESC
address = $1
AND topic1 = $2
AND topic4 = $3
LIMIT
1
) storage_logs
JOIN factory_deps ON factory_deps.bytecode_hash = storage_logs.value
LEFT JOIN transactions ON transactions.hash = storage_logs.tx_hash
) deploy_event
JOIN factory_deps ON factory_deps.bytecode_hash = deploy_event.topic3
LEFT JOIN transactions ON transactions.hash = deploy_event.tx_hash
WHERE
storage_logs.value != $2
deploy_event.miniblock_number <= (
SELECT
MAX(number)
FROM
miniblocks
)
"#,
hashed_key.as_bytes(),
FAILED_CONTRACT_DEPLOYMENT_BYTECODE_HASH.as_bytes()
CONTRACT_DEPLOYER_ADDRESS.as_bytes(),
DEPLOY_EVENT_SIGNATURE.as_bytes(),
address_h256.as_bytes(),
)
.fetch_optional(self.storage.conn())
.await?
Expand Down
1 change: 0 additions & 1 deletion core/lib/dal/src/models/storage_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub struct DbStorageLog {
pub key: H256,
pub value: H256,
pub operation_number: u64,
pub tx_hash: H256,
pub l2_block_number: L2BlockNumber,
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/dal/src/pruning_dal/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ async fn insert_l2_block_storage_logs(
storage_logs: Vec<StorageLog>,
) {
conn.storage_logs_dal()
.insert_storage_logs(l2_block_number, &[(H256::zero(), storage_logs)])
.insert_storage_logs(l2_block_number, &storage_logs)
.await
.unwrap();
}
Expand Down
8 changes: 4 additions & 4 deletions core/lib/dal/src/snapshots_creator_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod tests {
logs.sort_unstable_by_key(|log| log.key.hashed_key());

conn.storage_logs_dal()
.insert_storage_logs(L2BlockNumber(1), &[(H256::zero(), logs.clone())])
.insert_storage_logs(L2BlockNumber(1), &logs)
.await
.unwrap();
let mut written_keys: Vec<_> = logs.iter().map(|log| log.key).collect();
Expand Down Expand Up @@ -198,7 +198,7 @@ mod tests {
let all_new_logs: Vec<_> = new_logs.chain(updated_logs).collect();
let all_new_logs_len = all_new_logs.len();
conn.storage_logs_dal()
.insert_storage_logs(L2BlockNumber(2), &[(H256::zero(), all_new_logs)])
.insert_storage_logs(L2BlockNumber(2), &all_new_logs)
.await
.unwrap();
conn.storage_logs_dedup_dal()
Expand Down Expand Up @@ -271,14 +271,14 @@ mod tests {
StorageLog::new_write_log(key, H256::zero()),
];
conn.storage_logs_dal()
.insert_storage_logs(L2BlockNumber(1), &[(H256::zero(), phantom_writes)])
.insert_storage_logs(L2BlockNumber(1), &phantom_writes)
.await
.unwrap();
// initial writes are intentionally not inserted.

let real_write = StorageLog::new_write_log(key, H256::repeat_byte(2));
conn.storage_logs_dal()
.insert_storage_logs(L2BlockNumber(2), &[(H256::zero(), vec![real_write])])
.insert_storage_logs(L2BlockNumber(2), &[real_write])
.await
.unwrap();
conn.storage_logs_dedup_dal()
Expand Down
45 changes: 20 additions & 25 deletions core/lib/dal/src/storage_logs_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ impl StorageLogsDal<'_, '_> {
pub async fn insert_storage_logs(
&mut self,
block_number: L2BlockNumber,
logs: &[(H256, Vec<StorageLog>)],
logs: &[StorageLog],
) -> DalResult<()> {
self.insert_storage_logs_inner(block_number, logs, 0).await
}

async fn insert_storage_logs_inner(
&mut self,
block_number: L2BlockNumber,
logs: &[(H256, Vec<StorageLog>)],
logs: &[StorageLog],
mut operation_number: u32,
) -> DalResult<()> {
let logs_len = logs.len();
let copy = CopyStatement::new(
"COPY storage_logs(
hashed_key, address, key, value, operation_number, tx_hash, miniblock_number,
hashed_key, address, key, value, operation_number, miniblock_number,
created_at, updated_at
)
FROM STDIN WITH (DELIMITER '|')",
Expand All @@ -53,23 +53,21 @@ impl StorageLogsDal<'_, '_> {

let mut buffer = String::new();
let now = Utc::now().naive_utc().to_string();
for (tx_hash, logs) in logs {
for log in logs {
write_str!(
&mut buffer,
r"\\x{hashed_key:x}|\\x{address:x}|\\x{key:x}|\\x{value:x}|",
hashed_key = log.key.hashed_key(),
address = log.key.address(),
key = log.key.key(),
value = log.value
);
writeln_str!(
&mut buffer,
r"{operation_number}|\\x{tx_hash:x}|{block_number}|{now}|{now}"
);

operation_number += 1;
}
for log in logs {
write_str!(
&mut buffer,
r"\\x{hashed_key:x}|\\x{address:x}|\\x{key:x}|\\x{value:x}|",
hashed_key = log.key.hashed_key(),
address = log.key.address(),
key = log.key.key(),
value = log.value
);
writeln_str!(
&mut buffer,
r"{operation_number}|{block_number}|{now}|{now}"
);

operation_number += 1;
}
copy.send(buffer.as_bytes()).await
}
Expand Down Expand Up @@ -117,7 +115,7 @@ impl StorageLogsDal<'_, '_> {
pub async fn append_storage_logs(
&mut self,
block_number: L2BlockNumber,
logs: &[(H256, Vec<StorageLog>)],
logs: &[StorageLog],
) -> DalResult<()> {
let operation_number = sqlx::query!(
r#"
Expand Down Expand Up @@ -565,7 +563,6 @@ impl StorageLogsDal<'_, '_> {
key,
value,
operation_number,
tx_hash,
miniblock_number
FROM
storage_logs
Expand All @@ -585,7 +582,6 @@ impl StorageLogsDal<'_, '_> {
key: H256::from_slice(&row.key),
value: H256::from_slice(&row.value),
operation_number: row.operation_number as u64,
tx_hash: H256::from_slice(&row.tx_hash),
l2_block_number: L2BlockNumber(row.miniblock_number as u32),
})
.collect()
Expand Down Expand Up @@ -745,7 +741,6 @@ mod tests {
.await
.unwrap();

let logs = [(H256::zero(), logs)];
conn.storage_logs_dal()
.insert_storage_logs(L2BlockNumber(number), &logs)
.await
Expand Down Expand Up @@ -783,7 +778,7 @@ mod tests {

// Add more logs and check log ordering.
let third_log = StorageLog::new_write_log(first_key, H256::repeat_byte(3));
let more_logs = [(H256::repeat_byte(1), vec![third_log])];
let more_logs = vec![third_log];
conn.storage_logs_dal()
.append_storage_logs(L2BlockNumber(1), &more_logs)
.await
Expand Down
11 changes: 5 additions & 6 deletions core/lib/dal/src/storage_logs_dedup_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use zksync_db_connection::{
instrument::{CopyStatement, InstrumentExt},
};
use zksync_types::{
snapshots::SnapshotStorageLog, zk_evm_types::LogQuery, AccountTreeId, Address, L1BatchNumber,
StorageKey, H256,
snapshots::SnapshotStorageLog, AccountTreeId, Address, L1BatchNumber, StorageKey, StorageLog,
H256,
};
use zksync_utils::u256_to_h256;

pub use crate::models::storage_log::DbInitialWrite;
use crate::Core;
Expand All @@ -24,7 +23,7 @@ impl StorageLogsDedupDal<'_, '_> {
pub async fn insert_protective_reads(
&mut self,
l1_batch_number: L1BatchNumber,
read_logs: &[LogQuery],
read_logs: &[StorageLog],
) -> DalResult<()> {
let read_logs_len = read_logs.len();
let copy = CopyStatement::new(
Expand All @@ -40,8 +39,8 @@ impl StorageLogsDedupDal<'_, '_> {
let mut bytes: Vec<u8> = Vec::new();
let now = Utc::now().naive_utc().to_string();
for log in read_logs.iter() {
let address_str = format!("\\\\x{}", hex::encode(log.address.0));
let key_str = format!("\\\\x{}", hex::encode(u256_to_h256(log.key).0));
let address_str = format!("\\\\x{}", hex::encode(log.key.address()));
let key_str = format!("\\\\x{}", hex::encode(log.key.key()));
let row = format!(
"{}|{}|{}|{}|{}\n",
l1_batch_number, address_str, key_str, now, now
Expand Down
Loading

0 comments on commit 437cfed

Please sign in to comment.