Skip to content

Commit

Permalink
state: Nice system call
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Oct 30, 2023
1 parent a5efe7e commit 4111e5d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion test/blockchaintest/blockchaintest_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TransitionResult apply_block(state::State& state, evmc::VM& vm, const state::Blo
const std::vector<state::Transaction>& txs, evmc_revision rev,
std::optional<int64_t> block_reward)
{
state::system_calls(state, block, rev, vm);
state::system_call(state, block, rev, vm);

std::vector<state::Log> txs_logs;
int64_t block_gas_left = block.gas_limit;
Expand Down
37 changes: 19 additions & 18 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,32 @@ void delete_empty_accounts(State& state)
}
} // namespace

void system_calls(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm)
void system_call(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm)
{
static constexpr auto SystemAddress = 0xfffffffffffffffffffffffffffffffffffffffe_address;
static constexpr auto BeaconRootsAddress = 0xbEac00dDB15f3B6d645C48263dC93862413A222D_address;

if (rev >= EVMC_CANCUN)
{
evmc_message msg{};
msg.kind = EVMC_CALL;
msg.sender = SystemAddress;
msg.recipient = BeaconRootsAddress;
msg.code_address = BeaconRootsAddress;
msg.gas = 30'000'000;
msg.input_data = block.parent_beacon_block_root.bytes;
msg.input_size = sizeof(block.parent_beacon_block_root);
msg.depth = 1; // Prevent nonce bump.
Host host{rev, vm, state, block, {}};
[[maybe_unused]] const auto result = host.call(msg);
assert(result.status_code == EVMC_SUCCESS);

// Set accounts and their storage access status to cold in the end of transition process
for (auto& acc : state.get_accounts())
if (const auto acc = state.find(BeaconRootsAddress); acc != nullptr)
{
acc.second.access_status = EVMC_ACCESS_COLD;
for (auto& [_, val] : acc.second.storage)
const evmc_message msg{
.kind = EVMC_CALL,
.gas = 30'000'000,
.recipient = BeaconRootsAddress,
.sender = SystemAddress,
.input_data = block.parent_beacon_block_root.bytes,
.input_size = sizeof(block.parent_beacon_block_root),
};

Host host{rev, vm, state, block, {}};
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;
Expand Down
7 changes: 5 additions & 2 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ std::variant<int64_t, std::error_code> validate_transaction(const Account& sende
const BlockInfo& block, const Transaction& tx, evmc_revision rev,
int64_t block_gas_left) noexcept;

/// Performs system calls.
void system_calls(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm);
/// Performs the system call.
///
/// Executes code at pre-defined accounts 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);

/// Defines how to RLP-encode a Transaction.
[[nodiscard]] bytes rlp_encode(const Transaction& tx);
Expand Down

0 comments on commit 4111e5d

Please sign in to comment.