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

[PAN-1247] Implement eth_submitHashrate JSON RPC API #1726

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public boolean submitWork(final EthHashSolution solution) {
return nonceSolver.submitSolution(solution);
}

public boolean submitHashesPerSecond(final String remoteMinerId, final long hashrate) {
return nonceSolver.submitHashesPerSecond(remoteMinerId, hashrate);
}

@Override
public void cancel() {
super.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public Optional<Long> getHashesPerSecond() {
public boolean submitWork(final EthHashSolution solution) {
return blockCreator.submitWork(solution);
}

public boolean submitHashesPerSecond(final String remoteMinerId, final long hashrate) {
return blockCreator.submitHashesPerSecond(remoteMinerId, hashrate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public boolean submitWork(final EthHashSolution solution) {
}
}

@Override
public boolean submitHashesPerSecond(final String remoteMinerId, final long hashrate) {
synchronized (this) {
return currentRunningMiner
.map(miner -> miner.submitHashesPerSecond(remoteMinerId, hashrate))
.orElse(false);
}
}

@Override
protected void haltCurrentMiningOperation() {
currentRunningMiner.ifPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ default Optional<Long> hashesPerSecond() {
"Current consensus mechanism prevents querying of hashrate.");
}

default boolean submitHashesPerSecond(final String remoteMinerId, final long hashrate) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents submitting hashrate.");
}

default Optional<EthHashSolverInputs> getWorkDefinition() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying work definition.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import tech.pegasys.pantheon.util.uint.UInt256;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.base.Stopwatch;

Expand Down Expand Up @@ -72,6 +75,7 @@ public EthHashSolution getSolution() throws InterruptedException, ExecutionExcep
private final Iterable<Long> nonceGenerator;
private final EthHasher ethHasher;
private volatile long hashesPerSecond = NO_MINING_CONDUCTED;
private final Map<String, Long> hashPerSecondOfRemoteMiners = new ConcurrentHashMap<>();

private volatile Optional<EthHashSolverJob> currentJob = Optional.empty();

Expand Down Expand Up @@ -129,10 +133,16 @@ public Optional<EthHashSolverInputs> getWorkDefinition() {
}

public Optional<Long> hashesPerSecond() {
if (hashesPerSecond == NO_MINING_CONDUCTED) {
if (hashesPerSecond == NO_MINING_CONDUCTED && hashPerSecondOfRemoteMiners.isEmpty()) {
return Optional.empty();
}
return Optional.of(hashesPerSecond);

AtomicLong totalHashrate = new AtomicLong(hashesPerSecond);
// add remote miners hashes per second if present
totalHashrate.addAndGet(
hashPerSecondOfRemoteMiners.values().stream().mapToLong(Long::longValue).sum());

return Optional.of(totalHashrate.longValue());
}

public boolean submitSolution(final EthHashSolution solution) {
Expand All @@ -156,4 +166,9 @@ public boolean submitSolution(final EthHashSolution solution) {
}
return false;
}

public boolean submitHashesPerSecond(final String remoteMinerId, final long hashesPerSecond) {
hashPerSecondOfRemoteMiners.put(remoteMinerId, hashesPerSecond);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public enum RpcMethod {
ETH_GET_UNCLE_COUNT_BY_BLOCK_NUMBER("eth_getUncleCountByBlockNumber"),
ETH_GET_WORK("eth_getWork"),
ETH_HASHRATE("eth_hashrate"),
ETH_SUBMIT_HASHRATE("eth_submitHashrate"),
ETH_MINING("eth_mining"),
ETH_NEW_BLOCK_FILTER("eth_newBlockFilter"),
ETH_NEW_FILTER("eth_newFilter"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

public class EthSubmitHashrate implements JsonRpcMethod {

private final MiningCoordinator miningCoordinator;
private final JsonRpcParameter parameters;

public EthSubmitHashrate(
final MiningCoordinator miningCoordinator, final JsonRpcParameter parameters) {
this.miningCoordinator = miningCoordinator;
this.parameters = parameters;
}

@Override
public String getName() {
return RpcMethod.ETH_SUBMIT_HASHRATE.getMethodName();
}

@Override
public JsonRpcResponse response(final JsonRpcRequest req) {
try {

if (req.getParamLength() != 2) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS);
}

final long hashrate = parameters.required(req.getParams(), 0, long.class);
final String remoteMinerId = parameters.required(req.getParams(), 1, String.class);

return new JsonRpcSuccessResponse(
req.getId(), miningCoordinator.submitHashesPerSecond(remoteMinerId, hashrate));
} catch (final UnsupportedOperationException ex) {
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_REQUEST);
}
}
}