Skip to content

Commit

Permalink
fix: index on principal_stx_txs table for faster `/v1/address/{addr…
Browse files Browse the repository at this point in the history
…}/transactions` lookups (#2059)

* fix: index on `principal_stx_txs` table for faster `v1/address/{addr}/transactions` lookups

* test: fix tx_index ordering in test
  • Loading branch information
zone117x authored Aug 21, 2024
1 parent 40dbcc6 commit ab64ab7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 48 deletions.
22 changes: 22 additions & 0 deletions migrations/1724229744369_principal_stx_txs_idx.js
Original file line number Diff line number Diff line change
@@ -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',
});
};
96 changes: 48 additions & 48 deletions src/tests/address-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('')),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: [],
Expand All @@ -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,
Expand All @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit ab64ab7

Please sign in to comment.