Skip to content

Commit 2686ae0

Browse files
committed
feat: add total tx count to burn block endpoints
1 parent 31c2eed commit 2686ae0

File tree

8 files changed

+33
-12
lines changed

8 files changed

+33
-12
lines changed

docs/api/blocks/get-burn-blocks.example.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"0xb9e9b308cf9621ecbf66ca7b4689fe384b9b67c4588ec827d8163ab602fb935e",
1515
"0x754562cba6ec243f90485e97778ab472f462fd123ef5b83cc79d8759ca8875f5"
1616
],
17-
"avg_block_time": 15.3
17+
"avg_block_time": 15.3,
18+
"total_tx_count": 728
1819
}
1920
]
2021
}

docs/entities/blocks/burn-block.example.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"0xb9e9b308cf9621ecbf66ca7b4689fe384b9b67c4588ec827d8163ab602fb935e",
1010
"0x754562cba6ec243f90485e97778ab472f462fd123ef5b83cc79d8759ca8875f5"
1111
],
12-
"avg_block_time": 15.3
12+
"avg_block_time": 15.3,
13+
"total_tx_count": 728
1314
}

docs/entities/blocks/burn-block.schema.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"burn_block_hash",
1010
"burn_block_height",
1111
"stacks_blocks",
12-
"avg_block_time"
12+
"avg_block_time",
13+
"total_tx_count"
1314
],
1415
"properties": {
1516
"burn_block_time": {
@@ -38,6 +39,10 @@
3839
"avg_block_time": {
3940
"type": "number",
4041
"description": "Average time between blocks in seconds. Returns 0 if there is only one block in the burn block."
42+
},
43+
"total_tx_count": {
44+
"type": "integer",
45+
"description": "Total number of transactions in the Stacks blocks associated with this burn block"
4146
}
4247
}
4348
}

docs/generated.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,10 @@ export interface BurnBlock {
14701470
* Average time between blocks in seconds. Returns 0 if there is only one block in the burn block.
14711471
*/
14721472
avg_block_time: number;
1473+
/**
1474+
* Total number of transactions in the Stacks blocks associated with this burn block
1475+
*/
1476+
total_tx_count: number;
14731477
}
14741478
/**
14751479
* GET request that returns blocks

src/api/routes/v2/burn-blocks.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as express from 'express';
22
import {
33
BurnBlockListResponse,
4+
BurnBlock,
45
NakamotoBlockListResponse,
56
} from '@stacks/stacks-blockchain-api-types';
67
import { getETagCacheHandler, setETagCacheHeaders } from '../../controllers/cache-controller';
@@ -52,8 +53,9 @@ export function createV2BurnBlocksRouter(db: PgStore): express.Router {
5253
res.status(404).json({ errors: 'Not found' });
5354
return;
5455
}
56+
const response: BurnBlock = parseDbBurnBlock(block);
5557
setETagCacheHeaders(res);
56-
res.json(parseDbBurnBlock(block));
58+
res.json(response);
5759
})
5860
);
5961

src/api/routes/v2/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function parseDbBurnBlock(block: DbBurnBlock): BurnBlock {
6161
burn_block_height: block.burn_block_height,
6262
stacks_blocks: block.stacks_blocks,
6363
avg_block_time: parseFloat(parseFloat(block.avg_block_time ?? '0').toFixed(2)),
64+
total_tx_count: parseInt(block.total_tx_count),
6465
};
6566
return burnBlock;
6667
}

src/datastore/common.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface DbBurnBlock {
5656
burn_block_height: number;
5757
stacks_blocks: string[];
5858
avg_block_time: string | null;
59+
total_tx_count: string;
5960
}
6061

6162
export interface DbBurnchainReward {

src/datastore/pg-store-v2.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,18 @@ export class PgStoreV2 extends BasePgStoreModule {
288288
b.block_hash,
289289
b.block_time,
290290
b.block_height,
291+
b.tx_count,
291292
LAG(b.block_time) OVER (PARTITION BY b.burn_block_height ORDER BY b.block_height) AS previous_block_time
292293
FROM blocks b
293294
WHERE
294295
canonical = true AND
295296
b.burn_block_height IN (SELECT burn_block_height FROM RelevantBlocks)
296297
),
297-
AverageTimes AS (
298+
BlockStatistics AS (
298299
SELECT
299300
burn_block_height,
300-
AVG(block_time - previous_block_time) FILTER (WHERE previous_block_time IS NOT NULL) AS avg_block_time
301+
AVG(block_time - previous_block_time) FILTER (WHERE previous_block_time IS NOT NULL) AS avg_block_time,
302+
SUM(tx_count) AS total_tx_count
301303
FROM BlocksWithPrevTime
302304
GROUP BY burn_block_height
303305
)
@@ -311,10 +313,11 @@ export class PgStoreV2 extends BasePgStoreModule {
311313
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
312314
) AS stacks_blocks,
313315
(SELECT block_count FROM chain_tip)::int AS total,
314-
a.avg_block_time
316+
a.avg_block_time,
317+
a.total_tx_count
315318
FROM RelevantBlocks r
316319
JOIN BlocksWithPrevTime b ON b.burn_block_height = r.burn_block_height
317-
JOIN AverageTimes a ON a.burn_block_height = r.burn_block_height
320+
JOIN BlockStatistics a ON a.burn_block_height = r.burn_block_height
318321
ORDER BY r.burn_block_height DESC, b.block_height DESC;
319322
`;
320323
return {
@@ -343,14 +346,16 @@ export class PgStoreV2 extends BasePgStoreModule {
343346
block_hash,
344347
block_time,
345348
block_height,
349+
tx_count,
346350
LAG(block_time) OVER (PARTITION BY burn_block_height ORDER BY block_height) AS previous_block_time
347351
FROM blocks
348352
WHERE canonical = true AND ${filter}
349353
),
350-
AverageTimes AS (
354+
BlockStatistics AS (
351355
SELECT
352356
burn_block_height,
353-
AVG(block_time - previous_block_time) FILTER (WHERE previous_block_time IS NOT NULL) AS avg_block_time
357+
AVG(block_time - previous_block_time) FILTER (WHERE previous_block_time IS NOT NULL) AS avg_block_time,
358+
SUM(tx_count) AS total_tx_count
354359
FROM BlocksWithPrevTime
355360
GROUP BY burn_block_height
356361
)
@@ -363,9 +368,10 @@ export class PgStoreV2 extends BasePgStoreModule {
363368
ORDER BY b.block_height DESC
364369
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
365370
) AS stacks_blocks,
366-
a.avg_block_time
371+
a.avg_block_time,
372+
a.total_tx_count
367373
FROM BlocksWithPrevTime b
368-
JOIN AverageTimes a ON a.burn_block_height = b.burn_block_height
374+
JOIN BlockStatistics a ON a.burn_block_height = b.burn_block_height
369375
LIMIT 1
370376
`;
371377
if (blockQuery.count > 0) return blockQuery[0];

0 commit comments

Comments
 (0)