-
Notifications
You must be signed in to change notification settings - Fork 123
/
subgraphs.ts
524 lines (476 loc) · 14.7 KB
/
subgraphs.ts
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
513
514
515
516
517
518
519
520
521
522
523
524
import { base58 } from 'ethers/lib/utils'
import { BigNumber, utils } from 'ethers'
import { Logger, SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { SubgraphDeployment } from './types'
import {
INDEXING_RULE_GLOBAL,
IndexingDecisionBasis,
IndexingRuleAttributes,
} from './indexer-management'
import { DocumentNode, print } from 'graphql'
import gql from 'graphql-tag'
import { QueryResult } from './network-subgraph'
import { mergeSelectionSets, sleep } from './utils'
export enum SubgraphIdentifierType {
DEPLOYMENT = 'deployment',
SUBGRAPH = 'subgraph',
GROUP = 'group',
}
export async function validateSubgraphID(
s: string | undefined,
): Promise<SubgraphIdentifierType> {
const type = SubgraphIdentifierType.SUBGRAPH
// Case 1: undefined
if (s === undefined) {
throw new Error(`No subgraph ID provided. Must be a valid subgraph ID`)
}
if (typeof s !== 'string') {
throw new Error('Subgraph ID must be a string')
}
const values = s.split('-')
if (
values.length == 2 &&
utils.isHexString(values[0], 20) &&
!isNaN(parseInt(values[1]))
) {
return type
}
throw new Error(
`Subgraph ID is not formatted correctly. Must take the form '0x00000000000000000000-01'`,
)
}
export async function validateDeploymentID(
s: string | undefined,
): Promise<SubgraphIdentifierType> {
const type = SubgraphIdentifierType.DEPLOYMENT
// Case 1: undefined
if (s === undefined) {
throw new Error(`No deployment ID provided. Must be a valid deployment ID`)
}
// Case 4: 'Qm...'
try {
// This will throw if it's not valid
base58.decode(s)
if (s.length === 46) {
return type
}
} catch {
// no-op
}
// Case 5: '0x...' (32 bytes)
try {
if (utils.isHexString(s, 32)) {
return type
}
} catch {
// no-op
}
throw new Error(
`Invalid deployment ID "${s}". Cost models currently must use deployment identifiers, please provide a valid deployment ID.`,
)
}
export async function validateSubgraphGroupID(
s: string | undefined,
{ all, global }: { all?: boolean; global?: boolean },
): Promise<SubgraphIdentifierType> {
const type = SubgraphIdentifierType.GROUP
// Case 1: undefined
if (s === undefined) {
throw new Error(
`No ID provided. Must be a valid subgraph group ID (${
global ? ' or "global"' : ''
}${all ? ' or "all"' : ''})`,
)
}
// Case 2: 'global'
if (global && s === 'global') {
return type
}
// Case 3 (only if permitted): 'all'
if (all && s === 'all') {
return type
}
throw new Error(`Invalid subgraph group ID "${s}"`)
}
export async function processIdentifier(
identifier: string,
{ all, global }: { all?: boolean; global?: boolean },
): Promise<[string, SubgraphIdentifierType]> {
let type = SubgraphIdentifierType.GROUP
const validationActions = [
validateDeploymentID(identifier),
validateSubgraphID(identifier),
validateSubgraphGroupID(identifier, { all, global }),
]
const results = await Promise.allSettled(validationActions)
const rejected = results.filter(
(result) => result.status === 'rejected',
) as PromiseRejectedResult[]
const fulfilled = results.filter(
(result) => result.status === 'fulfilled',
) as PromiseFulfilledResult<SubgraphIdentifierType>[]
if (rejected.length > 2 || fulfilled.length !== 1) {
throw new Error(
`Invalid subgraph identifier "${identifier}". Subgraph identifier should match 1 type of [deployment ID, subgraph ID, group identifier].`,
)
}
type = fulfilled[0].value
return [
type == SubgraphIdentifierType.DEPLOYMENT
? new SubgraphDeploymentID(identifier).ipfsHash
: identifier,
type,
]
}
// enum RuleType {
// GLOBAL,
// DEPLOYMENT,
// SUBGRAPH,
// }
export enum ActivationCriteria {
NA = 'na',
NONE = 'none',
ALWAYS = 'always',
SIGNAL_THRESHOLD = 'signal_threshold',
MIN_STAKE = 'min_stake',
MIN_AVG_QUERY_FEES = 'min_avg_query_fees',
UNSUPPORTED = 'unsupported',
NEVER = 'never',
OFFCHAIN = 'offchain',
INVALID_ALLOCATION_AMOUNT = 'invalid_allocation_amount',
L2_TRANSFER_SUPPORT = 'l2_transfer_support',
}
interface RuleMatch {
rule: IndexingRuleAttributes | undefined
activationCriteria: ActivationCriteria
}
export class AllocationDecision {
declare deployment: SubgraphDeploymentID
declare toAllocate: boolean
declare ruleMatch: RuleMatch
declare protocolNetwork: string
constructor(
deployment: SubgraphDeploymentID,
matchingRule: IndexingRuleAttributes | undefined,
toAllocate: boolean,
ruleActivator: ActivationCriteria,
protocolNetwork: string,
) {
this.deployment = deployment
this.toAllocate = toAllocate
this.ruleMatch = {
rule: matchingRule,
activationCriteria: ruleActivator,
}
this.protocolNetwork = protocolNetwork
}
public reasonString(): string {
return `${this.ruleMatch.rule?.identifierType ?? 'none'}:${
this.ruleMatch.activationCriteria
}`
}
}
export function evaluateDeployments(
logger: Logger,
networkDeployments: SubgraphDeployment[],
rules: IndexingRuleAttributes[],
): AllocationDecision[] {
return networkDeployments.map((deployment) =>
isDeploymentWorthAllocatingTowards(logger, deployment, rules),
)
}
export function isDeploymentWorthAllocatingTowards(
logger: Logger,
deployment: SubgraphDeployment,
rules: IndexingRuleAttributes[],
): AllocationDecision {
const globalRule = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
const deploymentRule =
rules
.filter((rule) => rule.identifierType == SubgraphIdentifierType.DEPLOYMENT)
.find(
(rule) =>
new SubgraphDeploymentID(rule.identifier).bytes32 === deployment.id.bytes32,
) || globalRule
logger.trace('Evaluating whether subgraphDeployment is worth allocating towards', {
deployment,
matchingRule: deploymentRule,
})
// The deployment is not eligible for deployment if it doesn't have an allocation amount
if (!deploymentRule?.allocationAmount) {
logger.debug(`Could not find matching rule with defined 'allocationAmount':`, {
deployment: deployment.id.display,
})
return new AllocationDecision(
deployment.id,
deploymentRule,
false,
ActivationCriteria.INVALID_ALLOCATION_AMOUNT,
deployment.protocolNetwork,
)
}
// Reject unsupported subgraphs early
if (deployment.deniedAt > 0 && deploymentRule.requireSupported) {
return new AllocationDecision(
deployment.id,
deploymentRule,
false,
ActivationCriteria.UNSUPPORTED,
deployment.protocolNetwork,
)
}
switch (deploymentRule?.decisionBasis) {
case undefined:
return new AllocationDecision(
deployment.id,
undefined,
false,
ActivationCriteria.NA,
deployment.protocolNetwork,
)
case IndexingDecisionBasis.ALWAYS:
return new AllocationDecision(
deployment.id,
deploymentRule,
true,
ActivationCriteria.ALWAYS,
deployment.protocolNetwork,
)
case IndexingDecisionBasis.NEVER:
return new AllocationDecision(
deployment.id,
deploymentRule,
false,
ActivationCriteria.NEVER,
deployment.protocolNetwork,
)
case IndexingDecisionBasis.OFFCHAIN:
return new AllocationDecision(
deployment.id,
deploymentRule,
false,
ActivationCriteria.OFFCHAIN,
deployment.protocolNetwork,
)
case IndexingDecisionBasis.RULES: {
const stakedTokens = BigNumber.from(deployment.stakedTokens)
const signalledTokens = BigNumber.from(deployment.signalledTokens)
const avgQueryFees = BigNumber.from(deployment.queryFeesAmount)
if (deploymentRule.minStake && stakedTokens.gte(deploymentRule.minStake)) {
return new AllocationDecision(
deployment.id,
deploymentRule,
true,
ActivationCriteria.MIN_STAKE,
deployment.protocolNetwork,
)
} else if (
deploymentRule.minSignal &&
signalledTokens.gte(deploymentRule.minSignal)
) {
return new AllocationDecision(
deployment.id,
deploymentRule,
true,
ActivationCriteria.SIGNAL_THRESHOLD,
deployment.protocolNetwork,
)
} else if (
deploymentRule.minAverageQueryFees &&
avgQueryFees.gte(deploymentRule.minAverageQueryFees)
) {
return new AllocationDecision(
deployment.id,
deploymentRule,
true,
ActivationCriteria.MIN_AVG_QUERY_FEES,
deployment.protocolNetwork,
)
} else {
return new AllocationDecision(
deployment.id,
deploymentRule,
false,
ActivationCriteria.NONE,
deployment.protocolNetwork,
)
}
}
}
}
export interface ProviderInterface {
getBlockNumber(): Promise<number>
}
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface LoggerInterface {
trace(msg: string, o?: object, ...args: any[]): void
error(msg: string, o?: object, ...args: any[]): void
warn(msg: string, o?: object, ...args: any[]): void
}
export interface SubgraphQueryInterface {
query<Data = any>(
query: DocumentNode,
variables?: Record<string, any>,
): Promise<QueryResult<Data>>
}
/* eslint-enable @typescript-eslint/no-explicit-any */
const blockNumberQuery = gql`
{
_meta {
block {
number
}
}
}
`
interface BlockNumberInterface {
data: { _meta: { block: { number: number } } }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
errors?: any
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Variables = Record<string, any>
export class SubgraphFreshnessChecker {
subgraphName: string
provider: ProviderInterface
threshold: number
logger: LoggerInterface
sleepDurationMillis: number
retries: number
constructor(
subgraphName: string,
provider: ProviderInterface,
freshnessThreshold: number,
sleepDurationMillis: number,
logger: LoggerInterface,
retries: number,
) {
this.subgraphName = subgraphName
this.provider = provider
this.threshold = freshnessThreshold
this.sleepDurationMillis = sleepDurationMillis
this.logger = logger
this.retries = retries
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async checkedQuery<Data = any>(
subgraph: SubgraphQueryInterface,
query: DocumentNode,
variables?: Variables,
): Promise<QueryResult<Data>> {
// Try to inject the latest block number into the original query.
let updatedQuery = query
try {
updatedQuery = mergeSelectionSets(query, blockNumberQuery)
} catch (err) {
const errorMsg = `Failed to append block number into ${this.subgraphName} query`
this.logger.error(errorMsg, { subgraph: this.subgraphName, query: print(query) })
throw new Error(errorMsg)
}
// Try obtaining a fresh subgraph query at most `this.retry` times
return this.checkedQueryRecursive<Data>(
updatedQuery,
subgraph,
this.retries,
variables,
)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async checkedQueryRecursive<Data = any>(
updatedQuery: DocumentNode,
subgraph: SubgraphQueryInterface,
retriesLeft: number,
variables?: Variables,
): Promise<QueryResult<Data>> {
if (retriesLeft < 0) {
const errorMsg = `Max retries reached for ${this.subgraphName} freshness check`
this.logger.error(errorMsg, {
subgraph: this.subgraphName,
query: print(updatedQuery),
})
throw new Error(errorMsg)
}
// Obtain the latest network block number and subgraph query in parallel.
const subgraphQueryPromise = subgraph.query(updatedQuery, variables) as Promise<
QueryResult<Data> & BlockNumberInterface
>
const latestNetworkBlockPromise = this.provider.getBlockNumber()
const [subgraphQueryResult, latestNetworkBlock] = await Promise.all([
subgraphQueryPromise,
latestNetworkBlockPromise,
])
// Return it early if query results contains errors
if (subgraphQueryResult.errors || subgraphQueryResult.error) {
return subgraphQueryResult
}
// Check for missing block metadata
const queryShapeError = this.checkMalformedQueryResult(subgraphQueryResult)
if (queryShapeError) {
const errorMsg = `Failed to infer block number for ${this.subgraphName} query: ${queryShapeError}`
this.logger.error(errorMsg, {
query: print(updatedQuery),
subgraph: this.subgraphName,
error: queryShapeError,
subgraphQueryResult,
})
throw new Error(errorMsg)
}
// At this point we have validated that this value exists and is numeric.
const latestIndexedBlock: number = subgraphQueryResult.data._meta.block.number
// Check subgraph freshness
const blockDistance = latestNetworkBlock - latestIndexedBlock
const logInfo = {
latestIndexedBlock,
latestNetworkBlock,
blockDistance,
freshnessThreshold: this.threshold,
subgraph: this.subgraphName,
retriesLeft,
}
this.logger.trace('Performing subgraph freshness check', logInfo)
if (blockDistance < 0) {
// Invariant violated: Subgraph can't be ahead of network latest block
const errorMsg = `${this.subgraphName}'s latest indexed block (${latestIndexedBlock}) is higher than Network's latest block (${latestNetworkBlock})`
this.logger.warn(errorMsg, logInfo)
}
if (blockDistance > this.threshold) {
// Reenter function
this.logger.warn(
`${this.subgraphName} is not fresh. Sleeping for ${this.sleepDurationMillis} ms before retrying`,
logInfo,
)
await sleep(this.sleepDurationMillis)
return this.checkedQueryRecursive(
updatedQuery,
subgraph,
retriesLeft - 1,
variables,
)
} else {
this.logger.trace(`${this.subgraphName} is fresh`, logInfo)
}
return subgraphQueryResult
}
// Checks if the query result has the expecte
// eslint-disable-next-line @typescript-eslint/no-explicit-any
checkMalformedQueryResult(subgraphQueryResult: any): string | undefined {
if (!subgraphQueryResult) {
return 'Subgraph query result is null or undefined'
}
if (!subgraphQueryResult.data) {
return 'Subgraph query data is null or undefined'
}
if (!subgraphQueryResult.data._meta) {
return 'Query metadata is null or undefined'
}
if (!subgraphQueryResult.data._meta.block) {
return 'Block metadata is null or undefined'
}
if (
!subgraphQueryResult.data._meta.block.number &&
typeof subgraphQueryResult.data._meta.block.number === 'number'
) {
return 'Block number is null or undefined'
}
}
}