From c3b9828d42fd3c8eaf6d01037c11de6a129d66ad Mon Sep 17 00:00:00 2001 From: natsoni Date: Fri, 12 Jul 2024 16:00:51 +0900 Subject: [PATCH] Move block audit cache to apiService --- frontend/src/app/components/block/block.component.ts | 1 - .../src/app/components/tracker/tracker.component.ts | 1 - .../components/transaction/transaction.component.ts | 2 +- frontend/src/app/services/api.service.ts | 11 +++++++++++ frontend/src/app/services/cache.service.ts | 11 +---------- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/frontend/src/app/components/block/block.component.ts b/frontend/src/app/components/block/block.component.ts index 980921a3c..01702487f 100644 --- a/frontend/src/app/components/block/block.component.ts +++ b/frontend/src/app/components/block/block.component.ts @@ -295,7 +295,6 @@ export class BlockComponent implements OnInit, OnDestroy { ), !this.isAuditAvailableFromBlockHeight(block.height) ? of(null) : this.apiService.getBlockAudit$(block.id) .pipe( - tap(() => this.cacheService.setBlockAuditLoaded(block.id)), catchError((err) => { this.overviewError = err; return of(null); diff --git a/frontend/src/app/components/tracker/tracker.component.ts b/frontend/src/app/components/tracker/tracker.component.ts index 72640822d..698226d50 100644 --- a/frontend/src/app/components/tracker/tracker.component.ts +++ b/frontend/src/app/components/tracker/tracker.component.ts @@ -322,7 +322,6 @@ export class TrackerComponent implements OnInit, OnDestroy { }) ), fetchAudit ? this.apiService.getBlockAudit$(hash).pipe( - tap((blockAudit) => this.cacheService.setBlockAuditLoaded(hash)), map(audit => { const isAdded = audit.addedTxs.includes(txid); const isPrioritized = audit.prioritizedTxs.includes(txid); diff --git a/frontend/src/app/components/transaction/transaction.component.ts b/frontend/src/app/components/transaction/transaction.component.ts index 0b2a4c861..16c7adb8d 100644 --- a/frontend/src/app/components/transaction/transaction.component.ts +++ b/frontend/src/app/components/transaction/transaction.component.ts @@ -366,7 +366,7 @@ export class TransactionComponent implements OnInit, AfterViewInit, OnDestroy { const fetchAudit = auditAvailable && !isCoinbase; if (fetchAudit) { // If block audit is already cached, use it to get transaction audit - const blockAuditLoaded = this.cacheService.getBlockAuditLoaded(hash); + const blockAuditLoaded = this.apiService.getBlockAuditLoaded(hash); if (blockAuditLoaded) { return this.apiService.getBlockAudit$(hash).pipe( map(audit => { diff --git a/frontend/src/app/services/api.service.ts b/frontend/src/app/services/api.service.ts index 9e2c008e4..d7efa4d02 100644 --- a/frontend/src/app/services/api.service.ts +++ b/frontend/src/app/services/api.service.ts @@ -18,6 +18,7 @@ export class ApiService { private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet private requestCache = new Map, expiry: number }>; + public blockAuditLoaded: { [hash: string]: boolean } = {}; constructor( private httpClient: HttpClient, @@ -370,6 +371,7 @@ export class ApiService { } getBlockAudit$(hash: string) : Observable { + this.setBlockAuditLoaded(hash); return this.httpClient.get( this.apiBaseUrl + this.apiBasePath + `/api/v1/block/${hash}/audit-summary` ); @@ -533,4 +535,13 @@ export class ApiService { this.apiBaseUrl + this.apiBasePath + '/api/v1/accelerations/total' + (queryString?.length ? '?' + queryString : '') ); } + + // Cache methods + async setBlockAuditLoaded(hash: string) { + this.blockAuditLoaded[hash] = true; + } + + getBlockAuditLoaded(hash) { + return this.blockAuditLoaded[hash]; + } } diff --git a/frontend/src/app/services/cache.service.ts b/frontend/src/app/services/cache.service.ts index 7a3d6f0a2..f15154b46 100644 --- a/frontend/src/app/services/cache.service.ts +++ b/frontend/src/app/services/cache.service.ts @@ -20,7 +20,6 @@ export class CacheService { network: string; blockHashCache: { [hash: string]: BlockExtended } = {}; blockCache: { [height: number]: BlockExtended } = {}; - blockAuditLoaded: { [hash: string]: boolean } = {}; blockLoading: { [height: number]: boolean } = {}; copiesInBlockQueue: { [height: number]: number } = {}; blockPriorities: number[] = []; @@ -98,10 +97,6 @@ export class CacheService { } } - async setBlockAuditLoaded(hash: string) { - this.blockAuditLoaded[hash] = true; - } - // increase the priority of a block, to delay removal bumpBlockPriority(height) { this.blockPriorities.push(height); @@ -129,7 +124,7 @@ export class CacheService { resetBlockCache() { this.blockHashCache = {}; this.blockCache = {}; - this.blockAuditLoaded = {}; + this.apiService.blockAuditLoaded = {}; this.blockLoading = {}; this.copiesInBlockQueue = {}; this.blockPriorities = []; @@ -138,8 +133,4 @@ export class CacheService { getCachedBlock(height) { return this.blockCache[height]; } - - getBlockAuditLoaded(hash) { - return this.blockAuditLoaded[hash]; - } } \ No newline at end of file