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