envio 3.2.0 → 3.3.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/package.json +6 -7
  2. package/src/Batch.res +12 -15
  3. package/src/Batch.res.mjs +4 -5
  4. package/src/BatchProcessing.res +199 -0
  5. package/src/BatchProcessing.res.mjs +125 -0
  6. package/src/ChainFetching.res +375 -0
  7. package/src/ChainFetching.res.mjs +206 -0
  8. package/src/ChainMetadata.res +27 -0
  9. package/src/ChainMetadata.res.mjs +27 -0
  10. package/src/ChainState.res +618 -0
  11. package/src/ChainState.res.mjs +461 -0
  12. package/src/ChainState.resi +85 -0
  13. package/src/Config.res +13 -4
  14. package/src/Config.res.mjs +8 -3
  15. package/src/ContractRegisterContext.res +106 -0
  16. package/src/ContractRegisterContext.res.mjs +76 -0
  17. package/src/Core.res +3 -0
  18. package/src/CrossChainState.res +269 -0
  19. package/src/CrossChainState.res.mjs +197 -0
  20. package/src/CrossChainState.resi +47 -0
  21. package/src/Ecosystem.res +58 -0
  22. package/src/Ecosystem.res.mjs +43 -1
  23. package/src/Env.res +7 -2
  24. package/src/Env.res.mjs +5 -2
  25. package/src/EventConfigBuilder.res +35 -0
  26. package/src/EventConfigBuilder.res.mjs +21 -0
  27. package/src/EventProcessing.res +60 -46
  28. package/src/EventProcessing.res.mjs +33 -32
  29. package/src/EventUtils.res +0 -4
  30. package/src/EventUtils.res.mjs +0 -4
  31. package/src/ExitOnCaughtUp.res +10 -0
  32. package/src/ExitOnCaughtUp.res.mjs +22 -0
  33. package/src/FetchState.res +162 -113
  34. package/src/FetchState.res.mjs +181 -119
  35. package/src/HandlerLoader.res +6 -1
  36. package/src/HandlerLoader.res.mjs +13 -36
  37. package/src/InMemoryStore.res +36 -451
  38. package/src/InMemoryStore.res.mjs +20 -369
  39. package/src/IndexerLoop.res +44 -0
  40. package/src/IndexerLoop.res.mjs +46 -0
  41. package/src/IndexerState.res +544 -0
  42. package/src/IndexerState.res.mjs +547 -0
  43. package/src/IndexerState.resi +132 -0
  44. package/src/Internal.res +44 -7
  45. package/src/LoadLayer.res +20 -18
  46. package/src/LoadLayer.res.mjs +14 -12
  47. package/src/LoadLayer.resi +6 -3
  48. package/src/LogSelection.res +86 -17
  49. package/src/LogSelection.res.mjs +67 -14
  50. package/src/Logging.res +11 -44
  51. package/src/Logging.res.mjs +10 -42
  52. package/src/Main.res +54 -79
  53. package/src/Main.res.mjs +44 -56
  54. package/src/PgStorage.res +8 -71
  55. package/src/PgStorage.res.mjs +3 -47
  56. package/src/Prometheus.res +7 -8
  57. package/src/Prometheus.res.mjs +7 -4
  58. package/src/PruneStaleHistory.res +45 -0
  59. package/src/PruneStaleHistory.res.mjs +46 -0
  60. package/src/RawEvent.res +73 -0
  61. package/src/RawEvent.res.mjs +51 -0
  62. package/src/Rollback.res +204 -0
  63. package/src/Rollback.res.mjs +118 -0
  64. package/src/SimulateItems.res +12 -10
  65. package/src/SimulateItems.res.mjs +1 -1
  66. package/src/TestIndexer.res +34 -27
  67. package/src/TestIndexer.res.mjs +4 -1
  68. package/src/UserContext.res +26 -114
  69. package/src/UserContext.res.mjs +15 -78
  70. package/src/Writing.res +242 -0
  71. package/src/Writing.res.mjs +215 -0
  72. package/src/db/InternalTable.res +14 -27
  73. package/src/sources/Evm.res +40 -1
  74. package/src/sources/Evm.res.mjs +94 -84
  75. package/src/sources/Fuel.res +40 -1
  76. package/src/sources/Fuel.res.mjs +36 -26
  77. package/src/sources/HyperFuel.res +41 -120
  78. package/src/sources/HyperFuel.res.mjs +42 -80
  79. package/src/sources/HyperFuel.resi +1 -24
  80. package/src/sources/HyperFuelClient.res +16 -297
  81. package/src/sources/HyperFuelClient.res.mjs +5 -4
  82. package/src/sources/HyperFuelSource.res +33 -8
  83. package/src/sources/HyperFuelSource.res.mjs +56 -10
  84. package/src/sources/HyperSyncSource.res +7 -5
  85. package/src/sources/HyperSyncSource.res.mjs +3 -3
  86. package/src/sources/RpcSource.res +2 -2
  87. package/src/sources/RpcSource.res.mjs +1 -1
  88. package/src/sources/SourceManager.res +8 -18
  89. package/src/sources/SourceManager.res.mjs +8 -5
  90. package/src/sources/SourceManager.resi +4 -1
  91. package/src/sources/Svm.res +18 -1
  92. package/src/sources/Svm.res.mjs +30 -22
  93. package/src/sources/SvmHyperSyncSource.res +1 -1
  94. package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
  95. package/src/tui/Tui.res +38 -36
  96. package/src/tui/Tui.res.mjs +51 -51
  97. package/src/ChainFetcher.res +0 -519
  98. package/src/ChainFetcher.res.mjs +0 -314
  99. package/src/ChainManager.res +0 -358
  100. package/src/ChainManager.res.mjs +0 -291
  101. package/src/Ctx.res +0 -6
  102. package/src/Ctx.res.mjs +0 -2
  103. package/src/GlobalState.res +0 -1056
  104. package/src/GlobalState.res.mjs +0 -1086
  105. package/src/GlobalStateManager.res +0 -57
  106. package/src/GlobalStateManager.res.mjs +0 -68
  107. package/src/GlobalStateManager.resi +0 -7
  108. package/src/Ports.res +0 -5
  109. package/src/Ports.res.mjs +0 -9
  110. package/src/adapters/MarkBatchProcessedAdapter.res +0 -5
  111. package/src/adapters/MarkBatchProcessedAdapter.res.mjs +0 -14
  112. package/src/sources/EnvioApiClient.res +0 -15
  113. package/src/sources/EnvioApiClient.res.mjs +0 -24
