Skip to content
Open
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
50 changes: 25 additions & 25 deletions lib/batch_types/src/batch_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy::consensus::{BlobTransactionSidecar, SidecarBuilder, SimpleCoder};
use alloy::primitives::{Address, B256, BlockNumber, U256, keccak256};
use alloy::primitives::{B256, BlockNumber, U256, keccak256};
use alloy::sol_types::SolValue;
use blake2::{Blake2s256, Digest};
use serde::{Deserialize, Serialize};
Expand All @@ -14,38 +14,36 @@ use zksync_os_types::{

const PUBDATA_SOURCE_CALLDATA: u8 = 0;

/// Commitment information about a batch.
/// Contains enough data to restore `StoredBatchInfo` that got applied on-chain.
/// Contains enough data to construct public input hash.
/// todo: these fields should be a part of `CommitBatchInfo` but needs to be changed on L1 contracts' side first
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct BatchInfo {
pub struct CommitBatchInfoExt {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bikeshedding: _Ext suffix makes it look this is an extension trait. Maybe, use ExtendedCommitBatchInfo?

#[serde(flatten)]
pub commit_info: CommitBatchInfo,
/// Chain's diamond proxy address on L1.
// todo: this should not be a part of this struct as this is static information for the entire chain
// but we cannot remove it without breaking backwards compatibility
pub chain_address: Address,
/// L1 protocol upgrade transaction that was finalized in this batch. Missing for the vast
/// majority of batches.
pub upgrade_tx_hash: Option<B256>,
/// Blobs sidecar that should be sent with commit operation.
pub blob_sidecar: Option<BlobTransactionSidecar>,
pub protocol_version: ProtocolSemanticVersion,
}

impl BatchInfo {
impl CommitBatchInfoExt {
#[allow(clippy::too_many_arguments)]
pub fn new(
pub fn build(
blocks: Vec<(
&BlockOutput,
&BlockContext,
&[ZkTransaction],
&zksync_os_merkle_tree::TreeBatchOutput,
)>,
Comment on lines 34 to 39
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether this is in the scope of the PR, but this looks like too much data supplied, since BlockContext and TreeBatchOutput are only read for the last block.

chain_id: u64,
chain_address: Address,
batch_number: u64,
pubdata_mode: PubdataMode,
sl_chain_id: u64,
multichain_root: B256,
protocol_version: &ProtocolSemanticVersion,
) -> Self {
) -> (Self, Option<BlobTransactionSidecar>) {
let mut priority_operations_hash = keccak256([]);
let mut number_of_layer1_txs = 0;
let mut number_of_layer2_txs = 0;
Expand Down Expand Up @@ -178,19 +176,21 @@ impl BatchInfo {
operator_da_input: da_fields.operator_da_input,
sl_chain_id,
};
Self {
commit_info,
chain_address,
upgrade_tx_hash,
blob_sidecar: da_fields.blob_sidecar,
}
(
Self {
commit_info,
protocol_version: protocol_version.clone(),
upgrade_tx_hash,
},
da_fields.blob_sidecar,
)
}

/// Calculate keccak256 hash of BatchOutput part of public input
pub fn public_input_hash(&self, protocol_version: &ProtocolSemanticVersion) -> B256 {
pub fn public_input_hash(&self) -> B256 {
let commit_info = &self.commit_info;
let upgrade_tx_hash = self.upgrade_tx_hash.unwrap_or(B256::ZERO);
match protocol_version.minor {
match self.protocol_version.minor {
// v30 and v31 use different packed layouts for batch output hash:
// v31 inserts number_of_layer2_txs between L1 tx count and priority_operations_hash.
30 => B256::from(keccak256(
Expand Down Expand Up @@ -225,12 +225,12 @@ impl BatchInfo {
)
.abi_encode_packed(),
)),
_ => panic!("Unsupported protocol version: {protocol_version}"),
_ => panic!("Unsupported protocol version: {}", self.protocol_version),
}
}

pub fn into_stored(self, protocol_version: &ProtocolSemanticVersion) -> StoredBatchInfo {
let commitment = self.public_input_hash(protocol_version);
pub fn into_stored(self) -> StoredBatchInfo {
let commitment = self.public_input_hash();
let commit_info = self.commit_info;
StoredBatchInfo {
batch_number: commit_info.batch_number,
Expand All @@ -246,15 +246,15 @@ impl BatchInfo {
}
}

impl Deref for BatchInfo {
impl Deref for CommitBatchInfoExt {
type Target = CommitBatchInfo;

fn deref(&self) -> &Self::Target {
&self.commit_info
}
}

impl DerefMut for BatchInfo {
impl DerefMut for CommitBatchInfoExt {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.commit_info
}
Expand Down
27 changes: 15 additions & 12 deletions lib/batch_types/src/batch_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use alloy::sol;
use alloy::sol_types::{Eip712Domain, SolStruct};
use serde::{Deserialize, Serialize};
use zksync_os_contract_interface::calldata::encode_commit_batch_data;
use zksync_os_contract_interface::models::StoredBatchInfo;
use zksync_os_contract_interface::models::{CommitBatchInfo, StoredBatchInfo};
use zksync_os_types::ProtocolSemanticVersion;

use crate::BatchInfo;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BatchSignatureSet(Vec<ValidatedBatchSignature>);

Expand Down Expand Up @@ -62,15 +60,17 @@ impl BatchSignature {
/// Sign a batch for `commitBatchesMultisig`
pub async fn sign_batch(
prev_batch_info: &StoredBatchInfo,
batch_info: &BatchInfo,
commit_batch_info: &CommitBatchInfo,
diamond_proxy_sl: Address,
sl_chain_id: u64,
multisig_committer: Address,
protocol_version: &ProtocolSemanticVersion,
private_key: &PrivateKeySigner,
) -> Self {
let digest = eip712_multisig_digest(
prev_batch_info,
batch_info,
commit_batch_info,
diamond_proxy_sl,
sl_chain_id,
multisig_committer,
protocol_version,
Expand All @@ -82,7 +82,8 @@ impl BatchSignature {
pub fn verify_signature(
self,
prev_batch_info: &StoredBatchInfo,
batch_info: &BatchInfo,
commit_batch_info: &CommitBatchInfo,
diamond_proxy_sl: Address,
sl_chain_id: u64,
multisig_committer: Address,
protocol_version: &ProtocolSemanticVersion,
Expand All @@ -92,7 +93,8 @@ impl BatchSignature {
.0
.recover_address_from_prehash(&eip712_multisig_digest(
prev_batch_info,
batch_info,
commit_batch_info,
diamond_proxy_sl,
sl_chain_id,
multisig_committer,
protocol_version,
Expand Down Expand Up @@ -125,21 +127,22 @@ sol! {
/// and L1 domain parameters.
fn eip712_multisig_digest(
prev_batch_info: &StoredBatchInfo,
batch_info: &BatchInfo,
commit_batch_info: &CommitBatchInfo,
diamond_proxy_sl: Address,
sl_chain_id: u64,
multisig_committer: Address,
protocol_version: &ProtocolSemanticVersion,
) -> B256 {
let batch_data = encode_commit_batch_data(
prev_batch_info,
batch_info.commit_info.clone(),
commit_batch_info.clone(),
protocol_version.minor,
);

let message = CommitBatchesMultisig {
chainAddress: batch_info.chain_address,
processBatchFrom: U256::from(batch_info.batch_number),
processBatchTo: U256::from(batch_info.batch_number),
chainAddress: diamond_proxy_sl,
processBatchFrom: U256::from(commit_batch_info.batch_number),
processBatchTo: U256::from(commit_batch_info.batch_number),
batchData: batch_data.into(),
};

Expand Down
2 changes: 1 addition & 1 deletion lib/batch_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ mod block_merkle_tree_data;
pub use block_merkle_tree_data::BlockMerkleTreeData;

mod batch_info;
pub use batch_info::{BatchInfo, DiscoveredCommittedBatch};
pub use batch_info::{CommitBatchInfoExt, DiscoveredCommittedBatch};
13 changes: 9 additions & 4 deletions lib/batch_verification/src/main_node/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct BatchVerificationRunner {
verify_request_tx: mpsc::Sender<VerifyBatch>,
verify_result_rx: mpsc::Receiver<PeerVerifyBatchResult>,
l1_chain_id: u64,
diamond_proxy_sl: Address,
multisig_committer: Address,
last_committed_batch_number: u64,
}
Expand Down Expand Up @@ -155,6 +156,7 @@ impl BatchVerificationRunner {
verify_request_tx: component.verify_request_tx,
verify_result_rx: component.verify_result_rx,
l1_chain_id: component.l1_state.sl_chain_id,
diamond_proxy_sl: component.l1_state.diamond_proxy_address_sl(),
multisig_committer: component.l1_state.validator_timelock_sl,
last_committed_batch_number: component.last_committed_batch_number,
}
Expand Down Expand Up @@ -397,10 +399,11 @@ impl BatchVerificationRunner {

let Ok(validated_signature) = signature.verify_signature(
&batch_envelope.batch.previous_stored_batch_info,
&batch_envelope.batch.batch_info,
&batch_envelope.batch.batch_info.commit_info,
self.diamond_proxy_sl,
self.l1_chain_id,
self.multisig_committer,
&batch_envelope.batch.protocol_version,
&batch_envelope.batch.batch_info.protocol_version,
) else {
BATCH_VERIFICATION_SEQUENCER_METRICS.failed_responses[&"invalid_signature"].inc();
tracing::warn!(
Expand Down Expand Up @@ -479,10 +482,11 @@ mod tests {
let addr = signer.address();
let sig = BatchSignature::sign_batch(
&batch.batch.previous_stored_batch_info,
&batch.batch.batch_info,
&batch.batch.batch_info.commit_info,
batch.batch.chain_address,
CHAIN_ID,
MULTISIG_COMMITTER_DUMMY.parse().unwrap(),
&batch.batch.protocol_version,
&batch.batch.batch_info.protocol_version,
&signer,
)
.await;
Expand Down Expand Up @@ -521,6 +525,7 @@ mod tests {
verify_request_tx,
verify_result_rx,
l1_chain_id: CHAIN_ID,
diamond_proxy_sl: Address::ZERO,
multisig_committer: MULTISIG_COMMITTER_DUMMY.parse().unwrap(),
last_committed_batch_number,
};
Expand Down
11 changes: 5 additions & 6 deletions lib/batch_verification/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy::primitives::{Address, B256};
/// This module is for sharing various testing utilities and helpers.
use tokio::sync::watch;
use zksync_os_batch_types::BatchInfo;
use zksync_os_batch_types::CommitBatchInfoExt;
use zksync_os_contract_interface::models::{CommitBatchInfo, DACommitmentScheme, StoredBatchInfo};
use zksync_os_l1_sender::batcher_model::{BatchEnvelope, BatchMetadata, MissingSignature};
use zksync_os_storage_api::{FinalityStatus, ReadFinality};
Expand Down Expand Up @@ -70,18 +70,17 @@ pub fn dummy_batch_metadata(batch_number: u64, from: u64, to: u64) -> BatchMetad
// unused
last_block_timestamp: Some(0),
},
batch_info: BatchInfo {
batch_info: CommitBatchInfoExt {
commit_info: dummy_commit_batch_info(batch_number, from, to),
chain_address: Address::ZERO,
protocol_version: ProtocolSemanticVersion::legacy_genesis_version(),
upgrade_tx_hash: None,
blob_sidecar: None,
},
chain_address: Address::ZERO,
blob_sidecar: None,
first_block_number: from,
last_block_number: to,
pubdata_mode: zksync_os_types::PubdataMode::Calldata,
tx_count: 0,
execution_version: 1,
protocol_version: ProtocolSemanticVersion::legacy_genesis_version(),
computational_native_used: None,
logs: vec![],
messages: vec![],
Expand Down
8 changes: 4 additions & 4 deletions lib/batch_verification/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use secrecy::{ExposeSecret, SecretString};
use std::str::FromStr;
use tokio::sync::{broadcast, mpsc};
use zksync_os_batch_types::BlockMerkleTreeData;
use zksync_os_batch_types::{BatchInfo, BatchSignature};
use zksync_os_batch_types::{BatchSignature, CommitBatchInfoExt};
use zksync_os_contract_interface::l1_discovery::{BatchVerificationSL, L1State};
use zksync_os_interface::types::BlockOutput;
use zksync_os_merkle_tree::TreeBatchOutput;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<Finality: ReadFinality, ReadState: ReadStateHistory>
let state_view = self.read_state.state_view_at(request.last_block_number)?;
let multichain_root = read_multichain_root(state_view);

let batch_info = BatchInfo::new(
let (batch_info, _) = CommitBatchInfoExt::build(
blocks
.iter()
.map(|(block_output, replay_record, tree)| {
Expand All @@ -135,7 +135,6 @@ impl<Finality: ReadFinality, ReadState: ReadStateHistory>
})
.collect(),
self.chain_id,
self.diamond_proxy_sl,
request.batch_number,
request.pubdata_mode,
self.l1_state.sl_chain_id,
Expand All @@ -153,7 +152,8 @@ impl<Finality: ReadFinality, ReadState: ReadStateHistory>

let signature = BatchSignature::sign_batch(
&request.prev_commit_data,
&batch_info,
&batch_info.commit_info,
self.diamond_proxy_sl,
self.l1_state.sl_chain_id,
self.l1_state.validator_timelock_sl,
&blocks.first().unwrap().1.protocol_version,
Expand Down
5 changes: 3 additions & 2 deletions lib/batch_verification/src/verify_batch_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ pub(crate) fn encode_verify_batch_request<E>(
batch_envelope: &BatchForSigning<E>,
request_id: u64,
) -> Result<VerifyBatch, BatchVerificationError> {
let execution_protocol_version = u16::try_from(batch_envelope.batch.protocol_version.minor)
.map_err(|_| BatchVerificationError::Internal("protocol version overflow".into()))?;
let execution_protocol_version =
u16::try_from(batch_envelope.batch.batch_info.protocol_version.minor)
.map_err(|_| BatchVerificationError::Internal("protocol version overflow".into()))?;
let commit_data = encode_commit_data(
batch_envelope.batch.batch_info.commit_info.clone(),
execution_protocol_version,
Expand Down
Loading
Loading