diff --git a/src/contracts/deposits-registry/deposits-registry.service.ts b/src/contracts/deposits-registry/deposits-registry.service.ts index 66fa09c5..58c337c8 100644 --- a/src/contracts/deposits-registry/deposits-registry.service.ts +++ b/src/contracts/deposits-registry/deposits-registry.service.ts @@ -38,7 +38,7 @@ export class DepositRegistryService { public async initialize() { await this.store.initialize(); - const cachedEvents = await this.store.getEventsCache(); + const cachedEvents = this.store.getEventsCache(); await this.sanityChecker.initialize(cachedEvents); await this.updateEventsCache(); @@ -49,7 +49,7 @@ export class DepositRegistryService { * @returns event group */ public async getCachedEvents(): Promise { - const { headers, ...rest } = await this.store.getEventsCache(); + const { headers, ...rest } = this.store.getEventsCache(); const deploymentBlock = await this.fetcher.getDeploymentBlockByNetwork(); return { diff --git a/src/contracts/deposits-registry/store/store.service.spec.ts b/src/contracts/deposits-registry/store/store.service.spec.ts index 079751dd..36c4eba5 100644 --- a/src/contracts/deposits-registry/store/store.service.spec.ts +++ b/src/contracts/deposits-registry/store/store.service.spec.ts @@ -10,8 +10,11 @@ import { PrometheusModule } from 'common/prometheus'; const getEventsDepositCount = async ( dbService: DepositsRegistryStoreService, ) => { - const result = await dbService.getEventsCache(); - const expectedDeposits = result.data.map((event) => event.depositCount); + const resultCache = dbService.getEventsCache(); + const resultDB = await dbService.getEventsFromDB(); + + expect(resultCache).toEqual(resultDB); + const expectedDeposits = resultCache.data.map((event) => event.depositCount); return expectedDeposits; }; @@ -46,7 +49,7 @@ describe('dbService', () => { }); it('should return default cache', async () => { - const result = await dbService.getEventsCache(); + const result = dbService.getEventsCache(); expect(result).toEqual(defaultCacheValue); }); @@ -54,9 +57,12 @@ describe('dbService', () => { const expected = cacheMock; await dbService.insertEventsCacheBatch(expected); - const result = await dbService.getEventsCache(); - expect(result).toEqual(expected); + const resultCache = await dbService.getEventsCache(); + const resultDB = await dbService.getEventsFromDB(); + + expect(resultCache).toEqual(resultDB); + expect(resultCache).toEqual(expected); }); describe('deleteDepositsGreaterThanNBatch', () => { diff --git a/src/contracts/deposits-registry/store/store.service.ts b/src/contracts/deposits-registry/store/store.service.ts index 75dea410..d7584f1f 100644 --- a/src/contracts/deposits-registry/store/store.service.ts +++ b/src/contracts/deposits-registry/store/store.service.ts @@ -20,6 +20,7 @@ import { Histogram } from 'prom-client'; @Injectable() export class DepositsRegistryStoreService { private db!: Level; + private cache!: VerifiedDepositEventsCache; constructor( private providerService: ProviderService, @InjectMetric(METRIC_JOB_DURATION) @@ -35,6 +36,7 @@ export class DepositsRegistryStoreService { public async initialize() { await this.setupLevel(); + await this.setupEventsCache(); } /** @@ -50,6 +52,23 @@ export class DepositsRegistryStoreService { await this.db.open(); } + /** + * Initializes or updates the event cache by fetching events from the database. + * This method asynchronously sets the `this.cache` property with the event data obtained from the database. + */ + private async setupEventsCache(): Promise { + this.cache = await this.getEventsFromDB(); + } + + /** + * Returns a default value for the cache by deep cloning the predefined default cache value. + * This method uses JSON serialization to ensure a deep clone of `this.cacheDefaultValue`. + * @returns {VerifiedDepositEventsCache} A deep cloned copy of the default cache value. + */ + private getDefaultCachedValue(): VerifiedDepositEventsCache { + return JSON.parse(JSON.stringify(this.cacheDefaultValue)); + } + /** * Fetches and constructs the cache directory path for the current blockchain network. * @@ -71,14 +90,14 @@ export class DepositsRegistryStoreService { * @returns {Promise<{data: VerifiedDepositEvent[], headers: VerifiedDepositEventsCacheHeaders}>} Cache data and headers. * @public */ - public async getEventsCache(): Promise<{ + public async getEventsFromDB(): Promise<{ data: VerifiedDepositEvent[]; headers: VerifiedDepositEventsCacheHeaders; lastValidEvent?: VerifiedDepositEvent; }> { const endTimer = this.jobDurationMetric .labels({ - jobName: 'getEventsCache_deposits', + jobName: 'getEventsCache_deposits_db', }) .startTimer(); @@ -98,13 +117,22 @@ export class DepositsRegistryStoreService { return { data, headers, lastValidEvent }; } catch (error: any) { - if (error.code === 'LEVEL_NOT_FOUND') return this.cacheDefaultValue; + if (error.code === 'LEVEL_NOT_FOUND') return this.getDefaultCachedValue(); throw error; } finally { endTimer(); } } + /** + * Retrieves the current event cache. + * This method returns the cache of events which includes verified deposit events. + * @returns {VerifiedDepositEventsCache} The current event cache. + */ + public getEventsCache(): VerifiedDepositEventsCache { + return this.cache; + } + /** * Retrieves the last valid deposit event from the database. * This method queries the database for the 'last-valid-event' key to fetch the most recent @@ -169,6 +197,7 @@ export class DepositsRegistryStoreService { // Execute the batch operation if there are any operations to perform if (ops.length > 0) { await this.db.batch(ops); + await this.setupEventsCache(); } } @@ -250,6 +279,9 @@ export class DepositsRegistryStoreService { value: JSON.stringify(records.headers), }); await this.db.batch(ops); + + this.cache.data = this.cache.data.concat(records.data); + this.cache.headers = { ...records.headers }; } /** @@ -261,6 +293,7 @@ export class DepositsRegistryStoreService { */ public async insertLastValidEvent(event: VerifiedDepositEvent) { await this.db.put('last-valid-event', this.serializeDepositEvent(event)); + this.cache.lastValidEvent = event; } /** @@ -271,6 +304,7 @@ export class DepositsRegistryStoreService { */ public async deleteCache(): Promise { await this.db.clear(); + this.cache = this.getDefaultCachedValue(); } /** @@ -296,5 +330,8 @@ export class DepositsRegistryStoreService { ...cachedEvents.headers, }, }); + + this.cache.data = this.cache.data.concat(cachedEvents.data); + this.cache.headers = { ...cachedEvents.headers }; } }