From 2f68c6f0de143dfa0ed2eeccbc03eaae169f4045 Mon Sep 17 00:00:00 2001 From: juanmardefago Date: Thu, 18 Jan 2024 16:57:05 -0300 Subject: [PATCH] fix: schema metadata not linked and potential id collisions --- src/mappings/ethereumDIDRegistry.ts | 3 ++- src/mappings/gns.ts | 15 ++++++++++----- src/mappings/ipfs.ts | 6 ++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/mappings/ethereumDIDRegistry.ts b/src/mappings/ethereumDIDRegistry.ts index ba856e4..25bc015 100644 --- a/src/mappings/ethereumDIDRegistry.ts +++ b/src/mappings/ethereumDIDRegistry.ts @@ -17,7 +17,8 @@ export function handleDIDAttributeChanged(event: DIDAttributeChanged): void { // called it directly, it could crash the subgraph let hexHash = changetype(addQm(event.params.value)) let base58Hash = hexHash.toBase58() - let metadataId = graphAccount.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(graphAccount.id.concat('-').concat(base58Hash)) graphAccount.metadata = metadataId graphAccount.save() diff --git a/src/mappings/gns.ts b/src/mappings/gns.ts index a7b3226..b1a1c9c 100644 --- a/src/mappings/gns.ts +++ b/src/mappings/gns.ts @@ -179,7 +179,8 @@ export function handleSubgraphMetadataUpdated(event: SubgraphMetadataUpdated): v let hexHash = changetype(addQm(event.params.subgraphMetadata)) let base58Hash = hexHash.toBase58() - let metadataId = subgraph.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(subgraph.id.concat('-').concat(base58Hash)) subgraph.metadataHash = event.params.subgraphMetadata subgraph.metadata = metadataId subgraph.updatedAt = event.block.timestamp.toI32() @@ -237,7 +238,8 @@ export function handleSubgraphPublished(event: SubgraphPublished): void { subgraphVersion.createdAt = event.block.timestamp.toI32() let hexHash = changetype(addQm(event.params.versionMetadata)) let base58Hash = hexHash.toBase58() - let metadataId = subgraphVersion.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(subgraphVersion.id.concat('-').concat(base58Hash)) subgraphVersion.metadataHash = event.params.versionMetadata subgraphVersion.metadata = metadataId subgraphVersion.save() @@ -717,7 +719,8 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1) let hexHash = changetype(addQm(event.params.subgraphMetadata)) let base58Hash = hexHash.toBase58() - let metadataId = subgraph.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(subgraph.id.concat('-').concat(base58Hash)) subgraph.metadataHash = event.params.subgraphMetadata subgraph.metadata = metadataId; subgraph.updatedAt = event.block.timestamp.toI32() @@ -1083,7 +1086,8 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi let subgraphVersion = SubgraphVersion.load(versionID)! let hexHash = changetype(addQm(event.params.versionMetadata)) let base58Hash = hexHash.toBase58() - let metadataId = subgraphVersion.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(subgraphVersion.id.concat('-').concat(base58Hash)) subgraphVersion.metadataHash = event.params.versionMetadata subgraphVersion.metadata = metadataId subgraphVersion.save() @@ -1114,7 +1118,8 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi subgraphVersion.createdAt = event.block.timestamp.toI32() let hexHash = changetype(addQm(event.params.versionMetadata)) let base58Hash = hexHash.toBase58() - let metadataId = subgraphVersion.id.concat('-').concat(base58Hash) + let uniqueTxID = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()) + let metadataId = uniqueTxID.concat('-').concat(subgraphVersion.id.concat('-').concat(base58Hash)) subgraphVersion.metadataHash = event.params.versionMetadata subgraphVersion.metadata = metadataId diff --git a/src/mappings/ipfs.ts b/src/mappings/ipfs.ts index e71b081..d237e00 100644 --- a/src/mappings/ipfs.ts +++ b/src/mappings/ipfs.ts @@ -81,6 +81,7 @@ export function handleSubgraphDeploymentSchema(content: Bytes): void { } export function handleSubgraphDeploymentManifest(content: Bytes): void { + // Shouldn't need ID since the handler isn't gonna be called more than once, given that it's only on deployment creation. let subgraphDeploymentManifest = new SubgraphDeploymentManifest(dataSource.stringParam()) if (content !== null) { subgraphDeploymentManifest.manifest = content.toString() @@ -98,11 +99,12 @@ export function handleSubgraphDeploymentManifest(content: Bytes): void { let schemaIpfsHashTry = schemaFileSplit.split('\n', 2) if (schemaIpfsHashTry.length == 2) { let schemaIpfsHash = schemaIpfsHashTry[0] - subgraphDeploymentManifest.schema = schemaIpfsHash + let schemaId = subgraphDeploymentManifest.id.concat('-').concat(schemaIpfsHash) + subgraphDeploymentManifest.schema = schemaId subgraphDeploymentManifest.schemaIpfsHash = schemaIpfsHash let context = new DataSourceContext() - context.setString('id', subgraphDeploymentManifest.id.concat('-').concat(schemaIpfsHash)) + context.setString('id', schemaId) SubgraphDeploymentSchemaTemplate.createWithContext(schemaIpfsHash, context) } else { log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, schema file hash can't be retrieved. Error: schemaIpfsHashTry.length isn't 2, actual length: {}", [dataSource.stringParam(), schemaIpfsHashTry.length.toString()])