envio 3.3.0-alpha.9 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/index.d.ts +19 -0
  2. package/licenses/CLA.md +35 -0
  3. package/licenses/EULA.md +67 -0
  4. package/licenses/LICENSE.md +45 -0
  5. package/licenses/README.md +35 -0
  6. package/package.json +9 -8
  7. package/src/Batch.res.mjs +1 -1
  8. package/src/BatchProcessing.res +0 -7
  9. package/src/BatchProcessing.res.mjs +1 -8
  10. package/src/ChainFetching.res +30 -18
  11. package/src/ChainFetching.res.mjs +23 -13
  12. package/src/ChainState.res +134 -55
  13. package/src/ChainState.res.mjs +78 -36
  14. package/src/ChainState.resi +14 -2
  15. package/src/Config.res +9 -5
  16. package/src/Config.res.mjs +2 -2
  17. package/src/Core.res +20 -0
  18. package/src/Core.res.mjs +12 -0
  19. package/src/CrossChainState.res +115 -34
  20. package/src/CrossChainState.res.mjs +37 -14
  21. package/src/EffectState.res +100 -0
  22. package/src/EffectState.res.mjs +74 -0
  23. package/src/EffectState.resi +32 -0
  24. package/src/Envio.res +25 -7
  25. package/src/Envio.res.mjs +15 -19
  26. package/src/EventConfigBuilder.res +21 -46
  27. package/src/EventConfigBuilder.res.mjs +18 -25
  28. package/src/EventProcessing.res +8 -5
  29. package/src/EventProcessing.res.mjs +2 -1
  30. package/src/FetchState.res +814 -467
  31. package/src/FetchState.res.mjs +541 -369
  32. package/src/HandlerRegister.res +3 -1
  33. package/src/HandlerRegister.res.mjs +3 -2
  34. package/src/InMemoryStore.res +14 -26
  35. package/src/InMemoryStore.res.mjs +6 -19
  36. package/src/IndexerState.res +15 -39
  37. package/src/IndexerState.res.mjs +18 -29
  38. package/src/IndexerState.resi +2 -10
  39. package/src/Internal.res +98 -14
  40. package/src/Internal.res.mjs +96 -2
  41. package/src/LoadLayer.res +44 -24
  42. package/src/LoadLayer.res.mjs +43 -20
  43. package/src/LoadLayer.resi +1 -0
  44. package/src/LogSelection.res +0 -62
  45. package/src/LogSelection.res.mjs +0 -74
  46. package/src/Metrics.res +1 -1
  47. package/src/Metrics.res.mjs +1 -1
  48. package/src/Persistence.res +12 -3
  49. package/src/PgStorage.res +155 -82
  50. package/src/PgStorage.res.mjs +130 -77
  51. package/src/Prometheus.res +20 -10
  52. package/src/Prometheus.res.mjs +22 -10
  53. package/src/PruneStaleHistory.res +146 -35
  54. package/src/PruneStaleHistory.res.mjs +114 -20
  55. package/src/RawEvent.res +2 -2
  56. package/src/Rollback.res +32 -13
  57. package/src/Rollback.res.mjs +24 -11
  58. package/src/SimulateDeadInputTracker.res +1 -1
  59. package/src/SimulateItems.res +38 -6
  60. package/src/SimulateItems.res.mjs +28 -9
  61. package/src/TestIndexer.res +2 -2
  62. package/src/TestIndexer.res.mjs +2 -2
  63. package/src/TopicFilter.res +1 -25
  64. package/src/TopicFilter.res.mjs +0 -74
  65. package/src/UserContext.res +62 -23
  66. package/src/UserContext.res.mjs +26 -6
  67. package/src/Writing.res +60 -21
  68. package/src/Writing.res.mjs +27 -9
  69. package/src/bindings/NodeJs.res +5 -0
  70. package/src/bindings/Viem.res +0 -5
  71. package/src/sources/EventRouter.res +0 -21
  72. package/src/sources/EventRouter.res.mjs +0 -12
  73. package/src/sources/EvmChain.res +2 -27
  74. package/src/sources/EvmChain.res.mjs +2 -23
  75. package/src/sources/EvmRpcClient.res +58 -23
  76. package/src/sources/EvmRpcClient.res.mjs +11 -5
  77. package/src/sources/HyperSync.res +9 -38
  78. package/src/sources/HyperSync.res.mjs +16 -28
  79. package/src/sources/HyperSync.resi +2 -2
  80. package/src/sources/HyperSyncClient.res +88 -11
  81. package/src/sources/HyperSyncClient.res.mjs +39 -6
  82. package/src/sources/HyperSyncSource.res +18 -199
  83. package/src/sources/HyperSyncSource.res.mjs +9 -128
  84. package/src/sources/Rpc.res +0 -32
  85. package/src/sources/Rpc.res.mjs +1 -46
  86. package/src/sources/RpcSource.res +129 -522
  87. package/src/sources/RpcSource.res.mjs +161 -366
  88. package/src/sources/SimulateSource.res +37 -19
  89. package/src/sources/SimulateSource.res.mjs +27 -10
  90. package/src/sources/SourceManager.res +3 -7
  91. package/src/sources/SourceManager.res.mjs +2 -7
  92. package/src/sources/SourceManager.resi +0 -2
  93. package/src/sources/SvmHyperSyncSource.res +13 -3
  94. 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
  }
