Skip to content

Commit 78d7d9c

Browse files
authored
feat: add transaction_count for microblocks_accepted in block (#1162)
* feat: added transaction_count for block * refactor: added microblock_tx_count in block * refactor: removed unecessary type * docs: updated block.example.json * refactor: rebased with develop * refactor: moved initializations with declarations
1 parent 15c0c11 commit 78d7d9c

File tree

8 files changed

+62
-16
lines changed

8 files changed

+62
-16
lines changed

docs/entities/blocks/block.example.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@
3030
"execution_cost_read_length": 1659409,
3131
"execution_cost_runtime": 2520952000,
3232
"execution_cost_write_count": 608,
33-
"execution_cost_write_length": 80170
33+
"execution_cost_write_length": 80170,
34+
"microblock_tx_count": {
35+
"0xce0b1a4099d3fc7d5885cc7a3baa952b6d999f9709d0683b98b843597208231c": 5,
36+
"0x4c0529b6448a5885991c5021bd869cc97f1692c128a98b382729dc962203c326": 6,
37+
"0x64968846291dfea1015228a9d4bbd60aac81378cd6774b810b08e59e6b0e7494": 9
38+
}
3439
}

docs/entities/blocks/block.schema.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"execution_cost_read_length",
2323
"execution_cost_runtime",
2424
"execution_cost_write_count",
25-
"execution_cost_write_length"
25+
"execution_cost_write_length",
26+
"microblock_tx_count"
2627
],
2728
"properties": {
2829
"canonical": {
@@ -112,6 +113,13 @@
112113
"execution_cost_write_length": {
113114
"type": "integer",
114115
"description": "Execution cost write length."
116+
},
117+
"microblock_tx_count": {
118+
"type": "object",
119+
"description": "List of txs counts in each accepted microblock",
120+
"additionalProperties": {
121+
"type": "number"
122+
}
115123
}
116124
}
117125
}

