From 9343eeeea5d5ef44286ba2d3498542f38bdb35a6 Mon Sep 17 00:00:00 2001 From: gozzy Date: Wed, 21 Sep 2022 04:24:44 +0000 Subject: [PATCH] improve batching, fix cache & events --- scripts/helpers/download.js | 10 ++-------- scripts/updateEvents.js | 2 +- services/events.js | 24 +++++++++++++++--------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/helpers/download.js b/scripts/helpers/download.js index fb05282..e19b6db 100644 --- a/scripts/helpers/download.js +++ b/scripts/helpers/download.js @@ -30,14 +30,9 @@ export async function loadCachedEvents({ name, directory, deployedBlock }) { if (module) { const events = JSON.parse(module) - const [lastEvent] = JSON.parse(module).sort( - (a, b) => (b.block || b.blockNumber) - (a.block || a.blockNumber) - ) - const lastBlock = lastEvent.block || lastEvent.blockNumber - return { events, - lastBlock + lastBlock: events[events.length - 1].blockNumber } } } catch (err) { @@ -68,8 +63,7 @@ export async function getPastEvents({ type, fromBlock, netId, events, contractAt const blockDifference = Math.ceil(blockNumberBuffer - fromBlock) // eth_logs and eth_filter are restricted > 10,000 block queries - const blockDenom = blockDifference > 10000 ? 4950 : 20 - const blockRange = blockDifference / blockDenom + const blockRange = 10000 let chunksCount = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange) const chunkSize = Math.ceil(blockDifference / chunksCount) diff --git a/scripts/updateEvents.js b/scripts/updateEvents.js index 211b0d9..9eca0cd 100644 --- a/scripts/updateEvents.js +++ b/scripts/updateEvents.js @@ -64,7 +64,7 @@ async function main(type, netId) { let freshEvents = cachedEvents.events.concat(events) if (type === 'Withdrawal') { - freshEvents = uniqBy(freshEvents, 'nullifierHash').sort((a, b) => b.blockNumber - a.blockNumber) + freshEvents = uniqBy(freshEvents, 'nullifierHash').sort((a, b) => a.blockNumber - b.blockNumber) } else { freshEvents = freshEvents.filter((e, index) => Number(e.leafIndex) === index) } diff --git a/services/events.js b/services/events.js index 7ee1495..0b32ac8 100644 --- a/services/events.js +++ b/services/events.js @@ -14,7 +14,7 @@ class EventService { this.idb = window.$nuxt.$indexedDB(netId) const { nativeCurrency } = networkConfig[`netId${netId}`] - const hasCache = supportedCaches.indexOf(Number(this.netId)) !== 0 + const hasCache = supportedCaches.includes(netId.toString()) this.netId = netId this.amount = amount @@ -37,6 +37,7 @@ class EventService { if (!cachedEvents && this.hasCache) { cachedEvents = await this.getEventsFromCache(type) } + return cachedEvents } async updateEvents(type, cachedEvents) { @@ -45,6 +46,7 @@ class EventService { const savedEvents = cachedEvents || (await this.getEvents(type)) let fromBlock = deployedBlock + if (savedEvents) { fromBlock = savedEvents.lastBlock + 1 } @@ -138,22 +140,23 @@ class EventService { async getEventsFromDB(type) { try { const instanceName = this.getInstanceName(type) - const savedEvents = await this.idb.getAll({ storeName: instanceName }) if (!savedEvents || !savedEvents.length) { return undefined } - const event = await this.idb.getFromIndex({ - storeName: 'lastEvents', - indexName: 'name', - key: instanceName + // IndexedDB scrambles assortment + savedEvents.sort((a, b) => { + if (a.leafIndex && b.leafIndex) { + return a.leafIndex - b.leafIndex + } + return a.blockNumber - b.blockNumber }) return { events: savedEvents, - lastBlock: event.blockNumber + lastBlock: savedEvents[savedEvents.length - 1].blockNumber } } catch (err) { return undefined @@ -268,7 +271,7 @@ class EventService { async getBatchEventsFromRpc({ fromBlock, type }) { try { - const blockRange = 4950 + const blockRange = 10000 const { blockDifference, currentBlockNumber } = await this.getBlocksDiff({ fromBlock }) let numberParts = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange) @@ -311,15 +314,17 @@ class EventService { async getEventsFromRpc({ fromBlock, type }) { try { + const { blockDifference } = await this.getBlocksDiff({ fromBlock }) let events - if (Number(this.netId) === 1) { + if (blockDifference < 10000) { const rpcEvents = await this.getEventsPartFromRpc({ fromBlock, toBlock: 'latest', type }) events = rpcEvents?.events || [] } else { const rpcEvents = await this.getBatchEventsFromRpc({ fromBlock, type }) events = rpcEvents?.events || [] } + return events } catch (err) { return [] @@ -332,6 +337,7 @@ class EventService { const rpcEvents = await this.getEventsFromRpc({ fromBlock, type }) const allEvents = [].concat(rpcEvents || []) + if (allEvents.length) { return { events: allEvents,