Skip to content

Commit 7691109

Browse files
authored
fix: optimize getMicroblocks query (#1179)
* refactor: optimized query for getMicroblocks * test: updated tx with microblock * refactor: changed the query * refactor: removed unnecessary query check * fix: resolved lint error
1 parent 943e2d1 commit 7691109

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/datastore/pg-store.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -563,22 +563,13 @@ export class PgStore {
563563
const microblockQuery = await sql<
564564
(MicroblockQueryResult & { tx_id?: string | null; tx_index?: number | null })[]
565565
>`
566-
SELECT microblocks.*, tx_id FROM (
567-
SELECT ${sql(MICROBLOCK_COLUMNS)}
568-
FROM microblocks
569-
WHERE canonical = true AND microblock_canonical = true
570-
ORDER BY block_height DESC, microblock_sequence DESC
571-
LIMIT ${args.limit}
572-
OFFSET ${args.offset}
573-
) microblocks
574-
LEFT JOIN (
575-
SELECT tx_id, tx_index, microblock_hash
576-
FROM txs
577-
WHERE canonical = true AND microblock_canonical = true
578-
ORDER BY tx_index DESC
579-
) txs
580-
ON microblocks.microblock_hash = txs.microblock_hash
581-
ORDER BY microblocks.block_height DESC, microblocks.microblock_sequence DESC, txs.tx_index DESC
566+
SELECT microblocks.*, txs.tx_id
567+
FROM microblocks LEFT JOIN txs USING(microblock_hash)
568+
WHERE microblocks.canonical = true AND microblocks.microblock_canonical = true AND
569+
txs.canonical = true AND txs.microblock_canonical = true
570+
ORDER BY microblocks.block_height DESC, microblocks.microblock_sequence DESC, txs.tx_index DESC
571+
LIMIT ${args.limit}
572+
OFFSET ${args.offset};
582573
`;
583574
const microblocks: { microblock: DbMicroblock; txs: string[] }[] = [];
584575
microblockQuery.forEach(row => {

src/tests/api-tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10847,6 +10847,33 @@ describe('api tests', () => {
1084710847
expect(result.body.txs).toHaveLength(1);
1084810848
expect(result.body.txs[0]).toEqual(tx_id);
1084910849
});
10850+
test('/microblock', async () => {
10851+
const microblock_hash = '0x0fff';
10852+
const block = new TestBlockBuilder({ block_hash: '0x1234', block_height: 1 }).build();
10853+
await db.update(block);
10854+
10855+
const microblock = new TestMicroblockStreamBuilder()
10856+
.addMicroblock({ microblock_hash, parent_index_block_hash: block.block.index_block_hash })
10857+
.addTx({
10858+
tx_id: '0xffff',
10859+
})
10860+
.addTx({
10861+
tx_id: '0x1234',
10862+
canonical: false,
10863+
microblock_canonical: false,
10864+
})
10865+
.build();
10866+
await db.updateMicroblocks(microblock);
10867+
const microblockResult = await supertest(api.server).get(`/extended/v1/microblock/`);
10868+
const response = microblockResult.body;
10869+
const expectedTxs = ['0xffff'];
10870+
10871+
expect(response.total).toEqual(1);
10872+
expect(response.results).toHaveLength(1);
10873+
expect(response.results[0].microblock_hash).toEqual(microblock_hash);
10874+
expect(response.results[0].txs).toHaveLength(1);
10875+
expect(response.results[0].txs).toEqual(expectedTxs);
10876+
});
1085010877

1085110878
test('/block', async () => {
1085210879
const block_hash = '0x1234',

0 commit comments

Comments
 (0)