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