Skip to content

Commit d0e79ba

Browse files
committed
chore: remove block_proposal and block_response dupe filter CTEs getSignerForCycle query
1 parent eac1fab commit d0e79ba

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

src/pg/pg-store.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export class PgStore extends BasePgStore {
346346
}[]
347347
>`
348348
WITH signer_data AS (
349-
-- Fetch the signer for the given cycle
349+
-- Fetch the specific signer for the given cycle
350350
SELECT
351351
rss.signer_key,
352352
rss.signer_weight,
@@ -356,39 +356,28 @@ export class PgStore extends BasePgStore {
356356
AND rss.signer_key = ${normalizeHexString(signerId)}
357357
),
358358
proposal_data AS (
359-
-- Fetch the first (oldest) proposal for each block_hash for the given cycle
359+
-- Select all proposals for the given cycle
360360
SELECT
361361
bp.block_hash,
362362
bp.block_height,
363363
bp.received_at AS proposal_received_at
364364
FROM block_proposals bp
365365
WHERE bp.reward_cycle = ${cycleNumber}
366-
AND bp.id = (
367-
-- Select the earliest proposal for each block_hash
368-
SELECT MIN(sub_bp.id)
369-
FROM block_proposals sub_bp
370-
WHERE sub_bp.block_hash = bp.block_hash
371-
)
372366
),
373367
response_data AS (
374-
-- Fetch the first (oldest) response for each (signer_key, signer_sighash) pair
375-
SELECT DISTINCT ON (br.signer_key, br.signer_sighash)
368+
-- Select all responses for the proposals in the given cycle
369+
SELECT
376370
br.signer_key,
377371
br.signer_sighash,
378372
br.accepted,
379373
br.received_at,
380374
br.id
381375
FROM block_responses br
382-
WHERE br.id = (
383-
-- Select the earliest response for each signer_sighash and signer_key
384-
SELECT MIN(sub_br.id)
385-
FROM block_responses sub_br
386-
WHERE sub_br.signer_key = br.signer_key
387-
AND sub_br.signer_sighash = br.signer_sighash
388-
)
376+
JOIN proposal_data pd ON br.signer_sighash = pd.block_hash
377+
WHERE br.signer_key = ${normalizeHexString(signerId)} -- Filter for the specific signer
389378
),
390379
signer_proposal_data AS (
391-
-- Cross join signers with proposals and left join filtered responses
380+
-- Cross join the specific signer with proposals and left join filtered responses
392381
SELECT
393382
sd.signer_key,
394383
pd.block_hash,
@@ -397,13 +386,13 @@ export class PgStore extends BasePgStore {
397386
rd.received_at AS response_received_at,
398387
EXTRACT(MILLISECOND FROM (rd.received_at - pd.proposal_received_at)) AS response_time_ms
399388
FROM signer_data sd
400-
CROSS JOIN proposal_data pd -- Cross join to associate all signers with all proposals
389+
CROSS JOIN proposal_data pd
401390
LEFT JOIN response_data rd
402391
ON pd.block_hash = rd.signer_sighash
403392
AND sd.signer_key = rd.signer_key -- Match signers with their corresponding responses
404393
),
405394
aggregated_data AS (
406-
-- Aggregate the proposal and response data by signer
395+
-- Aggregate the proposal and response data for the specific signer
407396
SELECT
408397
spd.signer_key,
409398
COUNT(CASE WHEN spd.accepted = true THEN 1 END)::integer AS proposals_accepted_count,

0 commit comments

Comments
 (0)