Search in sources :

Example 1 with BlockTrace

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace in project besu by hyperledger.

the class DebugTraceBlockByNumber method resultByBlockNumber.

@Override
protected Object resultByBlockNumber(final JsonRpcRequestContext request, final long blockNumber) {
    final Optional<Hash> blockHash = getBlockchainQueries().getBlockHashByNumber(blockNumber);
    final TraceOptions traceOptions = request.getOptionalParameter(1, TransactionTraceParams.class).map(TransactionTraceParams::traceOptions).orElse(TraceOptions.DEFAULT);
    return blockHash.flatMap(hash -> blockTracerSupplier.get().trace(hash, new DebugOperationTracer(traceOptions)).map(BlockTrace::getTransactionTraces).map(DebugTraceTransactionResult::of)).orElse(null);
}
Also used : TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) Supplier(java.util.function.Supplier) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) DebugTraceTransactionResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult) Optional(java.util.Optional) TransactionTraceParams(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams) Hash(org.hyperledger.besu.datatypes.Hash) BlockTracer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer) DebugTraceTransactionResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) Hash(org.hyperledger.besu.datatypes.Hash)

Example 2 with BlockTrace

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace in project besu by hyperledger.

the class DebugTraceBlockByHashTest method shouldReturnCorrectResponse.

@Test
public void shouldReturnCorrectResponse() {
    final Object[] params = new Object[] { blockHash };
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceBlockByHash", params));
    final TraceFrame traceFrame = new TraceFrame(12, Optional.of("NONE"), 45L, OptionalLong.of(56L), 0L, 2, Optional.empty(), null, Wei.ZERO, Bytes.EMPTY, Bytes.EMPTY, Optional.empty(), Optional.empty(), Optional.empty(), null, Optional.empty(), Optional.empty(), Optional.empty(), 0, Optional.empty(), false, Optional.empty(), Optional.empty());
    final TransactionProcessingResult transaction1Result = mock(TransactionProcessingResult.class);
    final TransactionProcessingResult transaction2Result = mock(TransactionProcessingResult.class);
    final TransactionTrace transaction1Trace = mock(TransactionTrace.class);
    final TransactionTrace transaction2Trace = mock(TransactionTrace.class);
    BlockTrace blockTrace = new BlockTrace(Arrays.asList(transaction1Trace, transaction2Trace));
    when(transaction1Trace.getTraceFrames()).thenReturn(Arrays.asList(traceFrame));
    when(transaction2Trace.getTraceFrames()).thenReturn(Arrays.asList(traceFrame));
    when(transaction1Trace.getResult()).thenReturn(transaction1Result);
    when(transaction2Trace.getResult()).thenReturn(transaction2Result);
    when(transaction1Result.getOutput()).thenReturn(Bytes.fromHexString("1234"));
    when(transaction2Result.getOutput()).thenReturn(Bytes.fromHexString("1234"));
    when(blockTracer.trace(eq(blockHash), any())).thenReturn(Optional.of(blockTrace));
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) debugTraceBlockByHash.response(request);
    final Collection<?> result = (Collection<?>) response.getResult();
    assertThat(result).hasSize(2);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Collection(java.util.Collection) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) TraceFrame(org.hyperledger.besu.ethereum.debug.TraceFrame) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) Test(org.junit.Test)

Example 3 with BlockTrace

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace in project besu by hyperledger.

the class DebugTraceBlockByNumberTest method shouldReturnCorrectResponse.

@Test
public void shouldReturnCorrectResponse() {
    final long blockNumber = 1L;
    final Object[] params = new Object[] { Long.toHexString(blockNumber) };
    final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_traceBlockByNumber", params));
    final TraceFrame traceFrame = new TraceFrame(12, Optional.of("NONE"), 45L, OptionalLong.of(56L), 0L, 2, Optional.empty(), null, Wei.ZERO, Bytes.EMPTY, Bytes.EMPTY, Optional.empty(), Optional.empty(), Optional.empty(), null, Optional.empty(), Optional.empty(), Optional.empty(), 0, Optional.empty(), false, Optional.empty(), Optional.empty());
    final TransactionProcessingResult transaction1Result = mock(TransactionProcessingResult.class);
    final TransactionProcessingResult transaction2Result = mock(TransactionProcessingResult.class);
    final TransactionTrace transaction1Trace = mock(TransactionTrace.class);
    final TransactionTrace transaction2Trace = mock(TransactionTrace.class);
    final BlockTrace blockTrace = new BlockTrace(asList(transaction1Trace, transaction2Trace));
    when(transaction1Trace.getTraceFrames()).thenReturn(singletonList(traceFrame));
    when(transaction2Trace.getTraceFrames()).thenReturn(singletonList(traceFrame));
    when(transaction1Trace.getResult()).thenReturn(transaction1Result);
    when(transaction2Trace.getResult()).thenReturn(transaction2Result);
    when(transaction1Result.getOutput()).thenReturn(Bytes.fromHexString("1234"));
    when(transaction2Result.getOutput()).thenReturn(Bytes.fromHexString("1234"));
    when(blockchain.getBlockHashByNumber(blockNumber)).thenReturn(Optional.of(blockHash));
    when(blockTracer.trace(eq(blockHash), any())).thenReturn(Optional.of(blockTrace));
    final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) debugTraceBlockByNumber.response(request);
    final Collection<DebugTraceTransactionResult> result = getResult(response);
    assertThat(result).usingFieldByFieldElementComparator().isEqualTo(DebugTraceTransactionResult.of(blockTrace.getTransactionTraces()));
}
Also used : DebugTraceTransactionResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) TraceFrame(org.hyperledger.besu.ethereum.debug.TraceFrame) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) Test(org.junit.Test)

