Skip to content

Commit

Permalink
feat: store total transaction size in blocks table (#2204)
Browse files Browse the repository at this point in the history
* feat: save block size to database

* feat: store total transaction size in blocks table

* fix: inserts
  • Loading branch information
rafaelcr authored Jan 22, 2025
1 parent 26c53dc commit ac7c41b
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 0 deletions.
25 changes: 25 additions & 0 deletions migrations/1737567411419_block-tx-total-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable camelcase */

exports.shorthands = undefined;

exports.up = pgm => {
pgm.addColumn('blocks', {
tx_total_size: {
type: 'int',
notNull: true,
default: 0,
},
});
pgm.sql(`
UPDATE blocks
SET tx_total_size = (
SELECT SUM(OCTET_LENGTH(raw_tx))
FROM txs
WHERE index_block_hash = blocks.index_block_hash
)
`);
};

exports.down = pgm => {
pgm.dropColumn('blocks', ['tx_total_size']);
};
3 changes: 3 additions & 0 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface DbBlock {
execution_cost_runtime: number;
execution_cost_write_count: number;
execution_cost_write_length: number;
tx_total_size: number;
tx_count: number;
block_time: number;
signer_bitvec: string | null;
Expand Down Expand Up @@ -862,6 +863,7 @@ export interface BlockQueryResult {
execution_cost_runtime: string;
execution_cost_write_count: string;
execution_cost_write_length: string;
tx_total_size: number;
tx_count: number;
signer_bitvec: string | null;
tenure_height: number | null;
Expand Down Expand Up @@ -1287,6 +1289,7 @@ export interface BlockInsertValues {
execution_cost_runtime: number;
execution_cost_write_count: number;
execution_cost_write_length: number;
tx_total_size: number;
tx_count: number;
signer_bitvec: string | null;
signer_signatures: PgBytea[] | null;
Expand Down
2 changes: 2 additions & 0 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export const BLOCK_COLUMNS = [
'execution_cost_write_count',
'execution_cost_write_length',
'tx_count',
'tx_total_size',
'signer_bitvec',
'tenure_height',
];
Expand Down Expand Up @@ -485,6 +486,7 @@ export function parseBlockQueryResult(row: BlockQueryResult): DbBlock {
execution_cost_runtime: Number.parseInt(row.execution_cost_runtime),
execution_cost_write_count: Number.parseInt(row.execution_cost_write_count),
execution_cost_write_length: Number.parseInt(row.execution_cost_write_length),
tx_total_size: row.tx_total_size,
tx_count: row.tx_count,
signer_bitvec: row.signer_bitvec,
signer_signatures: null, // this field is not queried from db by default due to size constraints
Expand Down
2 changes: 2 additions & 0 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ export class PgWriteStore extends PgStore {
execution_cost_runtime: block.execution_cost_runtime,
execution_cost_write_count: block.execution_cost_write_count,
execution_cost_write_length: block.execution_cost_write_length,
tx_total_size: block.tx_total_size,
tx_count: block.tx_count,
signer_bitvec: block.signer_bitvec,
signer_signatures: block.signer_signatures,
Expand Down Expand Up @@ -3372,6 +3373,7 @@ export class PgWriteStore extends PgStore {
execution_cost_runtime: block.execution_cost_runtime,
execution_cost_write_count: block.execution_cost_write_count,
execution_cost_write_length: block.execution_cost_write_length,
tx_total_size: block.tx_total_size,
tx_count: block.tx_count,
signer_bitvec: block.signer_bitvec,
signer_signatures: block.signer_signatures,
Expand Down
7 changes: 7 additions & 0 deletions src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,12 @@ export function parseNewBlockMessage(
write_length: 0,
}
);
// Compute total transaction size in this block. Remove `0x` prefix and check byte length on all
// raw txs.
const txTotalSize = parsedTxs.reduce(
(acc, { core_tx: { raw_tx } }) => acc + Math.ceil((raw_tx.length - 2) / 2),
0
);

if (typeof msg.tenure_height !== 'number' && msg.signer_bitvec) {
logger.warn(
Expand All @@ -1093,6 +1099,7 @@ export function parseNewBlockMessage(
execution_cost_runtime: execCost.runtime,
execution_cost_write_count: execCost.write_count,
execution_cost_write_length: execCost.write_length,
tx_total_size: txTotalSize,
tx_count: msg.transactions.length,
block_time: blockData.block_time,
signer_bitvec: signerBitvec,
Expand Down
3 changes: 3 additions & 0 deletions tests/api/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
tx_total_size: 1,
signer_bitvec: null,
signer_signatures: null,
};
Expand Down Expand Up @@ -1171,6 +1172,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
tx_total_size: 1,
signer_bitvec: null,
signer_signatures: null,
};
Expand Down Expand Up @@ -2390,6 +2392,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
tx_total_size: 1,
signer_bitvec: null,
signer_signatures: null,
};
Expand Down
1 change: 1 addition & 0 deletions tests/api/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('block tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
tx_total_size: 1,
signer_bitvec: null,
signer_signatures: null,
};
Expand Down
1 change: 1 addition & 0 deletions tests/api/cache-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('cache-control tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
tx_total_size: 1,
signer_bitvec: null,
signer_signatures: null,
};
Expand Down
Loading

0 comments on commit ac7c41b

Please sign in to comment.