TESTING
[rust-lightning] / fuzz / src / full_stack.rs
index 2af173cfcb4374ff3bdc9ffc6c9f69a900dbc1c8..4c1504edf9339a68b33c86feeae6fe780974238a 100644 (file)
@@ -38,7 +38,7 @@ use bitcoin::secp256k1::Secp256k1;
 use std::cell::RefCell;
 use std::collections::{HashMap, hash_map};
 use std::cmp;
-use std::sync::Arc;
+use std::sync::{Arc,Mutex};
 use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
 
 #[inline]
@@ -95,6 +95,7 @@ struct FuzzEstimator {
 }
 impl FeeEstimator for FuzzEstimator {
        fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
+println!("fee_get");
                //TODO: We should actually be testing at least much more than 64k...
                match self.input.get_slice(2) {
                        Some(slice) => cmp::max(slice_to_be16(slice) as u64, 253),
@@ -103,9 +104,13 @@ impl FeeEstimator for FuzzEstimator {
        }
 }
 
-struct TestBroadcaster {}
+pub struct TestBroadcaster {
+       pub txn_broadcasted: Mutex<Vec<Transaction>>,
+}
 impl BroadcasterInterface for TestBroadcaster {
-       fn broadcast_transaction(&self, _tx: &Transaction) {}
+       fn broadcast_transaction(&self, tx: &Transaction) {
+               self.txn_broadcasted.lock().unwrap().push(tx.clone());
+       }
 }
 
 #[derive(Clone)]
@@ -137,6 +142,7 @@ impl<'a> std::hash::Hash for Peer<'a> {
 struct MoneyLossDetector<'a> {
        manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
        monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>,
+       broadcaster: Arc<TestBroadcaster>,
        handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>, Arc<dyn Logger>>,
 
        peers: &'a RefCell<[bool; 256]>,
@@ -151,10 +157,12 @@ impl<'a> MoneyLossDetector<'a> {
        pub fn new(peers: &'a RefCell<[bool; 256]>,
                   manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>,
                   monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>,
+                  broadcaster: Arc<TestBroadcaster>,
                   handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<ChainWatchInterfaceUtil>>>, Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<FuzzEstimator>, Arc<dyn Logger>>>, Arc<dyn Logger>>) -> Self {
                MoneyLossDetector {
                        manager,
                        monitor,
+                       broadcaster,
                        handler,
 
                        peers,
@@ -222,7 +230,38 @@ impl<'a> Drop for MoneyLossDetector<'a> {
 
                        // Force all channels onto the chain (and time out claim txn)
                        self.manager.force_close_all_channels();
+                       for _ in 0..6*24*14 {
+                               self.connect_block(&[]);
+                       }
+               }
+
+               // Test that all broadcasted transactions either spend one of our funding transactions or
+               // some other broadcasted transaction:
+
+               let mut txn_map = HashMap::new();
+               let mut funding_txn_map = HashMap::new();
+               for tx in self.funding_txn.drain(..) {
+                       funding_txn_map.insert(tx.txid(), tx);
                }
+               let mut txn_broadcasted = self.broadcaster.txn_broadcasted.lock().unwrap();
+               for tx in txn_broadcasted.drain(..) {
+                       txn_map.insert(tx.txid(), tx);
+               }
+               /*for (_, tx) in txn_map.iter() {
+                       for inp in tx.input.iter() {
+                               let prev_tx = match funding_txn_map.get(&inp.prev_hash) {
+                                       Some(ptx) => ptx,
+                                       None => {
+                                               txn_map.get(&inp.prev_hash).unwrap()
+                                       }
+                               };
+                               assert!(prev_tx.output.len() > inp.prev_index as usize);
+                       }
+               }*/
+
+               //XXX: Find all non-conflicting sets of txn broadcasted and ensure that in each case we
+               //always get back at least the amount we expect minus tx fees (which we should be able to
+               //calculate now!
        }
 }
 
@@ -324,7 +363,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
        };
 
        let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin));
-       let broadcast = Arc::new(TestBroadcaster{});
+       let broadcast = Arc::new(TestBroadcaster{ txn_broadcasted: Mutex::new(Vec::new()) });
        let monitor = Arc::new(channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone()));
 
        let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), counter: AtomicU64::new(0) });
@@ -337,7 +376,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
        let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(watch.clone(), Arc::clone(&logger)));
 
        let peers = RefCell::new([false; 256]);
-       let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler {
+       let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), broadcast.clone(), PeerManager::new(MessageHandler {
                chan_handler: channelmanager.clone(),
                route_handler: net_graph_msg_handler.clone(),
        }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger)));
@@ -350,7 +389,9 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
        let mut pending_funding_relay = Vec::new();
 
        loop {
-               match get_slice!(1)[0] {
+let a = get_slice!(1)[0];
+println!("action: {}", a);
+               match a {
                        0 => {
                                let mut new_id = 0;
                                for i in 1..256 {
@@ -537,13 +578,16 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
                        // 15 is above
                        _ => return,
                }
+println!("PROCESSING EVENTS");
                loss_detector.handler.process_events();
                for event in loss_detector.manager.get_and_clear_pending_events() {
                        match event {
                                Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, output_script, .. } => {
+println!("fgr");
                                        pending_funding_generation.push((temporary_channel_id, channel_value_satoshis, output_script));
                                },
                                Event::FundingBroadcastSafe { funding_txo, .. } => {
+println!("fbs");
                                        pending_funding_relay.push(pending_funding_signatures.remove(&funding_txo).unwrap());
                                },
                                Event::PaymentReceived { payment_hash, payment_secret, amt } => {
@@ -553,6 +597,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
                                Event::PaymentSent {..} => {},
                                Event::PaymentFailed {..} => {},
                                Event::PendingHTLCsForwardable {..} => {
+println!("PENDING HTLCS FORWARDABLE");
                                        should_forward = true;
                                },
                                Event::SpendableOutputs {..} => {},