Merge pull request #460 from lightning-signer/channel-value
[rust-lightning] / fuzz / src / chanmon_consistency.rs
index 52d5a658d5d6f6124ea9a851059ed8d443f0790d..b01fabad3053468347303272b0151c9db33ca849 100644 (file)
@@ -29,7 +29,8 @@ use lightning::ln::channelmonitor;
 use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
 use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
 use lightning::ln::router::{Route, RouteHop};
-use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, LightningError, UpdateAddHTLC, LocalFeatures};
+use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
+use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init};
 use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
 use lightning::util::events;
 use lightning::util::logger::Logger;
@@ -84,7 +85,7 @@ pub struct TestChannelMonitor {
 impl TestChannelMonitor {
        pub fn new(chain_monitor: Arc<dyn chaininterface::ChainWatchInterface>, broadcaster: Arc<dyn chaininterface::BroadcasterInterface>, logger: Arc<dyn Logger>, feeest: Arc<dyn chaininterface::FeeEstimator>) -> Self {
                Self {
-                       simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
+                       simple_monitor: Arc::new(channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest)),
                        update_ret: Mutex::new(Ok(())),
                        latest_good_update: Mutex::new(HashMap::new()),
                        latest_update_good: Mutex::new(HashMap::new()),
@@ -149,7 +150,7 @@ impl KeysInterface for KeyProvider {
                PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[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, 3, self.node_id]).unwrap())
        }
 
-       fn get_channel_keys(&self, _inbound: bool) -> EnforcingChannelKeys {
+       fn get_channel_keys(&self, _inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
                EnforcingChannelKeys::new(InMemoryChannelKeys {
                        funding_key:               SecretKey::from_slice(&[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, 4, self.node_id]).unwrap(),
                        revocation_base_key:       SecretKey::from_slice(&[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, 5, self.node_id]).unwrap(),
@@ -157,6 +158,8 @@ impl KeysInterface for KeyProvider {
                        delayed_payment_base_key:  SecretKey::from_slice(&[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, 7, self.node_id]).unwrap(),
                        htlc_base_key:             SecretKey::from_slice(&[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, 8, self.node_id]).unwrap(),
                        commitment_seed: [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, 9, self.node_id],
+                       remote_channel_pubkeys: None,
+                       channel_value_satoshis,
                })
        }
 
@@ -188,7 +191,7 @@ pub fn do_test(data: &[u8]) {
                        config.channel_options.fee_proportional_millionths = 0;
                        config.channel_options.announced_channel = true;
                        config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
-                       (ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap(),
+                       (Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
                        monitor)
                } }
        }
@@ -219,14 +222,14 @@ pub fn do_test(data: &[u8]) {
                        let read_args = ChannelManagerReadArgs {
                                keys_manager,
                                fee_estimator: fee_est.clone(),
-                               monitor: monitor.clone(),
+                               monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>,
                                tx_broadcaster: broadcast.clone(),
                                logger,
                                default_config: config,
                                channel_monitors: &mut monitor_refs,
                        };
 
-                       let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
+                       let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
                        for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
                                if !was_good {
                                        // If the last time we updated a monitor we didn't successfully update (and we
@@ -252,7 +255,7 @@ pub fn do_test(data: &[u8]) {
                                } else { panic!("Wrong event type"); }
                        };
 
-                       $dest.handle_open_channel(&$source.get_our_node_id(), LocalFeatures::new(), &open_channel).unwrap();
+                       $dest.handle_open_channel(&$source.get_our_node_id(), InitFeatures::supported(), &open_channel);
                        let accept_channel = {
                                let events = $dest.get_and_clear_pending_msg_events();
                                assert_eq!(events.len(), 1);
@@ -261,7 +264,7 @@ pub fn do_test(data: &[u8]) {
                                } else { panic!("Wrong event type"); }
                        };
 
-                       $source.handle_accept_channel(&$dest.get_our_node_id(), LocalFeatures::new(), &accept_channel).unwrap();
+                       $source.handle_accept_channel(&$dest.get_our_node_id(), InitFeatures::supported(), &accept_channel);
                        {
                                let events = $source.get_and_clear_pending_events();
                                assert_eq!(events.len(), 1);
@@ -282,7 +285,7 @@ pub fn do_test(data: &[u8]) {
                                        msg.clone()
                                } else { panic!("Wrong event type"); }
                        };
-                       $dest.handle_funding_created(&$source.get_our_node_id(), &funding_created).unwrap();
+                       $dest.handle_funding_created(&$source.get_our_node_id(), &funding_created);
 
                        let funding_signed = {
                                let events = $dest.get_and_clear_pending_msg_events();
@@ -291,7 +294,7 @@ pub fn do_test(data: &[u8]) {
                                        msg.clone()
                                } else { panic!("Wrong event type"); }
                        };
-                       $source.handle_funding_signed(&$dest.get_our_node_id(), &funding_signed).unwrap();
+                       $source.handle_funding_signed(&$dest.get_our_node_id(), &funding_signed);
 
                        {
                                let events = $source.get_and_clear_pending_events();
@@ -330,7 +333,7 @@ pub fn do_test(data: &[u8]) {
                                        if let events::MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = event {
                                                for node in $nodes.iter() {
                                                        if node.get_our_node_id() == *node_id {
-                                                               node.handle_funding_locked(&$nodes[idx].get_our_node_id(), msg).unwrap();
+                                                               node.handle_funding_locked(&$nodes[idx].get_our_node_id(), msg);
                                                        }
                                                }
                                        } else { panic!("Wrong event type"); }
@@ -381,16 +384,6 @@ pub fn do_test(data: &[u8]) {
        let mut node_c_ser = VecWriter(Vec::new());
        nodes[2].write(&mut node_c_ser).unwrap();
 
-       macro_rules! test_err {
-               ($res: expr) => {
-                       match $res {
-                               Ok(()) => {},
-                               Err(LightningError { action: ErrorAction::IgnoreError, .. }) => { },
-                               _ => { $res.unwrap() },
-                       }
-               }
-       }
-
        macro_rules! test_return {
                () => { {
                        assert_eq!(nodes[0].list_channels().len(), 1);
@@ -422,7 +415,9 @@ pub fn do_test(data: &[u8]) {
                                if let Err(_) = $source.send_payment(Route {
                                        hops: vec![RouteHop {
                                                pubkey: $dest.0.get_our_node_id(),
+                                               node_features: NodeFeatures::empty(),
                                                short_channel_id: $dest.1,
+                                               channel_features: ChannelFeatures::empty(),
                                                fee_msat: 5000000,
                                                cltv_expiry_delta: 200,
                                        }],
@@ -437,12 +432,16 @@ pub fn do_test(data: &[u8]) {
                                if let Err(_) = $source.send_payment(Route {
                                        hops: vec![RouteHop {
                                                pubkey: $middle.0.get_our_node_id(),
+                                               node_features: NodeFeatures::empty(),
                                                short_channel_id: $middle.1,
+                                               channel_features: ChannelFeatures::empty(),
                                                fee_msat: 50000,
                                                cltv_expiry_delta: 100,
                                        },RouteHop {
                                                pubkey: $dest.0.get_our_node_id(),
+                                               node_features: NodeFeatures::empty(),
                                                short_channel_id: $dest.1,
+                                               channel_features: ChannelFeatures::empty(),
                                                fee_msat: 5000000,
                                                cltv_expiry_delta: 200,
                                        }],
@@ -470,7 +469,7 @@ pub fn do_test(data: &[u8]) {
                                                                        assert!(update_fee.is_none());
                                                                        for update_add in update_add_htlcs {
                                                                                if !$corrupt_forward {
-                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &update_add));
+                                                                                       dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &update_add);
                                                                                } else {
                                                                                        // Corrupt the update_add_htlc message so that its HMAC
                                                                                        // check will fail and we generate a
@@ -479,33 +478,33 @@ pub fn do_test(data: &[u8]) {
                                                                                        let mut msg_ser = update_add.encode();
                                                                                        msg_ser[1000] ^= 0xff;
                                                                                        let new_msg = UpdateAddHTLC::read(&mut Cursor::new(&msg_ser)).unwrap();
-                                                                                       test_err!(dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &new_msg));
+                                                                                       dest.handle_update_add_htlc(&nodes[$node].get_our_node_id(), &new_msg);
                                                                                }
                                                                        }
                                                                        for update_fulfill in update_fulfill_htlcs {
-                                                                               test_err!(dest.handle_update_fulfill_htlc(&nodes[$node].get_our_node_id(), &update_fulfill));
+                                                                               dest.handle_update_fulfill_htlc(&nodes[$node].get_our_node_id(), &update_fulfill);
                                                                        }
                                                                        for update_fail in update_fail_htlcs {
-                                                                               test_err!(dest.handle_update_fail_htlc(&nodes[$node].get_our_node_id(), &update_fail));
+                                                                               dest.handle_update_fail_htlc(&nodes[$node].get_our_node_id(), &update_fail);
                                                                        }
                                                                        for update_fail_malformed in update_fail_malformed_htlcs {
-                                                                               test_err!(dest.handle_update_fail_malformed_htlc(&nodes[$node].get_our_node_id(), &update_fail_malformed));
+                                                                               dest.handle_update_fail_malformed_htlc(&nodes[$node].get_our_node_id(), &update_fail_malformed);
                                                                        }
-                                                                       test_err!(dest.handle_commitment_signed(&nodes[$node].get_our_node_id(), &commitment_signed));
+                                                                       dest.handle_commitment_signed(&nodes[$node].get_our_node_id(), &commitment_signed);
                                                                }
                                                        }
                                                },
                                                events::MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
                                                        for dest in nodes.iter() {
                                                                if dest.get_our_node_id() == *node_id {
-                                                                       test_err!(dest.handle_revoke_and_ack(&nodes[$node].get_our_node_id(), msg));
+                                                                       dest.handle_revoke_and_ack(&nodes[$node].get_our_node_id(), msg);
                                                                }
                                                        }
                                                },
                                                events::MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
                                                        for dest in nodes.iter() {
                                                                if dest.get_our_node_id() == *node_id {
-                                                                       test_err!(dest.handle_channel_reestablish(&nodes[$node].get_our_node_id(), msg));
+                                                                       dest.handle_channel_reestablish(&nodes[$node].get_our_node_id(), msg);
                                                                }
                                                        }
                                                },
@@ -516,6 +515,10 @@ pub fn do_test(data: &[u8]) {
                                                        // Can be generated due to a payment forward being rejected due to a
                                                        // channel having previously failed a monitor update
                                                },
+                                               events::MessageSendEvent::HandleError { action: ErrorAction::IgnoreError, .. } => {
+                                                       // Can be generated at any processing step to send back an error, disconnect
+                                                       // peer or just ignore
+                                               },
                                                _ => panic!("Unhandled message event"),
                                        }
                                }
@@ -532,6 +535,7 @@ pub fn do_test(data: &[u8]) {
                                                        events::MessageSendEvent::SendChannelReestablish { .. } => {},
                                                        events::MessageSendEvent::SendFundingLocked { .. } => {},
                                                        events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       events::MessageSendEvent::HandleError { action: ErrorAction::IgnoreError, .. } => {},
                                                        _ => panic!("Unhandled message event"),
                                                }
                                        }
@@ -544,6 +548,7 @@ pub fn do_test(data: &[u8]) {
                                                        events::MessageSendEvent::SendChannelReestablish { .. } => {},
                                                        events::MessageSendEvent::SendFundingLocked { .. } => {},
                                                        events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       events::MessageSendEvent::HandleError { action: ErrorAction::IgnoreError, .. } => {},
                                                        _ => panic!("Unhandled message event"),
                                                }
                                        }
@@ -565,6 +570,7 @@ pub fn do_test(data: &[u8]) {
                                                },
                                                events::MessageSendEvent::SendFundingLocked { .. } => false,
                                                events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => false,
+                                               events::MessageSendEvent::HandleError { action: ErrorAction::IgnoreError, .. } => false,
                                                _ => panic!("Unhandled message event"),
                                        };
                                        if push { msg_sink.push(event); }
@@ -651,15 +657,15 @@ pub fn do_test(data: &[u8]) {
                        },
                        0x11 => {
                                if chan_a_disconnected {
-                                       nodes[0].peer_connected(&nodes[1].get_our_node_id());
-                                       nodes[1].peer_connected(&nodes[0].get_our_node_id());
+                                       nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
+                                       nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::empty() });
                                        chan_a_disconnected = false;
                                }
                        },
                        0x12 => {
                                if chan_b_disconnected {
-                                       nodes[1].peer_connected(&nodes[2].get_our_node_id());
-                                       nodes[2].peer_connected(&nodes[1].get_our_node_id());
+                                       nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::empty() });
+                                       nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::empty() });
                                        chan_b_disconnected = false;
                                }
                        },