Skip to content

Commit dbfef5c

Browse files
committed
fix: update getRecentBlocks to use pox info from chain_tip
1 parent 0d490f0 commit dbfef5c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/pg/pg-store.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ export class PgStore extends BasePgStore {
6363
// they differ from the existing values. Return true if the row was updated, false otherwise.
6464
// Should only update the row if the values are null (i.e. the first time the values are set).
6565
const updateResult = await this.sql`
66-
UPDATE chain_tip
67-
SET
66+
UPDATE chain_tip
67+
SET
6868
first_burnchain_block_height = ${poxInfo.first_burnchain_block_height},
6969
reward_cycle_length = ${poxInfo.reward_cycle_length}
7070
WHERE
71-
first_burnchain_block_height IS DISTINCT FROM ${poxInfo.first_burnchain_block_height}
71+
first_burnchain_block_height IS DISTINCT FROM ${poxInfo.first_burnchain_block_height}
7272
OR reward_cycle_length IS DISTINCT FROM ${poxInfo.reward_cycle_length}
7373
`;
7474
return { rowUpdated: updateResult.count > 0 };
@@ -149,8 +149,13 @@ export class PgStore extends BasePgStore {
149149
}[]
150150
>`
151151
WITH latest_blocks AS (
152-
SELECT * FROM blocks
153-
ORDER BY block_height DESC
152+
SELECT
153+
b.*,
154+
ct.block_height AS chain_tip_block_height,
155+
(b.burn_block_height - ct.first_burnchain_block_height) / ct.reward_cycle_length AS cycle_number
156+
FROM blocks b
157+
CROSS JOIN chain_tip ct
158+
ORDER BY b.block_height DESC
154159
LIMIT ${limit}
155160
OFFSET ${offset}
156161
),
@@ -172,7 +177,7 @@ export class PgStore extends BasePgStore {
172177
lb.block_hash,
173178
lb.index_block_hash,
174179
lb.burn_block_height,
175-
bp.cycle_number,
180+
lb.cycle_number,
176181
bp.received_at AS block_proposal_time_ms,
177182
rs.signer_key,
178183
COALESCE(rs.signer_weight, 0) AS signer_weight,
@@ -186,7 +191,7 @@ export class PgStore extends BasePgStore {
186191
EXTRACT(MILLISECOND FROM (fbr.received_at - bp.received_at)) AS response_time_ms
187192
FROM latest_blocks lb
188193
LEFT JOIN filtered_block_proposals bp ON lb.block_hash = bp.block_hash
189-
LEFT JOIN reward_set_signers rs ON bp.cycle_number = rs.cycle_number
194+
LEFT JOIN reward_set_signers rs ON lb.cycle_number = rs.cycle_number
190195
LEFT JOIN block_signer_signatures bss ON lb.block_height = bss.block_height AND rs.signer_key = bss.signer_key
191196
LEFT JOIN filtered_block_responses fbr ON fbr.signer_key = rs.signer_key AND fbr.signer_sighash = lb.block_hash
192197
),
@@ -219,7 +224,8 @@ export class PgStore extends BasePgStore {
219224
lb.burn_block_height,
220225
lb.tenure_height,
221226
EXTRACT(EPOCH FROM lb.block_time)::integer AS block_time,
222-
bsa.cycle_number,
227+
lb.cycle_number,
228+
lb.chain_tip_block_height,
223229
(EXTRACT(EPOCH FROM bsa.block_proposal_time_ms) * 1000)::bigint AS block_proposal_time_ms,
224230
bsa.total_signer_count::integer,
225231
bsa.signer_accepted_mined_count::integer,
@@ -234,11 +240,9 @@ export class PgStore extends BasePgStore {
234240
bsa.accepted_mined_weight::integer,
235241
bsa.accepted_excluded_weight::integer,
236242
bsa.rejected_weight::integer,
237-
bsa.missing_weight::integer,
238-
ct.block_height AS chain_tip_block_height
243+
bsa.missing_weight::integer
239244
FROM latest_blocks lb
240245
JOIN signer_state_aggregation bsa ON lb.id = bsa.block_id
241-
CROSS JOIN chain_tip ct
242246
ORDER BY lb.block_height DESC
243247
`;
244248
return result;

0 commit comments

Comments
 (0)