mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 13:54:44 +00:00
Refactor Peerdb and PeerManager (#2660)
## Proposed Changes This is a refactor of the PeerDB and PeerManager. A number of bugs have been surfacing around the connection state of peers and their interaction with the score state. This refactor tightens the mutability properties of peers such that only specific modules are able to modify the state of peer information preventing inadvertant state changes that can lead to our local peer manager db being out of sync with libp2p. Further, the logic around connection and scoring was quite convoluted and the distinction between the PeerManager and Peerdb was not well defined. Although these issues are not fully resolved, this PR is step to cleaning up this logic. The peerdb solely manages most mutability operations of peers leaving high-order logic to the peer manager. A single `update_connection_state()` function has been added to the peer-db making it solely responsible for modifying the peer's connection state. The way the peer's scores can be modified have been reduced to three simple functions (`update_scores()`, `update_gossipsub_scores()` and `report_peer()`). This prevents any add-hoc modifications of scores and only natural processes of score modification is allowed which simplifies the reasoning of score and state changes.
This commit is contained in:
@@ -7,7 +7,7 @@ use eth2_libp2p::{
|
||||
discv5::enr::{CombinedKey, EnrBuilder},
|
||||
rpc::methods::{MetaData, MetaDataV2},
|
||||
types::{EnrAttestationBitfield, EnrSyncCommitteeBitfield, SyncState},
|
||||
Enr, NetworkGlobals, PeerId,
|
||||
ConnectedPoint, Enr, NetworkConfig, NetworkGlobals, PeerId, PeerManager,
|
||||
};
|
||||
use http_api::{Config, Context};
|
||||
use network::NetworkMessage;
|
||||
@@ -46,7 +46,7 @@ pub struct ApiServer<E: EthSpec, SFut: Future<Output = ()>> {
|
||||
}
|
||||
|
||||
impl<E: EthSpec> InteractiveTester<E> {
|
||||
pub fn new(spec: Option<ChainSpec>, validator_count: usize) -> Self {
|
||||
pub async fn new(spec: Option<ChainSpec>, validator_count: usize) -> Self {
|
||||
let harness = BeaconChainHarness::new(
|
||||
E::default(),
|
||||
spec,
|
||||
@@ -59,7 +59,7 @@ impl<E: EthSpec> InteractiveTester<E> {
|
||||
shutdown_tx: _server_shutdown,
|
||||
network_rx,
|
||||
..
|
||||
} = create_api_server(harness.chain.clone(), harness.logger().clone());
|
||||
} = create_api_server(harness.chain.clone(), harness.logger().clone()).await;
|
||||
|
||||
tokio::spawn(server);
|
||||
|
||||
@@ -82,7 +82,7 @@ impl<E: EthSpec> InteractiveTester<E> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_api_server<T: BeaconChainTypes>(
|
||||
pub async fn create_api_server<T: BeaconChainTypes>(
|
||||
chain: Arc<BeaconChain<T>>,
|
||||
log: Logger,
|
||||
) -> ApiServer<T::EthSpec, impl Future<Output = ()>> {
|
||||
@@ -96,15 +96,30 @@ pub fn create_api_server<T: BeaconChainTypes>(
|
||||
});
|
||||
let enr_key = CombinedKey::generate_secp256k1();
|
||||
let enr = EnrBuilder::new("v4").build(&enr_key).unwrap();
|
||||
let network_globals =
|
||||
NetworkGlobals::new(enr.clone(), TCP_PORT, UDP_PORT, meta_data, vec![], &log);
|
||||
let network_globals = Arc::new(NetworkGlobals::new(
|
||||
enr.clone(),
|
||||
TCP_PORT,
|
||||
UDP_PORT,
|
||||
meta_data,
|
||||
vec![],
|
||||
&log,
|
||||
));
|
||||
|
||||
// Only a peer manager can add peers, so we create a dummy manager.
|
||||
let network_config = NetworkConfig::default();
|
||||
let mut pm = PeerManager::new(&network_config, network_globals.clone(), &log)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// add a peer
|
||||
let peer_id = PeerId::random();
|
||||
network_globals
|
||||
.peers
|
||||
.write()
|
||||
.connect_ingoing(&peer_id, EXTERNAL_ADDR.parse().unwrap(), None);
|
||||
|
||||
let connected_point = ConnectedPoint::Listener {
|
||||
local_addr: EXTERNAL_ADDR.parse().unwrap(),
|
||||
send_back_addr: EXTERNAL_ADDR.parse().unwrap(),
|
||||
};
|
||||
let num_established = std::num::NonZeroU32::new(1).unwrap();
|
||||
pm.inject_connection_established(peer_id, connected_point, num_established, None);
|
||||
*network_globals.sync_state.write() = SyncState::Synced;
|
||||
|
||||
let eth1_service = eth1::Service::new(eth1::Config::default(), log.clone(), chain.spec.clone());
|
||||
@@ -119,7 +134,7 @@ pub fn create_api_server<T: BeaconChainTypes>(
|
||||
},
|
||||
chain: Some(chain.clone()),
|
||||
network_tx: Some(network_tx),
|
||||
network_globals: Some(Arc::new(network_globals)),
|
||||
network_globals: Some(network_globals),
|
||||
eth1_service: Some(eth1_service),
|
||||
log,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user