use std::time::Duration;
use log::*;
use tari_comms::{connectivity::ConnectivityRequester, peer_manager::NodeId};
use crate::base_node::BlockchainSyncConfig;
const LOG_TARGET: &str = "c::bn::sync";
pub struct PeerBanManager {
config: BlockchainSyncConfig,
connectivity: ConnectivityRequester,
}
impl PeerBanManager {
pub fn new(config: BlockchainSyncConfig, connectivity: ConnectivityRequester) -> Self {
Self { config, connectivity }
}
pub async fn ban_peer_if_required(&mut self, node_id: &NodeId, ban_reason: String, ban_duration: Duration) {
if self.config.forced_sync_peers.contains(node_id) || self.config.monitored_peers.contains(node_id) {
debug!(
target: LOG_TARGET,
"Not banning peer that is on the allow list for sync. Ban reason = {ban_reason}"
);
return;
}
debug!(target: LOG_TARGET, "Sync peer {node_id} removed from the sync peer list because {ban_reason}");
match self
.connectivity
.ban_peer_until(node_id.clone(), ban_duration, ban_reason.clone())
.await
{
Ok(_) => {
warn!(target: LOG_TARGET, "Banned sync peer {node_id} for {ban_duration:?} because {ban_reason}")
},
Err(err) => error!(target: LOG_TARGET, "Failed to ban sync peer {node_id}: {err}"),
}
}
}