Skip to content

Commit 4c2cea4

Browse files
committed
core, graph, node: use ChainName instead of String
1 parent bd74ea5 commit 4c2cea4

File tree

8 files changed

+51
-41
lines changed

8 files changed

+51
-41
lines changed

core/src/amp_subgraph/manager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use graph::{
99
components::{
1010
link_resolver::{LinkResolver, LinkResolverContext},
1111
metrics::MetricsRegistry,
12-
network_provider::{AmpChainConfig, AmpClients},
12+
network_provider::{AmpChainConfig, AmpClients, ChainName},
1313
store::{DeploymentLocator, SubgraphStore},
1414
subgraph::SubgraphInstanceManager,
1515
},
@@ -35,7 +35,7 @@ pub struct Manager<SS, NC> {
3535
subgraph_store: Arc<SS>,
3636
link_resolver: Arc<dyn LinkResolver>,
3737
amp_clients: AmpClients<NC>,
38-
amp_chain_configs: HashMap<String, AmpChainConfig>,
38+
amp_chain_configs: HashMap<ChainName, AmpChainConfig>,
3939
}
4040

4141
impl<SS, NC> Manager<SS, NC>
@@ -52,7 +52,7 @@ where
5252
subgraph_store: Arc<SS>,
5353
link_resolver: Arc<dyn LinkResolver>,
5454
amp_clients: AmpClients<NC>,
55-
amp_chain_configs: HashMap<String, AmpChainConfig>,
55+
amp_chain_configs: HashMap<ChainName, AmpChainConfig>,
5656
) -> Self {
5757
let logger = logger_factory.component_logger("AmpSubgraphManager", None);
5858
let logger_factory = logger_factory.with_parent(logger);
@@ -145,7 +145,7 @@ where
145145
}
146146
};
147147

