Skip to content

Commit 7b238b5

Browse files
committed
chore: address PR feedback and fix failing unit test
1 parent 96cf7d5 commit 7b238b5

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

stackslib/src/net/stackerdb/sync.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,29 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
226226
if self.last_eviction_time + 60 < get_epoch_time_secs() {
227227
self.last_eviction_time = get_epoch_time_secs();
228228
if self.replicas.len() > 0 {
229-
eviction_index = Some(thread_rng().gen::<usize>() % self.replicas.len());
229+
eviction_index = Some(thread_rng().gen_range(0..self.replicas.len()));
230230
}
231231
}
232232

233-
let mut remove_naddr = None;
234-
for (i, naddr) in self.replicas.iter().enumerate() {
235-
if let Some(eviction_index) = eviction_index.as_ref() {
236-
if *eviction_index == i {
237-
debug!(
238-
"{:?}: {}: don't reuse connection for replica {:?}",
239-
network.get_local_peer(),
240-
&self.smart_contract_id,
241-
&naddr,
242-
);
243-
remove_naddr = Some(naddr.clone());
244-
continue;
245-
}
233+
let remove_naddr = eviction_index.and_then(|idx| {
234+
let removed = self.replicas.iter().nth(idx).cloned();
235+
if let Some(naddr) = removed.as_ref() {
236+
debug!(
237+
"{:?}: {}: don't reuse connection for replica {:?}",
238+
network.get_local_peer(),
239+
&self.smart_contract_id,
240+
&naddr,
241+
);
246242
}
243+
removed
244+
});
245+
246+
if let Some(naddr) = remove_naddr {
247+
self.replicas.remove(&naddr);
248+
}
249+
250+
// retain the remaining replica connections
251+
for naddr in self.replicas.iter() {
247252
if let Some(event_id) = network.get_event_id(&naddr.to_neighbor_key(network)) {
248253
self.comms.pin_connection(event_id);
249254
debug!(
@@ -255,9 +260,6 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
255260
);
256261
}
257262
}
258-
if let Some(naddr) = remove_naddr.take() {
259-
self.replicas.remove(&naddr);
260-
}
261263
}
262264

263265
// reload from config

stackslib/src/net/stackerdb/tests/sync.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,14 @@ fn test_reconnect(network: &mut PeerNetwork) {
199199
.expect("FATAL: did not replace stacker dbs");
200200

201201
for (_sc, stacker_db_sync) in stacker_db_syncs.iter_mut() {
202-
stacker_db_sync.connect_begin(network).unwrap();
202+
match stacker_db_sync.connect_begin(network) {
203+
Ok(x) => {}
204+
Err(net_error::PeerNotConnected) => {}
205+
Err(net_error::NoSuchNeighbor) => {}
206+
Err(e) => {
207+
panic!("Failed to connect_begin: {:?}", &e);
208+
}
209+
}
203210
}
204211

205212
network.stacker_db_syncs = Some(stacker_db_syncs);

0 commit comments

Comments
 (0)