envio 3.3.0-alpha.10 → 3.3.0-alpha.12

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 (60) hide show
  1. package/licenses/CLA.md +35 -0
  2. package/licenses/EULA.md +67 -0
  3. package/licenses/LICENSE.md +45 -0
  4. package/licenses/README.md +35 -0
  5. package/package.json +9 -8
  6. package/src/Batch.res.mjs +1 -1
  7. package/src/ChainFetching.res +30 -18
  8. package/src/ChainFetching.res.mjs +23 -13
  9. package/src/ChainState.res +45 -30
  10. package/src/ChainState.res.mjs +22 -8
  11. package/src/ChainState.resi +5 -0
  12. package/src/Config.res +9 -5
  13. package/src/Config.res.mjs +2 -2
  14. package/src/Core.res +20 -0
  15. package/src/Core.res.mjs +12 -0
  16. package/src/CrossChainState.res +64 -29
  17. package/src/CrossChainState.res.mjs +17 -8
  18. package/src/EventConfigBuilder.res +21 -46
  19. package/src/EventConfigBuilder.res.mjs +18 -25
  20. package/src/EventProcessing.res +8 -5
  21. package/src/EventProcessing.res.mjs +2 -1
  22. package/src/FetchState.res +428 -322
  23. package/src/FetchState.res.mjs +316 -233
  24. package/src/HandlerRegister.res +3 -1
  25. package/src/HandlerRegister.res.mjs +3 -2
  26. package/src/Internal.res +11 -2
  27. package/src/LogSelection.res +0 -62
  28. package/src/LogSelection.res.mjs +0 -74
  29. package/src/RawEvent.res +2 -2
  30. package/src/Rollback.res +32 -13
  31. package/src/Rollback.res.mjs +24 -11
  32. package/src/SimulateDeadInputTracker.res +1 -1
  33. package/src/SimulateItems.res +38 -6
  34. package/src/SimulateItems.res.mjs +28 -9
  35. package/src/TestIndexer.res +2 -2
  36. package/src/TestIndexer.res.mjs +2 -2
  37. package/src/TopicFilter.res +1 -25
  38. package/src/TopicFilter.res.mjs +0 -74
  39. package/src/bindings/Viem.res +0 -5
  40. package/src/sources/EventRouter.res +0 -21
  41. package/src/sources/EventRouter.res.mjs +0 -12
  42. package/src/sources/EvmChain.res +2 -27
  43. package/src/sources/EvmChain.res.mjs +2 -23
  44. package/src/sources/EvmRpcClient.res +12 -15
  45. package/src/sources/EvmRpcClient.res.mjs +3 -3
  46. package/src/sources/HyperSync.res +9 -38
  47. package/src/sources/HyperSync.res.mjs +16 -28
  48. package/src/sources/HyperSync.resi +2 -2
  49. package/src/sources/HyperSyncClient.res +88 -11
  50. package/src/sources/HyperSyncClient.res.mjs +39 -6
  51. package/src/sources/HyperSyncSource.res +18 -199
  52. package/src/sources/HyperSyncSource.res.mjs +9 -128
  53. package/src/sources/Rpc.res +0 -32
  54. package/src/sources/Rpc.res.mjs +1 -46
  55. package/src/sources/RpcSource.res +29 -175
  56. package/src/sources/RpcSource.res.mjs +32 -110
  57. package/src/sources/SimulateSource.res +37 -19
  58. package/src/sources/SimulateSource.res.mjs +27 -10
  59. package/src/sources/SvmHyperSyncSource.res +13 -3
  60. package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
@@ -28,27 +28,23 @@ function densityItemsTarget(density, fromBlock, toBlock, chainTargetBlock) {
28
28
  }
29
29
 
30
30
  function getMinHistoryRange(p) {
31
- let match = p.prevQueryRange;
32
- let match$1 = p.prevPrevQueryRange;
31
+ let match = p.sourceRangeCapacity;
32
+ let match$1 = p.prevSourceRangeCapacity;
33
33
  if (match !== 0 && match$1 !== 0) {
34
34
  return match < match$1 ? match : match$1;
35
35
  }
36
36
  }
37
37
 
38
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
- }
39
+ return p.eventDensity;
44
40
  }
45
41
 
46
42
  function getMinQueryRange(partitions) {
47
43
  let min = 0;
48
44
  for (let i = 0, i_finish = partitions.length; i < i_finish; ++i) {
49
45
  let p = partitions[i];
50
- let a = p.prevQueryRange;
51
- let b = p.prevPrevQueryRange;
46
+ let a = p.sourceRangeCapacity;
47
+ let b = p.prevSourceRangeCapacity;
52
48
  if (a > 0 && (min === 0 || a < min)) {
53
49
  min = a;
54
50
  }
@@ -88,10 +84,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
88
84
  mergeBlock: potentialMergeBlock,
89
85
  dynamicContract: p1.dynamicContract,
90
86
  mutPendingQueries: p1.mutPendingQueries,
91
- prevQueryRange: p1.prevQueryRange,
92
- prevPrevQueryRange: p1.prevPrevQueryRange,
93
- prevRangeSize: p1.prevRangeSize,
94
- latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
87
+ sourceRangeCapacity: p1.sourceRangeCapacity,
88
+ prevSourceRangeCapacity: p1.prevSourceRangeCapacity,
89
+ eventDensity: p1.eventDensity,
90
+ latestSourceRangeCapacityUpdateBlock: p1.latestSourceRangeCapacityUpdateBlock
95
91
  });
