fix(gossipsub): signed messages use monotonically increasing seq numbers

This modifies the gossipsub implementation to use monotonically increasing sequence numbers for signed messages (as dictated by the specification). There is a discussion about this in #3453. This change will make rust-libp2p gossipsub align with the go-implementation when messages are signed.

Messages will however still use randomized sequence numbers when messages are unsigned for security reasons (as discussed in the issue linked).

This shouldn't change any user-level API, only the seqno behavior. It is fully backwards compatible.

Resolves #3453.

Pull-Request: #3551.
This commit is contained in:
Age Manning
2023-03-14 11:06:01 +11:00
committed by GitHub
parent 2a18f7a5f0
commit eb5e269165
4 changed files with 22 additions and 8 deletions

View File

@ -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.
///
@ -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,
@ -190,6 +192,7 @@ impl From<MessageAuthenticity> for PublishConfig {
keypair,
author: public_key.to_peer_id(),
inline_key: key,
last_seq_no: rand::random(),
}
}
MessageAuthenticity::Author(peer_id) => PublishConfig::Author(peer_id),
@ -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 {