Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/fraccaman+grarco/check-pre-genes…
Browse files Browse the repository at this point in the history
…is-pk' (#1140) into maint-0.14

* namada/fraccaman+grarco/check-pre-genesis-pk:
  fix: review comments
  fix: logic
  misc: refactor + clippy + fmt
  ledger: check if pk is valid validator in pre-genesis setup
  • Loading branch information
juped committed Mar 21, 2023
2 parents 27d7aa3 + 8ba6651 commit a8d17bb
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ 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,
self, GenesisConfig, HexString, ValidatorPreGenesisConfig,
};
use crate::config::global::GlobalConfig;
use crate::config::{self, Config, TendermintMode};
Expand Down Expand Up @@ -260,10 +260,22 @@ pub async fn join_network(

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_config =
genesis_config::open_genesis_config(genesis_file_path).unwrap();

if !is_valid_validator_for_current_chain(
&tendermint_node_key.ref_to(),
&genesis_config,
) {
eprintln!(
"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_config);

let address = wallet
.find_address(&validator_alias)
Expand Down Expand Up @@ -1058,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(
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(&tendermint_node_pk.to_string())
} else {
false
}
})
}

0 comments on commit a8d17bb

Please sign in to comment.