Skip to content

Commit a0c3f0c

Browse files
committed
refactor!: introduce Info and separate info/debug strings
1 parent f9539ae commit a0c3f0c

File tree

11 files changed

+156
-112
lines changed

11 files changed

+156
-112
lines changed

example/managed.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn main() {
4646
// The number of connections we would like to maintain
4747
.required_peers(1)
4848
// Omit informational messages
49-
.log_level(LogLevel::Warning)
49+
.log_level(LogLevel::Info)
5050
// Create the node and client
5151
.build()
5252
.unwrap();
@@ -55,17 +55,23 @@ async fn main() {
5555

5656
let Client {
5757
requester,
58-
mut log_rx,
59-
warn_rx: _,
58+
log_rx: _,
59+
mut info_rx,
60+
mut warn_rx,
6061
mut event_rx,
6162
} = client;
6263

6364
// Continually listen for events until the node is synced to its peers.
6465
loop {
6566
tokio::select! {
66-
log = log_rx.recv() => {
67-
if let Some(log) = log {
68-
tracing::info!("{log}");
67+
info = info_rx.recv() => {
68+
if let Some(info) = info {
69+
tracing::info!("{info}");
70+
}
71+
}
72+
warn = warn_rx.recv() => {
73+
if let Some(warn) = warn {
74+
tracing::warn!("{warn}");
6975
}
7076
}
7177
event = event_rx.recv() => {

example/rescan.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async fn main() {
4545
let Client {
4646
requester,
4747
mut log_rx,
48+
mut info_rx,
4849
mut warn_rx,
4950
mut event_rx,
5051
} = client;
@@ -63,6 +64,11 @@ async fn main() {
6364
tracing::info!("{log}");
6465
}
6566
}
67+
info = info_rx.recv() => {
68+
if let Some(info) = info {
69+
tracing::info!("{info}");
70+
}
71+
}
6672
warn = warn_rx.recv() => {
6773
if let Some(warn) = warn {
6874
tracing::warn!("{warn}");

example/signet.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Usual sync on Signet.
22
33
use kyoto::{builder::NodeBuilder, chain::checkpoints::HeaderCheckpoint};
4-
use kyoto::{AddrV2, Address, Client, Event, Log, Network, ServiceFlags, TrustedPeer};
4+
use kyoto::{AddrV2, Address, Client, Event, Info, Network, ServiceFlags, TrustedPeer};
55
use std::collections::HashSet;
66
use std::{
77
net::{IpAddr, Ipv4Addr},
@@ -57,6 +57,7 @@ async fn main() {
5757
let Client {
5858
requester,
5959
mut log_rx,
60+
mut info_rx,
6061
mut warn_rx,
6162
mut event_rx,
6263
} = client;
@@ -84,16 +85,20 @@ async fn main() {
8485
}
8586
}
8687
}
87-
log = log_rx.recv() => {
88-
if let Some(log) = log {
89-
match log {
90-
Log::Debug(d)=> tracing::info!("{d}"),
91-
Log::StateChange(node_state) => tracing::info!("{node_state}"),
92-
Log::ConnectionsMet => tracing::info!("All required connections met"),
88+
info = info_rx.recv() => {
89+
if let Some(info) = info {
90+
match info {
91+
Info::StateChange(node_state) => tracing::info!("{node_state}"),
92+
Info::ConnectionsMet => tracing::info!("All required connections met"),
9393
_ => (),
9494
}
9595
}
9696
}
97+
log = log_rx.recv() => {
98+
if let Some(log) = log {
99+
tracing::info!("{log}");
100+
}
101+
}
97102
warn = warn_rx.recv() => {
98103
if let Some(warn) = warn {
99104
tracing::warn!("{warn}");

example/testnet4.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Usual sync on Testnet.
22
33
use kyoto::{builder::NodeBuilder, chain::checkpoints::HeaderCheckpoint};
4-
use kyoto::{Address, Client, Event, Log, Network, PeerStoreSizeConfig, TrustedPeer};
4+
use kyoto::{Address, Client, Event, Info, Network, PeerStoreSizeConfig, TrustedPeer};
55
use std::collections::HashSet;
66
use std::{net::Ipv4Addr, str::FromStr};
77

@@ -52,6 +52,7 @@ async fn main() {
5252
let Client {
5353
requester,
5454
mut log_rx,
55+
mut info_rx,
5556
mut warn_rx,
5657
mut event_rx,
5758
} = client;
@@ -76,16 +77,20 @@ async fn main() {
7677
}
7778
}
7879
}
79-
log = log_rx.recv() => {
80-
if let Some(log) = log {
81-
match log {
82-
Log::Debug(d)=> tracing::info!("{d}"),
83-
Log::StateChange(node_state) => tracing::info!("{node_state}"),
84-
Log::ConnectionsMet => tracing::info!("All required connections met"),
80+
info = info_rx.recv() => {
81+
if let Some(info) = info {
82+
match info {
83+
Info::StateChange(node_state) => tracing::info!("{node_state}"),
84+
Info::ConnectionsMet => tracing::info!("All required connections met"),
8585
_ => (),
8686
}
8787
}
8888
}
89+
log = log_rx.recv() => {
90+
if let Some(log) = log {
91+
tracing::info!("{log}");
92+
}
93+
}
8994
warn = warn_rx.recv() => {
9095
if let Some(warn) = warn {
9196
tracing::warn!("{warn}");

src/chain/chain.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ mod tests {
688688
filters::cfheader_chain::AppendAttempt,
689689
{
690690
dialog::Dialog,
691-
messages::{Event, Log, Warning},
691+
messages::{Event, Info, Warning},
692692
},
693693
};
694694

@@ -699,7 +699,8 @@ mod tests {
699699
height_monitor: Arc<Mutex<HeightMonitor>>,
700700
peers: usize,
701701
) -> Chain<()> {
702-
let (log_tx, _) = tokio::sync::mpsc::channel::<Log>(1);
702+
let (log_tx, _) = tokio::sync::mpsc::channel::<String>(1);
703+
let (info_tx, _) = tokio::sync::mpsc::channel::<Info>(1);
703704
let (warn_tx, _) = tokio::sync::mpsc::unbounded_channel::<Warning>();
704705
let (event_tx, _) = tokio::sync::mpsc::unbounded_channel::<Event>();
705706
let mut checkpoints = HeaderCheckpoints::new(&bitcoin::Network::Regtest);
@@ -712,6 +713,7 @@ mod tests {
712713
Arc::new(Dialog::new(
713714
crate::LogLevel::Debug,
714715
log_tx,
716+
info_tx,
715717
warn_tx,
716718
event_tx,
717719
)),

src/client.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{collections::BTreeMap, ops::Range, time::Duration};
88
use tokio::sync::mpsc;
99
use tokio::sync::mpsc::Sender;
1010

11-
use crate::{Event, Log, TrustedPeer, TxBroadcast, Warning};
11+
use crate::{Event, Info, TrustedPeer, TxBroadcast, Warning};
1212

1313
#[cfg(feature = "filter-control")]
1414
use super::{error::FetchBlockError, messages::BlockRequest, BlockReceiver, IndexedBlock};
@@ -22,8 +22,10 @@ use super::{
2222
pub struct Client {
2323
/// Send events to a node, such as broadcasting a transaction.
2424
pub requester: Requester,
25-
/// Receive log messages from a node.
26-
pub log_rx: mpsc::Receiver<Log>,
25+
/// Receive log/debug messages from a node.
26+
pub log_rx: mpsc::Receiver<String>,
27+
/// Receive informational messages from the node.
28+
pub info_rx: mpsc::Receiver<Info>,
2729
/// Receive warning messages from a node.
2830
pub warn_rx: mpsc::UnboundedReceiver<Warning>,
2931
/// Receive [`Event`] from a node to act on.
@@ -32,14 +34,16 @@ pub struct Client {
3234

3335
impl Client {
3436
pub(crate) fn new(
35-
log_rx: mpsc::Receiver<Log>,
37+
log_rx: mpsc::Receiver<String>,
38+
info_rx: mpsc::Receiver<Info>,
3639
warn_rx: mpsc::UnboundedReceiver<Warning>,
3740
event_rx: mpsc::UnboundedReceiver<Event>,
3841
ntx: Sender<ClientMessage>,
3942
) -> Self {
4043
Self {
4144
requester: Requester::new(ntx),
4245
log_rx,
46+
info_rx,
4347
warn_rx,
4448
event_rx,
4549
}
@@ -354,25 +358,23 @@ mod tests {
354358
#[tokio::test]
355359
async fn test_client_works() {
356360
let transaction: Transaction = deserialize(&hex::decode("0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000").unwrap()).unwrap();
357-
let (log_tx, log_rx) = tokio::sync::mpsc::channel::<Log>(1);
361+
let (log_tx, log_rx) = tokio::sync::mpsc::channel::<String>(1);
362+
let (_, info_rx) = tokio::sync::mpsc::channel::<Info>(1);
358363
let (_, warn_rx) = tokio::sync::mpsc::unbounded_channel::<Warning>();
359364
let (_, event_rx) = tokio::sync::mpsc::unbounded_channel::<Event>();
360365
let (ctx, crx) = mpsc::channel::<ClientMessage>(5);
361366
let Client {
362367
requester,
363368
mut log_rx,
369+
info_rx: _,
364370
warn_rx: _,
365371
event_rx: _,
366-
} = Client::new(log_rx, warn_rx, event_rx, ctx);
367-
let send_res = log_tx.send(Log::Debug("An important message".into())).await;
372+
} = Client::new(log_rx, info_rx, warn_rx, event_rx, ctx);
373+
let send_res = log_tx.send("An important message".into()).await;
368374
assert!(send_res.is_ok());
369375
let message = log_rx.recv().await;
370376
assert!(message.is_some());
371-
tokio::task::spawn(async move {
372-
log_tx
373-
.send(Log::Debug("Another important message".into()))
374-
.await
375-
});
377+
tokio::task::spawn(async move { log_tx.send("Another important message".into()).await });
376378
assert!(send_res.is_ok());
377379
let message = log_rx.recv().await;
378380
assert!(message.is_some());

src/dialog.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
use tokio::sync::mpsc::{Sender, UnboundedSender};
22

3-
use super::messages::{Event, Log, Progress, Warning};
3+
use super::messages::{Event, Info, Progress, Warning};
44
use crate::LogLevel;
55

66
#[derive(Debug, Clone)]
77
pub(crate) struct Dialog {
88
pub(crate) log_level: LogLevel,
9-
log_tx: Sender<Log>,
9+
log_tx: Sender<String>,
10+
info_tx: Sender<Info>,
1011
warn_tx: UnboundedSender<Warning>,
1112
event_tx: UnboundedSender<Event>,
1213
}
1314

1415
impl Dialog {
1516
pub(crate) fn new(
1617
log_level: LogLevel,
17-
log_tx: Sender<Log>,
18+
log_tx: Sender<String>,
19+
info_tx: Sender<Info>,
1820
warn_tx: UnboundedSender<Warning>,
1921
event_tx: UnboundedSender<Event>,
2022
) -> Self {
2123
Self {
2224
log_level,
2325
log_tx,
26+
info_tx,
2427
warn_tx,
2528
event_tx,
2629
}
2730
}
2831

2932
pub(crate) async fn send_dialog(&self, dialog: impl Into<String>) {
30-
let _ = self.log_tx.send(Log::Debug(dialog.into())).await;
33+
let _ = self.log_tx.send(dialog.into()).await;
3134
}
3235

3336
pub(crate) async fn chain_update(
@@ -37,29 +40,31 @@ impl Dialog {
3740
num_filters: u32,
3841
best_height: u32,
3942
) {
40-
let _ = self
41-
.log_tx
42-
.send(Log::Progress(Progress::new(
43-
num_cf_headers,
44-
num_filters,
45-
best_height,
46-
)))
47-
.await;
43+
if matches!(self.log_level, LogLevel::Debug | LogLevel::Info) {
44+
let _ = self
45+
.info_tx
46+
.send(Info::Progress(Progress::new(
47+
num_cf_headers,
48+
num_filters,
49+
best_height,
50+
)))
51+
.await;
52+
}
4853
if matches!(self.log_level, LogLevel::Debug) {
4954
let message = format!(
5055
"Headers ({}/{}) Compact Filter Headers ({}/{}) Filters ({}/{})",
5156
num_headers, best_height, num_cf_headers, best_height, num_filters, best_height
5257
);
53-
let _ = self.log_tx.send(Log::Debug(message)).await;
58+
let _ = self.log_tx.send(message).await;
5459
}
5560
}
5661

5762
pub(crate) fn send_warning(&self, warning: Warning) {
5863
let _ = self.warn_tx.send(warning);
5964
}
6065

61-
pub(crate) async fn send_info(&self, info: Log) {
62-
let _ = self.log_tx.send(info).await;
66+
pub(crate) async fn send_info(&self, info: Info) {
67+
let _ = self.info_tx.send(info).await;
6368
}
6469

6570
pub(crate) fn send_event(&self, message: Event) {

0 commit comments

Comments
 (0)