Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve flat trace generation performance #6472

Merged
merged 17 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
- Log blob count when importing a block via Engine API [#6466](https://github.com/hyperledger/besu/pull/6466)
- Introduce `--Xbonsai-limit-trie-logs-enabled` experimental feature which by default will only retain the latest 512 trie logs, saving about 3GB per week in database growth [#5390](https://github.com/hyperledger/besu/issues/5390)
- Introduce `besu storage x-trie-log prune` experimental offline subcommand which will prune all redundant trie logs except the latest 512 [#6303](https://github.com/hyperledger/besu/pull/6303)
- Improve flat trace generation performance [#6472](https://github.com/hyperledger/besu/pull/6472)
- SNAP and CHECKPOINT sync - early access flag removed so now simply SNAP and CHECKPOINT [#6405](https://github.com/hyperledger/besu/pull/6405)
- X_SNAP and X_CHECKPOINT are marked for deprecation and will be removed in 24.4.0
- X_SNAP and X_CHECKPOINT are marked for deprecation and will be removed in 24.4.0
- Github Actions based build.
- Introduce caching mechanism to optimize Keccak hash calculations for account storage slots during block processing [#6452](https://github.com/hyperledger/besu/pull/6452)
- Added configuration options for `pragueTime` to genesis file for Prague fork development [#6473](https://github.com/hyperledger/besu/pull/6473)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.operation.ReturnOperation;
import org.hyperledger.besu.evm.operation.RevertOperation;

import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -530,11 +532,19 @@ private static FlatTrace.Context handleCallDataLoad(

private static boolean hasRevertInSubCall(
final TransactionTrace transactionTrace, final TraceFrame callFrame) {
return transactionTrace.getTraceFrames().stream()
.filter(traceFrame -> !traceFrame.equals(callFrame))
.takeWhile(traceFrame -> !traceFrame.getOpcode().equals("RETURN"))
.filter(traceFrame -> traceFrame.getOpcode().equals("REVERT"))
.anyMatch(traceFrame -> traceFrame.getDepth() == callFrame.getDepth());
for (int i = 0; i < transactionTrace.getTraceFrames().size(); i++) {
if (i + 1 < transactionTrace.getTraceFrames().size()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of a random question (Side note: I dont really have Java exp) but what is the point of this If condition? Wouldn't starting the loop at 1 and running it until size() - 1 have the same result? (Or if iterating in reverse has no side-effects, even starting at size() - 1 and running while its > 0)

final TraceFrame next = transactionTrace.getTraceFrames().get(i + 1);
if (next.getDepth() == callFrame.getDepth()) {
if (next.getOpcodeNumber() == RevertOperation.OPCODE) {
return true;
} else if (next.getOpcodeNumber() == ReturnOperation.OPCODE) {
return false;
}
}
}
}
return false;
}

private static String calculateCallingAddress(final FlatTrace.Context lastContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void shouldReturnCorrectResponse() {
new TraceFrame(
12,
Optional.of("NONE"),
Integer.MAX_VALUE,
45L,
OptionalLong.of(56L),
0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void shouldReturnCorrectResponse() {
new TraceFrame(
12,
Optional.of("NONE"),
Integer.MAX_VALUE,
45L,
OptionalLong.of(56L),
0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void shouldReturnCorrectResponse() {
new TraceFrame(
12,
Optional.of("NONE"),
Integer.MAX_VALUE,
45L,
OptionalLong.of(56L),
0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void shouldTraceTheTransactionUsingTheTransactionTracer() {
new TraceFrame(
12,
Optional.of("NONE"),
Integer.MAX_VALUE,
45L,
OptionalLong.of(56L),
0L,
Expand Down Expand Up @@ -183,6 +184,7 @@ public void shouldNotTraceTheTransactionIfNotFound() {
new TraceFrame(
12,
Optional.of("NONE"),
Integer.MAX_VALUE,
45L,
OptionalLong.of(56L),
0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class TraceFrame {

private final int pc;
private final Optional<String> opcode;
private final int opcodeNumber;
private final long gasRemaining;
private final OptionalLong gasCost;
private final long gasRefund;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class TraceFrame {
public TraceFrame(
final int pc,
final Optional<String> opcode,
final int opcodeNumber,
final long gasRemaining,
final OptionalLong gasCost,
final long gasRefund,
Expand All @@ -86,6 +88,7 @@ public TraceFrame(
final Optional<StorageEntry> maybeUpdatedStorage) {
this.pc = pc;
this.opcode = opcode;
this.opcodeNumber = opcodeNumber;
this.gasRemaining = gasRemaining;
this.gasCost = gasCost;
this.gasRefund = gasRefund;
Expand Down Expand Up @@ -118,6 +121,10 @@ public String getOpcode() {
return opcode.orElse("");
}

public int getOpcodeNumber() {
return opcodeNumber;
}

public long getGasRemaining() {
return gasRemaining;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void tracePreExecution(final MessageFrame frame) {
public void tracePostExecution(final MessageFrame frame, final OperationResult operationResult) {
final Operation currentOperation = frame.getCurrentOperation();
final String opcode = currentOperation.getName();
final int opcodeNumber = (opcode != null) ? currentOperation.getOpcode() : Integer.MAX_VALUE;
final WorldUpdater worldUpdater = frame.getWorldUpdater();
final Bytes outputData = frame.getOutputData();
final Optional<Bytes[]> memory = captureMemory(frame);
Expand All @@ -103,6 +104,7 @@ public void tracePostExecution(final MessageFrame frame, final OperationResult o
new TraceFrame(
pc,
Optional.of(opcode),
opcodeNumber,
gasRemaining,
thisGasCost == 0 ? OptionalLong.empty() : OptionalLong.of(thisGasCost),
frame.getGasRefund(),
Expand Down Expand Up @@ -136,6 +138,7 @@ public void tracePrecompileCall(
new TraceFrame(
frame.getPC(),
Optional.empty(),
Integer.MAX_VALUE,
frame.getRemainingGas(),
OptionalLong.empty(),
frame.getGasRefund(),
Expand Down Expand Up @@ -182,6 +185,7 @@ public void traceAccountCreationResult(
new TraceFrame(
frame.getPC(),
Optional.empty(),
Integer.MAX_VALUE,
frame.getRemainingGas(),
OptionalLong.empty(),
frame.getGasRefund(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
/** The Return operation. */
public class ReturnOperation extends AbstractOperation {

/** RETURN opcode number */
public static final int OPCODE = 0xF3;

/**
* Instantiates a new Return operation.
*
* @param gasCalculator the gas calculator
*/
public ReturnOperation(final GasCalculator gasCalculator) {
super(0xF3, "RETURN", 2, 0, gasCalculator);
super(OPCODE, "RETURN", 2, 0, gasCalculator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
/** The Revert operation. */
public class RevertOperation extends AbstractOperation {

/** REVERT opcode number */
public static final int OPCODE = 0xFD;

/**
* Instantiates a new Revert operation.
*
* @param gasCalculator the gas calculator
*/
public RevertOperation(final GasCalculator gasCalculator) {
super(0xFD, "REVERT", 2, 0, gasCalculator);
super(OPCODE, "REVERT", 2, 0, gasCalculator);
}

@Override
Expand Down
Loading