Skip to content

docs: Add nonce to chat messages to prevent deduplication of identica… #60

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ async fn main() -> Result<()> {
// broadcast each line we type
println!("> type a message and hit enter to broadcast...");
while let Some(text) = line_rx.recv().await {
let message = Message::Message { text: text.clone() };
let message = Message::Message {
text: text.clone(),
// use a random nonce to ensure the message is unique - the gossip protocol deduplicates
// messages based on the hash of the message, so identical messages are ignored.
nonce: rand::random(),
};
let encoded_message = SignedMessage::sign_and_encode(endpoint.secret_key(), &message)?;
sender.broadcast(encoded_message).await?;
println!("> sent: {text}");
Expand All @@ -182,7 +187,7 @@ async fn subscribe_loop(mut receiver: GossipReceiver) -> Result<()> {
names.insert(from, name.clone());
println!("> {} is now known as {}", from.fmt_short(), name);
}
Message::Message { text } => {
Message::Message { text, nonce: _ } => {
let name = names
.get(&from)
.map_or_else(|| from.fmt_short(), String::to_string);
Expand Down Expand Up @@ -234,10 +239,21 @@ impl SignedMessage {
}
}

/// A chat message.
#[derive(Debug, Serialize, Deserialize)]
enum Message {
AboutMe { name: String },
Message { text: String },
AboutMe {
name: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message probably also needs a nonce, since when a new peer joins, you'd want to send yet another AboutMe message with your current username, but that'd currently fail sending.

Suggested change
name: String,
name: String,
nonce: [u8; 16],

},
/// A chat message.
///
/// `nonce` is used to uniquely identify each message - iroh-gossip deduplicates
/// based on the hash of the message, so we need to ensure that the message is
/// unique, so two identical chat messages are still delivered.
Message {
text: String,
nonce: u32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good! Can you make it a [u8; 16] though, just to make it borderline practically impossible for colliding nonces for the price of just a couple more bytes to send?

Suggested change
nonce: u32,
nonce: [u8; 16],

},
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
Loading