2025-01-04 23:28:29 -05:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2025-01-04 22:21:23 -05:00
|
|
|
|
|
|
|
|
use serai_client::{primitives::NetworkId, validator_sets::primitives::Session, Serai};
|
|
|
|
|
|
|
|
|
|
use libp2p::PeerId;
|
|
|
|
|
|
|
|
|
|
use crate::p2p::peer_id_from_public;
|
|
|
|
|
|
|
|
|
|
pub(crate) struct Validators {
|
|
|
|
|
serai: Serai,
|
|
|
|
|
|
|
|
|
|
// A cache for which session we're populated with the validators of
|
|
|
|
|
sessions: HashMap<NetworkId, Session>,
|
|
|
|
|
// The validators by network
|
2025-01-04 23:28:29 -05:00
|
|
|
by_network: HashMap<NetworkId, HashSet<PeerId>>,
|
|
|
|
|
// The validators and their networks
|
|
|
|
|
validators: HashMap<PeerId, HashSet<NetworkId>>,
|
2025-01-04 22:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Validators {
|
2025-01-04 23:28:29 -05:00
|
|
|
/// Update the view of the validators.
|
|
|
|
|
///
|
|
|
|
|
/// Returns all validators removed from the active validator set.
|
|
|
|
|
pub(crate) async fn update(&mut self) -> Result<HashSet<PeerId>, String> {
|
|
|
|
|
let mut removed = HashSet::new();
|
|
|
|
|
|
2025-01-04 22:21:23 -05:00
|
|
|
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) {
|
|
|
|
|
let new_validators =
|
|
|
|
|
temporal_serai.active_network_validators(network).await.map_err(|e| format!("{e:?}"))?;
|
|
|
|
|
let new_validators =
|
2025-01-04 23:28:29 -05:00
|
|
|
new_validators.into_iter().map(peer_id_from_public).collect::<HashSet<_>>();
|
2025-01-04 22:21:23 -05:00
|
|
|
|
|
|
|
|
// Remove the existing validators
|
2025-01-04 23:28:29 -05:00
|
|
|
for validator in self.by_network.remove(&network).unwrap_or_else(HashSet::new) {
|
|
|
|
|
let mut networks = self.validators.remove(&validator).unwrap();
|
|
|
|
|
networks.remove(&network);
|
|
|
|
|
if networks.is_empty() {
|
|
|
|
|
removed.insert(validator);
|
|
|
|
|
} else {
|
|
|
|
|
self.validators.insert(validator, networks);
|
2025-01-04 22:21:23 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the new validators
|
|
|
|
|
for validator in new_validators.iter().copied() {
|
2025-01-04 23:28:29 -05:00
|
|
|
self.validators.entry(validator).or_insert_with(HashSet::new).insert(network);
|
2025-01-04 22:21:23 -05:00
|
|
|
}
|
|
|
|
|
self.by_network.insert(network, new_validators);
|
|
|
|
|
|
|
|
|
|
// Update the session we have populated
|
|
|
|
|
self.sessions.insert(network, session);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-04 23:28:29 -05:00
|
|
|
|
|
|
|
|
Ok(removed)
|
2025-01-04 22:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-04 23:28:29 -05:00
|
|
|
pub(crate) fn by_network(&self) -> &HashMap<NetworkId, HashSet<PeerId>> {
|
2025-01-04 22:21:23 -05:00
|
|
|
&self.by_network
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn contains(&self, peer_id: &PeerId) -> bool {
|
2025-01-04 23:28:29 -05:00
|
|
|
self.validators.contains_key(peer_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn networks(&self, peer_id: &PeerId) -> Option<&HashSet<NetworkId>> {
|
|
|
|
|
self.validators.get(peer_id)
|
2025-01-04 22:21:23 -05:00
|
|
|
}
|
|
|
|
|
}
|