condition event fetching

This commit is contained in:
gozzy 2022-11-23 12:05:23 +00:00
parent ef4104a186
commit 2ec1ed18b1
3 changed files with 42 additions and 44 deletions

@ -93,9 +93,7 @@ export default {
} }
}, },
mounted() { mounted() {
if (!this.timer) {
this.updateEvents() this.updateEvents()
}
}, },
beforeDestroy() { beforeDestroy() {
clearTimeout(this.timer) clearTimeout(this.timer)
@ -103,10 +101,6 @@ export default {
methods: { methods: {
updateEvents() { updateEvents() {
this.$store.dispatch('application/updateSelectEvents') this.$store.dispatch('application/updateSelectEvents')
this.timer = setTimeout(() => {
this.updateEvents()
}, 60 * 1000)
} }
} }
} }

@ -4,7 +4,7 @@ import graph from '@/services/graph'
import { download } from '@/store/snark' import { download } from '@/store/snark'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
import InstanceABI from '@/abis/Instance.abi.json' import InstanceABI from '@/abis/Instance.abi.json'
import { CONTRACT_INSTANCES, eventsType, corsConfig } from '@/constants' import { CONTRACT_INSTANCES, eventsType, httpConfig } from '@/constants'
import { sleep, flattenNArray, formatEvents, capitalizeFirstLetter } from '@/utils' import { sleep, flattenNArray, formatEvents, capitalizeFirstLetter } from '@/utils'
const supportedCaches = ['1', '56', '100', '137'] const supportedCaches = ['1', '56', '100', '137']
@ -332,8 +332,6 @@ class EventService {
} }
async getBatchEventsFromRpc({ fromBlock, type }) { async getBatchEventsFromRpc({ fromBlock, type }) {
this.updateEventProgress(0, type)
try { try {
const batchSize = 10 const batchSize = 10
const blockRange = 10000 const blockRange = 10000
@ -349,6 +347,8 @@ class EventService {
let events = [] let events = []
if (fromBlock < currentBlockNumber) { if (fromBlock < currentBlockNumber) {
this.updateEventProgress(0, type)
for (let batchIndex = 0; batchIndex < batchCount; batchIndex++) { for (let batchIndex = 0; batchIndex < batchCount; batchIndex++) {
const batch = await Promise.all( const batch = await Promise.all(
this.createBatchRequest({ batchIndex, batchBlocks, blockDenom, batchSize, type }) this.createBatchRequest({ batchIndex, batchBlocks, blockDenom, batchSize, type })
@ -375,10 +375,11 @@ class EventService {
async getEventsFromRpc({ fromBlock, type }) { async getEventsFromRpc({ fromBlock, type }) {
try { try {
const { blockDifference } = await this.getBlocksDiff({ fromBlock }) const { blockDifference } = await this.getBlocksDiff({ fromBlock })
const blockRange = 10000
let events let events
if (blockDifference < 10000) { if (blockDifference < blockRange) {
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 {
@ -441,7 +442,7 @@ class EventsFactory {
instances = new Map() instances = new Map()
constructor(rpcUrl) { constructor(rpcUrl) {
const httpProvider = new Web3.providers.HttpProvider(rpcUrl, corsConfig(rpcUrl)) const httpProvider = new Web3.providers.HttpProvider(rpcUrl, httpConfig)
this.provider = new Web3(httpProvider).eth this.provider = new Web3(httpProvider).eth
} }

@ -4,7 +4,7 @@ import { BigNumber as BN } from 'bignumber.js'
import { toChecksumAddress, isAddress } from 'web3-utils' import { toChecksumAddress, isAddress } from 'web3-utils'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
import { REGISTRY_DEPLOYED_BLOCK, corsConfig } from '@/constants' import { REGISTRY_DEPLOYED_BLOCK } from '@/constants'
import { sleep, flattenNArray } from '@/utils' import { sleep, flattenNArray } from '@/utils'
import AggregatorABI from '@/abis/Aggregator.abi.json' import AggregatorABI from '@/abis/Aggregator.abi.json'
@ -25,9 +25,9 @@ class RelayerRegister {
this.relayerRegistry = new this.provider.Contract(RelayerRegistryABI, registryContract) this.relayerRegistry = new this.provider.Contract(RelayerRegistryABI, registryContract)
} }
fetchEvents = ({ fromBlock, toBlock }) => { fetchEvents = ({ fromBlock, toBlock }, shouldRetry = false) => {
if (fromBlock <= toBlock) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (fromBlock <= toBlock) {
try { try {
const registeredEventsPart = this.relayerRegistry.getPastEvents('RelayerRegistered', { const registeredEventsPart = this.relayerRegistry.getPastEvents('RelayerRegistered', {
fromBlock, fromBlock,
@ -36,22 +36,20 @@ class RelayerRegister {
resolve(registeredEventsPart) resolve(registeredEventsPart)
} catch (error) { } catch (error) {
sleep(200) if (shouldRetry) {
sleep(1000)
const midBlock = (fromBlock + toBlock) >> 1 const events = this.fetchEvents({ fromBlock, toBlock })
if (midBlock - fromBlock < 2) { resolve(events)
reject(new Error(`error fetching events: ${error.message}`)) } else {
reject(new Error(error))
} }
}
const arr1 = this.fetchEvents({ fromBlock, toBlock: midBlock }) } else {
const arr2 = this.fetchEvents({ fromBlock: midBlock + 1, toBlock }) resolve([])
resolve([...arr1, ...arr2])
} }
}) })
} else {
return []
}
} }
batchFetchEvents = async ({ fromBlock, toBlock }) => { batchFetchEvents = async ({ fromBlock, toBlock }) => {
@ -60,10 +58,19 @@ class RelayerRegister {
const chunkCount = Math.ceil(blockDifference / blockRange) const chunkCount = Math.ceil(blockDifference / blockRange)
const blockDenom = Math.ceil(blockDifference / chunkCount) const blockDenom = Math.ceil(blockDifference / chunkCount)
const promises = new Array(chunkCount).fill('').map((_, i) => const promises = new Array(chunkCount).fill('').map(
this.fetchEvents({ (_, i) =>
new Promise((resolve) => {
sleep(300)
const batch = this.fetchEvents(
{
fromBlock: i * blockDenom + fromBlock, fromBlock: i * blockDenom + fromBlock,
toBlock: (i + 1) * blockDenom + fromBlock toBlock: (i + 1) * blockDenom + fromBlock
},
true
)
resolve(batch)
}) })
) )
@ -123,8 +130,7 @@ class RelayerRegister {
getENSAddress = async (ensName) => { getENSAddress = async (ensName) => {
const { url } = Object.values(networkConfig.netId1.rpcUrls)[0] const { url } = Object.values(networkConfig.netId1.rpcUrls)[0]
const httpProvider = new Web3.providers.HttpProvider(url, corsConfig(url)) const provider = new Web3(url)
const provider = new Web3(httpProvider)
const ensAddress = await provider.eth.ens.getAddress(ensName) const ensAddress = await provider.eth.ens.getAddress(ensName)
@ -144,24 +150,21 @@ class RelayerRegister {
try { try {
let toBlock let toBlock
let registerRelayerEvents let registerRelayerEvents
let lastBlock let lastSyncBlock = blockTo
if (cachedEvents.length > 0 || blockDifference === 0) { if (cachedEvents.length > 0 || blockDifference === 0) {
return cachedEvents return cachedEvents
} else if (blockDifference >= blockRange) { } else if (blockDifference >= blockRange) {
toBlock = currentBlockNumber toBlock = currentBlockNumber
registerRelayerEvents = await this.batchFetchEvents({ fromBlock, toBlock }) registerRelayerEvents = await this.batchFetchEvents({ fromBlock, toBlock })
lastBlock = toBlock lastSyncBlock = toBlock
} else { } else {
toBlock = fromBlock + blockRange toBlock = fromBlock + blockRange
registerRelayerEvents = await this.fetchEvents({ fromBlock, toBlock }) registerRelayerEvents = await this.fetchEvents({ fromBlock, toBlock }, true)
lastBlock = toBlock lastSyncBlock = toBlock
} }
const lastIndex = registerRelayerEvents.length - 1
const lastSyncBlock = registerRelayerEvents[lastIndex]?.blockNumber || lastBlock
const relayerEvents = cachedEvents.concat(registerRelayerEvents || []) const relayerEvents = cachedEvents.concat(registerRelayerEvents || [])
const events = [] const events = []
for (let x = 0; x < relayerEvents.length; x++) { for (let x = 0; x < relayerEvents.length; x++) {
@ -180,8 +183,8 @@ class RelayerRegister {
await this.saveEvents({ storeName: 'register_events', lastSyncBlock, events }) await this.saveEvents({ storeName: 'register_events', lastSyncBlock, events })
allRelayers = allRelayers.concat(events) allRelayers = allRelayers.concat(events)
} catch (e) { } catch (err) {
return cachedEvents console.log(err)
} }
return allRelayers return allRelayers
} }