envio 3.3.0-alpha.0 → 3.3.0-alpha.10

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.
Files changed (140) hide show
  1. package/evm.schema.json +10 -0
  2. package/index.d.ts +45 -25
  3. package/package.json +7 -7
  4. package/src/Address.res +5 -2
  5. package/src/Address.res.mjs +3 -1
  6. package/src/Api.res +1 -1
  7. package/src/Api.res.mjs +1 -1
  8. package/src/Batch.res +0 -6
  9. package/src/Batch.res.mjs +1 -12
  10. package/src/BatchProcessing.res +7 -12
  11. package/src/BatchProcessing.res.mjs +8 -7
  12. package/src/Bin.res +3 -0
  13. package/src/Bin.res.mjs +4 -0
  14. package/src/ChainFetching.res +48 -16
  15. package/src/ChainFetching.res.mjs +36 -17
  16. package/src/ChainMetadata.res +1 -11
  17. package/src/ChainMetadata.res.mjs +1 -10
  18. package/src/ChainState.res +628 -149
  19. package/src/ChainState.res.mjs +440 -102
  20. package/src/ChainState.resi +74 -4
  21. package/src/Config.res +61 -33
  22. package/src/Config.res.mjs +50 -10
  23. package/src/ContractRegisterContext.res +2 -12
  24. package/src/ContractRegisterContext.res.mjs +3 -5
  25. package/src/Core.res +14 -3
  26. package/src/CrossChainState.res +142 -93
  27. package/src/CrossChainState.res.mjs +88 -60
  28. package/src/CrossChainState.resi +2 -10
  29. package/src/Ecosystem.res +5 -6
  30. package/src/Ecosystem.res.mjs +3 -3
  31. package/src/Env.res +0 -7
  32. package/src/Env.res.mjs +0 -6
  33. package/src/Envio.res +17 -11
  34. package/src/EnvioGlobal.res +53 -0
  35. package/src/EnvioGlobal.res.mjs +31 -0
  36. package/src/EventConfigBuilder.res +177 -88
  37. package/src/EventConfigBuilder.res.mjs +99 -42
  38. package/src/EventProcessing.res +74 -28
  39. package/src/EventProcessing.res.mjs +55 -29
  40. package/src/ExitOnCaughtUp.res +10 -2
  41. package/src/ExitOnCaughtUp.res.mjs +10 -1
  42. package/src/FetchState.res +558 -300
  43. package/src/FetchState.res.mjs +437 -423
  44. package/src/HandlerLoader.res +8 -104
  45. package/src/HandlerLoader.res.mjs +2 -81
  46. package/src/HandlerRegister.res +518 -144
  47. package/src/HandlerRegister.res.mjs +285 -133
  48. package/src/HandlerRegister.resi +24 -8
  49. package/src/IndexerLoop.res +2 -3
  50. package/src/IndexerLoop.res.mjs +1 -1
  51. package/src/IndexerState.res +10 -11
  52. package/src/IndexerState.res.mjs +14 -11
  53. package/src/IndexerState.resi +2 -5
  54. package/src/IndexingAddresses.res +108 -0
  55. package/src/IndexingAddresses.res.mjs +101 -0
  56. package/src/IndexingAddresses.resi +34 -0
  57. package/src/Internal.res +155 -46
  58. package/src/Internal.res.mjs +30 -2
  59. package/src/LoadLayer.res +5 -5
  60. package/src/LoadLayer.res.mjs +6 -6
  61. package/src/LogSelection.res +102 -165
  62. package/src/LogSelection.res.mjs +101 -116
  63. package/src/Main.res +71 -236
  64. package/src/Main.res.mjs +60 -145
  65. package/src/Metrics.res +74 -0
  66. package/src/Metrics.res.mjs +75 -0
  67. package/src/PgStorage.res +4 -4
  68. package/src/PgStorage.res.mjs +5 -5
  69. package/src/Prometheus.res +10 -74
  70. package/src/Prometheus.res.mjs +124 -186
  71. package/src/PruneStaleHistory.res +2 -2
  72. package/src/PruneStaleHistory.res.mjs +3 -3
  73. package/src/RawEvent.res +7 -2
  74. package/src/RawEvent.res.mjs +4 -4
  75. package/src/Rollback.res +3 -3
  76. package/src/Rollback.res.mjs +4 -4
  77. package/src/RollbackCommit.res +4 -1
  78. package/src/RollbackCommit.res.mjs +3 -2
  79. package/src/SimulateDeadInputTracker.res +85 -0
  80. package/src/SimulateDeadInputTracker.res.mjs +73 -0
  81. package/src/SimulateDeadInputTracker.resi +12 -0
  82. package/src/SimulateItems.res +91 -23
  83. package/src/SimulateItems.res.mjs +55 -32
  84. package/src/TestIndexer.res +54 -40
  85. package/src/TestIndexer.res.mjs +37 -30
  86. package/src/bindings/Performance.res +7 -0
  87. package/src/bindings/Performance.res.mjs +21 -0
  88. package/src/bindings/Performance.resi +7 -0
  89. package/src/bindings/Viem.res +0 -41
  90. package/src/bindings/Viem.res.mjs +1 -43
  91. package/src/sources/BlockStore.res +46 -0
  92. package/src/sources/BlockStore.res.mjs +24 -0
  93. package/src/sources/EventRouter.res +36 -33
  94. package/src/sources/EventRouter.res.mjs +13 -14
  95. package/src/sources/Evm.res +83 -72
  96. package/src/sources/Evm.res.mjs +53 -69
  97. package/src/sources/EvmChain.res +18 -20
  98. package/src/sources/EvmChain.res.mjs +15 -15
  99. package/src/sources/EvmRpcClient.res +74 -5
  100. package/src/sources/EvmRpcClient.res.mjs +13 -4
  101. package/src/sources/FieldMask.res +39 -0
  102. package/src/sources/FieldMask.res.mjs +42 -0
  103. package/src/sources/Fuel.res +9 -6
  104. package/src/sources/Fuel.res.mjs +5 -10
  105. package/src/sources/HyperFuelSource.res +46 -50
  106. package/src/sources/HyperFuelSource.res.mjs +64 -54
  107. package/src/sources/HyperSync.res +39 -7
  108. package/src/sources/HyperSync.res.mjs +57 -37
  109. package/src/sources/HyperSync.resi +12 -2
  110. package/src/sources/HyperSyncClient.res +22 -111
  111. package/src/sources/HyperSyncClient.res.mjs +2 -30
  112. package/src/sources/HyperSyncHeightStream.res +1 -8
  113. package/src/sources/HyperSyncHeightStream.res.mjs +0 -2
  114. package/src/sources/HyperSyncSource.res +94 -107
  115. package/src/sources/HyperSyncSource.res.mjs +82 -86
  116. package/src/sources/Rpc.res +15 -47
  117. package/src/sources/Rpc.res.mjs +25 -56
  118. package/src/sources/RpcSource.res +320 -467
  119. package/src/sources/RpcSource.res.mjs +268 -385
  120. package/src/sources/RpcWebSocketHeightStream.res +0 -5
  121. package/src/sources/RpcWebSocketHeightStream.res.mjs +0 -2
  122. package/src/sources/SimulateSource.res +10 -4
  123. package/src/sources/SimulateSource.res.mjs +16 -6
  124. package/src/sources/Source.res +34 -6
  125. package/src/sources/SourceManager.res +86 -27
  126. package/src/sources/SourceManager.res.mjs +84 -47
  127. package/src/sources/SourceManager.resi +12 -3
  128. package/src/sources/Svm.res +31 -16
  129. package/src/sources/Svm.res.mjs +36 -13
  130. package/src/sources/SvmHyperSyncClient.res +5 -32
  131. package/src/sources/SvmHyperSyncSource.res +159 -134
  132. package/src/sources/SvmHyperSyncSource.res.mjs +164 -129
  133. package/src/sources/TransactionStore.res +49 -0
  134. package/src/sources/TransactionStore.res.mjs +30 -0
  135. package/src/tui/Tui.res +13 -16
  136. package/src/tui/Tui.res.mjs +12 -14
  137. package/svm.schema.json +49 -37
  138. package/src/bindings/Hrtime.res +0 -58
  139. package/src/bindings/Hrtime.res.mjs +0 -90
  140. package/src/bindings/Hrtime.resi +0 -30
@@ -5,13 +5,26 @@ import * as Js_math from "@rescript/runtime/lib/es6/Js_math.js";
5
5
  import * as Logging from "./Logging.res.mjs";
6
6
  import * as Prometheus from "./Prometheus.res.mjs";
7
7
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
8
+ import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
8
9
  import * as Primitive_int from "@rescript/runtime/lib/es6/Primitive_int.js";
9
10
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
10
11
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
12
+ import * as Primitive_float from "@rescript/runtime/lib/es6/Primitive_float.js";
11
13
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
14
+ import * as IndexingAddresses from "./IndexingAddresses.res.mjs";
12
15
 
