From c2bbaa6867d2cbf9dd8236bd6bd353ed6cb53ba4 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Fri, 16 Sep 2022 09:15:11 +0530 Subject: [PATCH] Update meta data verification to allow branched checkpoints (#183) --- .../src/cli/compare/compare-blocks.ts | 4 +-- packages/graph-node/src/cli/compare/utils.ts | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/graph-node/src/cli/compare/compare-blocks.ts b/packages/graph-node/src/cli/compare/compare-blocks.ts index 60b2e196..970b34e8 100644 --- a/packages/graph-node/src/cli/compare/compare-blocks.ts +++ b/packages/graph-node/src/cli/compare/compare-blocks.ts @@ -64,7 +64,7 @@ export const main = async (): Promise => { let diffFound = false; let blockDelay = wait(0); let subgraphContracts: string[] = []; - const contractLatestStateCIDMap: Map = new Map(); + const contractLatestStateCIDMap: Map = new Map(); let db: Database | undefined, subgraphGQLClient: GraphQLClient | undefined; if (config.watcher) { @@ -82,7 +82,7 @@ export const main = async (): Promise => { } subgraphContracts.forEach(subgraphContract => { - contractLatestStateCIDMap.set(subgraphContract, ''); + contractLatestStateCIDMap.set(subgraphContract, { diff: '', checkpoint: '' }); }); } diff --git a/packages/graph-node/src/cli/compare/utils.ts b/packages/graph-node/src/cli/compare/utils.ts index c56faa65..c58363a9 100644 --- a/packages/graph-node/src/cli/compare/utils.ts +++ b/packages/graph-node/src/cli/compare/utils.ts @@ -211,7 +211,7 @@ export const getIPLDsByBlock = async (client: GraphQLClient, contracts: string[] })); }; -export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLatestStateCIDMap: Map, rawJson: boolean) => { +export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLatestStateCIDMap: Map, rawJson: boolean) => { // Return if IPLD for a contract not found if (!contractIPLD) { return; @@ -219,22 +219,35 @@ export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLa const { contractAddress, cid, kind, block } = contractIPLD; - let parentCID = contractLatestStateCIDMap.get(contractAddress); + const parentCIDs = contractLatestStateCIDMap.get(contractAddress); + assert(parentCIDs); + // If CID is same as the parent CID, skip the check - if (cid === parentCID) { + if (cid === parentCIDs.diff || cid === parentCIDs.checkpoint) { return; } - // Update the parent CID in the map - contractLatestStateCIDMap.set(contractAddress, cid); + // Update the parent CIDs in the map + // Keep previous 'diff' if kind is 'checkpoint' + const nextParentCIDs = (kind === 'checkpoint') + ? { diff: parentCIDs.diff, checkpoint: cid as string } + : { diff: cid, checkpoint: '' }; + contractLatestStateCIDMap.set(contractAddress, nextParentCIDs); // Actual meta data from the GQL result const data = JSON.parse(contractIPLD.data); // If parentCID not initialized (is empty at start) // Take the expected parentCID from the actual data itself - if (parentCID === '') { - parentCID = data.meta.parent['/']; + let parentCID: string; + const actualParentCID = data.meta.parent['/']; + if (parentCIDs.diff === '') { + parentCID = actualParentCID; + } else { + // Check if actual parent CID points to previous 'checkpoint' + parentCID = (parentCIDs.checkpoint !== '' && actualParentCID === parentCIDs.checkpoint) + ? parentCIDs.checkpoint + : parentCIDs.diff; } // Expected meta data