Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromtcosta committed Dec 1, 2022
1 parent 1fa3bec commit 6108e15
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 49 deletions.
87 changes: 53 additions & 34 deletions src/services/utxoDiffSincePoint.ts
Expand Up @@ -65,23 +65,31 @@ const extractBodyParameters = async (
const diffLimit: number = body.diffLimit;

const afterBestblocks: Array<string> | 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.");
Expand Down Expand Up @@ -341,35 +349,44 @@ const buildFullQuery = (paginationPoinType: DiffItemType | null) => {
LIMIT $${5 + (paginationPoinType != null ? 2 : 0)}::word31type;`;
};

const resolveBestblocksRequest = (pool: Pool) => async (hashes: Array<string> | 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<string> | 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) {
Expand All @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions src/utils/queries/block.ts
Expand Up @@ -79,26 +79,26 @@ export const getLatestBestBlockFromHashes =

export const getLatestSafeBlockFromHashes =
(pool: Pool) =>
async (hashes: Array<string>): Promise<BlockFrag | undefined> => {
const result = await pool.query(
`${baseGetBlockQuery}
async (hashes: Array<string>): Promise<BlockFrag | undefined> => {
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,
};
};

0 comments on commit 6108e15

Please sign in to comment.