envio 3.3.0-alpha.8 → 3.3.0-rc.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 (107) 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 +205 -59
  13. package/src/ChainState.res.mjs +130 -27
  14. package/src/ChainState.resi +16 -1
  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 +147 -39
  20. package/src/CrossChainState.res.mjs +42 -10
  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/EnvioGlobal.res +53 -0
  27. package/src/EnvioGlobal.res.mjs +31 -0
  28. package/src/EventConfigBuilder.res +30 -56
  29. package/src/EventConfigBuilder.res.mjs +24 -30
  30. package/src/EventProcessing.res +8 -5
  31. package/src/EventProcessing.res.mjs +2 -1
  32. package/src/FetchState.res +842 -474
  33. package/src/FetchState.res.mjs +545 -356
  34. package/src/HandlerLoader.res +1 -1
  35. package/src/HandlerLoader.res.mjs +1 -1
  36. package/src/HandlerRegister.res +478 -305
  37. package/src/HandlerRegister.res.mjs +277 -209
  38. package/src/HandlerRegister.resi +11 -6
  39. package/src/InMemoryStore.res +14 -26
  40. package/src/InMemoryStore.res.mjs +6 -19
  41. package/src/IndexerState.res +15 -39
  42. package/src/IndexerState.res.mjs +18 -29
  43. package/src/IndexerState.resi +2 -10
  44. package/src/Internal.res +121 -18
  45. package/src/Internal.res.mjs +96 -2
  46. package/src/LoadLayer.res +44 -24
  47. package/src/LoadLayer.res.mjs +43 -20
  48. package/src/LoadLayer.resi +1 -0
  49. package/src/LogSelection.res +92 -217
  50. package/src/LogSelection.res.mjs +95 -184
  51. package/src/Main.res +29 -143
  52. package/src/Main.res.mjs +25 -88
  53. package/src/Metrics.res +1 -1
  54. package/src/Metrics.res.mjs +1 -1
  55. package/src/Persistence.res +12 -3
  56. package/src/PgStorage.res +155 -82
  57. package/src/PgStorage.res.mjs +130 -77
  58. package/src/Prometheus.res +20 -10
  59. package/src/Prometheus.res.mjs +22 -10
  60. package/src/PruneStaleHistory.res +146 -35
  61. package/src/PruneStaleHistory.res.mjs +114 -20
  62. package/src/RawEvent.res +2 -2
  63. package/src/Rollback.res +32 -13
  64. package/src/Rollback.res.mjs +24 -11
  65. package/src/RollbackCommit.res +4 -1
  66. package/src/RollbackCommit.res.mjs +3 -2
  67. package/src/SimulateDeadInputTracker.res +1 -1
  68. package/src/SimulateItems.res +39 -7
  69. package/src/SimulateItems.res.mjs +28 -9
  70. package/src/TestIndexer.res +2 -2
  71. package/src/TestIndexer.res.mjs +2 -2
  72. package/src/TopicFilter.res +1 -25
  73. package/src/TopicFilter.res.mjs +0 -74
  74. package/src/UserContext.res +62 -23
  75. package/src/UserContext.res.mjs +26 -6
  76. package/src/Writing.res +60 -21
  77. package/src/Writing.res.mjs +27 -9
  78. package/src/bindings/NodeJs.res +5 -0
  79. package/src/bindings/Viem.res +0 -5
  80. package/src/sources/EventRouter.res +0 -21
  81. package/src/sources/EventRouter.res.mjs +0 -12
  82. package/src/sources/Evm.res +4 -1
  83. package/src/sources/Evm.res.mjs +1 -1
  84. package/src/sources/EvmChain.res +2 -27
  85. package/src/sources/EvmChain.res.mjs +2 -23
  86. package/src/sources/EvmRpcClient.res +58 -23
  87. package/src/sources/EvmRpcClient.res.mjs +11 -5
  88. package/src/sources/Fuel.res +3 -1
  89. package/src/sources/Fuel.res.mjs +1 -1
  90. package/src/sources/HyperSync.res +9 -38
  91. package/src/sources/HyperSync.res.mjs +16 -28
  92. package/src/sources/HyperSync.resi +2 -2
  93. package/src/sources/HyperSyncClient.res +88 -11
  94. package/src/sources/HyperSyncClient.res.mjs +39 -6
  95. package/src/sources/HyperSyncSource.res +18 -210
  96. package/src/sources/HyperSyncSource.res.mjs +9 -137
  97. package/src/sources/Rpc.res +0 -32
  98. package/src/sources/Rpc.res.mjs +1 -46
  99. package/src/sources/RpcSource.res +129 -533
  100. package/src/sources/RpcSource.res.mjs +161 -379
  101. package/src/sources/SimulateSource.res +37 -19
  102. package/src/sources/SimulateSource.res.mjs +27 -10
  103. package/src/sources/SourceManager.res +4 -14
  104. package/src/sources/SourceManager.res.mjs +3 -9
  105. package/src/sources/SourceManager.resi +0 -2
  106. package/src/sources/SvmHyperSyncSource.res +13 -3
  107. package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
@@ -23,25 +23,28 @@ let deriveContractNameByAddress = Utils.$$WeakMap.memoize(addressesByContractNam
23
23
  return result;
24
24
  });
25
25
 
