Skip to content

Commit e0ba24a

Browse files
committed
state: Nice system call
1 parent 95866c5 commit e0ba24a

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

test/blockchaintest/blockchaintest_runner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static TransitionResult apply_block(state::State& state, evmc::VM& vm,
3030
const state::BlockInfo& block, const std::vector<state::Transaction>& txs, evmc_revision rev,
3131
std::optional<int64_t> block_reward)
3232
{
33-
state::system_calls(state, block, rev, vm);
33+
state::system_call(state, block, rev, vm);
3434

3535
std::vector<state::Log> txs_logs;
3636
int64_t block_gas_left = block.gas_limit;

test/state/state.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,32 @@ void clear_empty_accounts(State& state)
146146
}
147147
} // namespace
148148

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

154154
if (rev >= EVMC_CANCUN)
155155
{
156-
evmc_message msg{};
157-
msg.kind = EVMC_CALL;
158-
msg.sender = SystemAddress;
159-
msg.recipient = BeaconRootsAddress;
160-
msg.code_address = BeaconRootsAddress;
161-
msg.gas = 30'000'000;
162-
msg.input_data = block.parent_beacon_block_root.bytes;
163-
msg.input_size = sizeof(block.parent_beacon_block_root);
164-
msg.depth = 1; // Prevent nonce bump.
165-
Host host{rev, vm, state, block, {}};
166-
[[maybe_unused]] const auto result = host.call(msg);
167-
assert(result.status_code == EVMC_SUCCESS);
168-
169-
// Set accounts and their storage access status to cold in the end of transition process
170-
for (auto& acc : state.get_accounts())
156+
if (const auto acc = state.find(BeaconRootsAddress); acc != nullptr)
171157
{
172-
acc.second.access_status = EVMC_ACCESS_COLD;
173-
for (auto& [_, val] : acc.second.storage)
158+
const evmc_message msg{
159+
.kind = EVMC_CALL,
160+
.gas = 30'000'000,
161+
.recipient = BeaconRootsAddress,
162+
.sender = SystemAddress,
163+
.input_data = block.parent_beacon_block_root.bytes,
164+
.input_size = sizeof(block.parent_beacon_block_root),
165+
};
166+
167+
Host host{rev, vm, state, block, {}};
168+
const auto& code = acc->code;
169+
[[maybe_unused]] const auto res = vm.execute(host, rev, msg, code.data(), code.size());
170+
assert(res.status_code == EVMC_SUCCESS);
171+
assert(acc->access_status == EVMC_ACCESS_COLD);
172+
173+
// Reset storage status.
174+
for (auto& [_, val] : acc->storage)
174175
{
175176
val.access_status = EVMC_ACCESS_COLD;
176177
val.original = val.current;

test/state/state.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ std::variant<int64_t, std::error_code> validate_transaction(const Account& sende
193193
const BlockInfo& block, const Transaction& tx, evmc_revision rev,
194194
int64_t block_gas_left) noexcept;
195195

196-
/// Performs system calls.
197-
void system_calls(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm);
196+
/// Performs the system call.
197+
///
198+
/// Executes code at pre-defined accounts from the system sender (0xff...fe).
199+
/// The sender's nonce is not increased.
200+
void system_call(State& state, const BlockInfo& block, evmc_revision rev, evmc::VM& vm);
198201

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

0 commit comments

Comments
 (0)