Skip to content

Commit

Permalink
Decouple pow from ethash
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Toulme <[email protected]>
  • Loading branch information
atoulme committed Feb 5, 2021
1 parent 055f7ce commit 243c733
Show file tree
Hide file tree
Showing 73 changed files with 633 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.chainimport.internal.BlockData;
import org.hyperledger.besu.chainimport.internal.ChainData;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.PowAlgorithm;
import org.hyperledger.besu.controller.BesuController;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.core.Address;
Expand Down Expand Up @@ -119,7 +120,7 @@ private void setOptionalFields(
final BlockData blockData,
final GenesisConfigOptions genesisConfig) {
// Some fields can only be configured for ethash
if (genesisConfig.isEthHash()) {
if (genesisConfig.getPowAlgorithm() != PowAlgorithm.NONE) {
// For simplicity only set these for ethash. Other consensus algorithms use these fields for
// special purposes or ignore them
miner.setCoinbase(blockData.getCoinbase().orElse(Address.ZERO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.cli.config.EthNetworkConfig;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.PowAlgorithm;
import org.hyperledger.besu.crypto.NodeKey;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApi;
Expand Down Expand Up @@ -191,7 +192,7 @@ BesuControllerBuilder fromGenesisConfig(
genesisConfig.getConfigOptions(genesisConfigOverrides);
final BesuControllerBuilder builder;

if (configOptions.isEthHash()) {
if (configOptions.getPowAlgorithm() != PowAlgorithm.NONE) {
builder = new MainnetBesuControllerBuilder();
} else if (configOptions.isIbft2()) {
builder = new IbftBesuControllerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.blockcreation.DefaultBlockScheduler;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMinerExecutor;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMinerExecutor;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
Expand All @@ -42,8 +42,9 @@ protected MiningCoordinator createMiningCoordinator(
final MiningParameters miningParameters,
final SyncState syncState,
final EthProtocolManager ethProtocolManager) {
final EthHashMinerExecutor executor =
new EthHashMinerExecutor(

final PoWMinerExecutor executor =
new PoWMinerExecutor(
protocolContext,
protocolSchedule,
transactionPool.getPendingTransactions(),
Expand All @@ -55,8 +56,8 @@ protected MiningCoordinator createMiningCoordinator(
gasLimitCalculator,
epochCalculator);

final EthHashMiningCoordinator miningCoordinator =
new EthHashMiningCoordinator(
final PoWMiningCoordinator miningCoordinator =
new PoWMiningCoordinator(
protocolContext.getBlockchain(),
executor,
syncState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,11 @@ public interface GenesisConfigOptions {
* @return block number to activate Quorum Permissioning
*/
OptionalLong getQip714BlockNumber();

/**
* The PoW algorithm associated with the genesis file.
*
* @return the PoW algorithm in use.
*/
PowAlgorithm getPowAlgorithm();
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ public OptionalLong getQip714BlockNumber() {
return getOptionalLong("qip714block");
}

@Override
public PowAlgorithm getPowAlgorithm() {
return isEthHash() ? PowAlgorithm.ETHASH : PowAlgorithm.NONE;
}

@Override
public Map<String, Object> asMap() {
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
Expand Down
16 changes: 16 additions & 0 deletions config/src/main/java/org/hyperledger/besu/config/PowAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.hyperledger.besu.config;

/** An enumeration of supported Proof-of-work algorithms. */
public enum PowAlgorithm {
ETHASH,
NONE;

public static PowAlgorithm fromString(final String str) {
for (final PowAlgorithm powAlgorithm : PowAlgorithm.values()) {
if (powAlgorithm.name().equalsIgnoreCase(str)) {
return powAlgorithm;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public OptionalLong getQip714BlockNumber() {
return OptionalLong.empty();
}

@Override
public PowAlgorithm getPowAlgorithm() {
return isEthHash() ? PowAlgorithm.ETHASH : PowAlgorithm.NONE;
}

@Override
public List<Long> getForks() {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public static ProtocolSchedule create(
privacyParameters.getGoQuorumPrivacyParameters().isPresent()),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
config.isQuorum(),
config.getPowAlgorithm())
.createProtocolSchedule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockScheduler;
import org.hyperledger.besu.ethereum.blockcreation.AbstractMinerExecutor;
import org.hyperledger.besu.ethereum.blockcreation.GasLimitCalculator;
import org.hyperledger.besu.ethereum.chain.EthHashObserver;
import org.hyperledger.besu.ethereum.chain.MinedBlockObserver;
import org.hyperledger.besu.ethereum.chain.PoWObserver;
import org.hyperledger.besu.ethereum.core.Address;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.MiningParameters;
Expand Down Expand Up @@ -72,7 +72,7 @@ public CliqueMinerExecutor(
@Override
public CliqueBlockMiner createMiner(
final Subscribers<MinedBlockObserver> observers,
final Subscribers<EthHashObserver> ethHashObservers,
final Subscribers<PoWObserver> ethHashObservers,
final BlockHeader parentHeader) {
final Function<BlockHeader, CliqueBlockCreator> blockCreator =
(header) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public static ProtocolSchedule create(
builder -> applyBftChanges(config.getBftConfigOptions(), builder, config.isQuorum()),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
config.isQuorum(),
config.getPowAlgorithm())
.createProtocolSchedule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public static ProtocolSchedule create(
builder -> applyIbftChanges(blockPeriod, builder, config.isQuorum()),
privacyParameters,
isRevertReasonEnabled,
config.isQuorum())
config.isQuorum(),
config.getPowAlgorithm())
.createProtocolSchedule();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockImporter;
Expand Down Expand Up @@ -90,7 +90,7 @@ public Map<String, JsonRpcMethod> methods() {
final P2PNetwork peerDiscovery = mock(P2PNetwork.class);
final EthPeers ethPeers = mock(EthPeers.class);
final TransactionPool transactionPool = mock(TransactionPool.class);
final EthHashMiningCoordinator miningCoordinator = mock(EthHashMiningCoordinator.class);
final PoWMiningCoordinator miningCoordinator = mock(PoWMiningCoordinator.class);
final ObservableMetricsSystem metricsSystem = new NoOpMetricsSystem();
final Optional<AccountLocalConfigPermissioningController> accountWhitelistController =
Optional.of(mock(AccountLocalConfigPermissioningController.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.mainnet.DirectAcyclicGraphSeed;
import org.hyperledger.besu.ethereum.mainnet.EpochCalculator;
import org.hyperledger.besu.ethereum.mainnet.EthHashSolverInputs;
import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs;

import java.util.Optional;

Expand All @@ -42,8 +42,8 @@ public class EthGetWork implements JsonRpcMethod {

public EthGetWork(final MiningCoordinator miner) {
this.miner = miner;
if (miner instanceof EthHashMiningCoordinator) {
this.epochCalculator = ((EthHashMiningCoordinator) miner).getEpochCalculator();
if (miner instanceof PoWMiningCoordinator) {
this.epochCalculator = ((PoWMiningCoordinator) miner).getEpochCalculator();
} else {
this.epochCalculator = new EpochCalculator.DefaultEpochCalculator();
}
Expand All @@ -56,9 +56,9 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Optional<EthHashSolverInputs> solver = miner.getWorkDefinition();
final Optional<PoWSolverInputs> solver = miner.getWorkDefinition();
if (solver.isPresent()) {
final EthHashSolverInputs rawResult = solver.get();
final PoWSolverInputs rawResult = solver.get();
final byte[] dagSeed =
DirectAcyclicGraphSeed.dagSeed(rawResult.getBlockNumber(), epochCalculator);
final String[] result = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.mainnet.EthHashSolution;
import org.hyperledger.besu.ethereum.mainnet.EthHashSolverInputs;
import org.hyperledger.besu.ethereum.mainnet.PoWSolution;
import org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs;

import java.util.Optional;

Expand All @@ -48,10 +48,10 @@ public String getName() {

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Optional<EthHashSolverInputs> solver = miner.getWorkDefinition();
final Optional<PoWSolverInputs> solver = miner.getWorkDefinition();
if (solver.isPresent()) {
final EthHashSolution solution =
new EthHashSolution(
final PoWSolution solution =
new PoWSolution(
Bytes.fromHexString(requestContext.getRequiredParameter(0, String.class)).getLong(0),
requestContext.getRequiredParameter(2, Hash.class),
Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.Block;
Expand Down Expand Up @@ -123,8 +123,7 @@ public void setupTest() throws Exception {
final SyncStatus status = new DefaultSyncStatus(1, 2, 3, Optional.of(4L), Optional.of(5L));
Mockito.when(synchronizerMock.getSyncStatus()).thenReturn(Optional.of(status));

final EthHashMiningCoordinator miningCoordinatorMock =
Mockito.mock(EthHashMiningCoordinator.class);
final PoWMiningCoordinator miningCoordinatorMock = Mockito.mock(PoWMiningCoordinator.class);
Mockito.when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(Wei.of(16));

final TransactionPool transactionPoolMock = Mockito.mock(TransactionPool.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.hyperledger.besu.ethereum.api.graphql;

import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
Expand Down Expand Up @@ -210,8 +210,7 @@ private GraphQLHttpService createGraphQLHttpServiceWithAllowedDomains(
final BlockchainQueries blockchainQueries = Mockito.mock(BlockchainQueries.class);
final Synchronizer synchronizer = Mockito.mock(Synchronizer.class);

final EthHashMiningCoordinator miningCoordinatorMock =
Mockito.mock(EthHashMiningCoordinator.class);
final PoWMiningCoordinator miningCoordinatorMock = Mockito.mock(PoWMiningCoordinator.class);

final GraphQLDataFetcherContextImpl dataFetcherContext =
Mockito.mock(GraphQLDataFetcherContextImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.hyperledger.besu.ethereum.api.graphql;

import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
Expand Down Expand Up @@ -71,8 +71,7 @@ private GraphQLHttpService createGraphQLHttpService() throws Exception {
final BlockchainQueries blockchainQueries = Mockito.mock(BlockchainQueries.class);
final Synchronizer synchronizer = Mockito.mock(Synchronizer.class);

final EthHashMiningCoordinator miningCoordinatorMock =
Mockito.mock(EthHashMiningCoordinator.class);
final PoWMiningCoordinator miningCoordinatorMock = Mockito.mock(PoWMiningCoordinator.class);

final GraphQLDataFetcherContextImpl dataFetcherContext =
Mockito.mock(GraphQLDataFetcherContextImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.hyperledger.besu.ethereum.api.query.BlockWithMetadata;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.Hash;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.core.Wei;
Expand Down Expand Up @@ -70,7 +70,7 @@ public class GraphQLHttpServiceTest {
private static BlockchainQueries blockchainQueries;
private static GraphQL graphQL;
private static GraphQLDataFetcherContextImpl dataFetcherContext;
private static EthHashMiningCoordinator miningCoordinatorMock;
private static PoWMiningCoordinator miningCoordinatorMock;

private final GraphQLTestHelper testHelper = new GraphQLTestHelper();

Expand All @@ -80,7 +80,7 @@ public static void initServerAndClient() throws Exception {
final Synchronizer synchronizer = Mockito.mock(Synchronizer.class);
graphQL = Mockito.mock(GraphQL.class);

miningCoordinatorMock = Mockito.mock(EthHashMiningCoordinator.class);
miningCoordinatorMock = Mockito.mock(PoWMiningCoordinator.class);

dataFetcherContext = Mockito.mock(GraphQLDataFetcherContextImpl.class);
Mockito.when(dataFetcherContext.getBlockchainQueries()).thenReturn(blockchainQueries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
Expand Down Expand Up @@ -117,7 +117,7 @@ protected Map<String, JsonRpcMethod> getRpcMethods(
final Synchronizer synchronizerMock = mock(Synchronizer.class);
final P2PNetwork peerDiscoveryMock = mock(P2PNetwork.class);
final TransactionPool transactionPoolMock = mock(TransactionPool.class);
final EthHashMiningCoordinator miningCoordinatorMock = mock(EthHashMiningCoordinator.class);
final PoWMiningCoordinator miningCoordinatorMock = mock(PoWMiningCoordinator.class);
when(transactionPoolMock.addLocalTransaction(any(Transaction.class)))
.thenReturn(ValidationResult.valid());
// nonce too low tests uses a tx with nonce=16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void initServerAndClient() throws Exception {
new StubGenesisConfigOptions().constantinopleBlock(0).chainId(CHAIN_ID)),
mock(FilterManager.class),
mock(TransactionPool.class),
mock(EthHashMiningCoordinator.class),
mock(PoWMiningCoordinator.class),
new NoOpMetricsSystem(),
supportedCapabilities,
Optional.of(mock(AccountLocalConfigPermissioningController.class)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory;
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.blockcreation.EthHashMiningCoordinator;
import org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
Expand Down Expand Up @@ -134,7 +134,7 @@ public static void initServerAndClient() throws Exception {
MainnetProtocolSchedule.fromConfig(genesisConfigOptions),
mock(FilterManager.class),
mock(TransactionPool.class),
mock(EthHashMiningCoordinator.class),
mock(PoWMiningCoordinator.class),
new NoOpMetricsSystem(),
supportedCapabilities,
Optional.empty(),
Expand Down
Loading

0 comments on commit 243c733

Please sign in to comment.