mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-04-25 11:02:12 +00:00
Use a random sequence number in floodsub (#634)
* Use a random sequence number in floodsub * Comment
This commit is contained in:
parent
2e549884ef
commit
ded89b4be3
@ -12,6 +12,7 @@ fnv = "1.0"
|
|||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
libp2p-core = { path = "../../core" }
|
libp2p-core = { path = "../../core" }
|
||||||
protobuf = "2.0.2"
|
protobuf = "2.0.2"
|
||||||
|
rand = "0.5"
|
||||||
smallvec = "0.6.5"
|
smallvec = "0.6.5"
|
||||||
tokio-codec = "0.1"
|
tokio-codec = "0.1"
|
||||||
tokio-io = "0.1"
|
tokio-io = "0.1"
|
||||||
|
@ -24,6 +24,7 @@ use handler::FloodsubHandler;
|
|||||||
use libp2p_core::nodes::{ConnectedPoint, NetworkBehavior, NetworkBehaviorAction};
|
use libp2p_core::nodes::{ConnectedPoint, NetworkBehavior, NetworkBehaviorAction};
|
||||||
use libp2p_core::{nodes::protocols_handler::ProtocolsHandler, PeerId};
|
use libp2p_core::{nodes::protocols_handler::ProtocolsHandler, PeerId};
|
||||||
use protocol::{FloodsubMessage, FloodsubRpc, FloodsubSubscription, FloodsubSubscriptionAction};
|
use protocol::{FloodsubMessage, FloodsubRpc, FloodsubSubscription, FloodsubSubscriptionAction};
|
||||||
|
use rand;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::{collections::VecDeque, iter, marker::PhantomData};
|
use std::{collections::VecDeque, iter, marker::PhantomData};
|
||||||
use std::collections::hash_map::{DefaultHasher, HashMap};
|
use std::collections::hash_map::{DefaultHasher, HashMap};
|
||||||
@ -48,9 +49,6 @@ pub struct FloodsubBehaviour<TSubstream> {
|
|||||||
// erroneously.
|
// erroneously.
|
||||||
subscribed_topics: SmallVec<[Topic; 16]>,
|
subscribed_topics: SmallVec<[Topic; 16]>,
|
||||||
|
|
||||||
// Sequence number for the messages we send.
|
|
||||||
seq_no: usize,
|
|
||||||
|
|
||||||
// We keep track of the messages we received (in the format `hash(source ID, seq_no)`) so that
|
// We keep track of the messages we received (in the format `hash(source ID, seq_no)`) so that
|
||||||
// we don't dispatch the same message twice if we receive it twice on the network.
|
// we don't dispatch the same message twice if we receive it twice on the network.
|
||||||
received: CuckooFilter<DefaultHasher>,
|
received: CuckooFilter<DefaultHasher>,
|
||||||
@ -67,7 +65,6 @@ impl<TSubstream> FloodsubBehaviour<TSubstream> {
|
|||||||
local_peer_id,
|
local_peer_id,
|
||||||
connected_peers: HashMap::new(),
|
connected_peers: HashMap::new(),
|
||||||
subscribed_topics: SmallVec::new(),
|
subscribed_topics: SmallVec::new(),
|
||||||
seq_no: 0,
|
|
||||||
received: CuckooFilter::new(),
|
received: CuckooFilter::new(),
|
||||||
marker: PhantomData,
|
marker: PhantomData,
|
||||||
}
|
}
|
||||||
@ -144,7 +141,10 @@ impl<TSubstream> FloodsubBehaviour<TSubstream> {
|
|||||||
let message = FloodsubMessage {
|
let message = FloodsubMessage {
|
||||||
source: self.local_peer_id.clone(),
|
source: self.local_peer_id.clone(),
|
||||||
data: data.into(),
|
data: data.into(),
|
||||||
sequence_number: self.next_sequence_number(),
|
// If the sequence numbers are predictable, then an attacker could flood the network
|
||||||
|
// with packets with the predetermined sequence numbers and absorb our legitimate
|
||||||
|
// messages. We therefore use a random number.
|
||||||
|
sequence_number: rand::random::<[u8; 20]>().to_vec(),
|
||||||
topics: topic.into_iter().map(|t| t.into().clone()).collect(),
|
topics: topic.into_iter().map(|t| t.into().clone()).collect(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -170,13 +170,6 @@ impl<TSubstream> FloodsubBehaviour<TSubstream> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds a unique sequence number to put in a `FloodsubMessage`.
|
|
||||||
fn next_sequence_number(&mut self) -> Vec<u8> {
|
|
||||||
let data = self.seq_no.to_string();
|
|
||||||
self.seq_no += 1;
|
|
||||||
data.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TSubstream> NetworkBehavior for FloodsubBehaviour<TSubstream>
|
impl<TSubstream> NetworkBehavior for FloodsubBehaviour<TSubstream>
|
||||||
|
@ -25,6 +25,7 @@ extern crate fnv;
|
|||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate libp2p_core;
|
extern crate libp2p_core;
|
||||||
extern crate protobuf;
|
extern crate protobuf;
|
||||||
|
extern crate rand;
|
||||||
extern crate smallvec;
|
extern crate smallvec;
|
||||||
extern crate tokio_codec;
|
extern crate tokio_codec;
|
||||||
extern crate tokio_io;
|
extern crate tokio_io;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user