|
| 1 | +open Belt |
| 2 | +open RescriptMocha |
| 3 | + |
| 4 | +describe("PartitionedFetchState getMostBehindPartitions", () => { |
| 5 | + let mockPartitionedFetchState = (~partitions, ~maxAddrInPartition=1): PartitionedFetchState.t => { |
| 6 | + { |
| 7 | + partitions, |
| 8 | + maxAddrInPartition, |
| 9 | + startBlock: 0, |
| 10 | + endBlock: None, |
| 11 | + logger: Logging.logger, |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + it("Partition id never changes when adding new partitions", () => { |
| 16 | + let rootContractAddressMapping = ContractAddressingMap.make() |
| 17 | + |
| 18 | + for i in 0 to 3 { |
| 19 | + let address = TestHelpers.Addresses.mockAddresses[i]->Option.getExn |
| 20 | + rootContractAddressMapping->ContractAddressingMap.addAddress(~address, ~name="MockContract") |
| 21 | + } |
| 22 | + |
| 23 | + let rootRegister: FetchState.register = { |
| 24 | + registerType: RootRegister({endBlock: None}), |
| 25 | + latestFetchedBlock: { |
| 26 | + blockNumber: 100, |
| 27 | + blockTimestamp: 100 * 15, |
| 28 | + }, |
| 29 | + contractAddressMapping: rootContractAddressMapping, |
| 30 | + fetchedEventQueue: [], |
| 31 | + dynamicContracts: FetchState.DynamicContractsMap.empty, |
| 32 | + firstEventBlockNumber: None, |
| 33 | + } |
| 34 | + |
| 35 | + let dynamicContractId: FetchState.dynamicContractId = { |
| 36 | + blockNumber: 10, |
| 37 | + logIndex: 0, |
| 38 | + } |
| 39 | + |
| 40 | + let baseRegister: FetchState.register = { |
| 41 | + registerType: DynamicContractRegister({ |
| 42 | + id: dynamicContractId, |
| 43 | + nextRegister: rootRegister, |
| 44 | + }), |
| 45 | + latestFetchedBlock: { |
| 46 | + blockNumber: dynamicContractId.blockNumber, |
| 47 | + blockTimestamp: dynamicContractId.blockNumber * 15, |
| 48 | + }, |
| 49 | + contractAddressMapping: ContractAddressingMap.make(), |
| 50 | + fetchedEventQueue: [], |
| 51 | + dynamicContracts: FetchState.DynamicContractsMap.empty, |
| 52 | + firstEventBlockNumber: None, |
| 53 | + } |
| 54 | + |
| 55 | + let fetchState0: FetchState.t = { |
| 56 | + baseRegister, |
| 57 | + isFetchingAtHead: false, |
| 58 | + pendingDynamicContracts: [], |
| 59 | + } |
| 60 | + |
| 61 | + let maxAddrInPartition = 4 |
| 62 | + |
| 63 | + let partitionedFetchState = mockPartitionedFetchState( |
| 64 | + ~partitions=[fetchState0], |
| 65 | + ~maxAddrInPartition, |
| 66 | + ) |
| 67 | + let id = { |
| 68 | + PartitionedFetchState.partitionId: 0, |
| 69 | + fetchStateId: DynamicContract(dynamicContractId), |
| 70 | + } |
| 71 | + |
| 72 | + //Check the expected query if requsted in this state |
| 73 | + Assert.deepEqual( |
| 74 | + partitionedFetchState.partitions->PartitionedFetchState.getReadyPartitions( |
| 75 | + ~maxPerChainQueueSize=10, |
| 76 | + ~fetchingPartitions=Utils.Set.make(), |
| 77 | + ), |
| 78 | + [ |
| 79 | + { |
| 80 | + partitionId: 0, |
| 81 | + fetchState: fetchState0, |
| 82 | + }, |
| 83 | + ], |
| 84 | + ~message="Should have only one partition with id 0", |
| 85 | + ) |
| 86 | + |
| 87 | + let updatedPartitionedFetchState = |
| 88 | + partitionedFetchState->PartitionedFetchState.registerDynamicContracts( |
| 89 | + { |
| 90 | + registeringEventChain: ChainMap.Chain.makeUnsafe(~chainId=1), |
| 91 | + registeringEventBlockNumber: 10, |
| 92 | + registeringEventLogIndex: 0, |
| 93 | + dynamicContracts: [ |
| 94 | + { |
| 95 | + id: ContextEnv.makeDynamicContractId( |
| 96 | + ~chainId=1, |
| 97 | + ~contractAddress=TestHelpers.Addresses.mockAddresses[5]->Option.getExn, |
| 98 | + ), |
| 99 | + chainId: 1, |
| 100 | + registeringEventBlockTimestamp: 10 * 15, |
| 101 | + registeringEventBlockNumber: 10, |
| 102 | + registeringEventLogIndex: 0, |
| 103 | + registeringEventContractName: "MockFactory", |
| 104 | + registeringEventName: "MockCreateGravatar", |
| 105 | + registeringEventSrcAddress: TestHelpers.Addresses.mockAddresses[0]->Option.getExn, |
| 106 | + contractAddress: TestHelpers.Addresses.mockAddresses[5]->Option.getExn, |
| 107 | + contractType: Enums.ContractType.Gravatar, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ~isFetchingAtHead=false, |
| 112 | + ) |
| 113 | + |
| 114 | + Assert.equal( |
| 115 | + updatedPartitionedFetchState.partitions->Array.length, |
| 116 | + 2, |
| 117 | + ~message="Should have added a new partition since it's over the maxAddrInPartition threshold", |
| 118 | + ) |
| 119 | + |
| 120 | + Assert.deepEqual( |
| 121 | + updatedPartitionedFetchState.partitions->PartitionedFetchState.getReadyPartitions( |
| 122 | + ~maxPerChainQueueSize=1000, |
| 123 | + ~fetchingPartitions=Utils.Set.make(), |
| 124 | + ), |
| 125 | + [ |
| 126 | + { |
| 127 | + partitionId: 0, |
| 128 | + fetchState: fetchState0, |
| 129 | + }, |
| 130 | + { |
| 131 | + partitionId: 1, |
| 132 | + fetchState: { |
| 133 | + baseRegister: { |
| 134 | + registerType: RootRegister({endBlock: None}), |
| 135 | + latestFetchedBlock: {blockNumber: 0, blockTimestamp: 0}, |
| 136 | + contractAddressMapping: ContractAddressingMap.fromArray([ |
| 137 | + (TestHelpers.Addresses.mockAddresses[5]->Option.getExn, "Gravatar"), |
| 138 | + ]), |
| 139 | + fetchedEventQueue: [], |
| 140 | + dynamicContracts: FetchState.DynamicContractsMap.empty->FetchState.DynamicContractsMap.addAddress( |
| 141 | + { |
| 142 | + blockNumber: 10, |
| 143 | + logIndex: 0, |
| 144 | + }, |
| 145 | + TestHelpers.Addresses.mockAddresses[5]->Option.getExn, |
| 146 | + ), |
| 147 | + firstEventBlockNumber: None, |
| 148 | + }, |
| 149 | + pendingDynamicContracts: [], |
| 150 | + isFetchingAtHead: false, |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + ~message="Should have a new partition with id 1", |
| 155 | + ) |
| 156 | + |
| 157 | + //Check that the original partition is available at it's id |
| 158 | + //and the new partition has not overwritten it |
| 159 | + switch updatedPartitionedFetchState->PartitionedFetchState.update( |
| 160 | + ~id, |
| 161 | + ~currentBlockHeight=200, |
| 162 | + ~latestFetchedBlock={blockNumber: 20, blockTimestamp: 20 * 15}, |
| 163 | + ~newItems=[], |
| 164 | + ) { |
| 165 | + | Ok(_) => () |
| 166 | + | Error(PartitionedFetchState.UnexpectedPartitionDoesNotExist(_)) => |
| 167 | + Assert.fail("Partition should exist") |
| 168 | + | Error(FetchState.UnexpectedRegisterDoesNotExist(_)) => |
| 169 | + Assert.fail("Dynamic contract register should exist") |
| 170 | + | _ => Assert.fail("Unexpected error") |
| 171 | + } |
| 172 | + }) |
| 173 | +}) |
0 commit comments