Skip to content

Commit e69f9e4

Browse files
authored
fix: optimize query for transactions in block (#2259)
* fix: optimize query for transactions in block * test: filters
1 parent e1044d1 commit e69f9e4

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/datastore/pg-store-v2.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -335,38 +335,37 @@ export class PgStoreV2 extends BasePgStoreModule {
335335
return await this.sqlTransaction(async sql => {
336336
const limit = args.limit ?? TransactionLimitParamSchema.default;
337337
const offset = args.offset ?? 0;
338-
const filter =
339-
args.block.type === 'latest'
340-
? sql`index_block_hash = (SELECT index_block_hash FROM blocks WHERE canonical = TRUE ORDER BY block_height DESC LIMIT 1)`
341-
: args.block.type === 'hash'
342-
? sql`(
343-
block_hash = ${normalizeHashString(args.block.hash)}
344-
OR index_block_hash = ${normalizeHashString(args.block.hash)}
345-
)`
346-
: sql`block_height = ${args.block.height}`;
347-
const blockCheck = await sql`SELECT index_block_hash FROM blocks WHERE ${filter} LIMIT 1`;
348-
if (blockCheck.count === 0)
349-
throw new InvalidRequestError(`Block not found`, InvalidRequestErrorType.invalid_param);
350338
const txsQuery = await sql<(TxQueryResult & { total: number })[]>`
351-
WITH tx_count AS (
352-
SELECT tx_count AS total FROM blocks WHERE canonical = TRUE AND ${filter}
339+
WITH block_ptr AS (
340+
SELECT index_block_hash FROM blocks
341+
WHERE ${
342+
args.block.type === 'latest'
343+
? sql`canonical = TRUE ORDER BY block_height DESC`
344+
: args.block.type === 'hash'
345+
? sql`(
346+
block_hash = ${normalizeHashString(args.block.hash)}
347+
OR index_block_hash = ${normalizeHashString(args.block.hash)}
348+
) AND canonical = TRUE`
349+
: sql`block_height = ${args.block.height} AND canonical = TRUE`
350+
}
351+
LIMIT 1
352+
),
353+
tx_count AS (
354+
SELECT tx_count AS total
355+
FROM blocks
356+
WHERE index_block_hash = (SELECT index_block_hash FROM block_ptr)
353357
)
354358
SELECT ${sql(TX_COLUMNS)}, (SELECT total FROM tx_count)::int AS total
355359
FROM txs
356360
WHERE canonical = true
357361
AND microblock_canonical = true
358-
AND ${filter}
362+
AND index_block_hash = (SELECT index_block_hash FROM block_ptr)
359363
ORDER BY microblock_sequence ASC, tx_index ASC
360364
LIMIT ${limit}
361365
OFFSET ${offset}
362366
`;
363367
if (txsQuery.count === 0)
364-
return {
365-
limit,
366-
offset,
367-
results: [],
368-
total: 0,
369-
};
368+
throw new InvalidRequestError(`Block not found`, InvalidRequestErrorType.invalid_param);
370369
return {
371370
limit,
372371
offset,

tests/api/tx.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -4198,7 +4198,7 @@ describe('tx tests', () => {
41984198
);
41994199
expect(result.status).toBe(200);
42004200
expect(result.type).toBe('application/json');
4201-
const json = JSON.parse(result.text);
4201+
let json = JSON.parse(result.text);
42024202
expect(json.total).toBe(2);
42034203
expect(json.results[0]).toStrictEqual({
42044204
anchor_mode: 'any',
@@ -4244,6 +4244,18 @@ describe('tx tests', () => {
42444244
tx_type: 'coinbase',
42454245
});
42464246

4247+
result = await supertest(api.server).get(`/extended/v2/blocks/latest/transactions`);
4248+
expect(result.status).toBe(200);
4249+
expect(result.type).toBe('application/json');
4250+
json = JSON.parse(result.text);
4251+
expect(json.total).toBe(2);
4252+
4253+
result = await supertest(api.server).get(`/extended/v2/blocks/1/transactions`);
4254+
expect(result.status).toBe(200);
4255+
expect(result.type).toBe('application/json');
4256+
json = JSON.parse(result.text);
4257+
expect(json.total).toBe(2);
4258+
42474259
// Try a non-existent block
42484260
result = await supertest(api.server).get(
42494261
`/extended/v2/blocks/0x00000000000000000001e2ee7f0c6bd5361b5e7afd76156ca7d6f524ee999999/transactions?limit=20&offset=0`

0 commit comments

Comments
 (0)