26
- function densityItemsTarget(p, fromBlock, toBlock, chainTargetBlock) {
27
- let density = p.prevRangeSize / p.prevQueryRange;
28
- return ((Stdlib_Option.getOr(toBlock, chainTargetBlock) - fromBlock | 0) + 1 | 0) * density;
26
+ function densityItemsTarget(density, fromBlock, toBlock, chainTargetBlock) {
27
+ return Primitive_int.max(1, Math.ceil(((Stdlib_Option.getOr(toBlock, chainTargetBlock) - fromBlock | 0) + 1 | 0) * density) | 0);
29
28
  }
30
29
 
31
30
  function getMinHistoryRange(p) {
32
- let match = p.prevQueryRange;
33
- let match$1 = p.prevPrevQueryRange;
31
+ let match = p.sourceRangeCapacity;
32
+ let match$1 = p.prevSourceRangeCapacity;
34
33
  if (match !== 0 && match$1 !== 0) {
35
34
  return match < match$1 ? match : match$1;
36
35
  }
37
36
  }
38
37
 
38
+ function getTrustedDensity(p) {
39
+ return p.eventDensity;
40
+ }
41
+
39
42
  function getMinQueryRange(partitions) {
40
43
  let min = 0;
41
44
  for (let i = 0, i_finish = partitions.length; i < i_finish; ++i) {
42
45
  let p = partitions[i];
43
- let a = p.prevQueryRange;
44
- let b = p.prevPrevQueryRange;
46
+ let a = p.sourceRangeCapacity;
47
+ let b = p.prevSourceRangeCapacity;
45
48
  if (a > 0 && (min === 0 || a < min)) {
46
49
  min = a;
47
50
  }
@@ -81,10 +84,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
81
84
  mergeBlock: potentialMergeBlock,
82
85
  dynamicContract: p1.dynamicContract,
83
86
  mutPendingQueries: p1.mutPendingQueries,
84
- prevQueryRange: p1.prevQueryRange,
85
- prevPrevQueryRange: p1.prevPrevQueryRange,
86
- prevRangeSize: p1.prevRangeSize,
87
- latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
87
+ sourceRangeCapacity: p1.sourceRangeCapacity,
88
+ prevSourceRangeCapacity: p1.prevSourceRangeCapacity,
89
+ eventDensity: p1.eventDensity,
90
+ latestSourceRangeCapacityUpdateBlock: p1.latestSourceRangeCapacityUpdateBlock
88
91
  });
89
92
  completed.push({
90
93
  id: p2.id,
@@ -94,10 +97,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
94
97
  mergeBlock: potentialMergeBlock,
95
98
  dynamicContract: p2.dynamicContract,
96
99
  mutPendingQueries: p2.mutPendingQueries,
97
- prevQueryRange: p2.prevQueryRange,
98
- prevPrevQueryRange: p2.prevPrevQueryRange,
99
- prevRangeSize: p2.prevRangeSize,
100
- latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
100
+ sourceRangeCapacity: p2.sourceRangeCapacity,
101
+ prevSourceRangeCapacity: p2.prevSourceRangeCapacity,
102
+ eventDensity: p2.eventDensity,
103
+ latestSourceRangeCapacityUpdateBlock: p2.latestSourceRangeCapacityUpdateBlock
101
104
  });
102
105
  let newId = nextPartitionIndexRef.contents.toString();
103
106
  nextPartitionIndexRef.contents = nextPartitionIndexRef.contents + 1 | 0;
@@ -105,6 +108,13 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
105
108
  p1,
106
109
  p2
107
110
  ]);
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
+ );
108
118
  continuingBase = {
109
119
  id: newId,
110
120
  latestFetchedBlock: {
@@ -116,10 +126,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
116
126
  mergeBlock: undefined,
117
127
  dynamicContract: contractName,
118
128
  mutPendingQueries: [],
119
- prevQueryRange: minRange,
120
- prevPrevQueryRange: minRange,
121
- prevRangeSize: 0,
122
- latestBlockRangeUpdateBlock: 0
129
+ sourceRangeCapacity: minRange,
130
+ prevSourceRangeCapacity: minRange,
131
+ eventDensity: inheritedDensity,
132
+ latestSourceRangeCapacityUpdateBlock: 0
123
133
  };
124
134
  } else {
125
135
  completed.push({
@@ -130,10 +140,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
130
140
  mergeBlock: potentialMergeBlock,
131
141
  dynamicContract: p1.dynamicContract,
132
142
  mutPendingQueries: p1.mutPendingQueries,
133
- prevQueryRange: p1.prevQueryRange,
134
- prevPrevQueryRange: p1.prevPrevQueryRange,
135
- prevRangeSize: p1.prevRangeSize,
136
- latestBlockRangeUpdateBlock: p1.latestBlockRangeUpdateBlock
143
+ sourceRangeCapacity: p1.sourceRangeCapacity,
144
+ prevSourceRangeCapacity: p1.prevSourceRangeCapacity,
145
+ eventDensity: p1.eventDensity,
146
+ latestSourceRangeCapacityUpdateBlock: p1.latestSourceRangeCapacityUpdateBlock
137
147
  });
138
148
  continuingBase = p2;
139
149
  }
@@ -146,10 +156,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
146
156
  mergeBlock: potentialMergeBlock,
147
157
  dynamicContract: p2.dynamicContract,
148
158
  mutPendingQueries: p2.mutPendingQueries,
149
- prevQueryRange: p2.prevQueryRange,
150
- prevPrevQueryRange: p2.prevPrevQueryRange,
151
- prevRangeSize: p2.prevRangeSize,
152
- latestBlockRangeUpdateBlock: p2.latestBlockRangeUpdateBlock
159
+ sourceRangeCapacity: p2.sourceRangeCapacity,
160
+ prevSourceRangeCapacity: p2.prevSourceRangeCapacity,
161
+ eventDensity: p2.eventDensity,
162
+ latestSourceRangeCapacityUpdateBlock: p2.latestSourceRangeCapacityUpdateBlock
153
163
  });
154
164
  continuingBase = p1;
155
165
  } else {
@@ -170,10 +180,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
170
180
  mergeBlock: continuingBase.mergeBlock,
171
181
  dynamicContract: continuingBase.dynamicContract,
172
182
  mutPendingQueries: continuingBase.mutPendingQueries,
173
- prevQueryRange: continuingBase.prevQueryRange,
174
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
175
- prevRangeSize: continuingBase.prevRangeSize,
176
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
183
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
184
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
185
+ eventDensity: continuingBase.eventDensity,
186
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
177
187
  });
178
188
  let restId = nextPartitionIndexRef.contents.toString();
179
189
  nextPartitionIndexRef.contents = nextPartitionIndexRef.contents + 1 | 0;
