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

Implement System Calls #7263

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
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 @@ -241,10 +243,18 @@ protected BlockCreationResult createBlock(
// EIP-7685: process EL requests
final Optional<RequestProcessorCoordinator> requestProcessor =
newProtocolSpec.getRequestProcessorCoordinator();

ProcessRequestContext context =
new ProcessRequestContext(
processableBlockHeader,
disposableWorldState,
newProtocolSpec,
transactionResults.getReceipts(),
new CachingBlockHashLookup(processableBlockHeader, protocolContext.getBlockchain()),
operationTracer);

Optional<List<Request>> maybeRequests =
requestProcessor.flatMap(
processor ->
processor.process(disposableWorldState, transactionResults.getReceipts()));
requestProcessor.flatMap(processor -> processor.process(context));

throwIfStopped();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.CancunFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.requests.DepositRequestProcessor;
import org.hyperledger.besu.ethereum.mainnet.requests.DepositRequestValidator;
import org.hyperledger.besu.ethereum.mainnet.requests.ProcessRequestContext;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestProcessorCoordinator;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestsValidatorCoordinator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
Expand Down Expand Up @@ -135,7 +136,8 @@ void findDepositRequestsFromReceipts() {
final List<DepositRequest> expectedDepositRequests = List.of(expectedDepositRequest);

var depositRequestsFromReceipts =
new DepositRequestProcessor(DEFAULT_DEPOSIT_CONTRACT_ADDRESS).process(null, receipts);
new DepositRequestProcessor(DEFAULT_DEPOSIT_CONTRACT_ADDRESS)
.process(new ProcessRequestContext(null, null, null, receipts, null, null));
assertThat(depositRequestsFromReceipts.get()).isEqualTo(expectedDepositRequests);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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.mainnet.requests.ProcessRequestContext;
import org.hyperledger.besu.ethereum.mainnet.requests.RequestProcessorCoordinator;
import org.hyperledger.besu.ethereum.privacy.storage.PrivateMetadataUpdater;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
Expand Down Expand Up @@ -107,6 +108,7 @@ public BlockProcessingResult processBlock(
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockHeader(blockHeader);

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

for (final Transaction transaction : transactions) {
if (!hasAvailableBlockBudget(blockHeader, transaction, currentGasUsed)) {
Expand All @@ -115,7 +117,6 @@ public BlockProcessingResult processBlock(

final WorldUpdater worldStateUpdater = worldState.updater();

final BlockHashLookup blockHashLookup = new CachingBlockHashLookup(blockHeader, blockchain);
final Address miningBeneficiary =
miningBeneficiaryCalculator.calculateBeneficiary(blockHeader);

Expand Down Expand Up @@ -197,7 +198,16 @@ public BlockProcessingResult processBlock(
protocolSpec.getRequestProcessorCoordinator();
Optional<List<Request>> maybeRequests = Optional.empty();
if (requestProcessor.isPresent()) {
maybeRequests = requestProcessor.get().process(worldState, receipts);
ProcessRequestContext context =
new ProcessRequestContext(
blockHeader,
worldState,
protocolSpec,
receipts,
blockHashLookup,
OperationTracer.NO_TRACING);

maybeRequests = requestProcessor.get().process(context);
}

if (!rewardCoinbase(worldState, blockHeader, ommers, skipZeroBlockRewards)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public void process(final MessageFrame frame, final OperationTracer operationTra
executor.process(frame, operationTracer);
}

private AbstractMessageProcessor getMessageProcessor(final MessageFrame.Type type) {
public AbstractMessageProcessor getMessageProcessor(final MessageFrame.Type type) {
return switch (type) {
case MESSAGE_CALL -> messageCallProcessor;
case CONTRACT_CREATION -> contractCreationProcessor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright contributors to Hyperledger Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.mainnet;

import static org.hyperledger.besu.evm.frame.MessageFrame.DEFAULT_MAX_STACK_SIZE;

import org.hyperledger.besu.datatypes.Address;
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.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;

import java.util.Deque;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SystemCallProcessor {
private static final Logger LOG = LoggerFactory.getLogger(SystemCallProcessor.class);

/** The system address */
static final Address SYSTEM_ADDRESS =
Address.fromHexString("0xfffffffffffffffffffffffffffffffffffffffe");

private final MainnetTransactionProcessor mainnetTransactionProcessor;

public SystemCallProcessor(final MainnetTransactionProcessor mainnetTransactionProcessor) {
this.mainnetTransactionProcessor = mainnetTransactionProcessor;
}

/**
* Processes a system call to a specified address, using the provided world state, block header,
* operation tracer, and block hash lookup.
*
* @param callAddress the address to call.
* @param worldState the current world state.
* @param blockHeader the current block header.
* @param operationTracer the operation tracer for tracing EVM operations.
* @param blockHashLookup the block hash lookup function.
* @return the output data from the call
*/
public Bytes process(
final Address callAddress,
final WorldUpdater worldState,
final ProcessableBlockHeader blockHeader,
final OperationTracer operationTracer,
final BlockHashOperation.BlockHashLookup blockHashLookup) {

// if no code exists at CALL_ADDRESS, the call must fail silently
final Account maybeContract = worldState.get(callAddress);
if (maybeContract == null) {
LOG.trace("System call address not found {}", callAddress);
return null;
Gabriel-Trintinalia marked this conversation as resolved.
Show resolved Hide resolved
}

final AbstractMessageProcessor messageProcessor =
mainnetTransactionProcessor.getMessageProcessor(MessageFrame.Type.MESSAGE_CALL);
final MessageFrame initialFrame =
createCallFrame(callAddress, worldState, blockHeader, blockHashLookup);

return processFrame(initialFrame, messageProcessor, operationTracer, worldState);
}

private Bytes processFrame(
final MessageFrame frame,
final AbstractMessageProcessor processor,
final OperationTracer tracer,
final WorldUpdater updater) {

if (!frame.getCode().isValid()) {
throw new RuntimeException("System call did not execute to completion - opcode invalid");
}

Deque<MessageFrame> stack = frame.getMessageFrameStack();
while (!stack.isEmpty()) {
processor.process(stack.peekFirst(), tracer);
}

if (frame.getState() == MessageFrame.State.COMPLETED_SUCCESS) {
updater.commit();
return frame.getOutputData();
}

// the call must execute to completion
throw new RuntimeException("System call did not execute to completion");
}

private MessageFrame createCallFrame(
final Address callAddress,
final WorldUpdater worldUpdater,
final ProcessableBlockHeader blockHeader,
final BlockHashOperation.BlockHashLookup blockHashLookup) {

final Optional<Account> maybeContract = Optional.ofNullable(worldUpdater.get(callAddress));
final AbstractMessageProcessor processor =
mainnetTransactionProcessor.getMessageProcessor(MessageFrame.Type.MESSAGE_CALL);

return MessageFrame.builder()
.maxStackSize(DEFAULT_MAX_STACK_SIZE)
.worldUpdater(worldUpdater)
.initialGas(30_000_000L)
.originator(SYSTEM_ADDRESS)
.gasPrice(Wei.ZERO)
.blobGasPrice(Wei.ZERO)
.value(Wei.ZERO)
.apparentValue(Wei.ZERO)
.blockValues(blockHeader)
.completer(__ -> {})
.miningBeneficiary(Address.ZERO) // Confirm this
.type(MessageFrame.Type.MESSAGE_CALL)
.address(callAddress)
.contract(callAddress)
.inputData(Bytes.EMPTY)
.sender(SYSTEM_ADDRESS)
.blockHashLookup(blockHashLookup)
.code(
maybeContract
.map(c -> processor.getCodeFromEVM(c.getCodeHash(), c.getCode()))
.orElse(CodeV0.EMPTY_CODE))
.build();
}
}
Loading
Loading