@@ -318,17 +320,25 @@ function make(partitions, maxAddrInPartition, nextPartitionIndex, dynamicContrac
318
320
 
319
321
  function consumeFetchedQueries(mutPendingQueries, initialLatestFetchedBlock) {
320
322
  let latestFetchedBlock = initialLatestFetchedBlock;
321
- while ((() => {
322
- let tmp = false;
323
- if (mutPendingQueries.length !== 0) {
324
- let pq = Utils.$$Array.firstUnsafe(mutPendingQueries);
325
- tmp = pq.fetchedBlock !== undefined && pq.fromBlock <= (latestFetchedBlock.blockNumber + 1 | 0);
323
+ let consumedCount = 0;
324
+ let canContinue = true;
325
+ while (canContinue) {
326
+ let match = mutPendingQueries[consumedCount];
327
+ if (match !== undefined) {
328
+ let fetchedBlock = match.fetchedBlock;
329
+ if (fetchedBlock !== undefined && match.fromBlock <= (latestFetchedBlock.blockNumber + 1 | 0)) {
330
+ latestFetchedBlock = fetchedBlock;
331
+ consumedCount = consumedCount + 1 | 0;
332
+ } else {
333
+ canContinue = false;
326
334
  }
327
- return tmp;
328
- })()) {
329
- let removedQuery = mutPendingQueries.shift();
330
- latestFetchedBlock = removedQuery.fetchedBlock;
335
+ } else {
336
+ canContinue = false;
337
+ }
331
338
  };
339
+ if (consumedCount > 0) {
340
+ mutPendingQueries.splice(0, consumedCount);
341
+ }
332
342
  return latestFetchedBlock;
333
343
  }
334
344
 
@@ -358,67 +368,112 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
358
368
  let pendingQuery = getPendingQueryOrThrow(p$1, query.fromBlock);
359
369
  pendingQuery.fetchedBlock = latestFetchedBlock;
360
370
  let blockRange = (latestFetchedBlock.blockNumber - query.fromBlock | 0) + 1 | 0;
361
- let shouldUpdateBlockRange = false;
362
- if (latestFetchedBlock.blockNumber > p$1.latestBlockRangeUpdateBlock) {
371
+ let observedEventDensity = itemsCount / blockRange;
372
+ let eventDensity = p$1.eventDensity;
373
+ let updatedEventDensity = eventDensity !== undefined ? (eventDensity + observedEventDensity) / 2 : observedEventDensity;
374
+ let shouldUpdateSourceRangeCapacity = false;
375
+ if (latestFetchedBlock.blockNumber > p$1.latestSourceRangeCapacityUpdateBlock) {
363
376
  let queryToBlock = query.toBlock;
364
377
  let tmp;
365
378
  if (queryToBlock !== undefined) {
366
- if (latestFetchedBlock.blockNumber >= queryToBlock) {
379
+ if (latestFetchedBlock.blockNumber < queryToBlock) {
380
+ tmp = itemsCount < query.itemsTarget;
381
+ } else {
367
382
  let minHistoryRange = getMinHistoryRange(p$1);
368
383
  tmp = minHistoryRange !== undefined ? ((queryToBlock - query.fromBlock | 0) + 1 | 0) >= minHistoryRange : false;
369
- } else {
370
- tmp = true;
371
384
  }
372
385
  } else {
373
386
  tmp = latestFetchedBlock.blockNumber < (knownHeight - 10 | 0);
374
387
  }
375
- shouldUpdateBlockRange = tmp;
388
+ shouldUpdateSourceRangeCapacity = tmp;
376
389
  }
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;
390
+ let updatedSourceRangeCapacity = shouldUpdateSourceRangeCapacity ? blockRange : p$1.sourceRangeCapacity;
391
+ let updatedPrevSourceRangeCapacity = shouldUpdateSourceRangeCapacity ? p$1.sourceRangeCapacity : p$1.prevSourceRangeCapacity;
380
392
  let mutPendingQueries = p$1.mutPendingQueries;
381
393
  let latestFetchedBlock$1 = p$1.latestFetchedBlock;
382
- while ((() => {
383
- let tmp = false;
384
- if (mutPendingQueries.length !== 0) {
385
- let pq = Utils.$$Array.firstUnsafe(mutPendingQueries);
386
- tmp = pq.fetchedBlock !== undefined && pq.fromBlock <= (latestFetchedBlock$1.blockNumber + 1 | 0);
394
+ let consumedCount = 0;
395
+ let canContinue = true;
396
+ while (canContinue) {
397
+ let match = mutPendingQueries[consumedCount];
398
+ if (match !== undefined) {
399
+ let fetchedBlock = match.fetchedBlock;
400
+ if (fetchedBlock !== undefined && match.fromBlock <= (latestFetchedBlock$1.blockNumber + 1 | 0)) {
401
+ latestFetchedBlock$1 = fetchedBlock;
402
+ consumedCount = consumedCount + 1 | 0;
403
+ } else {
404
+ canContinue = false;
387
405
  }
388
- return tmp;
389
- })()) {
390
- let removedQuery = mutPendingQueries.shift();
391
- latestFetchedBlock$1 = removedQuery.fetchedBlock;
406
+ } else {
407
+ canContinue = false;
408
+ }
392
409
  };
410
+ if (consumedCount > 0) {
411
+ mutPendingQueries.splice(0, consumedCount);
412
+ }
393
413
  let updatedLatestFetchedBlock = latestFetchedBlock$1;
394
414
  let mergeBlock = p$1.mergeBlock;
395
415
  let partitionReachedMergeBlock = mergeBlock !== undefined ? updatedLatestFetchedBlock.blockNumber >= mergeBlock : false;
396
416
  if (partitionReachedMergeBlock) {
397
417
  Utils.Dict.deleteInPlace(mutEntities, p$1.id);
398
- } else {
399
- let updatedMainPartition_id = p$1.id;
400
- let updatedMainPartition_selection = p$1.selection;
401
- let updatedMainPartition_addressesByContractName = p$1.addressesByContractName;
402
- let updatedMainPartition_mergeBlock = p$1.mergeBlock;
403
- let updatedMainPartition_dynamicContract = p$1.dynamicContract;
404
- let updatedMainPartition_mutPendingQueries = p$1.mutPendingQueries;
405
- let updatedMainPartition_latestBlockRangeUpdateBlock = shouldUpdateBlockRange ? latestFetchedBlock.blockNumber : p$1.latestBlockRangeUpdateBlock;
406
- let updatedMainPartition = {
407
- id: updatedMainPartition_id,
408
- latestFetchedBlock: updatedLatestFetchedBlock,
409
- selection: updatedMainPartition_selection,
410
- addressesByContractName: updatedMainPartition_addressesByContractName,
411
- mergeBlock: updatedMainPartition_mergeBlock,
412
- dynamicContract: updatedMainPartition_dynamicContract,
413
- mutPendingQueries: updatedMainPartition_mutPendingQueries,
414
- prevQueryRange: updatedPrevQueryRange,
415
- prevPrevQueryRange: updatedPrevPrevQueryRange,
416
- prevRangeSize: updatedPrevRangeSize,
417
- latestBlockRangeUpdateBlock: updatedMainPartition_latestBlockRangeUpdateBlock
418
+ return make(Object.values(mutEntities), optimizedPartitions.maxAddrInPartition, optimizedPartitions.nextPartitionIndex, optimizedPartitions.dynamicContracts);
419
+ }
420
+ let updatedMainPartition_id = p$1.id;
421
+ let updatedMainPartition_selection = p$1.selection;
422
+ let updatedMainPartition_addressesByContractName = p$1.addressesByContractName;
423
+ let updatedMainPartition_mergeBlock = p$1.mergeBlock;
424
+ let updatedMainPartition_dynamicContract = p$1.dynamicContract;
425
+ let updatedMainPartition_mutPendingQueries = p$1.mutPendingQueries;
426
+ let updatedMainPartition_latestSourceRangeCapacityUpdateBlock = shouldUpdateSourceRangeCapacity ? latestFetchedBlock.blockNumber : p$1.latestSourceRangeCapacityUpdateBlock;
427
+ let updatedMainPartition = {
428
+ id: updatedMainPartition_id,
429
+ latestFetchedBlock: updatedLatestFetchedBlock,
430
+ selection: updatedMainPartition_selection,
431
+ addressesByContractName: updatedMainPartition_addressesByContractName,
432
+ mergeBlock: updatedMainPartition_mergeBlock,
433
+ dynamicContract: updatedMainPartition_dynamicContract,
434
+ mutPendingQueries: updatedMainPartition_mutPendingQueries,
435
+ sourceRangeCapacity: updatedSourceRangeCapacity,
436
+ prevSourceRangeCapacity: updatedPrevSourceRangeCapacity,
437
+ eventDensity: updatedEventDensity,
438
+ latestSourceRangeCapacityUpdateBlock: updatedMainPartition_latestSourceRangeCapacityUpdateBlock
439
+ };
440
+ mutEntities[p$1.id] = updatedMainPartition;
441
+ if (optimizedPartitions.dynamicContracts.size !== 0) {
442
+ return make(Object.values(mutEntities), optimizedPartitions.maxAddrInPartition, optimizedPartitions.nextPartitionIndex, optimizedPartitions.dynamicContracts);
443
+ }
444
+ let ids = optimizedPartitions.idsInAscOrder;
445
+ let count = ids.length;
446
+ let idx = ids.indexOf(p$1.id);
447
+ let isAfter = jdx => {
448
+ if (jdx < count) {
449
+ return mutEntities[ids[jdx]].latestFetchedBlock.blockNumber < updatedLatestFetchedBlock.blockNumber;
450
+ } else {
451
+ return false;
452
+ }
453
+ };
454
+ if (!isAfter(idx + 1 | 0)) {
455
+ return {
456
+ idsInAscOrder: optimizedPartitions.idsInAscOrder,
457
+ entities: mutEntities,
458
+ maxAddrInPartition: optimizedPartitions.maxAddrInPartition,
459
+ nextPartitionIndex: optimizedPartitions.nextPartitionIndex,
460
+ dynamicContracts: optimizedPartitions.dynamicContracts
418
461
  };
419
- mutEntities[p$1.id] = updatedMainPartition;
420
462
  }
421
- return make(Object.values(mutEntities), optimizedPartitions.maxAddrInPartition, optimizedPartitions.nextPartitionIndex, optimizedPartitions.dynamicContracts);
463
+ let reordered = ids.slice();
464
+ let jdx = idx;
465
+ while (isAfter(jdx + 1 | 0)) {
466
+ reordered[jdx] = ids[jdx + 1 | 0];
467
+ jdx = jdx + 1 | 0;
468
+ };
469
+ reordered[jdx] = p$1.id;
470
+ return {
471
+ idsInAscOrder: reordered,
472
+ entities: mutEntities,
473
+ maxAddrInPartition: optimizedPartitions.maxAddrInPartition,
474
+ nextPartitionIndex: optimizedPartitions.nextPartitionIndex,
475
+ dynamicContracts: optimizedPartitions.dynamicContracts
476
+ };
422
477
  }
423
478
 
424
479
  function getLatestFullyFetchedBlock(optimizedPartitions) {
@@ -484,13 +539,90 @@ function bufferReadyCount(fetchState) {
484
539
  return lo;
485
540
  }
486
541
 
542
+ function getRegistrationIndex(item) {
543
+ if (item.kind === 0) {
544
+ return item.onEventRegistration.index;
545
+ } else {
546
+ return item.onBlockRegistration.index;
547
+ }
548
+ }
549
+
487
550
  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);
551
+ let ba = a.blockNumber;
552
+ let bb = b.blockNumber;
553
+ if (ba !== bb) {
554
+ if (ba < bb) {
555
+ return -1;
556
+ } else {
557
+ return 1;
558
+ }
559
+ }
560
+ let la = a.logIndex;
561
+ let lb = b.logIndex;
562
+ if (la !== lb) {
563
+ if (la < lb) {
564
+ return -1;
565
+ } else {
566
+ return 1;
567
+ }
568
+ }
569
+ let ia = getRegistrationIndex(a);
570
+ let ib = getRegistrationIndex(b);
571
+ if (ia < ib) {
572
+ return -1;
573
+ } else if (ia > ib) {
574
+ return 1;
491
575
  } else {
492
- return blockOrdering;
576
+ return 0;
577
+ }
578
+ }
579
+
580
+ function mergeIntoBuffer(buffer, newItems) {
581
+ let n = newItems.length;
582
+ for (let i = 1; i < n; ++i) {
583
+ let x = newItems[i];
584
+ let j = i - 1 | 0;
585
+ while (j >= 0 && compareBufferItem(newItems[j], x) > 0) {
586
+ newItems[j + 1 | 0] = newItems[j];
587
+ j = j - 1 | 0;
588
+ };
589
+ newItems[j + 1 | 0] = x;
493
590
  }
591
+ let m = buffer.length;
592
+ let merged = [];
593
+ let last = {
594
+ contents: undefined
595
+ };
596
+ let push = item => {
597
+ let l = last.contents;
598
+ if (l !== undefined && compareBufferItem(l, item) === 0) {
599
+ return;
600
+ }
601
+ merged.push(item);
602
+ last.contents = item;
603
+ };
604
+ let i$1 = 0;
605
+ let j$1 = 0;
606
+ while (i$1 < m && j$1 < n) {
607
+ let a = buffer[i$1];
608
+ let b = newItems[j$1];
609
+ if (compareBufferItem(a, b) <= 0) {
610
+ push(a);
611
+ i$1 = i$1 + 1 | 0;
612
+ } else {
613
+ push(b);
614
+ j$1 = j$1 + 1 | 0;
615
+ }
616
+ };
617
+ while (i$1 < m) {
618
+ push(buffer[i$1]);
619
+ i$1 = i$1 + 1 | 0;
620
+ };
621
+ while (j$1 < n) {
622
+ push(newItems[j$1]);
623
+ j$1 = j$1 + 1 | 0;
624
+ };
625
+ return merged;
494
626
  }
495
627
 
496
628
  function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
@@ -522,18 +654,19 @@ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, f
522
654
  return latestOnBlockBlockNumber;
523
655
  }
524
656
 
525
- function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOpt, knownHeightOpt) {
657
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, mutItemsSortedOpt, blockLagOpt, knownHeightOpt) {
526
658
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
659
+ let mutItemsSorted = mutItemsSortedOpt !== undefined ? mutItemsSortedOpt : false;
527
660
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
528
661
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
529
- let mutItemsRef = mutItems;
662
+ let base = mutItems !== undefined ? (
663
+ mutItemsSorted ? mutItems : mergeIntoBuffer([], mutItems)
664
+ ) : fetchState.buffer;
665
+ let blockItems = [];
530
666
  let onBlockRegistrations = fetchState.onBlockRegistrations;
531
667
  let latestOnBlockBlockNumber;
532
668
  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];
669
+ let item = base[fetchState.maxOnBlockBufferSize - 1 | 0];
537
670
  let maxBlockNumber;
538
671
  if (item !== undefined) {
539
672
  maxBlockNumber = item.blockNumber;
@@ -542,20 +675,16 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
542
675
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
543
676
  maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
544
677
  }
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);
678
+ latestOnBlockBlockNumber = appendOnBlockItems(blockItems, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
549
679
  } else {
550
680
  latestOnBlockBlockNumber = knownHeight;
551
681
  }
552
- let mutItems$4 = mutItemsRef;
553
682
  let updatedFetchState_startBlock = fetchState.startBlock;
554
683
  let updatedFetchState_endBlock = fetchState.endBlock;
555
684
  let updatedFetchState_normalSelection = fetchState.normalSelection;
556
685
  let updatedFetchState_contractConfigs = fetchState.contractConfigs;
557
686
  let updatedFetchState_chainId = fetchState.chainId;
558
- let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
687
+ let updatedFetchState_buffer = blockItems.length !== 0 ? mergeIntoBuffer(base, blockItems) : base;
559
688
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
560
689
  let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
561
690
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
@@ -665,10 +794,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
665
794
  mergeBlock: undefined,
666
795
  dynamicContract: isDynamic ? contractName : undefined,
667
796
  mutPendingQueries: [],
668
- prevQueryRange: 0,
669
- prevPrevQueryRange: 0,
670
- prevRangeSize: 0,
671
- latestBlockRangeUpdateBlock: 0
797
+ sourceRangeCapacity: 0,
798
+ prevSourceRangeCapacity: 0,
799
+ eventDensity: undefined,
800
+ latestSourceRangeCapacityUpdateBlock: 0
672
801
  });
673
802
  nextPartitionIndexRef = nextPartitionIndexRef + 1 | 0;
674
803
  };
