networkresult
This commit is contained in:
@@ -389,8 +389,8 @@ impl RoutingTable {
|
||||
best_inbound_relay.map(|(k, e)| NodeRef::new(self.clone(), k, e, None))
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret, err)]
|
||||
pub fn register_find_node_answer(&self, peers: Vec<PeerInfo>) -> EyreResult<Vec<NodeRef>> {
|
||||
#[instrument(level = "trace", skip(self), ret)]
|
||||
pub fn register_find_node_answer(&self, peers: Vec<PeerInfo>) -> Vec<NodeRef> {
|
||||
let node_id = self.node_id();
|
||||
|
||||
// register nodes we'd found
|
||||
@@ -401,40 +401,57 @@ impl RoutingTable {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// register the node if it's new
|
||||
let nr = self
|
||||
.register_node_with_signed_node_info(p.node_id.key, p.signed_node_info.clone())?;
|
||||
out.push(nr);
|
||||
if let Some(nr) =
|
||||
self.register_node_with_signed_node_info(p.node_id.key, p.signed_node_info.clone())
|
||||
{
|
||||
out.push(nr);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
out
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret, err)]
|
||||
pub async fn find_node(&self, node_ref: NodeRef, node_id: DHTKey) -> EyreResult<Vec<NodeRef>> {
|
||||
pub async fn find_node(
|
||||
&self,
|
||||
node_ref: NodeRef,
|
||||
node_id: DHTKey,
|
||||
) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
|
||||
let rpc_processor = self.rpc_processor();
|
||||
|
||||
let res = rpc_processor
|
||||
.clone()
|
||||
.rpc_call_find_node(
|
||||
Destination::Direct(node_ref.clone()),
|
||||
node_id,
|
||||
None,
|
||||
rpc_processor.make_respond_to_sender(node_ref.clone()),
|
||||
)
|
||||
.await?;
|
||||
let res = network_result_try!(
|
||||
rpc_processor
|
||||
.clone()
|
||||
.rpc_call_find_node(
|
||||
Destination::Direct(node_ref.clone()),
|
||||
node_id,
|
||||
None,
|
||||
rpc_processor.make_respond_to_sender(node_ref.clone()),
|
||||
)
|
||||
.await?
|
||||
);
|
||||
|
||||
// register nodes we'd found
|
||||
self.register_find_node_answer(res.answer)
|
||||
Ok(NetworkResult::value(
|
||||
self.register_find_node_answer(res.answer),
|
||||
))
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret, err)]
|
||||
pub async fn find_self(&self, node_ref: NodeRef) -> EyreResult<Vec<NodeRef>> {
|
||||
pub async fn find_self(&self, node_ref: NodeRef) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
|
||||
let node_id = self.node_id();
|
||||
self.find_node(node_ref, node_id).await
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret, err)]
|
||||
pub async fn find_target(&self, node_ref: NodeRef) -> EyreResult<Vec<NodeRef>> {
|
||||
pub async fn find_target(&self, node_ref: NodeRef) -> EyreResult<NetworkResult<Vec<NodeRef>>> {
|
||||
let node_id = node_ref.node_id();
|
||||
self.find_node(node_ref, node_id).await
|
||||
}
|
||||
@@ -445,26 +462,35 @@ impl RoutingTable {
|
||||
// and then contact those nodes to inform -them- that we exist
|
||||
|
||||
// Ask bootstrap server for nodes closest to our own node
|
||||
let closest_nodes = match self.find_self(node_ref.clone()).await {
|
||||
let closest_nodes = network_result_value_or_log!(debug match self.find_self(node_ref.clone()).await {
|
||||
Err(e) => {
|
||||
log_rtab!(error
|
||||
"reverse_find_node: find_self failed for {:?}: {:?}",
|
||||
"find_self failed for {:?}: {:?}",
|
||||
&node_ref, e
|
||||
);
|
||||
return;
|
||||
}
|
||||
Ok(v) => v,
|
||||
};
|
||||
} => {
|
||||
return;
|
||||
});
|
||||
|
||||
// Ask each node near us to find us as well
|
||||
if wide {
|
||||
for closest_nr in closest_nodes {
|
||||
if let Err(e) = self.find_self(closest_nr.clone()).await {
|
||||
log_rtab!(error
|
||||
"reverse_find_node: closest node find_self failed for {:?}: {:?}",
|
||||
&closest_nr, e
|
||||
);
|
||||
}
|
||||
network_result_value_or_log!(debug match self.find_self(closest_nr.clone()).await {
|
||||
Err(e) => {
|
||||
log_rtab!(error
|
||||
"find_self failed for {:?}: {:?}",
|
||||
&closest_nr, e
|
||||
);
|
||||
continue;
|
||||
}
|
||||
Ok(v) => v,
|
||||
} => {
|
||||
// Do nothing with non-values
|
||||
continue;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -576,13 +576,14 @@ impl RoutingTable {
|
||||
// Create a node reference, possibly creating a bucket entry
|
||||
// the 'update_func' closure is called on the node, and, if created,
|
||||
// in a locked fashion as to ensure the bucket entry state is always valid
|
||||
pub fn create_node_ref<F>(&self, node_id: DHTKey, update_func: F) -> EyreResult<NodeRef>
|
||||
pub fn create_node_ref<F>(&self, node_id: DHTKey, update_func: F) -> Option<NodeRef>
|
||||
where
|
||||
F: FnOnce(&mut BucketEntryInner),
|
||||
{
|
||||
// Ensure someone isn't trying register this node itself
|
||||
if node_id == self.node_id() {
|
||||
bail!("can't register own node");
|
||||
log_rtab!(debug "can't register own node");
|
||||
return None;
|
||||
}
|
||||
|
||||
// Lock this entire operation
|
||||
@@ -627,7 +628,7 @@ impl RoutingTable {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(noderef)
|
||||
Some(noderef)
|
||||
}
|
||||
|
||||
pub fn lookup_node_ref(&self, node_id: DHTKey) -> Option<NodeRef> {
|
||||
@@ -645,22 +646,22 @@ impl RoutingTable {
|
||||
&self,
|
||||
node_id: DHTKey,
|
||||
signed_node_info: SignedNodeInfo,
|
||||
) -> EyreResult<NodeRef> {
|
||||
) -> Option<NodeRef> {
|
||||
// validate signed node info is not something malicious
|
||||
if node_id == self.node_id() {
|
||||
bail!("can't register own node id in routing table");
|
||||
log_rtab!(debug "can't register own node id in routing table");
|
||||
return None;
|
||||
}
|
||||
if let Some(rpi) = &signed_node_info.node_info.relay_peer_info {
|
||||
if rpi.node_id.key == node_id {
|
||||
bail!("node can not be its own relay");
|
||||
log_rtab!(debug "node can not be its own relay");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let nr = self.create_node_ref(node_id, |e| {
|
||||
self.create_node_ref(node_id, |e| {
|
||||
e.update_node_info(signed_node_info);
|
||||
})?;
|
||||
|
||||
Ok(nr)
|
||||
})
|
||||
}
|
||||
|
||||
// Shortcut function to add a node to our routing table if it doesn't exist
|
||||
@@ -670,13 +671,11 @@ impl RoutingTable {
|
||||
node_id: DHTKey,
|
||||
descriptor: ConnectionDescriptor,
|
||||
timestamp: u64,
|
||||
) -> EyreResult<NodeRef> {
|
||||
let nr = self.create_node_ref(node_id, |e| {
|
||||
) -> Option<NodeRef> {
|
||||
self.create_node_ref(node_id, |e| {
|
||||
// set the most recent node address for connection finding and udp replies
|
||||
e.set_last_connection(descriptor, timestamp);
|
||||
})?;
|
||||
|
||||
Ok(nr)
|
||||
})
|
||||
}
|
||||
|
||||
// Ticks about once per second
|
||||
|
@@ -104,8 +104,6 @@ impl NodeRef {
|
||||
// Register relay node and return noderef
|
||||
self.routing_table
|
||||
.register_node_with_signed_node_info(t.node_id.key, t.signed_node_info)
|
||||
.map_err(logthru_rtab!(error))
|
||||
.ok()
|
||||
.map(|mut nr| {
|
||||
nr.set_filter(self.filter_ref().cloned());
|
||||
nr
|
||||
|
@@ -213,13 +213,13 @@ impl RoutingTable {
|
||||
for pi in peer_info {
|
||||
let k = pi.node_id.key;
|
||||
// Register the node
|
||||
let nr = self.register_node_with_signed_node_info(k, pi.signed_node_info)?;
|
||||
|
||||
// Add this our futures to process in parallel
|
||||
unord.push(
|
||||
// lets ask bootstrap to find ourselves now
|
||||
self.reverse_find_node(nr, true),
|
||||
);
|
||||
if let Some(nr) = self.register_node_with_signed_node_info(k, pi.signed_node_info) {
|
||||
// Add this our futures to process in parallel
|
||||
unord.push(
|
||||
// lets ask bootstrap to find ourselves now
|
||||
self.reverse_find_node(nr, true),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +299,7 @@ impl RoutingTable {
|
||||
log_rtab!("--- bootstrapping {} with {:?}", k.encode(), &v);
|
||||
|
||||
// Make invalid signed node info (no signature)
|
||||
let nr = self.register_node_with_signed_node_info(
|
||||
if let Some(nr) = self.register_node_with_signed_node_info(
|
||||
k,
|
||||
SignedNodeInfo::with_no_signature(NodeInfo {
|
||||
network_class: NetworkClass::InboundCapable, // Bootstraps are always inbound capable
|
||||
@@ -309,27 +309,27 @@ impl RoutingTable {
|
||||
dial_info_detail_list: v.dial_info_details, // Dial info is as specified in the bootstrap list
|
||||
relay_peer_info: None, // Bootstraps never require a relay themselves
|
||||
}),
|
||||
)?;
|
||||
) {
|
||||
// Add this our futures to process in parallel
|
||||
let this = self.clone();
|
||||
unord.push(async move {
|
||||
// Need VALID signed peer info, so ask bootstrap to find_node of itself
|
||||
// which will ensure it has the bootstrap's signed peer info as part of the response
|
||||
let _ = this.find_target(nr.clone()).await;
|
||||
|
||||
// Add this our futures to process in parallel
|
||||
let this = self.clone();
|
||||
unord.push(async move {
|
||||
// Need VALID signed peer info, so ask bootstrap to find_node of itself
|
||||
// which will ensure it has the bootstrap's signed peer info as part of the response
|
||||
let _ = this.find_target(nr.clone()).await;
|
||||
|
||||
// Ensure we got the signed peer info
|
||||
if !nr.operate(|e| e.has_valid_signed_node_info()) {
|
||||
log_rtab!(warn
|
||||
"bootstrap at {:?} did not return valid signed node info",
|
||||
nr
|
||||
);
|
||||
// If this node info is invalid, it will time out after being unpingable
|
||||
} else {
|
||||
// otherwise this bootstrap is valid, lets ask it to find ourselves now
|
||||
this.reverse_find_node(nr, true).await
|
||||
}
|
||||
});
|
||||
// Ensure we got the signed peer info
|
||||
if !nr.operate(|e| e.has_valid_signed_node_info()) {
|
||||
log_rtab!(warn
|
||||
"bootstrap at {:?} did not return valid signed node info",
|
||||
nr
|
||||
);
|
||||
// If this node info is invalid, it will time out after being unpingable
|
||||
} else {
|
||||
// otherwise this bootstrap is valid, lets ask it to find ourselves now
|
||||
this.reverse_find_node(nr, true).await
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for all bootstrap operations to complete before we complete the singlefuture
|
||||
|
Reference in New Issue
Block a user