148-
let amp_context = network_name.as_deref().and_then(|chain| {
148+
let amp_context = network_name.as_ref().and_then(|chain| {
149149
manager
150150
.amp_chain_configs
151151
.get(chain)

core/src/subgraph/instance_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<S: SubgraphStore, AC: amp::Client> SubgraphInstanceManager<S, AC> {
273273
// Look up the per-chain Amp client based on the network from the
274274
// raw manifest (before the manifest is moved into parse).
275275
let amp_client = network_name_from_raw_manifest(&raw_manifest)
276-
.as_deref()
276+
.as_ref()
277277
.and_then(|network| self.amp_clients.get(network));
278278

279279
let manifest =

core/src/subgraph/registrar.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
33
use async_trait::async_trait;
44
use graph::amp;
55
use graph::blockchain::{Blockchain, BlockchainKind, BlockchainMap};
6+
use graph::components::network_provider::ChainName;
67
use graph::components::{
78
link_resolver::LinkResolverContext,
89
network_provider::{AmpChainConfig, AmpChainNames, AmpClients},
@@ -26,7 +27,7 @@ pub struct SubgraphRegistrar<P, S, SM, AC> {
2627
store: Arc<S>,
2728
subscription_manager: Arc<SM>,
2829
amp_clients: AmpClients<AC>,
29-
amp_chain_configs: HashMap<String, AmpChainConfig>,
30+
amp_chain_configs: HashMap<ChainName, AmpChainConfig>,
3031
chains: Arc<BlockchainMap>,
3132
node_id: NodeId,
3233
version_switching_mode: SubgraphVersionSwitchingMode,
@@ -49,7 +50,7 @@ where
4950
store: Arc<S>,
5051
subscription_manager: Arc<SM>,
5152
amp_clients: AmpClients<AC>,
52-
amp_chain_configs: HashMap<String, AmpChainConfig>,
53+
amp_chain_configs: HashMap<ChainName, AmpChainConfig>,
5354
chains: Arc<BlockchainMap>,
5455
node_id: NodeId,
5556
version_switching_mode: SubgraphVersionSwitchingMode,
@@ -302,15 +303,13 @@ where
302303

303304
// Extract the network name from the raw manifest and resolve the
304305
// per-chain Amp client (if any).
305-
let resolved_amp_chain = network_name_from_raw_manifest(&raw).map(|network| {
306-
let resolved = self.amp_chain_names.resolve(&Word::from(network));
307-
resolved.to_string()
308-
});
306+
let resolved_amp_chain = network_name_from_raw_manifest(&raw)
307+
.map(|network| self.amp_chain_names.resolve(&network));
309308
let amp_client = resolved_amp_chain
310-
.as_deref()
309+
.as_ref()
311310
.and_then(|chain| self.amp_clients.get(chain));
312311
let amp_context = resolved_amp_chain
313-
.as_deref()
312+
.as_ref()
314313
.and_then(|chain| self.amp_chain_configs.get(chain))
315314
.map(|cfg| cfg.context());
316315

graph/src/components/network_provider/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ impl AmpChainConfig {
5555
/// instead of a single global `Option<Arc<AC>>`. Use `get(chain_name)` to
5656
/// retrieve the client for a specific chain.
5757
pub struct AmpClients<AC> {
58-
clients: HashMap<String, Arc<AC>>,
58+
clients: HashMap<ChainName, Arc<AC>>,
5959
}
6060

6161
impl<AC> AmpClients<AC> {
6262
/// Creates a new `AmpClients` from a map of chain names to clients.
63-
pub fn new(clients: HashMap<String, Arc<AC>>) -> Self {
63+
pub fn new(clients: HashMap<ChainName, Arc<AC>>) -> Self {
6464
Self { clients }
6565
}
6666

6767
/// Returns the Amp client for the given chain, or `None` if no client
6868
/// is configured for that chain.
69-
pub fn get(&self, chain_name: &str) -> Option<Arc<AC>> {
69+
pub fn get(&self, chain_name: &ChainName) -> Option<Arc<AC>> {
7070
self.clients.get(chain_name).cloned()
7171
}
7272

@@ -166,26 +166,26 @@ mod tests {
166166
#[test]
167167
fn amp_clients_returns_client_for_configured_chain() {
168168
let mut map = HashMap::new();
169-
map.insert("mainnet".to_string(), Arc::new(42u32));
169+
map.insert(ChainName::from("mainnet"), Arc::new(42u32));
170170
let clients = AmpClients::new(map);
171-
let client = clients.get("mainnet");
171+
let client = clients.get(&ChainName::from("mainnet"));
172172
assert!(client.is_some());
173173
assert_eq!(*client.unwrap(), 42);
174174
}
175175

176176
#[test]
177177
fn amp_clients_returns_none_for_unconfigured_chain() {
178-
let map: HashMap<String, Arc<u32>> = HashMap::new();
178+
let map: HashMap<ChainName, Arc<u32>> = HashMap::new();
179179
let clients = AmpClients::new(map);
180-
assert!(clients.get("mainnet").is_none());
180+
assert!(clients.get(&ChainName::from("mainnet")).is_none());
181181
}
182182

183183
/// Verifies the condition that causes Amp manager registration:
184184
/// `!amp_clients.is_empty()` is true when at least one chain has config.
185185
#[test]
186186
fn amp_manager_registered_when_chain_has_config() {
187187
let mut map = HashMap::new();
188-
map.insert("mainnet".to_string(), Arc::new(42u32));
188+
map.insert(ChainName::from("mainnet"), Arc::new(42u32));
189189
let clients = AmpClients::new(map);
190190
assert!(
191191
!clients.is_empty(),
@@ -210,12 +210,12 @@ mod tests {
210210
#[test]
211211
fn amp_clients_error_for_unconfigured_amp_chain() {
212212
let mut map = HashMap::new();
213-
map.insert("mainnet".to_string(), Arc::new(1u32));
213+
map.insert(ChainName::from("mainnet"), Arc::new(1u32));
214214
let clients = AmpClients::new(map);
215215

216216
// "matic" is not configured.
217217
let result = clients
218-
.get("matic")
218+
.get(&ChainName::from("matic"))
219219
.ok_or_else(|| "Amp is not configured for chain 'matic'".to_string());
220220
assert!(result.is_err());
221221
assert_eq!(

graph/src/data/subgraph/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ pub mod status;
1414

1515
pub use features::{SubgraphFeature, SubgraphFeatureValidationError};
1616

17-
use crate::{cheap_clone::CheapClone, components::store::BLOCK_NUMBER_MAX, object};
17+
use crate::{
18+
cheap_clone::CheapClone,
19+
components::{network_provider::ChainName, store::BLOCK_NUMBER_MAX},
20+
object,
21+
};
1822
use anyhow::{anyhow, Context, Error};
1923
use futures03::future::try_join_all;
2024
use itertools::Itertools;
@@ -1393,15 +1397,15 @@ fn display_vector(input: &[impl std::fmt::Display]) -> impl std::fmt::Display {
13931397
///
13941398
/// Navigates `dataSources[0].network` and returns the network name as an owned string,
13951399
/// or `None` if any step in the path is missing.
1396-
pub fn network_name_from_raw_manifest(raw: &serde_yaml::Mapping) -> Option<String> {
1400+
pub fn network_name_from_raw_manifest(raw: &serde_yaml::Mapping) -> Option<ChainName> {
13971401
use serde_yaml::Value;
13981402
raw.get(Value::String("dataSources".to_owned()))
13991403
.and_then(|ds| ds.as_sequence())
14001404
.and_then(|ds| ds.first())
14011405
.and_then(|ds| ds.as_mapping())
14021406
.and_then(|ds| ds.get(Value::String("network".to_owned())))
14031407
.and_then(|n| n.as_str())
1404-
.map(|s| s.to_owned())
1408+
.map(|s| s.into())
14051409
}
14061410

14071411
#[test]
@@ -1473,7 +1477,7 @@ mod tests {
14731477

14741478
assert_eq!(
14751479
network_name_from_raw_manifest(&raw),
1476-
Some("mainnet".to_string())
1480+
Some(ChainName::from("mainnet"))
14771481
);
14781482
}
14791483

node/src/config.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Config {
139139
/// that has an `[amp]` section. The `AmpConfig.address` string is parsed
140140
/// into a `Uri`; this is expected to always succeed because
141141
/// `ChainSection::validate` already rejects invalid URIs.
142-
pub fn amp_chain_configs(&self) -> Result<HashMap<String, AmpChainConfig>> {
142+
pub fn amp_chain_configs(&self) -> Result<HashMap<ChainName, AmpChainConfig>> {
143143
let mut map = HashMap::new();
144144
for (chain_name, chain) in &self.chains.chains {
145145
if let Some(amp) = &chain.amp {
@@ -152,7 +152,7 @@ impl Config {
152152
)
153153
})?;
154154
map.insert(
155-
chain_name.clone(),
155+
chain_name.as_str().into(),
156156
AmpChainConfig {
157157
address: uri,
158158
token: amp.token.clone(),
@@ -1375,8 +1375,8 @@ mod tests {
13751375
use crate::config::{default_polling_interval, ChainSection, Web3Rule};
13761376

13771377
use super::{
1378-
AmpConfig, Chain, Config, FirehoseProvider, Provider, ProviderDetails, Shard, Transport,
1379-
Web3Provider,
1378+
AmpConfig, Chain, ChainName, Config, FirehoseProvider, Provider, ProviderDetails, Shard,
1379+
Transport, Web3Provider,
13801380
};
13811381
use graph::blockchain::BlockchainKind;
13821382
use graph::firehose::SubgraphLimit;
@@ -2425,9 +2425,11 @@ fdw_pool_size = [
24252425

24262426
// Only mainnet (with amp) should be in the map
24272427
assert_eq!(map.len(), 1);
2428-
assert!(!map.contains_key("sepolia"));
2428+
assert!(!map.contains_key(&ChainName::from("sepolia")));
24292429

2430-
let mainnet = map.get("mainnet").expect("mainnet should be in map");
2430+
let mainnet = map
2431+
.get(&ChainName::from("mainnet"))
2432+
.expect("mainnet should be in map");
24312433
assert_eq!(mainnet.address.to_string(), "http://localhost:50051/");
24322434
assert_eq!(mainnet.token.as_deref(), Some("my-token"));
24332435
assert_eq!(mainnet.context_dataset, "eth");
@@ -2563,7 +2565,9 @@ fdw_pool_size = [
25632565
};
25642566

25652567
let map = config.amp_chain_configs().unwrap();
2566-
let mainnet = map.get("mainnet").expect("mainnet should be in map");
2568+
let mainnet = map
2569+
.get(&ChainName::from("mainnet"))
2570+
.expect("mainnet should be in map");
25672571

25682572
// Identifiers with special characters should be double-quoted
25692573
assert_eq!(mainnet.context_dataset, "\"ns/data@v1\"");
@@ -2603,7 +2607,9 @@ fdw_pool_size = [
26032607
};
26042608

26052609
let map = config.amp_chain_configs().unwrap();
2606-
let mainnet = map.get("mainnet").expect("mainnet should be in map");
2610+
let mainnet = map
2611+
.get(&ChainName::from("mainnet"))
2612+
.expect("mainnet should be in map");
26072613

26082614
// Simple identifiers should be lowercased and unquoted
26092615
assert_eq!(mainnet.context_dataset, "eth");

node/src/launcher.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use std::{
2+
collections::HashMap,
23
io::{BufRead, BufReader},
34
path::Path,
45
time::Duration,
56
};
67

78
use anyhow::Result;
89
use git_testament::{git_testament, render_testament};
9-
use graph::components::link_resolver::{ArweaveClient, FileSizeLimit};
1010
use graph::components::subgraph::Settings;
11+
use graph::components::{
12+
link_resolver::{ArweaveClient, FileSizeLimit},
13+
network_provider::ChainName,
14+
};
1115
use graph::data::graphql::load_manager::LoadManager;
1216
use graph::endpoint::EndpointMetrics;
1317
use graph::env::EnvVars;
@@ -276,10 +280,7 @@ fn build_subgraph_registrar<AC>(
276280
arweave_service: ArweaveService,
277281
ipfs_service: IpfsService,
278282
amp_clients: AmpClients<AC>,
279-
amp_chain_configs: std::collections::HashMap<
280-
String,
281-
graph::components::network_provider::AmpChainConfig,
282-
>,
283+
amp_chain_configs: HashMap<ChainName, graph::components::network_provider::AmpChainConfig>,
283284
cancel_token: CancellationToken,
284285
amp_chain_names: Arc<AmpChainNames>,
285286
) -> Arc<

server/index-node/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ where
524524
// Extract the network name from the raw yaml to look up the
525525
// per-chain Amp client.
526526
let amp_client = network_name_from_raw_manifest(&raw_yaml)
527-
.as_deref()
527+
.as_ref()
528528
.and_then(|network| self.amp_clients.get(network));
529529

530530
let max_spec_version = ENV_VARS.max_spec_version.clone();

0 commit comments

Comments
 (0)