envio 3.3.0-alpha.7 → 3.3.0-alpha.8

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 (89) hide show
  1. package/index.d.ts +23 -5
  2. package/package.json +6 -6
  3. package/src/Api.res +1 -1
  4. package/src/Api.res.mjs +1 -1
  5. package/src/ChainFetching.res +21 -10
  6. package/src/ChainFetching.res.mjs +11 -10
  7. package/src/ChainState.res +328 -165
  8. package/src/ChainState.res.mjs +205 -100
  9. package/src/ChainState.resi +16 -7
  10. package/src/Config.res +11 -27
  11. package/src/Config.res.mjs +8 -7
  12. package/src/Core.res +7 -0
  13. package/src/CrossChainState.res +53 -80
  14. package/src/CrossChainState.res.mjs +43 -59
  15. package/src/CrossChainState.resi +1 -1
  16. package/src/Ecosystem.res +3 -3
  17. package/src/Ecosystem.res.mjs +3 -3
  18. package/src/Envio.res +14 -9
  19. package/src/EventConfigBuilder.res +105 -56
  20. package/src/EventConfigBuilder.res.mjs +69 -33
  21. package/src/EventProcessing.res +19 -15
  22. package/src/EventProcessing.res.mjs +13 -12
  23. package/src/FetchState.res +375 -171
  24. package/src/FetchState.res.mjs +249 -251
  25. package/src/HandlerLoader.res +7 -112
  26. package/src/HandlerLoader.res.mjs +1 -87
  27. package/src/HandlerRegister.res +209 -6
  28. package/src/HandlerRegister.res.mjs +90 -5
  29. package/src/HandlerRegister.resi +13 -2
  30. package/src/IndexerState.res +6 -3
  31. package/src/IndexerState.res.mjs +3 -3
  32. package/src/IndexerState.resi +1 -1
  33. package/src/IndexingAddresses.res +10 -7
  34. package/src/IndexingAddresses.res.mjs +8 -7
  35. package/src/IndexingAddresses.resi +3 -1
  36. package/src/Internal.res +82 -36
  37. package/src/Internal.res.mjs +11 -1
  38. package/src/Main.res +22 -23
  39. package/src/Main.res.mjs +15 -17
  40. package/src/Metrics.res +43 -2
  41. package/src/Metrics.res.mjs +39 -3
  42. package/src/Prometheus.res +0 -35
  43. package/src/Prometheus.res.mjs +33 -68
  44. package/src/RawEvent.res +7 -2
  45. package/src/RawEvent.res.mjs +4 -4
  46. package/src/SimulateItems.res +20 -7
  47. package/src/SimulateItems.res.mjs +7 -11
  48. package/src/TestIndexer.res +1 -1
  49. package/src/TestIndexer.res.mjs +1 -1
  50. package/src/sources/BlockStore.res +46 -0
  51. package/src/sources/BlockStore.res.mjs +24 -0
  52. package/src/sources/EventRouter.res +19 -12
  53. package/src/sources/EventRouter.res.mjs +7 -5
  54. package/src/sources/Evm.res +54 -4
  55. package/src/sources/Evm.res.mjs +43 -4
  56. package/src/sources/EvmChain.res +14 -18
  57. package/src/sources/EvmChain.res.mjs +12 -13
  58. package/src/sources/FieldMask.res +39 -0
  59. package/src/sources/FieldMask.res.mjs +42 -0
  60. package/src/sources/Fuel.res +5 -2
  61. package/src/sources/Fuel.res.mjs +4 -3
  62. package/src/sources/HyperFuelSource.res +31 -40
  63. package/src/sources/HyperFuelSource.res.mjs +52 -44
  64. package/src/sources/HyperSync.res +31 -9
  65. package/src/sources/HyperSync.res.mjs +47 -31
  66. package/src/sources/HyperSync.resi +10 -3
  67. package/src/sources/HyperSyncClient.res +15 -4
  68. package/src/sources/HyperSyncHeightStream.res +1 -8
  69. package/src/sources/HyperSyncHeightStream.res.mjs +0 -2
  70. package/src/sources/HyperSyncSource.res +37 -58
  71. package/src/sources/HyperSyncSource.res.mjs +42 -42
  72. package/src/sources/RpcSource.res +91 -82
  73. package/src/sources/RpcSource.res.mjs +76 -45
  74. package/src/sources/RpcWebSocketHeightStream.res +0 -5
  75. package/src/sources/RpcWebSocketHeightStream.res.mjs +0 -2
  76. package/src/sources/SimulateSource.res +5 -3
  77. package/src/sources/SimulateSource.res.mjs +12 -4
  78. package/src/sources/Source.res +19 -5
  79. package/src/sources/SourceManager.res +62 -6
  80. package/src/sources/SourceManager.res.mjs +55 -8
  81. package/src/sources/SourceManager.resi +10 -0
  82. package/src/sources/Svm.res +14 -10
  83. package/src/sources/Svm.res.mjs +23 -6
  84. package/src/sources/SvmHyperSyncClient.res +5 -6
  85. package/src/sources/SvmHyperSyncSource.res +82 -41
  86. package/src/sources/SvmHyperSyncSource.res.mjs +86 -41
  87. package/src/sources/TransactionStore.res +6 -107
  88. package/src/sources/TransactionStore.res.mjs +5 -86
  89. package/svm.schema.json +19 -0
