mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-08 19:12:49 +00:00
Update meta data verification to allow branched checkpoints (#183)
This commit is contained in:
parent
5a7dcbd20f
commit
c2bbaa6867
@ -64,7 +64,7 @@ export const main = async (): Promise<void> => {
|
|||||||
let diffFound = false;
|
let diffFound = false;
|
||||||
let blockDelay = wait(0);
|
let blockDelay = wait(0);
|
||||||
let subgraphContracts: string[] = [];
|
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;
|
let db: Database | undefined, subgraphGQLClient: GraphQLClient | undefined;
|
||||||
|
|
||||||
if (config.watcher) {
|
if (config.watcher) {
|
||||||
@ -82,7 +82,7 @@ export const main = async (): Promise<void> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subgraphContracts.forEach(subgraphContract => {
|
subgraphContracts.forEach(subgraphContract => {
|
||||||
contractLatestStateCIDMap.set(subgraphContract, '');
|
contractLatestStateCIDMap.set(subgraphContract, { diff: '', checkpoint: '' });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
// Return if IPLD for a contract not found
|
||||||
if (!contractIPLD) {
|
if (!contractIPLD) {
|
||||||
return;
|
return;
|
||||||
@ -219,22 +219,35 @@ export const checkIPLDMetaData = (contractIPLD: {[key: string]: any}, contractLa
|
|||||||
|
|
||||||
const { contractAddress, cid, kind, block } = contractIPLD;
|
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 is same as the parent CID, skip the check
|
||||||
if (cid === parentCID) {
|
if (cid === parentCIDs.diff || cid === parentCIDs.checkpoint) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the parent CID in the map
|
// Update the parent CIDs in the map
|
||||||
contractLatestStateCIDMap.set(contractAddress, cid);
|
// 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
|
// Actual meta data from the GQL result
|
||||||
const data = JSON.parse(contractIPLD.data);
|
const data = JSON.parse(contractIPLD.data);
|
||||||
|
|
||||||
// If parentCID not initialized (is empty at start)
|
// If parentCID not initialized (is empty at start)
|
||||||
// Take the expected parentCID from the actual data itself
|
// Take the expected parentCID from the actual data itself
|
||||||
if (parentCID === '') {
|
let parentCID: string;
|
||||||
parentCID = data.meta.parent['/'];
|
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
|
// Expected meta data
|
||||||
|
Loading…
Reference in New Issue
Block a user