Skip to content

Commit

Permalink
Merge branch 'master' into feature/add-tx-io-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgrakkurt committed Nov 30, 2021
2 parents 2064efa + 2f5db78 commit 5aa62d7
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 45 deletions.
49 changes: 47 additions & 2 deletions README.md
Expand Up @@ -403,6 +403,46 @@ We recommend querying using payment key hashes (`addr_vkh`) when possible (other
}
```
</details>
<details>
<summary>GET v2/tipStatus</summary>
Input

None (GET request)

Output

```js
{
safeBlock: string,
bestBlock: string
}
```
</details>
<details>
<summary>POST v2/tipStatus</summary>
Input

```js
{
reference: {
bestBlocks: string[]
}
}
```

Output

```js
{
safeBlock: string,
bestBlock: string,
reference: {
lastFoundSafeBlock: string,
lastFoundBestBlock: string
}
}
```
</details>
<details>
<summary>txs/signed</summary>
Input
Expand Down Expand Up @@ -496,8 +536,13 @@ We recommend querying using payment key hashes (`addr_vkh`) when possible (other
Output

```js
// current supplies of given assets
Array<number>
{
// current supplies of given assets.
// entry for an asset is null if it is not found.
supplies: {
"${asset.policy}.${asset.name}": number | null
}
}
```
</details>
<details>
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Expand Up @@ -28,8 +28,8 @@ import { handleGetRewardHistory } from "./services/rewardHistory";
import { handleGetMultiAssetSupply } from "./services/multiAssetSupply";
import { handleGetMultiAssetTxMintMetadata } from "./services/multiAssetTxMint";
import { handleTxStatus } from "./services/txStatus";
import { handleSafeBlock } from "./services/safeBlock";
import { handleGetTxIO } from "./services/txIO";
import { handleTipStatusGet, handleTipStatusPost } from "./services/tipStatus";

import { HealthChecker } from "./HealthChecker";

Expand Down Expand Up @@ -298,9 +298,13 @@ const routes : Route[] = [
, method: "get"
, handler: bestBlock(pool)
}
, { path: "/v2/safeblock"
, { path: "/v2/tipStatus"
, method: "get"
, handler: handleSafeBlock(pool)
, handler: handleTipStatusGet(pool)
}
, { path: "/v2/tipStatus"
, method: "post"
, handler: handleTipStatusPost(pool)
}
, { path: "/v2/addresses/filterUsed"
, method: "post"
Expand Down
10 changes: 7 additions & 3 deletions src/services/multiAssetSupply.ts
Expand Up @@ -14,15 +14,19 @@ export const handleGetMultiAssetSupply = (pool: Pool) => async (req: Request, re

const assets: Asset[] = req.body.assets;

const ret = await Promise.all(assets.map(async (asset) => {
const supplies: {[key: string]: number} = {};

await Promise.all(assets.map(async (asset) => {
const supply = await getMultiAssetSupply(pool, asset);

const policyAndName = `${asset.policy}.${asset.name}`;

return supply;
supplies[policyAndName] = supply;
}));

res.send(ret);
res.send({
supplies
});
};

const getMultiAssetSupply = async (pool: Pool, asset: Asset): Promise<number> => {
Expand Down
37 changes: 0 additions & 37 deletions src/services/safeBlock.ts

This file was deleted.

108 changes: 108 additions & 0 deletions src/services/tipStatus.ts
@@ -0,0 +1,108 @@
import config from "config";

import { Pool } from "pg";
import { Request, Response } from "express";

const safeBlockDifference = parseInt(config.get("safeBlockDifference"));

const bestBlockQuery = `
SELECT epoch_no AS "epoch",
epoch_slot_no AS "slot",
encode(hash, 'hex') as hash,
block_no AS height
FROM BLOCK
ORDER BY id DESC
LIMIT 1;`;

const safeBlockQuery = `SELECT epoch_no AS "epoch",
epoch_slot_no AS "slot",
encode(hash, 'hex') as hash,
block_no AS height
FROM BLOCK
WHERE block_no <= (SELECT MAX(block_no) FROM block) - ($1)::int
ORDER BY id DESC
LIMIT 1;
`;

const bestBlockFromReferenceQuery = `SELECT encode(hash, 'hex') as "hash", block_no as "blockNumber"
FROM block
WHERE encode(hash, 'hex') = any(($1)::varchar array)
AND block_no IS NOT NULL
ORDER BY block_no DESC
LIMIT 1`;

const safeBlockFromReferenceQuery = `SELECT encode(hash, 'hex') as "hash", block_no as "blockNumber"
FROM block
WHERE encode(hash, 'hex') = any(($1)::varchar array)
AND block_no IS NOT NULL
AND block_no <= (SELECT MAX(block_no) FROM block) - ($2)::int
ORDER BY block_no DESC
LIMIT 1`;

const getBestAndSafeBlocks = async (pool: Pool): Promise<{
safeBlock: string | undefined,
bestBlock: string | undefined
}> => {
const [bestBlockResult, safeBlockResult] = await Promise.all([
pool.query(bestBlockQuery),
pool.query(safeBlockQuery, [safeBlockDifference])
]);

return {
safeBlock: safeBlockResult.rowCount > 0 ? safeBlockResult.rows[0].hash : undefined,
bestBlock: bestBlockResult.rowCount > 0 ? bestBlockResult.rows[0].hash : undefined,
};
};

export const handleTipStatusGet = (pool: Pool) => async (req: Request, res: Response) => {
const result = await getBestAndSafeBlocks(pool);
res.send(result);
};

export const handleTipStatusPost = (pool: Pool) => async (req: Request, res: Response) => {
if (!req.body.reference) {
throw new Error("error, missing reference");
}

if (!req.body.reference.bestBlocks) {
throw new Error("error, missing bestBlocks inside reference");
}

const bestBlocks: string[] = req.body.reference.bestBlocks;
if (!Array.isArray(bestBlocks)) {
throw new Error("error, bestBlocks should be an array");
}

if (bestBlocks.length === 0) {
throw new Error("error, bestBlocks should not be empty");
}

const [
{ safeBlock, bestBlock },
bestBlockFromReferenceResult,
safeBlockFromReferenceResult
] = await Promise.all([
getBestAndSafeBlocks(pool),
pool.query(bestBlockFromReferenceQuery, [bestBlocks]),
pool.query(safeBlockFromReferenceQuery, [bestBlocks, safeBlockDifference])
]);

if (bestBlockFromReferenceResult.rowCount === 0) {
throw new Error("REFERENCE_POINT_BLOCK_NOT_FOUND");
}

const lastFoundBestBlock: string = bestBlockFromReferenceResult.rows[0].hash;
if (safeBlockFromReferenceResult.rowCount === 0) {
throw new Error("REFERENCE_POINT_BLOCK_NOT_FOUND");
}
const lastFoundSafeBlock: string = safeBlockFromReferenceResult.rows[0].hash;

res.send({
safeBlock,
bestBlock,
reference: {
lastFoundSafeBlock,
lastFoundBestBlock
}
});
};

0 comments on commit 5aa62d7

Please sign in to comment.