Skip to content

Commit

Permalink
Opcode Tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
shemnon committed Nov 10, 2023
1 parent 4c3bff3 commit a514614
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ protected PluginServiceFactory createAdditionalPluginServices(
@Override
protected ProtocolSchedule createProtocolSchedule() {
return MainnetProtocolSchedule.fromConfig(
configOptionsSupplier.get(), privacyParameters, isRevertReasonEnabled, evmConfiguration);
configOptionsSupplier.get(),
privacyParameters,
isRevertReasonEnabled,
evmConfiguration,
metricsSystem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.hyperledger.besu.ethereum.mainnet.MiningBeneficiaryCalculator;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

import java.util.Collections;
import java.util.List;
import java.util.OptionalLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -91,7 +91,8 @@ public void assertThatTraceGeneratorReturnValidRewardsForMainnetBlockProcessor()
blockReward,
BlockHeader::getCoinbase,
true,
protocolSchedule);
protocolSchedule,
new NoOpMetricsSystem());
when(protocolSpec.getBlockProcessor()).thenReturn(blockProcessor);

final Stream<Trace> traceStream =
Expand Down Expand Up @@ -131,7 +132,7 @@ public void assertThatTraceGeneratorReturnValidRewardsForMainnetBlockProcessor()
.type("reward")
.build();

final List<Trace> traces = traceStream.collect(Collectors.toList());
final List<Trace> traces = traceStream.toList();

// check block reward
assertThat(traces.get(0)).usingRecursiveComparison().isEqualTo(blocReward);
Expand All @@ -151,7 +152,8 @@ public void assertThatTraceGeneratorReturnValidRewardsForClassicBlockProcessor()
BlockHeader::getCoinbase,
true,
eraRounds,
protocolSchedule);
protocolSchedule,
new NoOpMetricsSystem());
when(protocolSpec.getBlockProcessor()).thenReturn(blockProcessor);

final Stream<Trace> traceStream =
Expand Down Expand Up @@ -191,7 +193,7 @@ public void assertThatTraceGeneratorReturnValidRewardsForClassicBlockProcessor()
.type("reward")
.build();

final List<Trace> traces = traceStream.collect(Collectors.toList());
final List<Trace> traces = traceStream.toList();