@@ -716,10 +845,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
716
845
  mergeBlock: currentPBlock < nextPBlock ? nextPBlock : undefined,
717
846
  dynamicContract: currentP.dynamicContract,
718
847
  mutPendingQueries: currentP.mutPendingQueries,
719
- prevQueryRange: currentP.prevQueryRange,
720
- prevPrevQueryRange: currentP.prevPrevQueryRange,
721
- prevRangeSize: currentP.prevRangeSize,
722
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
848
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
849
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
850
+ eventDensity: currentP.eventDensity,
851
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
723
852
  });
724
853
  currentPRef = {
725
854
  id: nextP.id,
@@ -729,10 +858,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
729
858
  mergeBlock: nextP.mergeBlock,
730
859
  dynamicContract: nextP.dynamicContract,
731
860
  mutPendingQueries: nextP.mutPendingQueries,
732
- prevQueryRange: nextP.prevQueryRange,
733
- prevPrevQueryRange: nextP.prevPrevQueryRange,
734
- prevRangeSize: nextP.prevRangeSize,
735
- latestBlockRangeUpdateBlock: nextP.latestBlockRangeUpdateBlock
861
+ sourceRangeCapacity: nextP.sourceRangeCapacity,
862
+ prevSourceRangeCapacity: nextP.prevSourceRangeCapacity,
863
+ eventDensity: nextP.eventDensity,
864
+ latestSourceRangeCapacityUpdateBlock: nextP.latestSourceRangeCapacityUpdateBlock
736
865
  };
