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: update getRecentBlocks to use pox info from chain_tip #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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 @@ -144,8 +144,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 @@ -157,7 +162,7 @@ export class PgStore extends BasePgStore {
lb.block_hash,
lb.index_block_hash,
lb.burn_block_height,
bp.reward_cycle as 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 @@ -171,7 +176,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 block_proposals bp ON lb.block_hash = bp.block_hash
LEFT JOIN reward_set_signers rs ON bp.reward_cycle = 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 block_responses fbr ON fbr.signer_key = rs.signer_key AND fbr.signer_sighash = lb.block_hash
),
Expand Down Expand Up @@ -204,7 +209,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 @@ -219,11 +225,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
Loading