fix: separate idb by netId

This commit is contained in:
Danil Kovtonyuk 2022-06-14 20:02:45 +10:00
parent c7d1a5cb8e
commit 497675ad27
6 changed files with 78 additions and 79 deletions

@ -1,7 +1,7 @@
import { openDB, deleteDB } from 'idb' import { openDB, deleteDB } from 'idb'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
import { INDEX_DB_ERROR, NETWORKS } from '@/constants' import { INDEX_DB_ERROR } from '@/constants'
// TODO method for migration, remove indexed // TODO method for migration, remove indexed
class IndexedDB { class IndexedDB {
@ -222,7 +222,9 @@ class IndexedDB {
} }
} }
export default async (ctx, inject) => { export default (ctx, inject) => {
const instances = new Map()
const DEPOSIT_INDEXES = [ const DEPOSIT_INDEXES = [
{ name: 'transactionHash', unique: false }, { name: 'transactionHash', unique: false },
{ name: 'commitment', unique: true } { name: 'commitment', unique: true }
@ -232,7 +234,6 @@ export default async (ctx, inject) => {
] ]
const LAST_EVENT_INDEXES = [{ name: 'name', unique: false }] const LAST_EVENT_INDEXES = [{ name: 'name', unique: false }]
// TODO: generate from config
const defaultState = [ const defaultState = [
{ {
name: 'encrypted_events', name: 'encrypted_events',
@ -245,66 +246,62 @@ export default async (ctx, inject) => {
} }
] ]
const stores = [ Object.keys(networkConfig).forEach(async (key) => {
{
name: 'register_events',
keyPath: 'ensName'
}
]
NETWORKS.forEach((netId) => {
defaultState.map((item) => {
stores.push({
...item,
name: `${item.name}_${netId}`
})
})
})
Object.keys(networkConfig).forEach((key) => {
const { tokens, nativeCurrency } = networkConfig[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).forEach((token) => {
Object.keys(tokens[token].instanceAddress).forEach((amount) => { Object.keys(tokens[token].instanceAddress).forEach((amount) => {
if (nativeCurrency === token) { if (nativeCurrency === token && netId === 1) {
// ToDo make good)
stores.push({ stores.push({
name: `stringify_bloom_${token}_${amount}_${netId}`, name: `stringify_bloom_${token}_${amount}`,
keyPath: 'hashBloom' keyPath: 'hashBloom'
}) })
} }
stores.push({ stores.push(
name: `deposits_${token}_${amount}_${netId}`, {
name: `deposits_${token}_${amount}`,
keyPath: 'leafIndex', // the key by which it refers to the object must be in all instances of the storage keyPath: 'leafIndex', // the key by which it refers to the object must be in all instances of the storage
indexes: DEPOSIT_INDEXES indexes: DEPOSIT_INDEXES
}) },
{
stores.push({ name: `withdrawals_${token}_${amount}`,
name: `withdrawals_${token}_${amount}_${netId}`,
keyPath: 'transactionHash', keyPath: 'transactionHash',
indexes: WITHDRAWAL_INDEXES indexes: WITHDRAWAL_INDEXES
}) },
{
stores.push({ name: `stringify_tree_${token}_${amount}`,
name: `stringify_tree_${token}_${amount}_${netId}`,
keyPath: 'hashTree' keyPath: 'hashTree'
}) }
}) )
}) })
}) })
const options = { const options = {
stores, stores,
dbName: 'tornado_cash' dbName: `tornado_cash_${netId}`
} }
const instance = new IndexedDB(options) const instance = new IndexedDB(options)
await instance.initDB() await instance.initDB()
ctx.$indexedDB = instance instances.set(options.dbName, instance)
inject('indexedDB', instance) })
const getInstance = (netId) => instances.get(`tornado_cash_${netId}`)
ctx.$indexedDB = getInstance
inject('indexedDB', getInstance)
} }

