Skip to content

Commit

Permalink
eip-7709 implement BLOCKHASH opcode from system contract state (#7971)
Browse files Browse the repository at this point in the history
* eip-7709 implement BLOCKHASH opcode from system contract state

Signed-off-by: Luis Pinto <[email protected]>

* fixup! eip-7709 implement BLOCKHASH opcode from system contract state

 reimplement blockhashlookup with MessageFrame instead of WorldUpdater

Signed-off-by: Luis Pinto <[email protected]>

* fixup! eip-7709 implement BLOCKHASH opcode from system contract state

 address review comments

Signed-off-by: Luis Pinto <[email protected]>

* fixup! eip-7709 implement BLOCKHASH opcode from system contract state

 add comment about unused BlockHashProcessor

Signed-off-by: Luis Pinto <[email protected]>

---------

Signed-off-by: Luis Pinto <[email protected]>
  • Loading branch information
lu-pinto authored Dec 20, 2024
1 parent c72d02e commit b3b33da
Show file tree
Hide file tree
Showing 67 changed files with 699 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockTraceResult;
Expand Down Expand Up @@ -217,7 +216,9 @@ private List<TransactionProcessingResult> trace(
transaction,
protocolSpec.getMiningBeneficiaryCalculator().calculateBeneficiary(header),
tracer,
new CachingBlockHashLookup(header, blockchain),
protocolSpec
.getBlockHashProcessor()
.createBlockHashLookup(blockchain, header),
false,
blobGasPrice);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
import org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -95,7 +94,8 @@ public TransactionTrace apply(final TransactionTrace transactionTrace) {
maybeParentHeader
.map(parent -> calculateExcessBlobGasForParent(protocolSpec, parent))
.orElse(BlobGas.ZERO));
final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(header, blockchain);
final BlockHashLookup blockHashLookup =
protocolSpec.getBlockHashProcessor().createBlockHashLookup(blockchain, header);
result =
transactionProcessor.processTransaction(
chainUpdater.getNextUpdater(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -90,7 +89,8 @@ public <T> Optional<T> beforeTransactionInBlock(
return performActionWithBlock(
blockHash,
(body, header, blockchain, transactionProcessor, protocolSpec) -> {
final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(header, blockchain);
final BlockHashLookup blockHashLookup =
protocolSpec.getBlockHashProcessor().createBlockHashLookup(blockchain, header);
final Wei blobGasPrice =
protocolSpec
.getFeeMarket()
Expand Down Expand Up @@ -137,7 +137,7 @@ public <T> Optional<T> afterTransactionInBlock(
blockHeader,
transaction,
spec.getMiningBeneficiaryCalculator().calculateBeneficiary(blockHeader),
new CachingBlockHashLookup(blockHeader, blockchain),
spec.getBlockHashProcessor().createBlockHashLookup(blockchain, blockHeader),
false,
TransactionValidationParams.blockReplay(),
blobGasPrice);
Expand Down Expand Up @@ -180,6 +180,10 @@ private Optional<Block> getBlock(final Hash blockHash) {
return Optional.empty();
}

public ProtocolSpec getProtocolSpec(final BlockHeader header) {
return protocolSchedule.getByBlockHeader(header);
}

@FunctionalInterface
public interface BlockAction<T> {
Optional<T> perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.debug.TraceFrame;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

Expand Down Expand Up @@ -69,7 +68,10 @@ private BlockReplay.TransactionAction<TransactionTrace> prepareReplayAction(
transaction,
header.getCoinbase(),
tracer,
new CachingBlockHashLookup(header, blockchain),
blockReplay
.getProtocolSpec(header)
.getBlockHashProcessor()
.createBlockHashLookup(blockchain, header),
false,
blobGasPrice);
final List<TraceFrame> traceFrames = tracer.copyTraceFrames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.hyperledger.besu.ethereum.mainnet.ImmutableTransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.tracing.StandardJsonTracer;
Expand Down Expand Up @@ -192,7 +191,10 @@ private TransactionProcessingResult processTransaction(
transaction,
header.getCoinbase(),
tracer,
new CachingBlockHashLookup(header, blockchain),
blockReplay
.getProtocolSpec(header)
.getBlockHashProcessor()
.createBlockHashLookup(blockchain, header),
false,
ImmutableTransactionValidationParams.builder().isAllowFutureNonce(true).build(),
blobGasPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ private <T> Optional<T> performActionWithBlock(
return action.perform(body, header, blockchain, transactionProcessor, protocolSpec);
}

public ProtocolSpec getProtocolSpec(final BlockHeader header) {
return protocolSchedule.getByBlockHeader(header);
}

@FunctionalInterface
public interface BlockAction<T> {
Optional<T> perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hyperledger.besu.ethereum.debug.TraceFrame;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateBlockMetadata;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
import org.hyperledger.besu.evm.worldstate.StackedUpdater;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
Expand Down Expand Up @@ -76,7 +75,10 @@ private PrivateBlockReplay.TransactionAction<PrivateTransactionTrace> prepareRep
transaction,
header.getCoinbase(),
tracer,
new CachingBlockHashLookup(header, blockchain),
blockReplay
.getProtocolSpec(header)
.getBlockHashProcessor()
.createBlockHashLookup(blockchain, header),
Bytes32.wrap(Bytes.fromBase64String(privacyGroupId)));

final List<TraceFrame> traceFrames = tracer.copyTraceFrames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.blockhash.BlockHashProcessor;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.DebugOperationTracer;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class TransactionTracerTest {

@Mock private ProtocolSpec protocolSpec;
@Mock private GasCalculator gasCalculator;
@Mock private BlockHashProcessor blockHashProcessor;

@Mock private Tracer.TraceableState mutableWorldState;

Expand Down Expand Up @@ -120,6 +122,7 @@ public void setUp() throws Exception {
when(protocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
when(blockchain.getChainHeadHeader()).thenReturn(blockHeader);
when(protocolSpec.getGasCalculator()).thenReturn(gasCalculator);
when(protocolSpec.getBlockHashProcessor()).thenReturn(blockHashProcessor);
when(protocolContext.getBadBlockManager()).thenReturn(badBlockManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator;
import org.hyperledger.besu.ethereum.mainnet.requests.ProcessRequestContext;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestProcessorCoordinator;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.exception.StorageException;
Expand Down Expand Up @@ -212,8 +211,7 @@ protected BlockCreationResult createBlock(

newProtocolSpec
.getBlockHashProcessor()
.processBlockHashes(
protocolContext.getBlockchain(), disposableWorldState, processableBlockHeader);
.processBlockHashes(disposableWorldState, processableBlockHeader);

throwIfStopped();

Expand Down Expand Up @@ -260,7 +258,9 @@ protected BlockCreationResult createBlock(
disposableWorldState,
newProtocolSpec,
transactionResults.getReceipts(),
new CachingBlockHashLookup(processableBlockHeader, protocolContext.getBlockchain()),
newProtocolSpec
.getBlockHashProcessor()
.createBlockHashLookup(protocolContext.getBlockchain(), processableBlockHeader),
operationTracer);

Optional<List<Request>> maybeRequests =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
import org.hyperledger.besu.ethereum.mainnet.blockhash.BlockHashProcessor;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.data.TransactionSelectionResult;
import org.hyperledger.besu.plugin.services.tracer.BlockAwareOperationTracer;
Expand Down Expand Up @@ -376,7 +375,9 @@ private TransactionSelectionResult evaluatePostProcessing(
private TransactionProcessingResult processTransaction(
final PendingTransaction pendingTransaction, final WorldUpdater worldStateUpdater) {
final BlockHashLookup blockHashLookup =
new CachingBlockHashLookup(blockSelectionContext.pendingBlockHeader(), blockchain);
blockSelectionContext
.blockHashProcessor()
.createBlockHashLookup(blockchain, blockSelectionContext.pendingBlockHeader());
return transactionProcessor.processTransaction(
worldStateUpdater,
blockSelectionContext.pendingBlockHeader(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;

import org.hyperledger.besu.config.GenesisConfig;
import org.hyperledger.besu.crypto.KeyPair;
Expand All @@ -34,11 +33,13 @@
import org.hyperledger.besu.ethereum.debug.TraceOptions;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;

import java.util.List;
Expand Down Expand Up @@ -75,11 +76,14 @@ public void setUp() {
blockchain = contextTestFixture.getBlockchain();
worldStateArchive = contextTestFixture.getStateArchive();
final ProtocolSchedule protocolSchedule = contextTestFixture.getProtocolSchedule();
transactionProcessor =
protocolSchedule
.getByBlockHeader(new BlockHeaderTestFixture().number(0L).buildHeader())
.getTransactionProcessor();
blockHashLookup = new CachingBlockHashLookup(genesisBlock.getHeader(), blockchain);
final ProtocolSpec protocolSpec =
protocolSchedule.getByBlockHeader(new BlockHeaderTestFixture().number(0L).buildHeader());

transactionProcessor = protocolSpec.getTransactionProcessor();
blockHashLookup =
protocolSpec
.getBlockHashProcessor()
.createBlockHashLookup(blockchain, genesisBlock.getHeader());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.hyperledger.besu.ethereum.vm.operations;

import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.ethereum.vm.BlockchainBasedBlockHashLookup;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.PetersburgGasCalculator;
import org.hyperledger.besu.evm.operation.BlockHashOperation;
Expand Down Expand Up @@ -68,7 +68,7 @@ public Bytes executeOperationWithEmptyHashCache() {
operationBenchmarkHelper
.createMessageFrameBuilder()
.blockHashLookup(
new CachingBlockHashLookup(
new BlockchainBasedBlockHashLookup(
(ProcessableBlockHeader) frame.getBlockValues(),
operationBenchmarkHelper.getBlockchain()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.hyperledger.besu.ethereum.mainnet;

import static org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator.calculateExcessBlobGasForParent;
import static org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.TransactionType;
Expand All @@ -36,8 +35,7 @@
import org.hyperledger.besu.ethereum.trie.MerkleTrieException;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldState;
import org.hyperledger.besu.ethereum.trie.diffbased.bonsai.worldview.BonsaiWorldStateUpdateAccumulator;
import org.hyperledger.besu.ethereum.vm.CachingBlockHashLookup;
import org.hyperledger.besu.evm.operation.BlockHashOperation;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldState;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
Expand Down Expand Up @@ -107,8 +105,9 @@ public BlockProcessingResult processBlock(

final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader);

protocolSpec.getBlockHashProcessor().processBlockHashes(blockchain, worldState, blockHeader);
final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(blockHeader, blockchain);
protocolSpec.getBlockHashProcessor().processBlockHashes(worldState, blockHeader);
final BlockHashLookup blockHashLookup =
protocolSpec.getBlockHashProcessor().createBlockHashLookup(blockchain, blockHeader);

final Address miningBeneficiary = miningBeneficiaryCalculator.calculateBeneficiary(blockHeader);

Expand Down Expand Up @@ -262,7 +261,7 @@ protected Optional<PreprocessingContext> runBlockPreProcessing(
final BlockHeader blockHeader,
final List<Transaction> transactions,
final Address miningBeneficiary,
final BlockHashOperation.BlockHashLookup blockHashLookup,
final BlockHashLookup blockHashLookup,
final Wei blobGasPrice) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.hyperledger.besu.ethereum.mainnet.PrivateStateUtils.KEY_PRIVATE_METADATA_UPDATER;
import static org.hyperledger.besu.ethereum.mainnet.PrivateStateUtils.KEY_TRANSACTION;
import static org.hyperledger.besu.ethereum.mainnet.PrivateStateUtils.KEY_TRANSACTION_HASH;
import static org.hyperledger.besu.evm.operation.BlockHashOperation.BlockHashLookup;

import org.hyperledger.besu.collections.trie.BytesTrieSet;
import org.hyperledger.besu.datatypes.AccessListEntry;
Expand All @@ -35,6 +34,7 @@
import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.code.CodeInvalid;
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.operation.BlockHashOperation;
import org.hyperledger.besu.evm.processor.AbstractMessageProcessor;
import org.hyperledger.besu.evm.tracing.OperationTracer;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
Expand Down Expand Up @@ -64,7 +64,7 @@ public Bytes process(
final WorldUpdater worldState,
final ProcessableBlockHeader blockHeader,
final OperationTracer operationTracer,
final BlockHashOperation.BlockHashLookup blockHashLookup) {
final BlockHashLookup blockHashLookup) {

// if no code exists at CALL_ADDRESS, the call must fail silently
final Account maybeContract = worldState.get(callAddress);
Expand Down Expand Up @@ -109,7 +109,7 @@ private MessageFrame createCallFrame(
final Address callAddress,
final WorldUpdater worldUpdater,
final ProcessableBlockHeader blockHeader,
final BlockHashOperation.BlockHashLookup blockHashLookup) {
final BlockHashLookup blockHashLookup) {

final Optional<Account> maybeContract = Optional.ofNullable(worldUpdater.get(callAddress));
final AbstractMessageProcessor processor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.plugin.data.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.evm.blockhash.BlockHashLookup;

public interface BlockHashProcessor {

void processBlockHashes(
Blockchain blockchain,
MutableWorldState worldState,
ProcessableBlockHeader currentBlockHeader);
void processBlockHashes(MutableWorldState worldState, ProcessableBlockHeader currentBlockHeader);

BlockHashLookup createBlockHashLookup(Blockchain blockchain, ProcessableBlockHeader blockHeader);
}
Loading

0 comments on commit b3b33da

Please sign in to comment.