13
- function deriveEffectiveStartBlock(registrationBlock, contractStartBlock) {
14
- return Primitive_int.max(Primitive_int.max(registrationBlock, 0), Stdlib_Option.getOr(contractStartBlock, 0));
16
+ let deriveContractNameByAddress = Utils.$$WeakMap.memoize(addressesByContractName => {
17
+ let result = {};
18
+ Utils.Dict.forEachWithKey(addressesByContractName, (addresses, contractName) => {
19
+ for (let i = 0, i_finish = addresses.length; i < i_finish; ++i) {
20
+ result[addresses[i]] = contractName;
21
+ }
22
+ });
23
+ return result;
24
+ });
25
+
26
+ function densityItemsTarget(density, fromBlock, toBlock, chainTargetBlock) {
27
+ return Primitive_int.max(1, Math.ceil(((Stdlib_Option.getOr(toBlock, chainTargetBlock) - fromBlock | 0) + 1 | 0) * density) | 0);
15
28
  }
16
29
 
17
30
  function getMinHistoryRange(p) {
@@ -22,6 +35,14 @@ function getMinHistoryRange(p) {
22
35
  }
23
36
  }
24
37
 
38
+ function getTrustedDensity(p) {
39
+ let match = p.prevQueryRange;
40
+ let match$1 = p.prevPrevQueryRange;
41
+ if (match !== 0 && match$1 !== 0) {
42
+ return p.prevRangeSize / match;
43
+ }
44
+ }
45
+
25
46
  function getMinQueryRange(partitions) {
26
47
  let min = 0;
27
48
  for (let i = 0, i_finish = partitions.length; i < i_finish; ++i) {
@@ -69,6 +90,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
69
90
  mutPendingQueries: p1.mutPendingQueries,
70
91
  prevQueryRange: p1.prevQueryRange,
71
92
  prevPrevQueryRange: p1.prevPrevQueryRange,
93
+ prevRangeSize: p1.prevRangeSize,
72
94
  latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
73
95
  });
74
96
  completed.push({
@@ -81,6 +103,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
81
103
  mutPendingQueries: p2.mutPendingQueries,
82
104
  prevQueryRange: p2.prevQueryRange,
83
105
  prevPrevQueryRange: p2.prevPrevQueryRange,
106
+ prevRangeSize: p2.prevRangeSize,
84
107
  latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
85
108
  });
86
109
  let newId = nextPartitionIndexRef.contents.toString();
@@ -89,6 +112,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
89
112
  p1,
90
113
  p2
91
114
  ]);
115
+ let inheritedDensity = Stdlib_Option.getOr(getTrustedDensity(p1), 0) + Stdlib_Option.getOr(getTrustedDensity(p2), 0);
92
116
  continuingBase = {
93
117
  id: newId,
94
118
  latestFetchedBlock: {
@@ -102,6 +126,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
102
126
  mutPendingQueries: [],
103
127
  prevQueryRange: minRange,
104
128
  prevPrevQueryRange: minRange,
129
+ prevRangeSize: Math.ceil(inheritedDensity * minRange) | 0,
105
130
  latestBlockRangeUpdateBlock: 0
106
131
  };
107
132
  } else {
@@ -115,6 +140,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
115
140
  mutPendingQueries: p1.mutPendingQueries,
116
141
  prevQueryRange: p1.prevQueryRange,
117
142
  prevPrevQueryRange: p1.prevPrevQueryRange,
143
+ prevRangeSize: p1.prevRangeSize,
118
144
  latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
119
145
  });
120
146
  continuingBase = p2;
@@ -130,6 +156,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
130
156
  mutPendingQueries: p2.mutPendingQueries,
131
157
  prevQueryRange: p2.prevQueryRange,
132
158
  prevPrevQueryRange: p2.prevPrevQueryRange,
159
+ prevRangeSize: p2.prevRangeSize,
133
160
  latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
134
161
  });
135
162
  continuingBase = p1;
@@ -153,6 +180,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
153
180
  mutPendingQueries: continuingBase.mutPendingQueries,
154
181
  prevQueryRange: continuingBase.prevQueryRange,
155
182
  prevPrevQueryRange: continuingBase.prevPrevQueryRange,
183
+ prevRangeSize: continuingBase.prevRangeSize,
156
184
  latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
157
185
  });
158
186
  let restId = nextPartitionIndexRef.contents.toString();
@@ -167,6 +195,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
167
195
  mutPendingQueries: [],
168
196
  prevQueryRange: continuingBase.prevQueryRange,
169
197
  prevPrevQueryRange: continuingBase.prevPrevQueryRange,
198
+ prevRangeSize: continuingBase.prevRangeSize,
170
199
  latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
171
200
  });
172
201
  return completed;
@@ -183,6 +212,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
183
212
  mutPendingQueries: continuingBase.mutPendingQueries,
184
213
  prevQueryRange: continuingBase.prevQueryRange,
185
214
  prevPrevQueryRange: continuingBase.prevPrevQueryRange,
215
+ prevRangeSize: continuingBase.prevRangeSize,
186
216
  latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
187
217
  });
188
218
  return completed;
@@ -320,7 +350,7 @@ function getPendingQueryOrThrow(p, fromBlock) {
320
350
  }
321
351
  }
322
352
 
323
- function handleQueryResponse(optimizedPartitions, query, knownHeight, latestFetchedBlock) {
353
+ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount, latestFetchedBlock) {
324
354
  let partitionId = query.partitionId;
325
355
  let p = optimizedPartitions.entities[partitionId];
326
356
  let p$1 = p !== undefined ? p : Stdlib_JsError.throwWithMessage(`Unexpected case: Couldn't find partition ` + partitionId);
@@ -333,11 +363,11 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, latestFetc
333
363
  let queryToBlock = query.toBlock;
334
364
  let tmp;
335
365
  if (queryToBlock !== undefined) {
336
- if (latestFetchedBlock.blockNumber >= queryToBlock) {
366
+ if (latestFetchedBlock.blockNumber < queryToBlock) {
367
+ tmp = itemsCount < query.itemsTarget;
368
+ } else {
337
369
  let minHistoryRange = getMinHistoryRange(p$1);
338
370
  tmp = minHistoryRange !== undefined ? ((queryToBlock - query.fromBlock | 0) + 1 | 0) >= minHistoryRange : false;
339
- } else {
340
- tmp = true;
341
371
  }
342
372
  } else {
343
373
  tmp = latestFetchedBlock.blockNumber < (knownHeight - 10 | 0);
@@ -346,6 +376,7 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, latestFetc
346
376
  }
347
377
  let updatedPrevQueryRange = shouldUpdateBlockRange ? blockRange : p$1.prevQueryRange;
348
378
  let updatedPrevPrevQueryRange = shouldUpdateBlockRange ? p$1.prevQueryRange : p$1.prevPrevQueryRange;
379
+ let updatedPrevRangeSize = shouldUpdateBlockRange ? itemsCount : p$1.prevRangeSize;
349
380
  let mutPendingQueries = p$1.mutPendingQueries;
350
381
  let latestFetchedBlock$1 = p$1.latestFetchedBlock;
351
382
  while ((() => {
@@ -382,6 +413,7 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, latestFetc
382
413
  mutPendingQueries: updatedMainPartition_mutPendingQueries,
383
414
  prevQueryRange: updatedPrevQueryRange,
384
415
  prevPrevQueryRange: updatedPrevPrevQueryRange,
416
+ prevRangeSize: updatedPrevRangeSize,
385
417
  latestBlockRangeUpdateBlock: updatedMainPartition_latestBlockRangeUpdateBlock
386
418
  };
387
419
  mutEntities[p$1.id] = updatedMainPartition;
@@ -436,6 +468,22 @@ function bufferBlock(param) {
436
468
  }
437
469
  }
438
470
 
471
+ function bufferReadyCount(fetchState) {
472
+ let frontier = bufferBlockNumber(fetchState);
473
+ let buffer = fetchState.buffer;
474
+ let lo = 0;
475
+ let hi = buffer.length;
476
+ while (lo < hi) {
477
+ let mid = (lo + hi | 0) / 2 | 0;
478
+ if (buffer[mid].blockNumber <= frontier) {
479
+ lo = mid + 1 | 0;
480
+ } else {
481
+ hi = mid;
482
+ }
483
+ };
484
+ return lo;
485
+ }
486
+
439
487
  function compareBufferItem(a, b) {
440
488
  let blockOrdering = Primitive_int.compare(a.blockNumber, b.blockNumber);
441
489
  if (blockOrdering === 0) {
@@ -445,31 +493,27 @@ function compareBufferItem(a, b) {
445
493
  }
446
494
  }
447
495
 
448
- function numAddresses(fetchState) {
449
- return Utils.Dict.size(fetchState.indexingAddresses);
450
- }
451
-
452
- function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
496
+ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
453
497
  let newItemsCounter = 0;
454
498
  let latestOnBlockBlockNumber = fromBlock;
455
499
  while (latestOnBlockBlockNumber < maxBlockNumber && newItemsCounter <= maxOnBlockBufferSize) {
456
500
  let blockNumber = latestOnBlockBlockNumber + 1 | 0;
457
501
  latestOnBlockBlockNumber = blockNumber;
458
- for (let configIdx = 0, configIdx_finish = onBlockConfigs.length; configIdx < configIdx_finish; ++configIdx) {
459
- let onBlockConfig = onBlockConfigs[configIdx];
460
- let startBlock = onBlockConfig.startBlock;
502
+ for (let configIdx = 0, configIdx_finish = onBlockRegistrations.length; configIdx < configIdx_finish; ++configIdx) {
503
+ let onBlockRegistration = onBlockRegistrations[configIdx];
504
+ let startBlock = onBlockRegistration.startBlock;
461
505
  let handlerStartBlock = startBlock !== undefined ? startBlock : indexerStartBlock;
462
506
  let tmp = false;
463
507
  if (blockNumber >= handlerStartBlock) {
464
- let endBlock = onBlockConfig.endBlock;
508
+ let endBlock = onBlockRegistration.endBlock;
465
509
  tmp = endBlock !== undefined ? blockNumber <= endBlock : true;
466
510
  }
467
- if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockConfig.interval) === 0) {
511
+ if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockRegistration.interval) === 0) {
468
512
  mutItems.push({
469
513
  kind: 1,
470
- onBlockConfig: onBlockConfig,
514
+ onBlockRegistration: onBlockRegistration,
471
515
  blockNumber: blockNumber,
472
- logIndex: 16777216 + onBlockConfig.index | 0
516
+ logIndex: 16777216 + onBlockRegistration.index | 0
473
517
  });
474
518
  newItemsCounter = newItemsCounter + 1 | 0;
475
519
  }
@@ -478,15 +522,14 @@ function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlo
478
522
  return latestOnBlockBlockNumber;
479
523
  }
480
524
 
481
- function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt, mutItems, blockLagOpt, knownHeightOpt) {
525
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOpt, knownHeightOpt) {
482
526
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
483
- let indexingAddresses = indexingAddressesOpt !== undefined ? indexingAddressesOpt : fetchState.indexingAddresses;
484
527
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
485
528
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
486
529
  let mutItemsRef = mutItems;
487
- let onBlockConfigs = fetchState.onBlockConfigs;
530
+ let onBlockRegistrations = fetchState.onBlockRegistrations;
488
531
  let latestOnBlockBlockNumber;
489
- if (onBlockConfigs.length !== 0) {
532
+ if (onBlockRegistrations.length !== 0) {
490
533
  let mutItems$1 = mutItemsRef;
491
534
  let item = (
492
535
  mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
@@ -502,7 +545,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
502
545
  let mutItems$2 = mutItemsRef;
503
546
  let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
504
547
  mutItemsRef = mutItems$3;
505
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockConfigs, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
548
+ latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
506
549
  } else {
507
550
  latestOnBlockBlockNumber = knownHeight;
508
551
  }
@@ -514,30 +557,26 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
514
557
  let updatedFetchState_chainId = fetchState.chainId;
515
558
  let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
516
559
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
517
- let updatedFetchState_onBlockConfigs = fetchState.onBlockConfigs;
560
+ let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
518
561
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
519
562
  let updatedFetchState = {
520
563
  optimizedPartitions: optimizedPartitions,
521
564
  startBlock: updatedFetchState_startBlock,
522
565
  endBlock: updatedFetchState_endBlock,
523
566
  normalSelection: updatedFetchState_normalSelection,
524
- indexingAddresses: indexingAddresses,
525
567
  contractConfigs: updatedFetchState_contractConfigs,
526
568
  chainId: updatedFetchState_chainId,
527
569
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
528
570
  blockLag: blockLag,
529
571
  buffer: updatedFetchState_buffer,
530
572
  maxOnBlockBufferSize: updatedFetchState_maxOnBlockBufferSize,
531
- onBlockConfigs: updatedFetchState_onBlockConfigs,
573
+ onBlockRegistrations: updatedFetchState_onBlockRegistrations,
532
574
  knownHeight: knownHeight,
533
575
  firstEventBlock: updatedFetchState_firstEventBlock
534
576
  };
535
577
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, fetchState.chainId);
536
578
  Prometheus.IndexingBufferSize.set(updatedFetchState_buffer.length, fetchState.chainId);
537
579
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(updatedFetchState), fetchState.chainId);
538
- if (indexingAddresses !== fetchState.indexingAddresses) {
539
- Prometheus.IndexingAddresses.set(Utils.Dict.size(indexingAddresses), fetchState.chainId);
540
- }
541
580
  return updatedFetchState;
542
581
  }
543
582
 
@@ -628,6 +667,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
628
667
  mutPendingQueries: [],
629
668
  prevQueryRange: 0,
630
669
  prevPrevQueryRange: 0,
670
+ prevRangeSize: 0,
631
671
  latestBlockRangeUpdateBlock: 0
632
672
  });
