Skip to content

Commit b7f93ce

Browse files
committed
node, chain: Use features pattern for RPC compression configuration
Replace the separate `compression = "gzip"` field on Web3Provider with `features = ["compression/gzip"]`, aligning with the pattern established by firehose providers. This keeps extensibility open for future compression methods (e.g. "compression/brotli") without schema changes.
1 parent cf7477b commit b7f93ce

File tree

6 files changed

+43
-87
lines changed

6 files changed

+43
-87
lines changed

chain/ethereum/src/network.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ mod tests {
313313
use graph::components::network_provider::ProviderManager;
314314
use graph::components::network_provider::ProviderName;
315315
use graph::data::value::Word;
316-
use graph::endpoint::Compression;
316+
317317
use graph::http::HeaderMap;
318318
use graph::{
319319
endpoint::EndpointMetrics,
@@ -394,7 +394,7 @@ mod tests {
394394
HeaderMap::new(),
395395
metrics.clone(),
396396
"",
397-
Compression::None,
397+
false,
398398
);
399399
let provider_metrics = Arc::new(ProviderEthRpcMetrics::new(mock_registry.clone()));
400400

@@ -501,7 +501,7 @@ mod tests {
501501
HeaderMap::new(),
502502
metrics.clone(),
503503
"",
504-
Compression::None,
504+
false,
505505
);
506506
let provider_metrics = Arc::new(ProviderEthRpcMetrics::new(mock_registry.clone()));
507507

@@ -573,7 +573,7 @@ mod tests {
573573
HeaderMap::new(),
574574
metrics.clone(),
575575
"",
576-
Compression::None,
576+
false,
577577
);
578578
let provider_metrics = Arc::new(ProviderEthRpcMetrics::new(mock_registry.clone()));
579579

@@ -641,7 +641,7 @@ mod tests {
641641
HeaderMap::new(),
642642
metrics.clone(),
643643
"",
644-
Compression::None,
644+
false,
645645
);
646646
let provider_metrics = Arc::new(ProviderEthRpcMetrics::new(mock_registry.clone()));
647647

@@ -936,7 +936,7 @@ mod tests {
936936
HeaderMap::new(),
937937
endpoint_metrics.clone(),
938938
"",
939-
Compression::None,
939+
false,
940940
);
941941

942942
Arc::new(

chain/ethereum/src/transport.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use graph::components::network_provider::ProviderName;
2-
use graph::endpoint::{Compression, EndpointMetrics, RequestLabels};
2+
use graph::endpoint::{EndpointMetrics, RequestLabels};
33
use jsonrpc_core::types::Call;
44
use jsonrpc_core::Value;
55

@@ -54,22 +54,13 @@ impl Transport {
5454
headers: graph::http::HeaderMap,
5555
metrics: Arc<EndpointMetrics>,
5656
provider: impl AsRef<str>,
57-
compression: Compression,
57+
gzip: bool,
5858
) -> Self {
5959
// Unwrap: This only fails if something is wrong with the system's TLS config.
6060
let mut client_builder = reqwest::Client::builder().default_headers(headers);
6161

62-
match compression {
63-
Compression::Gzip => {
64-
// Enable gzip compression/decompression for requests and responses
65-
client_builder = client_builder.gzip(true);
66-
}
67-
Compression::None => {
68-
// No compression
69-
} // Future compression methods can be handled here:
70-
// Compression::Brotli => {
71-
// client_builder = client_builder.brotli(true);
72-
// }
62+
if gzip {
63+
client_builder = client_builder.gzip(true);
7364
}
7465

7566
let client = client_builder.build().unwrap();

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
{ label = "substreams", details = { type = "substreams", url = "http://localhost:9000", features = [] }},
5454
]

node/src/chain.rs

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

280280
let logger = logger.new(o!("provider" => provider.label.clone()));
281+
let gzip = web3.gzip_compression_enabled();
281282
info!(
282283
logger,
283284
"Creating transport";
284285
"url" => &web3.url,
285286
"capabilities" => capabilities,
286-
"compression" => ?web3.compression
287+
"gzip" => gzip
287288
);
288289

289290
use crate::config::Transport::*;
@@ -294,7 +295,7 @@ pub async fn create_ethereum_networks_for_chain(
294295
web3.headers.clone(),
295296
endpoint_metrics.cheap_clone(),
296297
&provider.label,
297-
web3.compression,
298+
gzip,
298299
),
299300
Ipc => Transport::new_ipc(&web3.url).await,
300301
Ws => Transport::new_ws(&web3.url).await,

node/src/config.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use graph::{
22
anyhow::Error,
33
blockchain::BlockchainKind,
44
components::network_provider::ChainName,
5-
endpoint::Compression,
65
env::ENV_VARS,
76
firehose::{SubgraphLimit, SUBGRAPHS_PER_CONN},
87
itertools::Itertools,
@@ -503,7 +502,6 @@ impl ChainSection {
503502
features,
504503
headers: Default::default(),
505504
rules: vec![],
506-
compression: Compression::None,
507505
}),
508506
};
509507
let entry = chains.entry(name.to_string()).or_insert_with(|| Chain {
@@ -707,10 +705,6 @@ pub struct Web3Provider {
707705

708706
#[serde(default, rename = "match")]
709707
rules: Vec<Web3Rule>,
710-
711-
/// Compression method for RPC requests and responses
712-
#[serde(default)]
713-
pub compression: Compression,
714708
}
715709

716710
impl Web3Provider {
@@ -721,12 +715,16 @@ impl Web3Provider {
721715
}
722716
}
723717

718+
pub fn gzip_compression_enabled(&self) -> bool {
719+
self.features.contains("compression/gzip")
720+
}
721+
724722
pub fn limit_for(&self, node: &NodeId) -> SubgraphLimit {
725723
self.rules.limit_for(node)
726724
}
727725
}
728726

729-
const PROVIDER_FEATURES: [&str; 3] = ["traces", "archive", "no_eip1898"];
727+
const PROVIDER_FEATURES: [&str; 4] = ["traces", "archive", "no_eip1898", "compression/gzip"];
730728
const DEFAULT_PROVIDER_FEATURES: [&str; 2] = ["traces", "archive"];
731729

732730
impl Provider {
@@ -907,7 +905,6 @@ impl<'de> Deserialize<'de> for Provider {
907905
.ok_or_else(|| serde::de::Error::missing_field("features"))?,
908906
headers: headers.unwrap_or_else(HeaderMap::new),
909907
rules: nodes,
910-
compression: Compression::None,
911908
}),
912909
};
913910

@@ -1226,7 +1223,6 @@ mod tests {
12261223
Chain, Config, FirehoseProvider, Provider, ProviderDetails, Shard, Transport, Web3Provider,
12271224
};
12281225
use graph::blockchain::BlockchainKind;
1229-
use graph::endpoint::Compression;
12301226
use graph::firehose::SubgraphLimit;
12311227
use graph::http::{HeaderMap, HeaderValue};
12321228
use graph::prelude::regex::Regex;
@@ -1315,7 +1311,6 @@ mod tests {
13151311
features: BTreeSet::new(),
13161312
headers: HeaderMap::new(),
13171313
rules: Vec::new(),
1318-
compression: Compression::None,
13191314
}),
13201315
},
13211316
actual
@@ -1342,7 +1337,6 @@ mod tests {
13421337
features: BTreeSet::new(),
13431338
headers: HeaderMap::new(),
13441339
rules: Vec::new(),
1345-
compression: Compression::None,
13461340
}),
13471341
},
13481342
actual
@@ -1450,7 +1444,6 @@ mod tests {
14501444
features,
14511445
headers,
14521446
rules: Vec::new(),
1453-
compression: Compression::None,
14541447
}),
14551448
},
14561449
actual
@@ -1476,7 +1469,6 @@ mod tests {
14761469
features: BTreeSet::new(),
14771470
headers: HeaderMap::new(),
14781471
rules: Vec::new(),
1479-
compression: Compression::None,
14801472
}),
14811473
},
14821474
actual
@@ -1846,7 +1838,6 @@ mod tests {
18461838
features: BTreeSet::new(),
18471839
headers: HeaderMap::new(),
18481840
rules: Vec::new(),
1849-
compression: Compression::None,
18501841
}),
18511842
},
18521843
actual
@@ -1860,63 +1851,57 @@ mod tests {
18601851
}
18611852

