Skip to content

Commit

Permalink
fix: handle multiple peer_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Dec 6, 2024
1 parent c04fe62 commit 817ebc7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
25 changes: 19 additions & 6 deletions api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

mod event;

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use std::{future::Future, ops::Range};
use std::{marker::PhantomData, ops::RangeBounds};
Expand Down Expand Up @@ -1298,15 +1298,28 @@ where
}
}
}
if !addrs.iter().any(|addr| {
!addr
.iter()
.any(|protocol| matches!(protocol, Protocol::P2p(_)))
}) {
let peer_ids: HashSet<_> = addrs
.iter()
.flat_map(|addr| {
addr.iter().filter_map(|protocol| {
if let Protocol::P2p(peer_id) = protocol {
Some(peer_id)
} else {
None
}
})
})
.collect();
if peer_ids.is_empty() {
return Ok(PeersPostResponse::BadRequest(BadRequestResponse::new(
"at least one address must contain a peer id".to_string(),
)));
};
if peer_ids.len() > 1 {
return Ok(PeersPostResponse::BadRequest(BadRequestResponse::new(
"more than one unique peer id found in the addresses".to_string(),
)));
};
self.peer_connect(addrs)
.await
.or_else(|err| Ok(PeersPostResponse::InternalServerError(err)))
Expand Down
61 changes: 61 additions & 0 deletions api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,67 @@ async fn peer_connect() {
"#]]
.assert_debug_eq(&result);
}
#[test(tokio::test)]
async fn peer_connect_no_peer_id() {
let node_id = NodeKey::random().id();
let network = Network::InMemory;
let addresses = vec!["/ip4/127.0.0.1/tcp/4101", "/ip4/127.0.0.1/udp/4111/quic-v1"];
let server = create_test_server(
node_id,
network,
MockAccessInterestStoreTest::new(),
Arc::new(MockEventStoreTest::new()),
MockP2PService::new(),
None,
);
let result = server
.peers_post(
&addresses.into_iter().map(ToOwned::to_owned).collect(),
&Context,
)
.await
.unwrap();
expect![[r#"
BadRequest(
BadRequestResponse {
message: "at least one address must contain a peer id",
},
)
"#]]
.assert_debug_eq(&result);
}
#[test(tokio::test)]
async fn peer_connect_conflicting_peer_ids() {
let node_id = NodeKey::random().id();
let network = Network::InMemory;
let addresses = vec![
"/ip4/127.0.0.1/tcp/4101/p2p/12D3KooWPFGbRHWfDaWt5MFFeqAHBBq3v5BqeJ4X7pmn2V1t6uNs",
"/ip4/127.0.0.1/udp/4111/quic-v1/p2p/12D3KooWFpSKYLQ6bKLnjrXN5CkGyDsQzAron9UvtHq9yLEKETVQ",
];
let server = create_test_server(
node_id,
network,
MockAccessInterestStoreTest::new(),
Arc::new(MockEventStoreTest::new()),
MockP2PService::new(),
None,
);
let result = server
.peers_post(
&addresses.into_iter().map(ToOwned::to_owned).collect(),
&Context,
)
.await
.unwrap();
expect![[r#"
BadRequest(
BadRequestResponse {
message: "more than one unique peer id found in the addresses",
},
)
"#]]
.assert_debug_eq(&result);
}

#[test(tokio::test)]
async fn stream_state() {
Expand Down

0 comments on commit 817ebc7

Please sign in to comment.