633
673
  nextPartitionIndexRef = nextPartitionIndexRef + 1 | 0;
@@ -678,6 +718,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
678
718
  mutPendingQueries: currentP.mutPendingQueries,
679
719
  prevQueryRange: currentP.prevQueryRange,
680
720
  prevPrevQueryRange: currentP.prevPrevQueryRange,
721
+ prevRangeSize: currentP.prevRangeSize,
681
722
  latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
682
723
  });
683
724
  currentPRef = {
@@ -690,6 +731,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
690
731
  mutPendingQueries: nextP.mutPendingQueries,
691
732
  prevQueryRange: nextP.prevQueryRange,
692
733
  prevPrevQueryRange: nextP.prevPrevQueryRange,
734
+ prevRangeSize: nextP.prevRangeSize,
693
735
  latestBlockRangeUpdateBlock: nextP.latestBlockRangeUpdateBlock
694
736
  };
695
737
  } else {
@@ -703,6 +745,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
703
745
  mutPendingQueries: currentP.mutPendingQueries,
704
746
  prevQueryRange: currentP.prevQueryRange,
705
747
  prevPrevQueryRange: currentP.prevPrevQueryRange,
748
+ prevRangeSize: currentP.prevRangeSize,
706
749
  latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
707
750
  };
708
751
  }
@@ -715,11 +758,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
715
758
  return make(existingPartitions.concat(mergedPartitions), maxAddrInPartition, nextPartitionIndexRef, dynamicContracts);
716
759
  }
717
760
 
