Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use total_count CTE and return it with the parsed results #2073

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions src/datastore/pg-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class PgStoreV2 extends BasePgStoreModule {
ORDER BY block_height DESC
)
SELECT offset_block_height as block_height
FROM ordered_blocks
FROM ordered_blocks
WHERE index_block_hash = ${cursor ?? sql`(SELECT index_block_hash FROM chain_tip LIMIT 1)`}
LIMIT 1
),
Expand Down Expand Up @@ -518,6 +518,12 @@ export class PgStoreV2 extends BasePgStoreModule {
FROM nft_events
WHERE sender = ${args.address} OR recipient = ${args.address}
)
),
count AS (
SELECT COUNT(*)::int AS total_count
FROM address_txs
INNER JOIN txs USING (tx_id, index_block_hash, microblock_hash)
WHERE canonical = TRUE AND microblock_canonical = TRUE
Comment on lines +525 to +526
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this join, add the WHERE canonical = TRUE AND microblock_canonical = TRUE filters to the union tables above

)
SELECT
${sql(TX_COLUMNS)},
Expand Down Expand Up @@ -572,7 +578,7 @@ export class PgStoreV2 extends BasePgStoreModule {
SELECT COUNT(*)::int FROM nft_events
WHERE ${eventAcctCond} AND asset_event_type_id = ${DbAssetEventTypeId.Burn}
) AS nft_burn,
(COUNT(*) OVER())::int AS count
(SELECT total_count FROM count) AS count
FROM address_txs
INNER JOIN txs USING (tx_id, index_block_hash, microblock_hash)
WHERE canonical = TRUE AND microblock_canonical = TRUE
Expand Down Expand Up @@ -712,33 +718,33 @@ export class PgStoreV2 extends BasePgStoreModule {
AND end_cycle_id = ${cycleNumber + 1}
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
), delegated_stackers AS (
SELECT DISTINCT ON (main.stacker)
main.stacker,
SELECT DISTINCT ON (main.stacker)
main.stacker,
sk.signer_key
FROM pox4_events main
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
WHERE main.canonical = true
WHERE main.canonical = true
AND main.microblock_canonical = true
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
AND main.start_cycle_id <= ${cycleNumber}
AND main.start_cycle_id <= ${cycleNumber}
AND main.end_cycle_id > ${cycleNumber}
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
), solo_stackers AS (
SELECT DISTINCT ON (stacker)
stacker,
SELECT DISTINCT ON (stacker)
stacker,
signer_key
FROM pox4_events
WHERE canonical = true AND microblock_canonical = true
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
AND start_cycle_id <= ${cycleNumber}
AND start_cycle_id <= ${cycleNumber}
AND end_cycle_id > ${cycleNumber}
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
)
SELECT
SELECT
ps.signing_key,
ps.weight,
ps.stacked_amount,
ps.weight_percent,
ps.weight,
ps.stacked_amount,
ps.weight_percent,
ps.stacked_amount_percent,
COUNT(DISTINCT ds.stacker)::int AS pooled_stacker_count,
COUNT(DISTINCT ss.stacker)::int AS solo_stacker_count,
Expand Down Expand Up @@ -782,20 +788,20 @@ export class PgStoreV2 extends BasePgStoreModule {
AND end_cycle_id = ${cycleNumber + 1}
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
), delegated_stackers AS (
SELECT DISTINCT ON (main.stacker)
main.stacker,
SELECT DISTINCT ON (main.stacker)
main.stacker,
sk.signer_key
FROM pox4_events main
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
WHERE main.canonical = true
WHERE main.canonical = true
AND main.microblock_canonical = true
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
AND main.start_cycle_id <= ${cycleNumber}
AND main.start_cycle_id <= ${cycleNumber}
AND main.end_cycle_id > ${cycleNumber}
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
), solo_stackers AS (
SELECT DISTINCT ON (stacker)
stacker,
SELECT DISTINCT ON (stacker)
stacker,
signer_key
FROM pox4_events
WHERE canonical = true AND microblock_canonical = true
Expand All @@ -804,19 +810,19 @@ export class PgStoreV2 extends BasePgStoreModule {
AND end_cycle_id > ${cycleNumber}
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
)
SELECT
SELECT
ps.signing_key,
ps.weight,
ps.stacked_amount,
ps.weight_percent,
ps.weight,
ps.stacked_amount,
ps.weight_percent,
ps.stacked_amount_percent,
COUNT(DISTINCT ds.stacker)::int AS pooled_stacker_count,
COUNT(DISTINCT ss.stacker)::int AS solo_stacker_count
FROM pox_sets ps
LEFT JOIN delegated_stackers ds ON ps.signing_key = ds.signer_key
LEFT JOIN solo_stackers ss ON ps.signing_key = ss.signer_key
WHERE ps.canonical = TRUE
AND ps.cycle_number = ${cycleNumber}
WHERE ps.canonical = TRUE
AND ps.cycle_number = ${cycleNumber}
AND ps.signing_key = ${signerKey}
GROUP BY ps.signing_key, ps.weight, ps.stacked_amount, ps.weight_percent, ps.stacked_amount_percent
LIMIT 1
Expand Down Expand Up @@ -862,8 +868,8 @@ export class PgStoreV2 extends BasePgStoreModule {
AND end_cycle_id = ${cycleNumber + 1}
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
), delegated_stackers AS (
SELECT DISTINCT ON (main.stacker)
main.stacker,
SELECT DISTINCT ON (main.stacker)
main.stacker,
sk.signer_key,
main.locked,
main.pox_addr,
Expand All @@ -872,15 +878,15 @@ export class PgStoreV2 extends BasePgStoreModule {
'pooled' as stacker_type
FROM pox4_events main
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
WHERE main.canonical = true
WHERE main.canonical = true
AND main.microblock_canonical = true
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
AND main.start_cycle_id <= ${cycleNumber}
AND main.end_cycle_id > ${cycleNumber}
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
), solo_stackers AS (
SELECT DISTINCT ON (stacker)
stacker,
SELECT DISTINCT ON (stacker)
stacker,
signer_key,
locked,
pox_addr,
Expand Down Expand Up @@ -909,8 +915,8 @@ export class PgStoreV2 extends BasePgStoreModule {
COUNT(*) OVER()::int AS total
FROM pox_sets ps
LEFT JOIN combined_stackers cs ON ps.signing_key = cs.signer_key
WHERE ps.canonical = TRUE
AND ps.cycle_number = ${cycleNumber}
WHERE ps.canonical = TRUE
AND ps.cycle_number = ${cycleNumber}
AND ps.signing_key = ${signerKey}
ORDER BY locked DESC
LIMIT ${limit}
Expand Down
9 changes: 9 additions & 0 deletions src/tests/address-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ describe('address tests', () => {
burn: 0,
});

// fetch with offset
const v2Fetch1offset = await supertest(api.server).get(
`/extended/v2/addresses/${testAddr2}/transactions?offset=1`
);
expect(v2Fetch1offset.status).toBe(200);
expect(v2Fetch1offset.type).toBe('application/json');
const v2Fetch1offsetJson = JSON.parse(v2Fetch1offset.text);
expect(v2Fetch1offsetJson.total).toBe(7);

const v2Fetch2 = await supertest(api.server).get(
`/extended/v2/addresses/${testAddr2}/transactions/${v2Fetch1Json.results[0].tx.tx_id}/events?limit=3`
);
Expand Down
Loading