Search in sources :

Example 1 with PoWSolution

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();
}
Also used : EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeaderBuilder(org.hyperledger.besu.ethereum.core.BlockHeaderBuilder) Test(org.junit.Test)

Example 2 with PoWSolution

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);
    }
}
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 3 with PoWSolution

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();
}
Also used : PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) Test(org.junit.Test)

Example 4 with PoWSolution

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();
}
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 PoWSolution

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();
    }
}
Also used : PoWSolution(org.hyperledger.besu.ethereum.mainnet.PoWSolution) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

PoWSolution (org.hyperledger.besu.ethereum.mainnet.PoWSolution)12 Hash (org.hyperledger.besu.datatypes.Hash)6 Test (org.junit.Test)6 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)5 PoWSolverInputs (org.hyperledger.besu.ethereum.mainnet.PoWSolverInputs)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 BlockHeaderBuilder (org.hyperledger.besu.ethereum.core.BlockHeaderBuilder)3 EpochCalculator (org.hyperledger.besu.ethereum.mainnet.EpochCalculator)3 UInt256 (org.apache.tuweni.units.bigints.UInt256)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BigInteger (java.math.BigInteger)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Address (org.hyperledger.besu.datatypes.Address)1 Wei (org.hyperledger.besu.datatypes.Wei)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1