1
1
/* eslint-disable promise/always-return */
2
2
import * as Postgres from '@cardano-sdk/projection-typeorm' ;
3
+ import {
4
+ Bootstrap ,
5
+ InMemory ,
6
+ Projections ,
7
+ Sink ,
8
+ StabilityWindowBuffer ,
9
+ WithBlock ,
10
+ projectIntoSink
11
+ } from '@cardano-sdk/projection' ;
3
12
import {
4
13
Cardano ,
5
14
ChainSyncEvent ,
@@ -10,7 +19,6 @@ import {
10
19
} from '@cardano-sdk/core' ;
11
20
import { ChainSyncDataSet , chainSyncData , logger } from '@cardano-sdk/util-dev' ;
12
21
import { ConnectionConfig } from '@cardano-ogmios/client' ;
13
- import { InMemory , Projections , SinksFactory , WithBlock , projectIntoSink } from '@cardano-sdk/projection' ;
14
22
import { Observable , filter , firstValueFrom , lastValueFrom , of , share , take } from 'rxjs' ;
15
23
import { OgmiosObservableCardanoNode } from '@cardano-sdk/ogmios' ;
16
24
import { createDatabase } from 'typeorm-extension' ;
@@ -108,35 +116,36 @@ describe('resuming projection when intersection is not local tip', () => {
108
116
ogmiosCardanoNode = new OgmiosObservableCardanoNode ( { connectionConfig$ : of ( ogmiosConnectionConfig ) } , { logger } ) ;
109
117
} ) ;
110
118
111
- const project = ( cardanoNode : ObservableCardanoNode , sinksFactory : SinksFactory < typeof projections > ) =>
119
+ const project = ( cardanoNode : ObservableCardanoNode , buffer : StabilityWindowBuffer , sink : Sink < typeof projections > ) =>
112
120
projectIntoSink ( {
113
- cardanoNode,
114
121
logger,
115
122
projections,
116
- sinksFactory
123
+ sink,
124
+ source$ : Bootstrap . fromCardanoNode ( { buffer, cardanoNode, logger } )
117
125
} ) ;
118
126
119
127
const testRollbackAndContinue = (
120
- sinksFactory : SinksFactory < typeof projections > ,
128
+ buffer : StabilityWindowBuffer ,
129
+ sink : Sink < typeof projections > ,
121
130
getNumberOfLocalStakeKeys : ( ) => Promise < number >
122
131
) => {
123
132
it ( 'rolls back local data to intersection and resumes projection from there' , async ( ) => {
124
133
// Project some events until we find at least 1 stake key registration
125
134
const firstEventWithKeyRegistrations = await firstValueFrom (
126
- project ( ogmiosCardanoNode , sinksFactory ) . pipe ( filter ( ( evt ) => evt . stakeKeys . insert . length > 0 ) )
135
+ project ( ogmiosCardanoNode , buffer , sink ) . pipe ( filter ( ( evt ) => evt . stakeKeys . insert . length > 0 ) )
127
136
) ;
128
137
const lastEventFromOriginalSync = firstEventWithKeyRegistrations ;
129
138
const numStakeKeysBeforeFork = await getNumberOfLocalStakeKeys ( ) ;
130
139
expect ( numStakeKeysBeforeFork ) . toBe ( firstEventWithKeyRegistrations . stakeKeys . insert . length ) ; // sanity check
131
140
132
141
// Simulate a fork by adding some blocks that are not on the ogmios chain
133
142
const stubForkCardanoNode = createForkProjectionSource ( ogmiosCardanoNode , lastEventFromOriginalSync ) ;
134
- await lastValueFrom ( project ( stubForkCardanoNode , sinksFactory ) . pipe ( take ( 4 ) ) ) ;
143
+ await lastValueFrom ( project ( stubForkCardanoNode , buffer , sink ) . pipe ( take ( 4 ) ) ) ;
135
144
const numStakeKeysAfterFork = await getNumberOfLocalStakeKeys ( ) ;
136
145
expect ( numStakeKeysAfterFork ) . toBeGreaterThan ( numStakeKeysBeforeFork ) ;
137
146
138
147
// Continue projection from ogmios
139
- const continue$ = project ( ogmiosCardanoNode , sinksFactory ) . pipe ( share ( ) ) ;
148
+ const continue$ = project ( ogmiosCardanoNode , buffer , sink ) . pipe ( share ( ) ) ;
140
149
const rollForward$ = continue$ . pipe ( filter ( ( evt ) => evt . eventType === ChainSyncEventType . RollForward ) ) ;
141
150
const rolledBackKeyRegistrations$ = continue$ . pipe (
142
151
filter (
@@ -178,14 +187,13 @@ describe('resuming projection when intersection is not local tip', () => {
178
187
179
188
describe ( 'InMemory' , ( ) => {
180
189
const store = InMemory . createStore ( ) ;
181
- const sinks = InMemory . createSinks ( store ) ;
182
- testRollbackAndContinue (
183
- ( ) => sinks ,
184
- async ( ) => store . stakeKeys . size
185
- ) ;
190
+ const buffer = new InMemory . InMemoryStabilityWindowBuffer ( ) ;
191
+ const sink = InMemory . createSink ( store , buffer ) ;
192
+ testRollbackAndContinue ( buffer , sink , async ( ) => store . stakeKeys . size ) ;
186
193
} ) ;
187
194
188
195
describe ( 'typeorm' , ( ) => {
196
+ const buffer = new Postgres . TypeormStabilityWindowBuffer ( { logger } ) ;
189
197
const dataSource = Postgres . createDataSource ( {
190
198
connectionConfig : pgConnectionConfig ,
191
199
devOptions : {
@@ -198,7 +206,8 @@ describe('resuming projection when intersection is not local tip', () => {
198
206
} ,
199
207
projections
200
208
} ) ;
201
- const sinksFactory = Postgres . createSinksFactory ( {
209
+ const sink = Postgres . createSink ( {
210
+ buffer,
202
211
dataSource$ : of ( dataSource ) ,
203
212
logger
204
213
} ) ;
@@ -219,6 +228,6 @@ describe('resuming projection when intersection is not local tip', () => {
219
228
} ) ;
220
229
afterAll ( ( ) => dataSource . destroy ( ) ) ;
221
230
222
- testRollbackAndContinue ( sinksFactory , getNumberOfLocalStakeKeys ) ;
231
+ testRollbackAndContinue ( buffer , sink , getNumberOfLocalStakeKeys ) ;
223
232
} ) ;
224
233
} ) ;
0 commit comments