mirror of https://github.com/hyperledger/besu
Extract common components from MiningCoordinator (#21)
The MiningCoordinator has been split into common, and ethhash specific funcationality. This adversely affects the JSON RPC, in that all mining related RPCs are now generic based on the type of miner being used. Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
02eb60fa47
commit
76a50ee180
@ -0,0 +1,25 @@ |
|||||||
|
package net.consensys.pantheon.consensus.ibft.blockcreation; |
||||||
|
|
||||||
|
import net.consensys.pantheon.consensus.ibft.IbftContext; |
||||||
|
import net.consensys.pantheon.ethereum.ProtocolContext; |
||||||
|
import net.consensys.pantheon.ethereum.blockcreation.AbstractBlockScheduler; |
||||||
|
import net.consensys.pantheon.ethereum.blockcreation.AbstractMiningCoordinator.MinedBlockObserver; |
||||||
|
import net.consensys.pantheon.ethereum.blockcreation.BlockMiner; |
||||||
|
import net.consensys.pantheon.ethereum.core.BlockHeader; |
||||||
|
import net.consensys.pantheon.ethereum.mainnet.ProtocolSchedule; |
||||||
|
import net.consensys.pantheon.util.Subscribers; |
||||||
|
|
||||||
|
public class IbftBlockMiner extends BlockMiner<IbftContext, IbftBlockCreator> { |
||||||
|
|
||||||
|
// TODO(tmm): Currently a place holder to allow infrastructure code to continue to operate
|
||||||
|
// with the advent of multiple consensus methods.
|
||||||
|
public IbftBlockMiner( |
||||||
|
final IbftBlockCreator blockCreator, |
||||||
|
final ProtocolSchedule<IbftContext> protocolSchedule, |
||||||
|
final ProtocolContext<IbftContext> protocolContext, |
||||||
|
final Subscribers<MinedBlockObserver> observers, |
||||||
|
final AbstractBlockScheduler scheduler, |
||||||
|
final BlockHeader parentHeader) { |
||||||
|
super(blockCreator, protocolSchedule, protocolContext, observers, scheduler, parentHeader); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,83 @@ |
|||||||
|
package net.consensys.pantheon.ethereum.blockcreation; |
||||||
|
|
||||||
|
import net.consensys.pantheon.ethereum.chain.BlockAddedObserver; |
||||||
|
import net.consensys.pantheon.ethereum.chain.Blockchain; |
||||||
|
import net.consensys.pantheon.ethereum.core.Address; |
||||||
|
import net.consensys.pantheon.ethereum.mainnet.EthHashSolution; |
||||||
|
import net.consensys.pantheon.ethereum.mainnet.EthHashSolverInputs; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
/** |
||||||
|
* Responsible for determining when a block mining operation should be started/stopped, then |
||||||
|
* creating an appropriate miner and starting it running in a thread. |
||||||
|
*/ |
||||||
|
public class EthHashMiningCoordinator extends AbstractMiningCoordinator<Void, EthHashBlockMiner> |
||||||
|
implements BlockAddedObserver { |
||||||
|
|
||||||
|
private final EthHashMinerExecutor executor; |
||||||
|
private volatile Optional<Long> cachedHashesPerSecond = Optional.empty(); |
||||||
|
|
||||||
|
public EthHashMiningCoordinator( |
||||||
|
final Blockchain blockchain, final EthHashMinerExecutor executor) { |
||||||
|
super(blockchain, executor); |
||||||
|
this.executor = executor; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void enable() { |
||||||
|
synchronized (this) { |
||||||
|
if (isEnabled) { |
||||||
|
return; |
||||||
|
} |
||||||
|
startAsyncMiningOperation(); |
||||||
|
isEnabled = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void disable() { |
||||||
|
synchronized (this) { |
||||||
|
if (!isEnabled) { |
||||||
|
return; |
||||||
|
} |
||||||
|
haltCurrentMiningOperation(); |
||||||
|
isEnabled = false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setCoinbase(final Address coinbase) { |
||||||
|
executor.setCoinbase(coinbase); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Optional<Address> getCoinbase() { |
||||||
|
return executor.getCoinbase(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Optional<Long> hashesPerSecond() { |
||||||
|
final Optional<Long> currentHashesPerSecond = |
||||||
|
currentRunningMiner.flatMap(EthHashBlockMiner::getHashesPerSecond); |
||||||
|
|
||||||
|
if (currentHashesPerSecond.isPresent()) { |
||||||
|
cachedHashesPerSecond = currentHashesPerSecond; |
||||||
|
return currentHashesPerSecond; |
||||||
|
} else { |
||||||
|
return cachedHashesPerSecond; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Optional<EthHashSolverInputs> getWorkDefinition() { |
||||||
|
return currentRunningMiner.flatMap(EthHashBlockMiner::getWorkDefinition); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean submitWork(final EthHashSolution solution) { |
||||||
|
synchronized (this) { |
||||||
|
return currentRunningMiner.map(miner -> miner.submitWork(solution)).orElse(false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue