Merge pull request #3129 from optout21/splicing-msgs-update
[rust-lightning] / fuzz / src / full_stack.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Test that no series of bytes received over the wire/connections created/payments sent can
11 //! result in a crash. We do this by standing up a node and then reading bytes from input to denote
12 //! actions such as creating new inbound/outbound connections, bytes to be read from a connection,
13 //! or payments to send/ways to handle events generated.
14 //! This test has been very useful, though due to its complexity good starting inputs are critical.
15
16 use bitcoin::amount::Amount;
17 use bitcoin::blockdata::constants::genesis_block;
18 use bitcoin::blockdata::locktime::absolute::LockTime;
19 use bitcoin::blockdata::opcodes;
20 use bitcoin::blockdata::script::{Builder, ScriptBuf};
21 use bitcoin::blockdata::transaction::{Transaction, TxOut};
22 use bitcoin::consensus::encode::deserialize;
23 use bitcoin::network::Network;
24 use bitcoin::transaction::Version;
25
26 use bitcoin::hash_types::{BlockHash, Txid};
27 use bitcoin::hashes::hex::FromHex;
28 use bitcoin::hashes::sha256::Hash as Sha256;
29 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
30 use bitcoin::hashes::Hash as _;
31 use bitcoin::WPubkeyHash;
32
33 use lightning::blinded_path::payment::ReceiveTlvs;
34 use lightning::blinded_path::BlindedPath;
35 use lightning::chain;
36 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
37 use lightning::chain::chainmonitor;
38 use lightning::chain::transaction::OutPoint;
39 use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
40 use lightning::events::Event;
41 use lightning::ln::channel_state::ChannelDetails;
42 use lightning::ln::channelmanager::{
43         ChainParameters, ChannelManager, InterceptId, PaymentId, RecipientOnionFields, Retry,
44 };
45 use lightning::ln::functional_test_utils::*;
46 use lightning::ln::msgs::{self, DecodeError};
47 use lightning::ln::peer_handler::{
48         IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
49 };
50 use lightning::ln::script::ShutdownScript;
51 use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
52 use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
53 use lightning::offers::invoice_request::UnsignedInvoiceRequest;
54 use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
55 use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
56 use lightning::routing::router::{
57         InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router,
58 };
59 use lightning::routing::utxo::UtxoLookup;
60 use lightning::sign::{
61         EntropySource, InMemorySigner, KeyMaterial, NodeSigner, Recipient, SignerProvider,
62 };
63 use lightning::util::config::{ChannelConfig, UserConfig};
64 use lightning::util::errors::APIError;
65 use lightning::util::hash_tables::*;
66 use lightning::util::logger::Logger;
67 use lightning::util::ser::{Readable, ReadableArgs, Writeable};
68 use lightning::util::test_channel_signer::{EnforcementState, TestChannelSigner};
69
70 use crate::utils::test_logger;
71 use crate::utils::test_persister::TestPersister;
72
73 use bitcoin::secp256k1::ecdh::SharedSecret;
74 use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
75 use bitcoin::secp256k1::schnorr;
76 use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
77
78 use bech32::u5;
79 use std::cell::RefCell;
80 use std::cmp;
81 use std::convert::TryInto;
82 use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
83 use std::sync::{Arc, Mutex};
84
85 #[inline]
86 #[rustfmt::skip]
87 pub fn slice_to_be16(v: &[u8]) -> u16 {
88         ((v[0] as u16) << 8*1) |
89         ((v[1] as u16) << 8*0)
90 }
91
92 #[inline]
93 pub fn be16_to_array(u: u16) -> [u8; 2] {
94         let mut v = [0; 2];
95         v[0] = ((u >> 8 * 1) & 0xff) as u8;
96         v[1] = ((u >> 8 * 0) & 0xff) as u8;
97         v
98 }
99
100 #[inline]
101 #[rustfmt::skip]
102 pub fn slice_to_be24(v: &[u8]) -> u32 {
103         ((v[0] as u32) << 8*2) |
104         ((v[1] as u32) << 8*1) |
105         ((v[2] as u32) << 8*0)
106 }
107
108 struct InputData {
109         data: Vec<u8>,
110         read_pos: AtomicUsize,
111         halt_fee_est_reads: AtomicBool,
112 }
113 impl InputData {
114         fn get_slice(&self, len: usize) -> Option<&[u8]> {
115                 let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
116                 if self.data.len() < old_pos + len {
117                         return None;
118                 }
119                 Some(&self.data[old_pos..old_pos + len])
120         }
121 }
122 impl std::io::Read for &InputData {
123         fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
124                 if let Some(sl) = self.get_slice(buf.len()) {
125                         buf.copy_from_slice(sl);
126                         Ok(buf.len())
127                 } else {
128                         Ok(0)
129                 }
130         }
131 }
132
133 struct FuzzEstimator {
134         input: Arc<InputData>,
135 }
136 impl FeeEstimator for FuzzEstimator {
137         fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 {
138                 if self.input.halt_fee_est_reads.load(Ordering::Acquire) {
139                         return 253;
140                 }
141                 //TODO: We should actually be testing at least much more than 64k...
142                 match self.input.get_slice(2) {
143                         Some(slice) => cmp::max(slice_to_be16(slice) as u32, 253),
144                         None => 253,
145                 }
146         }
147 }
148
149 struct FuzzRouter {}
150
151 impl Router for FuzzRouter {
152         fn find_route(
153                 &self, _payer: &PublicKey, _params: &RouteParameters,
154                 _first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs,
155         ) -> Result<Route, msgs::LightningError> {
156                 Err(msgs::LightningError {
157                         err: String::from("Not implemented"),
158                         action: msgs::ErrorAction::IgnoreError,
159                 })
160         }
161
162         fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
163                 &self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
164                 _amount_msats: u64, _secp_ctx: &Secp256k1<T>,
165         ) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
166                 unreachable!()
167         }
168 }
169
170 impl MessageRouter for FuzzRouter {
171         fn find_path(
172                 &self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
173         ) -> Result<OnionMessagePath, ()> {
174                 unreachable!()
175         }
176
177         fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
178                 &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
179         ) -> Result<Vec<BlindedPath>, ()> {
180                 unreachable!()
181         }
182 }
183
184 struct TestBroadcaster {
185         txn_broadcasted: Mutex<Vec<Transaction>>,
186 }
187 impl BroadcasterInterface for TestBroadcaster {
188         fn broadcast_transactions(&self, txs: &[&Transaction]) {
189                 let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
190                 self.txn_broadcasted.lock().unwrap().extend(owned_txs);
191         }
192 }
193
194 #[derive(Clone)]
195 struct Peer<'a> {
196         id: u8,
197         peers_connected: &'a RefCell<[bool; 256]>,
198 }
199 impl<'a> SocketDescriptor for Peer<'a> {
200         fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
201                 data.len()
202         }
203         fn disconnect_socket(&mut self) {
204                 assert!(self.peers_connected.borrow()[self.id as usize]);
205                 self.peers_connected.borrow_mut()[self.id as usize] = false;
206         }
207 }
208 impl<'a> PartialEq for Peer<'a> {
209         fn eq(&self, other: &Self) -> bool {
210                 self.id == other.id
211         }
212 }
213 impl<'a> Eq for Peer<'a> {}
214 impl<'a> std::hash::Hash for Peer<'a> {
215         fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
216                 self.id.hash(h)
217         }
218 }
219
220 type ChannelMan<'a> = ChannelManager<
221         Arc<
222                 chainmonitor::ChainMonitor<
223                         TestChannelSigner,
224                         Arc<dyn chain::Filter>,
225                         Arc<TestBroadcaster>,
226                         Arc<FuzzEstimator>,
227                         Arc<dyn Logger>,
228                         Arc<TestPersister>,
229                 >,
230         >,
231         Arc<TestBroadcaster>,
232         Arc<KeyProvider>,
233         Arc<KeyProvider>,
234         Arc<KeyProvider>,
235         Arc<FuzzEstimator>,
236         &'a FuzzRouter,
237         Arc<dyn Logger>,
238 >;
239 type PeerMan<'a> = PeerManager<
240         Peer<'a>,
241         Arc<ChannelMan<'a>>,
242         Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn UtxoLookup>, Arc<dyn Logger>>>,
243         IgnoringMessageHandler,
244         Arc<dyn Logger>,
245         IgnoringMessageHandler,
246         Arc<KeyProvider>,
247 >;
248
249 struct MoneyLossDetector<'a> {
250         manager: Arc<ChannelMan<'a>>,
251         monitor: Arc<
252                 chainmonitor::ChainMonitor<
253                         TestChannelSigner,
254                         Arc<dyn chain::Filter>,
255                         Arc<TestBroadcaster>,
256                         Arc<FuzzEstimator>,
257                         Arc<dyn Logger>,
258                         Arc<TestPersister>,
259                 >,
260         >,
261         handler: PeerMan<'a>,
262
263         peers: &'a RefCell<[bool; 256]>,
264         funding_txn: Vec<Transaction>,
265         txids_confirmed: HashMap<Txid, usize>,
266         header_hashes: Vec<(BlockHash, u32)>,
267         height: usize,
268         max_height: usize,
269         blocks_connected: u32,
270         error_message: String,
271 }
272 impl<'a> MoneyLossDetector<'a> {
273         pub fn new(
274                 peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelMan<'a>>,
275                 monitor: Arc<
276                         chainmonitor::ChainMonitor<
277                                 TestChannelSigner,
278                                 Arc<dyn chain::Filter>,
279                                 Arc<TestBroadcaster>,
280                                 Arc<FuzzEstimator>,
281                                 Arc<dyn Logger>,
282                                 Arc<TestPersister>,
283                         >,
284                 >,
285                 handler: PeerMan<'a>,
286         ) -> Self {
287                 MoneyLossDetector {
288                         manager,
289                         monitor,
290                         handler,
291
292                         peers,
293                         funding_txn: Vec::new(),
294                         txids_confirmed: new_hash_map(),
295                         header_hashes: vec![(genesis_block(Network::Bitcoin).block_hash(), 0)],
296                         height: 0,
297                         max_height: 0,
298                         blocks_connected: 0,
299                         error_message: "Channel force-closed".to_string(),
300                 }
301         }
302
303         fn connect_block(&mut self, all_txn: &[Transaction]) {
304                 let mut txdata = Vec::with_capacity(all_txn.len());
305                 for (idx, tx) in all_txn.iter().enumerate() {
306                         let txid = tx.txid();
307                         self.txids_confirmed.entry(txid).or_insert_with(|| {
308                                 txdata.push((idx + 1, tx));
309                                 self.height
310                         });
311                 }
312
313                 self.blocks_connected += 1;
314                 let header = create_dummy_header(self.header_hashes[self.height].0, self.blocks_connected);
315                 self.height += 1;
316                 self.manager.transactions_confirmed(&header, &txdata, self.height as u32);
317                 self.manager.best_block_updated(&header, self.height as u32);
318                 (*self.monitor).transactions_confirmed(&header, &txdata, self.height as u32);
319                 (*self.monitor).best_block_updated(&header, self.height as u32);
320                 if self.header_hashes.len() > self.height {
321                         self.header_hashes[self.height] = (header.block_hash(), self.blocks_connected);
322                 } else {
323                         assert_eq!(self.header_hashes.len(), self.height);
324                         self.header_hashes.push((header.block_hash(), self.blocks_connected));
325                 }
326                 self.max_height = cmp::max(self.height, self.max_height);
327         }
328
329         fn disconnect_block(&mut self) {
330                 if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
331                         let header = create_dummy_header(
332                                 self.header_hashes[self.height - 1].0,
333                                 self.header_hashes[self.height].1,
334                         );
335                         self.manager.block_disconnected(&header, self.height as u32);
336                         self.monitor.block_disconnected(&header, self.height as u32);
337                         self.height -= 1;
338                         let removal_height = self.height;
339                         self.txids_confirmed.retain(|_, height| removal_height != *height);
340                 }
341         }
342 }
343
344 impl<'a> Drop for MoneyLossDetector<'a> {
345         fn drop(&mut self) {
346                 if !::std::thread::panicking() {
347                         // Disconnect all peers
348                         for (idx, peer) in self.peers.borrow().iter().enumerate() {
349                                 if *peer {
350                                         self.handler
351                                                 .socket_disconnected(&Peer { id: idx as u8, peers_connected: &self.peers });
352                                 }
353                         }
354
355                         // Force all channels onto the chain (and time out claim txn)
356                         self.manager
357                                 .force_close_all_channels_broadcasting_latest_txn(self.error_message.to_string());
358                 }
359         }
360 }
361
362 struct KeyProvider {
363         node_secret: SecretKey,
364         inbound_payment_key: KeyMaterial,
365         counter: AtomicU64,
366         signer_state: RefCell<HashMap<u8, (bool, Arc<Mutex<EnforcementState>>)>>,
367 }
368
369 impl EntropySource for KeyProvider {
370         fn get_secure_random_bytes(&self) -> [u8; 32] {
371                 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
372                 #[rustfmt::skip]
373                 let random_bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
374                         (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8,
375                         (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8];
376                 random_bytes
377         }
378 }
379
380 impl NodeSigner for KeyProvider {
381         fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
382                 let node_secret = match recipient {
383                         Recipient::Node => Ok(&self.node_secret),
384                         Recipient::PhantomNode => Err(()),
385                 }?;
386                 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
387         }
388
389         fn ecdh(
390                 &self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>,
391         ) -> Result<SharedSecret, ()> {
392                 let mut node_secret = match recipient {
393                         Recipient::Node => Ok(self.node_secret.clone()),
394                         Recipient::PhantomNode => Err(()),
395                 }?;
396                 if let Some(tweak) = tweak {
397                         node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
398                 }
399                 Ok(SharedSecret::new(other_key, &node_secret))
400         }
401
402         fn get_inbound_payment_key_material(&self) -> KeyMaterial {
403                 self.inbound_payment_key.clone()
404         }
405
406         fn sign_invoice(
407                 &self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
408         ) -> Result<RecoverableSignature, ()> {
409                 unreachable!()
410         }
411
412         fn sign_bolt12_invoice_request(
413                 &self, _invoice_request: &UnsignedInvoiceRequest,
414         ) -> Result<schnorr::Signature, ()> {
415                 unreachable!()
416         }
417
418         fn sign_bolt12_invoice(
419                 &self, _invoice: &UnsignedBolt12Invoice,
420         ) -> Result<schnorr::Signature, ()> {
421                 unreachable!()
422         }
423
424         fn sign_gossip_message(
425                 &self, msg: lightning::ln::msgs::UnsignedGossipMessage,
426         ) -> Result<Signature, ()> {
427                 let msg_hash = Message::from_digest(Sha256dHash::hash(&msg.encode()[..]).to_byte_array());
428                 let secp_ctx = Secp256k1::signing_only();
429                 Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
430         }
431 }
432
433 impl SignerProvider for KeyProvider {
434         type EcdsaSigner = TestChannelSigner;
435         #[cfg(taproot)]
436         type TaprootSigner = TestChannelSigner;
437
438         fn generate_channel_keys_id(
439                 &self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128,
440         ) -> [u8; 32] {
441                 let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
442                 self.signer_state
443                         .borrow_mut()
444                         .insert(ctr, (inbound, Arc::new(Mutex::new(EnforcementState::new()))));
445                 [ctr; 32]
446         }
447
448         fn derive_channel_signer(
449                 &self, channel_value_satoshis: u64, channel_keys_id: [u8; 32],
450         ) -> Self::EcdsaSigner {
451                 let secp_ctx = Secp256k1::signing_only();
452                 let ctr = channel_keys_id[0];
453                 let (inbound, state) = self.signer_state.borrow().get(&ctr).unwrap().clone();
454                 TestChannelSigner::new_with_revoked(
455                         if inbound {
456                                 InMemorySigner::new(
457                                         &secp_ctx,
458                                         SecretKey::from_slice(&[
459                                                 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,
460                                                 0, 0, 0, 0, 0, 1, ctr,
461                                         ])
462                                         .unwrap(),
463                                         SecretKey::from_slice(&[
464                                                 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,
465                                                 0, 0, 0, 0, 0, 2, ctr,
466                                         ])
467                                         .unwrap(),
468                                         SecretKey::from_slice(&[
469                                                 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,
470                                                 0, 0, 0, 0, 0, 3, ctr,
471                                         ])
472                                         .unwrap(),
473                                         SecretKey::from_slice(&[
474                                                 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,
475                                                 0, 0, 0, 0, 0, 4, ctr,
476                                         ])
477                                         .unwrap(),
478                                         SecretKey::from_slice(&[
479                                                 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,
480                                                 0, 0, 0, 0, 0, 5, ctr,
481                                         ])
482                                         .unwrap(),
483                                         [
484                                                 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,
485                                                 0, 0, 0, 0, 0, 6, ctr,
486                                         ],
487                                         channel_value_satoshis,
488                                         channel_keys_id,
489                                         channel_keys_id,
490                                 )
491                         } else {
492                                 InMemorySigner::new(
493                                         &secp_ctx,
494                                         SecretKey::from_slice(&[
495                                                 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,
496                                                 0, 0, 0, 0, 0, 7, ctr,
497                                         ])
498                                         .unwrap(),
499                                         SecretKey::from_slice(&[
500                                                 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,
501                                                 0, 0, 0, 0, 0, 8, ctr,
502                                         ])
503                                         .unwrap(),
504                                         SecretKey::from_slice(&[
505                                                 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,
506                                                 0, 0, 0, 0, 0, 9, ctr,
507                                         ])
508                                         .unwrap(),
509                                         SecretKey::from_slice(&[
510                                                 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,
511                                                 0, 0, 0, 0, 0, 10, ctr,
512                                         ])
513                                         .unwrap(),
514                                         SecretKey::from_slice(&[
515                                                 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,
516                                                 0, 0, 0, 0, 0, 11, ctr,
517                                         ])
518                                         .unwrap(),
519                                         [
520                                                 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,
521                                                 0, 0, 0, 0, 0, 12, ctr,
522                                         ],
523                                         channel_value_satoshis,
524                                         channel_keys_id,
525                                         channel_keys_id,
526                                 )
527                         },
528                         state,
529                         false,
530                 )
531         }
532
533         fn read_chan_signer(&self, mut data: &[u8]) -> Result<TestChannelSigner, DecodeError> {
534                 let inner: InMemorySigner = ReadableArgs::read(&mut data, self)?;
535                 let state = Arc::new(Mutex::new(EnforcementState::new()));
536
537                 Ok(TestChannelSigner::new_with_revoked(inner, state, false))
538         }
539
540         fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {
541                 let secp_ctx = Secp256k1::signing_only();
542                 let channel_monitor_claim_key = SecretKey::from_slice(
543                         &<Vec<u8>>::from_hex(
544                                 "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
545                         )
546                         .unwrap()[..],
547                 )
548                 .unwrap();
549                 let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(
550                         &PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize(),
551                 );
552                 Ok(Builder::new()
553                         .push_opcode(opcodes::all::OP_PUSHBYTES_0)
554                         .push_slice(our_channel_monitor_claim_key_hash)
555                         .into_script())
556         }
557
558         fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
559                 let secp_ctx = Secp256k1::signing_only();
560                 let secret_key = SecretKey::from_slice(&[
561                         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,
562                         0, 0, 1,
563                 ])
564                 .unwrap();
565                 let pubkey_hash =
566                         WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
567                 Ok(ShutdownScript::new_p2wpkh(&pubkey_hash))
568         }
569 }
570
571 #[inline]
572 pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
573         if data.len() < 32 {
574                 return;
575         }
576
577         let our_network_key = match SecretKey::from_slice(&data[..32]) {
578                 Ok(key) => key,
579                 Err(_) => return,
580         };
581         data = &data[32..];
582
583         let config: UserConfig = if let Ok(config) = Readable::read(&mut data) {
584                 config
585         } else {
586                 return;
587         };
588
589         let input = Arc::new(InputData {
590                 data: data.to_vec(),
591                 read_pos: AtomicUsize::new(0),
592                 halt_fee_est_reads: AtomicBool::new(false),
593         });
594         let fee_est = Arc::new(FuzzEstimator { input: input.clone() });
595         let router = FuzzRouter {};
596
597         macro_rules! get_slice {
598                 ($len: expr) => {
599                         match input.get_slice($len as usize) {
600                                 Some(slice) => slice,
601                                 None => return,
602                         }
603                 };
604         }
605
606         macro_rules! get_bytes {
607                 ($len: expr) => {{
608                         let mut res = [0; $len];
609                         match input.get_slice($len as usize) {
610                                 Some(slice) => res.copy_from_slice(slice),
611                                 None => return,
612                         }
613                         res
614                 }};
615         }
616
617         macro_rules! get_pubkey {
618                 () => {
619                         match PublicKey::from_slice(get_slice!(33)) {
620                                 Ok(key) => key,
621                                 Err(_) => return,
622                         }
623                 };
624         }
625
626         let inbound_payment_key = [
627                 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,
628                 0, 42,
629         ];
630
631         let broadcast = Arc::new(TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()) });
632         let monitor = Arc::new(chainmonitor::ChainMonitor::new(
633                 None,
634                 broadcast.clone(),
635                 Arc::clone(&logger),
636                 fee_est.clone(),
637                 Arc::new(TestPersister { update_ret: Mutex::new(ChannelMonitorUpdateStatus::Completed) }),
638         ));
639
640         let keys_manager = Arc::new(KeyProvider {
641                 node_secret: our_network_key.clone(),
642                 inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()),
643                 counter: AtomicU64::new(0),
644                 signer_state: RefCell::new(new_hash_map()),
645         });
646         let network = Network::Bitcoin;
647         let best_block_timestamp = genesis_block(network).header.time;
648         let params = ChainParameters { network, best_block: BestBlock::from_network(network) };
649         let channelmanager = Arc::new(ChannelManager::new(
650                 fee_est.clone(),
651                 monitor.clone(),
652                 broadcast.clone(),
653                 &router,
654                 Arc::clone(&logger),
655                 keys_manager.clone(),
656                 keys_manager.clone(),
657                 keys_manager.clone(),
658                 config,
659                 params,
660                 best_block_timestamp,
661         ));
662         // Adding new calls to `EntropySource::get_secure_random_bytes` during startup can change all the
663         // keys subsequently generated in this test. Rather than regenerating all the messages manually,
664         // it's easier to just increment the counter here so the keys don't change.
665         keys_manager.counter.fetch_sub(3, Ordering::AcqRel);
666         let network_graph = Arc::new(NetworkGraph::new(network, Arc::clone(&logger)));
667         let gossip_sync =
668                 Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
669
670         let peers = RefCell::new([false; 256]);
671         let message_handler = MessageHandler {
672                 chan_handler: channelmanager.clone(),
673                 route_handler: gossip_sync.clone(),
674                 onion_message_handler: IgnoringMessageHandler {},
675                 custom_message_handler: IgnoringMessageHandler {},
676         };
677         let random_data = [
678                 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,
679                 15, 0,
680         ];
681         let peer_manager = PeerManager::new(
682                 message_handler,
683                 0,
684                 &random_data,
685                 Arc::clone(&logger),
686                 keys_manager.clone(),
687         );
688         let mut loss_detector =
689                 MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), peer_manager);
690
691         let mut should_forward = false;
692         let mut payments_received: Vec<PaymentHash> = Vec::new();
693         let mut intercepted_htlcs: Vec<InterceptId> = Vec::new();
694         let mut payments_sent: u16 = 0;
695         let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, ScriptBuf)> = Vec::new();
696         let mut pending_funding_signatures = new_hash_map();
697
698         loop {
699                 match get_slice!(1)[0] {
700                         0 => {
701                                 let mut new_id = 0;
702                                 for i in 1..256 {
703                                         if !peers.borrow()[i - 1] {
704                                                 new_id = i;
705                                                 break;
706                                         }
707                                 }
708                                 if new_id == 0 {
709                                         return;
710                                 }
711                                 let peer = Peer { id: (new_id - 1) as u8, peers_connected: &peers };
712                                 loss_detector.handler.new_outbound_connection(get_pubkey!(), peer, None).unwrap();
713                                 peers.borrow_mut()[new_id - 1] = true;
714                         },
715                         1 => {
716                                 let mut new_id = 0;
717                                 for i in 1..256 {
718                                         if !peers.borrow()[i - 1] {
719                                                 new_id = i;
720                                                 break;
721                                         }
722                                 }
723                                 if new_id == 0 {
724                                         return;
725                                 }
726                                 let peer = Peer { id: (new_id - 1) as u8, peers_connected: &peers };
727                                 loss_detector.handler.new_inbound_connection(peer, None).unwrap();
728                                 peers.borrow_mut()[new_id - 1] = true;
729                         },
730                         2 => {
731                                 let peer_id = get_slice!(1)[0];
732                                 if !peers.borrow()[peer_id as usize] {
733                                         return;
734                                 }
735                                 let peer = Peer { id: peer_id, peers_connected: &peers };
736                                 loss_detector.handler.socket_disconnected(&peer);
737                                 peers.borrow_mut()[peer_id as usize] = false;
738                         },
739                         3 => {
740                                 let peer_id = get_slice!(1)[0];
741                                 if !peers.borrow()[peer_id as usize] {
742                                         return;
743                                 }
744                                 let mut peer = Peer { id: peer_id, peers_connected: &peers };
745                                 match loss_detector.handler.read_event(&mut peer, get_slice!(get_slice!(1)[0])) {
746                                         Ok(res) => assert!(!res),
747                                         Err(_) => {
748                                                 peers.borrow_mut()[peer_id as usize] = false;
749                                         },
750                                 }
751                         },
752                         4 => {
753                                 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
754                                 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
755                                 let params = RouteParameters::from_payment_params_and_value(
756                                         payment_params,
757                                         final_value_msat,
758                                 );
759                                 let mut payment_hash = PaymentHash([0; 32]);
760                                 payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
761                                 payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
762                                 payments_sent += 1;
763                                 let _ = channelmanager.send_payment(
764                                         payment_hash,
765                                         RecipientOnionFields::spontaneous_empty(),
766                                         PaymentId(payment_hash.0),
767                                         params,
768                                         Retry::Attempts(2),
769                                 );
770                         },
771                         15 => {
772                                 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
773                                 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
774                                 let params = RouteParameters::from_payment_params_and_value(
775                                         payment_params,
776                                         final_value_msat,
777                                 );
778                                 let mut payment_hash = PaymentHash([0; 32]);
779                                 payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
780                                 payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
781                                 payments_sent += 1;
782                                 let mut payment_secret = PaymentSecret([0; 32]);
783                                 payment_secret.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
784                                 payments_sent += 1;
785                                 let _ = channelmanager.send_payment(
786                                         payment_hash,
787                                         RecipientOnionFields::secret_only(payment_secret),
788                                         PaymentId(payment_hash.0),
789                                         params,
790                                         Retry::Attempts(2),
791                                 );
792                         },
793                         17 => {
794                                 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
795                                 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
796                                 let params = RouteParameters::from_payment_params_and_value(
797                                         payment_params,
798                                         final_value_msat,
799                                 );
800                                 let _ = channelmanager.send_preflight_probes(params, None);
801                         },
802                         18 => {
803                                 let idx = u16::from_be_bytes(get_bytes!(2)) % cmp::max(payments_sent, 1);
804                                 let mut payment_id = PaymentId([0; 32]);
805                                 payment_id.0[0..2].copy_from_slice(&idx.to_be_bytes());
806                                 channelmanager.abandon_payment(payment_id);
807                         },
808                         5 => {
809                                 let peer_id = get_slice!(1)[0];
810                                 if !peers.borrow()[peer_id as usize] {
811                                         return;
812                                 }
813                                 let their_key = get_pubkey!();
814                                 let chan_value = slice_to_be24(get_slice!(3)) as u64;
815                                 let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
816                                 if channelmanager
817                                         .create_channel(their_key, chan_value, push_msat_value, 0, None, None)
818                                         .is_err()
819                                 {
820                                         return;
821                                 }
822                         },
823                         6 => {
824                                 let mut channels = channelmanager.list_channels();
825                                 let channel_id = get_slice!(1)[0] as usize;
826                                 if channel_id >= channels.len() {
827                                         return;
828                                 }
829                                 channels.sort_by(|a, b| a.channel_id.cmp(&b.channel_id));
830                                 if channelmanager
831                                         .close_channel(
832                                                 &channels[channel_id].channel_id,
833                                                 &channels[channel_id].counterparty.node_id,
834                                         )
835                                         .is_err()
836                                 {
837                                         return;
838                                 }
839                         },
840                         7 => {
841                                 if should_forward {
842                                         channelmanager.process_pending_htlc_forwards();
843                                         should_forward = false;
844                                 }
845                         },
846                         8 => {
847                                 for payment in payments_received.drain(..) {
848                                         // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
849                                         // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
850                                         // fulfill this HTLC, but if they are, we can just take the first byte and
851                                         // place that anywhere in our preimage.
852                                         if &payment.0[1..] != &[0; 31] {
853                                                 channelmanager.fail_htlc_backwards(&payment);
854                                         } else {
855                                                 let mut payment_preimage = PaymentPreimage([0; 32]);
856                                                 payment_preimage.0[0] = payment.0[0];
857                                                 channelmanager.claim_funds(payment_preimage);
858                                         }
859                                 }
860                         },
861                         16 => {
862                                 let payment_preimage = PaymentPreimage(keys_manager.get_secure_random_bytes());
863                                 let payment_hash =
864                                         PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
865                                 // Note that this may fail - our hashes may collide and we'll end up trying to
866                                 // double-register the same payment_hash.
867                                 let _ = channelmanager.create_inbound_payment_for_hash(payment_hash, None, 1, None);
868                         },
869                         9 => {
870                                 for payment in payments_received.drain(..) {
871                                         channelmanager.fail_htlc_backwards(&payment);
872                                 }
873                         },
874                         10 => {
875                                 let mut tx = Transaction {
876                                         version: Version(0),
877                                         lock_time: LockTime::ZERO,
878                                         input: Vec::new(),
879                                         output: Vec::new(),
880                                 };
881                                 let mut channels = Vec::new();
882                                 for funding_generation in pending_funding_generation.drain(..) {
883                                         let txout = TxOut {
884                                                 value: Amount::from_sat(funding_generation.2),
885                                                 script_pubkey: funding_generation.3,
886                                         };
887                                         if !tx.output.contains(&txout) {
888                                                 tx.output.push(txout);
889                                                 channels.push((funding_generation.0, funding_generation.1));
890                                         }
891                                 }
892                                 // Once we switch to V2 channel opens we should be able to drop this entirely as
893                                 // channel_ids no longer change when we set the funding tx.
894                                 'search_loop: loop {
895                                         if tx.version.0 > 0xff {
896                                                 break;
897                                         }
898                                         let funding_txid = tx.txid();
899                                         if loss_detector.txids_confirmed.get(&funding_txid).is_none() {
900                                                 let outpoint = OutPoint { txid: funding_txid, index: 0 };
901                                                 for chan in channelmanager.list_channels() {
902                                                         if chan.channel_id == ChannelId::v1_from_funding_outpoint(outpoint) {
903                                                                 tx.version = Version(tx.version.0 + 1);
904                                                                 continue 'search_loop;
905                                                         }
906                                                 }
907                                                 break;
908                                         }
909                                         tx.version = Version(tx.version.0 + 1);
910                                 }
911                                 if tx.version.0 <= 0xff && !channels.is_empty() {
912                                         let chans = channels.iter().map(|(a, b)| (a, b)).collect::<Vec<_>>();
913                                         if let Err(e) =
914                                                 channelmanager.batch_funding_transaction_generated(&chans, tx.clone())
915                                         {
916                                                 // It's possible the channel has been closed in the mean time, but any other
917                                                 // failure may be a bug.
918                                                 if let APIError::ChannelUnavailable { .. } = e {
919                                                 } else {
920                                                         panic!();
921                                                 }
922                                         }
923                                         let funding_txid = tx.txid();
924                                         for idx in 0..tx.output.len() {
925                                                 let outpoint = OutPoint { txid: funding_txid, index: idx as u16 };
926                                                 pending_funding_signatures.insert(outpoint, tx.clone());
927                                         }
928                                 }
929                         },
930                         11 => {
931                                 let mut txn = broadcast.txn_broadcasted.lock().unwrap().split_off(0);
932                                 if !txn.is_empty() {
933                                         input.halt_fee_est_reads.store(true, Ordering::Release);
934                                         loss_detector.connect_block(&txn[..]);
935                                         for _ in 2..100 {
936                                                 loss_detector.connect_block(&[]);
937                                         }
938                                         input.halt_fee_est_reads.store(false, Ordering::Release);
939                                 }
940                                 for tx in txn.drain(..) {
941                                         loss_detector.funding_txn.push(tx);
942                                 }
943                         },
944                         12 => {
945                                 let txlen = u16::from_be_bytes(get_bytes!(2));
946                                 if txlen == 0 {
947                                         loss_detector.connect_block(&[]);
948                                 } else {
949                                         let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
950                                         if let Ok(tx) = txres {
951                                                 let mut output_val = Amount::ZERO;
952                                                 for out in tx.output.iter() {
953                                                         if out.value > Amount::MAX_MONEY {
954                                                                 return;
955                                                         }
956                                                         output_val += out.value;
957                                                         if output_val > Amount::MAX_MONEY {
958                                                                 return;
959                                                         }
960                                                 }
961                                                 loss_detector.connect_block(&[tx]);
962                                         } else {
963                                                 return;
964                                         }
965                                 }
966                         },
967                         13 => {
968                                 loss_detector.disconnect_block();
969                         },
970                         14 => {
971                                 let mut channels = channelmanager.list_channels();
972                                 let channel_id = get_slice!(1)[0] as usize;
973                                 let error_message = "Channel force-closed";
974                                 if channel_id >= channels.len() {
975                                         return;
976                                 }
977                                 channels.sort_by(|a, b| a.channel_id.cmp(&b.channel_id));
978                                 channelmanager
979                                         .force_close_broadcasting_latest_txn(
980                                                 &channels[channel_id].channel_id,
981                                                 &channels[channel_id].counterparty.node_id,
982                                                 error_message.to_string(),
983                                         )
984                                         .unwrap();
985                         },
986                         // 15, 16, 17, 18 is above
987                         19 => {
988                                 let mut list = loss_detector.handler.list_peers();
989                                 list.sort_by_key(|v| v.counterparty_node_id);
990                                 if let Some(peer_details) = list.get(0) {
991                                         loss_detector.handler.disconnect_by_node_id(peer_details.counterparty_node_id);
992                                 }
993                         },
994                         20 => loss_detector.handler.disconnect_all_peers(),
995                         21 => loss_detector.handler.timer_tick_occurred(),
996                         22 => loss_detector.handler.broadcast_node_announcement([42; 3], [43; 32], Vec::new()),
997                         32 => channelmanager.timer_tick_occurred(),
998                         33 => {
999                                 for id in intercepted_htlcs.drain(..) {
1000                                         channelmanager.fail_intercepted_htlc(id).unwrap();
1001                                 }
1002                         },
1003                         34 => {
1004                                 let amt = u64::from_be_bytes(get_bytes!(8));
1005                                 let chans = channelmanager.list_channels();
1006                                 for id in intercepted_htlcs.drain(..) {
1007                                         if chans.is_empty() {
1008                                                 channelmanager.fail_intercepted_htlc(id).unwrap();
1009                                         } else {
1010                                                 let chan = &chans[amt as usize % chans.len()];
1011                                                 channelmanager
1012                                                         .forward_intercepted_htlc(
1013                                                                 id,
1014                                                                 &chan.channel_id,
1015                                                                 chan.counterparty.node_id,
1016                                                                 amt,
1017                                                         )
1018                                                         .unwrap();
1019                                         }
1020                                 }
1021                         },
1022                         35 => {
1023                                 let config: ChannelConfig = if let Ok(c) = Readable::read(&mut &*input) {
1024                                         c
1025                                 } else {
1026                                         return;
1027                                 };
1028                                 let chans = channelmanager.list_channels();
1029                                 if let Some(chan) = chans.get(0) {
1030                                         let _ = channelmanager.update_channel_config(
1031                                                 &chan.counterparty.node_id,
1032                                                 &[chan.channel_id],
1033                                                 &config,
1034                                         );
1035                                 }
1036                         },
1037                         _ => return,
1038                 }
1039                 loss_detector.handler.process_events();
1040                 for event in loss_detector.manager.get_and_clear_pending_events() {
1041                         match event {
1042                                 Event::FundingGenerationReady {
1043                                         temporary_channel_id,
1044                                         counterparty_node_id,
1045                                         channel_value_satoshis,
1046                                         output_script,
1047                                         ..
1048                                 } => {
1049                                         pending_funding_generation.push((
1050                                                 temporary_channel_id,
1051                                                 counterparty_node_id,
1052                                                 channel_value_satoshis,
1053                                                 output_script,
1054                                         ));
1055                                 },
1056                                 Event::PaymentClaimable { payment_hash, .. } => {
1057                                         //TODO: enhance by fetching random amounts from fuzz input?
1058                                         payments_received.push(payment_hash);
1059                                 },
1060                                 Event::PendingHTLCsForwardable { .. } => {
1061                                         should_forward = true;
1062                                 },
1063                                 Event::HTLCIntercepted { intercept_id, .. } => {
1064                                         if !intercepted_htlcs.contains(&intercept_id) {
1065                                                 intercepted_htlcs.push(intercept_id);
1066                                         }
1067                                 },
1068                                 _ => {},
1069                         }
1070                 }
1071         }
1072 }
1073
1074 pub fn full_stack_test<Out: test_logger::Output>(data: &[u8], out: Out) {
1075         let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
1076         do_test(data, &logger);
1077 }
1078
1079 #[no_mangle]
1080 pub extern "C" fn full_stack_run(data: *const u8, datalen: usize) {
1081         let logger: Arc<dyn Logger> =
1082                 Arc::new(test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {}));
1083         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
1084 }
1085
1086 #[cfg(test)]
1087 mod tests {
1088         use bitcoin::hashes::hex::FromHex;
1089         use lightning::util::logger::{Logger, Record};
1090         use std::collections::HashMap;
1091         use std::sync::{Arc, Mutex};
1092
1093         struct TrackingLogger {
1094                 /// (module, message) -> count
1095                 pub lines: Mutex<HashMap<(String, String), usize>>,
1096         }
1097         impl Logger for TrackingLogger {
1098                 fn log(&self, record: Record) {
1099                         *self
1100                                 .lines
1101                                 .lock()
1102                                 .unwrap()
1103                                 .entry((record.module_path.to_string(), format!("{}", record.args)))
1104                                 .or_insert(0) += 1;
1105                         println!(
1106                                 "{:<5} [{} : {}, {}] {}",
1107                                 record.level.to_string(),
1108                                 record.module_path,
1109                                 record.file,
1110                                 record.line,
1111                                 record.args
1112                         );
1113                 }
1114         }
1115
1116         fn ext_from_hex(hex_with_spaces: &str, out: &mut Vec<u8>) {
1117                 for hex in hex_with_spaces.split(" ") {
1118                         out.append(&mut <Vec<u8>>::from_hex(hex).unwrap());
1119                 }
1120         }
1121
1122         #[test]
1123         fn test_no_existing_test_breakage() {
1124                 // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
1125                 // changes (such as requesting feerate info in a new place), we run a pretty full
1126                 // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky,
1127                 // so this should be updated pretty liberally, but at least we'll know when changes occur.
1128                 // If nothing else, this test serves as a pretty great initial full_stack_target seed.
1129
1130                 // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length +
1131                 // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
1132                 // of the Lightning message
1133                 // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
1134                 // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
1135
1136                 // Writing new code generating transactions and see a new failure ? Don't forget to add input for the FuzzEstimator !
1137
1138                 let mut test = Vec::new();
1139                 // our network key
1140                 ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
1141                 // config
1142                 ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
1143
1144                 // new outbound connection with id 0
1145                 ext_from_hex("00", &mut test);
1146                 // peer's pubkey
1147                 ext_from_hex(
1148                         "030000000000000000000000000000000000000000000000000000000000000002",
1149                         &mut test,
1150                 );
1151                 // inbound read from peer id 0 of len 50
1152                 ext_from_hex("030032", &mut test);
1153                 // noise act two (0||pubkey||mac)
1154                 ext_from_hex("00 030000000000000000000000000000000000000000000000000000000000000002 03000000000000000000000000000000", &mut test);
1155
1156                 // inbound read from peer id 0 of len 18
1157                 ext_from_hex("030012", &mut test);
1158                 // message header indicating message length 16
1159                 ext_from_hex("0010 03000000000000000000000000000000", &mut test);
1160                 // inbound read from peer id 0 of len 32
1161                 ext_from_hex("030020", &mut test);
1162                 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1163                 ext_from_hex(
1164                         "0010 00021aaa 0008aaa20aaa2a0a9aaa 03000000000000000000000000000000",
1165                         &mut test,
1166                 );
1167
1168                 // inbound read from peer id 0 of len 18
1169                 ext_from_hex("030012", &mut test);
1170                 // message header indicating message length 327
1171                 ext_from_hex("0147 03000000000000000000000000000000", &mut test);
1172                 // inbound read from peer id 0 of len 254
1173                 ext_from_hex("0300fe", &mut test);
1174                 // beginning of open_channel message
1175                 ext_from_hex("0020 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 000000000000c350 0000000000000000 0000000000000162 ffffffffffffffff 0000000000000222 0000000000000000 000000fd 0006 01e3 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 030000000000000000000000000000000000000000000000000000000000000004", &mut test);
1176                 // inbound read from peer id 0 of len 89
1177                 ext_from_hex("030059", &mut test);
1178                 // rest of open_channel and mac
1179                 ext_from_hex("030000000000000000000000000000000000000000000000000000000000000005 020900000000000000000000000000000000000000000000000000000000000000 01 0000 01021000 03000000000000000000000000000000", &mut test);
1180
1181                 // One feerate request returning min feerate, which our open_channel also uses (ingested by FuzzEstimator)
1182                 ext_from_hex("00fd", &mut test);
1183                 // client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
1184
1185                 // inbound read from peer id 0 of len 18
1186                 ext_from_hex("030012", &mut test);
1187                 // message header indicating message length 132
1188                 ext_from_hex("0084 03000000000000000000000000000000", &mut test);
1189                 // inbound read from peer id 0 of len 148
1190                 ext_from_hex("030094", &mut test);
1191                 // funding_created and mac
1192                 ext_from_hex("0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 3d00000000000000000000000000000000000000000000000000000000000000 0000 00000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1193                 // client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000)
1194
1195                 // connect a block with one transaction of len 94
1196                 ext_from_hex("0c005e", &mut test);
1197                 // the funding transaction
1198                 ext_from_hex("020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000", &mut test);
1199                 // Two feerate requests during block connection
1200                 ext_from_hex("00fd00fd", &mut test);
1201                 // connect a block with no transactions, one per line
1202                 ext_from_hex("0c0000", &mut test);
1203                 // Two feerate requests during block connection
1204                 ext_from_hex("00fd00fd", &mut test);
1205                 ext_from_hex("0c0000", &mut test);
1206                 // Two feerate requests during block connection
1207                 ext_from_hex("00fd00fd", &mut test);
1208                 ext_from_hex("0c0000", &mut test);
1209                 // Two feerate requests during block connection
1210                 ext_from_hex("00fd00fd", &mut test);
1211                 ext_from_hex("0c0000", &mut test);
1212                 // Two feerate requests during block connection
1213                 ext_from_hex("00fd00fd", &mut test);
1214                 ext_from_hex("0c0000", &mut test);
1215                 // Two feerate requests during block connection
1216                 ext_from_hex("00fd00fd", &mut test);
1217                 ext_from_hex("0c0000", &mut test);
1218                 // Two feerate requests during block connection
1219                 ext_from_hex("00fd00fd", &mut test);
1220                 ext_from_hex("0c0000", &mut test);
1221                 // Two feerate requests during block connection
1222                 ext_from_hex("00fd00fd", &mut test);
1223                 ext_from_hex("0c0000", &mut test);
1224                 // Two feerate requests during block connection
1225                 ext_from_hex("00fd00fd", &mut test);
1226                 ext_from_hex("0c0000", &mut test);
1227                 // Two feerate requests during block connection
1228                 ext_from_hex("00fd00fd", &mut test);
1229                 ext_from_hex("0c0000", &mut test);
1230                 // Two feerate requests during block connection
1231                 ext_from_hex("00fd00fd", &mut test);
1232                 ext_from_hex("0c0000", &mut test);
1233                 // Two feerate requests during block connection
1234                 ext_from_hex("00fd00fd", &mut test);
1235                 ext_from_hex("0c0000", &mut test);
1236                 // Two feerate requests during block connection
1237                 ext_from_hex("00fd00fd", &mut test);
1238                 // by now client should have sent a channel_ready (CHECK 3: SendChannelReady to 03000000 for chan 3d000000)
1239
1240                 // inbound read from peer id 0 of len 18
1241                 ext_from_hex("030012", &mut test);
1242                 // message header indicating message length 67
1243                 ext_from_hex("0043 03000000000000000000000000000000", &mut test);
1244                 // inbound read from peer id 0 of len 83
1245                 ext_from_hex("030053", &mut test);
1246                 // channel_ready and mac
1247                 ext_from_hex("0024 3d00000000000000000000000000000000000000000000000000000000000000 020800000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1248
1249                 // new inbound connection with id 1
1250                 ext_from_hex("01", &mut test);
1251                 // inbound read from peer id 1 of len 50
1252                 ext_from_hex("030132", &mut test);
1253                 // inbound noise act 1
1254                 ext_from_hex("0003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000", &mut test);
1255                 // inbound read from peer id 1 of len 66
1256                 ext_from_hex("030142", &mut test);
1257                 // inbound noise act 3
1258                 ext_from_hex("000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000", &mut test);
1259
1260                 // inbound read from peer id 1 of len 18
1261                 ext_from_hex("030112", &mut test);
1262                 // message header indicating message length 16
1263                 ext_from_hex("0010 01000000000000000000000000000000", &mut test);
1264                 // inbound read from peer id 1 of len 32
1265                 ext_from_hex("030120", &mut test);
1266                 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1267                 ext_from_hex(
1268                         "0010 00021aaa 0008aaa20aaa2a0a9aaa 01000000000000000000000000000000",
1269                         &mut test,
1270                 );
1271
1272                 // create outbound channel to peer 1 for 50k sat
1273                 ext_from_hex("05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8", &mut test);
1274                 // One feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
1275                 ext_from_hex("00fd", &mut test);
1276
1277                 // inbound read from peer id 1 of len 18
1278                 ext_from_hex("030112", &mut test);
1279                 // message header indicating message length 274
1280                 ext_from_hex("0112 01000000000000000000000000000000", &mut test);
1281                 // inbound read from peer id 1 of len 255
1282                 ext_from_hex("0301ff", &mut test);
1283                 // beginning of accept_channel
1284                 ext_from_hex("0021 0000000000000000000000000000000000000000000000000000000000000e05 0000000000000162 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 02660000000000000000000000000000", &mut test);
1285                 // inbound read from peer id 1 of len 35
1286                 ext_from_hex("030123", &mut test);
1287                 // rest of accept_channel and mac
1288                 ext_from_hex(
1289                         "0000000000000000000000000000000000 0000 01000000000000000000000000000000",
1290                         &mut test,
1291                 );
1292
1293                 // create the funding transaction (client should send funding_created now)
1294                 ext_from_hex("0a", &mut test);
1295                 // Two feerate requests to check the dust exposure on the initial commitment tx
1296                 ext_from_hex("00fd00fd", &mut test);
1297
1298                 // inbound read from peer id 1 of len 18
1299                 ext_from_hex("030112", &mut test);
1300                 // message header indicating message length 98
1301                 ext_from_hex("0062 01000000000000000000000000000000", &mut test);
1302                 // inbound read from peer id 1 of len 114
1303                 ext_from_hex("030172", &mut test);
1304                 // funding_signed message and mac
1305                 ext_from_hex("0023 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1306
1307                 // broadcast funding transaction
1308                 ext_from_hex("0b", &mut test);
1309                 // by now client should have sent a channel_ready (CHECK 4: SendChannelReady to 03020000 for chan 3f000000)
1310
1311                 // inbound read from peer id 1 of len 18
1312                 ext_from_hex("030112", &mut test);
1313                 // message header indicating message length 67
1314                 ext_from_hex("0043 01000000000000000000000000000000", &mut test);
1315                 // inbound read from peer id 1 of len 83
1316                 ext_from_hex("030153", &mut test);
1317                 // channel_ready and mac
1318                 ext_from_hex("0024 3a00000000000000000000000000000000000000000000000000000000000000 026700000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1319
1320                 // inbound read from peer id 0 of len 18
1321                 ext_from_hex("030012", &mut test);
1322                 // message header indicating message length 1452
1323                 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1324                 // inbound read from peer id 0 of len 255
1325                 ext_from_hex("0300ff", &mut test);
1326                 // beginning of update_add_htlc from 0 to 1 via client
1327                 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1328                 // inbound read from peer id 0 of len 255
1329                 ext_from_hex("0300ff", &mut test);
1330                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1331                 // inbound read from peer id 0 of len 255
1332                 ext_from_hex("0300ff", &mut test);
1333                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1334                 // inbound read from peer id 0 of len 255
1335                 ext_from_hex("0300ff", &mut test);
1336                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1337                 // inbound read from peer id 0 of len 255
1338                 ext_from_hex("0300ff", &mut test);
1339                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1340                 // inbound read from peer id 0 of len 193
1341                 ext_from_hex("0300c1", &mut test);
1342                 // end of update_add_htlc from 0 to 1 via client and mac
1343                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1344
1345                 // Two feerate requests to check dust exposure
1346                 ext_from_hex("00fd00fd", &mut test);
1347
1348                 // inbound read from peer id 0 of len 18
1349                 ext_from_hex("030012", &mut test);
1350                 // message header indicating message length 100
1351                 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1352                 // inbound read from peer id 0 of len 116
1353                 ext_from_hex("030074", &mut test);
1354                 // commitment_signed and mac
1355                 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000300100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1356                 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000)
1357
1358                 // inbound read from peer id 0 of len 18
1359                 ext_from_hex("030012", &mut test);
1360                 // message header indicating message length 99
1361                 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1362                 // inbound read from peer id 0 of len 115
1363                 ext_from_hex("030073", &mut test);
1364                 // revoke_and_ack and mac
1365                 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0900000000000000000000000000000000000000000000000000000000000000 020b00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1366
1367                 // process the now-pending HTLC forward
1368                 ext_from_hex("07", &mut test);
1369                 // Three feerate requests to check dust exposure
1370                 ext_from_hex("00fd00fd00fd", &mut test);
1371                 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: UpdateHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
1372
1373                 // we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
1374                 // inbound read from peer id 1 of len 18
1375                 ext_from_hex("030112", &mut test);
1376                 // message header indicating message length 100
1377                 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1378                 // inbound read from peer id 1 of len 116
1379                 ext_from_hex("030174", &mut test);
1380                 // commitment_signed and mac
1381                 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000006a0001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1382                 //
1383                 // inbound read from peer id 1 of len 18
1384                 ext_from_hex("030112", &mut test);
1385                 // message header indicating message length 99
1386                 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1387                 // inbound read from peer id 1 of len 115
1388                 ext_from_hex("030173", &mut test);
1389                 // revoke_and_ack and mac
1390                 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6600000000000000000000000000000000000000000000000000000000000000 026400000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1391                 //
1392                 // inbound read from peer id 1 of len 18
1393                 ext_from_hex("030112", &mut test);
1394                 // message header indicating message length 74
1395                 ext_from_hex("004a 01000000000000000000000000000000", &mut test);
1396                 // inbound read from peer id 1 of len 90
1397                 ext_from_hex("03015a", &mut test);
1398                 // update_fulfill_htlc and mac
1399                 ext_from_hex("0082 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000", &mut test);
1400                 // client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000)
1401
1402                 // inbound read from peer id 1 of len 18
1403                 ext_from_hex("030112", &mut test);
1404                 // message header indicating message length 100
1405                 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1406                 // inbound read from peer id 1 of len 116
1407                 ext_from_hex("030174", &mut test);
1408                 // commitment_signed and mac
1409                 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000100001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1410
1411                 // inbound read from peer id 1 of len 18
1412                 ext_from_hex("030112", &mut test);
1413                 // message header indicating message length 99
1414                 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1415                 // inbound read from peer id 1 of len 115
1416                 ext_from_hex("030173", &mut test);
1417                 // revoke_and_ack and mac
1418                 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6700000000000000000000000000000000000000000000000000000000000000 026500000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1419
1420                 // before responding to the commitment_signed generated above, send a new HTLC
1421                 // inbound read from peer id 0 of len 18
1422                 ext_from_hex("030012", &mut test);
1423                 // message header indicating message length 1452
1424                 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1425                 // inbound read from peer id 0 of len 255
1426                 ext_from_hex("0300ff", &mut test);
1427                 // beginning of update_add_htlc from 0 to 1 via client
1428                 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1429                 // inbound read from peer id 0 of len 255
1430                 ext_from_hex("0300ff", &mut test);
1431                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1432                 // inbound read from peer id 0 of len 255
1433                 ext_from_hex("0300ff", &mut test);
1434                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1435                 // inbound read from peer id 0 of len 255
1436                 ext_from_hex("0300ff", &mut test);
1437                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1438                 // inbound read from peer id 0 of len 255
1439                 ext_from_hex("0300ff", &mut test);
1440                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1441                 // inbound read from peer id 0 of len 193
1442                 ext_from_hex("0300c1", &mut test);
1443                 // end of update_add_htlc from 0 to 1 via client and mac
1444                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1445
1446                 // Two feerate requests to check dust exposure
1447                 ext_from_hex("00fd00fd", &mut test);
1448
1449                 // now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
1450                 // inbound read from peer id 0 of len 18
1451                 ext_from_hex("030012", &mut test);
1452                 // message header indicating message length 99
1453                 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1454                 // inbound read from peer id 0 of len 115
1455                 ext_from_hex("030073", &mut test);
1456                 // revoke_and_ack and mac
1457                 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0800000000000000000000000000000000000000000000000000000000000000 020a00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1458                 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
1459
1460                 // inbound read from peer id 0 of len 18
1461                 ext_from_hex("030012", &mut test);
1462                 // message header indicating message length 100
1463                 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1464                 // inbound read from peer id 0 of len 116
1465                 ext_from_hex("030074", &mut test);
1466                 // commitment_signed and mac
1467                 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000c30100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1468
1469                 // inbound read from peer id 0 of len 18
1470                 ext_from_hex("030012", &mut test);
1471                 // message header indicating message length 99
1472                 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1473                 // inbound read from peer id 0 of len 115
1474                 ext_from_hex("030073", &mut test);
1475                 // revoke_and_ack and mac
1476                 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0b00000000000000000000000000000000000000000000000000000000000000 020d00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1477
1478                 // process the now-pending HTLC forward
1479                 ext_from_hex("07", &mut test);
1480
1481                 // Three feerate requests to check dust exposure
1482                 ext_from_hex("00fd00fd00fd", &mut test);
1483
1484                 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
1485                 // we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
1486
1487                 // inbound read from peer id 1 of len 18
1488                 ext_from_hex("030112", &mut test);
1489                 // message header indicating message length 100
1490                 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1491                 // inbound read from peer id 1 of len 116
1492                 ext_from_hex("030174", &mut test);
1493                 // commitment_signed and mac
1494                 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1495
1496                 // inbound read from peer id 1 of len 18
1497                 ext_from_hex("030112", &mut test);
1498                 // message header indicating message length 99
1499                 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1500                 // inbound read from peer id 1 of len 115
1501                 ext_from_hex("030173", &mut test);
1502                 // revoke_and_ack and mac
1503                 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6400000000000000000000000000000000000000000000000000000000000000 027000000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1504
1505                 // inbound read from peer id 1 of len 18
1506                 ext_from_hex("030112", &mut test);
1507                 // message header indicating message length 44
1508                 ext_from_hex("002c 01000000000000000000000000000000", &mut test);
1509                 // inbound read from peer id 1 of len 60
1510                 ext_from_hex("03013c", &mut test);
1511                 // update_fail_htlc and mac
1512                 ext_from_hex("0083 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000", &mut test);
1513
1514                 // inbound read from peer id 1 of len 18
1515                 ext_from_hex("030112", &mut test);
1516                 // message header indicating message length 100
1517                 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1518                 // inbound read from peer id 1 of len 116
1519                 ext_from_hex("030174", &mut test);
1520                 // commitment_signed and mac
1521                 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1522
1523                 // inbound read from peer id 1 of len 18
1524                 ext_from_hex("030112", &mut test);
1525                 // message header indicating message length 99
1526                 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1527                 // inbound read from peer id 1 of len 115
1528                 ext_from_hex("030173", &mut test);
1529                 // revoke_and_ack and mac
1530                 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6500000000000000000000000000000000000000000000000000000000000000 027100000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1531
1532                 // process the now-pending HTLC forward
1533                 ext_from_hex("07", &mut test);
1534                 // client now sends id 0 update_fail_htlc and commitment_signed (CHECK 9)
1535                 // now respond to the update_fail_htlc+commitment_signed messages the client sent to peer 0
1536
1537                 // inbound read from peer id 0 of len 18
1538                 ext_from_hex("030012", &mut test);
1539                 // message header indicating message length 99
1540                 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1541                 // inbound read from peer id 0 of len 115
1542                 ext_from_hex("030073", &mut test);
1543                 // revoke_and_ack and mac
1544                 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0a00000000000000000000000000000000000000000000000000000000000000 020c00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1545
1546                 // inbound read from peer id 0 of len 18
1547                 ext_from_hex("030012", &mut test);
1548                 // message header indicating message length 100
1549                 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1550                 // inbound read from peer id 0 of len 116
1551                 ext_from_hex("030074", &mut test);
1552                 // commitment_signed and mac
1553                 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000320100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1554                 // client should now respond with revoke_and_ack (CHECK 5 duplicate)
1555
1556                 // inbound read from peer id 0 of len 18
1557                 ext_from_hex("030012", &mut test);
1558                 // message header indicating message length 1452
1559                 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1560                 // inbound read from peer id 0 of len 255
1561                 ext_from_hex("0300ff", &mut test);
1562                 // beginning of update_add_htlc from 0 to 1 via client
1563                 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000002 00000000000b0838 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 12 02030927c0 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1564                 // inbound read from peer id 0 of len 255
1565                 ext_from_hex("0300ff", &mut test);
1566                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1567                 // inbound read from peer id 0 of len 255
1568                 ext_from_hex("0300ff", &mut test);
1569                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1570                 // inbound read from peer id 0 of len 255
1571                 ext_from_hex("0300ff", &mut test);
1572                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1573                 // inbound read from peer id 0 of len 255
1574                 ext_from_hex("0300ff", &mut test);
1575                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1576                 // inbound read from peer id 0 of len 193
1577                 ext_from_hex("0300c1", &mut test);
1578                 // end of update_add_htlc from 0 to 1 via client and mac
1579                 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1580
1581                 // Two feerate requests to check dust exposure
1582                 ext_from_hex("00fd00fd", &mut test);
1583
1584                 // inbound read from peer id 0 of len 18
1585                 ext_from_hex("030012", &mut test);
1586                 // message header indicating message length 164
1587                 ext_from_hex("00a4 03000000000000000000000000000000", &mut test);
1588                 // inbound read from peer id 0 of len 180
1589                 ext_from_hex("0300b4", &mut test);
1590                 // commitment_signed and mac
1591                 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000750100000000000000000000000000000000000000000000000000000000000000 0001 00000000000000000000000000000000000000000000000000000000000000670500000000000000000000000000000000000000000000000000000000000006 03000000000000000000000000000000", &mut test);
1592                 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
1593
1594                 // inbound read from peer id 0 of len 18
1595                 ext_from_hex("030012", &mut test);
1596                 // message header indicating message length 99
1597                 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1598                 // inbound read from peer id 0 of len 115
1599                 ext_from_hex("030073", &mut test);
1600                 // revoke_and_ack and mac
1601                 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0d00000000000000000000000000000000000000000000000000000000000000 020f00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1602
1603                 // process the now-pending HTLC forward
1604                 ext_from_hex("07", &mut test);
1605                 // Three feerate requests to check dust exposure
1606                 ext_from_hex("00fd00fd00fd", &mut test);
1607                 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
1608
1609                 // connect a block with one transaction of len 125
1610                 ext_from_hex("0c007d", &mut test);
1611                 // the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
1612                 ext_from_hex("02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c0000000000000160014280000000000000000000000000000000000000005000020", &mut test);
1613                 // Two feerate requests during block connection
1614                 ext_from_hex("00fd00fd", &mut test);
1615                 //
1616                 // connect a block with one transaction of len 94
1617                 ext_from_hex("0c005e", &mut test);
1618                 // the HTLC timeout transaction
1619                 ext_from_hex("0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b20000000000000000000000000000000000000000000000000000000000000000000000", &mut test);
1620                 // Two feerate requests during block connection
1621                 ext_from_hex("00fd00fd", &mut test);
1622                 // connect a block with no transactions
1623                 ext_from_hex("0c0000", &mut test);
1624                 // Two feerate requests during block connection
1625                 ext_from_hex("00fd00fd", &mut test);
1626                 // connect a block with no transactions
1627                 ext_from_hex("0c0000", &mut test);
1628                 // Two feerate requests during block connection
1629                 ext_from_hex("00fd00fd", &mut test);
1630                 // connect a block with no transactions
1631                 ext_from_hex("0c0000", &mut test);
1632                 // Two feerate requests during block connection
1633                 ext_from_hex("00fd00fd", &mut test);
1634                 // connect a block with no transactions
1635                 ext_from_hex("0c0000", &mut test);
1636                 // Two feerate requests during block connection
1637                 ext_from_hex("00fd00fd", &mut test);
1638                 // connect a block with no transactions
1639                 ext_from_hex("0c0000", &mut test);
1640                 // Two feerate requests during block connection
1641                 ext_from_hex("00fd00fd", &mut test);
1642
1643                 // process the now-pending HTLC forward
1644                 ext_from_hex("07", &mut test);
1645                 // client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)
1646
1647                 let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
1648                 super::do_test(&test, &(Arc::clone(&logger) as Arc<dyn Logger>));
1649
1650                 let log_entries = logger.lines.lock().unwrap();
1651                 // 1
1652                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendAcceptChannel event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1));
1653                 // 2
1654                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingSigned event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1655                 // 3
1656                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendChannelReady event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1657                 // 4
1658                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendChannelReady event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3a00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1659                 // 5
1660                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendRevokeAndACK event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&4));
1661                 // 6
1662                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 0 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3));
1663                 // 7
1664                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3a00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3));
1665                 // 8
1666                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1667                 // 9
1668                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2));
1669                 // 10
1670                 assert_eq!(log_entries.get(&("lightning::chain::channelmonitor".to_string(), "Input spending counterparty commitment tx (0000000000000000000000000000000000000000000000000000000000000073:0) in 0000000000000000000000000000000000000000000000000000000000000067 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1));
1671         }
1672
1673         #[test]
1674         fn test_gossip_exchange_breakage() {
1675                 // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
1676                 // changes (such as requesting feerate info in a new place), we exchange some gossip
1677                 // messages. Obviously this is pretty finicky, so this should be updated pretty liberally,
1678                 // but at least we'll know when changes occur.
1679                 // This test serves as a pretty good full_stack_target seed.
1680
1681                 // What each byte represents is broken down below, and then everything is concatenated into
1682                 // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
1683
1684                 // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length +
1685                 // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
1686                 // of the Lightning message
1687                 // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
1688                 // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
1689
1690                 // Writing new code generating transactions and see a new failure ? Don't forget to add input for the FuzzEstimator !
1691
1692                 let mut test = Vec::new();
1693
1694                 // our network key
1695                 ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
1696                 // config
1697                 ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
1698
1699                 // new outbound connection with id 0
1700                 ext_from_hex("00", &mut test);
1701                 // peer's pubkey
1702                 ext_from_hex(
1703                         "030000000000000000000000000000000000000000000000000000000000000002",
1704                         &mut test,
1705                 );
1706                 // inbound read from peer id 0 of len 50
1707                 ext_from_hex("030032", &mut test);
1708                 // noise act two (0||pubkey||mac)
1709                 ext_from_hex("00 030000000000000000000000000000000000000000000000000000000000000002 03000000000000000000000000000000", &mut test);
1710
1711                 // inbound read from peer id 0 of len 18
1712                 ext_from_hex("030012", &mut test);
1713                 // message header indicating message length 16
1714                 ext_from_hex("0010 03000000000000000000000000000000", &mut test);
1715                 // inbound read from peer id 0 of len 32
1716                 ext_from_hex("030020", &mut test);
1717                 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1718                 ext_from_hex(
1719                         "0010 00021aaa 0008aaa20aaa2a0a9aaa 03000000000000000000000000000000",
1720                         &mut test,
1721                 );
1722
1723                 // new inbound connection with id 1
1724                 ext_from_hex("01", &mut test);
1725                 // inbound read from peer id 1 of len 50
1726                 ext_from_hex("030132", &mut test);
1727                 // inbound noise act 1
1728                 ext_from_hex("0003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000", &mut test);
1729                 // inbound read from peer id 1 of len 66
1730                 ext_from_hex("030142", &mut test);
1731                 // inbound noise act 3
1732                 ext_from_hex("000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000", &mut test);
1733
1734                 // inbound read from peer id 1 of len 18
1735                 ext_from_hex("030112", &mut test);
1736                 // message header indicating message length 16
1737                 ext_from_hex("0010 01000000000000000000000000000000", &mut test);
1738                 // inbound read from peer id 1 of len 32
1739                 ext_from_hex("030120", &mut test);
1740                 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1741                 ext_from_hex(
1742                         "0010 00021aaa 0008aaa20aaa2a0a9aaa 01000000000000000000000000000000",
1743                         &mut test,
1744                 );
1745
1746                 // inbound read from peer id 0 of len 18
1747                 ext_from_hex("030012", &mut test);
1748                 // message header indicating message length 432
1749                 ext_from_hex("01b0 03000000000000000000000000000000", &mut test);
1750                 // inbound read from peer id 0 of len 255
1751                 ext_from_hex("0300ff", &mut test);
1752                 // First part of channel_announcement (type 256)
1753                 ext_from_hex("0100 00000000000000000000000000000000000000000000000000000000000000b20303030303030303030303030303030303030303030303030303030303030303 00000000000000000000000000000000000000000000000000000000000000b20202020202020202020202020202020202020202020202020202020202020202 00000000000000000000000000000000000000000000000000000000000000b20303030303030303030303030303030303030303030303030303030303030303 00000000000000000000000000000000000000000000000000000000000000b20202020202020202020202020202020202020202020202020202020202", &mut test);
1754                 // inbound read from peer id 0 of len 193
1755                 ext_from_hex("0300c1", &mut test);
1756                 // Last part of channel_announcement and mac
1757                 ext_from_hex("020202 00006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000000002a030303030303030303030303030303030303030303030303030303030303030303020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030303020202020202020202020202020202020202020202020202020202020202020202 03000000000000000000000000000000", &mut test);
1758
1759                 // inbound read from peer id 0 of len 18
1760                 ext_from_hex("030012", &mut test);
1761                 // message header indicating message length 138
1762                 ext_from_hex("008a 03000000000000000000000000000000", &mut test);
1763                 // inbound read from peer id 0 of len 154
1764                 ext_from_hex("03009a", &mut test);
1765                 // channel_update (type 258) and mac
1766                 ext_from_hex("0102 00000000000000000000000000000000000000000000000000000000000000a60303030303030303030303030303030303030303030303030303030303030303 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 000000000000002a0000002c01000028000000000000000000000000000000000000000005f5e100 03000000000000000000000000000000", &mut test);
1767
1768                 // inbound read from peer id 0 of len 18
1769                 ext_from_hex("030012", &mut test);
1770                 // message header indicating message length 142
1771                 ext_from_hex("008e 03000000000000000000000000000000", &mut test);
1772                 // inbound read from peer id 0 of len 158
1773                 ext_from_hex("03009e", &mut test);
1774                 // node_announcement (type 257) and mac
1775                 ext_from_hex("0101 00000000000000000000000000000000000000000000000000000000000000280303030303030303030303030303030303030303030303030303030303030303 00000000002b03030303030303030303030303030303030303030303030303030303030303030300000000000000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1776
1777                 let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
1778                 super::do_test(&test, &(Arc::clone(&logger) as Arc<dyn Logger>));
1779
1780                 let log_entries = logger.lines.lock().unwrap();
1781                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)) or the announced channel's counterparties: ChannelAnnouncement { node_signature_1: 3026020200b202200303030303030303030303030303030303030303030303030303030303030303, node_signature_2: 3026020200b202200202020202020202020202020202020202020202020202020202020202020202, bitcoin_signature_1: 3026020200b202200303030303030303030303030303030303030303030303030303030303030303, bitcoin_signature_2: 3026020200b202200202020202020202020202020202020202020202020202020202020202020202, contents: UnsignedChannelAnnouncement { features: [], chain_hash: 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000, short_channel_id: 42, node_id_1: NodeId(030303030303030303030303030303030303030303030303030303030303030303), node_id_2: NodeId(020202020202020202020202020202020202020202020202020202020202020202), bitcoin_key_1: NodeId(030303030303030303030303030303030303030303030303030303030303030303), bitcoin_key_2: NodeId(020202020202020202020202020202020202020202020202020202020202020202), excess_data: [] } }".to_string())), Some(&1));
1782                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)): ChannelUpdate { signature: 3026020200a602200303030303030303030303030303030303030303030303030303030303030303, contents: UnsignedChannelUpdate { chain_hash: 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000, short_channel_id: 42, timestamp: 44, flags: 0, cltv_expiry_delta: 40, htlc_minimum_msat: 0, htlc_maximum_msat: 100000000, fee_base_msat: 0, fee_proportional_millionths: 0, excess_data: [] } }".to_string())), Some(&1));
1783                 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)) or the announced node: NodeAnnouncement { signature: 302502012802200303030303030303030303030303030303030303030303030303030303030303, contents: UnsignedNodeAnnouncement { features: [], timestamp: 43, node_id: NodeId(030303030303030303030303030303030303030303030303030303030303030303), rgb: [0, 0, 0], alias: NodeAlias([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, 0, 0]), addresses: [], excess_address_data: [], excess_data: [] } }".to_string())), Some(&1));
1784         }
1785 }