envio 3.2.1 → 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 (106) 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 +2 -2
  26. package/src/EventProcessing.res +60 -46
  27. package/src/EventProcessing.res.mjs +33 -32
  28. package/src/EventUtils.res +0 -4
  29. package/src/EventUtils.res.mjs +0 -4
  30. package/src/ExitOnCaughtUp.res +10 -0
  31. package/src/ExitOnCaughtUp.res.mjs +22 -0
  32. package/src/FetchState.res +124 -62
  33. package/src/FetchState.res.mjs +159 -93
  34. package/src/InMemoryStore.res +36 -451
  35. package/src/InMemoryStore.res.mjs +20 -369
  36. package/src/IndexerLoop.res +44 -0
  37. package/src/IndexerLoop.res.mjs +46 -0
  38. package/src/IndexerState.res +544 -0
  39. package/src/IndexerState.res.mjs +547 -0
  40. package/src/IndexerState.resi +132 -0
  41. package/src/Internal.res +29 -8
  42. package/src/LoadLayer.res +20 -18
  43. package/src/LoadLayer.res.mjs +14 -12
  44. package/src/LoadLayer.resi +6 -3
  45. package/src/Logging.res +11 -44
  46. package/src/Logging.res.mjs +10 -42
  47. package/src/Main.res +54 -79
  48. package/src/Main.res.mjs +44 -56
  49. package/src/PgStorage.res +8 -71
  50. package/src/PgStorage.res.mjs +3 -47
  51. package/src/Prometheus.res +7 -8
  52. package/src/Prometheus.res.mjs +7 -4
  53. package/src/PruneStaleHistory.res +45 -0
  54. package/src/PruneStaleHistory.res.mjs +46 -0
  55. package/src/RawEvent.res +73 -0
  56. package/src/RawEvent.res.mjs +51 -0
  57. package/src/Rollback.res +204 -0
  58. package/src/Rollback.res.mjs +118 -0
  59. package/src/SimulateItems.res +12 -10
  60. package/src/SimulateItems.res.mjs +1 -1
  61. package/src/UserContext.res +26 -114
  62. package/src/UserContext.res.mjs +15 -78
  63. package/src/Writing.res +242 -0
  64. package/src/Writing.res.mjs +215 -0
  65. package/src/db/InternalTable.res +14 -27
  66. package/src/sources/Evm.res +40 -1
  67. package/src/sources/Evm.res.mjs +94 -84
  68. package/src/sources/Fuel.res +40 -1
  69. package/src/sources/Fuel.res.mjs +36 -26
  70. package/src/sources/HyperFuel.res +41 -120
  71. package/src/sources/HyperFuel.res.mjs +42 -80
  72. package/src/sources/HyperFuel.resi +1 -24
  73. package/src/sources/HyperFuelClient.res +16 -297
  74. package/src/sources/HyperFuelClient.res.mjs +5 -4
  75. package/src/sources/HyperFuelSource.res +33 -8
  76. package/src/sources/HyperFuelSource.res.mjs +56 -10
  77. package/src/sources/HyperSyncSource.res +5 -3
  78. package/src/sources/HyperSyncSource.res.mjs +1 -1
  79. package/src/sources/RpcSource.res +2 -2
  80. package/src/sources/RpcSource.res.mjs +1 -1
  81. package/src/sources/SourceManager.res +8 -18
  82. package/src/sources/SourceManager.res.mjs +8 -5
  83. package/src/sources/SourceManager.resi +4 -1
  84. package/src/sources/Svm.res +18 -1
  85. package/src/sources/Svm.res.mjs +30 -22
  86. package/src/sources/SvmHyperSyncSource.res +1 -1
  87. package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
  88. package/src/tui/Tui.res +38 -36
  89. package/src/tui/Tui.res.mjs +51 -51
  90. package/src/ChainFetcher.res +0 -519
  91. package/src/ChainFetcher.res.mjs +0 -314
  92. package/src/ChainManager.res +0 -358
  93. package/src/ChainManager.res.mjs +0 -291
  94. package/src/Ctx.res +0 -6
  95. package/src/Ctx.res.mjs +0 -2
  96. package/src/GlobalState.res +0 -1056
  97. package/src/GlobalState.res.mjs +0 -1086
  98. package/src/GlobalStateManager.res +0 -57
  99. package/src/GlobalStateManager.res.mjs +0 -68
  100. package/src/GlobalStateManager.resi +0 -7
  101. package/src/Ports.res +0 -5
  102. package/src/Ports.res.mjs +0 -9
  103. package/src/adapters/MarkBatchProcessedAdapter.res +0 -5
  104. package/src/adapters/MarkBatchProcessedAdapter.res.mjs +0 -14
  105. package/src/sources/EnvioApiClient.res +0 -15
  106. 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