Example 4 with BlockTrace

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace in project besu by hyperledger.

the class TraceReplayBlockTransactions method traceBlock.

private Object traceBlock(final Block block, final TraceTypeParameter traceTypeParameter) {
    if (block == null || block.getBody().getTransactions().isEmpty()) {
        return emptyResult();
    }
    final Set<TraceTypeParameter.TraceType> traceTypes = traceTypeParameter.getTraceTypes();
    final TraceOptions traceOptions = new TraceOptions(false, false, traceTypes.contains(VM_TRACE) || traceTypes.contains(TRACE));
    return blockTracerSupplier.get().trace(block, new DebugOperationTracer(traceOptions)).map(BlockTrace::getTransactionTraces).map((traces) -> generateTracesFromTransactionTrace(traces, block, traceTypes)).orElse(null);
}
Also used : TRACE(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType.TRACE) TraceType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType) TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) Arrays(java.util.Arrays) TraceTypeParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter) LoggerFactory(org.slf4j.LoggerFactory) TraceFormatter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.TraceFormatter) Supplier(java.util.function.Supplier) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BlockParameter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) Suppliers(com.google.common.base.Suppliers) JsonNode(com.fasterxml.jackson.databind.JsonNode) Block(org.hyperledger.besu.ethereum.core.Block) FlatTraceGenerator(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator) TraceWriter(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.TraceWriter) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Logger(org.slf4j.Logger) VmTraceGenerator(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.vm.VmTraceGenerator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) Optional(java.util.Optional) StateDiffGenerator(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.diff.StateDiffGenerator) VM_TRACE(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType.VM_TRACE) BlockTracer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer) TraceType(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TraceTypeParameter.TraceType) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace)

Example 5 with BlockTrace

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace in project besu by hyperledger.

the class DebugAccountAt method resultByBlockHash.

@Override
protected Object resultByBlockHash(final JsonRpcRequestContext requestContext, final Hash blockHash) {
    final Integer txIndex = requestContext.getRequiredParameter(1, Integer.class);
    final Address address = requestContext.getRequiredParameter(2, Address.class);
    Optional<BlockWithMetadata<TransactionWithMetadata, Hash>> block = blockchainQueries.get().blockByHash(blockHash);
    if (block.isEmpty()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.BLOCK_NOT_FOUND);
    }
    List<TransactionWithMetadata> transactions = block.get().getTransactions();
    if (transactions.isEmpty() || txIndex < 0 || txIndex > block.get().getTransactions().size()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
    }
    final Optional<TransactionTrace> transactionTrace = blockTracerSupplier.get().trace(blockHash, new DebugOperationTracer(new TraceOptions(false, true, true))).map(BlockTrace::getTransactionTraces).orElse(Collections.emptyList()).stream().filter(trxTrace -> trxTrace.getTransaction().getHash().equals(transactions.get(txIndex).getTransaction().getHash())).findFirst();
    if (transactionTrace.isEmpty()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.TRANSACTION_NOT_FOUND);
    }
    Optional<Account> account = transactionTrace.get().getTraceFrames().stream().map(traceFrame -> traceFrame.getWorldUpdater().get(address)).filter(Objects::nonNull).filter(a -> a.getAddress().equals(address)).findFirst();
    if (account.isEmpty()) {
        return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.NO_ACCOUNT_FOUND);
    }
    return debugAccountAtResult(account.get().getCode(), Quantity.create(account.get().getNonce()), Quantity.create(account.get().getBalance()), Quantity.create(account.get().getCodeHash()));
}
Also used : TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Account(org.hyperledger.besu.evm.account.Account) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) ImmutableDebugAccountAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.ImmutableDebugAccountAtResult) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) Supplier(java.util.function.Supplier) Quantity(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity) Objects(java.util.Objects) List(java.util.List) RpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) BlockParameterOrBlockHash(org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) Optional(java.util.Optional) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) BlockTracer(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer) Account(org.hyperledger.besu.evm.account.Account) Address(org.hyperledger.besu.datatypes.Address) DebugOperationTracer(org.hyperledger.besu.ethereum.vm.DebugOperationTracer) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) TransactionTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace) Objects(java.util.Objects) TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) BlockTrace(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace) JsonRpcErrorResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)

Aggregations

BlockTrace (org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace)8 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)6 TransactionTrace (org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace)5 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)5 TraceOptions (org.hyperledger.besu.ethereum.debug.TraceOptions)5 DebugOperationTracer (org.hyperledger.besu.ethereum.vm.DebugOperationTracer)5 DebugTraceTransactionResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugTraceTransactionResult)4 TransactionProcessingResult (org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)4 Optional (java.util.Optional)3 Supplier (java.util.function.Supplier)3 Hash (org.hyperledger.besu.datatypes.Hash)3 RpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod)3 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)3 BlockTracer (org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer)3 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)3 Block (org.hyperledger.besu.ethereum.core.Block)3 Collection (java.util.Collection)2 List (java.util.List)2 BlockParameter (org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter)2 JsonRpcErrorResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse)2