737
866
  } else {
738
867
  currentPRef = {
@@ -743,10 +872,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
743
872
  mergeBlock: currentP.mergeBlock,
744
873
  dynamicContract: currentP.dynamicContract,
745
874
  mutPendingQueries: currentP.mutPendingQueries,
746
- prevQueryRange: currentP.prevQueryRange,
747
- prevPrevQueryRange: currentP.prevPrevQueryRange,
748
- prevRangeSize: currentP.prevRangeSize,
749
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
875
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
876
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
877
+ eventDensity: currentP.eventDensity,
878
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
750
879
  };
751
880
  }
752
881
  }
@@ -893,10 +1022,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
893
1022
  mergeBlock: p.mergeBlock,
894
1023
  dynamicContract: p.dynamicContract,
895
1024
  mutPendingQueries: p.mutPendingQueries,
896
- prevQueryRange: p.prevQueryRange,
897
- prevPrevQueryRange: p.prevPrevQueryRange,
898
- prevRangeSize: p.prevRangeSize,
899
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1025
+ sourceRangeCapacity: p.sourceRangeCapacity,
1026
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1027
+ eventDensity: p.eventDensity,
1028
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
900
1029
  };
901
1030
  let addressesByContractName = {};
902
1031
  addressesByContractName[contractName] = addresses;
@@ -908,10 +1037,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
908
1037
  mergeBlock: undefined,
909
1038
  dynamicContract: contractName,
910
1039
  mutPendingQueries: p.mutPendingQueries,
911
- prevQueryRange: p.prevQueryRange,
912
- prevPrevQueryRange: p.prevPrevQueryRange,
913
- prevRangeSize: p.prevRangeSize,
914
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1040
+ sourceRangeCapacity: p.sourceRangeCapacity,
1041
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1042
+ eventDensity: p.eventDensity,
1043
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
915
1044
  });
916
1045
  }
917
1046
  } else {
@@ -923,10 +1052,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
923
1052
  mergeBlock: p.mergeBlock,
924
1053
  dynamicContract: contractName,
925
1054
  mutPendingQueries: p.mutPendingQueries,
926
- prevQueryRange: p.prevQueryRange,
927
- prevPrevQueryRange: p.prevPrevQueryRange,
928
- prevRangeSize: p.prevRangeSize,
929
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1055
+ sourceRangeCapacity: p.sourceRangeCapacity,
1056
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1057
+ eventDensity: p.eventDensity,
1058
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
930
1059
  };
931
1060
  }
932
1061
  }
@@ -937,11 +1066,11 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
937
1066
  }
938
1067
  IndexingAddresses.register(indexingAddresses, noEventsAddresses);
939
1068
  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);
1069
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined, undefined);
941
1070
  }
942
1071
 
943
- function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBlock, newItems) {
944
- let newItems$1 = newItems.filter(item => {
1072
+ function filterByClientAddress(items, indexingAddresses) {
1073
+ return items.filter(item => {
945
1074
  if (item.kind !== 0) {
946
1075
  return true;
947
1076
  }
@@ -952,7 +1081,10 @@ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBl
952
1081
  return true;
953
1082
  }
954
1083
  });
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);
1084
+ }
1085
+
1086
+ function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
1087
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems.length, latestFetchedBlock), newItems.length !== 0 ? mergeIntoBuffer(fetchState.buffer, newItems) : undefined, true, undefined, undefined);
956
1088
  }
957
1089
 
958
1090
  function startFetchingQueries(param, queries) {
@@ -967,6 +1099,7 @@ function startFetchingQueries(param, queries) {
967
1099
  toBlock: q.toBlock,
968
1100
  isChunk: q.isChunk,
969
1101
  itemsTarget: q.itemsTarget,
1102
+ itemsEst: q.itemsEst,
970
1103
  fetchedBlock: undefined
971
1104
  };
972
1105
  let inserted = false;
@@ -984,92 +1117,223 @@ function startFetchingQueries(param, queries) {
984
1117
  }
985
1118
  }
986
1119
 
