Skip to content

Commit

Permalink
refactor: Refactor fee-related types (#3121)
Browse files Browse the repository at this point in the history
## What ❔

- Moves basic logic for fee types close to the type declaration (i.e.,
to `zksync_types`).
- Unifies `PubdataSendingMode` and `PubdataDA`.
- Removes dependency of `zksync_types` on `zksync_config`.

## Why ❔

This allows external apps depending on fee types (e.g., the test node)
to not have behemoths like `zksync_dal` in their dependency graph.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.
  • Loading branch information
slowli authored Oct 21, 2024
1 parent 78839e9 commit cfbcc11
Show file tree
Hide file tree
Showing 31 changed files with 564 additions and 601 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use anyhow::Context;
use zksync_config::{
configs::{
da_client::DAClientConfig, eth_sender::PubdataSendingMode,
secrets::DataAvailabilitySecrets, wallets::Wallets, GeneralConfig, Secrets,
da_client::DAClientConfig, secrets::DataAvailabilitySecrets, wallets::Wallets,
GeneralConfig, Secrets,
},
ContractsConfig, GenesisConfig,
};
Expand Down Expand Up @@ -69,7 +69,9 @@ use zksync_node_framework::{
},
service::{ZkStackService, ZkStackServiceBuilder},
};
use zksync_types::{settlement::SettlementMode, SHARED_BRIDGE_ETHER_TOKEN_ADDRESS};
use zksync_types::{
pubdata_da::PubdataSendingMode, settlement::SettlementMode, SHARED_BRIDGE_ETHER_TOKEN_ADDRESS,
};
use zksync_vlog::prometheus::PrometheusExporterConfig;

/// Macro that looks into a path to fetch an optional config,
Expand Down Expand Up @@ -189,7 +191,7 @@ impl MainNodeBuilder {
.add_layer(BaseTokenRatioProviderLayer::new(base_token_adjuster_config));
}
let state_keeper_config = try_load_config!(self.configs.state_keeper_config);
let l1_gas_layer = L1GasLayer::new(state_keeper_config);
let l1_gas_layer = L1GasLayer::new(&state_keeper_config);
self.node.add_layer(l1_gas_layer);
Ok(self)
}
Expand Down
1 change: 1 addition & 0 deletions core/lib/basic_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod commitment;
pub mod network;
pub mod protocol_version;
pub mod prover_dal;
pub mod pubdata_da;
pub mod seed_phrase;
pub mod settlement;
pub mod tee_types;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! Types related to data availability.
use chrono::{DateTime, Utc};
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use zksync_basic_types::L1BatchNumber;
use zksync_config::configs::eth_sender::PubdataSendingMode;

use crate::L1BatchNumber;

