Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mev-boost-rs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethereum_consensus::{
use futures::StreamExt;
use mev_rs::{
blinded_block_provider::Server as BlindedBlockProviderServer,
relay::{parse_relay_endpoints, Relay, RelayEndpoint},
relay::{Relay, RelayEndpoints},
Error,
};
use serde::Deserialize;
Expand All @@ -30,13 +30,13 @@ impl Default for Config {
pub struct Service {
host: Ipv4Addr,
port: u16,
relays: Vec<RelayEndpoint>,
relays: RelayEndpoints,
network: Network,
}

impl Service {
pub fn from(network: Network, config: Config) -> Self {
let relays = parse_relay_endpoints(&config.relays);
let relays = config.relays.into();

Self { host: config.host, port: config.port, relays, network }
}
Expand All @@ -50,7 +50,7 @@ impl Service {
} else {
let count = relays.len();
info!("configured with {count} relay(s)");
for relay in &relays {
for relay in relays.iter() {
info!(%relay, "configured with relay");
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Future for ServiceHandle {
let this = self.project();
let relay_mux = this.relay_mux.poll(cx);
if relay_mux.is_ready() {
return relay_mux
return relay_mux;
}
this.server.poll(cx)
}
Expand Down
12 changes: 6 additions & 6 deletions mev-build-rs/src/reth_builder/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethereum_consensus::{
};
use ethers::signers::{coins_bip39::English, MnemonicBuilder, Signer};
use futures::StreamExt;
use mev_rs::{relay::parse_relay_endpoints, Error, Relay};
use mev_rs::{relay::RelayEndpoints, Error, Relay};
use reth_primitives::{Bytes, ChainSpec};
use serde::Deserialize;
use std::{future::Future, pin::Pin, sync::Arc, task::Poll};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<
chain_spec: Arc<ChainSpec>,
) -> Result<(Self, Builder<Pool, Client>), Error> {
let secret_key = &config.secret_key;
let relays = parse_relay_endpoints(&config.relays)
let relays = RelayEndpoints::from(&config.relays)
.into_iter()
.map(|endpoint| Arc::new(Relay::from(endpoint)))
.collect::<Vec<_>>();
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<
Ok(stream) => stream,
Err(err) => {
tracing::error!(err = ?err, "could not open builds stream");
return
return;
}
};

Expand Down Expand Up @@ -175,7 +175,7 @@ impl<
Ok(stream) => stream,
Err(err) => {
tracing::error!(err = ?err, "could not open payload attributes stream");
return
return;
}
};

Expand Down Expand Up @@ -230,11 +230,11 @@ impl Future for ServiceHandle {

let clock = this.clock.poll(cx);
if clock.is_ready() {
return clock
return clock;
}
let builder = this.payload_builder.poll(cx);
if builder.is_ready() {
return builder
return builder;
}
this.bidder.poll(cx)
}
Expand Down
101 changes: 86 additions & 15 deletions mev-rs/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use ethereum_consensus::{
crypto::Error as CryptoError, primitives::BlsPublicKey, serde::try_bytes_from_hex_str,
};
use std::{cmp, fmt, hash, ops::Deref};
use tracing::{error, warn};
use url::Url;

#[derive(Clone, Debug)]
Expand All @@ -30,28 +29,86 @@ impl TryFrom<Url> for RelayEndpoint {
}
}

impl fmt::Display for RelayEndpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(self.url.as_str())
/// A wrapper around a vector of [`RelayEndpoint`]s.
#[derive(Clone, Debug)]
pub struct RelayEndpoints(Vec<RelayEndpoint>);

impl RelayEndpoints {
pub fn iter(&self) -> impl Iterator<Item = &RelayEndpoint> {
self.0.iter()
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl IntoIterator for RelayEndpoints {
type Item = RelayEndpoint;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<T> From<Vec<T>> for RelayEndpoints
where
T: AsRef<str>,
{
fn from(value: Vec<T>) -> Self {
Self::from(value.as_slice())
}
}

pub fn parse_relay_endpoints(relay_urls: &[String]) -> Vec<RelayEndpoint> {
let mut relays = vec![];
impl<T> From<&Vec<T>> for RelayEndpoints
where
T: AsRef<str>,
{
fn from(value: &Vec<T>) -> Self {
Self::from(value.as_slice())
}
}

for relay_url in relay_urls {
match relay_url.parse::<Url>() {
Ok(url) => match RelayEndpoint::try_from(url) {
impl<T> From<&[T]> for RelayEndpoints
where
T: AsRef<str>,
{
fn from(value: &[T]) -> Self {
let mut relays = vec![];
for endpoint in value {
let e = endpoint.as_ref();
let url = match Url::parse(e) {
Ok(url) => url,
Err(err) => {
tracing::error!(%err, %e, "error parsing relay URL from config");
continue;
}
};
match RelayEndpoint::try_from(url) {
Ok(relay) => relays.push(relay),
Err(err) => warn!(%err, %relay_url, "error parsing relay from URL"),
},
Err(err) => warn!(%err, %relay_url, "error parsing relay URL from config"),
Err(err) => {
tracing::warn!(%err, %e, "error parsing relay from URL")
}
}
}
if relays.is_empty() {
tracing::error!(
"no relays could be loaded from the configuration; please fix and restart"
);
}
RelayEndpoints(relays)
}
if relays.is_empty() {
error!("no relays could be loaded from the configuration; please fix and restart");
}

impl fmt::Display for RelayEndpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str(self.url.as_str())
}
relays
}

#[derive(Clone)]
Expand Down Expand Up @@ -126,6 +183,20 @@ mod tests {
const URL: &str = "https://relay.com";
const RELAY_URL: &str = "https://0x845bd072b7cd566f02faeb0a4033ce9399e42839ced64e8b2adcfc859ed1e8e1a5a293336a49feac6d9a5edb779be53a@boost-relay-sepolia.flashbots.net";

#[test]
fn test_relay_endpoints_from_vec() {
let endpoints = vec![URL, RELAY_URL];
let relay_endpoints = RelayEndpoints::from(endpoints);
assert_eq!(relay_endpoints.len(), 1);
}

#[test]
fn test_empty_relay_endpoints_from_vec() {
let endpoints = vec![URL];
let relay_endpoints = RelayEndpoints::from(endpoints);
assert_eq!(relay_endpoints.len(), 0);
}

#[test]
fn parse_relay_endpoint() {
let mut rng = rand::thread_rng();
Expand Down