Skip to content

Commit 4f61a6d

Browse files
committed
refactor!: add ChainSyncRollBackward.point
It might not be the same as 'tip', because node might already have more than 1 block of the selected fork
1 parent 4dc6232 commit 4f61a6d

File tree

9 files changed

+43
-10
lines changed

9 files changed

+43
-10
lines changed

packages/core/src/CardanoNode/types/CardanoNode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export interface ChainSyncRollForward {
8080

8181
export interface ChainSyncRollBackward {
8282
eventType: ChainSyncEventType.RollBackward;
83+
point: PointOrOrigin;
8384
tip: TipOrOrigin;
8485
requestNext: RequestNext;
8586
}

packages/golden-test-generator/src/ChainSyncEvents/chainSyncEvents.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ const blocksWithRollbacks = (blockHeights: number[], requestedBlocks: RequestedB
2929
const requestedBlock = requestedBlocks[blockNo];
3030
if (!requestedBlock) throw new Error(`Cannot rollback to a non-requested block: ${blockHeight}`);
3131
const header = ogmiosToCore.blockHeader(requestedBlock);
32-
header && result.push({ eventType: ChainSyncEventType.RollBackward, tip: header });
32+
header &&
33+
result.push({
34+
eventType: ChainSyncEventType.RollBackward,
35+
point: {
36+
hash: header.hash,
37+
slot: header.slot
38+
},
39+
tip: header
40+
});
3341
}
3442
}
3543
return result;

