use of org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs 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.PoWSolverInputs in project besu by hyperledger.
the class EthSubmitWorkTest method shouldFailIfMissingArguments.
@Test
public void shouldFailIfMissingArguments() {
final JsonRpcRequestContext request = requestWithParams();
final PoWSolverInputs values = new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 0);
when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.of(values));
assertThatThrownBy(() -> method.response(request), "Missing required json rpc parameter at index 0").isInstanceOf(InvalidJsonRpcParameters.class);
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs in project besu by hyperledger.
the class EthGetWorkTest method shouldReturnCorrectResultOnHighBlockSeedEcip1099.
@Test
public void shouldReturnCorrectResultOnHighBlockSeedEcip1099() {
when(miningCoordinator.getEpochCalculator()).thenReturn(new EpochCalculator.Ecip1099EpochCalculator());
method = new EthGetWork(miningCoordinator);
final JsonRpcRequestContext request = requestWithParams();
final PoWSolverInputs values = new PoWSolverInputs(UInt256.fromHexString(hexValue), Bytes.fromHexString(hexValue), 60000);
final String[] expectedValue = { "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0x" + BaseEncoding.base16().lowerCase().encode(DirectAcyclicGraphSeed.dagSeed(60000, miningCoordinator.getEpochCalculator())), "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0xea60" };
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(request.getRequest().getId(), expectedValue);
when(miningCoordinator.getWorkDefinition()).thenReturn(Optional.of(values));
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).usingRecursiveComparison().isEqualTo(expectedResponse);
}
use of org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs 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.PoWSolverInputs in project besu by hyperledger.
the class StratumServer method newJob.
@Override
public void newJob(final PoWSolverInputs poWSolverInputs) {
if (!started.get()) {
logger.debug("Discarding {} as stratum server is not started", poWSolverInputs);
return;
}
logger.debug("stratum newJob with inputs: {}", poWSolverInputs);
for (StratumProtocol protocol : protocols) {
protocol.setCurrentWorkTask(poWSolverInputs);
}
// reverse the target calculation to get the difficulty
// and ensure we do not get divide by zero:
UInt256 difficulty = Optional.of(poWSolverInputs.getTarget().toUnsignedBigInteger()).filter(td -> td.compareTo(BigInteger.ONE) > 0).map(EthHash.TARGET_UPPER_BOUND::divide).map(UInt256::valueOf).orElse(UInt256.MAX_VALUE);
currentDifficulty.set(difficulty.toUnsignedBigInteger().doubleValue());
}
Aggregations