@canton-network/core-ledger-client 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -724
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -722
- package/dist/index.js.map +1 -1
- package/dist/ledger-client.d.ts +1 -33
- package/dist/ledger-client.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/acs/acs-container.d.ts +0 -36
- package/dist/acs/acs-container.d.ts.map +0 -1
- package/dist/acs/acs-helper.d.ts +0 -56
- package/dist/acs/acs-helper.d.ts.map +0 -1
- package/dist/acs/acs-shared-cache.d.ts +0 -14
- package/dist/acs/acs-shared-cache.d.ts.map +0 -1
- package/dist/acs/types.d.ts +0 -10
- package/dist/acs/types.d.ts.map +0 -1
- package/dist/acs/ws-support.d.ts +0 -18
- package/dist/acs/ws-support.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { supportedLedgerApiVersions } from '@canton-network/core-ledger-client-types';
|
|
2
2
|
import createClient from 'openapi-fetch';
|
|
3
3
|
import { ErrorInfo, RetryInfo } from '@canton-network/core-ledger-proto';
|
|
4
|
-
import { LRUCache } from 'typescript-lru-cache';
|
|
5
4
|
|
|
6
5
|
var __defProp = Object.defineProperty;
|
|
7
6
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
@@ -142,482 +141,6 @@ var asGrpcError = (e) => {
|
|
|
142
141
|
throw e;
|
|
143
142
|
};
|
|
144
143
|
|
|
145
|
-
// src/acs/acs-container.ts
|
|
146
|
-
var ACS_UPDATE_CONFIG = {
|
|
147
|
-
// How many events do we accumulate before we prune (compact) the ACS history - set to 0 to enable to compact all events, which is more efficient as long as application always ask for increasing (or equal) offsets
|
|
148
|
-
maxEventsBeforePrune: 150,
|
|
149
|
-
// When we compact the ACS history, we keep all events within this offset delta of the last seen update offset - set 0 to allow to compact everything
|
|
150
|
-
safeOffsetDeltaForPrune: 200,
|
|
151
|
-
// How many updates do we fetch at once when fetching updates - if there are more updates, we will fetch again until we have caught up (returned data is always complete to the requested endInclusive offset - even if that means multiple fetches)
|
|
152
|
-
maxUpdatesToFetch: 100,
|
|
153
|
-
// Timeout for receiving the first element from the WebSocket stream when reading the ACS - if this timeout is reached before the first element is returned, we fall back to HTTP
|
|
154
|
-
// lower values will make the UI more responsive in case of problems, but may lead to unnecessary fallbacks
|
|
155
|
-
wsTimeoutBeforeFirstElement: 1e4
|
|
156
|
-
};
|
|
157
|
-
if (typeof window !== "undefined") {
|
|
158
|
-
window["ACS_UPDATE_CONFIG"] = ACS_UPDATE_CONFIG;
|
|
159
|
-
}
|
|
160
|
-
var ACSContainer = class _ACSContainer {
|
|
161
|
-
constructor(orginalContainer = null, settings = { includeCreatedEventBlob: true }) {
|
|
162
|
-
__publicField(this, "acsSet", null);
|
|
163
|
-
__publicField(this, "settings");
|
|
164
|
-
this.acsSet = orginalContainer ? orginalContainer.acsSet : null;
|
|
165
|
-
this.settings = settings;
|
|
166
|
-
}
|
|
167
|
-
async update(offset, key, api, wsSupport) {
|
|
168
|
-
if (this.acsSet === null) {
|
|
169
|
-
const acs = await this.readACS(offset, key, api, wsSupport);
|
|
170
|
-
this.acsSet = acs;
|
|
171
|
-
return _ACSContainer.calculateAt(acs, offset);
|
|
172
|
-
}
|
|
173
|
-
if (this.acsSet.acsOffset > offset) {
|
|
174
|
-
this.acsSet = null;
|
|
175
|
-
return this.update(offset, key, api);
|
|
176
|
-
}
|
|
177
|
-
if (this.acsSet.updates.length >= ACS_UPDATE_CONFIG.maxEventsBeforePrune) {
|
|
178
|
-
this.acsSet = await _ACSContainer.compact(this.acsSet);
|
|
179
|
-
}
|
|
180
|
-
const updates = await _ACSContainer.updateContracts(
|
|
181
|
-
this.acsSet.lastUpdateOffset,
|
|
182
|
-
offset,
|
|
183
|
-
this.createEventFormat(key),
|
|
184
|
-
api
|
|
185
|
-
);
|
|
186
|
-
const [newEvents, newOffset] = _ACSContainer.extractEvents(
|
|
187
|
-
this.acsSet.lastUpdateOffset,
|
|
188
|
-
updates
|
|
189
|
-
);
|
|
190
|
-
if (newOffset > this.acsSet.lastUpdateOffset) {
|
|
191
|
-
this.acsSet = {
|
|
192
|
-
...this.acsSet,
|
|
193
|
-
lastUpdateOffset: newOffset,
|
|
194
|
-
updates: this.acsSet.updates.concat(newEvents)
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
if (updates.length >= ACS_UPDATE_CONFIG.maxUpdatesToFetch) {
|
|
198
|
-
return this.update(offset, key, api);
|
|
199
|
-
}
|
|
200
|
-
this.acsSet = {
|
|
201
|
-
...this.acsSet,
|
|
202
|
-
lastUpdateOffset: offset
|
|
203
|
-
// we have caught up
|
|
204
|
-
};
|
|
205
|
-
return _ACSContainer.calculateAt(this.acsSet, offset);
|
|
206
|
-
}
|
|
207
|
-
static calculateAt(acs, offset) {
|
|
208
|
-
if (acs.acsOffset > offset) {
|
|
209
|
-
return Promise.reject(
|
|
210
|
-
Error(
|
|
211
|
-
// This should never happen as we check this before calling calculateAt
|
|
212
|
-
`Cannot calculate ACS at offset ${offset} when initial ACS is at ${acs.acsOffset}`
|
|
213
|
-
)
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
const addedContracts = [];
|
|
217
|
-
const removedContractIds = /* @__PURE__ */ new Set();
|
|
218
|
-
acs.updates.forEach((update) => {
|
|
219
|
-
if (update.offset <= offset) {
|
|
220
|
-
if (update.created) {
|
|
221
|
-
const createdEv = update.created;
|
|
222
|
-
const activeContract = {
|
|
223
|
-
workflowId: update.workflowId ?? "",
|
|
224
|
-
contractEntry: {
|
|
225
|
-
JsActiveContract: {
|
|
226
|
-
createdEvent: createdEv,
|
|
227
|
-
synchronizerId: update.synchronizerId ?? "",
|
|
228
|
-
reassignmentCounter: 0
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
addedContracts.push(activeContract);
|
|
233
|
-
} else if (update.archivedContractId) {
|
|
234
|
-
removedContractIds.add(update.archivedContractId);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
const createdContracts = acs.initialAcs.concat(addedContracts);
|
|
239
|
-
const result = createdContracts.filter(({ contractEntry }) => {
|
|
240
|
-
if (!contractEntry) {
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
const id = "JsActiveContract" in contractEntry ? contractEntry.JsActiveContract?.createdEvent?.contractId ?? "" : "";
|
|
244
|
-
return !removedContractIds.has(id);
|
|
245
|
-
});
|
|
246
|
-
return Promise.resolve(result);
|
|
247
|
-
}
|
|
248
|
-
static extractEvents(fromOffset, updates) {
|
|
249
|
-
const newEvents = [];
|
|
250
|
-
let newOffset = fromOffset;
|
|
251
|
-
updates.forEach((update) => {
|
|
252
|
-
if (!update || !update.update) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if ("Transaction" in update.update) {
|
|
256
|
-
const transaction = update.update.Transaction;
|
|
257
|
-
const trOffset = transaction?.value?.offset;
|
|
258
|
-
if (trOffset && trOffset > newOffset) {
|
|
259
|
-
const events = transaction?.value?.events ?? [];
|
|
260
|
-
events.forEach((event) => {
|
|
261
|
-
if (!event) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
if ("CreatedEvent" in event) {
|
|
265
|
-
const acUpdate = {
|
|
266
|
-
created: event.CreatedEvent,
|
|
267
|
-
archivedContractId: null,
|
|
268
|
-
offset: trOffset,
|
|
269
|
-
workflowId: transaction?.value?.workflowId ?? null,
|
|
270
|
-
synchronizerId: transaction?.value?.synchronizerId ?? null
|
|
271
|
-
};
|
|
272
|
-
newEvents.push(acUpdate);
|
|
273
|
-
newOffset = trOffset;
|
|
274
|
-
} else if ("ArchivedEvent" in event) {
|
|
275
|
-
const archivedEvent = event.ArchivedEvent;
|
|
276
|
-
const acUpdate = {
|
|
277
|
-
created: null,
|
|
278
|
-
archivedContractId: archivedEvent?.contractId ?? null,
|
|
279
|
-
offset: trOffset,
|
|
280
|
-
workflowId: transaction?.value?.workflowId ?? null,
|
|
281
|
-
synchronizerId: transaction?.value?.synchronizerId ?? null
|
|
282
|
-
};
|
|
283
|
-
newEvents.push(acUpdate);
|
|
284
|
-
newOffset = trOffset;
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
} else if ("OffsetCheckpoint" in update.update) {
|
|
289
|
-
const checkpoint = update.update.OffsetCheckpoint;
|
|
290
|
-
const offset = checkpoint?.value?.offset;
|
|
291
|
-
if (offset) {
|
|
292
|
-
newOffset = offset;
|
|
293
|
-
}
|
|
294
|
-
} else {
|
|
295
|
-
console.log(
|
|
296
|
-
`ACS Update got unknown update type: ${JSON.stringify(update.update)}`
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
return [newEvents, newOffset];
|
|
301
|
-
}
|
|
302
|
-
static async updateContracts(startOffset, endOffset, eventFormat, api) {
|
|
303
|
-
const request = {
|
|
304
|
-
beginExclusive: startOffset,
|
|
305
|
-
endInclusive: endOffset,
|
|
306
|
-
updateFormat: {
|
|
307
|
-
includeTransactions: {
|
|
308
|
-
eventFormat,
|
|
309
|
-
transactionShape: "TRANSACTION_SHAPE_ACS_DELTA"
|
|
310
|
-
}
|
|
311
|
-
},
|
|
312
|
-
verbose: false
|
|
313
|
-
};
|
|
314
|
-
const params = {
|
|
315
|
-
query: {
|
|
316
|
-
limit: ACS_UPDATE_CONFIG.maxUpdatesToFetch,
|
|
317
|
-
stream_idle_timeout_ms: 1e3
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
return api.postWithRetry(
|
|
321
|
-
"/v2/updates/flats",
|
|
322
|
-
request,
|
|
323
|
-
defaultRetryableOptions,
|
|
324
|
-
params
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
static async compact(acs) {
|
|
328
|
-
const newAcsOffset = Math.max(
|
|
329
|
-
acs.acsOffset,
|
|
330
|
-
acs.lastUpdateOffset - ACS_UPDATE_CONFIG.safeOffsetDeltaForPrune
|
|
331
|
-
);
|
|
332
|
-
if (newAcsOffset > acs.acsOffset) {
|
|
333
|
-
const responses = await _ACSContainer.calculateAt(acs, newAcsOffset);
|
|
334
|
-
return {
|
|
335
|
-
acsOffset: newAcsOffset,
|
|
336
|
-
initialAcs: responses,
|
|
337
|
-
lastUpdateOffset: acs.lastUpdateOffset,
|
|
338
|
-
updates: acs.updates.filter(
|
|
339
|
-
(update) => update.offset > newAcsOffset
|
|
340
|
-
)
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
return acs;
|
|
344
|
-
}
|
|
345
|
-
async readACS(offset, key, api, wsSupport) {
|
|
346
|
-
if (wsSupport && wsSupport.enabled()) {
|
|
347
|
-
return this.readACSUsingWs(offset, key, wsSupport).catch(() => {
|
|
348
|
-
console.log("Falling back to HTTP for ACS read");
|
|
349
|
-
return this.readHttpACS(offset, key, api);
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
return this.readHttpACS(offset, key, api);
|
|
353
|
-
}
|
|
354
|
-
async readHttpACS(offset, key, api) {
|
|
355
|
-
const format = this.createEventFormat(key);
|
|
356
|
-
const acs = await api.postWithRetry(
|
|
357
|
-
"/v2/state/active-contracts",
|
|
358
|
-
{
|
|
359
|
-
activeAtOffset: offset,
|
|
360
|
-
verbose: false,
|
|
361
|
-
eventFormat: format
|
|
362
|
-
},
|
|
363
|
-
defaultRetryableOptions
|
|
364
|
-
);
|
|
365
|
-
return {
|
|
366
|
-
acsOffset: offset,
|
|
367
|
-
initialAcs: acs,
|
|
368
|
-
lastUpdateOffset: offset,
|
|
369
|
-
updates: []
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
readACSUsingWs(offset, key, wsSupport) {
|
|
373
|
-
const wsACSURL = `${wsSupport.baseUrl}/v2/state/active-contracts`;
|
|
374
|
-
const format = this.createEventFormat(key);
|
|
375
|
-
const request = {
|
|
376
|
-
activeAtOffset: offset,
|
|
377
|
-
verbose: false,
|
|
378
|
-
eventFormat: format
|
|
379
|
-
};
|
|
380
|
-
return new Promise((resolve, reject) => {
|
|
381
|
-
const ws = new WebSocket(wsACSURL, wsSupport.extractProtocols());
|
|
382
|
-
const results = [];
|
|
383
|
-
let finished = false;
|
|
384
|
-
let error = null;
|
|
385
|
-
setTimeout(() => {
|
|
386
|
-
if (!finished && !error && results.length === 0) {
|
|
387
|
-
error = Error(
|
|
388
|
-
`No data received from WebSocket ${wsACSURL} within ${ACS_UPDATE_CONFIG.wsTimeoutBeforeFirstElement}ms`
|
|
389
|
-
);
|
|
390
|
-
wsSupport.reportError(error);
|
|
391
|
-
reject(error);
|
|
392
|
-
ws.close();
|
|
393
|
-
}
|
|
394
|
-
}, ACS_UPDATE_CONFIG.wsTimeoutBeforeFirstElement);
|
|
395
|
-
ws.onopen = () => {
|
|
396
|
-
ws.send(JSON.stringify(request));
|
|
397
|
-
};
|
|
398
|
-
ws.onmessage = (event) => {
|
|
399
|
-
try {
|
|
400
|
-
const data = JSON.parse(
|
|
401
|
-
event.data
|
|
402
|
-
);
|
|
403
|
-
results.push(data);
|
|
404
|
-
} catch (err) {
|
|
405
|
-
console.error("Invalid JSON:", err);
|
|
406
|
-
}
|
|
407
|
-
};
|
|
408
|
-
ws.onerror = () => {
|
|
409
|
-
error = new Error("WebSocket error");
|
|
410
|
-
wsSupport.reportError(error);
|
|
411
|
-
reject(error);
|
|
412
|
-
};
|
|
413
|
-
ws.onclose = () => {
|
|
414
|
-
finished = true;
|
|
415
|
-
if (!error) {
|
|
416
|
-
wsSupport.reportSuccess();
|
|
417
|
-
resolve({
|
|
418
|
-
acsOffset: offset,
|
|
419
|
-
initialAcs: results,
|
|
420
|
-
lastUpdateOffset: offset,
|
|
421
|
-
updates: []
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
createInterfaceFilter(interfaceId) {
|
|
428
|
-
return {
|
|
429
|
-
cumulative: [
|
|
430
|
-
{
|
|
431
|
-
identifierFilter: {
|
|
432
|
-
InterfaceFilter: {
|
|
433
|
-
value: {
|
|
434
|
-
interfaceId,
|
|
435
|
-
includeInterfaceView: true,
|
|
436
|
-
includeCreatedEventBlob: this.settings.includeCreatedEventBlob
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
]
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
createTemplateFilter(templateId) {
|
|
445
|
-
return {
|
|
446
|
-
cumulative: [
|
|
447
|
-
{
|
|
448
|
-
identifierFilter: {
|
|
449
|
-
TemplateFilter: {
|
|
450
|
-
value: {
|
|
451
|
-
templateId,
|
|
452
|
-
includeCreatedEventBlob: this.settings.includeCreatedEventBlob
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
]
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
createEventFormat(key) {
|
|
461
|
-
const cumulativeFilter = key.templateId ? this.createTemplateFilter(key.templateId) : this.createInterfaceFilter(key.interfaceId ?? "");
|
|
462
|
-
if (key.party) {
|
|
463
|
-
if (!key.interfaceId && !key.templateId) {
|
|
464
|
-
return {
|
|
465
|
-
filtersByParty: {
|
|
466
|
-
[key.party]: {}
|
|
467
|
-
},
|
|
468
|
-
verbose: false
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
return {
|
|
472
|
-
filtersByParty: {
|
|
473
|
-
[key.party]: cumulativeFilter
|
|
474
|
-
},
|
|
475
|
-
verbose: false
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
return {
|
|
479
|
-
filtersForAnyParty: cumulativeFilter,
|
|
480
|
-
verbose: false
|
|
481
|
-
};
|
|
482
|
-
}
|
|
483
|
-
};
|
|
484
|
-
var DEFAULT_MAX_CACHE_SIZE = 100;
|
|
485
|
-
var DEFAULT_ENTRY_EXPIRATION_TIME = 10 * 60 * 1e3;
|
|
486
|
-
var SharedACSCache = new LRUCache({
|
|
487
|
-
maxSize: DEFAULT_MAX_CACHE_SIZE,
|
|
488
|
-
entryExpirationTimeInMS: DEFAULT_ENTRY_EXPIRATION_TIME,
|
|
489
|
-
onEntryEvicted: (entry) => {
|
|
490
|
-
console.debug(`${entry} has expired`);
|
|
491
|
-
SharedACSCacheStats.evictions++;
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
var SharedACSCacheStats = {
|
|
495
|
-
hits: 0,
|
|
496
|
-
misses: 0,
|
|
497
|
-
evictions: 0,
|
|
498
|
-
totalLookupTime: 0,
|
|
499
|
-
totalCacheServeTime: 0
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
// src/acs/acs-helper.ts
|
|
503
|
-
var ACSHelper = class _ACSHelper {
|
|
504
|
-
constructor(apiInstance, _logger, options, sharedCache) {
|
|
505
|
-
__publicField(this, "contractsSet");
|
|
506
|
-
__publicField(this, "apiInstance");
|
|
507
|
-
__publicField(this, "wsSupport");
|
|
508
|
-
__publicField(this, "logger");
|
|
509
|
-
__publicField(this, "includeCreatedEventBlob");
|
|
510
|
-
__publicField(this, "totalCacheServeTime", 0);
|
|
511
|
-
this.contractsSet = sharedCache ?? new LRUCache({
|
|
512
|
-
maxSize: options?.maxCacheSize ?? DEFAULT_MAX_CACHE_SIZE,
|
|
513
|
-
entryExpirationTimeInMS: options?.entryExpirationTime ?? DEFAULT_ENTRY_EXPIRATION_TIME,
|
|
514
|
-
// 10 minutes
|
|
515
|
-
onEntryEvicted: (entry) => {
|
|
516
|
-
this.logger.debug(
|
|
517
|
-
`entry ${entry.key} isExpired = ${entry.isExpired}. evicting entry.`
|
|
518
|
-
);
|
|
519
|
-
SharedACSCacheStats.evictions++;
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
this.apiInstance = apiInstance;
|
|
523
|
-
this.wsSupport = options?.wsSupport;
|
|
524
|
-
this.logger = _logger.child({ component: "ACSHelper" });
|
|
525
|
-
this.includeCreatedEventBlob = options?.includeCreatedEventBlob ?? true;
|
|
526
|
-
}
|
|
527
|
-
getCacheStats() {
|
|
528
|
-
const totalCalls = SharedACSCacheStats.hits + SharedACSCacheStats.misses;
|
|
529
|
-
const hitRate = totalCalls ? SharedACSCacheStats.hits / totalCalls * 100 : 0;
|
|
530
|
-
const avgLookupTime = totalCalls > 0 ? SharedACSCacheStats.totalLookupTime / totalCalls : 0;
|
|
531
|
-
return {
|
|
532
|
-
totalCalls,
|
|
533
|
-
hits: SharedACSCacheStats.hits,
|
|
534
|
-
misses: SharedACSCacheStats.misses,
|
|
535
|
-
evictions: SharedACSCacheStats.evictions,
|
|
536
|
-
cacheSize: this.contractsSet.size,
|
|
537
|
-
hitRate: hitRate.toFixed(2) + "%",
|
|
538
|
-
averageLookupTime: avgLookupTime.toFixed(3) + " ms",
|
|
539
|
-
cacheServeTime: SharedACSCacheStats.totalCacheServeTime.toFixed(3)
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
static createKey(party, templateId, interfaceId) {
|
|
543
|
-
return { party, templateId, interfaceId };
|
|
544
|
-
}
|
|
545
|
-
static keyToString(key, ledgerBaseUrl) {
|
|
546
|
-
return `${ledgerBaseUrl}_${key.party ? key.party : "ANY"}_T:${key.templateId ?? "()"}_I:${key.interfaceId ?? "()"}`;
|
|
547
|
-
}
|
|
548
|
-
findACSContainer(key) {
|
|
549
|
-
const keyStr = _ACSHelper.keyToString(key, this.apiInstance.baseUrl.href);
|
|
550
|
-
const start = performance.now();
|
|
551
|
-
const existing = this.contractsSet.get(keyStr);
|
|
552
|
-
const end = performance.now();
|
|
553
|
-
SharedACSCacheStats.totalLookupTime += end - start;
|
|
554
|
-
if (existing) {
|
|
555
|
-
SharedACSCacheStats.hits++;
|
|
556
|
-
this.logger.debug("cache hit");
|
|
557
|
-
return existing;
|
|
558
|
-
}
|
|
559
|
-
this.logger.debug("cache miss");
|
|
560
|
-
SharedACSCacheStats.misses++;
|
|
561
|
-
const newContainer = new ACSContainer(void 0, {
|
|
562
|
-
includeCreatedEventBlob: this.includeCreatedEventBlob
|
|
563
|
-
});
|
|
564
|
-
this.contractsSet.set(keyStr, newContainer);
|
|
565
|
-
return newContainer;
|
|
566
|
-
}
|
|
567
|
-
async updateSingleKey(offset, key) {
|
|
568
|
-
const start = performance.now();
|
|
569
|
-
const container = this.findACSContainer(key);
|
|
570
|
-
const result = await container.update(
|
|
571
|
-
offset,
|
|
572
|
-
key,
|
|
573
|
-
this.apiInstance,
|
|
574
|
-
this.wsSupport
|
|
575
|
-
);
|
|
576
|
-
const end = performance.now();
|
|
577
|
-
const keyStr = _ACSHelper.keyToString(key, this.apiInstance.baseUrl.href);
|
|
578
|
-
if (this.contractsSet.has(keyStr)) {
|
|
579
|
-
SharedACSCacheStats.totalCacheServeTime += end - start;
|
|
580
|
-
}
|
|
581
|
-
return result;
|
|
582
|
-
}
|
|
583
|
-
async queryAcsByKeys(offset, keys) {
|
|
584
|
-
const result = [];
|
|
585
|
-
for (const key of keys) {
|
|
586
|
-
const contracts = await this.updateSingleKey(offset, key);
|
|
587
|
-
result.push(...contracts);
|
|
588
|
-
}
|
|
589
|
-
return result;
|
|
590
|
-
}
|
|
591
|
-
async activeContractsForTemplates(offset, parties, templateIds) {
|
|
592
|
-
const keys = parties.flatMap(
|
|
593
|
-
(party) => templateIds.map(
|
|
594
|
-
(templateId) => _ACSHelper.createKey(party, templateId, void 0)
|
|
595
|
-
)
|
|
596
|
-
);
|
|
597
|
-
return this.queryAcsByKeys(offset, keys);
|
|
598
|
-
}
|
|
599
|
-
async activeContractsForInterfaces(offset, parties, interfaceIds) {
|
|
600
|
-
const keys = parties.flatMap(
|
|
601
|
-
(party) => interfaceIds.map(
|
|
602
|
-
(interfaceId) => _ACSHelper.createKey(party, void 0, interfaceId)
|
|
603
|
-
)
|
|
604
|
-
);
|
|
605
|
-
return this.queryAcsByKeys(offset, keys);
|
|
606
|
-
}
|
|
607
|
-
async activeContractsForTemplate(offset, partyFilter, templateId) {
|
|
608
|
-
return this.updateSingleKey(
|
|
609
|
-
offset,
|
|
610
|
-
_ACSHelper.createKey(partyFilter, templateId, void 0)
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
async activeContractsForInterface(offset, partyFilter, interfaceId) {
|
|
614
|
-
return this.updateSingleKey(
|
|
615
|
-
offset,
|
|
616
|
-
_ACSHelper.createKey(partyFilter, void 0, interfaceId)
|
|
617
|
-
);
|
|
618
|
-
}
|
|
619
|
-
};
|
|
620
|
-
|
|
621
144
|
// src/ledger-client.ts
|
|
622
145
|
var supportedVersions = supportedLedgerApiVersions;
|
|
623
146
|
var LedgerClient = class {
|
|
@@ -625,15 +148,13 @@ var LedgerClient = class {
|
|
|
625
148
|
baseUrl,
|
|
626
149
|
logger,
|
|
627
150
|
accessTokenProvider,
|
|
628
|
-
version
|
|
629
|
-
acsHelperOptions
|
|
151
|
+
version
|
|
630
152
|
}) {
|
|
631
153
|
// privately manage the active connected version and associated client codegen
|
|
632
154
|
__publicField(this, "clients");
|
|
633
155
|
__publicField(this, "clientVersion", "3.5");
|
|
634
156
|
__publicField(this, "initialized", false);
|
|
635
157
|
__publicField(this, "accessTokenProvider");
|
|
636
|
-
__publicField(this, "acsHelper");
|
|
637
158
|
__publicField(this, "logger");
|
|
638
159
|
__publicField(this, "synchronizerId");
|
|
639
160
|
__publicField(this, "baseUrl");
|
|
@@ -660,12 +181,6 @@ var LedgerClient = class {
|
|
|
660
181
|
};
|
|
661
182
|
this.clientVersion = version ?? this.clientVersion;
|
|
662
183
|
this.baseUrl = baseUrl;
|
|
663
|
-
this.acsHelper = new ACSHelper(
|
|
664
|
-
this,
|
|
665
|
-
logger,
|
|
666
|
-
acsHelperOptions,
|
|
667
|
-
SharedACSCache
|
|
668
|
-
);
|
|
669
184
|
}
|
|
670
185
|
get currentClient() {
|
|
671
186
|
return this.clients[this.clientVersion];
|
|
@@ -920,199 +435,6 @@ var LedgerClient = class {
|
|
|
920
435
|
);
|
|
921
436
|
return this.valueOrError(resp);
|
|
922
437
|
}
|
|
923
|
-
/*
|
|
924
|
-
if limit is provided, this function performs a one-time query. Automatically splits into multiple `/v2/updates` calls with `continueUntilCompletion` on.
|
|
925
|
-
if limit is omitted, results may be served from the ACS cache
|
|
926
|
-
current cache design doesn't support limiting queries because updates/deltas for the acs at offset x will be incorrect
|
|
927
|
-
TODO: expose query mode vs subscribe mode to call queryActiveContracts vs subscribeActiveContracts
|
|
928
|
-
*/
|
|
929
|
-
async activeContracts(options) {
|
|
930
|
-
const {
|
|
931
|
-
offset,
|
|
932
|
-
templateIds,
|
|
933
|
-
parties,
|
|
934
|
-
interfaceIds,
|
|
935
|
-
limit,
|
|
936
|
-
continueUntilCompletion
|
|
937
|
-
} = options;
|
|
938
|
-
if (continueUntilCompletion) {
|
|
939
|
-
const filter2 = this.buildActiveContractFilter(options);
|
|
940
|
-
return await this.fetchActiveContractsUntilComplete(
|
|
941
|
-
filter2,
|
|
942
|
-
limit ?? 200
|
|
943
|
-
);
|
|
944
|
-
}
|
|
945
|
-
const hasLimit = typeof limit === "number";
|
|
946
|
-
if (hasLimit) {
|
|
947
|
-
const filter2 = this.buildActiveContractFilter(options);
|
|
948
|
-
return await this.postWithRetry(
|
|
949
|
-
"/v2/state/active-contracts",
|
|
950
|
-
filter2,
|
|
951
|
-
defaultRetryableOptions,
|
|
952
|
-
{ query: { limit: limit.toString() } }
|
|
953
|
-
);
|
|
954
|
-
}
|
|
955
|
-
this.logger.debug(options, "options for active contracts");
|
|
956
|
-
const hasParties = Array.isArray(parties) && parties.length > 0;
|
|
957
|
-
if (templateIds?.length && hasParties) {
|
|
958
|
-
return this.acsHelper.activeContractsForTemplates(
|
|
959
|
-
offset,
|
|
960
|
-
parties,
|
|
961
|
-
templateIds
|
|
962
|
-
);
|
|
963
|
-
}
|
|
964
|
-
if (interfaceIds?.length && hasParties) {
|
|
965
|
-
return this.acsHelper.activeContractsForInterfaces(
|
|
966
|
-
offset,
|
|
967
|
-
parties,
|
|
968
|
-
interfaceIds
|
|
969
|
-
);
|
|
970
|
-
}
|
|
971
|
-
const filter = this.buildActiveContractFilter(options);
|
|
972
|
-
this.logger.debug("falling back to post request");
|
|
973
|
-
return await this.postWithRetry(
|
|
974
|
-
"/v2/state/active-contracts",
|
|
975
|
-
filter,
|
|
976
|
-
defaultRetryableOptions
|
|
977
|
-
);
|
|
978
|
-
}
|
|
979
|
-
/**
|
|
980
|
-
* Fetches active contracts by splitting requests into multiple `/v2/updates` calls.
|
|
981
|
-
* Should only be used when the number of contracts exceeds http-list-max-elements-limit (200 by default).
|
|
982
|
-
* For limits at or below http-list-max-elements-limit, use a single `/v2/state/active-contracts` call instead.
|
|
983
|
-
* @param activeContractsArgs The request parameters for active contracts query
|
|
984
|
-
* @returns A promise that resolves to an array of active contract responses
|
|
985
|
-
* @private
|
|
986
|
-
*/
|
|
987
|
-
async fetchActiveContractsUntilComplete(activeContractsArgs, limit) {
|
|
988
|
-
const ledgerEnd = await this.getWithRetry("/v2/state/ledger-end");
|
|
989
|
-
const bodyRequest = {
|
|
990
|
-
beginExclusive: 0,
|
|
991
|
-
endInclusive: ledgerEnd.offset,
|
|
992
|
-
verbose: false,
|
|
993
|
-
updateFormat: {}
|
|
994
|
-
};
|
|
995
|
-
if (!activeContractsArgs.filter)
|
|
996
|
-
bodyRequest.filter = activeContractsArgs.filter;
|
|
997
|
-
let currentOffset = 0;
|
|
998
|
-
const allContractsData = /* @__PURE__ */ new Map();
|
|
999
|
-
const exercisedContracts = /* @__PURE__ */ new Set();
|
|
1000
|
-
while (currentOffset < ledgerEnd.offset) {
|
|
1001
|
-
bodyRequest.beginExclusive = currentOffset;
|
|
1002
|
-
const results = (await this.postWithRetry(
|
|
1003
|
-
"/v2/updates",
|
|
1004
|
-
bodyRequest,
|
|
1005
|
-
defaultRetryableOptions,
|
|
1006
|
-
{
|
|
1007
|
-
query: { limit: limit.toString() }
|
|
1008
|
-
}
|
|
1009
|
-
)).filter(({ update }) => update && "Transaction" in update).map(({ update }) => {
|
|
1010
|
-
if (update && "Transaction" in update) {
|
|
1011
|
-
return update.Transaction.value;
|
|
1012
|
-
}
|
|
1013
|
-
throw new Error("Expected Transaction update");
|
|
1014
|
-
}).map((data) => {
|
|
1015
|
-
const exercisedEvents = data.events?.filter(
|
|
1016
|
-
(event) => !!event && "ExercisedEvent" in event
|
|
1017
|
-
).map(
|
|
1018
|
-
(event) => event.ExercisedEvent
|
|
1019
|
-
).filter((event) => !!event).filter((event) => !!event.consuming);
|
|
1020
|
-
const createdEvents = data.events?.filter((event) => !!event && "CreatedEvent" in event).map(
|
|
1021
|
-
(event) => event.CreatedEvent
|
|
1022
|
-
).filter((event) => !!event).filter(
|
|
1023
|
-
(event) => Object.keys(
|
|
1024
|
-
activeContractsArgs.filter?.filtersByParty ?? {}
|
|
1025
|
-
).includes(
|
|
1026
|
-
event.createArgument?.owner ?? ""
|
|
1027
|
-
)
|
|
1028
|
-
);
|
|
1029
|
-
exercisedEvents?.forEach((event) => {
|
|
1030
|
-
if (event.contractId)
|
|
1031
|
-
exercisedContracts.add(event.contractId);
|
|
1032
|
-
});
|
|
1033
|
-
createdEvents?.forEach((event) => {
|
|
1034
|
-
if (!event.contractId) return;
|
|
1035
|
-
allContractsData.set(event.contractId, {
|
|
1036
|
-
workflowId: data.workflowId,
|
|
1037
|
-
contractEntry: {
|
|
1038
|
-
JsActiveContract: {
|
|
1039
|
-
synchronizerId: data.synchronizerId,
|
|
1040
|
-
createdEvent: event,
|
|
1041
|
-
reassignmentCounter: data.offset
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
});
|
|
1045
|
-
});
|
|
1046
|
-
currentOffset = Math.max(currentOffset, data.offset);
|
|
1047
|
-
return true;
|
|
1048
|
-
});
|
|
1049
|
-
if (!results.length) currentOffset++;
|
|
1050
|
-
}
|
|
1051
|
-
exercisedContracts.forEach((cid) => {
|
|
1052
|
-
allContractsData.delete(cid);
|
|
1053
|
-
});
|
|
1054
|
-
return Array.from(allContractsData.values());
|
|
1055
|
-
}
|
|
1056
|
-
buildActiveContractFilter(options) {
|
|
1057
|
-
const filter = {
|
|
1058
|
-
eventFormat: {
|
|
1059
|
-
filtersByParty: {},
|
|
1060
|
-
verbose: false
|
|
1061
|
-
},
|
|
1062
|
-
activeAtOffset: options?.offset
|
|
1063
|
-
};
|
|
1064
|
-
const buildTemplateFilter = (templateIds) => {
|
|
1065
|
-
if (!templateIds) return [];
|
|
1066
|
-
return [
|
|
1067
|
-
{
|
|
1068
|
-
identifierFilter: {
|
|
1069
|
-
TemplateFilter: {
|
|
1070
|
-
value: {
|
|
1071
|
-
templateId: templateIds[0],
|
|
1072
|
-
includeCreatedEventBlob: true
|
|
1073
|
-
//TODO: figure out if this should be configurable
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
];
|
|
1079
|
-
};
|
|
1080
|
-
const buildInterfaceFilter = (interfaceIds) => {
|
|
1081
|
-
if (!interfaceIds) return [];
|
|
1082
|
-
return [
|
|
1083
|
-
{
|
|
1084
|
-
identifierFilter: {
|
|
1085
|
-
InterfaceFilter: {
|
|
1086
|
-
value: {
|
|
1087
|
-
interfaceId: interfaceIds[0],
|
|
1088
|
-
includeCreatedEventBlob: true,
|
|
1089
|
-
//TODO: figure out if this should be configurable
|
|
1090
|
-
includeInterfaceView: true
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
];
|
|
1096
|
-
};
|
|
1097
|
-
this.logger.info(options, "active contract query options");
|
|
1098
|
-
if (options?.filterByParty && options.parties && options.parties.length > 0) {
|
|
1099
|
-
const cumulativeFilter = options?.templateIds && !options?.interfaceIds ? buildTemplateFilter(options.templateIds) : options?.interfaceIds && !options?.templateIds ? buildInterfaceFilter(options.interfaceIds) : [];
|
|
1100
|
-
for (const party of options.parties) {
|
|
1101
|
-
filter.filter.filtersByParty[party] = {
|
|
1102
|
-
cumulative: cumulativeFilter
|
|
1103
|
-
};
|
|
1104
|
-
}
|
|
1105
|
-
} else if (options?.templateIds) {
|
|
1106
|
-
filter.filter.filtersForAnyParty = {
|
|
1107
|
-
cumulative: buildTemplateFilter(options.templateIds)
|
|
1108
|
-
};
|
|
1109
|
-
} else if (options?.interfaceIds) {
|
|
1110
|
-
filter.filter.filtersForAnyParty = {
|
|
1111
|
-
cumulative: buildInterfaceFilter(options.templateIds)
|
|
1112
|
-
};
|
|
1113
|
-
}
|
|
1114
|
-
return filter;
|
|
1115
|
-
}
|
|
1116
438
|
// Retrieve an (arbitrary) synchronizer id from the validator.
|
|
1117
439
|
// This synchronizer id is cached for the remainder of this object's life.
|
|
1118
440
|
async getSynchronizerId() {
|
|
@@ -1169,9 +491,6 @@ var LedgerClient = class {
|
|
|
1169
491
|
throw asJsCantonError(e);
|
|
1170
492
|
});
|
|
1171
493
|
}
|
|
1172
|
-
getCacheStats() {
|
|
1173
|
-
return this.acsHelper.getCacheStats();
|
|
1174
|
-
}
|
|
1175
494
|
async patch(path, body, params, additionalOptions) {
|
|
1176
495
|
const options = { body, params, ...additionalOptions };
|
|
1177
496
|
const resp = await this.currentClient.PATCH(path, options);
|
|
@@ -1197,45 +516,6 @@ var LedgerClient = class {
|
|
|
1197
516
|
}
|
|
1198
517
|
};
|
|
1199
518
|
|
|
1200
|
-
|
|
1201
|
-
var INITIAL_BACKOFF_MS = 1e3;
|
|
1202
|
-
var MAX_BACKOFF_MS = 10 * 60 * 1e3;
|
|
1203
|
-
var WSSupportWithBackOffSwitch = class {
|
|
1204
|
-
constructor(extractProtocols, baseUrl) {
|
|
1205
|
-
this.extractProtocols = extractProtocols;
|
|
1206
|
-
this.baseUrl = baseUrl;
|
|
1207
|
-
__publicField(this, "backOffUntil", null);
|
|
1208
|
-
__publicField(this, "backOffDurationMs", INITIAL_BACKOFF_MS);
|
|
1209
|
-
}
|
|
1210
|
-
enabled() {
|
|
1211
|
-
if (this.backOffUntil === null) {
|
|
1212
|
-
return true;
|
|
1213
|
-
}
|
|
1214
|
-
const now = Date.now();
|
|
1215
|
-
if (now >= this.backOffUntil) {
|
|
1216
|
-
this.backOffUntil = null;
|
|
1217
|
-
return true;
|
|
1218
|
-
}
|
|
1219
|
-
return false;
|
|
1220
|
-
}
|
|
1221
|
-
reportError(error) {
|
|
1222
|
-
console.error("WebSocket error reported:", error);
|
|
1223
|
-
const now = Date.now();
|
|
1224
|
-
if (this.backOffUntil !== null && now - this.backOffUntil < 500) {
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
this.backOffUntil = Date.now() + this.backOffDurationMs;
|
|
1228
|
-
this.backOffDurationMs = Math.min(
|
|
1229
|
-
this.backOffDurationMs * 1.6,
|
|
1230
|
-
MAX_BACKOFF_MS
|
|
1231
|
-
);
|
|
1232
|
-
}
|
|
1233
|
-
reportSuccess() {
|
|
1234
|
-
this.backOffUntil = null;
|
|
1235
|
-
this.backOffDurationMs = INITIAL_BACKOFF_MS;
|
|
1236
|
-
}
|
|
1237
|
-
};
|
|
1238
|
-
|
|
1239
|
-
export { ACSContainer, ACS_UPDATE_CONFIG, LedgerClient, WSSupportWithBackOffSwitch, asJsCantonError, awaitCompletion, defaultRetryableOptions, isJsCantonError, promiseWithTimeout, supportedVersions };
|
|
519
|
+
export { LedgerClient, asJsCantonError, awaitCompletion, defaultRetryableOptions, isJsCantonError, promiseWithTimeout, supportedVersions };
|
|
1240
520
|
//# sourceMappingURL=index.js.map
|
|
1241
521
|
//# sourceMappingURL=index.js.map
|