Skip to content

Commit ab64ab7

Browse files
authored
fix: index on principal_stx_txs table for faster /v1/address/{addr}/transactions lookups (#2059)
* fix: index on `principal_stx_txs` table for faster `v1/address/{addr}/transactions` lookups * test: fix tx_index ordering in test
1 parent 40dbcc6 commit ab64ab7

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
2+
exports.up = pgm => {
3+
pgm.createIndex(
4+
'principal_stx_txs',
5+
[
6+
'principal',
7+
{ name: 'block_height', order: 'DESC' },
8+
{ name: 'microblock_sequence', order: 'DESC' },
9+
{ name: 'tx_index', order: 'DESC' }],
10+
{
11+
name: 'idx_principal_stx_txs_optimized',
12+
where: 'canonical = TRUE AND microblock_canonical = TRUE',
13+
}
14+
);
15+
};
16+
17+
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
18+
exports.down = pgm => {
19+
pgm.dropIndex('principal_stx_txs', ['principal', 'block_height', 'microblock_sequence', 'tx_index'], {
20+
name: 'idx_principal_stx_txs_optimized',
21+
});
22+
};

src/tests/address-tests.ts

+48-48
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ describe('address tests', () => {
12241224

12251225
const tx: DbTxRaw = {
12261226
tx_id: '0x1234',
1227-
tx_index: 4,
1227+
tx_index: 9,
12281228
anchor_mode: 3,
12291229
nonce: 0,
12301230
raw_tx: bufferToHex(Buffer.from('')),
@@ -1416,7 +1416,7 @@ describe('address tests', () => {
14161416
};
14171417
const contractCall: DbTx = {
14181418
tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000',
1419-
tx_index: 5,
1419+
tx_index: 10,
14201420
anchor_mode: 3,
14211421
nonce: 0,
14221422
index_block_hash: block.index_block_hash,
@@ -1775,18 +1775,19 @@ describe('address tests', () => {
17751775
total: 5,
17761776
results: [
17771777
{
1778-
tx_id: '0x12340005',
1778+
tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000',
17791779
tx_status: 'success',
17801780
tx_result: {
17811781
hex: '0x0100000000000000000000000000000001', // u1
17821782
repr: 'u1',
17831783
},
1784-
tx_type: 'token_transfer',
1785-
fee_rate: '1234',
1784+
tx_type: 'contract_call',
1785+
fee_rate: '10',
17861786
is_unanchored: false,
17871787
nonce: 0,
17881788
anchor_mode: 'any',
17891789
sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
1790+
sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
17901791
sponsored: false,
17911792
post_condition_mode: 'allow',
17921793
post_conditions: [],
@@ -1804,13 +1805,28 @@ describe('address tests', () => {
18041805
parent_block_hash: '0x',
18051806
parent_burn_block_time: 1626122935,
18061807
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
1807-
tx_index: 5,
1808-
token_transfer: {
1809-
recipient_address: 'ST3DWSXBPYDB484QXFTR81K4AWG4ZB5XZNFF3H70C',
1810-
amount: '15',
1811-
memo: '0x6869',
1808+
tx_index: 10,
1809+
contract_call: {
1810+
contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
1811+
function_name: 'test-contract-fn',
1812+
function_signature:
1813+
'(define-public (test-contract-fn (amount uint) (desc string-ascii)))',
1814+
function_args: [
1815+
{
1816+
hex: '0x010000000000000000000000000001e240',
1817+
name: 'amount',
1818+
repr: 'u123456',
1819+
type: 'uint',
1820+
},
1821+
{
1822+
hex: '0x0d0000000568656c6c6f',
1823+
name: 'desc',
1824+
repr: '"hello"',
1825+
type: 'string-ascii',
1826+
},
1827+
],
18121828
},
1813-
event_count: 0,
1829+
event_count: 5,
18141830
events: [],
18151831
execution_cost_read_count: 0,
18161832
execution_cost_read_length: 0,
@@ -1819,19 +1835,18 @@ describe('address tests', () => {
18191835
execution_cost_write_length: 0,
18201836
},
18211837
{
1822-
tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000',
1838+
tx_id: '0x1234',
18231839
tx_status: 'success',
18241840
tx_result: {
18251841
hex: '0x0100000000000000000000000000000001', // u1
18261842
repr: 'u1',
18271843
},
1828-
tx_type: 'contract_call',
1829-
fee_rate: '10',
1844+
tx_type: 'coinbase',
1845+
fee_rate: '1234',
18301846
is_unanchored: false,
18311847
nonce: 0,
18321848
anchor_mode: 'any',
1833-
sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
1834-
sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
1849+
sender_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
18351850
sponsored: false,
18361851
post_condition_mode: 'allow',
18371852
post_conditions: [],
@@ -1849,26 +1864,10 @@ describe('address tests', () => {
18491864
parent_block_hash: '0x',
18501865
parent_burn_block_time: 1626122935,
18511866
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
1852-
tx_index: 5,
1853-
contract_call: {
1854-
contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
1855-
function_name: 'test-contract-fn',
1856-
function_signature:
1857-
'(define-public (test-contract-fn (amount uint) (desc string-ascii)))',
1858-
function_args: [
1859-
{
1860-
hex: '0x010000000000000000000000000001e240',
1861-
name: 'amount',
1862-
repr: 'u123456',
1863-
type: 'uint',
1864-
},
1865-
{
1866-
hex: '0x0d0000000568656c6c6f',
1867-
name: 'desc',
1868-
repr: '"hello"',
1869-
type: 'string-ascii',
1870-
},
1871-
],
1867+
tx_index: 9,
1868+
coinbase_payload: {
1869+
data: '0x636f696e62617365206869',
1870+
alt_recipient: null,
18721871
},
18731872
event_count: 5,
18741873
events: [],
@@ -1879,18 +1878,18 @@ describe('address tests', () => {
18791878
execution_cost_write_length: 0,
18801879
},
18811880
{
1882-
tx_id: '0x1234',
1881+
tx_id: '0x12340005',
18831882
tx_status: 'success',
18841883
tx_result: {
18851884
hex: '0x0100000000000000000000000000000001', // u1
18861885
repr: 'u1',
18871886
},
1888-
tx_type: 'coinbase',
1887+
tx_type: 'token_transfer',
18891888
fee_rate: '1234',
18901889
is_unanchored: false,
18911890
nonce: 0,
18921891
anchor_mode: 'any',
1893-
sender_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
1892+
sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
18941893
sponsored: false,
18951894
post_condition_mode: 'allow',
18961895
post_conditions: [],
@@ -1908,12 +1907,13 @@ describe('address tests', () => {
19081907
parent_block_hash: '0x',
19091908
parent_burn_block_time: 1626122935,
19101909
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
1911-
tx_index: 4,
1912-
coinbase_payload: {
1913-
data: '0x636f696e62617365206869',
1914-
alt_recipient: null,
1910+
tx_index: 5,
1911+
token_transfer: {
1912+
recipient_address: 'ST3DWSXBPYDB484QXFTR81K4AWG4ZB5XZNFF3H70C',
1913+
amount: '15',
1914+
memo: '0x6869',
19151915
},
1916-
event_count: 5,
1916+
event_count: 0,
19171917
events: [],
19181918
execution_cost_read_count: 0,
19191919
execution_cost_read_length: 0,
@@ -2054,7 +2054,7 @@ describe('address tests', () => {
20542054
parent_block_hash: '0x',
20552055
parent_burn_block_time: 1626122935,
20562056
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
2057-
tx_index: 5,
2057+
tx_index: 10,
20582058
contract_call: {
20592059
contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
20602060
function_name: 'test-contract-fn',
@@ -2161,7 +2161,7 @@ describe('address tests', () => {
21612161
sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
21622162
sponsored: false,
21632163
tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000',
2164-
tx_index: 5,
2164+
tx_index: 10,
21652165
tx_result: {
21662166
hex: '0x0100000000000000000000000000000001',
21672167
repr: 'u1',
@@ -2241,7 +2241,7 @@ describe('address tests', () => {
22412241
sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1',
22422242
sponsored: false,
22432243
tx_id: '0x1232000000000000000000000000000000000000000000000000000000000000',
2244-
tx_index: 5,
2244+
tx_index: 10,
22452245
tx_result: {
22462246
hex: '0x0100000000000000000000000000000001',
22472247
repr: 'u1',
@@ -2283,7 +2283,7 @@ describe('address tests', () => {
22832283
parent_block_hash: '0x',
22842284
parent_burn_block_time: 1626122935,
22852285
parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z',
2286-
tx_index: 5,
2286+
tx_index: 10,
22872287
contract_call: {
22882288
contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world',
22892289
function_name: 'test-contract-fn',

0 commit comments

Comments
 (0)