987
- function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock, knownHeight, chainTargetBlock, maybeChunkRange, maxChunks, partition, partitionBudget, selection, addressesByContractName) {
988
- let cost = {
989
- contents: 0
990
- };
991
- if (rangeFromBlock <= knownHeight && maxChunks > 0) {
992
- let exit = 0;
993
- if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
994
- exit = 1;
1120
+ function pushDensityPricedQuery(queries, partitionId, fromBlock, toBlock, isChunk, density, chunkItemsMultiplier, chainTargetBlock, itemsTargetFloor, selection, addressesByContractName) {
1121
+ let itemsEst = densityItemsTarget(density, fromBlock, toBlock, chainTargetBlock);
1122
+ let itemsTarget = densityItemsTarget(density * chunkItemsMultiplier, fromBlock, toBlock, chainTargetBlock);
1123
+ queries.push({
1124
+ partitionId: partitionId,
1125
+ fromBlock: fromBlock,
1126
+ toBlock: toBlock,
1127
+ isChunk: isChunk,
1128
+ itemsTarget: toBlock !== undefined ? Primitive_int.max(itemsTarget, itemsTargetFloor) : itemsTarget,
1129
+ itemsEst: itemsEst,
1130
+ selection: selection,
1131
+ addressesByContractName: addressesByContractName
1132
+ });
1133
+ return itemsEst;
1134
+ }
1135
+
1136
+ function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock, headBlockNumber, chainTargetBlock, maybeChunkRange, maxChunks, partition, partitionBudget, chunkItemsMultiplier, itemsTargetFloor, selection, addressesByContractName) {
1137
+ if (!(rangeFromBlock <= Primitive_int.min(headBlockNumber, chainTargetBlock) && maxChunks > 0)) {
1138
+ return;
1139
+ }
1140
+ let exit = 0;
1141
+ if (rangeEndBlock !== undefined) {
1142
+ if (rangeFromBlock > rangeEndBlock) {
1143
+ return;
995
1144
  }
996
- if (exit === 1) {
997
- let trustedDensity = getTrustedDensity(partition);
998
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
999
- let pushSingleQuery = (density, isChunk) => {
1000
- let itemsTarget = densityItemsTarget(density, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1001
- queries.push({
1002
- partitionId: partitionId,
1003
- fromBlock: rangeFromBlock,
1004
- toBlock: rangeEndBlock,
1005
- isChunk: isChunk,
1006
- itemsTarget: itemsTarget,
1007
- selection: selection,
1008
- addressesByContractName: addressesByContractName
1009
- });
1010
- cost.contents = cost.contents + itemsTarget;
1011
- };
1012
- if (trustedDensity !== undefined) {
1013
- if (maybeChunkRange !== undefined && trustedDensity > 0) {
1145
+ exit = 1;
1146
+ } else {
1147
+ exit = 1;
1148
+ }
1149
+ if (exit === 1) {
1150
+ let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
1151
+ let pushSingleQuery = (density, isChunk) => {
1152
+ pushDensityPricedQuery(queries, partitionId, rangeFromBlock, rangeEndBlock, isChunk, density, chunkItemsMultiplier, chainTargetBlock, itemsTargetFloor, selection, addressesByContractName);
1153
+ };
1154
+ let match = partition.eventDensity;
1155
+ let exit$1 = 0;
1156
+ if (match !== undefined) {
1157
+ if (maybeChunkRange !== undefined) {
1158
+ if (match > 0) {
1014
1159
  let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1015
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1016
- let chunkFromBlock = rangeFromBlock;
1017
- let chunkIdx = 0;
1018
- while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
1019
- let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
1020
- let itemsTarget = densityItemsTarget(trustedDensity, chunkFromBlock, chunkToBlock, chainTargetBlock);
1021
- queries.push({
1022
- partitionId: partitionId,
1023
- fromBlock: chunkFromBlock,
1024
- toBlock: chunkToBlock,
1025
- isChunk: true,
1026
- itemsTarget: itemsTarget,
1027
- selection: selection,
1028
- addressesByContractName: addressesByContractName
1029
- });
1030
- cost.contents = cost.contents + itemsTarget;
1031
- chunkFromBlock = chunkToBlock + 1 | 0;
1032
- chunkIdx = chunkIdx + 1 | 0;
1033
- };
1034
- } else {
1035
- pushSingleQuery(trustedDensity, rangeEndBlock !== undefined);
1160
+ if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) > maxBlock) {
1161
+ return pushSingleQuery(match, rangeEndBlock !== undefined);
1036
1162
  }
1037
- } else {
1038
- pushSingleQuery(trustedDensity, false);
1163
+ let chunkFromBlock = rangeFromBlock;
1164
+ let chunkIdx = 0;
1165
+ while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
1166
+ let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
1167
+ pushDensityPricedQuery(queries, partitionId, chunkFromBlock, chunkToBlock, true, match, chunkItemsMultiplier, chainTargetBlock, itemsTargetFloor, selection, addressesByContractName);
1168
+ chunkFromBlock = chunkToBlock + 1 | 0;
1169
+ chunkIdx = chunkIdx + 1 | 0;
1170
+ };
1171
+ return;
1039
1172
  }
1173
+ exit$1 = 2;
1040
1174
  } else {
1041
- let remainingRange = Primitive_int.max(1, (chainTargetBlock - rangeFromBlock | 0) + 1 | 0);
1042
- pushSingleQuery(partitionBudget / remainingRange, false);
1175
+ if (match > 0) {
1176
+ return pushSingleQuery(match, false);
1177
+ }
1178
+ exit$1 = 2;
1043
1179
  }
1180
+ } else {
1181
+ exit$1 = 2;
1182
+ }
1183
+ if (exit$1 === 2) {
1184
+ let remainingRange = Primitive_int.max(1, (chainTargetBlock - rangeFromBlock | 0) + 1 | 0);
1185
+ return pushSingleQuery(partitionBudget / remainingRange, false);
1044
1186
  }
1045
1187
  }
1046
- return cost.contents;
1047
1188
  }
1048
1189
 
1049
- function waterLevel(budget, footprints) {
1050
- let sorted = footprints.toSorted(Primitive_float.compare);
1051
- let n = sorted.length;
1052
- let prefix = 0;
1053
- let level;
1054
- let idx = 0;
1055
- while (level === undefined && idx < n) {
1056
- let i = idx;
1057
- prefix = prefix + sorted[i];
1058
- let candidate = (budget + prefix) / (i + 1 | 0);
1059
- if (i === (n - 1 | 0) || candidate <= sorted[i + 1 | 0]) {
1060
- level = candidate;
1190
+ function walkPartitionPending(p, partitionId, inFlightCount, candidates, headBlockNumber, chainTargetBlock, chunkItemsMultiplier, itemsTargetFloor, partitionBudget, queryEndBlock) {
1191
+ let maybeChunkRange = getMinHistoryRange(p);
1192
+ let pendingCount = p.mutPendingQueries.length;
1193
+ let cursor = p.latestFetchedBlock.blockNumber + 1 | 0;
1194
+ let canContinue = true;
1195
+ let chunksUsedThisCall = 0;
1196
+ let pqIdx = 0;
1197
+ while (pqIdx < pendingCount && canContinue) {
1198
+ let pq = p.mutPendingQueries[pqIdx];
1199
+ if (pq.fromBlock > cursor) {
1200
+ let beforeLen = candidates.length;
1201
+ pushGapFillQueries(candidates, partitionId, cursor, Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock), headBlockNumber, chainTargetBlock, maybeChunkRange, (12 - inFlightCount | 0) - chunksUsedThisCall | 0, p, partitionBudget, chunkItemsMultiplier, itemsTargetFloor, p.selection, p.addressesByContractName);
1202
+ chunksUsedThisCall = chunksUsedThisCall + (candidates.length - beforeLen | 0) | 0;
1061
1203
  }
1062
- idx = idx + 1 | 0;
1204
+ let toBlock = pq.toBlock;
1205
+ if (toBlock !== undefined && pq.isChunk) {
1206
+ let match = pq.fetchedBlock;
1207
+ if (match !== undefined) {
1208
+ let blockNumber = match.blockNumber;
1209
+ cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
1210
+ } else {
1211
+ cursor = toBlock + 1 | 0;
1212
+ }
1213
+ } else {
1214
+ canContinue = false;
1215
+ }
1216
+ pqIdx = pqIdx + 1 | 0;
1063
1217
  };
1064
- return Stdlib_Option.getOr(level, 0);
1218
+ if (canContinue) {
1219
+ return {
1220
+ partitionId: partitionId,
1221
+ p: p,
1222
+ cursor: cursor,
1223
+ chunksUsedThisCall: chunksUsedThisCall,
1224
+ inFlightCount: inFlightCount,
1225
+ queryEndBlock: queryEndBlock,
1226
+ maybeChunkRange: maybeChunkRange
1227
+ };
1228
+ }
1065
1229
  }
1066
1230
 
1067
- function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1068
- let knownHeight = param.knownHeight;
1231
+ function pushForwardCandidates(candidates, inRangeStates, inRangeCount, chainTargetBlock, freshBudget, chunkItemsMultiplier, itemsTargetFloor) {
1232
+ let probeShare = inRangeCount === 0 ? 0 : freshBudget / inRangeCount;
1233
+ let frontierCursor = Stdlib_Array.reduce(inRangeStates, chainTargetBlock, (min, fs) => {
1234
+ if (fs.cursor < min) {
1235
+ return fs.cursor;
1236
+ } else {
1237
+ return min;
1238
+ }
1239
+ });
1240
+ let rangeToTarget = (chainTargetBlock - frontierCursor | 0) + 1 | 0;
1241
+ let rangeTargetDensity = inRangeCount > 0 && rangeToTarget > 0 ? freshBudget / rangeToTarget : 0;
1242
+ inRangeStates.forEach(fs => {
1243
+ let p = fs.p;
1244
+ let eb = fs.queryEndBlock;
1245
+ let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1246
+ let match = fs.maybeChunkRange;
1247
+ let match$1 = p.eventDensity;
1248
+ if (match !== undefined && match$1 !== undefined && match$1 > 0) {
1249
+ let chunkSize = Js_math.ceil_int(match * 1.8);
1250
+ let maxChunksRemaining = (12 - fs.inFlightCount | 0) - fs.chunksUsedThisCall | 0;
1251
+ let chunkStartCeiling = Primitive_int.min(maxBlock, chainTargetBlock);
1252
+ let created = 0;
1253
+ let chunkFromBlock = fs.cursor;
1254
+ let generatedItems = 0;
1255
+ while (created < maxChunksRemaining && chunkFromBlock <= chunkStartCeiling && generatedItems <= freshBudget) {
1256
+ let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1257
+ let itemsEst = pushDensityPricedQuery(candidates, fs.partitionId, chunkFromBlock, chunkToBlock, true, match$1, chunkItemsMultiplier, chainTargetBlock, itemsTargetFloor, p.selection, p.addressesByContractName);
1258
+ generatedItems = generatedItems + itemsEst;
1259
+ chunkFromBlock = chunkToBlock + 1 | 0;
1260
+ created = created + 1 | 0;
1261
+ };
1262
+ return;
1263
+ }
1264
+ let itemsTarget = 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);
1265
+ candidates.push({
1266
+ partitionId: fs.partitionId,
1267
+ fromBlock: fs.cursor,
1268
+ toBlock: fs.queryEndBlock,
1269
+ isChunk: false,
1270
+ itemsTarget: itemsTarget,
1271
+ itemsEst: itemsTarget,
1272
+ selection: p.selection,
1273
+ addressesByContractName: p.addressesByContractName
1274
+ });
1275
+ });
1276
+ }
1277
+
1278
+ function acceptCandidates(candidates, reservations, chainTargetItems, partitionIndexById, queriesByPartitionIndex) {
1279
+ let acceptanceStream = [];
1280
+ candidates.forEach(query => {
1281
+ acceptanceStream.push([
1282
+ query.fromBlock,
1283
+ query.itemsEst,
1284
+ query
1285
+ ]);
1286
+ });
1287
+ reservations.forEach(param => {
1288
+ acceptanceStream.push([
1289
+ param[0],
1290
+ param[1],
1291
+ undefined
1292
+ ]);
1293
+ });
1294
+ acceptanceStream.sort((param, param$1) => {
1295
+ let bFrom = param$1[0];
1296
+ let aFrom = param[0];
1297
+ if (aFrom !== bFrom) {
1298
+ return Primitive_int.compare(aFrom, bFrom);
1299
+ }
1300
+ let bQuery = param$1[2];
1301
+ if (param[2] !== undefined) {
1302
+ if (bQuery !== undefined) {
1303
+ return 0;
1304
+ } else {
1305
+ return 1;
1306
+ }
1307
+ } else if (bQuery !== undefined) {
1308
+ return -1;
1309
+ } else {
1310
+ return 0;
1311
+ }
1312
+ });
1313
+ let streamCount = acceptanceStream.length;
1314
+ let remainingBudget = chainTargetItems;
1315
+ let acceptIdx = 0;
1316
+ let usedConcurrency = reservations.length;
1317
+ while (remainingBudget > 0 && acceptIdx < streamCount && usedConcurrency < 100) {
1318
+ let match = acceptanceStream[acceptIdx];
1319
+ let maybeQuery = match[2];
1320
+ if (maybeQuery !== undefined) {
1321
+ let partitionIdx = partitionIndexById[maybeQuery.partitionId];
1322
+ queriesByPartitionIndex[partitionIdx].push(maybeQuery);
1323
+ usedConcurrency = usedConcurrency + 1 | 0;
1324
+ }
1325
+ remainingBudget = remainingBudget - match[1];
1326
+ acceptIdx = acceptIdx + 1 | 0;
1327
+ };
1328
+ }
1329
+
1330
+ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star, $staropt$star$1) {
1069
1331
  let blockLag = param.blockLag;
1070
1332
  let endBlock = param.endBlock;
1071
1333
  let optimizedPartitions = param.optimizedPartitions;
1072
- let headBlockNumber = knownHeight - blockLag | 0;
1334
+ let chunkItemsMultiplier = $staropt$star !== undefined ? $staropt$star : 1;
1335
+ let itemsTargetFloor = $staropt$star$1 !== undefined ? $staropt$star$1 : 0;
1336
+ let headBlockNumber = param.knownHeight - blockLag | 0;
1073
1337
  if (headBlockNumber <= 0) {
1074
1338
  return "WaitingForNewBlock";
1075
1339
  }
@@ -1078,14 +1342,34 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1078
1342
  endBlock !== undefined ? headBlockNumber < endBlock : true
1079
1343
  ) && !isOnBlockBehindTheHead;
1080
1344
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
1345
+ let inFlightCounts = Array(partitionsCount);
1346
+ let reservations = [];
1347
+ let chainReserved = 0;
1348
+ let partitionIndexById = {};
1081
1349
  for (let idx = 0; idx < partitionsCount; ++idx) {
1082
1350
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1083
1351
  let p = optimizedPartitions.entities[partitionId];
1352
+ partitionIndexById[partitionId] = idx;
1353
+ let inFlightCount = 0;
1354
+ for (let pqIdx = 0, pqIdx_finish = p.mutPendingQueries.length; pqIdx < pqIdx_finish; ++pqIdx) {
1355
+ let pq = p.mutPendingQueries[pqIdx];
1356
+ if (pq.fetchedBlock === undefined) {
1357
+ inFlightCount = inFlightCount + 1 | 0;
1358
+ chainReserved = chainReserved + pq.itemsEst;
1359
+ reservations.push([
1360
+ pq.fromBlock,
1361
+ pq.itemsEst
1362
+ ]);
1363
+ }
1364
+ }
1365
+ inFlightCounts[idx] = inFlightCount;
1084
1366
  if (p.mutPendingQueries.length !== 0 || p.latestFetchedBlock.blockNumber < headBlockNumber) {
1085
1367
  shouldWaitForNewBlock = false;
1086
1368
  }
1087
1369
  }
1088
- let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount, param => []);
1370
+ let availableConcurrency = 100 - reservations.length | 0;
1371
+ let partitionsCount$1 = chainTargetItems <= 0 || shouldWaitForNewBlock || availableConcurrency <= 0 ? 0 : partitionsCount;
1372
+ let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount$1, param => []);
1089
1373
  let computeQueryEndBlock = p => {
1090
1374
  let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1091
1375
  if (blockLag !== 0) {
@@ -1094,75 +1378,18 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1094
1378
  return queryEndBlock;
1095
1379
  }
1096
1380
  };
