Merge pull request #1695 from TheBlueMatt/2022-08-log-chan_update
[rust-lightning] / fuzz / src / chanmon_consistency.rs
index 5db658c5818195866843b1a2dc1d386507a3a633..372bed6049370c065c0d7a660f61a466ec715bd4 100644 (file)
 //! send-side handling is correct, other peers. We consider it a failure if any action results in a
 //! channel being force-closed.
 
+use bitcoin::TxMerkleNode;
 use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::blockdata::script::{Builder, Script};
 use bitcoin::blockdata::opcodes;
+use bitcoin::blockdata::locktime::PackedLockTime;
 use bitcoin::network::constants::Network;
 
 use bitcoin::hashes::Hash as TraitImport;
@@ -53,7 +55,8 @@ use lightning::routing::router::{Route, RouteHop};
 use utils::test_logger::{self, Output};
 use utils::test_persister::TestPersister;
 
-use bitcoin::secp256k1::{PublicKey,SecretKey};
+use bitcoin::secp256k1::{PublicKey, SecretKey, Scalar};
+use bitcoin::secp256k1::ecdh::SharedSecret;
 use bitcoin::secp256k1::ecdsa::RecoverableSignature;
 use bitcoin::secp256k1::Secp256k1;
 
@@ -140,7 +143,7 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
                };
                let deserialized_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::
                        read(&mut Cursor::new(&map_entry.get().1), &*self.keys).unwrap().1;
-               deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &&FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
+               deserialized_monitor.update_monitor(&update, &&TestBroadcaster{}, &FuzzEstimator { ret_val: atomic::AtomicU32::new(253) }, &self.logger).unwrap();
                let mut ser = VecWriter(Vec::new());
                deserialized_monitor.write(&mut ser).unwrap();
                map_entry.insert((update.update_id, ser.0));
@@ -148,7 +151,7 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
                self.chain_monitor.update_channel(funding_txo, update)
        }
 
-       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>)> {
+       fn release_pending_monitor_events(&self) -> Vec<(OutPoint, Vec<MonitorEvent>, Option<PublicKey>)> {
                return self.chain_monitor.release_pending_monitor_events();
        }
 }
@@ -165,6 +168,14 @@ impl KeysInterface for KeyProvider {
                Ok(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, 1, self.node_id]).unwrap())
        }
 
+       fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
+               let mut node_secret = self.get_node_secret(recipient)?;
+               if let Some(tweak) = tweak {
+                       node_secret = node_secret.mul_tweak(tweak).unwrap();
+               }
+               Ok(SharedSecret::new(other_key, &node_secret))
+       }
+
        fn get_inbound_payment_key_material(&self) -> KeyMaterial {
                KeyMaterial([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, 1, self.node_id])
        }
@@ -356,8 +367,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                Arc::new(TestPersister { update_ret: Mutex::new(Ok(())) }), Arc::clone(&keys_manager)));
 
                        let mut config = UserConfig::default();
-                       config.channel_options.forwarding_fee_proportional_millionths = 0;
-                       config.channel_options.announced_channel = true;
+                       config.channel_config.forwarding_fee_proportional_millionths = 0;
+                       config.channel_handshake_config.announced_channel = true;
                        let network = Network::Bitcoin;
                        let params = ChainParameters {
                                network,
@@ -376,8 +387,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                Arc::new(TestPersister { update_ret: Mutex::new(Ok(())) }), Arc::clone(& $keys_manager)));
 
                        let mut config = UserConfig::default();
-                       config.channel_options.forwarding_fee_proportional_millionths = 0;
-                       config.channel_options.announced_channel = true;
+                       config.channel_config.forwarding_fee_proportional_millionths = 0;
+                       config.channel_handshake_config.announced_channel = true;
 
                        let mut monitors = HashMap::new();
                        let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();
@@ -438,11 +449,11 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                let events = $source.get_and_clear_pending_events();
                                assert_eq!(events.len(), 1);
                                if let events::Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, .. } = events[0] {
-                                       let tx = Transaction { version: $chan_id, lock_time: 0, input: Vec::new(), output: vec![TxOut {
+                                       let tx = Transaction { version: $chan_id, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
                                                value: *channel_value_satoshis, script_pubkey: output_script.clone(),
                                        }]};
                                        funding_output = OutPoint { txid: tx.txid(), index: 0 };
-                                       $source.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
+                                       $source.funding_transaction_generated(&temporary_channel_id, &$dest.get_our_node_id(), tx.clone()).unwrap();
                                        channel_txn.push(tx);
                                } else { panic!("Wrong event type"); }
                        }