@@ -5,6 +5,7 @@ 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";
@@ -22,16 +23,9 @@ let deriveContractNameByAddress = Utils.$$WeakMap.memoize(addressesByContractNam
22
23
  return result;
23
24
  });
24
25
 
25
- function calculateDefaultEstResponseSize(partitionsCount) {
26
- return Primitive_float.max(2000, Primitive_float.min(10000, 20000 / partitionsCount));
27
- }
28
-
29
- function calculateEstResponseSize(p, fromBlock, toBlock, knownHeight, partitionsCount) {
30
- if (p.prevQueryRange <= 0) {
31
- return calculateDefaultEstResponseSize(partitionsCount);
32
- }
26
+ function densityItemsTarget(p, fromBlock, toBlock, chainTargetBlock) {
33
27
  let density = p.prevRangeSize / p.prevQueryRange;
34
- return ((Stdlib_Option.getOr(toBlock, knownHeight) - fromBlock | 0) + 1 | 0) * density;
28
+ return ((Stdlib_Option.getOr(toBlock, chainTargetBlock) - fromBlock | 0) + 1 | 0) * density;
35
29
  }
36
30
 
37
31
  function getMinHistoryRange(p) {
@@ -491,27 +485,27 @@ function compareBufferItem(a, b) {
491
485
  }
492
486
  }
493
487
 
494
- function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
488
+ function appendOnBlockItems(mutItems, onBlockRegistrations, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
495
489
  let newItemsCounter = 0;
496
490
  let latestOnBlockBlockNumber = fromBlock;
497
491
  while (latestOnBlockBlockNumber < maxBlockNumber && newItemsCounter <= maxOnBlockBufferSize) {
498
492
  let blockNumber = latestOnBlockBlockNumber + 1 | 0;
499
493
  latestOnBlockBlockNumber = blockNumber;
500
- for (let configIdx = 0, configIdx_finish = onBlockConfigs.length; configIdx < configIdx_finish; ++configIdx) {
501
- let onBlockConfig = onBlockConfigs[configIdx];
502
- let startBlock = onBlockConfig.startBlock;
494
+ for (let configIdx = 0, configIdx_finish = onBlockRegistrations.length; configIdx < configIdx_finish; ++configIdx) {
495
+ let onBlockRegistration = onBlockRegistrations[configIdx];
496
+ let startBlock = onBlockRegistration.startBlock;
503
497
  let handlerStartBlock = startBlock !== undefined ? startBlock : indexerStartBlock;
504
498
  let tmp = false;
505
499
  if (blockNumber >= handlerStartBlock) {
506
- let endBlock = onBlockConfig.endBlock;
500
+ let endBlock = onBlockRegistration.endBlock;
507
501
  tmp = endBlock !== undefined ? blockNumber <= endBlock : true;
508
502
  }
509
- if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockConfig.interval) === 0) {
503
+ if (tmp && Primitive_int.mod_(blockNumber - handlerStartBlock | 0, onBlockRegistration.interval) === 0) {
510
504
  mutItems.push({
511
505
  kind: 1,
512
- onBlockConfig: onBlockConfig,
506
+ onBlockRegistration: onBlockRegistration,
513
507
  blockNumber: blockNumber,
514
- logIndex: 16777216 + onBlockConfig.index | 0
508
+ logIndex: 16777216 + onBlockRegistration.index | 0
515
509
  });
516
510
  newItemsCounter = newItemsCounter + 1 | 0;
517
511
  }
@@ -525,9 +519,9 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
525
519
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
526
520
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
527
521
  let mutItemsRef = mutItems;
528
- let onBlockConfigs = fetchState.onBlockConfigs;
522
+ let onBlockRegistrations = fetchState.onBlockRegistrations;
529
523
  let latestOnBlockBlockNumber;
530
- if (onBlockConfigs.length !== 0) {
524
+ if (onBlockRegistrations.length !== 0) {
531
525
  let mutItems$1 = mutItemsRef;
532
526
  let item = (
533
527
  mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
@@ -543,7 +537,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
543
537
  let mutItems$2 = mutItemsRef;
544
538
  let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
545
539
  mutItemsRef = mutItems$3;
546
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockConfigs, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
540
+ latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockRegistrations, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
547
541
  } else {
548
542
  latestOnBlockBlockNumber = knownHeight;
549
543
  }
@@ -555,7 +549,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
555
549
  let updatedFetchState_chainId = fetchState.chainId;
556
550
  let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
557
551
  let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
558
- let updatedFetchState_onBlockConfigs = fetchState.onBlockConfigs;
552
+ let updatedFetchState_onBlockRegistrations = fetchState.onBlockRegistrations;
559
553
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
560
554
  let updatedFetchState = {
561
555
  optimizedPartitions: optimizedPartitions,
@@ -568,7 +562,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOp
568
562
  blockLag: blockLag,
569
563
  buffer: updatedFetchState_buffer,
570
564
  maxOnBlockBufferSize: updatedFetchState_maxOnBlockBufferSize,
571
- onBlockConfigs: updatedFetchState_onBlockConfigs,
565
+ onBlockRegistrations: updatedFetchState_onBlockRegistrations,
572
566
  knownHeight: knownHeight,
573
567
  firstEventBlock: updatedFetchState_firstEventBlock
574
568
  };
@@ -757,7 +751,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
757
751
  }
758
752
 
759
753
  function registerDynamicContracts(fetchState, indexingAddresses, items) {
760
- if (Utils.$$Array.isEmpty(fetchState.normalSelection.eventConfigs)) {
754
+ if (Utils.$$Array.isEmpty(fetchState.normalSelection.onEventRegistrations)) {
761
755
  Stdlib_JsError.throwWithMessage("Invalid configuration. No events to fetch for the dynamic contract registration.");
762
756
  }
763
757
  let registeringContractsByContract = {};
@@ -943,7 +937,7 @@ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBl
943
937
  if (item.kind !== 0) {
944
938
  return true;
945
939
  }
946
- let filter = item.eventConfig.clientAddressFilter;
940
+ let filter = item.onEventRegistration.clientAddressFilter;
947
941
  if (filter !== undefined) {
948
942
  return filter(item.payload, item.blockNumber, IndexingAddresses.rawForFilter(indexingAddresses));
949
943
  } else {
@@ -964,7 +958,7 @@ function startFetchingQueries(param, queries) {
964
958
  fromBlock: q.fromBlock,
965
959
  toBlock: q.toBlock,
966
960
  isChunk: q.isChunk,
967
- estResponseSize: q.estResponseSize,
961
+ itemsTarget: q.itemsTarget,
968
962
  fetchedBlock: undefined
969
963
  };
970
964
  let inserted = false;
@@ -982,64 +976,68 @@ function startFetchingQueries(param, queries) {
982
976
  }
983
977
  }
984
978
 
985
- function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock, knownHeight, maybeChunkRange, maxChunks, partition, selection, addressesByContractName, partitionsCount) {
986
- if (!(rangeFromBlock <= knownHeight && maxChunks > 0)) {
987
- return;
988
- }
989
- if (rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock) {
990
- return;
991
- }
992
- if (maybeChunkRange !== undefined) {
993
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : knownHeight;
994
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
995
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
996
- let chunkFromBlock = rangeFromBlock;
997
- let chunkIdx = 0;
998
- while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
999
- let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
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;
985
+ }
986
+ if (exit === 1) {
987
+ 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) {
991
+ let chunkFromBlock = rangeFromBlock;
992
+ let chunkIdx = 0;
993
+ while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
994
+ 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;
1006
+ chunkFromBlock = chunkToBlock + 1 | 0;
1007
+ chunkIdx = chunkIdx + 1 | 0;
1008
+ };
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;
1021
+ }
1022
+ } else {
1023
+ let itemsTarget$2 = densityItemsTarget(partition, rangeFromBlock, rangeEndBlock, chainTargetBlock);
1000
1024
  queries.push({
1001
1025
  partitionId: partitionId,
1002
- fromBlock: chunkFromBlock,
1003
- toBlock: chunkToBlock,
1004
- isChunk: true,
1005
- estResponseSize: calculateEstResponseSize(partition, chunkFromBlock, chunkToBlock, knownHeight, partitionsCount),
1006
- chainId: 0,
1007
- progress: 0,
1026
+ fromBlock: rangeFromBlock,
1027
+ toBlock: rangeEndBlock,
1028
+ isChunk: false,
1029
+ itemsTarget: itemsTarget$2,
1008
1030
  selection: selection,
1009
1031
  addressesByContractName: addressesByContractName
1010
1032
  });
1011
- chunkFromBlock = chunkToBlock + 1 | 0;
1012
- chunkIdx = chunkIdx + 1 | 0;
1013
- };
1014
- return;
1033
+ cost = cost + itemsTarget$2;
1034
+ }
1015
1035
  }
1016
- queries.push({
1017
- partitionId: partitionId,
1018
- fromBlock: rangeFromBlock,
1019
- toBlock: rangeEndBlock,
1020
- isChunk: rangeEndBlock !== undefined,
1021
- estResponseSize: calculateEstResponseSize(partition, rangeFromBlock, rangeEndBlock, knownHeight, partitionsCount),
1022
- chainId: 0,
1023
- progress: 0,
1024
- selection: selection,
1025
- addressesByContractName: addressesByContractName
1026
- });
1027
- return;
1028
1036
  }
1029
- queries.push({
1030
- partitionId: partitionId,
1031
- fromBlock: rangeFromBlock,
1032
- toBlock: rangeEndBlock,
1033
- isChunk: false,
1034
- estResponseSize: calculateEstResponseSize(partition, rangeFromBlock, rangeEndBlock, knownHeight, partitionsCount),
1035
- chainId: 0,
1036
- progress: 0,
1037
- selection: selection,
1038
- addressesByContractName: addressesByContractName
1039
- });
1037
+ return cost;
1040
1038
  }
1041
1039
 
1042
- function getNextQuery(param) {
1040
+ function getNextQuery(param, chainTargetBlock, chainTargetItems) {
1043
1041
  let knownHeight = param.knownHeight;
1044
1042
  let blockLag = param.blockLag;
1045
1043
  let endBlock = param.endBlock;
@@ -1052,90 +1050,55 @@ function getNextQuery(param) {
1052
1050
  let shouldWaitForNewBlock = (
1053
1051
  endBlock !== undefined ? headBlockNumber < endBlock : true
1054
1052
  ) && !isOnBlockBehindTheHead;
1055
- let queries = [];
1056
1053
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
1057
- let idxRef = 0;
1058
- while (idxRef < partitionsCount) {
1059
- let idx = idxRef;
1054
+ for (let idx = 0; idx < partitionsCount; ++idx) {
1060
1055
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1061
1056
  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) {
1057
+ if (p.mutPendingQueries.length !== 0 || p.latestFetchedBlock.blockNumber < headBlockNumber) {
1066
1058
  shouldWaitForNewBlock = false;
1067
1059
  }
1068
- let partitionQueriesStart = queries.length;
1060
+ }
1061
+ let queriesByPartitionIndex = Stdlib_Array.fromInitializer(partitionsCount, param => []);
1062
+ let computeQueryEndBlock = p => {
1069
1063
  let queryEndBlock = Utils.$$Math.minOptInt(endBlock, p.mergeBlock);
1070
- let queryEndBlock$1 = blockLag !== 0 ? Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock) : queryEndBlock;
1071
- let maybeChunkRange = getMinHistoryRange(p);
1072
- let cursor = p.latestFetchedBlock.blockNumber + 1 | 0;
1064
+ if (blockLag !== 0) {
1065
+ return Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock);
1066
+ } else {
1067
+ return queryEndBlock;
1068
+ }
1069
+ };
1070
+ let existingReservedByPartition = {};
1071
+ let chainReserved = 0;
1072
+ for (let idx$1 = 0; idx$1 < partitionsCount; ++idx$1) {
1073
+ let partitionId$1 = optimizedPartitions.idsInAscOrder[idx$1];
1074
+ 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;
1078
+ }
1079
+ existingReservedByPartition[partitionId$1] = cost;
1080
+ chainReserved = chainReserved + cost;
1081
+ }
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;
1073
1092
  let canContinue = true;
1074
- let pqIdx = 0;
1075
- while (pqIdx < p.mutPendingQueries.length && canContinue) {
1076
- let pq = p.mutPendingQueries[pqIdx];
1093
+ let chunksUsedThisCall = 0;
1094
+ let pqIdx$1 = 0;
1095
+ while (pqIdx$1 < pendingCount && canContinue) {
1096
+ let pq = p$2.mutPendingQueries[pqIdx$1];
1077
1097
  if (pq.fromBlock > cursor) {
1078
- let addressesByContractName = p.addressesByContractName;
1079
- let selection = p.selection;
1080
- let maxChunks = (10 - pendingCount | 0) - (queries.length - partitionQueriesStart | 0) | 0;
1081
- let rangeEndBlock = Utils.$$Math.minOptInt(pq.fromBlock - 1 | 0, queryEndBlock$1);
1082
- let rangeFromBlock = cursor;
1083
- if (rangeFromBlock <= knownHeight && maxChunks > 0) {
1084
- let exit = 0;
1085
- if (!(rangeEndBlock !== undefined && rangeFromBlock > rangeEndBlock)) {
1086
- exit = 1;
1087
- }
1088
- if (exit === 1) {
1089
- if (maybeChunkRange !== undefined) {
1090
- let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : knownHeight;
1091
- let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1092
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1093
- let chunkFromBlock = rangeFromBlock;
1094
- let chunkIdx = 0;
1095
- while (chunkIdx < maxChunks && ((chunkFromBlock + chunkSize | 0) - 1 | 0) <= maxBlock) {
1096
- let chunkToBlock = (chunkFromBlock + chunkSize | 0) - 1 | 0;
1097
- queries.push({
1098
- partitionId: partitionId,
1099
- fromBlock: chunkFromBlock,
1100
- toBlock: chunkToBlock,
1101
- isChunk: true,
1102
- estResponseSize: calculateEstResponseSize(p, chunkFromBlock, chunkToBlock, knownHeight, partitionsCount),
1103
- chainId: 0,
1104
- progress: 0,
1105
- selection: selection,
1106
- addressesByContractName: addressesByContractName
1107
- });
1108
- chunkFromBlock = chunkToBlock + 1 | 0;
1109
- chunkIdx = chunkIdx + 1 | 0;
1110
- };
1111
- } else {
1112
- queries.push({
1113
- partitionId: partitionId,
1114
- fromBlock: rangeFromBlock,
1115
- toBlock: rangeEndBlock,
1116
- isChunk: rangeEndBlock !== undefined,
1117
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock, rangeEndBlock, knownHeight, partitionsCount),
1118
- chainId: 0,
1119
- progress: 0,
1120
- selection: selection,
1121
- addressesByContractName: addressesByContractName
1122
- });
1123
- }
1124
- } else {
1125
- queries.push({
1126
- partitionId: partitionId,
1127
- fromBlock: rangeFromBlock,
1128
- toBlock: rangeEndBlock,
1129
- isChunk: false,
1130
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock, rangeEndBlock, knownHeight, partitionsCount),
1131
- chainId: 0,
1132
- progress: 0,
1133
- selection: selection,
1134
- addressesByContractName: addressesByContractName
1135
- });
1136
- }
1137
- }
1138
- }
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;
1139
1102
  }
1140
1103
  let toBlock = pq.toBlock;
1141
1104
  if (toBlock !== undefined && pq.isChunk) {
@@ -1149,72 +1112,122 @@ function getNextQuery(param) {
1149
1112
  } else {
1150
1113
  canContinue = false;
1151
1114
  }
1152
- pqIdx = pqIdx + 1 | 0;
1115
+ pqIdx$1 = pqIdx$1 + 1 | 0;
1153
1116
  };
1154
1117
  if (canContinue) {
1155
- let addressesByContractName$1 = p.addressesByContractName;
1156
- let selection$1 = p.selection;
1157
- let maxChunks$1 = (10 - pendingCount | 0) - (queries.length - partitionQueriesStart | 0) | 0;
1158
- let rangeFromBlock$1 = cursor;
1159
- if (rangeFromBlock$1 <= knownHeight && maxChunks$1 > 0) {
1160
- let exit$1 = 0;
1161
- if (!(queryEndBlock$1 !== undefined && rangeFromBlock$1 > queryEndBlock$1)) {
1162
- exit$1 = 1;
1163
- }
1164
- if (exit$1 === 1) {
1165
- if (maybeChunkRange !== undefined) {
1166
- let maxBlock$1 = queryEndBlock$1 !== undefined ? queryEndBlock$1 : knownHeight;
1167
- let chunkSize$1 = Js_math.ceil_int(maybeChunkRange * 1.8);
1168
- if (((rangeFromBlock$1 + (chunkSize$1 << 1) | 0) - 1 | 0) <= maxBlock$1) {
1169
- let chunkFromBlock$1 = rangeFromBlock$1;
1170
- let chunkIdx$1 = 0;
1171
- while (chunkIdx$1 < maxChunks$1 && ((chunkFromBlock$1 + chunkSize$1 | 0) - 1 | 0) <= maxBlock$1) {
1172
- let chunkToBlock$1 = (chunkFromBlock$1 + chunkSize$1 | 0) - 1 | 0;
1173
- queries.push({
1174
- partitionId: partitionId,
1175
- fromBlock: chunkFromBlock$1,
1176
- toBlock: chunkToBlock$1,
1177
- isChunk: true,
1178
- estResponseSize: calculateEstResponseSize(p, chunkFromBlock$1, chunkToBlock$1, knownHeight, partitionsCount),
1179
- chainId: 0,
1180
- progress: 0,
1181
- selection: selection$1,
1182
- addressesByContractName: addressesByContractName$1
1183
- });
1184
- chunkFromBlock$1 = chunkToBlock$1 + 1 | 0;
1185
- chunkIdx$1 = chunkIdx$1 + 1 | 0;
1186
- };
1187
- } else {
1188
- queries.push({
1189
- partitionId: partitionId,
1190
- fromBlock: rangeFromBlock$1,
1191
- toBlock: queryEndBlock$1,
1192
- isChunk: queryEndBlock$1 !== undefined,
1193
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock$1, queryEndBlock$1, knownHeight, partitionsCount),
1194
- chainId: 0,
1195
- progress: 0,
1196
- selection: selection$1,
1197
- addressesByContractName: addressesByContractName$1
1198
- });
1199
- }
1200
- } else {
1201
- queries.push({
1202
- partitionId: partitionId,
1203
- fromBlock: rangeFromBlock$1,
1204
- toBlock: queryEndBlock$1,
1205
- isChunk: false,
1206
- estResponseSize: calculateEstResponseSize(p, rangeFromBlock$1, queryEndBlock$1, knownHeight, partitionsCount),
1207
- chainId: 0,
1208
- progress: 0,
1209
- selection: selection$1,
1210
- addressesByContractName: addressesByContractName$1
1211
- });
1212
- }
1213
- }
1214
- }
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
+ });
1215
1128
  }
