Set user agent on requests to builder (#4199)

## Issue Addressed

Closes #4185

## Proposed Changes

- Set user agent to `Lighthouse/vX.Y.Z-<commit hash>` by default
- Allow tweaking user agent via `--builder-user-agent "agent"`
This commit is contained in:
Michael Sproul
2023-04-18 02:47:36 +00:00
parent 1d92e3f77c
commit e9a7316f1d
7 changed files with 71 additions and 12 deletions

View File

@@ -10,3 +10,4 @@ sensitive_url = { path = "../../common/sensitive_url" }
eth2 = { path = "../../common/eth2" }
serde = { version = "1.0.116", features = ["derive"] }
serde_json = "1.0.58"
lighthouse_version = { path = "../../common/lighthouse_version" }

View File

@@ -17,6 +17,9 @@ 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;
/// Default user agent for HTTP requests.
pub const DEFAULT_USER_AGENT: &str = lighthouse_version::VERSION;
#[derive(Clone)]
pub struct Timeouts {
get_header: Duration,
@@ -41,23 +44,23 @@ pub struct BuilderHttpClient {
client: reqwest::Client,
server: SensitiveUrl,
timeouts: Timeouts,
user_agent: String,
}
impl BuilderHttpClient {
pub fn new(server: SensitiveUrl) -> Result<Self, Error> {
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()?;
Ok(Self {
client: reqwest::Client::new(),
client,
server,
timeouts: Timeouts::default(),
user_agent,
})
}
pub fn new_with_timeouts(server: SensitiveUrl, timeouts: Timeouts) -> Result<Self, Error> {
Ok(Self {
client: reqwest::Client::new(),
server,
timeouts,
})
pub fn get_user_agent(&self) -> &str {
&self.user_agent
}
async fn get_with_timeout<T: DeserializeOwned, U: IntoUrl>(