Clean up sc-peerset (#9806)

* Clean up sc-peerset

* cargo +nightly fmt --all

* Nit

* Nit

* .

* Nit

* .

* Apply suggestions from code review

* .

* Update client/peerset/src/peersstate.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Liu-Cheng Xu
2021-10-01 17:43:55 +08:00
committed by GitHub
parent f8ce186496
commit 00973f5b62
6 changed files with 62 additions and 72 deletions
+5 -5
View File
@@ -155,14 +155,14 @@ pub enum Role {
}
impl Role {
/// True for `Role::Authority`
/// True for [`Role::Authority`].
pub fn is_authority(&self) -> bool {
matches!(self, Role::Authority { .. })
matches!(self, Self::Authority { .. })
}
/// True for `Role::Light`
/// True for [`Role::Light`].
pub fn is_light(&self) -> bool {
matches!(self, Role::Light { .. })
matches!(self, Self::Light { .. })
}
}
@@ -329,7 +329,7 @@ impl FromStr for MultiaddrWithPeerId {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (peer_id, multiaddr) = parse_str_addr(s)?;
Ok(MultiaddrWithPeerId { peer_id, multiaddr })
Ok(Self { peer_id, multiaddr })
}
}
@@ -417,7 +417,7 @@ impl Notifications {
/// Returns true if we have an open substream to the given peer.
pub fn is_open(&self, peer_id: &PeerId, set_id: sc_peerset::SetId) -> bool {
self.peers.get(&(peer_id.clone(), set_id)).map(|p| p.is_open()).unwrap_or(false)
self.peers.get(&(*peer_id, set_id)).map(|p| p.is_open()).unwrap_or(false)
}
/// Disconnects the given peer if we are connected to it.
@@ -1777,7 +1777,7 @@ impl NetworkBehaviour for Notifications {
"Handler({}, {:?}) => CloseResult({:?})",
source, connection, set_id);
match self.peers.get_mut(&(source.clone(), set_id)) {
match self.peers.get_mut(&(source, set_id)) {
// Move the connection from `Closing` to `Closed`.
Some(PeerState::Incoming { connections, .. }) |
Some(PeerState::DisabledPendingEnable { connections, .. }) |
+7 -10
View File
@@ -92,7 +92,7 @@ struct Metrics {
impl Metrics {
fn register(r: &Registry) -> Result<Self, PrometheusError> {
Ok(Metrics {
Ok(Self {
propagated_transactions: register(
Counter::new(
"sync_propagated_transactions",
@@ -133,7 +133,7 @@ pub struct TransactionsHandlerPrototype {
impl TransactionsHandlerPrototype {
/// Create a new instance.
pub fn new(protocol_id: ProtocolId) -> Self {
TransactionsHandlerPrototype {
Self {
protocol_name: Cow::from({
let mut proto = String::new();
proto.push_str("/");
@@ -401,7 +401,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
let hash = self.transaction_pool.hash_of(&t);
peer.known_transactions.insert(hash.clone());
self.service.report_peer(who.clone(), rep::ANY_TRANSACTION);
self.service.report_peer(who, rep::ANY_TRANSACTION);
match self.pending_transactions_peers.entry(hash.clone()) {
Entry::Vacant(entry) => {
@@ -409,10 +409,10 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
validation: self.transaction_pool.import(t),
tx_hash: hash,
});
entry.insert(vec![who.clone()]);
entry.insert(vec![who]);
},
Entry::Occupied(mut entry) => {
entry.get_mut().push(who.clone());
entry.get_mut().push(who);
},
}
}
@@ -468,11 +468,8 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
propagated_to.entry(hash).or_default().push(who.to_base58());
}
trace!(target: "sync", "Sending {} transactions to {}", to_send.len(), who);
self.service.write_notification(
who.clone(),
self.protocol_name.clone(),
to_send.encode(),
);
self.service
.write_notification(*who, self.protocol_name.clone(), to_send.encode());
}
}