2022-07-01 01:15:19 +00:00
|
|
|
use eth2::types::builder_bid::SignedBuilderBid;
|
|
|
|
use eth2::types::{
|
2022-10-26 19:15:26 +00:00
|
|
|
AbstractExecPayload, BlindedPayload, EthSpec, ExecutionBlockHash, ExecutionPayload,
|
2023-07-07 14:17:04 +00:00
|
|
|
ForkVersionedResponse, PublicKeyBytes, SignedBlockContents, SignedValidatorRegistrationData,
|
2022-07-01 01:15:19 +00:00
|
|
|
Slot,
|
|
|
|
};
|
|
|
|
pub use eth2::Error;
|
2022-07-30 00:22:37 +00:00
|
|
|
use eth2::{ok_or_error, StatusCode};
|
2022-07-01 01:15:19 +00:00
|
|
|
use reqwest::{IntoUrl, Response};
|
|
|
|
use sensitive_url::SensitiveUrl;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2022-07-30 00:22:37 +00:00
|
|
|
pub const DEFAULT_TIMEOUT_MILLIS: u64 = 15000;
|
|
|
|
|
|
|
|
/// This timeout is in accordance with v0.2.0 of the [builder specs](https://github.com/flashbots/mev-boost/pull/20).
|
|
|
|
pub const DEFAULT_GET_HEADER_TIMEOUT_MILLIS: u64 = 1000;
|
2022-07-01 01:15:19 +00:00
|
|
|
|
2023-04-18 02:47:36 +00:00
|
|
|
/// Default user agent for HTTP requests.
|
|
|
|
pub const DEFAULT_USER_AGENT: &str = lighthouse_version::VERSION;
|
|
|
|
|
2022-07-01 01:15:19 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Timeouts {
|
|
|
|
get_header: Duration,
|
2022-07-30 00:22:37 +00:00
|
|
|
post_validators: Duration,
|
|
|
|
post_blinded_blocks: Duration,
|
|
|
|
get_builder_status: Duration,
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Timeouts {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
get_header: Duration::from_millis(DEFAULT_GET_HEADER_TIMEOUT_MILLIS),
|
2022-07-30 00:22:37 +00:00
|
|
|
post_validators: Duration::from_millis(DEFAULT_TIMEOUT_MILLIS),
|
|
|
|
post_blinded_blocks: Duration::from_millis(DEFAULT_TIMEOUT_MILLIS),
|
|
|
|
get_builder_status: Duration::from_millis(DEFAULT_TIMEOUT_MILLIS),
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct BuilderHttpClient {
|
|
|
|
client: reqwest::Client,
|
|
|
|
server: SensitiveUrl,
|
|
|
|
timeouts: Timeouts,
|
2023-04-18 02:47:36 +00:00
|
|
|
user_agent: String,
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuilderHttpClient {
|
2023-04-18 02:47:36 +00:00
|
|
|
pub fn new(server: SensitiveUrl, user_agent: Option<String>) -> Result<Self, Error> {
|
|
|
|
let user_agent = user_agent.unwrap_or(DEFAULT_USER_AGENT.to_string());
|
|
|
|
let client = reqwest::Client::builder().user_agent(&user_agent).build()?;
|
2022-07-01 01:15:19 +00:00
|
|
|
Ok(Self {
|
2023-04-18 02:47:36 +00:00
|
|
|
client,
|
2022-07-01 01:15:19 +00:00
|
|
|
server,
|
|
|
|
timeouts: Timeouts::default(),
|
2023-04-18 02:47:36 +00:00
|
|
|
user_agent,
|
2022-07-01 01:15:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-18 02:47:36 +00:00
|
|
|
pub fn get_user_agent(&self) -> &str {
|
|
|
|
&self.user_agent
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_with_timeout<T: DeserializeOwned, U: IntoUrl>(
|
|
|
|
&self,
|
|
|
|
url: U,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<T, Error> {
|
|
|
|
self.get_response_with_timeout(url, Some(timeout))
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await
|
Tidy formatting of `Reqwest` errors (#4336)
## Issue Addressed
NA
## Proposed Changes
Implements the `PrettyReqwestError` to wrap a `reqwest::Error` and give nicer `Debug` formatting. It also wraps the `Url` component in a `SensitiveUrl` to avoid leaking sensitive info in logs.
### Before
```
Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(9999), path: "/eth/v1/node/version", query: None, fragment: None }, source: hyper::Error(Connect, ConnectError("tcp connect error", Os { code: 61, kind: ConnectionRefused, message: "Connection refused" })) })
```
### After
```
HttpClient(url: http://localhost:9999/, kind: request, detail: error trying to connect: tcp connect error: Connection refused (os error 61))
```
## Additional Info
I've also renamed the `Reqwest` error enum variants to `HttpClient`, to give people a better chance at knowing what's going on. Reqwest is pretty odd and looks like a typo.
I've implemented it in the `eth2` and `execution_layer` crates. This should affect most logs in the VC and EE-related ones in the BN.
I think the last crate that could benefit from the is the `beacon_node/eth1` crate. I haven't updated it in this PR since its error type is not so amenable to it (everything goes into a `String`). I don't have a whole lot of time to jig around with that at the moment and I feel that this PR as it stands is a significant enough improvement to merge on its own. Leaving it as-is is fine for the time being and we can always come back for it later (or implement in-protocol deposits!).
2023-06-27 01:06:50 +00:00
|
|
|
.map_err(Into::into)
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform a HTTP GET request, returning the `Response` for further processing.
|
|
|
|
async fn get_response_with_timeout<U: IntoUrl>(
|
|
|
|
&self,
|
|
|
|
url: U,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> Result<Response, Error> {
|
|
|
|
let mut builder = self.client.get(url);
|
|
|
|
if let Some(timeout) = timeout {
|
|
|
|
builder = builder.timeout(timeout);
|
|
|
|
}
|
Tidy formatting of `Reqwest` errors (#4336)
## Issue Addressed
NA
## Proposed Changes
Implements the `PrettyReqwestError` to wrap a `reqwest::Error` and give nicer `Debug` formatting. It also wraps the `Url` component in a `SensitiveUrl` to avoid leaking sensitive info in logs.
### Before
```
Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(9999), path: "/eth/v1/node/version", query: None, fragment: None }, source: hyper::Error(Connect, ConnectError("tcp connect error", Os { code: 61, kind: ConnectionRefused, message: "Connection refused" })) })
```
### After
```
HttpClient(url: http://localhost:9999/, kind: request, detail: error trying to connect: tcp connect error: Connection refused (os error 61))
```
## Additional Info
I've also renamed the `Reqwest` error enum variants to `HttpClient`, to give people a better chance at knowing what's going on. Reqwest is pretty odd and looks like a typo.
I've implemented it in the `eth2` and `execution_layer` crates. This should affect most logs in the VC and EE-related ones in the BN.
I think the last crate that could benefit from the is the `beacon_node/eth1` crate. I haven't updated it in this PR since its error type is not so amenable to it (everything goes into a `String`). I don't have a whole lot of time to jig around with that at the moment and I feel that this PR as it stands is a significant enough improvement to merge on its own. Leaving it as-is is fine for the time being and we can always come back for it later (or implement in-protocol deposits!).
2023-06-27 01:06:50 +00:00
|
|
|
let response = builder.send().await.map_err(Error::from)?;
|
2022-07-01 01:15:19 +00:00
|
|
|
ok_or_error(response).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic POST function supporting arbitrary responses and timeouts.
|
|
|
|
async fn post_generic<T: Serialize, U: IntoUrl>(
|
|
|
|
&self,
|
|
|
|
url: U,
|
|
|
|
body: &T,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> Result<Response, Error> {
|
|
|
|
let mut builder = self.client.post(url);
|
|
|
|
if let Some(timeout) = timeout {
|
|
|
|
builder = builder.timeout(timeout);
|
|
|
|
}
|
|
|
|
let response = builder.json(body).send().await?;
|
|
|
|
ok_or_error(response).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn post_with_raw_response<T: Serialize, U: IntoUrl>(
|
|
|
|
&self,
|
|
|
|
url: U,
|
|
|
|
body: &T,
|
2022-07-30 00:22:37 +00:00
|
|
|
timeout: Option<Duration>,
|
2022-07-01 01:15:19 +00:00
|
|
|
) -> Result<Response, Error> {
|
2022-07-30 00:22:37 +00:00
|
|
|
let mut builder = self.client.post(url);
|
|
|
|
if let Some(timeout) = timeout {
|
|
|
|
builder = builder.timeout(timeout);
|
|
|
|
}
|
Tidy formatting of `Reqwest` errors (#4336)
## Issue Addressed
NA
## Proposed Changes
Implements the `PrettyReqwestError` to wrap a `reqwest::Error` and give nicer `Debug` formatting. It also wraps the `Url` component in a `SensitiveUrl` to avoid leaking sensitive info in logs.
### Before
```
Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("localhost")), port: Some(9999), path: "/eth/v1/node/version", query: None, fragment: None }, source: hyper::Error(Connect, ConnectError("tcp connect error", Os { code: 61, kind: ConnectionRefused, message: "Connection refused" })) })
```
### After
```
HttpClient(url: http://localhost:9999/, kind: request, detail: error trying to connect: tcp connect error: Connection refused (os error 61))
```
## Additional Info
I've also renamed the `Reqwest` error enum variants to `HttpClient`, to give people a better chance at knowing what's going on. Reqwest is pretty odd and looks like a typo.
I've implemented it in the `eth2` and `execution_layer` crates. This should affect most logs in the VC and EE-related ones in the BN.
I think the last crate that could benefit from the is the `beacon_node/eth1` crate. I haven't updated it in this PR since its error type is not so amenable to it (everything goes into a `String`). I don't have a whole lot of time to jig around with that at the moment and I feel that this PR as it stands is a significant enough improvement to merge on its own. Leaving it as-is is fine for the time being and we can always come back for it later (or implement in-protocol deposits!).
2023-06-27 01:06:50 +00:00
|
|
|
let response = builder.json(body).send().await.map_err(Error::from)?;
|
2022-07-01 01:15:19 +00:00
|
|
|
ok_or_error(response).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `POST /eth/v1/builder/validators`
|
|
|
|
pub async fn post_builder_validators(
|
|
|
|
&self,
|
|
|
|
validator: &[SignedValidatorRegistrationData],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut path = self.server.full.clone();
|
|
|
|
|
|
|
|
path.path_segments_mut()
|
|
|
|
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
|
|
|
|
.push("eth")
|
|
|
|
.push("v1")
|
|
|
|
.push("builder")
|
|
|
|
.push("validators");
|
|
|
|
|
2022-07-30 00:22:37 +00:00
|
|
|
self.post_generic(path, &validator, Some(self.timeouts.post_validators))
|
|
|
|
.await?;
|
2022-07-01 01:15:19 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `POST /eth/v1/builder/blinded_blocks`
|
|
|
|
pub async fn post_builder_blinded_blocks<E: EthSpec>(
|
|
|
|
&self,
|
2023-07-07 14:17:04 +00:00
|
|
|
blinded_block: &SignedBlockContents<E, BlindedPayload<E>>,
|
2022-07-01 01:15:19 +00:00
|
|
|
) -> Result<ForkVersionedResponse<ExecutionPayload<E>>, Error> {
|
|
|
|
let mut path = self.server.full.clone();
|
|
|
|
|
|
|
|
path.path_segments_mut()
|
|
|
|
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
|
|
|
|
.push("eth")
|
|
|
|
.push("v1")
|
|
|
|
.push("builder")
|
|
|
|
.push("blinded_blocks");
|
|
|
|
|
|
|
|
Ok(self
|
2022-07-30 00:22:37 +00:00
|
|
|
.post_with_raw_response(
|
|
|
|
path,
|
|
|
|
&blinded_block,
|
|
|
|
Some(self.timeouts.post_blinded_blocks),
|
|
|
|
)
|
2022-07-01 01:15:19 +00:00
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `GET /eth/v1/builder/header`
|
2022-10-26 19:15:26 +00:00
|
|
|
pub async fn get_builder_header<E: EthSpec, Payload: AbstractExecPayload<E>>(
|
2022-07-01 01:15:19 +00:00
|
|
|
&self,
|
|
|
|
slot: Slot,
|
|
|
|
parent_hash: ExecutionBlockHash,
|
|
|
|
pubkey: &PublicKeyBytes,
|
2022-07-30 00:22:37 +00:00
|
|
|
) -> Result<Option<ForkVersionedResponse<SignedBuilderBid<E, Payload>>>, Error> {
|
2022-07-01 01:15:19 +00:00
|
|
|
let mut path = self.server.full.clone();
|
|
|
|
|
|
|
|
path.path_segments_mut()
|
|
|
|
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
|
|
|
|
.push("eth")
|
|
|
|
.push("v1")
|
|
|
|
.push("builder")
|
|
|
|
.push("header")
|
|
|
|
.push(slot.to_string().as_str())
|
|
|
|
.push(format!("{parent_hash:?}").as_str())
|
|
|
|
.push(pubkey.as_hex_string().as_str());
|
|
|
|
|
2022-07-30 00:22:37 +00:00
|
|
|
let resp = self.get_with_timeout(path, self.timeouts.get_header).await;
|
|
|
|
|
|
|
|
if matches!(resp, Err(Error::StatusCode(StatusCode::NO_CONTENT))) {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
resp.map(Some)
|
|
|
|
}
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// `GET /eth/v1/builder/status`
|
|
|
|
pub async fn get_builder_status<E: EthSpec>(&self) -> Result<(), Error> {
|
|
|
|
let mut path = self.server.full.clone();
|
|
|
|
|
|
|
|
path.path_segments_mut()
|
|
|
|
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
|
|
|
|
.push("eth")
|
|
|
|
.push("v1")
|
|
|
|
.push("builder")
|
|
|
|
.push("status");
|
|
|
|
|
2022-07-30 00:22:37 +00:00
|
|
|
self.get_with_timeout(path, self.timeouts.get_builder_status)
|
|
|
|
.await
|
2022-07-01 01:15:19 +00:00
|
|
|
}
|
|
|
|
}
|