use of org.hyperledger.besu.ethereum.mainnet.PoWSolution in project besu by hyperledger.
the class ProofOfWorkValidationRuleTest method failsWithNonEip1559BlockAfterFork.
@Test
public void failsWithNonEip1559BlockAfterFork() {
final ProofOfWorkValidationRule proofOfWorkValidationRule = new ProofOfWorkValidationRule(new EpochCalculator.DefaultEpochCalculator(), PoWHasher.ETHASH_LIGHT, Optional.of(FeeMarket.london(0L)));
final BlockHeaderBuilder headerBuilder = BlockHeaderBuilder.fromHeader(blockHeader).difficulty(Difficulty.ONE).blockHeaderFunctions(mainnetBlockHashFunction()).timestamp(1);
final BlockHeader preHeader = headerBuilder.buildBlockHeader();
final Hash headerHash = validationRule.hashHeader(preHeader);
PoWSolution solution = PoWHasher.ETHASH_LIGHT.hash(preHeader.getNonce(), preHeader.getNumber(), new EpochCalculator.DefaultEpochCalculator(), headerHash);
final BlockHeader header = headerBuilder.mixHash(solution.getMixHash()).buildBlockHeader();
assertThat(proofOfWorkValidationRule.validate(header, parentHeader)).isFalse();
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolution in project besu by hyperledger.
the class EthSubmitWork method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Optional<PoWSolverInputs> solver = miner.getWorkDefinition();
if (solver.isPresent()) {
final PoWSolution solution = new PoWSolution(Bytes.fromHexString(requestContext.getRequiredParameter(0, String.class)).getLong(0), requestContext.getRequiredParameter(2, Hash.class), null, Bytes.fromHexString(requestContext.getRequiredParameter(1, String.class)));
final boolean result = miner.submitWork(solution);
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), result);
} else {
LOG.trace("Mining is not operational, eth_submitWork request cannot be processed");
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.NO_MINING_WORK_FOUND);
}
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolution in project besu by hyperledger.
the class PoWMiningCoordinatorTest method miningCoordinatorIsCreatedDisabledWithNoReportableMiningStatistics.
@Test
public void miningCoordinatorIsCreatedDisabledWithNoReportableMiningStatistics() {
final PoWMiningCoordinator miningCoordinator = new PoWMiningCoordinator(executionContext.getBlockchain(), executor, syncState, DEFAULT_REMOTE_SEALERS_LIMIT, DEFAULT_REMOTE_SEALERS_TTL);
final PoWSolution solution = new PoWSolution(1L, Hash.EMPTY, null, Bytes32.ZERO);
assertThat(miningCoordinator.isMining()).isFalse();
assertThat(miningCoordinator.hashesPerSecond()).isEqualTo(Optional.empty());
assertThat(miningCoordinator.getWorkDefinition()).isEqualTo(Optional.empty());
assertThat(miningCoordinator.submitWork(solution)).isFalse();
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolution in project besu by hyperledger.
the class PoWBlockCreator method createFinalBlockHeader.
@Override
protected BlockHeader createFinalBlockHeader(final SealableBlockHeader sealableBlockHeader) {
final PoWSolverInputs workDefinition = generateNonceSolverInputs(sealableBlockHeader);
final PoWSolution solution;
try {
solution = nonceSolver.solveFor(PoWSolver.PoWSolverJob.createFromInputs(workDefinition));
} catch (final InterruptedException ex) {
throw new CancellationException();
} catch (final ExecutionException ex) {
throw new RuntimeException("Failure occurred during nonce calculations.", ex);
}
return BlockHeaderBuilder.create().populateFrom(sealableBlockHeader).mixHash(solution.getMixHash()).nonce(solution.getNonce()).blockHeaderFunctions(blockHeaderFunctions).buildBlockHeader();
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolution in project besu by hyperledger.
the class GetWorkProtocol method handle.
@Override
public void handle(final StratumConnection conn, final String message) {
JsonNode jsonrpcMessage = readMessage(message);
if (jsonrpcMessage == null) {
LOG.warn("Invalid message {}", message);
conn.close();
return;
}
JsonNode methodNode = jsonrpcMessage.get("method");
JsonNode idNode = jsonrpcMessage.get("id");
if (methodNode == null || idNode == null) {
LOG.warn("Invalid message {}", message);
conn.close();
return;
}
String method = methodNode.textValue();
if ("eth_getWork".equals(method)) {
JsonRpcSuccessResponse response = new JsonRpcSuccessResponse(idNode.intValue(), getWorkResult);
try {
String responseMessage = mapper.writeValueAsString(response);
conn.send("HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nKeep-Alive: timeout=5, max=1000\r\nContent-Length: " + responseMessage.length() + CRLF + CRLF + responseMessage);
} catch (JsonProcessingException e) {
LOG.warn("Error sending work", e);
conn.close();
}
} else if ("eth_submitWork".equals(method)) {
JsonNode paramsNode = jsonrpcMessage.get("params");
if (paramsNode == null || paramsNode.size() != 3) {
LOG.warn("Invalid eth_submitWork params {}", message);
conn.close();
return;
}
final PoWSolution solution = new PoWSolution(Bytes.fromHexString(paramsNode.get(0).textValue()).getLong(0), Hash.fromHexString(paramsNode.get(2).textValue()), null, Bytes.fromHexString(paramsNode.get(1).textValue()));
final boolean result = submitCallback.apply(solution);
try {
String resultMessage = mapper.writeValueAsString(new JsonRpcSuccessResponse(idNode.intValue(), result));
conn.send("HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nKeep-Alive: timeout=5, max=1000\r\nContent-Length: " + resultMessage.length() + CRLF + CRLF + resultMessage);
} catch (JsonProcessingException e) {
LOG.warn("Error accepting solution work", e);
conn.close();
}
} else {
LOG.warn("Unknown method {}", method);
conn.close();
}
}
Aggregations