Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Print an error if we discover our own network identity (#6047)
Browse files Browse the repository at this point in the history
* Add an error if we discover our own network identity

* Fix tests
  • Loading branch information
tomaka authored May 18, 2020
1 parent c17fce2 commit adb5acb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {
/// Create a new instance.
pub fn new(
config: ProtocolConfig,
local_peer_id: PeerId,
chain: Arc<dyn Client<B>>,
transaction_pool: Arc<dyn TransactionPool<H, B>>,
finality_proof_provider: Option<Arc<dyn FinalityProofProvider<B>>>,
Expand Down Expand Up @@ -396,7 +397,13 @@ impl<B: BlockT, H: ExHashT> Protocol<B, H> {

let (peerset, peerset_handle) = sc_peerset::Peerset::from_config(peerset_config);
let versions = &((MIN_VERSION as u8)..=(CURRENT_VERSION as u8)).collect::<Vec<u8>>();
let mut behaviour = GenericProto::new(protocol_id.clone(), versions, peerset, queue_size_report);
let mut behaviour = GenericProto::new(
local_peer_id,
protocol_id.clone(),
versions,
peerset,
queue_size_report
);

let mut legacy_equiv_by_name = HashMap::new();

Expand Down Expand Up @@ -2193,6 +2200,7 @@ mod tests {

let (mut protocol, _) = Protocol::<Block, Hash>::new(
ProtocolConfig::default(),
PeerId::random(),
client.clone(),
Arc::new(EmptyTransactionPool),
None,
Expand Down
18 changes: 16 additions & 2 deletions client/network/src/protocol/generic_proto/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ use wasm_timer::Instant;
/// tries to connect, the connection is accepted. A ban only delays dialing attempts.
///
pub struct GenericProto {
/// `PeerId` of the local node.
local_peer_id: PeerId,

/// Legacy protocol to open with peers. Never modified.
legacy_protocol: RegisteredProtocol,

Expand Down Expand Up @@ -321,6 +324,7 @@ impl GenericProto {
/// The `queue_size_report` is an optional Prometheus metric that can report the size of the
/// messages queue. If passed, it must have one label for the protocol name.
pub fn new(
local_peer_id: PeerId,
protocol: impl Into<ProtocolId>,
versions: &[u8],
peerset: sc_peerset::Peerset,
Expand All @@ -329,6 +333,7 @@ impl GenericProto {
let legacy_protocol = RegisteredProtocol::new(protocol, versions);

GenericProto {
local_peer_id,
legacy_protocol,
notif_protocols: Vec::new(),
peerset,
Expand Down Expand Up @@ -507,9 +512,18 @@ impl GenericProto {
///
/// Can be called multiple times with the same `PeerId`s.
pub fn add_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) {
self.peerset.discovered(peer_ids.map(|peer_id| {
let local_peer_id = &self.local_peer_id;
self.peerset.discovered(peer_ids.filter_map(|peer_id| {
if peer_id == *local_peer_id {
error!(
target: "sub-libp2p",
"Discovered our own identity. This is a minor inconsequential bug."
);
return None;
}

debug!(target: "sub-libp2p", "PSM <= Discovered({:?})", peer_id);
peer_id
Some(peer_id)
}));
}

Expand Down
3 changes: 2 additions & 1 deletion client/network/src/protocol/generic_proto/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {

for index in 0 .. 2 {
let keypair = keypairs[index].clone();
let local_peer_id = keypair.public().into_peer_id();
let transport = libp2p::core::transport::MemoryTransport
.and_then(move |out, endpoint| {
let secio = libp2p::secio::SecioConfig::new(keypair);
Expand Down Expand Up @@ -82,7 +83,7 @@ fn build_nodes() -> (Swarm<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
});

let behaviour = CustomProtoWithAddr {
inner: GenericProto::new(&b"test"[..], &[1], peerset, None),
inner: GenericProto::new(local_peer_id, &b"test"[..], &[1], peerset, None),
addrs: addrs
.iter()
.enumerate()
Expand Down
1 change: 1 addition & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
roles: From::from(&params.role),
max_parallel_downloads: params.network_config.max_parallel_downloads,
},
local_peer_id.clone(),
params.chain.clone(),
params.transaction_pool,
params.finality_proof_provider.clone(),
Expand Down

0 comments on commit adb5acb

Please sign in to comment.