Search in sources :

Example 1 with RejectedRequestException

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);
}
Also used : Eth1RequestException(tech.pegasys.teku.pow.exception.Eth1RequestException) RejectedRequestException(tech.pegasys.teku.pow.exception.RejectedRequestException) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BigInteger(java.math.BigInteger) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) DepositContract(tech.pegasys.teku.pow.contract.DepositContract) Test(org.junit.jupiter.api.Test)

Aggregations

BigInteger (java.math.BigInteger)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1 DepositContract (tech.pegasys.teku.pow.contract.DepositContract)1 Eth1RequestException (tech.pegasys.teku.pow.exception.Eth1RequestException)1 RejectedRequestException (tech.pegasys.teku.pow.exception.RejectedRequestException)1