Revert: Stop using msg hash as a libp2p msg ID

This commit is contained in:
Youngjoon Lee 2023-10-31 19:56:36 +09:00
parent 722476f3b1
commit 7dce530d13
2 changed files with 15 additions and 6 deletions

View File

@ -18,6 +18,7 @@ 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"

View File

@ -9,7 +9,9 @@ use std::time::Duration;
pub use config::SwarmConfig;
pub use libp2p;
use libp2p::gossipsub::{MessageId, TopicHash};
use blake2::digest::{consts::U32, Digest};
use blake2::Blake2b;
use libp2p::gossipsub::{Message, MessageId, TopicHash};
#[allow(deprecated)]
pub use libp2p::{
core::upgrade,
@ -63,14 +65,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())
// 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)
.validation_mode(gossipsub::ValidationMode::None)
.message_id_fn(compute_message_id)
.build()?,
)?;
@ -156,3 +158,9 @@ impl futures::Stream for Swarm {
Pin::new(&mut self.swarm).poll_next(cx)
}
}
fn compute_message_id(message: &Message) -> MessageId {
let mut hasher = Blake2b::<U32>::new();
hasher.update(&message.data);
MessageId::from(hasher.finalize().to_vec())
}