-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathOgmiosObservableCardanoNode.ts
220 lines (208 loc) · 7.92 KB
/
OgmiosObservableCardanoNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Tested in packages/e2e/test/projection
import {
Cardano,
CardanoNodeError,
CardanoNodeUtil,
EraSummary,
GeneralCardanoNodeError,
GeneralCardanoNodeErrorCode,
HealthCheckResponse,
Milliseconds,
ObservableCardanoNode,
ObservableChainSync,
PointOrOrigin,
StateQueryErrorCode,
TxCBOR
} from '@cardano-sdk/core';
import {
ChainSynchronization,
ConnectionConfig,
TransactionSubmission,
createConnectionObject,
createLedgerStateQueryClient,
getServerHealth
} from '@cardano-ogmios/client';
import { InteractionContextProps, createObservableInteractionContext } from './createObservableInteractionContext';
import { Logger } from 'ts-log';
import {
Observable,
catchError,
distinctUntilChanged,
from,
map,
of,
shareReplay,
switchMap,
take,
throwError,
timeout
} from 'rxjs';
import { RetryBackoffConfig, retryBackoff } from 'backoff-rxjs';
import { WithLogger, contextLogger } from '@cardano-sdk/util';
import { createObservableChainSyncClient } from './createObservableChainSyncClient';
import { ogmiosServerHealthToHealthCheckResponse } from '../../util';
import { ogmiosToCorePointOrOrigin, ogmiosToCoreTipOrOrigin, pointOrOriginToOgmios } from './util';
import { queryEraSummaries, queryGenesisParameters, withCoreCardanoNodeError } from '../queries';
import isEqual from 'lodash/isEqual.js';
const ogmiosToCoreIntersection = (intersection: ChainSynchronization.Intersection) => ({
point: ogmiosToCorePointOrOrigin(intersection.intersection),
tip: ogmiosToCoreTipOrOrigin(intersection.tip)
});
export type LocalStateQueryRetryConfig = Pick<RetryBackoffConfig, 'initialInterval' | 'maxInterval'>;
export type SubmitTxRetryConfig = Pick<RetryBackoffConfig, 'initialInterval' | 'maxInterval' | 'maxRetries'>;
const DEFAULT_HEALTH_CHECK_TIMEOUT = Milliseconds(2000);
const DEFAULT_SUBMIT_MAX_RETRIES = 5;
const DEFAULT_RETRY_CONFIG: LocalStateQueryRetryConfig = {
initialInterval: 1000,
maxInterval: 30_000
};
export type OgmiosObservableCardanoNodeProps = InteractionContextProps & {
/** Default: 2000ms */
healthCheckTimeout?: Milliseconds;
/** Default: {initialInterval: 1000, maxInterval: 30_000} */
localStateQueryRetryConfig?: LocalStateQueryRetryConfig;
/** Default: {initialInterval: 1000, maxInterval: 30_000, maxRetries: 5} */
submitTxQueryRetryConfig?: SubmitTxRetryConfig;
};
const retryableCardanoNodeErrors = new Set<number>([
GeneralCardanoNodeErrorCode.ServerNotReady,
GeneralCardanoNodeErrorCode.ConnectionFailure
]);
const retryableStateQueryErrors = new Set<number>([
...retryableCardanoNodeErrors,
StateQueryErrorCode.UnavailableInCurrentEra
]);
const stateQueryRetryBackoffConfig = (
retryConfig: LocalStateQueryRetryConfig = DEFAULT_RETRY_CONFIG,
logger: Logger
): RetryBackoffConfig => ({
...retryConfig,
shouldRetry: (error) => {
if (retryableStateQueryErrors.has(CardanoNodeUtil.asCardanoNodeError(error)?.code)) {
logger.info('Local state query unavailable yet, will retry...');
return true;
}
return false;
}
});
const submitTxRetryBackoffConfig = (
retryConfig: SubmitTxRetryConfig = DEFAULT_RETRY_CONFIG,
logger: Logger
): RetryBackoffConfig => ({
...retryConfig,
maxRetries: retryConfig.maxRetries || DEFAULT_SUBMIT_MAX_RETRIES,
shouldRetry: (error) => {
if (retryableStateQueryErrors.has(CardanoNodeUtil.asCardanoNodeError(error)?.code)) {
logger.warn('Failed to submitTx, will retry', error);
return true;
}
return false;
}
});
export class OgmiosObservableCardanoNode implements ObservableCardanoNode {
readonly #connectionConfig$: Observable<ConnectionConfig>;
readonly #submitTxRetryBackoffConfig: RetryBackoffConfig;
readonly #logger: Logger;
readonly #interactionContext$;
readonly eraSummaries$: Observable<EraSummary[]>;
readonly genesisParameters$: Observable<Cardano.CompactGenesis>;
readonly healthCheck$: Observable<HealthCheckResponse>;
constructor(props: OgmiosObservableCardanoNodeProps, { logger }: WithLogger) {
this.#connectionConfig$ = props.connectionConfig$;
this.#logger = contextLogger(logger, 'ObservableOgmiosCardanoNode');
this.#submitTxRetryBackoffConfig = submitTxRetryBackoffConfig(props.submitTxQueryRetryConfig, logger);
this.#interactionContext$ = createObservableInteractionContext(
{
...props
},
{ logger: this.#logger }
).pipe(shareReplay({ bufferSize: 1, refCount: true }));
const stateQueryClient$ = this.#interactionContext$.pipe(
switchMap((interactionContext) => from(createLedgerStateQueryClient(interactionContext))),
distinctUntilChanged((a, b) => isEqual(a, b)),
shareReplay({ bufferSize: 1, refCount: true })
);
this.eraSummaries$ = stateQueryClient$.pipe(
switchMap((client) => from(queryEraSummaries(client, this.#logger))),
retryBackoff(stateQueryRetryBackoffConfig(props.localStateQueryRetryConfig, logger))
);
this.genesisParameters$ = stateQueryClient$.pipe(
switchMap((client) => from(queryGenesisParameters(client, this.#logger))),
retryBackoff(stateQueryRetryBackoffConfig(props.localStateQueryRetryConfig, logger)),
distinctUntilChanged(isEqual),
shareReplay({ bufferSize: 1, refCount: true })
);
this.healthCheck$ = this.#connectionConfig$.pipe(
switchMap((connectionConfig) => from(getServerHealth({ connection: createConnectionObject(connectionConfig) }))),
map(ogmiosServerHealthToHealthCheckResponse),
timeout({
first: props.healthCheckTimeout || DEFAULT_HEALTH_CHECK_TIMEOUT,
with: () => {
logger.error('healthCheck$ didnt emit within healthCheckTimeout');
return throwError(
() =>
new GeneralCardanoNodeError(
GeneralCardanoNodeErrorCode.ConnectionFailure,
null,
'Healthcheck request timeout'
)
);
}
}),
catchError((error) => {
this.#logger.error(error);
return of({ ok: false });
}),
shareReplay({ bufferSize: 1, refCount: true })
);
}
/**
* See {@link ObservableCardanoNode.findIntersect}.
*
* This implementation of `chainSync$` in the emitted object should only have
* a single subscriber per client (instance of OgmiosObservableCardanoNode),
* because it's using a stateful connection - limited to 1 cursor per connection.
*/
findIntersect(points: PointOrOrigin[]) {
return this.#interactionContext$.pipe(
switchMap(
(interactionContext) =>
new Observable<ObservableChainSync>((subscriber) => {
if (subscriber.closed) return;
void ChainSynchronization.findIntersection(interactionContext, points.map(pointOrOriginToOgmios))
.then((ogmiosIntersection) => {
const intersection = ogmiosToCoreIntersection(ogmiosIntersection);
subscriber.next({
chainSync$: createObservableChainSyncClient(
{ intersectionPoint: intersection.point },
{ interactionContext$: this.#interactionContext$, logger: this.#logger }
),
intersection
});
})
.catch((error) => {
this.#logger.error('"findIntersect" failed', error);
if (error instanceof CardanoNodeError && error.code === GeneralCardanoNodeErrorCode.ConnectionFailure) {
// interactionContext$ will reconnect and trigger a retry
return;
}
subscriber.error(error);
});
})
)
);
}
submitTx(tx: TxCBOR): Observable<Cardano.TransactionId> {
return this.#interactionContext$.pipe(
switchMap((context) =>
from(
withCoreCardanoNodeError(() =>
TransactionSubmission.submitTransaction(context, tx)
) as Promise<Cardano.TransactionId>
)
),
retryBackoff(this.#submitTxRetryBackoffConfig),
take(1)
);
}
}