@ -3,14 +3,14 @@ import BloomFilter from 'bloomfilter.js'
import { download } from '@/store/snark' import { download } from '@/store/snark'
class BloomService { class BloomService {
constructor({ amount, commitment, instanceName, fileName, fileFolder }) { constructor({ netId, amount, commitment, instanceName, fileName, fileFolder }) {
this.amount = amount this.amount = amount
this.fileFolder = fileFolder this.fileFolder = fileFolder
this.commitment = commitment this.commitment = commitment
this.instanceName = instanceName this.instanceName = instanceName
this.fileName = `${fileFolder}/${fileName}` this.fileName = `${fileFolder}/${fileName}`
this.idb = window.$nuxt.$indexedDB this.idb = window.$nuxt.$indexedDB(netId)
} }
async downloadBloom() { async downloadBloom() {

@ -9,7 +9,7 @@ import { sleep, formatEvents, capitalizeFirstLetter } from '@/utils'
class EventService { class EventService {
constructor({ netId, amount, currency, factoryMethods }) { constructor({ netId, amount, currency, factoryMethods }) {
this.idb = window.$nuxt.$indexedDB this.idb = window.$nuxt.$indexedDB(netId)
const { nativeCurrency } = networkConfig[`netId${netId}`] const { nativeCurrency } = networkConfig[`netId${netId}`]
@ -24,11 +24,8 @@ class EventService {
this.hasCache = this.isNative && (Number(this.netId) === 1 || Number(this.netId) === 56) this.hasCache = this.isNative && (Number(this.netId) === 1 || Number(this.netId) === 56)
} }
getStoreNames(type) { getInstanceName(type) {
const instanceName = `${type}s_${this.currency}_${this.amount}` return `${type}s_${this.currency}_${this.amount}`
const storeName = `${instanceName}_${this.netId}`
return { instanceName, storeName }
} }
async getEvents(type) { async getEvents(type) {
@ -72,10 +69,10 @@ class EventService {
} }
} }
async findEvent({ eventName, eventToFind, type }) { async findEvent({ eventName, eventToFind, type }) {
const { storeName } = this.getStoreNames(type) const instanceName = this.getInstanceName(type)
let event = await this.idb.getFromIndex({ let event = await this.idb.getFromIndex({
storeName, storeName: instanceName,
indexName: eventName, indexName: eventName,
key: eventToFind key: eventToFind
}) })
@ -106,7 +103,7 @@ class EventService {
async getEventsFromCache(type) { async getEventsFromCache(type) {
try { try {
const { instanceName } = this.getStoreNames(type) const instanceName = this.getInstanceName(type)
if (!CONTRACT_INSTANCES.includes(String(this.amount))) { if (!CONTRACT_INSTANCES.includes(String(this.amount))) {
console.error(`Amount doesn't includes in contract instances`) console.error(`Amount doesn't includes in contract instances`)
return return
@ -137,16 +134,16 @@ class EventService {
async getEventsFromDB(type) { async getEventsFromDB(type) {
try { 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) { if (!savedEvents || !savedEvents.length) {
return undefined return undefined
} }
const event = await this.idb.getFromIndex({ const event = await this.idb.getFromIndex({
storeName: `lastEvents_${this.netId}`, storeName: 'lastEvents',
indexName: 'name', indexName: 'name',
key: instanceName key: instanceName
}) })
@ -334,11 +331,11 @@ class EventService {
return return
} }
const { instanceName, storeName } = this.getStoreNames(type) const instanceName = this.getInstanceName(type)
await this.idb.createMultipleTransactions({ await this.idb.createMultipleTransactions({
data: events, data: events,
storeName storeName: instanceName
}) })
await this.idb.putItem({ await this.idb.putItem({
@ -346,7 +343,7 @@ class EventService {
blockNumber: lastBlock, blockNumber: lastBlock,
name: instanceName name: instanceName
}, },
storeName: `lastEvents_${this.netId}` storeName: 'lastEvents'
}) })
} catch (err) { } catch (err) {
console.error('saveEvents has error:', err.message) console.error('saveEvents has error:', err.message)

@ -13,8 +13,9 @@ class MerkleTreeService {
this.commitment = commitment this.commitment = commitment
this.instanceName = instanceName this.instanceName = instanceName
this.idb = window.$nuxt.$indexedDB this.idb = window.$nuxt.$indexedDB(netId)
this.bloomService = bloomService({ this.bloomService = bloomService({
netId,
amount, amount,
commitment, commitment,
instanceName, instanceName,

@ -16,7 +16,7 @@ const subdomains = Object.values(networkConfig).map(({ ensSubdomainKey }) => ens
class RelayerRegister { class RelayerRegister {
constructor(provider) { constructor(provider) {
this.provider = provider this.provider = provider
this.$indexedDB = window.$nuxt.$indexedDB this.$indexedDB = window.$nuxt.$indexedDB(1)
const { registryContract, aggregatorContract } = networkConfig.netId1 const { registryContract, aggregatorContract } = networkConfig.netId1
@ -59,7 +59,7 @@ class RelayerRegister {
blockNumber: lastSyncBlock, blockNumber: lastSyncBlock,
name: storeName name: storeName
}, },
storeName: 'lastEvents_1' storeName: 'lastEvents'
}) })
if (events.length) { if (events.length) {
@ -83,7 +83,7 @@ class RelayerRegister {
const lastBlock = await this.$indexedDB.getFromIndex({ const lastBlock = await this.$indexedDB.getFromIndex({
indexName: 'name', indexName: 'name',
key: 'register_events', key: 'register_events',
storeName: 'lastEvents_1' storeName: 'lastEvents'
}) })
if (lastBlock) { if (lastBlock) {

@ -333,9 +333,9 @@ const actions = {
const { deployedBlock } = networkConfig[`netId${netId}`] const { deployedBlock } = networkConfig[`netId${netId}`]
if (currency === nativeCurrency && !lastEvent) { if (currency === nativeCurrency && !lastEvent) {
lastBlock = await this.$indexedDB.getFromIndex({ lastBlock = await this.$indexedDB(netId).getFromIndex({
indexName: 'name', indexName: 'name',
storeName: `lastEvents_${netId}`, storeName: 'lastEvents',
key: `${type}s_${currency}_${amount}` key: `${type}s_${currency}_${amount}`
}) })
} }
@ -430,11 +430,13 @@ const actions = {
}, },
async getEncryptedEventsFromDb(_, { netId }) { async getEncryptedEventsFromDb(_, { netId }) {
try { try {
if (this.$indexedDB.isBlocked) { const idb = this.$indexedDB(netId)
if (idb.isBlocked) {
return [] return []
} }
const cachedEvents = await this.$indexedDB.getAll({ storeName: `encrypted_events_${netId}` }) const cachedEvents = await idb.getAll({ storeName: 'encrypted_events' })
return cachedEvents return cachedEvents
} catch (err) { } catch (err) {
@ -541,13 +543,15 @@ const actions = {
} }
}, },
async saveEncryptedEventsToDB(_, { events, netId }) { async saveEncryptedEventsToDB(_, { events, netId }) {
if (!events || !events.length || this.$indexedDB.isBlocked) { const idb = this.$indexedDB(netId)
if (!events || !events.length || idb.isBlocked) {
return return
} }
await this.$indexedDB.createMultipleTransactions({ await idb.createMultipleTransactions({
data: events, data: events,
storeName: `encrypted_events_${netId}` storeName: `encrypted_events`
}) })
}, },
async sendDeposit({ state, rootState, getters, rootGetters, dispatch, commit }, { isEncrypted, gasPrice }) { async sendDeposit({ state, rootState, getters, rootGetters, dispatch, commit }, { isEncrypted, gasPrice }) {
@ -670,7 +674,7 @@ const actions = {
} }
}, },
async buildTree({ dispatch }, { currency, amount, netId, commitmentHex }) { async buildTree({ dispatch }, { currency, amount, netId, commitmentHex }) {
const treeInstanceName = `${currency}_${amount}_${netId}` const treeInstanceName = `${currency}_${amount}`
const params = { netId, amount, currency } const params = { netId, amount, currency }
const treeService = treesInterface.getService({ const treeService = treesInterface.getService({