@@ -449,10 +449,10 @@ function numAddresses(fetchState) {
449
449
  return Utils.Dict.size(fetchState.indexingAddresses);
450
450
  }
451
451
 
452
- function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, targetBufferSize) {
452
+ function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
453
453
  let newItemsCounter = 0;
454
454
  let latestOnBlockBlockNumber = fromBlock;
455
- while (latestOnBlockBlockNumber < maxBlockNumber && newItemsCounter <= targetBufferSize) {
455
+ while (latestOnBlockBlockNumber < maxBlockNumber && newItemsCounter <= maxOnBlockBufferSize) {
456
456
  let blockNumber = latestOnBlockBlockNumber + 1 | 0;
457
457
  latestOnBlockBlockNumber = blockNumber;
458
458
  for (let configIdx = 0, configIdx_finish = onBlockConfigs.length; configIdx < configIdx_finish; ++configIdx) {
@@ -490,7 +490,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
490
490
  let mutItems$1 = mutItemsRef;
491
491
  let item = (
492
492
  mutItems$1 !== undefined ? mutItems$1 : fetchState.buffer
493
- )[fetchState.targetBufferSize - 1 | 0];
493
+ )[fetchState.maxOnBlockBufferSize - 1 | 0];
494
494
  let maxBlockNumber;
495
495
  if (item !== undefined) {
496
496
  maxBlockNumber = item.blockNumber;
@@ -502,7 +502,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
502
502
  let mutItems$2 = mutItemsRef;
503
503
  let mutItems$3 = mutItems$2 !== undefined ? mutItems$2 : fetchState.buffer.slice();
504
504
  mutItemsRef = mutItems$3;
505
- latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockConfigs, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.targetBufferSize);
505
+ latestOnBlockBlockNumber = appendOnBlockItems(mutItems$3, onBlockConfigs, fetchState.startBlock, fetchState.latestOnBlockBlockNumber, maxBlockNumber, fetchState.maxOnBlockBufferSize);
506
506
  } else {
507
507
  latestOnBlockBlockNumber = knownHeight;
508
508
  }
@@ -513,7 +513,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
513
513
  let updatedFetchState_contractConfigs = fetchState.contractConfigs;
514
514
  let updatedFetchState_chainId = fetchState.chainId;
515
515
  let updatedFetchState_buffer = mutItems$4 !== undefined ? (mutItems$4.sort(compareBufferItem), mutItems$4) : fetchState.buffer;
516
- let updatedFetchState_targetBufferSize = fetchState.targetBufferSize;
516
+ let updatedFetchState_maxOnBlockBufferSize = fetchState.maxOnBlockBufferSize;
517
517
  let updatedFetchState_onBlockConfigs = fetchState.onBlockConfigs;
518
518
  let updatedFetchState_firstEventBlock = fetchState.firstEventBlock;
519
519
  let updatedFetchState = {
@@ -527,7 +527,7 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
527
527
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
528
528
  blockLag: blockLag,
529
529
  buffer: updatedFetchState_buffer,
530
- targetBufferSize: updatedFetchState_targetBufferSize,
530
+ maxOnBlockBufferSize: updatedFetchState_maxOnBlockBufferSize,
531
531
  onBlockConfigs: updatedFetchState_onBlockConfigs,
532
532
  knownHeight: knownHeight,
533
533
  firstEventBlock: updatedFetchState_firstEventBlock
@@ -571,7 +571,7 @@ function addressesByContractNameGetAll(addressesByContractName) {
571
571
  return all;
572
572
  }
573
573
 
574
- function createPartitionsFromIndexingAddresses(registeringContractsByContract, contractConfigs, dynamicContracts, normalSelection, maxAddrInPartition, nextPartitionIndex, existingPartitions, progressBlockNumber) {
574
+ function createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, nextPartitionIndex, existingPartitions, progressBlockNumber) {
575
575
  let nextPartitionIndexRef = nextPartitionIndex;
576
576
  let dynamicPartitions = [];
577
577
  let nonDynamicPartitions = [];
@@ -580,7 +580,6 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, c
580
580
  let contractName = contractNames[cIdx];
581
581
  let registeringContracts = registeringContractsByContract[contractName];
582
582
  let addresses = Object.keys(registeringContracts);
583
- let contractConfig = contractConfigs[contractName];
584
583
  let isDynamic = dynamicContracts.has(contractName);
585
584
  let partitions = isDynamic ? dynamicPartitions : nonDynamicPartitions;
586
585
  let byStartBlock = {};
@@ -596,9 +595,7 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, c
596
595
  for (let idx = 0, idx_finish = ascKeys.length; idx < idx_finish; ++idx) {
597
596
  let maybeNextStartBlockKey = ascKeys[idx + 1 | 0];
598
597
  let shouldAllocateNewPartition;
599
- if (contractConfig.filterByAddresses || maybeNextStartBlockKey === undefined) {
600
- shouldAllocateNewPartition = true;
601
- } else {
598
+ if (maybeNextStartBlockKey !== undefined) {
602
599
  let nextStartBlock = Stdlib_Int.fromString(maybeNextStartBlockKey, undefined);
603
600
  let shouldJoinCurrentStartBlock = (nextStartBlock - startBlockRef | 0) < 20000;
604
601
  if (shouldJoinCurrentStartBlock) {
@@ -607,6 +604,8 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, c
607
604
  } else {
608
605
  shouldAllocateNewPartition = true;
609
606
  }
607
+ } else {
608
+ shouldAllocateNewPartition = true;
610
609
  }
611
610
  if (shouldAllocateNewPartition) {
612
611
  let latestFetchedBlock_blockNumber = Primitive_int.max(startBlockRef - 1 | 0, progressBlockNumber);
@@ -667,10 +666,8 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, c
667
666
  mergedAddresses[cn] = currentAddrs;
668
667
  }
669
668
  }
670
- let nextContractName = Utils.$$Array.firstUnsafe(Object.keys(nextP.addressesByContractName));
671
- let hasFilterByAddresses = contractConfigs[nextContractName].filterByAddresses;
672
669
  let isTooFar = (currentPBlock + 20000 | 0) < nextPBlock;
673
- if (isTooFar || hasFilterByAddresses) {
670
+ if (isTooFar) {
674
671
  mergedNonDynamic.push({
675
672
  id: currentP.id,
676
673
  latestFetchedBlock: currentP.latestFetchedBlock,
@@ -725,7 +722,6 @@ function registerDynamicContracts(fetchState, items) {
725
722
  let indexingAddresses = fetchState.indexingAddresses;
726
723
  let registeringContractsByContract = {};
727
724
  let earliestRegisteringEventBlockNumber = Infinity;
728
- let hasDCWithFilterByAddresses = false;
729
725
  let noEventsAddresses = {};
730
726
  let registeringAddresses = {};
731
727
  for (let itemIdx = 0, itemIdx_finish = items.length; itemIdx < itemIdx_finish; ++itemIdx) {
@@ -764,18 +760,9 @@ function registerDynamicContracts(fetchState, items) {
764
760
  shouldRemove = true;
765
761
  } else {
766
762
  let registeringContract = registeringAddresses[dc.address];
767
- let shouldUpdate;
768
- if (registeringContract !== undefined) {
769
- if (registeringContract.contractName !== dc.contractName) {
770
- warnDifferentContractType(fetchState, registeringContract, dcWithStartBlock);
771
- shouldUpdate = false;
772
- } else {
773
- shouldUpdate = false;
774
- }
775
- } else {
776
- hasDCWithFilterByAddresses = hasDCWithFilterByAddresses || match.filterByAddresses;
777
- shouldUpdate = true;
778
- }
763
+ let shouldUpdate = registeringContract !== undefined ? (
764
+ registeringContract.contractName !== dc.contractName ? (warnDifferentContractType(fetchState, registeringContract, dcWithStartBlock), false) : false
765
+ ) : true;
779
766
  if (shouldUpdate) {
780
767
  earliestRegisteringEventBlockNumber = Primitive_int.min(earliestRegisteringEventBlockNumber, dcWithStartBlock_effectiveStartBlock);
781
768
  Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, dc.contractName)[dc.address] = dcWithStartBlock;
@@ -898,7 +885,7 @@ function registerDynamicContracts(fetchState, items) {
898
885
  Object.assign(newIndexingAddresses, registeringContracts);
899
886
  }
900
887
  Object.assign(newIndexingAddresses, noEventsAddresses);
901
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, fetchState.contractConfigs, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
888
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
902
889
  return updateInternal(fetchState, optimizedPartitions, newIndexingAddresses, undefined, undefined, undefined);
903
890
  }
904
891
  if (!hasNoEventsUpdates) {
@@ -910,7 +897,18 @@ function registerDynamicContracts(fetchState, items) {
910
897
  }
911
898
 
912
899
  function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
913
- return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, latestFetchedBlock), undefined, newItems.length !== 0 ? fetchState.buffer.concat(newItems) : undefined, undefined, undefined);
900
+ let newItems$1 = newItems.filter(item => {
901
+ if (item.kind !== 0) {
902
+ return true;
903
+ }
904
+ let filter = item.eventConfig.clientAddressFilter;
905
+ if (filter !== undefined) {
906
+ return filter(item.payload, item.blockNumber, fetchState.indexingAddresses);
907
+ } else {
908
+ return true;
909
+ }
910
+ });
911
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, latestFetchedBlock), undefined, newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
914
912
  }
915
913
 
916
914
  function startFetchingQueries(param, queries) {
@@ -951,36 +949,42 @@ function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock
951
949
  if (maybeChunkRange !== undefined) {
952
950
  let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
953
951
  let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
954
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
955
- queries.push({
956
- partitionId: partitionId,
957
- fromBlock: rangeFromBlock,
958
- toBlock: (rangeFromBlock + chunkSize | 0) - 1 | 0,
959
- isChunk: true,
960
- selection: selection,
961
- addressesByContractName: addressesByContractName,
962
- indexingAddresses: indexingAddresses
963
- });
964
- queries.push({
965
- partitionId: partitionId,
966
- fromBlock: rangeFromBlock + chunkSize | 0,
967
- toBlock: (rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0,
968
- isChunk: true,
969
- selection: selection,
970
- addressesByContractName: addressesByContractName,
971
- indexingAddresses: indexingAddresses
972
- });
973
- } else {
974
- queries.push({
975
- partitionId: partitionId,
976
- fromBlock: rangeFromBlock,
977
- toBlock: rangeEndBlock,
978
- isChunk: rangeEndBlock !== undefined,
979
- selection: selection,
980
- addressesByContractName: addressesByContractName,
981
- indexingAddresses: indexingAddresses
982
- });
952
+ let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
953
+ let getChunkSize = chunkIdx => {
954
+ if (chunkIdx < 2) {
955
+ return probeSize;
956
+ } else {
957
+ return chunkSize;
958
+ }
959
+ };
960
+ if ((((rangeFromBlock + probeSize | 0) + probeSize | 0) - 1 | 0) <= maxBlock) {
961
+ let chunkFromBlock = rangeFromBlock;
962
+ let chunkIdx = 0;
963
+ while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
964
+ let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
965
+ queries.push({
966
+ partitionId: partitionId,
967
+ fromBlock: chunkFromBlock,
968
+ toBlock: chunkToBlock,
969
+ isChunk: true,
970
+ selection: selection,
971
+ addressesByContractName: addressesByContractName,
972
+ indexingAddresses: indexingAddresses
973
+ });
974
+ chunkFromBlock = chunkToBlock + 1 | 0;
975
+ chunkIdx = chunkIdx + 1 | 0;
976
+ };
977
+ return;
983
978
  }
979
+ queries.push({
980
+ partitionId: partitionId,
981
+ fromBlock: rangeFromBlock,
982
+ toBlock: rangeEndBlock,
983
+ isChunk: rangeEndBlock !== undefined,
984
+ selection: selection,
985
+ addressesByContractName: addressesByContractName,
986
+ indexingAddresses: indexingAddresses
987
+ });
984
988
  return;
985
989
  }
986
990
  queries.push({
@@ -994,7 +998,7 @@ function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock
994
998
  });
995
999
  }
996
1000
 
997
- function getNextQuery(fetchState, concurrencyLimit) {
1001
+ function getNextQuery(fetchState, concurrencyLimit, bufferLimit) {
998
1002
  let knownHeight = fetchState.knownHeight;
999
1003
  let blockLag = fetchState.blockLag;
1000
1004
  let indexingAddresses = fetchState.indexingAddresses;
@@ -1006,12 +1010,15 @@ function getNextQuery(fetchState, concurrencyLimit) {
1006
1010
  if (concurrencyLimit === 0) {
1007
1011
  return "ReachedMaxConcurrency";
1008
1012
  }
1013
+ if (bufferLimit <= 0) {
1014
+ return "NothingToQuery";
1015
+ }
1009
1016
  let isOnBlockBehindTheHead = fetchState.latestOnBlockBlockNumber < headBlockNumber;
1010
1017
  let endBlock = fetchState.endBlock;
1011
1018
  let shouldWaitForNewBlock = (
1012
1019
  endBlock !== undefined ? headBlockNumber < endBlock : true
1013
1020
  ) && !isOnBlockBehindTheHead;
1014
- let item = fetchState.buffer[fetchState.targetBufferSize - 1 | 0];
1021
+ let item = fetchState.buffer[bufferLimit - 1 | 0];
1015
1022
  let maxQueryBlockNumber = item !== undefined ? Primitive_int.min(item.blockNumber, knownHeight) : knownHeight;
1016
1023
  let queries = [];
1017
1024
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
@@ -1020,11 +1027,13 @@ function getNextQuery(fetchState, concurrencyLimit) {
1020
1027
  let idx = idxRef;
1021
1028
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1022
1029
  let p = optimizedPartitions.entities[partitionId];
1030
+ let pendingCount = p.mutPendingQueries.length;
1023
1031
  let isBehindTheHead = p.latestFetchedBlock.blockNumber < headBlockNumber;
1024
- let hasPendingQueries = Utils.$$Array.notEmpty(p.mutPendingQueries);
1032
+ let hasPendingQueries = pendingCount > 0;
1025
1033
  if (hasPendingQueries || isBehindTheHead) {
1026
1034
  shouldWaitForNewBlock = false;
1027
1035
  }
1036
+ let partitionQueriesStart = queries.length;
1028
1037
  let queryEndBlock = Utils.$$Math.minOptInt(fetchState.endBlock, p.mergeBlock);
1029
1038
  let queryEndBlock$1 = blockLag !== 0 ? Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock) : queryEndBlock;
1030
1039
  let match = maxQueryBlockNumber < knownHeight;
@@ -1053,25 +1062,31 @@ function getNextQuery(fetchState, concurrencyLimit) {
1053
1062
  if (maybeChunkRange !== undefined) {
1054
1063
  let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
1055
1064
  let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1056
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1057
- queries.push({
1058
- partitionId: partitionId,
1059
- fromBlock: rangeFromBlock,
1060
- toBlock: (rangeFromBlock + chunkSize | 0) - 1 | 0,
1061
- isChunk: true,
1062
- selection: selection,
1063
- addressesByContractName: addressesByContractName,
1064
- indexingAddresses: indexingAddresses
1065
- });
1066
- queries.push({
1067
- partitionId: partitionId,
1068
- fromBlock: rangeFromBlock + chunkSize | 0,
1069
- toBlock: (rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0,
1070
- isChunk: true,
1071
- selection: selection,
1072
- addressesByContractName: addressesByContractName,
1073
- indexingAddresses: indexingAddresses
1074
- });
1065
+ let probeSize = Js_math.ceil_int(maybeChunkRange * 0.9);
1066
+ let getChunkSize = chunkIdx => {
1067
+ if (chunkIdx < 2) {
1068
+ return probeSize;
1069
+ } else {
1070
+ return chunkSize;
1071
+ }
1072
+ };
1073
+ if ((((rangeFromBlock + getChunkSize(0) | 0) + getChunkSize(1) | 0) - 1 | 0) <= maxBlock) {
1074
+ let chunkFromBlock = rangeFromBlock;
1075
+ let chunkIdx = 0;
1076
+ while (chunkIdx < 5 && ((chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0) <= maxBlock) {
1077
+ let chunkToBlock = (chunkFromBlock + getChunkSize(chunkIdx) | 0) - 1 | 0;
1078
+ queries.push({
1079
+ partitionId: partitionId,
1080
+ fromBlock: chunkFromBlock,
1081
+ toBlock: chunkToBlock,
1082
+ isChunk: true,
1083
+ selection: selection,
1084
+ addressesByContractName: addressesByContractName,
1085
+ indexingAddresses: indexingAddresses
1086
+ });
1087
+ chunkFromBlock = chunkToBlock + 1 | 0;
1088
+ chunkIdx = chunkIdx + 1 | 0;
1089
+ };
1075
1090
  } else {
1076
1091
  queries.push({
1077
1092
  partitionId: partitionId,
@@ -1124,25 +1139,31 @@ function getNextQuery(fetchState, concurrencyLimit) {
1124
1139
  if (maybeChunkRange !== undefined) {
1125
1140
  let maxBlock$1 = queryEndBlock$2 !== undefined ? queryEndBlock$2 : maxQueryBlockNumber;
1126
1141
  let chunkSize$1 = Js_math.ceil_int(maybeChunkRange * 1.8);
1127
- if (((rangeFromBlock$1 + (chunkSize$1 << 1) | 0) - 1 | 0) <= maxBlock$1) {
1128
- queries.push({
1129
- partitionId: partitionId,
1130
- fromBlock: rangeFromBlock$1,
1131
- toBlock: (rangeFromBlock$1 + chunkSize$1 | 0) - 1 | 0,
1132
- isChunk: true,
1133
- selection: selection$1,
1134
- addressesByContractName: addressesByContractName$1,
1135
- indexingAddresses: indexingAddresses
1136
- });
1137
- queries.push({
1138
- partitionId: partitionId,
1139
- fromBlock: rangeFromBlock$1 + chunkSize$1 | 0,
1140
- toBlock: (rangeFromBlock$1 + (chunkSize$1 << 1) | 0) - 1 | 0,
1141
- isChunk: true,
1142
- selection: selection$1,
1143
- addressesByContractName: addressesByContractName$1,
1144
- indexingAddresses: indexingAddresses
1145
- });
1142
+ let probeSize$1 = Js_math.ceil_int(maybeChunkRange * 0.9);
1143
+ let getChunkSize$1 = chunkIdx => {
1144
+ if (chunkIdx < 2) {
1145
+ return probeSize$1;
1146
+ } else {
1147
+ return chunkSize$1;
1148
+ }
1149
+ };
1150
+ if ((((rangeFromBlock$1 + getChunkSize$1(0) | 0) + getChunkSize$1(1) | 0) - 1 | 0) <= maxBlock$1) {
1151
+ let chunkFromBlock$1 = rangeFromBlock$1;
1152
+ let chunkIdx$1 = 0;
1153
+ while (chunkIdx$1 < 5 && ((chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0) <= maxBlock$1) {
1154
+ let chunkToBlock$1 = (chunkFromBlock$1 + getChunkSize$1(chunkIdx$1) | 0) - 1 | 0;
1155
+ queries.push({
1156
+ partitionId: partitionId,
1157
+ fromBlock: chunkFromBlock$1,
1158
+ toBlock: chunkToBlock$1,
1159
+ isChunk: true,
1160
+ selection: selection$1,
1161
+ addressesByContractName: addressesByContractName$1,
1162
+ indexingAddresses: indexingAddresses
1163
+ });
1164
+ chunkFromBlock$1 = chunkToBlock$1 + 1 | 0;
1165
+ chunkIdx$1 = chunkIdx$1 + 1 | 0;
1166
+ };
1146
1167
  } else {
1147
1168
  queries.push({
1148
1169
  partitionId: partitionId,
@@ -1168,6 +1189,11 @@ function getNextQuery(fetchState, concurrencyLimit) {
1168
1189
  }
1169
1190
  }
1170
1191
  }
1192
+ let maxNewChunks = Primitive_int.max(0, 10 - pendingCount | 0);
1193
+ let generatedCount = queries.length - partitionQueriesStart | 0;
1194
+ if (generatedCount > maxNewChunks) {
1195
+ queries.splice(partitionQueriesStart + maxNewChunks | 0, generatedCount - maxNewChunks | 0);
1196
+ }
1171
1197
  idxRef = idxRef + 1 | 0;
1172
1198
  };
1173
1199
  if (Utils.$$Array.isEmpty(queries)) {
@@ -1216,7 +1242,7 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1216
1242
  return acc;
1217
1243
  }
1218
1244
 
1219
- function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartition, chainId, targetBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1245
+ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1220
1246
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1221
1247
  let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1222
1248
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
@@ -1236,7 +1262,6 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1236
1262
  let startBlock = match.startBlock;
1237
1263
  let match$1 = ec.startBlock;
1238
1264
  contractConfigs[ec.contractName] = {
1239
- filterByAddresses: match.filterByAddresses || ec.filterByAddresses,
1240
1265
  startBlock: startBlock !== undefined ? (
1241
1266
  match$1 !== undefined ? Primitive_int.min(startBlock, match$1) : startBlock
1242
1267
  ) : (
@@ -1245,7 +1270,6 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1245
1270
  };
1246
1271
  } else {
1247
1272
  contractConfigs[ec.contractName] = {
1248
- filterByAddresses: ec.filterByAddresses,
1249
1273
  startBlock: ec.startBlock
1250
1274
  };
1251
1275
  }
@@ -1305,7 +1329,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1305
1329
  return;
1306
1330
  }
1307
1331
  });
1308
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, contractConfigs, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
1332
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
1309
1333
  if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockConfigs)) {
1310
1334
  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.`);
1311
1335
  }
@@ -1315,7 +1339,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1315
1339
  let id = optimizedPartitions.idsInAscOrder[0];
1316
1340
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
1317
1341
  let maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
1318
- latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, targetBufferSize);
1342
+ latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1319
1343
  } else {
1320
1344
  latestOnBlockBlockNumber = progressBlockNumber;
1321
1345
  }
@@ -1330,7 +1354,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1330
1354
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
1331
1355
  blockLag: blockLag,
1332
1356
  buffer: buffer,
1333
- targetBufferSize: targetBufferSize,
1357
+ maxOnBlockBufferSize: maxOnBlockBufferSize,
1334
1358
  onBlockConfigs: onBlockConfigs,
1335
1359
  knownHeight: knownHeight,
1336
1360
  firstEventBlock: firstEventBlock
@@ -1350,6 +1374,22 @@ function bufferSize(param) {
1350
1374
  return param.buffer.length;
1351
1375
  }
1352
1376
 
1377
+ function bufferReadyCount(fetchState) {
1378
+ let frontier = bufferBlockNumber(fetchState);
1379
+ let buffer = fetchState.buffer;
1380
+ let lo = 0;
1381
+ let hi = buffer.length;
1382
+ while (lo < hi) {
1383
+ let mid = (lo + hi | 0) / 2 | 0;
1384
+ if (buffer[mid].blockNumber <= frontier) {
1385
+ lo = mid + 1 | 0;
1386
+ } else {
1387
+ hi = mid;
1388
+ }
1389
+ };
1390
+ return lo;
1391
+ }
1392
+
1353
1393
  function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1354
1394
  let adjusted = [];
1355
1395
  for (let qIdx = 0, qIdx_finish = mutPendingQueries.length; qIdx < qIdx_finish; ++qIdx) {
@@ -1453,7 +1493,7 @@ function rollback(fetchState, targetBlockNumber) {
1453
1493
  });
1454
1494
  }
1455
1495
  }
1456
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, fetchState.contractConfigs, fetchState.optimizedPartitions.dynamicContracts, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, nextKeptIdRef, keptPartitions, targetBlockNumber);
1496
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, fetchState.optimizedPartitions.dynamicContracts, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, nextKeptIdRef, keptPartitions, targetBlockNumber);
1457
1497
  return updateInternal({
1458
1498
  optimizedPartitions: fetchState.optimizedPartitions,
1459
1499
  startBlock: fetchState.startBlock,
@@ -1465,7 +1505,7 @@ function rollback(fetchState, targetBlockNumber) {
1465
1505
  latestOnBlockBlockNumber: Primitive_int.min(fetchState.latestOnBlockBlockNumber, targetBlockNumber),
1466
1506
  blockLag: fetchState.blockLag,
1467
1507
  buffer: fetchState.buffer,
1468
- targetBufferSize: fetchState.targetBufferSize,
1508
+ maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1469
1509
  onBlockConfigs: fetchState.onBlockConfigs,
1470
1510
  knownHeight: fetchState.knownHeight,
1471
1511
  firstEventBlock: fetchState.firstEventBlock
@@ -1515,7 +1555,7 @@ function resetPendingQueries(fetchState) {
1515
1555
  latestOnBlockBlockNumber: fetchState.latestOnBlockBlockNumber,
1516
1556
  blockLag: fetchState.blockLag,
1517
1557
  buffer: fetchState.buffer,
1518
- targetBufferSize: fetchState.targetBufferSize,
1558
+ maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1519
1559
  onBlockConfigs: fetchState.onBlockConfigs,
1520
1560
  knownHeight: fetchState.knownHeight,
1521
1561
  firstEventBlock: fetchState.firstEventBlock
@@ -1535,24 +1575,31 @@ function isActivelyIndexing(fetchState) {
1535
1575
  }
1536
1576
  }
1537
1577
 
1538
- function isReadyToEnterReorgThreshold(fetchState) {
1578
+ function isFetchingAtHead(fetchState) {
1539
1579
  let knownHeight = fetchState.knownHeight;
1540
1580
  let blockLag = fetchState.blockLag;
1541
1581
  let endBlock = fetchState.endBlock;
1542
1582
  let bufferBlockNumber$1 = bufferBlockNumber(fetchState);
1543
- if (knownHeight !== 0 && (
1544
- endBlock !== undefined && bufferBlockNumber$1 >= endBlock ? true : bufferBlockNumber$1 >= (knownHeight - blockLag | 0)
1545
- )) {
1546
- return Utils.$$Array.isEmpty(fetchState.buffer);
1583
+ if (knownHeight !== 0) {
1584
+ if (endBlock !== undefined && bufferBlockNumber$1 >= endBlock) {
1585
+ return true;
1586
+ } else {
1587
+ return bufferBlockNumber$1 >= (knownHeight - blockLag | 0);
1588
+ }
1547
1589
  } else {
1548
1590
  return false;
1549
1591
  }
1550
1592
  }
1551
1593
 
1552
- function hasFullBatch(fetchState, batchSizeTarget) {
1553
- let item = fetchState.buffer[batchSizeTarget - 1 | 0];
1554
- if (item !== undefined) {
1555
- return item.blockNumber <= bufferBlockNumber(fetchState);
1594
+ function isReadyToEnterReorgThreshold(fetchState) {
1595
+ let knownHeight = fetchState.knownHeight;
1596
+ let blockLag = fetchState.blockLag;
1597
+ let endBlock = fetchState.endBlock;
1598
+ let bufferBlockNumber$1 = bufferBlockNumber(fetchState);
1599
+ if (knownHeight !== 0 && (
1600
+ endBlock !== undefined && bufferBlockNumber$1 >= endBlock ? true : bufferBlockNumber$1 >= (knownHeight - blockLag | 0)
1601
+ )) {
1602
+ return Utils.$$Array.isEmpty(fetchState.buffer);
1556
1603
  } else {
1557
1604
  return false;
1558
1605
  }
@@ -1572,7 +1619,16 @@ function getProgressPercentage(fetchState) {
1572
1619
  return progress / totalRange;
1573
1620
  }
1574
1621
 
1575
- function sortForUnorderedBatch(fetchStates, batchSizeTarget) {
1622
+ function hasFullBatch(fetchState, batchSizeTarget) {
1623
+ let item = fetchState.buffer[batchSizeTarget - 1 | 0];
1624
+ if (item !== undefined) {
1625
+ return item.blockNumber <= bufferBlockNumber(fetchState);
1626
+ } else {
1627
+ return false;
1628
+ }
1629
+ }
1630
+
1631
+ function sortForBatch(fetchStates, batchSizeTarget) {
1576
1632
  let copied = fetchStates.slice();
1577
1633
  copied.sort((a, b) => {
1578
1634
  let match = hasFullBatch(a, batchSizeTarget);
@@ -1618,6 +1674,8 @@ function updateKnownHeight(fetchState, knownHeight) {
1618
1674
 
1619
1675
  let blockItemLogIndex = 16777216;
1620
1676
 
1677
+ let maxPendingChunksPerPartition = 10;
1678
+
1621
1679
  export {
1622
1680
  deriveEffectiveStartBlock,
1623
1681
  getMinHistoryRange,
@@ -1638,17 +1696,21 @@ export {
1638
1696
  handleQueryResult,
1639
1697
  startFetchingQueries,
1640
1698
  pushQueriesForRange,
1699
+ maxPendingChunksPerPartition,
1641
1700
  getNextQuery,
1642
1701
  hasReadyItem,
1643
1702
  getReadyItemsCount,
1644
1703
  make$1 as make,
1645
1704
  bufferSize,
1705
+ bufferReadyCount,
1646
1706
  rollbackPendingQueries,
1647
1707
  rollback,
1648
1708
  resetPendingQueries,
1649
1709
  isActivelyIndexing,
1710
+ isFetchingAtHead,
1650
1711
  isReadyToEnterReorgThreshold,
1651
- sortForUnorderedBatch,
1712
+ getProgressPercentage,
1713
+ sortForBatch,
1652
1714
  getProgressBlockNumberAt,
1653
1715
  updateKnownHeight,
1654
1716
  }
@@ -115,7 +115,11 @@ let applyRegistrations = (~config: Config.t): Config.t => {
115
115
  ~eventName=ev.name,
116
116
  )
117
117
  let indexedParams = evmEv.paramsMetadata->Array.filter(p => p.indexed)
118
- let {getEventFiltersOrThrow, filterByAddresses} = LogSelection.parseEventFiltersOrThrow(
118
+ let {
119
+ getEventFiltersOrThrow,
120
+ filterByAddresses,
121
+ addressFilterParamGroups,
122
+ } = LogSelection.parseEventFiltersOrThrow(
119
123
  ~eventFilters,
120
124
  ~sighash=evmEv.sighash,
121
125
  ~params=indexedParams->Array.map(p => p.name),
@@ -140,6 +144,7 @@ let applyRegistrations = (~config: Config.t): Config.t => {
140
144
  contractRegister,
141
145
  getEventFiltersOrThrow,
142
146
  filterByAddresses,
147
+ clientAddressFilter: ?EventConfigBuilder.buildAddressFilter(addressFilterParamGroups),
143
148
  dependsOnAddresses: Internal.dependsOnAddresses(~isWildcard, ~filterByAddresses),
144
149
  } :> Internal.eventConfig)
145
150
  | Svm =>
@@ -75,48 +75,25 @@ function applyRegistrations(config) {
75
75
  let indexedParams = ev.paramsMetadata.filter(p => p.indexed);
76
76
  let match$1 = LogSelection.parseEventFiltersOrThrow(eventFilters, ev.sighash, indexedParams.map(p => p.name), ev.contractName, chain.id, config.ecosystem.onEventBlockFilterSchema, Stdlib_Option.map(indexedParams[0], EventConfigBuilder.buildTopicGetter), Stdlib_Option.map(indexedParams[1], EventConfigBuilder.buildTopicGetter), Stdlib_Option.map(indexedParams[2], EventConfigBuilder.buildTopicGetter));
77
77
  let filterByAddresses = match$1.filterByAddresses;
78
- return {
79
- id: ev.id,
80
- name: ev.name,
81
- contractName: ev.contractName,
82
- isWildcard: isWildcard,
83
- filterByAddresses: filterByAddresses,
84
- dependsOnAddresses: Internal.dependsOnAddresses(isWildcard, filterByAddresses),
85
- handler: handler,
86
- contractRegister: contractRegister,
87
- paramsRawEventSchema: ev.paramsRawEventSchema,
88
- simulateParamsSchema: ev.simulateParamsSchema,
89
- startBlock: ev.startBlock,
90
- getEventFiltersOrThrow: match$1.getEventFiltersOrThrow,
91
- selectedBlockFields: ev.selectedBlockFields,
92
- selectedTransactionFields: ev.selectedTransactionFields,
93
- sighash: ev.sighash,
94
- topicCount: ev.topicCount,
95
- paramsMetadata: ev.paramsMetadata
96
- };
97
- case "fuel" :
98
- return {
99
- id: ev.id,
100
- name: ev.name,
101
- contractName: ev.contractName,
102
- isWildcard: isWildcard,
103
- filterByAddresses: ev.filterByAddresses,
104
- dependsOnAddresses: Internal.dependsOnAddresses(isWildcard, false),
105
- handler: handler,
106
- contractRegister: contractRegister,
107
- paramsRawEventSchema: ev.paramsRawEventSchema,
108
- simulateParamsSchema: ev.simulateParamsSchema,
109
- startBlock: ev.startBlock,
110
- kind: ev.kind
111
- };
112
- case "svm" :
113
78
  let newrecord = {...ev};
79
+ newrecord.getEventFiltersOrThrow = match$1.getEventFiltersOrThrow;
114
80
  newrecord.contractRegister = contractRegister;
115
81
  newrecord.handler = handler;
116
- newrecord.dependsOnAddresses = Internal.dependsOnAddresses(isWildcard, false);
82
+ newrecord.clientAddressFilter = EventConfigBuilder.buildAddressFilter(match$1.addressFilterParamGroups);
83
+ newrecord.dependsOnAddresses = Internal.dependsOnAddresses(isWildcard, filterByAddresses);
84
+ newrecord.filterByAddresses = filterByAddresses;
117
85
  newrecord.isWildcard = isWildcard;
118
86
  return newrecord;
87
+ case "fuel" :
88
+ case "svm" :
89
+ break;
119
90
  }
91
+ let newrecord$1 = {...ev};
92
+ newrecord$1.contractRegister = contractRegister;
93
+ newrecord$1.handler = handler;
94
+ newrecord$1.dependsOnAddresses = Internal.dependsOnAddresses(isWildcard, false);
95
+ newrecord$1.isWildcard = isWildcard;
96
+ return newrecord$1;
120
97
  });
121
98
  return {
122
99
  name: contract.name,