/// Enum holding the current values used for DA Layers.
#[repr(u8)]
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Serialize)]
#[derive(TryFromPrimitive)]
pub enum PubdataDA {
#[derive(Debug, Clone, Copy, Default, PartialEq, Deserialize, Serialize, TryFromPrimitive)]
pub enum PubdataSendingMode {
/// Pubdata is sent to the L1 as a tx calldata.
#[default]
Calldata = 0,
/// Pubdata is sent to L1 as EIP-4844 blobs.
Blobs,
Expand All @@ -19,17 +21,6 @@ pub enum PubdataDA {
RelayedL2Calldata,
}

impl From<PubdataSendingMode> for PubdataDA {
fn from(value: PubdataSendingMode) -> Self {
match value {
PubdataSendingMode::Calldata => PubdataDA::Calldata,
PubdataSendingMode::Blobs => PubdataDA::Blobs,
PubdataSendingMode::Custom => PubdataDA::Custom,
PubdataSendingMode::RelayedL2Calldata => PubdataDA::RelayedL2Calldata,
}
}
}

/// Represents a blob in the data availability layer.
#[derive(Debug, Clone)]
pub struct DataAvailabilityBlob {
Expand Down
11 changes: 1 addition & 10 deletions core/lib/config/src/configs/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use anyhow::Context as _;
use serde::Deserialize;
use zksync_basic_types::{settlement::SettlementMode, H256};
use zksync_basic_types::{pubdata_da::PubdataSendingMode, settlement::SettlementMode, H256};
use zksync_crypto_primitives::K256PrivateKey;

use crate::EthWatchConfig;
Expand Down Expand Up @@ -80,15 +80,6 @@ pub enum ProofLoadingMode {
FriProofFromGcs,
}

#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Default)]
pub enum PubdataSendingMode {
#[default]
Calldata,
Blobs,
Custom,
RelayedL2Calldata,
}

#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct SenderConfig {
pub aggregated_proof_sizes: Vec<usize>,
Expand Down
15 changes: 2 additions & 13 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use zksync_basic_types::{
commitment::L1BatchCommitmentMode,
network::Network,
protocol_version::{ProtocolSemanticVersion, ProtocolVersionId, VersionPatch},
pubdata_da::PubdataSendingMode,
seed_phrase::SeedPhrase,
vm::FastVmMode,
L1BatchNumber, L1ChainId, L2ChainId,
Expand All @@ -16,8 +17,7 @@ use zksync_crypto_primitives::K256PrivateKey;

use crate::{
configs::{
self, da_client::DAClientConfig::Avail, eth_sender::PubdataSendingMode,
external_price_api_client::ForcedPriceClientConfig,
self, da_client::DAClientConfig::Avail, external_price_api_client::ForcedPriceClientConfig,
},
AvailConfig,
};
Expand Down Expand Up @@ -388,17 +388,6 @@ impl Distribution<configs::eth_sender::ProofLoadingMode> for EncodeDist {
}
}

impl Distribution<configs::eth_sender::PubdataSendingMode> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::eth_sender::PubdataSendingMode {
type T = configs::eth_sender::PubdataSendingMode;
match rng.gen_range(0..3) {
0 => T::Calldata,
1 => T::Blobs,
_ => T::Custom,
}
}
}

impl Distribution<configs::eth_sender::SenderConfig> for EncodeDist {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> configs::eth_sender::SenderConfig {
configs::eth_sender::SenderConfig {
Expand Down
3 changes: 2 additions & 1 deletion core/lib/env_config/src/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl FromEnv for GasAdjusterConfig {

#[cfg(test)]
mod tests {
use zksync_config::configs::eth_sender::{ProofSendingMode, PubdataSendingMode};
use zksync_basic_types::pubdata_da::PubdataSendingMode;
use zksync_config::configs::eth_sender::ProofSendingMode;

use super::*;
use crate::test_utils::{hash, EnvMutex};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use zksync_types::{
commitment::{L1BatchCommitmentMode, L1BatchWithMetadata},
ethabi::Token,
pubdata_da::PubdataDA,
pubdata_da::PubdataSendingMode,
};

use crate::{
Expand All @@ -14,7 +14,7 @@ use crate::{
pub struct CommitBatches<'a> {
pub last_committed_l1_batch: &'a L1BatchWithMetadata,
pub l1_batches: &'a [L1BatchWithMetadata],
pub pubdata_da: PubdataDA,
pub pubdata_da: PubdataSendingMode,
pub mode: L1BatchCommitmentMode,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use zksync_types::{
L1BatchWithMetadata,
},
ethabi::Token,
pubdata_da::PubdataDA,
pubdata_da::PubdataSendingMode,
web3::contract::Error as ContractError,
ProtocolVersionId, U256,
};
Expand All @@ -24,14 +24,14 @@ const PUBDATA_SOURCE_CUSTOM: u8 = 2;
pub struct CommitBatchInfo<'a> {
mode: L1BatchCommitmentMode,
l1_batch_with_metadata: &'a L1BatchWithMetadata,
pubdata_da: PubdataDA,
pubdata_da: PubdataSendingMode,
}

impl<'a> CommitBatchInfo<'a> {
pub fn new(
mode: L1BatchCommitmentMode,
l1_batch_with_metadata: &'a L1BatchWithMetadata,
pubdata_da: PubdataDA,
pubdata_da: PubdataSendingMode,
) -> Self {
Self {
mode,
Expand Down Expand Up @@ -204,24 +204,24 @@ impl Tokenizable for CommitBatchInfo<'_> {
// Here we're not pushing any pubdata on purpose; no pubdata is sent in Validium mode.
(
L1BatchCommitmentMode::Validium,
PubdataDA::Calldata | PubdataDA::RelayedL2Calldata,
PubdataSendingMode::Calldata | PubdataSendingMode::RelayedL2Calldata,
) => {
vec![PUBDATA_SOURCE_CALLDATA]
}
(L1BatchCommitmentMode::Validium, PubdataDA::Blobs) => {
(L1BatchCommitmentMode::Validium, PubdataSendingMode::Blobs) => {
vec![PUBDATA_SOURCE_BLOBS]
}

(L1BatchCommitmentMode::Rollup, PubdataDA::Custom) => {
(L1BatchCommitmentMode::Rollup, PubdataSendingMode::Custom) => {
panic!("Custom pubdata DA is incompatible with Rollup mode")
}
(L1BatchCommitmentMode::Validium, PubdataDA::Custom) => {
(L1BatchCommitmentMode::Validium, PubdataSendingMode::Custom) => {
vec![PUBDATA_SOURCE_CUSTOM]
}

(
L1BatchCommitmentMode::Rollup,
PubdataDA::Calldata | PubdataDA::RelayedL2Calldata,
PubdataSendingMode::Calldata | PubdataSendingMode::RelayedL2Calldata,
) => {
// We compute and add the blob commitment to the pubdata payload so that we can verify the proof
// even if we are not using blobs.
Expand All @@ -232,7 +232,7 @@ impl Tokenizable for CommitBatchInfo<'_> {
.chain(blob_commitment)
.collect()
}
(L1BatchCommitmentMode::Rollup, PubdataDA::Blobs) => {
(L1BatchCommitmentMode::Rollup, PubdataSendingMode::Blobs) => {
let pubdata = self.pubdata_input();
let pubdata_commitments =
pubdata.chunks(ZK_SYNC_BYTES_PER_BLOB).flat_map(|blob| {
Expand Down
23 changes: 11 additions & 12 deletions core/lib/protobuf_config/src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Context as _;
use zksync_config::configs::{self};
use zksync_protobuf::{required, ProtoRepr};
use zksync_types::pubdata_da::PubdataSendingMode;

use crate::{proto::eth as proto, read_optional_repr};

Expand All @@ -25,23 +26,21 @@ impl proto::ProofSendingMode {
}

impl proto::PubdataSendingMode {
fn new(x: &configs::eth_sender::PubdataSendingMode) -> Self {
use configs::eth_sender::PubdataSendingMode as From;
fn new(x: &PubdataSendingMode) -> Self {
match x {
From::Calldata => Self::Calldata,
From::Blobs => Self::Blobs,
From::Custom => Self::Custom,
From::RelayedL2Calldata => Self::RelayedL2Calldata,
PubdataSendingMode::Calldata => Self::Calldata,
PubdataSendingMode::Blobs => Self::Blobs,
PubdataSendingMode::Custom => Self::Custom,
PubdataSendingMode::RelayedL2Calldata => Self::RelayedL2Calldata,
}
}

fn parse(&self) -> configs::eth_sender::PubdataSendingMode {
use configs::eth_sender::PubdataSendingMode as To;
fn parse(&self) -> PubdataSendingMode {
match self {
Self::Calldata => To::Calldata,
Self::Blobs => To::Blobs,
Self::Custom => To::Custom,
Self::RelayedL2Calldata => To::RelayedL2Calldata,
Self::Calldata => PubdataSendingMode::Calldata,
Self::Blobs => PubdataSendingMode::Blobs,
Self::Custom => PubdataSendingMode::Custom,
Self::RelayedL2Calldata => PubdataSendingMode::RelayedL2Calldata,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/lib/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
# **IMPORTANT.** Please do not add dependency on `zksync_config` etc. This crate has a heavy dependency graph as is.
zksync_system_constants.workspace = true
zksync_utils.workspace = true
zksync_basic_types.workspace = true
zksync_contracts.workspace = true
zksync_mini_merkle_tree.workspace = true
zksync_config.workspace = true
zksync_protobuf.workspace = true
zksync_crypto_primitives.workspace = true

Expand All @@ -39,7 +39,6 @@ itertools.workspace = true
tracing.workspace = true

# Crypto stuff
secp256k1.workspace = true
blake2.workspace = true

[dev-dependencies]
Expand Down
Loading

0 comments on commit cfbcc11

Please sign in to comment.