envio 3.3.0-alpha.4 → 3.3.0-alpha.5

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.
@@ -9,10 +9,7 @@ import * as Primitive_int from "@rescript/runtime/lib/es6/Primitive_int.js";
9
9
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
10
10
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
11
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
12
-
13
- function deriveEffectiveStartBlock(registrationBlock, contractStartBlock) {
14
- return Primitive_int.max(Primitive_int.max(registrationBlock, 0), Stdlib_Option.getOr(contractStartBlock, 0));
15
- }
12
+ import * as IndexingAddresses from "./IndexingAddresses.res.mjs";
16
13
 
17
14
  let deriveContractNameByAddress = Utils.$$WeakMap.memoize(addressesByContractName => {
18
15
  let result = {};
@@ -464,6 +461,22 @@ function bufferBlock(param) {
464
461
  }
465
462
  }
466
463
 
464
+ function bufferReadyCount(fetchState) {
465
+ let frontier = bufferBlockNumber(fetchState);
466
+ let buffer = fetchState.buffer;
467
+ let lo = 0;
468
+ let hi = buffer.length;
469
+ while (lo < hi) {
470
+ let mid = (lo + hi | 0) / 2 | 0;
471
+ if (buffer[mid].blockNumber <= frontier) {
472
+ lo = mid + 1 | 0;
473
+ } else {
474
+ hi = mid;
475
+ }
476
+ };
477
+ return lo;
478
+ }
479
+
467
480
  function compareBufferItem(a, b) {
468
481
  let blockOrdering = Primitive_int.compare(a.blockNumber, b.blockNumber);
469
482
  if (blockOrdering === 0) {
@@ -473,10 +486,6 @@ function compareBufferItem(a, b) {
473
486
  }
474
487
  }
475
488
 
476
- function numAddresses(fetchState) {
477
- return Utils.Dict.size(fetchState.indexingAddresses);
478
- }
479
-
480
489
  function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlock, maxBlockNumber, maxOnBlockBufferSize) {
481
490
  let newItemsCounter = 0;
482
491
  let latestOnBlockBlockNumber = fromBlock;
@@ -506,9 +515,8 @@ function appendOnBlockItems(mutItems, onBlockConfigs, indexerStartBlock, fromBlo
506
515
  return latestOnBlockBlockNumber;
507
516
  }
508
517
 
509
- function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt, mutItems, blockLagOpt, knownHeightOpt) {
518
+ function updateInternal(fetchState, optimizedPartitionsOpt, mutItems, blockLagOpt, knownHeightOpt) {
510
519
  let optimizedPartitions = optimizedPartitionsOpt !== undefined ? optimizedPartitionsOpt : fetchState.optimizedPartitions;
511
- let indexingAddresses = indexingAddressesOpt !== undefined ? indexingAddressesOpt : fetchState.indexingAddresses;
512
520
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : fetchState.blockLag;
513
521
  let knownHeight = knownHeightOpt !== undefined ? knownHeightOpt : fetchState.knownHeight;
514
522
  let mutItemsRef = mutItems;
@@ -549,7 +557,6 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
549
557
  startBlock: updatedFetchState_startBlock,
550
558
  endBlock: updatedFetchState_endBlock,
551
559
  normalSelection: updatedFetchState_normalSelection,
552
- indexingAddresses: indexingAddresses,
553
560
  contractConfigs: updatedFetchState_contractConfigs,
554
561
  chainId: updatedFetchState_chainId,
555
562
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
@@ -563,9 +570,6 @@ function updateInternal(fetchState, optimizedPartitionsOpt, indexingAddressesOpt
563
570
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, fetchState.chainId);
564
571
  Prometheus.IndexingBufferSize.set(updatedFetchState_buffer.length, fetchState.chainId);
565
572
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(updatedFetchState), fetchState.chainId);
566
- if (indexingAddresses !== fetchState.indexingAddresses) {
567
- Prometheus.IndexingAddresses.set(Utils.Dict.size(indexingAddresses), fetchState.chainId);
568
- }
569
573
  return updatedFetchState;
570
574
  }
571
575
 
@@ -747,11 +751,10 @@ function createPartitionsFromIndexingAddresses(registeringContractsByContract, d
747
751
  return make(existingPartitions.concat(mergedPartitions), maxAddrInPartition, nextPartitionIndexRef, dynamicContracts);
748
752
  }
749
753
 
750
- function registerDynamicContracts(fetchState, items) {
754
+ function registerDynamicContracts(fetchState, indexingAddresses, items) {
751
755
  if (Utils.$$Array.isEmpty(fetchState.normalSelection.eventConfigs)) {
752
756
  Stdlib_JsError.throwWithMessage("Invalid configuration. No events to fetch for the dynamic contract registration.");
753
757
  }
754
- let indexingAddresses = fetchState.indexingAddresses;
755
758
  let registeringContractsByContract = {};
756
759
  let earliestRegisteringEventBlockNumber = Infinity;
757
760
  let noEventsAddresses = {};
@@ -769,14 +772,14 @@ function registerDynamicContracts(fetchState, items) {
769
772
  let dcWithStartBlock_address = dc.address;
770
773
  let dcWithStartBlock_contractName = dc.contractName;
771
774
  let dcWithStartBlock_registrationBlock = dc.registrationBlock;
772
- let dcWithStartBlock_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
775
+ let dcWithStartBlock_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, match.startBlock);
773
776
  let dcWithStartBlock = {
774
777
  address: dcWithStartBlock_address,
775
778
  contractName: dcWithStartBlock_contractName,
776
779
  registrationBlock: dcWithStartBlock_registrationBlock,
777
780
  effectiveStartBlock: dcWithStartBlock_effectiveStartBlock
778
781
  };
779
- let existingContract = indexingAddresses[dc.address];
782
+ let existingContract = IndexingAddresses.get(indexingAddresses, dc.address);
780
783
  if (existingContract !== undefined) {
781
784
  if (existingContract.contractName !== dc.contractName) {
782
785
  warnDifferentContractType(fetchState, existingContract, dcWithStartBlock);
@@ -807,14 +810,14 @@ function registerDynamicContracts(fetchState, items) {
807
810
  let dcAsIndexingAddress_address = dc.address;
808
811
  let dcAsIndexingAddress_contractName = dc.contractName;
809
812
  let dcAsIndexingAddress_registrationBlock = dc.registrationBlock;
810
- let dcAsIndexingAddress_effectiveStartBlock = deriveEffectiveStartBlock(dc.registrationBlock, undefined);
813
+ let dcAsIndexingAddress_effectiveStartBlock = IndexingAddresses.deriveEffectiveStartBlock(dc.registrationBlock, undefined);
811
814
  let dcAsIndexingAddress = {
812
815
  address: dcAsIndexingAddress_address,
813
816
  contractName: dcAsIndexingAddress_contractName,
814
817
  registrationBlock: dcAsIndexingAddress_registrationBlock,
815
818
  effectiveStartBlock: dcAsIndexingAddress_effectiveStartBlock
816
819
  };
817
- let existingContract$1 = indexingAddresses[dc.address];
820
+ let existingContract$1 = IndexingAddresses.get(indexingAddresses, dc.address);
818
821
  if (existingContract$1 !== undefined) {
819
822
  if (existingContract$1.contractName !== dc.contractName) {
820
823
  warnDifferentContractType(fetchState, existingContract$1, dcAsIndexingAddress);
@@ -849,101 +852,100 @@ function registerDynamicContracts(fetchState, items) {
849
852
  }
850
853
  let dcContractNamesToStore = Object.keys(registeringContractsByContract);
851
854
  let hasNoEventsUpdates = !Utils.Dict.isEmpty(noEventsAddresses);
852
- if (dcContractNamesToStore.length !== 0) {
853
- let newPartitions = [];
854
- let newIndexingAddresses = Utils.Dict.shallowCopy(indexingAddresses);
855
- let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
856
- let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
857
- for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
858
- let contractName = dcContractNamesToStore[idx$1];
859
- if (!dynamicContractsRef.has(contractName)) {
860
- dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
861
- for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
862
- let p = mutExistingPartitions[idx$2];
863
- let addresses = p.addressesByContractName[contractName];
864
- if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
865
- let allPartitionContractNames = Object.keys(p.addressesByContractName);
866
- if (allPartitionContractNames.length !== 1) {
867
- let isFetching = p.mutPendingQueries.length !== 0;
868
- if (!isFetching) {
869
- let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
870
- let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
871
- Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
872
- mutExistingPartitions[idx$2] = {
873
- id: p.id,
874
- latestFetchedBlock: p.latestFetchedBlock,
875
- selection: p.selection,
876
- addressesByContractName: restAddressesByContractName,
877
- mergeBlock: p.mergeBlock,
878
- dynamicContract: p.dynamicContract,
879
- mutPendingQueries: p.mutPendingQueries,
880
- prevQueryRange: p.prevQueryRange,
881
- prevPrevQueryRange: p.prevPrevQueryRange,
882
- prevRangeSize: p.prevRangeSize,
883
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
884
- };
885
- let addressesByContractName = {};
886
- addressesByContractName[contractName] = addresses;
887
- newPartitions.push({
888
- id: newPartitionId,
889
- latestFetchedBlock: p.latestFetchedBlock,
890
- selection: fetchState.normalSelection,
891
- addressesByContractName: addressesByContractName,
892
- mergeBlock: undefined,
893
- dynamicContract: contractName,
894
- mutPendingQueries: p.mutPendingQueries,
895
- prevQueryRange: p.prevQueryRange,
896
- prevPrevQueryRange: p.prevPrevQueryRange,
897
- prevRangeSize: p.prevRangeSize,
898
- latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
899
- });
900
- }
901
- } else {
855
+ if (dcContractNamesToStore.length === 0) {
856
+ if (hasNoEventsUpdates) {
857
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
858
+ return fetchState;
859
+ } else {
860
+ return fetchState;
861
+ }
862
+ }
863
+ let newPartitions = [];
864
+ let dynamicContractsRef = fetchState.optimizedPartitions.dynamicContracts;
865
+ let mutExistingPartitions = Object.values(fetchState.optimizedPartitions.entities);
866
+ for (let idx$1 = 0, idx_finish = dcContractNamesToStore.length; idx$1 < idx_finish; ++idx$1) {
867
+ let contractName = dcContractNamesToStore[idx$1];
868
+ if (!dynamicContractsRef.has(contractName)) {
869
+ dynamicContractsRef = Utils.$$Set.immutableAdd(dynamicContractsRef, contractName);
870
+ for (let idx$2 = 0, idx_finish$1 = mutExistingPartitions.length; idx$2 < idx_finish$1; ++idx$2) {
871
+ let p = mutExistingPartitions[idx$2];
872
+ let addresses = p.addressesByContractName[contractName];
873
+ if (addresses !== undefined && p.selection.dependsOnAddresses && p.mergeBlock === undefined) {
874
+ let allPartitionContractNames = Object.keys(p.addressesByContractName);
875
+ if (allPartitionContractNames.length !== 1) {
876
+ let isFetching = p.mutPendingQueries.length !== 0;
877
+ if (!isFetching) {
878
+ let newPartitionId = (fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0).toString();
879
+ let restAddressesByContractName = Utils.Dict.shallowCopy(p.addressesByContractName);
880
+ Utils.Dict.deleteInPlace(restAddressesByContractName, contractName);
902
881
  mutExistingPartitions[idx$2] = {
903
882
  id: p.id,
904
883
  latestFetchedBlock: p.latestFetchedBlock,
905
884
  selection: p.selection,
906
- addressesByContractName: p.addressesByContractName,
885
+ addressesByContractName: restAddressesByContractName,
907
886
  mergeBlock: p.mergeBlock,
908
- dynamicContract: contractName,
887
+ dynamicContract: p.dynamicContract,
909
888
  mutPendingQueries: p.mutPendingQueries,
910
889
  prevQueryRange: p.prevQueryRange,
911
890
  prevPrevQueryRange: p.prevPrevQueryRange,
912
891
  prevRangeSize: p.prevRangeSize,
913
892
  latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
914
893
  };
894
+ let addressesByContractName = {};
895
+ addressesByContractName[contractName] = addresses;
896
+ newPartitions.push({
897
+ id: newPartitionId,
898
+ latestFetchedBlock: p.latestFetchedBlock,
899
+ selection: fetchState.normalSelection,
900
+ addressesByContractName: addressesByContractName,
901
+ mergeBlock: undefined,
902
+ dynamicContract: contractName,
903
+ mutPendingQueries: p.mutPendingQueries,
904
+ prevQueryRange: p.prevQueryRange,
905
+ prevPrevQueryRange: p.prevPrevQueryRange,
906
+ prevRangeSize: p.prevRangeSize,
907
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
908
+ });
915
909
  }
910
+ } else {
911
+ mutExistingPartitions[idx$2] = {
912
+ id: p.id,
913
+ latestFetchedBlock: p.latestFetchedBlock,
914
+ selection: p.selection,
915
+ addressesByContractName: p.addressesByContractName,
916
+ mergeBlock: p.mergeBlock,
917
+ dynamicContract: contractName,
918
+ mutPendingQueries: p.mutPendingQueries,
919
+ prevQueryRange: p.prevQueryRange,
920
+ prevPrevQueryRange: p.prevPrevQueryRange,
921
+ prevRangeSize: p.prevRangeSize,
922
+ latestBlockRangeUpdateBlock: p.latestBlockRangeUpdateBlock
923
+ };
916
924
  }
917
925
  }
918
926
  }
919
- let registeringContracts = registeringContractsByContract[contractName];
920
- Object.assign(newIndexingAddresses, registeringContracts);
921
927
  }
922
- Object.assign(newIndexingAddresses, noEventsAddresses);
923
- let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
924
- return updateInternal(fetchState, optimizedPartitions, newIndexingAddresses, undefined, undefined, undefined);
925
- }
926
- if (!hasNoEventsUpdates) {
927
- return fetchState;
928
+ let registeringContracts = registeringContractsByContract[contractName];
929
+ IndexingAddresses.register(indexingAddresses, registeringContracts);
928
930
  }
929
- let newIndexingContracts = Utils.Dict.shallowCopy(indexingAddresses);
930
- Object.assign(newIndexingContracts, noEventsAddresses);
931
- return updateInternal(fetchState, undefined, newIndexingContracts, undefined, undefined, undefined);
931
+ IndexingAddresses.register(indexingAddresses, noEventsAddresses);
932
+ let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContractsRef, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, fetchState.optimizedPartitions.nextPartitionIndex + newPartitions.length | 0, mutExistingPartitions.concat(newPartitions), 0);
933
+ return updateInternal(fetchState, optimizedPartitions, undefined, undefined, undefined);
932
934
  }
933
935
 
934
- function handleQueryResult(fetchState, query, latestFetchedBlock, newItems) {
936
+ function handleQueryResult(fetchState, indexingAddresses, query, latestFetchedBlock, newItems) {
935
937
  let newItems$1 = newItems.filter(item => {
936
938
  if (item.kind !== 0) {
937
939
  return true;
938
940
  }
939
941
  let filter = item.eventConfig.clientAddressFilter;
940
942
  if (filter !== undefined) {
941
- return filter(item.payload, item.blockNumber, fetchState.indexingAddresses);
943
+ return filter(item.payload, item.blockNumber, IndexingAddresses.rawForFilter(indexingAddresses));
942
944
  } else {
943
945
  return true;
944
946
  }
945
947
  });
946
- return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems$1.length, latestFetchedBlock), undefined, newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
948
+ return updateInternal(fetchState, handleQueryResponse(fetchState.optimizedPartitions, query, fetchState.knownHeight, newItems$1.length, latestFetchedBlock), newItems$1.length !== 0 ? fetchState.buffer.concat(newItems$1) : undefined, undefined, undefined);
947
949
  }
948
950
 
949
951
  function startFetchingQueries(param, queries) {
@@ -1048,7 +1050,7 @@ function getNextQuery(fetchState, budget, chainPendingBudget) {
1048
1050
  let shouldWaitForNewBlock = (
1049
1051
  endBlock !== undefined ? headBlockNumber < endBlock : true
1050
1052
  ) && !isOnBlockBehindTheHead;
1051
- let item = fetchState.buffer[(budget + (chainPendingBudget | 0) | 0) - 1 | 0];
1053
+ let item = fetchState.buffer[((bufferReadyCount(fetchState) + budget | 0) + (chainPendingBudget | 0) | 0) - 1 | 0];
1052
1054
  let maxQueryBlockNumber = item !== undefined ? Primitive_int.min(item.blockNumber, knownHeight) : knownHeight;
1053
1055
  let queries = [];
1054
1056
  let partitionsCount = optimizedPartitions.idsInAscOrder.length;
@@ -1265,7 +1267,7 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1265
1267
  return acc;
1266
1268
  }
1267
1269
 
1268
- function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1270
+ function make$1(startBlock, endBlock, eventConfigs, contractConfigs, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockConfigsOpt, blockLagOpt, firstEventBlockOpt) {
1269
1271
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1270
1272
  let onBlockConfigs = onBlockConfigsOpt !== undefined ? onBlockConfigsOpt : [];
1271
1273
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
@@ -1277,25 +1279,7 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1277
1279
  let notDependingOnAddresses = [];
1278
1280
  let normalEventConfigs = [];
1279
1281
  let contractNamesWithNormalEvents = new Set();
1280
- let indexingAddresses = {};
1281
- let contractConfigs = {};
1282
1282
  eventConfigs.forEach(ec => {
1283
- let match = contractConfigs[ec.contractName];
1284
- if (match !== undefined) {
1285
- let startBlock = match.startBlock;
1286
- let match$1 = ec.startBlock;
1287
- contractConfigs[ec.contractName] = {
1288
- startBlock: startBlock !== undefined ? (
1289
- match$1 !== undefined ? Primitive_int.min(startBlock, match$1) : startBlock
1290
- ) : (
1291
- match$1 !== undefined ? match$1 : undefined
1292
- )
1293
- };
1294
- } else {
1295
- contractConfigs[ec.contractName] = {
1296
- startBlock: ec.startBlock
1297
- };
1298
- }
1299
1283
  if (ec.dependsOnAddresses) {
1300
1284
  normalEventConfigs.push(ec);
1301
1285
  contractNamesWithNormalEvents.add(ec.contractName);
@@ -1330,27 +1314,14 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1330
1314
  let dynamicContracts = new Set();
1331
1315
  addresses.forEach(contract => {
1332
1316
  let contractName = contract.contractName;
1333
- let match = contractConfigs[contractName];
1334
- let contractStartBlock = match !== undefined ? match.startBlock : undefined;
1335
- let ia_address = contract.address;
1336
- let ia_contractName = contract.contractName;
1337
- let ia_registrationBlock = contract.registrationBlock;
1338
- let ia_effectiveStartBlock = deriveEffectiveStartBlock(contract.registrationBlock, contractStartBlock);
1339
- let ia = {
1340
- address: ia_address,
1341
- contractName: ia_contractName,
1342
- registrationBlock: ia_registrationBlock,
1343
- effectiveStartBlock: ia_effectiveStartBlock
1344
- };
1345
- indexingAddresses[contract.address] = ia;
1346
- if (!contractNamesWithNormalEvents.has(contractName)) {
1347
- return;
1348
- }
1349
- let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1350
- registeringContracts[contract.address] = ia;
1351
- if (contract.registrationBlock !== -1) {
1352
- dynamicContracts.add(contractName);
1353
- return;
1317
+ if (contractNamesWithNormalEvents.has(contractName)) {
1318
+ Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName)[contract.address] = IndexingAddresses.makeIndexingAddress(contract, contractConfigs);
1319
+ if (contract.registrationBlock !== -1) {
1320
+ dynamicContracts.add(contractName);
1321
+ return;
1322
+ } else {
1323
+ return;
1324
+ }
1354
1325
  }
1355
1326
  });
1356
1327
  let optimizedPartitions = createPartitionsFromIndexingAddresses(registeringContractsByContract, dynamicContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber);
@@ -1372,7 +1343,6 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1372
1343
  startBlock: startBlock,
1373
1344
  endBlock: endBlock,
1374
1345
  normalSelection: normalSelection,
1375
- indexingAddresses: indexingAddresses,
1376
1346
  contractConfigs: contractConfigs,
1377
1347
  chainId: chainId,
1378
1348
  latestOnBlockBlockNumber: latestOnBlockBlockNumber,
@@ -1383,8 +1353,6 @@ function make$1(startBlock, endBlock, eventConfigs, addresses, maxAddrInPartitio
1383
1353
  knownHeight: knownHeight,
1384
1354
  firstEventBlock: firstEventBlock
1385
1355
  };
1386
- let numAddresses = Utils.Dict.size(indexingAddresses);
1387
- Prometheus.IndexingAddresses.set(numAddresses, chainId);
1388
1356
  Prometheus.IndexingPartitions.set(optimizedPartitions.idsInAscOrder.length, chainId);
1389
1357
  Prometheus.IndexingBufferSize.set(buffer.length, chainId);
1390
1358
  Prometheus.IndexingBufferBlockNumber.set(bufferBlockNumber(fetchState), chainId);
@@ -1398,22 +1366,6 @@ function bufferSize(param) {
1398
1366
  return param.buffer.length;
1399
1367
  }
1400
1368
 
1401
- function bufferReadyCount(fetchState) {
1402
- let frontier = bufferBlockNumber(fetchState);
1403
- let buffer = fetchState.buffer;
1404
- let lo = 0;
1405
- let hi = buffer.length;
1406
- while (lo < hi) {
1407
- let mid = (lo + hi | 0) / 2 | 0;
1408
- if (buffer[mid].blockNumber <= frontier) {
1409
- lo = mid + 1 | 0;
1410
- } else {
1411
- hi = mid;
1412
- }
1413
- };
1414
- return lo;
1415
- }
1416
-
1417
1369
  function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1418
1370
  let adjusted = [];
1419
1371
  for (let qIdx = 0, qIdx_finish = mutPendingQueries.length; qIdx < qIdx_finish; ++qIdx) {
@@ -1443,16 +1395,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1443
1395
  return adjusted;
1444
1396
  }
1445
1397
 
1446
- function rollback(fetchState, targetBlockNumber) {
1447
- let addressesToRemove = new Set();
1448
- let indexingAddresses = {};
1449
- Utils.Dict.forEachWithKey(fetchState.indexingAddresses, (indexingContract, address) => {
1450
- if (indexingContract.registrationBlock > targetBlockNumber) {
1451
- addressesToRemove.add(address);
1452
- } else {
1453
- indexingAddresses[address] = indexingContract;
1454
- }
1455
- });
1398
+ function rollback(fetchState, indexingAddresses, targetBlockNumber) {
1399
+ IndexingAddresses.rollbackInPlace(indexingAddresses, targetBlockNumber);
1456
1400
  let keptPartitions = [];
1457
1401
  let nextKeptIdRef = 0;
1458
1402
  let registeringContractsByContract = {};
@@ -1463,11 +1407,12 @@ function rollback(fetchState, targetBlockNumber) {
1463
1407
  if (p.latestFetchedBlock.blockNumber > targetBlockNumber) {
1464
1408
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1465
1409
  addresses.forEach(address => {
1466
- if (!(!addressesToRemove.has(address) && Stdlib_Option.isSome(indexingAddresses[address]))) {
1410
+ let indexingContract = IndexingAddresses.get(indexingAddresses, address);
1411
+ if (indexingContract === undefined) {
1467
1412
  return;
1468
1413
  }
1469
1414
  let registeringContracts = Utils.Dict.getOrInsertEmptyDict(registeringContractsByContract, contractName);
1470
- registeringContracts[address] = indexingAddresses[address];
1415
+ registeringContracts[address] = indexingContract;
1471
1416
  });
1472
1417
  });
1473
1418
  } else {
@@ -1475,7 +1420,7 @@ function rollback(fetchState, targetBlockNumber) {
1475
1420
  let mergeBlock$1 = mergeBlock !== undefined && mergeBlock > targetBlockNumber ? targetBlockNumber : mergeBlock;
1476
1421
  let rollbackedAddressesByContractName = {};
1477
1422
  Utils.Dict.forEachWithKey(p.addressesByContractName, (addresses, contractName) => {
1478
- let keptAddresses = addresses.filter(address => !addressesToRemove.has(address));
1423
+ let keptAddresses = addresses.filter(address => Stdlib_Option.isSome(IndexingAddresses.get(indexingAddresses, address)));
1479
1424
  if (keptAddresses.length !== 0) {
1480
1425
  rollbackedAddressesByContractName[contractName] = keptAddresses;
1481
1426
  return;
@@ -1526,7 +1471,6 @@ function rollback(fetchState, targetBlockNumber) {
1526
1471
  startBlock: fetchState.startBlock,
1527
1472
  endBlock: fetchState.endBlock,
1528
1473
  normalSelection: fetchState.normalSelection,
1529
- indexingAddresses: fetchState.indexingAddresses,
1530
1474
  contractConfigs: fetchState.contractConfigs,
1531
1475
  chainId: fetchState.chainId,
1532
1476
  latestOnBlockBlockNumber: Primitive_int.min(fetchState.latestOnBlockBlockNumber, targetBlockNumber),
@@ -1536,7 +1480,7 @@ function rollback(fetchState, targetBlockNumber) {
1536
1480
  onBlockConfigs: fetchState.onBlockConfigs,
1537
1481
  knownHeight: fetchState.knownHeight,
1538
1482
  firstEventBlock: fetchState.firstEventBlock
1539
- }, optimizedPartitions, indexingAddresses, fetchState.buffer.filter(item => {
1483
+ }, optimizedPartitions, fetchState.buffer.filter(item => {
1540
1484
  let tmp;
1541
1485
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1542
1486
  return tmp <= targetBlockNumber;
@@ -1577,7 +1521,6 @@ function resetPendingQueries(fetchState) {
1577
1521
  startBlock: fetchState.startBlock,
1578
1522
  endBlock: fetchState.endBlock,
1579
1523
  normalSelection: fetchState.normalSelection,
1580
- indexingAddresses: fetchState.indexingAddresses,
1581
1524
  contractConfigs: fetchState.contractConfigs,
1582
1525
  chainId: fetchState.chainId,
1583
1526
  latestOnBlockBlockNumber: fetchState.latestOnBlockBlockNumber,
@@ -1707,7 +1650,7 @@ function getProgressBlockNumberAt(fetchState, index) {
1707
1650
  function updateKnownHeight(fetchState, knownHeight) {
1708
1651
  if (knownHeight > fetchState.knownHeight) {
1709
1652
  Prometheus.IndexingKnownHeight.set(knownHeight, fetchState.chainId);
1710
- return updateInternal(fetchState, undefined, undefined, undefined, undefined, knownHeight);
1653
+ return updateInternal(fetchState, undefined, undefined, undefined, knownHeight);
1711
1654
  } else {
1712
1655
  return fetchState;
1713
1656
  }
@@ -1720,7 +1663,6 @@ let blockItemLogIndex = 16777216;
1720
1663
  let maxPendingChunksPerPartition = 10;
1721
1664
 
1722
1665
  export {
1723
- deriveEffectiveStartBlock,
1724
1666
  deriveContractNameByAddress,
1725
1667
  defaultEstResponseSize,
1726
1668
  calculateEstResponseSize,
@@ -1729,9 +1671,9 @@ export {
1729
1671
  OptimizedPartitions,
1730
1672
  bufferBlockNumber,
1731
1673
  bufferBlock,
1674
+ bufferReadyCount,
1732
1675
  compareBufferItem,
1733
1676
  blockItemLogIndex,
1734
- numAddresses,
1735
1677
  appendOnBlockItems,
1736
1678
  updateInternal,
1737
1679
  warnDifferentContractType,
@@ -1748,7 +1690,6 @@ export {
1748
1690
  getReadyItemsCount,
1749
1691
  make$1 as make,
1750
1692
  bufferSize,
1751
- bufferReadyCount,
1752
1693
  rollbackPendingQueries,
1753
1694
  rollback,
1754
1695
  resetPendingQueries,
@@ -0,0 +1,105 @@
1
+ type indexingAddress = Internal.indexingContract
2
+
3
+ type contractConfig = {startBlock: option<int>}
4
+
5
+ type t = dict<indexingAddress>
6
+
7
+ let deriveEffectiveStartBlock = (~registrationBlock: int, ~contractStartBlock: option<int>) => {
8
+ Pervasives.max(Pervasives.max(registrationBlock, 0), contractStartBlock->Option.getOr(0))
9
+ }
10
+
11
+ let makeContractConfigs = (~eventConfigs: array<Internal.eventConfig>): dict<contractConfig> => {
12
+ let contractConfigs: dict<contractConfig> = Dict.make()
13
+ eventConfigs->Array.forEach(ec => {
14
+ switch contractConfigs->Utils.Dict.dangerouslyGetNonOption(ec.contractName) {
15
+ | Some({startBlock}) =>
16
+ contractConfigs->Dict.set(
17
+ ec.contractName,
18
+ {
19
+ startBlock: switch (startBlock, ec.startBlock) {
20
+ | (Some(a), Some(b)) => Some(Pervasives.min(a, b))
21
+ | (Some(_) as s, None) | (None, Some(_) as s) => s
22
+ | (None, None) => None
23
+ },
24
+ },
25
+ )
26
+ | None =>
27
+ contractConfigs->Dict.set(
28
+ ec.contractName,
29
+ {
30
+ startBlock: ec.startBlock,
31
+ },
32
+ )
33
+ }
34
+ })
35
+ contractConfigs
36
+ }
37
+
38
+ let makeIndexingAddress = (
39
+ ~contract: Internal.indexingAddress,
40
+ ~contractConfigs: dict<contractConfig>,
41
+ ): indexingAddress => {
42
+ let contractStartBlock = switch contractConfigs->Utils.Dict.dangerouslyGetNonOption(
43
+ contract.contractName,
44
+ ) {
45
+ | Some({startBlock}) => startBlock
46
+ | None => None
47
+ }
48
+ {
49
+ address: contract.address,
50
+ contractName: contract.contractName,
51
+ registrationBlock: contract.registrationBlock,
52
+ effectiveStartBlock: deriveEffectiveStartBlock(
53
+ ~registrationBlock=contract.registrationBlock,
54
+ ~contractStartBlock,
55
+ ),
56
+ }
57
+ }
58
+
59
+ let make = (
60
+ ~contractConfigs: dict<contractConfig>,
61
+ ~addresses: array<Internal.indexingAddress>,
62
+ ): t => {
63
+ let indexingAddresses = Dict.make()
64
+ addresses->Array.forEach(contract => {
65
+ indexingAddresses->Dict.set(
66
+ contract.address->Address.toString,
67
+ makeIndexingAddress(~contract, ~contractConfigs),
68
+ )
69
+ })
70
+ indexingAddresses
71
+ }
72
+
73
+ let get = (indexingAddresses: t, address) =>
74
+ indexingAddresses->Utils.Dict.dangerouslyGetNonOption(address)
75
+
76
+ let size = (indexingAddresses: t) => indexingAddresses->Utils.Dict.size
77
+
78
+ let getContractAddresses = (indexingAddresses: t, ~contractName): array<Address.t> => {
79
+ let addresses = []
80
+ indexingAddresses->Utils.Dict.forEach(ia => {
81
+ if ia.contractName === contractName {
82
+ addresses->Array.push(ia.address)
83
+ }
84
+ })
85
+ addresses
86
+ }
87
+
88
+ // Underlying dict for the precompiled `clientAddressFilter` only — it does raw
89
+ // `indexingAddresses[srcAddress]` access in generated JS and can't take the opaque
90
+ // type. Don't reach for this elsewhere; use the domain accessors above.
91
+ let rawForFilter = (indexingAddresses: t): dict<indexingAddress> => indexingAddresses
92
+
93
+ let register = (indexingAddresses: t, additions: dict<indexingAddress>) => {
94
+ let _ = Utils.Dict.mergeInPlace(indexingAddresses, additions)
95
+ }
96
+
97
+ let rollbackInPlace = (indexingAddresses: t, ~targetBlockNumber: int): unit => {
98
+ // forEachWithKey is a `for..in`, so deleting the key currently being visited is
99
+ // safe — it doesn't affect enumeration of the remaining keys.
100
+ indexingAddresses->Utils.Dict.forEachWithKey((indexingContract, address) => {
101
+ if indexingContract.registrationBlock > targetBlockNumber {
102
+ indexingAddresses->Utils.Dict.deleteInPlace(address)
103
+ }
104
+ })
105
+ }