@@ -472,11 +483,11 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
        macro_rules! confirm_txn {
                ($node: expr) => { {
                        let chain_hash = genesis_block(Network::Bitcoin).block_hash();
-                       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: chain_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                       let mut header = BlockHeader { version: 0x20000000, prev_blockhash: chain_hash, merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
                        let txdata: Vec<_> = channel_txn.iter().enumerate().map(|(i, tx)| (i + 1, tx)).collect();
                        $node.transactions_confirmed(&header, &txdata, 1);
                        for _ in 2..100 {
-                               header = BlockHeader { version: 0x20000000, prev_blockhash: header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+                               header = BlockHeader { version: 0x20000000, prev_blockhash: header.block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
                        }
                        $node.best_block_updated(&header, 99);
                } }
@@ -490,10 +501,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                        }
                        for (idx, node_event) in node_events.iter().enumerate() {
                                for event in node_event {
-                                       if let events::MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = event {
+                                       if let events::MessageSendEvent::SendChannelReady { 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);
+                                                               node.handle_channel_ready(&$nodes[idx].get_our_node_id(), msg);
                                                        }
                                                }
                                        } else { panic!("Wrong event type"); }
@@ -597,7 +608,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                                        if Some(*node_id) == expect_drop_id { panic!("peer_disconnected should drop msgs bound for the disconnected peer"); }
                                                        *node_id == a_id
                                                },
-                                               events::MessageSendEvent::SendFundingLocked { .. } => continue,
+                                               events::MessageSendEvent::SendChannelReady { .. } => continue,
                                                events::MessageSendEvent::SendAnnouncementSignatures { .. } => continue,
                                                events::MessageSendEvent::SendChannelUpdate { ref node_id, ref msg } => {
                                                        assert_eq!(msg.contents.flags & 2, 0); // The disable bit must never be set!
@@ -725,7 +736,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                                                }
                                                        }
                                                },
-                                               events::MessageSendEvent::SendFundingLocked { .. } => {
+                                               events::MessageSendEvent::SendChannelReady { .. } => {
                                                        // Can be generated as a reestablish response
                                                },
                                                events::MessageSendEvent::SendAnnouncementSignatures { .. } => {
@@ -771,7 +782,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                                        events::MessageSendEvent::UpdateHTLCs { .. } => {},
                                                        events::MessageSendEvent::SendRevokeAndACK { .. } => {},
                                                        events::MessageSendEvent::SendChannelReestablish { .. } => {},
-                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReady { .. } => {},
                                                        events::MessageSendEvent::SendAnnouncementSignatures { .. } => {},
                                                        events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
                                                                assert_eq!(msg.contents.flags & 2, 0); // The disable bit must never be set!
@@ -792,7 +803,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                                        events::MessageSendEvent::UpdateHTLCs { .. } => {},
                                                        events::MessageSendEvent::SendRevokeAndACK { .. } => {},
                                                        events::MessageSendEvent::SendChannelReestablish { .. } => {},
-                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReady { .. } => {},
                                                        events::MessageSendEvent::SendAnnouncementSignatures { .. } => {},
                                                        events::MessageSendEvent::SendChannelUpdate { ref msg, .. } => {
                                                                assert_eq!(msg.contents.flags & 2, 0); // The disable bit must never be set!
@@ -840,19 +851,27 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
                                                events::Event::PaymentReceived { payment_hash, .. } => {
                                                        if claim_set.insert(payment_hash.0) {
                                                                if $fail {
-                                                                       assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
+                                                                       nodes[$node].fail_htlc_backwards(&payment_hash);
                                                                } else {
-                                                                       assert!(nodes[$node].claim_funds(PaymentPreimage(payment_hash.0)));
+                                                                       nodes[$node].claim_funds(PaymentPreimage(payment_hash.0));
                                                                }
                                                        }
                                                },
                                                events::Event::PaymentSent { .. } => {},
+                                               events::Event::PaymentClaimed { .. } => {},
                                                events::Event::PaymentPathSuccessful { .. } => {},
                                                events::Event::PaymentPathFailed { .. } => {},
+                                               events::Event::ProbeSuccessful { .. } | events::Event::ProbeFailed { .. } => {
+                                                       // Even though we don't explicitly send probes, because probes are
+                                                       // detected based on hashing the payment hash+preimage, its rather
+                                                       // trivial for the fuzzer to build payments that accidentally end up
+                                                       // looking like probes.
+                                               },
                                                events::Event::PaymentForwarded { .. } if $node == 1 => {},
                                                events::Event::PendingHTLCsForwardable { .. } => {
                                                        nodes[$node].process_pending_htlc_forwards();
                                                },
+                                               events::Event::HTLCHandlingFailed { .. } => {},
                                                _ => if out.may_fail.load(atomic::Ordering::Acquire) {
                                                        return;
                                                } else {