From 0059c392a7cf6782abf0262bfbfafe218265af46 Mon Sep 17 00:00:00 2001 From: 0x009922 <43530070+0x009922@users.noreply.github.com> Date: Tue, 8 Oct 2024 05:12:30 +0000 Subject: [PATCH] fix(iroha_config): broken trusted peers check (#5121) Signed-off-by: 0x009922 <43530070+0x009922@users.noreply.github.com> --- crates/iroha_config/Cargo.toml | 1 + crates/iroha_config/src/parameters/actual.rs | 45 ++++++++++++++++++-- crates/irohad/src/lib.rs | 8 +++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/iroha_config/Cargo.toml b/crates/iroha_config/Cargo.toml index 0d3644e4839..68b18e6b4fd 100644 --- a/crates/iroha_config/Cargo.toml +++ b/crates/iroha_config/Cargo.toml @@ -40,3 +40,4 @@ stderrlog = "0.6.0" [dev-dependencies] expect-test = { workspace = true } assertables = { workspace = true } +iroha_crypto = { workspace = true, features = ["rand"] } diff --git a/crates/iroha_config/src/parameters/actual.rs b/crates/iroha_config/src/parameters/actual.rs index c8e27fdce6d..bf80379e84a 100644 --- a/crates/iroha_config/src/parameters/actual.rs +++ b/crates/iroha_config/src/parameters/actual.rs @@ -135,12 +135,10 @@ impl TrustedPeers { pub fn into_non_empty_vec(self) -> UniqueVec { std::iter::once(self.myself).chain(self.others).collect() } -} -impl Sumeragi { /// Tells whether a trusted peers list has some other peers except for the peer itself pub fn contains_other_trusted_peers(&self) -> bool { - self.trusted_peers.value().others.len() > 1 + !self.others.is_empty() } } @@ -192,3 +190,44 @@ pub struct Telemetry { pub min_retry_period: Duration, pub max_retry_delay_exponent: u8, } + +#[cfg(test)] +mod tests { + use iroha_primitives::{addr::socket_addr, unique_vec}; + + use super::*; + + fn dummy_id(port: u16) -> PeerId { + PeerId::new( + socket_addr!(127.0.0.1:port), + KeyPair::random().into_parts().0, + ) + } + + #[test] + fn no_trusted_peers() { + let value = TrustedPeers { + myself: dummy_id(80), + others: unique_vec![], + }; + assert!(!value.contains_other_trusted_peers()); + } + + #[test] + fn one_trusted_peer() { + let value = TrustedPeers { + myself: dummy_id(80), + others: unique_vec![dummy_id(81)], + }; + assert!(value.contains_other_trusted_peers()); + } + + #[test] + fn many_trusted_peers() { + let value = TrustedPeers { + myself: dummy_id(80), + others: unique_vec![dummy_id(1), dummy_id(2), dummy_id(3), dummy_id(4),], + }; + assert!(value.contains_other_trusted_peers()); + } +} diff --git a/crates/irohad/src/lib.rs b/crates/irohad/src/lib.rs index bf73d25b466..9563e94970a 100644 --- a/crates/irohad/src/lib.rs +++ b/crates/irohad/src/lib.rs @@ -535,7 +535,13 @@ fn validate_config(config: &Config) -> Result<(), ConfigError> { // maybe validate only if snapshot mode is enabled validate_directory_path(&mut emitter, &config.snapshot.store_dir); - if config.genesis.file.is_none() && !config.sumeragi.contains_other_trusted_peers() { + if config.genesis.file.is_none() + && !config + .sumeragi + .trusted_peers + .value() + .contains_other_trusted_peers() + { emitter.emit(Report::new(ConfigError::LonePeer).attach_printable("\ Reason: the network consists from this one peer only (no `sumeragi.trusted_peers` provided).\n\ Since `genesis.file` is not set, there is no way to receive the genesis block.\n\