718
- function registerDynamicContracts(fetchState, items) {
719
- if (Utils.$$Array.isEmpty(fetchState.normalSelection.eventConfigs)) {
761
+ function registerDynamicContracts(fetchState, indexingAddresses, items) {
762
+ if (Utils.$$Array.isEmpty(fetchState.normalSelection.onEventRegistrations)) {
720
763
  Stdlib_JsError.throwWithMessage("Invalid configuration. No events to fetch for the dynamic contract registration.");
721
764
  }
722
- let indexingAddresses = fetchState.indexingAddresses;
723
765
  let registeringContractsByContract = {};
724
766
  let earliestRegisteringEventBlockNumber = Infinity;
725
767
  let noEventsAddresses = {};
@@ -737,14 +779,14 @@ function registerDynamicContracts(fetchState, items) {
737
779
  let dcWithStartBlock_address = dc.address;
738
780
  let dcWithStartBlock_contractName = dc.contractName;
739
781
  let dcWithStartBlock_registrationBlock = dc.registrationBlock;
740
- let dcWithStartBlock_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
782
+ let dcWithStartBlock_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
741
783
  let dcWithStartBlock = {
742
784
  address: dcWithStartBlock_address,
743
785
  contractName: dcWithStartBlock_contractName,
744
786
  registrationBlock: dcWithStartBlock_registrationBlock,
745
787
  effectiveStartBlock: dcWithStartBlock_effectiveStartBlock
746
788
  };
747
- let existingContract = indexingAddresses[dc.address];
789
+ let existingContract = IndexingAddresses.get(indexingAddresses, dc.address);
748
790
  if (existingContract !== undefined) {
749
791
  if (existingContract.contractName !== dc.contractName) {
750
792
  warnDifferentContractType(fetchState, existingContract, dcWithStartBlock);
@@ -775,14 +817,14 @@ function registerDynamicContracts(fetchState, items) {
775
817
  let dcAsIndexingAddress_address = dc.address;
776
818
  let dcAsIndexingAddress_contractName = dc.contractName;
777
819
  let dcAsIndexingAddress_registrationBlock = dc.registrationBlock;
778
- let dcAsIndexingAddress_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, undefined);
820
+ let dcAsIndexingAddress_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, undefined);
779
821
  let dcAsIndexingAddress = {
780
822
  address: dcAsIndexingAddress_address,
781
823
  contractName: dcAsIndexingAddress_contractName,
782
824
  registrationBlock: dcAsIndexingAddress_registrationBlock,
783
825
  effectiveStartBlock: dcAsIndexingAddress_effectiveStartBlock
784
826
  };
785
- let existingContract$1 = indexingAddresses[dc.address];
827
+ let existingContract$1 = IndexingAddresses.get(indexingAddresses, dc.address);
786
828
  if (existingContract$1 !== undefined) {
787
829
  if (existingContract$1.contractName !== dc.contractName) {
788
830
  warnDifferentContractType(fetchState, existingContract$1, dcAsIndexingAddress);
@@ -817,98 +859,100 @@ function registerDynamicContracts(fetchState, items) {
817
859
  }
818
860
  let dcContractNamesToStore = Object.keys(registeringContractsByContract);
819
861
  let hasNoEventsUpdates = !Utils.Dict.isEmpty(noEventsAddresses);
820
- if (dcContractNamesToStore.length !== 0) {
821
- let newPartitions = [];
822
- let newIndexingAddresses = Utils.Dict.shallowCopy(indexingAddresses);
823
- let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
824
- let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
825
- for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
826
- let contractName = dcContractNamesToStore[idx$1];
827
- if (!dynamicContractsRef.has(contractName)) {
828
- dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
829
- for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
830
- let p = mutExistingPartitions[idx$2];
831
- let addresses = p.addressesByContractName[contractName];
832
- if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
833
- let allPartitionContractNames = Object.keys(p.addressesByContractName);
834
- if (allPartitionContractNames.length !== 1) {
835
- let isFetching = p.mutPendingQueries.length !== 0;
836
- if (!isFetching) {
837
- let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
838
- let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
839
- Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
840
- mutExistingPartitions[idx$2] = {
841
- id: p.id,
842
- latestFetchedBlock: p.latestFetchedBlock,
843
- selection: p.selection,
844
- addressesByContractName: restAddressesByContractName,
845
- mergeBlock: p.mergeBlock,
846
- dynamicContract: p.dynamicContract,
847
- mutPendingQueries: p.mutPendingQueries,
848
- prevQueryRange: p.prevQueryRange,
849
- prevPrevQueryRange: p.prevPrevQueryRange,
850
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
851
- };
852
- let addressesByContractName = {};
853
- addressesByContractName[contractName] = addresses;
854
- newPartitions.push({
855
- id: newPartitionId,
856
- latestFetchedBlock: p.latestFetchedBlock,
857
- selection: fetchState.normalSelection,
858
- addressesByContractName: addressesByContractName,
859
- mergeBlock: undefined,
860
- dynamicContract: contractName,
861
- mutPendingQueries: p.mutPendingQueries,
862
- prevQueryRange: p.prevQueryRange,
863
- prevPrevQueryRange: p.prevPrevQueryRange,
864
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
865
- });
866
- }
867
- } else {
862
+ if (dcContractNamesToStore.length === 0) {
863
+ if (hasNoEventsUpdates) {
864
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
865
+ return fetchState;
866
+ } else {
867
+ return fetchState;
868
+ }
869
+ }
870
+ let newPartitions = [];
871
+ let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
872
+ let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
873
+ for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
874
+ let contractName = dcContractNamesToStore[idx$1];
875
+ if (!dynamicContractsRef.has(contractName)) {
876
+ dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
877
+ for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
878
+ let p = mutExistingPartitions[idx$2];
879
+ let addresses = p.addressesByContractName[contractName];
880
+ if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
881
+ let allPartitionContractNames = Object.keys(p.addressesByContractName);
882
+ if (allPartitionContractNames.length !== 1) {
883
+ let isFetching = p.mutPendingQueries.length !== 0;
884
+ if (!isFetching) {
885
+ let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
886
+ let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
887
+ Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
868
888
  mutExistingPartitions[idx$2] = {
869
889
  id: p.id,
870
890
  latestFetchedBlock: p.latestFetchedBlock,
871
891
  selection: p.selection,
872
- addressesByContractName: p.addressesByContractName,
892
+ addressesByContractName: restAddressesByContractName,
873
893
  mergeBlock: p.mergeBlock,
874
- dynamicContract: contractName,
894
+ dynamicContract: p.dynamicContract,
875
895
  mutPendingQueries: p.mutPendingQueries,
876
896
  prevQueryRange: p.prevQueryRange,
877
897
  prevPrevQueryRange: p.prevPrevQueryRange,
898
+ prevRangeSize: p.prevRangeSize,
878
899
  latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
879
900
  };
901
+ let addressesByContractName = {};
902
+ addressesByContractName[contractName] = addresses;
903
+ newPartitions.push({
904
+ id: newPartitionId,
905
+ latestFetchedBlock: p.latestFetchedBlock,
906
+ selection: fetchState.normalSelection,
907
+ addressesByContractName: addressesByContractName,
908
+ mergeBlock: undefined,
909
+ dynamicContract: contractName,
910
+ mutPendingQueries: p.mutPendingQueries,
911
+ prevQueryRange: p.prevQueryRange,
912
+ prevPrevQueryRange: p.prevPrevQueryRange,
913
+ prevRangeSize: p.prevRangeSize,
914
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
915
+ });
880
916
  }
917
+ } else {
918
+ mutExistingPartitions[idx$2] = {
919
+ id: p.id,
920
+ latestFetchedBlock: p.latestFetchedBlock,
921
+ selection: p.selection,
922
+ addressesByContractName: p.addressesByContractName,
923
+ mergeBlock: p.mergeBlock,
924
+ dynamicContract: contractName,
925
+ mutPendingQueries: p.mutPendingQueries,
926
+ prevQueryRange: p.prevQueryRange,
927
+ prevPrevQueryRange: p.prevPrevQueryRange,
928
+ prevRangeSize: p.prevRangeSize,
929
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
930
+ };
881
931
  }
882
932
  }
883
933
  }
884
- let registeringContracts = registeringContractsByContract[contractName];
885
- Object.assign(newIndexingAddresses, registeringContracts);
886
934
  }
887
- Object.assign(newIndexingAddresses, noEventsAddresses);
888
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
889
- return updateInternal(fetchState, optimizedPartitions, newIndexingAddresses, undefined, undefined, undefined);
890
- }
891
- if (!hasNoEventsUpdates) {
892
- return fetchState;
935
+ let registeringContracts = registeringContractsByContract[contractName];
936
+ IndexingAddresses.register(indexingAddresses, registeringContracts);
893
937
  }
894
- let newIndexingContracts = Utils.Dict.shallowCopy(indexingAddresses);
895
- Object.assign(newIndexingContracts, noEventsAddresses);
896
- return updateInternal(fetchState, undefined, newIndexingContracts, undefined, undefined, undefined);
938
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
939
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
940
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined);
897
941
  }
898
942
 
899
- function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
943
+ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBlock, newItems) {
900
944
  let newItems$1 = newItems.filter(item => {
901
945
  if (item.kind !== 0) {
902
946
  return true;
903
947
  }
904
- let filter = item.eventConfig.clientAddressFilter;
948
+ let filter = item.onEventRegistration.clientAddressFilter;
905
949
  if (filter !== undefined) {
906
- return filter(item.payload, item.blockNumber, fetchState.indexingAddresses);
950
+ return filter(item.payload, item.blockNumber, IndexingAddresses.rawForFilter(indexingAddresses));
907
951
  } else {
908
952
  return true;
909
953
  }
910
954
  });
911
- return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, latestFetchedBlock), undefined, newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
955
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems$1.length, latestFetchedBlock), newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
912
956
  }
913
957
 
914
958
  function startFetchingQueries(param, queries) {
@@ -922,6 +966,8 @@ function startFetchingQueries(param, queries) {
922
966
  fromBlock: q.fromBlock,
923
967
  toBlock: q.toBlock,
924
968
  isChunk: q.isChunk,
969
+ itemsTarget: q.itemsTarget,
970
+ itemsEst: q.itemsEst,
925
971
  fetchedBlock: undefined
926
972
  };
927
973
  let inserted = false;
@@ -939,184 +985,159 @@ function startFetchingQueries(param, queries) {
939
985
  }
940
986
  }
941
987
 
942
- function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber, maybeChunkRange, selection, addressesByContractName, indexingAddresses) {
943
- if (rangeFromBlock > maxQueryBlockNumber) {
944
- return;
945
- }
946
- if (rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock) {
947
- return;
948
- }
949
- if (maybeChunkRange !== undefined) {
950
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
951
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
952
- let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
953
- let getChunkSize = chunkIdx => {
954
- if (chunkIdx < 2) {
955
- return probeSize;
956
- } else {
957
- return chunkSize;
958
- }
959
- };
960
- if ((((rangeFromBlock + probeSize | 0) + probeSize | 0) - 1 | 0) <= maxBlock) {
961
- let chunkFromBlock = rangeFromBlock;
962
- let chunkIdx = 0;
963
- while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
964
- let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
988
+ function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock, knownHeight, chainTargetBlock, maybeChunkRange, maxChunks, partition, partitionBudget, chunkItemsMultiplier, selection, addressesByContractName) {
989
+ let cost = {
990
+ contents: 0
991
+ };
992
+ if (rangeFromBlock <= Primitive_int.min(knownHeight, chainTargetBlock) && maxChunks > 0) {
993
+ let exit = 0;
994
+ if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
995
+ exit = 1;
996
+ }
997
+ if (exit === 1) {
998
+ let trustedDensity = getTrustedDensity(partition);
999
+ let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
1000
+ let pushSingleQuery = (density, isChunk) => {
1001
+ let itemsTarget = densityItemsTarget(density * chunkItemsMultiplier, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1002
+ let itemsEst = densityItemsTarget(density, rangeFromBlock, rangeEndBlock, chainTargetBlock);
965
1003
  queries.push({
966
1004
  partitionId: partitionId,
967
- fromBlock: chunkFromBlock,
968
- toBlock: chunkToBlock,
969
- isChunk: true,
1005
+ fromBlock: rangeFromBlock,
1006
+ toBlock: rangeEndBlock,
1007
+ isChunk: isChunk,
1008
+ itemsTarget: itemsTarget,
1009
+ itemsEst: itemsEst,
970
1010
  selection: selection,
971
- addressesByContractName: addressesByContractName,
972
- indexingAddresses: indexingAddresses
1011
+ addressesByContractName: addressesByContractName
973
1012
  });
974
- chunkFromBlock = chunkToBlock + 1 | 0;
975
- chunkIdx = chunkIdx + 1 | 0;
1013
+ cost.contents = cost.contents + itemsEst;
976
1014
  };
977
- return;
1015
+ if (trustedDensity !== undefined) {
1016
+ if (maybeChunkRange !== undefined && trustedDensity > 0) {
1017
+ let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1018
+ if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1019
+ let chunkFromBlock = rangeFromBlock;
1020
+ let chunkIdx = 0;
1021
+ while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
1022
+ let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
1023
+ let itemsTarget = densityItemsTarget(trustedDensity * chunkItemsMultiplier, chunkFromBlock, chunkToBlock, chainTargetBlock);
1024
+ let itemsEst = densityItemsTarget(trustedDensity, chunkFromBlock, chunkToBlock, chainTargetBlock);
1025
+ queries.push({
1026
+ partitionId: partitionId,
1027
+ fromBlock: chunkFromBlock,
1028
+ toBlock: chunkToBlock,
1029
+ isChunk: true,
1030
+ itemsTarget: itemsTarget,
1031
+ itemsEst: itemsEst,
1032
+ selection: selection,
1033
+ addressesByContractName: addressesByContractName
1034
+ });
1035
+ cost.contents = cost.contents + itemsEst;
1036
+ chunkFromBlock = chunkToBlock + 1 | 0;
1037
+ chunkIdx = chunkIdx + 1 | 0;
1038
+ };
1039
+ } else {
1040
+ pushSingleQuery(trustedDensity, rangeEndBlock !== undefined);
1041
+ }
1042
+ } else {
1043
+ pushSingleQuery(trustedDensity, false);
1044
+ }
1045
+ } else {
1046
+ let remainingRange = Primitive_int.max(1, (chainTargetBlock - rangeFromBlock | 0) + 1 | 0);
1047
+ pushSingleQuery(partitionBudget / remainingRange, false);
1048
+ }
978
1049
  }
979
- queries.push({
980
- partitionId: partitionId,
981
- fromBlock: rangeFromBlock,
982
- toBlock: rangeEndBlock,
983
- isChunk: rangeEndBlock !== undefined,
984
- selection: selection,
985
- addressesByContractName: addressesByContractName,
986
- indexingAddresses: indexingAddresses
987
- });
988
- return;
989
1050
  }
990
- queries.push({
991
- partitionId: partitionId,
992
- fromBlock: rangeFromBlock,
993
- toBlock: rangeEndBlock,
994
- isChunk: false,
995
- selection: selection,
996
- addressesByContractName: addressesByContractName,
997
- indexingAddresses: indexingAddresses
998
- });
1051
+ return cost.contents;
999
1052
  }
1000
1053
 
1001
- function getNextQuery(fetchState, concurrencyLimit, bufferLimit) {
1002
- let knownHeight = fetchState.knownHeight;
1003
- let blockLag = fetchState.blockLag;
1004
- let indexingAddresses = fetchState.indexingAddresses;
1005
- let optimizedPartitions = fetchState.optimizedPartitions;
1054
+ function waterLevel(budget, footprints) {
1055
+ let sorted = footprints.toSorted(Primitive_float.compare);
1056
+ let n = sorted.length;
1057
+ let prefix = 0;
1058
+ let level;
1059
+ let idx = 0;
1060
+ while (level === undefined && idx < n) {
1061
+ let i = idx;
1062
+ prefix = prefix + sorted[i];
1063
+ let candidate = (budget + prefix) / (i + 1 | 0);
1064
+ if (i === (n - 1 | 0) || candidate <= sorted[i + 1 | 0]) {
1065
+ level = candidate;
1066
+ }
1067
+ idx = idx + 1 | 0;
1068
+ };
1069
+ return Stdlib_Option.getOr(level, 0);
1070
+ }
1071
+
1072
+ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star) {
1073
+ let knownHeight = param.knownHeight;
1074
+ let blockLag = param.blockLag;
1075
+ let endBlock = param.endBlock;
1076
+ let optimizedPartitions = param.optimizedPartitions;
1077
+ let chunkItemsMultiplier = $staropt$star !== undefined ? $staropt$star : 1;
1006
1078
  let headBlockNumber = knownHeight - blockLag | 0;
1007
1079
  if (headBlockNumber <= 0) {
1008
1080
  return "WaitingForNewBlock";
1009
1081
  }
1010
- if (concurrencyLimit === 0) {
1011
- return "ReachedMaxConcurrency";
1012
- }
1013
- if (bufferLimit <= 0) {
1014
- return "NothingToQuery";
1015
- }
1016
- let isOnBlockBehindTheHead = fetchState.latestOnBlockBlockNumber < headBlockNumber;
1017
- let endBlock = fetchState.endBlock;
1082
+ let isOnBlockBehindTheHead = param.latestOnBlockBlockNumber < headBlockNumber;
1018
1083
  let shouldWaitForNewBlock = (
1019
1084
  endBlock !== undefined ? headBlockNumber < endBlock : true
1020
1085
  ) && !isOnBlockBehindTheHead;
1021
- let item = fetchState.buffer[bufferLimit - 1 | 0];
1022
- let maxQueryBlockNumber = item !== undefined ? Primitive_int.min(item.blockNumber, knownHeight) : knownHeight;
1023
- let queries = [];
1024
1086
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
1025
- let idxRef = 0;
1026
- while (idxRef < partitionsCount) {
1027
- let idx = idxRef;
1087
+ for (let idx = 0; idx < partitionsCount; ++idx) {
1028
1088
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1029
1089
  let p = optimizedPartitions.entities[partitionId];
1030
- let pendingCount = p.mutPendingQueries.length;
1031
- let isBehindTheHead = p.latestFetchedBlock.blockNumber < headBlockNumber;
1032
- let hasPendingQueries = pendingCount > 0;
1033
- if (hasPendingQueries || isBehindTheHead) {
1090
+ if (p.mutPendingQueries.length !== 0 || p.latestFetchedBlock.blockNumber < headBlockNumber) {
1034
1091
  shouldWaitForNewBlock = false;
1035
1092
  }
1036
- let partitionQueriesStart = queries.length;
1037
- let queryEndBlock = Utils.$$Math.minOptInt(fetchState.endBlock, p.mergeBlock);
1038
- let queryEndBlock$1 = blockLag !== 0 ? Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock) : queryEndBlock;
1039
- let match = maxQueryBlockNumber < knownHeight;
1040
- let queryEndBlock$2 = queryEndBlock$1 !== undefined ? (
1041
- match ? Primitive_int.min(maxQueryBlockNumber, queryEndBlock$1) : queryEndBlock$1
1042
- ) : (
1043
- match ? maxQueryBlockNumber : queryEndBlock$1
1044
- );
1045
- let maybeChunkRange = getMinHistoryRange(p);
1046
- let cursor = p.latestFetchedBlock.blockNumber + 1 | 0;
1093
+ }
1094
+ let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount, param => []);
1095
+ let computeQueryEndBlock = p => {
1096
+ let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1097
+ if (blockLag !== 0) {
1098
+ return Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock);
1099
+ } else {
1100
+ return queryEndBlock;
1101
+ }
1102
+ };
1103
+ let existingReservedByPartition = {};
1104
+ let chainReserved = 0;
1105
+ for (let idx$1 = 0; idx$1 < partitionsCount; ++idx$1) {
1106
+ let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1107
+ let p$1 = optimizedPartitions.entities[partitionId$1];
1108
+ let cost = 0;
1109
+ for (let pqIdx = 0, pqIdx_finish = p$1.mutPendingQueries.length; pqIdx < pqIdx_finish; ++pqIdx) {
1110
+ cost = cost + p$1.mutPendingQueries[pqIdx].itemsEst;
1111
+ }
1112
+ existingReservedByPartition[partitionId$1] = cost;
1113
+ chainReserved = chainReserved + cost;
1114
+ }
1115
+ let fillStates = [];
1116
+ let gapFillCost = 0;
1117
+ for (let idx$2 = 0; idx$2 < partitionsCount; ++idx$2) {
1118
+ let partitionId$2 = optimizedPartitions.idsInAscOrder[idx$2];
1119
+ let p$2 = optimizedPartitions.entities[partitionId$2];
1120
+ let bucket = queriesByPartitionIndex[idx$2];
1121
+ let pendingCount = p$2.mutPendingQueries.length;
1122
+ let queryEndBlock = computeQueryEndBlock(p$2);
1123
+ let maybeChunkRange = getMinHistoryRange(p$2);
1124
+ let cursor = p$2.latestFetchedBlock.blockNumber + 1 | 0;
1047
1125
  let canContinue = true;
1048
- let pqIdx = 0;
1049
- while (pqIdx < p.mutPendingQueries.length && canContinue) {
1050
- let pq = p.mutPendingQueries[pqIdx];
1126
+ let chunksUsedThisCall = 0;
1127
+ let pqIdx$1 = 0;
1128
+ while (pqIdx$1 < pendingCount && canContinue) {
1129
+ let pq = p$2.mutPendingQueries[pqIdx$1];
1051
1130
  if (pq.fromBlock > cursor) {
1052
- let addressesByContractName = p.addressesByContractName;
1053
- let selection = p.selection;
1054
- let rangeEndBlock = Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock$2);
1055
- let rangeFromBlock = cursor;
1056
- if (rangeFromBlock <= maxQueryBlockNumber) {
1057
- let exit = 0;
1058
- if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
1059
- exit = 1;
1060
- }
1061
- if (exit === 1) {
1062
- if (maybeChunkRange !== undefined) {
1063
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
1064
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1065
- let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
1066
- let getChunkSize = chunkIdx => {
1067
- if (chunkIdx < 2) {
1068
- return probeSize;
1069
- } else {
1070
- return chunkSize;
1071
- }
1072
- };
1073
- if ((((rangeFromBlock + getChunkSize(0) | 0) + getChunkSize(1) | 0) - 1 | 0) <= maxBlock) {
1074
- let chunkFromBlock = rangeFromBlock;
1075
- let chunkIdx = 0;
1076
- while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
1077
- let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
1078
- queries.push({
1079
- partitionId: partitionId,
1080
- fromBlock: chunkFromBlock,
1081
- toBlock: chunkToBlock,
1082
- isChunk: true,
1083
- selection: selection,
1084
- addressesByContractName: addressesByContractName,
1085
- indexingAddresses: indexingAddresses
1086
- });
1087
- chunkFromBlock = chunkToBlock + 1 | 0;
1088
- chunkIdx = chunkIdx + 1 | 0;
1089
- };
1090
- } else {
1091
- queries.push({
1092
- partitionId: partitionId,
1093
- fromBlock: rangeFromBlock,
1094
- toBlock: rangeEndBlock,
1095
- isChunk: rangeEndBlock !== undefined,
1096
- selection: selection,
1097
- addressesByContractName: addressesByContractName,
1098
- indexingAddresses: indexingAddresses
1099
- });
1100
- }
1101
- } else {
1102
- queries.push({
1103
- partitionId: partitionId,
1104
- fromBlock: rangeFromBlock,
1105
- toBlock: rangeEndBlock,
1106
- isChunk: false,
1107
- selection: selection,
1108
- addressesByContractName: addressesByContractName,
1109
- indexingAddresses: indexingAddresses
1110
- });
1111
- }
1112
- }
1113
- }
1131
+ let beforeLen = bucket.length;
1132
+ let cost$1 = pushGapFillQueries(bucket, partitionId$2, cursor, Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock), knownHeight, chainTargetBlock, maybeChunkRange, (12 - pendingCount | 0) - chunksUsedThisCall | 0, p$2, chainTargetItems / partitionsCount, chunkItemsMultiplier, p$2.selection, p$2.addressesByContractName);
1133
+ chunksUsedThisCall = chunksUsedThisCall + (bucket.length - beforeLen | 0) | 0;
1134
+ gapFillCost = gapFillCost + cost$1;
1114
1135
  }
