Skip to content

Commit 76cc85b

Browse files
committed
Hoist the state override struct definition into the State class.
1 parent 03a4692 commit 76cc85b

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

category/execution/ethereum/state3/state.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,61 @@ State::State(
100100
{
101101
}
102102

103+
State::State(
104+
BlockState &block_state, Incarnation const incarnation,
105+
bool const relaxed_validation, Overrides const &state_overrides)
106+
: block_state_{block_state}
107+
, incarnation_{incarnation}
108+
, relaxed_validation_{relaxed_validation}
109+
{
110+
for (auto const &[address, state_delta] : state_overrides) {
111+
OriginalAccountState &original_account_state =
112+
this->original_account_state(address);
113+
if (!original_account_state.account_.has_value()) {
114+
original_account_state.account_ =
115+
Account{.incarnation = incarnation};
116+
}
117+
Account &account = original_account_state.account_.value();
118+
119+
if (state_delta.balance.has_value()) {
120+
account.balance = state_delta.balance.value();
121+
}
122+
123+
if (state_delta.nonce.has_value()) {
124+
account.nonce = state_delta.nonce.value();
125+
}
126+
127+
if (state_delta.code.has_value()) {
128+
auto const code = state_delta.code.value();
129+
auto const code_hash = to_bytes(keccak256(code));
130+
code_[code_hash] = block_state_.vm().try_insert_varcode(
131+
code_hash, vm::make_shared_intercode(code));
132+
account.code_hash = code_hash;
133+
}
134+
135+
auto const update_state =
136+
[&](std::map<bytes32_t, bytes32_t> const &diff) {
137+
for (auto const &[key, value] : diff) {
138+
(void)original_account_state.set_storage(
139+
key, value, bytes32_t{});
140+
}
141+
};
142+
143+
// Remove single storage
144+
if (!state_delta.state_diff.empty()) {
145+
// we need to access the account first before accessing its
146+
// storage
147+
update_state(state_delta.state_diff);
148+
}
149+
150+
// Remove all override
151+
if (!state_delta.state.empty()) {
152+
account.incarnation = incarnation;
153+
update_state(state_delta.state);
154+
}
155+
}
156+
}
157+
103158
State::Map<Address, OriginalAccountState> const &State::original() const
104159
{
105160
return original_;

category/execution/ethereum/state3/state.hpp

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -71,64 +71,23 @@ class State
7171

7272
std::optional<Account> &current_account(Address const &);
7373

74+
public:
75+
struct Override
76+
{
77+
std::optional<uint256_t> balance{std::nullopt};
78+
std::optional<uint64_t> nonce{std::nullopt};
79+
std::optional<byte_string> code{std::nullopt};
80+
std::map<bytes32_t, bytes32_t> state{};
81+
std::map<bytes32_t, bytes32_t> state_diff{};
82+
};
83+
84+
using Overrides = std::map<Address, Override>;
85+
7486
public:
7587
State(BlockState &, Incarnation, bool relaxed_validation = false);
7688

77-
template <typename It>
7889
State(
79-
BlockState &block_state, Incarnation incarnation,
80-
bool relaxed_validation, It const state_overrides)
81-
: block_state_{block_state}
82-
, incarnation_{incarnation}
83-
, relaxed_validation_{relaxed_validation}
84-
{
85-
for (auto const &[address, state_delta] : state_overrides) {
86-
OriginalAccountState &original_account_state =
87-
this->original_account_state(address);
88-
if (!original_account_state.account_.has_value()) {
89-
original_account_state.account_ =
90-
Account{.incarnation = incarnation};
91-
}
92-
Account &account = original_account_state.account_.value();
93-
94-
if (state_delta.balance.has_value()) {
95-
account.balance = state_delta.balance.value();
96-
}
97-
98-
if (state_delta.nonce.has_value()) {
99-
account.nonce = state_delta.nonce.value();
100-
}
101-
102-
if (state_delta.code.has_value()) {
103-
auto const code = state_delta.code.value();
104-
auto const code_hash = to_bytes(keccak256(code));
105-
code_[code_hash] = block_state_.vm().try_insert_varcode(
106-
code_hash, vm::make_shared_intercode(code));
107-
account.code_hash = code_hash;
108-
}
109-
110-
auto const update_state =
111-
[&](std::map<bytes32_t, bytes32_t> const &diff) {
112-
for (auto const &[key, value] : diff) {
113-
(void)original_account_state.set_storage(
114-
key, value, bytes32_t{});
115-
}
116-
};
117-
118-
// Remove single storage
119-
if (!state_delta.state_diff.empty()) {
120-
// we need to access the account first before accessing its
121-
// storage
122-
update_state(state_delta.state_diff);
123-
}
124-
125-
// Remove all override
126-
if (!state_delta.state.empty()) {
127-
account.incarnation = incarnation;
128-
update_state(state_delta.state);
129-
}
130-
}
131-
}
90+
BlockState &, Incarnation, bool relaxed_validation, Overrides const &);
13291

13392
State(State &&) = delete;
13493
State(State const &) = delete;

category/rpc/eth_call.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ using namespace monad::vm;
6969

7070
struct monad_state_override
7171
{
72-
struct monad_state_override_object
73-
{
74-
std::optional<uint256_t> balance{std::nullopt};
75-
std::optional<uint64_t> nonce{std::nullopt};
76-
std::optional<byte_string> code{std::nullopt};
77-
std::map<bytes32_t, bytes32_t> state{};
78-
std::map<bytes32_t, bytes32_t> state_diff{};
79-
};
80-
81-
std::map<Address, monad_state_override_object> override_sets;
72+
std::map<Address, State::Override> override_sets{};
8273
};
8374

8475
namespace
@@ -90,7 +81,6 @@ namespace
9081
"failure to submit eth_call to thread pool: queue size exceeded";
9182
char const *const TIMEOUT_ERR_MSG =
9283
"failure to execute eth_call: queuing time exceeded timeout threshold";
93-
using StateOverrideObj = monad_state_override::monad_state_override_object;
9484

9585
template <Traits traits>
9686
Result<evmc::Result> eth_call_impl(
@@ -199,7 +189,7 @@ void add_override_address(
199189
std::memcpy(address.bytes, addr, sizeof(Address));
200190

201191
MONAD_ASSERT(m->override_sets.find(address) == m->override_sets.end());
202-
m->override_sets.emplace(address, StateOverrideObj{});
192+
m->override_sets.emplace(address, State::Override{});
203193
}
204194

205195
void set_override_balance(

0 commit comments

Comments
 (0)