From 835b21ca62a9677d740c2d6d090e4950bb5e074c Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli <> Date: Thu, 9 Feb 2023 15:42:41 +0100 Subject: [PATCH 1/4] ledger: check if pk is valid validator in pre-genesis setup --- apps/src/lib/client/utils.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/apps/src/lib/client/utils.rs b/apps/src/lib/client/utils.rs index 21fed46c11..c6e4a316df 100644 --- a/apps/src/lib/client/utils.rs +++ b/apps/src/lib/client/utils.rs @@ -21,12 +21,12 @@ use serde_json::json; use sha2::{Digest, Sha256}; use crate::cli::context::ENV_VAR_WASM_DIR; -use crate::cli::{self, args}; +use crate::cli::{self, args, safe_exit}; use crate::config::genesis::genesis_config::{ self, HexString, ValidatorPreGenesisConfig, }; use crate::config::global::GlobalConfig; -use crate::config::{self, Config, TendermintMode}; +use crate::config::{self, genesis, Config, TendermintMode}; use crate::facade::tendermint::node::Id as TendermintNodeId; use crate::facade::tendermint_config::net::Address as TendermintAddress; use crate::node::ledger::tendermint_node; @@ -257,13 +257,28 @@ pub async fn join_network( ); cli::safe_exit(1) }); - let genesis_file_path = base_dir.join(format!("{}.toml", chain_id.as_str())); - let mut wallet = Wallet::load_or_new_from_genesis( - &chain_dir, - genesis_config::open_genesis_config(genesis_file_path).unwrap(), - ); + let genesis = + genesis_config::open_genesis_config(genesis_file_path).unwrap(); + + let tendermint_node_pk = tendermint_node_key.ref_to(); + if !genesis.validator.iter().any(|(alias, config)| { + if let Some(tm_node_key) = &config.tendermint_node_key { + tm_node_key.0.eq(&tendermint_node_pk.to_string()) + } else { + false + } + }) { + println!( + "The pre-genesis config doesn't match any validator for {} \ + chain.", + chain_id.as_str() + ); + safe_exit(1) + }; + + let mut wallet = Wallet::load_or_new_from_genesis(&chain_dir, genesis); let address = wallet .find_address(&validator_alias) From 2b105f3dbea8e46f35ea4495428650ece579cdc2 Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli <> Date: Thu, 9 Feb 2023 18:18:39 +0100 Subject: [PATCH 2/4] misc: refactor + clippy + fmt --- apps/src/lib/client/utils.rs | 40 ++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/apps/src/lib/client/utils.rs b/apps/src/lib/client/utils.rs index c6e4a316df..982563b1b3 100644 --- a/apps/src/lib/client/utils.rs +++ b/apps/src/lib/client/utils.rs @@ -23,10 +23,10 @@ use sha2::{Digest, Sha256}; use crate::cli::context::ENV_VAR_WASM_DIR; use crate::cli::{self, args, safe_exit}; use crate::config::genesis::genesis_config::{ - self, HexString, ValidatorPreGenesisConfig, + self, GenesisConfig, HexString, ValidatorPreGenesisConfig, }; use crate::config::global::GlobalConfig; -use crate::config::{self, genesis, Config, TendermintMode}; +use crate::config::{self, Config, TendermintMode}; use crate::facade::tendermint::node::Id as TendermintNodeId; use crate::facade::tendermint_config::net::Address as TendermintAddress; use crate::node::ledger::tendermint_node; @@ -257,28 +257,25 @@ pub async fn join_network( ); cli::safe_exit(1) }); + let genesis_file_path = base_dir.join(format!("{}.toml", chain_id.as_str())); - let genesis = + let genesis_config = genesis_config::open_genesis_config(genesis_file_path).unwrap(); - let tendermint_node_pk = tendermint_node_key.ref_to(); - if !genesis.validator.iter().any(|(alias, config)| { - if let Some(tm_node_key) = &config.tendermint_node_key { - tm_node_key.0.eq(&tendermint_node_pk.to_string()) - } else { - false - } - }) { + if !is_valid_validator_for_current_chain( + &tendermint_node_key.ref_to(), + &genesis_config, + ) { println!( - "The pre-genesis config doesn't match any validator for {} \ - chain.", + "The current validator is not valid for chain {}.", chain_id.as_str() ); safe_exit(1) - }; + } - let mut wallet = Wallet::load_or_new_from_genesis(&chain_dir, genesis); + let mut wallet = + Wallet::load_or_new_from_genesis(&chain_dir, genesis_config); let address = wallet .find_address(&validator_alias) @@ -1073,3 +1070,16 @@ pub fn validator_pre_genesis_file(pre_genesis_path: &Path) -> PathBuf { pub fn validator_pre_genesis_dir(base_dir: &Path, alias: &str) -> PathBuf { base_dir.join(PRE_GENESIS_DIR).join(alias) } + +fn is_valid_validator_for_current_chain( + validator_pk: &common::PublicKey, + genesis_config: &GenesisConfig, +) -> bool { + !genesis_config.validator.iter().any(|(_alias, config)| { + if let Some(tm_node_key) = &config.tendermint_node_key { + tm_node_key.0.eq(&validator_pk.to_string()) + } else { + false + } + }) +} From 1c7201ff84db6b504d5cb6e867a6cc946d69b65f Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli <> Date: Fri, 10 Feb 2023 11:10:36 +0100 Subject: [PATCH 3/4] fix: logic --- apps/src/lib/client/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/src/lib/client/utils.rs b/apps/src/lib/client/utils.rs index 982563b1b3..2ab9f7cb12 100644 --- a/apps/src/lib/client/utils.rs +++ b/apps/src/lib/client/utils.rs @@ -1075,7 +1075,7 @@ fn is_valid_validator_for_current_chain( validator_pk: &common::PublicKey, genesis_config: &GenesisConfig, ) -> bool { - !genesis_config.validator.iter().any(|(_alias, config)| { + genesis_config.validator.iter().any(|(_alias, config)| { if let Some(tm_node_key) = &config.tendermint_node_key { tm_node_key.0.eq(&validator_pk.to_string()) } else { From 8ba6651e417466a4103acbb8306524dfdd83e6fd Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli <> Date: Wed, 22 Feb 2023 14:19:09 +0100 Subject: [PATCH 4/4] fix: review comments --- apps/src/lib/client/utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/src/lib/client/utils.rs b/apps/src/lib/client/utils.rs index 2ab9f7cb12..8e774ca572 100644 --- a/apps/src/lib/client/utils.rs +++ b/apps/src/lib/client/utils.rs @@ -267,7 +267,7 @@ pub async fn join_network( &tendermint_node_key.ref_to(), &genesis_config, ) { - println!( + eprintln!( "The current validator is not valid for chain {}.", chain_id.as_str() ); @@ -1072,12 +1072,12 @@ pub fn validator_pre_genesis_dir(base_dir: &Path, alias: &str) -> PathBuf { } fn is_valid_validator_for_current_chain( - validator_pk: &common::PublicKey, + tendermint_node_pk: &common::PublicKey, genesis_config: &GenesisConfig, ) -> bool { genesis_config.validator.iter().any(|(_alias, config)| { if let Some(tm_node_key) = &config.tendermint_node_key { - tm_node_key.0.eq(&validator_pk.to_string()) + tm_node_key.0.eq(&tendermint_node_pk.to_string()) } else { false }