@@ -185,10 +195,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
185
195
  mergeBlock: continuingBase.mergeBlock,
186
196
  dynamicContract: continuingBase.dynamicContract,
187
197
  mutPendingQueries: [],
188
- prevQueryRange: continuingBase.prevQueryRange,
189
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
190
- prevRangeSize: continuingBase.prevRangeSize,
191
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
198
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
199
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
200
+ eventDensity: continuingBase.eventDensity,
201
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
192
202
  });
193
203
  return completed;
194
204
  }
@@ -202,10 +212,10 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
202
212
  mergeBlock: continuingBase.mergeBlock,
203
213
  dynamicContract: continuingBase.dynamicContract,
204
214
  mutPendingQueries: continuingBase.mutPendingQueries,
205
- prevQueryRange: continuingBase.prevQueryRange,
206
- prevPrevQueryRange: continuingBase.prevPrevQueryRange,
207
- prevRangeSize: continuingBase.prevRangeSize,
208
- latestBlockRangeUpdateBlock: continuingBase.latestBlockRangeUpdateBlock
215
+ sourceRangeCapacity: continuingBase.sourceRangeCapacity,
216
+ prevSourceRangeCapacity: continuingBase.prevSourceRangeCapacity,
217
+ eventDensity: continuingBase.eventDensity,
218
+ latestSourceRangeCapacityUpdateBlock: continuingBase.latestSourceRangeCapacityUpdateBlock
209
219
  });
210
220
  return completed;
211
221
  }
@@ -310,17 +320,25 @@ function make(partitions, maxAddrInPartition, nextPartitionIndex, dynamicContrac
310
320
 
311
321
  function consumeFetchedQueries(mutPendingQueries, initialLatestFetchedBlock) {
312
322
  let latestFetchedBlock = initialLatestFetchedBlock;
313
- while ((() => {
314
- let tmp = false;
315
- if (mutPendingQueries.length !== 0) {
316
- let pq = Utils.$$Array.firstUnsafe(mutPendingQueries);
317
- 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;
318
334
  }
319
- return tmp;
320
- })()) {
321
- let removedQuery = mutPendingQueries.shift();
322
- latestFetchedBlock = removedQuery.fetchedBlock;
335
+ } else {
336
+ canContinue = false;
337
+ }
323
338
  };
339
+ if (consumedCount > 0) {
340
+ mutPendingQueries.splice(0, consumedCount);
341
+ }
324
342
  return latestFetchedBlock;
325
343
  }
326
344
 
@@ -350,67 +368,112 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
350
368
  let pendingQuery = getPendingQueryOrThrow(p$1, query.fromBlock);
351
369
  pendingQuery.fetchedBlock = latestFetchedBlock;
352
370
  let blockRange = (latestFetchedBlock.blockNumber - query.fromBlock | 0) + 1 | 0;
353
- let shouldUpdateBlockRange = false;
354
- 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) {
355
376
  let queryToBlock = query.toBlock;
356
377
  let tmp;
357
378
  if (queryToBlock !== undefined) {
358
- if (latestFetchedBlock.blockNumber >= queryToBlock) {
379
+ if (latestFetchedBlock.blockNumber < queryToBlock) {
380
+ tmp = itemsCount < query.itemsTarget;
381
+ } else {
359
382
  let minHistoryRange = getMinHistoryRange(p$1);
360
383
  tmp = minHistoryRange !== undefined ? ((queryToBlock - query.fromBlock | 0) + 1 | 0) >= minHistoryRange : false;
361
- } else {
362
- tmp = true;
363
384
  }
364
385
  } else {
365
386
  tmp = latestFetchedBlock.blockNumber < (knownHeight - 10 | 0);
366
387
  }
367
- shouldUpdateBlockRange = tmp;
388
+ shouldUpdateSourceRangeCapacity = tmp;
368
389
  }
369
- let updatedPrevQueryRange = shouldUpdateBlockRange ? blockRange : p$1.prevQueryRange;
370
- let updatedPrevPrevQueryRange = shouldUpdateBlockRange ? p$1.prevQueryRange : p$1.prevPrevQueryRange;
371
- 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;
372
392
  let mutPendingQueries = p$1.mutPendingQueries;
373
393
  let latestFetchedBlock$1 = p$1.latestFetchedBlock;
374
- while ((() => {
375
- let tmp = false;
376
- if (mutPendingQueries.length !== 0) {
377
- let pq = Utils.$$Array.firstUnsafe(mutPendingQueries);
378
- 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;
379
405
  }
380
- return tmp;
381
- })()) {
382
- let removedQuery = mutPendingQueries.shift();
383
- latestFetchedBlock$1 = removedQuery.fetchedBlock;
406
+ } else {
407
+ canContinue = false;
408
+ }
384
409
  };
410
+ if (consumedCount > 0) {
411
+ mutPendingQueries.splice(0, consumedCount);
412
+ }
385
413
  let updatedLatestFetchedBlock = latestFetchedBlock$1;
386
414
  let mergeBlock = p$1.mergeBlock;
387
415
  let partitionReachedMergeBlock = mergeBlock !== undefined ? updatedLatestFetchedBlock.blockNumber >= mergeBlock : false;
388
416
  if (partitionReachedMergeBlock) {
389
417
  Utils.Dict.deleteInPlace(mutEntities, p$1.id);
390
- } else {
391
- let updatedMainPartition_id = p$1.id;
392
- let updatedMainPartition_selection = p$1.selection;
393
- let updatedMainPartition_addressesByContractName = p$1.addressesByContractName;
394
- let updatedMainPartition_mergeBlock = p$1.mergeBlock;
395
- let updatedMainPartition_dynamicContract = p$1.dynamicContract;
396
- let updatedMainPartition_mutPendingQueries = p$1.mutPendingQueries;
397
- let updatedMainPartition_latestBlockRangeUpdateBlock = shouldUpdateBlockRange ? latestFetchedBlock.blockNumber : p$1.latestBlockRangeUpdateBlock;
398
- let updatedMainPartition = {
399
- id: updatedMainPartition_id,
400
- latestFetchedBlock: updatedLatestFetchedBlock,
401
- selection: updatedMainPartition_selection,
402
- addressesByContractName: updatedMainPartition_addressesByContractName,
403
- mergeBlock: updatedMainPartition_mergeBlock,
404
- dynamicContract: updatedMainPartition_dynamicContract,
405
- mutPendingQueries: updatedMainPartition_mutPendingQueries,
406
- prevQueryRange: updatedPrevQueryRange,
407
- prevPrevQueryRange: updatedPrevPrevQueryRange,
408
- prevRangeSize: updatedPrevRangeSize,
409
- 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
410
461
  };
411
- mutEntities[p$1.id] = updatedMainPartition;
412
462
  }
