improve batching, fix cache & events

This commit is contained in:
gozzy 2022-09-21 04:24:44 +00:00
parent b277b603c9
commit 9343eeeea5
3 changed files with 18 additions and 18 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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,