From 42635b7aeca584565589ab9c6ab8055cf4299d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Grze=C5=9Bkiewicz?= Date: Wed, 10 Apr 2024 17:40:43 +0200 Subject: [PATCH 1/2] feat(en): docker compose better postgres settings (#1621) I've updated the EN docker compose settings taking inspiration in settings in ansible-en repository. I've also updated the port, so that it doesn't collide with postgres set-up by zk init --------- Signed-off-by: tomg10 --- .../mainnet-external-node-docker-compose.yml | 39 ++++++++++++------- .../testnet-external-node-docker-compose.yml | 39 ++++++++++++------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/docs/guides/external-node/docker-compose-examples/mainnet-external-node-docker-compose.yml b/docs/guides/external-node/docker-compose-examples/mainnet-external-node-docker-compose.yml index 04f665bb2a7..0b72277c7c7 100644 --- a/docs/guides/external-node/docker-compose-examples/mainnet-external-node-docker-compose.yml +++ b/docs/guides/external-node/docker-compose-examples/mainnet-external-node-docker-compose.yml @@ -5,8 +5,8 @@ services: volumes: - mainnet-prometheus-data:/prometheus - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml - ports: - - "9090:9090" + expose: + - 9090 grafana: image: grafana/grafana:9.3.6 volumes: @@ -17,14 +17,25 @@ services: GF_AUTH_ANONYMOUS_ENABLED: "true" GF_AUTH_DISABLE_LOGIN_FORM: "true" ports: - - "3000:3000" - expose: - - 3000 + - "127.0.0.1:3000:3000" postgres: image: "postgres:14" - command: postgres -c 'max_connections=200' - ports: - - "5432:5432" + command: > + postgres + -c max_connections=200 + -c log_error_verbosity=terse + -c shared_buffers=2GB + -c effective_cache_size=4GB + -c maintenance_work_mem=1GB + -c checkpoint_completion_target=0.9 + -c random_page_cost=1.1 + -c effective_io_concurrency=200 + -c min_wal_size=4GB + -c max_wal_size=16GB + -c max_worker_processes=16 + -c checkpoint_timeout=1800 + expose: + - 5430 volumes: - mainnet-postgres:/var/lib/postgresql/data healthcheck: @@ -32,23 +43,23 @@ services: timeout: 3s test: psql -U postgres -c "select exists (select * from pg_stat_activity where datname = '{{ database_name }}' and application_name = 'pg_restore')" | grep -e ".f$" environment: - # We bind only to localhost, so setting insecure password is acceptable here - POSTGRES_PASSWORD=notsecurepassword + - PGPORT=5430 external-node: image: "matterlabs/external-node:e03e201-1712331604214-linux-amd64" depends_on: postgres: condition: service_healthy ports: - - "3322:3322" + - "127.0.0.1:3060:3060" + - "127.0.0.1:3061:3061" + - "127.0.0.1:3081:3081" volumes: - mainnet-rocksdb:/db expose: - - 3060 - - 3061 - - 3081 + - 3322 environment: - DATABASE_URL: "postgres://postgres:notsecurepassword@postgres/zksync_local_ext_node" + DATABASE_URL: "postgres://postgres:notsecurepassword@postgres:5430/zksync_local_ext_node" DATABASE_POOL_SIZE: 10 EN_HTTP_PORT: 3060 diff --git a/docs/guides/external-node/docker-compose-examples/testnet-external-node-docker-compose.yml b/docs/guides/external-node/docker-compose-examples/testnet-external-node-docker-compose.yml index 2314c9b6ad5..657a0f95be7 100644 --- a/docs/guides/external-node/docker-compose-examples/testnet-external-node-docker-compose.yml +++ b/docs/guides/external-node/docker-compose-examples/testnet-external-node-docker-compose.yml @@ -5,8 +5,8 @@ services: volumes: - testnet-prometheus-data:/prometheus - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml - ports: - - "9090:9090" + expose: + - 9090 grafana: image: grafana/grafana:9.3.6 volumes: @@ -17,14 +17,25 @@ services: GF_AUTH_ANONYMOUS_ENABLED: "true" GF_AUTH_DISABLE_LOGIN_FORM: "true" ports: - - "3000:3000" - expose: - - 3000 + - "127.0.0.1:3000:3000" postgres: image: "postgres:14" - command: postgres -c 'max_connections=200' - ports: - - "5432:5432" + command: > + postgres + -c max_connections=200 + -c log_error_verbosity=terse + -c shared_buffers=2GB + -c effective_cache_size=4GB + -c maintenance_work_mem=1GB + -c checkpoint_completion_target=0.9 + -c random_page_cost=1.1 + -c effective_io_concurrency=200 + -c min_wal_size=4GB + -c max_wal_size=16GB + -c max_worker_processes=16 + -c checkpoint_timeout=1800 + expose: + - 5430 volumes: - testnet-postgres:/var/lib/postgresql/data healthcheck: @@ -32,23 +43,23 @@ services: timeout: 3s test: psql -U postgres -c "select exists (select * from pg_stat_activity where datname = '{{ database_name }}' and application_name = 'pg_restore')" | grep -e ".f$" environment: - # We bind only to localhost, so setting insecure password is acceptable here - POSTGRES_PASSWORD=notsecurepassword + - PGPORT=5430 external-node: image: "matterlabs/external-node:e03e201-1712331604214-linux-amd64" depends_on: postgres: condition: service_healthy ports: - - "3322:3322" + - "127.0.0.1:3060:3060" + - "127.0.0.1:3061:3061" + - "127.0.0.1:3081:3081" volumes: - testnet-rocksdb:/db expose: - - 3060 - - 3061 - - 3081 + - 3322 environment: - DATABASE_URL: "postgres://postgres:notsecurepassword@postgres/zksync_local_ext_node" + DATABASE_URL: "postgres://postgres:notsecurepassword@postgres:5430/zksync_local_ext_node" DATABASE_POOL_SIZE: 10 EN_HTTP_PORT: 3060 From 26ca22ad46248af2143730d0df74dd2dfc0e0981 Mon Sep 17 00:00:00 2001 From: pompon0 Date: Thu, 11 Apr 2024 09:58:03 +0200 Subject: [PATCH 2/2] refactor: moved consensus config to config crate (#1594) moved consensus config to config crate --- Cargo.lock | 3 + Cargo.toml | 2 +- core/bin/external_node/Cargo.toml | 1 + core/bin/external_node/src/config/mod.rs | 26 ++- core/bin/zksync_server/src/config.rs | 16 +- core/lib/config/src/configs/consensus.rs | 69 +++++++ core/lib/config/src/configs/mod.rs | 1 + core/lib/config/src/testonly.rs | 34 ++++ core/lib/protobuf_config/build.rs | 2 +- core/lib/protobuf_config/src/consensus.rs | 92 +++++++++ core/lib/protobuf_config/src/lib.rs | 3 +- .../src/proto/{ => config}/api.proto | 0 .../src/proto/{ => config}/chain.proto | 0 .../proto/{ => config}/circuit_breaker.proto | 0 .../{ => config}/contract_verifier.proto | 0 .../src/proto/{ => config}/contracts.proto | 0 .../src/proto/{ => config}/database.proto | 0 .../src/proto/{ => config}/eth_sender.proto | 0 .../src/proto/{ => config}/general.proto | 0 .../src/proto/{ => config}/genesis.proto | 0 .../src/proto/{ => config}/house_keeper.proto | 0 .../src/proto/{ => config}/object_store.proto | 0 .../proto/{ => config}/observability.proto | 0 .../src/proto/{ => config}/prover.proto | 0 .../{ => config}/snapshots_creator.proto | 0 .../src/proto/{ => config}/utils.proto | 0 .../src/proto/{ => config}/wallets.proto | 0 .../src/proto/core}/consensus.proto | 6 +- core/lib/protobuf_config/src/proto/mod.rs | 2 + core/lib/protobuf_config/src/tests.rs | 2 + core/lib/types/Cargo.toml | 2 +- core/lib/zksync_core/Cargo.toml | 1 + core/lib/zksync_core/build.rs | 2 +- core/lib/zksync_core/src/consensus/config.rs | 176 +++++------------- core/lib/zksync_core/src/consensus/era.rs | 15 +- core/lib/zksync_core/src/consensus/mod.rs | 4 +- core/lib/zksync_core/src/consensus/tests.rs | 37 +--- core/lib/zksync_core/src/lib.rs | 13 +- .../zksync_core/src/temp_config_store/mod.rs | 12 +- core/node/node_framework/Cargo.toml | 1 + .../node/node_framework/examples/main_node.rs | 19 +- .../src/implementations/layers/consensus.rs | 9 +- 42 files changed, 337 insertions(+), 213 deletions(-) create mode 100644 core/lib/config/src/configs/consensus.rs create mode 100644 core/lib/protobuf_config/src/consensus.rs rename core/lib/protobuf_config/src/proto/{ => config}/api.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/chain.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/circuit_breaker.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/contract_verifier.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/contracts.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/database.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/eth_sender.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/general.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/genesis.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/house_keeper.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/object_store.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/observability.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/prover.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/snapshots_creator.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/utils.proto (100%) rename core/lib/protobuf_config/src/proto/{ => config}/wallets.proto (100%) rename core/lib/{zksync_core/src/proto => protobuf_config/src/proto/core}/consensus.proto (96%) diff --git a/Cargo.lock b/Cargo.lock index f000dc83cc7..59f014065fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8360,6 +8360,7 @@ dependencies = [ "zksync_object_store", "zksync_protobuf", "zksync_protobuf_build", + "zksync_protobuf_config", "zksync_prover_interface", "zksync_queued_job_processor", "zksync_shared_metrics", @@ -8554,6 +8555,7 @@ dependencies = [ "zksync_health_check", "zksync_l1_contract_interface", "zksync_object_store", + "zksync_protobuf_config", "zksync_snapshots_applier", "zksync_state", "zksync_storage", @@ -8667,6 +8669,7 @@ dependencies = [ "zksync_eth_watch", "zksync_health_check", "zksync_object_store", + "zksync_protobuf_config", "zksync_state", "zksync_storage", "zksync_types", diff --git a/Cargo.toml b/Cargo.toml index cefe6294361..d3894ee0dd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -121,7 +121,7 @@ reqwest = "0.11" rlp = "0.5" rocksdb = "0.21.0" rustc_version = "0.4.0" -secp256k1 = "0.27.0" +secp256k1 = { version = "0.27.0", features = ["recovery", "global-context"] } semver = "1" sentry = "0.31" serde = "1" diff --git a/core/bin/external_node/Cargo.toml b/core/bin/external_node/Cargo.toml index ebdc0ed13a3..b447d649190 100644 --- a/core/bin/external_node/Cargo.toml +++ b/core/bin/external_node/Cargo.toml @@ -15,6 +15,7 @@ zksync_core.workspace = true zksync_dal.workspace = true zksync_db_connection.workspace = true zksync_config.workspace = true +zksync_protobuf_config.workspace = true zksync_eth_client.workspace = true zksync_storage.workspace = true zksync_utils.workspace = true diff --git a/core/bin/external_node/src/config/mod.rs b/core/bin/external_node/src/config/mod.rs index bfa59a000bd..22425cc1d06 100644 --- a/core/bin/external_node/src/config/mod.rs +++ b/core/bin/external_node/src/config/mod.rs @@ -8,15 +8,21 @@ use anyhow::Context; use serde::Deserialize; use url::Url; use zksync_basic_types::{Address, L1ChainId, L2ChainId}; -use zksync_config::{configs::chain::L1BatchCommitDataGeneratorMode, ObjectStoreConfig}; +use zksync_config::{ + configs::{ + chain::L1BatchCommitDataGeneratorMode, + consensus::{ConsensusConfig, ConsensusSecrets}, + }, + ObjectStoreConfig, +}; use zksync_core::{ api_server::{ tx_sender::TxSenderConfig, web3::{state::InternalApiConfig, Namespace}, }, - consensus, - temp_config_store::decode_yaml, + temp_config_store::decode_yaml_repr, }; +use zksync_protobuf_config::proto; use zksync_types::{api::BridgeAddresses, fee_model::FeeParams}; use zksync_web3_decl::{ client::L2Client, @@ -564,20 +570,24 @@ impl PostgresConfig { } } -pub(crate) fn read_consensus_secrets() -> anyhow::Result> { +pub(crate) fn read_consensus_secrets() -> anyhow::Result> { let Ok(path) = std::env::var("EN_CONSENSUS_SECRETS_PATH") else { return Ok(None); }; let cfg = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&cfg).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&cfg).context("failed decoding YAML")?, + )) } -pub(crate) fn read_consensus_config() -> anyhow::Result> { +pub(crate) fn read_consensus_config() -> anyhow::Result> { let Ok(path) = std::env::var("EN_CONSENSUS_CONFIG_PATH") else { return Ok(None); }; let cfg = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&cfg).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&cfg).context("failed decoding YAML")?, + )) } /// Configuration for snapshot recovery. Loaded optionally, only if the corresponding command-line argument @@ -604,7 +614,7 @@ pub(crate) struct ExternalNodeConfig { pub postgres: PostgresConfig, pub optional: OptionalENConfig, pub remote: RemoteENConfig, - pub consensus: Option, + pub consensus: Option, pub api_component: ApiComponentConfig, pub tree_component: TreeComponentConfig, } diff --git a/core/bin/zksync_server/src/config.rs b/core/bin/zksync_server/src/config.rs index 10b611a85f3..fadcced7942 100644 --- a/core/bin/zksync_server/src/config.rs +++ b/core/bin/zksync_server/src/config.rs @@ -1,20 +1,26 @@ use anyhow::Context as _; -use zksync_core::{consensus, temp_config_store::decode_yaml}; +use zksync_config::configs::consensus::{ConsensusConfig, ConsensusSecrets}; +use zksync_core::temp_config_store::decode_yaml_repr; +use zksync_protobuf_config::proto; -pub(crate) fn read_consensus_secrets() -> anyhow::Result> { +pub(crate) fn read_consensus_secrets() -> anyhow::Result> { // Read public config. let Ok(path) = std::env::var("CONSENSUS_SECRETS_PATH") else { return Ok(None); }; let secrets = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&secrets).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&secrets).context("failed decoding YAML")?, + )) } -pub(crate) fn read_consensus_config() -> anyhow::Result> { +pub(crate) fn read_consensus_config() -> anyhow::Result> { // Read public config. let Ok(path) = std::env::var("CONSENSUS_CONFIG_PATH") else { return Ok(None); }; let cfg = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&cfg).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&cfg).context("failed decoding YAML")?, + )) } diff --git a/core/lib/config/src/configs/consensus.rs b/core/lib/config/src/configs/consensus.rs new file mode 100644 index 00000000000..d7bcb5503cc --- /dev/null +++ b/core/lib/config/src/configs/consensus.rs @@ -0,0 +1,69 @@ +use std::{ + collections::{BTreeMap, BTreeSet}, + fmt, +}; + +/// Public key of the validator (consensus participant) of the form "validator:public::" +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct ValidatorPublicKey(pub String); + +// Secret key of the validator (consensus participant) of the form "validator:secret::" +#[derive(PartialEq)] +pub struct ValidatorSecretKey(pub String); + +/// Public key of the node (gossip network participant) of the form "node:public::" +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct NodePublicKey(pub String); + +// Secret key of the node (gossip network participant) of the form "node:secret::" +#[derive(PartialEq)] +pub struct NodeSecretKey(pub String); + +impl fmt::Debug for ValidatorSecretKey { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str("") + } +} + +impl fmt::Debug for NodeSecretKey { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str("") + } +} + +/// Network address in the `:port` format. +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Host(pub String); + +/// Config (shared between main node and external node). +#[derive(Clone, Debug, PartialEq)] +pub struct ConsensusConfig { + /// Local socket address to listen for the incoming connections. + pub server_addr: std::net::SocketAddr, + /// Public address of this node (should forward to `server_addr`) + /// that will be advertised to peers, so that they can connect to this + /// node. + pub public_addr: Host, + + /// Validators participating in consensus. + pub validators: BTreeSet, + + /// Maximal allowed size of the payload in bytes. + pub max_payload_size: usize, + + /// Limit on the number of inbound connections outside + /// of the `static_inbound` set. + pub gossip_dynamic_inbound_limit: usize, + /// Inbound gossip connections that should be unconditionally accepted. + pub gossip_static_inbound: BTreeSet, + /// Outbound gossip connections that the node should actively try to + /// establish and maintain. + pub gossip_static_outbound: BTreeMap, +} + +/// Secrets need for consensus. +#[derive(Debug, PartialEq)] +pub struct ConsensusSecrets { + pub validator_key: Option, + pub node_key: Option, +} diff --git a/core/lib/config/src/configs/mod.rs b/core/lib/config/src/configs/mod.rs index 94fe69a441d..a78283ac2d0 100644 --- a/core/lib/config/src/configs/mod.rs +++ b/core/lib/config/src/configs/mod.rs @@ -22,6 +22,7 @@ pub use self::{ pub mod api; pub mod chain; +pub mod consensus; pub mod contract_verifier; pub mod contracts; pub mod database; diff --git a/core/lib/config/src/testonly.rs b/core/lib/config/src/testonly.rs index 39c1af11810..72fdfe07f08 100644 --- a/core/lib/config/src/testonly.rs +++ b/core/lib/config/src/testonly.rs @@ -673,3 +673,37 @@ impl Distribution for EncodeDist { } } } + +impl Distribution for EncodeDist { + fn sample(&self, rng: &mut R) -> configs::consensus::ConsensusConfig { + use configs::consensus::{ConsensusConfig, Host, NodePublicKey, ValidatorPublicKey}; + ConsensusConfig { + server_addr: self.sample(rng), + public_addr: Host(self.sample(rng)), + validators: self + .sample_range(rng) + .map(|_| ValidatorPublicKey(self.sample(rng))) + .collect(), + max_payload_size: self.sample(rng), + gossip_dynamic_inbound_limit: self.sample(rng), + gossip_static_inbound: self + .sample_range(rng) + .map(|_| NodePublicKey(self.sample(rng))) + .collect(), + gossip_static_outbound: self + .sample_range(rng) + .map(|_| (NodePublicKey(self.sample(rng)), Host(self.sample(rng)))) + .collect(), + } + } +} + +impl Distribution for EncodeDist { + fn sample(&self, rng: &mut R) -> configs::consensus::ConsensusSecrets { + use configs::consensus::{ConsensusSecrets, NodeSecretKey, ValidatorSecretKey}; + ConsensusSecrets { + validator_key: self.sample_opt(|| ValidatorSecretKey(self.sample(rng))), + node_key: self.sample_opt(|| NodeSecretKey(self.sample(rng))), + } + } +} diff --git a/core/lib/protobuf_config/build.rs b/core/lib/protobuf_config/build.rs index 66afd8fea6d..9a23d015239 100644 --- a/core/lib/protobuf_config/build.rs +++ b/core/lib/protobuf_config/build.rs @@ -2,7 +2,7 @@ fn main() { zksync_protobuf_build::Config { input_root: "src/proto".into(), - proto_root: "zksync/config".into(), + proto_root: "zksync".into(), dependencies: vec![], protobuf_crate: "::zksync_protobuf".parse().unwrap(), is_public: true, diff --git a/core/lib/protobuf_config/src/consensus.rs b/core/lib/protobuf_config/src/consensus.rs new file mode 100644 index 00000000000..5d290f9c5b1 --- /dev/null +++ b/core/lib/protobuf_config/src/consensus.rs @@ -0,0 +1,92 @@ +use anyhow::Context as _; +use zksync_config::configs::consensus::{ + ConsensusConfig, ConsensusSecrets, Host, NodePublicKey, NodeSecretKey, ValidatorPublicKey, + ValidatorSecretKey, +}; +use zksync_protobuf::{repr::ProtoRepr, required}; + +use crate::proto::consensus as proto; + +impl ProtoRepr for proto::Config { + type Type = ConsensusConfig; + fn read(&self) -> anyhow::Result { + let read_addr = |e: &proto::NodeAddr| { + let key = NodePublicKey(required(&e.key).context("key")?.clone()); + let addr = Host(required(&e.addr).context("addr")?.clone()); + anyhow::Ok((key, addr)) + }; + Ok(Self::Type { + server_addr: required(&self.server_addr) + .and_then(|x| Ok(x.parse()?)) + .context("server_addr")?, + public_addr: Host(required(&self.public_addr).context("public_addr")?.clone()), + validators: self + .validators + .iter() + .map(|x| ValidatorPublicKey(x.clone())) + .collect(), + max_payload_size: required(&self.max_payload_size) + .and_then(|x| Ok((*x).try_into()?)) + .context("max_payload_size")?, + gossip_dynamic_inbound_limit: required(&self.gossip_dynamic_inbound_limit) + .and_then(|x| Ok((*x).try_into()?)) + .context("gossip_dynamic_inbound_limit")?, + gossip_static_inbound: self + .gossip_static_inbound + .iter() + .map(|x| NodePublicKey(x.clone())) + .collect(), + gossip_static_outbound: self + .gossip_static_outbound + .iter() + .enumerate() + .map(|(i, e)| read_addr(e).context(i)) + .collect::>()?, + }) + } + + fn build(this: &Self::Type) -> Self { + Self { + server_addr: Some(this.server_addr.to_string()), + public_addr: Some(this.public_addr.0.clone()), + validators: this.validators.iter().map(|x| x.0.clone()).collect(), + max_payload_size: Some(this.max_payload_size.try_into().unwrap()), + gossip_dynamic_inbound_limit: Some( + this.gossip_dynamic_inbound_limit.try_into().unwrap(), + ), + gossip_static_inbound: this + .gossip_static_inbound + .iter() + .map(|x| x.0.clone()) + .collect(), + gossip_static_outbound: this + .gossip_static_outbound + .iter() + .map(|x| proto::NodeAddr { + key: Some(x.0 .0.clone()), + addr: Some(x.1 .0.clone()), + }) + .collect(), + } + } +} + +impl ProtoRepr for proto::Secrets { + type Type = ConsensusSecrets; + fn read(&self) -> anyhow::Result { + Ok(Self::Type { + validator_key: self + .validator_key + .as_ref() + .map(|x| ValidatorSecretKey(x.clone())), + node_key: self.node_key.as_ref().map(|x| NodeSecretKey(x.clone())), + }) + } + + fn build(this: &Self::Type) -> Self { + Self { + validator_key: this.validator_key.as_ref().map(|x| x.0.clone()), + node_key: this.node_key.as_ref().map(|x| x.0.clone()), + } + } +} diff --git a/core/lib/protobuf_config/src/lib.rs b/core/lib/protobuf_config/src/lib.rs index 94b9317e961..21cbcba283e 100644 --- a/core/lib/protobuf_config/src/lib.rs +++ b/core/lib/protobuf_config/src/lib.rs @@ -7,6 +7,7 @@ mod api; mod chain; mod circuit_breaker; +mod consensus; mod contract_verifier; mod contracts; mod database; @@ -39,6 +40,6 @@ fn parse_h160(bytes: &str) -> anyhow::Result { Ok(H160::from_str(bytes)?) } -fn read_optional_repr(field: &Option

) -> anyhow::Result> { +pub fn read_optional_repr(field: &Option

) -> anyhow::Result> { field.as_ref().map(|x| x.read()).transpose() } diff --git a/core/lib/protobuf_config/src/proto/api.proto b/core/lib/protobuf_config/src/proto/config/api.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/api.proto rename to core/lib/protobuf_config/src/proto/config/api.proto diff --git a/core/lib/protobuf_config/src/proto/chain.proto b/core/lib/protobuf_config/src/proto/config/chain.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/chain.proto rename to core/lib/protobuf_config/src/proto/config/chain.proto diff --git a/core/lib/protobuf_config/src/proto/circuit_breaker.proto b/core/lib/protobuf_config/src/proto/config/circuit_breaker.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/circuit_breaker.proto rename to core/lib/protobuf_config/src/proto/config/circuit_breaker.proto diff --git a/core/lib/protobuf_config/src/proto/contract_verifier.proto b/core/lib/protobuf_config/src/proto/config/contract_verifier.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/contract_verifier.proto rename to core/lib/protobuf_config/src/proto/config/contract_verifier.proto diff --git a/core/lib/protobuf_config/src/proto/contracts.proto b/core/lib/protobuf_config/src/proto/config/contracts.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/contracts.proto rename to core/lib/protobuf_config/src/proto/config/contracts.proto diff --git a/core/lib/protobuf_config/src/proto/database.proto b/core/lib/protobuf_config/src/proto/config/database.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/database.proto rename to core/lib/protobuf_config/src/proto/config/database.proto diff --git a/core/lib/protobuf_config/src/proto/eth_sender.proto b/core/lib/protobuf_config/src/proto/config/eth_sender.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/eth_sender.proto rename to core/lib/protobuf_config/src/proto/config/eth_sender.proto diff --git a/core/lib/protobuf_config/src/proto/general.proto b/core/lib/protobuf_config/src/proto/config/general.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/general.proto rename to core/lib/protobuf_config/src/proto/config/general.proto diff --git a/core/lib/protobuf_config/src/proto/genesis.proto b/core/lib/protobuf_config/src/proto/config/genesis.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/genesis.proto rename to core/lib/protobuf_config/src/proto/config/genesis.proto diff --git a/core/lib/protobuf_config/src/proto/house_keeper.proto b/core/lib/protobuf_config/src/proto/config/house_keeper.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/house_keeper.proto rename to core/lib/protobuf_config/src/proto/config/house_keeper.proto diff --git a/core/lib/protobuf_config/src/proto/object_store.proto b/core/lib/protobuf_config/src/proto/config/object_store.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/object_store.proto rename to core/lib/protobuf_config/src/proto/config/object_store.proto diff --git a/core/lib/protobuf_config/src/proto/observability.proto b/core/lib/protobuf_config/src/proto/config/observability.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/observability.proto rename to core/lib/protobuf_config/src/proto/config/observability.proto diff --git a/core/lib/protobuf_config/src/proto/prover.proto b/core/lib/protobuf_config/src/proto/config/prover.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/prover.proto rename to core/lib/protobuf_config/src/proto/config/prover.proto diff --git a/core/lib/protobuf_config/src/proto/snapshots_creator.proto b/core/lib/protobuf_config/src/proto/config/snapshots_creator.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/snapshots_creator.proto rename to core/lib/protobuf_config/src/proto/config/snapshots_creator.proto diff --git a/core/lib/protobuf_config/src/proto/utils.proto b/core/lib/protobuf_config/src/proto/config/utils.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/utils.proto rename to core/lib/protobuf_config/src/proto/config/utils.proto diff --git a/core/lib/protobuf_config/src/proto/wallets.proto b/core/lib/protobuf_config/src/proto/config/wallets.proto similarity index 100% rename from core/lib/protobuf_config/src/proto/wallets.proto rename to core/lib/protobuf_config/src/proto/config/wallets.proto diff --git a/core/lib/zksync_core/src/proto/consensus.proto b/core/lib/protobuf_config/src/proto/core/consensus.proto similarity index 96% rename from core/lib/zksync_core/src/proto/consensus.proto rename to core/lib/protobuf_config/src/proto/core/consensus.proto index 65ffb078904..9390445b3a0 100644 --- a/core/lib/zksync_core/src/proto/consensus.proto +++ b/core/lib/protobuf_config/src/proto/core/consensus.proto @@ -7,6 +7,8 @@ // (note that opening IPv6 ports may not work depending on the VM capabilities). // examples: "203.0.113.7:3456", "[2001:DB8::1]:4567" // +// Host - network address in the `:port` format. +// // ValidatorPublicKey - public key of the validator (consensus participant) of the form "validator:public::" // Currently only bn254 signature scheme is supported for validators. // example: "validator:public:bn254:4b0c4697f0a35eab30f63684ae4611f3c1d631eecfd97237e2345a9b3d0c472dbb16c49b793beceaab0cdd89cda6ff1099bd1aaf1ad6cabde9a15793cc09b407" @@ -40,7 +42,7 @@ message Config { // Public IP:port to advertise, should forward to server_addr. // Can be `127.0.0.1:` for local tests. - optional string public_addr = 2; // required; IpAddr + optional string public_addr = 2; // required; Host // Public keys of all validators. // Currently it has to be a singleton with a public key corresponding to secret key in CONSENSUS_VALIDATOR_KEY env var. @@ -65,5 +67,3 @@ message Secrets { optional string validator_key = 1; // required for validator nodes; ValidatorSecretKey optional string node_key = 2; // required for any node; NodeSecretKey } - - diff --git a/core/lib/protobuf_config/src/proto/mod.rs b/core/lib/protobuf_config/src/proto/mod.rs index 9f44835b29c..73e081736c9 100644 --- a/core/lib/protobuf_config/src/proto/mod.rs +++ b/core/lib/protobuf_config/src/proto/mod.rs @@ -1,3 +1,5 @@ #![allow(warnings)] +pub use self::{config::*, core::*}; + include!(concat!(env!("OUT_DIR"), "/src/proto/gen.rs")); diff --git a/core/lib/protobuf_config/src/tests.rs b/core/lib/protobuf_config/src/tests.rs index 41f69477756..0db119e718b 100644 --- a/core/lib/protobuf_config/src/tests.rs +++ b/core/lib/protobuf_config/src/tests.rs @@ -20,6 +20,8 @@ fn test_encoding() { test_encode_all_formats::>(rng); test_encode_all_formats::>(rng); test_encode_all_formats::>(rng); + test_encode_all_formats::>(rng); + test_encode_all_formats::>(rng); test_encode_all_formats::>(rng); test_encode_all_formats::>(rng); test_encode_all_formats::>(rng); diff --git a/core/lib/types/Cargo.toml b/core/lib/types/Cargo.toml index 8ffa5bf678f..b6628481f35 100644 --- a/core/lib/types/Cargo.toml +++ b/core/lib/types/Cargo.toml @@ -34,7 +34,7 @@ prost.workspace = true itertools.workspace = true # Crypto stuff -secp256k1 = { workspace = true, features = ["recovery", "global-context"] } +secp256k1.workspace = true blake2.workspace = true [dev-dependencies] diff --git a/core/lib/zksync_core/Cargo.toml b/core/lib/zksync_core/Cargo.toml index a70c63178a3..8a2f2b97d5d 100644 --- a/core/lib/zksync_core/Cargo.toml +++ b/core/lib/zksync_core/Cargo.toml @@ -20,6 +20,7 @@ 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 zksync_contracts.workspace = true zksync_system_constants.workspace = true diff --git a/core/lib/zksync_core/build.rs b/core/lib/zksync_core/build.rs index cda803150a6..78ad3018e33 100644 --- a/core/lib/zksync_core/build.rs +++ b/core/lib/zksync_core/build.rs @@ -3,7 +3,7 @@ fn main() { zksync_protobuf_build::Config { input_root: "src/proto".into(), proto_root: "zksync/core".into(), - dependencies: vec![], + dependencies: vec!["::zksync_protobuf_config::proto".parse().unwrap()], protobuf_crate: "::zksync_protobuf".parse().unwrap(), is_public: true, } diff --git a/core/lib/zksync_core/src/consensus/config.rs b/core/lib/zksync_core/src/consensus/config.rs index 2a6d8e85f8e..31532cbd14e 100644 --- a/core/lib/zksync_core/src/consensus/config.rs +++ b/core/lib/zksync_core/src/consensus/config.rs @@ -1,22 +1,18 @@ //! Configuration utilities for the consensus component. -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use anyhow::Context as _; use zksync_concurrency::net; -use zksync_consensus_crypto::{read_required_text, Text, TextFmt}; +use zksync_config::configs::consensus::{ConsensusConfig, ConsensusSecrets, Host, NodePublicKey}; +use zksync_consensus_crypto::{Text, TextFmt}; use zksync_consensus_executor as executor; use zksync_consensus_roles::{node, validator}; -use zksync_protobuf::{required, ProtoFmt}; -use crate::{ - consensus::{fetcher::P2PConfig, MainNodeConfig}, - proto::consensus as proto, -}; +use crate::consensus::{fetcher::P2PConfig, MainNodeConfig}; -fn read_optional_secret_text(text: &Option) -> anyhow::Result> { - text.as_ref() - .map(|t| Text::new(t).decode()) - .transpose() +fn read_secret_text(text: Option<&String>) -> anyhow::Result { + Text::new(text.context("missing")?) + .decode() .map_err(|_| anyhow::format_err!("invalid format")) } @@ -46,128 +42,56 @@ pub struct Config { pub gossip_static_outbound: BTreeMap, } -impl Config { - pub fn main_node(&self, secrets: &Secrets) -> anyhow::Result { - Ok(MainNodeConfig { - executor: self.executor_config(secrets.node_key.clone().context("missing node_key")?), - validator_key: secrets - .validator_key - .clone() - .context("missing validator_key")?, - }) - } - - pub fn p2p(&self, secrets: &Secrets) -> anyhow::Result { - Ok(self.executor_config(secrets.node_key.clone().context("missing node_key")?)) - } - - fn executor_config(&self, node_key: node::SecretKey) -> executor::Config { - executor::Config { - server_addr: self.server_addr, - public_addr: self.public_addr.clone(), - max_payload_size: self.max_payload_size, - node_key, - gossip_dynamic_inbound_limit: self.gossip_dynamic_inbound_limit, - gossip_static_inbound: self.gossip_static_inbound.clone().into_iter().collect(), - gossip_static_outbound: self.gossip_static_outbound.clone().into_iter().collect(), - } - } +fn validator_key(secrets: &ConsensusSecrets) -> anyhow::Result { + read_secret_text(secrets.validator_key.as_ref().map(|x| &x.0)) } -impl ProtoFmt for Config { - type Proto = proto::Config; - fn read(r: &Self::Proto) -> anyhow::Result { - let validators = r - .validators - .iter() - .enumerate() - .map(|(i, v)| { - Text::new(v) - .decode() - .with_context(|| format!("validators[{i}]")) - }) - .collect::, _>>()?; - let validators = validator::ValidatorSet::new(validators).context("validators")?; - - let mut gossip_static_inbound = BTreeSet::new(); - for (i, v) in r.gossip_static_inbound.iter().enumerate() { - gossip_static_inbound.insert( - Text::new(v) - .decode() - .with_context(|| format!("gossip_static_inbound[{i}]"))?, - ); - } - let mut gossip_static_outbound = BTreeMap::new(); - for (i, e) in r.gossip_static_outbound.iter().enumerate() { - let key = read_required_text(&e.key) - .with_context(|| format!("gossip_static_outbound[{i}].key"))?; - let addr = net::Host( - required(&e.addr) - .with_context(|| format!("gossip_static_outbound[{i}].addr"))? - .clone(), - ); - gossip_static_outbound.insert(key, addr); - } - Ok(Self { - server_addr: read_required_text(&r.server_addr).context("server_addr")?, - public_addr: net::Host(required(&r.public_addr).context("public_addr")?.clone()), - validators, - max_payload_size: required(&r.max_payload_size) - .and_then(|x| Ok((*x).try_into()?)) - .context("max_payload_size")?, - gossip_dynamic_inbound_limit: required(&r.gossip_dynamic_inbound_limit) - .and_then(|x| Ok((*x).try_into()?)) - .context("gossip_dynamic_inbound_limit")?, - gossip_static_inbound, - gossip_static_outbound, - }) - } - - fn build(&self) -> Self::Proto { - Self::Proto { - server_addr: Some(self.server_addr.encode()), - public_addr: Some(self.public_addr.0.clone()), - validators: self.validators.iter().map(TextFmt::encode).collect(), - max_payload_size: Some(self.max_payload_size.try_into().unwrap()), - gossip_static_inbound: self - .gossip_static_inbound - .iter() - .map(TextFmt::encode) - .collect(), - gossip_static_outbound: self - .gossip_static_outbound - .iter() - .map(|(key, addr)| proto::NodeAddr { - key: Some(TextFmt::encode(key)), - addr: Some(addr.0.clone()), - }) - .collect(), - gossip_dynamic_inbound_limit: Some( - self.gossip_dynamic_inbound_limit.try_into().unwrap(), - ), - } - } +fn node_key(secrets: &ConsensusSecrets) -> anyhow::Result { + read_secret_text(secrets.node_key.as_ref().map(|x| &x.0)) } -#[derive(Debug)] -pub struct Secrets { - pub validator_key: Option, - pub node_key: Option, +/// Constructs a main node config from raw config. +pub fn main_node( + cfg: &ConsensusConfig, + secrets: &ConsensusSecrets, +) -> anyhow::Result { + Ok(MainNodeConfig { + executor: executor(cfg, secrets)?, + validator_key: validator_key(secrets).context("validator_key")?, + }) } -impl ProtoFmt for Secrets { - type Proto = proto::Secrets; - fn read(r: &Self::Proto) -> anyhow::Result { - Ok(Self { - validator_key: read_optional_secret_text(&r.validator_key).context("validator_key")?, - node_key: read_optional_secret_text(&r.node_key).context("node_key")?, - }) - } +pub(super) fn p2p(cfg: &ConsensusConfig, secrets: &ConsensusSecrets) -> anyhow::Result { + executor(cfg, secrets) +} - fn build(&self) -> Self::Proto { - Self::Proto { - validator_key: self.validator_key.as_ref().map(TextFmt::encode), - node_key: self.node_key.as_ref().map(TextFmt::encode), +fn executor(cfg: &ConsensusConfig, secrets: &ConsensusSecrets) -> anyhow::Result { + let mut gossip_static_outbound = HashMap::new(); + { + let mut append = |key: &NodePublicKey, addr: &Host| { + gossip_static_outbound.insert( + Text::new(&key.0).decode().context("key")?, + net::Host(addr.0.clone()), + ); + anyhow::Ok(()) + }; + for (i, (k, v)) in cfg.gossip_static_outbound.iter().enumerate() { + append(k, v).with_context(|| format!("gossip_static_outbound[{i}]"))?; } } + Ok(executor::Config { + server_addr: cfg.server_addr, + public_addr: net::Host(cfg.public_addr.0.clone()), + max_payload_size: cfg.max_payload_size, + node_key: node_key(secrets).context("node_key")?, + gossip_dynamic_inbound_limit: cfg.gossip_dynamic_inbound_limit, + gossip_static_inbound: cfg + .gossip_static_inbound + .iter() + .enumerate() + .map(|(i, x)| Text::new(&x.0).decode().context(i)) + .collect::>() + .context("gossip_static_inbound")?, + gossip_static_outbound, + }) } diff --git a/core/lib/zksync_core/src/consensus/era.rs b/core/lib/zksync_core/src/consensus/era.rs index df5f0f6229f..8853858ac71 100644 --- a/core/lib/zksync_core/src/consensus/era.rs +++ b/core/lib/zksync_core/src/consensus/era.rs @@ -7,13 +7,10 @@ use std::sync::Arc; use zksync_concurrency::ctx; +use zksync_config::configs::consensus::{ConsensusConfig, ConsensusSecrets}; use zksync_dal::{ConnectionPool, Core}; -use super::{ - config::{Config, Secrets}, - fetcher::Fetcher, - storage::Store, -}; +use super::{config, fetcher::Fetcher, storage::Store}; use crate::sync_layer::{sync_action::ActionQueueSender, MainNodeClient, SyncState}; /// Runs the consensus task in the main node mode. @@ -37,7 +34,7 @@ pub async fn run_main_node( /// The fetcher implementation may either be p2p or centralized. pub async fn run_fetcher( ctx: &ctx::Ctx, - cfg: Option<(Config, Secrets)>, + cfg: Option<(ConsensusConfig, ConsensusSecrets)>, pool: ConnectionPool, sync_state: SyncState, main_node_client: Arc, @@ -49,7 +46,11 @@ pub async fn run_fetcher( client: main_node_client, }; let res = match cfg { - Some((cfg, secrets)) => fetcher.run_p2p(ctx, actions, cfg.p2p(&secrets)?).await, + Some((cfg, secrets)) => { + fetcher + .run_p2p(ctx, actions, config::p2p(&cfg, &secrets)?) + .await + } None => fetcher.run_centralized(ctx, actions).await, }; tracing::info!("Consensus actor stopped"); diff --git a/core/lib/zksync_core/src/consensus/mod.rs b/core/lib/zksync_core/src/consensus/mod.rs index 282319240c2..433d79a53b8 100644 --- a/core/lib/zksync_core/src/consensus/mod.rs +++ b/core/lib/zksync_core/src/consensus/mod.rs @@ -10,7 +10,7 @@ use zksync_consensus_storage::BlockStore; pub use self::{fetcher::*, storage::Store}; -mod config; +pub mod config; pub mod era; mod fetcher; mod storage; @@ -19,8 +19,6 @@ pub(crate) mod testonly; #[cfg(test)] mod tests; -pub use config::{Config, Secrets}; - /// Main node consensus config. #[derive(Debug, Clone)] pub struct MainNodeConfig { diff --git a/core/lib/zksync_core/src/consensus/tests.rs b/core/lib/zksync_core/src/consensus/tests.rs index a80e450646f..dc0a5e80b0d 100644 --- a/core/lib/zksync_core/src/consensus/tests.rs +++ b/core/lib/zksync_core/src/consensus/tests.rs @@ -1,5 +1,5 @@ use anyhow::Context as _; -use rand::{distributions::Distribution, Rng}; +use rand::Rng; use test_casing::test_casing; use tracing::Instrument as _; use zksync_concurrency::{ctx, scope}; @@ -9,8 +9,6 @@ use zksync_consensus_network::testonly::{new_configs, new_fullnode}; use zksync_consensus_roles::validator::testonly::Setup; use zksync_consensus_storage as storage; use zksync_consensus_storage::PersistentBlockStore as _; -use zksync_consensus_utils::EncodeDist; -use zksync_protobuf::testonly::{test_encode_all_formats, FmtConv}; use zksync_types::{L1BatchNumber, MiniblockNumber}; use super::*; @@ -438,36 +436,3 @@ async fn test_centralized_fetcher(from_snapshot: bool) { .await .unwrap(); } - -impl Distribution for EncodeDist { - fn sample(&self, rng: &mut R) -> Config { - Config { - server_addr: self.sample(rng), - public_addr: self.sample(rng), - validators: rng.gen(), - max_payload_size: self.sample(rng), - gossip_dynamic_inbound_limit: self.sample(rng), - gossip_static_inbound: self.sample_range(rng).map(|_| rng.gen()).collect(), - gossip_static_outbound: self - .sample_range(rng) - .map(|_| (rng.gen(), self.sample(rng))) - .collect(), - } - } -} - -impl Distribution for EncodeDist { - fn sample(&self, rng: &mut R) -> Secrets { - Secrets { - validator_key: self.sample_opt(|| rng.gen()), - node_key: self.sample_opt(|| rng.gen()), - } - } -} - -#[test] -fn test_schema_encoding() { - let ctx = ctx::test_root(&ctx::RealClock); - let rng = &mut ctx.rng(); - test_encode_all_formats::>(rng); -} diff --git a/core/lib/zksync_core/src/lib.rs b/core/lib/zksync_core/src/lib.rs index dd2cf89b0a4..273924577da 100644 --- a/core/lib/zksync_core/src/lib.rs +++ b/core/lib/zksync_core/src/lib.rs @@ -29,6 +29,7 @@ use zksync_config::{ CircuitBreakerConfig, L1BatchCommitDataGeneratorMode, MempoolConfig, OperationsManagerConfig, StateKeeperConfig, }, + consensus::ConsensusConfig, database::{MerkleTreeConfig, MerkleTreeMode}, wallets, wallets::Wallets, @@ -240,7 +241,7 @@ pub async fn initialize_components( contracts_config: &ContractsConfig, components: &[Component], secrets: &Secrets, - consensus_config: Option, + consensus_config: Option, ) -> anyhow::Result<( Vec>>, watch::Sender, @@ -549,10 +550,12 @@ pub async fn initialize_components( if components.contains(&Component::Consensus) { let secrets = secrets.consensus.as_ref().context("Secrets are missing")?; - let cfg = consensus_config - .clone() - .context("consensus component's config is missing")? - .main_node(secrets)?; + let cfg = consensus::config::main_node( + consensus_config + .as_ref() + .context("consensus component's config is missing")?, + secrets, + )?; let started_at = Instant::now(); tracing::info!("initializing Consensus"); let pool = connection_pool.clone(); diff --git a/core/lib/zksync_core/src/temp_config_store/mod.rs b/core/lib/zksync_core/src/temp_config_store/mod.rs index f471a7b251c..55fb9e11d94 100644 --- a/core/lib/zksync_core/src/temp_config_store/mod.rs +++ b/core/lib/zksync_core/src/temp_config_store/mod.rs @@ -6,6 +6,7 @@ use zksync_config::{ CircuitBreakerConfig, MempoolConfig, NetworkConfig, OperationsManagerConfig, StateKeeperConfig, }, + consensus::ConsensusSecrets, fri_prover_group::FriProverGroupConfig, house_keeper::HouseKeeperConfig, wallets::{AddressWallet, EthSender, StateKeeper, Wallet, Wallets}, @@ -16,9 +17,10 @@ use zksync_config::{ ApiConfig, ContractVerifierConfig, DBConfig, ETHConfig, ETHWatchConfig, GasAdjusterConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig, }; -use zksync_protobuf::{read_optional, repr::ProtoRepr, ProtoFmt}; +use zksync_protobuf::{repr::ProtoRepr, ProtoFmt}; +use zksync_protobuf_config::read_optional_repr; -use crate::{consensus, proto}; +use crate::proto; pub fn decode_yaml(yaml: &str) -> anyhow::Result { let d = serde_yaml::Deserializer::from_str(yaml); @@ -68,20 +70,20 @@ pub struct TempConfigStore { #[derive(Debug)] pub struct Secrets { - pub consensus: Option, + pub consensus: Option, } impl ProtoFmt for Secrets { type Proto = proto::Secrets; fn read(r: &Self::Proto) -> anyhow::Result { Ok(Self { - consensus: read_optional(&r.consensus).context("consensus")?, + consensus: read_optional_repr(&r.consensus).context("consensus")?, }) } fn build(&self) -> Self::Proto { Self::Proto { - consensus: self.consensus.as_ref().map(|x| x.build()), + consensus: self.consensus.as_ref().map(ProtoRepr::build), } } } diff --git a/core/node/node_framework/Cargo.toml b/core/node/node_framework/Cargo.toml index 0c83e9d0f06..1b8341bf1ae 100644 --- a/core/node/node_framework/Cargo.toml +++ b/core/node/node_framework/Cargo.toml @@ -17,6 +17,7 @@ zksync_dal.workspace = true prover_dal.workspace = true zksync_db_connection.workspace = true zksync_config.workspace = true +zksync_protobuf_config.workspace = true zksync_state.workspace = true zksync_object_store.workspace = true zksync_core.workspace = true diff --git a/core/node/node_framework/examples/main_node.rs b/core/node/node_framework/examples/main_node.rs index 93a2856eb43..32ed33c644a 100644 --- a/core/node/node_framework/examples/main_node.rs +++ b/core/node/node_framework/examples/main_node.rs @@ -9,6 +9,7 @@ use zksync_config::{ CircuitBreakerConfig, MempoolConfig, NetworkConfig, OperationsManagerConfig, StateKeeperConfig, }, + consensus::{ConsensusConfig, ConsensusSecrets}, fri_prover_group::FriProverGroupConfig, house_keeper::HouseKeeperConfig, wallets::Wallets, @@ -23,9 +24,8 @@ use zksync_core::{ tx_sender::{ApiContracts, TxSenderConfig}, web3::{state::InternalApiConfig, Namespace}, }, - consensus, metadata_calculator::MetadataCalculatorConfig, - temp_config_store::decode_yaml, + temp_config_store::decode_yaml_repr, }; use zksync_env_config::FromEnv; use zksync_node_framework::{ @@ -60,6 +60,7 @@ use zksync_node_framework::{ }, service::{ZkStackService, ZkStackServiceBuilder, ZkStackServiceError}, }; +use zksync_protobuf_config::proto; struct MainNodeBuilder { node: ZkStackServiceBuilder, @@ -355,22 +356,28 @@ impl MainNodeBuilder { fn add_consensus_layer(mut self) -> anyhow::Result { // Copy-pasted from the zksync_server codebase. - fn read_consensus_secrets() -> anyhow::Result> { + fn read_consensus_secrets() -> anyhow::Result> { // Read public config. let Ok(path) = std::env::var("CONSENSUS_SECRETS_PATH") else { return Ok(None); }; let secrets = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&secrets).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&secrets) + .context("failed decoding YAML")?, + )) } - fn read_consensus_config() -> anyhow::Result> { + fn read_consensus_config() -> anyhow::Result> { // Read public config. let Ok(path) = std::env::var("CONSENSUS_CONFIG_PATH") else { return Ok(None); }; let cfg = std::fs::read_to_string(&path).context(path)?; - Ok(Some(decode_yaml(&cfg).context("failed decoding YAML")?)) + Ok(Some( + decode_yaml_repr::(&cfg) + .context("failed decoding YAML")?, + )) } let config = read_consensus_config().context("read_consensus_config()")?; diff --git a/core/node/node_framework/src/implementations/layers/consensus.rs b/core/node/node_framework/src/implementations/layers/consensus.rs index b5fd528416f..48f26bb1b8b 100644 --- a/core/node/node_framework/src/implementations/layers/consensus.rs +++ b/core/node/node_framework/src/implementations/layers/consensus.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use anyhow::Context as _; use zksync_concurrency::{ctx, scope}; +use zksync_config::configs::consensus::{ConsensusConfig, ConsensusSecrets}; use zksync_core::{ consensus::{self, MainNodeConfig}, sync_layer::{ActionQueueSender, MainNodeClient, SyncState}, @@ -27,8 +28,8 @@ pub enum Mode { #[derive(Debug)] pub struct ConsensusLayer { pub mode: Mode, - pub config: Option, - pub secrets: Option, + pub config: Option, + pub secrets: Option, } #[async_trait::async_trait] @@ -53,7 +54,7 @@ impl WiringLayer for ConsensusLayer { WiringError::Configuration("Missing private consensus config".to_string()) })?; - let main_node_config = config.main_node(&secrets)?; + let main_node_config = consensus::config::main_node(&config, &secrets)?; let task = MainNodeConsensusTask { config: main_node_config, @@ -134,7 +135,7 @@ impl Task for MainNodeConsensusTask { #[derive(Debug)] pub struct FetcherTask { - config: Option<(consensus::Config, consensus::Secrets)>, + config: Option<(ConsensusConfig, ConsensusSecrets)>, pool: ConnectionPool, main_node_client: Arc, sync_state: SyncState,