1216
- idxRef = idxRef + 1 | 0;
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;
1217
1187
  };
1188
+ let isInRange = fs => {
1189
+ let tmp = false;
1190
+ if (fs.cursor <= chainTargetBlock) {
1191
+ let eb = fs.queryEndBlock;
1192
+ tmp = eb !== undefined ? fs.cursor <= eb : true;
1193
+ }
1194
+ if (tmp) {
1195
+ return (fs.pendingCount + fs.chunksUsedThisCall | 0) < 10;
1196
+ } else {
1197
+ return false;
1198
+ }
1199
+ };
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
+ };
1230
+ let queries = queriesByPartitionIndex.flat();
1218
1231
  if (Utils.$$Array.isEmpty(queries)) {
1219
1232
  if (shouldWaitForNewBlock) {
1220
1233
  return "WaitingForNewBlock";
@@ -1261,9 +1274,9 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1261
1274
  return acc;
1262
1275
  }
1263
1276
 
1264
- function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1277
+ function make$1(startBlock, endBlock, onEventRegistrations, contractConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockRegistrationsOpt, blockLagOpt, firstEventBlockOpt) {
1265
1278
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1266
- let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1279
+ let onBlockRegistrations = onBlockRegistrationsOpt !== undefined ? onBlockRegistrationsOpt : [];
1267
1280
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
1268
1281
  let firstEventBlock = firstEventBlockOpt !== undefined ? Primitive_option.valFromOption(firstEventBlockOpt) : undefined;
1269
1282
  let latestFetchedBlock = {
@@ -1271,14 +1284,14 @@ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses,
1271
1284
  blockTimestamp: 0
1272
1285
  };
1273
1286
  let notDependingOnAddresses = [];
1274
- let normalEventConfigs = [];
1287
+ let normalRegistrations = [];
1275
1288
  let contractNamesWithNormalEvents = new Set();
1276
- eventConfigs.forEach(ec => {
1277
- if (ec.dependsOnAddresses) {
1278
- normalEventConfigs.push(ec);
1279
- contractNamesWithNormalEvents.add(ec.contractName);
1289
+ onEventRegistrations.forEach(reg => {
1290
+ if (reg.dependsOnAddresses) {
1291
+ normalRegistrations.push(reg);
1292
+ contractNamesWithNormalEvents.add(reg.eventConfig.contractName);
1280
1293
  } else {
1281
- notDependingOnAddresses.push(ec);
1294
+ notDependingOnAddresses.push(reg);
1282
1295
  }
1283
1296
  });
1284
1297
  let partitions = [];
@@ -1287,7 +1300,7 @@ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses,
1287
1300
  id: partitions.length.toString(),
1288
1301
  latestFetchedBlock: latestFetchedBlock,
1289
1302
  selection: {
1290
- eventConfigs: notDependingOnAddresses,
1303
+ onEventRegistrations: notDependingOnAddresses,
1291
1304
  dependsOnAddresses: false
1292
1305
  },
1293
1306
  addressesByContractName: {},
@@ -1301,7 +1314,7 @@ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses,
1301
1314
  });
1302
1315
  }
1303
1316
  let normalSelection = {
1304
- eventConfigs: normalEventConfigs,
1317
+ onEventRegistrations: normalRegistrations,
1305
1318
  dependsOnAddresses: true
1306
1319
  };
1307
1320
  let registeringContractsByContract = {};
@@ -1319,16 +1332,16 @@ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses,
1319
1332
  }
1320
1333
  });