1115
1136
  let toBlock = pq.toBlock;
1116
1137
  if (toBlock !== undefined && pq.isChunk) {
1117
- let match$1 = pq.fetchedBlock;
1118
- if (match$1 !== undefined) {
1119
- let blockNumber = match$1.blockNumber;
1138
+ let match = pq.fetchedBlock;
1139
+ if (match !== undefined) {
1140
+ let blockNumber = match.blockNumber;
1120
1141
  cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
1121
1142
  } else {
1122
1143
  cursor = toBlock + 1 | 0;
@@ -1124,90 +1145,134 @@ function getNextQuery(fetchState, concurrencyLimit, bufferLimit) {
1124
1145
  } else {
1125
1146
  canContinue = false;
1126
1147
  }
1127
- pqIdx = pqIdx + 1 | 0;
1148
+ pqIdx$1 = pqIdx$1 + 1 | 0;
1128
1149
  };
1129
1150
  if (canContinue) {
1130
- let addressesByContractName$1 = p.addressesByContractName;
1131
- let selection$1 = p.selection;
1132
- let rangeFromBlock$1 = cursor;
1133
- if (rangeFromBlock$1 <= maxQueryBlockNumber) {
1134
- let exit$1 = 0;
1135
- if (!(queryEndBlock$2 !== undefined && rangeFromBlock$1 > queryEndBlock$2)) {
1136
- exit$1 = 1;
1137
- }
1138
- if (exit$1 === 1) {
1139
- if (maybeChunkRange !== undefined) {
1140
- let maxBlock$1 = queryEndBlock$2 !== undefined ? queryEndBlock$2 : maxQueryBlockNumber;
1141
- let chunkSize$1 = Js_math.ceil_int(maybeChunkRange * 1.8);
1142
- let probeSize$1 = Js_math.ceil_int(maybeChunkRange * 0.9);
1143
- let getChunkSize$1 = chunkIdx => {
1144
- if (chunkIdx < 2) {
1145
- return probeSize$1;
1146
- } else {
1147
- return chunkSize$1;
1148
- }
1149
- };
1150
- if ((((rangeFromBlock$1 + getChunkSize$1(0) | 0) + getChunkSize$1(1) | 0) - 1 | 0) <= maxBlock$1) {
1151
- let chunkFromBlock$1 = rangeFromBlock$1;
1152
- let chunkIdx$1 = 0;
1153
- while (chunkIdx$1 < 5 && ((chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0) <= maxBlock$1) {
1154
- let chunkToBlock$1 = (chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0;
1155
- queries.push({
1156
- partitionId: partitionId,
1157
- fromBlock: chunkFromBlock$1,
1158
- toBlock: chunkToBlock$1,
1159
- isChunk: true,
1160
- selection: selection$1,
1161
- addressesByContractName: addressesByContractName$1,
1162
- indexingAddresses: indexingAddresses
1163
- });
1164
- chunkFromBlock$1 = chunkToBlock$1 + 1 | 0;
1165
- chunkIdx$1 = chunkIdx$1 + 1 | 0;
1166
- };
1167
- } else {
1168
- queries.push({
1169
- partitionId: partitionId,
1170
- fromBlock: rangeFromBlock$1,
1171
- toBlock: queryEndBlock$2,
1172
- isChunk: queryEndBlock$2 !== undefined,
1173
- selection: selection$1,
1174
- addressesByContractName: addressesByContractName$1,
1175
- indexingAddresses: indexingAddresses
1176
- });
1177
- }
1178
- } else {
1179
- queries.push({
1180
- partitionId: partitionId,
1181
- fromBlock: rangeFromBlock$1,
1182
- toBlock: queryEndBlock$2,
1183
- isChunk: false,
1184
- selection: selection$1,
1185
- addressesByContractName: addressesByContractName$1,
1186
- indexingAddresses: indexingAddresses
1187
- });
1188
- }
1189
- }
1190
- }
1151
+ fillStates.push({
1152
+ partitionId: partitionId$2,
1153
+ p: p$2,
1154
+ cursor: cursor,
1155
+ chunksUsedThisCall: chunksUsedThisCall,
1156
+ pendingCount: pendingCount,
1157
+ queryEndBlock: queryEndBlock,
1158
+ maybeChunkRange: maybeChunkRange,
1159
+ bucket: bucket
1160
+ });
1191
1161
  }
1192
- let maxNewChunks = Primitive_int.max(0, 10 - pendingCount | 0);
1193
- let generatedCount = queries.length - partitionQueriesStart | 0;
1194
- if (generatedCount > maxNewChunks) {
1195
- queries.splice(partitionQueriesStart + maxNewChunks | 0, generatedCount - maxNewChunks | 0);
1162
+ }
1163
+ let rangeItemsTarget = Primitive_float.max(0, chainTargetItems - chainReserved - gapFillCost);
1164
+ let reservedByPartition = {};
1165
+ fillStates.forEach(fs => {
1166
+ let cost = existingReservedByPartition[fs.partitionId];
1167
+ for (let i = 0, i_finish = fs.bucket.length; i < i_finish; ++i) {
1168
+ cost = cost + fs.bucket[i].itemsEst;
1196
1169
  }
1197
- idxRef = idxRef + 1 | 0;
1170
+ reservedByPartition[fs.partitionId] = cost;
1171
+ });
1172
+ let isInRange = fs => {
1173
+ let tmp = false;
1174
+ if (fs.cursor <= chainTargetBlock) {
1175
+ let eb = fs.queryEndBlock;
1176
+ tmp = eb !== undefined ? fs.cursor <= eb : true;
1177
+ }
1178
+ if (tmp) {
1179
+ return (fs.pendingCount + fs.chunksUsedThisCall | 0) < 12;
1180
+ } else {
1181
+ return false;
1182
+ }
1183
+ };
1184
+ let inRangeStates = fillStates.filter(isInRange);
1185
+ let emitQueries = (fs, budget) => {
1186
+ let p = fs.p;
1187
+ let eb = fs.queryEndBlock;
1188
+ let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1189
+ let match = fs.maybeChunkRange;
1190
+ let match$1 = getTrustedDensity(p);
1191
+ if (match !== undefined && match$1 !== undefined && match$1 > 0) {
1192
+ let chunkSize = Js_math.ceil_int(match * 1.8);
1193
+ let maxChunksRemaining = (12 - fs.pendingCount | 0) - fs.chunksUsedThisCall | 0;
1194
+ let consumed = 0;
1195
+ let created = 0;
1196
+ let chunkFromBlock = fs.cursor;
1197
+ let budgetLeft = true;
1198
+ while (budgetLeft && created < maxChunksRemaining && chunkFromBlock <= Primitive_int.min(maxBlock, chainTargetBlock)) {
1199
+ let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1200
+ let rawEst = match$1 * ((chunkToBlock - chunkFromBlock | 0) + 1 | 0);
1201
+ let itemsEst = Primitive_int.max(1, Math.ceil(rawEst) | 0);
1202
+ let itemsTarget = Primitive_int.max(1, Math.ceil(rawEst * chunkItemsMultiplier) | 0);
1203
+ if (consumed === 0 || consumed + itemsEst <= budget) {
1204
+ fs.bucket.push({
1205
+ partitionId: fs.partitionId,
1206
+ fromBlock: chunkFromBlock,
1207
+ toBlock: chunkToBlock,
1208
+ isChunk: true,
1209
+ itemsTarget: itemsTarget,
1210
+ itemsEst: itemsEst,
1211
+ selection: p.selection,
1212
+ addressesByContractName: p.addressesByContractName
1213
+ });
1214
+ consumed = consumed + itemsEst;
1215
+ chunkFromBlock = chunkToBlock + 1 | 0;
1216
+ created = created + 1 | 0;
1217
+ } else {
1218
+ budgetLeft = false;
1219
+ }
1220
+ };
1221
+ fs.cursor = chunkFromBlock;
1222
+ fs.chunksUsedThisCall = fs.chunksUsedThisCall + created | 0;
1223
+ return consumed;
1224
+ }
1225
+ let itemsTarget$1 = Primitive_int.max(1, Math.round(budget) | 0);
1226
+ fs.bucket.push({
1227
+ partitionId: fs.partitionId,
1228
+ fromBlock: fs.cursor,
1229
+ toBlock: fs.queryEndBlock,
1230
+ isChunk: false,
1231
+ itemsTarget: itemsTarget$1,
1232
+ itemsEst: itemsTarget$1,
1233
+ selection: p.selection,
1234
+ addressesByContractName: p.addressesByContractName
1235
+ });
1236
+ fs.cursor = maxBlock + 1 | 0;
1237
+ fs.chunksUsedThisCall = fs.chunksUsedThisCall + 1 | 0;
1238
+ return itemsTarget$1;
1239
+ };
1240
+ let notFilledPartitions = inRangeStates;
1241
+ let remainingBudget = {
1242
+ contents: rangeItemsTarget
1198
1243
  };
1244
+ while (notFilledPartitions.length !== 0 && remainingBudget.contents > 0) {
1245
+ let level = waterLevel(remainingBudget.contents, notFilledPartitions.map(fs => reservedByPartition[fs.partitionId]));
1246
+ let next = [];
1247
+ notFilledPartitions.forEach(fs => {
1248
+ let reserved = reservedByPartition[fs.partitionId];
1249
+ let budget = level - reserved;
1250
+ if (budget <= 0) {
1251
+ return;
1252
+ }
1253
+ let consumed = emitQueries(fs, budget);
1254
+ reservedByPartition[fs.partitionId] = reserved + consumed;
1255
+ remainingBudget.contents = remainingBudget.contents - consumed;
1256
+ if (isInRange(fs)) {
1257
+ next.push(fs);
1258
+ return;
1259
+ }
1260
+ });
1261
+ notFilledPartitions = next;
1262
+ };
1263
+ let queries = queriesByPartitionIndex.flat();
1199
1264
  if (Utils.$$Array.isEmpty(queries)) {
1200
1265
  if (shouldWaitForNewBlock) {
1201
1266
  return "WaitingForNewBlock";
1202
1267
  } else {
1203
1268
  return "NothingToQuery";
1204
1269
  }
1270
+ } else {
1271
+ return {
1272
+ TAG: "Ready",
1273
+ _0: queries
1274
+ };
1205
1275
  }
1206
- let queries$1 = queries.length > concurrencyLimit ? (queries.sort((a, b) => Primitive_int.compare(a.fromBlock, b.fromBlock)), queries.slice(0, concurrencyLimit)) : queries;
1207
- return {
1208
- TAG: "Ready",
1209
- _0: queries$1
1210
- };
1211
1276
  }
1212
1277
 
1213
1278
  function hasReadyItem(fetchState) {
@@ -1242,9 +1307,9 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1242
1307
  return acc;
1243
1308
  }
1244
1309
 
1245
- function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1310
+ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockRegistrationsOpt, blockLagOpt, firstEventBlockOpt) {
1246
1311
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1247
- let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1312
+ let onBlockRegistrations = onBlockRegistrationsOpt !== undefined ? onBlockRegistrationsOpt : [];
1248
1313
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
1249
1314
  let firstEventBlock = firstEventBlockOpt !== undefined ? Primitive_option.valFromOption(firstEventBlockOpt) : undefined;
1250
1315
  let latestFetchedBlock = {
@@ -1252,32 +1317,14 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1252
1317
  blockTimestamp: 0
1253
1318
  };
1254
1319
  let notDependingOnAddresses = [];
1255
- let normalEventConfigs = [];
1320
+ let normalRegistrations = [];
1256
1321
  let contractNamesWithNormalEvents = new Set();
1257
- let indexingAddresses = {};
1258
- let contractConfigs = {};
1259
- eventConfigs.forEach(ec => {
1260
- let match = contractConfigs[ec.contractName];
1261
- if (match !== undefined) {
1262
- let startBlock = match.startBlock;
1263
- let match$1 = ec.startBlock;
1264
- contractConfigs[ec.contractName] = {
1265
- startBlock: startBlock !== undefined ? (
1266
- match$1 !== undefined ? Primitive_int.min(startBlock, match$1) : startBlock
1267
- ) : (
1268
- match$1 !== undefined ? match$1 : undefined
1269
- )
1270
- };
1322
+ onEventRegistrations.forEach(reg => {
1323
+ if (reg.dependsOnAddresses) {
1324
+ normalRegistrations.push(reg);
1325
+ contractNamesWithNormalEvents.add(reg.eventConfig.contractName);
1271
1326
  } else {
1272
- contractConfigs[ec.contractName] = {
1273
- startBlock: ec.startBlock
1274
- };
1275
- }
1276
- if (ec.dependsOnAddresses) {
1277
- normalEventConfigs.push(ec);
1278
- contractNamesWithNormalEvents.add(ec.contractName);
1279
- } else {
1280
- notDependingOnAddresses.push(ec);
1327
+ notDependingOnAddresses.push(reg);
1281
1328
  }
1282
1329
  });
1283
1330
  let partitions = [];
@@ -1286,7 +1333,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1286
1333
  id: partitions.length.toString(),
1287
1334
  latestFetchedBlock: latestFetchedBlock,
1288
1335
  selection: {
1289
- eventConfigs: notDependingOnAddresses,
1336
+ onEventRegistrations: notDependingOnAddresses,
1290
1337
  dependsOnAddresses: false
1291
1338
  },
1292
1339
  addressesByContractName: {},
@@ -1295,51 +1342,39 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1295
1342
  mutPendingQueries: [],
1296
1343
  prevQueryRange: 0,
1297
1344
  prevPrevQueryRange: 0,
1345
+ prevRangeSize: 0,
1298
1346
  latestBlockRangeUpdateBlock: 0
1299
1347
  });
1300
1348
  }
1301
1349
  let normalSelection = {
1302
- eventConfigs: normalEventConfigs,
1350
+ onEventRegistrations: normalRegistrations,
1303
1351
  dependsOnAddresses: true
1304
1352
  };
1305
1353
  let registeringContractsByContract = {};
1306
1354
  let dynamicContracts = new Set();
1307
1355
  addresses.forEach(contract => {
1308
1356
  let contractName = contract.contractName;
1309
- let match = contractConfigs[contractName];
1310
- let contractStartBlock = match !== undefined ? match.startBlock : undefined;
1311
- let ia_address = contract.address;
1312
- let ia_contractName = contract.contractName;
1313
- let ia_registrationBlock = contract.registrationBlock;
1314
- let ia_effectiveStartBlock = deriveEffectiveStartBlock(contract.registrationBlock, contractStartBlock);
1315
- let ia = {
1316
- address: ia_address,
1317
- contractName: ia_contractName,
1318
- registrationBlock: ia_registrationBlock,
1319
- effectiveStartBlock: ia_effectiveStartBlock
1320
- };
1321
- indexingAddresses[contract.address] = ia;
1322
- if (!contractNamesWithNormalEvents.has(contractName)) {
1323
- return;
1324
- }
1325
- let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1326
- registeringContracts[contract.address] = ia;
1327
- if (contract.registrationBlock !== -1) {
1328
- dynamicContracts.add(contractName);
1329
- return;
1357
+ if (contractNamesWithNormalEvents.has(contractName)) {
1358
+ Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName)[contract.address] = IndexingAddresses.makeIndexingAddress(contract, contractConfigs);
1359
+ if (contract.registrationBlock !== -1) {
1360
+ dynamicContracts.add(contractName);
1361
+ return;
1362
+ } else {
1363
+ return;
1364
+ }
1330
1365
  }
1331
1366
  });
1332
1367
  let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
1333
- if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockConfigs)) {
1334
- Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + chainId.toString() + `. ` + (`addresses=` + addresses.length.toString() + `, `) + (`eventConfigs=` + eventConfigs.length.toString() + `, `) + (`normalEventConfigs=` + normalEventConfigs.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1368
+ if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockRegistrations)) {
1369
+ Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + chainId.toString() + `. ` + (`addresses=` + addresses.length.toString() + `, `) + (`onEventRegistrations=` + onEventRegistrations.length.toString() + `, `) + (`normalRegistrations=` + normalRegistrations.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1335
1370
  }
1336
1371
  let buffer = [];
1337
1372
  let latestOnBlockBlockNumber;
1338
- if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockConfigs)) {
1373
+ if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockRegistrations)) {
1339
1374
  let id = optimizedPartitions.idsInAscOrder[0];
1340
1375
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
1341
1376
  let maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
1342
- latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1377
+ latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockRegistrations, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1343
1378
  } else {
1344
1379
  latestOnBlockBlockNumber = progressBlockNumber;
1345
1380
  }
