fallback event indexing & extend caching support

This commit is contained in:
gozzy 2022-09-08 11:14:09 +00:00
parent a83fae0772
commit 1b922fa445
8 changed files with 84 additions and 27 deletions

0
assets/img/metamask-fox.svg Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -0,0 +1,54 @@
import networkConfig from '../networkConfig'
import ABI from '../abis/Instance.abi.json'
import { loadCachedEvents, getPastEvents } from './helpers'
const EVENTS_PATH = './static/events/'
const enabledChains = ['1', '56', '100', '137' ]
async function main() {
for (let network in enabledChains) {
const netId = enabledChains[network]
const config = networkConfig[`netId${netId}`]
const { constants, tokens, nativeCurrency, deployedBlock } = config
const CONTRACTS = tokens[nativeCurrency].instanceAddress
console.log(`\n ::: ${netId} [${nativeCurrency.toUpperCase()}] :::`)
for (const [instance, _contract] of Object.entries(CONTRACTS)) {
console.log(`\n instanceDenomation - ${instance}`)
const withdrawalCachedEvents = await loadCachedEvents({
name: `withdrawals_${nativeCurrency}_${instance}.json`,
directory: EVENTS_PATH,
deployedBlock
})
console.log('- Withdrawals')
console.log('cachedEvents count - ', withdrawalCachedEvents.events.length)
console.log('lastBlock - ', withdrawalCachedEvents.lastBlock)
const depositCachedEvents = await loadCachedEvents({
name: `withdrawals_${nativeCurrency}_${instance}.json`,
directory: EVENTS_PATH,
deployedBlock
})
console.log('- Deposits')
console.log('cachedEvents count - ', depositCachedEvents.events.length)
console.log('lastBlock - ', depositCachedEvents.lastBlock)
const notesCachedEvents = await loadCachedEvents({
name: `encrypted_notes_${netId}.json`,
directory: EVENTS_PATH,
deployedBlock: constants.ENCRYPTED_NOTES_BLOCK
})
console.log('- Notes')
console.log('cachedEvents count - ', notesCachedEvents.events.length)
console.log('lastBlock - ', notesCachedEvents.lastBlock)
}
}
}
main()

View File

