Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroha_config): broken trusted peers check #5121

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] }
s8sato marked this conversation as resolved.
Show resolved Hide resolved
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
Loading