96
92
  completed.push({
97
93
  id: p2.id,
@@ -101,10 +97,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
101
97
  mergeBlock: potentialMergeBlock,
102
98
  dynamicContract: p2.dynamicContract,
103
99
  mutPendingQueries: p2.mutPendingQueries,
104
- prevQueryRange: p2.prevQueryRange,
105
- prevPrevQueryRange: p2.prevPrevQueryRange,
106
- prevRangeSize: p2.prevRangeSize,
107
- latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
100
+ sourceRangeCapacity: p2.sourceRangeCapacity,
101
+ prevSourceRangeCapacity: p2.prevSourceRangeCapacity,
102
+ eventDensity: p2.eventDensity,
103
+ latestSourceRangeCapacityUpdateBlock: p2.latestSourceRangeCapacityUpdateBlock
108
104
  });
109
105
  let newId = nextPartitionIndexRef.contents.toString();
110
106
  nextPartitionIndexRef.contents = nextPartitionIndexRef.contents + 1 | 0;
@@ -112,7 +108,13 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
112
108
  p1,
113
109
  p2
114
110
  ]);
115
- let inheritedDensity = Stdlib_Option.getOr(getTrustedDensity(p1), 0) + Stdlib_Option.getOr(getTrustedDensity(p2), 0);
111
+ let match = p1.eventDensity;
112
+ let match$1 = p2.eventDensity;
113
+ let inheritedDensity = match !== undefined ? (
114
+ match$1 !== undefined ? match + match$1 : match
115
+ ) : (
116
+ match$1 !== undefined ? match$1 : undefined
117
+ );
116
118
  continuingBase = {
117
119
  id: newId,
118
120
  latestFetchedBlock: {
@@ -124,10 +126,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
124
126
  mergeBlock: undefined,
125
127
  dynamicContract: contractName,
126
128
  mutPendingQueries: [],
127
- prevQueryRange: minRange,
128
- prevPrevQueryRange: minRange,
129
- prevRangeSize: Math.ceil(inheritedDensity * minRange) | 0,
130
- latestBlockRangeUpdateBlock: 0
129
+ sourceRangeCapacity: minRange,
130
+ prevSourceRangeCapacity: minRange,
131
+ eventDensity: inheritedDensity,
132
+ latestSourceRangeCapacityUpdateBlock: 0
131
133
  };
132
134
  } else {
133
135
  completed.push({
@@ -138,10 +140,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
138
140
  mergeBlock: potentialMergeBlock,
139
141
  dynamicContract: p1.dynamicContract,
140
142
  mutPendingQueries: p1.mutPendingQueries,
141
- prevQueryRange: p1.prevQueryRange,
142
- prevPrevQueryRange: p1.prevPrevQueryRange,
143
- prevRangeSize: p1.prevRangeSize,
144
- latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
143
+ sourceRangeCapacity: p1.sourceRangeCapacity,
144
+ prevSourceRangeCapacity: p1.prevSourceRangeCapacity,
145
+ eventDensity: p1.eventDensity,
146
+ latestSourceRangeCapacityUpdateBlock: p1.latestSourceRangeCapacityUpdateBlock
145
147
  });
146
148
  continuingBase = p2;
147
149
  }
@@ -154,10 +156,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
154
156
  mergeBlock: potentialMergeBlock,
155
157
  dynamicContract: p2.dynamicContract,
156
158
  mutPendingQueries: p2.mutPendingQueries,
157
- prevQueryRange: p2.prevQueryRange,
158
- prevPrevQueryRange: p2.prevPrevQueryRange,
159
- prevRangeSize: p2.prevRangeSize,
160
- latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
159
+ sourceRangeCapacity: p2.sourceRangeCapacity,
160
+ prevSourceRangeCapacity: p2.prevSourceRangeCapacity,
161
+ eventDensity: p2.eventDensity,
162
+ latestSourceRangeCapacityUpdateBlock: p2.latestSourceRangeCapacityUpdateBlock
161
163
  });
162
164
  continuingBase = p1;
163
165
  } else {
@@ -178,10 +180,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
178
180
  mergeBlock: continuingBase.mergeBlock,
179
181
  dynamicContract: continuingBase.dynamicContract,
180
182
  mutPendingQueries: continuingBase.mutPendingQueries,
181
- prevQueryRange: continuingBase.prevQueryRange,
182
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
183
- prevRangeSize: continuingBase.prevRangeSize,
184
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
183
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
184
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
185
+ eventDensity: continuingBase.eventDensity,
186
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
185
187
  });
186
188
  let restId = nextPartitionIndexRef.contents.toString();
187
189
  nextPartitionIndexRef.contents = nextPartitionIndexRef.contents + 1 | 0;
@@ -193,10 +195,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
193
195
  mergeBlock: continuingBase.mergeBlock,
194
196
  dynamicContract: continuingBase.dynamicContract,
195
197
  mutPendingQueries: [],
196
- prevQueryRange: continuingBase.prevQueryRange,
197
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
198
- prevRangeSize: continuingBase.prevRangeSize,
199
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
198
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
199
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
200
+ eventDensity: continuingBase.eventDensity,
201
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
200
202
  });
201
203
  return completed;
202
204
  }
@@ -210,10 +212,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
210
212
  mergeBlock: continuingBase.mergeBlock,
211
213
  dynamicContract: continuingBase.dynamicContract,
212
214
  mutPendingQueries: continuingBase.mutPendingQueries,
