Skip to content

Commit e9a7316

Browse files
committed
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"`
1 parent 1d92e3f commit e9a7316

File tree

7 files changed

+71
-12
lines changed

7 files changed

+71
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon_node/builder_client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ sensitive_url = { path = "../../common/sensitive_url" }
1010
eth2 = { path = "../../common/eth2" }
1111
serde = { version = "1.0.116", features = ["derive"] }
1212
serde_json = "1.0.58"
13+
lighthouse_version = { path = "../../common/lighthouse_version" }

beacon_node/builder_client/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub const DEFAULT_TIMEOUT_MILLIS: u64 = 15000;
1717
/// This timeout is in accordance with v0.2.0 of the [builder specs](https://github.com/flashbots/mev-boost/pull/20).
1818
pub const DEFAULT_GET_HEADER_TIMEOUT_MILLIS: u64 = 1000;
1919

20+
/// Default user agent for HTTP requests.
21+
pub const DEFAULT_USER_AGENT: &str = lighthouse_version::VERSION;
22+
2023
#[derive(Clone)]
2124
pub struct Timeouts {
2225
get_header: Duration,
@@ -41,23 +44,23 @@ pub struct BuilderHttpClient {
4144
client: reqwest::Client,
4245
server: SensitiveUrl,
4346
timeouts: Timeouts,
47+
user_agent: String,
4448
}
4549

4650
impl BuilderHttpClient {
47-
pub fn new(server: SensitiveUrl) -> Result<Self, Error> {
51+
pub fn new(server: SensitiveUrl, user_agent: Option<String>) -> Result<Self, Error> {
52+
let user_agent = user_agent.unwrap_or(DEFAULT_USER_AGENT.to_string());
53+
let client = reqwest::Client::builder().user_agent(&user_agent).build()?;
4854
Ok(Self {
49-
client: reqwest::Client::new(),
55+
client,
5056
server,
5157
timeouts: Timeouts::default(),
58+
user_agent,
5259
})
5360
}
5461

55-
pub fn new_with_timeouts(server: SensitiveUrl, timeouts: Timeouts) -> Result<Self, Error> {
56-
Ok(Self {
57-
client: reqwest::Client::new(),
58-
server,
59-
timeouts,
60-
})
62+
pub fn get_user_agent(&self) -> &str {
63+
&self.user_agent
6164
}
6265

6366
async fn get_with_timeout<T: DeserializeOwned, U: IntoUrl>(

beacon_node/execution_layer/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ pub struct Config {
230230
pub execution_endpoints: Vec<SensitiveUrl>,
231231
/// Endpoint urls for services providing the builder api.
232232
pub builder_url: Option<SensitiveUrl>,
233+
/// User agent to send with requests to the builder API.
234+
pub builder_user_agent: Option<String>,
233235
/// JWT secrets for the above endpoints running the engine api.
234236
pub secret_files: Vec<PathBuf>,
235237
/// The default fee recipient to use on the beacon node if none if provided from
@@ -260,6 +262,7 @@ impl<T: EthSpec> ExecutionLayer<T> {
260262
let Config {
261263
execution_endpoints: urls,
262264
builder_url,
265+
builder_user_agent,
263266
secret_files,
264267
suggested_fee_recipient,
265268
jwt_id,
@@ -320,12 +323,17 @@ impl<T: EthSpec> ExecutionLayer<T> {
320323

321324
let builder = builder_url
322325
.map(|url| {
323-
let builder_client = BuilderHttpClient::new(url.clone()).map_err(Error::Builder);
324-
info!(log,
326+
let builder_client = BuilderHttpClient::new(url.clone(), builder_user_agent)
327+
.map_err(Error::Builder)?;
328+
329+
info!(
330+
log,
325331
"Connected to external block builder";
326332
"builder_url" => ?url,
327-
"builder_profit_threshold" => builder_profit_threshold);
328-
builder_client
333+
"builder_profit_threshold" => builder_profit_threshold,
334+
"local_user_agent" => builder_client.get_user_agent(),
335+
);
336+
Ok::<_, Error>(builder_client)
329337
})
330338
.transpose()?;
331339

beacon_node/src/cli.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,15 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
10001000
.default_value("0")
10011001
.takes_value(true)
10021002
)
1003+
.arg(
1004+
Arg::with_name("builder-user-agent")
1005+
.long("builder-user-agent")
1006+
.value_name("STRING")
1007+
.help("The HTTP user agent to send alongside requests to the builder URL. The \
1008+
default is Lighthouse's version string.")
1009+
.requires("builder")
1010+
.takes_value(true)
1011+
)
10031012
.arg(
10041013
Arg::with_name("count-unrealized")
10051014
.long("count-unrealized")

beacon_node/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ pub fn get_config<E: EthSpec>(
329329
let payload_builder =
330330
parse_only_one_value(endpoint, SensitiveUrl::parse, "--builder", log)?;
331331
el_config.builder_url = Some(payload_builder);
332+
333+
el_config.builder_user_agent =
334+
clap_utils::parse_optional(cli_args, "builder-user-agent")?;
332335
}
333336

334337
// Set config values from parse values.

lighthouse/tests/beacon_node.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,40 @@ fn builder_fallback_flags() {
716716
);
717717
}
718718

719+
#[test]
720+
fn builder_user_agent() {
721+
run_payload_builder_flag_test_with_config(
722+
"builder",
723+
"http://meow.cats",
724+
None,
725+
None,
726+
|config| {
727+
assert_eq!(
728+
config.execution_layer.as_ref().unwrap().builder_user_agent,
729+
None
730+
);
731+
},
732+
);
733+
run_payload_builder_flag_test_with_config(
734+
"builder",
735+
"http://meow.cats",
736+
Some("builder-user-agent"),
737+
Some("anon"),
738+
|config| {
739+
assert_eq!(
740+
config
741+
.execution_layer
742+
.as_ref()
743+
.unwrap()
744+
.builder_user_agent
745+
.as_ref()
746+
.unwrap(),
747+
"anon"
748+
);
749+
},
750+
);
751+
}
752+
719753
fn run_jwt_optional_flags_test(jwt_flag: &str, jwt_id_flag: &str, jwt_version_flag: &str) {
720754
use sensitive_url::SensitiveUrl;
721755

0 commit comments

Comments
 (0)