Skip to content

Commit 213f2e1

Browse files
committed
Add "chainstate_verbose_block_ids" tracing target to print full block ids in functions that are called from p2p
1 parent 836bfc0 commit 213f2e1

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

chainstate/src/detail/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ type ChainstateEventHandler = EventHandler<ChainstateEvent>;
9494

9595
pub type OrphanErrorHandler = dyn Fn(&BlockError) + Send + Sync;
9696

97+
/// A tracing target that either forces full block ids to be printed where they're normally
98+
/// printed in the abbreviated form, or just makes block ids be printed where normally they won't
99+
/// be.
100+
pub const CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS: &str = "chainstate_verbose_block_ids";
101+
97102
#[must_use]
98103
pub struct Chainstate<S, V> {
99104
chain_config: Arc<ChainConfig>,
@@ -607,13 +612,9 @@ impl<S: BlockchainStorage, V: TransactionVerificationStrategy> Chainstate<S, V>
607612
None => result,
608613
};
609614

610-
if let Some(new_block_index) = &result {
611-
self.broadcast_new_tip_event(new_block_index);
612-
} else {
613-
log::debug!("Stale block received: {block_id:x}");
614-
}
615+
if let Some(bi) = &result {
616+
self.broadcast_new_tip_event(bi);
615617

616-
if let Some(ref bi) = result {
617618
let compact_target = match bi.block_header().consensus_data() {
618619
common::chain::block::ConsensusData::None => Compact::from(Uint256::ZERO),
619620
common::chain::block::ConsensusData::PoW(data) => data.bits(),
@@ -634,6 +635,11 @@ impl<S: BlockchainStorage, V: TransactionVerificationStrategy> Chainstate<S, V>
634635

635636
self.update_initial_block_download_flag()
636637
.map_err(BlockError::BestBlockIdQueryError)?;
638+
} else {
639+
tracing::debug!(
640+
target: CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS,
641+
"Stale block received: {block_id}"
642+
);
637643
}
638644

639645
Ok(result)

chainstate/src/interface/chainstate_interface_impl.rs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
bootstrap::{export_bootstrap_stream, import_bootstrap_stream},
2424
calculate_median_time_past,
2525
tx_verification_strategy::TransactionVerificationStrategy,
26-
BlockSource, OrphanBlocksRef,
26+
BlockSource, OrphanBlocksRef, CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS,
2727
},
2828
ChainInfo, ChainstateConfig, ChainstateError, ChainstateEvent, ChainstateInterface, Locator,
2929
NonZeroPoolBalances,
@@ -71,7 +71,20 @@ where
7171
self.chainstate.subscribe_to_event_broadcast()
7272
}
7373

74-
#[tracing::instrument(skip_all, fields(block_id = %block.get_id()))]
74+
// Note: in this and some other functions below (in particular, in those that are called from
75+
// p2p when processing blocks coming from peers) we add an additional DEBUG span that prints
76+
// the block via `format!("{:x}")`. This is because the other span prints the id via Display
77+
// (due to the '%' sigil), in which case it is shortened, e.g. "778b…b100".
78+
// Always printing the full id would clutter the log, so we don't want to do that.
79+
// So we add an additional span for the cases when the full id is needed.
80+
// Also note that we add the extra span first, but in the output it will be printed after
81+
// the normal one.
82+
#[tracing::instrument(
83+
skip_all, level = tracing::Level::DEBUG, name = "",
84+
fields(id = format!("{:x}", block.get_id())),
85+
target = CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS
86+
)]
87+
#[tracing::instrument(skip_all, fields(id = %block.get_id()))]
7588
fn process_block(
7689
&mut self,
7790
block: Block,
@@ -82,23 +95,34 @@ where
8295
.map_err(ChainstateError::ProcessBlockError)
8396
}
8497

85-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
98+
#[tracing::instrument(skip_all, fields(id = %block_id))]
8699
fn invalidate_block(&mut self, block_id: &Id<Block>) -> Result<(), ChainstateError> {
87100
self.chainstate
88101
.invalidate_block(block_id)
89102
.map_err(ChainstateError::BlockInvalidatorError)
90103
}
91104

92-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
105+
#[tracing::instrument(skip_all, fields(id = %block_id))]
93106
fn reset_block_failure_flags(&mut self, block_id: &Id<Block>) -> Result<(), ChainstateError> {
94107
BlockInvalidator::new(&mut self.chainstate)
95108
.reset_block_failure_flags(block_id)
96109
.map_err(ChainstateError::BlockInvalidatorError)
97110
}
98111

112+
#[tracing::instrument(
113+
skip_all, level = tracing::Level::DEBUG, name = "",
114+
fields(first_id =
115+
if let Some(first_header) = headers.first() {
116+
format!("{:x}", first_header.get_id())
117+
} else {
118+
"None".to_owned()
119+
}
120+
),
121+
target = CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS
122+
)]
99123
#[tracing::instrument(
100124
skip_all,
101-
fields(first_block_id = %headers.first().map(|header| header.get_id()).as_displayable())
125+
fields(first_id = %headers.first().map(|header| header.get_id()).as_displayable())
102126
)]
103127
fn preliminary_headers_check(
104128
&self,
@@ -109,7 +133,12 @@ where
109133
.map_err(ChainstateError::ProcessBlockError)
110134
}
111135

