@@ -51,6 +51,7 @@ BlockState::BlockState(Db &db, vm::VM &monad_vm)
5151 : db_{db}
5252 , vm_{monad_vm}
5353 , state_(std::make_unique<StateDeltas>())
54+ , final_state_(std::make_unique<StateDeltas>())
5455{
5556}
5657
@@ -211,13 +212,26 @@ void BlockState::merge(State const &state)
211212 }
212213
213214 MONAD_ASSERT (state_);
215+ MONAD_ASSERT (final_state_);
214216 for (auto const &[address, stack] : current) {
215217 auto const &account_state = stack.recent ();
216218 auto const &account = account_state.account_ ;
217219 auto const &storage = account_state.storage_ ;
218220 StateDeltas::accessor it{};
219221 MONAD_ASSERT (state_->find (it, address));
220222 it->second .account .second = account;
223+ StateDeltas::accessor final_it{};
224+ if (final_state_->find (final_it, address)) {
225+ MONAD_ASSERT (
226+ final_it->second .account .first == it->second .account .first );
227+ final_it->second .account .second = account;
228+ }
229+ else {
230+ final_state_->emplace (
231+ final_it,
232+ address,
233+ StateDelta{.account = it->second .account , .storage = {}});
234+ }
221235 if (account.has_value ()) {
222236 for (auto const &[key, value] : storage) {
223237 StorageDeltas::accessor it2{};
@@ -226,12 +240,20 @@ void BlockState::merge(State const &state)
226240 }
227241 else {
228242 it->second .storage .emplace (
229- key, std::make_pair (bytes32_t {}, value));
243+ it2, key, std::make_pair (bytes32_t {}, value));
244+ }
245+ StorageDeltas::accessor final_it2{};
246+ if (final_it->second .storage .find (final_it2, key)) {
247+ final_it2->second .second = value;
248+ }
249+ else {
250+ final_it->second .storage .emplace (key, it2->second );
230251 }
231252 }
232253 }
233254 else {
234255 it->second .storage .clear ();
256+ final_it->second .storage .clear ();
235257 }
236258 }
237259}
@@ -246,7 +268,7 @@ void BlockState::commit(
246268 std::optional<std::vector<Withdrawal>> const &withdrawals)
247269{
248270 db_.commit (
249- std::move (state_ ),
271+ std::move (final_state_ ),
250272 code_,
251273 block_id,
252274 header,
@@ -262,6 +284,7 @@ void BlockState::log_debug()
262284{
263285 MONAD_ASSERT (state_);
264286 LOG_DEBUG (" State Deltas: {}" , *state_);
287+ LOG_DEBUG (" Final States: {}" , *final_state_);
265288 LOG_DEBUG (" Code Deltas: {}" , code_);
266289}
267290
0 commit comments