Move router to a separate module
[rust-lightning] / fuzz / src / router.rs
index 99013d87e6e8bc97d710799bcbe1199ae7a85c6c..7dd439bef4abaf486d03dd1bfdcdd0e853ba8672 100644 (file)
@@ -1,17 +1,18 @@
-use bitcoin_hashes::sha256d::Hash as Sha256dHash;
 use bitcoin::blockdata::script::{Script, Builder};
 use bitcoin::blockdata::block::Block;
 use bitcoin::blockdata::transaction::Transaction;
+use bitcoin::hash_types::{Txid, BlockHash};
 
 use lightning::chain::chaininterface::{ChainError,ChainWatchInterface};
 use lightning::ln::channelmanager::ChannelDetails;
+use lightning::ln::features::InitFeatures;
 use lightning::ln::msgs;
-use lightning::ln::msgs::{RoutingMessageHandler};
-use lightning::ln::router::{Router, RouteHint};
+use lightning::ln::msgs::RoutingMessageHandler;
+use lightning::routing::router::{Router, RouteHint};
 use lightning::util::logger::Logger;
 use lightning::util::ser::Readable;
 
-use secp256k1::key::PublicKey;
+use bitcoin::secp256k1::key::PublicKey;
 
 use utils::test_logger;
 
@@ -71,15 +72,15 @@ struct DummyChainWatcher {
 }
 
 impl ChainWatchInterface for DummyChainWatcher {
-       fn install_watch_tx(&self, _txid: &Sha256dHash, _script_pub_key: &Script) { }
-       fn install_watch_outpoint(&self, _outpoint: (Sha256dHash, u32), _out_script: &Script) { }
+       fn install_watch_tx(&self, _txid: &Txid, _script_pub_key: &Script) { }
+       fn install_watch_outpoint(&self, _outpoint: (Txid, u32), _out_script: &Script) { }
        fn watch_all_txn(&self) { }
        fn filter_block<'a>(&self, _block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
                (Vec::new(), Vec::new())
        }
        fn reentered(&self) -> usize { 0 }
 
-       fn get_chain_utxo(&self, _genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
+       fn get_chain_utxo(&self, _genesis_hash: BlockHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
                match self.input.get_slice(2) {
                        Some(&[0, _]) => Err(ChainError::NotSupported),
                        Some(&[1, _]) => Err(ChainError::NotWatched),
@@ -92,7 +93,7 @@ impl ChainWatchInterface for DummyChainWatcher {
 }
 
 #[inline]
-pub fn do_test(data: &[u8]) {
+pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
        let input = Arc::new(InputData {
                data: data.to_vec(),
                read_pos: AtomicUsize::new(0),
@@ -117,13 +118,12 @@ pub fn do_test(data: &[u8]) {
        macro_rules! decode_msg {
                ($MsgType: path, $len: expr) => {{
                        let mut reader = ::std::io::Cursor::new(get_slice!($len));
-                       match <($MsgType)>::read(&mut reader) {
+                       match <$MsgType>::read(&mut reader) {
                                Ok(msg) => msg,
                                Err(e) => match e {
                                        msgs::DecodeError::UnknownVersion => return,
                                        msgs::DecodeError::UnknownRequiredFeature => return,
                                        msgs::DecodeError::InvalidValue => return,
-                                       msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::BadLengthDescriptor => return,
                                        msgs::DecodeError::ShortRead => panic!("We picked the length..."),
                                        msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
@@ -150,7 +150,7 @@ pub fn do_test(data: &[u8]) {
                }
        }
 
-       let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
+       let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
        let chain_monitor = Arc::new(DummyChainWatcher {
                input: Arc::clone(&input),
        });
@@ -198,6 +198,7 @@ pub fn do_test(data: &[u8]) {
                                                                channel_id: [0; 32],
                                                                short_channel_id: Some(slice_to_be64(get_slice!(8))),
                                                                remote_network_id: get_pubkey!(),
+                                                               counterparty_features: InitFeatures::empty(),
                                                                channel_value_satoshis: slice_to_be64(get_slice!(8)),
                                                                user_id: 0,
                                                                inbound_capacity_msat: 0,
@@ -231,7 +232,11 @@ pub fn do_test(data: &[u8]) {
        }
 }
 
+pub fn router_test<Out: test_logger::Output>(data: &[u8], out: Out) {
+       do_test(data, out);
+}
+
 #[no_mangle]
 pub extern "C" fn router_run(data: *const u8, datalen: usize) {
-       do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
+       do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
 }