@@ -903,7 +903,7 @@ function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
903
903
  }
904
904
  let filter = item.eventConfig.clientAddressFilter;
905
905
  if (filter !== undefined) {
906
- return filter(item.event, item.blockNumber, fetchState.indexingAddresses);
906
+ return filter(item.payload, item.blockNumber, fetchState.indexingAddresses);
907
907
  } else {
908
908
  return true;
909
909
  }
@@ -949,36 +949,42 @@ function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock
949
949
  if (maybeChunkRange !== undefined) {
950
950
  let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
951
951
  let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
952
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
953
- queries.push({
954
- partitionId: partitionId,
955
- fromBlock: rangeFromBlock,
956
- toBlock: (rangeFromBlock + chunkSize | 0) - 1 | 0,
957
- isChunk: true,
958
- selection: selection,
959
- addressesByContractName: addressesByContractName,
960
- indexingAddresses: indexingAddresses
961
- });
962
- queries.push({
963
- partitionId: partitionId,
964
- fromBlock: rangeFromBlock + chunkSize | 0,
965
- toBlock: (rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0,
966
- isChunk: true,
967
- selection: selection,
968
- addressesByContractName: addressesByContractName,
969
- indexingAddresses: indexingAddresses
970
- });
971
- } else {
972
- queries.push({
973
- partitionId: partitionId,
974
- fromBlock: rangeFromBlock,
975
- toBlock: rangeEndBlock,
976
- isChunk: rangeEndBlock !== undefined,
977
- selection: selection,
978
- addressesByContractName: addressesByContractName,
979
- indexingAddresses: indexingAddresses
980
- });
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;
981
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
+ });
982
988
  return;
983
989
  }
984
990
  queries.push({
@@ -992,7 +998,7 @@ function pushQueriesForRange(queries, partitionId, rangeFromBlock, rangeEndBlock
992
998
  });
993
999
  }
994
1000
 
