X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Ftypes.rs;h=f38a3760b316897b78729d4e96aa865c122b8969;hb=f46a13944933c9c6c42fbeba66473d6e548d7cda;hp=2d5a281e02024a8ab8c25b74e82684c17dc18d07;hpb=4d776e5e9e51758f17dee01a7b8e186f1091e460;p=rapid-gossip-sync-server diff --git a/src/types.rs b/src/types.rs index 2d5a281..f38a376 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,39 +1,93 @@ use std::sync::Arc; -use std::ops::Deref; -use lightning::chain::keysinterface::KeysManager; -use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate}; +use lightning::sign::KeysManager; +use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement}; use lightning::ln::peer_handler::{ErroringMessageHandler, IgnoringMessageHandler, PeerManager}; use lightning::util::logger::{Logger, Record}; +use crate::config; use crate::downloader::GossipRouter; use crate::verifier::ChainVerifier; -pub(crate) type GossipChainAccess = Arc; -pub(crate) type GossipPeerManager = Arc, IgnoringMessageHandler, TestLogger, IgnoringMessageHandler, Arc>>; +pub(crate) type GossipChainAccess = Arc>; +pub(crate) type GossipPeerManager = Arc>, IgnoringMessageHandler, L, IgnoringMessageHandler, Arc>>; #[derive(Debug)] pub(crate) enum GossipMessage { - ChannelAnnouncement(ChannelAnnouncement), - ChannelUpdate(ChannelUpdate), + NodeAnnouncement(NodeAnnouncement, Option), + // the second element is an optional override for the seen value + ChannelAnnouncement(ChannelAnnouncement, Option), + ChannelUpdate(ChannelUpdate, Option), } #[derive(Clone, Copy)] -pub(crate) struct TestLogger {} -impl Deref for TestLogger { - type Target = Self; - fn deref(&self) -> &Self { self } -} +pub struct RGSSLogger {} -impl TestLogger { - pub(crate) fn new() -> TestLogger { +impl RGSSLogger { + pub fn new() -> RGSSLogger { Self {} } } -impl Logger for TestLogger { - fn log(&self, record: &Record) { - // TODO: allow log level threshold to be set +impl Logger for RGSSLogger { + fn log(&self, record: Record) { + let threshold = config::log_level(); + if record.level < threshold { + return; + } println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); } } + +#[cfg(test)] +pub mod tests { + use std::collections::HashMap; + use std::sync::{Mutex}; + use lightning::util::logger::{Level, Logger, Record}; + + pub struct TestLogger { + level: Level, + pub(crate) id: String, + pub lines: Mutex>, + } + + impl TestLogger { + pub fn new() -> TestLogger { + let id = crate::tests::db_test_schema(); + Self::with_id(id) + } + pub fn with_id(id: String) -> TestLogger { + TestLogger { + level: Level::Gossip, + id, + lines: Mutex::new(HashMap::new()), + } + } + pub fn enable(&mut self, level: Level) { + self.level = level; + } + pub fn assert_log(&self, module: String, line: String, count: usize) { + let log_entries = self.lines.lock().unwrap(); + assert_eq!(log_entries.get(&(module, line)), Some(&count)); + } + + /// Search for the number of occurrence of the logged lines which + /// 1. belongs to the specified module and + /// 2. contains `line` in it. + /// And asserts if the number of occurrences is the same with the given `count` + pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) { + let log_entries = self.lines.lock().unwrap(); + let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| { + m == module && l.contains(line) + }).map(|(_, c)| { c }).sum(); + assert_eq!(l, count) + } + } + + impl Logger for TestLogger { + fn log(&self, record: Record) { + *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; + println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args); + } + } +}