From 722476f3b126cc7acb35a5052b050a37f9dd1e36 Mon Sep 17 00:00:00 2001 From: Youngjoon Lee Date: Tue, 31 Oct 2023 19:52:32 +0900 Subject: [PATCH] Stop using msg hash as a libp2p msg ID --- nomos-libp2p/Cargo.toml | 1 - nomos-libp2p/src/lib.rs | 20 ++++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/nomos-libp2p/Cargo.toml b/nomos-libp2p/Cargo.toml index 4f798e7c..31faeff3 100644 --- a/nomos-libp2p/Cargo.toml +++ b/nomos-libp2p/Cargo.toml @@ -18,7 +18,6 @@ libp2p = { version = "0.52.4", features = [ "tokio", "secp256k1", ] } -blake2 = { version = "0.10" } serde = { version = "1.0.166", features = ["derive"] } hex = "0.4.3" log = "0.4.19" diff --git a/nomos-libp2p/src/lib.rs b/nomos-libp2p/src/lib.rs index faf4596e..c9a66d56 100644 --- a/nomos-libp2p/src/lib.rs +++ b/nomos-libp2p/src/lib.rs @@ -9,9 +9,7 @@ use std::time::Duration; pub use config::SwarmConfig; pub use libp2p; -use blake2::digest::{consts::U32, Digest}; -use blake2::Blake2b; -use libp2p::gossipsub::{Message, MessageId, TopicHash}; +use libp2p::gossipsub::{MessageId, TopicHash}; #[allow(deprecated)] pub use libp2p::{ core::upgrade, @@ -65,14 +63,14 @@ impl Swarm { // Wrapping TCP transport into DNS transport to resolve hostnames. let tcp_transport = dns::tokio::Transport::system(tcp_transport)?.boxed(); - // TODO: consider using Signed or Anonymous. - // For Anonymous, a custom `message_id` function need to be set - // to prevent all messages from a peer being filtered as duplicates. let gossipsub = gossipsub::Behaviour::new( gossipsub::MessageAuthenticity::Author(local_peer_id), gossipsub::ConfigBuilder::from(config.gossipsub_config.clone()) - .validation_mode(gossipsub::ValidationMode::None) - .message_id_fn(compute_message_id) + // To use the default `message_id_fn` that uses the message sender info, + // we shouldn't use ValidationMode::None that discards sender info + // from the received message, which causes the message ID to be created wrongly. + // https://github.com/libp2p/rust-libp2p/blob/ec157171a7c1d733dcaff80b00ad9596c15701a1/protocols/gossipsub/src/protocol.rs#L377 + .validation_mode(gossipsub::ValidationMode::Permissive) .build()?, )?; @@ -158,9 +156,3 @@ impl futures::Stream for Swarm { Pin::new(&mut self.swarm).poll_next(cx) } } - -fn compute_message_id(message: &Message) -> MessageId { - let mut hasher = Blake2b::::new(); - hasher.update(&message.data); - MessageId::from(hasher.finalize().to_vec()) -}