-
Notifications
You must be signed in to change notification settings - Fork 293
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
Refactor system_call()
#976
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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,74 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2023 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "system_contracts.hpp" | ||
#include "host.hpp" | ||
#include "state.hpp" | ||
|
||
namespace evmone::state | ||
{ | ||
namespace | ||
{ | ||
/// Information about a registered system contract. | ||
struct SystemContract | ||
{ | ||
using GetInputFn = bytes_view(const BlockInfo&) noexcept; | ||
|
||
evmc_revision since = EVMC_MAX_REVISION; ///< EVM revision in which added. | ||
address addr; ///< Address of the system contract. | ||
GetInputFn* get_input = nullptr; ///< How to get the input for the system call. | ||
}; | ||
|
||
/// Registered system contracts. | ||
constexpr std::array SYSTEM_CONTRACTS{ | ||
SystemContract{EVMC_CANCUN, BEACON_ROOTS_ADDRESS, | ||
[](const BlockInfo& block) noexcept { return bytes_view{block.parent_beacon_block_root}; }}, | ||
}; | ||
|
||
static_assert(std::ranges::is_sorted(SYSTEM_CONTRACTS, | ||
[](const auto& a, const auto& b) noexcept { return a.since < b.since; }), | ||
"system contract entries must be ordered by revision"); | ||
|
||
} // namespace | ||
|
||
void system_call(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm) | ||
{ | ||
for (const auto& [since, addr, get_input] : SYSTEM_CONTRACTS) | ||
{ | ||
if (rev < since) | ||
return; // Because entries are ordered, there are no other contracts for this revision. | ||
|
||
// Skip the call if the target account doesn't exist. This is by EIP-4788 spec. | ||
// > if no code exists at [address], the call must fail silently. | ||
const auto acc = state.find(addr); | ||
if (acc == nullptr) | ||
continue; | ||
|
||
const auto input = get_input(block); | ||
|
||
const evmc_message msg{ | ||
.kind = EVMC_CALL, | ||
.gas = 30'000'000, | ||
.recipient = addr, | ||
.sender = SYSTEM_ADDRESS, | ||
.input_data = input.data(), | ||
.input_size = input.size(), | ||
}; | ||
|
||
const Transaction empty_tx{}; | ||
Host host{rev, vm, state, block, empty_tx}; | ||
const auto& code = acc->code; | ||
[[maybe_unused]] const auto res = vm.execute(host, rev, msg, code.data(), code.size()); | ||
assert(res.status_code == EVMC_SUCCESS); | ||
assert(acc->access_status == EVMC_ACCESS_COLD); | ||
|
||
// Reset storage status. | ||
for (auto& [_, val] : acc->storage) | ||
{ | ||
val.access_status = EVMC_ACCESS_COLD; | ||
val.original = val.current; | ||
} | ||
} | ||
} | ||
} // namespace evmone::state |
This file contains 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,26 @@ | ||
// evmone: Fast Ethereum Virtual Machine implementation | ||
// Copyright 2023 The evmone Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#pragma once | ||
|
||
#include <evmc/evmc.hpp> | ||
|
||
namespace evmone::state | ||
{ | ||
using namespace evmc::literals; | ||
|
||
/// The address of the sender of the system calls (EIP-4788). | ||
constexpr auto SYSTEM_ADDRESS = 0xfffffffffffffffffffffffffffffffffffffffe_address; | ||
|
||
/// The address of the system contract storing the root hashes of beacon chain blocks (EIP-4788). | ||
constexpr auto BEACON_ROOTS_ADDRESS = 0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02_address; | ||
|
||
struct BlockInfo; | ||
class State; | ||
|
||
/// Performs the system call: invokes system contracts. | ||
/// | ||
/// Executes code of pre-defined accounts via pseudo-transaction from the system sender (0xff...fe). | ||
/// The sender's nonce is not increased. | ||
void system_call(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm); | ||
} // namespace evmone::state |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2023 is the year then this code was introduced.