1321
1334
  let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
1322
- if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockConfigs)) {
1323
- 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.`);
1335
+ if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockRegistrations)) {
1336
+ 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.`);
1324
1337
  }
1325
1338
  let buffer = [];
1326
1339
  let latestOnBlockBlockNumber;
1327
- if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockConfigs)) {
1340
+ if (knownHeight > 0 && Utils.$$Array.notEmpty(onBlockRegistrations)) {
1328
1341
  let id = optimizedPartitions.idsInAscOrder[0];
1329
1342
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
1330
1343
  let maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
1331
- latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1344
+ latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockRegistrations, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1332
1345
  } else {
1333
1346
  latestOnBlockBlockNumber = progressBlockNumber;
1334
1347
  }
@@ -1343,7 +1356,7 @@ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses,
1343
1356
  blockLag: blockLag,
1344
1357
  buffer: buffer,
1345
1358
  maxOnBlockBufferSize: maxOnBlockBufferSize,
1346
- onBlockConfigs: onBlockConfigs,
1359
+ onBlockRegistrations: onBlockRegistrations,
1347
1360
  knownHeight: knownHeight,
1348
1361
  firstEventBlock: firstEventBlock
1349
1362
  };
@@ -1372,7 +1385,7 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1372
1385
  fromBlock: pq.fromBlock,