213
- prevQueryRange: continuingBase.prevQueryRange,
214
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
215
- prevRangeSize: continuingBase.prevRangeSize,
216
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
215
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
216
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
217
+ eventDensity: continuingBase.eventDensity,
218
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
217
219
  });
218
220
  return completed;
219
221
  }
@@ -358,8 +360,11 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
358
360
  let pendingQuery = getPendingQueryOrThrow(p$1, query.fromBlock);
359
361
  pendingQuery.fetchedBlock = latestFetchedBlock;
360
362
  let blockRange = (latestFetchedBlock.blockNumber - query.fromBlock | 0) + 1 | 0;
361
- let shouldUpdateBlockRange = false;
362
- if (latestFetchedBlock.blockNumber > p$1.latestBlockRangeUpdateBlock) {
363
+ let observedEventDensity = itemsCount / blockRange;
364
+ let eventDensity = p$1.eventDensity;
365
+ let updatedEventDensity = eventDensity !== undefined ? (eventDensity + observedEventDensity) / 2 : observedEventDensity;
366
+ let shouldUpdateSourceRangeCapacity = false;
367
+ if (latestFetchedBlock.blockNumber > p$1.latestSourceRangeCapacityUpdateBlock) {
363
368
  let queryToBlock = query.toBlock;
364
369
  let tmp;
365
370
  if (queryToBlock !== undefined) {
@@ -372,11 +377,10 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
372
377
  } else {
373
378
  tmp = latestFetchedBlock.blockNumber < (knownHeight - 10 | 0);
374
379
  }
375
- shouldUpdateBlockRange = tmp;
380
+ shouldUpdateSourceRangeCapacity = tmp;
376
381
  }
377
- let updatedPrevQueryRange = shouldUpdateBlockRange ? blockRange : p$1.prevQueryRange;
378
- let updatedPrevPrevQueryRange = shouldUpdateBlockRange ? p$1.prevQueryRange : p$1.prevPrevQueryRange;
379
- let updatedPrevRangeSize = shouldUpdateBlockRange ? itemsCount : p$1.prevRangeSize;
382
+ let updatedSourceRangeCapacity = shouldUpdateSourceRangeCapacity ? blockRange : p$1.sourceRangeCapacity;
383
+ let updatedPrevSourceRangeCapacity = shouldUpdateSourceRangeCapacity ? p$1.sourceRangeCapacity : p$1.prevSourceRangeCapacity;
380
384
  let mutPendingQueries = p$1.mutPendingQueries;
381
385
  let latestFetchedBlock$1 = p$1.latestFetchedBlock;
382
386
  while ((() => {
@@ -402,7 +406,7 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
402
406
  let updatedMainPartition_mergeBlock = p$1.mergeBlock;
403
407
  let updatedMainPartition_dynamicContract = p$1.dynamicContract;
404
408
  let updatedMainPartition_mutPendingQueries = p$1.mutPendingQueries;
405
- let updatedMainPartition_latestBlockRangeUpdateBlock = shouldUpdateBlockRange ? latestFetchedBlock.blockNumber : p$1.latestBlockRangeUpdateBlock;
409
+ let updatedMainPartition_latestSourceRangeCapacityUpdateBlock = shouldUpdateSourceRangeCapacity ? latestFetchedBlock.blockNumber : p$1.latestSourceRangeCapacityUpdateBlock;
406
410
  let updatedMainPartition = {
407
411
  id: updatedMainPartition_id,
408
412
  latestFetchedBlock: updatedLatestFetchedBlock,
@@ -411,10 +415,10 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
411
415
  mergeBlock: updatedMainPartition_mergeBlock,
412
416
  dynamicContract: updatedMainPartition_dynamicContract,
413
417
  mutPendingQueries: updatedMainPartition_mutPendingQueries,
414
- prevQueryRange: updatedPrevQueryRange,
415
- prevPrevQueryRange: updatedPrevPrevQueryRange,
416
- prevRangeSize: updatedPrevRangeSize,
417
- latestBlockRangeUpdateBlock: updatedMainPartition_latestBlockRangeUpdateBlock
418
+ sourceRangeCapacity: updatedSourceRangeCapacity,
419
+ prevSourceRangeCapacity: updatedPrevSourceRangeCapacity,
420
+ eventDensity: updatedEventDensity,
421
+ latestSourceRangeCapacityUpdateBlock: updatedMainPartition_latestSourceRangeCapacityUpdateBlock
418
422
  };
419
423
  mutEntities[p$1.id] = updatedMainPartition;
420
424
  }
@@ -484,15 +488,92 @@ function bufferReadyCount(fetchState) {
484
488
  return lo;
485
489
  }
486
490
 
491
+ function getRegistrationIndex(item) {
492
+ if (item.kind === 0) {
493
+ return item.onEventRegistration.index;
494
+ } else {
495
+ return item.onBlockRegistration.index;
496
+ }
497
+ }
498
+
487
499
  function compareBufferItem(a, b) {
488
- let blockOrdering = Primitive_int.compare(a.blockNumber, b.blockNumber);
489
- if (blockOrdering === 0) {
490
- return Primitive_int.compare(a.logIndex, b.logIndex);
500
+ let ba = a.blockNumber;
501
+ let bb = b.blockNumber;
502
+ if (ba !== bb) {
503
+ if (ba < bb) {
504
+ return -1;
505
+ } else {
506
+ return 1;
507
+ }
508
+ }
509
+ let la = a.logIndex;
510
+ let lb = b.logIndex;
511
+ if (la !== lb) {
512
+ if (la < lb) {
513
+ return -1;
514
+ } else {
515
+ return 1;
516
+ }
517
+ }
518
+ let ia = getRegistrationIndex(a);
519
+ let ib = getRegistrationIndex(b);
520
+ if (ia < ib) {
521
+ return -1;
522
+ } else if (ia > ib) {
523
+ return 1;
491
524
  } else {
492
- return blockOrdering;
525
+ return 0;
493
526
  }
494
527
  }
495
528
 
529
+ function mergeIntoBuffer(buffer, newItems) {
530
+ let n = newItems.length;
531
+ for (let i = 1; i < n; ++i) {
532
+ let x = newItems[i];
533
+ let j = i - 1 | 0;
534
+ while (j >= 0 && compareBufferItem(newItems[j], x) > 0) {
535
+ newItems[j + 1 | 0] = newItems[j];
536
+ j = j - 1 | 0;
537
+ };
538
+ newItems[j + 1 | 0] = x;
539
+ }
540
+ let m = buffer.length;
541
+ let merged = [];
542
+ let last = {
543
+ contents: undefined
544
+ };
545
+ let push = item => {
546
+ let l = last.contents;
547
+ if (l !== undefined && compareBufferItem(l, item) === 0) {
548
+ return;
549
+ }
550
+ merged.push(item);
551
+ last.contents = item;
552
+ };
553
+ let i$1 = 0;
554
+ let j$1 = 0;
555
+ while (i$1 < m && j$1 < n) {
556
+ let a = buffer[i$1];
557
+ let b = newItems[j$1];
558
+ if (compareBufferItem(a, b) <= 0) {
559
+ push(a);
560
+ i$1 = i$1 + 1 | 0;
561
+ } else {
562
+ push(b);
563
+ j$1 = j$1 + 1 | 0;
564
+ }
565
+ };
566
+ while (i$1 < m) {
567
+ push(buffer[i$1]);
568
+ i$1 = i$1 + 1 | 0;
569
+ };
570
+ while (j$1 < n) {
571
+ push(newItems[j$1]);
572
+ j$1 = j$1 + 1 | 0;
573
+ };
574
+ return merged;
575
+ }
576
+
496
577
  function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
497
578
  let newItemsCounter = 0;
498
579
  let latestOnBlockBlockNumber = fromBlock;
@@ -522,18 +603,19 @@ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, f
522
603
  return latestOnBlockBlockNumber;
523
604
  }
524
605
 
525
- function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOpt, knownHeightOpt) {
606
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, mutItemsSortedOpt, blockLagOpt, knownHeightOpt) {
526
607
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
608
+ let mutItemsSorted = mutItemsSortedOpt !== undefined ? mutItemsSortedOpt : false;
527
609
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
528
610
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
529
- let mutItemsRef = mutItems;
611
+ let base = mutItems !== undefined ? (
612
+ mutItemsSorted ? mutItems : mergeIntoBuffer([], mutItems)
613
+ ) : fetchState.buffer;
614
+ let blockItems = [];
530
615
  let onBlockRegistrations = fetchState.onBlockRegistrations;
531
616
  let latestOnBlockBlockNumber;
532
617
  if (onBlockRegistrations.length !== 0) {
533
- let mutItems$1 = mutItemsRef;
534
- let item = (
535
- mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
536
- )[fetchState.maxOnBlockBufferSize - 1 | 0];
618
+ let item = base[fetchState.maxOnBlockBufferSize - 1 | 0];
537
619
  let maxBlockNumber;
538
620
  if (item !== undefined) {
539
621
  maxBlockNumber = item.blockNumber;
@@ -542,20 +624,16 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
542
624
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
543
625
  maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
544
626
  }
545
- let mutItems$2 = mutItemsRef;
546
- let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
547
- mutItemsRef = mutItems$3;
548
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
627
+ latestOnBlockBlockNumber = appendOnBlockItems(blockItems, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
549
628
  } else {
550
629
  latestOnBlockBlockNumber = knownHeight;
551
630
  }
552
- let mutItems$4 = mutItemsRef;
553
631
  let updatedFetchState_startBlock = fetchState.startBlock;
554
632
  let updatedFetchState_endBlock = fetchState.endBlock;
555
633
  let updatedFetchState_normalSelection = fetchState.normalSelection;
556
634
  let updatedFetchState_contractConfigs = fetchState.contractConfigs;
557
635
  let updatedFetchState_chainId = fetchState.chainId;
558
- let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
636
+ let updatedFetchState_buffer = blockItems.length !== 0 ? mergeIntoBuffer(base, blockItems) : base;
559
637
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
560
638
  let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
561
639
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
@@ -665,10 +743,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
665
743
  mergeBlock: undefined,
666
744
  dynamicContract: isDynamic ? contractName : undefined,
667
745
  mutPendingQueries: [],
668
- prevQueryRange: 0,
669
- prevPrevQueryRange: 0,
670
- prevRangeSize: 0,
671
- latestBlockRangeUpdateBlock: 0
746
+ sourceRangeCapacity: 0,
747
+ prevSourceRangeCapacity: 0,
748
+ eventDensity: undefined,
749
+ latestSourceRangeCapacityUpdateBlock: 0
672
750
  });
673
751
  nextPartitionIndexRef = nextPartitionIndexRef + 1 | 0;
674
752
  };
@@ -716,10 +794,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
716
794
  mergeBlock: currentPBlock < nextPBlock ? nextPBlock : undefined,
717
795
  dynamicContract: currentP.dynamicContract,
718
796
  mutPendingQueries: currentP.mutPendingQueries,
719
- prevQueryRange: currentP.prevQueryRange,
720
- prevPrevQueryRange: currentP.prevPrevQueryRange,
721
- prevRangeSize: currentP.prevRangeSize,
722
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
797
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
798
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
799
+ eventDensity: currentP.eventDensity,
800
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
723
801
  });