995
- function getNextQuery(fetchState, concurrencyLimit) {
1001
+ function getNextQuery(fetchState, concurrencyLimit, bufferLimit) {
996
1002
  let knownHeight = fetchState.knownHeight;
997
1003
  let blockLag = fetchState.blockLag;
998
1004
  let indexingAddresses = fetchState.indexingAddresses;
@@ -1004,12 +1010,15 @@ function getNextQuery(fetchState, concurrencyLimit) {
1004
1010
  if (concurrencyLimit === 0) {
1005
1011
  return "ReachedMaxConcurrency";
1006
1012
  }
1013
+ if (bufferLimit <= 0) {
1014
+ return "NothingToQuery";
1015
+ }
1007
1016
  let isOnBlockBehindTheHead = fetchState.latestOnBlockBlockNumber < headBlockNumber;
1008
1017
  let endBlock = fetchState.endBlock;
1009
1018
  let shouldWaitForNewBlock = (
1010
1019
  endBlock !== undefined ? headBlockNumber < endBlock : true
1011
1020
  ) && !isOnBlockBehindTheHead;
1012
- let item = fetchState.buffer[fetchState.targetBufferSize - 1 | 0];
1021
+ let item = fetchState.buffer[bufferLimit - 1 | 0];
1013
1022
  let maxQueryBlockNumber = item !== undefined ? Primitive_int.min(item.blockNumber, knownHeight) : knownHeight;
1014
1023
  let queries = [];
1015
1024
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
@@ -1018,11 +1027,13 @@ function getNextQuery(fetchState, concurrencyLimit) {
1018
1027
  let idx = idxRef;
1019
1028
  let partitionId = optimizedPartitions.idsInAscOrder[idx];
1020
1029
  let p = optimizedPartitions.entities[partitionId];
1030
+ let pendingCount = p.mutPendingQueries.length;
1021
1031
  let isBehindTheHead = p.latestFetchedBlock.blockNumber < headBlockNumber;
1022
- let hasPendingQueries = Utils.$$Array.notEmpty(p.mutPendingQueries);
1032
+ let hasPendingQueries = pendingCount > 0;
1023
1033
  if (hasPendingQueries || isBehindTheHead) {
1024
1034
  shouldWaitForNewBlock = false;
1025
1035
  }
1036
+ let partitionQueriesStart = queries.length;
1026
1037
  let queryEndBlock = Utils.$$Math.minOptInt(fetchState.endBlock, p.mergeBlock);
1027
1038
  let queryEndBlock$1 = blockLag !== 0 ? Utils.$$Math.minOptInt(headBlockNumber, queryEndBlock) : queryEndBlock;
1028
1039
  let match = maxQueryBlockNumber < knownHeight;
@@ -1051,25 +1062,31 @@ function getNextQuery(fetchState, concurrencyLimit) {
1051
1062
  if (maybeChunkRange !== undefined) {
1052
1063
  let maxBlock = rangeEndBlock !== undefined ? rangeEndBlock : maxQueryBlockNumber;
1053
1064
  let chunkSize = Js_math.ceil_int(maybeChunkRange * 1.8);
1054
- if (((rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0) <= maxBlock) {
1055
- queries.push({
1056
- partitionId: partitionId,
1057
- fromBlock: rangeFromBlock,
1058
- toBlock: (rangeFromBlock + chunkSize | 0) - 1 | 0,
1059
- isChunk: true,
1060
- selection: selection,
1061
- addressesByContractName: addressesByContractName,
1062
- indexingAddresses: indexingAddresses
1063
- });
1064
- queries.push({
1065
- partitionId: partitionId,
1066
- fromBlock: rangeFromBlock + chunkSize | 0,
1067
- toBlock: (rangeFromBlock + (chunkSize << 1) | 0) - 1 | 0,
1068
- isChunk: true,
1069
- selection: selection,
1070
- addressesByContractName: addressesByContractName,
1071
- indexingAddresses: indexingAddresses
1072
- });
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
+ };
1073
1090
  } else {
1074
1091
  queries.push({
1075
1092
  partitionId: partitionId,
@@ -1122,25 +1139,31 @@ function getNextQuery(fetchState, concurrencyLimit) {
1122
1139
  if (maybeChunkRange !== undefined) {
1123
1140
  let maxBlock$1 = queryEndBlock$2 !== undefined ? queryEndBlock$2 : maxQueryBlockNumber;
1124
1141
  let chunkSize$1 = Js_math.ceil_int(maybeChunkRange * 1.8);
1125
- if (((rangeFromBlock$1 + (chunkSize$1 << 1) | 0) - 1 | 0) <= maxBlock$1) {
1126
- queries.push({
1127
- partitionId: partitionId,
1128
- fromBlock: rangeFromBlock$1,
1129
- toBlock: (rangeFromBlock$1 + chunkSize$1 | 0) - 1 | 0,
1130
- isChunk: true,
1131
- selection: selection$1,
1132
- addressesByContractName: addressesByContractName$1,
1133
- indexingAddresses: indexingAddresses
1134
- });
1135
- queries.push({
1136
- partitionId: partitionId,
1137
- fromBlock: rangeFromBlock$1 + chunkSize$1 | 0,
1138
- toBlock: (rangeFromBlock$1 + (chunkSize$1 << 1) | 0) - 1 | 0,
1139
- isChunk: true,
1140
- selection: selection$1,
1141
- addressesByContractName: addressesByContractName$1,
1142
- indexingAddresses: indexingAddresses
1143
- });
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
+ };
1144
1167
  } else {
1145
1168
  queries.push({
1146
1169
  partitionId: partitionId,
@@ -1166,6 +1189,11 @@ function getNextQuery(fetchState, concurrencyLimit) {
1166
1189
  }
1167
1190
  }
1168
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
+ }
1169
1197
  idxRef = idxRef + 1 | 0;
1170
1198
  };
1171
1199
  if (Utils.$$Array.isEmpty(queries)) {
@@ -1214,7 +1242,7 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1214
1242
  return acc;
1215
1243
  }
1216
1244
 
1217
- 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) {
1218
1246
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1219
1247
  let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1220
1248
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
@@ -1311,7 +1339,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1311
1339
  let id = optimizedPartitions.idsInAscOrder[0];
1312
1340
  let latestFullyFetchedBlock = id !== undefined ? optimizedPartitions.entities[id].latestFetchedBlock : undefined;
1313
1341
  let maxBlockNumber = latestFullyFetchedBlock !== undefined ? latestFullyFetchedBlock.blockNumber : knownHeight;
1314
- latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, targetBufferSize);
1342
+ latestOnBlockBlockNumber = appendOnBlockItems(buffer, onBlockConfigs, startBlock, progressBlockNumber, maxBlockNumber, maxOnBlockBufferSize);
1315
1343
  } else {
1316
1344
  latestOnBlockBlockNumber = progressBlockNumber;
1317
1345
  }
@@ -1326,7 +1354,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1326
1354
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
1327
1355
  blockLag: blockLag,
1328
1356
  buffer: buffer,
1329
- targetBufferSize: targetBufferSize,
1357
+ maxOnBlockBufferSize: maxOnBlockBufferSize,
1330
1358
  onBlockConfigs: onBlockConfigs,
1331
1359
  knownHeight: knownHeight,
1332
1360
  firstEventBlock: firstEventBlock
@@ -1346,6 +1374,22 @@ function bufferSize(param) {
1346
1374
  return param.buffer.length;
1347
1375
  }
1348
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
+
1349
1393
  function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1350
1394
  let adjusted = [];
1351
1395
  for (let qIdx = 0, qIdx_finish = mutPendingQueries.length; qIdx < qIdx_finish; ++qIdx) {
@@ -1461,7 +1505,7 @@ function rollback(fetchState, targetBlockNumber) {
1461
1505
  latestOnBlockBlockNumber: Primitive_int.min(fetchState.latestOnBlockBlockNumber, targetBlockNumber),
1462
1506
  blockLag: fetchState.blockLag,
1463
1507
  buffer: fetchState.buffer,
1464
- targetBufferSize: fetchState.targetBufferSize,
1508
+ maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1465
1509
  onBlockConfigs: fetchState.onBlockConfigs,
1466
1510
  knownHeight: fetchState.knownHeight,
1467
1511
  firstEventBlock: fetchState.firstEventBlock
@@ -1511,7 +1555,7 @@ function resetPendingQueries(fetchState) {
1511
1555
  latestOnBlockBlockNumber: fetchState.latestOnBlockBlockNumber,
1512
1556
  blockLag: fetchState.blockLag,
1513
1557
  buffer: fetchState.buffer,
1514
- targetBufferSize: fetchState.targetBufferSize,
1558
+ maxOnBlockBufferSize: fetchState.maxOnBlockBufferSize,
1515
1559
  onBlockConfigs: fetchState.onBlockConfigs,
1516
1560
  knownHeight: fetchState.knownHeight,
1517
1561
  firstEventBlock: fetchState.firstEventBlock
@@ -1531,24 +1575,31 @@ function isActivelyIndexing(fetchState) {
1531
1575
  }
1532
1576
  }
1533
1577
 
1534
- function isReadyToEnterReorgThreshold(fetchState) {
1578
+ function isFetchingAtHead(fetchState) {
1535
1579
  let knownHeight = fetchState.knownHeight;
1536
1580
  let blockLag = fetchState.blockLag;
1537
1581
  let endBlock = fetchState.endBlock;
1538
1582
  let bufferBlockNumber$1 = bufferBlockNumber(fetchState);
1539
- if (knownHeight !== 0 && (
1540
- endBlock !== undefined && bufferBlockNumber$1 >= endBlock ? true : bufferBlockNumber$1 >= (knownHeight - blockLag | 0)
1541
- )) {
1542
- 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
+ }
1543
1589
  } else {
1544
1590
  return false;
1545
1591
  }
1546
1592
  }
1547
1593
 
1548
- function hasFullBatch(fetchState, batchSizeTarget) {
1549
- let item = fetchState.buffer[batchSizeTarget - 1 | 0];
1550
- if (item !== undefined) {
1551
- 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);
1552
1603
  } else {
1553
1604
  return false;
1554
1605
  }
@@ -1568,7 +1619,16 @@ function getProgressPercentage(fetchState) {
1568
1619
  return progress / totalRange;
1569
1620
  }
1570
1621
 
1571
- 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) {
1572
1632
  let copied = fetchStates.slice();
1573
1633
  copied.sort((a, b) => {
1574
1634
  let match = hasFullBatch(a, batchSizeTarget);
@@ -1614,6 +1674,8 @@ function updateKnownHeight(fetchState, knownHeight) {
1614
1674
 
1615
1675
  let blockItemLogIndex = 16777216;
1616
1676
 
1677
+ let maxPendingChunksPerPartition = 10;
1678
+
1617
1679
  export {
1618
1680
  deriveEffectiveStartBlock,
1619
1681
  getMinHistoryRange,
@@ -1634,17 +1696,21 @@ export {
1634
1696
  handleQueryResult,
1635
1697
  startFetchingQueries,
1636
1698
  pushQueriesForRange,
1699
+ maxPendingChunksPerPartition,
1637
1700
  getNextQuery,
1638
1701
  hasReadyItem,
1639
1702
  getReadyItemsCount,
1640
1703
  make$1 as make,
1641
1704
  bufferSize,
1705
+ bufferReadyCount,
1642
1706
  rollbackPendingQueries,
1643
1707
  rollback,
1644
1708
  resetPendingQueries,
1645
1709
  isActivelyIndexing,
1710
+ isFetchingAtHead,
1646
1711
  isReadyToEnterReorgThreshold,
1647
- sortForUnorderedBatch,
1712
+ getProgressPercentage,
1713
+ sortForBatch,
1648
1714
  getProgressBlockNumberAt,
1649
1715
  updateKnownHeight,
1650
1716
  }