Refactor MessageRouter::create_blinded_paths
[rust-lightning] / fuzz / src / full_stack.rs
index a68cef1b9b4c1ad760679efd2328758c426b4c5c..bdd29be9129f9a66a494ba428771c539edebb6f8 100644 (file)
 //! or payments to send/ways to handle events generated.
 //! This test has been very useful, though due to its complexity good starting inputs are critical.
 
+use bitcoin::amount::Amount;
 use bitcoin::blockdata::constants::genesis_block;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::blockdata::script::{Builder, ScriptBuf};
 use bitcoin::blockdata::opcodes;
 use bitcoin::blockdata::locktime::absolute::LockTime;
 use bitcoin::consensus::encode::deserialize;
-use bitcoin::network::constants::Network;
+use bitcoin::network::Network;
+use bitcoin::transaction::Version;
 
+use bitcoin::WPubkeyHash;
 use bitcoin::hashes::hex::FromHex;
 use bitcoin::hashes::Hash as _;
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
-use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
+use bitcoin::hash_types::{Txid, BlockHash};
 
 use lightning::blinded_path::BlindedPath;
 use lightning::blinded_path::payment::ReceiveTlvs;
@@ -37,7 +40,8 @@ use lightning::chain::transaction::OutPoint;
 use lightning::sign::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
 use lightning::events::Event;
 use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
-use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId, RecipientOnionFields, Retry, InterceptId};
+use lightning::ln::channel_state::ChannelDetails;
+use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentId, RecipientOnionFields, Retry, InterceptId};
 use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
 use lightning::ln::msgs::{self, DecodeError};
 use lightning::ln::script::ShutdownScript;
@@ -48,7 +52,8 @@ use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessa
 use lightning::routing::gossip::{P2PGossipSync, NetworkGraph};
 use lightning::routing::utxo::UtxoLookup;
 use lightning::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router};
-use lightning::util::config::UserConfig;
+use lightning::util::config::{ChannelConfig, UserConfig};
+use lightning::util::hash_tables::*;
 use lightning::util::errors::APIError;
 use lightning::util::test_channel_signer::{TestChannelSigner, EnforcementState};
 use lightning::util::logger::Logger;
@@ -63,12 +68,11 @@ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
 use bitcoin::secp256k1::schnorr;
 
 use std::cell::RefCell;
-use hashbrown::{HashMap, hash_map};
 use std::convert::TryInto;
 use std::cmp;
 use std::sync::{Arc, Mutex};
 use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
-use bitcoin::bech32::u5;
+use bech32::u5;
 
 #[inline]
 pub fn slice_to_be16(v: &[u8]) -> u16 {
@@ -104,6 +108,16 @@ impl InputData {
                Some(&self.data[old_pos..old_pos + len])
        }
 }
+impl std::io::Read for &InputData {
+       fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+               if let Some(sl) = self.get_slice(buf.len()) {
+                       buf.copy_from_slice(sl);
+                       Ok(buf.len())
+               } else {
+                       Ok(0)
+               }
+       }
+}
 
 struct FuzzEstimator {
        input: Arc<InputData>,
@@ -206,6 +220,7 @@ struct MoneyLossDetector<'a> {
        height: usize,
        max_height: usize,
        blocks_connected: u32,
+       error_message: String,
 }
 impl<'a> MoneyLossDetector<'a> {
        pub fn new(peers: &'a RefCell<[bool; 256]>,
@@ -219,11 +234,12 @@ impl<'a> MoneyLossDetector<'a> {
 
                        peers,
                        funding_txn: Vec::new(),
-                       txids_confirmed: HashMap::new(),
+                       txids_confirmed: new_hash_map(),
                        header_hashes: vec![(genesis_block(Network::Bitcoin).block_hash(), 0)],
                        height: 0,
                        max_height: 0,
                        blocks_connected: 0,
+                       error_message: "Channel force-closed".to_string(),
                }
        }
 
@@ -231,13 +247,10 @@ impl<'a> MoneyLossDetector<'a> {
                let mut txdata = Vec::with_capacity(all_txn.len());
                for (idx, tx) in all_txn.iter().enumerate() {
                        let txid = tx.txid();
-                       match self.txids_confirmed.entry(txid) {
-                               hash_map::Entry::Vacant(e) => {
-                                       e.insert(self.height);
-                                       txdata.push((idx + 1, tx));
-                               },
-                               _ => {},
-                       }
+                       self.txids_confirmed.entry(txid).or_insert_with(|| {
+                               txdata.push((idx + 1, tx));
+                               self.height
+                       });
                }
 
                self.blocks_connected += 1;
@@ -281,7 +294,7 @@ impl<'a> Drop for MoneyLossDetector<'a> {
                        }
 
                        // Force all channels onto the chain (and time out claim txn)
-                       self.manager.force_close_all_channels_broadcasting_latest_txn();
+                       self.manager.force_close_all_channels_broadcasting_latest_txn(self.error_message.to_string());
                }
        }
 }