413
- 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
+ };
414
477
  }
415
478
 
416
479
  function getLatestFullyFetchedBlock(optimizedPartitions) {
@@ -476,13 +539,90 @@ function bufferReadyCount(fetchState) {
476
539
  return lo;
477
540
  }
478
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
+
479
550
  function compareBufferItem(a, b) {
480
- let blockOrdering = Primitive_int.compare(a.blockNumber, b.blockNumber);
481
- if (blockOrdering === 0) {
482
- 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;
483
575
  } else {
484
- 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;
485
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;
486
626
  }
487
627
 
488
628
  function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
@@ -514,18 +654,19 @@ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, f
514
654
  return latestOnBlockBlockNumber;
515
655
  }
516
656
 
517
- function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOpt, knownHeightOpt) {
657
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, mutItemsSortedOpt, blockLagOpt, knownHeightOpt) {
518
658
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
659
+ let mutItemsSorted = mutItemsSortedOpt !== undefined ? mutItemsSortedOpt : false;
519
660
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
520
661
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
521
- let mutItemsRef = mutItems;
662
+ let base = mutItems !== undefined ? (
663
+ mutItemsSorted ? mutItems : mergeIntoBuffer([], mutItems)
664
+ ) : fetchState.buffer;
665
+ let blockItems = [];
522
666
  let onBlockRegistrations = fetchState.onBlockRegistrations;
523
667
  let latestOnBlockBlockNumber;
524
668
  if (onBlockRegistrations.length !== 0) {
525
- let mutItems$1 = mutItemsRef;
526
- let item = (
527
- mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
528
- )[fetchState.maxOnBlockBufferSize - 1 | 0];
669
+ let item = base[fetchState.maxOnBlockBufferSize - 1 | 0];
529
670
  let maxBlockNumber;
530
671
  if (item !== undefined) {
531
672
  maxBlockNumber = item.blockNumber;
@@ -534,20 +675,16 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
534
675
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
535
676
  maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
536
677
  }
537
- let mutItems$2 = mutItemsRef;
538
- let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
539
- mutItemsRef = mutItems$3;
540
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
678
+ latestOnBlockBlockNumber = appendOnBlockItems(blockItems, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
541
679
  } else {
542
680
  latestOnBlockBlockNumber = knownHeight;
543
681
  }
544
- let mutItems$4 = mutItemsRef;
545
682
  let updatedFetchState_startBlock = fetchState.startBlock;
546
683
  let updatedFetchState_endBlock = fetchState.endBlock;
547
684
  let updatedFetchState_normalSelection = fetchState.normalSelection;
548
685
  let updatedFetchState_contractConfigs = fetchState.contractConfigs;
549
686
  let updatedFetchState_chainId = fetchState.chainId;
550
- 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;
551
688
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
552
689
  let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
553
690
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
@@ -657,10 +794,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
657
794
  mergeBlock: undefined,
658
795
  dynamicContract: isDynamic ? contractName : undefined,
659
796
  mutPendingQueries: [],
660
- prevQueryRange: 0,
661
- prevPrevQueryRange: 0,
662
- prevRangeSize: 0,
663
- latestBlockRangeUpdateBlock: 0
797
+ sourceRangeCapacity: 0,
798
+ prevSourceRangeCapacity: 0,
799
+ eventDensity: undefined,
800
+ latestSourceRangeCapacityUpdateBlock: 0
664
801
  });
665
802
  nextPartitionIndexRef = nextPartitionIndexRef + 1 | 0;
666
803
  };
@@ -708,10 +845,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
708
845
  mergeBlock: currentPBlock < nextPBlock ? nextPBlock : undefined,
709
846
  dynamicContract: currentP.dynamicContract,
710
847
  mutPendingQueries: currentP.mutPendingQueries,
711
- prevQueryRange: currentP.prevQueryRange,
712
- prevPrevQueryRange: currentP.prevPrevQueryRange,
713
- prevRangeSize: currentP.prevRangeSize,
714
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
848
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
849
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
850
+ eventDensity: currentP.eventDensity,
851
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
715
852
  });
716
853
  currentPRef = {
717
854
  id: nextP.id,
@@ -721,10 +858,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
721
858
  mergeBlock: nextP.mergeBlock,
722
859
  dynamicContract: nextP.dynamicContract,
723
860
  mutPendingQueries: nextP.mutPendingQueries,
724
- prevQueryRange: nextP.prevQueryRange,
725
- prevPrevQueryRange: nextP.prevPrevQueryRange,
726
- prevRangeSize: nextP.prevRangeSize,
727
- latestBlockRangeUpdateBlock: nextP.latestBlockRangeUpdateBlock
861
+ sourceRangeCapacity: nextP.sourceRangeCapacity,
862
+ prevSourceRangeCapacity: nextP.prevSourceRangeCapacity,
863
+ eventDensity: nextP.eventDensity,
864
+ latestSourceRangeCapacityUpdateBlock: nextP.latestSourceRangeCapacityUpdateBlock
728
865
  };
