mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 20:22:02 +00:00
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:
@@ -14,7 +14,9 @@ use crate::types::{
|
||||
SubnetDiscovery,
|
||||
};
|
||||
use crate::Eth2Enr;
|
||||
use crate::{error, metrics, Enr, NetworkConfig, NetworkGlobals, PubsubMessage, TopicHash};
|
||||
use crate::{
|
||||
error, metrics, Enr, NetworkConfig, NetworkGlobals, PubsubMessage, SyncStatus, TopicHash,
|
||||
};
|
||||
use libp2p::{
|
||||
core::{
|
||||
connection::ConnectionId, identity::Keypair, multiaddr::Protocol as MProtocol, Multiaddr,
|
||||
@@ -32,7 +34,7 @@ use libp2p::{
|
||||
},
|
||||
NetworkBehaviour, PeerId,
|
||||
};
|
||||
use slog::{crit, debug, o, trace, warn};
|
||||
use slog::{crit, debug, error, o, trace, warn};
|
||||
use ssz::Encode;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
@@ -455,8 +457,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
|
||||
} {
|
||||
if let Some(client) = self
|
||||
.network_globals
|
||||
.peers
|
||||
.read()
|
||||
.peers()
|
||||
.peer_info(propagation_source)
|
||||
.map(|info| info.client().kind.as_ref())
|
||||
{
|
||||
@@ -568,6 +569,25 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
|
||||
self.discovery.add_enr(enr);
|
||||
}
|
||||
|
||||
pub fn update_peers_sync_status(&mut self, peer_id: &PeerId, sync_status: SyncStatus) {
|
||||
let status_repr = sync_status.as_str();
|
||||
match self
|
||||
.network_globals
|
||||
.peers_mut()
|
||||
.update_sync_status(peer_id, sync_status)
|
||||
{
|
||||
Some(true) => {
|
||||
trace!(self.log, "Peer sync status updated"; "peer_id" => %peer_id, "sync_status" => status_repr);
|
||||
}
|
||||
Some(false) => {
|
||||
// Sync status is the same for known peer
|
||||
}
|
||||
None => {
|
||||
error!(self.log, "Sync status update notification for unknown peer"; "peer_id" => %peer_id, "sync_status" => status_repr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates a subnet value to the ENR attnets/syncnets bitfield.
|
||||
///
|
||||
/// The `value` is `true` if a subnet is being added and false otherwise.
|
||||
@@ -593,8 +613,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
|
||||
// Extend min_ttl of connected peers on required subnets
|
||||
if let Some(min_ttl) = s.min_ttl {
|
||||
self.network_globals
|
||||
.peers
|
||||
.write()
|
||||
.peers_mut()
|
||||
.extend_peers_on_subnet(&s.subnet, min_ttl);
|
||||
if let Subnet::SyncCommittee(sync_subnet) = s.subnet {
|
||||
self.peer_manager_mut()
|
||||
@@ -604,8 +623,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
|
||||
// Already have target number of peers, no need for subnet discovery
|
||||
let peers_on_subnet = self
|
||||
.network_globals
|
||||
.peers
|
||||
.read()
|
||||
.peers()
|
||||
.good_peers_on_subnet(s.subnet)
|
||||
.count();
|
||||
if peers_on_subnet >= TARGET_SUBNET_PEERS {
|
||||
@@ -755,7 +773,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
|
||||
.discovery
|
||||
.cached_enrs()
|
||||
.filter_map(|(peer_id, enr)| {
|
||||
let peers = self.network_globals.peers.read();
|
||||
let peers = self.network_globals.peers();
|
||||
if predicate(enr) && peers.should_dial(peer_id) {
|
||||
Some(*peer_id)
|
||||
} else {
|
||||
@@ -848,16 +866,14 @@ impl<TSpec: EthSpec> NetworkBehaviourEventProcess<GossipsubEvent> for Behaviour<
|
||||
GossipsubEvent::Subscribed { peer_id, topic } => {
|
||||
if let Some(subnet_id) = subnet_from_topic_hash(&topic) {
|
||||
self.network_globals
|
||||
.peers
|
||||
.write()
|
||||
.peers_mut()
|
||||
.add_subscription(&peer_id, subnet_id);
|
||||
}
|
||||
}
|
||||
GossipsubEvent::Unsubscribed { peer_id, topic } => {
|
||||
if let Some(subnet_id) = subnet_from_topic_hash(&topic) {
|
||||
self.network_globals
|
||||
.peers
|
||||
.write()
|
||||
.peers_mut()
|
||||
.remove_subscription(&peer_id, &subnet_id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user