-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathsubgraph.rs
512 lines (452 loc) · 14.5 KB
/
subgraph.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use crate::{
blockchain::{block_stream::EntitySourceOperation, Block, Blockchain},
components::{link_resolver::LinkResolver, store::BlockNumber},
data::{
subgraph::{
calls_host_fn, SubgraphManifest, UnresolvedSubgraphManifest, LATEST_VERSION,
SPEC_VERSION_1_3_0,
},
value::Word,
},
data_source::{self, common::DeclaredCall},
ensure,
prelude::{CheapClone, DataSourceContext, DeploymentHash, Link},
schema::TypeKind,
};
use anyhow::{anyhow, Context, Error, Result};
use futures03::{stream::FuturesOrdered, TryStreamExt};
use serde::Deserialize;
use slog::{info, Logger};
use std::{fmt, sync::Arc};
use super::{
common::{CallDecls, FindMappingABI, MappingABI, UnresolvedMappingABI},
DataSourceTemplateInfo, TriggerWithHandler,
};
pub const SUBGRAPH_DS_KIND: &str = "subgraph";
const ENTITY_HANDLER_KINDS: &str = "entity";
#[derive(Debug, Clone)]
pub struct DataSource {
pub kind: String,
pub name: String,
pub network: String,
pub manifest_idx: u32,
pub source: Source,
pub mapping: Mapping,
pub context: Arc<Option<DataSourceContext>>,
pub creation_block: Option<BlockNumber>,
}
impl DataSource {
pub fn new(
kind: String,
name: String,
network: String,
manifest_idx: u32,
source: Source,
mapping: Mapping,
context: Arc<Option<DataSourceContext>>,
creation_block: Option<BlockNumber>,
) -> Self {
Self {
kind,
name,
network,
manifest_idx,
source,
mapping,
context,
creation_block,
}
}
pub fn min_spec_version(&self) -> semver::Version {
SPEC_VERSION_1_3_0
}
pub fn handler_kind(&self) -> &str {
ENTITY_HANDLER_KINDS
}
pub fn network(&self) -> Option<&str> {
Some(&self.network)
}
pub fn match_and_decode<C: Blockchain>(
&self,
block: &Arc<C::Block>,
trigger: &TriggerData,
) -> Result<Option<TriggerWithHandler<super::MappingTrigger<C>>>> {
if self.source.address != trigger.source {
return Ok(None);
}
let mut matching_handlers: Vec<_> = self
.mapping
.handlers
.iter()
.filter(|handler| handler.entity == trigger.entity_type())
.collect();
// Get the matching handler if any
let handler = match matching_handlers.pop() {
Some(handler) => handler,
None => return Ok(None),
};
ensure!(
matching_handlers.is_empty(),
format!(
"Multiple handlers defined for entity `{}`, only one is supported",
trigger.entity_type()
)
);
let calls =
DeclaredCall::from_entity_trigger(&self.mapping, &handler.calls, &trigger.entity)?;
let mapping_trigger = MappingEntityTrigger {
data: trigger.clone(),
calls,
};
Ok(Some(TriggerWithHandler::new(
data_source::MappingTrigger::Subgraph(mapping_trigger),
handler.handler.clone(),
block.ptr(),
block.timestamp(),
)))
}
pub fn address(&self) -> Option<Vec<u8>> {
Some(self.source.address().to_bytes())
}
pub fn source_subgraph(&self) -> DeploymentHash {
self.source.address()
}
}
pub type Base64 = Word;
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
pub struct Source {
pub address: DeploymentHash,
#[serde(default)]
pub start_block: BlockNumber,
}
impl Source {
/// The concept of an address may or not make sense for a subgraph data source, but graph node
/// will use this in a few places where some sort of not necessarily unique id is useful:
/// 1. This is used as the value to be returned to mappings from the `dataSource.address()` host
/// function, so changing this is a breaking change.
/// 2. This is used to match with triggers with hosts in `fn hosts_for_trigger`, so make sure
/// the `source` of the data source is equal the `source` of the `TriggerData`.
pub fn address(&self) -> DeploymentHash {
self.address.clone()
}
}
#[derive(Clone, Debug)]
pub struct Mapping {
pub language: String,
pub api_version: semver::Version,
pub abis: Vec<Arc<MappingABI>>,
pub entities: Vec<String>,
pub handlers: Vec<EntityHandler>,
pub runtime: Arc<Vec<u8>>,
pub link: Link,
}
impl Mapping {
pub fn requires_archive(&self) -> anyhow::Result<bool> {
calls_host_fn(&self.runtime, "ethereum.call")
}
}
impl FindMappingABI for Mapping {
fn find_abi(&self, abi_name: &str) -> Result<Arc<MappingABI>, Error> {
Ok(self
.abis
.iter()
.find(|abi| abi.name == abi_name)
.ok_or_else(|| anyhow!("No ABI entry with name `{}` found", abi_name))?
.cheap_clone())
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Deserialize)]
pub struct EntityHandler {
pub handler: String,
pub entity: String,
#[serde(default)]
pub calls: CallDecls,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize)]
pub struct UnresolvedDataSource {
pub kind: String,
pub name: String,
pub network: String,
pub source: UnresolvedSource,
pub mapping: UnresolvedMapping,
pub context: Option<DataSourceContext>,
}
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnresolvedSource {
address: DeploymentHash,
#[serde(default)]
start_block: BlockNumber,
}
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnresolvedMapping {
pub api_version: String,
pub language: String,
pub file: Link,
pub handlers: Vec<EntityHandler>,
pub abis: Option<Vec<UnresolvedMappingABI>>,
pub entities: Vec<String>,
}
impl UnresolvedDataSource {
fn validate_mapping_entities<C: Blockchain>(
mapping_entities: &[String],
source_manifest: &SubgraphManifest<C>,
) -> Result<(), Error> {
for entity in mapping_entities {
let type_kind = source_manifest.schema.kind_of_declared_type(&entity);
match type_kind {
Some(TypeKind::Interface) => {
return Err(anyhow!(
"Entity {} is an interface and cannot be used as a mapping entity",
entity
));
}
Some(TypeKind::Aggregation) => {
return Err(anyhow!(
"Entity {} is an aggregation and cannot be used as a mapping entity",
entity
));
}
None => {
return Err(anyhow!("Entity {} not found in source manifest", entity));
}
Some(TypeKind::Object) => {}
}
}
Ok(())
}
async fn resolve_source_manifest<C: Blockchain>(
&self,
resolver: &Arc<dyn LinkResolver>,
logger: &Logger,
) -> Result<Arc<SubgraphManifest<C>>, Error> {
let source_raw = resolver
.cat(logger, &self.source.address.to_ipfs_link())
.await
.context(format!(
"Failed to resolve source subgraph [{}] manifest",
self.source.address,
))?;
let source_raw: serde_yaml::Mapping =
serde_yaml::from_slice(&source_raw).context(format!(
"Failed to parse source subgraph [{}] manifest as YAML",
self.source.address
))?;
let deployment_hash = self.source.address.clone();
let source_manifest = UnresolvedSubgraphManifest::<C>::parse(deployment_hash, source_raw)
.context(format!(
"Failed to parse source subgraph [{}] manifest",
self.source.address
))?;
source_manifest
.resolve(resolver, logger, LATEST_VERSION.clone())
.await
.context(format!(
"Failed to resolve source subgraph [{}] manifest",
self.source.address
))
.map(Arc::new)
}
#[allow(dead_code)]
pub(super) async fn resolve<C: Blockchain>(
self,
resolver: &Arc<dyn LinkResolver>,
logger: &Logger,
manifest_idx: u32,
) -> Result<DataSource, Error> {
info!(logger, "Resolve subgraph data source";
"name" => &self.name,
"kind" => &self.kind,
"source" => format_args!("{:?}", &self.source),
);
let kind = self.kind.clone();
let source_manifest = self.resolve_source_manifest::<C>(resolver, logger).await?;
let source_spec_version = &source_manifest.spec_version;
if source_manifest
.data_sources
.iter()
.any(|ds| matches!(ds, crate::data_source::DataSource::Subgraph(_)))
{
return Err(anyhow!(
"Nested subgraph data sources [{}] are not supported.",
self.name
));
}
if source_spec_version < &SPEC_VERSION_1_3_0 {
return Err(anyhow!(
"Source subgraph [{}] manifest spec version {} is not supported, minimum supported version is {}",
self.source.address,
source_spec_version,
SPEC_VERSION_1_3_0
));
}
let pruning_enabled = match source_manifest.indexer_hints.as_ref() {
None => false,
Some(hints) => hints.prune.is_some(),
};
if pruning_enabled {
return Err(anyhow!(
"Pruning is enabled for source subgraph [{}], which is not supported",
self.source.address
));
}
let mapping_entities: Vec<String> = self
.mapping
.handlers
.iter()
.map(|handler| handler.entity.clone())
.collect();
Self::validate_mapping_entities(&mapping_entities, &source_manifest)?;
let source = Source {
address: self.source.address,
start_block: self.source.start_block,
};
Ok(DataSource {
manifest_idx,
kind,
name: self.name,
network: self.network,
source,
mapping: self.mapping.resolve(resolver, logger).await?,
context: Arc::new(self.context),
creation_block: None,
})
}
}
impl UnresolvedMapping {
pub async fn resolve(
self,
resolver: &Arc<dyn LinkResolver>,
logger: &Logger,
) -> Result<Mapping, Error> {
info!(logger, "Resolve subgraph ds mapping"; "link" => &self.file.link);
// Resolve each ABI and collect the results
let abis = match self.abis {
Some(abis) => {
abis.into_iter()
.map(|unresolved_abi| {
let resolver = Arc::clone(resolver);
let logger = logger.clone();
async move {
let resolved_abi = unresolved_abi.resolve(&resolver, &logger).await?;
Ok::<_, Error>(Arc::new(resolved_abi))
}
})
.collect::<FuturesOrdered<_>>()
.try_collect::<Vec<_>>()
.await?
}
None => Vec::new(),
};
Ok(Mapping {
language: self.language,
api_version: semver::Version::parse(&self.api_version)?,
entities: self.entities,
handlers: self.handlers,
abis,
runtime: Arc::new(resolver.cat(logger, &self.file).await?),
link: self.file,
})
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct UnresolvedDataSourceTemplate {
pub kind: String,
pub network: Option<String>,
pub name: String,
pub mapping: UnresolvedMapping,
}
#[derive(Clone, Debug)]
pub struct DataSourceTemplate {
pub kind: String,
pub network: Option<String>,
pub name: String,
pub manifest_idx: u32,
pub mapping: Mapping,
}
impl Into<DataSourceTemplateInfo> for DataSourceTemplate {
fn into(self) -> DataSourceTemplateInfo {
let DataSourceTemplate {
kind,
network: _,
name,
manifest_idx,
mapping,
} = self;
DataSourceTemplateInfo {
api_version: mapping.api_version.clone(),
runtime: Some(mapping.runtime),
name,
manifest_idx: Some(manifest_idx),
kind: kind.to_string(),
}
}
}
impl UnresolvedDataSourceTemplate {
pub async fn resolve(
self,
resolver: &Arc<dyn LinkResolver>,
logger: &Logger,
manifest_idx: u32,
) -> Result<DataSourceTemplate, Error> {
let kind = self.kind;
let mapping = self
.mapping
.resolve(resolver, logger)
.await
.with_context(|| format!("failed to resolve data source template {}", self.name))?;
Ok(DataSourceTemplate {
kind,
network: self.network,
name: self.name,
manifest_idx,
mapping,
})
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct MappingEntityTrigger {
pub data: TriggerData,
pub calls: Vec<DeclaredCall>,
}
#[derive(Clone, PartialEq, Eq)]
pub struct TriggerData {
pub source: DeploymentHash,
pub entity: EntitySourceOperation,
pub source_idx: u32,
}
impl TriggerData {
pub fn new(source: DeploymentHash, entity: EntitySourceOperation, source_idx: u32) -> Self {
Self {
source,
entity,
source_idx,
}
}
pub fn entity_type(&self) -> &str {
self.entity.entity_type.as_str()
}
}
impl Ord for TriggerData {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.source_idx.cmp(&other.source_idx) {
std::cmp::Ordering::Equal => self.entity.vid.cmp(&other.entity.vid),
ord => ord,
}
}
}
impl PartialOrd for TriggerData {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Debug for TriggerData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"TriggerData {{ source: {:?}, entity: {:?} }}",
self.source, self.entity,
)
}
}