From f54c1cf39a391165aa6ba23352bc5153e5bd0bcd Mon Sep 17 00:00:00 2001 From: Danil Date: Tue, 10 Sep 2024 12:04:14 +0200 Subject: [PATCH] set correct key configs Signed-off-by: Danil --- zk_toolbox/Cargo.lock | 1 + zk_toolbox/Cargo.toml | 1 + zk_toolbox/crates/zk_inception/Cargo.toml | 1 + .../zk_inception/src/commands/chain/init.rs | 5 ++- .../commands/external_node/prepare_configs.rs | 32 +++++++++++++++---- .../crates/zk_inception/src/messages.rs | 5 ++- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/zk_toolbox/Cargo.lock b/zk_toolbox/Cargo.lock index cd3fb7e8e00c..d500b5d9e5dd 100644 --- a/zk_toolbox/Cargo.lock +++ b/zk_toolbox/Cargo.lock @@ -6440,6 +6440,7 @@ dependencies = [ "ethers", "human-panic", "lazy_static", + "secrecy", "serde", "serde_json", "serde_yaml", diff --git a/zk_toolbox/Cargo.toml b/zk_toolbox/Cargo.toml index cbcee0701120..16fc76328223 100644 --- a/zk_toolbox/Cargo.toml +++ b/zk_toolbox/Cargo.toml @@ -61,3 +61,4 @@ toml = "0.8.12" url = { version = "2.5.0", features = ["serde"] } xshell = "0.2.6" clap-markdown = "0.1.4" +secrecy = "0.8.0" diff --git a/zk_toolbox/crates/zk_inception/Cargo.toml b/zk_toolbox/crates/zk_inception/Cargo.toml index e3a61c569f35..61983d59e6e9 100644 --- a/zk_toolbox/crates/zk_inception/Cargo.toml +++ b/zk_toolbox/crates/zk_inception/Cargo.toml @@ -36,3 +36,4 @@ zksync_basic_types.workspace = true clap-markdown.workspace = true zksync_consensus_roles.workspace = true zksync_consensus_crypto.workspace = true +secrecy.workspace = true diff --git a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs index b76aa3961e6a..df34fb7436e0 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/chain/init.rs @@ -335,17 +335,16 @@ fn get_genesis_specs(chain_config: &ChainConfig, consensus_keys: &ConsensusKeys) let public_keys = get_consensus_public_keys(consensus_keys); let validator_key = public_keys.validator_key.encode(); let attester_key = public_keys.attester_key.encode(); - let node_key = public_keys.node_key.encode(); let validator = WeightedValidator { - key: ValidatorPublicKey(validator_key), + key: ValidatorPublicKey(validator_key.clone()), weight: 1, }; let attester = WeightedAttester { key: AttesterPublicKey(attester_key), weight: 1, }; - let leader = ValidatorPublicKey(node_key); + let leader = ValidatorPublicKey(validator_key); GenesisSpec { chain_id: chain_config.chain_id, diff --git a/zk_toolbox/crates/zk_inception/src/commands/external_node/prepare_configs.rs b/zk_toolbox/crates/zk_inception/src/commands/external_node/prepare_configs.rs index 3e19e371cf22..aca3336c8819 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/external_node/prepare_configs.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/external_node/prepare_configs.rs @@ -10,13 +10,14 @@ use config::{ external_node::ENConfig, ports_config, set_rocks_db_config, traits::SaveConfigWithBasePath, update_ports, ChainConfig, EcosystemConfig, SecretsConfig, }; +use secrecy::ExposeSecret as _; use xshell::Shell; use zksync_basic_types::url::SensitiveUrl; use zksync_config::configs::{ consensus::{ConsensusConfig, ConsensusSecrets, Host, NodePublicKey, NodeSecretKey, Secret}, DatabaseSecrets, L1Secrets, }; -use zksync_consensus_crypto::TextFmt; +use zksync_consensus_crypto::{Text, TextFmt}; use zksync_consensus_roles as roles; use crate::{ @@ -24,7 +25,9 @@ use crate::{ consts::{GOSSIP_DYNAMIC_INBOUND_LIMIT, MAX_BATCH_SIZE, MAX_PAYLOAD_SIZE}, messages::{ msg_preparing_en_config_is_done, MSG_API_CONFIG_MISSING_ERR, MSG_CHAIN_NOT_INITIALIZED, - MSG_CONSENSUS_CONFIG_MISSING_ERR, MSG_GENESIS_SPEC_MISSING_ERR, MSG_PREPARING_EN_CONFIGS, + MSG_CONSENSUS_CONFIG_MISSING_ERR, MSG_CONSENSUS_SECRETS_MISSING_ERR, + MSG_CONSENSUS_SECRETS_NODE_KEY_MISSING_ERR, MSG_GENESIS_SPEC_MISSING_ERR, + MSG_PREPARING_EN_CONFIGS, }, utils::{ consensus::parse_public_addr, @@ -96,11 +99,15 @@ fn prepare_configs( .context(MSG_API_CONFIG_MISSING_ERR)?; let public_addr = parse_public_addr(&api_config)?; let server_addr = public_addr.parse()?; - let genesis_spec = main_node_consensus_config - .genesis_spec - .context(MSG_GENESIS_SPEC_MISSING_ERR)?; let mut gossip_static_outbound = BTreeMap::new(); - let main_node_public_key = NodePublicKey(genesis_spec.leader.0); + let main_node_public_key = node_public_key( + &config + .get_secrets_config()? + .consensus + .context(MSG_CONSENSUS_SECRETS_MISSING_ERR)?, + )? + .context(MSG_CONSENSUS_SECRETS_NODE_KEY_MISSING_ERR)?; + gossip_static_outbound.insert(main_node_public_key, main_node_consensus_config.public_addr); let en_consensus_config = ConsensusConfig { server_addr, @@ -142,3 +149,16 @@ fn prepare_configs( Ok(()) } + +fn node_public_key(secrets: &ConsensusSecrets) -> anyhow::Result> { + Ok(node_key(secrets)?.map(|node_secret_key| NodePublicKey(node_secret_key.public().encode()))) +} +fn node_key(secrets: &ConsensusSecrets) -> anyhow::Result> { + read_secret_text(secrets.node_key.as_ref().map(|x| &x.0)) +} + +fn read_secret_text(text: Option<&Secret>) -> anyhow::Result> { + text.map(|text| Text::new(text.expose_secret()).decode()) + .transpose() + .map_err(|_| anyhow::format_err!("invalid format")) +} diff --git a/zk_toolbox/crates/zk_inception/src/messages.rs b/zk_toolbox/crates/zk_inception/src/messages.rs index a0ad5f4c7803..f8b401352172 100644 --- a/zk_toolbox/crates/zk_inception/src/messages.rs +++ b/zk_toolbox/crates/zk_inception/src/messages.rs @@ -305,7 +305,10 @@ pub(super) fn msg_preparing_en_config_is_done(path: &Path) -> String { pub(super) const MSG_EXTERNAL_NODE_CONFIG_NOT_INITIALIZED: &str = "External node is not initialized"; -pub(super) const MSG_CONSENSUS_CONFIG_MISSING_ERR: &str = "Consensus config missing"; +pub(super) const MSG_CONSENSUS_CONFIG_MISSING_ERR: &str = "Consensus config is missing"; +pub(super) const MSG_CONSENSUS_SECRETS_MISSING_ERR: &str = "Consensus secrets config is missing"; +pub(super) const MSG_CONSENSUS_SECRETS_NODE_KEY_MISSING_ERR: &str = "Consensus node key is missing"; + pub(super) const MSG_GENESIS_SPEC_MISSING_ERR: &str = "Genesis spec missing"; pub(super) const MSG_PUBLIC_ADDR_ERR: &str = "Public address error";