Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(en): Check block hash correspondence #572

Merged
merged 9 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/bin/system-constants-generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zksync_contracts::{
};
use zksync_state::{InMemoryStorage, StorageView, WriteStorage};
use zksync_types::{
block::legacy_miniblock_hash, ethabi::Token, fee::Fee, l1::L1Tx, l2::L2Tx,
block::MiniblockHasher, ethabi::Token, fee::Fee, l1::L1Tx, l2::L2Tx,
utils::storage_key_for_eth_balance, AccountTreeId, Address, Execute, L1BatchNumber,
L1TxCommonData, L2ChainId, MiniblockNumber, Nonce, ProtocolVersionId, StorageKey, Timestamp,
Transaction, BOOTLOADER_ADDRESS, H256, SYSTEM_CONTEXT_ADDRESS,
Expand Down Expand Up @@ -181,7 +181,7 @@ fn default_l1_batch() -> L1BatchEnv {
first_l2_block: L2BlockEnv {
number: 1,
timestamp: 100,
prev_block_hash: legacy_miniblock_hash(MiniblockNumber(0)),
prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
max_virtual_blocks_to_create: 100,
},
}
Expand Down
34 changes: 14 additions & 20 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl BlocksWeb3Dal<'_, '_> {
mod tests {
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{
block::{miniblock_hash, MiniblockHeader},
block::{MiniblockHasher, MiniblockHeader},
MiniblockNumber, ProtocolVersion, ProtocolVersionId,
};

Expand All @@ -613,16 +613,13 @@ mod tests {
};
conn.blocks_dal().insert_miniblock(&header).await.unwrap();

let block_hash = MiniblockHasher::new(MiniblockNumber(0), 0, H256::zero())
.finalize(ProtocolVersionId::latest());
let block_ids = [
api::BlockId::Number(api::BlockNumber::Earliest),
api::BlockId::Number(api::BlockNumber::Latest),
api::BlockId::Number(api::BlockNumber::Number(0.into())),
api::BlockId::Hash(miniblock_hash(
MiniblockNumber(0),
0,
H256::zero(),
H256::zero(),
)),
api::BlockId::Hash(block_hash),
];
for block_id in block_ids {
let block = conn
Expand All @@ -632,24 +629,18 @@ mod tests {
let block = block.unwrap().unwrap();
assert!(block.transactions.is_empty());
assert_eq!(block.number, U64::zero());
assert_eq!(
block.hash,
miniblock_hash(MiniblockNumber(0), 0, H256::zero(), H256::zero())
);
assert_eq!(block.hash, block_hash);

let tx_count = conn.blocks_web3_dal().get_block_tx_count(block_id).await;
assert_eq!(tx_count.unwrap(), Some((MiniblockNumber(0), 8.into())));
}

let non_existing_block_hash = MiniblockHasher::new(MiniblockNumber(1), 1, H256::zero())
.finalize(ProtocolVersionId::latest());
let non_existing_block_ids = [
api::BlockId::Number(api::BlockNumber::Pending),
api::BlockId::Number(api::BlockNumber::Number(1.into())),
api::BlockId::Hash(miniblock_hash(
MiniblockNumber(1),
1,
H256::zero(),
H256::zero(),
)),
api::BlockId::Hash(non_existing_block_hash),
];
for block_id in non_existing_block_ids {
let block = conn
Expand Down Expand Up @@ -751,14 +742,16 @@ mod tests {
.await
.unwrap();

let hash = miniblock_hash(MiniblockNumber(0), 0, H256::zero(), H256::zero());
let hash = MiniblockHasher::new(MiniblockNumber(0), 0, H256::zero())
.finalize(ProtocolVersionId::latest());
let miniblock_number = conn
.blocks_web3_dal()
.resolve_block_id(api::BlockId::Hash(hash))
.await;
assert_eq!(miniblock_number.unwrap(), Some(MiniblockNumber(0)));

let hash = miniblock_hash(MiniblockNumber(1), 1, H256::zero(), H256::zero());
let hash = MiniblockHasher::new(MiniblockNumber(1), 1, H256::zero())
.finalize(ProtocolVersionId::latest());
let miniblock_number = conn
.blocks_web3_dal()
.resolve_block_id(api::BlockId::Hash(hash))
Expand All @@ -778,7 +771,8 @@ mod tests {
let mut header = MiniblockHeader {
number: MiniblockNumber(0),
timestamp: 0,
hash: miniblock_hash(MiniblockNumber(0), 0, H256::zero(), H256::zero()),
hash: MiniblockHasher::new(MiniblockNumber(0), 0, H256::zero())
.finalize(ProtocolVersionId::latest()),
l1_tx_count: 0,
l2_tx_count: 0,
base_fee_per_gas: 100,
Expand Down
10 changes: 6 additions & 4 deletions core/lib/dal/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fs, time::Duration};

use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{
block::{miniblock_hash, L1BatchHeader, MiniblockHeader},
block::{L1BatchHeader, MiniblockHasher, MiniblockHeader},
fee::{Fee, TransactionExecutionMetrics},
helpers::unix_timestamp_ms,
l1::{L1Tx, OpProcessingType, PriorityQueueType},
Expand Down Expand Up @@ -30,17 +30,19 @@ fn mock_tx_execution_metrics() -> TransactionExecutionMetrics {
}

pub(crate) fn create_miniblock_header(number: u32) -> MiniblockHeader {
let number = MiniblockNumber(number);
let protocol_version = ProtocolVersionId::default();
MiniblockHeader {
number: MiniblockNumber(number),
number,
timestamp: 0,
hash: miniblock_hash(MiniblockNumber(number), 0, H256::zero(), H256::zero()),
hash: MiniblockHasher::new(number, 0, H256::zero()).finalize(protocol_version),
l1_tx_count: 0,
l2_tx_count: 0,
base_fee_per_gas: 100,
l1_gas_price: 100,
l2_fair_gas_price: 100,
base_system_contracts_hashes: BaseSystemContractsHashes::default(),
protocol_version: Some(ProtocolVersionId::default()),
protocol_version: Some(protocol_version),
virtual_blocks: 1,
}
}
Expand Down
12 changes: 5 additions & 7 deletions core/lib/dal/src/transactions_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ impl TransactionsWeb3Dal<'_, '_> {
#[cfg(test)]
mod tests {
use zksync_types::{
block::miniblock_hash, fee::TransactionExecutionMetrics, l2::L2Tx, ProtocolVersion,
block::MiniblockHasher, fee::TransactionExecutionMetrics, l2::L2Tx, ProtocolVersion,
ProtocolVersionId,
};

use super::*;
Expand Down Expand Up @@ -401,15 +402,12 @@ mod tests {
let tx_hash = tx.hash();
prepare_transaction(&mut conn, tx).await;

let block_hash = MiniblockHasher::new(MiniblockNumber(1), 0, H256::zero())
.finalize(ProtocolVersionId::latest());
let block_ids = [
api::BlockId::Number(api::BlockNumber::Latest),
api::BlockId::Number(api::BlockNumber::Number(1.into())),
api::BlockId::Hash(miniblock_hash(
MiniblockNumber(1),
0,
H256::zero(),
H256::zero(),
)),
api::BlockId::Hash(block_hash),
];
let transaction_ids = block_ids
.iter()
Expand Down
34 changes: 18 additions & 16 deletions core/lib/multivm/src/versions/vm_latest/tests/l2_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use zk_evm_1_4_0::aux_structures::Timestamp;
use zksync_state::WriteStorage;
use zksync_system_constants::REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE;
use zksync_types::{
block::{legacy_miniblock_hash, miniblock_hash, pack_block_info},
block::{pack_block_info, MiniblockHasher},
AccountTreeId, Execute, ExecuteTransactionCommon, L1BatchNumber, L1TxCommonData,
MiniblockNumber, StorageKey, Transaction, H160, H256, SYSTEM_CONTEXT_ADDRESS,
SYSTEM_CONTEXT_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION,
SYSTEM_CONTEXT_CURRENT_TX_ROLLING_HASH_POSITION, U256,
MiniblockNumber, ProtocolVersionId, StorageKey, Transaction, H160, H256,
SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_BLOCK_INFO_POSITION,
SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_CURRENT_TX_ROLLING_HASH_POSITION,
U256,
};
use zksync_utils::{h256_to_u256, u256_to_h256};

Expand Down Expand Up @@ -64,7 +65,7 @@ fn test_l2_block_initialization_timestamp() {
vm.vm.bootloader_state.push_l2_block(L2BlockEnv {
number: 1,
timestamp: 0,
prev_block_hash: legacy_miniblock_hash(MiniblockNumber(0)),
prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
max_virtual_blocks_to_create: 1,
});
let l1_tx = get_l1_noop();
Expand All @@ -87,7 +88,7 @@ fn test_l2_block_initialization_number_non_zero() {
let first_l2_block = L2BlockEnv {
number: 0,
timestamp: l1_batch.timestamp,
prev_block_hash: legacy_miniblock_hash(MiniblockNumber(0)),
prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
max_virtual_blocks_to_create: 1,
};

Expand Down Expand Up @@ -246,7 +247,7 @@ fn test_l2_block_new_l2_block() {
let correct_first_block = L2BlockEnv {
number: 1,
timestamp: 1,
prev_block_hash: legacy_miniblock_hash(MiniblockNumber(0)),
prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
max_virtual_blocks_to_create: 1,
};

Expand Down Expand Up @@ -340,7 +341,7 @@ fn test_first_in_batch(
);
storage_ptr.borrow_mut().set_value(
prev_block_hash_position,
legacy_miniblock_hash(MiniblockNumber(miniblock_number - 1)),
MiniblockHasher::legacy_hash(MiniblockNumber(miniblock_number - 1)),
);

// In order to skip checks from the Rust side of the VM, we firstly use some definitely correct L2 block info.
Expand Down Expand Up @@ -369,6 +370,9 @@ fn test_first_in_batch(

#[test]
fn test_l2_block_first_in_batch() {
let prev_block_hash = MiniblockHasher::legacy_hash(MiniblockNumber(0));
let prev_block_hash = MiniblockHasher::new(MiniblockNumber(1), 1, prev_block_hash)
.finalize(ProtocolVersionId::latest());
test_first_in_batch(
1,
1,
Expand All @@ -379,17 +383,15 @@ fn test_l2_block_first_in_batch() {
L2BlockEnv {
number: 2,
timestamp: 2,
prev_block_hash: miniblock_hash(
MiniblockNumber(1),
1,
legacy_miniblock_hash(MiniblockNumber(0)),
H256::zero(),
),
prev_block_hash,
max_virtual_blocks_to_create: 1,
},
None,
);

let prev_block_hash = MiniblockHasher::legacy_hash(MiniblockNumber(0));
let prev_block_hash = MiniblockHasher::new(MiniblockNumber(1), 8, prev_block_hash)
.finalize(ProtocolVersionId::latest());
test_first_in_batch(
8,
1,
Expand All @@ -400,8 +402,8 @@ fn test_l2_block_first_in_batch() {
L2BlockEnv {
number: 2,
timestamp: 9,
prev_block_hash: miniblock_hash(MiniblockNumber(1), 8, legacy_miniblock_hash(MiniblockNumber(0)), H256::zero()),
max_virtual_blocks_to_create: 1
prev_block_hash,
max_virtual_blocks_to_create: 1,
},
Some(Halt::FailedToSetL2Block("The timestamp of the L2 block must be greater than or equal to the timestamp of the current batch".to_string())),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::marker::PhantomData;
use zksync_contracts::BaseSystemContracts;
use zksync_state::{InMemoryStorage, StoragePtr, StorageView, WriteStorage};
use zksync_types::{
block::legacy_miniblock_hash,
block::MiniblockHasher,
get_code_key, get_is_account_key,
helpers::unix_timestamp_ms,
utils::{deployed_address_create, storage_key_for_eth_balance},
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<H: HistoryMode> VmTester<H> {
let last_l2_block = load_last_l2_block(self.storage.clone()).unwrap_or(L2Block {
number: 0,
timestamp: 0,
hash: legacy_miniblock_hash(MiniblockNumber(0)),
hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
});
l1_batch.first_l2_block = L2BlockEnv {
number: last_l2_block.number + 1,
Expand Down Expand Up @@ -258,7 +258,7 @@ pub(crate) fn default_l1_batch(number: L1BatchNumber) -> L1BatchEnv {
first_l2_block: L2BlockEnv {
number: 1,
timestamp,
prev_block_hash: legacy_miniblock_hash(MiniblockNumber(0)),
prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)),
max_virtual_blocks_to_create: 100,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zk_evm_1_4_0::{
use zksync_state::{StoragePtr, WriteStorage};
use zksync_system_constants::BOOTLOADER_ADDRESS;
use zksync_types::{
block::legacy_miniblock_hash, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
block::MiniblockHasher, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
MiniblockNumber,
};
use zksync_utils::h256_to_u256;
Expand Down Expand Up @@ -73,7 +73,9 @@ pub(crate) fn new_vm_state<S: WriteStorage, H: HistoryMode>(
L2Block {
number: l1_batch_env.first_l2_block.number.saturating_sub(1),
timestamp: 0,
hash: legacy_miniblock_hash(MiniblockNumber(l1_batch_env.first_l2_block.number) - 1),
hash: MiniblockHasher::legacy_hash(
MiniblockNumber(l1_batch_env.first_l2_block.number) - 1,
),
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zk_evm_1_3_3::{
use zksync_state::{StoragePtr, WriteStorage};
use zksync_system_constants::BOOTLOADER_ADDRESS;
use zksync_types::{
block::legacy_miniblock_hash, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
block::MiniblockHasher, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
MiniblockNumber,
};
use zksync_utils::h256_to_u256;
Expand Down Expand Up @@ -73,7 +73,9 @@ pub(crate) fn new_vm_state<S: WriteStorage, H: HistoryMode>(
L2Block {
number: l1_batch_env.first_l2_block.number.saturating_sub(1),
timestamp: 0,
hash: legacy_miniblock_hash(MiniblockNumber(l1_batch_env.first_l2_block.number) - 1),
hash: MiniblockHasher::legacy_hash(
MiniblockNumber(l1_batch_env.first_l2_block.number) - 1,
),
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zk_evm_1_3_3::{
use zksync_state::{StoragePtr, WriteStorage};
use zksync_system_constants::BOOTLOADER_ADDRESS;
use zksync_types::{
block::legacy_miniblock_hash, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
block::MiniblockHasher, zkevm_test_harness::INITIAL_MONOTONIC_CYCLE_COUNTER, Address,
MiniblockNumber,
};
use zksync_utils::h256_to_u256;
Expand Down Expand Up @@ -73,7 +73,9 @@ pub(crate) fn new_vm_state<S: WriteStorage, H: HistoryMode>(
L2Block {
number: l1_batch_env.first_l2_block.number.saturating_sub(1),
timestamp: 0,
hash: legacy_miniblock_hash(MiniblockNumber(l1_batch_env.first_l2_block.number) - 1),
hash: MiniblockHasher::legacy_hash(
MiniblockNumber(l1_batch_env.first_l2_block.number) - 1,
),
}
};

Expand Down
Loading