feat(blockchain-tree): add Display for blocks in Chain (#3230)

This commit is contained in:
Thomas Coratger
2023-06-19 19:54:16 +02:00
committed by GitHub
parent 79aa9cb2c2
commit c481987558
3 changed files with 33 additions and 5 deletions

View File

@ -22,7 +22,8 @@ use reth_provider::{
chain::{ChainSplit, SplitAt}, chain::{ChainSplit, SplitAt},
post_state::PostState, post_state::PostState,
BlockNumProvider, CanonStateNotification, CanonStateNotificationSender, BlockNumProvider, CanonStateNotification, CanonStateNotificationSender,
CanonStateNotifications, Chain, DatabaseProvider, ExecutorFactory, HeaderProvider, CanonStateNotifications, Chain, DatabaseProvider, DisplayBlocksChain, ExecutorFactory,
HeaderProvider,
}; };
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
@ -923,8 +924,7 @@ impl<DB: Database, C: Consensus, EF: ExecutorFactory> BlockchainTree<DB, C, EF>
let chain_notification; let chain_notification;
info!( info!(
target: "blockchain_tree", target: "blockchain_tree",
"Committing new canonical chain: {:?}", "Committing new canonical chain: {}", DisplayBlocksChain(new_canon_chain.blocks())
new_canon_chain.blocks().iter().map(|(_, b)| b.num_hash()).collect::<Vec<_>>()
); );
// if joins to the tip; // if joins to the tip;
if new_canon_chain.fork_block_hash() == old_tip.hash { if new_canon_chain.fork_block_hash() == old_tip.hash {

View File

@ -6,7 +6,7 @@ use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock, SealedBlockWithSenders, BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock, SealedBlockWithSenders,
TransactionSigned, TxHash, TransactionSigned, TxHash,
}; };
use std::{borrow::Cow, collections::BTreeMap}; use std::{borrow::Cow, collections::BTreeMap, fmt};
/// A chain of blocks and their final state. /// 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<BlockNumber, SealedBlockWithSenders>);
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 /// All blocks in the chain
#[derive(Clone, Debug, Default, PartialEq, Eq)] #[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ChainBlocks<'a> { pub struct ChainBlocks<'a> {

View File

@ -47,4 +47,4 @@ pub mod test_utils;
pub use reth_interfaces::provider::ProviderError; pub use reth_interfaces::provider::ProviderError;
pub mod chain; pub mod chain;
pub use chain::Chain; pub use chain::{Chain, DisplayBlocksChain};