-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.rs
306 lines (271 loc) · 9.61 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Graphix configuration parsing and validation.
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use anyhow::Context;
use graphix_common_types::IndexerAddress;
use graphix_indexer_client::{IndexerClient, IndexerId, IndexerInterceptor, RealIndexer};
use graphix_network_sg_client::NetworkSubgraphClient;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::{info, warn};
use url::Url;
use crate::block_choice::BlockChoicePolicy;
use crate::PrometheusMetrics;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BlockExplorerUrlTemplateForBlock(String);
impl BlockExplorerUrlTemplateForBlock {
pub fn url_for_block(&self, block_height: u64) -> String {
self.0.replace("{block}", block_height.to_string().as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ChainSpeedConfig {
pub sample_block_height: u64,
/// In RFC 3339 format.
pub sample_timestamp: chrono::DateTime<chrono::Utc>,
pub avg_block_time_in_msecs: u64,
}
/// Chain-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ChainConfig {
pub caip2: Option<String>,
/// Specifies an approximation of the standard block time for this chain, to
/// approximate block timestamps.
#[serde(flatten, default)]
pub speed: Option<ChainSpeedConfig>,
/// URL to a block explorer for this chain, with `{block}` as a placeholder
/// for the block number.
#[serde(default)]
pub block_explorer_url_template_for_block: Option<BlockExplorerUrlTemplateForBlock>,
}
/// A [`serde`]-compatible representation of Graphix's YAML configuration file.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// Chain-specific configuration.
#[serde(default)]
pub chains: HashMap<String, ChainConfig>,
// Indexing options
// ----------------
#[serde(default)]
pub sources: Vec<ConfigSource>,
#[serde(default)]
pub block_choice_policy: BlockChoicePolicy,
#[serde(default = "Config::default_polling_period_in_seconds")]
pub polling_period_in_seconds: u64,
}
impl Default for Config {
fn default() -> Self {
Self {
chains: Default::default(),
sources: Default::default(),
block_choice_policy: Default::default(),
polling_period_in_seconds: Self::default_polling_period_in_seconds(),
}
}
}
impl Config {
pub fn read(path: &Path) -> anyhow::Result<Self> {
let file_contents = std::fs::read_to_string(path)?;
Self::from_str(&file_contents)
}
pub fn from_str(s: &str) -> anyhow::Result<Self> {
serde_yaml::from_str(s).context("invalid config file")
}
pub fn indexers(&self) -> Vec<IndexerConfig> {
self.sources
.iter()
.filter_map(|source| match source {
ConfigSource::Indexer(config) => Some(config),
_ => None,
})
.cloned()
.collect()
}
pub fn indexers_by_address(&self) -> Vec<IndexerByAddressConfig> {
self.sources
.iter()
.filter_map(|source| match source {
ConfigSource::IndexerByAddress(config) => Some(config),
_ => None,
})
.cloned()
.collect()
}
pub fn interceptors(&self) -> Vec<InterceptorConfig> {
self.sources
.iter()
.filter_map(|source| match source {
ConfigSource::Interceptor(config) => Some(config),
_ => None,
})
.cloned()
.collect()
}
pub fn network_subgraphs(&self) -> Vec<NetworkSubgraphConfig> {
self.sources
.iter()
.filter_map(|source| match source {
ConfigSource::NetworkSubgraph(config) => Some(config),
_ => None,
})
.cloned()
.collect()
}
fn default_polling_period_in_seconds() -> u64 {
120
}
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct IndexerConfig {
pub name: Option<String>,
pub address: IndexerAddress,
pub index_node_endpoint: Url,
}
impl IndexerId for IndexerConfig {
fn address(&self) -> IndexerAddress {
self.address
}
fn name(&self) -> Option<Cow<str>> {
match &self.name {
Some(name) => Some(Cow::Borrowed(name)),
None => None,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct IndexerByAddressConfig {
pub address: IndexerAddress,
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NetworkSubgraphConfig {
pub endpoint: String,
/// What query out of several available ones to use to fetch the list of
/// indexers from the network subgraph?
#[serde(default)]
pub query: NetworkSubgraphQuery,
pub stake_threshold: f64,
pub limit: Option<u32>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum NetworkSubgraphQuery {
#[default]
ByAllocations,
ByStakedTokens,
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct InterceptorConfig {
pub name: String,
pub target: IndexerAddress,
pub poi_byte: u8,
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum ConfigSource {
Indexer(IndexerConfig),
IndexerByAddress(IndexerByAddressConfig),
Interceptor(InterceptorConfig),
NetworkSubgraph(NetworkSubgraphConfig),
}
pub async fn config_to_indexers(
config: Config,
metrics: &PrometheusMetrics,
) -> anyhow::Result<Vec<Arc<dyn IndexerClient>>> {
let mut indexers: Vec<Arc<dyn IndexerClient>> = vec![];
// First, configure all the real, static indexers.
for config in config.indexers() {
info!(indexer_address = %config.address_string(), "Configuring indexer");
indexers.push(Arc::new(RealIndexer::new(
config.name().map(|s| s.into_owned()),
config.address(),
config.index_node_endpoint.to_string(),
metrics.public_proofs_of_indexing_requests.clone(),
)));
}
// Then, configure the network subgraphs, if required, resulting in "dynamic"
// indexers.
for config in config.network_subgraphs() {
info!(endpoint = %config.endpoint, "Configuring network subgraph");
let network_subgraph = NetworkSubgraphClient::new(
config.endpoint.as_str().parse()?,
metrics.public_proofs_of_indexing_requests.clone(),
);
let network_subgraph_indexers_res = match config.query {
NetworkSubgraphQuery::ByAllocations => {
network_subgraph.indexers_by_allocations(config.limit).await
}
NetworkSubgraphQuery::ByStakedTokens => {
network_subgraph.indexers_by_staked_tokens().await
}
};
if let Ok(mut network_subgraph_indexers) = network_subgraph_indexers_res {
if let Some(limit) = config.limit {
network_subgraph_indexers.truncate(limit as usize);
}
indexers.extend(network_subgraph_indexers);
} else {
warn!(
endpoint = %config.endpoint,
error = %network_subgraph_indexers_res.as_ref().unwrap_err(),
"Failed to configure network subgraph"
);
}
}
info!(
indexer_count = indexers.len(),
"Configured all network subgraphs"
);
// Then, configure indexers by address, which requires access to a network subgraph.
for indexer_config in config.indexers_by_address() {
// FIXME: when looking up indexers by address, we don't really know
// which network subgraph to use for the lookup. Should this be
// indicated inside the data source's configuration? Should we try all
// network subgraphs until one succeeds?
let network_subgraph = NetworkSubgraphClient::new(
config
.network_subgraphs()
.first()
.ok_or_else(|| anyhow::anyhow!("indexer by address requires a network subgraph"))?
.endpoint
.parse()?,
metrics.public_proofs_of_indexing_requests.clone(),
);
let indexer = network_subgraph
.indexer_by_address(&indexer_config.address)
.await?;
indexers.push(indexer);
}
// Finally, configure all the interceptors, referring to the real, static
// indexers by ID.
for config in config.interceptors() {
info!(interceptor_id = %config.name, "Configuring interceptor");
let target = indexers
.iter()
.find(|indexer| indexer.address() == config.target)
.expect("interceptor target indexer not found");
indexers.push(Arc::new(IndexerInterceptor::new(
target.clone(),
config.poi_byte,
)));
}
Ok(indexers)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_example_configs() {
Config::from_str(include_str!("../../../configs/testnet.graphix.yml")).unwrap();
Config::from_str(include_str!("../../../configs/network.graphix.yml")).unwrap();
Config::from_str(include_str!("../../../configs/readonly.graphix.yml")).unwrap();
}
}