Search in sources :

Example 1 with BadBlockManager

use of org.hyperledger.besu.ethereum.chain.BadBlockManager in project besu by hyperledger.

the class DebugStandardTraceBadBlockToFile method response.

@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
    final Hash blockHash = requestContext.getRequiredParameter(0, Hash.class);
    final Optional<TransactionTraceParams> transactionTraceParams = requestContext.getOptionalParameter(1, TransactionTraceParams.class);
    final Blockchain blockchain = blockchainQueries.get().getBlockchain();
    final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockchain.getChainHeadHeader().getNumber());
    final BadBlockManager badBlockManager = protocolSpec.getBadBlocksManager();
    return badBlockManager.getBadBlock(blockHash).map(block -> (JsonRpcResponse) new JsonRpcSuccessResponse(requestContext.getRequest().getId(), traceBlock(block, transactionTraceParams))).orElse(new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.BLOCK_NOT_FOUND));
}
Also used : JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) TransactionTracer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) Supplier(java.util.function.Supplier) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Optional(java.util.Optional) TransactionTraceParams(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Path(java.nio.file.Path) Hash(org.hyperledger.besu.datatypes.Hash) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) TransactionTraceParams(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) 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 BadBlockManager

use of org.hyperledger.besu.ethereum.chain.BadBlockManager in project besu by hyperledger.

the class DebugStandardTraceBadBlockToFileTest method shouldTraceTheTransactionUsingTheTransactionTracer.

@SuppressWarnings("rawtypes")
@Test
public void shouldTraceTheTransactionUsingTheTransactionTracer() {
    final BlockDataGenerator blockGenerator = new BlockDataGenerator();
    final Block genesis = blockGenerator.genesisBlock();
    final Block block = blockGenerator.block(new BlockDataGenerator.BlockOptions().setParentHash(genesis.getHeader().getHash()));
    final Object[] params = new Object[] { block.getHash(), new HashMap<>() };
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_standardTraceBadBlockToFile", params));
    final List<String> paths = new ArrayList<>();
    paths.add("path-1");
    final BadBlockManager badBlockManager = new BadBlockManager();
    badBlockManager.addBadBlock(block);
    final BlockHeader blockHeader = new BlockHeaderTestFixture().buildHeader();
    when(protocolSpec.getBadBlocksManager()).thenReturn(badBlockManager);
    when(blockchainQueries.getBlockchain()).thenReturn(blockchain);
    when(blockchain.getChainHeadHeader()).thenReturn(new BlockHeaderTestFixture().buildHeader());
    when(protocolSchedule.getByBlockNumber(blockHeader.getNumber())).thenReturn(protocolSpec);
    when(transactionTracer.traceTransactionToFile(eq(block.getHash()), any(), any())).thenReturn(paths);
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) debugStandardTraceBadBlockToFile.response(request);
    final List result = (ArrayList) response.getResult();
    assertThat(result.size()).isEqualTo(1);
}
Also used : JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) Block(org.hyperledger.besu.ethereum.core.Block) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) ArrayList(java.util.ArrayList) List(java.util.List) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 3 with BadBlockManager

use of org.hyperledger.besu.ethereum.chain.BadBlockManager in project besu by hyperledger.

the class TransactionTracerTest method setUp.

@Before
public void setUp() throws Exception {
    transactionTracer = new TransactionTracer(new BlockReplay(protocolSchedule, blockchain, worldStateArchive));
    when(transaction.getHash()).thenReturn(transactionHash);
    when(otherTransaction.getHash()).thenReturn(otherTransactionHash);
    when(blockHeader.getNumber()).thenReturn(12L);
    when(blockHeader.getHash()).thenReturn(blockHash);
    when(blockHeader.getParentHash()).thenReturn(previousBlockHash);
    when(previousBlockHeader.getStateRoot()).thenReturn(Hash.ZERO);
    when(worldStateArchive.getMutable(Hash.ZERO, null, false)).thenReturn(Optional.of(mutableWorldState));
    when(protocolSchedule.getByBlockNumber(12)).thenReturn(protocolSpec);
    when(protocolSpec.getTransactionProcessor()).thenReturn(transactionProcessor);
    when(protocolSpec.getMiningBeneficiaryCalculator()).thenReturn(BlockHeader::getCoinbase);
    when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
    when(protocolSpec.getBadBlocksManager()).thenReturn(new BadBlockManager());
}
Also used : BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Before(org.junit.Before)

Example 4 with BadBlockManager

use of org.hyperledger.besu.ethereum.chain.BadBlockManager in project besu by hyperledger.

the class AbstractBlockPropagationManagerTest method shouldDetectAndCacheInvalidBlocks.