729
866
  } else {
730
867
  currentPRef = {
@@ -735,10 +872,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
735
872
  mergeBlock: currentP.mergeBlock,
736
873
  dynamicContract: currentP.dynamicContract,
737
874
  mutPendingQueries: currentP.mutPendingQueries,
738
- prevQueryRange: currentP.prevQueryRange,
739
- prevPrevQueryRange: currentP.prevPrevQueryRange,
740
- prevRangeSize: currentP.prevRangeSize,
741
- latestBlockRangeUpdateBlock: currentP.latestBlockRangeUpdateBlock
875
+ sourceRangeCapacity: currentP.sourceRangeCapacity,
876
+ prevSourceRangeCapacity: currentP.prevSourceRangeCapacity,
877
+ eventDensity: currentP.eventDensity,
878
+ latestSourceRangeCapacityUpdateBlock: currentP.latestSourceRangeCapacityUpdateBlock
742
879
  };
743
880
  }
744
881
  }
@@ -885,10 +1022,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
885
1022
  mergeBlock: p.mergeBlock,
886
1023
  dynamicContract: p.dynamicContract,
887
1024
  mutPendingQueries: p.mutPendingQueries,
888
- prevQueryRange: p.prevQueryRange,
889
- prevPrevQueryRange: p.prevPrevQueryRange,
890
- prevRangeSize: p.prevRangeSize,
891
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1025
+ sourceRangeCapacity: p.sourceRangeCapacity,
1026
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1027
+ eventDensity: p.eventDensity,
1028
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
892
1029
  };
893
1030
  let addressesByContractName = {};
894
1031
  addressesByContractName[contractName] = addresses;
@@ -900,10 +1037,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
900
1037
  mergeBlock: undefined,
901
1038
  dynamicContract: contractName,
902
1039
  mutPendingQueries: p.mutPendingQueries,
903
- prevQueryRange: p.prevQueryRange,
904
- prevPrevQueryRange: p.prevPrevQueryRange,
905
- prevRangeSize: p.prevRangeSize,
906
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1040
+ sourceRangeCapacity: p.sourceRangeCapacity,
1041
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1042
+ eventDensity: p.eventDensity,
1043
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
907
1044
  });
908
1045
  }
909
1046
  } else {
@@ -915,10 +1052,10 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
915
1052
  mergeBlock: p.mergeBlock,
916
1053
  dynamicContract: contractName,
917
1054
  mutPendingQueries: p.mutPendingQueries,
918
- prevQueryRange: p.prevQueryRange,
919
- prevPrevQueryRange: p.prevPrevQueryRange,
920
- prevRangeSize: p.prevRangeSize,
921
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1055
+ sourceRangeCapacity: p.sourceRangeCapacity,
1056
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1057
+ eventDensity: p.eventDensity,
1058
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
922
1059
  };
923
1060
  }
924
1061
  }
@@ -929,11 +1066,11 @@ function registerDynamicContracts(fetchState, indexingAddresses, items) {
929
1066
  }
930
1067
  IndexingAddresses.register(indexingAddresses, noEventsAddresses);
931
1068
  let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
932
- return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined);
1069
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined, undefined);
933
1070
  }
934
1071
 
935
- function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBlock, newItems) {
936
- let newItems$1 = newItems.filter(item => {
1072
+ function filterByClientAddress(items, indexingAddresses) {
1073
+ return items.filter(item => {
937
1074
  if (item.kind !== 0) {
938
1075
  return true;
939
1076
  }
@@ -944,7 +1081,10 @@ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBl
944
1081
  return true;
945
1082
  }
946
1083
  });
947
- 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);
948
1088
  }
949
1089
 
950
1090
  function startFetchingQueries(param, queries) {
@@ -959,6 +1099,7 @@ function startFetchingQueries(param, queries) {
959
1099
  toBlock: q.toBlock,
960
1100
  isChunk: q.isChunk,
961
1101
  itemsTarget: q.itemsTarget,
1102
+ itemsEst: q.itemsEst,
962
1103
  fetchedBlock: undefined
963
1104
  };
964
1105
  let inserted = false;
@@ -976,73 +1117,223 @@ function startFetchingQueries(param, queries) {
976
1117
  }
977
1118
  }
978
1119
 
979
- function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock, knownHeight, chainTargetBlock, maybeChunkRange, maxChunks, partition, selection, addressesByContractName) {
980
- let cost = 0;
981
- if (rangeFromBlock <= knownHeight && maxChunks > 0) {
982
- let exit = 0;
983
- if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
984
- 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;
985
1144
  }
986
- if (exit === 1) {
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) {
987
1157
  if (maybeChunkRange !== undefined) {
988
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
989
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
990
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1158
+ if (match > 0) {
1159
+ let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1160
+ if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) > maxBlock) {
1161
+ return pushSingleQuery(match, rangeEndBlock !== undefined);
1162
+ }
991
1163
  let chunkFromBlock = rangeFromBlock;
992
1164
  let chunkIdx = 0;
993
1165
  while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
994
1166
  let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
995
- let itemsTarget = densityItemsTarget(partition, chunkFromBlock, chunkToBlock, chainTargetBlock);
996
- queries.push({
997
- partitionId: partitionId,
998
- fromBlock: chunkFromBlock,
999
- toBlock: chunkToBlock,
1000
- isChunk: true,
1001
- itemsTarget: itemsTarget,
1002
- selection: selection,
1003
- addressesByContractName: addressesByContractName
1004
- });
1005
- cost = cost + itemsTarget;
1167
+ pushDensityPricedQuery(queries, partitionId, chunkFromBlock, chunkToBlock, true, match, chunkItemsMultiplier, chainTargetBlock, itemsTargetFloor, selection, addressesByContractName);
1006
1168
  chunkFromBlock = chunkToBlock + 1 | 0;
1007
1169
  chunkIdx = chunkIdx + 1 | 0;
1008
1170
  };
1009
- } else {
1010
- let itemsTarget$1 = densityItemsTarget(partition, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1011
- queries.push({
1012
- partitionId: partitionId,
1013
- fromBlock: rangeFromBlock,
1014
- toBlock: rangeEndBlock,
1015
- isChunk: rangeEndBlock !== undefined,
1016
- itemsTarget: itemsTarget$1,
1017
- selection: selection,
1018
- addressesByContractName: addressesByContractName
1019
- });
1020
- cost = cost + itemsTarget$1;
1171
+ return;
1021
1172
  }
1173
+ exit$1 = 2;
1022
1174
  } else {
1023
- let itemsTarget$2 = densityItemsTarget(partition, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1024
- queries.push({
1025
- partitionId: partitionId,
1026
- fromBlock: rangeFromBlock,
1027
- toBlock: rangeEndBlock,
1028
- isChunk: false,
1029
- itemsTarget: itemsTarget$2,
1030
- selection: selection,
1031
- addressesByContractName: addressesByContractName
1032
- });
1033
- cost = cost + itemsTarget$2;
1175
+ if (match > 0) {
1176
+ return pushSingleQuery(match, false);
1177
+ }
1178
+ exit$1 = 2;
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);
1186
+ }
1187
+ }
1188
+ }
1189
+
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;
1203
+ }
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;
1034
1212
  }