@ -55,7 +55,7 @@ export async function getPastEvents({ type, fromBlock, netId, events, contractAt
let [{ url: rpcUrl }] = Object.values(networkConfig[`netId${netId}`].rpcUrls) let [{ url: rpcUrl }] = Object.values(networkConfig[`netId${netId}`].rpcUrls)
if (netId === '5') { if (netId === '5') {
rpcUrl = `https://goerli.infura.io/v3/${process.env.INFURA_KEY}` rpcUrl = 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'
} }
const provider = new Web3.providers.HttpProvider(rpcUrl) const provider = new Web3.providers.HttpProvider(rpcUrl)
@ -63,9 +63,13 @@ export async function getPastEvents({ type, fromBlock, netId, events, contractAt
const contract = new web3.eth.Contract(...contractAttrs) const contract = new web3.eth.Contract(...contractAttrs)
const currentBlockNumber = await web3.eth.getBlockNumber() const currentBlockNumber = await web3.eth.getBlockNumber()
const blockDifference = Math.ceil(currentBlockNumber - fromBlock) // PoS networks index blocks too fast, so a buffer is needed
const blockNumberBuffer = currentBlockNumber - 3
const blockDifference = Math.ceil(blockNumberBuffer - fromBlock)
const blockRange = Number(netId) === 56 ? 4950 : blockDifference / 20 // eth_logs and eth_filter are restricted > 10,000 block queries
const blockDenom = blockDifference > 10000 ? 4950 : 20
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)
@ -97,7 +101,6 @@ export async function getPastEvents({ type, fromBlock, netId, events, contractAt
toBlock += chunkSize toBlock += chunkSize
} catch (err) { } catch (err) {
console.log('getPastEvents events', `chunk number - ${i}, has error: ${err.message}`) console.log('getPastEvents events', `chunk number - ${i}, has error: ${err.message}`)
chunksCount = chunksCount + 1
} }
} }
return downloadedEvents return downloadedEvents

View File

@ -8,7 +8,7 @@ import ABI from '../abis/TornadoProxy.abi.json'
import { getPastEvents, loadCachedEvents } from './helpers' import { getPastEvents, loadCachedEvents } from './helpers'
const EVENTS_PATH = './static/events/' const EVENTS_PATH = './static/events/'
const enabledChains = ['1', '5', '56'] const enabledChains = ['1', '5', '56', '100', '137']
async function saveEncryptedNote(netId) { async function saveEncryptedNote(netId) {
const { const {

View File

@ -9,7 +9,7 @@ import { loadCachedEvents, getPastEvents } from './helpers'
const EVENTS_PATH = './static/events/' const EVENTS_PATH = './static/events/'
const EVENTS = ['Deposit', 'Withdrawal'] const EVENTS = ['Deposit', 'Withdrawal']
const enabledChains = ['1', '56', '5'] const enabledChains = ['1', '56', '5', '100', '137' ]
async function main(type, netId) { async function main(type, netId) {
const { tokens, nativeCurrency, deployedBlock } = networkConfig[`netId${netId}`] const { tokens, nativeCurrency, deployedBlock } = networkConfig[`netId${netId}`]

View File

@ -14,7 +14,7 @@ const TREES_PATH = './static/trees/'
const EVENTS_PATH = './static/events/' const EVENTS_PATH = './static/events/'
const EVENTS = ['deposit'] const EVENTS = ['deposit']
const enabledChains = ['1'] const enabledChains = ['1', '56', '100', '137' ]
let mimcHash let mimcHash
const trees = { const trees = {
@ -28,8 +28,8 @@ function getName({ path, type, instance, format = '.json', currName = 'eth' }) {
function createTreeZip(netId) { function createTreeZip(netId) {
try { try {
const config = networkConfig[`netId${netId}`] const { tokens, nativeCurrency, currencyName } = networkConfig[`netId${netId}`]
const { instanceAddress: CONTRACTS } = config.tokens.eth const CONTRACTS = tokens[nativeCurrency].instanceAddress
for (const type of EVENTS) { for (const type of EVENTS) {
for (const [instance] of Object.entries(CONTRACTS)) { for (const [instance] of Object.entries(CONTRACTS)) {
@ -38,7 +38,7 @@ function createTreeZip(netId) {
instance, instance,
format: '', format: '',
path: TREES_PATH, path: TREES_PATH,
currName: config.currencyName.toLowerCase() currName: currencyName.toLowerCase()
}) })
const treesFolder = fs.readdirSync(TREES_FOLDER) const treesFolder = fs.readdirSync(TREES_FOLDER)
@ -58,25 +58,24 @@ function createTreeZip(netId) {
async function createTree(netId) { async function createTree(netId) {
try { try {
const { currencyName, tokens, deployedBlock } = networkConfig[`netId${netId}`] const config = networkConfig[`netId${netId}`]
const { nativeCurrency, currencyName, deployedBlock } = config
const currName = currencyName.toLowerCase() const CONTRACTS = config.tokens[nativeCurrency].instanceAddress
const { instanceAddress: CONTRACTS } = tokens.eth
for (const type of EVENTS) { for (const type of EVENTS) {
for (const [instance] of Object.entries(CONTRACTS)) { for (const [instance] of Object.entries(CONTRACTS)) {
const filePath = getName({ const filePath = getName({
type, type,
instance, instance,
currName,
format: '', format: '',
path: TREES_PATH path: TREES_PATH,
currName: currencyName.toLowerCase()
}) })
console.log('createTree', { type, instance }) console.log('createTree', { type, instance })
const { events } = await loadCachedEvents({ const { events } = await loadCachedEvents({
name: `${type}s_${currName}_${instance}.json`, name: `${type}s_${nativeCurrency}_${instance}.json`,
directory: EVENTS_PATH, directory: EVENTS_PATH,
deployedBlock deployedBlock
}) })

View File

@ -58,7 +58,7 @@ async function testCommon(netId, type, filename) {
} }
async function main() { async function main() {
const NETWORKS = [1, 5, 56] const NETWORKS = [1, 5, 56, 100, 137 ]
for await (const netId of NETWORKS) { for await (const netId of NETWORKS) {
updateEncrypted(netId) updateEncrypted(netId)

View File

@ -1,12 +1,13 @@
import Web3 from 'web3' import Web3 from 'web3'
import { graph } from '@/services'
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 } from '@/constants' import { CONTRACT_INSTANCES, eventsType } from '@/constants'
import { sleep, formatEvents, capitalizeFirstLetter } from '@/utils' import { sleep, formatEvents, capitalizeFirstLetter } from '@/utils'
const supportedNetworkCaches = [ '1', '56', '100', '137' ]
class EventService { class EventService {
constructor({ netId, amount, currency, factoryMethods }) { constructor({ netId, amount, currency, factoryMethods }) {
this.idb = window.$nuxt.$indexedDB(netId) this.idb = window.$nuxt.$indexedDB(netId)
@ -21,7 +22,8 @@ class EventService {
this.contract = this.getContract({ netId, amount, currency }) this.contract = this.getContract({ netId, amount, currency })
this.isNative = nativeCurrency === this.currency this.isNative = nativeCurrency === this.currency
this.hasCache = this.isNative && (Number(this.netId) === 1 || Number(this.netId) === 56) this.hasCache = this.isNative
&& supportedNetworkCaches.indexOf(this.netId) !== 0
} }
getInstanceName(type) { getInstanceName(type) {
@ -310,11 +312,11 @@ class EventService {
try { try {
let events let events
if (Number(this.netId) === 56) { if (Number(this.netId) === 1) {
const rpcEvents = await this.getBatchEventsFromRpc({ fromBlock, type }) const rpcEvents = await this.getEventsPartFromRpc({ fromBlock, toBlock: 'latest', type })
events = rpcEvents?.events || [] events = rpcEvents?.events || []
} else { } else {
const rpcEvents = await this.getEventsPartFromRpc({ fromBlock, toBlock: 'latest', type }) const rpcEvents = await this.getBatchEventsFromRpc({ fromBlock, type })
events = rpcEvents?.events || [] events = rpcEvents?.events || []
} }
return events return events
@ -326,11 +328,9 @@ class EventService {
async getEventsFromBlock({ fromBlock, graphMethod, type }) { async getEventsFromBlock({ fromBlock, graphMethod, type }) {
try { try {
// ToDo think about undefined // ToDo think about undefined
const graphEvents = await this.getEventsFromGraph({ fromBlock, methodName: graphMethod }) const rpcEvents = await this.getEventsFromRpc({ fromBlock, type })
const lastSyncBlock = fromBlock > graphEvents?.lastBlock ? fromBlock : graphEvents?.lastBlock
const rpcEvents = await this.getEventsFromRpc({ fromBlock: lastSyncBlock, type })
const allEvents = [].concat(graphEvents?.events || [], rpcEvents || []) const allEvents = [].concat(rpcEvents || [])
if (allEvents.length) { if (allEvents.length) {
return { return {
events: allEvents, events: allEvents,
@ -386,6 +386,7 @@ class EventsFactory {
getService = (payload) => { getService = (payload) => {
const instanceName = `${payload.currency}_${payload.amount}` const instanceName = `${payload.currency}_${payload.amount}`
if (this.instances.has(instanceName)) { if (this.instances.has(instanceName)) {
return this.instances.get(instanceName) return this.instances.get(instanceName)
} }