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

Add MiningCoordinator interface #168

Merged
merged 1 commit into from
Oct 26, 2018
Merged
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 @@ -50,7 +50,7 @@ public void startNode(final PantheonNode node) {
final PantheonControllerBuilder builder = new PantheonControllerBuilder();
final EthNetworkConfig ethNetworkConfig =
new EthNetworkConfig.Builder(mainnet()).setNetworkId(NETWORK_ID).build();
final PantheonController<?, ?> pantheonController;
final PantheonController<?> pantheonController;
try {
pantheonController =
builder.build(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public abstract class AbstractMiningCoordinator<
C, M extends BlockMiner<C, ? extends AbstractBlockCreator<C>>>
implements BlockAddedObserver {
implements BlockAddedObserver, MiningCoordinator {
private static final Logger LOG = getLogger();
protected boolean isEnabled = false;
protected volatile Optional<M> currentRunningMiner = Optional.empty();
Expand All @@ -54,6 +54,7 @@ public AbstractMiningCoordinator(
syncState.addInSyncListener(this::inSyncChanged);
}

@Override
public void enable() {
synchronized (this) {
if (isEnabled) {
Expand All @@ -66,6 +67,7 @@ public void enable() {
}
}

@Override
public void disable() {
synchronized (this) {
if (!isEnabled) {
Expand All @@ -76,6 +78,7 @@ public void disable() {
}
}

@Override
public boolean isRunning() {
synchronized (this) {
return currentRunningMiner.isPresent();
Expand Down Expand Up @@ -121,38 +124,46 @@ public void addMinedBlockObserver(final MinedBlockObserver obs) {
}

// Required for JSON RPC, and are deemed to be valid for all mining mechanisms
@Override
public void setMinTransactionGasPrice(final Wei minGasPrice) {
executor.setMinTransactionGasPrice(minGasPrice);
}

@Override
public Wei getMinTransactionGasPrice() {
return executor.getMinTransactionGasPrice();
}

@Override
public void setExtraData(final BytesValue extraData) {
executor.setExtraData(extraData);
}

@Override
public void setCoinbase(final Address coinbase) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents setting coinbase.");
}

@Override
public Optional<Address> getCoinbase() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of coinbase.");
}

@Override
public Optional<Long> hashesPerSecond() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying of hashrate.");
}

@Override
public Optional<EthHashSolverInputs> getWorkDefinition() {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents querying work definition.");
}

@Override
public boolean submitWork(final EthHashSolution solution) {
throw new UnsupportedOperationException(
"Current consensus mechanism prevents submission of work solutions.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.blockcreation;

import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolution;
import tech.pegasys.pantheon.ethereum.mainnet.EthHashSolverInputs;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.Optional;

public interface MiningCoordinator {

void enable();

void disable();

boolean isRunning();

// Required for JSON RPC, and are deemed to be valid for all mining mechanisms
void setMinTransactionGasPrice(Wei minGasPrice);

Wei getMinTransactionGasPrice();

void setExtraData(BytesValue extraData);

void setCoinbase(Address coinbase);

Optional<Address> getCoinbase();

Optional<Long> hashesPerSecond();

Optional<EthHashSolverInputs> getWorkDefinition();

boolean submitWork(EthHashSolution solution);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.chain.Blockchain;
import tech.pegasys.pantheon.ethereum.core.Synchronizer;
import tech.pegasys.pantheon.ethereum.core.TransactionPool;
Expand Down Expand Up @@ -94,7 +94,7 @@ public Map<String, JsonRpcMethod> methods(
final Synchronizer synchronizer,
final TransactionPool transactionPool,
final ProtocolSchedule<?> protocolSchedule,
final AbstractMiningCoordinator<?, ?> miningCoordinator,
final MiningCoordinator miningCoordinator,
final Set<Capability> supportedCapabilities,
final Collection<RpcApi> rpcApis,
final FilterManager filterManager) {
Expand Down Expand Up @@ -123,7 +123,7 @@ public Map<String, JsonRpcMethod> methods(
final ProtocolSchedule<?> protocolSchedule,
final FilterManager filterManager,
final TransactionPool transactionPool,
final AbstractMiningCoordinator<?, ?> miningCoordinator,
final MiningCoordinator miningCoordinator,
final Set<Capability> supportedCapabilities,
final Collection<RpcApi> rpcApis) {
final Map<String, JsonRpcMethod> enabledMethods = new HashMap<>();
Expand Down Expand Up @@ -174,10 +174,10 @@ public Map<String, JsonRpcMethod> methods(
blockchainQueries.getWorldStateArchive(),
protocolSchedule),
parameter),
new EthMining<>(miningCoordinator),
new EthMining(miningCoordinator),
new EthCoinbase(miningCoordinator),
new EthProtocolVersion(supportedCapabilities),
new EthGasPrice<>(miningCoordinator),
new EthGasPrice(miningCoordinator),
new EthGetWork(miningCoordinator));
}
if (rpcApis.contains(RpcApis.DEBUG)) {
Expand Down Expand Up @@ -207,8 +207,8 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
final MinerSetCoinbase minerSetCoinbase = new MinerSetCoinbase(miningCoordinator, parameter);
addMethods(
enabledMethods,
new MinerStart<>(miningCoordinator),
new MinerStop<>(miningCoordinator),
new MinerStart(miningCoordinator),
new MinerStop(miningCoordinator),
minerSetCoinbase,
new MinerSetEtherbase(minerSetCoinbase));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
Expand All @@ -24,9 +24,9 @@

public class EthCoinbase implements JsonRpcMethod {

private final AbstractMiningCoordinator<?, ?> miningCoordinator;
private final MiningCoordinator miningCoordinator;

public EthCoinbase(final AbstractMiningCoordinator<?, ?> miningCoordinator) {
public EthCoinbase(final MiningCoordinator miningCoordinator) {
this.miningCoordinator = miningCoordinator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractBlockCreator;
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.BlockMiner;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.Quantity;

public class EthGasPrice<C, M extends BlockMiner<C, ? extends AbstractBlockCreator<C>>>
implements JsonRpcMethod {
public class EthGasPrice implements JsonRpcMethod {

private final AbstractMiningCoordinator<C, M> miningCoordinator;
private final MiningCoordinator miningCoordinator;

public EthGasPrice(final AbstractMiningCoordinator<C, M> miningCoordinator) {
public EthGasPrice(final MiningCoordinator miningCoordinator) {
this.miningCoordinator = miningCoordinator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand All @@ -30,10 +30,10 @@

public class EthGetWork implements JsonRpcMethod {

private final AbstractMiningCoordinator<?, ?> miner;
private final MiningCoordinator miner;
private static final Logger LOG = getLogger();

public EthGetWork(final AbstractMiningCoordinator<?, ?> miner) {
public EthGetWork(final MiningCoordinator miner) {
this.miner = miner;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractBlockCreator;
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.BlockMiner;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

public class EthMining<C, M extends BlockMiner<C, ? extends AbstractBlockCreator<C>>>
implements JsonRpcMethod {
public class EthMining implements JsonRpcMethod {

private final AbstractMiningCoordinator<C, M> miningCoordinator;
private final MiningCoordinator miningCoordinator;

public EthMining(final AbstractMiningCoordinator<C, M> miningCoordinator) {
public EthMining(final MiningCoordinator miningCoordinator) {
this.miningCoordinator = miningCoordinator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.miner;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
Expand All @@ -24,11 +24,11 @@

public class MinerSetCoinbase implements JsonRpcMethod {

private final AbstractMiningCoordinator<?, ?> miningCoordinator;
private final MiningCoordinator miningCoordinator;
private final JsonRpcParameter parameters;

public MinerSetCoinbase(
final AbstractMiningCoordinator<?, ?> miningCoordinator, final JsonRpcParameter parameters) {
final MiningCoordinator miningCoordinator, final JsonRpcParameter parameters) {
this.miningCoordinator = miningCoordinator;
this.parameters = parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,20 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.miner;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractBlockCreator;
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.BlockMiner;
import tech.pegasys.pantheon.ethereum.blockcreation.CoinbaseNotSetException;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
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 MinerStart<C, M extends BlockMiner<C, ? extends AbstractBlockCreator<C>>>
implements JsonRpcMethod {
public class MinerStart implements JsonRpcMethod {

private final AbstractMiningCoordinator<C, M> miningCoordinator;
private final MiningCoordinator miningCoordinator;

public MinerStart(final AbstractMiningCoordinator<C, M> miningCoordinator) {
public MinerStart(final MiningCoordinator miningCoordinator) {
this.miningCoordinator = miningCoordinator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.miner;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractBlockCreator;
import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.BlockMiner;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

public class MinerStop<C, M extends BlockMiner<C, ? extends AbstractBlockCreator<C>>>
implements JsonRpcMethod {
public class MinerStop implements JsonRpcMethod {

private final AbstractMiningCoordinator<C, M> miningCoordinator;
private final MiningCoordinator miningCoordinator;

public MinerStop(final AbstractMiningCoordinator<C, M> miningCoordinator) {
public MinerStop(final MiningCoordinator miningCoordinator) {
this.miningCoordinator = miningCoordinator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator;
import tech.pegasys.pantheon.ethereum.blockcreation.MiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
Expand All @@ -36,7 +36,7 @@
@RunWith(MockitoJUnitRunner.class)
public class EthCoinbaseTest {

@Mock private AbstractMiningCoordinator<?, ?> miningCoordinator;
@Mock private MiningCoordinator miningCoordinator;
private EthCoinbase method;
private final String JSON_RPC_VERSION = "2.0";
private final String ETH_METHOD = "eth_coinbase";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.ethereum.blockcreation.EthHashBlockMiner;
import tech.pegasys.pantheon.ethereum.blockcreation.EthHashMiningCoordinator;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
Expand All @@ -34,13 +33,13 @@
public class EthGasPriceTest {

@Mock private EthHashMiningCoordinator miningCoordinator;
private EthGasPrice<Void, EthHashBlockMiner> method;
private EthGasPrice method;
private final String JSON_RPC_VERSION = "2.0";
private final String ETH_METHOD = "eth_gasPrice";

@Before
public void setUp() {
method = new EthGasPrice<>(miningCoordinator);
method = new EthGasPrice(miningCoordinator);
}

@Test
Expand Down
Loading