93527a45b837c7368d9bc2842167323ff4c2e397
[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         // the second element is an optional override for the seen value
18         ChannelAnnouncement(ChannelAnnouncement, Option<u32>),
19         ChannelUpdate(ChannelUpdate, Option<u32>),
20 }
21
22 #[derive(Clone, Copy)]
23 pub struct RGSSLogger {}
24
25 impl RGSSLogger {
26         pub fn new() -> RGSSLogger {
27                 Self {}
28         }
29 }
30
31 impl Logger for RGSSLogger {
32         fn log(&self, record: &Record) {
33                 let threshold = config::log_level();
34                 if record.level < threshold {
35                         return;
36                 }
37                 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
38         }
39 }
40
41 #[cfg(test)]
42 pub mod tests {
43         use std::collections::HashMap;
44         use std::sync::{Mutex};
45         use lightning::util::logger::{Level, Logger, Record};
46
47         pub struct TestLogger {
48                 level: Level,
49                 pub(crate) id: String,
50                 pub lines: Mutex<HashMap<(String, String), usize>>,
51         }
52
53         impl TestLogger {
54                 pub fn new() -> TestLogger {
55                         let id = crate::tests::db_test_schema();
56                         Self::with_id(id)
57                 }
58                 pub fn with_id(id: String) -> TestLogger {
59                         TestLogger {
60                                 level: Level::Gossip,
61                                 id,
62                                 lines: Mutex::new(HashMap::new()),
63                         }
64                 }
65                 pub fn enable(&mut self, level: Level) {
66                         self.level = level;
67                 }
68                 pub fn assert_log(&self, module: String, line: String, count: usize) {
69                         let log_entries = self.lines.lock().unwrap();
70                         assert_eq!(log_entries.get(&(module, line)), Some(&count));
71                 }
72
73                 /// Search for the number of occurrence of the logged lines which
74                 /// 1. belongs to the specified module and
75                 /// 2. contains `line` in it.
76                 /// And asserts if the number of occurrences is the same with the given `count`
77                 pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) {
78                         let log_entries = self.lines.lock().unwrap();
79                         let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| {
80                                 m == module && l.contains(line)
81                         }).map(|(_, c)| { c }).sum();
82                         assert_eq!(l, count)
83                 }
84         }
85
86         impl Logger for TestLogger {
87                 fn log(&self, record: &Record) {
88                         *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
89                         println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args);
90                 }
91         }
92 }