Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Fix get signerMetrics when there are fewer blocks in the network than…
Browse files Browse the repository at this point in the history
… the default range (#1725)
  • Loading branch information
matkt authored and ajsutton committed Jul 21, 2019
1 parent b81c838 commit ad4b5d4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
package tech.pegasys.pantheon.consensus.clique.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.AdditionalMatchers.lt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.consensus.common.BlockInterface;
Expand Down Expand Up @@ -141,6 +144,30 @@ public void getSignerMetrics() {
.containsExactlyInAnyOrderElementsOf(signerMetricResultList);
}

@Test
@SuppressWarnings("unchecked")
public void getSignerMetricsWhenThereAreFewerBlocksThanTheDefaultRange() {
final long startBlock = 0L;
final long headBlock = 2L;

final List<SignerMetricResult> signerMetricResultList = new ArrayList<>();

when(blockchainQueries.headBlockNumber()).thenReturn(headBlock);

LongStream.range(startBlock, headBlock)
.forEach(value -> signerMetricResultList.add(generateBlock(value)));

final JsonRpcRequest request = requestWithParams();

final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);

// verify getBlockHeaderByNumber is not called with negative values
verify(blockchainQueries, never()).getBlockHeaderByNumber(lt(0L));

assertThat((Collection<SignerMetricResult>) response.getResult())
.containsExactlyInAnyOrderElementsOf(signerMetricResultList);
}

@Test
@SuppressWarnings("unchecked")
public void getSignerMetricsWithLatest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public JsonRpcResponse response(final JsonRpcRequest request) {
private long getFromBlockNumber(final Optional<BlockParameter> startBlockParameter) {
return startBlockParameter
.map(this::resolveBlockNumber)
.orElseGet(() -> blockchainQueries.headBlockNumber() - DEFAULT_RANGE_BLOCK);
.orElseGet(() -> Math.max(0, blockchainQueries.headBlockNumber() - DEFAULT_RANGE_BLOCK));
}

private long getEndBlockNumber(final Optional<BlockParameter> endBlockParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
package tech.pegasys.pantheon.consensus.ibft.jsonrpc.methods;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.AdditionalMatchers.lt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.consensus.common.BlockInterface;
Expand Down Expand Up @@ -141,6 +144,30 @@ public void getSignerMetrics() {
.containsExactlyInAnyOrderElementsOf(signerMetricResultList);
}

@Test
@SuppressWarnings("unchecked")
public void getSignerMetricsWhenThereAreFewerBlocksThanTheDefaultRange() {
final long startBlock = 0L;
final long headBlock = 2L;

final List<SignerMetricResult> signerMetricResultList = new ArrayList<>();

when(blockchainQueries.headBlockNumber()).thenReturn(headBlock);

LongStream.range(startBlock, headBlock)
.forEach(value -> signerMetricResultList.add(generateBlock(value)));

final JsonRpcRequest request = requestWithParams();

final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request);

// verify getBlockHeaderByNumber is not called with negative values
verify(blockchainQueries, never()).getBlockHeaderByNumber(lt(0L));

assertThat((Collection<SignerMetricResult>) response.getResult())
.containsExactlyInAnyOrderElementsOf(signerMetricResultList);
}

@Test
@SuppressWarnings("unchecked")
public void getSignerMetricsWithLatest() {
Expand Down

0 comments on commit ad4b5d4

Please sign in to comment.