Skip to content

Commit e0deec1

Browse files
committed
fix: include block times in redis updates
1 parent eaa42ce commit e0deec1

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/datastore/common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,14 @@ export interface DbPoxCycleSignerStacker {
11101110
stacker_type: 'solo' | 'pooled';
11111111
}
11121112

1113+
export interface BlockHeader {
1114+
index_block_hash: string;
1115+
block_height: number;
1116+
block_time: number;
1117+
}
1118+
11131119
interface ReOrgEntities {
1114-
blockHeaders: { index_block_hash: string; block_height: number }[];
1120+
blockHeaders: BlockHeader[];
11151121
blocks: number;
11161122
microblockHashes: string[];
11171123
microblocks: number;

src/datastore/pg-write-store.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ export class PgWriteStore extends PgStore {
406406
}
407407
});
408408
if (isCanonical) {
409-
await this.redisNotifier?.notify(reorg, data.block.index_block_hash, data.block.block_height);
409+
await this.redisNotifier?.notify(
410+
{
411+
index_block_hash: data.block.index_block_hash,
412+
block_height: data.block.block_height,
413+
block_time: data.block.block_time,
414+
},
415+
reorg
416+
);
410417
}
411418
// Do we have an IBD height defined in ENV? If so, check if this block update reached it.
412419
const ibdHeight = getIbdBlockHeight();
@@ -3590,6 +3597,7 @@ export class PgWriteStore extends PgStore {
35903597
updatedEntities.markedCanonical.blockHeaders.unshift({
35913598
index_block_hash: restoredBlockResult[0].index_block_hash,
35923599
block_height: restoredBlockResult[0].block_height,
3600+
block_time: restoredBlockResult[0].block_time,
35933601
});
35943602

35953603
// Orphan the now conflicting block at the same height
@@ -3632,6 +3640,7 @@ export class PgWriteStore extends PgStore {
36323640
updatedEntities.markedNonCanonical.blockHeaders.unshift({
36333641
index_block_hash: orphanedBlockResult[0].index_block_hash,
36343642
block_height: orphanedBlockResult[0].block_height,
3643+
block_time: orphanedBlockResult[0].block_time,
36353644
});
36363645
const markNonCanonicalResult = await this.markEntitiesCanonical(
36373646
sql,

src/datastore/redis-notifier.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Redis, { Cluster, RedisOptions } from 'ioredis';
2-
import { ReOrgUpdatedEntities } from './common';
2+
import { BlockHeader, ReOrgUpdatedEntities } from './common';
33
import { ChainID } from '@stacks/transactions';
44
import { getApiConfiguredChainID } from '../helpers';
55
import { logger } from '@hirosystems/api-toolkit';
@@ -24,32 +24,36 @@ export class RedisNotifier {
2424
/**
2525
* Broadcast index progress message to the Redis queue.
2626
* @param reOrg - The re-org updated entities, if any
27-
* @param indexBlockHash - Block hash of the newest canonical block
28-
* @param blockHeight - Block height of the newest canonical block
27+
* @param block - The newest canonical block
2928
*/
30-
async notify(reOrg: ReOrgUpdatedEntities, indexBlockHash: string, blockHeight: number) {
29+
async notify(block: BlockHeader, reOrg: ReOrgUpdatedEntities) {
30+
const time = Date.now();
3131
const message = {
32-
id: `stacks-${blockHeight}-${indexBlockHash}-${Date.now()}`,
32+
id: `stacks-${block.block_height}-${block.index_block_hash}-${time}`,
3333
payload: {
3434
chain: 'stacks',
3535
network: this.chainId === ChainID.Mainnet ? 'mainnet' : 'testnet',
36+
time,
3637
apply_blocks: [
3738
...reOrg.markedCanonical.blockHeaders.map(block => ({
3839
hash: block.index_block_hash,
3940
index: block.block_height,
41+
time: block.block_time,
4042
})),
4143
{
42-
hash: indexBlockHash,
43-
index: blockHeight,
44+
hash: block.index_block_hash,
45+
index: block.block_height,
46+
time: block.block_time,
4447
},
4548
],
4649
rollback_blocks: reOrg.markedNonCanonical.blockHeaders.map(block => ({
4750
hash: block.index_block_hash,
4851
index: block.block_height,
52+
time: block.block_time,
4953
})),
5054
},
5155
};
52-
logger.debug(message, 'RedisNotifier broadcasting index progress message');
56+
logger.info(message, 'RedisNotifier broadcasting index progress message');
5357
await this.redis.rpush(this.queue, JSON.stringify(message));
5458
}
5559

tests/api/datastore.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,10 +3773,10 @@ describe('postgres datastore', () => {
37733773
const expectedReorgResult: ReOrgUpdatedEntities = {
37743774
markedCanonical: {
37753775
blockHeaders: [
3776-
{ block_height: 1, index_block_hash: '0xaa' },
3777-
{ block_height: 2, index_block_hash: '0xbb' },
3778-
{ block_height: 3, index_block_hash: '0xcc' },
3779-
{ block_height: 4, index_block_hash: '0xdd' },
3776+
{ block_height: 1, index_block_hash: '0xaa', block_time: 1234 },
3777+
{ block_height: 2, index_block_hash: '0xbb', block_time: 1234 },
3778+
{ block_height: 3, index_block_hash: '0xcc', block_time: 1234 },
3779+
{ block_height: 4, index_block_hash: '0xdd', block_time: 1234 },
37803780
],
37813781
blocks: 4,
37823782
microblocks: 0,
@@ -3799,7 +3799,7 @@ describe('postgres datastore', () => {
37993799
poxSigners: 0,
38003800
},
38013801
markedNonCanonical: {
3802-
blockHeaders: [{ block_height: 3, index_block_hash: '0xccbb' }],
3802+
blockHeaders: [{ block_height: 3, index_block_hash: '0xccbb', block_time: 1234 }],
38033803
blocks: 1,
38043804
microblocks: 0,
38053805
microblockHashes: [],

0 commit comments

Comments
 (0)