Return hashrate of 0 when not mining. (#1672)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Rob Dawson 5 years ago committed by GitHub
parent 15bc6e243d
commit d0e69a2433
  1. 5
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthHashrate.java
  2. 5
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthHashrateTest.java

@ -40,10 +40,7 @@ public class EthHashrate implements JsonRpcMethod {
public JsonRpcResponse response(final JsonRpcRequest req) { public JsonRpcResponse response(final JsonRpcRequest req) {
try { try {
final Optional<Long> hashesPerSecond = miningCoordinator.hashesPerSecond(); final Optional<Long> hashesPerSecond = miningCoordinator.hashesPerSecond();
if (hashesPerSecond.isPresent()) { return new JsonRpcSuccessResponse(req.getId(), Quantity.create(hashesPerSecond.orElse(0L)));
return new JsonRpcSuccessResponse(req.getId(), Quantity.create(hashesPerSecond.get()));
}
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.NO_HASHES_PER_SECOND);
} catch (final UnsupportedOperationException ex) { } catch (final UnsupportedOperationException ex) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_REQUEST); return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_REQUEST);
} }

@ -59,10 +59,9 @@ public class EthHashrateTest {
} }
@Test @Test
public void shouldReturnErrorWhenMiningCoordinatorDoesNotHaveHashes() { public void shouldReturnZeroWhenMiningCoordinatorDoesNotHaveHashes() {
final JsonRpcRequest request = requestWithParams(); final JsonRpcRequest request = requestWithParams();
final JsonRpcResponse expectedResponse = final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getId(), "0x0");
new JsonRpcErrorResponse(request.getId(), JsonRpcError.NO_HASHES_PER_SECOND);
when(miningCoordinator.hashesPerSecond()).thenReturn(Optional.empty()); when(miningCoordinator.hashesPerSecond()).thenReturn(Optional.empty());
final JsonRpcResponse actualResponse = method.response(request); final JsonRpcResponse actualResponse = method.response(request);

Loading…
Cancel
Save