Skip to content

Commit

Permalink
fix: update getRecentBlocks to use pox info from chain_tip
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 28, 2024
1 parent 0d490f0 commit dbfef5c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export class PgStore extends BasePgStore {
// they differ from the existing values. Return true if the row was updated, false otherwise.
// Should only update the row if the values are null (i.e. the first time the values are set).
const updateResult = await this.sql`
UPDATE chain_tip
SET
UPDATE chain_tip
SET
first_burnchain_block_height = ${poxInfo.first_burnchain_block_height},
reward_cycle_length = ${poxInfo.reward_cycle_length}
WHERE
first_burnchain_block_height IS DISTINCT FROM ${poxInfo.first_burnchain_block_height}
first_burnchain_block_height IS DISTINCT FROM ${poxInfo.first_burnchain_block_height}
OR reward_cycle_length IS DISTINCT FROM ${poxInfo.reward_cycle_length}
`;
return { rowUpdated: updateResult.count > 0 };
Expand Down Expand Up @@ -149,8 +149,13 @@ export class PgStore extends BasePgStore {
}[]
>`
WITH latest_blocks AS (
SELECT * FROM blocks
ORDER BY block_height DESC
SELECT
b.*,
ct.block_height AS chain_tip_block_height,
(b.burn_block_height - ct.first_burnchain_block_height) / ct.reward_cycle_length AS cycle_number
FROM blocks b
CROSS JOIN chain_tip ct
ORDER BY b.block_height DESC
LIMIT ${limit}
OFFSET ${offset}
),
Expand All @@ -172,7 +177,7 @@ export class PgStore extends BasePgStore {
lb.block_hash,
lb.index_block_hash,
lb.burn_block_height,
bp.cycle_number,
lb.cycle_number,
bp.received_at AS block_proposal_time_ms,
rs.signer_key,
COALESCE(rs.signer_weight, 0) AS signer_weight,
Expand All @@ -186,7 +191,7 @@ export class PgStore extends BasePgStore {
EXTRACT(MILLISECOND FROM (fbr.received_at - bp.received_at)) AS response_time_ms
FROM latest_blocks lb
LEFT JOIN filtered_block_proposals bp ON lb.block_hash = bp.block_hash
LEFT JOIN reward_set_signers rs ON bp.cycle_number = rs.cycle_number
LEFT JOIN reward_set_signers rs ON lb.cycle_number = rs.cycle_number
LEFT JOIN block_signer_signatures bss ON lb.block_height = bss.block_height AND rs.signer_key = bss.signer_key
LEFT JOIN filtered_block_responses fbr ON fbr.signer_key = rs.signer_key AND fbr.signer_sighash = lb.block_hash
),
Expand Down Expand Up @@ -219,7 +224,8 @@ export class PgStore extends BasePgStore {
lb.burn_block_height,
lb.tenure_height,
EXTRACT(EPOCH FROM lb.block_time)::integer AS block_time,
bsa.cycle_number,
lb.cycle_number,
lb.chain_tip_block_height,
(EXTRACT(EPOCH FROM bsa.block_proposal_time_ms) * 1000)::bigint AS block_proposal_time_ms,
bsa.total_signer_count::integer,
bsa.signer_accepted_mined_count::integer,
Expand All @@ -234,11 +240,9 @@ export class PgStore extends BasePgStore {
bsa.accepted_mined_weight::integer,
bsa.accepted_excluded_weight::integer,
bsa.rejected_weight::integer,
bsa.missing_weight::integer,
ct.block_height AS chain_tip_block_height
bsa.missing_weight::integer
FROM latest_blocks lb
JOIN signer_state_aggregation bsa ON lb.id = bsa.block_id
CROSS JOIN chain_tip ct
ORDER BY lb.block_height DESC
`;
return result;
Expand Down

0 comments on commit dbfef5c

Please sign in to comment.