724
802
  currentPRef = {
725
803
  id: nextP.id,
@@ -729,10 +807,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
729
807
  mergeBlock: nextP.mergeBlock,
730
808
  dynamicContract: nextP.dynamicContract,
731
809
  mutPendingQueries: nextP.mutPendingQueries,
732
- prevQueryRange: nextP.prevQueryRange,
733
- prevPrevQueryRange: nextP.prevPrevQueryRange,
734
- prevRangeSize: nextP.prevRangeSize,
735
- latestBlockRangeUpdateBlock: nextP.latestBlockRangeUpdateBlock
810
+ sourceRangeCapacity: nextP.sourceRangeCapacity,
811
+ prevSourceRangeCapacity: nextP.prevSourceRangeCapacity,
812
+ eventDensity: nextP.eventDensity,
813
+ latestSourceRangeCapacityUpdateBlock: nextP.latestSourceRangeCapacityUpdateBlock
736
814
  };
737
815
  } else {
738
816
  currentPRef = {
@@ -743,10 +821,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
743
821
  mergeBlock: currentP.mergeBlock,
744
822
  dynamicContract: currentP.dynamicContract,
745
823
  mutPendingQueries: currentP.mutPendingQueries,
746
- prevQueryRange: currentP.prevQueryRange,
747
- prevPrevQueryRange: currentP.prevPrevQueryRange,
748
- prevRangeSize: currentP.prevRangeSize,
749
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
824
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
825
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
826
+ eventDensity: currentP.eventDensity,
827
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
750
828
  };
751
829
  }
