diff --git a/src/services/utxoDiffSincePoint.ts b/src/services/utxoDiffSincePoint.ts index eed60e7c..3df0bcca 100644 --- a/src/services/utxoDiffSincePoint.ts +++ b/src/services/utxoDiffSincePoint.ts @@ -65,23 +65,31 @@ const extractBodyParameters = async ( const diffLimit: number = body.diffLimit; const afterBestblocks: Array | undefined = body.afterBestblocks; - const afterPoint: { - blockHash: string; - paginationPointType: DiffItemType | null; - txHash?: string; - paginationPointValue?: string; - } | undefined = body.afterPoint; + const afterPoint: + | { + blockHash: string; + paginationPointType: DiffItemType | null; + txHash?: string; + paginationPointValue?: string; + } + | undefined = body.afterPoint; if (afterPoint == null) { if (afterBestblocks == null) { - throw new Error("error, one of `afterBestblocks` or `afterPoint` is required"); + throw new Error( + "error, one of `afterBestblocks` or `afterPoint` is required" + ); } if (!Array.isArray(afterBestblocks) || afterBestblocks.length === 0) { - throw new Error("error, `afterBestblocks` is expected to be a non empty array of block hashes"); + throw new Error( + "error, `afterBestblocks` is expected to be a non empty array of block hashes" + ); } } else { if (afterBestblocks != null) { - throw new Error("error, only one of `afterBestblocks` or `afterPoint` is expected"); + throw new Error( + "error, only one of `afterBestblocks` or `afterPoint` is expected" + ); } if (afterPoint.blockHash == null) { throw new Error("error, missing blockHash in afterPoint."); @@ -341,35 +349,44 @@ const buildFullQuery = (paginationPoinType: DiffItemType | null) => { LIMIT $${5 + (paginationPoinType != null ? 2 : 0)}::word31type;`; }; -const resolveBestblocksRequest = (pool: Pool) => async (hashes: Array | undefined): Promise<{ - lastFoundSafeblock?: string; - lastFoundBestblock?: string; - bestReferencePoint?: { blockHash: string; paginationPointType: null }; -}> => { - if (hashes == null) { - return {}; - } - const [safeMatch, bestMatch] = await Promise.all([ - getLatestSafeBlockFromHashes(pool)(hashes), - getLatestBestBlockFromHashes(pool)(hashes), - ]); - if (bestMatch == null) { - throw new Error("REFERENCE_POINT_BLOCK_NOT_FOUND"); - } - return { - lastFoundSafeblock: safeMatch?.hash, - lastFoundBestblock: bestMatch.hash, - bestReferencePoint: { - blockHash: bestMatch.hash, - paginationPointType: null, +const resolveBestblocksRequest = + (pool: Pool) => + async ( + hashes: Array | undefined + ): Promise<{ + lastFoundSafeblock?: string; + lastFoundBestblock?: string; + bestReferencePoint?: { blockHash: string; paginationPointType: null }; + }> => { + if (hashes == null) { + return {}; } + const [safeMatch, bestMatch] = await Promise.all([ + getLatestSafeBlockFromHashes(pool)(hashes), + getLatestBestBlockFromHashes(pool)(hashes), + ]); + if (bestMatch == null) { + throw new Error("REFERENCE_POINT_BLOCK_NOT_FOUND"); + } + return { + lastFoundSafeblock: safeMatch?.hash, + lastFoundBestblock: bestMatch.hash, + bestReferencePoint: { + blockHash: bestMatch.hash, + paginationPointType: null, + }, + }; }; -}; export const handleUtxoDiffSincePoint = (pool: Pool) => async (req: Request, res: Response) => { - const { addresses, untilBlockHash, afterPoint: afterPointParam, afterBestblocks, diffLimit } = - await extractBodyParameters(req.body); + const { + addresses, + untilBlockHash, + afterPoint: afterPointParam, + afterBestblocks, + diffLimit, + } = await extractBodyParameters(req.body); const untilBlock = await getBlock(pool)(untilBlockHash); if (!untilBlock) { @@ -380,7 +397,9 @@ export const handleUtxoDiffSincePoint = await resolveBestblocksRequest(pool)(afterBestblocks); const afterPoint = afterPointParam || bestReferencePoint; if (afterPoint == null) { - throw new Error("error, no `afterPoint` specified and no bestblock matched"); + throw new Error( + "error, no `afterPoint` specified and no bestblock matched" + ); } const afterBlock = await getBlock(pool)(afterPoint.blockHash); diff --git a/src/utils/queries/block.ts b/src/utils/queries/block.ts index f19ae15f..53ac8c45 100644 --- a/src/utils/queries/block.ts +++ b/src/utils/queries/block.ts @@ -79,26 +79,26 @@ export const getLatestBestBlockFromHashes = export const getLatestSafeBlockFromHashes = (pool: Pool) => - async (hashes: Array): Promise => { - const result = await pool.query( - `${baseGetBlockQuery} + async (hashes: Array): Promise => { + const result = await pool.query( + `${baseGetBlockQuery} WHERE hash in ( select decode(n, 'hex') from unnest(($1)::varchar array) as n ) AND block_no <= (SELECT MAX(block_no) FROM block) - ($2)::int ORDER BY block_no DESC limit 1`, - [hashes, SAFE_BLOCK_DEPTH] - ); + [hashes, SAFE_BLOCK_DEPTH] + ); - if (!result.rows || result.rows.length === 0) { - return undefined; - } + if (!result.rows || result.rows.length === 0) { + return undefined; + } - const row = result.rows[0]; + const row = result.rows[0]; - return { - epochNo: row.epoch_no, - hash: row.hash, - slotNo: row.slot_no, - number: row.block_no, - }; + return { + epochNo: row.epoch_no, + hash: row.hash, + slotNo: row.slot_no, + number: row.block_no, }; + };