Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: builder-api review #111

Merged
merged 4 commits into from
Jul 4, 2024
Merged
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
14 changes: 7 additions & 7 deletions bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::time::Duration;

use alloy_rpc_types_beacon::events::HeadEvent;
use tokio::sync::mpsc;
use tracing::info;

use bolt_sidecar::{
crypto::{bls::Signer, SignableBLS, SignerBLS},
Expand All @@ -13,7 +12,7 @@ use bolt_sidecar::{
},
start_builder_proxy_server, start_rpc_server,
state::{ConsensusState, ExecutionState, HeadTracker, StateClient},
BuilderProxyConfig, Config, ConstraintsApi, LocalBuilder, MevBoostClient,
BeaconClient, BuilderProxyConfig, Config, ConstraintsApi, LocalBuilder, MevBoostClient,
};

#[tokio::main]
Expand All @@ -22,29 +21,30 @@ async fn main() -> eyre::Result<()> {

let config = Config::parse_from_cli()?;

info!(chain = config.chain.name(), "Starting Bolt sidecar");
tracing::info!(chain = config.chain.name(), "Starting Bolt sidecar");

// TODO: support external signers
// probably it's cleanest to have the Config parser initialize a generic Signer
let signer = Signer::new(config.private_key.clone().unwrap());

let state_client = StateClient::new(&config.execution_api_url);
let state_client = StateClient::new(config.execution_api_url.clone());
let mut execution_state = ExecutionState::new(state_client).await?;

let mevboost_client = MevBoostClient::new(&config.mevboost_url);
let mevboost_client = MevBoostClient::new(config.mevboost_url.clone());
let beacon_client = BeaconClient::new(config.beacon_api_url.clone());

let (api_events, mut api_events_rx) = mpsc::channel(1024);
let shutdown_tx = start_rpc_server(&config, api_events).await?;
let mut consensus_state = ConsensusState::new(
&config.beacon_api_url,
beacon_client.clone(),
&config.validator_indexes,
config.chain.commitment_deadline(),
);

// TODO: this can be replaced with ethereum_consensus::clock::from_system_time()
// but using beacon node events is easier to work on a custom devnet for now
// (as we don't need to specify genesis time and slot duration)
let mut head_tracker = HeadTracker::start(&config.beacon_api_url);
let mut head_tracker = HeadTracker::start(beacon_client);

let builder_proxy_config = BuilderProxyConfig {
mevboost_url: config.mevboost_url.clone(),
Expand Down
7 changes: 4 additions & 3 deletions bolt-sidecar/src/api/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethereum_consensus::{
Fork,
};
use parking_lot::Mutex;
use reqwest::Url;
use serde::Deserialize;
use std::{sync::Arc, time::Duration};
use tokio::net::TcpListener;
Expand Down Expand Up @@ -240,7 +241,7 @@ where
#[derive(Debug, Clone)]
pub struct BuilderProxyConfig {
/// The URL of the target mev-boost server.
pub mevboost_url: String,
pub mevboost_url: Url,
/// The port on which the builder proxy should listen.
pub server_port: u16,
}
Expand All @@ -255,11 +256,11 @@ where
{
tracing::info!(
port = config.server_port,
target = config.mevboost_url,
target = config.mevboost_url.to_string(),
"Starting builder proxy..."
);

let mev_boost = MevBoostClient::new(&config.mevboost_url);
let mev_boost = MevBoostClient::new(config.mevboost_url);
let server = Arc::new(BuilderProxyServer::new(mev_boost, payload_fetcher));

let router = Router::new()
Expand Down
3 changes: 2 additions & 1 deletion bolt-sidecar/src/builder/call_trace_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use alloy_rpc_types_trace::geth::{
};
use alloy_transport::TransportResult;
use futures::{stream::FuturesOrdered, Future, StreamExt};
use reqwest::Url;
use tokio::{
sync::{mpsc, oneshot},
task::JoinHandle,
Expand Down Expand Up @@ -131,7 +132,7 @@ impl Future for CallTraceManager {
impl CallTraceManager {
/// Creates a new [CallTraceManager] instance, which will listen for incoming
/// trace requests and process them in the background using the given RPC client.
pub fn new(url: &str) -> (Self, CallTraceHandle) {
pub fn new<U: Into<Url>>(url: U) -> (Self, CallTraceHandle) {
let rpc = RpcClient::new(url);
let (cmd_tx, cmd_rx) = mpsc::channel(512);

Expand Down
4 changes: 3 additions & 1 deletion bolt-sidecar/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum BuilderError {
Transport(#[from] alloy_transport::TransportError),
#[error("Failed in SSZ merkleization: {0}")]
Merkleization(#[from] MerkleizationError),
#[error("Failed while interacting with beacon client: {0}")]
BeaconApi(#[from] beacon_api_client::Error),
#[error("Failed to parse hint from engine response: {0}")]
InvalidEngineHint(String),
#[error("Failed to build payload: {0}")]
Expand All @@ -66,7 +68,7 @@ pub enum BuilderError {

/// Local builder instance that can ingest a sealed header and
/// create the corresponding builder bid ready for the Builder API.
#[derive(Debug)]
#[allow(missing_debug_implementations)]
pub struct LocalBuilder {
/// BLS credentials for the local builder. We use this to sign the
/// payload bid submissions built by the sidecar.
Expand Down
41 changes: 22 additions & 19 deletions bolt-sidecar/src/builder/payload_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloy_rpc_types_engine::ExecutionPayload as AlloyExecutionPayload;
use beacon_api_client::{BlockId, StateId};
use hex::FromHex;
use regex::Regex;
use reqwest::Url;
use reth_primitives::{
constants::BEACON_NONCE, proofs, BlockBody, Bloom, Header, SealedBlock, TransactionSigned,
Withdrawals, EMPTY_OMMER_ROOT_HASH,
Expand All @@ -16,7 +17,7 @@ use super::{
compat::{to_alloy_execution_payload, to_reth_withdrawal},
BuilderError,
};
use crate::{Config, RpcClient};
use crate::{BeaconClient, Config, RpcClient};

/// Extra-data payload field used for locally built blocks, decoded in UTF-8.
///
Expand All @@ -37,11 +38,11 @@ const DEFAULT_EXTRA_DATA: [u8; 20] = [
///
/// Find more information about this process & its reasoning here:
/// <https://github.com/chainbound/bolt/discussions/59>
#[derive(Debug)]
#[allow(missing_debug_implementations)]
pub struct FallbackPayloadBuilder {
extra_data: Bytes,
fee_recipient: Address,
beacon_api_url: String,
beacon_api_client: BeaconClient,
execution_rpc_client: RpcClient,
engine_hinter: EngineHinter,
slot_time_in_seconds: u64,
Expand All @@ -53,15 +54,15 @@ impl FallbackPayloadBuilder {
let engine_hinter = EngineHinter {
client: reqwest::Client::new(),
jwt_hex: config.jwt_hex.to_string(),
engine_rpc_url: config.engine_api_url.to_string(),
engine_rpc_url: config.engine_api_url.clone(),
};

Self {
engine_hinter,
extra_data: DEFAULT_EXTRA_DATA.into(),
fee_recipient: config.fee_recipient,
beacon_api_url: config.beacon_api_url.to_string(),
execution_rpc_client: RpcClient::new(&config.execution_api_url),
beacon_api_client: BeaconClient::new(config.beacon_api_url.clone()),
execution_rpc_client: RpcClient::new(config.execution_api_url.clone()),
slot_time_in_seconds: config.chain.slot_time(),
}
}
Expand Down Expand Up @@ -106,28 +107,30 @@ impl FallbackPayloadBuilder {
let latest_block = self.execution_rpc_client.get_block(None, true).await?;
tracing::debug!(num = ?latest_block.header.number, "got latest block");

// TODO: refactor this once ConsensusState (https://github.com/chainbound/bolt/issues/58) is ready
let beacon_api_endpoint = reqwest::Url::parse(&self.beacon_api_url).unwrap();
let beacon_api = beacon_api_client::mainnet::Client::new(beacon_api_endpoint);

let withdrawals = beacon_api
let withdrawals = self
.beacon_api_client
// Slot: Defaults to the slot after the parent state if not specified.
.get_expected_withdrawals(StateId::Head, None)
.await
.unwrap()
.await?
.into_iter()
.map(to_reth_withdrawal)
.collect::<Vec<_>>();

tracing::debug!(amount = ?withdrawals.len(), "got withdrawals");

// let prev_randao = self
// .beacon_api_client
// .get_randao(StateId::Head, None)
// .await?;
// let prev_randao = B256::from_slice(&prev_randao);

// NOTE: for some reason, this call fails with an ApiResult deserialization error
// when using the beacon_api_client crate directly, so we use reqwest temporarily.
// this is to be refactored.
let prev_randao = reqwest::Client::new()
.get(format!(
"{}/eth/v1/beacon/states/head/randao",
self.beacon_api_url
self.beacon_api_client.endpoint.as_str()
))
.send()
.await
Expand All @@ -143,10 +146,10 @@ impl FallbackPayloadBuilder {
let prev_randao = B256::from_hex(prev_randao).unwrap();
tracing::debug!("got prev_randao");

let parent_beacon_block_root = beacon_api
let parent_beacon_block_root = self
.beacon_api_client
.get_beacon_block_root(BlockId::Head)
.await
.unwrap();
.await?;
tracing::debug!(parent = ?parent_beacon_block_root, "got parent_beacon_block_root");

let versioned_hashes = transactions
Expand Down Expand Up @@ -265,7 +268,7 @@ pub(crate) enum EngineApiHint {
pub(crate) struct EngineHinter {
client: reqwest::Client,
jwt_hex: String,
engine_rpc_url: String,
engine_rpc_url: Url,
}

impl EngineHinter {
Expand All @@ -287,7 +290,7 @@ impl EngineHinter {

let raw_hint = self
.client
.post(&self.engine_rpc_url)
.post(self.engine_rpc_url.as_str())
.header("Content-Type", "application/json")
.header("Authorization", auth_jwt.clone())
.body(body)
Expand Down
6 changes: 4 additions & 2 deletions bolt-sidecar/src/builder/state_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod tests {

use alloy_primitives::{keccak256, B256, U256};
use partial_mpt::StateTrie;
use reqwest::Url;

use crate::{builder::CallTraceManager, client::rpc::RpcClient};

Expand All @@ -19,9 +20,10 @@ mod tests {
tracing::info!("Starting test_trace_call");

let rpc_url = std::env::var("RPC_URL").expect("RPC_URL must be set");
let client = RpcClient::new(&rpc_url);
let rpc_url = Url::parse(&rpc_url).unwrap();
let client = RpcClient::new(rpc_url.clone());

let (call_trace_manager, call_trace_handler) = CallTraceManager::new(&rpc_url);
let (call_trace_manager, call_trace_handler) = CallTraceManager::new(rpc_url);
tokio::spawn(call_trace_manager);

// https://etherscan.io/block/20125606
Expand Down
7 changes: 4 additions & 3 deletions bolt-sidecar/src/client/mevboost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use beacon_api_client::VersionedValue;
use ethereum_consensus::{
builder::SignedValidatorRegistration, deneb::mainnet::SignedBlindedBeaconBlock, Fork,
};
use reqwest::Url;

use crate::{
api::{
Expand All @@ -22,15 +23,15 @@ use crate::{
/// A client for interacting with the MEV-Boost API.
#[derive(Debug)]
pub struct MevBoostClient {
url: String,
url: Url,
client: reqwest::Client,
}

impl MevBoostClient {
/// Creates a new MEV-Boost client with the given URL.
pub fn new(url: &str) -> Self {
pub fn new<U: Into<Url>>(url: U) -> Self {
Self {
url: url.trim_end_matches('/').to_string(),
url: url.into(),
client: reqwest::ClientBuilder::new()
.user_agent("bolt-sidecar")
.build()
Expand Down
3 changes: 3 additions & 0 deletions bolt-sidecar/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub mod commit_boost;
pub mod mevboost;
pub mod pubsub;
pub mod rpc;

// Re-export the beacon_api_client
pub use beacon_api_client::mainnet::Client as BeaconClient;
15 changes: 8 additions & 7 deletions bolt-sidecar/src/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use futures::future::join_all;
use std::{
collections::HashSet,
ops::{Deref, DerefMut},
str::FromStr,
};

use alloy::ClientBuilder;
Expand All @@ -28,10 +27,8 @@ pub struct RpcClient(alloy::RpcClient<Http<Client>>);

impl RpcClient {
/// Create a new `RpcClient` with the given URL.
pub fn new(url: &str) -> Self {
let url = Url::from_str(url).unwrap();

let client = ClientBuilder::default().http(url);
pub fn new<U: Into<Url>>(url: U) -> Self {
let client = ClientBuilder::default().http(url.into());

Self(client)
}
Expand Down Expand Up @@ -178,6 +175,8 @@ impl DerefMut for RpcClient {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use alloy_consensus::constants::ETH_TO_WEI;
use alloy_primitives::{uint, Uint};
use alloy_rpc_types::EIP1186AccountProofResponse;
Expand All @@ -190,7 +189,8 @@ mod tests {
#[tokio::test]
async fn test_rpc_client() {
let anvil = launch_anvil();
let client = RpcClient::new(&anvil.endpoint());
let anvil_url = Url::from_str(&anvil.endpoint()).unwrap();
let client = RpcClient::new(anvil_url);

let addr = anvil.addresses().first().unwrap();

Expand All @@ -207,7 +207,8 @@ mod tests {

#[tokio::test]
async fn test_get_proof() -> eyre::Result<()> {
let rpc_client = RpcClient::new("https://cloudflare-eth.com");
let rpc_url = Url::parse("https://cloudflare-eth.com")?;
let rpc_client = RpcClient::new(rpc_url);

let proof: EIP1186AccountProofResponse = rpc_client
.0
Expand Down
8 changes: 4 additions & 4 deletions bolt-sidecar/src/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ pub struct ChainConfig {
/// The slot time duration in seconds. If provided,
/// it overrides the default for the selected [Chain].
#[clap(short = 's', long, default_value_t = DEFAULT_SLOT_TIME_IN_SECONDS)]
slot_time_in_seconds: u64,
slot_time: u64,
}

impl Default for ChainConfig {
fn default() -> Self {
Self {
chain: Chain::Mainnet,
commitment_deadline: DEFAULT_COMMITMENT_DEADLINE_IN_MILLIS,
slot_time_in_seconds: DEFAULT_SLOT_TIME_IN_SECONDS,
slot_time: DEFAULT_SLOT_TIME_IN_SECONDS,
}
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ChainConfig {

/// Get the slot time for the given chain in seconds.
pub fn slot_time(&self) -> u64 {
self.slot_time_in_seconds
self.slot_time
}

/// Get the domain for signing messages on the given chain.
Expand Down Expand Up @@ -145,7 +145,7 @@ impl ChainConfig {
pub fn kurtosis(slot_time_in_seconds: u64, commitment_deadline: u64) -> Self {
Self {
chain: Chain::Kurtosis,
slot_time_in_seconds,
slot_time: slot_time_in_seconds,
commitment_deadline,
}
}
Expand Down
Loading