1097
- let existingReservedByPartition = {};
1098
- let chainReserved = 0;
1099
- for (let idx$1 = 0; idx$1 < partitionsCount; ++idx$1) {
1381
+ let candidates = [];
1382
+ let partitionBudget = partitionsCount$1 === 0 ? 0 : chainTargetItems / partitionsCount$1;
1383
+ let fillStates = [];
1384
+ for (let idx$1 = 0; idx$1 < partitionsCount$1; ++idx$1) {
1100
1385
  let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1101
1386
  let p$1 = optimizedPartitions.entities[partitionId$1];
1102
- let cost = 0;
1103
- for (let pqIdx = 0, pqIdx_finish = p$1.mutPendingQueries.length; pqIdx < pqIdx_finish; ++pqIdx) {
1104
- cost = cost + p$1.mutPendingQueries[pqIdx].itemsTarget;
1387
+ let fillState = walkPartitionPending(p$1, partitionId$1, inFlightCounts[idx$1], candidates, headBlockNumber, chainTargetBlock, chunkItemsMultiplier, itemsTargetFloor, partitionBudget, computeQueryEndBlock(p$1));
1388
+ if (fillState !== undefined) {
1389
+ fillStates.push(fillState);
1105
1390
  }
1106
- existingReservedByPartition[partitionId$1] = cost;
1107
- chainReserved = chainReserved + cost;
1108
1391
  }
1109
- let fillStates = [];
1110
- let gapFillCost = 0;
1111
- for (let idx$2 = 0; idx$2 < partitionsCount; ++idx$2) {
1112
- let partitionId$2 = optimizedPartitions.idsInAscOrder[idx$2];
1113
- let p$2 = optimizedPartitions.entities[partitionId$2];
1114
- let bucket = queriesByPartitionIndex[idx$2];
1115
- let pendingCount = p$2.mutPendingQueries.length;
1116
- let queryEndBlock = computeQueryEndBlock(p$2);
1117
- let maybeChunkRange = getMinHistoryRange(p$2);
1118
- let cursor = p$2.latestFetchedBlock.blockNumber + 1 | 0;
1119
- let canContinue = true;
1120
- let chunksUsedThisCall = 0;
1121
- let pqIdx$1 = 0;
1122
- while (pqIdx$1 < pendingCount && canContinue) {
1123
- let pq = p$2.mutPendingQueries[pqIdx$1];
1124
- if (pq.fromBlock > cursor) {
1125
- let beforeLen = bucket.length;
1126
- let cost$1 = pushGapFillQueries(bucket, partitionId$2, cursor, Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock), knownHeight, chainTargetBlock, maybeChunkRange, (10 - pendingCount | 0) - chunksUsedThisCall | 0, p$2, chainTargetItems / partitionsCount, p$2.selection, p$2.addressesByContractName);
1127
- chunksUsedThisCall = chunksUsedThisCall + (bucket.length - beforeLen | 0) | 0;
1128
- gapFillCost = gapFillCost + cost$1;
1129
- }
1130
- let toBlock = pq.toBlock;
1131
- if (toBlock !== undefined && pq.isChunk) {
1132
- let match = pq.fetchedBlock;
1133
- if (match !== undefined) {
1134
- let blockNumber = match.blockNumber;
1135
- cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
1136
- } else {
1137
- cursor = toBlock + 1 | 0;
1138
- }
1139
- } else {
1140
- canContinue = false;
1141
- }
1142
- pqIdx$1 = pqIdx$1 + 1 | 0;
1143
- };
1144
- if (canContinue) {
1145
- fillStates.push({
1146
- partitionId: partitionId$2,
1147
- p: p$2,
1148
- cursor: cursor,
1149
- chunksUsedThisCall: chunksUsedThisCall,
1150
- pendingCount: pendingCount,
1151
- queryEndBlock: queryEndBlock,
1152
- maybeChunkRange: maybeChunkRange,
1153
- bucket: bucket
1154
- });
1155
- }
1156
- }
1157
- let rangeItemsTarget = Primitive_float.max(0, chainTargetItems - chainReserved - gapFillCost);
1158
- let reservedByPartition = {};
1159
- fillStates.forEach(fs => {
1160
- let cost = existingReservedByPartition[fs.partitionId];
1161
- for (let i = 0, i_finish = fs.bucket.length; i < i_finish; ++i) {
1162
- cost = cost + fs.bucket[i].itemsTarget;
1163
- }
1164
- reservedByPartition[fs.partitionId] = cost;
1165
- });
1392
+ let freshBudget = Primitive_float.max(0, chainTargetItems - chainReserved);
1166
1393
  let isInRange = fs => {
1167
1394
  let tmp = false;
1168
1395
  if (fs.cursor <= chainTargetBlock) {
@@ -1170,84 +1397,16 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1170
1397
  tmp = eb !== undefined ? fs.cursor <= eb : true;
1171
1398
  }
1172
1399
  if (tmp) {
1173
- return (fs.pendingCount + fs.chunksUsedThisCall | 0) < 10;
1400
+ return (fs.inFlightCount + fs.chunksUsedThisCall | 0) < 12;
1174
1401
  } else {
1175
1402
  return false;
1176
1403
  }
1177
1404
  };
1178
1405
  let inRangeStates = fillStates.filter(isInRange);
1179
- let emitQueries = (fs, budget) => {
1180
- let p = fs.p;
1181
- let eb = fs.queryEndBlock;
1182
- let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1183
- let match = fs.maybeChunkRange;
1184
- let match$1 = getTrustedDensity(p);
1185
- if (match !== undefined && match$1 !== undefined && match$1 > 0) {
1186
- let chunkSize = Js_math.ceil_int(match * 1.8);
1187
- let chunkCost = match$1 * chunkSize;
1188
- let maxChunksRemaining = (10 - fs.pendingCount | 0) - fs.chunksUsedThisCall | 0;
1189
- let affordable = Math.floor(budget / chunkCost) | 0;
1190
- let numChunks = Primitive_int.max(1, Primitive_int.min(affordable, maxChunksRemaining));
1191
- let consumed = 0;
1192
- let created = 0;
1193
- let chunkFromBlock = fs.cursor;
1194
- while (created < numChunks && chunkFromBlock <= maxBlock) {
1195
- let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1196
- let itemsTarget = Primitive_int.max(1, Math.ceil(match$1 * ((chunkToBlock - chunkFromBlock | 0) + 1 | 0)) | 0);
1197
- fs.bucket.push({
1198
- partitionId: fs.partitionId,
1199
- fromBlock: chunkFromBlock,
1200
- toBlock: chunkToBlock,
1201
- isChunk: true,
1202
- itemsTarget: itemsTarget,
1203
- selection: p.selection,
1204
- addressesByContractName: p.addressesByContractName
1205
- });
1206
- consumed = consumed + itemsTarget;
1207
- chunkFromBlock = chunkToBlock + 1 | 0;
1208
- created = created + 1 | 0;
1209
- };
1210
- fs.cursor = chunkFromBlock;
1211
- fs.chunksUsedThisCall = fs.chunksUsedThisCall + created | 0;
1212
- return consumed;
1213
- }
1214
- let itemsTarget$1 = Primitive_int.max(1, Math.round(budget) | 0);
1215
- fs.bucket.push({
1216
- partitionId: fs.partitionId,
1217
- fromBlock: fs.cursor,
1218
- toBlock: fs.queryEndBlock,
1219
- isChunk: false,
1220
- itemsTarget: itemsTarget$1,
1221
- selection: p.selection,
1222
- addressesByContractName: p.addressesByContractName
1223
- });
1224
- fs.cursor = maxBlock + 1 | 0;
1225
- fs.chunksUsedThisCall = fs.chunksUsedThisCall + 1 | 0;
1226
- return itemsTarget$1;
1227
- };
1228
- let notFilledPartitions = inRangeStates;
1229
- let remainingBudget = {
1230
- contents: rangeItemsTarget
1231
- };
1232
- while (notFilledPartitions.length !== 0 && remainingBudget.contents > 0) {
1233
- let level = waterLevel(remainingBudget.contents, notFilledPartitions.map(fs => reservedByPartition[fs.partitionId]));
1234
- let next = [];
1235
- notFilledPartitions.forEach(fs => {
1236
- let reserved = reservedByPartition[fs.partitionId];
1237
- let budget = level - reserved;
1238
- if (budget <= 0) {
1239
- return;
1240
- }
1241
- let consumed = emitQueries(fs, budget);
1242
- reservedByPartition[fs.partitionId] = reserved + consumed;
1243
- remainingBudget.contents = remainingBudget.contents - consumed;
1244
- if (isInRange(fs)) {
1245
- next.push(fs);
1246
- return;
1247
- }
1248
- });
1249
- notFilledPartitions = next;
1250
- };
1406
+ let inRangeCount = inRangeStates.length;
1407
+ let inRangeStates$1 = inRangeCount > availableConcurrency ? (inRangeStates.sort((a, b) => Primitive_int.compare(a.cursor, b.cursor)), inRangeStates.slice(0, availableConcurrency)) : inRangeStates;
1408
+ pushForwardCandidates(candidates, inRangeStates$1, inRangeCount, chainTargetBlock, freshBudget, chunkItemsMultiplier, itemsTargetFloor);
1409
+ acceptCandidates(candidates, reservations, chainTargetItems, partitionIndexById, queriesByPartitionIndex);
1251
1410
  let queries = queriesByPartitionIndex.flat();
1252
1411
  if (Utils.$$Array.isEmpty(queries)) {
1253
1412
  if (shouldWaitForNewBlock) {
@@ -1328,10 +1487,10 @@ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, add
1328
1487
  mergeBlock: undefined,
1329
1488
  dynamicContract: undefined,
1330
1489
  mutPendingQueries: [],
1331
- prevQueryRange: 0,
1332
- prevPrevQueryRange: 0,
1333
- prevRangeSize: 0,
1334
- latestBlockRangeUpdateBlock: 0
1490
+ sourceRangeCapacity: 0,
1491
+ prevSourceRangeCapacity: 0,
1492
+ eventDensity: undefined,
1493
+ latestSourceRangeCapacityUpdateBlock: 0
1335
1494
  });
1336
1495
  }
