mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Simplify enum -> str with strum (#2164)
## Issue Addressed NA ## Proposed Changes As per #2100, uses derives from the sturm library to implement AsRef<str> and AsStaticRef to easily get str values from enums without creating new Strings. Furthermore unifies all attestation error counter into one IntCounterVec vector. These works are originally by @blacktemplar, I've just created this PR so I can resolve some merge conflicts. ## Additional Info NA Co-authored-by: blacktemplar <blacktemplar@a1.net>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
use libp2p::identify::IdentifyInfo;
|
||||
use serde::Serialize;
|
||||
use strum::{AsRefStr, AsStaticStr};
|
||||
|
||||
/// Various client and protocol information related to a node.
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
@@ -20,7 +21,7 @@ pub struct Client {
|
||||
pub agent_string: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, PartialEq)]
|
||||
#[derive(Clone, Debug, Serialize, PartialEq, AsRefStr, AsStaticStr)]
|
||||
pub enum ClientKind {
|
||||
/// A lighthouse node (the best kind).
|
||||
Lighthouse,
|
||||
@@ -98,26 +99,6 @@ impl std::fmt::Display for Client {
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientKind {
|
||||
pub fn as_static_ref(&self) -> &'static str {
|
||||
use ClientKind::*;
|
||||
match self {
|
||||
Lighthouse => "Lighthouse",
|
||||
Nimbus => "Nimbus",
|
||||
Teku => "Teku",
|
||||
Prysm => "Prysm",
|
||||
Lodestar => "Lodestar",
|
||||
Unknown => "Unknown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for ClientKind {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.as_static_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ClientKind {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.as_ref())
|
||||
|
||||
@@ -158,8 +158,8 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::PEER_ACTION_EVENTS_PER_CLIENT,
|
||||
&[
|
||||
info.client.kind.as_static_ref(),
|
||||
PeerAction::Fatal.as_static_str(),
|
||||
info.client.kind.as_ref(),
|
||||
PeerAction::Fatal.as_ref(),
|
||||
source.into(),
|
||||
],
|
||||
);
|
||||
@@ -193,11 +193,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
|
||||
info.apply_peer_action_to_score(action);
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::PEER_ACTION_EVENTS_PER_CLIENT,
|
||||
&[
|
||||
info.client.kind.as_static_ref(),
|
||||
action.as_static_str(),
|
||||
source.into(),
|
||||
],
|
||||
&[info.client.kind.as_ref(), action.as_ref(), source.into()],
|
||||
);
|
||||
|
||||
Self::handle_score_transitions(
|
||||
@@ -407,9 +403,9 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
|
||||
metrics::inc_counter_vec(
|
||||
&metrics::TOTAL_RPC_ERRORS_PER_CLIENT,
|
||||
&[
|
||||
client.kind.as_static_ref(),
|
||||
client.kind.as_ref(),
|
||||
err.as_static_str(),
|
||||
direction.as_static_str(),
|
||||
direction.as_ref(),
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ use serde::{
|
||||
use std::collections::HashSet;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::time::Instant;
|
||||
use strum::AsRefStr;
|
||||
use types::{EthSpec, SubnetId};
|
||||
use PeerConnectionStatus::*;
|
||||
|
||||
@@ -320,21 +321,13 @@ impl Default for PeerStatus {
|
||||
}
|
||||
|
||||
/// Connection Direction of connection.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, AsRefStr)]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum ConnectionDirection {
|
||||
Incoming,
|
||||
Outgoing,
|
||||
}
|
||||
|
||||
impl ConnectionDirection {
|
||||
pub fn as_static_str(&self) -> &'static str {
|
||||
match self {
|
||||
ConnectionDirection::Incoming => "incoming",
|
||||
ConnectionDirection::Outgoing => "outgoing",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Connection Status of the peer.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PeerConnectionStatus {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
use crate::behaviour::GOSSIPSUB_GREYLIST_THRESHOLD;
|
||||
use serde::Serialize;
|
||||
use std::time::Instant;
|
||||
use strum::AsRefStr;
|
||||
use tokio::time::Duration;
|
||||
|
||||
lazy_static! {
|
||||
@@ -42,7 +43,8 @@ const GOSSIPSUB_POSITIVE_SCORE_WEIGHT: f64 = GOSSIPSUB_NEGATIVE_SCORE_WEIGHT;
|
||||
/// Each variant has an associated score change.
|
||||
// To easily assess the behaviour of scores changes the number of variants should stay low, and
|
||||
// somewhat generic.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, AsRefStr)]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum PeerAction {
|
||||
/// We should not communicate more with this peer.
|
||||
/// This action will cause the peer to get banned.
|
||||
@@ -94,17 +96,6 @@ impl std::fmt::Display for PeerAction {
|
||||
}
|
||||
}
|
||||
|
||||
impl PeerAction {
|
||||
pub fn as_static_str(&self) -> &'static str {
|
||||
match self {
|
||||
PeerAction::HighToleranceError => "high_tolerance",
|
||||
PeerAction::MidToleranceError => "mid_tolerance",
|
||||
PeerAction::LowToleranceError => "low_tolerance",
|
||||
PeerAction::Fatal => "fatal",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The expected state of the peer given the peer's score.
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub(crate) enum ScoreState {
|
||||
|
||||
Reference in New Issue
Block a user