From 6fbf53e06d7b5f2240199ad8476c60680f4c6670 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Wed, 21 Aug 2024 10:47:30 +0200 Subject: [PATCH 1/2] fix: index on `principal_stx_txs` table for faster `v1/address/{addr}/transactions` lookups --- .../1724229744369_principal_stx_txs_idx.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 migrations/1724229744369_principal_stx_txs_idx.js diff --git a/migrations/1724229744369_principal_stx_txs_idx.js b/migrations/1724229744369_principal_stx_txs_idx.js new file mode 100644 index 000000000..fe9135c35 --- /dev/null +++ b/migrations/1724229744369_principal_stx_txs_idx.js @@ -0,0 +1,22 @@ +/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ +exports.up = pgm => { + pgm.createIndex( + 'principal_stx_txs', + [ + 'principal', + { name: 'block_height', order: 'DESC' }, + { name: 'microblock_sequence', order: 'DESC' }, + { name: 'tx_index', order: 'DESC' }], + { + name: 'idx_principal_stx_txs_optimized', + where: 'canonical = TRUE AND microblock_canonical = TRUE', + } + ); +}; + +/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ +exports.down = pgm => { + pgm.dropIndex('principal_stx_txs', ['principal', 'block_height', 'microblock_sequence', 'tx_index'], { + name: 'idx_principal_stx_txs_optimized', + }); +}; From 1510c8931e6353ef43fd36a0f29647a94073aeb7 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Wed, 21 Aug 2024 12:15:17 +0200 Subject: [PATCH 2/2] test: fix tx_index ordering in test --- src/tests/address-tests.ts | 96 +++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/tests/address-tests.ts b/src/tests/address-tests.ts index b9d2a6a04..e7641dba3 100644 --- a/src/tests/address-tests.ts +++ b/src/tests/address-tests.ts @@ -1224,7 +1224,7 @@ describe('address tests', () => { const tx: DbTxRaw = { tx_id: '0x1234', - tx_index: 4, + tx_index: 9, anchor_mode: 3, nonce: 0, raw_tx: bufferToHex(Buffer.from('')), @@ -1416,7 +1416,7 @@ describe('address tests', () => { }; const contractCall: DbTx = { tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000', - tx_index: 5, + tx_index: 10, anchor_mode: 3, nonce: 0, index_block_hash: block.index_block_hash, @@ -1775,18 +1775,19 @@ describe('address tests', () => { total: 5, results: [ { - tx_id: '0x12340005', + tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000', tx_status: 'success', tx_result: { hex: '0x0100000000000000000000000000000001', // u1 repr: 'u1', }, - tx_type: 'token_transfer', - fee_rate: '1234', + tx_type: 'contract_call', + fee_rate: '10', is_unanchored: false, nonce: 0, anchor_mode: 'any', sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', + sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', sponsored: false, post_condition_mode: 'allow', post_conditions: [], @@ -1804,13 +1805,28 @@ describe('address tests', () => { parent_block_hash: '0x', parent_burn_block_time: 1626122935, parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', - tx_index: 5, - token_transfer: { - recipient_address: 'ST3DWSXBPYDB484QXFTR81K4AWG4ZB5XZNFF3H70C', - amount: '15', - memo: '0x6869', + tx_index: 10, + contract_call: { + contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', + function_name: 'test-contract-fn', + function_signature: + '(define-public (test-contract-fn (amount uint) (desc string-ascii)))', + function_args: [ + { + hex: '0x010000000000000000000000000001e240', + name: 'amount', + repr: 'u123456', + type: 'uint', + }, + { + hex: '0x0d0000000568656c6c6f', + name: 'desc', + repr: '"hello"', + type: 'string-ascii', + }, + ], }, - event_count: 0, + event_count: 5, events: [], execution_cost_read_count: 0, execution_cost_read_length: 0, @@ -1819,19 +1835,18 @@ describe('address tests', () => { execution_cost_write_length: 0, }, { - tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000', + tx_id: '0x1234', tx_status: 'success', tx_result: { hex: '0x0100000000000000000000000000000001', // u1 repr: 'u1', }, - tx_type: 'contract_call', - fee_rate: '10', + tx_type: 'coinbase', + fee_rate: '1234', is_unanchored: false, nonce: 0, anchor_mode: 'any', - sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', - sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', + sender_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', sponsored: false, post_condition_mode: 'allow', post_conditions: [], @@ -1849,26 +1864,10 @@ describe('address tests', () => { parent_block_hash: '0x', parent_burn_block_time: 1626122935, parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', - tx_index: 5, - contract_call: { - contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', - function_name: 'test-contract-fn', - function_signature: - '(define-public (test-contract-fn (amount uint) (desc string-ascii)))', - function_args: [ - { - hex: '0x010000000000000000000000000001e240', - name: 'amount', - repr: 'u123456', - type: 'uint', - }, - { - hex: '0x0d0000000568656c6c6f', - name: 'desc', - repr: '"hello"', - type: 'string-ascii', - }, - ], + tx_index: 9, + coinbase_payload: { + data: '0x636f696e62617365206869', + alt_recipient: null, }, event_count: 5, events: [], @@ -1879,18 +1878,18 @@ describe('address tests', () => { execution_cost_write_length: 0, }, { - tx_id: '0x1234', + tx_id: '0x12340005', tx_status: 'success', tx_result: { hex: '0x0100000000000000000000000000000001', // u1 repr: 'u1', }, - tx_type: 'coinbase', + tx_type: 'token_transfer', fee_rate: '1234', is_unanchored: false, nonce: 0, anchor_mode: 'any', - sender_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', + sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', sponsored: false, post_condition_mode: 'allow', post_conditions: [], @@ -1908,12 +1907,13 @@ describe('address tests', () => { parent_block_hash: '0x', parent_burn_block_time: 1626122935, parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', - tx_index: 4, - coinbase_payload: { - data: '0x636f696e62617365206869', - alt_recipient: null, + tx_index: 5, + token_transfer: { + recipient_address: 'ST3DWSXBPYDB484QXFTR81K4AWG4ZB5XZNFF3H70C', + amount: '15', + memo: '0x6869', }, - event_count: 5, + event_count: 0, events: [], execution_cost_read_count: 0, execution_cost_read_length: 0, @@ -2054,7 +2054,7 @@ describe('address tests', () => { parent_block_hash: '0x', parent_burn_block_time: 1626122935, parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', - tx_index: 5, + tx_index: 10, contract_call: { contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', function_name: 'test-contract-fn', @@ -2161,7 +2161,7 @@ describe('address tests', () => { sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', sponsored: false, tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000', - tx_index: 5, + tx_index: 10, tx_result: { hex: '0x0100000000000000000000000000000001', repr: 'u1', @@ -2241,7 +2241,7 @@ describe('address tests', () => { sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', sponsored: false, tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000', - tx_index: 5, + tx_index: 10, tx_result: { hex: '0x0100000000000000000000000000000001', repr: 'u1', @@ -2283,7 +2283,7 @@ describe('address tests', () => { parent_block_hash: '0x', parent_burn_block_time: 1626122935, parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', - tx_index: 5, + tx_index: 10, contract_call: { contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', function_name: 'test-contract-fn',