-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathindexer.ts
245 lines (224 loc) · 7.73 KB
/
indexer.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
import {
connectDatabase,
createLogger,
createMetrics,
Logger,
Metrics,
parseGRT,
} from '@graphprotocol/common-ts'
import {
createIndexerManagementClient,
defineIndexerManagementModels,
IndexerManagementClient,
IndexerManagementModels,
GraphNode,
Operator,
Network,
POIDisputeAttributes,
specification,
QueryFeeModels,
defineQueryFeeModels,
loadTestYamlConfig,
} from '@graphprotocol/indexer-common'
import { BigNumber } from 'ethers'
import { Sequelize } from 'sequelize'
const TEST_DISPUTE_1: POIDisputeAttributes = {
allocationID: '0xbAd8935f75903A1eF5ea62199d98Fd7c3c1ab20C',
subgraphDeploymentID: 'QmRhYzT8HEZ9LziQhP6JfNfd4co9A7muUYQhPMJsMUojSF',
allocationIndexer: '0x3C17A4c7cD8929B83e4705e04020fA2B1bca2E55',
allocationAmount: '500000000000000000000000',
allocationProof:
'0xdb5b142ba36abbd98d41ebe627d96e7fffb8d79a3f2f25c70a9724e6cdc39ad4',
closedEpoch: 203,
closedEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
closedEpochStartBlockNumber: 848484,
closedEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
previousEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
previousEpochStartBlockNumber: 848484,
previousEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
status: 'potential',
protocolNetwork: 'eip155:421614',
}
const TEST_DISPUTE_2: POIDisputeAttributes = {
allocationID: '0x085fd2ADc1B96c26c266DecAb6A3098EA0eda619',
subgraphDeploymentID: 'QmRhYzT8HEZ9LziQhP6JfNfd4co9A7muUYQhPMJsMUojSF',
allocationIndexer: '0x3C17A4c7cD8929B83e4705e04020fA2B1bca2E55',
allocationAmount: '5000000',
allocationProof:
'0xdb5b142ba36abbd98d41ebe627d96e7fffb8d79a3f2f25c70a9724e6cdc39ad4',
closedEpoch: 210,
closedEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
closedEpochStartBlockNumber: 848484,
closedEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
previousEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
previousEpochStartBlockNumber: 848484,
previousEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
status: 'potential',
protocolNetwork: 'eip155:421614',
}
const POI_DISPUTES_CONVERTERS_FROM_GRAPHQL: Record<
keyof POIDisputeAttributes,
(x: never) => string | BigNumber | number | undefined
> = {
allocationID: x => x,
subgraphDeploymentID: x => x,
allocationIndexer: x => x,
allocationAmount: x => x,
allocationProof: x => x,
closedEpoch: x => +x,
closedEpochStartBlockHash: x => x,
closedEpochStartBlockNumber: x => +x,
closedEpochReferenceProof: x => x,
previousEpochStartBlockHash: x => x,
previousEpochStartBlockNumber: x => +x,
previousEpochReferenceProof: x => x,
status: x => x,
protocolNetwork: x => x,
}
/**
* Parses a POI dispute returned from the indexer management GraphQL
* API into normalized form.
*/
const disputeFromGraphQL = (
dispute: Partial<POIDisputeAttributes>,
): POIDisputeAttributes => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const obj = {} as any
for (const [key, value] of Object.entries(dispute)) {
if (key === '__typename') {
continue
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
obj[key] = (POI_DISPUTES_CONVERTERS_FROM_GRAPHQL as any)[key](value)
}
return obj as POIDisputeAttributes
}
declare const __DATABASE__: never
let sequelize: Sequelize
let models: IndexerManagementModels
let queryFeeModels: QueryFeeModels
let logger: Logger
let indexerManagementClient: IndexerManagementClient
let graphNode: GraphNode
let operator: Operator
let metrics: Metrics
const setupAll = async () => {
metrics = createMetrics()
}
const setup = async () => {
// Clearing the registry prevents duplicate metric registration in the default registry.
metrics.registry.clear()
logger = createLogger({
name: 'IndexerAgent',
async: false,
level: 'trace',
})
sequelize = await connectDatabase(__DATABASE__)
models = defineIndexerManagementModels(sequelize)
queryFeeModels = defineQueryFeeModels(sequelize)
sequelize = await sequelize.sync({ force: true })
graphNode = new GraphNode(
logger,
'http://test-admin-endpoint.xyz',
'https://test-query-endpoint.xyz',
'https://test-status-endpoint.xyz',
)
const yamlObj = loadTestYamlConfig()
const networkSpecification = specification.NetworkSpecification.parse(yamlObj)
const network = await Network.create(
logger,
networkSpecification,
models,
queryFeeModels,
graphNode,
metrics,
)
indexerManagementClient = await createIndexerManagementClient({
models,
graphNode,
logger,
defaults: {
globalIndexingRule: {
allocationAmount: parseGRT('1000'),
parallelAllocations: 1,
},
},
network,
})
operator = new Operator(logger, indexerManagementClient, networkSpecification)
}
const teardown = async () => {
await sequelize.drop({})
}
describe('Indexer tests', () => {
jest.setTimeout(60_000)
beforeAll(setupAll)
beforeEach(setup)
afterEach(teardown)
// test('Parse Dispute from GraphQL', async () => {})
test('Store POI Disputes rejects invalid indexer address', async () => {
const badDispute: POIDisputeAttributes = {
allocationID: '0x085fd2ADc1B96c26c266DecAb6A3098EA0eda619',
subgraphDeploymentID: 'QmRhYzT8HEZ9LziQhP6JfNfd4co9A7muUYQhPMJsMUojSF',
allocationIndexer: '0xCOFFEECOFFEECOFFEE',
allocationAmount: '500000000',
allocationProof:
'0xdb5b142ba36abbd98d41ebe627d96e7fffb8d79a3f2f25c70a9724e6cdc39ad4',
closedEpoch: 203,
closedEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
closedEpochStartBlockNumber: 848484,
closedEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
previousEpochStartBlockHash:
'0x675e9411241c431570d07b920321b2ff6aed2359aa8e26109905d34bffd8932a',
previousEpochStartBlockNumber: 848484,
previousEpochReferenceProof:
'0xd04b5601739a1638719696d0735c92439267a89248c6fd21388d9600f5c942f6',
status: 'potential',
protocolNetwork: 'eip155:421614',
}
const disputes = [badDispute]
await expect(operator.storePoiDisputes(disputes)).rejects.toThrow(
'Failed to store potential POI disputes',
)
})
test('Store POI Disputes is idempotent', async () => {
const disputes: POIDisputeAttributes[] = [TEST_DISPUTE_1, TEST_DISPUTE_2]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const expectedResult = disputes.map((dispute: Record<string, any>) => {
return disputeFromGraphQL(dispute)
})
await expect(operator.storePoiDisputes(disputes)).resolves.toEqual(
expectedResult,
)
await expect(operator.storePoiDisputes(disputes)).resolves.toEqual(
expectedResult,
)
await expect(operator.storePoiDisputes(disputes)).resolves.toEqual(
expectedResult,
)
})
test('Fetch POIDisputes', async () => {
const disputes: POIDisputeAttributes[] = [TEST_DISPUTE_1, TEST_DISPUTE_2]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const expectedResult = disputes.map((dispute: Record<string, any>) => {
return disputeFromGraphQL(dispute)
})
const expectedFilteredResult = [disputeFromGraphQL(TEST_DISPUTE_2)]
await expect(operator.storePoiDisputes(disputes)).resolves.toEqual(
expectedResult,
)
await expect(
operator.fetchPOIDisputes('potential', 205, 'eip155:421614'),
).resolves.toEqual(expectedFilteredResult)
})
})