Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: Remove initialize_components function (matter-labs#2284)
Browse files Browse the repository at this point in the history
Makes node framework the only way to run main node.
Removes `initialize_components` function.

⚠️ Some leftovers are left in `core_leftovers` -- these are still used
in some places. Removing them will be tacked separately.

Part of the migration.

<!-- 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`.
  • Loading branch information
popzxc authored and gabrieldemian committed Jun 21, 2024
1 parent 8316942 commit 7dbce33
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-core-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ jobs:
# `sleep 5` because we need to wait until server started properly
- name: Run server
run: |
ci_run zk server --use-node-framework --components=$SERVER_COMPONENTS &>server.log &
ci_run zk server --components=$SERVER_COMPONENTS &>server.log &
ci_run sleep 5
- name: Run contract verifier
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-zk-toolbox-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
- name: Run server
run: |
ci_run zk_inception server --ignore-prerequisites -a --use-node-framework --verbose &>server.log &
ci_run zk_inception server --ignore-prerequisites -a --verbose &>server.log &
ci_run sleep 5
- name: Run integration tests
Expand Down
144 changes: 53 additions & 91 deletions core/bin/zksync_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str::FromStr, time::Duration};
use std::str::FromStr;

use anyhow::Context as _;
use clap::Parser;
Expand All @@ -20,14 +20,12 @@ use zksync_config::{
GasAdjusterConfig, GenesisConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig,
};
use zksync_core_leftovers::{
genesis_init, initialize_components, is_genesis_needed, setup_sigint_handler,
genesis_init, is_genesis_needed,
temp_config_store::{decode_yaml_repr, TempConfigStore},
Component, Components,
};
use zksync_env_config::FromEnv;
use zksync_eth_client::clients::Client;
use zksync_storage::RocksDB;
use zksync_utils::wait_for_tasks::ManagedTasks;

use crate::node_builder::MainNodeBuilder;

Expand Down Expand Up @@ -67,7 +65,8 @@ struct Cli {
/// Path to the yaml with genesis. If set, it will be used instead of env vars.
#[arg(long)]
genesis_path: Option<std::path::PathBuf>,
/// Run the node using the node framework.
/// Used to enable node framework.
/// Now the node framework is used by default and this argument is left for backward compatibility.
#[arg(long)]
use_node_framework: bool,
}
Expand All @@ -88,8 +87,7 @@ impl FromStr for ComponentsToRun {
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
fn main() -> anyhow::Result<()> {
let opt = Cli::parse();

// Load env config and use it if file config is not provided
Expand Down Expand Up @@ -181,32 +179,10 @@ async fn main() -> anyhow::Result<()> {
}
};

let database_secrets = secrets.database.clone().context("DatabaseSecrets")?;

if opt.genesis || is_genesis_needed(&database_secrets).await {
genesis_init(genesis.clone(), &database_secrets)
.await
.context("genesis_init")?;

if let Some(ecosystem_contracts) = &contracts_config.ecosystem_contracts {
let l1_secrets = secrets.l1.as_ref().context("l1_screts")?;
let query_client = Client::http(l1_secrets.l1_rpc_url.clone())
.context("Ethereum client")?
.for_network(genesis.l1_chain_id.into())
.build();
zksync_node_genesis::save_set_chain_id_tx(
&query_client,
contracts_config.diamond_proxy_addr,
ecosystem_contracts.state_transition_proxy_addr,
&database_secrets,
)
.await
.context("Failed to save SetChainId upgrade transaction")?;
}

if opt.genesis {
return Ok(());
}
run_genesis_if_needed(opt.genesis, &genesis, &contracts_config, &secrets)?;
if opt.genesis {
// If genesis is requested, we don't need to run the node.
return Ok(());
}

let components = if opt.rebuild_tree {
Expand All @@ -215,69 +191,55 @@ async fn main() -> anyhow::Result<()> {
opt.components.0
};

// If the node framework is used, run the node.
if opt.use_node_framework {
// We run the node from a different thread, since the current thread is in tokio context.
std::thread::spawn(move || -> anyhow::Result<()> {
let node = MainNodeBuilder::new(
configs,
wallets,
genesis,
contracts_config,
secrets,
consensus,
)
.build(components)?;
node.run()?;
Ok(())
})
.join()
.expect("Failed to run the node")?;

return Ok(());
}

// Run core actors.
let sigint_receiver = setup_sigint_handler();
let (core_task_handles, stop_sender, health_check_handle) = initialize_components(
&configs,
&wallets,
&genesis,
&contracts_config,
&components,
&secrets,
let node = MainNodeBuilder::new(
configs,
wallets,
genesis,
contracts_config,
secrets,
consensus,
)
.await
.context("Unable to start Core actors")?;

tracing::info!("Running {} core task handlers", core_task_handles.len());

let mut tasks = ManagedTasks::new(core_task_handles);
tokio::select! {
_ = tasks.wait_single() => {},
_ = sigint_receiver => {
tracing::info!("Stop signal received, shutting down");
},
}

stop_sender.send(true).ok();
tokio::task::spawn_blocking(RocksDB::await_rocksdb_termination)
.await
.context("error waiting for RocksDB instances to drop")?;
let complete_timeout =
if components.contains(&Component::HttpApi) || components.contains(&Component::WsApi) {
// Increase timeout because of complicated graceful shutdown procedure for API servers.
Duration::from_secs(30)
} else {
Duration::from_secs(5)
};
tasks.complete(complete_timeout).await;
health_check_handle.stop().await;
tracing::info!("Stopped");
.build(components)?;
node.run()?;
Ok(())
}

fn run_genesis_if_needed(
force_genesis: bool,
genesis: &GenesisConfig,
contracts_config: &ContractsConfig,
secrets: &Secrets,
) -> anyhow::Result<()> {
let tokio_runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
tokio_runtime.block_on(async move {
let database_secrets = secrets.database.clone().context("DatabaseSecrets")?;
if force_genesis || is_genesis_needed(&database_secrets).await {
genesis_init(genesis.clone(), &database_secrets)
.await
.context("genesis_init")?;

if let Some(ecosystem_contracts) = &contracts_config.ecosystem_contracts {
let l1_secrets = secrets.l1.as_ref().context("l1_screts")?;
let query_client = Client::http(l1_secrets.l1_rpc_url.clone())
.context("Ethereum client")?
.for_network(genesis.l1_chain_id.into())
.build();
zksync_node_genesis::save_set_chain_id_tx(
&query_client,
contracts_config.diamond_proxy_addr,
ecosystem_contracts.state_transition_proxy_addr,
&database_secrets,
)
.await
.context("Failed to save SetChainId upgrade transaction")?;
}
}
Ok(())
})
}

fn load_env_config() -> anyhow::Result<TempConfigStore> {
Ok(TempConfigStore {
postgres_config: PostgresConfig::from_env().ok(),
Expand Down
54 changes: 3 additions & 51 deletions core/lib/zksync_core_leftovers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
vise.workspace = true
zksync_state.workspace = true
vm_utils.workspace = true
zksync_types.workspace = true
zksync_dal.workspace = true
prover_dal.workspace = true
zksync_db_connection.workspace = true
zksync_config.workspace = true
zksync_protobuf_config.workspace = true
zksync_utils.workspace = true
Expand Down Expand Up @@ -68,51 +62,9 @@ zksync_consensus_executor.workspace = true
zksync_consensus_bft.workspace = true
zksync_consensus_utils.workspace = true
zksync_protobuf.workspace = true
zksync_node_genesis.workspace = true

prost.workspace = true
secrecy.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
anyhow.workspace = true
tokio = { workspace = true, features = ["time"] }
serde_yaml.workspace = true
itertools.workspace = true
ctrlc.workspace = true
rand.workspace = true

tokio = { workspace = true, features = ["time"] }
futures = { workspace = true, features = ["compat"] }
pin-project-lite.workspace = true
chrono = { workspace = true, features = ["serde"] }
anyhow.workspace = true
thiserror.workspace = true
async-trait.workspace = true
thread_local.workspace = true

reqwest = { workspace = true, features = ["blocking", "json"] }
hex.workspace = true
lru.workspace = true
governor.workspace = true
tower-http = { workspace = true, features = ["full"] }
tower = { workspace = true, features = ["full"] }
axum = { workspace = true, features = [
"http1",
"json",
"tokio",
] }
once_cell.workspace = true
dashmap.workspace = true

tracing.workspace = true

[dev-dependencies]
zksync_test_account.workspace = true
zksync_node_test_utils.workspace = true

assert_matches.workspace = true
jsonrpsee.workspace = true
tempfile.workspace = true
test-casing.workspace = true
test-log.workspace = true
backon.workspace = true

[build-dependencies]
zksync_protobuf_build.workspace = true
19 changes: 1 addition & 18 deletions core/lib/zksync_core_leftovers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#![allow(clippy::upper_case_acronyms, clippy::derive_partial_eq_without_eq)]

use std::{
net::Ipv4Addr,
str::FromStr,
sync::Arc,
time::{Duration, Instant},
};
use std::str::FromStr;

use anyhow::Context as _;
use prometheus_exporter::PrometheusExporterConfig;
Expand Down Expand Up @@ -67,18 +62,6 @@ use zksync_node_fee_model::{
l1_gas_price::GasAdjusterSingleton, BatchFeeModelInputProvider, MainNodeFeeInputProvider,
};
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};
use zksync_state::{PostgresStorageCaches, RocksdbStorageOptions};
use zksync_state_keeper::{
create_state_keeper, io::seal_logic::l2_block_seal_subtasks::L2BlockSealProcess,
AsyncRocksdbCache, MempoolFetcher, MempoolGuard, OutputHandler, StateKeeperPersistence,
TreeWritesPersistence,
};
use zksync_tee_verifier_input_producer::TeeVerifierInputProducer;
use zksync_types::{ethabi::Contract, fee_model::FeeModelConfig, Address, L2ChainId};
use zksync_web3_decl::client::{Client, DynClient, L1};

pub mod temp_config_store;

Expand Down
10 changes: 2 additions & 8 deletions core/lib/zksync_core_leftovers/src/temp_config_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ use zksync_config::{
ApiConfig, ContractVerifierConfig, DADispatcherConfig, DBConfig, EthConfig, EthWatchConfig,
GasAdjusterConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig,
};
use zksync_protobuf::{repr::ProtoRepr, ProtoFmt};

pub fn decode_yaml<T: ProtoFmt>(yaml: &str) -> anyhow::Result<T> {
let d = serde_yaml::Deserializer::from_str(yaml);
let this: T = zksync_protobuf::serde::deserialize(d)?;
Ok(this)
}
use zksync_protobuf::repr::ProtoRepr;

pub fn decode_yaml_repr<T: ProtoRepr>(yaml: &str) -> anyhow::Result<T::Type> {
let d = serde_yaml::Deserializer::from_str(yaml);
let this: T = zksync_protobuf::serde::deserialize_proto_with_options(d, false)?;
this.read()
}
//

// TODO (QIT-22): This structure is going to be removed when components will be responsible for their own configs.
/// A temporary config store allowing to pass deserialized configs from `zksync_server` to `zksync_core`.
/// All the configs are optional, since for some component combination it is not needed to pass all the configs.
Expand Down
4 changes: 0 additions & 4 deletions infrastructure/zk/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export async function server(rebuildTree: boolean, uring: boolean, components?:
if (components) {
options += ` --components=${components}`;
}
if (useNodeFramework) {
options += ' --use-node-framework';
}
await utils.spawn(`cargo run --bin zksync_server --release ${options}`);
}

Expand Down Expand Up @@ -82,7 +79,6 @@ export const serverCommand = new Command('server')
.option('--uring', 'enables uring support for RocksDB')
.option('--components <components>', 'comma-separated list of components to run')
.option('--chain-name <chain-name>', 'environment name')
.option('--use-node-framework', 'use node framework for server')
.action(async (cmd: Command) => {
cmd.chainName ? env.reload(cmd.chainName) : env.load();
if (cmd.genesis) {
Expand Down

0 comments on commit 7dbce33

Please sign in to comment.