@@ -1348,19 +1383,16 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1348
1383
  startBlock: startBlock,
1349
1384
  endBlock: endBlock,
1350
1385
  normalSelection: normalSelection,
1351
- indexingAddresses: indexingAddresses,
1352
1386
  contractConfigs: contractConfigs,
1353
1387
  chainId: chainId,
1354
1388
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
1355
1389
  blockLag: blockLag,
1356
1390
  buffer: buffer,
1357
1391
  maxOnBlockBufferSize: maxOnBlockBufferSize,
1358
- onBlockConfigs: onBlockConfigs,
1392
+ onBlockRegistrations: onBlockRegistrations,
1359
1393
  knownHeight: knownHeight,
1360
1394
  firstEventBlock: firstEventBlock
1361
1395
  };
1362
- let numAddresses = Utils.Dict.size(indexingAddresses);
1363
- Prometheus.IndexingAddresses.set(numAddresses, chainId);
1364
1396
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, chainId);
1365
1397
  Prometheus.IndexingBufferSize.set(buffer.length, chainId);
1366
1398
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(fetchState), chainId);
@@ -1374,22 +1406,6 @@ function bufferSize(param) {
1374
1406
  return param.buffer.length;
1375
1407
  }
1376
1408
 
1377
- function bufferReadyCount(fetchState) {
1378
- let frontier = bufferBlockNumber(fetchState);
1379
- let buffer = fetchState.buffer;
1380
- let lo = 0;
1381
- let hi = buffer.length;
1382
- while (lo < hi) {
1383
- let mid = (lo + hi | 0) / 2 | 0;
1384
- if (buffer[mid].blockNumber <= frontier) {
1385
- lo = mid + 1 | 0;
1386
- } else {
1387
- hi = mid;
1388
- }
1389
- };
1390
- return lo;
1391
- }
1392
-
1393
1409
  function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1394
