diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 9cd869c11..7b013bf9b 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -22,7 +22,8 @@ use reth_provider::{ chain::{ChainSplit, SplitAt}, post_state::PostState, BlockNumProvider, CanonStateNotification, CanonStateNotificationSender, - CanonStateNotifications, Chain, DatabaseProvider, ExecutorFactory, HeaderProvider, + CanonStateNotifications, Chain, DatabaseProvider, DisplayBlocksChain, ExecutorFactory, + HeaderProvider, }; use std::{ collections::{BTreeMap, HashMap}, @@ -923,8 +924,7 @@ impl BlockchainTree let chain_notification; info!( target: "blockchain_tree", - "Committing new canonical chain: {:?}", - new_canon_chain.blocks().iter().map(|(_, b)| b.num_hash()).collect::>() + "Committing new canonical chain: {}", DisplayBlocksChain(new_canon_chain.blocks()) ); // if joins to the tip; if new_canon_chain.fork_block_hash() == old_tip.hash { diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index 90928f13e..ff74d4ced 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -6,7 +6,7 @@ use reth_primitives::{ BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock, SealedBlockWithSenders, TransactionSigned, TxHash, }; -use std::{borrow::Cow, collections::BTreeMap}; +use std::{borrow::Cow, collections::BTreeMap, fmt}; /// A chain of blocks and their final state. /// @@ -217,6 +217,34 @@ impl Chain { } } +/// Wrapper type for `blocks` display in `Chain` +pub struct DisplayBlocksChain<'a>(pub &'a BTreeMap); + +impl<'a> fmt::Display for DisplayBlocksChain<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.0.len() <= 3 { + write!(f, "[")?; + let mut iter = self.0.values().map(|block| block.num_hash()); + if let Some(block_num_hash) = iter.next() { + write!(f, "{:?}", block_num_hash)?; + for block_num_hash_iter in iter { + write!(f, ", {:?}", block_num_hash_iter)?; + } + } + write!(f, "]")?; + } else { + write!( + f, + "[{:?}, ..., {:?}]", + self.0.values().next().unwrap().num_hash(), + self.0.values().last().unwrap().num_hash() + )?; + } + + Ok(()) + } +} + /// All blocks in the chain #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct ChainBlocks<'a> { diff --git a/crates/storage/provider/src/lib.rs b/crates/storage/provider/src/lib.rs index c290ed61e..51b743795 100644 --- a/crates/storage/provider/src/lib.rs +++ b/crates/storage/provider/src/lib.rs @@ -47,4 +47,4 @@ pub mod test_utils; pub use reth_interfaces::provider::ProviderError; pub mod chain; -pub use chain::Chain; +pub use chain::{Chain, DisplayBlocksChain};