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

fix: set user-agent header in runtime transport #9434

Merged
merged 11 commits into from
Nov 29, 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
8 changes: 1 addition & 7 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ pub const STATIC_FUZZ_SEED: [u8; 32] = [
0x5d, 0x64, 0x0b, 0x19, 0xad, 0xf0, 0xe3, 0x57, 0xb8, 0xd4, 0xbe, 0x7d, 0x49, 0xee, 0x70, 0xe6,
];

const DEFAULT_USER_AGENT: &str = concat!("foundry/", env!("CARGO_PKG_VERSION"));

/// Useful extensions to [`std::path::Path`].
pub trait FoundryPathExt {
/// Returns true if the [`Path`] ends with `.t.sol`
Expand Down Expand Up @@ -112,11 +110,7 @@ pub fn get_provider_builder(config: &Config) -> Result<ProviderBuilder> {
builder = builder.timeout(Duration::from_secs(rpc_timeout));
}

if let Some(mut rpc_headers) = config.eth_rpc_headers.clone() {
if !rpc_headers.iter().any(|h| h.starts_with("User-Agent:")) {
rpc_headers.push(format!("User-Agent:{DEFAULT_USER_AGENT}"));
}

if let Some(rpc_headers) = config.eth_rpc_headers.clone() {
builder = builder.headers(rpc_headers);
}

Expand Down
3 changes: 3 additions & 0 deletions crates/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub const OPTIMISM_SYSTEM_ADDRESS: Address = address!("deaddeaddeaddeaddeaddeadd
/// Transaction identifier of System transaction types
pub const SYSTEM_TRANSACTION_TYPE: u8 = 126;

/// Default user agent set as the header for requests that don't specify one.
pub const DEFAULT_USER_AGENT: &str = concat!("foundry/", env!("CARGO_PKG_VERSION"));

/// Returns whether the sender is a known L2 system sender that is the first tx in every block.
///
/// Transactions from these senders usually don't have a any fee information.
Expand Down
10 changes: 9 additions & 1 deletion crates/common/src/provider/runtime_transport.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Runtime transport that connects on first request, which can take either of an HTTP,
//! WebSocket, or IPC transport and supports retries based on CUPS logic.

use crate::REQUEST_TIMEOUT;
use crate::{DEFAULT_USER_AGENT, REQUEST_TIMEOUT};
use alloy_json_rpc::{RequestPacket, ResponsePacket};
use alloy_pubsub::{PubSubConnect, PubSubFrontend};
use alloy_rpc_types::engine::{Claims, JwtSecret};
Expand Down Expand Up @@ -176,6 +176,14 @@ impl RuntimeTransport {
);
}

if !headers.iter().any(|(k, _v)| k.as_str().starts_with("User-Agent:")) {
headers.insert(
reqwest::header::USER_AGENT,
HeaderValue::from_str(DEFAULT_USER_AGENT)
.expect("User-Agent should be valid string"),
);
}

client_builder = client_builder.default_headers(headers);

let client =
Expand Down