1213
+ } else {
1214
+ canContinue = false;
1035
1215
  }
1216
+ pqIdx = pqIdx + 1 | 0;
1217
+ };
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
+ };
1036
1228
  }
1037
- return cost;
1038
1229
  }
1039
1230
 
1040
- function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1041
- 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) {
1042
1331
  let blockLag = param.blockLag;
1043
1332
  let endBlock = param.endBlock;
1044
1333
  let optimizedPartitions = param.optimizedPartitions;
1045
- 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;
1046
1337
  if (headBlockNumber <= 0) {
1047
1338
  return "WaitingForNewBlock";
1048
1339
  }
@@ -1051,14 +1342,34 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1051
1342
  endBlock !== undefined ? headBlockNumber < endBlock : true
1052
1343
  ) && !isOnBlockBehindTheHead;
1053
1344
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
1345
+ let inFlightCounts = Array(partitionsCount);
1346
+ let reservations = [];
1347
+ let chainReserved = 0;
1348
+ let partitionIndexById = {};
1054
1349
  for (let idx = 0; idx < partitionsCount; ++idx) {
1055
1350
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1056
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;
1057
1366
  if (p.mutPendingQueries.length !== 0 || p.latestFetchedBlock.blockNumber < headBlockNumber) {
1058
1367
  shouldWaitForNewBlock = false;
1059
1368
  }
1060
1369
  }
1061
- 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 => []);
1062
1373
  let computeQueryEndBlock = p => {
1063
1374
  let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1064
1375
  if (blockLag !== 0) {
@@ -1067,124 +1378,18 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1067
1378
  return queryEndBlock;
1068
1379
  }
1069
1380
  };
1070
- let existingReservedByPartition = {};
1071
- let chainReserved = 0;
1072
- 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) {
1073
1385
  let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1074
1386
  let p$1 = optimizedPartitions.entities[partitionId$1];
1075
- let cost = 0;
1076
- for (let pqIdx = 0, pqIdx_finish = p$1.mutPendingQueries.length; pqIdx < pqIdx_finish; ++pqIdx) {
1077
- 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);
1078
1390
  }
1079
- existingReservedByPartition[partitionId$1] = cost;
1080
- chainReserved = chainReserved + cost;
1081
1391
  }
