use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace 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);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace 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()));
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace in project besu by hyperledger.
the class FlatTraceGenerator method handleReturn.
private static FlatTrace.Context handleReturn(final ProtocolSchedule protocolSchedule, final TransactionTrace transactionTrace, final Block block, final TraceFrame traceFrame, final Deque<FlatTrace.Context> tracesContexts, final FlatTrace.Context currentContext) {
final FlatTrace.Builder traceFrameBuilder = currentContext.getBuilder();
final Result.Builder resultBuilder = traceFrameBuilder.getResultBuilder();
final Action.Builder actionBuilder = traceFrameBuilder.getActionBuilder();
actionBuilder.value(Quantity.create(traceFrame.getValue()));
currentContext.setGasUsed(computeGasUsed(tracesContexts, currentContext, transactionTrace, traceFrame));
if ("STOP".equals(traceFrame.getOpcode()) && resultBuilder.isGasUsedEmpty()) {
final long callStipend = protocolSchedule.getByBlockNumber(block.getHeader().getNumber()).getGasCalculator().getAdditionalCallStipend();
tracesContexts.stream().filter(context -> !tracesContexts.getFirst().equals(context) && !tracesContexts.getLast().equals(context)).forEach(context -> context.decGasUsed(callStipend));
}
final Bytes outputData = traceFrame.getOutputData();
if (resultBuilder.getCode() == null) {
resultBuilder.output(outputData.toHexString());
}
// set value for contract creation TXes, CREATE, and CREATE2
if (actionBuilder.getCallType() == null && traceFrame.getMaybeCode().isPresent()) {
actionBuilder.init(traceFrame.getMaybeCode().get().getBytes().toHexString());
resultBuilder.code(outputData.toHexString());
if (currentContext.isCreateOp()) {
// this is from a CREATE/CREATE2, so add code deposit cost.
currentContext.incGasUsed(outputData.size() * 200L);
}
}
tracesContexts.removeLast();
final FlatTrace.Context nextContext = tracesContexts.peekLast();
if (nextContext != null) {
nextContext.getBuilder().incSubTraces();
}
return nextContext;
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace in project besu by hyperledger.
the class TraceCallMany method getSingleCallResult.
private JsonNode getSingleCallResult(final JsonCallParameter callParameter, final TraceTypeParameter traceTypeParameter, final BlockHeader header, final WorldUpdater worldUpdater) {
final Set<TraceTypeParameter.TraceType> traceTypes = traceTypeParameter.getTraceTypes();
final DebugOperationTracer tracer = new DebugOperationTracer(buildTraceOptions(traceTypes));
final Optional<TransactionSimulatorResult> maybeSimulatorResult = transactionSimulator.processWithWorldUpdater(callParameter, buildTransactionValidationParams(), tracer, header, worldUpdater);
LOG.trace("Executing {} call for transaction {}", traceTypeParameter, callParameter);
if (maybeSimulatorResult.isEmpty()) {
throw new EmptySimulatorResultException();
}
final TransactionSimulatorResult simulatorResult = maybeSimulatorResult.get();
if (simulatorResult.isInvalid()) {
throw new TransactionInvalidException();
}
final TransactionTrace transactionTrace = new TransactionTrace(simulatorResult.getTransaction(), simulatorResult.getResult(), tracer.getTraceFrames());
final Block block = blockchainQueriesSupplier.get().getBlockchain().getChainHeadBlock();
return getTraceCallResult(protocolSchedule, traceTypes, maybeSimulatorResult, transactionTrace, block);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace in project besu by hyperledger.
the class FlatTraceGeneratorTest method testGenerateFromTransactionTraceWithRevertReason.
@Test
public void testGenerateFromTransactionTraceWithRevertReason() {
final Bytes revertReason = Bytes.random(32);
Mockito.when(transaction.getSender()).thenReturn(Address.ZERO);
Mockito.when(transactionProcessingResult.getRevertReason()).thenReturn(Optional.of(revertReason));
final TransactionTrace transactionTrace = new TransactionTrace(transaction, transactionProcessingResult, Collections.emptyList());
final Stream<Trace> traceStream = FlatTraceGenerator.generateFromTransactionTrace(null, transactionTrace, null, new AtomicInteger());
final List<Trace> traces = traceStream.collect(Collectors.toList());
Assertions.assertThat(traces.isEmpty()).isFalse();
Assertions.assertThat(traces.get(0)).isNotNull();
Assertions.assertThat(traces.get(0) instanceof FlatTrace).isTrue();
Assertions.assertThat(((FlatTrace) traces.get(0)).getRevertReason()).isEqualTo(revertReason.toHexString());
}
Aggregations