Extract common functions from EthHasBlockMiner (#8)

The EthHashBlockMiner contains both the generic mining capabilities
and the EthHash JSON specific API.

The generic capabilities have been extracted into a base class which
can be reused for Clique (or other) consensus mechanisms.
tmohay 6 years ago committed by GitHub
parent db7acb06cd
commit 2549f5ee6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      ethereum/core/src/main/java/net/consensys/pantheon/ethereum/blockcreation/BlockMiner.java
  2. 5
      ethereum/core/src/main/java/net/consensys/pantheon/ethereum/blockcreation/EthHashBlockMiner.java

@ -24,19 +24,20 @@ import org.apache.logging.log4j.Logger;
* <p>This class is responsible for mining a single block only - the AbstractBlockCreator maintains * <p>This class is responsible for mining a single block only - the AbstractBlockCreator maintains
* state so must be destroyed between block mining activities. * state so must be destroyed between block mining activities.
*/ */
public class BlockMiner<C> implements Runnable { public class BlockMiner<C, M extends AbstractBlockCreator<C>> implements Runnable {
private static final Logger LOG = LogManager.getLogger(); private static final Logger LOG = LogManager.getLogger();
private final AbstractBlockCreator<C> blockCreator; protected final M blockCreator;
private final ProtocolContext<C> protocolContext; protected final ProtocolContext<C> protocolContext;
protected final BlockHeader parentHeader;
private final ProtocolSchedule<C> protocolSchedule; private final ProtocolSchedule<C> protocolSchedule;
private final Subscribers<MinedBlockObserver> observers; private final Subscribers<MinedBlockObserver> observers;
private final AbstractBlockScheduler scheduler; private final AbstractBlockScheduler scheduler;
private final BlockHeader parentHeader;
public BlockMiner( public BlockMiner(
final AbstractBlockCreator<C> blockCreator, final M blockCreator,
final ProtocolSchedule<C> protocolSchedule, final ProtocolSchedule<C> protocolSchedule,
final ProtocolContext<C> protocolContext, final ProtocolContext<C> protocolContext,
final Subscribers<MinedBlockObserver> observers, final Subscribers<MinedBlockObserver> observers,
@ -67,13 +68,13 @@ public class BlockMiner<C> implements Runnable {
} }
} }
private boolean mineBlock() throws InterruptedException { protected boolean mineBlock() throws InterruptedException {
// Ensure the block is allowed to be mined - i.e. the timestamp on the new block is sufficiently // Ensure the block is allowed to be mined - i.e. the timestamp on the new block is sufficiently
// ahead of the parent, and still within allowable clock tolerance. // ahead of the parent, and still within allowable clock tolerance.
LOG.trace("Waiting for next block timestamp to be valid."); LOG.trace("Started a mining operation.");
long newBlockTimestamp = scheduler.waitUntilNextBlockCanBeMined(parentHeader); long newBlockTimestamp = scheduler.waitUntilNextBlockCanBeMined(parentHeader);
LOG.trace("Started a mining operation."); LOG.trace("Mining a new block with timestamp {}", newBlockTimestamp);
Block block = blockCreator.createBlock(newBlockTimestamp); Block block = blockCreator.createBlock(newBlockTimestamp);
LOG.info( LOG.info(
"Block created, importing to local chain, block includes {} transactions", "Block created, importing to local chain, block includes {} transactions",

@ -18,9 +18,7 @@ import java.util.Optional;
* <p>All other aspects of mining (i.e. pre-block delays, block creation and importing to the chain) * <p>All other aspects of mining (i.e. pre-block delays, block creation and importing to the chain)
* are all conducted by the parent class. * are all conducted by the parent class.
*/ */
public class EthHashBlockMiner extends BlockMiner<Void> { public class EthHashBlockMiner extends BlockMiner<Void, EthHashBlockCreator> {
private final EthHashBlockCreator blockCreator;
public EthHashBlockMiner( public EthHashBlockMiner(
final EthHashBlockCreator blockCreator, final EthHashBlockCreator blockCreator,
@ -30,7 +28,6 @@ public class EthHashBlockMiner extends BlockMiner<Void> {
final AbstractBlockScheduler scheduler, final AbstractBlockScheduler scheduler,
final BlockHeader parentHeader) { final BlockHeader parentHeader) {
super(blockCreator, protocolSchedule, protocolContext, observers, scheduler, parentHeader); super(blockCreator, protocolSchedule, protocolContext, observers, scheduler, parentHeader);
this.blockCreator = blockCreator;
} }
public Optional<EthHashSolverInputs> getWorkDefinition() { public Optional<EthHashSolverInputs> getWorkDefinition() {

Loading…
Cancel
Save