Skip to content

Commit d596c1a

Browse files
committed
refactor: avoid double hash
1 parent d683a58 commit d596c1a

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

crates/dkg/examples/sync.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ impl ClusterInfo {
155155

156156
fn peer_label(&self, peer_id: &PeerId) -> String {
157157
match self.indices.get(peer_id) {
158-
Some(index) => format!(
159-
"node={} peer_id={peer_id}",
160-
index.checked_add(1).unwrap_or(*index)
161-
),
158+
Some(index) => format!("node={} peer_id={peer_id}", index.saturating_add(1)),
162159
None => format!("peer_id={peer_id}"),
163160
}
164161
}

crates/dkg/src/sync/handler.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ impl ConnectionHandler for Handler {
286286
self.outbound = OutboundState::Running(run_outbound_stream(client, stream).boxed());
287287
}
288288
ConnectionEvent::DialUpgradeError(error) => self.on_dial_upgrade_error(error),
289-
ConnectionEvent::AddressChange(_)
290-
| ConnectionEvent::LocalProtocolsChange(_)
291-
| ConnectionEvent::RemoteProtocolsChange(_) => {}
292-
ConnectionEvent::ListenUpgradeError(_) => {}
293289
_ => {}
294290
}
295291
}

crates/dkg/src/sync/server.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,12 @@ impl Server {
211211
}
212212

213213
pub(crate) async fn update_step(&self, peer_id: PeerId, step: i64) -> Result<bool> {
214+
use std::collections::hash_map::Entry;
215+
214216
let mut state = self.inner.state.write().await;
215-
match state.steps.get(&peer_id).copied() {
216-
Some(current) => {
217+
match state.steps.entry(peer_id) {
218+
Entry::Occupied(mut entry) => {
219+
let current = *entry.get();
217220
if step < current {
218221
return Err(Error::PeerStepBehind);
219222
}
@@ -226,14 +229,18 @@ impl Server {
226229
if step == current {
227230
return Ok(false);
228231
}
232+
233+
entry.insert(step);
229234
}
230-
None if !(0..=1).contains(&step) => {
231-
return Err(Error::AbnormalInitialStep);
235+
Entry::Vacant(entry) => {
236+
if !(0..=1).contains(&step) {
237+
return Err(Error::AbnormalInitialStep);
238+
}
239+
240+
entry.insert(step);
232241
}
233-
None => {}
234242
}
235243

236-
state.steps.insert(peer_id, step);
237244
self.inner.notify.notify_waiters();
238245
Ok(true)
239246
}

0 commit comments

Comments
 (0)