@SuppressWarnings("unchecked")
@Test
public void shouldDetectAndCacheInvalidBlocks() {
    final EthScheduler ethScheduler = mock(EthScheduler.class);
    when(ethScheduler.scheduleSyncWorkerTask(any(Supplier.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            return invocation.getArgument(0, Supplier.class).get();
        }
    });
    final EthContext ethContext = new EthContext(new EthPeers("eth", TestClock.fixed(), metricsSystem, 25), new EthMessages(), ethScheduler);
    final BlockPropagationManager blockPropagationManager = new BlockPropagationManager(syncConfig, protocolSchedule, protocolContext, ethContext, syncState, pendingBlocksManager, metricsSystem, blockBroadcaster);
    blockchainUtil.importFirstBlocks(2);
    final Block firstBlock = blockchainUtil.getBlock(1);
    final BadBlockManager badBlocksManager = protocolSchedule.getByBlockNumber(1).getBadBlocksManager();
    final Block badBlock = new BlockDataGenerator().block(BlockDataGenerator.BlockOptions.create().setBlockNumber(1).setParentHash(firstBlock.getHash()).setBlockHeaderFunctions(new MainnetBlockHeaderFunctions()));
    assertThat(badBlocksManager.getBadBlocks()).isEmpty();
    blockPropagationManager.importOrSavePendingBlock(badBlock, NODE_ID_1);
    assertThat(badBlocksManager.getBadBlocks().size()).isEqualTo(1);
    verify(ethScheduler, times(1)).scheduleSyncWorkerTask(any(Supplier.class));
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Block(org.hyperledger.besu.ethereum.core.Block) Supplier(java.util.function.Supplier) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) Test(org.junit.Test)

Example 5 with BadBlockManager

use of org.hyperledger.besu.ethereum.chain.BadBlockManager in project besu by hyperledger.

the class DownloadHeaderSequenceTask method processHeaders.

private CompletableFuture<List<BlockHeader>> processHeaders(final PeerTaskResult<List<BlockHeader>> headersResult) {
    return executeWorkerSubTask(ethContext.getScheduler(), () -> {
        final CompletableFuture<List<BlockHeader>> future = new CompletableFuture<>();
        BlockHeader child = null;
        boolean firstSkipped = false;
        final int previousHeaderIndex = lastFilledHeaderIndex;
        for (final BlockHeader header : headersResult.getResult()) {
            final int headerIndex = Ints.checkedCast(segmentLength - (referenceHeader.getNumber() - header.getNumber()));
            if (!firstSkipped) {
                // Skip over reference header
                firstSkipped = true;
                continue;
            }
            if (child == null) {
                child = (headerIndex == segmentLength - 1) ? referenceHeader : headers[headerIndex + 1];
            }
            final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(child.getNumber());
            final BadBlockManager badBlockManager = protocolSpec.getBadBlocksManager();
            if (!validateHeader(child, header)) {
                // Invalid headers - disconnect from peer
                final BlockHeader invalidBlock = child;
                // even though the header is known bad we are downloading the block body for the
                // debug_badBlocks RPC
                final AbstractPeerTask<Block> getBlockTask = GetBlockFromPeerTask.create(protocolSchedule, ethContext, Optional.of(child.getHash()), child.getNumber(), metricsSystem).assignPeer(headersResult.getPeer());
                getBlockTask.run().whenComplete((blockPeerTaskResult, error) -> {
                    if (error == null && blockPeerTaskResult.getResult() != null) {
                        badBlockManager.addBadBlock(blockPeerTaskResult.getResult());
                    }
                    headersResult.getPeer().disconnect(DisconnectReason.BREACH_OF_PROTOCOL);
                    LOG.debug("Received invalid headers from peer, disconnecting from: {}", headersResult.getPeer());
                    future.completeExceptionally(new InvalidBlockException("Header failed validation.", invalidBlock.getNumber(), invalidBlock.getHash()));
                });
                return future;
            }
            headers[headerIndex] = header;
            lastFilledHeaderIndex = headerIndex;
            child = header;
        }
        future.complete(asList(headers).subList(lastFilledHeaderIndex, previousHeaderIndex));
        return future;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InvalidBlockException(org.hyperledger.besu.ethereum.eth.sync.tasks.exceptions.InvalidBlockException) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Block(org.hyperledger.besu.ethereum.core.Block) Arrays.asList(java.util.Arrays.asList) List(java.util.List) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

BadBlockManager (org.hyperledger.besu.ethereum.chain.BadBlockManager)7 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)4 Block (org.hyperledger.besu.ethereum.core.Block)3 List (java.util.List)2 Supplier (java.util.function.Supplier)2 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Hash (org.hyperledger.besu.datatypes.Hash)1 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)1 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)1