use of tech.pegasys.teku.pow.exception.RejectedRequestException in project teku by ConsenSys.
the class DepositsFetcherTest method shouldReduceBatchSizeWhenRequestIsRejected.
@Test
void shouldReduceBatchSizeWhenRequestIsRejected() {
final BigInteger fromBlockNumber = BigInteger.ZERO;
final BigInteger toBlockNumber = BigInteger.valueOf(MAX_BLOCK_RANGE + 100);
final SafeFuture<List<DepositContract.DepositEventEventResponse>> request1Response = new SafeFuture<>();
final SafeFuture<List<DepositContract.DepositEventEventResponse>> request2Response = new SafeFuture<>();
final SafeFuture<List<DepositContract.DepositEventEventResponse>> request3Response = new SafeFuture<>();
when(depositEventsAccessor.depositEventInRange(any(), any())).thenReturn(request1Response).thenReturn(request2Response).thenReturn(request3Response);
final SafeFuture<Void> result = depositFetcher.fetchDepositsInRange(fromBlockNumber, toBlockNumber);
assertThat(result).isNotDone();
// First tries to request a full size batch
verify(depositEventsAccessor).depositEventInRange(refEq(DefaultBlockParameter.valueOf(fromBlockNumber)), refEq(DefaultBlockParameter.valueOf(BigInteger.valueOf(MAX_BLOCK_RANGE))));
verifyNoMoreInteractions(depositEventsAccessor);
// But there are too many results
final Eth1RequestException err = new Eth1RequestException();
err.addSuppressed(new RejectedRequestException(-32005, "Nah mate"));
request1Response.completeExceptionally(err);
// So it halves the batch size and retries
asyncRunner.executeQueuedActions();
final BigInteger endSuccessfulRange = fromBlockNumber.add(BigInteger.valueOf(MAX_BLOCK_RANGE / 2));
verify(depositEventsAccessor).depositEventInRange(refEq(DefaultBlockParameter.valueOf(fromBlockNumber)), refEq(DefaultBlockParameter.valueOf(endSuccessfulRange)));
verifyNoMoreInteractions(depositEventsAccessor);
// And that works
request2Response.complete(emptyList());
// So it increases the batch size by 10% to avoid getting stuck with a very small batch size
asyncRunner.executeQueuedActions();
verify(depositEventsAccessor).depositEventInRange(refEq(DefaultBlockParameter.valueOf(endSuccessfulRange.add(BigInteger.ONE))), refEq(DefaultBlockParameter.valueOf(toBlockNumber)));
verifyNoMoreInteractions(depositEventsAccessor);
}
Aggregations