1337
1496
  let normalSelection = {
@@ -1407,6 +1566,7 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1407
1566
  toBlock: pq.toBlock,
1408
1567
  isChunk: pq.isChunk,
1409
1568
  itemsTarget: pq.itemsTarget,
1569
+ itemsEst: pq.itemsEst,
1410
1570
  fetchedBlock: {
1411
1571
  blockNumber: targetBlockNumber,
1412
1572
  blockTimestamp: 0
@@ -1465,10 +1625,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1465
1625
  mergeBlock: mergeBlock$1,
1466
1626
  dynamicContract: p.dynamicContract,
1467
1627
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1468
- prevQueryRange: p.prevQueryRange,
1469
- prevPrevQueryRange: p.prevPrevQueryRange,
1470
- prevRangeSize: p.prevRangeSize,
1471
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1628
+ sourceRangeCapacity: p.sourceRangeCapacity,
1629
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1630
+ eventDensity: p.eventDensity,
1631
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1472
1632
  });
1473
1633
  }
1474
1634
  }
@@ -1486,10 +1646,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1486
1646
  mergeBlock: p.mergeBlock,
1487
1647
  dynamicContract: p.dynamicContract,
1488
1648
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1489
- prevQueryRange: p.prevQueryRange,
1490
- prevPrevQueryRange: p.prevPrevQueryRange,
1491
- prevRangeSize: p.prevRangeSize,
1492
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1649
+ sourceRangeCapacity: p.sourceRangeCapacity,
1650
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1651
+ eventDensity: p.eventDensity,
1652
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1493
1653
  });
