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) { if (module) {
const events = JSON.parse(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 { return {
events, events,
lastBlock lastBlock: events[events.length - 1].blockNumber
} }
} }
} catch (err) { } catch (err) {
@ -68,8 +63,7 @@ export async function getPastEvents({ type, fromBlock, netId, events, contractAt
const blockDifference = Math.ceil(blockNumberBuffer - fromBlock) const blockDifference = Math.ceil(blockNumberBuffer - fromBlock)
// eth_logs and eth_filter are restricted > 10,000 block queries // eth_logs and eth_filter are restricted > 10,000 block queries
const blockDenom = blockDifference > 10000 ? 4950 : 20 const blockRange = 10000
const blockRange = blockDifference / blockDenom
let chunksCount = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange) let chunksCount = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange)
const chunkSize = Math.ceil(blockDifference / chunksCount) const chunkSize = Math.ceil(blockDifference / chunksCount)

View File

@ -64,7 +64,7 @@ async function main(type, netId) {
let freshEvents = cachedEvents.events.concat(events) let freshEvents = cachedEvents.events.concat(events)
if (type === 'Withdrawal') { 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 { } else {
freshEvents = freshEvents.filter((e, index) => Number(e.leafIndex) === index) freshEvents = freshEvents.filter((e, index) => Number(e.leafIndex) === index)
} }

View File

@ -14,7 +14,7 @@ class EventService {
this.idb = window.$nuxt.$indexedDB(netId) this.idb = window.$nuxt.$indexedDB(netId)
const { nativeCurrency } = networkConfig[`netId${netId}`] const { nativeCurrency } = networkConfig[`netId${netId}`]
const hasCache = supportedCaches.indexOf(Number(this.netId)) !== 0 const hasCache = supportedCaches.includes(netId.toString())
this.netId = netId this.netId = netId
this.amount = amount this.amount = amount
@ -37,6 +37,7 @@ class EventService {
if (!cachedEvents && this.hasCache) { if (!cachedEvents && this.hasCache) {
cachedEvents = await this.getEventsFromCache(type) cachedEvents = await this.getEventsFromCache(type)
} }
return cachedEvents return cachedEvents
} }
async updateEvents(type, cachedEvents) { async updateEvents(type, cachedEvents) {
@ -45,6 +46,7 @@ class EventService {
const savedEvents = cachedEvents || (await this.getEvents(type)) const savedEvents = cachedEvents || (await this.getEvents(type))
let fromBlock = deployedBlock let fromBlock = deployedBlock
if (savedEvents) { if (savedEvents) {
fromBlock = savedEvents.lastBlock + 1 fromBlock = savedEvents.lastBlock + 1
} }
@ -138,22 +140,23 @@ class EventService {
async getEventsFromDB(type) { async getEventsFromDB(type) {
try { try {
const instanceName = this.getInstanceName(type) const instanceName = this.getInstanceName(type)
const savedEvents = await this.idb.getAll({ storeName: instanceName }) const savedEvents = await this.idb.getAll({ storeName: instanceName })
if (!savedEvents || !savedEvents.length) { if (!savedEvents || !savedEvents.length) {
return undefined return undefined
} }
const event = await this.idb.getFromIndex({ // IndexedDB scrambles assortment
storeName: 'lastEvents', savedEvents.sort((a, b) => {
indexName: 'name', if (a.leafIndex && b.leafIndex) {
key: instanceName return a.leafIndex - b.leafIndex
}
return a.blockNumber - b.blockNumber
}) })
return { return {
events: savedEvents, events: savedEvents,
lastBlock: event.blockNumber lastBlock: savedEvents[savedEvents.length - 1].blockNumber
} }
} catch (err) { } catch (err) {
return undefined return undefined
@ -268,7 +271,7 @@ class EventService {
async getBatchEventsFromRpc({ fromBlock, type }) { async getBatchEventsFromRpc({ fromBlock, type }) {
try { try {
const blockRange = 4950 const blockRange = 10000
const { blockDifference, currentBlockNumber } = await this.getBlocksDiff({ fromBlock }) const { blockDifference, currentBlockNumber } = await this.getBlocksDiff({ fromBlock })
let numberParts = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange) let numberParts = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange)
@ -311,15 +314,17 @@ class EventService {
async getEventsFromRpc({ fromBlock, type }) { async getEventsFromRpc({ fromBlock, type }) {
try { try {
const { blockDifference } = await this.getBlocksDiff({ fromBlock })
let events let events
if (Number(this.netId) === 1) { if (blockDifference < 10000) {
const rpcEvents = await this.getEventsPartFromRpc({ fromBlock, toBlock: 'latest', type }) const rpcEvents = await this.getEventsPartFromRpc({ fromBlock, toBlock: 'latest', type })
events = rpcEvents?.events || [] events = rpcEvents?.events || []
} else { } else {
const rpcEvents = await this.getBatchEventsFromRpc({ fromBlock, type }) const rpcEvents = await this.getBatchEventsFromRpc({ fromBlock, type })
events = rpcEvents?.events || [] events = rpcEvents?.events || []
} }
return events return events
} catch (err) { } catch (err) {
return [] return []
@ -332,6 +337,7 @@ class EventService {
const rpcEvents = await this.getEventsFromRpc({ fromBlock, type }) const rpcEvents = await this.getEventsFromRpc({ fromBlock, type })
const allEvents = [].concat(rpcEvents || []) const allEvents = [].concat(rpcEvents || [])
if (allEvents.length) { if (allEvents.length) {
return { return {
events: allEvents, events: allEvents,