From 497675ad277e3109760d06ef7cdd225a635889b6 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Tue, 14 Jun 2022 20:02:45 +1000 Subject: [PATCH] fix: separate idb by netId --- plugins/idb.js | 97 ++++++++++++++++++-------------------- services/bloom.js | 4 +- services/events.js | 27 +++++------ services/merkleTree.js | 3 +- services/registry/index.js | 6 +-- store/application.js | 20 ++++---- 6 files changed, 78 insertions(+), 79 deletions(-) diff --git a/plugins/idb.js b/plugins/idb.js index 5850e8b..1516961 100644 --- a/plugins/idb.js +++ b/plugins/idb.js @@ -1,7 +1,7 @@ import { openDB, deleteDB } from 'idb' import networkConfig from '@/networkConfig' -import { INDEX_DB_ERROR, NETWORKS } from '@/constants' +import { INDEX_DB_ERROR } from '@/constants' // TODO method for migration, remove indexed class IndexedDB { @@ -222,7 +222,9 @@ class IndexedDB { } } -export default async (ctx, inject) => { +export default (ctx, inject) => { + const instances = new Map() + const DEPOSIT_INDEXES = [ { name: 'transactionHash', unique: false }, { name: 'commitment', unique: true } @@ -232,7 +234,6 @@ export default async (ctx, inject) => { ] const LAST_EVENT_INDEXES = [{ name: 'name', unique: false }] - // TODO: generate from config const defaultState = [ { name: 'encrypted_events', @@ -245,66 +246,62 @@ export default async (ctx, inject) => { } ] - const stores = [ - { - name: 'register_events', - keyPath: 'ensName' - } - ] - - NETWORKS.forEach((netId) => { - defaultState.map((item) => { - stores.push({ - ...item, - name: `${item.name}_${netId}` - }) - }) - }) - - Object.keys(networkConfig).forEach((key) => { + Object.keys(networkConfig).forEach(async (key) => { const { tokens, nativeCurrency } = networkConfig[key] - const netId = key.replace('netId', '') + const netId = Number(key.replace('netId', '')) + + const stores = [...defaultState] + + if (netId === 1) { + stores.push({ + name: 'register_events', + keyPath: 'ensName' + }) + } Object.keys(tokens).forEach((token) => { Object.keys(tokens[token].instanceAddress).forEach((amount) => { - if (nativeCurrency === token) { - // ToDo make good) + if (nativeCurrency === token && netId === 1) { stores.push({ - name: `stringify_bloom_${token}_${amount}_${netId}`, + name: `stringify_bloom_${token}_${amount}`, keyPath: 'hashBloom' }) } - stores.push({ - name: `deposits_${token}_${amount}_${netId}`, - keyPath: 'leafIndex', // the key by which it refers to the object must be in all instances of the storage - indexes: DEPOSIT_INDEXES - }) - - stores.push({ - name: `withdrawals_${token}_${amount}_${netId}`, - keyPath: 'transactionHash', - indexes: WITHDRAWAL_INDEXES - }) - - stores.push({ - name: `stringify_tree_${token}_${amount}_${netId}`, - keyPath: 'hashTree' - }) + stores.push( + { + name: `deposits_${token}_${amount}`, + keyPath: 'leafIndex', // the key by which it refers to the object must be in all instances of the storage + indexes: DEPOSIT_INDEXES + }, + { + name: `withdrawals_${token}_${amount}`, + keyPath: 'transactionHash', + indexes: WITHDRAWAL_INDEXES + }, + { + name: `stringify_tree_${token}_${amount}`, + keyPath: 'hashTree' + } + ) }) }) + + const options = { + stores, + dbName: `tornado_cash_${netId}` + } + + const instance = new IndexedDB(options) + + await instance.initDB() + + instances.set(options.dbName, instance) }) - const options = { - stores, - dbName: 'tornado_cash' - } + const getInstance = (netId) => instances.get(`tornado_cash_${netId}`) - const instance = new IndexedDB(options) - - await instance.initDB() - - ctx.$indexedDB = instance - inject('indexedDB', instance) + ctx.$indexedDB = getInstance + inject('indexedDB', getInstance) } diff --git a/services/bloom.js b/services/bloom.js index 8a00da1..143617b 100644 --- a/services/bloom.js +++ b/services/bloom.js @@ -3,14 +3,14 @@ import BloomFilter from 'bloomfilter.js' import { download } from '@/store/snark' class BloomService { - constructor({ amount, commitment, instanceName, fileName, fileFolder }) { + constructor({ netId, amount, commitment, instanceName, fileName, fileFolder }) { this.amount = amount this.fileFolder = fileFolder this.commitment = commitment this.instanceName = instanceName this.fileName = `${fileFolder}/${fileName}` - this.idb = window.$nuxt.$indexedDB + this.idb = window.$nuxt.$indexedDB(netId) } async downloadBloom() { diff --git a/services/events.js b/services/events.js index 7be31ae..1e7e1e9 100644 --- a/services/events.js +++ b/services/events.js @@ -9,7 +9,7 @@ import { sleep, formatEvents, capitalizeFirstLetter } from '@/utils' class EventService { constructor({ netId, amount, currency, factoryMethods }) { - this.idb = window.$nuxt.$indexedDB + this.idb = window.$nuxt.$indexedDB(netId) const { nativeCurrency } = networkConfig[`netId${netId}`] @@ -24,11 +24,8 @@ class EventService { this.hasCache = this.isNative && (Number(this.netId) === 1 || Number(this.netId) === 56) } - getStoreNames(type) { - const instanceName = `${type}s_${this.currency}_${this.amount}` - const storeName = `${instanceName}_${this.netId}` - - return { instanceName, storeName } + getInstanceName(type) { + return `${type}s_${this.currency}_${this.amount}` } async getEvents(type) { @@ -72,10 +69,10 @@ class EventService { } } async findEvent({ eventName, eventToFind, type }) { - const { storeName } = this.getStoreNames(type) + const instanceName = this.getInstanceName(type) let event = await this.idb.getFromIndex({ - storeName, + storeName: instanceName, indexName: eventName, key: eventToFind }) @@ -106,7 +103,7 @@ class EventService { async getEventsFromCache(type) { try { - const { instanceName } = this.getStoreNames(type) + const instanceName = this.getInstanceName(type) if (!CONTRACT_INSTANCES.includes(String(this.amount))) { console.error(`Amount doesn't includes in contract instances`) return @@ -137,16 +134,16 @@ class EventService { async getEventsFromDB(type) { try { - const { storeName, instanceName } = this.getStoreNames(type) + const instanceName = this.getInstanceName(type) - const savedEvents = await this.idb.getAll({ storeName }) + const savedEvents = await this.idb.getAll({ storeName: instanceName }) if (!savedEvents || !savedEvents.length) { return undefined } const event = await this.idb.getFromIndex({ - storeName: `lastEvents_${this.netId}`, + storeName: 'lastEvents', indexName: 'name', key: instanceName }) @@ -334,11 +331,11 @@ class EventService { return } - const { instanceName, storeName } = this.getStoreNames(type) + const instanceName = this.getInstanceName(type) await this.idb.createMultipleTransactions({ data: events, - storeName + storeName: instanceName }) await this.idb.putItem({ @@ -346,7 +343,7 @@ class EventService { blockNumber: lastBlock, name: instanceName }, - storeName: `lastEvents_${this.netId}` + storeName: 'lastEvents' }) } catch (err) { console.error('saveEvents has error:', err.message) diff --git a/services/merkleTree.js b/services/merkleTree.js index 0080709..46fad51 100644 --- a/services/merkleTree.js +++ b/services/merkleTree.js @@ -13,8 +13,9 @@ class MerkleTreeService { this.commitment = commitment this.instanceName = instanceName - this.idb = window.$nuxt.$indexedDB + this.idb = window.$nuxt.$indexedDB(netId) this.bloomService = bloomService({ + netId, amount, commitment, instanceName, diff --git a/services/registry/index.js b/services/registry/index.js index 110169f..cacaba4 100644 --- a/services/registry/index.js +++ b/services/registry/index.js @@ -16,7 +16,7 @@ const subdomains = Object.values(networkConfig).map(({ ensSubdomainKey }) => ens class RelayerRegister { constructor(provider) { this.provider = provider - this.$indexedDB = window.$nuxt.$indexedDB + this.$indexedDB = window.$nuxt.$indexedDB(1) const { registryContract, aggregatorContract } = networkConfig.netId1 @@ -59,7 +59,7 @@ class RelayerRegister { blockNumber: lastSyncBlock, name: storeName }, - storeName: 'lastEvents_1' + storeName: 'lastEvents' }) if (events.length) { @@ -83,7 +83,7 @@ class RelayerRegister { const lastBlock = await this.$indexedDB.getFromIndex({ indexName: 'name', key: 'register_events', - storeName: 'lastEvents_1' + storeName: 'lastEvents' }) if (lastBlock) { diff --git a/store/application.js b/store/application.js index 296832c..514c585 100644 --- a/store/application.js +++ b/store/application.js @@ -333,9 +333,9 @@ const actions = { const { deployedBlock } = networkConfig[`netId${netId}`] if (currency === nativeCurrency && !lastEvent) { - lastBlock = await this.$indexedDB.getFromIndex({ + lastBlock = await this.$indexedDB(netId).getFromIndex({ indexName: 'name', - storeName: `lastEvents_${netId}`, + storeName: 'lastEvents', key: `${type}s_${currency}_${amount}` }) } @@ -430,11 +430,13 @@ const actions = { }, async getEncryptedEventsFromDb(_, { netId }) { try { - if (this.$indexedDB.isBlocked) { + const idb = this.$indexedDB(netId) + + if (idb.isBlocked) { return [] } - const cachedEvents = await this.$indexedDB.getAll({ storeName: `encrypted_events_${netId}` }) + const cachedEvents = await idb.getAll({ storeName: 'encrypted_events' }) return cachedEvents } catch (err) { @@ -541,13 +543,15 @@ const actions = { } }, async saveEncryptedEventsToDB(_, { events, netId }) { - if (!events || !events.length || this.$indexedDB.isBlocked) { + const idb = this.$indexedDB(netId) + + if (!events || !events.length || idb.isBlocked) { return } - await this.$indexedDB.createMultipleTransactions({ + await idb.createMultipleTransactions({ data: events, - storeName: `encrypted_events_${netId}` + storeName: `encrypted_events` }) }, async sendDeposit({ state, rootState, getters, rootGetters, dispatch, commit }, { isEncrypted, gasPrice }) { @@ -670,7 +674,7 @@ const actions = { } }, async buildTree({ dispatch }, { currency, amount, netId, commitmentHex }) { - const treeInstanceName = `${currency}_${amount}_${netId}` + const treeInstanceName = `${currency}_${amount}` const params = { netId, amount, currency } const treeService = treesInterface.getService({