From 8a508b4d15594484e459b742620fc29a66d3e2e7 Mon Sep 17 00:00:00 2001 From: Tanuj Soni Date: Wed, 25 Sep 2024 19:21:23 +0530 Subject: [PATCH] Add /tx-digest-hash endpoint to fetch last x tx digests --- src/Config.ts | 2 ++ src/txDigester/api.ts | 28 +++++++++++++++++++++++++++- src/txDigester/txDigestFunctions.ts | 7 +++++++ src/txDigester/txDigests.ts | 16 +++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index 49bb21a2..a7135134 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -61,6 +61,7 @@ export interface Config { MAX_ORIGINAL_TXS_PER_REQUEST: number MAX_CYCLES_PER_REQUEST: number MAX_BETWEEN_CYCLES_PER_REQUEST: number + MAX_DIGESTS_PER_REQUEST: number } cycleRecordsCache: { enabled: boolean @@ -157,6 +158,7 @@ let config: Config = { MAX_ORIGINAL_TXS_PER_REQUEST: 100, MAX_CYCLES_PER_REQUEST: 100, MAX_BETWEEN_CYCLES_PER_REQUEST: 100, + MAX_DIGESTS_PER_REQUEST: 100, }, cycleRecordsCache: { enabled: false, diff --git a/src/txDigester/api.ts b/src/txDigester/api.ts index 79189940..f320eeb4 100644 --- a/src/txDigester/api.ts +++ b/src/txDigester/api.ts @@ -1,6 +1,8 @@ import { FastifyInstance, FastifyRequest } from 'fastify' import { Server, IncomingMessage, ServerResponse } from 'http' -import { getTxDigestsForACycleRange } from './txDigestFunctions' +import { getLatestTxDigests, getTxDigestsForACycleRange } from './txDigestFunctions' +import { config } from '../Config' +import * as Utils from '../Utils' type GetTxDigestsRequest = FastifyRequest<{ Querystring: { @@ -8,6 +10,9 @@ type GetTxDigestsRequest = FastifyRequest<{ cycleEnd: number } }> +type GetLatestTxDigestsRequest = FastifyRequest<{ + Params: { count: string } +}> /* To-Do: Add LRU cache for the tx-digests */ export function registerRoutes(server: FastifyInstance): void { @@ -34,4 +39,25 @@ export function registerRoutes(server: FastifyInstance { + const err = Utils.validateTypes(_request.params, { count: 's' }) + if (err) { + reply.send({ success: false, error: err }) + return + } + let count: number = parseInt(_request.params.count) + if (count <= 0 || Number.isNaN(count)) { + reply.send({ success: false, error: `Invalid count` }) + return + } + if (count > config.REQUEST_LIMIT.MAX_DIGESTS_PER_REQUEST) { + count = config.REQUEST_LIMIT.MAX_DIGESTS_PER_REQUEST + } + + console.log(`Fetching latest ${count} tx digests`) + const txDigests = await getLatestTxDigests(count) + console.log('Fetched Tx digests', txDigests) + reply.send(txDigests) + }) } diff --git a/src/txDigester/txDigestFunctions.ts b/src/txDigester/txDigestFunctions.ts index 5438bda4..1fa53445 100644 --- a/src/txDigester/txDigestFunctions.ts +++ b/src/txDigester/txDigestFunctions.ts @@ -102,3 +102,10 @@ export const getTxDigestsForACycleRange = async ( const txDigests: txDigest.TransactionDigest[] = await txDigest.queryByCycleRange(cycleStart, cycleEnd) return txDigests } + +export const getLatestTxDigests = async ( + count: number +): Promise => { + const txDigests: txDigest.TransactionDigest[] = await txDigest.queryLatestTxDigests(count) + return txDigests +} diff --git a/src/txDigester/txDigests.ts b/src/txDigester/txDigests.ts index 6c895e62..cb2ed515 100644 --- a/src/txDigester/txDigests.ts +++ b/src/txDigester/txDigests.ts @@ -79,7 +79,21 @@ export async function queryByCycleRange(startCycle: number, endCycle: number): P } return txDigests || [] } catch (e) { - console.error(e) + console.error('Error fetching txDigests from DB: ', e) + return [] + } +} + +export async function queryLatestTxDigests(count: number): Promise { + try { + const sql = `SELECT * FROM txDigests ORDER BY cycleEnd DESC LIMIT ?` + const txDigests = (await db.all(digesterDatabase, sql, [count])) as TransactionDigest[] + if (config.VERBOSE) { + console.log('Latest Tx Digests', txDigests) + } + return txDigests || [] + } catch (e) { + console.error('Error fetching latest tx digests from DB: ', e) return [] } }