Move peer db writes to eth2 libp2p (#2724)

## Issue Addressed
Part of a bigger effort to make the network globals read only. This moves all writes to the `PeerDB` to the `eth2_libp2p` crate. Limiting writes to the peer manager is a slightly more complicated issue for a next PR, to keep things reviewable.

## Proposed Changes
- Make the peers field in the globals a private field.
- Allow mutable access to the peers field to `eth2_libp2p` for now.
- Add a new network message to update the sync state.

Co-authored-by: Age Manning <Age@AgeManning.com>
This commit is contained in:
Divma
2021-11-19 04:42:31 +00:00
parent 31386277c3
commit 53562010ec
16 changed files with 139 additions and 154 deletions

View File

@@ -10,7 +10,9 @@ use fnv::FnvHashMap;
use lighthouse_network::rpc::{
BlocksByRangeRequest, BlocksByRootRequest, GoodbyeReason, RequestId,
};
use lighthouse_network::{Client, NetworkGlobals, PeerAction, PeerId, ReportSource, Request};
use lighthouse_network::{
Client, NetworkGlobals, PeerAction, PeerId, ReportSource, Request, SyncStatus,
};
use slog::{debug, trace, warn};
use std::sync::Arc;
use tokio::sync::mpsc;
@@ -52,12 +54,7 @@ impl<T: EthSpec> SyncNetworkContext<T> {
/// Returns the Client type of the peer if known
pub fn client_type(&self, peer_id: &PeerId) -> Client {
self.network_globals
.peers
.read()
.peer_info(peer_id)
.map(|info| info.client().clone())
.unwrap_or_default()
self.network_globals.client(peer_id)
}
pub fn status_peers<C: ToStatusMessage>(
@@ -208,10 +205,17 @@ impl<T: EthSpec> SyncNetworkContext<T> {
});
}
pub fn update_peer_sync_status(&self, peer_id: PeerId, new_status: SyncStatus) {
let _ = self.send_network_msg(NetworkMessage::UpdatePeerSyncStatus {
peer_id,
sync_status: new_status,
});
}
/// Sends an arbitrary network message.
fn send_network_msg(&mut self, msg: NetworkMessage<T>) -> Result<(), &'static str> {
self.network_send.send(msg).map_err(|_| {
debug!(self.log, "Could not send message to the network service");
fn send_network_msg(&self, msg: NetworkMessage<T>) -> Result<(), &'static str> {
self.network_send.send(msg).map_err(|msg| {
warn!(self.log, "Could not send message to the network service"; "msg" => ?msg.0);
"Network channel send Failed"
})
}