From d7da4c0f2c15af0f8c9ba297025e7d557b12b7ce Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Sat, 14 May 2022 17:58:15 -0600 Subject: [PATCH] Refactor relay and builder concerns from boost --- .gitignore | 3 +- Cargo.toml | 40 +++---------------- LICENSE-APACHE | 2 + Cargo.lock => mev-boost-rs/Cargo.lock | 0 mev-boost-rs/Cargo.toml | 35 ++++++++++++++++ mev-boost-rs/LICENSE-APACHE | 1 + mev-boost-rs/LICENSE-MIT | 1 + README.md => mev-boost-rs/README.md | 0 {src => mev-boost-rs/src}/bin/main.rs | 0 mev-boost-rs/src/lib.rs | 4 ++ {src => mev-boost-rs/src}/relay_mux.rs | 12 +++--- {src => mev-boost-rs/src}/service.rs | 4 +- test.sh => mev-boost-rs/test.sh | 0 {tests => mev-boost-rs/tests}/integration.rs | 7 ++-- mev-build-rs/Cargo.toml | 23 +++++++++++ mev-build-rs/LICENSE-APACHE | 1 + mev-build-rs/LICENSE-MIT | 1 + {src => mev-build-rs/src}/builder.rs | 19 +++++++-- .../src}/builder_api_server.rs | 0 mev-build-rs/src/lib.rs | 11 +++++ {src => mev-build-rs/src}/serde.rs | 0 mev-build-rs/src/types.rs | 30 ++++++++++++++ mev-relay-rs/Cargo.toml | 19 +++++++++ mev-relay-rs/LICENSE-APACHE | 1 + mev-relay-rs/LICENSE-MIT | 1 + src/relay.rs => mev-relay-rs/src/client.rs | 24 ++++------- mev-relay-rs/src/lib.rs | 5 +++ .../src/relay.rs | 15 +++---- src/lib.rs | 14 ------- src/types.rs | 29 -------------- 30 files changed, 185 insertions(+), 117 deletions(-) rename Cargo.lock => mev-boost-rs/Cargo.lock (100%) create mode 100644 mev-boost-rs/Cargo.toml create mode 120000 mev-boost-rs/LICENSE-APACHE create mode 120000 mev-boost-rs/LICENSE-MIT rename README.md => mev-boost-rs/README.md (100%) rename {src => mev-boost-rs/src}/bin/main.rs (100%) create mode 100644 mev-boost-rs/src/lib.rs rename {src => mev-boost-rs/src}/relay_mux.rs (95%) rename {src => mev-boost-rs/src}/service.rs (94%) rename test.sh => mev-boost-rs/test.sh (100%) rename {tests => mev-boost-rs/tests}/integration.rs (96%) create mode 100644 mev-build-rs/Cargo.toml create mode 120000 mev-build-rs/LICENSE-APACHE create mode 120000 mev-build-rs/LICENSE-MIT rename {src => mev-build-rs/src}/builder.rs (59%) rename {src => mev-build-rs/src}/builder_api_server.rs (100%) create mode 100644 mev-build-rs/src/lib.rs rename {src => mev-build-rs/src}/serde.rs (100%) create mode 100644 mev-build-rs/src/types.rs create mode 100644 mev-relay-rs/Cargo.toml create mode 120000 mev-relay-rs/LICENSE-APACHE create mode 120000 mev-relay-rs/LICENSE-MIT rename src/relay.rs => mev-relay-rs/src/client.rs (77%) create mode 100644 mev-relay-rs/src/lib.rs rename src/relay_server.rs => mev-relay-rs/src/relay.rs (94%) delete mode 100644 src/lib.rs delete mode 100644 src/types.rs diff --git a/.gitignore b/.gitignore index ea8c4bf7..a7fd80cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target +target +/Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index e5910df3..fa5b7fc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,35 +1,7 @@ -[package] -name = "mev-boost-rs" -version = "0.1.0" -edition = "2021" -license = "MIT OR Apache-2.0" -rust-version = "1.60.0" +[workspace] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -tokio = { version = "1.0", features = ["full"] } -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } -futures = "0.3.21" -async-trait = "0.1.53" - -axum = "0.5.4" -url = { version = "2.2.2", default-features = false } - -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0.81" - -thiserror = "1.0.30" - -clap = { version = "3.1.14", features = ["derive"] } - -ethereum_consensus = { git = "https://github.com/ralexstokes/ethereum_consensus" } -ssz_rs = { git = "https://github.com/ralexstokes/ssz_rs" } -beacon-api-client = { git = "https://github.com/ralexstokes/beacon-api-client" } - -[dev-dependencies] -rand = "0.8.5" - -[[bin]] -name = "main" +members = [ + "mev-boost-rs", + "mev-relay-rs", + "mev-build-rs", +] \ No newline at end of file diff --git a/LICENSE-APACHE b/LICENSE-APACHE index 261eeb9e..59c0a30d 100644 --- a/LICENSE-APACHE +++ b/LICENSE-APACHE @@ -1,3 +1,5 @@ +Copyright (c) 2022 Alex Stokes + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ diff --git a/Cargo.lock b/mev-boost-rs/Cargo.lock similarity index 100% rename from Cargo.lock rename to mev-boost-rs/Cargo.lock diff --git a/mev-boost-rs/Cargo.toml b/mev-boost-rs/Cargo.toml new file mode 100644 index 00000000..2b15fecb --- /dev/null +++ b/mev-boost-rs/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "mev-boost-rs" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +rust-version = "1.60.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } +futures = "0.3.21" +async-trait = "0.1.53" + +url = { version = "2.2.2", default-features = false } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0.81" + +thiserror = "1.0.30" + +clap = { version = "3.1.14", features = ["derive"] } + +ethereum_consensus = { git = "https://github.com/ralexstokes/ethereum_consensus" } +beacon-api-client = { git = "https://github.com/ralexstokes/beacon-api-client" } + +mev-build-rs = { path = "../mev-build-rs" } +mev-relay-rs = { path = "../mev-relay-rs" } + +[dev-dependencies] +rand = "0.8.5" + +[[bin]] +name = "main" diff --git a/mev-boost-rs/LICENSE-APACHE b/mev-boost-rs/LICENSE-APACHE new file mode 120000 index 00000000..965b606f --- /dev/null +++ b/mev-boost-rs/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE \ No newline at end of file diff --git a/mev-boost-rs/LICENSE-MIT b/mev-boost-rs/LICENSE-MIT new file mode 120000 index 00000000..76219eb7 --- /dev/null +++ b/mev-boost-rs/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT \ No newline at end of file diff --git a/README.md b/mev-boost-rs/README.md similarity index 100% rename from README.md rename to mev-boost-rs/README.md diff --git a/src/bin/main.rs b/mev-boost-rs/src/bin/main.rs similarity index 100% rename from src/bin/main.rs rename to mev-boost-rs/src/bin/main.rs diff --git a/mev-boost-rs/src/lib.rs b/mev-boost-rs/src/lib.rs new file mode 100644 index 00000000..ef026186 --- /dev/null +++ b/mev-boost-rs/src/lib.rs @@ -0,0 +1,4 @@ +mod relay_mux; +mod service; + +pub use service::{Service, ServiceConfig}; diff --git a/src/relay_mux.rs b/mev-boost-rs/src/relay_mux.rs similarity index 95% rename from src/relay_mux.rs rename to mev-boost-rs/src/relay_mux.rs index 67ab861a..bd251c3f 100644 --- a/src/relay_mux.rs +++ b/mev-boost-rs/src/relay_mux.rs @@ -1,12 +1,12 @@ -use crate::builder::{Builder, Error as BuilderError}; -use crate::relay::{Error as RelayError, Relay}; -use crate::types::{ - BidRequest, ExecutionPayload, Hash32, SignedBlindedBeaconBlock, SignedBuilderBid, - SignedValidatorRegistration, Slot, -}; use async_trait::async_trait; use beacon_api_client::Error as ApiError; +use ethereum_consensus::primitives::{Hash32, Slot}; use futures::future::join_all; +use mev_build_rs::{ + BidRequest, Builder, Error as BuilderError, ExecutionPayload, SignedBlindedBeaconBlock, + SignedBuilderBid, SignedValidatorRegistration, +}; +use mev_relay_rs::{Client as Relay, ClientError as RelayError}; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::time::Duration; diff --git a/src/service.rs b/mev-boost-rs/src/service.rs similarity index 94% rename from src/service.rs rename to mev-boost-rs/src/service.rs index 30f7b39a..c77c8e26 100644 --- a/src/service.rs +++ b/mev-boost-rs/src/service.rs @@ -1,8 +1,8 @@ -use crate::builder_api_server::Server as ApiServer; -use crate::relay::Relay; use crate::relay_mux::RelayMux; use beacon_api_client::Client; use futures::future::join_all; +use mev_build_rs::ApiServer; +use mev_relay_rs::Client as Relay; use std::net::Ipv4Addr; use url::Url; diff --git a/test.sh b/mev-boost-rs/test.sh similarity index 100% rename from test.sh rename to mev-boost-rs/test.sh diff --git a/tests/integration.rs b/mev-boost-rs/tests/integration.rs similarity index 96% rename from tests/integration.rs rename to mev-boost-rs/tests/integration.rs index 5db6d395..eb85b52d 100644 --- a/tests/integration.rs +++ b/mev-boost-rs/tests/integration.rs @@ -6,10 +6,9 @@ use ethereum_consensus::builder::{SignedValidatorRegistration, ValidatorRegistra use ethereum_consensus::crypto::SecretKey; use ethereum_consensus::phase0::mainnet::Validator; use ethereum_consensus::primitives::{ExecutionAddress, Hash32, Slot}; -use mev_boost_rs::{ - builder::Builder, builder_api_server::Server as ApiServer, relay_server::Relay, BidRequest, - Relay as RelayClient, Service, ServiceConfig, -}; +use mev_boost_rs::{Service, ServiceConfig}; +use mev_build_rs::{ApiServer, BidRequest, Builder}; +use mev_relay_rs::{Client as RelayClient, Relay}; use rand; use rand::seq::SliceRandom; use url::Url; diff --git a/mev-build-rs/Cargo.toml b/mev-build-rs/Cargo.toml new file mode 100644 index 00000000..9504ceb6 --- /dev/null +++ b/mev-build-rs/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "mev-build-rs" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +rust-version = "1.60.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +default = ["serde", "api"] +api = ["axum", "beacon-api-client", "tracing"] + +[dependencies] +axum = { version = "0.5.4", optional = true } +tracing = { version = "0.1", optional = true } +async-trait = "0.1.53" +serde = { version = "1.0", features = ["derive"], optional = true } +thiserror = "1.0.30" + +ethereum_consensus = { git = "https://github.com/ralexstokes/ethereum_consensus" } +ssz_rs = { git = "https://github.com/ralexstokes/ssz_rs" } +beacon-api-client = { git = "https://github.com/ralexstokes/beacon-api-client", optional = true } diff --git a/mev-build-rs/LICENSE-APACHE b/mev-build-rs/LICENSE-APACHE new file mode 120000 index 00000000..965b606f --- /dev/null +++ b/mev-build-rs/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE \ No newline at end of file diff --git a/mev-build-rs/LICENSE-MIT b/mev-build-rs/LICENSE-MIT new file mode 120000 index 00000000..76219eb7 --- /dev/null +++ b/mev-build-rs/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT \ No newline at end of file diff --git a/src/builder.rs b/mev-build-rs/src/builder.rs similarity index 59% rename from src/builder.rs rename to mev-build-rs/src/builder.rs index 66d147b5..44fe14ec 100644 --- a/src/builder.rs +++ b/mev-build-rs/src/builder.rs @@ -1,8 +1,11 @@ -use crate::types::{BidRequest, SignedBuilderBid}; +use crate::types::{ + BidRequest, ExecutionPayload, SignedBlindedBeaconBlock, SignedBuilderBid, + SignedValidatorRegistration, +}; use async_trait::async_trait; use beacon_api_client::ApiError; -use ethereum_consensus::bellatrix::mainnet::{ExecutionPayload, SignedBlindedBeaconBlock}; -use ethereum_consensus::builder::SignedValidatorRegistration; +#[cfg(feature = "api")] +use beacon_api_client::Error as ApiClientError; use thiserror::Error; #[derive(Debug, Error)] @@ -15,6 +18,16 @@ pub enum Error { Custom(String), } +#[cfg(feature = "api")] +impl From for Error { + fn from(err: ApiClientError) -> Self { + match err { + ApiClientError::Api(err) => err.into(), + err => Error::Internal(err.to_string()), + } + } +} + #[async_trait] pub trait Builder { async fn register_validator( diff --git a/src/builder_api_server.rs b/mev-build-rs/src/builder_api_server.rs similarity index 100% rename from src/builder_api_server.rs rename to mev-build-rs/src/builder_api_server.rs diff --git a/mev-build-rs/src/lib.rs b/mev-build-rs/src/lib.rs new file mode 100644 index 00000000..f24df857 --- /dev/null +++ b/mev-build-rs/src/lib.rs @@ -0,0 +1,11 @@ +mod builder; +#[cfg(feature = "api")] +mod builder_api_server; +#[cfg(feature = "serde")] +mod serde; +mod types; + +pub use builder::{Builder, Error}; +#[cfg(feature = "api")] +pub use builder_api_server::Server as ApiServer; +pub use types::*; diff --git a/src/serde.rs b/mev-build-rs/src/serde.rs similarity index 100% rename from src/serde.rs rename to mev-build-rs/src/serde.rs diff --git a/mev-build-rs/src/types.rs b/mev-build-rs/src/types.rs new file mode 100644 index 00000000..1aaaa1ad --- /dev/null +++ b/mev-build-rs/src/types.rs @@ -0,0 +1,30 @@ +pub use ethereum_consensus::bellatrix::mainnet::{ + ExecutionPayload, ExecutionPayloadHeader, SignedBlindedBeaconBlock, +}; +pub use ethereum_consensus::builder::SignedValidatorRegistration; +use ethereum_consensus::primitives::{BlsPublicKey, BlsSignature, Hash32, Slot}; +use ssz_rs::prelude::*; + +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BidRequest { + #[serde(with = "crate::serde::as_string")] + pub slot: Slot, + pub parent_hash: Hash32, + pub public_key: BlsPublicKey, +} + +#[derive(Debug, Default, SimpleSerialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BuilderBid { + pub header: ExecutionPayloadHeader, + pub value: U256, + pub public_key: BlsPublicKey, +} + +#[derive(Debug, Default, SimpleSerialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct SignedBuilderBid { + pub message: BuilderBid, + pub signature: BlsSignature, +} diff --git a/mev-relay-rs/Cargo.toml b/mev-relay-rs/Cargo.toml new file mode 100644 index 00000000..72f4eb23 --- /dev/null +++ b/mev-relay-rs/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "mev-relay-rs" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +rust-version = "1.60.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-trait = "0.1.53" +thiserror = "1.0.30" +http = "0.2.7" + +ethereum_consensus = { git = "https://github.com/ralexstokes/ethereum_consensus" } +beacon-api-client = { git = "https://github.com/ralexstokes/beacon-api-client" } +ssz_rs = { git = "https://github.com/ralexstokes/ssz_rs" } + +mev-build-rs = { path = "../mev-build-rs"} \ No newline at end of file diff --git a/mev-relay-rs/LICENSE-APACHE b/mev-relay-rs/LICENSE-APACHE new file mode 120000 index 00000000..965b606f --- /dev/null +++ b/mev-relay-rs/LICENSE-APACHE @@ -0,0 +1 @@ +../LICENSE-APACHE \ No newline at end of file diff --git a/mev-relay-rs/LICENSE-MIT b/mev-relay-rs/LICENSE-MIT new file mode 120000 index 00000000..76219eb7 --- /dev/null +++ b/mev-relay-rs/LICENSE-MIT @@ -0,0 +1 @@ +../LICENSE-MIT \ No newline at end of file diff --git a/src/relay.rs b/mev-relay-rs/src/client.rs similarity index 77% rename from src/relay.rs rename to mev-relay-rs/src/client.rs index 1fd712be..820a11b7 100644 --- a/src/relay.rs +++ b/mev-relay-rs/src/client.rs @@ -1,27 +1,17 @@ -use crate::builder::{Builder, Error as BuilderError}; -use crate::types::{ - BidRequest, ExecutionPayload, SignedBlindedBeaconBlock, SignedBuilderBid, - SignedValidatorRegistration, -}; use async_trait::async_trait; use beacon_api_client::{api_error_or_ok, Client as BeaconApiClient, VersionedValue}; +use mev_build_rs::{ + BidRequest, Builder, Error as BuilderError, ExecutionPayload, SignedBlindedBeaconBlock, + SignedBuilderBid, SignedValidatorRegistration, +}; pub type Error = beacon_api_client::Error; -impl From for BuilderError { - fn from(err: Error) -> BuilderError { - match err { - Error::Api(err) => err.into(), - err => BuilderError::Internal(err.to_string()), - } - } -} - -pub struct Relay { +pub struct Client { api: BeaconApiClient, } -impl Relay { +impl Client { pub fn new(api_client: BeaconApiClient) -> Self { Self { api: api_client } } @@ -33,7 +23,7 @@ impl Relay { } #[async_trait] -impl Builder for Relay { +impl Builder for Client { async fn register_validator( &self, registration: &SignedValidatorRegistration, diff --git a/mev-relay-rs/src/lib.rs b/mev-relay-rs/src/lib.rs new file mode 100644 index 00000000..c92f0629 --- /dev/null +++ b/mev-relay-rs/src/lib.rs @@ -0,0 +1,5 @@ +mod client; +mod relay; + +pub use client::{Client, Error as ClientError}; +pub use relay::Relay; diff --git a/src/relay_server.rs b/mev-relay-rs/src/relay.rs similarity index 94% rename from src/relay_server.rs rename to mev-relay-rs/src/relay.rs index a218b3e0..0b0f3cf9 100644 --- a/src/relay_server.rs +++ b/mev-relay-rs/src/relay.rs @@ -1,12 +1,13 @@ -use crate::builder::{Builder, Error as BuilderError}; -use crate::types::{ - BidRequest, BlsPublicKey, BuilderBid, ExecutionAddress, ExecutionPayload, - ExecutionPayloadHeader, SignedBlindedBeaconBlock, SignedBuilderBid, - SignedValidatorRegistration, U256, -}; use async_trait::async_trait; -use axum::http::StatusCode; use beacon_api_client::ApiError; +use ethereum_consensus::primitives::{BlsPublicKey, ExecutionAddress}; +use http::StatusCode; +use mev_build_rs::{ + BidRequest, Builder, BuilderBid, Error as BuilderError, ExecutionPayload, + ExecutionPayloadHeader, SignedBlindedBeaconBlock, SignedBuilderBid, + SignedValidatorRegistration, +}; +use ssz_rs::prelude::U256; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use thiserror::Error; diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 4765b25d..00000000 --- a/src/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -pub mod builder; -pub mod builder_api_server; -mod relay; -mod relay_mux; -mod serde; -mod service; -mod types; - -pub use relay::Relay; -pub use service::{Service, ServiceConfig}; -pub use types::BidRequest; - -// temp mock for testing -pub mod relay_server; diff --git a/src/types.rs b/src/types.rs deleted file mode 100644 index 9fb05835..00000000 --- a/src/types.rs +++ /dev/null @@ -1,29 +0,0 @@ -pub(crate) use ethereum_consensus::bellatrix::mainnet::{ - ExecutionPayload, ExecutionPayloadHeader, SignedBlindedBeaconBlock, -}; -pub(crate) use ethereum_consensus::builder::SignedValidatorRegistration; -pub(crate) use ethereum_consensus::primitives::BlsPublicKey; -use ethereum_consensus::primitives::BlsSignature; -pub(crate) use ethereum_consensus::primitives::{ExecutionAddress, Hash32, Slot}; -pub(crate) use ssz_rs::prelude::*; - -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)] -pub struct BidRequest { - #[serde(with = "crate::serde::as_string")] - pub slot: Slot, - pub parent_hash: Hash32, - pub public_key: BlsPublicKey, -} - -#[derive(Debug, Default, serde::Serialize, serde::Deserialize)] -pub struct BuilderBid { - pub header: ExecutionPayloadHeader, - pub value: U256, - pub public_key: BlsPublicKey, -} - -#[derive(Debug, Default, serde::Serialize, serde::Deserialize)] -pub struct SignedBuilderBid { - pub message: BuilderBid, - pub signature: BlsSignature, -}