Skip to content

Commit

Permalink
fix: Apply Validium fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilLuta committed May 11, 2024
1 parent e166512 commit 40cb410
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 42 deletions.
14 changes: 12 additions & 2 deletions core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use tokio::{
};
use zksync_block_reverter::{BlockReverter, NodeRole};
use zksync_commitment_generator::{
commitment_post_processor::{
CommitmentPostProcessor, RollupCommitmentPostProcessor, ValidiumCommitmentPostProcessor,
},
input_generation::{InputGenerator, RollupInputGenerator, ValidiumInputGenerator},
CommitmentGenerator,
};
Expand Down Expand Up @@ -346,17 +349,20 @@ async fn run_core(
)
.await?;

let (l1_batch_commit_data_generator, input_generator): (
let (l1_batch_commit_data_generator, input_generator, commitment_post_processor): (
Arc<dyn L1BatchCommitDataGenerator>,
Box<dyn InputGenerator>,
Box<dyn CommitmentPostProcessor>,
) = match config.optional.l1_batch_commit_data_generator_mode {
L1BatchCommitDataGeneratorMode::Rollup => (
Arc::new(RollupModeL1BatchCommitDataGenerator {}),
Box::new(RollupInputGenerator),
Box::new(RollupCommitmentPostProcessor),
),
L1BatchCommitDataGeneratorMode::Validium => (
Arc::new(ValidiumModeL1BatchCommitDataGenerator {}),
Box::new(ValidiumInputGenerator),
Box::new(ValidiumCommitmentPostProcessor),
),
};

Expand Down Expand Up @@ -388,7 +394,11 @@ async fn run_core(
.build()
.await
.context("failed to build a commitment_generator_pool")?;
let commitment_generator = CommitmentGenerator::new(commitment_generator_pool, input_generator);
let commitment_generator = CommitmentGenerator::new(
commitment_generator_pool,
input_generator,
commitment_post_processor,
);
app_health.insert_component(commitment_generator.health_check())?;
let commitment_generator_handle = tokio::spawn(commitment_generator.run(stop_receiver.clone()));

Expand Down
31 changes: 20 additions & 11 deletions core/lib/basic_types/src/basic_fri_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type Eip4844BlobsInner = [Option<Blob>; MAX_4844_BLOBS_PER_BLOCK];

/// External, wrapper struct, containing EIP 4844 blobs and enforcing invariants.
/// Current invariants:
/// - there are between [1, 16] blobs
/// - there are between [0, 16] blobs
/// - [1, 16] blobs for Rollup Mode
/// - 0 blobs for Validium Mode
/// - all blobs are of the same size [`EIP_4844_BLOB_SIZE`]
/// Creating a structure violating these constraints will panic.
///
Expand All @@ -36,27 +38,31 @@ impl Eip4844Blobs {
pub fn blobs(self) -> Eip4844BlobsInner {
self.blobs
}
}

impl Eip4844Blobs {
/// No blobs are provided for Validium.
pub fn validium() -> Self {
Self {
blobs: Default::default(),
}
}

pub fn encode(self) -> Vec<u8> {
self.blobs().into_iter().flatten().flatten().collect()
}

pub fn decode(blobs: &[u8]) -> anyhow::Result<Self> {
// Current abstraction is not perfect. If ran as rollup, it should always have blobs.
// Validium has no blobs, but we can't distinguish between the two at the call-site.
// This abstraction has to be rethought, without leaking information into prover subsystems.
if blobs.is_empty() {
return Ok(Self::validium());
}

let mut chunks: Vec<Blob> = blobs
.chunks(EIP_4844_BLOB_SIZE)
.map(|chunk| chunk.into())
.collect();

if let Some(last_chunk) = chunks.last_mut() {
last_chunk.resize(EIP_4844_BLOB_SIZE, 0u8);
} else {
return Err(anyhow::anyhow!(
"cannot create Eip4844Blobs, received empty pubdata"
));
}

if chunks.len() > MAX_4844_BLOBS_PER_BLOCK {
return Err(anyhow::anyhow!(
"cannot create Eip4844Blobs, expected max {}, received {}",
Expand All @@ -65,6 +71,9 @@ impl Eip4844Blobs {
));
}

// We can unwrap here, because of the check at start of the function
chunks.last_mut().unwrap().resize(EIP_4844_BLOB_SIZE, 0u8);

let mut blobs: [Option<Blob>; MAX_4844_BLOBS_PER_BLOCK] = Default::default();
for (i, blob) in chunks.into_iter().enumerate() {
blobs[i] = Some(blob);
Expand Down
12 changes: 8 additions & 4 deletions core/lib/types/src/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ impl SerializeCommitment for StateDiffRecord {

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Serialize, Deserialize))]
struct L1BatchAuxiliaryCommonOutput {
pub struct L1BatchAuxiliaryCommonOutput {
l2_l1_logs_merkle_root: H256,
protocol_version: ProtocolVersionId,
}

/// Block Output produced by Virtual Machine
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(test, derive(Serialize, Deserialize))]
enum L1BatchAuxiliaryOutput {
pub enum L1BatchAuxiliaryOutput {
PreBoojum {
common: L1BatchAuxiliaryCommonOutput,
l2_l1_logs_linear_hash: H256,
Expand Down Expand Up @@ -528,8 +528,8 @@ impl L1BatchPassThroughData {
#[derive(Debug, Clone)]
pub struct L1BatchCommitment {
pass_through_data: L1BatchPassThroughData,
auxiliary_output: L1BatchAuxiliaryOutput,
meta_parameters: L1BatchMetaParameters,
pub auxiliary_output: L1BatchAuxiliaryOutput,
pub meta_parameters: L1BatchMetaParameters,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -574,6 +574,10 @@ impl L1BatchCommitment {
self.meta_parameters.clone()
}

pub fn aux_output(&self) -> L1BatchAuxiliaryOutput {
self.auxiliary_output.clone()
}

pub fn l2_l1_logs_merkle_root(&self) -> H256 {
self.auxiliary_output.common().l2_l1_logs_merkle_root
}
Expand Down
33 changes: 27 additions & 6 deletions core/lib/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use zksync_circuit_breaker::{
CircuitBreakerChecker, CircuitBreakers,
};
use zksync_commitment_generator::{
commitment_post_processor::{
CommitmentPostProcessor, RollupCommitmentPostProcessor, ValidiumCommitmentPostProcessor,
},
input_generation::{InputGenerator, RollupInputGenerator, ValidiumInputGenerator},
CommitmentGenerator,
};
Expand Down Expand Up @@ -76,6 +79,9 @@ use zksync_node_fee_model::{
};
use zksync_node_genesis::{ensure_genesis_state, GenesisParams};
use zksync_object_store::{ObjectStore, ObjectStoreFactory};
use zksync_proof_data_handler::blob_processor::{
BlobProcessor, RollupBlobProcessor, ValidiumBlobProcessor,
};
use zksync_shared_metrics::{InitStage, APP_METRICS};
use zksync_state::{PostgresStorageCaches, RocksdbStorageOptions};
use zksync_types::{ethabi::Contract, fee_model::FeeModelConfig, Address, L2ChainId};
Expand Down Expand Up @@ -605,13 +611,24 @@ pub async fn initialize_components(
tracing::info!("initialized ETH-Watcher in {elapsed:?}");
}

let input_generator: Box<dyn InputGenerator> = if genesis_config
.l1_batch_commit_data_generator_mode
let (input_generator, commitment_post_processor, blob_processor): (
Box<dyn InputGenerator>,
Box<dyn CommitmentPostProcessor>,
Arc<dyn BlobProcessor>,
) = if genesis_config.l1_batch_commit_data_generator_mode
== L1BatchCommitDataGeneratorMode::Validium
{
Box::new(ValidiumInputGenerator)
(
Box::new(ValidiumInputGenerator),
Box::new(ValidiumCommitmentPostProcessor),
Arc::new(ValidiumBlobProcessor),
)
} else {
Box::new(RollupInputGenerator)
(
Box::new(RollupInputGenerator),
Box::new(RollupCommitmentPostProcessor),
Arc::new(RollupBlobProcessor),
)
};

if components.contains(&Component::EthTxAggregator) {
Expand Down Expand Up @@ -771,6 +788,7 @@ pub async fn initialize_components(
.context("proof_data_handler_config")?,
store_factory.create_store().await,
connection_pool.clone(),
blob_processor,
stop_receiver.clone(),
)));
}
Expand All @@ -781,8 +799,11 @@ pub async fn initialize_components(
.build()
.await
.context("failed to build commitment_generator_pool")?;
let commitment_generator =
CommitmentGenerator::new(commitment_generator_pool, input_generator);
let commitment_generator = CommitmentGenerator::new(
commitment_generator_pool,
input_generator,
commitment_post_processor,
);
app_health.insert_component(commitment_generator.health_check())?;
task_futures.push(tokio::spawn(
commitment_generator.run(stop_receiver.clone()),
Expand Down
52 changes: 52 additions & 0 deletions core/node/commitment_generator/src/commitment_post_processor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::fmt;

use zksync_types::{
commitment::{L1BatchAuxiliaryOutput, L1BatchCommitment},
H256,
};

#[derive(Debug)]
pub struct ValidiumCommitmentPostProcessor;

#[derive(Debug)]
pub struct RollupCommitmentPostProcessor;

/// Definition of trait handling post processing the L1BatchCommitment depending on the DA solution
/// being utilized.
pub trait CommitmentPostProcessor: 'static + fmt::Debug + Send + Sync {
fn post_process_commitment(&self, commitment: L1BatchCommitment) -> L1BatchCommitment;
}

impl CommitmentPostProcessor for ValidiumCommitmentPostProcessor {
fn post_process_commitment(&self, mut commitment: L1BatchCommitment) -> L1BatchCommitment {
let aux_output = match commitment.aux_output() {
L1BatchAuxiliaryOutput::PostBoojum {
common,
system_logs_linear_hash,
state_diffs_compressed,
state_diffs_hash,
aux_commitments,
blob_linear_hashes,
blob_commitments,
} => L1BatchAuxiliaryOutput::PostBoojum {
common,
system_logs_linear_hash,
state_diffs_compressed,
state_diffs_hash,
aux_commitments,
blob_linear_hashes: vec![H256::zero(); blob_linear_hashes.len()],
blob_commitments: vec![H256::zero(); blob_commitments.len()],
},
_ => commitment.aux_output(),
};

commitment.auxiliary_output = aux_output;
commitment
}
}

impl CommitmentPostProcessor for RollupCommitmentPostProcessor {
fn post_process_commitment(&self, commitment: L1BatchCommitment) -> L1BatchCommitment {
commitment
}
}
8 changes: 8 additions & 0 deletions core/node/commitment_generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ use zksync_types::{
use zksync_utils::h256_to_u256;

use crate::{
commitment_post_processor::CommitmentPostProcessor,
input_generation::InputGenerator,
metrics::{CommitmentStage, METRICS},
utils::{bootloader_initial_content_commitment, events_queue_commitment},
};

pub mod commitment_post_processor;
pub mod input_generation;
mod metrics;
mod utils;
Expand All @@ -33,17 +35,20 @@ pub struct CommitmentGenerator {
connection_pool: ConnectionPool<Core>,
health_updater: HealthUpdater,
input_generator: Box<dyn InputGenerator>,
commitment_post_processor: Box<dyn CommitmentPostProcessor>,
}

impl CommitmentGenerator {
pub fn new(
connection_pool: ConnectionPool<Core>,
input_generator: Box<dyn InputGenerator>,
commitment_post_processor: Box<dyn CommitmentPostProcessor>,
) -> Self {
Self {
connection_pool,
health_updater: ReactiveHealthCheck::new("commitment_generator").1,
input_generator,
commitment_post_processor,
}
}

Expand Down Expand Up @@ -271,6 +276,9 @@ impl CommitmentGenerator {
let latency =
METRICS.generate_commitment_latency_stage[&CommitmentStage::Calculate].start();
let commitment = L1BatchCommitment::new(input);
let commitment = self
.commitment_post_processor
.post_process_commitment(commitment);
let artifacts = commitment.artifacts();
let latency = latency.observe();
tracing::debug!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use zksync_commitment_generator::{
commitment_post_processor::{
CommitmentPostProcessor, RollupCommitmentPostProcessor, ValidiumCommitmentPostProcessor,
},
input_generation::{InputGenerator, RollupInputGenerator, ValidiumInputGenerator},
CommitmentGenerator,
};
Expand All @@ -16,18 +19,30 @@ use crate::{

pub struct CommitmentGeneratorLayer {
input_generator: Box<dyn InputGenerator>,
commitment_post_processor: Box<dyn CommitmentPostProcessor>,
}

impl CommitmentGeneratorLayer {
pub fn new(commit_data_generator_mode: L1BatchCommitDataGeneratorMode) -> Self {
let input_generator: Box<dyn InputGenerator> =
if commit_data_generator_mode == L1BatchCommitDataGeneratorMode::Validium {
Box::new(ValidiumInputGenerator)
} else {
Box::new(RollupInputGenerator)
};
let (input_generator, commitment_post_processor): (
Box<dyn InputGenerator>,
Box<dyn CommitmentPostProcessor>,
) = if commit_data_generator_mode == L1BatchCommitDataGeneratorMode::Validium {
(
Box::new(ValidiumInputGenerator),
Box::new(ValidiumCommitmentPostProcessor),
)
} else {
(
Box::new(RollupInputGenerator),
Box::new(RollupCommitmentPostProcessor),
)
};

Self { input_generator }
Self {
input_generator,
commitment_post_processor,
}
}
}

Expand All @@ -41,7 +56,11 @@ impl WiringLayer for CommitmentGeneratorLayer {
let pool_resource = context.get_resource::<PoolResource<MasterPool>>().await?;
let main_pool = pool_resource.get().await.unwrap();

let commitment_generator = CommitmentGenerator::new(main_pool, self.input_generator);
let commitment_generator = CommitmentGenerator::new(
main_pool,
self.input_generator,
self.commitment_post_processor,
);

let AppHealthCheckResource(app_health) = context.get_resource_or_default().await;
app_health
Expand Down
Loading

0 comments on commit 40cb410

Please sign in to comment.