@@ -236,6 +236,65 @@ void BlockState::merge(State const &state)
236236 }
237237}
238238
239+ void BlockState::merge (State const &state, BlockMetrics &block_metrics)
240+ {
241+ ankerl::unordered_dense::segmented_set<bytes32_t > code_hashes;
242+
243+ auto const ¤t = state.current ();
244+ for (auto const &[address, stack] : current) {
245+ MONAD_ASSERT (stack.size () == 1 );
246+ MONAD_ASSERT (stack.version () == 0 );
247+ auto const &account_state = stack.recent ();
248+ auto const &account = account_state.account_ ;
249+ if (account.has_value ()) {
250+ code_hashes.insert (account.value ().code_hash );
251+ }
252+ }
253+
254+ auto const &code = state.code ();
255+ for (auto const &code_hash : code_hashes) {
256+ auto const it = code.find (code_hash);
257+ if (it == code.end ()) {
258+ continue ;
259+ }
260+ code_.emplace (code_hash, it->second ->intercode ()); // TODO try_emplace
261+ }
262+
263+ MONAD_ASSERT (state_);
264+ for (auto const &[address, stack] : current) {
265+ auto const &account_state = stack.recent ();
266+ auto const &account = account_state.account_ ;
267+ auto const &storage = account_state.storage_ ;
268+ StateDeltas::accessor it{};
269+ MONAD_ASSERT (state_->find (it, address));
270+ it->second .account .second = account;
271+ if (account.has_value ()) {
272+ block_metrics.add_account_touched (1 );
273+ if (it->second .account .first != account) {
274+ block_metrics.add_account_changed (1 );
275+ }
276+ for (auto const &[key, value] : storage) {
277+ StorageDeltas::accessor it2{};
278+ block_metrics.add_storage_touched (1 );
279+ if (it->second .storage .find (it2, key)) {
280+ it2->second .second = value;
281+ if (it2->second .first != value) {
282+ block_metrics.add_storage_changed (1 );
283+ }
284+ }
285+ else {
286+ it->second .storage .emplace (
287+ key, std::make_pair (bytes32_t {}, value));
288+ block_metrics.add_storage_changed (1 );
289+ }
290+ }
291+ }
292+ else {
293+ it->second .storage .clear ();
294+ }
295+ }
296+ }
297+
239298void BlockState::commit (
240299 bytes32_t const &block_id, BlockHeader const &header,
241300 std::vector<Receipt> const &receipts,
0 commit comments