docs/generated.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,12 @@ export interface Block {
12881288
* Execution cost write length.
12891289
*/
12901290
execution_cost_write_length: number;
1291+
/**
1292+
* List of txs counts in each accepted microblock
1293+
*/
1294+
microblock_tx_count: {
1295+
[k: string]: number | undefined;
1296+
};
12911297
[k: string]: unknown | undefined;
12921298
}
12931299
/**

src/api/controllers/db-controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ export async function getBlockFromDataStore({
446446
result.block,
447447
result.txs.map(tx => tx.tx_id),
448448
result.microblocks.accepted.map(mb => mb.microblock_hash),
449-
result.microblocks.streamed.map(mb => mb.microblock_hash)
449+
result.microblocks.streamed.map(mb => mb.microblock_hash),
450+
result.microblock_tx_count
450451
);
451452
return { found: true, result: apiBlock };
452453
}
@@ -455,7 +456,8 @@ function parseDbBlock(
455456
dbBlock: DbBlock,
456457
txIds: string[],
457458
microblocksAccepted: string[],
458-
microblocksStreamed: string[]
459+
microblocksStreamed: string[],
460+
microblock_tx_count: Record<string, number>
459461
): Block {
460462
const apiBlock: Block = {
461463
canonical: dbBlock.canonical,
@@ -479,6 +481,7 @@ function parseDbBlock(
479481
execution_cost_runtime: dbBlock.execution_cost_runtime,
480482
execution_cost_write_count: dbBlock.execution_cost_write_count,
481483
execution_cost_write_length: dbBlock.execution_cost_write_length,
484+
microblock_tx_count,
482485
};
483486
return apiBlock;
484487
}
@@ -1074,7 +1077,8 @@ export async function searchHashWithMetadata(
10741077
blockQuery.result.block,
10751078
blockQuery.result.txs.map(tx => tx.tx_id),
10761079
blockQuery.result.microblocks.accepted.map(mb => mb.microblock_hash),
1077-
blockQuery.result.microblocks.streamed.map(mb => mb.microblock_hash)
1080+
blockQuery.result.microblocks.streamed.map(mb => mb.microblock_hash),
1081+
blockQuery.result.microblock_tx_count
10781082
);
10791083
return {
10801084
found: true,

src/datastore/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ export interface DbGetBlockWithMetadataResponse<
506506
microblocks: TWithMicroblocks extends true
507507
? { accepted: DbMicroblock[]; streamed: DbMicroblock[] }
508508
: null;
509+
microblock_tx_count: Record<string, number>;
509510
}
510511

511512
export interface DbRawEventRequest {

src/datastore/pg-store.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ export class PgStore {
218218
return { found: false };
219219
}
220220
let txs: DbTx[] | null = null;
221-
let microblocksAccepted: DbMicroblock[] | null = null;
222-
let microblocksStreamed: DbMicroblock[] | null = null;
221+
const microblocksAccepted: DbMicroblock[] = [];
222+
const microblocksStreamed: DbMicroblock[] = [];
223+
const microblock_tx_count: Record<string, number> = {};
223224
if (metadata?.txs) {
224225
const txQuery = await sql<ContractTxQueryResult[]>`
225226
SELECT ${unsafeCols(sql, [...TX_COLUMNS, abiColumn()])}
@@ -231,21 +232,32 @@ export class PgStore {
231232
txs = txQuery.map(r => parseTxQueryResult(r));
232233
}
233234
if (metadata?.microblocks) {
234-
const microblocksQuery = await sql<MicroblockQueryResult[]>`
235-
SELECT ${sql(MICROBLOCK_COLUMNS)}
235+
const microblocksQuery = await sql<
236+
(MicroblockQueryResult & { transaction_count: number })[]
237+
>`
238+
SELECT ${sql(MICROBLOCK_COLUMNS)}, (
239+
SELECT COUNT(tx_id)::integer as transaction_count
240+
FROM txs
241+
WHERE txs.microblock_hash = microblocks.microblock_hash
242+
AND canonical = true AND microblock_canonical = true
243+
)
236244
FROM microblocks
237245
WHERE parent_index_block_hash
238246
IN ${sql([block.result.index_block_hash, block.result.parent_index_block_hash])}
239247
AND microblock_canonical = true
240248
ORDER BY microblock_sequence DESC
241249
`;
242-
const parsedMicroblocks = microblocksQuery.map(r => parseMicroblockQueryResult(r));
243-
microblocksAccepted = parsedMicroblocks.filter(
244-
mb => mb.parent_index_block_hash === block.result.parent_index_block_hash
245-
);
246-
microblocksStreamed = parsedMicroblocks.filter(
247-
mb => mb.parent_index_block_hash === block.result.index_block_hash
248-
);
250+
for (const mb of microblocksQuery) {
251+
const parsedMicroblock = parseMicroblockQueryResult(mb);
252+
const count = mb.transaction_count;
253+
if (parsedMicroblock.parent_index_block_hash === block.result.parent_index_block_hash) {
254+
microblocksAccepted.push(parsedMicroblock);
255+
microblock_tx_count[parsedMicroblock.microblock_hash] = count;
256+
}
257+
if (parsedMicroblock.parent_index_block_hash === block.result.index_block_hash) {
258+
microblocksStreamed.push(parsedMicroblock);
259+
}
260+
}
249261
}
250262
type ResultType = DbGetBlockWithMetadataResponse<TWithTxs, TWithMicroblocks>;
251263
const result: ResultType = {
@@ -255,6 +267,7 @@ export class PgStore {
255267
accepted: microblocksAccepted,
256268
streamed: microblocksStreamed,
257269
} as ResultType['microblocks'],
270+
microblock_tx_count,
258271
};
259272
return {
260273
found: true,

src/tests/api-tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,7 @@ describe('api tests', () => {
23432343
parent_microblock_hash: '0x',
23442344
parent_microblock_sequence: 0,
23452345
txs: ['0x4567000000000000000000000000000000000000000000000000000000000000'],
2346+
microblock_tx_count: {},
23462347
};
23472348

23482349
const searchResult1 = await supertest(api.server).get(
@@ -7288,6 +7289,7 @@ describe('api tests', () => {
72887289
execution_cost_runtime: 0,
72897290
execution_cost_write_count: 0,
72907291
execution_cost_write_length: 0,
7292+
microblock_tx_count: {},
72917293
};
72927294

72937295
expect(blockQuery.result).toEqual(expectedResp);
@@ -7409,6 +7411,7 @@ describe('api tests', () => {
74097411
parent_microblock_hash: '0x00',
74107412
parent_microblock_sequence: 0,
74117413
txs: ['0x0001'],
7414+
microblock_tx_count: {},
74127415
};
74137416
const fetch1 = await supertest(api.server).get(
74147417
`/extended/v1/block/by_height/${block1.block.block_height}`
@@ -7455,7 +7458,11 @@ describe('api tests', () => {
74557458
parent_microblock_sequence: microblock1.microblocks[0].microblock_sequence,
74567459
// Ensure micro-orphaned tx `0x1002` is not included
74577460
txs: ['0x0002', '0x1001'],
7461+
microblock_tx_count: {
7462+
'0xff01': microblock1.txs.length,
7463+
},
74587464
};
7465+
74597466
expect(fetch2.status).toBe(200);
74607467
expect(fetch2.type).toBe('application/json');
74617468
expect(JSON.parse(fetch2.text)).toEqual(expectedResp2);

src/tests/cache-control-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ describe('cache-control tests', () => {
173173
execution_cost_runtime: 0,
174174
execution_cost_write_count: 0,
175175
execution_cost_write_length: 0,
176+
microblock_tx_count: {},
176177
};
177178

178179
expect(blockQuery.result).toEqual(expectedResp1);
@@ -316,6 +317,7 @@ describe('cache-control tests', () => {
316317
execution_cost_runtime: 0,
317318
execution_cost_write_count: 0,
318319
execution_cost_write_length: 0,
320+
microblock_tx_count: {},
319321
};
320322

321323
const fetchBlockByHash2 = await supertest(api.server).get(

0 commit comments

Comments
 (0)