Skip to content

Commit

Permalink
fix(iroha_config): broken trusted peers check (#5121)
Browse files Browse the repository at this point in the history
Signed-off-by: 0x009922 <[email protected]>
  • Loading branch information
0x009922 authored Oct 8, 2024
1 parent 2ecd1cc commit 0059c39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/iroha_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ stderrlog = "0.6.0"
[dev-dependencies]
expect-test = { workspace = true }
assertables = { workspace = true }
iroha_crypto = { workspace = true, features = ["rand"] }
45 changes: 42 additions & 3 deletions crates/iroha_config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,10 @@ impl TrustedPeers {
pub fn into_non_empty_vec(self) -> UniqueVec<PeerId> {
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()
}
}

Expand Down Expand Up @@ -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());
}
}
8 changes: 7 additions & 1 deletion crates/irohad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down

0 comments on commit 0059c39

Please sign in to comment.