From ae9ecc2b340f77f1efe45519138340f02ebc7c43 Mon Sep 17 00:00:00 2001 From: pompon0 Date: Mon, 29 Apr 2024 17:55:35 +0200 Subject: [PATCH] chore: bumped consensus deps (#1824) This PR bumps era-consensus to a non-backward compatible version, so I've purged the consensus deployment on stage. I'll reenable consensus on stage once this PR is merged. --- Cargo.lock | 49 +++++++------------ Cargo.toml | 20 ++++---- core/lib/config/src/configs/consensus.rs | 3 -- core/lib/config/src/testonly.rs | 6 +-- core/lib/dal/src/consensus_dal.rs | 49 ++++++++++--------- core/lib/protobuf_config/src/consensus.rs | 9 +--- .../src/proto/core/consensus.proto | 7 ++- core/lib/zksync_core/src/consensus/config.rs | 31 ++---------- core/lib/zksync_core/src/consensus/fetcher.rs | 2 +- core/lib/zksync_core/src/consensus/mod.rs | 3 +- .../zksync_core/src/consensus/storage/mod.rs | 41 ++++++++++++---- core/lib/zksync_core/src/consensus/tests.rs | 29 ++++++----- core/lib/zksync_core/src/lib.rs | 1 + .../node/node_framework/examples/main_node.rs | 2 + .../src/implementations/layers/consensus.rs | 5 +- etc/env/consensus_config.yaml | 6 +-- etc/env/consensus_secrets.yaml | 6 ++- etc/env/en_consensus_config.yaml | 4 +- etc/env/en_consensus_secrets.yaml | 3 +- prover/Cargo.lock | 18 ++++--- 20 files changed, 140 insertions(+), 154 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a14f2a34908b..ba778bf08966 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3982,9 +3982,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", "libm", @@ -7849,9 +7849,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" dependencies = [ "zeroize_derive", ] @@ -8187,7 +8187,7 @@ dependencies = [ [[package]] name = "zksync_concurrency" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "once_cell", @@ -8217,7 +8217,7 @@ dependencies = [ [[package]] name = "zksync_consensus_bft" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "async-trait", @@ -8238,25 +8238,28 @@ dependencies = [ [[package]] name = "zksync_consensus_crypto" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "blst", "ed25519-dalek", "ff_ce", "hex", + "num-bigint 0.4.4", + "num-traits", "pairing_ce 0.28.5 (git+https://github.com/matter-labs/pairing.git?rev=d24f2c5871089c4cd4f54c0ca266bb9fef6115eb)", "rand 0.4.6", "rand 0.8.5", "sha3 0.10.8", "thiserror", "tracing", + "zeroize", ] [[package]] name = "zksync_consensus_executor" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "rand 0.8.5", @@ -8268,7 +8271,6 @@ dependencies = [ "zksync_consensus_network", "zksync_consensus_roles", "zksync_consensus_storage", - "zksync_consensus_sync_blocks", "zksync_consensus_utils", "zksync_protobuf", ] @@ -8276,7 +8278,7 @@ dependencies = [ [[package]] name = "zksync_consensus_network" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "async-trait", @@ -8301,11 +8303,12 @@ dependencies = [ [[package]] name = "zksync_consensus_roles" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "bit-vec", "hex", + "num-bigint 0.4.4", "prost 0.12.1", "rand 0.8.5", "serde", @@ -8321,7 +8324,7 @@ dependencies = [ [[package]] name = "zksync_consensus_storage" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "async-trait", @@ -8336,26 +8339,10 @@ dependencies = [ "zksync_protobuf_build", ] -[[package]] -name = "zksync_consensus_sync_blocks" -version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" -dependencies = [ - "anyhow", - "rand 0.8.5", - "thiserror", - "tracing", - "zksync_concurrency", - "zksync_consensus_network", - "zksync_consensus_roles", - "zksync_consensus_storage", - "zksync_consensus_utils", -] - [[package]] name = "zksync_consensus_utils" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "rand 0.8.5", "thiserror", @@ -8843,7 +8830,7 @@ dependencies = [ [[package]] name = "zksync_protobuf" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "bit-vec", @@ -8863,7 +8850,7 @@ dependencies = [ [[package]] name = "zksync_protobuf_build" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "heck 0.5.0", diff --git a/Cargo.toml b/Cargo.toml index 0d5355df0cff..f8163e811e36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -170,16 +170,16 @@ zk_evm_1_3_3 = { package = "zk_evm", git = "https://github.com/matter-labs/era-z zk_evm_1_4_0 = { package = "zk_evm", git = "https://github.com/matter-labs/era-zk_evm.git", branch = "v1.4.0" } zk_evm_1_4_1 = { package = "zk_evm", git = "https://github.com/matter-labs/era-zk_evm.git", branch = "v1.4.1" } zk_evm_1_5_0 = { package = "zk_evm", git = "https://github.com/matter-labs/era-zk_evm.git", branch = "v1.5.0" } -zksync_concurrency = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_bft = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_crypto = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_executor = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_network = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_roles = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_storage = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_consensus_utils = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_protobuf = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } -zksync_protobuf_build = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" } +zksync_concurrency = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_bft = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_crypto = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_executor = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_network = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_roles = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_storage = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_consensus_utils = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_protobuf = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } +zksync_protobuf_build = { version = "0.1.0", git = "https://github.com/matter-labs/era-consensus.git", rev = "92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" } # "Local" dependencies multivm = { path = "core/lib/multivm" } diff --git a/core/lib/config/src/configs/consensus.rs b/core/lib/config/src/configs/consensus.rs index d7bcb5503cc2..2033360e406a 100644 --- a/core/lib/config/src/configs/consensus.rs +++ b/core/lib/config/src/configs/consensus.rs @@ -45,9 +45,6 @@ pub struct ConsensusConfig { /// 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, diff --git a/core/lib/config/src/testonly.rs b/core/lib/config/src/testonly.rs index 2704f1738928..81a495f7d6dd 100644 --- a/core/lib/config/src/testonly.rs +++ b/core/lib/config/src/testonly.rs @@ -698,14 +698,10 @@ impl Distribution for EncodeDist { impl Distribution for EncodeDist { fn sample(&self, rng: &mut R) -> configs::consensus::ConsensusConfig { - use configs::consensus::{ConsensusConfig, Host, NodePublicKey, ValidatorPublicKey}; + use configs::consensus::{ConsensusConfig, Host, NodePublicKey}; 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 diff --git a/core/lib/dal/src/consensus_dal.rs b/core/lib/dal/src/consensus_dal.rs index 5abbff72ce58..6dab5f0ae2f5 100644 --- a/core/lib/dal/src/consensus_dal.rs +++ b/core/lib/dal/src/consensus_dal.rs @@ -33,11 +33,12 @@ impl ConsensusDal<'_, '_> { "# ) .try_map(|row| { - row.genesis - .map(|genesis| { - zksync_protobuf::serde::deserialize(genesis).decode_column("genesis") - }) - .transpose() + let Some(genesis) = row.genesis else { + return Ok(None); + }; + let genesis: validator::GenesisRaw = + zksync_protobuf::serde::deserialize(genesis).decode_column("genesis")?; + Ok(Some(genesis.with_hash())) }) .instrument("genesis") .fetch_optional(self.storage) @@ -57,10 +58,10 @@ impl ConsensusDal<'_, '_> { return Ok(()); } anyhow::ensure!( - got.fork.number < genesis.fork.number, + got.fork_number < genesis.fork_number, "transition to a past fork is not allowed: old = {:?}, new = {:?}", - got.fork.number, - genesis.fork.number, + got.fork_number, + genesis.fork_number, ); } let genesis = @@ -138,13 +139,16 @@ impl ConsensusDal<'_, '_> { .await .context("get_block_range()")? .end; - let new = validator::Genesis { - validators: old.validators, - fork: validator::Fork { - number: old.fork.number.next(), - first_block, - }, - }; + let new = validator::GenesisRaw { + chain_id: old.chain_id, + fork_number: old.fork_number.next(), + first_block, + + protocol_version: validator::ProtocolVersion::CURRENT, + committee: old.committee.clone(), + leader_selection: old.leader_selection.clone(), + } + .with_hash(); txn.consensus_dal().try_update_genesis(&new).await?; txn.commit().await?; Ok(()) @@ -355,17 +359,16 @@ mod tests { let mut conn = pool.connection().await.unwrap(); assert_eq!(None, conn.consensus_dal().genesis().await.unwrap()); for n in 0..3 { - let fork = validator::Fork { - number: validator::ForkNumber(n), - first_block: rng.gen(), - }; - let setup = validator::testonly::Setup::new_with_fork(rng, 3, fork); + let setup = validator::testonly::Setup::new(rng, 3); + let mut genesis = (*setup.genesis).clone(); + genesis.fork_number = validator::ForkNumber(n); + let genesis = genesis.with_hash(); conn.consensus_dal() - .try_update_genesis(&setup.genesis) + .try_update_genesis(&genesis) .await .unwrap(); assert_eq!( - setup.genesis, + genesis, conn.consensus_dal().genesis().await.unwrap().unwrap() ); assert_eq!( @@ -376,7 +379,7 @@ mod tests { let want: ReplicaState = rng.gen(); conn.consensus_dal().set_replica_state(&want).await.unwrap(); assert_eq!( - setup.genesis, + genesis, conn.consensus_dal().genesis().await.unwrap().unwrap() ); assert_eq!(want, conn.consensus_dal().replica_state().await.unwrap()); diff --git a/core/lib/protobuf_config/src/consensus.rs b/core/lib/protobuf_config/src/consensus.rs index 5d290f9c5b16..555cd1e64e0b 100644 --- a/core/lib/protobuf_config/src/consensus.rs +++ b/core/lib/protobuf_config/src/consensus.rs @@ -1,7 +1,6 @@ use anyhow::Context as _; use zksync_config::configs::consensus::{ - ConsensusConfig, ConsensusSecrets, Host, NodePublicKey, NodeSecretKey, ValidatorPublicKey, - ValidatorSecretKey, + ConsensusConfig, ConsensusSecrets, Host, NodePublicKey, NodeSecretKey, ValidatorSecretKey, }; use zksync_protobuf::{repr::ProtoRepr, required}; @@ -20,11 +19,6 @@ impl ProtoRepr for proto::Config { .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")?, @@ -49,7 +43,6 @@ impl ProtoRepr for proto::Config { 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(), diff --git a/core/lib/protobuf_config/src/proto/core/consensus.proto b/core/lib/protobuf_config/src/proto/core/consensus.proto index 9390445b3a06..6bc1ba023eea 100644 --- a/core/lib/protobuf_config/src/proto/core/consensus.proto +++ b/core/lib/protobuf_config/src/proto/core/consensus.proto @@ -36,6 +36,9 @@ message NodeAddr { } message Config { + reserved 3; + reserved "validators"; + // IP:port to listen on, for incoming TCP connections. // Use `0.0.0.0:` to listen on all network interfaces (i.e. on all IPs exposed by this VM). optional string server_addr = 1; // required; IpAddr @@ -44,10 +47,6 @@ message Config { // Can be `127.0.0.1:` for local tests. 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. - repeated string validators = 3; // required; ValidatorPublicKey - // Maximal allowed size of the payload. optional uint64 max_payload_size = 4; // required; bytes diff --git a/core/lib/zksync_core/src/consensus/config.rs b/core/lib/zksync_core/src/consensus/config.rs index 31532cbd14ef..998a56a554a0 100644 --- a/core/lib/zksync_core/src/consensus/config.rs +++ b/core/lib/zksync_core/src/consensus/config.rs @@ -1,5 +1,5 @@ //! Configuration utilities for the consensus component. -use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::collections::HashMap; use anyhow::Context as _; use zksync_concurrency::net; @@ -7,6 +7,7 @@ use zksync_config::configs::consensus::{ConsensusConfig, ConsensusSecrets, Host, use zksync_consensus_crypto::{Text, TextFmt}; use zksync_consensus_executor as executor; use zksync_consensus_roles::{node, validator}; +use zksync_types::L2ChainId; use crate::consensus::{fetcher::P2PConfig, MainNodeConfig}; @@ -16,32 +17,6 @@ fn read_secret_text(text: Option<&String>) -> anyhow::Result { .map_err(|_| anyhow::format_err!("invalid format")) } -/// Config (shared between main node and external node). -#[derive(Clone, Debug, PartialEq)] -pub struct Config { - /// 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: net::Host, - - /// Validators participating in consensus. - pub validators: validator::ValidatorSet, - - /// 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, -} - fn validator_key(secrets: &ConsensusSecrets) -> anyhow::Result { read_secret_text(secrets.validator_key.as_ref().map(|x| &x.0)) } @@ -54,10 +29,12 @@ fn node_key(secrets: &ConsensusSecrets) -> anyhow::Result { pub fn main_node( cfg: &ConsensusConfig, secrets: &ConsensusSecrets, + chain_id: L2ChainId, ) -> anyhow::Result { Ok(MainNodeConfig { executor: executor(cfg, secrets)?, validator_key: validator_key(secrets).context("validator_key")?, + chain_id: validator::ChainId(chain_id.as_u64()), }) } diff --git a/core/lib/zksync_core/src/consensus/fetcher.rs b/core/lib/zksync_core/src/consensus/fetcher.rs index f83700787c5d..787be42dbcc8 100644 --- a/core/lib/zksync_core/src/consensus/fetcher.rs +++ b/core/lib/zksync_core/src/consensus/fetcher.rs @@ -47,7 +47,7 @@ impl Fetcher { drop(conn); // Fetch blocks before the genesis. - self.fetch_blocks(ctx, &mut payload_queue, Some(genesis.fork.first_block)) + self.fetch_blocks(ctx, &mut payload_queue, Some(genesis.first_block)) .await?; // Monitor the genesis of the main node. // If it changes, it means that a hard fork occurred and we need to reset the consensus state. diff --git a/core/lib/zksync_core/src/consensus/mod.rs b/core/lib/zksync_core/src/consensus/mod.rs index fc6471afd57d..449532fdc5ef 100644 --- a/core/lib/zksync_core/src/consensus/mod.rs +++ b/core/lib/zksync_core/src/consensus/mod.rs @@ -23,6 +23,7 @@ mod tests; pub struct MainNodeConfig { pub executor: executor::Config, pub validator_key: validator::SecretKey, + pub chain_id: validator::ChainId, } impl MainNodeConfig { @@ -31,7 +32,7 @@ impl MainNodeConfig { pub async fn run(self, ctx: &ctx::Ctx, store: Store) -> anyhow::Result<()> { scope::run!(&ctx, |ctx, s| async { store - .try_init_genesis(ctx, &self.validator_key.public()) + .try_init_genesis(ctx, self.chain_id, &self.validator_key.public()) .await .wrap("block_store.try_init_genesis()")?; let (block_store, runner) = store diff --git a/core/lib/zksync_core/src/consensus/storage/mod.rs b/core/lib/zksync_core/src/consensus/storage/mod.rs index 22685538dfbe..0083481deed5 100644 --- a/core/lib/zksync_core/src/consensus/storage/mod.rs +++ b/core/lib/zksync_core/src/consensus/storage/mod.rs @@ -249,7 +249,7 @@ impl Store { .await .wrap("genesis()")? .context("genesis missing")?; - let first_expected_cert = genesis.fork.first_block.max(block_range.start); + let first_expected_cert = genesis.first_block.max(block_range.start); let last_cert = conn .last_certificate(ctx) .await @@ -308,6 +308,7 @@ impl Store { pub(super) async fn try_init_genesis( &self, ctx: &ctx::Ctx, + chain_id: validator::ChainId, validator_key: &validator::PublicKey, ) -> ctx::Result<()> { let mut conn = self.access(ctx).await.wrap("access()")?; @@ -316,17 +317,37 @@ impl Store { .start_transaction(ctx) .await .wrap("start_transaction()")?; - if txn.genesis(ctx).await.wrap("genesis()")?.is_some() { + // `Committee::new()` with a single validator should never fail. + let committee = validator::Committee::new([validator::WeightedValidator { + key: validator_key.clone(), + weight: 1, + }]) + .unwrap(); + let leader_selection = validator::LeaderSelectionMode::Sticky(validator_key.clone()); + let old = txn.genesis(ctx).await.wrap("genesis()")?; + // Check if the current config of the main node is compatible with the stored genesis. + if old.as_ref().map_or(false, |old| { + old.chain_id == chain_id + && old.protocol_version == validator::ProtocolVersion::CURRENT + && old.committee == committee + && old.leader_selection == leader_selection + }) { return Ok(()); } - let genesis = validator::Genesis { - // `ValidatorSet::new()` with a single validator should never fail. - validators: validator::ValidatorSet::new([validator_key.clone()]).unwrap(), - fork: validator::Fork { - number: validator::ForkNumber(0), - first_block: block_range.end, - }, - }; + // If not, perform a hard fork. + tracing::info!("Performing a hard fork of consensus."); + let genesis = validator::GenesisRaw { + chain_id, + fork_number: old + .as_ref() + .map_or(validator::ForkNumber(0), |old| old.fork_number.next()), + first_block: block_range.end, + + protocol_version: validator::ProtocolVersion::CURRENT, + committee, + leader_selection, + } + .with_hash(); txn.try_update_genesis(ctx, &genesis) .await .wrap("try_update_genesis()")?; diff --git a/core/lib/zksync_core/src/consensus/tests.rs b/core/lib/zksync_core/src/consensus/tests.rs index 2aa6bb05bf5c..fbf94fb21791 100644 --- a/core/lib/zksync_core/src/consensus/tests.rs +++ b/core/lib/zksync_core/src/consensus/tests.rs @@ -1,17 +1,18 @@ use anyhow::Context as _; -use rand::Rng; use test_casing::test_casing; use tracing::Instrument as _; use zksync_concurrency::{ctx, scope}; use zksync_consensus_executor as executor; use zksync_consensus_network as network; use zksync_consensus_network::testonly::{new_configs, new_fullnode}; -use zksync_consensus_roles::validator::testonly::Setup; +use zksync_consensus_roles::validator::testonly::{Setup, SetupSpec}; use zksync_types::{L1BatchNumber, L2BlockNumber}; use super::*; use crate::utils::testonly::Snapshot; +const CHAIN_ID: validator::ChainId = validator::ChainId(1337); + async fn new_store(from_snapshot: bool) -> Store { match from_snapshot { true => { @@ -36,16 +37,14 @@ async fn test_validator_block_store() { s.spawn_bg(runner.run(ctx)); sk.push_random_blocks(rng, 10).await; store.wait_for_payload(ctx, sk.last_block()).await?; - let fork = validator::Fork { - number: validator::ForkNumber(rng.gen()), - first_block: validator::BlockNumber(4), - }; - let mut setup = Setup::new_with_fork(rng, 3, fork.clone()); + let mut setup = SetupSpec::new(rng, 3); + setup.first_block = validator::BlockNumber(4); + let mut setup = Setup::from(setup); let mut conn = store.access(ctx).await.wrap("access()")?; conn.try_update_genesis(ctx, &setup.genesis) .await .wrap("try_update_genesis()")?; - for i in fork.first_block.0..sk.last_block().next().0 { + for i in setup.genesis.first_block.0..sk.last_block().next().0 { let i = validator::BlockNumber(i); let payload = conn .payload(ctx, i) @@ -128,6 +127,7 @@ async fn test_validator(from_snapshot: bool) { let cfg = MainNodeConfig { executor: executor_config(&cfgs[0]), validator_key: setup.keys[0].clone(), + chain_id: CHAIN_ID, }; s.spawn_bg(cfg.run(ctx, store.clone())); @@ -181,6 +181,7 @@ async fn test_nodes_from_various_snapshots() { let cfg = MainNodeConfig { executor: executor_config(&validator_cfg), validator_key: setup.keys[0].clone(), + chain_id: CHAIN_ID, }; s.spawn_bg(cfg.run(ctx, validator_store.clone())); @@ -272,21 +273,24 @@ async fn test_full_nodes(from_snapshot: bool) { .await .context("validator") }); - // Generate a couple of blocks, before initializing consensus genesis. + tracing::info!("Generate a couple of blocks, before initializing consensus genesis."); validator.push_random_blocks(rng, 5).await; + // API server needs at least 1 L1 batch to start. + validator.seal_batch().await; validator_store .wait_for_payload(ctx, validator.last_block()) .await .unwrap(); - // Run validator. + tracing::info!("Run validator."); let cfg = MainNodeConfig { executor: executor_config(&validator_cfgs[0]), validator_key: setup.keys[0].clone(), + chain_id: CHAIN_ID, }; s.spawn_bg(cfg.run(ctx, validator_store.clone())); - // Run nodes. + tracing::info!("Run nodes."); let mut node_stores = vec![]; for (i, cfg) in node_cfgs.iter().enumerate() { let i = ctx::NoCopy(i); @@ -308,7 +312,7 @@ async fn test_full_nodes(from_snapshot: bool) { )); } - // Make validator produce blocks and wait for fetchers to get them. + tracing::info!("Make validator produce blocks and wait for fetchers to get them."); // Note that block from before and after genesis have to be fetched. validator.push_random_blocks(rng, 5).await; let want_last = validator.last_block(); @@ -350,6 +354,7 @@ async fn test_p2p_fetcher_backfill_certs(from_snapshot: bool) { MainNodeConfig { executor: executor_config(&validator_cfg), validator_key: setup.keys[0].clone(), + chain_id: CHAIN_ID, } .run(ctx, validator_store.clone()), ); diff --git a/core/lib/zksync_core/src/lib.rs b/core/lib/zksync_core/src/lib.rs index b341cfd95b2f..a4bc519e9ef6 100644 --- a/core/lib/zksync_core/src/lib.rs +++ b/core/lib/zksync_core/src/lib.rs @@ -558,6 +558,7 @@ pub async fn initialize_components( .as_ref() .context("consensus component's config is missing")?, secrets, + l2_chain_id, )?; let started_at = Instant::now(); tracing::info!("initializing Consensus"); diff --git a/core/node/node_framework/examples/main_node.rs b/core/node/node_framework/examples/main_node.rs index 8160fcb6012c..8498c24c8a54 100644 --- a/core/node/node_framework/examples/main_node.rs +++ b/core/node/node_framework/examples/main_node.rs @@ -381,6 +381,7 @@ impl MainNodeBuilder { )) } + let genesis = GenesisConfig::from_env()?; let config = read_consensus_config().context("read_consensus_config()")?; let secrets = read_consensus_secrets().context("read_consensus_secrets()")?; @@ -388,6 +389,7 @@ impl MainNodeBuilder { mode: ConsensusMode::Main, config, secrets, + chain_id: genesis.l2_chain_id, }); Ok(self) diff --git a/core/node/node_framework/src/implementations/layers/consensus.rs b/core/node/node_framework/src/implementations/layers/consensus.rs index 1bc2be1f30d4..599340aa211b 100644 --- a/core/node/node_framework/src/implementations/layers/consensus.rs +++ b/core/node/node_framework/src/implementations/layers/consensus.rs @@ -6,6 +6,7 @@ use zksync_core::{ sync_layer::{ActionQueueSender, SyncState}, }; use zksync_dal::{ConnectionPool, Core}; +use zksync_types::L2ChainId; use zksync_web3_decl::client::BoxedL2Client; use crate::{ @@ -29,6 +30,7 @@ pub struct ConsensusLayer { pub mode: Mode, pub config: Option, pub secrets: Option, + pub chain_id: L2ChainId, } #[async_trait::async_trait] @@ -53,7 +55,8 @@ impl WiringLayer for ConsensusLayer { WiringError::Configuration("Missing private consensus config".to_string()) })?; - let main_node_config = consensus::config::main_node(&config, &secrets)?; + let main_node_config = + consensus::config::main_node(&config, &secrets, self.chain_id)?; let task = MainNodeConsensusTask { config: main_node_config, diff --git a/etc/env/consensus_config.yaml b/etc/env/consensus_config.yaml index ebb0986e4049..2b1b543b34f5 100644 --- a/etc/env/consensus_config.yaml +++ b/etc/env/consensus_config.yaml @@ -1,8 +1,4 @@ server_addr: '127.0.0.1:3054' public_addr: '127.0.0.1:3054' -validators: -- 'validator:public:bn254:8b0ff0ad1a250e64b0209277148ccee3b64534d8fa60cf25ba0bcc8b65d4d89309cdae79197c2db873d351401093fa0542a5a2071c1a247f2e1abe56d08cbabb' max_payload_size: 2500000 -gossip_static_inbound: -- 'node:public:ed25519:147bb71be895846e1d6f5b1c6a8be53848b82bdafcf66e9dfe6ca65581076a1d' -gossip_dynamic_inbound_limit: 0 +gossip_dynamic_inbound_limit: 1 diff --git a/etc/env/consensus_secrets.yaml b/etc/env/consensus_secrets.yaml index aa5ca1ef2f4d..fdceef5e8e47 100644 --- a/etc/env/consensus_secrets.yaml +++ b/etc/env/consensus_secrets.yaml @@ -1,2 +1,4 @@ -validator_key: 'validator:secret:bn254:038ec13f4dca210c9d3525204422f0584e1653a5684bff47f98316d9e64b6746' -node_key: 'node:secret:ed25519:b6666c3be2703e15028bbebd220d2678fde7431038641f36c52f02849595a8ab' +# 'validator:public:bls12_381:b14e3126668ae79e689a2d65c56522889a3812ef5433097c33bd7af601b073dcdddf46e188883aa381725c49e08f90c705df1f78bf918e1978912cebeadff0d0084b1a4fe2ddee243e826348045f528803207f5de303c6a95bc1a701a190dbcf' +validator_key: 'validator:secret:bls12_381:3cf20d771450fcd0cbb3839b21cab41161af1554e35d8407a53b0a5d98ff04d4' +# 'node:public:ed25519:a9995979f228c91e4f387f7e141a9afe409196ee0c4fca0045c1c6b6e7892cb5' +node_key: 'node:secret:ed25519:9a40791b5a6b1627fc538b1ddecfa843bd7c4cd01fc0a4d0da186f9d3e740d7c' diff --git a/etc/env/en_consensus_config.yaml b/etc/env/en_consensus_config.yaml index 27e6b80ca9d6..f759e72e891c 100644 --- a/etc/env/en_consensus_config.yaml +++ b/etc/env/en_consensus_config.yaml @@ -1,9 +1,7 @@ server_addr: '127.0.0.1:3055' public_addr: '127.0.0.1:3055' -validators: -- 'validator:public:bn254:8b0ff0ad1a250e64b0209277148ccee3b64534d8fa60cf25ba0bcc8b65d4d89309cdae79197c2db873d351401093fa0542a5a2071c1a247f2e1abe56d08cbabb' max_payload_size: 2500000 gossip_dynamic_inbound_limit: 0 gossip_static_outbound: -- key: 'node:public:ed25519:ee717abba6aec5baae5e09d457bd2ffc2f121b576cf4170ce15a68163ce4c868' +- key: 'node:public:ed25519:a9995979f228c91e4f387f7e141a9afe409196ee0c4fca0045c1c6b6e7892cb5' addr: '127.0.0.1:3054' diff --git a/etc/env/en_consensus_secrets.yaml b/etc/env/en_consensus_secrets.yaml index b54fb6cee695..3f3407a7035b 100644 --- a/etc/env/en_consensus_secrets.yaml +++ b/etc/env/en_consensus_secrets.yaml @@ -1 +1,2 @@ -node_key: 'node:secret:ed25519:d56de77c738326c305c64c25bffe1cc94ea7c639cf71ca3ff94229df27f167ac' +# 'node:public:ed25519:2621c2ae111901d4a9b46e96e64f71282b9209fc6b5e4df3d4208d3de28a482d' +node_key: 'node:secret:ed25519:19bc1ddd9fd2921d1b919e7dcfa465babdcf61a60a21e5df9b3f105bd9cfcb2c' diff --git a/prover/Cargo.lock b/prover/Cargo.lock index 41f4a685d0e8..71722be14408 100644 --- a/prover/Cargo.lock +++ b/prover/Cargo.lock @@ -7521,7 +7521,7 @@ dependencies = [ [[package]] name = "zksync_concurrency" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "once_cell", @@ -7551,29 +7551,33 @@ dependencies = [ [[package]] name = "zksync_consensus_crypto" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "blst", "ed25519-dalek", "ff_ce", "hex", + "num-bigint 0.4.4", + "num-traits", "pairing_ce 0.28.5 (git+https://github.com/matter-labs/pairing.git?rev=d24f2c5871089c4cd4f54c0ca266bb9fef6115eb)", "rand 0.4.6", "rand 0.8.5", "sha3 0.10.8", "thiserror", "tracing", + "zeroize", ] [[package]] name = "zksync_consensus_roles" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "bit-vec", "hex", + "num-bigint 0.4.4", "prost 0.12.3", "rand 0.8.5", "serde", @@ -7589,7 +7593,7 @@ dependencies = [ [[package]] name = "zksync_consensus_storage" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "async-trait", @@ -7607,7 +7611,7 @@ dependencies = [ [[package]] name = "zksync_consensus_utils" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "rand 0.8.5", "thiserror", @@ -7796,7 +7800,7 @@ dependencies = [ [[package]] name = "zksync_protobuf" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "bit-vec", @@ -7816,7 +7820,7 @@ dependencies = [ [[package]] name = "zksync_protobuf_build" version = "0.1.0" -source = "git+https://github.com/matter-labs/era-consensus.git?rev=1dcda70c1c25d0e4db6781ba8d2645d7e8966d49#1dcda70c1c25d0e4db6781ba8d2645d7e8966d49" +source = "git+https://github.com/matter-labs/era-consensus.git?rev=92ecb2d5d65e3bc4a883dacd18d0640e86576c8c#92ecb2d5d65e3bc4a883dacd18d0640e86576c8c" dependencies = [ "anyhow", "heck 0.5.0",