1410
  let adjusted = [];
1395
1411
  for (let qIdx = 0, qIdx_finish = mutPendingQueries.length; qIdx < qIdx_finish; ++qIdx) {
@@ -1402,6 +1418,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1402
1418
  fromBlock: pq.fromBlock,
1403
1419
  toBlock: pq.toBlock,
1404
1420
  isChunk: pq.isChunk,
1421
+ itemsTarget: pq.itemsTarget,
1422
+ itemsEst: pq.itemsEst,
1405
1423
  fetchedBlock: {
1406
1424
  blockNumber: targetBlockNumber,
1407
1425
  blockTimestamp: 0
@@ -1418,16 +1436,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1418
1436
  return adjusted;
1419
1437
  }
1420
1438
 
1421
- function rollback(fetchState, targetBlockNumber) {
1422
- let addressesToRemove = new Set();
1423
- let indexingAddresses = {};
1424
- Utils.Dict.forEachWithKey(fetchState.indexingAddresses, (indexingContract, address) => {
1425
- if (indexingContract.registrationBlock > targetBlockNumber) {
1426
- addressesToRemove.add(address);
1427
- } else {
1428
- indexingAddresses[address] = indexingContract;
1429
- }
1430
- });
1439
+ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1440
+ IndexingAddresses.rollbackInPlace(indexingAddresses, targetBlockNumber);
1431
1441
  let keptPartitions = [];
1432
1442
  let nextKeptIdRef = 0;
1433
1443
  let registeringContractsByContract = {};
@@ -1438,11 +1448,12 @@ function rollback(fetchState, targetBlockNumber) {
1438
1448
  if (p.latestFetchedBlock.blockNumber > targetBlockNumber) {
1439
1449
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1440
1450
  addresses.forEach(address => {
1441
- if (!(!addressesToRemove.has(address) && Stdlib_Option.isSome(indexingAddresses[address]))) {
1451
+ let indexingContract = IndexingAddresses.get(indexingAddresses, address);
1452
+ if (indexingContract === undefined) {
1442
1453
  return;
1443
1454
  }
1444
1455
  let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1445
- registeringContracts[address] = indexingAddresses[address];
1456
+ registeringContracts[address] = indexingContract;
1446
1457
  });
1447
1458
  });
1448
1459
  } else {
@@ -1450,7 +1461,7 @@ function rollback(fetchState, targetBlockNumber) {
1450
1461
  let mergeBlock$1 = mergeBlock !== undefined && mergeBlock > targetBlockNumber ? targetBlockNumber : mergeBlock;
1451
1462
  let rollbackedAddressesByContractName = {};
1452
1463
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1453
- let keptAddresses = addresses.filter(address => !addressesToRemove.has(address));
1464
+ let keptAddresses = addresses.filter(address => Stdlib_Option.isSome(IndexingAddresses.get(indexingAddresses, address)));
1454
1465
  if (keptAddresses.length !== 0) {
1455
1466
  rollbackedAddressesByContractName[contractName] = keptAddresses;
1456
1467
  return;
@@ -1469,6 +1480,7 @@ function rollback(fetchState, targetBlockNumber) {
1469
1480
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1470
1481
  prevQueryRange: p.prevQueryRange,
1471
1482
  prevPrevQueryRange: p.prevPrevQueryRange,
1483
+ prevRangeSize: p.prevRangeSize,
1472
1484
  latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1473
1485
  });
1474
1486
  }
@@ -1489,6 +1501,7 @@ function rollback(fetchState, targetBlockNumber) {
1489
1501
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1490
1502
  prevQueryRange: p.prevQueryRange,
1491
1503
  prevPrevQueryRange: p.prevPrevQueryRange,
1504
+ prevRangeSize: p.prevRangeSize,
1492
1505
  latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1493
1506
  });
1494
1507
  }
@@ -1499,17 +1512,16 @@ function rollback(fetchState, targetBlockNumber) {
1499
1512
  startBlock: fetchState.startBlock,
1500
1513
  endBlock: fetchState.endBlock,
1501
1514
  normalSelection: fetchState.normalSelection,
1502
- indexingAddresses: fetchState.indexingAddresses,
1503
1515
  contractConfigs: fetchState.contractConfigs,
1504
1516
  chainId: fetchState.chainId,
1505
1517
  latestOnBlockBlockNumber: Primitive_int.min(fetchState.latestOnBlockBlockNumber, targetBlockNumber),
1506
1518
  blockLag: fetchState.blockLag,
1507
1519
  buffer: fetchState.buffer,
1508
1520
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1509
- onBlockConfigs: fetchState.onBlockConfigs,
1521
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1510
1522
  knownHeight: fetchState.knownHeight,
1511
1523
  firstEventBlock: fetchState.firstEventBlock
1512
- }, optimizedPartitions, indexingAddresses, fetchState.buffer.filter(item => {
1524
+ }, optimizedPartitions, fetchState.buffer.filter(item => {
1513
1525
  let tmp;
1514
1526
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1515
1527
  return tmp <= targetBlockNumber;
@@ -1533,6 +1545,7 @@ function resetPendingQueries(fetchState) {
1533
1545
  mutPendingQueries: kept,
1534
1546
  prevQueryRange: partition.prevQueryRange,
1535
1547
  prevPrevQueryRange: partition.prevPrevQueryRange,
1548
+ prevRangeSize: partition.prevRangeSize,
1536
1549
  latestBlockRangeUpdateBlock: partition.latestBlockRangeUpdateBlock
1537
1550
  };
1538
1551
  }
@@ -1549,14 +1562,13 @@ function resetPendingQueries(fetchState) {
1549
1562
  startBlock: fetchState.startBlock,
1550
1563
  endBlock: fetchState.endBlock,
1551
1564
  normalSelection: fetchState.normalSelection,
1552
- indexingAddresses: fetchState.indexingAddresses,
1553
1565
  contractConfigs: fetchState.contractConfigs,
1554
1566
  chainId: fetchState.chainId,
1555
1567
  latestOnBlockBlockNumber: fetchState.latestOnBlockBlockNumber,
1556
1568
  blockLag: fetchState.blockLag,
1557
1569
  buffer: fetchState.buffer,
1558
1570
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1559
- onBlockConfigs: fetchState.onBlockConfigs,
1571
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1560
1572
  knownHeight: fetchState.knownHeight,
1561
1573
  firstEventBlock: fetchState.firstEventBlock
1562
1574
  };
@@ -1666,7 +1678,7 @@ function getProgressBlockNumberAt(fetchState, index) {
1666
1678
  function updateKnownHeight(fetchState, knownHeight) {
1667
1679
  if (knownHeight > fetchState.knownHeight) {
1668
1680
  Prometheus.IndexingKnownHeight.set(knownHeight, fetchState.chainId);
1669
- return updateInternal(fetchState, undefined, undefined, undefined, undefined, knownHeight);
1681
+ return updateInternal(fetchState, undefined, undefined, undefined, knownHeight);
1670
1682
  } else {
1671
1683
  return fetchState;
1672
1684
  }
@@ -1674,18 +1686,20 @@ function updateKnownHeight(fetchState, knownHeight) {
1674
1686
 
1675
1687
  let blockItemLogIndex = 16777216;
1676
1688
 
1677
- let maxPendingChunksPerPartition = 10;
1689
+ let maxPendingChunksPerPartition = 12;
1678
1690
 
1679
1691
  export {
1680
- deriveEffectiveStartBlock,
1692
+ deriveContractNameByAddress,
1693
+ densityItemsTarget,
1681
1694
  getMinHistoryRange,
1695
+ getTrustedDensity,
1682
1696
  getMinQueryRange,
1683
1697
  OptimizedPartitions,
1684
1698
  bufferBlockNumber,
1685
1699
  bufferBlock,
1700
+ bufferReadyCount,
1686
1701
  compareBufferItem,
1687
1702
  blockItemLogIndex,
1688
- numAddresses,
1689
1703
  appendOnBlockItems,
1690
1704
  updateInternal,
1691
1705
  warnDifferentContractType,
@@ -1695,14 +1709,14 @@ export {
1695
1709
  registerDynamicContracts,
1696
1710
  handleQueryResult,
1697
1711
  startFetchingQueries,
1698
- pushQueriesForRange,
1699
1712
  maxPendingChunksPerPartition,
1713
+ pushGapFillQueries,
1714
+ waterLevel,
1700
1715
  getNextQuery,
1701
1716
  hasReadyItem,
1702
1717
  getReadyItemsCount,
1703
1718
  make$1 as make,
1704
1719
  bufferSize,
1705
- bufferReadyCount,
1706
1720
  rollbackPendingQueries,
1707
1721
  rollback,
1708
1722
  resetPendingQueries,
@@ -1714,4 +1728,4 @@ export {
1714
1728
  getProgressBlockNumberAt,
1715
1729
  updateKnownHeight,
1716
1730
  }
1717
- /* Utils Not a pure module */
1731
+ /* deriveContractNameByAddress Not a pure module */