@@ -122,82 +122,92 @@ namespace
122122 BlockState block_state{tdb, vm};
123123 // avoid conflict with block reward txn
124124 Incarnation const incarnation{block_number, Incarnation::LAST_TX - 1u };
125- State state{block_state, incarnation};
126-
127- for (auto const &[addr, state_delta] : state_overrides.override_sets ) {
128- // address
129- Address address{};
130- std::memcpy (address.bytes , addr.data (), sizeof (Address));
131-
132- // This would avoid seg-fault on storage override for non-existing
133- // accounts
134- auto const &account = state.recent_account (address);
135- if (MONAD_UNLIKELY (!account.has_value ())) {
136- state.create_contract (address);
137- }
138-
139- if (state_delta.balance .has_value ()) {
140- auto const balance = intx::be::unsafe::load<uint256_t >(
141- state_delta.balance .value ().data ());
142- if (balance >
143- intx::be::load<uint256_t >(
144- state.get_current_balance_pessimistic (address))) {
145- state.add_to_balance (
146- address,
147- balance - intx::be::load<uint256_t >(
148- state.get_current_balance_pessimistic (
149- address)));
125+ {
126+ State state{block_state, incarnation};
127+
128+ for (auto const &[addr, state_delta] :
129+ state_overrides.override_sets ) {
130+ // address
131+ Address address{};
132+ std::memcpy (address.bytes , addr.data (), sizeof (Address));
133+
134+ // This would avoid seg-fault on storage override for
135+ // non-existing accounts
136+ auto const &account = state.recent_account (address);
137+ if (MONAD_UNLIKELY (!account.has_value ())) {
138+ state.create_contract (address);
150139 }
151- else {
152- state.subtract_from_balance (
153- address,
140+
141+ if (state_delta.balance .has_value ()) {
142+ auto const balance = intx::be::unsafe::load<uint256_t >(
143+ state_delta.balance .value ().data ());
144+ if (balance >
154145 intx::be::load<uint256_t >(
155- state.get_current_balance_pessimistic (address)) -
156- balance);
146+ state.get_current_balance_pessimistic (address))) {
147+ state.add_to_balance (
148+ address,
149+ balance - intx::be::load<uint256_t >(
150+ state.get_current_balance_pessimistic (
151+ address)));
152+ }
153+ else {
154+ state.subtract_from_balance (
155+ address,
156+ intx::be::load<uint256_t >(
157+ state.get_current_balance_pessimistic (
158+ address)) -
159+ balance);
160+ }
157161 }
158- }
159-
160- if (state_delta.nonce .has_value ()) {
161- state.set_nonce (address, state_delta.nonce .value ());
162- }
163162
164- if (state_delta.code .has_value ()) {
165- byte_string const code{
166- state_delta.code .value ().data (),
167- state_delta.code .value ().size ()};
168- state.set_code (address, code);
169- }
163+ if (state_delta.nonce .has_value ()) {
164+ state.set_nonce (address, state_delta.nonce .value ());
165+ }
170166
171- auto const update_state =
172- [&](std::map<byte_string, byte_string> const &diff) {
173- for (auto const &[key, value] : diff) {
174- bytes32_t storage_key;
175- bytes32_t storage_value;
176- std::memcpy (
177- storage_key.bytes , key.data (), sizeof (bytes32_t ));
178- std::memcpy (
179- storage_value.bytes ,
180- value.data (),
181- sizeof (bytes32_t ));
182-
183- state.set_storage (address, storage_key, storage_value);
184- }
185- };
167+ if (state_delta.code .has_value ()) {
168+ byte_string const code{
169+ state_delta.code .value ().data (),
170+ state_delta.code .value ().size ()};
171+ state.set_code (address, code);
172+ }
186173
187- // Remove single storage
188- if (!state_delta.state_diff .empty ()) {
189- // we need to access the account first before accessing its
190- // storage
191- (void )state.get_nonce (address);
192- update_state (state_delta.state_diff );
193- }
174+ auto const update_state =
175+ [&](std::map<byte_string, byte_string> const &diff) {
176+ for (auto const &[key, value] : diff) {
177+ bytes32_t storage_key;
178+ bytes32_t storage_value;
179+ std::memcpy (
180+ storage_key.bytes ,
181+ key.data (),
182+ sizeof (bytes32_t ));
183+ std::memcpy (
184+ storage_value.bytes ,
185+ value.data (),
186+ sizeof (bytes32_t ));
187+
188+ state.set_storage (
189+ address, storage_key, storage_value);
190+ }
191+ };
192+
193+ // Remove single storage
194+ if (!state_delta.state_diff .empty ()) {
195+ // we need to access the account first before accessing its
196+ // storage
197+ (void )state.get_nonce (address);
198+ update_state (state_delta.state_diff );
199+ }
194200
195- // Remove all override
196- if (!state_delta.state .empty ()) {
197- state.set_to_state_incarnation (address);
198- update_state (state_delta.state );
201+ // Remove all override
202+ if (!state_delta.state .empty ()) {
203+ state.set_to_state_incarnation (address);
204+ update_state (state_delta.state );
205+ }
199206 }
207+ MONAD_ASSERT (block_state.can_merge (state));
208+ block_state.merge (state);
200209 }
210+ State state{block_state, incarnation};
201211
202212 // validate_transaction expects nonce to match.
203213 // However, eth_call doesn't take a nonce parameter.
0 commit comments