@@ -342,7 +355,7 @@ impl NodeSigner for KeyProvider {
        }
 
        fn sign_gossip_message(&self, msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
-               let msg_hash = Message::from_slice(&Sha256dHash::hash(&msg.encode()[..])[..]).map_err(|_| ())?;
+               let msg_hash = Message::from_digest(Sha256dHash::hash(&msg.encode()[..]).to_byte_array());
                let secp_ctx = Secp256k1::signing_only();
                Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
        }
@@ -479,7 +492,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                node_secret: our_network_key.clone(),
                inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()),
                counter: AtomicU64::new(0),
-               signer_state: RefCell::new(HashMap::new())
+               signer_state: RefCell::new(new_hash_map())
        });
        let network = Network::Bitcoin;
        let best_block_timestamp = genesis_block(network).header.time;
@@ -508,7 +521,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
        let mut intercepted_htlcs: Vec<InterceptId> = Vec::new();
        let mut payments_sent: u16 = 0;
        let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, ScriptBuf)> = Vec::new();
-       let mut pending_funding_signatures = HashMap::new();
+       let mut pending_funding_signatures = new_hash_map();
 
        loop {
                match get_slice!(1)[0] {
@@ -643,11 +656,11 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                                }
                        },
                        10 => {
-                               let mut tx = Transaction { version: 0, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
+                               let mut tx = Transaction { version: Version(0), lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
                                let mut channels = Vec::new();
                                for funding_generation in pending_funding_generation.drain(..) {
                                        let txout = TxOut {
-                                               value: funding_generation.2, script_pubkey: funding_generation.3,
+                                               value: Amount::from_sat(funding_generation.2), script_pubkey: funding_generation.3,
                                        };
                                        if !tx.output.contains(&txout) {
                                                tx.output.push(txout);
@@ -657,7 +670,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                                // Once we switch to V2 channel opens we should be able to drop this entirely as
                                // channel_ids no longer change when we set the funding tx.
                                'search_loop: loop {
-                                       if tx.version > 0xff {
+                                       if tx.version.0 > 0xff {
                                                break;
                                        }
                                        let funding_txid = tx.txid();
@@ -665,15 +678,15 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                                                let outpoint = OutPoint { txid: funding_txid, index: 0 };
                                                for chan in channelmanager.list_channels() {
                                                        if chan.channel_id == ChannelId::v1_from_funding_outpoint(outpoint) {
-                                                               tx.version += 1;
+                                                               tx.version = Version(tx.version.0 + 1);
                                                                continue 'search_loop;
                                                        }
                                                }
                                                break;
                                        }
-                                       tx.version += 1;
+                                       tx.version = Version(tx.version.0 + 1);
                                }
-                               if tx.version <= 0xff && !channels.is_empty() {
+                               if tx.version.0 <= 0xff && !channels.is_empty() {
                                        let chans = channels.iter().map(|(a, b)| (a, b)).collect::<Vec<_>>();
                                        if let Err(e) = channelmanager.batch_funding_transaction_generated(&chans, tx.clone()) {
                                                // It's possible the channel has been closed in the mean time, but any other
@@ -706,11 +719,11 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                                } else {
                                        let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
                                        if let Ok(tx) = txres {
-                                               let mut output_val = 0;
+                                               let mut output_val = Amount::ZERO;
                                                for out in tx.output.iter() {
-                                                       if out.value > 21_000_000_0000_0000 { return; }
+                                                       if out.value > Amount::MAX_MONEY { return; }
                                                        output_val += out.value;
-                                                       if output_val > 21_000_000_0000_0000 { return; }
+                                                       if output_val > Amount::MAX_MONEY { return; }
                                                }
                                                loss_detector.connect_block(&[tx]);
                                        } else {
@@ -724,16 +737,17 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                        14 => {
                                let mut channels = channelmanager.list_channels();
                                let channel_id = get_slice!(1)[0] as usize;
+                               let error_message = "Channel force-closed";
                                if channel_id >= channels.len() { return; }
                                channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
-                               channelmanager.force_close_broadcasting_latest_txn(&channels[channel_id].channel_id, &channels[channel_id].counterparty.node_id).unwrap();
+                               channelmanager.force_close_broadcasting_latest_txn(&channels[channel_id].channel_id, &channels[channel_id].counterparty.node_id, error_message.to_string()).unwrap();
                        },
                        // 15, 16, 17, 18 is above
                        19 => {
-                               let mut list = loss_detector.handler.get_peer_node_ids();
-                               list.sort_by_key(|v| v.0);
-                               if let Some((id, _)) = list.get(0) {
-                                       loss_detector.handler.disconnect_by_node_id(*id);
+                               let mut list = loss_detector.handler.list_peers();
+                               list.sort_by_key(|v| v.counterparty_node_id);
+                               if let Some(peer_details) = list.get(0) {
+                                       loss_detector.handler.disconnect_by_node_id(peer_details.counterparty_node_id);
                                }
                        },
                        20 => loss_detector.handler.disconnect_all_peers(),
@@ -758,6 +772,16 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
                                        }
                                }
                        }
+                       35 => {
+                               let config: ChannelConfig =
+                                       if let Ok(c) = Readable::read(&mut &*input) { c } else { return; };
+                               let chans = channelmanager.list_channels();
+                               if let Some(chan) = chans.get(0) {
+                                       let _ = channelmanager.update_channel_config(
+                                               &chan.counterparty.node_id, &[chan.channel_id], &config
+                                       );
+                               }
+                       }
                        _ => return,
                }
                loss_detector.handler.process_events();
@@ -954,6 +978,8 @@ mod tests {
 
                // create the funding transaction (client should send funding_created now)
                ext_from_hex("0a", &mut test);
+               // Two feerate requests to check the dust exposure on the initial commitment tx
+               ext_from_hex("00fd00fd", &mut test);
 
                // inbound read from peer id 1 of len 18
                ext_from_hex("030112", &mut test);
@@ -1002,6 +1028,9 @@ mod tests {
                // end of update_add_htlc from 0 to 1 via client and mac
                ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
 
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
+
                // inbound read from peer id 0 of len 18
                ext_from_hex("030012", &mut test);
                // message header indicating message length 100
@@ -1023,6 +1052,8 @@ mod tests {
 
                // process the now-pending HTLC forward
                ext_from_hex("07", &mut test);
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
                // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: UpdateHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
 
                // we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
@@ -1098,6 +1129,9 @@ mod tests {
                // end of update_add_htlc from 0 to 1 via client and mac
                ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
 
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
+
                // now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
                // inbound read from peer id 0 of len 18
                ext_from_hex("030012", &mut test);
@@ -1129,6 +1163,10 @@ mod tests {
 
                // process the now-pending HTLC forward
                ext_from_hex("07", &mut test);
+
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
+
                // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
                // we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
 
@@ -1226,6 +1264,9 @@ mod tests {
                // end of update_add_htlc from 0 to 1 via client and mac
                ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
 
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
+
                // inbound read from peer id 0 of len 18
                ext_from_hex("030012", &mut test);
                // message header indicating message length 164
@@ -1247,6 +1288,8 @@ mod tests {
 
                // process the now-pending HTLC forward
                ext_from_hex("07", &mut test);
+               // Two feerate requests to check dust exposure
+               ext_from_hex("00fd00fd", &mut test);
                // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
 
                // connect a block with one transaction of len 125