Search in sources :

Example 1 with PoWSolverInputs

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);
    }
}
Also used : PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) PoWSolverInputs(org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs) Hash(org.hyperledger.besu.datatypes.Hash) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Example 2 with PoWSolverInputs

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);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) PoWSolverInputs(org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs) Test(org.junit.Test)

Example 3 with PoWSolverInputs

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);
}
Also used : JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) PoWSolverInputs(org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 4 with PoWSolverInputs

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();
}
Also used : CancellationException(java.util.concurrent.CancellationException) PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) PoWSolverInputs(org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with PoWSolverInputs

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());
}
Also used : Logger(org.slf4j.Logger) PoWSolverInputs(org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs) BesuMetricCategory(org.hyperledger.besu.metrics.BesuMetricCategory) EthHash(org.hyperledger.besu.ethereum.mainnet.EthHash) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) PoWObserver(org.hyperledger.besu.ethereum.chain.PoWObserver) Function(java.util.function.Function) AtomicLong(java.util.concurrent.atomic.AtomicLong) NetServerOptions(io.vertx.core.net.NetServerOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) Optional(java.util.Optional) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator) BigInteger(java.math.BigInteger) UInt256(org.apache.tuweni.units.bigints.UInt256) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) Counter(org.hyperledger.besu.plugin.services.metrics.Counter) NetSocket(io.vertx.core.net.NetSocket) UInt256(org.apache.tuweni.units.bigints.UInt256)

Aggregations

PoWSolverInputs (org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs)15 Test (org.junit.Test)10 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)6 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)5 PoWSolution (org.hyperledger.besu.ethereum.mainnet.PoWSolution)5 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 UInt256 (org.apache.tuweni.units.bigints.UInt256)3 BigInteger (java.math.BigInteger)2 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)2 MiningCoordinator (org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator)2 StubMetricsSystem (org.hyperledger.besu.metrics.StubMetricsSystem)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 NetServer (io.vertx.core.net.NetServer)1 NetServerOptions (io.vertx.core.net.NetServerOptions)1 NetSocket (io.vertx.core.net.NetSocket)1 Optional (java.util.Optional)1