veilid/veilid-core/src/routing_table/find_nodes.rs

629 lines
23 KiB
Rust
Raw Normal View History

2021-11-22 16:28:30 +00:00
use super::*;
use crate::dht::*;
use crate::xx::*;
use crate::*;
2022-08-18 20:21:13 +00:00
pub type LowLevelProtocolPorts = BTreeSet<(LowLevelProtocolType, AddressType, u16)>;
pub type ProtocolToPortMapping = BTreeMap<(ProtocolType, AddressType), (LowLevelProtocolType, u16)>;
#[derive(Clone, Debug)]
2022-10-06 15:40:55 +00:00
pub struct LowLevelPortInfo {
2022-08-18 20:21:13 +00:00
pub low_level_protocol_ports: LowLevelProtocolPorts,
pub protocol_to_port: ProtocolToPortMapping,
}
2021-11-22 16:28:30 +00:00
impl RoutingTable {
2022-08-05 18:48:02 +00:00
// Makes a filter that finds nodes with a matching inbound dialinfo
pub fn make_inbound_dial_info_entry_filter(
2022-09-01 01:41:48 +00:00
routing_domain: RoutingDomain,
2022-08-05 18:48:02 +00:00
dial_info_filter: DialInfoFilter,
2022-10-09 18:59:01 +00:00
) -> impl FnMut(&RoutingTableInner, &BucketEntryInner) -> bool {
2022-08-05 18:48:02 +00:00
// does it have matching public dial info?
2022-10-09 18:59:01 +00:00
move |_rti, e| {
2022-09-01 01:41:48 +00:00
if let Some(ni) = e.node_info(routing_domain) {
if ni
.first_filtered_dial_info_detail(DialInfoDetail::NO_SORT, |did| {
did.matches_filter(&dial_info_filter)
})
2022-09-01 01:41:48 +00:00
.is_some()
{
return true;
2022-08-31 01:21:16 +00:00
}
}
false
2022-08-05 18:48:02 +00:00
}
}
// Makes a filter that finds nodes capable of dialing a particular outbound dialinfo
2022-10-09 18:59:01 +00:00
pub fn make_outbound_dial_info_entry_filter<'s>(
2022-09-01 01:41:48 +00:00
routing_domain: RoutingDomain,
2022-08-05 18:48:02 +00:00
dial_info: DialInfo,
2022-10-09 18:59:01 +00:00
) -> impl FnMut(&RoutingTableInner, &'s BucketEntryInner) -> bool {
2022-08-05 18:48:02 +00:00
// does the node's outbound capabilities match the dialinfo?
2022-10-09 18:59:01 +00:00
move |_rti, e| {
2022-09-01 01:41:48 +00:00
if let Some(ni) = e.node_info(routing_domain) {
let dif = DialInfoFilter::all()
.with_protocol_type_set(ni.outbound_protocols)
.with_address_type_set(ni.address_types);
if dial_info.matches_filter(&dif) {
return true;
2022-08-31 01:21:16 +00:00
}
}
false
2022-08-05 18:48:02 +00:00
}
}
// Make a filter that wraps another filter
2022-10-09 18:59:01 +00:00
pub fn combine_entry_filters<'a, 'b, F, G>(
2022-09-04 18:17:28 +00:00
mut f1: F,
mut f2: G,
2022-10-09 18:59:01 +00:00
) -> impl FnMut(&'a RoutingTableInner, &'b BucketEntryInner) -> bool
2022-08-05 18:48:02 +00:00
where
2022-10-09 18:59:01 +00:00
F: FnMut(&'a RoutingTableInner, &'b BucketEntryInner) -> bool,
G: FnMut(&'a RoutingTableInner, &'b BucketEntryInner) -> bool,
2022-08-05 18:48:02 +00:00
{
2022-10-09 18:59:01 +00:00
move |rti, e| {
if !f1(rti, e) {
2022-08-05 18:48:02 +00:00
return false;
}
2022-10-09 18:59:01 +00:00
if !f2(rti, e) {
2022-08-05 18:48:02 +00:00
return false;
}
true
}
}
// Retrieve the fastest nodes in the routing table matching an entry filter
2022-10-12 19:52:19 +00:00
pub fn find_fast_public_nodes_filtered<'a, 'b, F>(
2022-04-17 17:28:39 +00:00
&self,
2022-06-25 14:57:33 +00:00
node_count: usize,
2022-08-05 18:48:02 +00:00
mut entry_filter: F,
) -> Vec<NodeRef>
where
2022-10-12 19:52:19 +00:00
F: FnMut(&'a RoutingTableInner, &'b BucketEntryInner) -> bool,
2022-08-05 18:48:02 +00:00
{
2021-11-22 16:28:30 +00:00
self.find_fastest_nodes(
2022-06-25 14:57:33 +00:00
// count
node_count,
2021-11-22 16:28:30 +00:00
// filter
2022-10-09 18:59:01 +00:00
|rti, _k: DHTKey, v: Option<Arc<BucketEntry>>| {
2022-06-25 14:57:33 +00:00
let entry = v.unwrap();
2022-10-09 18:59:01 +00:00
entry.with(rti, |rti, e| {
2022-08-31 01:21:16 +00:00
// skip nodes on local network
if e.node_info(RoutingDomain::LocalNetwork).is_some() {
return false;
}
// skip nodes not on public internet
if e.node_info(RoutingDomain::PublicInternet).is_none() {
2022-04-17 17:28:39 +00:00
return false;
}
2022-08-05 18:48:02 +00:00
// skip nodes that dont match entry filter
2022-10-09 18:59:01 +00:00
entry_filter(rti, e)
2022-06-25 14:57:33 +00:00
})
2022-09-04 18:17:28 +00:00
},
2021-11-22 16:28:30 +00:00
// transform
2022-10-09 18:59:01 +00:00
|_rti, k: DHTKey, v: Option<Arc<BucketEntry>>| {
2022-08-05 18:48:02 +00:00
NodeRef::new(self.clone(), k, v.unwrap().clone(), None)
2022-04-19 15:23:44 +00:00
},
2021-11-22 16:28:30 +00:00
)
}
2022-06-25 14:57:33 +00:00
// Retrieve up to N of each type of protocol capable nodes
pub fn find_bootstrap_nodes_filtered(&self, max_per_type: usize) -> Vec<NodeRef> {
let protocol_types = vec![
ProtocolType::UDP,
ProtocolType::TCP,
ProtocolType::WS,
ProtocolType::WSS,
];
let mut nodes_proto_v4 = vec![0usize, 0usize, 0usize, 0usize];
let mut nodes_proto_v6 = vec![0usize, 0usize, 0usize, 0usize];
self.find_fastest_nodes(
// count
protocol_types.len() * 2 * max_per_type,
// filter
2022-10-09 18:59:01 +00:00
move |rti, _k: DHTKey, v: Option<Arc<BucketEntry>>| {
2022-06-25 14:57:33 +00:00
let entry = v.unwrap();
2022-10-09 18:59:01 +00:00
entry.with(rti, |_rti, e| {
2022-06-25 14:57:33 +00:00
// skip nodes on our local network here
2022-09-04 18:17:28 +00:00
if e.has_node_info(RoutingDomain::LocalNetwork.into()) {
2022-06-25 14:57:33 +00:00
return false;
}
// does it have some dial info we need?
2022-09-04 18:17:28 +00:00
let filter = |n: &NodeInfo| {
2022-06-25 14:57:33 +00:00
let mut keep = false;
2022-09-04 19:40:35 +00:00
for did in &n.dial_info_detail_list {
2022-09-03 17:57:25 +00:00
if matches!(did.dial_info.address_type(), AddressType::IPV4) {
for (n, protocol_type) in protocol_types.iter().enumerate() {
if nodes_proto_v4[n] < max_per_type
&& did.dial_info.protocol_type() == *protocol_type
{
nodes_proto_v4[n] += 1;
keep = true;
2022-06-25 14:57:33 +00:00
}
2022-09-03 17:57:25 +00:00
}
} else if matches!(did.dial_info.address_type(), AddressType::IPV6) {
for (n, protocol_type) in protocol_types.iter().enumerate() {
if nodes_proto_v6[n] < max_per_type
&& did.dial_info.protocol_type() == *protocol_type
{
nodes_proto_v6[n] += 1;
keep = true;
2022-06-25 14:57:33 +00:00
}
}
}
}
keep
};
2022-08-31 01:21:16 +00:00
e.node_info(RoutingDomain::PublicInternet)
.map(filter)
.unwrap_or(false)
2022-06-25 14:57:33 +00:00
})
2022-09-04 18:17:28 +00:00
},
2022-06-25 14:57:33 +00:00
// transform
2022-10-09 18:59:01 +00:00
|_rti, k: DHTKey, v: Option<Arc<BucketEntry>>| {
2022-06-25 14:57:33 +00:00
NodeRef::new(self.clone(), k, v.unwrap().clone(), None)
},
)
}
2022-10-09 18:59:01 +00:00
pub fn filter_has_valid_signed_node_info_inner(
inner: &RoutingTableInner,
2022-09-04 18:17:28 +00:00
routing_domain: RoutingDomain,
2022-10-09 18:59:01 +00:00
has_valid_own_node_info: bool,
2022-06-25 14:57:33 +00:00
v: Option<Arc<BucketEntry>>,
) -> bool {
2022-06-25 14:57:33 +00:00
match v {
2022-10-09 18:59:01 +00:00
None => has_valid_own_node_info,
Some(entry) => entry.with(inner, |_rti, e| {
2022-09-04 18:17:28 +00:00
e.signed_node_info(routing_domain.into())
.map(|sni| sni.has_valid_signature())
.unwrap_or(false)
}),
2021-11-22 16:28:30 +00:00
}
}
2022-10-09 18:59:01 +00:00
pub fn transform_to_peer_info_inner(
inner: &RoutingTableInner,
2022-08-31 01:21:16 +00:00
routing_domain: RoutingDomain,
2022-10-09 18:59:01 +00:00
own_peer_info: PeerInfo,
2022-06-25 14:57:33 +00:00
k: DHTKey,
v: Option<Arc<BucketEntry>>,
2021-11-22 16:28:30 +00:00
) -> PeerInfo {
2022-06-25 14:57:33 +00:00
match v {
2022-10-09 18:59:01 +00:00
None => own_peer_info,
Some(entry) => entry.with(inner, |_rti, e| {
e.make_peer_info(k, routing_domain).unwrap()
}),
2021-11-22 16:28:30 +00:00
}
}
2022-10-12 19:52:19 +00:00
pub fn find_peers_with_sort_and_filter<'a, 'b, F, C, T, O>(
2021-11-22 16:28:30 +00:00
&self,
node_count: usize,
cur_ts: u64,
2022-06-25 14:57:33 +00:00
mut filter: F,
2021-11-22 16:28:30 +00:00
compare: C,
2022-06-25 14:57:33 +00:00
mut transform: T,
2021-11-22 16:28:30 +00:00
) -> Vec<O>
where
2022-10-12 19:52:19 +00:00
F: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> bool,
2022-06-25 14:57:33 +00:00
C: FnMut(
2022-10-12 19:52:19 +00:00
&'a RoutingTableInner,
&'b (DHTKey, Option<Arc<BucketEntry>>),
&'b (DHTKey, Option<Arc<BucketEntry>>),
2021-11-22 16:28:30 +00:00
) -> core::cmp::Ordering,
2022-10-12 19:52:19 +00:00
T: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> O,
2021-11-22 16:28:30 +00:00
{
2022-06-25 14:57:33 +00:00
let inner = self.inner.read();
2022-10-09 18:59:01 +00:00
let inner = &*inner;
2022-09-23 15:23:33 +00:00
let self_node_id = self.unlocked_inner.node_id;
2021-11-22 16:28:30 +00:00
// collect all the nodes for sorting
let mut nodes =
2022-06-25 14:57:33 +00:00
Vec::<(DHTKey, Option<Arc<BucketEntry>>)>::with_capacity(inner.bucket_entry_count + 1);
2021-11-22 16:28:30 +00:00
// add our own node (only one of there with the None entry)
2022-10-09 18:59:01 +00:00
if filter(inner, self_node_id, None) {
2022-06-25 14:57:33 +00:00
nodes.push((self_node_id, None));
2021-11-22 16:28:30 +00:00
}
2022-06-25 14:57:33 +00:00
2021-11-22 16:28:30 +00:00
// add all nodes from buckets
2022-10-09 18:59:01 +00:00
Self::with_entries(
&*inner,
cur_ts,
BucketEntryState::Unreliable,
|rti, k, v| {
// Apply filter
if filter(rti, k, Some(v.clone())) {
nodes.push((k, Some(v.clone())));
}
Option::<()>::None
},
);
2021-11-22 16:28:30 +00:00
// sort by preference for returning nodes
2022-10-09 18:59:01 +00:00
nodes.sort_by(|a, b| compare(inner, a, b));
2021-11-22 16:28:30 +00:00
// return transformed vector for filtered+sorted nodes
let cnt = usize::min(node_count, nodes.len());
let mut out = Vec::<O>::with_capacity(cnt);
2022-06-25 14:57:33 +00:00
for node in nodes {
2022-10-09 18:59:01 +00:00
let val = transform(inner, node.0, node.1);
2021-11-22 16:28:30 +00:00
out.push(val);
}
out
}
2022-10-12 19:52:19 +00:00
pub fn find_fastest_nodes<'a, T, F, O>(
2022-06-25 14:57:33 +00:00
&self,
node_count: usize,
2022-09-04 18:17:28 +00:00
mut filter: F,
2022-09-04 19:40:35 +00:00
transform: T,
2022-06-25 14:57:33 +00:00
) -> Vec<O>
2021-11-22 16:28:30 +00:00
where
2022-10-12 19:52:19 +00:00
F: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> bool,
T: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> O,
2021-11-22 16:28:30 +00:00
{
2022-06-28 03:46:29 +00:00
let cur_ts = intf::get_timestamp();
2021-11-22 16:28:30 +00:00
let out = self.find_peers_with_sort_and_filter(
node_count,
cur_ts,
// filter
2022-10-09 18:59:01 +00:00
|rti, k, v| {
2022-06-25 14:57:33 +00:00
if let Some(entry) = &v {
// always filter out dead nodes
2022-10-09 18:59:01 +00:00
if entry.with(rti, |_rti, e| e.state(cur_ts) == BucketEntryState::Dead) {
2022-06-25 14:57:33 +00:00
false
} else {
2022-10-09 18:59:01 +00:00
filter(rti, k, v)
2022-06-25 14:57:33 +00:00
}
} else {
// always filter out self peer, as it is irrelevant to the 'fastest nodes' search
false
2021-11-26 15:39:43 +00:00
}
2021-11-22 16:28:30 +00:00
},
// sort
2022-10-09 18:59:01 +00:00
|rti, (a_key, a_entry), (b_key, b_entry)| {
2021-11-22 16:28:30 +00:00
// same nodes are always the same
if a_key == b_key {
return core::cmp::Ordering::Equal;
}
// our own node always comes last (should not happen, here for completeness)
if a_entry.is_none() {
return core::cmp::Ordering::Greater;
}
if b_entry.is_none() {
return core::cmp::Ordering::Less;
}
// reliable nodes come first
let ae = a_entry.as_ref().unwrap();
let be = b_entry.as_ref().unwrap();
2022-10-09 18:59:01 +00:00
ae.with(rti, |rti, ae| {
be.with(rti, |_rti, be| {
2022-06-25 14:57:33 +00:00
let ra = ae.check_reliable(cur_ts);
let rb = be.check_reliable(cur_ts);
if ra != rb {
if ra {
return core::cmp::Ordering::Less;
} else {
return core::cmp::Ordering::Greater;
}
}
2021-11-22 16:28:30 +00:00
2022-06-25 14:57:33 +00:00
// latency is the next metric, closer nodes first
let a_latency = match ae.peer_stats().latency.as_ref() {
None => {
// treat unknown latency as slow
return core::cmp::Ordering::Greater;
}
Some(l) => l,
};
let b_latency = match be.peer_stats().latency.as_ref() {
None => {
// treat unknown latency as slow
return core::cmp::Ordering::Less;
}
Some(l) => l,
};
// Sort by average latency
a_latency.average.cmp(&b_latency.average)
})
})
2021-11-22 16:28:30 +00:00
},
// transform,
transform,
);
out
}
2022-10-12 19:52:19 +00:00
pub fn find_closest_nodes<'a, F, T, O>(
2021-11-22 16:28:30 +00:00
&self,
node_id: DHTKey,
2022-09-04 19:40:35 +00:00
filter: F,
2022-06-25 14:57:33 +00:00
mut transform: T,
2021-11-22 16:28:30 +00:00
) -> Vec<O>
where
2022-10-12 19:52:19 +00:00
F: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> bool,
T: FnMut(&'a RoutingTableInner, DHTKey, Option<Arc<BucketEntry>>) -> O,
2021-11-22 16:28:30 +00:00
{
2022-06-28 03:46:29 +00:00
let cur_ts = intf::get_timestamp();
2021-11-22 16:28:30 +00:00
let node_count = {
2022-09-23 15:23:33 +00:00
let c = self.unlocked_inner.config.get();
2021-11-22 16:28:30 +00:00
c.network.dht.max_find_node_count as usize
};
let out = self.find_peers_with_sort_and_filter(
node_count,
cur_ts,
// filter
2022-09-04 18:17:28 +00:00
filter,
2021-11-22 16:28:30 +00:00
// sort
2022-10-09 18:59:01 +00:00
|rti, (a_key, a_entry), (b_key, b_entry)| {
2021-11-22 16:28:30 +00:00
// same nodes are always the same
if a_key == b_key {
return core::cmp::Ordering::Equal;
}
2022-06-25 14:57:33 +00:00
2021-11-22 16:28:30 +00:00
// reliable nodes come first, pessimistically treating our own node as unreliable
2022-06-25 14:57:33 +00:00
let ra = a_entry
.as_ref()
2022-10-09 18:59:01 +00:00
.map_or(false, |x| x.with(rti, |_rti, x| x.check_reliable(cur_ts)));
2022-06-25 14:57:33 +00:00
let rb = b_entry
.as_ref()
2022-10-09 18:59:01 +00:00
.map_or(false, |x| x.with(rti, |_rti, x| x.check_reliable(cur_ts)));
2021-11-22 16:28:30 +00:00
if ra != rb {
if ra {
return core::cmp::Ordering::Less;
} else {
return core::cmp::Ordering::Greater;
}
}
// distance is the next metric, closer nodes first
let da = distance(a_key, &node_id);
let db = distance(b_key, &node_id);
da.cmp(&db)
},
// transform,
2022-06-25 14:57:33 +00:00
&mut transform,
2021-11-22 16:28:30 +00:00
);
2021-12-18 00:18:25 +00:00
log_rtab!(">> find_closest_nodes: node count = {}", out.len());
2021-11-22 16:28:30 +00:00
out
}
2022-06-25 14:57:33 +00:00
2022-08-18 20:21:13 +00:00
// Build a map of protocols to low level ports
// This way we can get the set of protocols required to keep our NAT mapping alive for keepalive pings
// Only one protocol per low level protocol/port combination is required
// For example, if WS/WSS and TCP protocols are on the same low-level TCP port, only TCP keepalives will be required
// and we do not need to do WS/WSS keepalive as well. If they are on different ports, then we will need WS/WSS keepalives too.
2022-10-06 15:40:55 +00:00
pub fn get_low_level_port_info(&self) -> LowLevelPortInfo {
2022-08-18 20:21:13 +00:00
let mut low_level_protocol_ports =
BTreeSet::<(LowLevelProtocolType, AddressType, u16)>::new();
let mut protocol_to_port =
BTreeMap::<(ProtocolType, AddressType), (LowLevelProtocolType, u16)>::new();
let our_dids = self.all_filtered_dial_info_details(
2022-09-04 18:17:28 +00:00
RoutingDomain::PublicInternet.into(),
2022-08-18 20:21:13 +00:00
&DialInfoFilter::all(),
);
for did in our_dids {
low_level_protocol_ports.insert((
did.dial_info.protocol_type().low_level_protocol_type(),
did.dial_info.address_type(),
did.dial_info.socket_address().port(),
));
protocol_to_port.insert(
(did.dial_info.protocol_type(), did.dial_info.address_type()),
(
did.dial_info.protocol_type().low_level_protocol_type(),
did.dial_info.socket_address().port(),
),
);
}
2022-10-06 15:40:55 +00:00
LowLevelPortInfo {
2022-08-18 20:21:13 +00:00
low_level_protocol_ports,
protocol_to_port,
}
}
2022-09-01 01:41:48 +00:00
fn make_public_internet_relay_node_filter(&self) -> impl Fn(&BucketEntryInner) -> bool {
2022-08-09 00:42:27 +00:00
// Get all our outbound protocol/address types
2022-10-09 18:59:01 +00:00
let outbound_dif = self.get_outbound_dial_info_filter(RoutingDomain::PublicInternet);
2022-10-06 15:40:55 +00:00
let mapped_port_info = self.get_low_level_port_info();
2022-08-09 00:42:27 +00:00
move |e: &BucketEntryInner| {
2022-09-01 01:41:48 +00:00
// Ensure this node is not on the local network
2022-09-04 18:17:28 +00:00
if e.has_node_info(RoutingDomain::LocalNetwork.into()) {
2022-08-09 00:42:27 +00:00
return false;
}
2022-08-18 20:21:13 +00:00
// Disqualify nodes that don't cover all our inbound ports for tcp and udp
// as we need to be able to use the relay for keepalives for all nat mappings
let mut low_level_protocol_ports = mapped_port_info.low_level_protocol_ports.clone();
2022-08-09 00:42:27 +00:00
let can_serve_as_relay = e
2022-09-01 01:41:48 +00:00
.node_info(RoutingDomain::PublicInternet)
2022-08-09 00:42:27 +00:00
.map(|n| {
let dids = n.all_filtered_dial_info_details(
Some(DialInfoDetail::reliable_sort), // By default, choose reliable protocol for relay
|did| did.matches_filter(&outbound_dif),
);
2022-08-18 20:21:13 +00:00
for did in &dids {
let pt = did.dial_info.protocol_type();
let at = did.dial_info.address_type();
if let Some((llpt, port)) = mapped_port_info.protocol_to_port.get(&(pt, at))
{
low_level_protocol_ports.remove(&(*llpt, at, *port));
2022-08-09 00:42:27 +00:00
}
}
2022-08-18 20:21:13 +00:00
low_level_protocol_ports.is_empty()
2022-08-09 00:42:27 +00:00
})
.unwrap_or(false);
if !can_serve_as_relay {
return false;
}
true
}
}
2022-06-25 14:57:33 +00:00
#[instrument(level = "trace", skip(self), ret)]
2022-09-01 01:41:48 +00:00
pub fn find_inbound_relay(
&self,
routing_domain: RoutingDomain,
cur_ts: u64,
) -> Option<NodeRef> {
2022-08-09 00:42:27 +00:00
// Get relay filter function
2022-09-01 01:41:48 +00:00
let relay_node_filter = match routing_domain {
RoutingDomain::PublicInternet => self.make_public_internet_relay_node_filter(),
RoutingDomain::LocalNetwork => {
unimplemented!();
}
};
2022-08-09 00:42:27 +00:00
// Go through all entries and find fastest entry that matches filter function
2022-06-25 14:57:33 +00:00
let inner = self.inner.read();
let inner = &*inner;
let mut best_inbound_relay: Option<(DHTKey, Arc<BucketEntry>)> = None;
// Iterate all known nodes for candidates
2022-10-09 18:59:01 +00:00
Self::with_entries(inner, cur_ts, BucketEntryState::Unreliable, |rti, k, v| {
2022-08-09 00:42:27 +00:00
let v2 = v.clone();
2022-10-09 18:59:01 +00:00
v.with(rti, |rti, e| {
2022-08-09 00:42:27 +00:00
// Ensure we have the node's status
2022-09-01 01:41:48 +00:00
if let Some(node_status) = e.node_status(routing_domain) {
2022-08-09 00:42:27 +00:00
// Ensure the node will relay
2022-09-01 01:41:48 +00:00
if node_status.will_relay() {
2022-08-09 00:42:27 +00:00
// Compare against previous candidate
if let Some(best_inbound_relay) = best_inbound_relay.as_mut() {
// Less is faster
2022-10-09 18:59:01 +00:00
let better = best_inbound_relay.1.with(rti, |_rti, best| {
2022-06-25 14:57:33 +00:00
BucketEntryInner::cmp_fastest_reliable(cur_ts, e, best)
== std::cmp::Ordering::Less
2022-08-09 00:42:27 +00:00
});
// Now apply filter function and see if this node should be included
if better && relay_node_filter(e) {
*best_inbound_relay = (k, v2);
}
} else if relay_node_filter(e) {
// Always store the first candidate
best_inbound_relay = Some((k, v2));
2022-06-25 14:57:33 +00:00
}
}
}
2022-08-09 00:42:27 +00:00
});
// Don't end early, iterate through all entries
2022-06-25 14:57:33 +00:00
Option::<()>::None
});
// Return the best inbound relay noderef
best_inbound_relay.map(|(k, e)| NodeRef::new(self.clone(), k, e, None))
}
2022-07-20 13:39:38 +00:00
#[instrument(level = "trace", skip(self), ret)]
2022-09-03 17:57:25 +00:00
pub fn register_find_node_answer(&self, peers: Vec<PeerInfo>) -> Vec<NodeRef> {
2022-06-25 14:57:33 +00:00
let node_id = self.node_id();
// register nodes we'd found
2022-07-04 21:58:26 +00:00
let mut out = Vec::<NodeRef>::with_capacity(peers.len());
for p in peers {
2022-06-25 14:57:33 +00:00
// if our own node if is in the list then ignore it, as we don't add ourselves to our own routing table
if p.node_id.key == node_id {
continue;
}
2022-07-20 13:39:38 +00:00
// node can not be its own relay
if let Some(rpi) = &p.signed_node_info.node_info.relay_peer_info {
if rpi.node_id == p.node_id {
continue;
}
}
2022-06-25 14:57:33 +00:00
// register the node if it's new
2022-08-27 02:52:08 +00:00
if let Some(nr) = self.register_node_with_signed_node_info(
2022-09-03 17:57:25 +00:00
RoutingDomain::PublicInternet,
2022-08-27 02:52:08 +00:00
p.node_id.key,
p.signed_node_info.clone(),
false,
) {
2022-07-20 13:39:38 +00:00
out.push(nr);
}
2022-06-25 14:57:33 +00:00
}
2022-07-20 13:39:38 +00:00
out
2022-06-25 14:57:33 +00:00
}
#[instrument(level = "trace", skip(self), ret, err)]
2022-07-20 13:39:38 +00:00
pub async fn find_node(
&self,
node_ref: NodeRef,
node_id: DHTKey,
) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
2022-06-25 14:57:33 +00:00
let rpc_processor = self.rpc_processor();
2022-07-20 13:39:38 +00:00
let res = network_result_try!(
rpc_processor
.clone()
2022-09-04 18:17:28 +00:00
.rpc_call_find_node(Destination::direct(node_ref), node_id)
2022-07-20 13:39:38 +00:00
.await?
);
2022-06-25 14:57:33 +00:00
// register nodes we'd found
2022-07-20 13:39:38 +00:00
Ok(NetworkResult::value(
2022-09-03 17:57:25 +00:00
self.register_find_node_answer(res.answer),
2022-07-20 13:39:38 +00:00
))
2022-06-25 14:57:33 +00:00
}
#[instrument(level = "trace", skip(self), ret, err)]
2022-09-04 18:17:28 +00:00
pub async fn find_self(&self, node_ref: NodeRef) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
2022-06-25 14:57:33 +00:00
let node_id = self.node_id();
2022-09-04 18:17:28 +00:00
self.find_node(node_ref, node_id).await
2022-06-25 14:57:33 +00:00
}
#[instrument(level = "trace", skip(self), ret, err)]
2022-09-04 18:17:28 +00:00
pub async fn find_target(&self, node_ref: NodeRef) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
2022-06-25 14:57:33 +00:00
let node_id = node_ref.node_id();
2022-09-04 18:17:28 +00:00
self.find_node(node_ref, node_id).await
2022-06-25 14:57:33 +00:00
}
#[instrument(level = "trace", skip(self))]
2022-09-04 18:17:28 +00:00
pub async fn reverse_find_node(&self, node_ref: NodeRef, wide: bool) {
2022-06-25 14:57:33 +00:00
// Ask bootstrap node to 'find' our own node so we can get some more nodes near ourselves
// and then contact those nodes to inform -them- that we exist
// Ask bootstrap server for nodes closest to our own node
2022-09-04 18:17:28 +00:00
let closest_nodes = network_result_value_or_log!(debug match self.find_self(node_ref.clone()).await {
2022-06-25 14:57:33 +00:00
Err(e) => {
log_rtab!(error
2022-07-20 13:39:38 +00:00
"find_self failed for {:?}: {:?}",
2022-06-25 14:57:33 +00:00
&node_ref, e
);
return;
}
Ok(v) => v,
2022-07-20 13:39:38 +00:00
} => {
return;
});
2022-06-25 14:57:33 +00:00
// Ask each node near us to find us as well
if wide {
for closest_nr in closest_nodes {
2022-09-04 18:17:28 +00:00
network_result_value_or_log!(debug match self.find_self(closest_nr.clone()).await {
2022-07-20 13:39:38 +00:00
Err(e) => {
log_rtab!(error
"find_self failed for {:?}: {:?}",
&closest_nr, e
);
continue;
}
Ok(v) => v,
} => {
// Do nothing with non-values
continue;
});
2022-06-25 14:57:33 +00:00
}
}
}
2021-11-22 16:28:30 +00:00
}