Skip to content

Commit

Permalink
feat: remove whitelist saving (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler authored Dec 13, 2024
1 parent 26085b4 commit fe797c7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 88 deletions.
48 changes: 5 additions & 43 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ pub(crate) struct Config {
pub is_seed_peer: bool,
pub debug_print_chain: bool,
pub sync_job_enabled: bool,
pub peer_list_folder: PathBuf,
pub sha3x_enabled: bool,
pub randomx_enabled: bool,
pub num_concurrent_syncs: usize,
Expand All @@ -182,7 +181,6 @@ impl Default for Config {
seed_peers: vec![],
peer_info_publish_interval: Duration::from_secs(60 * 15),
stable_peer: true,
peer_list_folder: PathBuf::from("."),
private_key_folder: PathBuf::from("."),
private_key: None,
mdns_enabled: false,
Expand Down Expand Up @@ -2072,9 +2070,6 @@ where S: ShareChain
let mut chain_height_exchange_interval = tokio::time::interval(self.config.chain_height_exchange_interval);
chain_height_exchange_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);

let mut whitelist_save_interval = tokio::time::interval(Duration::from_secs(60));
whitelist_save_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);

let mut connection_stats_publish = tokio::time::interval(Duration::from_secs(10));
connection_stats_publish.set_missed_tick_behavior(MissedTickBehavior::Skip);

Expand All @@ -2094,7 +2089,6 @@ where S: ShareChain
tokio::pin!(grey_list_clear_interval);
tokio::pin!(black_list_clear_interval);
tokio::pin!(chain_height_exchange_interval);
tokio::pin!(whitelist_save_interval);
tokio::pin!(connection_stats_publish);
tokio::pin!(seek_connections_interval);

Expand Down Expand Up @@ -2126,19 +2120,18 @@ where S: ShareChain
let info = self.swarm.network_info();
let counters = info.connection_counters();

if (counters.num_established_outgoing() + counters.num_pending_outgoing()) > 20 {
let num_connections = counters.num_established_incoming() + counters.num_established_outgoing();
if num_connections > 20 {
continue;
}
let mut num_dialed = 0;
let store_read_lock = self.network_peer_store.read().await;
// Rather try and search good peers rather than randomly dialing
// 1000 peers will take a long time to get through
for record in store_read_lock.whitelist_peers().values() {
// dbg!("Connecting");
// dbg!("Dialing peer: {:?} on {:?}", record.peer_id, record.peer_info.public_addresses());
// let peer_id = peers.0;
// Only dial seed peers if we have 0 connections
if !self.swarm.is_connected(&record.peer_id)
&& !store_read_lock.is_seed_peer(&record.peer_id) {
&& (num_connections == 0 || !store_read_lock.is_seed_peer(&record.peer_id)) {
let _unused = self.swarm.dial(record.peer_id);
num_dialed += 1;
// We can only do 30 connections
Expand Down Expand Up @@ -2224,21 +2217,7 @@ where S: ShareChain
warn!(target: LOG_TARGET, "Chain height exchange took too long: {:?}", timer.elapsed());
}
},
_ = whitelist_save_interval.tick() => {
let timer = Instant::now();
match self.network_peer_store.write().await.save_whitelist(&self.config.peer_list_folder.join("whitelist_peers.json")).await{
Ok(_) => {
info!(target: LOG_TARGET, "Whitelist saved");
},
Err(e) => {
error!(target: LOG_TARGET, "Failed to save whitelist: {e:?}");
}
}
if timer.elapsed() > MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT {
warn!(target: LOG_TARGET, "Saving whitelist took too long: {:?}", timer.elapsed());
}
},
_ = grey_list_clear_interval.tick() => {
_ = grey_list_clear_interval.tick() => {
let timer = Instant::now();
self.network_peer_store.write().await.clear_grey_list();
if timer.elapsed() > MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT {
Expand Down Expand Up @@ -2500,23 +2479,6 @@ where S: ShareChain

warn!(target: LOG_TARGET, "Starting main loop");

let _unused = self
.network_peer_store
.write()
.await
.load_whitelist(&self.config.peer_list_folder.join("whitelist_peers.json"))
.await
.inspect_err(|e| warn!(target: LOG_TARGET, "Failed to load whitelist: {e:?}"));

for record in self.network_peer_store.read().await.whitelist_peers().values() {
for address in record.peer_info.public_addresses() {
// self.swarm
// .behaviour_mut()
// .kademlia
// .add_address(&record.peer_id, address.clone());
self.swarm.add_peer_address(record.peer_id, address);
}
}
self.main_loop().await?;
info!(target: LOG_TARGET,"P2P service has been stopped!");
Ok(())
Expand Down
46 changes: 1 addition & 45 deletions src/server/p2p/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@

use std::{
collections::{HashMap, HashSet},
fs::File,
io::{BufReader, Write},
path::Path,
str::FromStr,
time::Instant,
};

use anyhow::Error;
use libp2p::PeerId;
use log::warn;
use tari_core::proof_of_work::PowAlgorithm;
use tari_utilities::epoch_time::EpochTime;

use crate::server::{http::stats_collector::StatsBroadcastClient, p2p::messages::PeerInfo, PROTOCOL_VERSION};
use crate::server::{http::stats_collector::StatsBroadcastClient, p2p::messages::PeerInfo};

const LOG_TARGET: &str = "tari::p2pool::peer_store";
// const PEER_BAN_TIME: Duration = Duration::from_secs(60 * 5);
Expand Down Expand Up @@ -51,11 +47,6 @@ impl PeerStoreRecord {
}
}

pub fn with_timestamp(mut self, timestamp: u64) -> Self {
self.peer_info.timestamp = timestamp;
self
}

pub fn last_seen(&self) -> EpochTime {
self.last_ping
.unwrap_or_else(|| EpochTime::from(self.peer_info.timestamp))
Expand Down Expand Up @@ -315,41 +306,6 @@ impl PeerStore {
AddPeerStatus::NewPeer
}

pub async fn save_whitelist(&self, path: &Path) -> Result<(), Error> {
let mut file = File::create(path)?;
let whitelist = self
.whitelist_peers
.iter()
.map(|(peer_id, record)| (peer_id.clone(), record.peer_info.clone()))
.collect::<HashMap<String, PeerInfo>>();
let json = serde_json::to_string(&whitelist)?;
file.write_all(json.as_bytes())?;
Ok(())
}

pub async fn load_whitelist(&mut self, path: &Path) -> Result<(), Error> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let whitelist: HashMap<String, PeerInfo> = serde_json::from_reader(reader)?;
self.whitelist_peers = whitelist
.iter()
.filter_map(|(peer_id, peer_info)| {
if let Ok(p) = PeerId::from_str(peer_id) {
if peer_info.version < PROTOCOL_VERSION {
return None;
}
Some((
peer_id.clone(),
PeerStoreRecord::new(p, peer_info.clone()).with_timestamp(EpochTime::now().as_u64()),
))
} else {
None
}
})
.collect();
Ok(())
}

pub fn clear_grey_list(&mut self) {
for (peer_id, record) in self.greylist_peers.drain() {
if record.num_grey_listings >= MAX_GREY_LISTINGS {
Expand Down

0 comments on commit fe797c7

Please sign in to comment.