1082
- let fillStates = [];
1083
- let gapFillCost = 0;
1084
- for (let idx$2 = 0; idx$2 < partitionsCount; ++idx$2) {
1085
- let partitionId$2 = optimizedPartitions.idsInAscOrder[idx$2];
1086
- let p$2 = optimizedPartitions.entities[partitionId$2];
1087
- let bucket = queriesByPartitionIndex[idx$2];
1088
- let pendingCount = p$2.mutPendingQueries.length;
1089
- let queryEndBlock = computeQueryEndBlock(p$2);
1090
- let maybeChunkRange = getMinHistoryRange(p$2);
1091
- let cursor = p$2.latestFetchedBlock.blockNumber + 1 | 0;
1092
- let canContinue = true;
1093
- let chunksUsedThisCall = 0;
1094
- let pqIdx$1 = 0;
1095
- while (pqIdx$1 < pendingCount && canContinue) {
1096
- let pq = p$2.mutPendingQueries[pqIdx$1];
1097
- if (pq.fromBlock > cursor) {
1098
- let beforeLen = bucket.length;
1099
- 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, p$2.selection, p$2.addressesByContractName);
1100
- chunksUsedThisCall = chunksUsedThisCall + (bucket.length - beforeLen | 0) | 0;
1101
- gapFillCost = gapFillCost + cost$1;
1102
- }
1103
- let toBlock = pq.toBlock;
1104
- if (toBlock !== undefined && pq.isChunk) {
1105
- let match = pq.fetchedBlock;
1106
- if (match !== undefined) {
1107
- let blockNumber = match.blockNumber;
1108
- cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
1109
- } else {
1110
- cursor = toBlock + 1 | 0;
1111
- }
1112
- } else {
1113
- canContinue = false;
1114
- }
1115
- pqIdx$1 = pqIdx$1 + 1 | 0;
1116
- };
1117
- if (canContinue) {
1118
- fillStates.push({
1119
- partitionId: partitionId$2,
1120
- p: p$2,
1121
- cursor: cursor,
1122
- chunksUsedThisCall: chunksUsedThisCall,
1123
- pendingCount: pendingCount,
1124
- queryEndBlock: queryEndBlock,
1125
- maybeChunkRange: maybeChunkRange,
1126
- bucket: bucket
1127
- });
1128
- }
1129
- }
1130
- let rangeItemsTarget = Primitive_float.max(0, chainTargetItems - chainReserved - gapFillCost);
1131
- let reservedByPartition = {};
1132
- fillStates.forEach(fs => {
1133
- let cost = existingReservedByPartition[fs.partitionId];
1134
- for (let i = 0, i_finish = fs.bucket.length; i < i_finish; ++i) {
1135
- cost = cost + fs.bucket[i].itemsTarget;
1136
- }
1137
- reservedByPartition[fs.partitionId] = cost;
1138
- });
1139
- let emitQueries = (fs, budget) => {
1140
- let p = fs.p;
1141
- let eb = fs.queryEndBlock;
1142
- let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1143
- let minHistoryRange = fs.maybeChunkRange;
1144
- if (minHistoryRange !== undefined) {
1145
- let density = p.prevRangeSize / p.prevQueryRange;
1146
- let chunkSize = Js_math.ceil_int(minHistoryRange * 1.8);
1147
- let chunkCost = density * chunkSize;
1148
- let maxChunksRemaining = (10 - fs.pendingCount | 0) - fs.chunksUsedThisCall | 0;
1149
- let affordable = chunkCost > 0 ? Math.floor(budget / chunkCost) | 0 : maxChunksRemaining;
1150
- let numChunks = Primitive_int.max(1, Primitive_int.min(affordable, maxChunksRemaining));
1151
- let consumed = 0;
1152
- let created = 0;
1153
- let chunkFromBlock = fs.cursor;
1154
- while (created < numChunks && chunkFromBlock <= maxBlock) {
1155
- let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1156
- let itemsTarget = density * ((chunkToBlock - chunkFromBlock | 0) + 1 | 0);
1157
- fs.bucket.push({
1158
- partitionId: fs.partitionId,
1159
- fromBlock: chunkFromBlock,
1160
- toBlock: chunkToBlock,
1161
- isChunk: true,
1162
- itemsTarget: itemsTarget,
1163
- selection: p.selection,
1164
- addressesByContractName: p.addressesByContractName
1165
- });
1166
- consumed = consumed + itemsTarget;
1167
- chunkFromBlock = chunkToBlock + 1 | 0;
1168
- created = created + 1 | 0;
1169
- };
1170
- fs.cursor = chunkFromBlock;
1171
- fs.chunksUsedThisCall = fs.chunksUsedThisCall + created | 0;
1172
- return consumed;
1173
- }
1174
- let itemsTarget$1 = Primitive_float.min(Math.round(budget), 10000);
1175
- fs.bucket.push({
1176
- partitionId: fs.partitionId,
1177
- fromBlock: fs.cursor,
1178
- toBlock: fs.queryEndBlock,
1179
- isChunk: false,
1180
- itemsTarget: itemsTarget$1,
1181
- selection: p.selection,
1182
- addressesByContractName: p.addressesByContractName
1183
- });
1184
- fs.cursor = maxBlock + 1 | 0;
1185
- fs.chunksUsedThisCall = fs.chunksUsedThisCall + 1 | 0;
1186
- return itemsTarget$1;
1187
- };
1392
+ let freshBudget = Primitive_float.max(0, chainTargetItems - chainReserved);
1188
1393
  let isInRange = fs => {
1189
1394
  let tmp = false;
1190
1395
  if (fs.cursor <= chainTargetBlock) {
@@ -1192,41 +1397,16 @@ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1192
1397
  tmp = eb !== undefined ? fs.cursor <= eb : true;
1193
1398
  }
1194
1399
  if (tmp) {
1195
- return (fs.pendingCount + fs.chunksUsedThisCall | 0) < 10;
1400
+ return (fs.inFlightCount + fs.chunksUsedThisCall | 0) < 12;
1196
1401
  } else {
1197
1402
  return false;
1198
1403
  }
1199
1404
  };
1200
- let notFilledPartitions = fillStates.filter(isInRange);
1201
- let reservedFromRange = {
1202
- contents: 0
1203
- };
1204
- while (notFilledPartitions.length !== 0 && reservedFromRange.contents < rangeItemsTarget) {
1205
- let n = notFilledPartitions.length;
1206
- let footprintSum = {
1207
- contents: 0
1208
- };
1209
- notFilledPartitions.forEach(fs => {
1210
- footprintSum.contents = footprintSum.contents + reservedByPartition[fs.partitionId];
1211
- });
1212
- let line = (rangeItemsTarget - reservedFromRange.contents + footprintSum.contents) / n;
1213
- let next = [];
1214
- notFilledPartitions.forEach(fs => {
1215
- let reserved = reservedByPartition[fs.partitionId];
1216
- let budget = line - reserved;
1217
- if (budget <= 0) {
1218
- return;
1219
- }
1220
- let consumed = emitQueries(fs, budget);
1221
- reservedByPartition[fs.partitionId] = reserved + consumed;
1222
- reservedFromRange.contents = reservedFromRange.contents + consumed;
1223
- if (isInRange(fs)) {
1224
- next.push(fs);
1225
- return;
1226
- }
1227
- });
1228
- notFilledPartitions = next;
1229
- };
1405
+ let inRangeStates = fillStates.filter(isInRange);
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);
1230
1410
  let queries = queriesByPartitionIndex.flat();
1231
1411
  if (Utils.$$Array.isEmpty(queries)) {
1232
1412
  if (shouldWaitForNewBlock) {
@@ -1307,10 +1487,10 @@ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, add
1307
1487
  mergeBlock: undefined,
1308
1488
  dynamicContract: undefined,
1309
1489
  mutPendingQueries: [],
1310
- prevQueryRange: 0,
1311
- prevPrevQueryRange: 0,
1312
- prevRangeSize: 0,
1313
- latestBlockRangeUpdateBlock: 0
1490
+ sourceRangeCapacity: 0,
1491
+ prevSourceRangeCapacity: 0,
1492
+ eventDensity: undefined,
1493
+ latestSourceRangeCapacityUpdateBlock: 0
1314
1494
  });
1315
1495
  }