18621853
#[test]
1863-
fn it_parses_web3_provider_with_compression() {
1864-
let actual = toml::from_str(
1854+
fn it_parses_web3_provider_with_gzip_compression_feature() {
1855+
let actual: Provider = toml::from_str(
18651856
r#"
18661857
label = "compressed"
1867-
details = { type = "web3", url = "http://localhost:8545", features = ["archive"], compression = "gzip" }
1858+
details = { type = "web3", url = "http://localhost:8545", features = ["archive", "compression/gzip"] }
18681859
"#,
18691860
)
18701861
.unwrap();
18711862

1863+
let mut features = BTreeSet::new();
1864+
features.insert("archive".to_string());
1865+
features.insert("compression/gzip".to_string());
1866+
18721867
assert_eq!(
18731868
Provider {
18741869
label: "compressed".to_owned(),
18751870
details: ProviderDetails::Web3(Web3Provider {
18761871
transport: Transport::Rpc,
18771872
url: "http://localhost:8545".to_owned(),
1878-
features: {
1879-
let mut features = BTreeSet::new();
1880-
features.insert("archive".to_string());
1881-
features
1882-
},
1873+
features,
18831874
headers: HeaderMap::new(),
18841875
rules: Vec::new(),
1885-
compression: Compression::Gzip,
18861876
}),
18871877
},
18881878
actual
18891879
);
1880+
1881+
match actual.details {
1882+
ProviderDetails::Web3(ref web3) => {
1883+
assert!(web3.gzip_compression_enabled());
1884+
}
1885+
_ => panic!("expected Web3 provider"),
1886+
}
18901887
}
18911888

18921889
#[test]
1893-
fn it_parses_web3_provider_with_no_compression() {
1894-
let actual = toml::from_str(
1890+
fn it_parses_web3_provider_without_compression_feature() {
1891+
let actual: Provider = toml::from_str(
18951892
r#"
18961893
label = "uncompressed"
1897-
details = { type = "web3", url = "http://localhost:8545", features = ["archive"], compression = "none" }
1894+
details = { type = "web3", url = "http://localhost:8545", features = ["archive"] }
18981895
"#,
18991896
)
19001897
.unwrap();
19011898

1902-
assert_eq!(
1903-
Provider {
1904-
label: "uncompressed".to_owned(),
1905-
details: ProviderDetails::Web3(Web3Provider {
1906-
transport: Transport::Rpc,
1907-
url: "http://localhost:8545".to_owned(),
1908-
features: {
1909-
let mut features = BTreeSet::new();
1910-
features.insert("archive".to_string());
1911-
features
1912-
},
1913-
headers: HeaderMap::new(),
1914-
rules: Vec::new(),
1915-
compression: Compression::None,
1916-
}),
1917-
},
1918-
actual
1919-
);
1899+
match actual.details {
1900+
ProviderDetails::Web3(ref web3) => {
1901+
assert!(!web3.gzip_compression_enabled());
1902+
}
1903+
_ => panic!("expected Web3 provider"),
1904+
}
19201905
}
19211906

19221907
#[test]

0 commit comments

Comments
 (0)