Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <nischal@web3labs.com>
  • Loading branch information
NickSneo committed Jul 26, 2024
1 parent 09fa25c commit 545458a
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

public abstract class AbstractPrivateTraceByHash implements JsonRpcMethod {

protected final Supplier<PrivateBlockTracer> blockTracerSupplier;
Expand Down Expand Up @@ -137,7 +133,7 @@ private PrivateTransactionTrace getTransactionTrace(
.trace(
mutableWorldState,
block,
new DebugOperationTracer(new TraceOptions(false, false, true), true),
new DebugOperationTracer(new TraceOptions(false, false, true), false),
enclaveKey,
privacyGroupId,
privateBlockMetadata)
Expand All @@ -159,13 +155,6 @@ private Stream<PrivateFlatTrace> getTraceStream(
.map(PrivateFlatTrace.class::cast);
}

protected JsonNode arrayNodeFromTraceStream(final Stream<PrivateFlatTrace> traceStream) {
final ObjectMapper mapper = new ObjectMapper();
final ArrayNode resultArrayNode = mapper.createArrayNode();
traceStream.forEachOrdered(resultArrayNode::addPOJO);
return resultArrayNode;
}

private void verifyPrivacyGroupMatchesAuthenticatedEnclaveKey(
final JsonRpcRequestContext request,
final String privacyGroupId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.privateProcessor.PrivateBlockTracer;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.privateTracing.PrivateFlatTrace;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.query.PrivacyQueries;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,4 +75,11 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
requestContext.getRequest().getId(),
arrayNodeFromTraceStream(resultByTransactionHash(transactionHash, requestContext)));
}

protected JsonNode arrayNodeFromTraceStream(final Stream<PrivateFlatTrace> traceStream) {
final ObjectMapper mapper = new ObjectMapper();
final ArrayNode resultArrayNode = mapper.createArrayNode();
traceStream.forEachOrdered(resultArrayNode::addPOJO);
return resultArrayNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.privateProcessor;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator.calculateExcessBlobGasForParent;

import org.hyperledger.besu.datatypes.BlobGas;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
Expand All @@ -38,17 +32,14 @@ public class PrivateBlockReplay {

private final ProtocolSchedule protocolSchedule;
private final Blockchain blockchain;
private final ProtocolContext protocolContext;
private final PrivacyController privacyController;

public PrivateBlockReplay(
final ProtocolSchedule protocolSchedule,
final Blockchain blockchain,
final ProtocolContext protocolContext,
final PrivacyController privacyController) {
this.protocolSchedule = protocolSchedule;
this.blockchain = blockchain;
this.protocolContext = protocolContext;
this.privacyController = privacyController;
}

Expand All @@ -61,15 +52,6 @@ public Optional<PrivateBlockTrace> block(
block.getHeader(),
block.getBody(),
(body, header, blockchain, transactionProcessor, protocolSpec) -> {
final Wei dataGasPrice =
protocolSpec
.getFeeMarket()
.blobGasPricePerGas(
blockchain
.getBlockHeader(header.getParentHash())
.map(parent -> calculateExcessBlobGasForParent(protocolSpec, parent))
.orElse(BlobGas.ZERO));

final List<PrivateTransactionTrace> transactionTraces =
privateBlockMetadata.getPrivateTransactionMetadataList().stream()
.map(
Expand All @@ -84,24 +66,14 @@ public Optional<PrivateBlockTrace> block(
executedPrivateTransaction,
header,
blockchain,
transactionProcessor,
dataGasPrice))
transactionProcessor))
.orElse(null))
.toList();

return Optional.of(new PrivateBlockTrace(transactionTraces));
});
}

public <T> Optional<T> performActionWithBlock(final Hash blockHash, final BlockAction<T> action) {
Optional<Block> maybeBlock = getBlock(blockHash);
if (maybeBlock.isEmpty()) {
maybeBlock = protocolContext.getBadBlockManager().getBadBlock(blockHash);
}
return maybeBlock.flatMap(
block -> performActionWithBlock(block.getHeader(), block.getBody(), action));
}