1316
1496
  let normalSelection = {
@@ -1386,6 +1566,7 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1386
1566
  toBlock: pq.toBlock,
1387
1567
  isChunk: pq.isChunk,
1388
1568
  itemsTarget: pq.itemsTarget,
1569
+ itemsEst: pq.itemsEst,
1389
1570
  fetchedBlock: {
1390
1571
  blockNumber: targetBlockNumber,
1391
1572
  blockTimestamp: 0
@@ -1444,10 +1625,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1444
1625
  mergeBlock: mergeBlock$1,
1445
1626
  dynamicContract: p.dynamicContract,
1446
1627
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1447
- prevQueryRange: p.prevQueryRange,
1448
- prevPrevQueryRange: p.prevPrevQueryRange,
1449
- prevRangeSize: p.prevRangeSize,
1450
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1628
+ sourceRangeCapacity: p.sourceRangeCapacity,
1629
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1630
+ eventDensity: p.eventDensity,
1631
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1451
1632
  });
1452
1633
  }
1453
1634
  }
@@ -1465,10 +1646,10 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1465
1646
  mergeBlock: p.mergeBlock,
1466
1647
  dynamicContract: p.dynamicContract,
1467
1648
  mutPendingQueries: rollbackPendingQueries(p.mutPendingQueries, targetBlockNumber),
1468
- prevQueryRange: p.prevQueryRange,
1469
- prevPrevQueryRange: p.prevPrevQueryRange,
1470
- prevRangeSize: p.prevRangeSize,
1471
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1649
+ sourceRangeCapacity: p.sourceRangeCapacity,
1650
+ prevSourceRangeCapacity: p.prevSourceRangeCapacity,
1651
+ eventDensity: p.eventDensity,
1652
+ latestSourceRangeCapacityUpdateBlock: p.latestSourceRangeCapacityUpdateBlock
1472
1653
  });
1473
1654
  }
1474
1655
  }
@@ -1491,7 +1672,7 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1491
1672
  let tmp;
1492
1673
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1493
1674
  return tmp <= targetBlockNumber;
1494
- }), undefined, undefined);
1675
+ }), true, undefined, undefined);
1495
1676
  }
1496
1677
 
1497
1678
  function resetPendingQueries(fetchState) {
@@ -1509,10 +1690,10 @@ function resetPendingQueries(fetchState) {
1509
1690
  mergeBlock: partition.mergeBlock,
1510
1691
  dynamicContract: partition.dynamicContract,
1511
1692
  mutPendingQueries: kept,
1512
- prevQueryRange: partition.prevQueryRange,
1513
- prevPrevQueryRange: partition.prevPrevQueryRange,
1514
- prevRangeSize: partition.prevRangeSize,
1515
- latestBlockRangeUpdateBlock: partition.latestBlockRangeUpdateBlock
1693
+ sourceRangeCapacity: partition.sourceRangeCapacity,
1694
+ prevSourceRangeCapacity: partition.prevSourceRangeCapacity,
1695
+ eventDensity: partition.eventDensity,
1696
+ latestSourceRangeCapacityUpdateBlock: partition.latestSourceRangeCapacityUpdateBlock
1516
1697
  };
1517
1698
  }
1518
1699
  }
@@ -1588,7 +1769,7 @@ function getProgressPercentage(fetchState) {
1588
1769
  if (firstEventBlock === undefined) {
1589
1770
  return 0;
1590
1771
  }
1591
- let totalRange = fetchState.knownHeight - firstEventBlock | 0;
1772
+ let totalRange = (fetchState.knownHeight - fetchState.blockLag | 0) - firstEventBlock | 0;
1592
1773
  if (totalRange <= 0) {
1593
1774
  return 0;
1594
1775
  }
@@ -1644,32 +1825,33 @@ function getProgressBlockNumberAt(fetchState, index) {
1644
1825
  function updateKnownHeight(fetchState, knownHeight) {
1645
1826
  if (knownHeight > fetchState.knownHeight) {
1646
1827
  Prometheus.IndexingKnownHeight.set(knownHeight, fetchState.chainId);
1647
- return updateInternal(fetchState, undefined, undefined, undefined, knownHeight);
1828
+ return updateInternal(fetchState, undefined, undefined, undefined, undefined, knownHeight);
1648
1829
  } else {
1649
1830
  return fetchState;
1650
1831
  }
1651
1832
  }
1652
1833
 
1653
- let minItemsTarget = 2000;
1834
+ let blockItemLogIndex = 16777216;
1654
1835
 
1655
- let maxItemsTarget = 10000;
1836
+ let maxInFlightChunksPerPartition = 12;
1656
1837
 
1657
- let blockItemLogIndex = 16777216;
1838
+ let maxChainConcurrency = 100;
1658
1839
 
1659
- let maxPendingChunksPerPartition = 10;
1840
+ let chunkRangeGrowthFactor = 1.8;
1660
1841
 
1661
1842
  export {
1662
1843
  deriveContractNameByAddress,
1663
- minItemsTarget,
1664
- maxItemsTarget,
1665
1844
  densityItemsTarget,
1666
1845
  getMinHistoryRange,
1846
+ getTrustedDensity,
1667
1847
  getMinQueryRange,
1668
1848
  OptimizedPartitions,
1669
1849
  bufferBlockNumber,
1670
1850
  bufferBlock,
1671
1851
  bufferReadyCount,
1852
+ getRegistrationIndex,
1672
1853
  compareBufferItem,
1854
+ mergeIntoBuffer,
1673
1855
  blockItemLogIndex,
1674
1856
  appendOnBlockItems,
1675
1857
  updateInternal,
@@ -1678,10 +1860,17 @@ export {
1678
1860
  addressesByContractNameGetAll,
1679
1861
  createPartitionsFromIndexingAddresses,
1680
1862
  registerDynamicContracts,
1863
+ filterByClientAddress,
1681
1864
  handleQueryResult,
1682
1865
  startFetchingQueries,
1683
- maxPendingChunksPerPartition,
1866
+ maxInFlightChunksPerPartition,
1867
+ maxChainConcurrency,
1868
+ chunkRangeGrowthFactor,
1869
+ pushDensityPricedQuery,
1684
1870
  pushGapFillQueries,
1871
+ walkPartitionPending,
1872
+ pushForwardCandidates,
1873
+ acceptCandidates,
1685
1874
  getNextQuery,
1686
1875
  hasReadyItem,
1687
1876
  getReadyItemsCount,