752
830
  }
@@ -893,10 +971,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
893
971
  mergeBlock: p.mergeBlock,
894
972
  dynamicContract: p.dynamicContract,
895
973
  mutPendingQueries: p.mutPendingQueries,
896
- prevQueryRange: p.prevQueryRange,
897
- prevPrevQueryRange: p.prevPrevQueryRange,
898
- prevRangeSize: p.prevRangeSize,
899
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
974
+ sourceRangeCapacity: p.sourceRangeCapacity,
975
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
976
+ eventDensity: p.eventDensity,
977
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
900
978
  };
901
979
  let addressesByContractName = {};
902
980
  addressesByContractName[contractName] = addresses;
@@ -908,10 +986,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
908
986
  mergeBlock: undefined,
909
987
  dynamicContract: contractName,
910
988
  mutPendingQueries: p.mutPendingQueries,
911
- prevQueryRange: p.prevQueryRange,
912
- prevPrevQueryRange: p.prevPrevQueryRange,
913
- prevRangeSize: p.prevRangeSize,
914
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
989
+ sourceRangeCapacity: p.sourceRangeCapacity,
990
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
991
+ eventDensity: p.eventDensity,
992
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
915
993
  });
916
994
  }
917
995
  } else {
@@ -923,10 +1001,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
923
1001
  mergeBlock: p.mergeBlock,
924
1002
  dynamicContract: contractName,
925
1003
  mutPendingQueries: p.mutPendingQueries,
926
- prevQueryRange: p.prevQueryRange,
927
- prevPrevQueryRange: p.prevPrevQueryRange,
928
- prevRangeSize: p.prevRangeSize,
929
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1004
+ sourceRangeCapacity: p.sourceRangeCapacity,
1005
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1006
+ eventDensity: p.eventDensity,
1007
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
930
1008
  };
931
1009
  }
932
1010
  }
@@ -937,11 +1015,11 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
937
1015
  }
938
1016
  IndexingAddresses.register(indexingAddresses, noEventsAddresses);
939
1017
  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);
1018
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined, undefined);
941
1019
  }
942
1020
 
943
- function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBlock, newItems) {
944
- let newItems$1 = newItems.filter(item => {
1021
+ function filterByClientAddress(items, indexingAddresses) {
1022
+ return items.filter(item => {
945
1023
  if (item.kind !== 0) {
946
1024
  return true;
947
1025
  }
@@ -952,7 +1030,10 @@ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBl
952
1030
  return true;
953
1031
  }
954
1032
  });
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);
1033
+ }
1034
+
1035
+ function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
1036
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems.length, latestFetchedBlock), newItems.length !== 0 ? mergeIntoBuffer(fetchState.buffer, newItems) : undefined, true, undefined, undefined);
956
1037
  }
957
1038
 
958
1039
  function startFetchingQueries(param, queries) {
@@ -995,7 +1076,7 @@ function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock,
995
1076
  exit = 1;
996
1077
  }
997
1078
  if (exit === 1) {
998
- let trustedDensity = getTrustedDensity(partition);
1079
+ let trustedDensity = partition.eventDensity;
999
1080
  let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
1000
1081
  let pushSingleQuery = (density, isChunk) => {
1001
1082
  let itemsTarget = densityItemsTarget(density * chunkItemsMultiplier, rangeFromBlock, rangeEndBlock, chainTargetBlock);
@@ -1051,24 +1132,6 @@ function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock,
1051
1132
  return cost.contents;
1052
1133
  }
1053
1134
 
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
1135
  function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star) {
1073
1136
  let knownHeight = param.knownHeight;
1074
1137
  let blockLag = param.blockLag;
@@ -1091,7 +1154,8 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star)
1091
1154
  shouldWaitForNewBlock = false;
