Update meta data verification to allow branched checkpoints (#183)

This commit is contained in:
prathamesh0 2022-09-16 09:15:11 +05:30 committed by GitHub
parent 5a7dcbd20f
commit c2bbaa6867
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 9 deletions

View File

@ -64,7 +64,7 @@ export const main = async (): Promise<void> => {
let diffFound = false;
let blockDelay = wait(0);
let subgraphContracts: string[] = [];
const contractLatestStateCIDMap: Map<string, string> = new Map();
const contractLatestStateCIDMap: Map<string, { diff: string, checkpoint: string }> = new Map();
let db: Database | undefined, subgraphGQLClient: GraphQLClient | undefined;
if (config.watcher) {
@ -82,7 +82,7 @@ export const main = async (): Promise<void> => {
}
subgraphContracts.forEach(subgraphContract => {
contractLatestStateCIDMap.set(subgraphContract, '');
contractLatestStateCIDMap.set(subgraphContract, { diff: '', checkpoint: '' });
});
}

View File

@ -211,7 +211,7 @@ export const getIPLDsByBlock = async (client: GraphQLClient, contracts: string[]
}));
};
export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLatestStateCIDMap: Map<string, string>, rawJson: boolean) => {
export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLatestStateCIDMap: Map<string, { diff: string, checkpoint: string }>, 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