Skip to content

Commit d4324b8

Browse files
authored
fix(notifier): include block times in redis updates (#2352)
* fix: include block times in redis updates * fix: tests * fix: tests again
1 parent eaa42ce commit d4324b8

File tree

5 files changed

+74
-42
lines changed

5 files changed

+74
-42
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: [],

tests/api/redis-notifier.test.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ describe('redis notifier', () => {
4444
block_height: 1,
4545
block_hash: '0x1234',
4646
index_block_hash: '0x1234',
47+
block_time: 1234,
4748
}).build();
4849
await db.update(block1);
4950

5051
expect(messages.length).toBe(1);
51-
expect(JSON.parse(messages[0]).payload).toEqual({
52-
chain: 'stacks',
53-
network: 'mainnet',
54-
apply_blocks: [{ hash: '0x1234', index: 1 }],
55-
rollback_blocks: [],
56-
});
52+
expect(JSON.parse(messages[0]).payload).toEqual(
53+
expect.objectContaining({
54+
chain: 'stacks',
55+
network: 'mainnet',
56+
apply_blocks: [{ hash: '0x1234', index: 1, time: 1234 }],
57+
rollback_blocks: [],
58+
})
59+
);
5760
});
5861

5962
test('updates redis with re-orgs', async () => {
@@ -62,31 +65,37 @@ describe('redis notifier', () => {
6265
block_height: 1,
6366
block_hash: '0x1234',
6467
index_block_hash: '0x1234',
68+
block_time: 1234,
6569
}).build()
6670
);
6771
expect(messages.length).toBe(1);
68-
expect(JSON.parse(messages[0]).payload).toEqual({
69-
chain: 'stacks',
70-
network: 'mainnet',
71-
apply_blocks: [{ hash: '0x1234', index: 1 }],
72-
rollback_blocks: [],
73-
});
72+
expect(JSON.parse(messages[0]).payload).toEqual(
73+
expect.objectContaining({
74+
chain: 'stacks',
75+
network: 'mainnet',
76+
apply_blocks: [{ hash: '0x1234', index: 1, time: 1234 }],
77+
rollback_blocks: [],
78+
})
79+
);
7480

7581
await db.update(
7682
new TestBlockBuilder({
7783
block_height: 2,
7884
block_hash: '0x1235',
7985
index_block_hash: '0x1235',
8086
parent_index_block_hash: '0x1234',
87+
block_time: 1234,
8188
}).build()
8289
);
8390
expect(messages.length).toBe(2);
84-
expect(JSON.parse(messages[1]).payload).toEqual({
85-
chain: 'stacks',
86-
network: 'mainnet',
87-
apply_blocks: [{ hash: '0x1235', index: 2 }],
88-
rollback_blocks: [],
89-
});
91+
expect(JSON.parse(messages[1]).payload).toEqual(
92+
expect.objectContaining({
93+
chain: 'stacks',
94+
network: 'mainnet',
95+
apply_blocks: [{ hash: '0x1235', index: 2, time: 1234 }],
96+
rollback_blocks: [],
97+
})
98+
);
9099

91100
// Re-org block 2, should not send a message because this block is not canonical
92101
await db.update(
@@ -95,6 +104,7 @@ describe('redis notifier', () => {
95104
block_hash: '0x1235aa',
96105
index_block_hash: '0x1235aa',
97106
parent_index_block_hash: '0x1234',
107+
block_time: 1234,
98108
}).build()
99109
);
100110
expect(messages.length).toBe(2);
@@ -106,17 +116,20 @@ describe('redis notifier', () => {
106116
block_hash: '0x1236',
107117
index_block_hash: '0x1236',
108118
parent_index_block_hash: '0x1235aa',
119+
block_time: 1234,
109120
}).build()
110121
);
111122
expect(messages.length).toBe(3);
112-
expect(JSON.parse(messages[2]).payload).toEqual({
113-
chain: 'stacks',
114-
network: 'mainnet',
115-
apply_blocks: [
116-
{ hash: '0x1235aa', index: 2 },
117-
{ hash: '0x1236', index: 3 },
118-
],
119-
rollback_blocks: [{ hash: '0x1235', index: 2 }],
120-
});
123+
expect(JSON.parse(messages[2]).payload).toEqual(
124+
expect.objectContaining({
125+
chain: 'stacks',
126+
network: 'mainnet',
127+
apply_blocks: [
128+
{ hash: '0x1235aa', index: 2, time: 1234 },
129+
{ hash: '0x1236', index: 3, time: 1234 },
130+
],
131+
rollback_blocks: [{ hash: '0x1235', index: 2, time: 1234 }],
132+
})
133+
);
121134
});
122135
});

0 commit comments

Comments
 (0)