Skip to content

Commit

Permalink
move traceEndTransaction after coinbase refund, add self destruct set…
Browse files Browse the repository at this point in the history
… to the method (hyperledger#7188)

Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
daniellehrner and macfarla authored Jun 11, 2024
1 parent 478b6d0 commit 04f304f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void shouldRetrieveStateUpdatePostTracingForOneBlock() {
verify(opTracer).traceStartTransaction(any(), eq(tx));
verify(opTracer)
.traceEndTransaction(
any(), eq(tx), anyBoolean(), any(), any(), anyLong(), anyLong());
any(), eq(tx), anyBoolean(), any(), any(), anyLong(), any(), anyLong());
});

verify(opTracer).traceEndBlock(tracedBlock.getHeader(), tracedBlock.getBody());
Expand Down Expand Up @@ -173,7 +173,14 @@ void shouldRetrieveStateUpdatePostTracingForAllBlocks() {
verify(opTracer).traceStartTransaction(any(), eq(tx));
verify(opTracer)
.traceEndTransaction(
any(), eq(tx), anyBoolean(), any(), any(), anyLong(), anyLong());
any(),
eq(tx),
anyBoolean(),
any(),
any(),
anyLong(),
any(),
anyLong());
});

verify(opTracer).traceEndBlock(tracedBlock.getHeader(), tracedBlock.getBody());
Expand Down Expand Up @@ -222,6 +229,7 @@ void shouldReturnTheCorrectWorldViewForTxStartEnd() {
assertThat(txStartEndTracer.txEndStatus).isTrue();
assertThat(txStartEndTracer.txEndOutput).isEqualTo(Bytes.fromHexString("0x"));
assertThat(txStartEndTracer.txEndGasUsed).isEqualTo(24303);
assertThat(txStartEndTracer.txEndSelfDestructs).isEmpty();
assertThat(txStartEndTracer.txEndTimeNs).isNotNull();

assertThat(txStartEndTracer.txEndLogs).isNotEmpty();
Expand Down Expand Up @@ -263,6 +271,7 @@ private static class TxStartEndTracer implements BlockAwareOperationTracer {
public Bytes txEndOutput;
public List<Log> txEndLogs;
public long txEndGasUsed;
public Set<Address> txEndSelfDestructs;
public Long txEndTimeNs;

private final Set<Transaction> traceStartTxCalled = new HashSet<>();
Expand All @@ -287,6 +296,7 @@ public void traceEndTransaction(
final Bytes output,
final List<Log> logs,
final long gasUsed,
final Set<Address> selfDestructs,
final long timeNs) {
if (!traceEndTxCalled.add(transaction)) {
fail("traceEndTransaction already called for tx " + transaction);
Expand All @@ -297,6 +307,7 @@ public void traceEndTransaction(
txEndOutput = output;
txEndLogs = logs;
txEndGasUsed = gasUsed;
txEndSelfDestructs = selfDestructs;
txEndTimeNs = timeNs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class MainnetTransactionProcessor {

private static final Logger LOG = LoggerFactory.getLogger(MainnetTransactionProcessor.class);

private static final Set<Address> EMPTY_ADDRESS_SET = Set.of();

protected final GasCalculator gasCalculator;

protected final TransactionValidatorFactory transactionValidatorFactory;
Expand Down Expand Up @@ -451,15 +453,6 @@ public TransactionProcessingResult processTransaction(
.log();
final long gasUsedByTransaction = transaction.getGasLimit() - initialFrame.getRemainingGas();

operationTracer.traceEndTransaction(
worldUpdater,
transaction,
initialFrame.getState() == MessageFrame.State.COMPLETED_SUCCESS,
initialFrame.getOutputData(),
initialFrame.getLogs(),
gasUsedByTransaction,
0L);

// update the coinbase
final var coinbase = worldState.getOrCreate(miningBeneficiary);
final long usedGas = transaction.getGasLimit() - refundedGas;
Expand All @@ -485,6 +478,16 @@ public TransactionProcessingResult processTransaction(

coinbase.incrementBalance(coinbaseWeiDelta);

operationTracer.traceEndTransaction(
worldUpdater,
transaction,
initialFrame.getState() == MessageFrame.State.COMPLETED_SUCCESS,
initialFrame.getOutputData(),
initialFrame.getLogs(),
gasUsedByTransaction,
initialFrame.getSelfDestructs(),
0L);

initialFrame.getSelfDestructs().forEach(worldState::deleteAccount);

if (clearEmptyAccounts) {
Expand Down Expand Up @@ -516,13 +519,27 @@ public TransactionProcessingResult processTransaction(
}
} catch (final MerkleTrieException re) {
operationTracer.traceEndTransaction(
worldState.updater(), transaction, false, Bytes.EMPTY, List.of(), 0, 0L);
worldState.updater(),
transaction,
false,
Bytes.EMPTY,
List.of(),
0,
EMPTY_ADDRESS_SET,
0L);

// need to throw to trigger the heal
throw re;
} catch (final RuntimeException re) {
operationTracer.traceEndTransaction(
worldState.updater(), transaction, false, Bytes.EMPTY, List.of(), 0, 0L);
worldState.updater(),
transaction,
false,
Bytes.EMPTY,
List.of(),
0,
EMPTY_ADDRESS_SET,
0L);

LOG.error("Critical Exception Processing Transaction", re);
return TransactionProcessingResult.invalid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

Expand Down Expand Up @@ -216,6 +217,7 @@ public void traceEndTransaction(
final Bytes output,
final List<Log> logs,
final long gasUsed,
final Set<Address> selfDestructs,
final long timeNs) {
this.traceEndTxCalled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.TimeUnit;
Expand All @@ -86,6 +87,8 @@

public class T8nExecutor {

private static final Set<Address> EMPTY_ADDRESS_SET = Set.of();

public record RejectedTransaction(int index, String error) {}

protected static List<Transaction> extractTransactions(
Expand Down Expand Up @@ -349,6 +352,7 @@ static T8nResult runTest(
result.getOutput(),
result.getLogs(),
gasUsed - intrinsicGas,
EMPTY_ADDRESS_SET,
timer.elapsed(TimeUnit.NANOSECONDS));
Bytes gasUsedInTransaction = Bytes.ofUnsignedLong(transactionGasUsed);
receipts.add(receipt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.evm.tracing;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
Expand All @@ -23,6 +24,7 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apache.tuweni.bytes.Bytes;

Expand Down Expand Up @@ -92,6 +94,7 @@ default void traceStartTransaction(final WorldView worldView, final Transaction
* @param output the bytes output from the transaction
* @param logs the logs emitted by this transaction
* @param gasUsed the gas used by the entire transaction
* @param selfDestructs the set of addresses that self-destructed during the transaction
* @param timeNs the time in nanoseconds it took to execute the transaction
*/
default void traceEndTransaction(
Expand All @@ -101,6 +104,7 @@ default void traceEndTransaction(
final Bytes output,
final List<Log> logs,
final long gasUsed,
final Set<Address> selfDestructs,
final long timeNs) {}

/**
Expand Down

0 comments on commit 04f304f

Please sign in to comment.