Skip to content

Commit

Permalink
feat(node): Extract genesis into separate crate (#1797)
Browse files Browse the repository at this point in the history
## What ❔

Extract utilities needed to perform genesis into a dedicated crate.

## Why ❔

Part of splitting `zksync_core` into a set of crates.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.

---------

Co-authored-by: Igor Aleksanov <popzxc@yandex.ru>
  • Loading branch information
AnastasiiaVashchuk and popzxc committed May 6, 2024
1 parent 4cf235f commit a8c4599
Show file tree
Hide file tree
Showing 38 changed files with 458 additions and 371 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"core/node/block_reverter",
"core/node/commitment_generator",
"core/node/house_keeper",
"core/node/genesis",
"core/node/shared_metrics",
# Libraries
"core/lib/db_connection",
"core/lib/zksync_core",
Expand Down Expand Up @@ -228,3 +230,4 @@ zksync_proof_data_handler = { path = "core/node/proof_data_handler" }
zksync_block_reverter = { path = "core/node/block_reverter" }
zksync_commitment_generator = { path = "core/node/commitment_generator" }
zksync_house_keeper = { path = "core/node/house_keeper" }
zksync_node_genesis = { path = "core/node/genesis" }
1 change: 1 addition & 0 deletions core/bin/external_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ zksync_web3_decl.workspace = true
zksync_types.workspace = true
zksync_block_reverter.workspace = true
zksync_shared_metrics.workspace = true
zksync_node_genesis.workspace = true
vlog.workspace = true

zksync_concurrency.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion core/bin/external_node/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use assert_matches::assert_matches;
use test_casing::test_casing;
use zksync_basic_types::protocol_version::ProtocolVersionId;
use zksync_core::genesis::{insert_genesis_batch, GenesisParams};
use zksync_eth_client::clients::MockEthereum;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_types::{api, ethabi, fee_model::FeeParams, L1BatchNumber, L2BlockNumber, H256};
use zksync_web3_decl::client::{BoxedL2Client, MockL2Client};

Expand Down
2 changes: 1 addition & 1 deletion core/bin/genesis_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ zksync_core.workspace = true
zksync_dal.workspace = true
zksync_contracts.workspace = true
zksync_protobuf.workspace = true

zksync_node_genesis.workspace = true

anyhow.workspace = true
serde_json.workspace = true
Expand Down
6 changes: 2 additions & 4 deletions core/bin/genesis_generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use clap::Parser;
use serde_yaml::Serializer;
use zksync_config::{GenesisConfig, PostgresConfig};
use zksync_contracts::BaseSystemContracts;
use zksync_core::{
genesis::{insert_genesis_batch, GenesisParams},
temp_config_store::decode_yaml_repr,
};
use zksync_core::temp_config_store::decode_yaml_repr;
use zksync_dal::{ConnectionPool, Core, CoreDal};
use zksync_env_config::FromEnv;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_protobuf::{
build::{prost_reflect, prost_reflect::ReflectMessage},
ProtoRepr,
Expand Down
1 change: 1 addition & 0 deletions core/bin/zksync_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ zksync_storage.workspace = true
zksync_utils.workspace = true
zksync_types.workspace = true
zksync_core.workspace = true
zksync_node_genesis.workspace = true

# Consensus dependenices
zksync_consensus_crypto.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions core/bin/zksync_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zksync_config::{
GenesisConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig,
};
use zksync_core::{
genesis, genesis_init, initialize_components, is_genesis_needed, setup_sigint_handler,
genesis_init, initialize_components, is_genesis_needed, setup_sigint_handler,
temp_config_store::{decode_yaml, decode_yaml_repr, Secrets, TempConfigStore},
Component, Components,
};
Expand Down Expand Up @@ -183,7 +183,7 @@ async fn main() -> anyhow::Result<()> {
let eth_config = configs.eth.as_ref().context("eth config")?;
let query_client =
QueryClient::new(eth_config.web3_url.clone()).context("Ethereum client")?;
genesis::save_set_chain_id_tx(
zksync_node_genesis::save_set_chain_id_tx(
&query_client,
contracts_config.diamond_proxy_addr,
ecosystem_contracts.state_transition_proxy_addr,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ zksync_shared_metrics.workspace = true
zksync_proof_data_handler.workspace = true
zksync_commitment_generator.workspace = true
zksync_house_keeper.workspace = true

zksync_node_genesis.workspace = true
multivm.workspace = true

# Consensus dependenices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use assert_matches::assert_matches;
use zksync_dal::ConnectionPool;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};

use super::*;
use crate::{
api_server::{execution_sandbox::apply::apply_vm_in_sandbox, tx_sender::ApiContracts},
genesis::{insert_genesis_batch, GenesisParams},
utils::testonly::{create_l2_block, create_l2_transaction, prepare_recovery_snapshot},
};

Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/api_server/tx_sender/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
use assert_matches::assert_matches;
use multivm::interface::ExecutionResult;
use zksync_config::configs::wallets::Wallets;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_types::{get_nonce_key, L1BatchNumber, L2BlockNumber, StorageLog};
use zksync_utils::u256_to_h256;

use super::*;
use crate::{
api_server::execution_sandbox::{testonly::MockTransactionExecutor, VmConcurrencyBarrier},
genesis::{insert_genesis_batch, GenesisParams},
utils::testonly::{
create_l2_block, create_l2_transaction, prepare_recovery_snapshot,
MockBatchFeeParamsProvider,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/api_server/web3/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use zksync_config::{
};
use zksync_dal::{transactions_dal::L2TxSubmissionResult, Connection, ConnectionPool, CoreDal};
use zksync_health_check::CheckHealth;
use zksync_node_genesis::{insert_genesis_batch, mock_genesis_config, GenesisParams};
use zksync_types::{
api,
block::L2BlockHeader,
Expand Down Expand Up @@ -53,7 +54,6 @@ use crate::{
execution_sandbox::testonly::MockTransactionExecutor,
tx_sender::tests::create_test_tx_sender,
},
genesis::{insert_genesis_batch, mock_genesis_config, GenesisParams},
utils::testonly::{
create_l1_batch, create_l1_batch_metadata, create_l2_block, create_l2_transaction,
l1_batch_metadata_to_commitment_artifacts, prepare_recovery_snapshot,
Expand Down
6 changes: 2 additions & 4 deletions core/lib/zksync_core/src/consensus/storage/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use anyhow::Context as _;
use zksync_concurrency::{ctx, error::Wrap as _, time};
use zksync_consensus_roles::validator;
use zksync_dal::ConnectionPool;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};

use super::Store;
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
utils::testonly::{recover, snapshot, Snapshot},
};
use crate::utils::testonly::{recover, snapshot, Snapshot};

impl Store {
/// Waits for the `number` L2 block to have a certificate.
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/consensus/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use zksync_config::{configs, GenesisConfig};
use zksync_consensus_roles::validator;
use zksync_contracts::BaseSystemContractsHashes;
use zksync_dal::{CoreDal, DalError};
use zksync_node_genesis::{mock_genesis_config, GenesisParams};
use zksync_types::{
api, snapshots::SnapshotRecoveryStatus, Address, L1BatchNumber, L2BlockNumber, L2ChainId,
ProtocolVersionId, H256,
Expand All @@ -21,7 +22,6 @@ use zksync_web3_decl::{
use crate::{
api_server::web3::{state::InternalApiConfig, tests::spawn_http_server},
consensus::{fetcher::P2PConfig, Fetcher, Store},
genesis::{mock_genesis_config, GenesisParams},
state_keeper::{
io::{IoCursor, L1BatchParams, L2BlockParams},
seal_criteria::NoopSealer,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/consistency_checker/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::sync::mpsc;
use zksync_config::GenesisConfig;
use zksync_dal::Connection;
use zksync_eth_client::{clients::MockEthereum, Options};
use zksync_node_genesis::{insert_genesis_batch, mock_genesis_config, GenesisParams};
use zksync_types::{
aggregated_operations::AggregatedActionType, commitment::L1BatchWithMetadata, Log,
ProtocolVersion, ProtocolVersionId, H256,
Expand All @@ -18,7 +19,6 @@ use crate::{
eth_sender::l1_batch_commit_data_generator::{
RollupModeL1BatchCommitDataGenerator, ValidiumModeL1BatchCommitDataGenerator,
},
genesis::{insert_genesis_batch, mock_genesis_config, GenesisParams},
utils::testonly::{
create_l1_batch, create_l1_batch_metadata, l1_batch_metadata_to_commitment_artifacts,
DeploymentMode,
Expand Down
5 changes: 2 additions & 3 deletions core/lib/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use zksync_house_keeper::{
periodic_job::PeriodicJob,
waiting_to_queued_fri_witness_job_mover::WaitingToQueuedFriWitnessJobMover,
};
use zksync_node_genesis::{ensure_genesis_state, GenesisParams};
use zksync_object_store::{ObjectStore, ObjectStoreFactory};
use zksync_queued_job_processor::JobProcessor;
use zksync_shared_metrics::{InitStage, APP_METRICS};
Expand All @@ -85,7 +86,6 @@ use crate::{
},
Aggregator, EthTxAggregator, EthTxManager,
},
genesis::GenesisParams,
l1_gas_price::{
GasAdjusterSingleton, PubdataPricing, RollupPubdataPricing, ValidiumPubdataPricing,
},
Expand All @@ -104,7 +104,6 @@ pub mod consistency_checker;
pub mod db_pruner;
pub mod eth_sender;
pub mod fee_model;
pub mod genesis;
pub mod l1_gas_price;
pub mod metadata_calculator;
pub mod proto;
Expand All @@ -128,7 +127,7 @@ pub async fn genesis_init(
let mut storage = pool.connection().await.context("connection()")?;

let params = GenesisParams::load_genesis_params(genesis_config)?;
genesis::ensure_genesis_state(&mut storage, &params).await?;
ensure_genesis_state(&mut storage, &params).await?;

Ok(())
}
Expand Down
8 changes: 3 additions & 5 deletions core/lib/zksync_core/src/metadata_calculator/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,15 +612,13 @@ impl L1BatchWithLogs {
mod tests {
use tempfile::TempDir;
use zksync_dal::{ConnectionPool, Core};
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_prover_interface::inputs::PrepareBasicCircuitsJob;
use zksync_types::{StorageKey, StorageLog};

use super::*;
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
metadata_calculator::tests::{
extend_db_state, gen_storage_logs, mock_config, reset_db_state,
},
use crate::metadata_calculator::tests::{
extend_db_state, gen_storage_logs, mock_config, reset_db_state,
};

impl L1BatchWithLogs {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/metadata_calculator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use zksync_dal::{ConnectionPool, Core};
use zksync_health_check::{CheckHealth, HealthUpdater, ReactiveHealthCheck};
use zksync_object_store::ObjectStore;

pub(crate) use self::helpers::{AsyncTreeReader, L1BatchWithLogs, MerkleTreeInfo};
pub(crate) use self::helpers::{AsyncTreeReader, MerkleTreeInfo};
pub use self::{helpers::LazyAsyncTreeReader, pruning::MerkleTreePruningTask};
use self::{
helpers::{create_db, Delayer, GenericAsyncTree, MerkleTreeHealth, MerkleTreeHealthCheck},
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/metadata_calculator/pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ impl MerkleTreePruningTask {
mod tests {
use tempfile::TempDir;
use test_casing::test_casing;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_types::{L1BatchNumber, L2BlockNumber};

use super::*;
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
metadata_calculator::{
tests::{extend_db_state_from_l1_batch, gen_storage_logs, mock_config, reset_db_state},
MetadataCalculator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use zksync_config::configs::{
use zksync_dal::CoreDal;
use zksync_health_check::{CheckHealth, HealthStatus, ReactiveHealthCheck};
use zksync_merkle_tree::{domain::ZkSyncTree, TreeInstruction};
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_types::{L1BatchNumber, ProtocolVersionId, StorageLog};

use super::*;
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
metadata_calculator::{
helpers::create_db,
tests::{
Expand Down
9 changes: 5 additions & 4 deletions core/lib/zksync_core/src/metadata_calculator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use zksync_config::configs::{
use zksync_dal::{Connection, ConnectionPool, Core, CoreDal};
use zksync_health_check::{CheckHealth, HealthStatus};
use zksync_merkle_tree::domain::ZkSyncTree;
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_object_store::{ObjectStore, ObjectStoreFactory};
use zksync_prover_interface::inputs::PrepareBasicCircuitsJob;
use zksync_storage::RocksDB;
Expand All @@ -22,11 +23,10 @@ use zksync_types::{
};
use zksync_utils::u32_to_h256;

use super::{GenericAsyncTree, L1BatchWithLogs, MetadataCalculator, MetadataCalculatorConfig};
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
utils::testonly::{create_l1_batch, create_l2_block},
use super::{
helpers::L1BatchWithLogs, GenericAsyncTree, MetadataCalculator, MetadataCalculatorConfig,
};
use crate::utils::testonly::{create_l1_batch, create_l2_block};

const RUN_TIMEOUT: Duration = Duration::from_secs(30);

Expand Down Expand Up @@ -116,6 +116,7 @@ async fn expected_tree_hash(pool: &ConnectionPool<Core>) -> H256 {
.await
.unwrap();
let logs = logs.expect("no L1 batch").storage_logs;

all_logs.extend(logs);
}
ZkSyncTree::process_genesis_batch(&all_logs).root_hash
Expand Down
6 changes: 2 additions & 4 deletions core/lib/zksync_core/src/reorg_detector/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ use assert_matches::assert_matches;
use test_casing::{test_casing, Product};
use tokio::sync::mpsc;
use zksync_dal::{Connection, CoreDal};
use zksync_node_genesis::{insert_genesis_batch, GenesisParams};
use zksync_types::{
block::{L2BlockHasher, L2BlockHeader},
ProtocolVersion,
};
use zksync_web3_decl::jsonrpsee::core::ClientError as RpcError;

use super::*;
use crate::{
genesis::{insert_genesis_batch, GenesisParams},
utils::testonly::{create_l1_batch, create_l2_block},
};
use crate::utils::testonly::{create_l1_batch, create_l2_block};

async fn store_l2_block(storage: &mut Connection<'_, Core>, number: u32, hash: H256) {
let header = L2BlockHeader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio::{sync::watch, task::JoinHandle};
use zksync_config::configs::chain::StateKeeperConfig;
use zksync_contracts::{get_loadnext_contract, test_contracts::LoadnextContractExecutionParams};
use zksync_dal::{ConnectionPool, Core, CoreDal};
use zksync_node_genesis::create_genesis_l1_batch;
use zksync_state::{ReadStorageFactory, RocksdbStorageOptions};
use zksync_test_account::{Account, DeployContractsTx, TxType};
use zksync_types::{
Expand All @@ -29,7 +30,6 @@ use super::{
StorageType,
};
use crate::{
genesis::create_genesis_l1_batch,
state_keeper::{
batch_executor::{BatchExecutorHandle, TxExecutionResult},
tests::{default_l1_batch_env, default_system_env, BASE_SYSTEM_CONTRACTS},
Expand Down
Loading

0 comments on commit a8c4599

Please sign in to comment.