1092
1155
  }
1093
1156
  }
1094
- let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount, param => []);
1157
+ let partitionsCount$1 = chainTargetItems <= 0 || shouldWaitForNewBlock ? 0 : partitionsCount;
1158
+ let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount$1, param => []);
1095
1159
  let computeQueryEndBlock = p => {
1096
1160
  let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1097
1161
  if (blockLag !== 0) {
@@ -1100,42 +1164,42 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star)
1100
1164
  return queryEndBlock;
1101
1165
  }
1102
1166
  };
1103
- let existingReservedByPartition = {};
1104
1167
  let chainReserved = 0;
1105
- for (let idx$1 = 0; idx$1 < partitionsCount; ++idx$1) {
1168
+ let reservations = [];
1169
+ let partitionIndexById = {};
1170
+ let candidates = [];
1171
+ let fillStates = [];
1172
+ for (let idx$1 = 0; idx$1 < partitionsCount$1; ++idx$1) {
1106
1173
  let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1107
1174
  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;
1175
+ partitionIndexById[partitionId$1] = idx$1;
1176
+ let pendingCount = p$1.mutPendingQueries.length;
1177
+ for (let pqIdx = 0; pqIdx < pendingCount; ++pqIdx) {
1178
+ let pq = p$1.mutPendingQueries[pqIdx];
1179
+ if (pq.fetchedBlock === undefined) {
1180
+ chainReserved = chainReserved + pq.itemsEst;
1181
+ reservations.push([
1182
+ pq.fromBlock,
1183
+ pq.itemsEst
1184
+ ]);
1185
+ }
1111
1186
  }
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;
1187
+ let queryEndBlock = computeQueryEndBlock(p$1);
1188
+ let maybeChunkRange = getMinHistoryRange(p$1);
1189
+ let cursor = p$1.latestFetchedBlock.blockNumber + 1 | 0;
1125
1190
  let canContinue = true;
1126
1191
  let chunksUsedThisCall = 0;
1127
1192
  let pqIdx$1 = 0;
1128
1193
  while (pqIdx$1 < pendingCount && canContinue) {
1129
- let pq = p$2.mutPendingQueries[pqIdx$1];
1130
- if (pq.fromBlock > cursor) {
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;
1194
+ let pq$1 = p$1.mutPendingQueries[pqIdx$1];
1195
+ if (pq$1.fromBlock > cursor) {
1196
+ let beforeLen = candidates.length;
1197
+ pushGapFillQueries(candidates, partitionId$1, cursor, Utils.$$Math.minOptInt(pq$1.fromBlock - 1 | 0, queryEndBlock), knownHeight, chainTargetBlock, maybeChunkRange, (12 - pendingCount | 0) - chunksUsedThisCall | 0, p$1, chainTargetItems / partitionsCount$1, chunkItemsMultiplier, p$1.selection, p$1.addressesByContractName);
1198
+ chunksUsedThisCall = chunksUsedThisCall + (candidates.length - beforeLen | 0) | 0;
1135
1199
  }
1136
- let toBlock = pq.toBlock;
1137
- if (toBlock !== undefined && pq.isChunk) {
1138
- let match = pq.fetchedBlock;
1200
+ let toBlock = pq$1.toBlock;
1201
+ if (toBlock !== undefined && pq$1.isChunk) {
1202
+ let match = pq$1.fetchedBlock;
1139
1203
  if (match !== undefined) {
1140
1204
  let blockNumber = match.blockNumber;
1141
1205
  cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
@@ -1149,26 +1213,17 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star)
1149
1213
  };
1150
1214
  if (canContinue) {
1151
1215
  fillStates.push({
1152
- partitionId: partitionId$2,
1153
- p: p$2,
1216
+ partitionId: partitionId$1,
1217
+ p: p$1,
1154
1218
  cursor: cursor,
1155
1219
  chunksUsedThisCall: chunksUsedThisCall,
1156
1220
  pendingCount: pendingCount,
1157
1221
  queryEndBlock: queryEndBlock,
1158
- maybeChunkRange: maybeChunkRange,
1159
- bucket: bucket
1222
+ maybeChunkRange: maybeChunkRange
1160
1223
  });
1161
1224
  }
1162
1225
  }
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;
1169
- }
1170
- reservedByPartition[fs.partitionId] = cost;
1171
- });
1226
+ let freshBudget = Primitive_float.max(0, chainTargetItems - chainReserved);
1172
1227
  let isInRange = fs => {
1173
1228
  let tmp = false;
1174
1229
  if (fs.cursor <= chainTargetBlock) {
@@ -1182,48 +1237,53 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star)
1182
1237
  }
1183
1238
  };
1184
1239
  let inRangeStates = fillStates.filter(isInRange);
