mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 00:01:09 +00:00
client/authority-discovery: Append PeerId to Multiaddr at most once (#6933)
* client/authority-discovery/worker: Extract address getter * client/authority-discovery: Test for no duplicate p2p components * client/authority-discovery: Append PeerId to Multiaddr at most once When collecting the addresses to be published for the local node, `addresses_to_publish` adds the local nodes `PeerId` to each `Multiaddr`. Before doing so, ensure the `Multiaddr` does not already contain one. * client/authority-discovery: Remove explicit return
This commit is contained in:
@@ -168,6 +168,7 @@ sp_api::mock_impl_runtime_apis! {
|
||||
|
||||
pub struct TestNetwork {
|
||||
peer_id: PeerId,
|
||||
external_addresses: Vec<Multiaddr>,
|
||||
// Whenever functions on `TestNetwork` are called, the function arguments are added to the
|
||||
// vectors below.
|
||||
pub put_value_call: Arc<Mutex<Vec<(kad::record::Key, Vec<u8>)>>>,
|
||||
@@ -179,6 +180,10 @@ impl Default for TestNetwork {
|
||||
fn default() -> Self {
|
||||
TestNetwork {
|
||||
peer_id: PeerId::random(),
|
||||
external_addresses: vec![
|
||||
"/ip6/2001:db8::/tcp/30333"
|
||||
.parse().unwrap(),
|
||||
],
|
||||
put_value_call: Default::default(),
|
||||
get_value_call: Default::default(),
|
||||
set_priority_group_call: Default::default(),
|
||||
@@ -212,7 +217,7 @@ impl NetworkStateInfo for TestNetwork {
|
||||
}
|
||||
|
||||
fn external_addresses(&self) -> Vec<Multiaddr> {
|
||||
vec!["/ip6/2001:db8::/tcp/30333".parse().unwrap()]
|
||||
self.external_addresses.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -691,3 +696,67 @@ fn do_not_cache_addresses_without_peer_id() {
|
||||
"Expect worker to only cache `Multiaddr`s with `PeerId`s.",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addresses_to_publish_adds_p2p() {
|
||||
let (_dht_event_tx, dht_event_rx) = channel(1000);
|
||||
let network: Arc<TestNetwork> = Arc::new(Default::default());
|
||||
|
||||
assert!(!matches!(
|
||||
network.external_addresses().pop().unwrap().pop().unwrap(),
|
||||
multiaddr::Protocol::P2p(_)
|
||||
));
|
||||
|
||||
let (_to_worker, from_service) = mpsc::channel(0);
|
||||
let worker = Worker::new(
|
||||
from_service,
|
||||
Arc::new(TestApi {
|
||||
authorities: vec![],
|
||||
}),
|
||||
network.clone(),
|
||||
vec![],
|
||||
dht_event_rx.boxed(),
|
||||
Role::Authority(KeyStore::new()),
|
||||
Some(prometheus_endpoint::Registry::new()),
|
||||
);
|
||||
|
||||
assert!(
|
||||
matches!(
|
||||
worker.addresses_to_publish().next().unwrap().pop().unwrap(),
|
||||
multiaddr::Protocol::P2p(_)
|
||||
),
|
||||
"Expect `addresses_to_publish` to append `p2p` protocol component.",
|
||||
);
|
||||
}
|
||||
|
||||
/// Ensure [`Worker::addresses_to_publish`] does not add an additional `p2p` protocol component in
|
||||
/// case one already exists.
|
||||
#[test]
|
||||
fn addresses_to_publish_respects_existing_p2p_protocol() {
|
||||
let (_dht_event_tx, dht_event_rx) = channel(1000);
|
||||
let network: Arc<TestNetwork> = Arc::new(TestNetwork {
|
||||
external_addresses: vec![
|
||||
"/ip6/2001:db8::/tcp/30333/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"
|
||||
.parse().unwrap(),
|
||||
],
|
||||
.. Default::default()
|
||||
});
|
||||
|
||||
let (_to_worker, from_service) = mpsc::channel(0);
|
||||
let worker = Worker::new(
|
||||
from_service,
|
||||
Arc::new(TestApi {
|
||||
authorities: vec![],
|
||||
}),
|
||||
network.clone(),
|
||||
vec![],
|
||||
dht_event_rx.boxed(),
|
||||
Role::Authority(KeyStore::new()),
|
||||
Some(prometheus_endpoint::Registry::new()),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
network.external_addresses, worker.addresses_to_publish().collect::<Vec<_>>(),
|
||||
"Expected Multiaddr from `TestNetwork` to not be altered.",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user