packages/projection/src/operators/withRolledBackBlock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const withRolledBackBlock =
2020
takeWhile(
2121
(block): block is Cardano.Block =>
2222
block !== 'origin' &&
23-
(chainSyncEvent.tip === 'origin' || chainSyncEvent.tip.hash !== block.header.hash)
23+
(chainSyncEvent.point === 'origin' || chainSyncEvent.point.hash !== block.header.hash)
2424
),
2525
map((block) => ({
2626
...chainSyncEvent,

packages/projection/src/projectIntoSink.ts

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export const projectIntoSink = <P extends object, PS extends P>(
104104
of({
105105
block,
106106
eventType: ChainSyncEventType.RollBackward,
107+
point: chainSync.intersection.point,
107108
// requestNext is a no-op when rolling back during initialization, because projectIntoSink will
108109
// delete block from the buffer for every RollBackward event via `manageBuffer`,
109110
// which will trigger the buffer to emit the next tip$

packages/projection/src/sinks/inMemory/stakePools.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export const stakePools: Sink<StakePoolsProjection, WithInMemoryStore> = {
2323
findOrCreate(evt, poolId).retirements.push(...poolRetirements);
2424
}
2525
} else {
26-
// Delete all updates and retirements >= current tip.
26+
// Delete all updates and retirements >= current cursor.
2727
const belowTip = ({ source }: WithCertificateSource) =>
28-
evt.tip !== 'origin' && source.slot < evt.block.header.slot;
28+
evt.point !== 'origin' && source.slot < evt.block.header.slot;
2929
for (const [_, stakePool] of evt.store.stakePools) {
3030
stakePool.updates = stakePool.updates.filter(belowTip);
3131
stakePool.retirements = stakePool.retirements.filter(belowTip);

packages/projection/test/events/data/with-pool-retirement.json

+8
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,10 @@
18431843
},
18441844
{
18451845
"eventType": 1,
1846+
"point": {
1847+
"hash": "75829e67ca40d91ef5fce0a665cc4c710cfa73adb59528925ae57264d8062abf",
1848+
"slot": 878533
1849+
},
18461850
"tip": {
18471851
"blockNo": 32205,
18481852
"hash": "75829e67ca40d91ef5fce0a665cc4c710cfa73adb59528925ae57264d8062abf",
@@ -3085,6 +3089,10 @@
30853089
},
30863090
{
30873091
"eventType": 1,
3092+
"point": {
3093+
"hash": "6bfb34f78a3a018a2b1453f78912df6b88fc470856b82f66184fee40ef73bc97",
3094+
"slot": 900033
3095+
},
30883096
"tip": {
30893097
"blockNo": 33275,
30903098
"hash": "6bfb34f78a3a018a2b1453f78912df6b88fc470856b82f66184fee40ef73bc97",

packages/projection/test/events/data/with-stake-key-deregistration.json

+8
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@
547547
},
548548
{
549549
"eventType": 1,
550+
"point": {
551+
"hash": "b830b7fb2eb6f7ed18a30cedd2c347769d83ecd7902edf89a4c9ea2b7ffa2020",
552+
"slot": 687859
553+
},
550554
"tip": {
551555
"blockNo": 22618,
552556
"hash": "b830b7fb2eb6f7ed18a30cedd2c347769d83ecd7902edf89a4c9ea2b7ffa2020",
@@ -4197,6 +4201,10 @@
41974201
},
41984202
{
41994203
"eventType": 1,
4204+
"point": {
4205+
"hash": "383bb0210d135b0701aca287b4b534957b64f364076158987af3a128d20c04f7",
4206+
"slot": 690875
4207+
},
42004208
"tip": {
42014209
"blockNo": 22734,
42024210
"hash": "383bb0210d135b0701aca287b4b534957b64f364076158987af3a128d20c04f7",

packages/projection/test/operators/withRolledBackBlock.test.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ const createEvent = (eventType: ChainSyncEventType, slot: number, tipSlot = slot
99
({
1010
block: { header: { blockNo: Cardano.BlockNo(slot), hash: stubBlockId(slot), slot: Cardano.Slot(slot) } },
1111
eventType,
12+
point:
13+
eventType === ChainSyncEventType.RollForward
14+
? undefined
15+
: { hash: stubBlockId(tipSlot), slot: Cardano.Slot(tipSlot) },
1216
requestNext: expect.anything(),
1317
tip: { blockNo: Cardano.BlockNo(tipSlot), hash: stubBlockId(tipSlot), slot: Cardano.Slot(tipSlot) }
1418
} as UnifiedProjectorEvent<{}>);
1519

16-
const sourceRollback = (slot: number) =>
17-
({
20+
const sourceRollback = (slot: number): ChainSyncRollBackward => {
21+
const point = { hash: stubBlockId(slot), slot: Cardano.Slot(slot) };
22+
return {
1823
eventType: ChainSyncEventType.RollBackward,
24+
point,
1925
requestNext: jest.fn(),
20-
tip: { blockNo: Cardano.BlockNo(slot), hash: stubBlockId(slot), slot: Cardano.Slot(slot) }
21-
} as ChainSyncRollBackward);
26+
tip: { ...point, blockNo: Cardano.BlockNo(slot) }
27+
};
28+
};
2229

2330
describe('withRolledBackBlocks', () => {
2431
let buffer: sinks.StabilityWindowBuffer;

packages/projection/test/projectIntoSink.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Crypto from '@cardano-sdk/crypto';
22
import { Cardano, ChainSyncEventType, ChainSyncRollForward } from '@cardano-sdk/core';
3-
import { InvalidIntersectionError, projectIntoSink, projections, sinks } from '../src';
3+
import { ObservableChainSyncError, projectIntoSink, projections, sinks } from '../src';
44
import { StubChainSyncData, dataWithPoolRetirement, dataWithStakeKeyDeregistration } from './events';
55
import { concat, defaultIfEmpty, firstValueFrom, lastValueFrom, toArray } from 'rxjs';
66
import { logger } from '@cardano-sdk/util-dev';
@@ -132,7 +132,7 @@ describe('projectIntoSink', () => {
132132
).pipe(defaultIfEmpty(null))
133133
);
134134
await expect(projectAll(dataWithStakeKeyDeregistration, inMemorySinks)).rejects.toThrowError(
135-
InvalidIntersectionError
135+
ObservableChainSyncError
136136
);
137137
});
138138

0 commit comments

Comments
 (0)