Skip to content

Commit

Permalink
use strum
Browse files Browse the repository at this point in the history
  • Loading branch information
uvizhe committed Nov 13, 2023
1 parent 2701f73 commit 6c8b775
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions validator_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ malloc_utils = { workspace = true }
sysinfo = { workspace = true }
system_health = { path = "../common/system_health" }
logging = { workspace = true }
strum = { workspace = true }
19 changes: 3 additions & 16 deletions validator_client/src/beacon_node_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::fmt;
use std::fmt::Debug;
use std::future::Future;
use std::marker::PhantomData;
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use strum::{EnumString, EnumVariantNames};
use tokio::{sync::RwLock, time::sleep};
use types::{ChainSpec, Config, EthSpec};

Expand Down Expand Up @@ -714,24 +714,11 @@ impl<T: SlotClock, E: EthSpec> BeaconNodeFallback<T, E> {
}

/// Serves as a cue for `BeaconNodeFallback` to tell which requests need to be broadcasted.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, EnumString, EnumVariantNames)]
#[strum(serialize_all = "kebab-case")]
pub enum ApiTopic {
Attestations,
Blocks,
Subscriptions,
SyncCommittee,
}

impl FromStr for ApiTopic {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"attestations" => Ok(ApiTopic::Attestations),
"blocks" => Ok(ApiTopic::Blocks),
"subscriptions" => Ok(ApiTopic::Subscriptions),
"sync-committee" => Ok(ApiTopic::SyncCommittee),
_ => Err(format!("Unknown API topic: `{s}`")),
}
}
}
10 changes: 6 additions & 4 deletions validator_client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use slog::{info, Logger};
use std::fs;
use std::net::IpAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use types::{Address, GRAFFITI_BYTES_LEN};

Expand Down Expand Up @@ -227,9 +226,12 @@ impl Config {
config.broadcast_topics = broadcast_topics
.split(',')
.filter(|t| *t != "none")
.map(ApiTopic::from_str)
.collect::<Result<_, _>>()
.map_err(|e| format!("Unable to parse API topics to broadcast: {:?}", e))?;
.map(|t| {
t.trim()
.parse::<ApiTopic>()
.map_err(|_| format!("Unknown API topic to broadcast: {t}"))
})
.collect::<Result<_, _>>()?;
}

/*
Expand Down

0 comments on commit 6c8b775

Please sign in to comment.