112-
#[tracing::instrument(skip_all, fields(block_id = %block.get_id()))]
136+
#[tracing::instrument(
137+
skip_all, level = tracing::Level::DEBUG, name = "",
138+
fields(id = format!("{:x}", block.get_id())),
139+
target = CHAINSTATE_TRACING_TARGET_VERBOSE_BLOCK_IDS
140+
)]
141+
#[tracing::instrument(skip_all, fields(id = %block.get_id()))]
113142
fn preliminary_block_check(&self, block: Block) -> Result<Block, ChainstateError> {
114143
let block = BlockChecker::new(&self.chainstate)
115144
.preliminary_block_check(block.into())
@@ -126,7 +155,7 @@ where
126155
.map_err(ChainstateError::FailedToReadProperty)
127156
}
128157

129-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
158+
#[tracing::instrument(skip_all, fields(id = %block_id))]
130159
fn is_block_in_main_chain(&self, block_id: &Id<GenBlock>) -> Result<bool, ChainstateError> {
131160
self.chainstate
132161
.query()
@@ -144,7 +173,7 @@ where
144173
.map_err(ChainstateError::FailedToReadProperty)
145174
}
146175

147-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
176+
#[tracing::instrument(skip_all, fields(id = %block_id))]
148177
fn get_block_height_in_main_chain(
149178
&self,
150179
block_id: &Id<GenBlock>,
@@ -168,7 +197,7 @@ where
168197
.map_err(ChainstateError::FailedToReadProperty)
169198
}
170199

171-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
200+
#[tracing::instrument(skip_all, fields(id = %block_id))]
172201
fn get_block(&self, block_id: Id<Block>) -> Result<Option<Block>, ChainstateError> {
173202
self.chainstate
174203
.query()
@@ -190,7 +219,7 @@ where
190219
.map_err(ChainstateError::FailedToReadProperty)
191220
}
192221

193-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
222+
#[tracing::instrument(skip_all, fields(id = %block_id))]
194223
fn get_block_header(
195224
&self,
196225
block_id: Id<Block>,
@@ -311,7 +340,7 @@ where
311340
.map_err(ChainstateError::FailedToReadProperty)
312341
}
313342

314-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
343+
#[tracing::instrument(skip_all, fields(id = %block_id))]
315344
fn get_block_index_for_persisted_block(
316345
&self,
317346
block_id: &Id<Block>,
@@ -323,7 +352,7 @@ where
323352
.map_err(ChainstateError::FailedToReadProperty)
324353
}
325354

326-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
355+
#[tracing::instrument(skip_all, fields(id = %block_id))]
327356
fn get_block_index_for_any_block(
328357
&self,
329358
block_id: &Id<Block>,
@@ -389,7 +418,7 @@ where
389418
Ok(calculate_median_time_past(&dbtx, starting_block))
390419
}
391420

392-
#[tracing::instrument(skip_all, fields(block_id = %block_id))]
421+
#[tracing::instrument(skip_all, fields(id = %block_id))]
393422
fn is_already_an_orphan(&self, block_id: &Id<Block>) -> bool {
394423
self.chainstate.orphan_blocks_pool().is_already_an_orphan(block_id)
395424
}
@@ -401,7 +430,7 @@ where
401430

402431
#[tracing::instrument(
403432
skip_all,
404-
fields(block_id = %block_index.block_id(), ancestor_height = %ancestor_height)
433+
fields(id = %block_index.block_id(), ancestor_height = %ancestor_height)
405434
)]
406435
fn get_ancestor(
407436
&self,
@@ -420,8 +449,8 @@ where
420449
#[tracing::instrument(
421450
skip_all,
422451
fields(
423-
first_block_id = %first_block_index.block_id(),
424-
second_block_id = %second_block_index.block_id()
452+
first_id = %first_block_index.block_id(),
453+
second_id = %second_block_index.block_id()
425454
)
426455
)]
427456
fn last_common_ancestor(
@@ -461,7 +490,7 @@ where
461490
}
462491
}
463492

464-
#[tracing::instrument(skip_all, fields(block_id = %block_index.block_id()))]
493+
#[tracing::instrument(skip_all, fields(id = %block_index.block_id()))]
465494
fn get_block_reward(
466495
&self,
467496
block_index: &BlockIndex,

mempool/src/pool/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ impl<M: MemoryUsageEstimator> Mempool<M> {
220220
&mut self,
221221
evt: ChainstateEvent,
222222
) -> Result<(), ChainstateEventError> {
223-
log::debug!("mempool: Processing chainstate event {evt:?}");
223+
log::debug!("Processing chainstate event {evt:?}");
224224
match evt {
225225
ChainstateEvent::NewTip(block_id, height) => self.on_new_tip(block_id, height)?,
226226
};
227227
Ok(())
228228
}
229229

230230
fn on_new_tip(&mut self, block_id: Id<Block>, height: BlockHeight) -> Result<(), ReorgError> {
231-
log::info!("New block tip: {block_id:?} at height {height}");
231+
log::debug!("New block tip: {block_id:x} at height {height}");
232232

233233
let mut finalizer = TxFinalizer::new(
234234
&mut self.orphans,

p2p/src/sync/peer/block_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,13 +716,13 @@ where
716716
self.peer_activity.set_expecting_blocks_since(Some(self.time_getter.get_time()));
717717
}
718718

719-
let block = self.chainstate_handle.call(|c| Ok(c.preliminary_block_check(block)?)).await?;
720-
721719
// Process the block and also determine the new value for peers_best_block_that_we_have.
722720
let old_peers_best_block_that_we_have = self.incoming.peers_best_block_that_we_have;
723721
let (best_block, new_tip_received) = self
724722
.chainstate_handle
725723
.call_mut(move |c| {
724+
let block = c.preliminary_block_check(block)?;
725+
726726
// If the block already exists in the block tree, skip it.
727727
let new_tip_received =
728728
if c.get_block_index_for_persisted_block(&block.get_id())?.is_some() {

0 commit comments

Comments
 (0)