Skip to content

Commit ac7c41b

Browse files
authored
feat: store total transaction size in blocks table (#2204)
* feat: save block size to database * feat: store total transaction size in blocks table * fix: inserts
1 parent 26c53dc commit ac7c41b

17 files changed

+127
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable camelcase */
2+
3+
exports.shorthands = undefined;
4+
5+
exports.up = pgm => {
6+
pgm.addColumn('blocks', {
7+
tx_total_size: {
8+
type: 'int',
9+
notNull: true,
10+
default: 0,
11+
},
12+
});
13+
pgm.sql(`
14+
UPDATE blocks
15+
SET tx_total_size = (
16+
SELECT SUM(OCTET_LENGTH(raw_tx))
17+
FROM txs
18+
WHERE index_block_hash = blocks.index_block_hash
19+
)
20+
`);
21+
};
22+
23+
exports.down = pgm => {
24+
pgm.dropColumn('blocks', ['tx_total_size']);
25+
};

src/datastore/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface DbBlock {
2222
execution_cost_runtime: number;
2323
execution_cost_write_count: number;
2424
execution_cost_write_length: number;
25+
tx_total_size: number;
2526
tx_count: number;
2627
block_time: number;
2728
signer_bitvec: string | null;
@@ -862,6 +863,7 @@ export interface BlockQueryResult {
862863
execution_cost_runtime: string;
863864
execution_cost_write_count: string;
864865
execution_cost_write_length: string;
866+
tx_total_size: number;
865867
tx_count: number;
866868
signer_bitvec: string | null;
867869
tenure_height: number | null;
@@ -1287,6 +1289,7 @@ export interface BlockInsertValues {
12871289
execution_cost_runtime: number;
12881290
execution_cost_write_count: number;
12891291
execution_cost_write_length: number;
1292+
tx_total_size: number;
12901293
tx_count: number;
12911294
signer_bitvec: string | null;
12921295
signer_signatures: PgBytea[] | null;

src/datastore/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export const BLOCK_COLUMNS = [
185185
'execution_cost_write_count',
186186
'execution_cost_write_length',
187187
'tx_count',
188+
'tx_total_size',
188189
'signer_bitvec',
189190
'tenure_height',
190191
];
@@ -485,6 +486,7 @@ export function parseBlockQueryResult(row: BlockQueryResult): DbBlock {
485486
execution_cost_runtime: Number.parseInt(row.execution_cost_runtime),
486487
execution_cost_write_count: Number.parseInt(row.execution_cost_write_count),
487488
execution_cost_write_length: Number.parseInt(row.execution_cost_write_length),
489+
tx_total_size: row.tx_total_size,
488490
tx_count: row.tx_count,
489491
signer_bitvec: row.signer_bitvec,
490492
signer_signatures: null, // this field is not queried from db by default due to size constraints

src/datastore/pg-write-store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ export class PgWriteStore extends PgStore {
480480
execution_cost_runtime: block.execution_cost_runtime,
481481
execution_cost_write_count: block.execution_cost_write_count,
482482
execution_cost_write_length: block.execution_cost_write_length,
483+
tx_total_size: block.tx_total_size,
483484
tx_count: block.tx_count,
484485
signer_bitvec: block.signer_bitvec,
485486
signer_signatures: block.signer_signatures,
@@ -3372,6 +3373,7 @@ export class PgWriteStore extends PgStore {
33723373
execution_cost_runtime: block.execution_cost_runtime,
33733374
execution_cost_write_count: block.execution_cost_write_count,
33743375
execution_cost_write_length: block.execution_cost_write_length,
3376+
tx_total_size: block.tx_total_size,
33753377
tx_count: block.tx_count,
33763378
signer_bitvec: block.signer_bitvec,
33773379
signer_signatures: block.signer_signatures,

src/event-stream/event-server.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,12 @@ export function parseNewBlockMessage(
10681068
write_length: 0,
10691069
}
10701070
);
1071+
// Compute total transaction size in this block. Remove `0x` prefix and check byte length on all
1072+
// raw txs.
1073+
const txTotalSize = parsedTxs.reduce(
1074+
(acc, { core_tx: { raw_tx } }) => acc + Math.ceil((raw_tx.length - 2) / 2),
1075+
0
1076+
);
10711077

10721078
if (typeof msg.tenure_height !== 'number' && msg.signer_bitvec) {
10731079
logger.warn(
@@ -1093,6 +1099,7 @@ export function parseNewBlockMessage(
10931099
execution_cost_runtime: execCost.runtime,
10941100
execution_cost_write_count: execCost.write_count,
10951101
execution_cost_write_length: execCost.write_length,
1102+
tx_total_size: txTotalSize,
10961103
tx_count: msg.transactions.length,
10971104
block_time: blockData.block_time,
10981105
signer_bitvec: signerBitvec,

tests/api/address.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ describe('address tests', () => {
9393
execution_cost_write_count: 0,
9494
execution_cost_write_length: 0,
9595
tx_count: 1,
96+
tx_total_size: 1,
9697
signer_bitvec: null,
9798
signer_signatures: null,
9899
};
@@ -1171,6 +1172,7 @@ describe('address tests', () => {
11711172
execution_cost_write_count: 0,
11721173
execution_cost_write_length: 0,
11731174
tx_count: 1,
1175+
tx_total_size: 1,
11741176
signer_bitvec: null,
11751177
signer_signatures: null,
11761178
};
@@ -2390,6 +2392,7 @@ describe('address tests', () => {
23902392
execution_cost_write_count: 0,
23912393
execution_cost_write_length: 0,
23922394
tx_count: 1,
2395+
tx_total_size: 1,
23932396
signer_bitvec: null,
23942397
signer_signatures: null,
23952398
};

tests/api/block.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ describe('block tests', () => {
8787
execution_cost_write_count: 0,
8888
execution_cost_write_length: 0,
8989
tx_count: 1,
90+
tx_total_size: 1,
9091
signer_bitvec: null,
9192
signer_signatures: null,
9293
};

tests/api/cache-control.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('cache-control tests', () => {
6060
execution_cost_write_count: 0,
6161
execution_cost_write_length: 0,
6262
tx_count: 1,
63+
tx_total_size: 1,
6364
signer_bitvec: null,
6465
signer_signatures: null,
6566
};

0 commit comments

Comments
 (0)