Skip to content

Commit 8d2d86f

Browse files
authored
feat: added execution cost to block response #735
1 parent 8a9222a commit 8d2d86f

File tree

12 files changed

+622
-5
lines changed

12 files changed

+622
-5
lines changed

docs/entities/blocks/block.schema.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@
33
"title": "Block",
44
"description": "A block",
55
"type": "object",
6-
"required": ["canonical", "height", "hash", "parent_block_hash", "txs", "burn_block_time", "burn_block_time_iso", "burn_block_hash", "burn_block_height", "miner_txid", "parent_microblock_hash", "parent_microblock_sequence", "microblocks_accepted", "microblocks_streamed"],
6+
"required": [
7+
"canonical",
8+
"height",
9+
"hash",
10+
"parent_block_hash",
11+
"txs",
12+
"burn_block_time",
13+
"burn_block_time_iso",
14+
"burn_block_hash",
15+
"burn_block_height",
16+
"miner_txid",
17+
"parent_microblock_hash",
18+
"parent_microblock_sequence",
19+
"microblocks_accepted",
20+
"microblocks_streamed",
21+
"execution_cost_read_count",
22+
"execution_cost_read_length",
23+
"execution_cost_runtime",
24+
"execution_cost_write_count",
25+
"execution_cost_write_length"
26+
],
727
"properties": {
828
"canonical": {
929
"type": "boolean",
@@ -72,6 +92,26 @@
7292
"type": "string",
7393
"description": "Microblock hash"
7494
}
95+
},
96+
"execution_cost_read_count": {
97+
"type": "integer",
98+
"description": "Execution cost read count."
99+
},
100+
"execution_cost_read_length": {
101+
"type": "integer",
102+
"description": "Execution cost read length."
103+
},
104+
"execution_cost_runtime": {
105+
"type": "integer",
106+
"description": "Execution cost runtime."
107+
},
108+
"execution_cost_write_count": {
109+
"type": "integer",
110+
"description": "Execution cost write count."
111+
},
112+
"execution_cost_write_length": {
113+
"type": "integer",
114+
"description": "Execution cost write length."
75115
}
76116
}
77117
}

docs/generated.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,26 @@ export interface Block {
12141214
* List of microblocks that were streamed/produced by this anchor block's miner. This list only includes microblocks that were accepted in the following anchor block. Microblocks that were orphaned are not included in this list.
12151215
*/
12161216
microblocks_streamed: string[];
1217+
/**
1218+
* Execution cost read count.
1219+
*/
1220+
execution_cost_read_count: number;
1221+
/**
1222+
* Execution cost read length.
1223+
*/
1224+
execution_cost_read_length: number;
1225+
/**
1226+
* Execution cost runtime.
1227+
*/
1228+
execution_cost_runtime: number;
1229+
/**
1230+
* Execution cost write count.
1231+
*/
1232+
execution_cost_write_count: number;
1233+
/**
1234+
* Execution cost write length.
1235+
*/
1236+
execution_cost_write_length: number;
12171237
[k: string]: unknown | undefined;
12181238
}
12191239
/**

src/api/controllers/db-controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ export function parseDbBlock(
466466
txs: [...txIds],
467467
microblocks_accepted: [...microblocksAccepted],
468468
microblocks_streamed: [...microblocksStreamed],
469+
execution_cost_read_count: dbBlock.execution_cost_read_count,
470+
execution_cost_read_length: dbBlock.execution_cost_read_length,
471+
execution_cost_runtime: dbBlock.execution_cost_runtime,
472+
execution_cost_write_count: dbBlock.execution_cost_write_count,
473+
execution_cost_write_length: dbBlock.execution_cost_write_length,
469474
};
470475
return apiBlock;
471476
}

src/datastore/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export interface DbBlock {
3333
block_height: number;
3434
/** Set to `true` if entry corresponds to the canonical chain tip */
3535
canonical: boolean;
36+
/** Sum of the execution costs for each tx included in the block */
37+
execution_cost_read_count: number;
38+
execution_cost_read_length: number;
39+
execution_cost_runtime: number;
40+
execution_cost_write_count: number;
41+
execution_cost_write_length: number;
3642
}
3743

3844
/** An interface representing the microblock data that can be constructed _only_ from the /new_microblocks payload */

src/datastore/postgres-store.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ const MEMPOOL_TX_ID_COLUMNS = `
293293
const BLOCK_COLUMNS = `
294294
block_hash, index_block_hash,
295295
parent_index_block_hash, parent_block_hash, parent_microblock_hash, parent_microblock_sequence,
296-
block_height, burn_block_time, burn_block_hash, burn_block_height, miner_txid, canonical
296+
block_height, burn_block_time, burn_block_hash, burn_block_height, miner_txid, canonical,
297+
execution_cost_read_count, execution_cost_read_length, execution_cost_runtime,
298+
execution_cost_write_count, execution_cost_write_length
297299
`;
298300

299301
const MICROBLOCK_COLUMNS = `
@@ -316,6 +318,11 @@ interface BlockQueryResult {
316318
burn_block_height: number;
317319
miner_txid: Buffer;
318320
canonical: boolean;
321+
execution_cost_read_count: string;
322+
execution_cost_read_length: string;
323+
execution_cost_runtime: string;
324+
execution_cost_write_count: string;
325+
execution_cost_write_length: string;
319326
}
320327

321328
interface MicroblockQueryResult {
@@ -972,6 +979,44 @@ export class PgDataStore
972979
}
973980
}
974981