1373
1386
  toBlock: pq.toBlock,
1374
1387
  isChunk: pq.isChunk,
1375
- estResponseSize: pq.estResponseSize,
1388
+ itemsTarget: pq.itemsTarget,
1376
1389
  fetchedBlock: {
1377
1390
  blockNumber: targetBlockNumber,
1378
1391
  blockTimestamp: 0
@@ -1471,7 +1484,7 @@ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1471
1484
  blockLag: fetchState.blockLag,
1472
1485
  buffer: fetchState.buffer,
1473
1486
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1474
- onBlockConfigs: fetchState.onBlockConfigs,
1487
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1475
1488
  knownHeight: fetchState.knownHeight,
1476
1489
  firstEventBlock: fetchState.firstEventBlock
1477
1490
  }, optimizedPartitions, fetchState.buffer.filter(item => {
@@ -1521,7 +1534,7 @@ function resetPendingQueries(fetchState) {
1521
1534
  blockLag: fetchState.blockLag,
1522
1535
  buffer: fetchState.buffer,
1523
1536
  maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1524
- onBlockConfigs: fetchState.onBlockConfigs,
1537
+ onBlockRegistrations: fetchState.onBlockRegistrations,
1525
1538
  knownHeight: fetchState.knownHeight,
1526
1539
  firstEventBlock: fetchState.firstEventBlock
1527
1540
  };
@@ -1584,19 +1597,6 @@ function getProgressPercentage(fetchState) {
1584
1597
  return progress / totalRange;
1585
1598
  }
1586
1599
 
1587
- function getProgressPercentageAt(fetchState, blockNumber) {
1588
- let firstEventBlock = fetchState.firstEventBlock;
1589
- if (firstEventBlock === undefined) {
1590
- return 0;
1591
- }
1592
- let totalRange = fetchState.knownHeight - firstEventBlock | 0;
1593
- if (totalRange <= 0) {
1594
- return 0;
1595
- } else {
1596
- return (blockNumber - firstEventBlock | 0) / totalRange;
1597
- }
1598
- }
1599
-
1600
1600
  function hasFullBatch(fetchState, batchSizeTarget) {
1601
1601
  let item = fetchState.buffer[batchSizeTarget - 1 | 0];
1602
1602
  if (item !== undefined) {
@@ -1650,9 +1650,9 @@ function updateKnownHeight(fetchState, knownHeight) {
1650
1650
  }
1651
1651
  }
1652
1652
 
1653
- let minEstResponseSize = 2000;
1653
+ let minItemsTarget = 2000;
1654
1654
 
1655
- let maxEstResponseSize = 10000;
1655
+ let maxItemsTarget = 10000;
1656
1656
 
1657
1657
  let blockItemLogIndex = 16777216;
1658
1658
 
@@ -1660,10 +1660,9 @@ let maxPendingChunksPerPartition = 10;
1660
1660
 
1661
1661
  export {
1662
1662
  deriveContractNameByAddress,
1663
- minEstResponseSize,
1664
- maxEstResponseSize,
1665
- calculateDefaultEstResponseSize,
1666
- calculateEstResponseSize,
1663
+ minItemsTarget,
1664
+ maxItemsTarget,
1665
+ densityItemsTarget,
1667
1666
  getMinHistoryRange,
1668
1667
  getMinQueryRange,
1669
1668
  OptimizedPartitions,
@@ -1682,7 +1681,7 @@ export {
1682
1681
  handleQueryResult,
1683
1682
  startFetchingQueries,
1684
1683
  maxPendingChunksPerPartition,
1685
- pushQueriesForRange,
1684
+ pushGapFillQueries,
1686
1685
  getNextQuery,
1687
1686
  hasReadyItem,
1688
1687
  getReadyItemsCount,
@@ -1695,7 +1694,6 @@ export {
1695
1694
  isFetchingAtHead,
1696
1695
  isReadyToEnterReorgThreshold,
1697
1696
  getProgressPercentage,
1698
- getProgressPercentageAt,
1699
1697
  sortForBatch,
1700
1698
  getProgressBlockNumberAt,
1701
1699
  updateKnownHeight,