Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove whitelist saving #228

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,9 +2072,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 +2091,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 +2122,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 +2219,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 +2481,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
23 changes: 0 additions & 23 deletions src/server/p2p/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,29 +327,6 @@ impl PeerStore {
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
Loading