-
Notifications
You must be signed in to change notification settings - Fork 1k
Implement System Calls #7263
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
Merged
Gabriel-Trintinalia
merged 14 commits into
besu-eth:main
from
Gabriel-Trintinalia:implement-system-call
Jul 4, 2024
Merged
Implement System Calls #7263
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3f762dc
Initial implementation of system call for withdrawal requests
Gabriel-Trintinalia 5044e90
Fix validations
Gabriel-Trintinalia 664a84f
Merge branch 'main' into implement-system-call
Gabriel-Trintinalia cce5d8f
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/main…
Gabriel-Trintinalia 6eaaa1a
Update ethereum/core/src/main/java/org/hyperledger/besu/ethereum/main…
Gabriel-Trintinalia 4c7fed3
remove helper
Gabriel-Trintinalia 6d11320
merge main into implement-system-call
Gabriel-Trintinalia ede6bc6
fix spotless
Gabriel-Trintinalia 4851d1f
Accept PR recommendations
Gabriel-Trintinalia ff6a90f
Accept PR recommendations
Gabriel-Trintinalia dfb4dbe
Merge branch 'main' into implement-system-call
Gabriel-Trintinalia b769a62
Merge branch 'main' into implement-system-call
jflo 87cdc4c
Do not throw exception if contract does not exist
Gabriel-Trintinalia 2a02f33
Merge branch 'implement-system-call' of https://github.com/Gabriel-Tr…
Gabriel-Trintinalia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/SystemCallProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.