Skip to content

Commit 8e01d4c

Browse files
committed
node, chain: Use features pattern for RPC compression configuration
1 parent f891ec6 commit 8e01d4c

File tree

9 files changed

+195
-78
lines changed

9 files changed

+195
-78
lines changed

Cargo.lock

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

chain/ethereum/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod transport;
1414
pub use self::capabilities::NodeCapabilities;
1515
pub use self::ethereum_adapter::EthereumAdapter;
1616
pub use self::runtime::RuntimeAdapter;
17-
pub use self::transport::Transport;
17+
pub use self::transport::{Compression, Transport};
1818
pub use env::ENV_VARS;
1919

2020
pub use buffered_call_cache::BufferedCallCache;

chain/ethereum/src/network.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ mod tests {
314314
use graph::components::network_provider::ProviderManager;
315315
use graph::components::network_provider::ProviderName;
316316
use graph::data::value::Word;
317-
use graph::endpoint::Compression;
317+
318318
use graph::http::HeaderMap;
319319
use graph::{
320320
endpoint::EndpointMetrics,
@@ -325,7 +325,9 @@ mod tests {
325325
};
326326
use std::sync::Arc;
327327

328-
use crate::{EthereumAdapter, EthereumAdapterTrait, ProviderEthRpcMetrics, Transport};
328+
use crate::{
329+
Compression, EthereumAdapter, EthereumAdapterTrait, ProviderEthRpcMetrics, Transport,
330+
};
329331

330332
use super::{EthereumNetworkAdapter, EthereumNetworkAdapters, NodeCapabilities};
331333

chain/ethereum/src/transport.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ use alloy::transports::{TransportError, TransportFut};
1111

1212
use graph::prelude::alloy::transports::{http::Http, ipc::IpcConnect, ws::WsConnect};
1313

14+
/// Compression method for RPC requests.
15+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16+
pub enum Compression {
17+
#[default]
18+
None,
19+
Gzip,
20+
Brotli,
21+
Deflate,
22+
}
23+
24+
impl std::fmt::Display for Compression {
25+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26+
match self {
27+
Compression::None => write!(f, "none"),
28+
Compression::Gzip => write!(f, "gzip"),
29+
Compression::Brotli => write!(f, "brotli"),
30+
Compression::Deflate => write!(f, "deflate"),
31+
}
32+
}
33+
}
34+
1435
/// Abstraction over different transport types for Alloy providers.
1536
#[derive(Clone, Debug)]
1637
pub enum Transport {
@@ -46,17 +67,24 @@ impl Transport {
4667
headers: graph::http::HeaderMap,
4768
metrics: Arc<EndpointMetrics>,
4869
provider: impl AsRef<str>,
49-
gzip: bool,
70+
compression: Compression,
5071
) -> Self {
5172
let mut client_builder = reqwest::Client::builder().default_headers(headers);
5273

53-
if gzip {
54-
client_builder = client_builder.gzip(true);
74+
match compression {
75+
Compression::None => {}
76+
Compression::Gzip => {
77+
client_builder = client_builder.gzip(true);
78+
}
79+
Compression::Brotli => {
80+
client_builder = client_builder.brotli(true);
81+
}
82+
Compression::Deflate => {
83+
client_builder = client_builder.deflate(true);
84+
}
5585
}
5686

57-
let client = client_builder
58-
.build()
59-
.expect("Failed to build HTTP client");
87+
let client = client_builder.build().expect("Failed to build HTTP client");
6088

6189
let http_transport = Http::with_client(client, rpc);
6290
let metrics_transport = MetricsHttp::new(http_transport, metrics, provider.as_ref().into());

graph/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ chrono = "0.4.43"
2727
envconfig = { workspace = true }
2828
Inflector = "0.11.3"
2929
atty = "0.2"
30-
reqwest = { version = "0.12.23", features = ["json", "stream", "multipart", "gzip"] }
30+
reqwest = { version = "0.12.23", features = ["json", "stream", "multipart", "gzip", "brotli", "deflate"] }
3131
ethabi = "17.2"
3232
hex = "0.4.3"
3333
http0 = { version = "0", package = "http" }

graph/src/endpoint.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
};
88

99
use prometheus::IntCounterVec;
10-
use serde::{Deserialize, Serialize};
1110
use slog::{warn, Logger};
1211

1312
use crate::components::network_provider::ProviderName;
@@ -18,26 +17,6 @@ use crate::{components::metrics::MetricsRegistry, data::value::Word};
1817
/// avoid locking since we don't need to modify the entire struture.
1918
type ProviderCount = Arc<HashMap<ProviderName, AtomicU64>>;
2019

21-
/// Compression methods for RPC transports
22-
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
23-
pub enum Compression {
24-
#[serde(rename = "none")]
25-
None,
26-
#[serde(rename = "gzip")]
27-
Gzip,
28-
// Future compression methods can be added here:
29-
// #[serde(rename = "brotli")]
30-
// Brotli,
31-
// #[serde(rename = "deflate")]
32-
// Deflate,
33-
}
34-
35-
impl Default for Compression {
36-
fn default() -> Self {
37-
Compression::None
38-
}
39-
}
40-
4120
/// This struct represents all the current labels except for the result
4221
/// which is added separately. If any new labels are necessary they should
4322
/// remain in the same order as added in [`EndpointMetrics::new`]

node/resources/tests/full_config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ shard = "primary"
4848
provider = [
4949
{ label = "mainnet-0", url = "http://rpc.mainnet.io", features = ["archive", "traces"] },
5050
{ label = "mainnet-1", details = { type = "web3call", url = "http://rpc.mainnet.io", features = ["archive", "traces"] }},
51-
{ label = "mainnet-2", details = { type = "web3", url = "http://rpc.mainnet.io", features = ["archive"], compression = "gzip" }},
51+
{ label = "mainnet-2", details = { type = "web3", url = "http://rpc.mainnet.io", features = ["archive", "compression/gzip"] }},
5252
{ label = "firehose", details = { type = "firehose", url = "http://localhost:9000", features = [] }},
5353
]
5454

node/src/chain.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,13 @@ pub async fn create_ethereum_networks_for_chain(
206206
}
207207

208208
let logger = logger.new(o!("provider" => provider.label.clone()));
209+
let compression = web3.compression();
209210
info!(
210211
logger,
211212
"Creating transport";
212213
"url" => &web3.url,
213214
"capabilities" => capabilities,
214-
"compression" => ?web3.compression
215+
"compression" => compression.to_string()
215216
);
216217

217218
use crate::config::Transport::*;
@@ -222,7 +223,7 @@ pub async fn create_ethereum_networks_for_chain(
222223
web3.headers.clone(),
223224
endpoint_metrics.cheap_clone(),
224225
&provider.label,
225-
web3.compression,
226+
compression,
226227
),
227228
Ipc => Transport::new_ipc(&web3.url).await,
228229
Ws => Transport::new_ws(&web3.url).await,

0 commit comments

Comments
 (0)