diff --git a/src/helpers/api.config.service.ts b/src/helpers/api.config.service.ts index 7ebc23bfe..ec5895504 100644 --- a/src/helpers/api.config.service.ts +++ b/src/helpers/api.config.service.ts @@ -93,6 +93,16 @@ export class ApiConfigService { return tracerFlag === 'true'; } + isDeephistoryActive(): boolean { + const deepHistoryFlag = this.configService.get( + 'ENABLE_DEEP_HISTORY', + ); + if (!deepHistoryFlag) { + return false; + } + return deepHistoryFlag === 'true'; + } + getRedisUrl(): string { const redisUrl = this.configService.get('REDIS_URL'); if (!redisUrl) { diff --git a/src/helpers/decorators/caching.decorator.ts b/src/helpers/decorators/caching.decorator.ts index 971242bfc..fcc0ed5a0 100644 --- a/src/helpers/decorators/caching.decorator.ts +++ b/src/helpers/decorators/caching.decorator.ts @@ -1,6 +1,7 @@ import { Inject } from '@nestjs/common'; import { CacheService } from '@multiversx/sdk-nestjs-cache'; import { generateCacheKeyFromParams } from 'src/utils/generate-cache-key'; +import { ContextTracker } from '@multiversx/sdk-nestjs-common'; export interface ICachingOptions { baseKey: string; @@ -20,12 +21,18 @@ export function GetOrSetCache(cachingOptions: ICachingOptions) { const originalMethod = descriptor.value; descriptor.value = async function (...args: any[]) { - const cacheKey = generateCacheKeyFromParams( + const context = ContextTracker.get(); + + let cacheKey = generateCacheKeyFromParams( cachingOptions.baseKey, propertyKey, ...args, ); + if (context && context.deepHistoryTimestamp) { + cacheKey = `${cacheKey}.${context.deepHistoryTimestamp}`; + } + const cachingService: CacheService = this.cachingService; return await cachingService.getOrSet( diff --git a/src/helpers/proxy.network.provider.profiler.ts b/src/helpers/proxy.network.provider.profiler.ts index 47c419d4b..f6ff6e24a 100644 --- a/src/helpers/proxy.network.provider.profiler.ts +++ b/src/helpers/proxy.network.provider.profiler.ts @@ -5,8 +5,19 @@ import { import { PerformanceProfiler } from '../utils/performance.profiler'; import { MetricsCollector } from '../utils/metrics.collector'; import { IContractQuery } from '@multiversx/sdk-network-providers/out/interface'; +import { ContextTracker } from '@multiversx/sdk-nestjs-common'; +import { AxiosRequestConfig } from 'axios'; +import { ApiConfigService } from './api.config.service'; export class ProxyNetworkProviderProfiler extends ProxyNetworkProvider { + constructor( + private readonly apiConfigService: ApiConfigService, + url: string, + config?: AxiosRequestConfig, + ) { + super(url, config); + } + async queryContract(query: IContractQuery): Promise { const profiler = new PerformanceProfiler(); @@ -22,4 +33,19 @@ export class ProxyNetworkProviderProfiler extends ProxyNetworkProvider { return result; } + + async doPostGeneric(resourceUrl: string, payload: any): Promise { + const context = ContextTracker.get(); + if ( + this.apiConfigService.isDeephistoryActive() && + context && + context.deepHistoryTimestamp + ) { + resourceUrl = resourceUrl.includes('?') + ? `${resourceUrl}×tamp=${context.deepHistoryTimestamp}` + : `${resourceUrl}?timestamp=${context.deepHistoryTimestamp}`; + } + const response = await super.doPostGeneric(resourceUrl, payload); + return response; + } } diff --git a/src/services/multiversx-communication/mx.api.service.ts b/src/services/multiversx-communication/mx.api.service.ts index dfff88adf..9f92cb551 100644 --- a/src/services/multiversx-communication/mx.api.service.ts +++ b/src/services/multiversx-communication/mx.api.service.ts @@ -19,6 +19,7 @@ import { } from 'src/utils/token.type.compare'; import { PendingExecutor } from 'src/utils/pending.executor'; import { MXProxyService } from './mx.proxy.service'; +import { ContextTracker } from '@multiversx/sdk-nestjs-common'; type GenericGetArgs = { methodName: string; @@ -82,6 +83,17 @@ export class MXApiService { ): Promise { const profiler = new PerformanceProfiler(`${name} ${resourceUrl}`); try { + const context = ContextTracker.get(); + if ( + this.apiConfigService.isDeephistoryActive() && + context && + context.deepHistoryTimestamp + ) { + resourceUrl = resourceUrl.includes('?') + ? `${resourceUrl}×tamp=${context.deepHistoryTimestamp}` + : `${resourceUrl}?timestamp=${context.deepHistoryTimestamp}`; + } + const response = await this.getService().doGetGeneric(resourceUrl); profiler.stop(); MetricsCollector.setApiCall( diff --git a/src/services/multiversx-communication/mx.proxy.service.ts b/src/services/multiversx-communication/mx.proxy.service.ts index 16487fb11..a2b24b785 100644 --- a/src/services/multiversx-communication/mx.proxy.service.ts +++ b/src/services/multiversx-communication/mx.proxy.service.ts @@ -31,6 +31,7 @@ export class MXProxyService { const httpsAgent = new HttpsAgent(keepAliveOptions); this.proxy = new ProxyNetworkProviderProfiler( + this.apiConfigService, this.apiConfigService.getApiUrl(), { timeout: mxConfig.proxyTimeout, diff --git a/src/utils/logging.interceptor.ts b/src/utils/logging.interceptor.ts index fdc0a65e6..033343c70 100644 --- a/src/utils/logging.interceptor.ts +++ b/src/utils/logging.interceptor.ts @@ -10,6 +10,7 @@ import { tap } from 'rxjs/operators'; import { CpuProfiler } from '@multiversx/sdk-nestjs-monitoring'; import { MetricsCollector } from './metrics.collector'; import { PerformanceProfiler } from './performance.profiler'; +import { ContextTracker } from '@multiversx/sdk-nestjs-common'; @Injectable() export class LoggingInterceptor implements NestInterceptor { @@ -23,8 +24,13 @@ export class LoggingInterceptor implements NestInterceptor { const { req } = gqlContext.getContext(); let origin = 'Unknown'; + let timestamp: number = undefined; if (req !== undefined) { origin = req?.headers?.['origin'] ?? 'Unknown'; + timestamp = req?.headers?.['timestamp']; + ContextTracker.assign({ + deepHistoryTimestamp: timestamp, + }); } const profiler = new PerformanceProfiler();