Skip to content

Commit

Permalink
Added signed cycles cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajdev committed May 10, 2024
1 parent d9c1026 commit a62e93f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
return
}
if (count > 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<{
Expand Down
13 changes: 7 additions & 6 deletions src/Data/Cycles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ 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'
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 {
Expand Down Expand Up @@ -519,10 +520,10 @@ function updateShardValues(cycle: P2PTypes.CycleCreatorTypes.CycleData): void {
}
}

export async function getLatestCycleRecords(count: number): Promise<P2PTypes.CycleCreatorTypes.CycleData[]> {
export async function getLatestCycleRecords(count: number): Promise<ArchiverCycleResponse> {
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 })
}
12 changes: 10 additions & 2 deletions src/cache/cycleRecordsCache.ts
Original file line number Diff line number Diff line change
@@ -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<number, ArchiverCycleResponse> = new Map()
let lastCacheUpdateFromDBRunning = false

async function updateCacheFromDB(): Promise<void> {
Expand Down Expand Up @@ -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<P2P.CycleCreatorTypes.CycleData[]> {
export async function getLatestCycleRecordsFromCache(count: number): Promise<ArchiverCycleResponse> {
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
}

0 comments on commit a62e93f

Please sign in to comment.