// check block reward
assertThat(traces.get(0)).usingRecursiveComparison().isEqualTo(blocReward);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.vm.BlockHashLookup;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.text.MessageFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -74,19 +74,23 @@ TransactionReceipt create(

protected final MiningBeneficiaryCalculator miningBeneficiaryCalculator;

protected final MetricsOperationTracer metricsTracer;

protected AbstractBlockProcessor(
final MainnetTransactionProcessor transactionProcessor,
final TransactionReceiptFactory transactionReceiptFactory,
final Wei blockReward,
final MiningBeneficiaryCalculator miningBeneficiaryCalculator,
final boolean skipZeroBlockRewards,
final ProtocolSchedule protocolSchedule) {
final ProtocolSchedule protocolSchedule,
final MetricsSystem metricsSystem) {
this.transactionProcessor = transactionProcessor;
this.transactionReceiptFactory = transactionReceiptFactory;
this.blockReward = blockReward;
this.miningBeneficiaryCalculator = miningBeneficiaryCalculator;
this.skipZeroBlockRewards = skipZeroBlockRewards;
this.protocolSchedule = protocolSchedule;
metricsTracer = new MetricsOperationTracer(metricsSystem);
}

@Override
Expand Down Expand Up @@ -141,7 +145,7 @@ public BlockProcessingResult processBlock(
blockHeader,
transaction,
miningBeneficiary,
OperationTracer.NO_TRACING,
metricsTracer,
blockHashLookup,
true,
TransactionValidationParams.processingBlock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.hyperledger.besu.ethereum.core.Deposit;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater;

Expand All @@ -32,40 +31,6 @@
/** Processes a block. */
public interface BlockProcessor {

/** A block processing result. */
interface Result {

/**
* The receipts generated for the transactions in a block
*
* <p>This is only valid when {@code BlockProcessor#isSuccessful} returns {@code true}.
*
* @return the receipts generated for the transactions in a block
*/
List<TransactionReceipt> getReceipts();

/**
* The private receipts generated for the private transactions in a block when in
* goQuorumCompatibilityMode
*
* <p>This is only valid when {@code BlockProcessor#isSuccessful} returns {@code true}.
*
* @return the receipts generated for the private transactions in a block
*/
List<TransactionReceipt> getPrivateReceipts();

/**
* Returns whether the block was successfully processed.
*
* @return {@code true} if the block was processed successfully; otherwise {@code false}
*/
boolean isSuccessful();

default boolean isFailed() {
return !isSuccessful();
}
}

/**
* Processes the block.
*
Expand Down Expand Up @@ -137,26 +102,6 @@ BlockProcessingResult processBlock(
Optional<List<Deposit>> deposits,
PrivateMetadataUpdater privateMetadataUpdater);

/**
* Processes the block when running Besu in GoQuorum-compatible mode
*
* @param blockchain the blockchain to append the block to
* @param worldState the world state to apply public transactions to
* @param privateWorldState the private world state to apply private transaction to
* @param block the block to process
* @return the block processing result
*/
default BlockProcessingResult processBlock(
final Blockchain blockchain,
final MutableWorldState worldState,
final MutableWorldState privateWorldState,
final Block block) {
/*
This method should never be executed. All GoQuorum processing must happen in the GoQuorumBlockProcessor.
*/
throw new IllegalStateException("Tried to process GoQuorum block on AbstractBlockProcessor");
}

/**
* Get ommer reward in ${@link Wei}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.math.BigInteger;
import java.util.List;
Expand All @@ -42,14 +43,16 @@ public ClassicBlockProcessor(
final MiningBeneficiaryCalculator miningBeneficiaryCalculator,
final boolean skipZeroBlockRewards,
final OptionalLong eraLen,
final ProtocolSchedule protocolSchedule) {
final ProtocolSchedule protocolSchedule,
final MetricsSystem metricsSystem) {
super(
transactionProcessor,
transactionReceiptFactory,
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
protocolSchedule);
protocolSchedule,
metricsSystem);
eraLength = eraLen.orElse(DEFAULT_ERA_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ public static ProtocolSpecBuilder gothamDefinition(
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
protocolSchedule) ->
protocolSchedule,
metricsSystem) ->
new ClassicBlockProcessor(
transactionProcessor,
transactionReceiptFactory,
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
ecip1017EraRounds,
protocolSchedule))
protocolSchedule,
metricsSystem))
.name("Gotham");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.List;

Expand All @@ -36,14 +37,16 @@ public MainnetBlockProcessor(
final Wei blockReward,
final MiningBeneficiaryCalculator miningBeneficiaryCalculator,
final boolean skipZeroBlockRewards,
final ProtocolSchedule protocolSchedule) {
final ProtocolSchedule protocolSchedule,
final MetricsSystem metricsSystem) {
super(
transactionProcessor,
transactionReceiptFactory,
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
protocolSchedule);
protocolSchedule,
metricsSystem);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import org.hyperledger.besu.ethereum.difficulty.fixed.FixedDifficultyCalculators;
import org.hyperledger.besu.ethereum.difficulty.fixed.FixedDifficultyProtocolSchedule;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.math.BigInteger;
import java.util.Optional;
import java.util.function.Function;

/** Provides {@link ProtocolSpec} lookups for mainnet hard forks. */
Expand All @@ -42,21 +45,46 @@ public static ProtocolSchedule fromConfig(
final GenesisConfigOptions config,
final PrivacyParameters privacyParameters,
final boolean isRevertReasonEnabled,
final EvmConfiguration evmConfiguration) {
final EvmConfiguration evmConfiguration,
final MetricsSystem metricsSystem) {
if (FixedDifficultyCalculators.isFixedDifficultyInConfig(config)) {
return FixedDifficultyProtocolSchedule.create(
config, privacyParameters, isRevertReasonEnabled, evmConfiguration);
}
return new ProtocolScheduleBuilder(
config,
DEFAULT_CHAIN_ID,
Optional.of(DEFAULT_CHAIN_ID),
ProtocolSpecAdapters.create(0, Function.identity()),
privacyParameters,
isRevertReasonEnabled,
evmConfiguration)
evmConfiguration,
metricsSystem)
.createProtocolSchedule();
}

/**
* Create a Mainnet protocol schedule from a config object
*
* @param config {@link GenesisConfigOptions} containing the config options for the milestone
* starting points
* @param privacyParameters the parameters set for private transactions
* @param isRevertReasonEnabled whether storing the revert reason is for failed transactions
* @param evmConfiguration how to configure the EVMs jumpdest cache
* @return A configured mainnet protocol schedule
*/
public static ProtocolSchedule fromConfig(
final GenesisConfigOptions config,
final PrivacyParameters privacyParameters,
final boolean isRevertReasonEnabled,
final EvmConfiguration evmConfiguration) {
return fromConfig(
config,
privacyParameters,
isRevertReasonEnabled,
evmConfiguration,
new NoOpMetricsSystem());
}

/**
* Create a Mainnet protocol schedule from a config object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.hyperledger.besu.ethereum.mainnet;

import static java.util.Objects.requireNonNull;

import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.PowAlgorithm;
import org.hyperledger.besu.datatypes.Address;
Expand Down Expand Up @@ -217,15 +219,17 @@ public static ProtocolSpecBuilder daoRecoveryInitDefinition(
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
protocolSchedule) ->
protocolSchedule,
metricsSystem) ->
new DaoBlockProcessor(
new MainnetBlockProcessor(
transactionProcessor,
transactionReceiptFactory,
blockReward,
miningBeneficiaryCalculator,
skipZeroBlockRewards,
protocolSchedule)))
protocolSchedule,
metricsSystem)))
.name("DaoRecoveryInit");
}

Expand Down Expand Up @@ -863,7 +867,8 @@ private void updateWorldStateForDao(final MutableWorldState worldState) {
final JsonArray json =
new JsonArray(
Resources.toString(
this.getClass().getResource("/daoAddresses.json"), StandardCharsets.UTF_8));
requireNonNull(this.getClass().getResource("/daoAddresses.json")),
StandardCharsets.UTF_8));
final List<Address> addresses =
IntStream.range(0, json.size())
.mapToObj(json::getString)
Expand Down
Loading

0 comments on commit a514614

Please sign in to comment.