Skip to content

Commit 3e2d524

Browse files
authored
feat: add burn_block_height to transactions (#1969)
* feat: add burn_block_height to transactions * test: update burn_block_height in tests
1 parent d38b78a commit 3e2d524

22 files changed

+157
-1
lines changed

docs/api/transaction/get-transactions.example.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"block_hash": "0x28a9e2749b82bdc058776781a5abd5c9a6efed38f05545a8a51152655b8e1f4b",
1717
"parent_block_hash": "0x3789b75ccfe7f2acf85c3f069fd5b8f95f73aba5332fa618243957d1c017a2a3",
1818
"block_height": 21709,
19+
"burn_block_height": 51396,
1920
"burn_block_time": 1626286436,
2021
"burn_block_time_iso": "2021-07-14T18:13:56.000Z",
2122
"canonical": true,

docs/entities/transactions/abstract-transaction.schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"block_height",
1414
"block_time",
1515
"block_time_iso",
16+
"burn_block_height",
1617
"burn_block_time",
1718
"burn_block_time_iso",
1819
"parent_burn_block_time",
@@ -51,6 +52,10 @@
5152
"type": "string",
5253
"description": "An ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) indicating when this block was mined."
5354
},
55+
"burn_block_height": {
56+
"type": "integer",
57+
"description": "Height of the anchor burn block."
58+
},
5459
"burn_block_time": {
5560
"type": "integer",
5661
"description": "Unix timestamp (in seconds) indicating when this block was mined"

docs/entities/transactions/transactions-list-detail.example.json

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"block_hash": "0x0123",
1616
"parent_block_hash": "0x5678",
1717
"block_height": 0,
18+
"burn_block_height": 69287,
1819
"burn_block_time": 39486,
1920
"burn_block_time_iso": "1970-01-01T10:58:06.000Z",
2021
"parent_burn_block_time": 1626122935,
@@ -51,6 +52,7 @@
5152
"block_hash": "0x0123",
5253
"parent_block_hash": "0x5678",
5354
"block_height": 0,
55+
"burn_block_height": 69287,
5456
"burn_block_time": 39486,
5557
"burn_block_time_iso": "1970-01-01T10:58:06.000Z",
5658
"parent_burn_block_time": 1626122935,

docs/generated.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ export type AbstractTransaction = BaseTransaction & {
440440
* An ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) indicating when this block was mined.
441441
*/
442442
block_time_iso: string;
443+
burn_block_height: number;
443444
/**
444445
* Unix timestamp (in seconds) indicating when this block was mined
445446
*/
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
2+
exports.up = pgm => {
3+
pgm.addColumn('txs', {
4+
burn_block_height: {
5+
type: 'integer',
6+
notNull: false,
7+
},
8+
});
9+
pgm.sql(`
10+
UPDATE txs
11+
SET burn_block_height = blocks.burn_block_height
12+
FROM blocks
13+
WHERE txs.index_block_hash = blocks.index_block_hash
14+
`);
15+
pgm.alterColumn('txs', 'burn_block_height', { notNull: true });
16+
};
17+
18+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
19+
exports.down = pgm => {
20+
pgm.dropColumn('txs', 'burn_block_height');
21+
};

src/api/controllers/db-controller.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ function parseDbAbstractTx(dbTx: DbTx, baseTx: BaseTransaction): AbstractTransac
10721072
: dbTx.burn_block_time > 0
10731073
? unixEpochToIso(dbTx.burn_block_time)
10741074
: '',
1075+
burn_block_height: dbTx.burn_block_height,
10751076
burn_block_time: dbTx.burn_block_time,
10761077
burn_block_time_iso: dbTx.burn_block_time > 0 ? unixEpochToIso(dbTx.burn_block_time) : '',
10771078
parent_burn_block_time: dbTx.parent_burn_block_time,

src/datastore/common.ts

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export interface DbTx extends BaseTx {
184184
parent_block_hash: string;
185185
block_height: number;
186186
burn_block_time: number;
187+
burn_block_height: number;
187188
parent_burn_block_time: number;
188189
block_time: number;
189190

@@ -953,6 +954,7 @@ export interface TxQueryResult {
953954
block_time: number;
954955
parent_block_hash: string;
955956
block_height: number;
957+
burn_block_height: number;
956958
burn_block_time: number;
957959
parent_burn_block_time: number;
958960
nonce: number;

src/datastore/helpers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const TX_COLUMNS = [
7979
'parent_block_hash',
8080
'block_height',
8181
'block_time',
82+
'burn_block_height',
8283
'burn_block_time',
8384
'parent_burn_block_time',
8485
'type_id',
@@ -369,6 +370,7 @@ export function parseTxQueryResult(result: ContractTxQueryResult): DbTx {
369370
block_time: result.block_time || result.burn_block_time,
370371
parent_block_hash: result.parent_block_hash,
371372
block_height: result.block_height,
373+
burn_block_height: result.burn_block_height,
372374
burn_block_time: result.burn_block_time,
373375
parent_burn_block_time: result.parent_burn_block_time,
374376
type_id: result.type_id as DbTxTypeId,
@@ -1185,6 +1187,7 @@ export function createDbTxFromCoreMsg(msg: CoreNodeParsedTxMessage): DbTxRaw {
11851187
parent_block_hash: msg.parent_block_hash,
11861188
block_hash: msg.block_hash,
11871189
block_height: msg.block_height,
1190+
burn_block_height: msg.burn_block_height,
11881191
burn_block_time: msg.burn_block_time,
11891192
parent_burn_block_time: msg.parent_burn_block_time,
11901193
block_time: msg.block_time,

src/datastore/pg-write-store.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export class PgWriteStore extends PgStore {
236236
parentMicroblockHash: data.block.parent_microblock_hash,
237237
parentMicroblockSequence: data.block.parent_microblock_sequence,
238238
burnBlockTime: data.block.burn_block_time,
239+
burnBlockHeight: data.block.burn_block_height,
239240
}
240241
);
241242

@@ -678,6 +679,7 @@ export class PgWriteStore extends PgStore {
678679
indexBlockHash: '',
679680
blockHash: '',
680681
burnBlockTime: -1,
682+
burnBlockHeight: -1,
681683
microblocks: orphanedMicroblocks,
682684
});
683685
const microOrphanedTxs = microOrphanResult.updatedTxs;
@@ -748,6 +750,7 @@ export class PgWriteStore extends PgStore {
748750
tx_index = tx_index + 1,
749751
block_hash = ${blockOne.block_hash},
750752
index_block_hash = ${blockOne.index_block_hash},
753+
burn_block_height = ${blockOne.burn_block_height},
751754
burn_block_time = ${blockOne.burn_block_time},
752755
parent_block_hash = ${blockOne.parent_block_hash}
753756
WHERE block_height = 0
@@ -1527,6 +1530,7 @@ export class PgWriteStore extends PgStore {
15271530
parentMicroblockHash: string;
15281531
parentMicroblockSequence: number;
15291532
burnBlockTime: number;
1533+
burnBlockHeight: number;
15301534
}
15311535
): Promise<{
15321536
acceptedMicroblockTxs: DbTx[];
@@ -1583,6 +1587,7 @@ export class PgWriteStore extends PgStore {
15831587
indexBlockHash: blockData.indexBlockHash,
15841588
blockHash: blockData.blockHash,
15851589
burnBlockTime: blockData.burnBlockTime,
1590+
burnBlockHeight: blockData.burnBlockHeight,
15861591
microblocks: orphanedMicroblocks,
15871592
});
15881593
orphanedMicroblockTxs = microOrphanResult.updatedTxs;
@@ -1595,6 +1600,7 @@ export class PgWriteStore extends PgStore {
15951600
indexBlockHash: blockData.indexBlockHash,
15961601
blockHash: blockData.blockHash,
15971602
burnBlockTime: blockData.burnBlockTime,
1603+
burnBlockHeight: blockData.burnBlockHeight,
15981604
microblocks: acceptedMicroblocks,
15991605
});
16001606
acceptedMicroblockTxs = microAcceptResult.updatedTxs;
@@ -1707,6 +1713,7 @@ export class PgWriteStore extends PgStore {
17071713
parent_block_hash: tx.parent_block_hash,
17081714
block_height: tx.block_height,
17091715
block_time: tx.block_time,
1716+
burn_block_height: tx.burn_block_height,
17101717
burn_block_time: tx.burn_block_time,
17111718
parent_burn_block_time: tx.parent_burn_block_time,
17121719
type_id: tx.type_id,
@@ -2328,6 +2335,7 @@ export class PgWriteStore extends PgStore {
23282335
indexBlockHash: string;
23292336
blockHash: string;
23302337
burnBlockTime: number;
2338+
burnBlockHeight: number;
23312339
microblocks: string[];
23322340
}
23332341
): Promise<{ updatedTxs: DbTx[] }> {
@@ -2349,7 +2357,8 @@ export class PgWriteStore extends PgStore {
23492357
UPDATE txs
23502358
SET microblock_canonical = ${args.isMicroCanonical},
23512359
canonical = ${args.isCanonical}, index_block_hash = ${args.indexBlockHash},
2352-
block_hash = ${args.blockHash}, burn_block_time = ${args.burnBlockTime}
2360+
block_hash = ${args.blockHash}, burn_block_time = ${args.burnBlockTime},
2361+
burn_block_height = ${args.burnBlockHeight}
23532362
WHERE microblock_hash IN ${sql(args.microblocks)}
23542363
AND (index_block_hash = ${args.indexBlockHash} OR index_block_hash = '\\x'::bytea)
23552364
RETURNING ${sql(TX_COLUMNS)}
@@ -2920,6 +2929,7 @@ export class PgWriteStore extends PgStore {
29202929
parentMicroblockHash: orphanedBlock.parent_microblock_hash,
29212930
parentMicroblockSequence: orphanedBlock.parent_microblock_sequence,
29222931
burnBlockTime: orphanedBlock.burn_block_time,
2932+
burnBlockHeight: orphanedBlock.burn_block_height,
29232933
});
29242934
microCanonicalUpdateResult.orphanedMicroblocks.forEach(mb => {
29252935
microblocksOrphaned.add(mb);
@@ -2958,6 +2968,7 @@ export class PgWriteStore extends PgStore {
29582968
parentMicroblockHash: restoredBlock.parent_microblock_hash,
29592969
parentMicroblockSequence: restoredBlock.parent_microblock_sequence,
29602970
burnBlockTime: restoredBlock.burn_block_time,
2971+
burnBlockHeight: restoredBlock.burn_block_height,
29612972
});
29622973
microCanonicalUpdateResult.orphanedMicroblocks.forEach(mb => {
29632974
microblocksOrphaned.add(mb);
@@ -3125,6 +3136,7 @@ export class PgWriteStore extends PgStore {
31253136
parent_block_hash: tx.parent_block_hash,
31263137
block_height: tx.block_height,
31273138
block_time: tx.block_time ?? 0,
3139+
burn_block_height: tx.burn_block_height,
31283140
burn_block_time: tx.burn_block_time,
31293141
parent_burn_block_time: tx.parent_burn_block_time,
31303142
type_id: tx.type_id,

src/event-stream/core-node-message.ts

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export interface CoreNodeParsedTxMessage {
337337
microblock_sequence: number;
338338
microblock_hash: string;
339339
block_height: number;
340+
burn_block_height: number;
340341
burn_block_time: number;
341342
parent_burn_block_time: number;
342343
parent_burn_block_hash: string;

src/event-stream/reader.ts

+1
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ export function parseMessageTransaction(
941941
parent_burn_block_hash: blockData.parent_burn_block_hash,
942942
parent_burn_block_time: blockData.parent_burn_block_timestamp,
943943
block_height: blockData.block_height,
944+
burn_block_height: blockData.burn_block_height,
944945
burn_block_time: blockData.burn_block_time,
945946
microblock_sequence: coreTx.microblock_sequence ?? I32_MAX,
946947
microblock_hash: coreTx.microblock_hash ?? '',

src/test-utils/test-builders.ts

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function testTx(args?: TestTxArgs): DataStoreTxEventData {
205205
index_block_hash: args?.index_block_hash ?? INDEX_BLOCK_HASH,
206206
block_hash: args?.block_hash ?? BLOCK_HASH,
207207
block_height: args?.block_height ?? BLOCK_HEIGHT,
208+
burn_block_height: args?.block_height ?? BLOCK_HEIGHT,
208209
burn_block_time: args?.burn_block_time ?? BURN_BLOCK_TIME,
209210
block_time: args?.block_time ?? STACKS_BLOCK_TIME,
210211
parent_burn_block_time: BURN_BLOCK_TIME,

0 commit comments

Comments
 (0)