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(gossipsub): signed messages use monotonically increasing seq numbers #3551

Merged
merged 5 commits into from
Mar 14, 2023
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
7 changes: 7 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.44.2 - unreleased
Copy link
Member

Choose a reason for hiding this comment

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

Missing Cargo.toml version bump. Thus a "ghost version". Will be fixed with #3668.


- Signed messages now use sequential integers in the sequence number field.
See [PR 3551].

[PR 3551]: https://github.com/libp2p/rust-libp2p/pull/3551

# 0.44.1

- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312].
Expand Down
16 changes: 11 additions & 5 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod tests;
#[derive(Clone)]
pub enum MessageAuthenticity {
/// Message signing is enabled. The author will be the owner of the key and the sequence number
/// will be a random number.
/// will be linearly increasing.
Signed(Keypair),
/// Message signing is disabled.
///
Expand Down Expand Up @@ -155,6 +155,8 @@ enum PublishConfig {
keypair: Keypair,
author: PeerId,
inline_key: Option<Vec<u8>>,
last_seq_no: u64, // This starts from a random number and increases then overflows (if
// required)
},
Author(PeerId),
RandomAuthor,
Expand Down Expand Up @@ -190,6 +192,7 @@ impl From<MessageAuthenticity> for PublishConfig {
keypair,
author: public_key.to_peer_id(),
inline_key: key,
last_seq_no: rand::random(),
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, I don't think it actually matters: Why start with a random number and not the current timestamp as the go implementation does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure really. I think I have some innate aversion to giving out more information about ourselves than needed. Maybe also because I was lazy about finding out which timestamp to use in go, ms vs s etc.

Happy to change this, no real reason tbh, random just felt slightly safer, with no logical reason to back it up. :)

}
}
MessageAuthenticity::Author(peer_id) => PublishConfig::Author(peer_id),
Expand Down Expand Up @@ -2749,18 +2752,21 @@ where

/// Constructs a [`RawMessage`] performing message signing if required.
pub(crate) fn build_raw_message(
&self,
&mut self,
topic: TopicHash,
data: Vec<u8>,
) -> Result<RawMessage, PublishError> {
match &self.publish_config {
match &mut self.publish_config {
PublishConfig::Signing {
ref keypair,
author,
inline_key,
mut last_seq_no,
} => {
// Build and sign the message
let sequence_number: u64 = rand::random();
// Increment the last sequence number
last_seq_no = last_seq_no.wrapping_add(1);

let sequence_number = last_seq_no;

let signature = {
let message = proto::Message {
Expand Down
5 changes: 3 additions & 2 deletions protocols/gossipsub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
//! - **Sequence Numbers** - A message on the gossipsub network is identified by the source
//! [`libp2p_core::PeerId`] and a nonce (sequence number) of the message. The sequence numbers in
//! this implementation are sent as raw bytes across the wire. They are 64-bit big-endian unsigned
//! integers. They are chosen at random in this implementation of gossipsub, but are sequential in
//! the current go implementation.
//! integers. When messages are signed, they are monotonically increasing integers starting from a
//! random value and wrapping around u64::MAX. When messages are unsigned, they are chosen at random.
//! NOTE: These numbers are sequential in the current go implementation.
//!
//! # Peer Discovery
//!
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ mod tests {

// generate an arbitrary GossipsubMessage using the behaviour signing functionality
let config = Config::default();
let gs: Behaviour =
let mut gs: Behaviour =
Behaviour::new(crate::MessageAuthenticity::Signed(keypair.0), config).unwrap();
let data = (0..g.gen_range(10..10024u32))
.map(|_| u8::arbitrary(g))
Expand Down