f530bd41a112e41e082079e20fce4f19295c3877
[rapid-gossip-sync-server] / src / types.rs
1 use std::sync::Arc;
2
3 use lightning::sign::KeysManager;
4 use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate};
5 use lightning::ln::peer_handler::{ErroringMessageHandler, IgnoringMessageHandler, PeerManager};
6 use lightning::util::logger::{Logger, Record};
7 use crate::config;
8
9 use crate::downloader::GossipRouter;
10 use crate::verifier::ChainVerifier;
11
12 pub(crate) type GossipChainAccess<L> = Arc<ChainVerifier<L>>;
13 pub(crate) type GossipPeerManager<L> = Arc<PeerManager<lightning_net_tokio::SocketDescriptor, ErroringMessageHandler, Arc<GossipRouter<L>>, IgnoringMessageHandler, L, IgnoringMessageHandler, Arc<KeysManager>>>;
14
15 #[derive(Debug)]
16 pub(crate) enum GossipMessage {
17         ChannelAnnouncement(ChannelAnnouncement),
18         ChannelUpdate(ChannelUpdate),
19 }
20
21 #[derive(Clone, Copy)]
22 pub struct RGSSLogger {}
23
24 impl RGSSLogger {
25         pub fn new() -> RGSSLogger {
26                 Self {}
27         }
28 }
29
30 impl Logger for RGSSLogger {
31         fn log(&self, record: &Record) {
32                 let threshold = config::log_level();
33                 if record.level < threshold {
34                         return;
35                 }
36                 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
37         }
38 }
39
40 #[cfg(test)]
41 pub mod tests {
42         use std::collections::HashMap;
43         use std::sync::{Mutex};
44         use lightning::util::logger::{Level, Logger, Record};
45
46         pub struct TestLogger {
47                 level: Level,
48                 pub(crate) id: String,
49                 pub lines: Mutex<HashMap<(String, String), usize>>,
50         }
51
52         impl TestLogger {
53                 pub fn new() -> TestLogger {
54                         let id = crate::tests::db_test_schema();
55                         Self::with_id(id)
56                 }
57                 pub fn with_id(id: String) -> TestLogger {
58                         TestLogger {
59                                 level: Level::Gossip,
60                                 id,
61                                 lines: Mutex::new(HashMap::new()),
62                         }
63                 }
64                 pub fn enable(&mut self, level: Level) {
65                         self.level = level;
66                 }
67                 pub fn assert_log(&self, module: String, line: String, count: usize) {
68                         let log_entries = self.lines.lock().unwrap();
69                         assert_eq!(log_entries.get(&(module, line)), Some(&count));
70                 }
71
72                 /// Search for the number of occurrence of the logged lines which
73                 /// 1. belongs to the specified module and
74                 /// 2. contains `line` in it.
75                 /// And asserts if the number of occurrences is the same with the given `count`
76                 pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) {
77                         let log_entries = self.lines.lock().unwrap();
78                         let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
79                                 m == module && l.contains(line)
80                         }).map(|(_, c)| { c }).sum();
81                         assert_eq!(l, count)
82                 }
83         }
84
85         impl Logger for TestLogger {
86                 fn log(&self, record: &Record) {
87                         *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
88                         println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
89                 }
90         }
91 }