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