Skip to content

Instantly share code, notes, and snippets.

@jeremyorme
Created June 20, 2022 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyorme/1b17e86bc0464dbbfa1ab17573d8d941 to your computer and use it in GitHub Desktop.
Save jeremyorme/1b17e86bc0464dbbfa1ab17573d8d941 to your computer and use it in GitHub Desktop.
DbStoreUpdater.merge updated to discard whole update if validation fails
async merge(entryBlockLists: IEntryBlockList[]) {
// Clone the entry block list map and set any received entry block lists that are new
const newEntryBlockLists = new Map(this._entryBlockLists);
entryBlockLists.filter(entryBlockList => this._isEntryBlockListNew(entryBlockList)).forEach(entryBlockList => {
newEntryBlockLists.set(entryBlockList.ownerIdentity, entryBlockList);
});
// Fetch the entry blocks
const listsAndBlocks = await Promise.all(Array.from(newEntryBlockLists.values())
.sort(byOwnerIdentity)
.map(async entryBlockList => ({
entryBlockList,
entryBlocks: await Promise.all(entryBlockList.entryBlockCids.map(entryBlockCid => ipfsGet<IEntryBlock>(this._ipfs, entryBlockCid)))
})));
// Validate the entry blocks being added
if (!listsAndBlocks.every(listAndBlock => this._entryBlockLists.has(listAndBlock.entryBlockList.ownerIdentity) ||
this._areEntryBlocksValid(listAndBlock.entryBlockList, listAndBlock.entryBlocks)))
return false;
// Set the new entry block lists as current and update the store CID
this._entryBlockLists = newEntryBlockLists;
if (!await this._updateStoreCid())
return;
// Regenerate the index and update the store clock
const entryBlocks: (IEntryBlock|null)[] = mergeArrays(listsAndBlocks.map(lab => lab.entryBlocks));
const allEntries: IEntry[] = mergeArrays(entryBlocks.map(entryBlock => entryBlock ? entryBlock.entries : []));
this._numEntries = allEntries.length;
allEntries.sort(byClock);
if (allEntries.length > 0)
this._clock = allEntries.slice(-1)[0].clock;
this._index.clear();
for (const entry of allEntries)
this._index.set(entry.value._id, entry.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment