envio 3.3.0-alpha.1 → 3.3.0-alpha.11

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 (143) hide show
  1. package/evm.schema.json +10 -0
  2. package/index.d.ts +45 -25
  3. package/licenses/CLA.md +35 -0
  4. package/licenses/EULA.md +67 -0
  5. package/licenses/LICENSE.md +45 -0
  6. package/licenses/README.md +35 -0
  7. package/package.json +10 -9
  8. package/src/Address.res +5 -2
  9. package/src/Address.res.mjs +3 -1
  10. package/src/Api.res +1 -1
  11. package/src/Api.res.mjs +1 -1
  12. package/src/Batch.res +0 -6
  13. package/src/Batch.res.mjs +1 -12
  14. package/src/BatchProcessing.res +7 -12
  15. package/src/BatchProcessing.res.mjs +8 -7
  16. package/src/Bin.res +3 -0
  17. package/src/Bin.res.mjs +4 -0
  18. package/src/ChainFetching.res +53 -16
  19. package/src/ChainFetching.res.mjs +37 -19
  20. package/src/ChainMetadata.res +1 -11
  21. package/src/ChainMetadata.res.mjs +1 -10
  22. package/src/ChainState.res +630 -161
  23. package/src/ChainState.res.mjs +438 -101
  24. package/src/ChainState.resi +77 -4
  25. package/src/Config.res +70 -38
  26. package/src/Config.res.mjs +52 -12
  27. package/src/ContractRegisterContext.res +2 -12
  28. package/src/ContractRegisterContext.res.mjs +3 -5
  29. package/src/Core.res +34 -3
  30. package/src/Core.res.mjs +12 -0
  31. package/src/CrossChainState.res +130 -89
  32. package/src/CrossChainState.res.mjs +66 -61
  33. package/src/CrossChainState.resi +1 -1
  34. package/src/Ecosystem.res +5 -6
  35. package/src/Ecosystem.res.mjs +3 -3
  36. package/src/Envio.res +17 -11
  37. package/src/EnvioGlobal.res +53 -0
  38. package/src/EnvioGlobal.res.mjs +31 -0
  39. package/src/EventConfigBuilder.res +191 -127
  40. package/src/EventConfigBuilder.res.mjs +116 -66
  41. package/src/EventProcessing.res +77 -28
  42. package/src/EventProcessing.res.mjs +56 -29
  43. package/src/ExitOnCaughtUp.res +10 -2
  44. package/src/ExitOnCaughtUp.res.mjs +10 -1
  45. package/src/FetchState.res +684 -415
  46. package/src/FetchState.res.mjs +505 -486
  47. package/src/HandlerLoader.res +8 -104
  48. package/src/HandlerLoader.res.mjs +2 -81
  49. package/src/HandlerRegister.res +520 -144
  50. package/src/HandlerRegister.res.mjs +286 -133
  51. package/src/HandlerRegister.resi +24 -8
  52. package/src/IndexerState.res +10 -3
  53. package/src/IndexerState.res.mjs +11 -4
  54. package/src/IndexerState.resi +2 -1
  55. package/src/IndexingAddresses.res +108 -0
  56. package/src/IndexingAddresses.res.mjs +101 -0
  57. package/src/IndexingAddresses.resi +34 -0
  58. package/src/Internal.res +166 -48
  59. package/src/Internal.res.mjs +30 -2
  60. package/src/LoadLayer.res +5 -5
  61. package/src/LoadLayer.res.mjs +6 -6
  62. package/src/LogSelection.res +92 -217
  63. package/src/LogSelection.res.mjs +95 -184
  64. package/src/Main.res +71 -236
  65. package/src/Main.res.mjs +60 -145
  66. package/src/Metrics.res +74 -0
  67. package/src/Metrics.res.mjs +75 -0
  68. package/src/PgStorage.res +4 -4
  69. package/src/PgStorage.res.mjs +5 -5
  70. package/src/Prometheus.res +10 -63
  71. package/src/Prometheus.res.mjs +124 -171
  72. package/src/PruneStaleHistory.res +2 -2
  73. package/src/PruneStaleHistory.res.mjs +3 -3
  74. package/src/RawEvent.res +7 -2
  75. package/src/RawEvent.res.mjs +4 -4
  76. package/src/Rollback.res +3 -3
  77. package/src/Rollback.res.mjs +4 -4
  78. package/src/RollbackCommit.res +4 -1
  79. package/src/RollbackCommit.res.mjs +3 -2
  80. package/src/SimulateDeadInputTracker.res +85 -0
  81. package/src/SimulateDeadInputTracker.res.mjs +73 -0
  82. package/src/SimulateDeadInputTracker.resi +12 -0
  83. package/src/SimulateItems.res +128 -28
  84. package/src/SimulateItems.res.mjs +82 -40
  85. package/src/TestIndexer.res +55 -41
  86. package/src/TestIndexer.res.mjs +39 -32
  87. package/src/TopicFilter.res +1 -25
  88. package/src/TopicFilter.res.mjs +0 -74
  89. package/src/bindings/Performance.res +7 -0
  90. package/src/bindings/Performance.res.mjs +21 -0
  91. package/src/bindings/Performance.resi +7 -0
  92. package/src/bindings/Viem.res +0 -46
  93. package/src/bindings/Viem.res.mjs +1 -43
  94. package/src/sources/BlockStore.res +46 -0
  95. package/src/sources/BlockStore.res.mjs +24 -0
  96. package/src/sources/EventRouter.res +31 -49
  97. package/src/sources/EventRouter.res.mjs +12 -25
  98. package/src/sources/Evm.res +83 -72
  99. package/src/sources/Evm.res.mjs +53 -69
  100. package/src/sources/EvmChain.res +7 -34
  101. package/src/sources/EvmChain.res.mjs +6 -27
  102. package/src/sources/EvmRpcClient.res +71 -5
  103. package/src/sources/EvmRpcClient.res.mjs +13 -4
  104. package/src/sources/FieldMask.res +39 -0
  105. package/src/sources/FieldMask.res.mjs +42 -0
  106. package/src/sources/Fuel.res +9 -6
  107. package/src/sources/Fuel.res.mjs +5 -10
  108. package/src/sources/HyperFuelSource.res +46 -50
  109. package/src/sources/HyperFuelSource.res.mjs +64 -54
  110. package/src/sources/HyperSync.res +45 -42
  111. package/src/sources/HyperSync.res.mjs +68 -60
  112. package/src/sources/HyperSync.resi +14 -4
  113. package/src/sources/HyperSyncClient.res +107 -119
  114. package/src/sources/HyperSyncClient.res.mjs +36 -31
  115. package/src/sources/HyperSyncHeightStream.res +1 -8
  116. package/src/sources/HyperSyncHeightStream.res.mjs +0 -2
  117. package/src/sources/HyperSyncSource.res +60 -254
  118. package/src/sources/HyperSyncSource.res.mjs +57 -180
  119. package/src/sources/Rpc.res +15 -79
  120. package/src/sources/Rpc.res.mjs +25 -101
  121. package/src/sources/RpcSource.res +221 -514
  122. package/src/sources/RpcSource.res.mjs +216 -411
  123. package/src/sources/RpcWebSocketHeightStream.res +0 -5
  124. package/src/sources/RpcWebSocketHeightStream.res.mjs +0 -2
  125. package/src/sources/SimulateSource.res +44 -20
  126. package/src/sources/SimulateSource.res.mjs +41 -14
  127. package/src/sources/Source.res +34 -6
  128. package/src/sources/SourceManager.res +77 -19
  129. package/src/sources/SourceManager.res.mjs +63 -21
  130. package/src/sources/SourceManager.resi +10 -0
  131. package/src/sources/Svm.res +31 -16
  132. package/src/sources/Svm.res.mjs +36 -13
  133. package/src/sources/SvmHyperSyncClient.res +5 -32
  134. package/src/sources/SvmHyperSyncSource.res +159 -134
  135. package/src/sources/SvmHyperSyncSource.res.mjs +164 -129
  136. package/src/sources/TransactionStore.res +49 -0
  137. package/src/sources/TransactionStore.res.mjs +30 -0
  138. package/src/tui/Tui.res +13 -16
  139. package/src/tui/Tui.res.mjs +12 -14
  140. package/svm.schema.json +49 -37
  141. package/src/bindings/Hrtime.res +0 -58
  142. package/src/bindings/Hrtime.res.mjs +0 -90
  143. package/src/bindings/Hrtime.resi +0 -30
@@ -5,21 +5,26 @@ import * as Js_math from "@rescript/runtime/lib/es6/Js_math.js";
5
5
  import * as Logging from "./Logging.res.mjs";
6
6
  import * as Prometheus from "./Prometheus.res.mjs";
7
7
  import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
8
+ import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
8
9
  import * as Primitive_int from "@rescript/runtime/lib/es6/Primitive_int.js";
9
10
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
10
11
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
12
+ import * as Primitive_float from "@rescript/runtime/lib/es6/Primitive_float.js";
11
13
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
14
+ import * as IndexingAddresses from "./IndexingAddresses.res.mjs";
12
15
 
13
- function deriveEffectiveStartBlock(registrationBlock, contractStartBlock) {
14
- return Primitive_int.max(Primitive_int.max(registrationBlock, 0), Stdlib_Option.getOr(contractStartBlock, 0));
15
- }
16
+ let deriveContractNameByAddress = Utils.$$WeakMap.memoize(addressesByContractName => {
17
+ let result = {};
18
+ Utils.Dict.forEachWithKey(addressesByContractName, (addresses, contractName) => {
19
+ for (let i = 0, i_finish = addresses.length; i < i_finish; ++i) {
20
+ result[addresses[i]] = contractName;
21
+ }
22
+ });
23
+ return result;
24
+ });
16
25
 
17
- function calculateEstResponseSize(p, fromBlock, toBlock, maxQueryBlockNumber) {
18
- if (p.prevQueryRange <= 0) {
19
- return 10000;
20
- }
21
- let density = p.prevRangeSize / p.prevQueryRange;
22
- return ((Stdlib_Option.getOr(toBlock, maxQueryBlockNumber) - 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);
23
28
  }
24
29
 
25
30
  function getMinHistoryRange(p) {
@@ -30,6 +35,14 @@ function getMinHistoryRange(p) {
30
35
  }
31
36
  }
32
37
 
38
+ function getTrustedDensity(p) {
39
+ let match = p.prevQueryRange;
40
+ let match$1 = p.prevPrevQueryRange;
41
+ if (match !== 0 && match$1 !== 0) {
42
+ return p.prevRangeSize / match;
43
+ }
44
+ }
45
+
33
46
  function getMinQueryRange(partitions) {
34
47
  let min = 0;
35
48
  for (let i = 0, i_finish = partitions.length; i < i_finish; ++i) {
@@ -99,6 +112,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
99
112
  p1,
100
113
  p2
101
114
  ]);
115
+ let inheritedDensity = Stdlib_Option.getOr(getTrustedDensity(p1), 0) + Stdlib_Option.getOr(getTrustedDensity(p2), 0);
102
116
  continuingBase = {
103
117
  id: newId,
104
118
  latestFetchedBlock: {
@@ -112,7 +126,7 @@ function mergePartitionsAtBlock(p1, p2, potentialMergeBlock, contractName, maxAd
112
126
  mutPendingQueries: [],
113
127
  prevQueryRange: minRange,
114
128
  prevPrevQueryRange: minRange,
115
- prevRangeSize: 0,
129
+ prevRangeSize: Math.ceil(inheritedDensity * minRange) | 0,
116
130
  latestBlockRangeUpdateBlock: 0
117
131
  };
118
132
  } else {
@@ -349,11 +363,11 @@ function handleQueryResponse(optimizedPartitions, query, knownHeight, itemsCount
349
363
  let queryToBlock = query.toBlock;
350
364
  let tmp;
351
365
  if (queryToBlock !== undefined) {
352
- if (latestFetchedBlock.blockNumber >= queryToBlock) {
366
+ if (latestFetchedBlock.blockNumber < queryToBlock) {
367
+ tmp = itemsCount < query.itemsTarget;
368
+ } else {
353
369
  let minHistoryRange = getMinHistoryRange(p$1);
354
370
  tmp = minHistoryRange !== undefined ? ((queryToBlock - query.fromBlock | 0) + 1 | 0) >= minHistoryRange : false;
355
- } else {
356
- tmp = true;
357
371
  }
358
372
  } else {
359
373
  tmp = latestFetchedBlock.blockNumber < (knownHeight - 10 | 0);
@@ -454,40 +468,129 @@ function bufferBlock(param) {
454
468
  }
455
469
  }
456
470
 
471
+ function bufferReadyCount(fetchState) {
472
+ let frontier = bufferBlockNumber(fetchState);
473
+ let buffer = fetchState.buffer;
474
+ let lo = 0;
475
+ let hi = buffer.length;
476
+ while (lo < hi) {
477
+ let mid = (lo + hi | 0) / 2 | 0;
478
+ if (buffer[mid].blockNumber <= frontier) {
479
+ lo = mid + 1 | 0;
480
+ } else {
481
+ hi = mid;
482
+ }
483
+ };
484
+ return lo;
485
+ }
486
+
487
+ function getRegistrationIndex(item) {
488
+ if (item.kind === 0) {
489
+ return item.onEventRegistration.index;
490
+ } else {
491
+ return item.onBlockRegistration.index;
492
+ }
493
+ }
494
+
457
495
  function compareBufferItem(a, b) {
458
- let blockOrdering = Primitive_int.compare(a.blockNumber, b.blockNumber);
459
- if (blockOrdering === 0) {
460
- return Primitive_int.compare(a.logIndex, b.logIndex);
496
+ let ba = a.blockNumber;
497
+ let bb = b.blockNumber;
498
+ if (ba !== bb) {
499
+ if (ba < bb) {
500
+ return -1;
501
+ } else {
502
+ return 1;
503
+ }
504
+ }
505
+ let la = a.logIndex;
506
+ let lb = b.logIndex;
507
+ if (la !== lb) {
508
+ if (la < lb) {
509
+ return -1;
510
+ } else {
511
+ return 1;
512
+ }
513
+ }
514
+ let ia = getRegistrationIndex(a);
515
+ let ib = getRegistrationIndex(b);
516
+ if (ia < ib) {
517
+ return -1;
518
+ } else if (ia > ib) {
519
+ return 1;
461
520
  } else {
462
- return blockOrdering;
521
+ return 0;
463
522
  }
464
523
  }
465
524
 
466
- function numAddresses(fetchState) {
467
- return Utils.Dict.size(fetchState.indexingAddresses);
525
+ function mergeIntoBuffer(buffer, newItems) {
526
+ let n = newItems.length;
527
+ for (let i = 1; i < n; ++i) {
528
+ let x = newItems[i];
529
+ let j = i - 1 | 0;
530
+ while (j >= 0 && compareBufferItem(newItems[j], x) > 0) {
531
+ newItems[j + 1 | 0] = newItems[j];
532
+ j = j - 1 | 0;
533
+ };
534
+ newItems[j + 1 | 0] = x;
535
+ }
536
+ let m = buffer.length;
537
+ let merged = [];
538
+ let last = {
539
+ contents: undefined
540
+ };
541
+ let push = item => {
542
+ let l = last.contents;
543
+ if (l !== undefined && compareBufferItem(l, item) === 0) {
544
+ return;
545
+ }
546
+ merged.push(item);
547
+ last.contents = item;
548
+ };
549
+ let i$1 = 0;
550
+ let j$1 = 0;
551
+ while (i$1 < m && j$1 < n) {
552
+ let a = buffer[i$1];
553
+ let b = newItems[j$1];
554
+ if (compareBufferItem(a, b) <= 0) {
555
+ push(a);
556
+ i$1 = i$1 + 1 | 0;
557
+ } else {
558
+ push(b);
559
+ j$1 = j$1 + 1 | 0;
560
+ }
561
+ };
562
+ while (i$1 < m) {
563
+ push(buffer[i$1]);
564
+ i$1 = i$1 + 1 | 0;
565
+ };
566
+ while (j$1 < n) {
567
+ push(newItems[j$1]);
568
+ j$1 = j$1 + 1 | 0;
569
+ };
570
+ return merged;
468
571
  }
469
572
 
470
- function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
573
+ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
471
574
  let newItemsCounter = 0;
472
575
  let latestOnBlockBlockNumber = fromBlock;
473
576
  while (latestOnBlockBlockNumber < maxBlockNumber && newItemsCounter <= maxOnBlockBufferSize) {
474
577
  let blockNumber = latestOnBlockBlockNumber + 1 | 0;
475
578
  latestOnBlockBlockNumber = blockNumber;
476
- for (let configIdx = 0, configIdx_finish = onBlockConfigs.length; configIdx < configIdx_finish; ++configIdx) {
477
- let onBlockConfig = onBlockConfigs[configIdx];
478
- let startBlock = onBlockConfig.startBlock;
579
+ for (let configIdx = 0, configIdx_finish = onBlockRegistrations.length; configIdx < configIdx_finish; ++configIdx) {
580
+ let onBlockRegistration = onBlockRegistrations[configIdx];
581
+ let startBlock = onBlockRegistration.startBlock;
479
582
  let handlerStartBlock = startBlock !== undefined ? startBlock : indexerStartBlock;
480
583
  let tmp = false;
481
584
  if (blockNumber >= handlerStartBlock) {
482
- let endBlock = onBlockConfig.endBlock;
585
+ let endBlock = onBlockRegistration.endBlock;
483
586
  tmp = endBlock !== undefined ? blockNumber <= endBlock : true;
484
587
  }
485
- if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockConfig.interval) === 0) {
588
+ if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockRegistration.interval) === 0) {
486
589
  mutItems.push({
487
590
  kind: 1,
488
- onBlockConfig: onBlockConfig,
591
+ onBlockRegistration: onBlockRegistration,
489
592
  blockNumber: blockNumber,
490
- logIndex: 16777216 + onBlockConfig.index | 0
593
+ logIndex: 16777216 + onBlockRegistration.index | 0
491
594
  });
492
595
  newItemsCounter = newItemsCounter + 1 | 0;
493
596
  }
@@ -496,19 +599,19 @@ function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlo
496
599
  return latestOnBlockBlockNumber;
497
600
  }
498
601
 
499
- function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt, mutItems, blockLagOpt, knownHeightOpt) {
602
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, mutItemsSortedOpt, blockLagOpt, knownHeightOpt) {
500
603
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
501
- let indexingAddresses = indexingAddressesOpt !== undefined ? indexingAddressesOpt : fetchState.indexingAddresses;
604
+ let mutItemsSorted = mutItemsSortedOpt !== undefined ? mutItemsSortedOpt : false;
502
605
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
503
606
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
504
- let mutItemsRef = mutItems;
505
- let onBlockConfigs = fetchState.onBlockConfigs;
607
+ let base = mutItems !== undefined ? (
608
+ mutItemsSorted ? mutItems : mergeIntoBuffer([], mutItems)
609
+ ) : fetchState.buffer;
610
+ let blockItems = [];
611
+ let onBlockRegistrations = fetchState.onBlockRegistrations;
506
612
  let latestOnBlockBlockNumber;
507
- if (onBlockConfigs.length !== 0) {
508
- let mutItems$1 = mutItemsRef;
509
- let item = (
510
- mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
511
- )[fetchState.maxOnBlockBufferSize - 1 | 0];
613
+ if (onBlockRegistrations.length !== 0) {
614
+ let item = base[fetchState.maxOnBlockBufferSize - 1 | 0];
512
615
  let maxBlockNumber;
513
616
  if (item !== undefined) {
514
617
  maxBlockNumber = item.blockNumber;
@@ -517,45 +620,37 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
517
620
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
518
621
  maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
519
622
  }
520
- let mutItems$2 = mutItemsRef;
521
- let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
522
- mutItemsRef = mutItems$3;
523
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockConfigs, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
623
+ latestOnBlockBlockNumber = appendOnBlockItems(blockItems, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
524
624
  } else {
525
625
  latestOnBlockBlockNumber = knownHeight;
526
626
  }
527
- let mutItems$4 = mutItemsRef;
528
627
  let updatedFetchState_startBlock = fetchState.startBlock;
529
628
  let updatedFetchState_endBlock = fetchState.endBlock;
530
629
  let updatedFetchState_normalSelection = fetchState.normalSelection;
531
630
  let updatedFetchState_contractConfigs = fetchState.contractConfigs;
532
631
  let updatedFetchState_chainId = fetchState.chainId;
533
- let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
632
+ let updatedFetchState_buffer = blockItems.length !== 0 ? mergeIntoBuffer(base, blockItems) : base;
534
633
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
535
- let updatedFetchState_onBlockConfigs = fetchState.onBlockConfigs;
634
+ let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
536
635
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
537
636
  let updatedFetchState = {
538
637
  optimizedPartitions: optimizedPartitions,
539
638
  startBlock: updatedFetchState_startBlock,
540
639
  endBlock: updatedFetchState_endBlock,
541
640
  normalSelection: updatedFetchState_normalSelection,
542
- indexingAddresses: indexingAddresses,
543
641
  contractConfigs: updatedFetchState_contractConfigs,
544
642
  chainId: updatedFetchState_chainId,
545
643
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
546
644
  blockLag: blockLag,
547
645
  buffer: updatedFetchState_buffer,
548
646
  maxOnBlockBufferSize: updatedFetchState_maxOnBlockBufferSize,
549
- onBlockConfigs: updatedFetchState_onBlockConfigs,
647
+ onBlockRegistrations: updatedFetchState_onBlockRegistrations,
550
648
  knownHeight: knownHeight,
551
649
  firstEventBlock: updatedFetchState_firstEventBlock
552
650
  };
553
651
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, fetchState.chainId);
554
652
  Prometheus.IndexingBufferSize.set(updatedFetchState_buffer.length, fetchState.chainId);
555
653
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(updatedFetchState), fetchState.chainId);
556
- if (indexingAddresses !== fetchState.indexingAddresses) {
557
- Prometheus.IndexingAddresses.set(Utils.Dict.size(indexingAddresses), fetchState.chainId);
558
- }
559
654
  return updatedFetchState;
560
655
  }
561
656
 
@@ -737,11 +832,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
737
832
  return make(existingPartitions.concat(mergedPartitions), maxAddrInPartition, nextPartitionIndexRef, dynamicContracts);
738
833
  }
739
834
 
740
- function registerDynamicContracts(fetchState, items) {
741
- if (Utils.$$Array.isEmpty(fetchState.normalSelection.eventConfigs)) {
835
+ function registerDynamicContracts(fetchState, indexingAddresses, items) {
836
+ if (Utils.$$Array.isEmpty(fetchState.normalSelection.onEventRegistrations)) {
742
837
  Stdlib_JsError.throwWithMessage("Invalid configuration. No events to fetch for the dynamic contract registration.");
743
838
  }
744
- let indexingAddresses = fetchState.indexingAddresses;
745
839
  let registeringContractsByContract = {};
746
840
  let earliestRegisteringEventBlockNumber = Infinity;
747
841
  let noEventsAddresses = {};
@@ -759,14 +853,14 @@ function registerDynamicContracts(fetchState, items) {
759
853
  let dcWithStartBlock_address = dc.address;
760
854
  let dcWithStartBlock_contractName = dc.contractName;
761
855
  let dcWithStartBlock_registrationBlock = dc.registrationBlock;
762
- let dcWithStartBlock_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
856
+ let dcWithStartBlock_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
763
857
  let dcWithStartBlock = {
764
858
  address: dcWithStartBlock_address,
765
859
  contractName: dcWithStartBlock_contractName,
766
860
  registrationBlock: dcWithStartBlock_registrationBlock,
767
861
  effectiveStartBlock: dcWithStartBlock_effectiveStartBlock
768
862
  };
769
- let existingContract = indexingAddresses[dc.address];
863
+ let existingContract = IndexingAddresses.get(indexingAddresses, dc.address);
770
864
  if (existingContract !== undefined) {
771
865
  if (existingContract.contractName !== dc.contractName) {
772
866
  warnDifferentContractType(fetchState, existingContract, dcWithStartBlock);
@@ -797,14 +891,14 @@ function registerDynamicContracts(fetchState, items) {
797
891
  let dcAsIndexingAddress_address = dc.address;
798
892
  let dcAsIndexingAddress_contractName = dc.contractName;
799
893
  let dcAsIndexingAddress_registrationBlock = dc.registrationBlock;
800
- let dcAsIndexingAddress_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, undefined);
894
+ let dcAsIndexingAddress_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, undefined);
801
895
  let dcAsIndexingAddress = {
802
896
  address: dcAsIndexingAddress_address,
803
897
  contractName: dcAsIndexingAddress_contractName,
804
898
  registrationBlock: dcAsIndexingAddress_registrationBlock,
805
899
  effectiveStartBlock: dcAsIndexingAddress_effectiveStartBlock
806
900
  };
807
- let existingContract$1 = indexingAddresses[dc.address];
901
+ let existingContract$1 = IndexingAddresses.get(indexingAddresses, dc.address);
808
902
  if (existingContract$1 !== undefined) {
809
903
  if (existingContract$1.contractName !== dc.contractName) {
810
904
  warnDifferentContractType(fetchState, existingContract$1, dcAsIndexingAddress);
@@ -839,101 +933,103 @@ function registerDynamicContracts(fetchState, items) {
839
933
  }
840
934
  let dcContractNamesToStore = Object.keys(registeringContractsByContract);
841
935
  let hasNoEventsUpdates = !Utils.Dict.isEmpty(noEventsAddresses);
842
- if (dcContractNamesToStore.length !== 0) {
843
- let newPartitions = [];
844
- let newIndexingAddresses = Utils.Dict.shallowCopy(indexingAddresses);
845
- let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
846
- let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
847
- for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
848
- let contractName = dcContractNamesToStore[idx$1];
849
- if (!dynamicContractsRef.has(contractName)) {
850
- dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
851
- for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
852
- let p = mutExistingPartitions[idx$2];
853
- let addresses = p.addressesByContractName[contractName];
854
- if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
855
- let allPartitionContractNames = Object.keys(p.addressesByContractName);
856
- if (allPartitionContractNames.length !== 1) {
857
- let isFetching = p.mutPendingQueries.length !== 0;
858
- if (!isFetching) {
859
- let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
860
- let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
861
- Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
862
- mutExistingPartitions[idx$2] = {
863
- id: p.id,
864
- latestFetchedBlock: p.latestFetchedBlock,
865
- selection: p.selection,
866
- addressesByContractName: restAddressesByContractName,
867
- mergeBlock: p.mergeBlock,
868
- dynamicContract: p.dynamicContract,
869
- mutPendingQueries: p.mutPendingQueries,
870
- prevQueryRange: p.prevQueryRange,
871
- prevPrevQueryRange: p.prevPrevQueryRange,
872
- prevRangeSize: p.prevRangeSize,
873
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
874
- };
875
- let addressesByContractName = {};
876
- addressesByContractName[contractName] = addresses;
877
- newPartitions.push({
878
- id: newPartitionId,
879
- latestFetchedBlock: p.latestFetchedBlock,
880
- selection: fetchState.normalSelection,
881
- addressesByContractName: addressesByContractName,
882
- mergeBlock: undefined,
883
- dynamicContract: contractName,
884
- mutPendingQueries: p.mutPendingQueries,
885
- prevQueryRange: p.prevQueryRange,
886
- prevPrevQueryRange: p.prevPrevQueryRange,
887
- prevRangeSize: p.prevRangeSize,
888
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
889
- });
890
- }
891
- } else {
936
+ if (dcContractNamesToStore.length === 0) {
937
+ if (hasNoEventsUpdates) {
938
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
939
+ return fetchState;
940
+ } else {
941
+ return fetchState;
942
+ }
943
+ }
944
+ let newPartitions = [];
945
+ let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
946
+ let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
947
+ for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
948
+ let contractName = dcContractNamesToStore[idx$1];
949
+ if (!dynamicContractsRef.has(contractName)) {
950
+ dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
951
+ for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
952
+ let p = mutExistingPartitions[idx$2];
953
+ let addresses = p.addressesByContractName[contractName];
954
+ if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
955
+ let allPartitionContractNames = Object.keys(p.addressesByContractName);
956
+ if (allPartitionContractNames.length !== 1) {
957
+ let isFetching = p.mutPendingQueries.length !== 0;
958
+ if (!isFetching) {
959
+ let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
960
+ let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
961
+ Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
892
962
  mutExistingPartitions[idx$2] = {
893
963
  id: p.id,
894
964
  latestFetchedBlock: p.latestFetchedBlock,
895
965
  selection: p.selection,
896
- addressesByContractName: p.addressesByContractName,
966
+ addressesByContractName: restAddressesByContractName,
897
967
  mergeBlock: p.mergeBlock,
898
- dynamicContract: contractName,
968
+ dynamicContract: p.dynamicContract,
899
969
  mutPendingQueries: p.mutPendingQueries,
900
970
  prevQueryRange: p.prevQueryRange,
901
971
  prevPrevQueryRange: p.prevPrevQueryRange,
902
972
  prevRangeSize: p.prevRangeSize,
903
973
  latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
904
974
  };
975
+ let addressesByContractName = {};
976
+ addressesByContractName[contractName] = addresses;
977
+ newPartitions.push({
978
+ id: newPartitionId,
979
+ latestFetchedBlock: p.latestFetchedBlock,
980
+ selection: fetchState.normalSelection,
981
+ addressesByContractName: addressesByContractName,
982
+ mergeBlock: undefined,
983
+ dynamicContract: contractName,
984
+ mutPendingQueries: p.mutPendingQueries,
985
+ prevQueryRange: p.prevQueryRange,
986
+ prevPrevQueryRange: p.prevPrevQueryRange,
987
+ prevRangeSize: p.prevRangeSize,
988
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
989
+ });
905
990
  }
991
+ } else {
992
+ mutExistingPartitions[idx$2] = {
993
+ id: p.id,
994
+ latestFetchedBlock: p.latestFetchedBlock,
995
+ selection: p.selection,
996
+ addressesByContractName: p.addressesByContractName,
997
+ mergeBlock: p.mergeBlock,
998
+ dynamicContract: contractName,
999
+ mutPendingQueries: p.mutPendingQueries,
1000
+ prevQueryRange: p.prevQueryRange,
1001
+ prevPrevQueryRange: p.prevPrevQueryRange,
1002
+ prevRangeSize: p.prevRangeSize,
1003
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
1004
+ };
906
1005
  }
907
1006
  }
908
1007
  }
909
- let registeringContracts = registeringContractsByContract[contractName];
910
- Object.assign(newIndexingAddresses, registeringContracts);
911
1008
  }
912
- Object.assign(newIndexingAddresses, noEventsAddresses);
913
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
914
- return updateInternal(fetchState, optimizedPartitions, newIndexingAddresses, undefined, undefined, undefined);
915
- }
916
- if (!hasNoEventsUpdates) {
917
- return fetchState;
1009
+ let registeringContracts = registeringContractsByContract[contractName];
1010
+ IndexingAddresses.register(indexingAddresses, registeringContracts);
918
1011
  }
919
- let newIndexingContracts = Utils.Dict.shallowCopy(indexingAddresses);
920
- Object.assign(newIndexingContracts, noEventsAddresses);
921
- return updateInternal(fetchState, undefined, newIndexingContracts, undefined, undefined, undefined);
1012
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
1013
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
1014
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined, undefined);
922
1015
  }
923
1016
 
924
- function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
925
- let newItems$1 = newItems.filter(item => {
1017
+ function filterByClientAddress(items, indexingAddresses) {
1018
+ return items.filter(item => {
926
1019
  if (item.kind !== 0) {
927
1020
  return true;
928
1021
  }
929
- let filter = item.eventConfig.clientAddressFilter;
1022
+ let filter = item.onEventRegistration.clientAddressFilter;
930
1023
  if (filter !== undefined) {
931
- return filter(item.payload, item.blockNumber, fetchState.indexingAddresses);
1024
+ return filter(item.payload, item.blockNumber, IndexingAddresses.rawForFilter(indexingAddresses));
932
1025
  } else {
933
1026
  return true;
934
1027
  }
935
1028
  });
936
- return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems$1.length, latestFetchedBlock), undefined, newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
1029
+ }
1030
+
1031
+ function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
1032
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems.length, latestFetchedBlock), newItems.length !== 0 ? mergeIntoBuffer(fetchState.buffer, newItems) : undefined, true, undefined, undefined);
937
1033
  }
938
1034
 
939
1035
  function startFetchingQueries(param, queries) {
@@ -947,7 +1043,8 @@ function startFetchingQueries(param, queries) {
947
1043
  fromBlock: q.fromBlock,
948
1044
  toBlock: q.toBlock,
949
1045
  isChunk: q.isChunk,
950
- estResponseSize: q.estResponseSize,
1046
+ itemsTarget: q.itemsTarget,
1047
+ itemsEst: q.itemsEst,
951
1048
  fetchedBlock: undefined
952
1049
  };
953
1050
  let inserted = false;
@@ -965,199 +1062,141 @@ function startFetchingQueries(param, queries) {
965
1062
  }
966
1063
  }
967
1064
 
968
- function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber, maybeChunkRange, partition, selection, addressesByContractName, indexingAddresses) {
969
- if (rangeFromBlock > maxQueryBlockNumber) {
970
- return;
971
- }
972
- if (rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock) {
973
- return;
974
- }
975
- if (maybeChunkRange !== undefined) {
976
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
977
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
978
- let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
979
- let getChunkSize = chunkIdx => {
980
- if (chunkIdx < 2) {
981
- return probeSize;
982
- } else {
983
- return chunkSize;
984
- }
985
- };
986
- if ((((rangeFromBlock + probeSize | 0) + probeSize | 0) - 1 | 0) <= maxBlock) {
987
- let chunkFromBlock = rangeFromBlock;
988
- let chunkIdx = 0;
989
- while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
990
- let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
1065
+ function pushGapFillQueries(queries, partitionId, rangeFromBlock, rangeEndBlock, knownHeight, chainTargetBlock, maybeChunkRange, maxChunks, partition, partitionBudget, chunkItemsMultiplier, selection, addressesByContractName) {
1066
+ let cost = {
1067
+ contents: 0
1068
+ };
1069
+ if (rangeFromBlock <= Primitive_int.min(knownHeight, chainTargetBlock) && maxChunks > 0) {
1070
+ let exit = 0;
1071
+ if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
1072
+ exit = 1;
1073
+ }
1074
+ if (exit === 1) {
1075
+ let trustedDensity = getTrustedDensity(partition);
1076
+ let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : chainTargetBlock;
1077
+ let pushSingleQuery = (density, isChunk) => {
1078
+ let itemsTarget = densityItemsTarget(density * chunkItemsMultiplier, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1079
+ let itemsEst = densityItemsTarget(density, rangeFromBlock, rangeEndBlock, chainTargetBlock);
991
1080
  queries.push({
992
1081
  partitionId: partitionId,
993
- fromBlock: chunkFromBlock,
994
- toBlock: chunkToBlock,
995
- isChunk: true,
996
- estResponseSize: calculateEstResponseSize(partition, chunkFromBlock, chunkToBlock, maxQueryBlockNumber),
997
- chainId: 0,
998
- progress: 0,
1082
+ fromBlock: rangeFromBlock,
1083
+ toBlock: rangeEndBlock,
1084
+ isChunk: isChunk,
1085
+ itemsTarget: itemsTarget,
1086
+ itemsEst: itemsEst,
999
1087
  selection: selection,
1000
- addressesByContractName: addressesByContractName,
1001
- indexingAddresses: indexingAddresses
1088
+ addressesByContractName: addressesByContractName
1002
1089
  });
1003
- chunkFromBlock = chunkToBlock + 1 | 0;
1004
- chunkIdx = chunkIdx + 1 | 0;
1090
+ cost.contents = cost.contents + itemsEst;
1005
1091
  };
1006
- return;
1092
+ if (trustedDensity !== undefined) {
1093
+ if (maybeChunkRange !== undefined && trustedDensity > 0) {
1094
+ let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1095
+ if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1096
+ let chunkFromBlock = rangeFromBlock;
1097
+ let chunkIdx = 0;
1098
+ while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
1099
+ let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
1100
+ let itemsTarget = densityItemsTarget(trustedDensity * chunkItemsMultiplier, chunkFromBlock, chunkToBlock, chainTargetBlock);
1101
+ let itemsEst = densityItemsTarget(trustedDensity, chunkFromBlock, chunkToBlock, chainTargetBlock);
1102
+ queries.push({
1103
+ partitionId: partitionId,
1104
+ fromBlock: chunkFromBlock,
1105
+ toBlock: chunkToBlock,
1106
+ isChunk: true,
1107
+ itemsTarget: itemsTarget,
1108
+ itemsEst: itemsEst,
1109
+ selection: selection,
1110
+ addressesByContractName: addressesByContractName
1111
+ });
1112
+ cost.contents = cost.contents + itemsEst;
1113
+ chunkFromBlock = chunkToBlock + 1 | 0;
1114
+ chunkIdx = chunkIdx + 1 | 0;
1115
+ };
1116
+ } else {
1117
+ pushSingleQuery(trustedDensity, rangeEndBlock !== undefined);
1118
+ }
1119
+ } else {
1120
+ pushSingleQuery(trustedDensity, false);
1121
+ }
1122
+ } else {
1123
+ let remainingRange = Primitive_int.max(1, (chainTargetBlock - rangeFromBlock | 0) + 1 | 0);
1124
+ pushSingleQuery(partitionBudget / remainingRange, false);
1125
+ }
1007
1126
  }
1008
- queries.push({
1009
- partitionId: partitionId,
1010
- fromBlock: rangeFromBlock,
1011
- toBlock: rangeEndBlock,
1012
- isChunk: rangeEndBlock !== undefined,
1013
- estResponseSize: calculateEstResponseSize(partition, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber),
1014
- chainId: 0,
1015
- progress: 0,
1016
- selection: selection,
1017
- addressesByContractName: addressesByContractName,
1018
- indexingAddresses: indexingAddresses
1019
- });
1020
- return;
1021
1127
  }
1022
- queries.push({
1023
- partitionId: partitionId,
1024
- fromBlock: rangeFromBlock,
1025
- toBlock: rangeEndBlock,
1026
- isChunk: false,
1027
- estResponseSize: calculateEstResponseSize(partition, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber),
1028
- chainId: 0,
1029
- progress: 0,
1030
- selection: selection,
1031
- addressesByContractName: addressesByContractName,
1032
- indexingAddresses: indexingAddresses
1033
- });
1128
+ return cost.contents;
1034
1129
  }
1035
1130
 
1036
- function getNextQuery(fetchState, budget, chainPendingBudget) {
1037
- let knownHeight = fetchState.knownHeight;
1038
- let blockLag = fetchState.blockLag;
1039
- let indexingAddresses = fetchState.indexingAddresses;
1040
- let optimizedPartitions = fetchState.optimizedPartitions;
1131
+ function getNextQuery(param, chainTargetBlock, chainTargetItems, $staropt$star) {
1132
+ let knownHeight = param.knownHeight;
1133
+ let blockLag = param.blockLag;
1134
+ let endBlock = param.endBlock;
1135
+ let optimizedPartitions = param.optimizedPartitions;
1136
+ let chunkItemsMultiplier = $staropt$star !== undefined ? $staropt$star : 1;
1041
1137
  let headBlockNumber = knownHeight - blockLag | 0;
1042
1138
  if (headBlockNumber <= 0) {
1043
1139
  return "WaitingForNewBlock";
1044
1140
  }
1045
- if (budget <= 0) {
1046
- return "NothingToQuery";
1047
- }
1048
- let isOnBlockBehindTheHead = fetchState.latestOnBlockBlockNumber < headBlockNumber;
1049
- let endBlock = fetchState.endBlock;
1141
+ let isOnBlockBehindTheHead = param.latestOnBlockBlockNumber < headBlockNumber;
1050
1142
  let shouldWaitForNewBlock = (
1051
1143
  endBlock !== undefined ? headBlockNumber < endBlock : true
1052
1144
  ) && !isOnBlockBehindTheHead;
1053
- let item = fetchState.buffer[(budget + (chainPendingBudget | 0) | 0) - 1 | 0];
1054
- let maxQueryBlockNumber = item !== undefined ? Primitive_int.min(item.blockNumber, knownHeight) : knownHeight;
1055
- let queries = [];
1056
1145
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
1057
- let idxRef = 0;
1058
- while (idxRef < partitionsCount) {
1059
- let idx = idxRef;
1146
+ for (let idx = 0; idx < partitionsCount; ++idx) {
1060
1147
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1061
1148
  let p = optimizedPartitions.entities[partitionId];
1062
- let pendingCount = p.mutPendingQueries.length;
1063
- let isBehindTheHead = p.latestFetchedBlock.blockNumber < headBlockNumber;
1064
- let hasPendingQueries = pendingCount > 0;
1065
- if (hasPendingQueries || isBehindTheHead) {
1149
+ if (p.mutPendingQueries.length !== 0 || p.latestFetchedBlock.blockNumber < headBlockNumber) {
1066
1150
  shouldWaitForNewBlock = false;
1067
1151
  }
1068
- let partitionQueriesStart = queries.length;
1069
- let queryEndBlock = Utils.$$Math.minOptInt(fetchState.endBlock, p.mergeBlock);
1070
- let queryEndBlock$1 = blockLag !== 0 ? Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock) : queryEndBlock;
1071
- let match = maxQueryBlockNumber < knownHeight;
1072
- let queryEndBlock$2 = queryEndBlock$1 !== undefined ? (
1073
- match ? Primitive_int.min(maxQueryBlockNumber, queryEndBlock$1) : queryEndBlock$1
1074
- ) : (
1075
- match ? maxQueryBlockNumber : queryEndBlock$1
1076
- );
1077
- let maybeChunkRange = getMinHistoryRange(p);
1078
- let cursor = p.latestFetchedBlock.blockNumber + 1 | 0;
1152
+ }
1153
+ let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount, param => []);
1154
+ let computeQueryEndBlock = p => {
1155
+ let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1156
+ if (blockLag !== 0) {
1157
+ return Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock);
1158
+ } else {
1159
+ return queryEndBlock;
1160
+ }
1161
+ };
1162
+ let chainReserved = 0;
1163
+ let reservations = [];
1164
+ let partitionIndexById = {};
1165
+ let candidates = [];
1166
+ let fillStates = [];
1167
+ for (let idx$1 = 0; idx$1 < partitionsCount; ++idx$1) {
1168
+ let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1169
+ let p$1 = optimizedPartitions.entities[partitionId$1];
1170
+ partitionIndexById[partitionId$1] = idx$1;
1171
+ let pendingCount = p$1.mutPendingQueries.length;
1172
+ for (let pqIdx = 0; pqIdx < pendingCount; ++pqIdx) {
1173
+ let pq = p$1.mutPendingQueries[pqIdx];
1174
+ if (pq.fetchedBlock === undefined) {
1175
+ chainReserved = chainReserved + pq.itemsEst;
1176
+ reservations.push([
1177
+ pq.fromBlock,
1178
+ pq.itemsEst
1179
+ ]);
1180
+ }
1181
+ }
1182
+ let queryEndBlock = computeQueryEndBlock(p$1);
1183
+ let maybeChunkRange = getMinHistoryRange(p$1);
1184
+ let cursor = p$1.latestFetchedBlock.blockNumber + 1 | 0;
1079
1185
  let canContinue = true;
1080
- let pqIdx = 0;
1081
- while (pqIdx < p.mutPendingQueries.length && canContinue) {
1082
- let pq = p.mutPendingQueries[pqIdx];
1083
- if (pq.fromBlock > cursor) {
1084
- let addressesByContractName = p.addressesByContractName;
1085
- let selection = p.selection;
1086
- let rangeEndBlock = Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock$2);
1087
- let rangeFromBlock = cursor;
1088
- if (rangeFromBlock <= maxQueryBlockNumber) {
1089
- let exit = 0;
1090
- if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
1091
- exit = 1;
1092
- }
1093
- if (exit === 1) {
1094
- if (maybeChunkRange !== undefined) {
1095
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
1096
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1097
- let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
1098
- let getChunkSize = chunkIdx => {
1099
- if (chunkIdx < 2) {
1100
- return probeSize;
1101
- } else {
1102
- return chunkSize;
1103
- }
1104
- };
1105
- if ((((rangeFromBlock + getChunkSize(0) | 0) + getChunkSize(1) | 0) - 1 | 0) <= maxBlock) {
1106
- let chunkFromBlock = rangeFromBlock;
1107
- let chunkIdx = 0;
1108
- while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
1109
- let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
1110
- queries.push({
1111
- partitionId: partitionId,
1112
- fromBlock: chunkFromBlock,
1113
- toBlock: chunkToBlock,
1114
- isChunk: true,
1115
- estResponseSize: calculateEstResponseSize(p, chunkFromBlock, chunkToBlock, maxQueryBlockNumber),
1116
- chainId: 0,
1117
- progress: 0,
1118
- selection: selection,
1119
- addressesByContractName: addressesByContractName,
1120
- indexingAddresses: indexingAddresses
1121
- });
1122
- chunkFromBlock = chunkToBlock + 1 | 0;
1123
- chunkIdx = chunkIdx + 1 | 0;
1124
- };
1125
- } else {
1126
- queries.push({
1127
- partitionId: partitionId,
1128
- fromBlock: rangeFromBlock,
1129
- toBlock: rangeEndBlock,
1130
- isChunk: rangeEndBlock !== undefined,
1131
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber),
1132
- chainId: 0,
1133
- progress: 0,
1134
- selection: selection,
1135
- addressesByContractName: addressesByContractName,
1136
- indexingAddresses: indexingAddresses
1137
- });
1138
- }
1139
- } else {
1140
- queries.push({
1141
- partitionId: partitionId,
1142
- fromBlock: rangeFromBlock,
1143
- toBlock: rangeEndBlock,
1144
- isChunk: false,
1145
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock, rangeEndBlock, maxQueryBlockNumber),
1146
- chainId: 0,
1147
- progress: 0,
1148
- selection: selection,
1149
- addressesByContractName: addressesByContractName,
1150
- indexingAddresses: indexingAddresses
1151
- });
1152
- }
1153
- }
1154
- }
1186
+ let chunksUsedThisCall = 0;
1187
+ let pqIdx$1 = 0;
1188
+ while (pqIdx$1 < pendingCount && canContinue) {
1189
+ let pq$1 = p$1.mutPendingQueries[pqIdx$1];
1190
+ if (pq$1.fromBlock > cursor) {
1191
+ let beforeLen = candidates.length;
1192
+ pushGapFillQueries(candidates, partitionId$1, cursor, Utils.$$Math.minOptInt(pq$1.fromBlock - 1 | 0, queryEndBlock), knownHeight, chainTargetBlock, maybeChunkRange, (12 - pendingCount | 0) - chunksUsedThisCall | 0, p$1, chainTargetItems / partitionsCount, chunkItemsMultiplier, p$1.selection, p$1.addressesByContractName);
1193
+ chunksUsedThisCall = chunksUsedThisCall + (candidates.length - beforeLen | 0) | 0;
1155
1194
  }
1156
- let toBlock = pq.toBlock;
1157
- if (toBlock !== undefined && pq.isChunk) {
1158
- let match$1 = pq.fetchedBlock;
1159
- if (match$1 !== undefined) {
1160
- let blockNumber = match$1.blockNumber;
1195
+ let toBlock = pq$1.toBlock;
1196
+ if (toBlock !== undefined && pq$1.isChunk) {
1197
+ let match = pq$1.fetchedBlock;
1198
+ if (match !== undefined) {
1199
+ let blockNumber = match.blockNumber;
1161
1200
  cursor = blockNumber < toBlock ? blockNumber + 1 | 0 : toBlock + 1 | 0;
1162
1201
  } else {
1163
1202
  cursor = toBlock + 1 | 0;
@@ -1165,87 +1204,139 @@ function getNextQuery(fetchState, budget, chainPendingBudget) {
1165
1204
  } else {
1166
1205
  canContinue = false;
1167
1206
  }
1168
- pqIdx = pqIdx + 1 | 0;
1207
+ pqIdx$1 = pqIdx$1 + 1 | 0;
1169
1208
  };
1170
1209
  if (canContinue) {
1171
- let addressesByContractName$1 = p.addressesByContractName;
1172
- let selection$1 = p.selection;
1173
- let rangeFromBlock$1 = cursor;
1174
- if (rangeFromBlock$1 <= maxQueryBlockNumber) {
1175
- let exit$1 = 0;
1176
- if (!(queryEndBlock$2 !== undefined && rangeFromBlock$1 > queryEndBlock$2)) {
1177
- exit$1 = 1;
1178
- }
1179
- if (exit$1 === 1) {
1180
- if (maybeChunkRange !== undefined) {
1181
- let maxBlock$1 = queryEndBlock$2 !== undefined ? queryEndBlock$2 : maxQueryBlockNumber;
1182
- let chunkSize$1 = Js_math.ceil_int(maybeChunkRange * 1.8);
1183
- let probeSize$1 = Js_math.ceil_int(maybeChunkRange * 0.9);
1184
- let getChunkSize$1 = chunkIdx => {
1185
- if (chunkIdx < 2) {
1186
- return probeSize$1;
1187
- } else {
1188
- return chunkSize$1;
1189
- }
1190
- };
1191
- if ((((rangeFromBlock$1 + getChunkSize$1(0) | 0) + getChunkSize$1(1) | 0) - 1 | 0) <= maxBlock$1) {
1192
- let chunkFromBlock$1 = rangeFromBlock$1;
1193
- let chunkIdx$1 = 0;
1194
- while (chunkIdx$1 < 5 && ((chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0) <= maxBlock$1) {
1195
- let chunkToBlock$1 = (chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0;
1196
- queries.push({
1197
- partitionId: partitionId,
1198
- fromBlock: chunkFromBlock$1,
1199
- toBlock: chunkToBlock$1,
1200
- isChunk: true,
1201
- estResponseSize: calculateEstResponseSize(p, chunkFromBlock$1, chunkToBlock$1, maxQueryBlockNumber),
1202
- chainId: 0,
1203
- progress: 0,
1204
- selection: selection$1,
1205
- addressesByContractName: addressesByContractName$1,
1206
- indexingAddresses: indexingAddresses
1207
- });
1208
- chunkFromBlock$1 = chunkToBlock$1 + 1 | 0;
1209
- chunkIdx$1 = chunkIdx$1 + 1 | 0;
1210
- };
1211
- } else {
1212
- queries.push({
1213
- partitionId: partitionId,
1214
- fromBlock: rangeFromBlock$1,
1215
- toBlock: queryEndBlock$2,
1216
- isChunk: queryEndBlock$2 !== undefined,
1217
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock$1, queryEndBlock$2, maxQueryBlockNumber),
1218
- chainId: 0,
1219
- progress: 0,
1220
- selection: selection$1,
1221
- addressesByContractName: addressesByContractName$1,
1222
- indexingAddresses: indexingAddresses
1223
- });
1224
- }
1225
- } else {
1226
- queries.push({
1227
- partitionId: partitionId,
1228
- fromBlock: rangeFromBlock$1,
1229
- toBlock: queryEndBlock$2,
1230
- isChunk: false,
1231
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock$1, queryEndBlock$2, maxQueryBlockNumber),
1232
- chainId: 0,
1233
- progress: 0,
1234
- selection: selection$1,
1235
- addressesByContractName: addressesByContractName$1,
1236
- indexingAddresses: indexingAddresses
1237
- });
1238
- }
1239
- }
1210
+ fillStates.push({
1211
+ partitionId: partitionId$1,
1212
+ p: p$1,
1213
+ cursor: cursor,
1214
+ chunksUsedThisCall: chunksUsedThisCall,
1215
+ pendingCount: pendingCount,
1216
+ queryEndBlock: queryEndBlock,
1217
+ maybeChunkRange: maybeChunkRange
1218
+ });
1219
+ }
1220
+ }
1221
+ let freshBudget = Primitive_float.max(0, chainTargetItems - chainReserved);
1222
+ let isInRange = fs => {
1223
+ let tmp = false;
1224
+ if (fs.cursor <= chainTargetBlock) {
1225
+ let eb = fs.queryEndBlock;
1226
+ tmp = eb !== undefined ? fs.cursor <= eb : true;
1227
+ }
1228
+ if (tmp) {
1229
+ return (fs.pendingCount + fs.chunksUsedThisCall | 0) < 12;
1230
+ } else {
1231
+ return false;
1232
+ }
1233
+ };
1234
+ let inRangeStates = fillStates.filter(isInRange);
1235
+ let inRangeCount = inRangeStates.length;
1236
+ let probeShare = inRangeCount === 0 ? 0 : freshBudget / inRangeCount;
1237
+ let frontierCursor = Stdlib_Array.reduce(inRangeStates, chainTargetBlock, (min, fs) => {
1238
+ if (fs.cursor < min) {
1239
+ return fs.cursor;
1240
+ } else {
1241
+ return min;
1242
+ }
1243
+ });
1244
+ let rangeToTarget = (chainTargetBlock - frontierCursor | 0) + 1 | 0;
1245
+ let rangeTargetDensity = inRangeCount > 0 && rangeToTarget > 0 ? freshBudget / rangeToTarget : 0;
1246
+ inRangeStates.forEach(fs => {
1247
+ let p = fs.p;
1248
+ let eb = fs.queryEndBlock;
1249
+ let maxBlock = eb !== undefined ? eb : chainTargetBlock;
1250
+ let match = fs.maybeChunkRange;
1251
+ let match$1 = getTrustedDensity(p);
1252
+ if (match !== undefined && match$1 !== undefined && match$1 > 0) {
1253
+ let chunkSize = Js_math.ceil_int(match * 1.8);
1254
+ let maxChunksRemaining = (12 - fs.pendingCount | 0) - fs.chunksUsedThisCall | 0;
1255
+ let chunkStartCeiling = Primitive_int.min(maxBlock, chainTargetBlock);
1256
+ let created = 0;
1257
+ let chunkFromBlock = fs.cursor;
1258
+ let generatedItems = 0;
1259
+ while (created < maxChunksRemaining && chunkFromBlock <= chunkStartCeiling && generatedItems <= freshBudget) {
1260
+ let chunkToBlock = Primitive_int.min((chunkFromBlock + chunkSize | 0) - 1 | 0, maxBlock);
1261
+ let rawEst = match$1 * ((chunkToBlock - chunkFromBlock | 0) + 1 | 0);
1262
+ let itemsEst = Primitive_int.max(1, Math.ceil(rawEst) | 0);
1263
+ let itemsTarget = Primitive_int.max(1, Math.ceil(rawEst * chunkItemsMultiplier) | 0);
1264
+ candidates.push({
1265
+ partitionId: fs.partitionId,
1266
+ fromBlock: chunkFromBlock,
1267
+ toBlock: chunkToBlock,
1268
+ isChunk: true,
1269
+ itemsTarget: itemsTarget,
1270
+ itemsEst: itemsEst,
1271
+ selection: p.selection,
1272
+ addressesByContractName: p.addressesByContractName
1273
+ });
1274
+ generatedItems = generatedItems + itemsEst;
1275
+ chunkFromBlock = chunkToBlock + 1 | 0;
1276
+ created = created + 1 | 0;
1277
+ };
1278
+ return;
1279
+ }
1280
+ let itemsTarget$1 = rangeToTarget > 0 ? Primitive_int.max(1, Math.round(rangeTargetDensity * ((chainTargetBlock - fs.cursor | 0) + 1 | 0) / inRangeCount) | 0) : Primitive_int.max(1, Math.round(probeShare) | 0);
1281
+ candidates.push({
1282
+ partitionId: fs.partitionId,
1283
+ fromBlock: fs.cursor,
1284
+ toBlock: fs.queryEndBlock,
1285
+ isChunk: false,
1286
+ itemsTarget: itemsTarget$1,
1287
+ itemsEst: itemsTarget$1,
1288
+ selection: p.selection,
1289
+ addressesByContractName: p.addressesByContractName
1290
+ });
1291
+ });
1292
+ let acceptanceStream = [];
1293
+ candidates.forEach(query => {
1294
+ acceptanceStream.push([
1295
+ query.fromBlock,
1296
+ query.itemsEst,
1297
+ query
1298
+ ]);
1299
+ });
1300
+ reservations.forEach(param => {
1301
+ acceptanceStream.push([
1302
+ param[0],
1303
+ param[1],
1304
+ undefined
1305
+ ]);
1306
+ });
1307
+ acceptanceStream.sort((param, param$1) => {
1308
+ let bFrom = param$1[0];
1309
+ let aFrom = param[0];
1310
+ if (aFrom !== bFrom) {
1311
+ return Primitive_int.compare(aFrom, bFrom);
1312
+ }
1313
+ let bQuery = param$1[2];
1314
+ if (param[2] !== undefined) {
1315
+ if (bQuery !== undefined) {
1316
+ return 0;
1317
+ } else {
1318
+ return 1;
1240
1319
  }
1320
+ } else if (bQuery !== undefined) {
1321
+ return -1;
1322
+ } else {
1323
+ return 0;
1241
1324
  }
1242
- let maxNewChunks = Primitive_int.max(0, 10 - pendingCount | 0);
1243
- let generatedCount = queries.length - partitionQueriesStart | 0;
1244
- if (generatedCount > maxNewChunks) {
1245
- queries.splice(partitionQueriesStart + maxNewChunks | 0, generatedCount - maxNewChunks | 0);
1325
+ });
1326
+ let streamCount = acceptanceStream.length;
1327
+ let remainingBudget = chainTargetItems;
1328
+ let acceptIdx = 0;
1329
+ while (remainingBudget > 0 && acceptIdx < streamCount) {
1330
+ let match$1 = acceptanceStream[acceptIdx];
1331
+ let maybeQuery = match$1[2];
1332
+ if (maybeQuery !== undefined) {
1333
+ let partitionIdx = partitionIndexById[maybeQuery.partitionId];
1334
+ queriesByPartitionIndex[partitionIdx].push(maybeQuery);
1246
1335
  }
1247
- idxRef = idxRef + 1 | 0;
1336
+ remainingBudget = remainingBudget - match$1[1];
1337
+ acceptIdx = acceptIdx + 1 | 0;
1248
1338
  };
1339
+ let queries = queriesByPartitionIndex.flat();
1249
1340
  if (Utils.$$Array.isEmpty(queries)) {
1250
1341
  if (shouldWaitForNewBlock) {
1251
1342
  return "WaitingForNewBlock";
@@ -1292,9 +1383,9 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1292
1383
  return acc;
1293
1384
  }
1294
1385
 
1295
- function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1386
+ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockRegistrationsOpt, blockLagOpt, firstEventBlockOpt) {
1296
1387
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1297
- let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1388
+ let onBlockRegistrations = onBlockRegistrationsOpt !== undefined ? onBlockRegistrationsOpt : [];
1298
1389
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
1299
1390
  let firstEventBlock = firstEventBlockOpt !== undefined ? Primitive_option.valFromOption(firstEventBlockOpt) : undefined;
1300
1391
  let latestFetchedBlock = {
@@ -1302,32 +1393,14 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1302
1393
  blockTimestamp: 0
1303
1394
  };
1304
1395
  let notDependingOnAddresses = [];
1305
- let normalEventConfigs = [];
1396
+ let normalRegistrations = [];
1306
1397
  let contractNamesWithNormalEvents = new Set();
1307
- let indexingAddresses = {};
1308
- let contractConfigs = {};
1309
- eventConfigs.forEach(ec => {
1310
- let match = contractConfigs[ec.contractName];
1311
- if (match !== undefined) {
1312
- let startBlock = match.startBlock;
1313
- let match$1 = ec.startBlock;
1314
- contractConfigs[ec.contractName] = {
1315
- startBlock: startBlock !== undefined ? (
1316
- match$1 !== undefined ? Primitive_int.min(startBlock, match$1) : startBlock
1317
- ) : (
1318
- match$1 !== undefined ? match$1 : undefined
1319
- )
1320
- };
1398
+ onEventRegistrations.forEach(reg => {
1399
+ if (reg.dependsOnAddresses) {
1400
+ normalRegistrations.push(reg);
1401
+ contractNamesWithNormalEvents.add(reg.eventConfig.contractName);
1321
1402
  } else {
1322
- contractConfigs[ec.contractName] = {
1323
- startBlock: ec.startBlock
1324
- };
1325
- }
1326
- if (ec.dependsOnAddresses) {
1327
- normalEventConfigs.push(ec);
1328
- contractNamesWithNormalEvents.add(ec.contractName);
1329
- } else {
1330
- notDependingOnAddresses.push(ec);
1403
+ notDependingOnAddresses.push(reg);
1331
1404
  }
1332
1405
  });
1333
1406
  let partitions = [];
@@ -1336,7 +1409,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1336
1409
  id: partitions.length.toString(),
1337
1410
  latestFetchedBlock: latestFetchedBlock,
1338
1411
  selection: {
1339
- eventConfigs: notDependingOnAddresses,
1412
+ onEventRegistrations: notDependingOnAddresses,
1340
1413
  dependsOnAddresses: false
1341
1414
  },
1342
1415
  addressesByContractName: {},
@@ -1350,47 +1423,34 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1350
1423
  });
1351
1424
  }
1352
1425
  let normalSelection = {
1353
- eventConfigs: normalEventConfigs,
1426
+ onEventRegistrations: normalRegistrations,
1354
1427
  dependsOnAddresses: true
1355
1428
  };
1356
1429
  let registeringContractsByContract = {};
1357
1430
  let dynamicContracts = new Set();
1358
1431
  addresses.forEach(contract => {
1359
1432
  let contractName = contract.contractName;
1360
- let match = contractConfigs[contractName];
1361
- let contractStartBlock = match !== undefined ? match.startBlock : undefined;
1362
- let ia_address = contract.address;
1363
- let ia_contractName = contract.contractName;
1364
- let ia_registrationBlock = contract.registrationBlock;
1365
- let ia_effectiveStartBlock = deriveEffectiveStartBlock(contract.registrationBlock, contractStartBlock);
1366
- let ia = {
1367
- address: ia_address,
1368
- contractName: ia_contractName,
1369
- registrationBlock: ia_registrationBlock,
1370
- effectiveStartBlock: ia_effectiveStartBlock
1371
- };
1372
- indexingAddresses[contract.address] = ia;
1373
- if (!contractNamesWithNormalEvents.has(contractName)) {
1374
- return;
1375
- }
1376
- let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1377
- registeringContracts[contract.address] = ia;
1378
- if (contract.registrationBlock !== -1) {
1379
- dynamicContracts.add(contractName);
1380
- return;
1433
+ if (contractNamesWithNormalEvents.has(contractName)) {
1434
+ Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName)[contract.address] = IndexingAddresses.makeIndexingAddress(contract, contractConfigs);
1435
+ if (contract.registrationBlock !== -1) {
1436
+ dynamicContracts.add(contractName);
1437
+ return;
1438
+ } else {
1439
+ return;
1440
+ }
1381
1441
  }
1382
1442
  });
1383
1443
  let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
1384
- if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockConfigs)) {
1385
- Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + chainId.toString() + `. ` + (`addresses=` + addresses.length.toString() + `, `) + (`eventConfigs=` + eventConfigs.length.toString() + `, `) + (`normalEventConfigs=` + normalEventConfigs.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1444
+ if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockRegistrations)) {
1445
+ Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + chainId.toString() + `. ` + (`addresses=` + addresses.length.toString() + `, `) + (`onEventRegistrations=` + onEventRegistrations.length.toString() + `, `) + (`normalRegistrations=` + normalRegistrations.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1386
1446
  }
1387
1447
  let buffer = [];
1388
1448
  let latestOnBlockBlockNumber;
1389
- if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockConfigs)) {
1449
+ if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockRegistrations)) {
1390
1450
  let id = optimizedPartitions.idsInAscOrder[0];
1391
1451
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
1392
1452
  let maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
1393
- latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1453
+ latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockRegistrations, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1394
1454
  } else {
1395
1455
  latestOnBlockBlockNumber = progressBlockNumber;
1396
1456
  }
@@ -1399,19 +1459,16 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1399
1459
  startBlock: startBlock,
1400
1460
  endBlock: endBlock,
1401
1461
  normalSelection: normalSelection,
1402
- indexingAddresses: indexingAddresses,
1403
1462
  contractConfigs: contractConfigs,
1404
1463
  chainId: chainId,
1405
1464
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
1406
1465
  blockLag: blockLag,
1407
1466
  buffer: buffer,
1408
1467
  maxOnBlockBufferSize: maxOnBlockBufferSize,
1409
- onBlockConfigs: onBlockConfigs,
1468
+ onBlockRegistrations: onBlockRegistrations,
1410
1469
  knownHeight: knownHeight,
1411
1470
  firstEventBlock: firstEventBlock
1412
1471
  };
1413
- let numAddresses = Utils.Dict.size(indexingAddresses);
1414
- Prometheus.IndexingAddresses.set(numAddresses, chainId);
1415
1472
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, chainId);
1416
1473
  Prometheus.IndexingBufferSize.set(buffer.length, chainId);
1417
1474
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(fetchState), chainId);
@@ -1425,22 +1482,6 @@ function bufferSize(param) {
1425
1482
  return param.buffer.length;
1426
1483
  }
1427
1484
 
1428
- function bufferReadyCount(fetchState) {
1429
- let frontier = bufferBlockNumber(fetchState);
1430
- let buffer = fetchState.buffer;
1431
- let lo = 0;
1432
- let hi = buffer.length;
1433
- while (lo < hi) {
1434
- let mid = (lo + hi | 0) / 2 | 0;
1435
- if (buffer[mid].blockNumber <= frontier) {
1436
- lo = mid + 1 | 0;
1437
- } else {
1438
- hi = mid;
1439
- }
1440
- };
1441
- return lo;
1442
- }
1443
-
1444
1485
  function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1445
1486
  let adjusted = [];
1446
1487
  for (let qIdx = 0, qIdx_finish = mutPendingQueries.length; qIdx < qIdx_finish; ++qIdx) {
@@ -1453,7 +1494,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1453
1494
  fromBlock: pq.fromBlock,
1454
1495
  toBlock: pq.toBlock,
1455
1496
  isChunk: pq.isChunk,
1456
- estResponseSize: pq.estResponseSize,
1497
+ itemsTarget: pq.itemsTarget,
1498
+ itemsEst: pq.itemsEst,
1457
1499
  fetchedBlock: {
1458
1500
  blockNumber: targetBlockNumber,
1459
1501
  blockTimestamp: 0
@@ -1470,16 +1512,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1470
1512
  return adjusted;
1471
1513
  }
1472
1514
 
1473
- function rollback(fetchState, targetBlockNumber) {
1474
- let addressesToRemove = new Set();
1475
- let indexingAddresses = {};
1476
- Utils.Dict.forEachWithKey(fetchState.indexingAddresses, (indexingContract, address) => {
1477
- if (indexingContract.registrationBlock > targetBlockNumber) {
1478
- addressesToRemove.add(address);
1479
- } else {
1480
- indexingAddresses[address] = indexingContract;
1481
- }
1482
- });
1515
+ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1516
+ IndexingAddresses.rollbackInPlace(indexingAddresses, targetBlockNumber);
1483
1517
  let keptPartitions = [];
1484
1518
  let nextKeptIdRef = 0;
1485
1519
  let registeringContractsByContract = {};
@@ -1490,11 +1524,12 @@ function rollback(fetchState, targetBlockNumber) {
1490
1524
  if (p.latestFetchedBlock.blockNumber > targetBlockNumber) {
1491
1525
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1492
1526
  addresses.forEach(address => {
1493
- if (!(!addressesToRemove.has(address) && Stdlib_Option.isSome(indexingAddresses[address]))) {
1527
+ let indexingContract = IndexingAddresses.get(indexingAddresses, address);
1528
+ if (indexingContract === undefined) {
1494
1529
  return;
1495
1530
  }
1496
1531
  let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1497
- registeringContracts[address] = indexingAddresses[address];
1532
+ registeringContracts[address] = indexingContract;
1498
1533
  });
1499
1534
  });
1500
1535
  } else {
@@ -1502,7 +1537,7 @@ function rollback(fetchState, targetBlockNumber) {
1502
1537
  let mergeBlock$1 = mergeBlock !== undefined && mergeBlock > targetBlockNumber ? targetBlockNumber : mergeBlock;
1503
1538
  let rollbackedAddressesByContractName = {};
1504
1539
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1505
- let keptAddresses = addresses.filter(address => !addressesToRemove.has(address));
1540
+ let keptAddresses = addresses.filter(address => Stdlib_Option.isSome(IndexingAddresses.get(indexingAddresses, address)));
1506
1541
  if (keptAddresses.length !== 0) {
1507
1542
  rollbackedAddressesByContractName[contractName] = keptAddresses;
1508
1543
  return;
@@ -1553,21 +1588,20 @@ function rollback(fetchState, targetBlockNumber) {
1553
1588
  startBlock: fetchState.startBlock,
1554
1589
  endBlock: fetchState.endBlock,
1555
1590
  normalSelection: fetchState.normalSelection,
1556
- indexingAddresses: fetchState.indexingAddresses,
1557
1591
  contractConfigs: fetchState.contractConfigs,
1558
1592
  chainId: fetchState.chainId,
1559
1593
  latestOnBlockBlockNumber: Primitive_int.min(fetchState.latestOnBlockBlockNumber, targetBlockNumber),
1560
1594
  blockLag: fetchState.blockLag,
1561
1595
  buffer: fetchState.buffer,
1562
1596
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1563
- onBlockConfigs: fetchState.onBlockConfigs,
1597
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1564
1598
  knownHeight: fetchState.knownHeight,
1565
1599
  firstEventBlock: fetchState.firstEventBlock
1566
- }, optimizedPartitions, indexingAddresses, fetchState.buffer.filter(item => {
1600
+ }, optimizedPartitions, fetchState.buffer.filter(item => {
1567
1601
  let tmp;
1568
1602
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1569
1603
  return tmp <= targetBlockNumber;
1570
- }), undefined, undefined);
1604
+ }), true, undefined, undefined);
1571
1605
  }
1572
1606
 
1573
1607
  function resetPendingQueries(fetchState) {
@@ -1604,14 +1638,13 @@ function resetPendingQueries(fetchState) {
1604
1638
  startBlock: fetchState.startBlock,
1605
1639
  endBlock: fetchState.endBlock,
1606
1640
  normalSelection: fetchState.normalSelection,
1607
- indexingAddresses: fetchState.indexingAddresses,
1608
1641
  contractConfigs: fetchState.contractConfigs,
1609
1642
  chainId: fetchState.chainId,
1610
1643
  latestOnBlockBlockNumber: fetchState.latestOnBlockBlockNumber,
1611
1644
  blockLag: fetchState.blockLag,
1612
1645
  buffer: fetchState.buffer,
1613
1646
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1614
- onBlockConfigs: fetchState.onBlockConfigs,
1647
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1615
1648
  knownHeight: fetchState.knownHeight,
1616
1649
  firstEventBlock: fetchState.firstEventBlock
1617
1650
  };
@@ -1674,19 +1707,6 @@ function getProgressPercentage(fetchState) {
1674
1707
  return progress / totalRange;
1675
1708
  }
1676
1709
 
1677
- function getProgressPercentageAt(fetchState, blockNumber) {
1678
- let firstEventBlock = fetchState.firstEventBlock;
1679
- if (firstEventBlock === undefined) {
1680
- return 0;
1681
- }
1682
- let totalRange = fetchState.knownHeight - firstEventBlock | 0;
1683
- if (totalRange <= 0) {
1684
- return 0;
1685
- } else {
1686
- return (blockNumber - firstEventBlock | 0) / totalRange;
1687
- }
1688
- }
1689
-
1690
1710
  function hasFullBatch(fetchState, batchSizeTarget) {
1691
1711
  let item = fetchState.buffer[batchSizeTarget - 1 | 0];
1692
1712
  if (item !== undefined) {
@@ -1740,24 +1760,24 @@ function updateKnownHeight(fetchState, knownHeight) {
1740
1760
  }
1741
1761
  }
1742
1762
 
1743
- let defaultEstResponseSize = 10000;
1744
-
1745
1763
  let blockItemLogIndex = 16777216;
1746
1764
 
1747
- let maxPendingChunksPerPartition = 10;
1765
+ let maxPendingChunksPerPartition = 12;
1748
1766
 
1749
1767
  export {
1750
- deriveEffectiveStartBlock,
1751
- defaultEstResponseSize,
1752
- calculateEstResponseSize,
1768
+ deriveContractNameByAddress,
1769
+ densityItemsTarget,
1753
1770
  getMinHistoryRange,
1771
+ getTrustedDensity,
1754
1772
  getMinQueryRange,
1755
1773
  OptimizedPartitions,
1756
1774
  bufferBlockNumber,
1757
1775
  bufferBlock,
1776
+ bufferReadyCount,
1777
+ getRegistrationIndex,
1758
1778
  compareBufferItem,
1779
+ mergeIntoBuffer,
1759
1780
  blockItemLogIndex,
1760
- numAddresses,
1761
1781
  appendOnBlockItems,
1762
1782
  updateInternal,
1763
1783
  warnDifferentContractType,
@@ -1765,16 +1785,16 @@ export {
1765
1785
  addressesByContractNameGetAll,
1766
1786
  createPartitionsFromIndexingAddresses,
1767
1787
  registerDynamicContracts,
1788
+ filterByClientAddress,
1768
1789
  handleQueryResult,
1769
1790
  startFetchingQueries,
1770
- pushQueriesForRange,
1771
1791
  maxPendingChunksPerPartition,
1792
+ pushGapFillQueries,
1772
1793
  getNextQuery,
1773
1794
  hasReadyItem,
1774
1795
  getReadyItemsCount,
1775
1796
  make$1 as make,
1776
1797
  bufferSize,
1777
- bufferReadyCount,
1778
1798
  rollbackPendingQueries,
1779
1799
  rollback,
1780
1800
  resetPendingQueries,
@@ -1782,9 +1802,8 @@ export {
1782
1802
  isFetchingAtHead,
1783
1803
  isReadyToEnterReorgThreshold,
1784
1804
  getProgressPercentage,
1785
- getProgressPercentageAt,
1786
1805
  sortForBatch,
1787
1806
  getProgressBlockNumberAt,
1788
1807
  updateKnownHeight,
1789
1808
  }
1790
- /* Utils Not a pure module */
1809
+ /* deriveContractNameByAddress Not a pure module */