1494
1654
  }
1495
1655
  }
@@ -1512,7 +1672,7 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1512
1672
  let tmp;
1513
1673
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1514
1674
  return tmp <= targetBlockNumber;
1515
- }), undefined, undefined);
1675
+ }), true, undefined, undefined);
1516
1676
  }
1517
1677
 
1518
1678
  function resetPendingQueries(fetchState) {
@@ -1530,10 +1690,10 @@ function resetPendingQueries(fetchState) {
1530
1690
  mergeBlock: partition.mergeBlock,
1531
1691
  dynamicContract: partition.dynamicContract,
1532
1692
  mutPendingQueries: kept,
1533
- prevQueryRange: partition.prevQueryRange,
1534
- prevPrevQueryRange: partition.prevPrevQueryRange,
1535
- prevRangeSize: partition.prevRangeSize,
1536
- latestBlockRangeUpdateBlock: partition.latestBlockRangeUpdateBlock
1693
+ sourceRangeCapacity: partition.sourceRangeCapacity,
1694
+ prevSourceRangeCapacity: partition.prevSourceRangeCapacity,
1695
+ eventDensity: partition.eventDensity,
1696
+ latestSourceRangeCapacityUpdateBlock: partition.latestSourceRangeCapacityUpdateBlock
1537
1697
  };
1538
1698
  }
1539
1699
  }
@@ -1609,7 +1769,7 @@ function getProgressPercentage(fetchState) {
1609
1769
  if (firstEventBlock === undefined) {
1610
1770
  return 0;
1611
1771
  }
1612
- let totalRange = fetchState.knownHeight - firstEventBlock | 0;
1772
+ let totalRange = (fetchState.knownHeight - fetchState.blockLag | 0) - firstEventBlock | 0;
1613
1773
  if (totalRange <= 0) {
1614
1774
  return 0;
1615
1775
  }
@@ -1665,7 +1825,7 @@ function getProgressBlockNumberAt(fetchState, index) {
1665
1825
  function updateKnownHeight(fetchState, knownHeight) {
1666
1826
  if (knownHeight > fetchState.knownHeight) {
1667
1827
  Prometheus.IndexingKnownHeight.set(knownHeight, fetchState.chainId);
1668
- return updateInternal(fetchState, undefined, undefined, undefined, knownHeight);
1828
+ return updateInternal(fetchState, undefined, undefined, undefined, undefined, knownHeight);
1669
1829
  } else {
1670
1830
  return fetchState;
1671
1831
  }
@@ -1673,7 +1833,11 @@ function updateKnownHeight(fetchState, knownHeight) {
1673
1833
 
1674
1834
  let blockItemLogIndex = 16777216;
1675
1835
 
1676
- let maxPendingChunksPerPartition = 10;
1836
+ let maxInFlightChunksPerPartition = 12;
1837
+
1838
+ let maxChainConcurrency = 100;
1839
+
1840
+ let chunkRangeGrowthFactor = 1.8;
1677
1841
 
1678
1842
  export {
1679
1843
  deriveContractNameByAddress,
@@ -1685,7 +1849,9 @@ export {
1685
1849
  bufferBlockNumber,
1686
1850
  bufferBlock,
1687
1851
  bufferReadyCount,
1852
+ getRegistrationIndex,
1688
1853
  compareBufferItem,
1854
+ mergeIntoBuffer,
1689
1855
  blockItemLogIndex,
1690
1856
  appendOnBlockItems,
1691
1857
  updateInternal,
@@ -1694,11 +1860,17 @@ export {
1694
1860
  addressesByContractNameGetAll,
1695
1861
  createPartitionsFromIndexingAddresses,
1696
1862
  registerDynamicContracts,
1863
+ filterByClientAddress,
1697
1864
  handleQueryResult,
1698
1865
  startFetchingQueries,
1699
- maxPendingChunksPerPartition,
1866
+ maxInFlightChunksPerPartition,
1867
+ maxChainConcurrency,
1868
+ chunkRangeGrowthFactor,
1869
+ pushDensityPricedQuery,
1700
1870
  pushGapFillQueries,
1701
- waterLevel,
1871
+ walkPartitionPending,
1872
+ pushForwardCandidates,
1873
+ acceptCandidates,
1702
1874
  getNextQuery,
1703
1875
  hasReadyItem,
1704
1876
  getReadyItemsCount,