1185
- let emitQueries = (fs, budget) => {
1240
+ let inRangeCount = inRangeStates.length;
1241
+ let probeShare = inRangeCount === 0 ? 0 : freshBudget / inRangeCount;
1242
+ let frontierCursor = Stdlib_Array.reduce(inRangeStates, chainTargetBlock, (min, fs) => {
1243
+ if (fs.cursor < min) {
1244
+ return fs.cursor;
1245
+ } else {
1246
+ return min;
1247
+ }
1248
+ });
1249
+ let rangeToTarget = (chainTargetBlock - frontierCursor | 0) + 1 | 0;
1250
+ let rangeTargetDensity = inRangeCount > 0 && rangeToTarget > 0 ? freshBudget / rangeToTarget : 0;
1251
+ inRangeStates.forEach(fs => {
1186
1252
  let p = fs.p;
1187
1253
  let eb = fs.queryEndBlock;
1188
1254
  let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1189
1255
  let match = fs.maybeChunkRange;
1190
- let match$1 = getTrustedDensity(p);
1256
+ let match$1 = p.eventDensity;
1191
1257
  if (match !== undefined && match$1 !== undefined && match$1 > 0) {
1192
1258
  let chunkSize = Js_math.ceil_int(match * 1.8);
1193
1259
  let maxChunksRemaining = (12 - fs.pendingCount | 0) - fs.chunksUsedThisCall | 0;
1194
- let consumed = 0;
1260
+ let chunkStartCeiling = Primitive_int.min(maxBlock, chainTargetBlock);
1195
1261
  let created = 0;
1196
1262
  let chunkFromBlock = fs.cursor;
1197
- let budgetLeft = true;
1198
- while (budgetLeft && created < maxChunksRemaining && chunkFromBlock <= Primitive_int.min(maxBlock, chainTargetBlock)) {
1263
+ let generatedItems = 0;
1264
+ while (created < maxChunksRemaining && chunkFromBlock <= chunkStartCeiling && generatedItems <= freshBudget) {
1199
1265
  let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1200
1266
  let rawEst = match$1 * ((chunkToBlock - chunkFromBlock | 0) + 1 | 0);
1201
1267
  let itemsEst = Primitive_int.max(1, Math.ceil(rawEst) | 0);
1202
1268
  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
- }
1269
+ candidates.push({
1270
+ partitionId: fs.partitionId,
1271
+ fromBlock: chunkFromBlock,
1272
+ toBlock: chunkToBlock,
1273
+ isChunk: true,
1274
+ itemsTarget: itemsTarget,
1275
+ itemsEst: itemsEst,
1276
+ selection: p.selection,
1277
+ addressesByContractName: p.addressesByContractName
1278
+ });
1279
+ generatedItems = generatedItems + itemsEst;
1280
+ chunkFromBlock = chunkToBlock + 1 | 0;
1281
+ created = created + 1 | 0;
1220
1282
  };
1221
- fs.cursor = chunkFromBlock;
1222
- fs.chunksUsedThisCall = fs.chunksUsedThisCall + created | 0;
1223
- return consumed;
1283
+ return;
1224
1284
  }
1225
- let itemsTarget$1 = Primitive_int.max(1, Math.round(budget) | 0);
1226
- fs.bucket.push({
1285
+ let itemsTarget$1 = rangeToTarget > 0 ? Primitive_int.max(1, Math.round(rangeTargetDensity * ((chainTargetBlock - fs.cursor | 0) + 1 | 0) / inRangeCount) | 0) : Primitive_int.max(1, Math.round(probeShare) | 0);
1286
+ candidates.push({
1227
1287
  partitionId: fs.partitionId,
1228
1288
  fromBlock: fs.cursor,
1229
1289
  toBlock: fs.queryEndBlock,
@@ -1233,32 +1293,53 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star)
1233
1293
  selection: p.selection,
1234
1294
  addressesByContractName: p.addressesByContractName
1235
1295
  });
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
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;
1296
+ });
1297
+ let acceptanceStream = [];
1298
+ candidates.forEach(query => {
1299
+ acceptanceStream.push([
1300
+ query.fromBlock,
1301
+ query.itemsEst,
1302
+ query
1303
+ ]);
1304
+ });
1305
+ reservations.forEach(param => {
1306
+ acceptanceStream.push([
1307
+ param[0],
1308
+ param[1],
1309
+ undefined
1310
+ ]);
1311
+ });
1312
+ acceptanceStream.sort((param, param$1) => {
1313
+ let bFrom = param$1[0];
1314
+ let aFrom = param[0];
1315
+ if (aFrom !== bFrom) {
1316
+ return Primitive_int.compare(aFrom, bFrom);
1317
+ }
1318
+ let bQuery = param$1[2];
1319
+ if (param[2] !== undefined) {
1320
+ if (bQuery !== undefined) {
1321
+ return 0;
1322
+ } else {
1323
+ return 1;
1259
1324
  }
1260
- });
1261
- notFilledPartitions = next;
1325
+ } else if (bQuery !== undefined) {
1326
+ return -1;
1327
+ } else {
1328
+ return 0;
1329
+ }
1330
+ });
1331
+ let streamCount = acceptanceStream.length;
1332
+ let remainingBudget = chainTargetItems;
1333
+ let acceptIdx = 0;
1334
+ while (remainingBudget > 0 && acceptIdx < streamCount) {
1335
+ let match$1 = acceptanceStream[acceptIdx];
1336
+ let maybeQuery = match$1[2];
1337
+ if (maybeQuery !== undefined) {
1338
+ let partitionIdx = partitionIndexById[maybeQuery.partitionId];
1339
+ queriesByPartitionIndex[partitionIdx].push(maybeQuery);
1340
+ }
1341
+ remainingBudget = remainingBudget - match$1[1];
1342
+ acceptIdx = acceptIdx + 1 | 0;
1262
1343
  };
