Skip to content

Commit 20466a1

Browse files
authored
feat: create /extended/v2/burn-blocks/:height_or_hash/blocks endpoint (#1782)
* chore: move to v2 pg submodule * fix: move blocks per burn block endpoint * docs: openapi * docs: endpoint name
1 parent 3a02f57 commit 20466a1

File tree

8 files changed

+348
-347
lines changed

8 files changed

+348
-347
lines changed

docs/openapi.yaml

Lines changed: 58 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ tags:
2121
url: https://docs.stacks.co/understand-stacks/accounts
2222
- name: Blocks
2323
description: Read-only endpoints to obtain Stacks block details
24+
- name: Burn Blocks
25+
description: Read-only endpoints to obtain burn block details
2426
- name: Faucets
2527
description: Endpoints to request STX or BTC tokens (not possible on Mainnet)
2628
- name: Fees
@@ -613,7 +615,7 @@ paths:
613615
description: |
614616
Retrieves a list of recent burn blocks
615617
tags:
616-
- Blocks
618+
- Burn Blocks
617619
operationId: get_burn_blocks
618620
parameters:
619621
- name: limit
@@ -646,7 +648,7 @@ paths:
646648
summary: Get burn block
647649
description: Retrieves a single burn block
648650
tags:
649-
- Blocks
651+
- Burn Blocks
650652
operationId: get_burn_block
651653
parameters:
652654
- name: height_or_hash
@@ -668,16 +670,26 @@ paths:
668670
$ref: ./entities/blocks/burn-block.schema.json
669671
example:
670672
$ref: ./entities/blocks/burn-block.example.json
671-
672-
/extended/v2/blocks:
673+
674+
/extended/v2/burn-blocks/{height_or_hash}/blocks:
673675
get:
674-
summary: Get blocks
676+
summary: Get blocks by burn block
675677
description: |
676-
Retrieves a list of recently mined blocks
678+
Retrieves a list of blocks confirmed by a specific burn block
677679
tags:
678680
- Blocks
679-
operationId: get_blocks
681+
operationId: get_blocks_by_burn_block
680682
parameters:
683+
- name: height_or_hash
684+
in: path
685+
description: filter by burn block height, hash, or the constant `latest` to filter for the most recent burn block
686+
required: true
687+
schema:
688+
oneOf:
689+
- type: integer
690+
example: 42000
691+
- type: string
692+
example: "0x4839a8b01cfb39ffcc0d07d3db31e848d5adf5279d529ed5062300b9f353ff79"
681693
- name: limit
682694
in: query
683695
description: max number of blocks to fetch
@@ -692,20 +704,39 @@ paths:
692704
schema:
693705
type: integer
694706
example: 0
695-
- name: burn_block_hash
707+
responses:
708+
200:
709+
description: List of blocks
710+
content:
711+
application/json:
712+
schema:
713+
$ref: ./api/blocks/get-nakamoto-blocks.schema.json
714+
example:
715+
$ref: ./api/blocks/get-nakamoto-blocks.example.json
716+
717+
/extended/v2/blocks:
718+
get:
719+
summary: Get blocks
720+
description: |
721+
Retrieves a list of recently mined blocks
722+
tags:
723+
- Blocks
724+
operationId: get_blocks
725+
parameters:
726+
- name: limit
696727
in: query
697-
description: filter blocks by burn block hash
728+
description: max number of blocks to fetch
698729
required: false
699730
schema:
700-
type: string
701-
example: "0xb154c008df2101023a6d0d54986b3964cee58119eed14f5bed98e15678e18fe2"
702-
- name: burn_block_height
731+
type: integer
732+
example: 20
733+
- name: offset
703734
in: query
704-
description: filter blocks by burn block height
735+
description: index of first burn block to fetch
705736
required: false
706737
schema:
707738
type: integer
708-
example: 810344
739+
example: 0
709740
responses:
710741
200:
711742
description: List of blocks
@@ -747,12 +778,12 @@ paths:
747778

748779
/extended/v2/blocks/{height_or_hash}/transactions:
749780
get:
750-
summary: Get block transactions
781+
summary: Get transactions by block
751782
description: |
752783
Retrieves transactions confirmed in a single block
753784
tags:
754-
- Blocks
755-
operationId: get_block_transactions
785+
- Transactions
786+
operationId: get_transactions_by_block
756787
parameters:
757788
- name: height_or_hash
758789
in: path
@@ -2913,77 +2944,15 @@ paths:
29132944
example:
29142945
$ref: ./api/bns/errors/bns-unsupported-blockchain.example.json
29152946

2916-
# /v1/subdomains:
2917-
# get:
2918-
# summary: Get All Subdomains
2919-
# description: Retrieves a list of all subdomains known to the node.
2920-
# tags:
2921-
# - Names
2922-
# operationId: get_all_subdomains
2923-
# parameters:
2924-
# - name: page
2925-
# in: query
2926-
# description: names are returned in pages of size 100, so specify the page number.
2927-
# required: true
2928-
# example: 3
2929-
# schema:
2930-
# type: integer
2931-
# responses:
2932-
# 200:
2933-
# description: Success
2934-
# content:
2935-
# application/json:
2936-
# schema:
2937-
# $ref: ./api/bns/name-querying/bns-get-all-subdomains-response.schema.json
2938-
# example:
2939-
# $ref: ./api/bns/name-querying/bns-get-all-subdomains-response.example.json
2940-
# 400:
2941-
# description: Error
2942-
# content:
2943-
# application/json:
2944-
# schema:
2945-
# $ref: ./api/bns/errors/bns-error.schema.json
2946-
# example:
2947-
# $ref: ./api/bns/errors/bns-invalid-page.example.json
2948-
#
2949-
# /v1/subdomains/{txid}:
2950-
# get:
2951-
# summary: Get Subdomain at Transaction
2952-
# description: Retrieves the list of subdomain operations processed by a given transaction. The returned array includes subdomain operations that have not yet been accepted as part of any subdomain’s history (checkable via the accepted field). If the given transaction ID does not correspond to a Stacks transaction that introduced new subdomain operations, and empty array will be returned.
2953-
# tags:
2954-
# - Names
2955-
# operationId: get_subdomain_at_transaction
2956-
# parameters:
2957-
# - name: txid
2958-
# in: path
2959-
# description: transaction id
2960-
# required: true
2961-
# schema:
2962-
# type: string
2963-
# example: "d04d708472ea3c147f50e43264efdb1535f71974053126dc4db67b3ac19d41fe"
2964-
# responses:
2965-
# 200:
2966-
# description: Success
2967-
# content:
2968-
# application/json:
2969-
# schema:
2970-
# $ref: ./api/bns/name-querying/bns-get-subdomain-at-tx-response.schema.json
2971-
# example:
2972-
# $ref: ./api/bns/name-querying/bns-get-subdomain-at-tx-response.example.json
2973-
# 400:
2974-
# description: Error
2975-
# content:
2976-
# application/json:
2977-
# schema:
2978-
# $ref: ./api/bns/errors/bns-error.schema.json
2979-
# example:
2980-
# $ref: ./api/bns/errors/bns-invalid-tx-id.example.json
2981-
29822947
/extended/v1/tx/block/{block_hash}:
29832948
get:
2949+
deprecated: true
29842950
operationId: get_transactions_by_block_hash
29852951
summary: Transactions by block hash
2986-
description: Retrieves a list of all transactions within a block for a given block hash.
2952+
description: |
2953+
**NOTE:** This endpoint is deprecated in favor of [Get transactions by block](#operation/get_transactions_by_block).
2954+
2955+
Retrieves a list of all transactions within a block for a given block hash.
29872956
tags:
29882957
- Transactions
29892958
parameters:
@@ -3020,9 +2989,13 @@ paths:
30202989

30212990
/extended/v1/tx/block_height/{height}:
30222991
get:
2992+
deprecated: true
30232993
operationId: get_transactions_by_block_height
30242994
summary: Transactions by block height
3025-
description: Retrieves all transactions within a block at a given height
2995+
description: |
2996+
**NOTE:** This endpoint is deprecated in favor of [Get transactions by block](#operation/get_transactions_by_block).
2997+
2998+
Retrieves all transactions within a block at a given height
30262999
tags:
30273000
- Transactions
30283001
parameters:

src/api/controllers/db-controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ import { getOperations, parseTransactionMemo } from '../../rosetta/rosetta-helpe
6868
import { PgStore } from '../../datastore/pg-store';
6969
import { SyntheticPoxEventName } from '../../pox-helpers';
7070
import { logger } from '../../logger';
71-
import { BlocksQueryParams } from '../routes/v2/schemas';
7271

7372
export function parseTxTypeStrings(values: string[]): TransactionType[] {
7473
return values.map(v => {

src/api/routes/v2/blocks.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import {
77
import { asyncHandler } from '../../async-handler';
88
import { NakamotoBlockListResponse, TransactionResults } from 'docs/generated';
99
import {
10-
BlocksQueryParams,
1110
BlockParams,
12-
CompiledBlocksQueryParams,
1311
CompiledBlockParams,
1412
CompiledTransactionPaginationQueryParams,
1513
TransactionPaginationQueryParams,
1614
validRequestQuery,
1715
validRequestParams,
16+
CompiledBlockPaginationQueryParams,
17+
BlockPaginationQueryParams,
1818
} from './schemas';
1919
import { parseDbNakamotoBlock } from './helpers';
2020
import { InvalidRequestError } from '../../../errors';
@@ -28,10 +28,10 @@ export function createV2BlocksRouter(db: PgStore): express.Router {
2828
'/',
2929
cacheHandler,
3030
asyncHandler(async (req, res) => {
31-
if (!validRequestQuery(req, res, CompiledBlocksQueryParams)) return;
32-
const query = req.query as BlocksQueryParams;
31+
if (!validRequestQuery(req, res, CompiledBlockPaginationQueryParams)) return;
32+
const query = req.query as BlockPaginationQueryParams;
3333

34-
const { limit, offset, results, total } = await db.getV2Blocks(query);
34+
const { limit, offset, results, total } = await db.v2.getBlocks(query);
3535
const response: NakamotoBlockListResponse = {
3636
limit,
3737
offset,
@@ -50,7 +50,7 @@ export function createV2BlocksRouter(db: PgStore): express.Router {
5050
if (!validRequestParams(req, res, CompiledBlockParams)) return;
5151
const params = req.params as BlockParams;
5252

53-
const block = await db.getV2Block(params);
53+
const block = await db.v2.getBlock(params);
5454
if (!block) {
5555
res.status(404).json({ errors: 'Not found' });
5656
return;
@@ -73,7 +73,7 @@ export function createV2BlocksRouter(db: PgStore): express.Router {
7373
const query = req.query as TransactionPaginationQueryParams;
7474

7575
try {
76-
const { limit, offset, results, total } = await db.getV2BlockTransactions({
76+
const { limit, offset, results, total } = await db.v2.getBlockTransactions({
7777
...params,
7878
...query,
7979
});

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as express from 'express';
2-
import { BurnBlockListResponse } from '@stacks/stacks-blockchain-api-types';
2+
import {
3+
BurnBlockListResponse,
4+
NakamotoBlockListResponse,
5+
} from '@stacks/stacks-blockchain-api-types';
36
import { getETagCacheHandler, setETagCacheHeaders } from '../../controllers/cache-controller';
47
import { asyncHandler } from '../../async-handler';
58
import { PgStore } from '../../../datastore/pg-store';
6-
import { parseDbBurnBlock } from './helpers';
9+
import { parseDbBurnBlock, parseDbNakamotoBlock } from './helpers';
710
import {
811
BlockPaginationQueryParams,
912
BlockParams,
@@ -12,6 +15,7 @@ import {
1215
validRequestParams,
1316
validRequestQuery,
1417
} from './schemas';
18+
import { InvalidRequestError } from '../../../errors';
1519

1620
export function createV2BurnBlocksRouter(db: PgStore): express.Router {
1721
const router = express.Router();
@@ -24,7 +28,7 @@ export function createV2BurnBlocksRouter(db: PgStore): express.Router {
2428
if (!validRequestQuery(req, res, CompiledBlockPaginationQueryParams)) return;
2529
const query = req.query as BlockPaginationQueryParams;
2630

27-
const { limit, offset, results, total } = await db.getBurnBlocks(query);
31+
const { limit, offset, results, total } = await db.v2.getBurnBlocks(query);
2832
const response: BurnBlockListResponse = {
2933
limit,
3034
offset,
@@ -43,7 +47,7 @@ export function createV2BurnBlocksRouter(db: PgStore): express.Router {
4347
if (!validRequestParams(req, res, CompiledBlockParams)) return;
4448
const params = req.params as BlockParams;
4549

46-
const block = await db.getBurnBlock(params);
50+
const block = await db.v2.getBurnBlock(params);
4751
if (!block) {
4852
res.status(404).json({ errors: 'Not found' });
4953
return;
@@ -53,5 +57,40 @@ export function createV2BurnBlocksRouter(db: PgStore): express.Router {
5357
})
5458
);
5559

60+
router.get(
61+
'/:height_or_hash/blocks',
62+
cacheHandler,
63+
asyncHandler(async (req, res) => {
64+
if (
65+
!validRequestParams(req, res, CompiledBlockParams) ||
66+
!validRequestQuery(req, res, CompiledBlockPaginationQueryParams)
67+
)
68+
return;
69+
const params = req.params as BlockParams;
70+
const query = req.query as BlockPaginationQueryParams;
71+
72+
try {
73+
const { limit, offset, results, total } = await db.v2.getBlocksByBurnBlock({
74+
...params,
75+
...query,
76+
});
77+
const response: NakamotoBlockListResponse = {
78+
limit,
79+
offset,
80+
total,
81+
results: results.map(r => parseDbNakamotoBlock(r)),
82+
};
83+
setETagCacheHeaders(res);
84+
res.json(response);
85+
} catch (error) {
86+
if (error instanceof InvalidRequestError) {
87+
res.status(404).json({ errors: error.message });
88+
return;
89+
}
90+
throw error;
91+
}
92+
})
93+
);
94+
5695
return router;
5796
}

src/api/routes/v2/schemas.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,30 +113,6 @@ export const CompiledTransactionPaginationQueryParams = ajv.compile(
113113
TransactionPaginationQueryParamsSchema
114114
);
115115

116-
const BlocksQueryParamsSchema = Type.Union([
117-
BlockPaginationQueryParamsSchema,
118-
Type.Composite(
119-
[
120-
Type.Object({
121-
burn_block_hash: Type.Union([Type.Literal('latest'), BurnBlockHashParamSchema]),
122-
}),
123-
BlockPaginationQueryParamsSchema,
124-
],
125-
{ additionalProperties: false }
126-
),
127-
Type.Composite(
128-
[
129-
Type.Object({
130-
burn_block_height: Type.Union([Type.Literal('latest'), BurnBlockHeightParamSchema]),
131-
}),
132-
BlockPaginationQueryParamsSchema,
133-
],
134-
{ additionalProperties: false }
135-
),
136-
]);
137-
export type BlocksQueryParams = Static<typeof BlocksQueryParamsSchema>;
138-
export const CompiledBlocksQueryParams = ajv.compile(BlocksQueryParamsSchema);
139-
140116
const BlockParamsSchema = Type.Object(
141117
{
142118
height_or_hash: Type.Union([

0 commit comments

Comments
 (0)