Skip to content

Commit

Permalink
Move code for starting new routing table sync to RoutingTableActor (#…
Browse files Browse the repository at this point in the history
…5163)

In preparation for moving routing table computation to `RoutingTableActor`, we should move  handling `start_routing_table_syncv2` from `PeerManager` to `RoutingTableActor` first.

Related to #5138
Change extracted from #5089, to make that PR shorter.
  • Loading branch information
pmnoxx authored Nov 8, 2021
1 parent fee5d0a commit 59fbb33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
26 changes: 14 additions & 12 deletions chain/network/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::types::{
};
use crate::types::{GetPeerId, GetPeerIdResult};
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
use crate::types::{RoutingState, RoutingSyncV2, RoutingVersion2};
use crate::types::{RoutingSyncV2, RoutingVersion2};

/// How often to request peers from active peers.
const REQUEST_PEERS_INTERVAL: Duration = Duration::from_millis(60_000);
Expand Down Expand Up @@ -353,9 +353,9 @@ impl PeerManagerActor {
act.routing_table_pool
.send(RoutingTableMessages::AddPeerIfMissing(peer_id, None))
.into_actor(act)
.map(move |response, act, _ctx| match response {
.map(move |response, act, ctx| match response {
Ok(RoutingTableMessagesResponse::AddPeerResponse { seed }) => {
act.start_routing_table_syncv2(addr, seed)
act.start_routing_table_syncv2(ctx, addr, seed)
}
_ => error!(target: "network", "expected AddIbfSetResponse"),
})
Expand All @@ -364,15 +364,17 @@ impl PeerManagerActor {
});
}
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
fn start_routing_table_syncv2(&self, addr: Addr<Peer>, seed: u64) {
let _ = addr.do_send(SendMessage {
message: PeerMessage::RoutingTableSyncV2(RoutingSyncV2::Version2(RoutingVersion2 {
known_edges: self.routing_table.get_edges_len(),
seed,
edges: Default::default(),
routing_state: RoutingState::InitializeIbf,
})),
});
fn start_routing_table_syncv2(&self, ctx: &mut Context<Self>, addr: Addr<Peer>, seed: u64) {
self.routing_table_pool
.send(RoutingTableMessages::StartRoutingTableSync { seed })
.into_actor(self)
.map(move |response, _act, _ctx| match response {
Ok(RoutingTableMessagesResponse::StartRoutingTableSyncResponse(response)) => {
let _ = addr.do_send(SendMessage { message: response });
}
_ => error!(target: "network", "expected StartRoutingTableSyncResponse"),
})
.spawn(ctx);
}

/// Register a direct connection to a new peer. This will be called after successfully
Expand Down
19 changes: 18 additions & 1 deletion chain/network/src/routing_table_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::routing::Edge;
use crate::routing::{SimpleEdge, ValidIBFLevel, MIN_IBF_LEVEL};
use crate::types::StopMsg;
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
use crate::types::{PartialSync, RoutingState, RoutingVersion2};
use crate::types::{PartialSync, PeerMessage, RoutingState, RoutingSyncV2, RoutingVersion2};

/// Actor that maintains routing table information.
/// TODO (PIOTR, #4859) Finish moving routing table computation to new thread.
Expand Down Expand Up @@ -68,6 +68,10 @@ pub enum RoutingTableMessages {
peer_id: PeerId,
ibf_msg: RoutingVersion2,
},
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
StartRoutingTableSync {
seed: u64,
},
}

impl Message for RoutingTableMessages {
Expand All @@ -88,6 +92,8 @@ pub enum RoutingTableMessagesResponse {
RequestRoutingTableResponse {
edges_info: Vec<Edge>,
},
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
StartRoutingTableSyncResponse(PeerMessage),
}

#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
Expand Down Expand Up @@ -142,6 +148,17 @@ impl Handler<RoutingTableMessages> for RoutingTableActor {
}
RoutingTableMessagesResponse::Empty
}
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
RoutingTableMessages::StartRoutingTableSync { seed } => {
RoutingTableMessagesResponse::StartRoutingTableSyncResponse(
PeerMessage::RoutingTableSyncV2(RoutingSyncV2::Version2(RoutingVersion2 {
known_edges: self.edges_info.len() as u64,
seed,
edges: Default::default(),
routing_state: RoutingState::InitializeIbf,
})),
)
}
RoutingTableMessages::RequestRoutingTable => {
RoutingTableMessagesResponse::RequestRoutingTableResponse {
edges_info: self.edges_info.iter().map(|(_k, v)| v.clone()).collect(),
Expand Down

0 comments on commit 59fbb33

Please sign in to comment.