From a0cf68ead87a089d7b7c30fefc83c6c2453eee0c Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Wed, 14 Aug 2024 18:04:39 -0700 Subject: [PATCH] fix(engine): aggregate state for chain notification (#10295) --- crates/chain-state/src/in_memory.rs | 49 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 942398f5c..069726277 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -16,7 +16,6 @@ use reth_storage_api::StateProviderBox; use reth_trie::{updates::TrieUpdates, HashedPostState}; use std::{ collections::{BTreeMap, HashMap}, - ops::Deref, sync::Arc, time::Instant, }; @@ -752,28 +751,34 @@ impl NewCanonicalChain { /// Converts the new chain into a notification that will be emitted to listeners pub fn to_chain_notification(&self) -> CanonStateNotification { - // TODO: do we need to merge execution outcome for multiblock commit or reorg? - // implement this properly match self { - Self::Commit { new } => CanonStateNotification::Commit { - new: Arc::new(Chain::new( - new.iter().map(ExecutedBlock::sealed_block_with_senders), - new.last().unwrap().execution_output.deref().clone(), - None, - )), - }, - Self::Reorg { new, old } => CanonStateNotification::Reorg { - new: Arc::new(Chain::new( - new.iter().map(ExecutedBlock::sealed_block_with_senders), - new.last().unwrap().execution_output.deref().clone(), - None, - )), - old: Arc::new(Chain::new( - old.iter().map(ExecutedBlock::sealed_block_with_senders), - old.last().unwrap().execution_output.deref().clone(), - None, - )), - }, + Self::Commit { new } => { + let new = Arc::new(new.iter().fold(Chain::default(), |mut chain, exec| { + chain.append_block( + exec.sealed_block_with_senders(), + exec.execution_outcome().clone(), + ); + chain + })); + CanonStateNotification::Commit { new } + } + Self::Reorg { new, old } => { + let new = Arc::new(new.iter().fold(Chain::default(), |mut chain, exec| { + chain.append_block( + exec.sealed_block_with_senders(), + exec.execution_outcome().clone(), + ); + chain + })); + let old = Arc::new(old.iter().fold(Chain::default(), |mut chain, exec| { + chain.append_block( + exec.sealed_block_with_senders(), + exec.execution_outcome().clone(), + ); + chain + })); + CanonStateNotification::Reorg { new, old } + } } }