Skip to content

Commit 5597c7a

Browse files
committed
feat(api): include index_block_hash and tenure_height in tx responses
* chore: tests
1 parent 32f879f commit 5597c7a

File tree

10 files changed

+440
-7
lines changed

10 files changed

+440
-7
lines changed

client/src/generated/schema.d.ts

Lines changed: 344 additions & 2 deletions
Large diffs are not rendered by default.

src/api/controllers/db-controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ function parseDbAbstractTx(dbTx: DbTx, baseTx: BaseTransaction): AbstractTransac
11251125
...baseTx,
11261126
is_unanchored: dbTx.block_hash === '0x',
11271127
block_hash: dbTx.block_hash,
1128+
index_block_hash: dbTx.index_block_hash,
11281129
parent_block_hash: dbTx.parent_block_hash,
11291130
block_height: dbTx.block_height,
11301131
block_time: dbTx.block_time || dbTx.burn_block_time,
@@ -1140,6 +1141,7 @@ function parseDbAbstractTx(dbTx: DbTx, baseTx: BaseTransaction): AbstractTransac
11401141
parent_burn_block_time_iso:
11411142
dbTx.parent_burn_block_time > 0 ? unixEpochToIso(dbTx.parent_burn_block_time) : '',
11421143
canonical: dbTx.canonical,
1144+
tenure_height: dbTx.tenure_height || dbTx.block_height,
11431145
tx_index: dbTx.tx_index,
11441146
tx_status: getTxStatusString(dbTx.status) as TransactionStatus,
11451147
tx_result: {

src/api/schemas/entities/transactions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ const AbstractTransactionProperties = {
7474
description:
7575
'An ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) timestamp indicating when this block was mined.',
7676
}),
77+
index_block_hash: Type.String({
78+
description:
79+
'The only hash that can uniquely identify an anchored block or an unconfirmed state trie',
80+
}),
7781
parent_burn_block_time: Type.Integer({
7882
description: 'Unix timestamp (in seconds) indicating when this parent block was mined',
7983
}),
@@ -84,6 +88,11 @@ const AbstractTransactionProperties = {
8488
canonical: Type.Boolean({
8589
description: 'Set to `true` if block corresponds to the canonical chain tip',
8690
}),
91+
tenure_height: Nullable(
92+
Type.Integer({
93+
description: 'The tenure height (AKA coinbase height) of this block',
94+
})
95+
),
8796
tx_index: Type.Integer({
8897
description:
8998
'Index of the transaction, indicating the order. Starts at `0` and increases with each transaction',

src/datastore/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ export interface DbTx extends BaseTx {
192192

193193
tx_index: number;
194194

195+
tenure_height?: number | null;
196+
195197
/** Hex encoded Clarity values. */
196198
raw_result: string;
197199

@@ -973,6 +975,7 @@ export interface TxQueryResult {
973975
status: number;
974976
raw_result: string;
975977
canonical: boolean;
978+
tenure_height?: number | null;
976979

977980
microblock_canonical: boolean;
978981
microblock_sequence: number;

src/datastore/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ export function abiColumn(sql: PgSqlClient, tableName: string = 'txs'): postgres
294294
`;
295295
}
296296

297+
export function tenureHeightColumn(sql: PgSqlClient): postgres.Fragment {
298+
return sql`
299+
SELECT tenure_height FROM blocks WHERE index_block_hash = txs.index_block_hash
300+
`;
301+
}
302+
297303
export function parseMempoolTxQueryResult(result: MempoolTxQueryResult): DbMempoolTx {
298304
const tx: DbMempoolTx = {
299305
pruned: result.pruned,

src/datastore/pg-store.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
POX4_SYNTHETIC_EVENT_COLUMNS,
8383
POX_SYNTHETIC_EVENT_COLUMNS,
8484
prefixedCols,
85+
tenureHeightColumn,
8586
TX_COLUMNS,
8687
validateZonefileHash,
8788
} from './helpers';
@@ -3334,15 +3335,15 @@ export class PgStore extends BasePgStore {
33343335
{ address: string; balance: string; count: number; total_supply: string }[]
33353336
>`
33363337
WITH totals AS (
3337-
SELECT
3338+
SELECT
33383339
SUM(balance) AS total,
33393340
COUNT(*)::int AS total_count
33403341
FROM ft_balances
33413342
WHERE token = ${args.token}
33423343
)
3343-
SELECT
3344-
fb.address,
3345-
fb.balance,
3344+
SELECT
3345+
fb.address,
3346+
fb.balance,
33463347
ts.total AS total_supply,
33473348
ts.total_count AS count
33483349
FROM ft_balances fb
@@ -3572,8 +3573,16 @@ export class PgStore extends BasePgStore {
35723573
}
35733574
return await this.sqlTransaction(async sql => {
35743575
const maxBlockHeight = await this.getMaxBlockHeight(sql, { includeUnanchored });
3576+
const query_tx = `
3577+
SELECT ${sql(TX_COLUMNS)}, ${abiColumn(sql)}, (${tenureHeightColumn(sql)}) AS tenure_height
3578+
FROM txs
3579+
WHERE tx_id IN ${sql(txIds)}
3580+
AND block_height <= ${maxBlockHeight}
3581+
AND canonical = true
3582+
AND microblock_canonical = true
3583+
`;
35753584
const result = await sql<ContractTxQueryResult[]>`
3576-
SELECT ${sql(TX_COLUMNS)}, ${abiColumn(sql)}
3585+
SELECT ${sql(TX_COLUMNS)}, ${abiColumn(sql)}, (${tenureHeightColumn(sql)}) AS tenure_height
35773586
FROM txs
35783587
WHERE tx_id IN ${sql(txIds)}
35793588
AND block_height <= ${maxBlockHeight}

tests/api/address.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ describe('address tests', () => {
273273
post_conditions: [],
274274
tx_status: 'success',
275275
block_hash: '0x1234',
276+
index_block_hash: '0x1234',
276277
block_height: 1,
278+
tenure_height: 1,
277279
block_time: 1594647994,
278280
block_time_iso: '2020-07-13T13:46:34.000Z',
279281
burn_block_height: 100123123,
@@ -364,7 +366,9 @@ describe('address tests', () => {
364366
post_conditions: [],
365367
tx_status: 'success',
366368
block_hash: '0x1234',
369+
index_block_hash: '0x1234',
367370
block_height: 1,
371+
tenure_height: 1,
368372
block_time: 1594647994,
369373
block_time_iso: '2020-07-13T13:46:34.000Z',
370374
burn_block_height: 100123123,
@@ -429,7 +433,9 @@ describe('address tests', () => {
429433
post_conditions: [],
430434
tx_status: 'success',
431435
block_hash: '0x1234',
436+
index_block_hash: '0x1234',
432437
block_height: 1,
438+
tenure_height: 1,
433439
block_time: 1594647994,
434440
block_time_iso: '2020-07-13T13:46:34.000Z',
435441
burn_block_height: 100123123,
@@ -768,7 +774,9 @@ describe('address tests', () => {
768774
post_conditions: [],
769775
tx_status: 'success',
770776
block_hash: '0x1234',
777+
index_block_hash: '0x1234',
771778
block_height: 1,
779+
tenure_height: 1,
772780
block_time: 1594647994,
773781
block_time_iso: '2020-07-13T13:46:34.000Z',
774782
burn_block_height: 100123123,
@@ -844,7 +852,9 @@ describe('address tests', () => {
844852
post_conditions: [],
845853
tx_status: 'success',
846854
block_hash: '0x1234',
855+
index_block_hash: '0x1234',
847856
block_height: 1,
857+
tenure_height: 1,
848858
block_time: 1594647994,
849859
block_time_iso: '2020-07-13T13:46:34.000Z',
850860
burn_block_height: 100123123,
@@ -935,7 +945,9 @@ describe('address tests', () => {
935945
post_conditions: [],
936946
tx_status: 'success',
937947
block_hash: '0x1234',
948+
index_block_hash: '0x1234',
938949
block_height: 1,
950+
tenure_height: 1,
939951
block_time: 1594647994,
940952
block_time_iso: '2020-07-13T13:46:34.000Z',
941953
burn_block_height: 100123123,
@@ -1915,7 +1927,9 @@ describe('address tests', () => {
19151927
post_condition_mode: 'allow',
19161928
post_conditions: [],
19171929
block_hash: '0x1234',
1930+
index_block_hash: '0x1234',
19181931
block_height: 1,
1932+
tenure_height: 1,
19191933
block_time: 39486,
19201934
block_time_iso: '1970-01-01T10:58:06.000Z',
19211935
burn_block_height: 100123123,
@@ -1975,7 +1989,9 @@ describe('address tests', () => {
19751989
post_condition_mode: 'allow',
19761990
post_conditions: [],
19771991
block_hash: '0x1234',
1992+
index_block_hash: '0x1234',
19781993
block_height: 1,
1994+
tenure_height: 1,
19791995
block_time: 39486,
19801996
block_time_iso: '1970-01-01T10:58:06.000Z',
19811997
burn_block_height: 100123123,
@@ -2019,7 +2035,9 @@ describe('address tests', () => {
20192035
post_condition_mode: 'allow',
20202036
post_conditions: [],
20212037
block_hash: '0x1234',
2038+
index_block_hash: '0x1234',
20222039
block_height: 1,
2040+
tenure_height: 1,
20232041
block_time: 39486,
20242042
block_time_iso: '1970-01-01T10:58:06.000Z',
20252043
burn_block_height: 100123123,
@@ -2064,7 +2082,9 @@ describe('address tests', () => {
20642082
post_condition_mode: 'allow',
20652083
post_conditions: [],
20662084
block_hash: '0x1234',
2085+
index_block_hash: '0x1234',
20672086
block_height: 1,
2087+
tenure_height: 1,
20682088
block_time: 39486,
20692089
block_time_iso: '1970-01-01T10:58:06.000Z',
20702090
burn_block_height: 100123123,
@@ -2109,7 +2129,9 @@ describe('address tests', () => {
21092129
post_condition_mode: 'allow',
21102130
post_conditions: [],
21112131
block_hash: '0x1234',
2132+
index_block_hash: '0x1234',
21122133
block_height: 1,
2134+
tenure_height: 1,
21132135
block_time: 39486,
21142136
burn_block_height: 100123123,
21152137
block_time_iso: '1970-01-01T10:58:06.000Z',
@@ -2169,7 +2191,9 @@ describe('address tests', () => {
21692191
post_condition_mode: 'allow',
21702192
post_conditions: [],
21712193
block_hash: '0x1234',
2194+
index_block_hash: '0x1234',
21722195
block_height: 1,
2196+
tenure_height: 1,
21732197
block_time: 39486,
21742198
burn_block_height: 100123123,
21752199
block_time_iso: '1970-01-01T10:58:06.000Z',
@@ -2241,9 +2265,11 @@ describe('address tests', () => {
22412265
tx: {
22422266
anchor_mode: 'any',
22432267
block_hash: '0x1234',
2268+
index_block_hash: '0x1234',
22442269
block_height: 1,
22452270
block_time: 39486,
22462271
block_time_iso: '1970-01-01T10:58:06.000Z',
2272+
tenure_height: 1,
22472273
burn_block_height: 100123123,
22482274
burn_block_time: 39486,
22492275
burn_block_time_iso: '1970-01-01T10:58:06.000Z',
@@ -2322,7 +2348,9 @@ describe('address tests', () => {
23222348
tx: {
23232349
anchor_mode: 'any',
23242350
block_hash: '0x1234',
2351+
index_block_hash: '0x1234',
23252352
block_height: 1,
2353+
tenure_height: 1,
23262354
block_time: 39486,
23272355
block_time_iso: '1970-01-01T10:58:06.000Z',
23282356
burn_block_height: 100123123,
@@ -2401,7 +2429,9 @@ describe('address tests', () => {
24012429
post_condition_mode: 'allow',
24022430
post_conditions: [],
24032431
block_hash: '0x1234',
2432+
index_block_hash: '0x1234',
24042433
block_height: 1,
2434+
tenure_height: 1,
24052435
block_time: 39486,
24062436
block_time_iso: '1970-01-01T10:58:06.000Z',
24072437
burn_block_height: 100123123,

tests/api/search.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ describe('search tests', () => {
555555
anchor_mode: 'any',
556556
is_unanchored: false,
557557
block_hash: '0x1234000000000000000000000000000000000000000000000000000000000000',
558+
index_block_hash: '0xdeadbeef',
558559
parent_block_hash: '0x',
559560
block_height: 1,
560561
block_time: 2837565,
@@ -565,6 +566,7 @@ describe('search tests', () => {
565566
parent_burn_block_time: 1626122935,
566567
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
567568
canonical: true,
569+
tenure_height: 1,
568570
tx_index: 4,
569571
tx_status: 'success',
570572
tx_result: {
@@ -1568,6 +1570,7 @@ describe('search tests', () => {
15681570
execution_cost_write_count: 0,
15691571
execution_cost_write_length: 0,
15701572
fee_rate: '1234',
1573+
index_block_hash: '0x1234',
15711574
is_unanchored: false,
15721575
microblock_canonical: true,
15731576
microblock_hash: '0x',
@@ -1585,6 +1588,7 @@ describe('search tests', () => {
15851588
source_code: '(some-src)',
15861589
},
15871590
sponsored: false,
1591+
tenure_height: 1,
15881592
tx_id: '0x1111880000000000000000000000000000000000000000000000000000000000',
15891593
tx_index: 0,
15901594
tx_result: {

0 commit comments

Comments
 (0)