1263
1344
  let queries = queriesByPartitionIndex.flat();
1264
1345
  if (Utils.$$Array.isEmpty(queries)) {
@@ -1340,10 +1421,10 @@ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, add
1340
1421
  mergeBlock: undefined,
1341
1422
  dynamicContract: undefined,
1342
1423
  mutPendingQueries: [],
1343
- prevQueryRange: 0,
1344
- prevPrevQueryRange: 0,
1345
- prevRangeSize: 0,
1346
- latestBlockRangeUpdateBlock: 0
1424
+ sourceRangeCapacity: 0,
1425
+ prevSourceRangeCapacity: 0,
1426
+ eventDensity: undefined,
1427
+ latestSourceRangeCapacityUpdateBlock: 0
1347
1428
  });
1348
1429
  }
1349
1430
  let normalSelection = {
@@ -1478,10 +1559,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1478
1559
  mergeBlock: mergeBlock$1,
1479
1560
  dynamicContract: p.dynamicContract,
1480
1561
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1481
- prevQueryRange: p.prevQueryRange,
1482
- prevPrevQueryRange: p.prevPrevQueryRange,
1483
- prevRangeSize: p.prevRangeSize,
1484
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1562
+ sourceRangeCapacity: p.sourceRangeCapacity,
1563
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1564
+ eventDensity: p.eventDensity,
1565
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1485
1566
  });
1486
1567
  }
1487
1568
  }
@@ -1499,10 +1580,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1499
1580
  mergeBlock: p.mergeBlock,
1500
1581
  dynamicContract: p.dynamicContract,
1501
1582
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1502
- prevQueryRange: p.prevQueryRange,
1503
- prevPrevQueryRange: p.prevPrevQueryRange,
1504
- prevRangeSize: p.prevRangeSize,
1505
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1583
+ sourceRangeCapacity: p.sourceRangeCapacity,
1584
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1585
+ eventDensity: p.eventDensity,
1586
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1506
1587
  });
1507
1588
  }
1508
1589
  }
@@ -1525,7 +1606,7 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1525
1606
  let tmp;
1526
1607
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1527
1608
  return tmp <= targetBlockNumber;
1528
- }), undefined, undefined);
1609
+ }), true, undefined, undefined);
1529
1610
  }
1530
1611
 
1531
1612
  function resetPendingQueries(fetchState) {
@@ -1543,10 +1624,10 @@ function resetPendingQueries(fetchState) {
1543
1624
  mergeBlock: partition.mergeBlock,
1544
1625
  dynamicContract: partition.dynamicContract,
1545
1626
  mutPendingQueries: kept,
1546
- prevQueryRange: partition.prevQueryRange,
1547
- prevPrevQueryRange: partition.prevPrevQueryRange,
1548
- prevRangeSize: partition.prevRangeSize,
1549
- latestBlockRangeUpdateBlock: partition.latestBlockRangeUpdateBlock
1627
+ sourceRangeCapacity: partition.sourceRangeCapacity,
1628
+ prevSourceRangeCapacity: partition.prevSourceRangeCapacity,
1629
+ eventDensity: partition.eventDensity,
1630
+ latestSourceRangeCapacityUpdateBlock: partition.latestSourceRangeCapacityUpdateBlock
1550
1631
  };
1551
1632
  }
1552
1633
  }
@@ -1622,7 +1703,7 @@ function getProgressPercentage(fetchState) {
1622
1703
  if (firstEventBlock === undefined) {
1623
1704
  return 0;
1624
1705
  }
1625
- let totalRange = fetchState.knownHeight - firstEventBlock | 0;
1706
+ let totalRange = (fetchState.knownHeight - fetchState.blockLag | 0) - firstEventBlock | 0;
1626
1707
  if (totalRange <= 0) {
1627
1708
  return 0;
1628
1709
  }
@@ -1678,7 +1759,7 @@ function getProgressBlockNumberAt(fetchState, index) {
1678
1759
  function updateKnownHeight(fetchState, knownHeight) {
1679
1760
  if (knownHeight > fetchState.knownHeight) {
1680
1761
  Prometheus.IndexingKnownHeight.set(knownHeight, fetchState.chainId);
1681
- return updateInternal(fetchState, undefined, undefined, undefined, knownHeight);
1762
+ return updateInternal(fetchState, undefined, undefined, undefined, undefined, knownHeight);
1682
1763
  } else {
1683
1764
  return fetchState;
1684
1765
  }
@@ -1698,7 +1779,9 @@ export {
1698
1779
  bufferBlockNumber,
1699
1780
  bufferBlock,
1700
1781
  bufferReadyCount,
1782
+ getRegistrationIndex,
1701
1783
  compareBufferItem,
1784
+ mergeIntoBuffer,
1702
1785
  blockItemLogIndex,
1703
1786
  appendOnBlockItems,
1704
1787
  updateInternal,
@@ -1707,11 +1790,11 @@ export {
1707
1790
  addressesByContractNameGetAll,
1708
1791
  createPartitionsFromIndexingAddresses,
1709
1792
  registerDynamicContracts,
1793
+ filterByClientAddress,
1710
1794
  handleQueryResult,
1711
1795
  startFetchingQueries,
1712
1796
  maxPendingChunksPerPartition,
1713
1797
  pushGapFillQueries,
1714
- waterLevel,
1715
1798
  getNextQuery,
1716
1799
  hasReadyItem,
1717
1800
  getReadyItemsCount,