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

View File

@ -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}`,
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
})
stores.push({
name: `withdrawals_${token}_${amount}_${netId}`,
},
{
name: `withdrawals_${token}_${amount}`,
keyPath: 'transactionHash',
indexes: WITHDRAWAL_INDEXES
})
stores.push({
name: `stringify_tree_${token}_${amount}_${netId}`,
},
{
name: `stringify_tree_${token}_${amount}`,
keyPath: 'hashTree'
})
})
}
)
})
})
const options = {
stores,
dbName: 'tornado_cash'
dbName: `tornado_cash_${netId}`
}
const instance = new IndexedDB(options)
await instance.initDB()
ctx.$indexedDB = instance
inject('indexedDB', instance)
instances.set(options.dbName, instance)
})
const getInstance = (netId) => instances.get(`tornado_cash_${netId}`)
ctx.$indexedDB = getInstance
inject('indexedDB', getInstance)
}

View File

@ -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() {

View File

@ -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)

View File

@ -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,

View File

@ -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) {

View File

@ -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({