-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMetadataEventProcessor.ts
More file actions
449 lines (421 loc) · 14.1 KB
/
MetadataEventProcessor.ts
File metadata and controls
449 lines (421 loc) · 14.1 KB
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
import { DDOManager, DDO, VersionedDDO } from '@oceanprotocol/ddo-js'
import { ethers, Signer, JsonRpcProvider, getAddress } from 'ethers'
import {
ENVIRONMENT_VARIABLES,
EVENTS,
MetadataStates
} from '../../../utils/constants.js'
import { deleteIndexedMetadataIfExists } from '../../../utils/asset.js'
import { getConfiguration } from '../../../utils/config.js'
import { checkCredentialOnAccessList } from '../../../utils/credentials.js'
import { getDatabase } from '../../../utils/database.js'
import { INDEXER_LOGGER } from '../../../utils/logging/common.js'
import { LOG_LEVELS_STR } from '../../../utils/logging/Logger.js'
import { asyncCallWithTimeout } from '../../../utils/util.js'
import { PolicyServer } from '../../policyServer/index.js'
import { wasNFTDeployedByOurFactory, getPricingStatsForDddo, getDid } from '../utils.js'
import { BaseEventProcessor } from './BaseProcessor.js'
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json' with { type: 'json' }
import { Purgatory } from '../purgatory.js'
export class MetadataEventProcessor extends BaseEventProcessor {
async processEvent(
event: ethers.Log,
chainId: number,
signer: Signer,
provider: JsonRpcProvider,
eventName: string
): Promise<any> {
let did = 'did:op'
try {
const { ddo: ddoDatabase, ddoState } = await getDatabase()
const wasDeployedByUs = await wasNFTDeployedByOurFactory(
chainId,
signer,
getAddress(event.address)
)
if (!wasDeployedByUs) {
INDEXER_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`NFT not deployed by OPF factory`,
true
)
return
}
const decodedEventData = await this.getEventData(
provider,
event.transactionHash,
ERC721Template.abi,
eventName
)
const metadata = decodedEventData.args[4]
const metadataHash = decodedEventData.args[5]
const flag = decodedEventData.args[3]
const owner = decodedEventData.args[0]
const dataNftAddress = ethers.getAddress(event.address)
did = getDid(event.address, chainId)
const templateContract = new ethers.Contract(
dataNftAddress,
ERC721Template.abi,
signer
)
const metaData = await templateContract.getMetaData()
const metadataState = Number(metaData[2])
if ([MetadataStates.DEPRECATED, MetadataStates.REVOKED].includes(metadataState)) {
INDEXER_LOGGER.logMessage(
`Delete DDO because Metadata state is ${metadataState}`,
true
)
const { ddo: ddoDatabase } = await getDatabase()
const ddo = await ddoDatabase.retrieve(did)
if (!ddo) {
INDEXER_LOGGER.logMessage(
`Detected MetadataState changed for ${did}, but it does not exists.`
)
return
}
const ddoInstance = DDOManager.getDDOClass(ddo)
INDEXER_LOGGER.logMessage(
`DDO became non-visible from ${
ddoInstance.getAssetFields().indexedMetadata.nft.state
} to ${metadataState}`
)
const shortDdoInstance = DDOManager.getDDOClass({
id: ddo.id,
version: 'deprecated',
chainId,
nftAddress: ddo.nftAddress,
indexedMetadata: {
nft: {
state: metadataState
}
}
})
const savedDDO = await this.createOrUpdateDDO(
shortDdoInstance,
EVENTS.METADATA_STATE
)
return savedDDO
}
const ddo = await this.decryptDDO(
decodedEventData.args[2],
flag,
owner,
event.address,
chainId,
event.transactionHash,
metadataHash,
metadata
)
const clonedDdo = structuredClone(ddo)
const updatedDdo = deleteIndexedMetadataIfExists(clonedDdo)
const ddoInstance = DDOManager.getDDOClass(updatedDdo)
if (updatedDdo.id !== ddoInstance.makeDid(event.address, chainId.toString(10))) {
INDEXER_LOGGER.error(
`Decrypted DDO ID is not matching the generated hash for DID.`
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
'Decrypted DDO ID does not match generated DID.'
)
return
}
// for unencrypted DDOs
if ((parseInt(flag) & 2) === 0 && !this.checkDdoHash(updatedDdo, metadataHash)) {
INDEXER_LOGGER.error('Unencrypted DDO hash does not match metadata hash.')
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
'Unencrypted DDO hash does not match metadata hash.'
)
return
}
// check authorized publishers
const { authorizedPublishers, authorizedPublishersList } = await getConfiguration()
if (authorizedPublishers.length > 0) {
// if is not there, do not index
const authorized: string[] = authorizedPublishers.filter((address) =>
// do a case insensitive search
address.toLowerCase().includes(owner.toLowerCase())
)
if (!authorized.length) {
INDEXER_LOGGER.error(
`DDO owner ${owner} is NOT part of the ${ENVIRONMENT_VARIABLES.AUTHORIZED_PUBLISHERS.name} group.`
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
`DDO owner ${owner} is NOT part of the ${ENVIRONMENT_VARIABLES.AUTHORIZED_PUBLISHERS.name} group.`
)
return
}
}
if (authorizedPublishersList) {
// check accessList
const isAuthorized = await checkCredentialOnAccessList(
authorizedPublishersList,
String(chainId),
owner,
signer
)
if (!isAuthorized) {
INDEXER_LOGGER.error(
`DDO owner ${owner} is NOT part of the ${ENVIRONMENT_VARIABLES.AUTHORIZED_PUBLISHERS_LIST.name} access group.`
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
`DDO owner ${owner} is NOT part of the ${ENVIRONMENT_VARIABLES.AUTHORIZED_PUBLISHERS_LIST.name} access group.`
)
return
}
}
// stuff that we overwrite
did = ddoInstance.getDid()
const { services } = ddoInstance.getDDOFields()
ddoInstance.updateFields({
chainId,
nftAddress: event.address,
datatokens: await this.getTokenInfo(services, signer)
})
INDEXER_LOGGER.logMessage(
`Processed new DDO data ${ddoInstance.getDid()} with txHash ${
event.transactionHash
} from block ${event.blockNumber}`,
true
)
let previousDdoInstance
const previousDdo = await ddoDatabase.retrieve(ddoInstance.getDid())
if (previousDdo) {
previousDdoInstance = DDOManager.getDDOClass(previousDdo)
}
if (eventName === EVENTS.METADATA_CREATED) {
if (
previousDdoInstance &&
previousDdoInstance.getAssetFields().indexedMetadata.nft.state ===
MetadataStates.ACTIVE
) {
const previousTxId =
previousDdoInstance.getAssetFields().indexedMetadata?.event?.txid
// If it's the same transaction being reprocessed, just skip (idempotent)
if (previousTxId === event.transactionHash) {
INDEXER_LOGGER.logMessage(
`DDO ${ddoInstance.getDid()} already indexed from same transaction ${
event.transactionHash
}. Skipping reprocessing.`,
true
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
true,
' '
)
return
}
INDEXER_LOGGER.logMessage(
`DDO ${ddoInstance.getDid()} is already registered as active from different transaction ${previousTxId}`,
true
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
`DDO ${ddoInstance.getDid()} is already registered as active from transaction ${previousTxId}`
)
return
}
}
if (eventName === EVENTS.METADATA_UPDATED) {
if (!previousDdoInstance) {
INDEXER_LOGGER.logMessage(
`Previous DDO with did ${ddoInstance.getDid()} was not found the database`,
true
)
return
}
const [isUpdateable, error] = this.isUpdateable(
previousDdoInstance,
event.transactionHash,
event.blockNumber
)
if (!isUpdateable) {
INDEXER_LOGGER.error(
`Error encountered when checking if the asset is eligiable for update: ${error}`
)
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
error
)
return
}
}
const from = decodedEventData.args[0].toString()
let ddoUpdatedWithPricing
// we need to store the event data (either metadata created or update and is updatable)
if (
[EVENTS.METADATA_CREATED, EVENTS.METADATA_UPDATED].includes(eventName) &&
this.isValidDtAddressFromServices(ddoInstance.getDDOFields().services)
) {
const ddoWithPricing = await getPricingStatsForDddo(ddoInstance, signer)
const nft = await this.getNFTInfo(
ddoWithPricing.getDDOFields().nftAddress,
signer,
owner,
parseInt(decodedEventData.args[6])
)
let block
let datetime
if (event.blockNumber) {
block = event.blockNumber
// try get block & timestamp from block (only wait 2.5 secs maximum)
const promiseFn = provider.getBlock(event.blockNumber)
const result = await asyncCallWithTimeout(promiseFn, 2500)
if (result.data !== null && !result.timeout) {
datetime = new Date(result.data.timestamp * 1000).toJSON()
}
}
const fieldsToUpdate = {
indexedMetadata: {
nft,
event: {
txid: event.transactionHash,
from,
contract: event.address,
block,
datetime
}
}
}
ddoWithPricing.updateFields(fieldsToUpdate)
// policyServer check
const policyServer = new PolicyServer()
let policyStatus
if (eventName === EVENTS.METADATA_UPDATED)
policyStatus = await policyServer.checkUpdateDDO(
ddoWithPricing.getDDOData() as DDO,
this.networkId,
event.transactionHash,
event
)
else
policyStatus = await policyServer.checknewDDO(
ddoWithPricing.getDDOData() as DDO,
this.networkId,
event.transactionHash,
event
)
if (!policyStatus.success) {
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
policyStatus.message
)
return
}
ddoUpdatedWithPricing = ddoWithPricing
}
// always call, but only create instance once
const purgatory = await Purgatory.getInstance()
// if purgatory is disabled just return false
const updatedDDO = await this.updatePurgatoryStateDdo(
ddoUpdatedWithPricing,
from,
purgatory
)
if (updatedDDO.getAssetFields().indexedMetadata.purgatory.state === false) {
// TODO: insert in a different collection for purgatory DDOs
const saveDDO = await this.createOrUpdateDDO(ddoUpdatedWithPricing, eventName)
INDEXER_LOGGER.logMessage(`saved DDO: ${JSON.stringify(saveDDO)}`)
return saveDDO
}
} catch (error) {
const { ddoState } = await getDatabase()
await ddoState.update(
this.networkId,
did,
event.address,
event.transactionHash,
false,
error.message
)
INDEXER_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`Error processMetadataEvents for did: ${did} and txHash: ${event.transactionHash} and error: ${error}`,
true
)
}
}
async updatePurgatoryStateDdo(
ddo: VersionedDDO,
owner: string,
purgatory: Purgatory
): Promise<VersionedDDO> {
if (!purgatory.isEnabled()) {
ddo.updateFields({
indexedMetadata: {
purgatory: {
state: false
}
}
})
return ddo
}
const state: boolean =
(await purgatory.isBannedAsset(ddo.getDid())) ||
(await purgatory.isBannedAccount(owner))
ddo.updateFields({
indexedMetadata: {
purgatory: {
state
}
}
})
return ddo
}
isUpdateable(
previousDdo: VersionedDDO,
txHash: string,
block: number
): [boolean, string] {
let errorMsg: string
const ddoTxId = previousDdo.getAssetFields().indexedMetadata?.event?.txid
// do not update if we have the same txid
if (txHash === ddoTxId) {
errorMsg = `Previous DDO has the same tx id, no need to update: event-txid=${txHash} <> asset-event-txid=${ddoTxId}`
INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_DEBUG, errorMsg, true)
return [false, errorMsg]
}
const ddoBlock = previousDdo.getAssetFields().indexedMetadata?.event?.block
// do not update if we have the same block
if (block === ddoBlock) {
errorMsg = `Asset was updated later (block: ${ddoBlock}) vs transaction block: ${block}`
INDEXER_LOGGER.log(LOG_LEVELS_STR.LEVEL_DEBUG, errorMsg, true)
return [false, errorMsg]
}
return [true, '']
}
}