From 59fbb336f5db3541b0c9208642f55b67f328e2d0 Mon Sep 17 00:00:00 2001 From: pmnoxx Date: Mon, 8 Nov 2021 00:39:54 -0800 Subject: [PATCH] Move code for starting new routing table sync to RoutingTableActor (#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 /~https://github.com/near/nearcore/issues/5138 Change extracted from /~https://github.com/near/nearcore/pull/5089, to make that PR shorter. --- chain/network/src/peer_manager.rs | 26 +++++++++++++----------- chain/network/src/routing_table_actor.rs | 19 ++++++++++++++++- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/chain/network/src/peer_manager.rs b/chain/network/src/peer_manager.rs index c8156ca3a1d..38e75dd6006 100644 --- a/chain/network/src/peer_manager.rs +++ b/chain/network/src/peer_manager.rs @@ -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); @@ -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"), }) @@ -364,15 +364,17 @@ impl PeerManagerActor { }); } #[cfg(feature = "protocol_feature_routing_exchange_algorithm")] - fn start_routing_table_syncv2(&self, addr: Addr, 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, addr: Addr, 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 diff --git a/chain/network/src/routing_table_actor.rs b/chain/network/src/routing_table_actor.rs index 38f4c225d42..22377fec88e 100644 --- a/chain/network/src/routing_table_actor.rs +++ b/chain/network/src/routing_table_actor.rs @@ -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. @@ -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 { @@ -88,6 +92,8 @@ pub enum RoutingTableMessagesResponse { RequestRoutingTableResponse { edges_info: Vec, }, + #[cfg(feature = "protocol_feature_routing_exchange_algorithm")] + StartRoutingTableSyncResponse(PeerMessage), } #[cfg(feature = "protocol_feature_routing_exchange_algorithm")] @@ -142,6 +148,17 @@ impl Handler 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(),