Skip to content

Commit

Permalink
fix: separate idb by netId
Browse files Browse the repository at this point in the history
  • Loading branch information
dan1kov committed Jun 16, 2022
1 parent c7d1a5c commit 497675a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 77 deletions.
93 changes: 45 additions & 48 deletions plugins/idb.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 }
Expand All @@ -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',
Expand All @@ -245,66 +246,62 @@ export default async (ctx, inject) => {
}
]

const stores = [
{
name: 'register_events',
keyPath: 'ensName'
}
]
Object.keys(networkConfig).forEach(async (key) => {
const { tokens, nativeCurrency } = networkConfig[key]

NETWORKS.forEach((netId) => {
defaultState.map((item) => {
stores.push({
...item,
name: `${item.name}_${netId}`
})
})
})
const netId = Number(key.replace('netId', ''))

Object.keys(networkConfig).forEach((key) => {
const { tokens, nativeCurrency } = networkConfig[key]
const stores = [...defaultState]

const netId = key.replace('netId', '')
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'
}
const options = {
stores,
dbName: `tornado_cash_${netId}`
}

const instance = new IndexedDB(options)
const instance = new IndexedDB(options)

await instance.initDB()

instances.set(options.dbName, instance)
})

await instance.initDB()
const getInstance = (netId) => instances.get(`tornado_cash_${netId}`)

ctx.$indexedDB = instance
inject('indexedDB', instance)
ctx.$indexedDB = getInstance
inject('indexedDB', getInstance)
}
4 changes: 2 additions & 2 deletions services/bloom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
27 changes: 12 additions & 15 deletions services/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`]

Expand All @@ -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) {
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -334,19 +331,19 @@ 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({
data: {
blockNumber: lastBlock,
name: instanceName
},
storeName: `lastEvents_${this.netId}`
storeName: 'lastEvents'
})
} catch (err) {
console.error('saveEvents has error:', err.message)
Expand Down
3 changes: 2 additions & 1 deletion services/merkleTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions services/registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,7 +59,7 @@ class RelayerRegister {
blockNumber: lastSyncBlock,
name: storeName
},
storeName: 'lastEvents_1'
storeName: 'lastEvents'
})

if (events.length) {
Expand All @@ -83,7 +83,7 @@ class RelayerRegister {
const lastBlock = await this.$indexedDB.getFromIndex({
indexName: 'name',
key: 'register_events',
storeName: 'lastEvents_1'
storeName: 'lastEvents'
})

if (lastBlock) {
Expand Down
20 changes: 12 additions & 8 deletions store/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
})
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 497675a

Please sign in to comment.