982+
//calculate total execution cost of the block
983+
const totalCost = data.txs.reduce(
984+
(previousValue, currentValue) => {
985+
const {
986+
execution_cost_read_count,
987+
execution_cost_read_length,
988+
execution_cost_runtime,
989+
execution_cost_write_count,
990+
execution_cost_write_length,
991+
} = previousValue;
992+
993+
return {
994+
execution_cost_read_count:
995+
execution_cost_read_count + currentValue.tx.execution_cost_read_count,
996+
execution_cost_read_length:
997+
execution_cost_read_length + currentValue.tx.execution_cost_read_length,
998+
execution_cost_runtime: execution_cost_runtime + currentValue.tx.execution_cost_runtime,
999+
execution_cost_write_count:
1000+
execution_cost_write_count + currentValue.tx.execution_cost_write_count,
1001+
execution_cost_write_length:
1002+
execution_cost_write_length + currentValue.tx.execution_cost_write_length,
1003+
};
1004+
},
1005+
{
1006+
execution_cost_read_count: 0,
1007+
execution_cost_read_length: 0,
1008+
execution_cost_runtime: 0,
1009+
execution_cost_write_count: 0,
1010+
execution_cost_write_length: 0,
1011+
}
1012+
);
1013+
1014+
data.block.execution_cost_read_count = totalCost.execution_cost_read_count;
1015+
data.block.execution_cost_read_length = totalCost.execution_cost_read_length;
1016+
data.block.execution_cost_runtime = totalCost.execution_cost_runtime;
1017+
data.block.execution_cost_write_count = totalCost.execution_cost_write_count;
1018+
data.block.execution_cost_write_length = totalCost.execution_cost_write_length;
1019+
9751020
// Find microblocks that weren't already inserted via the unconfirmed microblock event.
9761021
// This happens when a stacks-node is syncing and receives confirmed microblocks with their anchor block at the same time.
9771022
if (data.microblocks.length > 0) {
@@ -2277,8 +2322,10 @@ export class PgDataStore
22772322
INSERT INTO blocks(
22782323
block_hash, index_block_hash,
22792324
parent_index_block_hash, parent_block_hash, parent_microblock_hash, parent_microblock_sequence,
2280-
block_height, burn_block_time, burn_block_hash, burn_block_height, miner_txid, canonical
2281-
) values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
2325+
block_height, burn_block_time, burn_block_hash, burn_block_height, miner_txid, canonical,
2326+
execution_cost_read_count, execution_cost_read_length, execution_cost_runtime,
2327+
execution_cost_write_count, execution_cost_write_length
2328+
) values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
22822329
ON CONFLICT (index_block_hash)
22832330
DO NOTHING
22842331
`,
@@ -2295,6 +2342,11 @@ export class PgDataStore
22952342
block.burn_block_height,
22962343
hexToBuffer(block.miner_txid),
22972344
block.canonical,
2345+
block.execution_cost_read_count,
2346+
block.execution_cost_read_length,
2347+
block.execution_cost_runtime,
2348+
block.execution_cost_write_count,
2349+
block.execution_cost_write_length,
22982350
]
22992351
);
23002352
return result.rowCount;
@@ -2315,6 +2367,11 @@ export class PgDataStore
23152367
burn_block_height: row.burn_block_height,
23162368
miner_txid: bufferToHexPrefixString(row.miner_txid),
23172369
canonical: row.canonical,
2370+
execution_cost_read_count: Number.parseInt(row.execution_cost_read_count),
2371+
execution_cost_read_length: Number.parseInt(row.execution_cost_read_length),
2372+
execution_cost_runtime: Number.parseInt(row.execution_cost_runtime),
2373+
execution_cost_write_count: Number.parseInt(row.execution_cost_write_count),
2374+
execution_cost_write_length: Number.parseInt(row.execution_cost_write_length),
23182375
};
23192376
return block;
23202377
}

src/event-stream/event-server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ async function handleBlockMessage(
249249
burn_block_hash: msg.burn_block_hash,
250250
burn_block_height: msg.burn_block_height,
251251
miner_txid: msg.miner_txid,
252+
execution_cost_read_count: 0,
253+
execution_cost_read_length: 0,
254+
execution_cost_runtime: 0,
255+
execution_cost_write_count: 0,
256+
execution_cost_write_length: 0,
252257
};
253258

254259
logger.verbose(`Received block ${msg.block_hash} (${msg.block_height}) from node`, dbBlock);

src/migrations/1584604583726_blocks.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
5252
type: 'boolean',
5353
notNull: true,
5454
},
55+
execution_cost_read_count: {
56+
type: 'bigint',
57+
notNull: true,
58+
},
59+
execution_cost_read_length: {
60+
type: 'bigint',
61+
notNull: true,
62+
},
63+
execution_cost_runtime: {
64+
type: 'bigint',
65+
notNull: true,
66+
},
67+
execution_cost_write_count: {
68+
type: 'bigint',
69+
notNull: true,
70+
},
71+
execution_cost_write_length: {
72+
type: 'bigint',
73+
notNull: true,
74+
},
5575
});
5676
pgm.createIndex('blocks', 'block_height');
5777
pgm.createIndex('blocks', 'block_hash');

src/tests-bns/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ describe('BNS API tests', () => {
2626
burn_block_height: 123,
2727
miner_txid: '0x4321',
2828
canonical: true,
29+
execution_cost_read_count: 0,
30+
execution_cost_read_length: 0,
31+
execution_cost_runtime: 0,
32+
execution_cost_write_count: 0,
33+
execution_cost_write_length: 0,
2934
};
3035

3136
beforeAll(async () => {

0 commit comments

Comments
 (0)