Skip to content

Commit

Permalink
Feature/minimal safe tracing (#5197)
Browse files Browse the repository at this point in the history
tracing endpoints keep open a worldstate for the duration of tracing and
response generation.

---------

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Signed-off-by: garyschulte <garyschulte@gmail.com>
Co-authored-by: Karim TAAM <karim.t2am@gmail.com>
  • Loading branch information
garyschulte and matkt authored Mar 15, 2023
1 parent c0c329f commit d736203
Show file tree
Hide file tree
Showing 37 changed files with 671 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ DataFetcher<Optional<AccountAdapter>> getAccountDataFetcher() {
ws -> {
final Account account = ws.get(addr);
if (account == null) {
return new EmptyAccountAdapter(addr);
return Optional.of(new EmptyAccountAdapter(addr));
}
return new AccountAdapter(account);
return Optional.of(new AccountAdapter(account));
})
.or(
() -> {
Expand All @@ -246,9 +246,9 @@ DataFetcher<Optional<AccountAdapter>> getAccountDataFetcher() {
ws -> {
final Account account = ws.get(addr);
if (account == null) {
return new EmptyAccountAdapter(addr);
return Optional.of(new EmptyAccountAdapter(addr));
}
return new AccountAdapter(account);
return Optional.of(new AccountAdapter(account));
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Optional<AdapterBase> getMiner(final DataFetchingEnvironment environment)
}

return query
.getAndMapWorldState(blockNumber, ws -> ws.get(header.getCoinbase()))
.getAndMapWorldState(blockNumber, ws -> Optional.ofNullable(ws.get(header.getCoinbase())))
.map(account -> (AdapterBase) new AccountAdapter(account))
.or(() -> Optional.of(new EmptyAccountAdapter(header.getCoinbase())));
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public Optional<AccountAdapter> getAccount(final DataFetchingEnvironment environ
bn,
ws -> {
final Address address = environment.getArgument("address");
return new AccountAdapter(ws.get(address));
return Optional.of(new AccountAdapter(ws.get(address)));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public Optional<AccountAdapter> getAccount(final DataFetchingEnvironment environ
}

return query.getAndMapWorldState(
blockNumber, ws -> new AccountAdapter(ws.get(logWithMetadata.getLogger())));
blockNumber, ws -> Optional.of(new AccountAdapter(ws.get(logWithMetadata.getLogger()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Optional<AccountAdapter> getAccount(
final Long blockNumber = dataFetchingEnvironment.getArgument("blockNumber");
final long latestBlockNumber = blockchainQuery.latestBlock().get().getHeader().getNumber();
return blockchainQuery
.getAndMapWorldState(latestBlockNumber, ws -> ws.get(addr))
.getAndMapWorldState(latestBlockNumber, ws -> Optional.ofNullable(ws.get(addr)))
.map(AccountAdapter::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public Optional<AccountAdapter> getFrom(final DataFetchingEnvironment environmen
return query.getAndMapWorldState(
blockNumber,
mutableWorldState ->
new AccountAdapter(
mutableWorldState.get(transactionWithMetadata.getTransaction().getSender())));
Optional.of(
new AccountAdapter(
mutableWorldState.get(transactionWithMetadata.getTransaction().getSender()))));
}

public Optional<AccountAdapter> getTo(final DataFetchingEnvironment environment) {
Expand All @@ -102,9 +103,7 @@ public Optional<AccountAdapter> getTo(final DataFetchingEnvironment environment)
transactionWithMetadata
.getTransaction()
.getTo()
.map(address -> new AccountAdapter(address, ws.get(address)))
// safe because mapWorldState returns Optional.ofNullable
.orElse(null));
.map(address -> new AccountAdapter(address, ws.get(address))));
}

public Optional<Wei> getValue() {
Expand Down Expand Up @@ -174,7 +173,8 @@ public Optional<AccountAdapter> getCreatedContract(final DataFetchingEnvironment
return Optional.empty();
}
final long blockNumber = bn.orElseGet(txBlockNumber::get);
return query.getAndMapWorldState(blockNumber, ws -> new AccountAdapter(ws.get(addr.get())));
return query.getAndMapWorldState(
blockNumber, ws -> Optional.of(new AccountAdapter(ws.get(addr.get()))));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.FlatTraceGenerator;
Expand All @@ -28,6 +29,7 @@
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;

import java.util.Collections;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -62,19 +64,33 @@ private Stream<FlatTrace> getTraceBlock(final Block block, final Hash transactio
if (block == null || block.getBody().getTransactions().isEmpty()) {
return Stream.empty();
}
final TransactionTrace transactionTrace = getTransactionTrace(block, transactionHash);
return getTraceStream(transactionTrace, block);
return Tracer.processTracing(
blockchainQueries,
Optional.of(block.getHeader()),
mutableWorldState -> {
final TransactionTrace transactionTrace = getTransactionTrace(block, transactionHash);
return Optional.ofNullable(getTraceStream(transactionTrace, block));
})
.orElse(Stream.empty());
}

private TransactionTrace getTransactionTrace(final Block block, final Hash transactionHash) {
return blockTracerSupplier
.get()
.trace(block, new DebugOperationTracer(new TraceOptions(false, false, true)))
.map(BlockTrace::getTransactionTraces)
.orElse(Collections.emptyList())
.stream()
.filter(trxTrace -> trxTrace.getTransaction().getHash().equals(transactionHash))
.findFirst()
return Tracer.processTracing(
blockchainQueries,
Optional.of(block.getHeader()),
mutableWorldState -> {
return blockTracerSupplier
.get()
.trace(
mutableWorldState,
block,
new DebugOperationTracer(new TraceOptions(false, false, true)))
.map(BlockTrace::getTransactionTraces)
.orElse(Collections.emptyList())
.stream()
.filter(trxTrace -> trxTrace.getTransaction().getHash().equals(transactionHash))
.findFirst();
})
.orElseThrow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockTracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand Down Expand Up @@ -87,42 +88,56 @@ protected Object resultByBlockHash(
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()));
return Tracer.processTracing(
blockchainQueries.get(),
Optional.of(block.get().getHeader()),
mutableWorldState -> {
final Optional<TransactionTrace> transactionTrace =
blockTracerSupplier
.get()
.trace(
mutableWorldState,
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 Optional.of(
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 Optional.of(
new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.NO_ACCOUNT_FOUND));
}

return Optional.of(
debugAccountAtResult(
account.get().getCode(),
Quantity.create(account.get().getNonce()),
Quantity.create(account.get().getBalance()),
Quantity.create(account.get().getCodeHash())));
})
.orElse(
new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.WORLD_STATE_UNAVAILABLE));
}

protected ImmutableDebugAccountAtResult debugAccountAtResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
accounts.remove(maxResults);
}

return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(),
new DebugAccountRangeAtResult(
accounts.stream()
.collect(
Collectors.toMap(
account -> account.getAddressHash().toString(),
account -> account.getAddress().orElse(Address.ZERO).toString())),
nextKey.toString()));
return Optional.of(
new JsonRpcSuccessResponse(
requestContext.getRequest().getId(),
new DebugAccountRangeAtResult(
accounts.stream()
.collect(
Collectors.toMap(
account -> account.getAddressHash().toString(),
account ->
account.getAddress().orElse(Address.ZERO).toString())),
nextKey.toString())));
})
.orElse(emptyResponse(requestContext));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.TransactionTraceParams;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.Tracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand All @@ -29,11 +30,11 @@
import org.hyperledger.besu.ethereum.core.Block;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Suppliers;

public class DebugStandardTraceBlockToFile implements JsonRpcMethod {
Expand Down Expand Up @@ -79,14 +80,18 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

protected List<String> traceBlock(
final Block block, final Optional<TransactionTraceParams> transactionTraceParams) {
return transactionTracerSupplier
.get()
.traceTransactionToFile(
block.getHash(), transactionTraceParams, dataDir.resolve(TRACE_PATH));
}

protected Object emptyResult() {
final ObjectMapper mapper = new ObjectMapper();
return mapper.createArrayNode();
return Tracer.processTracing(
blockchainQueries.get(),
Optional.of(block.getHeader()),
mutableWorldState ->
Optional.of(
transactionTracerSupplier
.get()
.traceTransactionToFile(
mutableWorldState,
block.getHash(),
transactionTraceParams,
dataDir.resolve(TRACE_PATH))))
.orElse(new ArrayList<>());
}
}
Loading

0 comments on commit d736203

Please sign in to comment.