From a62e93fcf380d29ceb34dc1a66ec9fb16cd71622 Mon Sep 17 00:00:00 2001 From: jairajdev Date: Fri, 10 May 2024 21:02:49 +0545 Subject: [PATCH] Added signed cycles cache --- src/API.ts | 4 ++-- src/Data/Cycles.ts | 13 +++++++------ src/cache/cycleRecordsCache.ts | 12 ++++++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/API.ts b/src/API.ts index 086ec039..eb3c4bc8 100644 --- a/src/API.ts +++ b/src/API.ts @@ -339,8 +339,8 @@ export function registerRoutes(server: FastifyInstance MAX_CYCLES_PER_REQUEST) count = MAX_CYCLES_PER_REQUEST - const cycleInfo = await Cycles.getLatestCycleRecords(count) - reply.send({ cycleInfo }) + const res = await Cycles.getLatestCycleRecords(count) + reply.send(res) }) type ReceiptRequest = FastifyRequest<{ diff --git a/src/Data/Cycles.ts b/src/Data/Cycles.ts index 1816b0b0..c3db11fc 100644 --- a/src/Data/Cycles.ts +++ b/src/Data/Cycles.ts @@ -22,7 +22,7 @@ import fetch from 'node-fetch' import { getAdjacentLeftAndRightArchivers, sendDataToAdjacentArchivers, DataType } from './GossipData' import { cleanOldOriginalTxsMap, cleanOldReceiptsMap, storeCycleData } from './Collector' import { clearServingValidatorsInterval, initServingValidatorsInterval } from './AccountDataProvider' -import { hexstring } from '@shardus/crypto-utils' +import { Signature, hexstring } from '@shardus/crypto-utils' import { handleLostArchivers } from '../LostArchivers' import ShardFunctions from '../ShardFunctions' import { RequestDataType, queryFromArchivers } from '../API' @@ -30,8 +30,9 @@ import { stringifyReduce } from '../profiler/StringifyReduce' import { addCyclesToCache } from '../cache/cycleRecordsCache' import { queryLatestCycleRecords } from '../dbstore/cycles' -interface ArchiverCycleResponse { +export interface ArchiverCycleResponse { cycleInfo: P2PTypes.CycleCreatorTypes.CycleData[] + sign: Signature } interface ConsensorCycleResponse { @@ -519,10 +520,10 @@ function updateShardValues(cycle: P2PTypes.CycleCreatorTypes.CycleData): void { } } -export async function getLatestCycleRecords(count: number): Promise { +export async function getLatestCycleRecords(count: number): Promise { if (config.cycleRecordsCache.enabled) { - return await cycleDataCache.getLatestCycleRecords(count) + return await cycleDataCache.getLatestCycleRecordsFromCache(count) } - - return await queryLatestCycleRecords(count) + const cycleInfo = await queryLatestCycleRecords(count) + return Crypto.sign({ cycleInfo }) } diff --git a/src/cache/cycleRecordsCache.ts b/src/cache/cycleRecordsCache.ts index d998be4c..abdc50b1 100644 --- a/src/cache/cycleRecordsCache.ts +++ b/src/cache/cycleRecordsCache.ts @@ -1,8 +1,11 @@ import { P2P } from '@shardus/types' import { config } from '../Config' import { queryLatestCycleRecords } from '../dbstore/cycles' +import * as Crypto from '../Crypto' +import { ArchiverCycleResponse } from '../Data/Cycles' let cachedCycleRecords: P2P.CycleCreatorTypes.CycleData[] = [] +const signedCacheCycleRecords: Map = new Map() let lastCacheUpdateFromDBRunning = false async function updateCacheFromDB(): Promise { @@ -34,12 +37,17 @@ export async function addCyclesToCache(cycles: P2P.CycleCreatorTypes.CycleData[] if (cachedCycleRecords.length > config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) { cachedCycleRecords.splice(config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) } + signedCacheCycleRecords.clear() } -export async function getLatestCycleRecords(count: number): Promise { +export async function getLatestCycleRecordsFromCache(count: number): Promise { if (cachedCycleRecords.length === 0) { await updateCacheFromDB() } + if (signedCacheCycleRecords.has(count)) return signedCacheCycleRecords.get(count) - return cachedCycleRecords.slice(0, count) + const cycleInfo = cachedCycleRecords.slice(0, count) + const signedCycleRecords = Crypto.sign({ cycleInfo }) + signedCacheCycleRecords.set(count, signedCycleRecords) + return signedCycleRecords }