private <T> Optional<T> performActionWithBlock(
final BlockHeader header, final BlockBody body, final BlockAction<T> action) {
if (header == null) {
Expand All @@ -117,17 +89,6 @@ private <T> Optional<T> performActionWithBlock(
return action.perform(body, header, blockchain, transactionProcessor, protocolSpec);
}

private Optional<Block> getBlock(final Hash blockHash) {
final BlockHeader blockHeader = blockchain.getBlockHeader(blockHash).orElse(null);
if (blockHeader != null) {
final BlockBody blockBody = blockchain.getBlockBody(blockHeader.getHash()).orElse(null);
if (blockBody != null) {
return Optional.of(new Block(blockHeader, blockBody));
}
}
return Optional.empty();
}

@FunctionalInterface
public interface BlockAction<T> {
Optional<T> perform(
Expand All @@ -144,7 +105,6 @@ T performAction(
ExecutedPrivateTransaction transaction,
BlockHeader blockHeader,
Blockchain blockchain,
PrivateTransactionProcessor transactionProcessor,
Wei dataGasPrice);
PrivateTransactionProcessor transactionProcessor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class PrivateBlockTracer {
private final PrivateBlockReplay blockReplay;
// Either the initial block state or the state of the prior TX, including miner rewards.
private WorldUpdater chainedUpdater;
private WorldUpdater privateChainedUpdater;

public PrivateBlockTracer(final PrivateBlockReplay blockReplay) {
this.blockReplay = blockReplay;
Expand All @@ -57,18 +56,17 @@ private PrivateBlockReplay.TransactionAction<PrivateTransactionTrace> prepareRep
final PrivateTracer.TraceableState mutableWorldState,
final DebugOperationTracer tracer,
final String privacyGroupId) {
return (transaction, header, blockchain, transactionProcessor, dataGasPrice) -> {
return (transaction, header, blockchain, transactionProcessor) -> {
// if we have no prior updater, it must be the first TX, so use the block's initial state
if (chainedUpdater == null) {
chainedUpdater = mutableWorldState.updater();
privateChainedUpdater = mutableWorldState.privateUpdater();

} else if (chainedUpdater instanceof StackedUpdater<?, ?> stackedUpdater) {
stackedUpdater.markTransactionBoundary();
}
// create an updater for just this tx
chainedUpdater = chainedUpdater.updater();
privateChainedUpdater = mutableWorldState.privateUpdater();
WorldUpdater privateChainedUpdater = mutableWorldState.privateUpdater();
final TransactionProcessingResult result =
transactionProcessor.processTransaction(
chainedUpdater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ private TraceableState(
this.disposableWorldState = disposableWorldState;
}

@Override
public void persist(final BlockHeader blockHeader) {
mutableWorldState.persist(blockHeader);
}

@Override
public WorldUpdater updater() {
return mutableWorldState.updater();
Expand Down Expand Up @@ -116,5 +111,8 @@ public Account get(final Address address) {
public void close() throws Exception {
mutableWorldState.close();
}

@Override
public void persist(final BlockHeader blockHeader) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.privateProcessor.PrivateTransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.Trace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.Action;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.tracing.flat.Result;
import org.hyperledger.besu.ethereum.debug.TraceFrame;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -135,7 +137,22 @@ protected PrivateFlatTrace(
static PrivateFlatTrace.Builder freshBuilder(final PrivateTransactionTrace transactionTrace) {
return PrivateFlatTrace.builder()
.resultBuilder(Result.builder())
.actionBuilder(Action.Builder.from(transactionTrace));
.actionBuilder(from(transactionTrace));
}

public static Action.Builder from(final PrivateTransactionTrace trace) {
final Action.Builder builder =
Action.builder()
.from(trace.getPrivateTransaction().getSender().toHexString())
.value(Quantity.create(trace.getPrivateTransaction().getValue()));
if (!trace.getTraceFrames().isEmpty()) {
final TraceFrame traceFrame = trace.getTraceFrames().get(0);
builder.gas(
"0x"
+ Long.toHexString(
traceFrame.getGasRemaining() + (traceFrame.getPrecompiledGasCost().orElse(0L))));
}
return builder;
}

public Action getAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.TransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.privateProcessor.PrivateTransactionTrace;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.Quantity;
import org.hyperledger.besu.ethereum.debug.TraceFrame;

Expand Down Expand Up @@ -192,22 +191,6 @@ public static Builder from(final TransactionTrace trace) {
return builder;
}

public static Builder from(final PrivateTransactionTrace trace) {
final Builder builder =
new Builder()
.from(trace.getPrivateTransaction().getSender().toHexString())
.value(Quantity.create(trace.getPrivateTransaction().getValue()));
if (!trace.getTraceFrames().isEmpty()) {
final TraceFrame traceFrame = trace.getTraceFrames().get(0);
builder.gas(
"0x"
+ Long.toHexString(
traceFrame.getGasRemaining()
+ (traceFrame.getPrecompiledGasCost().orElse(0L))));
}
return builder;
}

public Builder creationMethod(final String creationMethod) {
this.creationMethod = creationMethod;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public Map<String, JsonRpcMethod> methods(
protocolSchedule,
transactionPool,
privacyParameters,
filterManager,
protocolContext),
filterManager),
new PrivxJsonRpcMethods(
blockchainQueries, protocolSchedule, transactionPool, privacyParameters),
new Web3JsonRpcMethods(clientVersion),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.methods;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcApis;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
Expand Down Expand Up @@ -50,18 +49,15 @@
public class PrivJsonRpcMethods extends PrivacyApiGroupJsonRpcMethods {

private final FilterManager filterManager;
private final ProtocolContext protocolContext;

public PrivJsonRpcMethods(
final BlockchainQueries blockchainQueries,
final ProtocolSchedule protocolSchedule,
final TransactionPool transactionPool,
final PrivacyParameters privacyParameters,
final FilterManager filterManager,
final ProtocolContext protocolContext) {
final FilterManager filterManager) {
super(blockchainQueries, protocolSchedule, transactionPool, privacyParameters);
this.filterManager = filterManager;
this.protocolContext = protocolContext;
}

@Override
Expand All @@ -77,10 +73,7 @@ protected Map<String, JsonRpcMethod> create(

final PrivateBlockReplay blockReplay =
new PrivateBlockReplay(
getProtocolSchedule(),
getBlockchainQueries().getBlockchain(),
protocolContext,
privacyController);
getProtocolSchedule(), getBlockchainQueries().getBlockchain(), privacyController);
final Map<String, JsonRpcMethod> RPC_METHODS =
mapOf(
new PrivCall(getBlockchainQueries(), privacyController, privacyIdProvider),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
Expand All @@ -45,20 +44,13 @@ public class PrivJsonRpcMethodsTest {
@Mock private TransactionPool transactionPool;
@Mock private PrivacyParameters privacyParameters;
@Mock private FilterManager filterManager;
@Mock private ProtocolContext protocolContext;

private PrivJsonRpcMethods privJsonRpcMethods;

@BeforeEach
public void setup() {
privJsonRpcMethods =
new PrivJsonRpcMethods(
blockchainQueries,
protocolSchedule,
transactionPool,
privacyParameters,
filterManager,
protocolContext);
blockchainQueries, protocolSchedule, transactionPool, privacyParameters, filterManager);

lenient().when(privacyParameters.isEnabled()).thenReturn(true);
}
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = '6L5dNJ975Ka/X7g4lTdpkBvPQrJgJu+vAf/m1dFCneU='
knownHash = 'FGYGGFh3bsPtx6lXw7LIPVCYS9G2XgX5paJrowv1WrA='
}
check.dependsOn('checkAPIChanges')

Expand Down

0 comments on commit 545458a

Please sign in to comment.