Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9758376

Browse files
authored
Track last blocks in informant display (#6429)
This implements tracking of the last seen blocks in informant display to prevent printing the import message twice. In Cumulus we first import blocks as part of the block building with `new_best == false` and set the best block after we know which one was included by the relay chain. This leads to printing the import messages two times. This pr solves the problem by track the latest seen blocks to not print the message twice.
1 parent 3ca1d91 commit 9758376

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

client/informant/src/lib.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use sp_blockchain::HeaderMetadata;
2828
use sp_runtime::traits::{Block as BlockT, Header};
2929
use sp_transaction_pool::TransactionPool;
3030
use sp_utils::{status_sinks, mpsc::tracing_unbounded};
31-
use std::fmt::Display;
32-
use std::sync::Arc;
33-
use std::time::Duration;
31+
use std::{fmt::Display, sync::Arc, time::Duration, collections::VecDeque};
3432
use parking_lot::Mutex;
3533

3634
mod display;
@@ -96,12 +94,30 @@ where
9694
future::ready(())
9795
});
9896

97+
future::join(
98+
display_notifications,
99+
display_block_import(client, format.prefix),
100+
).map(|_| ())
101+
}
102+
103+
fn display_block_import<B: BlockT, C>(
104+
client: Arc<C>,
105+
prefix: String,
106+
) -> impl Future<Output = ()>
107+
where
108+
C: UsageProvider<B> + HeaderMetadata<B> + BlockchainEvents<B>,
109+
<C as HeaderMetadata<B>>::Error: Display,
110+
{
99111
let mut last_best = {
100112
let info = client.usage_info();
101113
Some((info.chain.best_number, info.chain.best_hash))
102114
};
103115

104-
let display_block_import = client.import_notification_stream().for_each(move |n| {
116+
// Hashes of the last blocks we have seen at import.
117+
let mut last_blocks = VecDeque::new();
118+
let max_blocks_to_track = 100;
119+
120+
client.import_notification_stream().for_each(move |n| {
105121
// detect and log reorganizations.
106122
if let Some((ref last_num, ref last_hash)) = last_best {
107123
if n.header.parent_hash() != last_hash && n.is_new_best {
@@ -114,7 +130,7 @@ where
114130
match maybe_ancestor {
115131
Ok(ref ancestor) if ancestor.hash != *last_hash => info!(
116132
"♻️ {}Reorg on #{},{} to #{},{}, common ancestor #{},{}",
117-
format.prefix,
133+
prefix,
118134
Colour::Red.bold().paint(format!("{}", last_num)), last_hash,
119135
Colour::Green.bold().paint(format!("{}", n.header.number())), n.hash,
120136
Colour::White.bold().paint(format!("{}", ancestor.number)), ancestor.hash,
@@ -129,18 +145,25 @@ where
129145
last_best = Some((n.header.number().clone(), n.hash.clone()));
130146
}
131147

132-
info!(
133-
target: "substrate",
134-
"✨ {}Imported #{} ({})",
135-
format.prefix,
136-
Colour::White.bold().paint(format!("{}", n.header.number())),
137-
n.hash,
138-
);
139-
future::ready(())
140-
});
141148

142-
future::join(
143-
display_notifications,
144-
display_block_import
145-
).map(|_| ())
149+
// If we already printed a message for a given block recently,
150+
// we should not print it again.
151+
if !last_blocks.contains(&n.hash) {
152+
last_blocks.push_back(n.hash.clone());
153+
154+
if last_blocks.len() > max_blocks_to_track {
155+
last_blocks.pop_front();
156+
}
157+
158+
info!(
159+
target: "substrate",
160+
"✨ {}Imported #{} ({})",
161+
prefix,
162+
Colour::White.bold().paint(format!("{}", n.header.number())),
163+
n.hash,
164+
);
165+
}
166+
167+
future::ready(())
168+
})
146169
}

0 commit comments

Comments
 (0)