Skip to content

Commit 9b0ee4e

Browse files
DaMandal0rianlutter
authored andcommitted
node, chain: Use features pattern for RPC compression configuration
1 parent 3f1e4b7 commit 9b0ee4e

File tree

9 files changed

+195
-72
lines changed

9 files changed

+195
-72
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloy::transports::{TransportError, TransportErrorKind, TransportFut};
22
use graph::components::network_provider::ProviderName;
3-
use graph::endpoint::{Compression, ConnectionType, EndpointMetrics, RequestLabels};
3+
use graph::endpoint::{ConnectionType, EndpointMetrics, RequestLabels};
44
use graph::prelude::alloy::rpc::json_rpc::{RequestPacket, ResponsePacket};
55
use graph::prelude::alloy::transports::{ipc::IpcConnect, ws::WsConnect};
66
use graph::prelude::*;
@@ -10,6 +10,27 @@ use std::sync::Arc;
1010
use std::task::{Context, Poll};
1111
use tower::Service;
1212

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

56-
if matches!(cpmpr, Compression::Gzip) {
57-
client_builder = client_builder.gzip(true);
77+
match compression {
78+
Compression::None => {}
79+
Compression::Gzip => {
80+
client_builder = client_builder.gzip(true);
81+
}
82+
Compression::Brotli => {
83+
client_builder = client_builder.brotli(true);
84+
}
85+
Compression::Deflate => {
86+
client_builder = client_builder.deflate(true);
87+
}
5888
}
5989

6090
let client = client_builder.build().expect("Failed to build HTTP client");

graph/Cargo.toml

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

graph/src/endpoint.rs

Lines changed: 0 additions & 11 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,16 +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, Default, Deserialize, Serialize, PartialEq)]
23-
pub enum Compression {
24-
#[default]
25-
#[serde(rename = "none")]
26-
None,
27-
#[serde(rename = "gzip")]
28-
Gzip,
29-
}
30-
3120
/// This struct represents all the current labels except for the result
3221
/// which is added separately. If any new labels are necessary they should
3322
/// 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::*;
@@ -224,7 +225,7 @@ pub async fn create_ethereum_networks_for_chain(
224225
endpoint_metrics.cheap_clone(),
225226
&provider.label,
226227
no_eip2718,
227-
web3.compression,
228+
compression,
228229
),
229230
Ipc => Transport::new_ipc(&web3.url).await,
230231
Ws => Transport::new_ws(&web3.url).await,

0 commit comments

Comments
 (0)