-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement client/server for relay specs
- Loading branch information
1 parent
a591927
commit e738668
Showing
10 changed files
with
214 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::{ | ||
blinded_block_relayer::BlindedBlockRelayer, | ||
types::{ProposerSchedule, SignedBidReceipt, SignedBidSubmission}, | ||
Error, | ||
}; | ||
use beacon_api_client::{mainnet::Client as BeaconApiClient, ApiResult, Error as ApiError}; | ||
|
||
/// A `Client` for a service implementing the Builder APIs. | ||
/// Note that `Client` does not implement the `Builder` trait so that | ||
/// it can provide more flexibility to callers with respect to the types | ||
/// it accepts. | ||
#[derive(Clone)] | ||
pub struct Client { | ||
api: BeaconApiClient, | ||
} | ||
|
||
impl Client { | ||
pub fn new(api_client: BeaconApiClient) -> Self { | ||
Self { api: api_client } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl BlindedBlockRelayer for Client { | ||
async fn get_proposal_schedule(&self) -> Result<Vec<ProposerSchedule>, Error> { | ||
self.api.get("/relay/v1/builder/validators").await.map_err(From::from) | ||
} | ||
|
||
// TODO support content types | ||
async fn submit_bid( | ||
&self, | ||
signed_submission: &SignedBidSubmission, | ||
) -> Result<SignedBidReceipt, Error> { | ||
let response = self.api.http_post("/relay/v1/builder/blocks", signed_submission).await?; | ||
let receipt: ApiResult<SignedBidReceipt> = response.json().await.map_err(ApiError::from)?; | ||
match receipt { | ||
ApiResult::Ok(receipt) => Ok(receipt), | ||
ApiResult::Err(err) => Err(ApiError::from(err).into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod client; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::{ | ||
blinded_block_relayer::BlindedBlockRelayer, | ||
error::Error, | ||
types::{ProposerSchedule, SignedBidReceipt, SignedBidSubmission}, | ||
}; | ||
use axum::{ | ||
extract::{Json, State}, | ||
routing::{get, post, IntoMakeService}, | ||
Router, | ||
}; | ||
use hyper::server::conn::AddrIncoming; | ||
use std::net::{Ipv4Addr, SocketAddr}; | ||
use tokio::task::JoinHandle; | ||
|
||
/// Type alias for the configured axum server | ||
pub type BlockRelayerServer = axum::Server<AddrIncoming, IntoMakeService<Router>>; | ||
|
||
async fn handle_get_proposal_schedule<R: BlindedBlockRelayer>( | ||
State(relayer): State<R>, | ||
) -> Result<Json<Vec<ProposerSchedule>>, Error> { | ||
tracing::info!("serving proposal schedule for current and next epoch"); | ||
Ok(Json(relayer.get_proposal_schedule().await?)) | ||
} | ||
|
||
async fn handle_submit_bid<R: BlindedBlockRelayer>( | ||
State(relayer): State<R>, | ||
Json(signed_bid_submission): Json<SignedBidSubmission>, | ||
) -> Result<Json<SignedBidReceipt>, Error> { | ||
tracing::info!("handling bid submission"); | ||
Ok(Json(relayer.submit_bid(&signed_bid_submission).await?)) | ||
} | ||
|
||
pub struct Server<R: BlindedBlockRelayer> { | ||
host: Ipv4Addr, | ||
port: u16, | ||
relayer: R, | ||
} | ||
|
||
impl<R: BlindedBlockRelayer + Clone + Send + Sync + 'static> Server<R> { | ||
pub fn new(host: Ipv4Addr, port: u16, relayer: R) -> Self { | ||
Self { host, port, relayer } | ||
} | ||
|
||
/// Configures and returns the axum server | ||
pub fn serve(&self) -> BlockRelayerServer { | ||
let router = Router::new() | ||
.route("/relay/v1/builder/validators", get(handle_get_proposal_schedule::<R>)) | ||
.route("/relay/v1/builder/blocks", post(handle_submit_bid::<R>)) | ||
.with_state(self.relayer.clone()); | ||
let addr = SocketAddr::from((self.host, self.port)); | ||
axum::Server::bind(&addr).serve(router.into_make_service()) | ||
} | ||
|
||
/// Spawns the server on a new task returning the handle for it | ||
pub fn spawn(&self) -> JoinHandle<()> { | ||
let server = self.serve(); | ||
let address = server.local_addr(); | ||
tokio::spawn(async move { | ||
tracing::info!("listening at {address}..."); | ||
if let Err(err) = server.await { | ||
tracing::error!("error while listening for incoming: {err}") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[cfg(feature = "relay-api")] | ||
mod api; | ||
|
||
#[cfg(feature = "relay-api")] | ||
pub use {api::client::Client, api::server::Server}; | ||
|
||
use crate::{ | ||
error::Error, | ||
types::{ProposerSchedule, SignedBidReceipt, SignedBidSubmission}, | ||
}; | ||
use async_trait::async_trait; | ||
|
||
#[async_trait] | ||
pub trait BlindedBlockRelayer { | ||
async fn get_proposal_schedule(&self) -> Result<Vec<ProposerSchedule>, Error>; | ||
|
||
// TODO: support cancellations? | ||
async fn submit_bid( | ||
&self, | ||
signed_submission: &SignedBidSubmission, | ||
) -> Result<SignedBidReceipt, Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters