-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from chainbound/feat/sidecar/relay
Sidecar: Add relay connections
- Loading branch information
Showing
7 changed files
with
204 additions
and
39 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
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,19 @@ | ||
#![doc = include_str!("../README.md")] | ||
#![warn(missing_debug_implementations, missing_docs, rustdoc::all)] | ||
#![deny(unused_must_use, rust_2018_idioms)] | ||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] | ||
|
||
mod client; | ||
mod common; | ||
mod crypto; | ||
mod pubsub; | ||
mod relays; | ||
mod state; | ||
mod template; | ||
mod types; | ||
|
||
/// Configuration and command-line argument parsing for the sidecar | ||
pub mod config; | ||
|
||
/// JSON-RPC server and handlers for the sidecar | ||
pub mod json_rpc; |
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,114 @@ | ||
use serde_json::Value; | ||
use tokio::sync::broadcast; | ||
use tracing::{debug, error, warn}; | ||
|
||
/// The endpoint for the relay constraints API (where to broadcast commitments). | ||
const RELAY_CONSTRAINTS_ENDPOINT: &str = "/eth/v1/builder/constraints"; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum RelayCommand { | ||
BroadcastCommitment { params: Value }, | ||
Shutdown, | ||
} | ||
|
||
/// Component responsible for dispatching commands to all relays | ||
pub struct RelayManager { | ||
cmd_tx: broadcast::Sender<RelayCommand>, | ||
} | ||
|
||
impl RelayManager { | ||
/// Create a new relay manager with the given endpoints and start the relay clients. | ||
/// This method will spawn a new background task for each relay client. | ||
pub fn new(endpoints: Vec<String>) -> Self { | ||
let (cmd_tx, _) = broadcast::channel(64); | ||
|
||
for endpoint in endpoints { | ||
let relay = Relay { | ||
cmd_rx: cmd_tx.subscribe(), | ||
api: RelayClient { | ||
endpoint: endpoint.trim_end_matches('/').to_string(), | ||
client: reqwest::Client::new(), | ||
}, | ||
}; | ||
tokio::spawn(relay.start()); | ||
} | ||
|
||
Self { cmd_tx } | ||
} | ||
|
||
/// Broadcasts a commitment to all connected relays in the background. | ||
pub fn broadcast_commitment(&self, params: Value) { | ||
let _ = self | ||
.cmd_tx | ||
.send(RelayCommand::BroadcastCommitment { params }); | ||
} | ||
|
||
/// Shuts down all relay clients gracefully. | ||
pub fn shutdown(&self) { | ||
let _ = self.cmd_tx.send(RelayCommand::Shutdown); | ||
} | ||
} | ||
|
||
pub struct Relay<R> { | ||
cmd_rx: broadcast::Receiver<RelayCommand>, | ||
api: R, | ||
} | ||
|
||
pub trait RelayClientAPI { | ||
fn endpoint(&self) -> &str; | ||
|
||
fn broadcast_commitment(&self, params: Value); | ||
} | ||
|
||
impl<R: RelayClientAPI + Sync> Relay<R> { | ||
/// Start the relay client in a background task | ||
async fn start(mut self) { | ||
loop { | ||
tokio::select! { | ||
Ok(cmd) = self.cmd_rx.recv() => { | ||
match cmd { | ||
RelayCommand::BroadcastCommitment { params } => { | ||
self.api.broadcast_commitment(params); | ||
} | ||
RelayCommand::Shutdown => { | ||
warn!("Shutting down relay client: {}", self.api.endpoint()); | ||
break; | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct RelayClient { | ||
endpoint: String, | ||
client: reqwest::Client, | ||
} | ||
|
||
impl RelayClientAPI for RelayClient { | ||
fn endpoint(&self) -> &str { | ||
&self.endpoint | ||
} | ||
|
||
fn broadcast_commitment(&self, params: Value) { | ||
let endpoint = format!("{}{}", self.endpoint, RELAY_CONSTRAINTS_ENDPOINT); | ||
let request = self.client.post(endpoint.clone()).json(¶ms); | ||
|
||
tokio::spawn(async move { | ||
let response = match request.send().await { | ||
Ok(res) => res, | ||
Err(e) => { | ||
error!("Failed to broadcast commitment to {}: {}", endpoint, e); | ||
return; | ||
} | ||
}; | ||
|
||
debug!( | ||
"Broadcasted commitment to {} with status: {}", | ||
endpoint, | ||
response.status() | ||
); | ||
}); | ||
} | ||
} |