mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 12:49:23 +00:00
Only let active Serai validators connect over P2P
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
use core::future::Future;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use serai_client::{
|
||||
primitives::{NetworkId, PublicKey},
|
||||
validator_sets::primitives::Session,
|
||||
Serai,
|
||||
};
|
||||
use serai_client::Serai;
|
||||
|
||||
use libp2p::{
|
||||
core::multiaddr::{Protocol, Multiaddr},
|
||||
@@ -18,16 +13,13 @@ use libp2p::{
|
||||
|
||||
use serai_task::ContinuallyRan;
|
||||
|
||||
use crate::p2p::{PORT, Peers};
|
||||
use crate::p2p::{PORT, Peers, validators::Validators};
|
||||
|
||||
const TARGET_PEERS_PER_NETWORK: usize = 5;
|
||||
|
||||
struct DialTask {
|
||||
serai: Serai,
|
||||
|
||||
sessions: HashMap<NetworkId, Session>,
|
||||
validators: HashMap<NetworkId, Vec<PublicKey>>,
|
||||
|
||||
validators: Validators,
|
||||
peers: Peers,
|
||||
to_dial: mpsc::UnboundedSender<DialOpts>,
|
||||
}
|
||||
@@ -37,29 +29,7 @@ impl ContinuallyRan for DialTask {
|
||||
|
||||
fn run_iteration(&mut self) -> impl Send + Future<Output = Result<bool, String>> {
|
||||
async move {
|
||||
let temporal_serai =
|
||||
self.serai.as_of_latest_finalized_block().await.map_err(|e| format!("{e:?}"))?;
|
||||
let temporal_serai = temporal_serai.validator_sets();
|
||||
for network in serai_client::primitives::NETWORKS {
|
||||
if network == NetworkId::Serai {
|
||||
continue;
|
||||
}
|
||||
let Some(session) = temporal_serai.session(network).await.map_err(|e| format!("{e:?}"))?
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
// If the session has changed, populate it with the current validators
|
||||
if self.sessions.get(&network) != Some(&session) {
|
||||
self.validators.insert(
|
||||
network,
|
||||
temporal_serai
|
||||
.active_network_validators(network)
|
||||
.await
|
||||
.map_err(|e| format!("{e:?}"))?,
|
||||
);
|
||||
self.sessions.insert(network, session);
|
||||
}
|
||||
}
|
||||
self.validators.update().await?;
|
||||
|
||||
// If any of our peers is lacking, try to connect to more
|
||||
let mut dialed = false;
|
||||
@@ -81,7 +51,14 @@ impl ContinuallyRan for DialTask {
|
||||
only try to connect to most of the validators actually present.
|
||||
*/
|
||||
if (peer_count < TARGET_PEERS_PER_NETWORK) &&
|
||||
(peer_count < self.validators[&network].len().saturating_sub(1))
|
||||
(peer_count <
|
||||
self
|
||||
.validators
|
||||
.validators()
|
||||
.get(&network)
|
||||
.map(Vec::len)
|
||||
.unwrap_or(0)
|
||||
.saturating_sub(1))
|
||||
{
|
||||
let mut potential_peers =
|
||||
self.serai.p2p_validators(network).await.map_err(|e| format!("{e:?}"))?;
|
||||
|
||||
Reference in New Issue
Block a user