1 // This file is Copyright its original authors, visible in version control
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
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.
16 use bitcoin::amount::Amount;
17 use bitcoin::blockdata::constants::genesis_block;
18 use bitcoin::blockdata::locktime::absolute::LockTime;
19 use bitcoin::blockdata::opcodes;
20 use bitcoin::blockdata::script::{Builder, ScriptBuf};
21 use bitcoin::blockdata::transaction::{Transaction, TxOut};
22 use bitcoin::consensus::encode::deserialize;
23 use bitcoin::network::Network;
24 use bitcoin::transaction::Version;
26 use bitcoin::hash_types::{BlockHash, Txid};
27 use bitcoin::hashes::hex::FromHex;
28 use bitcoin::hashes::sha256::Hash as Sha256;
29 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
30 use bitcoin::hashes::Hash as _;
31 use bitcoin::WPubkeyHash;
33 use lightning::blinded_path::payment::ReceiveTlvs;
34 use lightning::blinded_path::BlindedPath;
36 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
37 use lightning::chain::chainmonitor;
38 use lightning::chain::transaction::OutPoint;
39 use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
40 use lightning::events::Event;
41 use lightning::ln::channel_state::ChannelDetails;
42 use lightning::ln::channelmanager::{
43 ChainParameters, ChannelManager, InterceptId, PaymentId, RecipientOnionFields, Retry,
45 use lightning::ln::functional_test_utils::*;
46 use lightning::ln::msgs::{self, DecodeError};
47 use lightning::ln::peer_handler::{
48 IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
50 use lightning::ln::script::ShutdownScript;
51 use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
52 use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
53 use lightning::offers::invoice_request::UnsignedInvoiceRequest;
54 use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
55 use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
56 use lightning::routing::router::{
57 InFlightHtlcs, PaymentParameters, Route, RouteParameters, Router,
59 use lightning::routing::utxo::UtxoLookup;
60 use lightning::sign::{
61 EntropySource, InMemorySigner, KeyMaterial, NodeSigner, Recipient, SignerProvider,
63 use lightning::util::config::{ChannelConfig, UserConfig};
64 use lightning::util::errors::APIError;
65 use lightning::util::hash_tables::*;
66 use lightning::util::logger::Logger;
67 use lightning::util::ser::{Readable, ReadableArgs, Writeable};
68 use lightning::util::test_channel_signer::{EnforcementState, TestChannelSigner};
70 use crate::utils::test_logger;
71 use crate::utils::test_persister::TestPersister;
73 use bitcoin::secp256k1::ecdh::SharedSecret;
74 use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
75 use bitcoin::secp256k1::schnorr;
76 use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey};
79 use std::cell::RefCell;
81 use std::convert::TryInto;
82 use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
83 use std::sync::{Arc, Mutex};
87 pub fn slice_to_be16(v: &[u8]) -> u16 {
88 ((v[0] as u16) << 8*1) |
89 ((v[1] as u16) << 8*0)
93 pub fn be16_to_array(u: u16) -> [u8; 2] {
95 v[0] = ((u >> 8 * 1) & 0xff) as u8;
96 v[1] = ((u >> 8 * 0) & 0xff) as u8;
102 pub fn slice_to_be24(v: &[u8]) -> u32 {
103 ((v[0] as u32) << 8*2) |
104 ((v[1] as u32) << 8*1) |
105 ((v[2] as u32) << 8*0)
110 read_pos: AtomicUsize,
111 halt_fee_est_reads: AtomicBool,
114 fn get_slice(&self, len: usize) -> Option<&[u8]> {
115 let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
116 if self.data.len() < old_pos + len {
119 Some(&self.data[old_pos..old_pos + len])
122 impl std::io::Read for &InputData {
123 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
124 if let Some(sl) = self.get_slice(buf.len()) {
125 buf.copy_from_slice(sl);
133 struct FuzzEstimator {
134 input: Arc<InputData>,
136 impl FeeEstimator for FuzzEstimator {
137 fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 {
138 if self.input.halt_fee_est_reads.load(Ordering::Acquire) {
141 //TODO: We should actually be testing at least much more than 64k...
142 match self.input.get_slice(2) {
143 Some(slice) => cmp::max(slice_to_be16(slice) as u32, 253),
151 impl Router for FuzzRouter {
153 &self, _payer: &PublicKey, _params: &RouteParameters,
154 _first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs,
155 ) -> Result<Route, msgs::LightningError> {
156 Err(msgs::LightningError {
157 err: String::from("Not implemented"),
158 action: msgs::ErrorAction::IgnoreError,
162 fn create_blinded_payment_paths<T: secp256k1::Signing + secp256k1::Verification>(
163 &self, _recipient: PublicKey, _first_hops: Vec<ChannelDetails>, _tlvs: ReceiveTlvs,
164 _amount_msats: u64, _secp_ctx: &Secp256k1<T>,
165 ) -> Result<Vec<(BlindedPayInfo, BlindedPath)>, ()> {
170 impl MessageRouter for FuzzRouter {
172 &self, _sender: PublicKey, _peers: Vec<PublicKey>, _destination: Destination,
173 ) -> Result<OnionMessagePath, ()> {
177 fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
178 &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
179 ) -> Result<Vec<BlindedPath>, ()> {
184 struct TestBroadcaster {
185 txn_broadcasted: Mutex<Vec<Transaction>>,
187 impl BroadcasterInterface for TestBroadcaster {
188 fn broadcast_transactions(&self, txs: &[&Transaction]) {
189 let owned_txs: Vec<Transaction> = txs.iter().map(|tx| (*tx).clone()).collect();
190 self.txn_broadcasted.lock().unwrap().extend(owned_txs);
197 peers_connected: &'a RefCell<[bool; 256]>,
199 impl<'a> SocketDescriptor for Peer<'a> {
200 fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
203 fn disconnect_socket(&mut self) {
204 assert!(self.peers_connected.borrow()[self.id as usize]);
205 self.peers_connected.borrow_mut()[self.id as usize] = false;
208 impl<'a> PartialEq for Peer<'a> {
209 fn eq(&self, other: &Self) -> bool {
213 impl<'a> Eq for Peer<'a> {}
214 impl<'a> std::hash::Hash for Peer<'a> {
215 fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
220 type ChannelMan<'a> = ChannelManager<
222 chainmonitor::ChainMonitor<
224 Arc<dyn chain::Filter>,
225 Arc<TestBroadcaster>,
231 Arc<TestBroadcaster>,
239 type PeerMan<'a> = PeerManager<
242 Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn UtxoLookup>, Arc<dyn Logger>>>,
243 IgnoringMessageHandler,
245 IgnoringMessageHandler,
249 struct MoneyLossDetector<'a> {
250 manager: Arc<ChannelMan<'a>>,
252 chainmonitor::ChainMonitor<
254 Arc<dyn chain::Filter>,
255 Arc<TestBroadcaster>,
261 handler: PeerMan<'a>,
263 peers: &'a RefCell<[bool; 256]>,
264 funding_txn: Vec<Transaction>,
265 txids_confirmed: HashMap<Txid, usize>,
266 header_hashes: Vec<(BlockHash, u32)>,
269 blocks_connected: u32,
270 error_message: String,
272 impl<'a> MoneyLossDetector<'a> {
274 peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelMan<'a>>,
276 chainmonitor::ChainMonitor<
278 Arc<dyn chain::Filter>,
279 Arc<TestBroadcaster>,
285 handler: PeerMan<'a>,
293 funding_txn: Vec::new(),
294 txids_confirmed: new_hash_map(),
295 header_hashes: vec![(genesis_block(Network::Bitcoin).block_hash(), 0)],
299 error_message: "Channel force-closed".to_string(),
303 fn connect_block(&mut self, all_txn: &[Transaction]) {
304 let mut txdata = Vec::with_capacity(all_txn.len());
305 for (idx, tx) in all_txn.iter().enumerate() {
306 let txid = tx.txid();
307 self.txids_confirmed.entry(txid).or_insert_with(|| {
308 txdata.push((idx + 1, tx));
313 self.blocks_connected += 1;
314 let header = create_dummy_header(self.header_hashes[self.height].0, self.blocks_connected);
316 self.manager.transactions_confirmed(&header, &txdata, self.height as u32);
317 self.manager.best_block_updated(&header, self.height as u32);
318 (*self.monitor).transactions_confirmed(&header, &txdata, self.height as u32);
319 (*self.monitor).best_block_updated(&header, self.height as u32);
320 if self.header_hashes.len() > self.height {
321 self.header_hashes[self.height] = (header.block_hash(), self.blocks_connected);
323 assert_eq!(self.header_hashes.len(), self.height);
324 self.header_hashes.push((header.block_hash(), self.blocks_connected));
326 self.max_height = cmp::max(self.height, self.max_height);
329 fn disconnect_block(&mut self) {
330 if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
331 let header = create_dummy_header(
332 self.header_hashes[self.height - 1].0,
333 self.header_hashes[self.height].1,
335 self.manager.block_disconnected(&header, self.height as u32);
336 self.monitor.block_disconnected(&header, self.height as u32);
338 let removal_height = self.height;
339 self.txids_confirmed.retain(|_, height| removal_height != *height);
344 impl<'a> Drop for MoneyLossDetector<'a> {
346 if !::std::thread::panicking() {
347 // Disconnect all peers
348 for (idx, peer) in self.peers.borrow().iter().enumerate() {
351 .socket_disconnected(&Peer { id: idx as u8, peers_connected: &self.peers });
355 // Force all channels onto the chain (and time out claim txn)
357 .force_close_all_channels_broadcasting_latest_txn(self.error_message.to_string());
363 node_secret: SecretKey,
364 inbound_payment_key: KeyMaterial,
366 signer_state: RefCell<HashMap<u8, (bool, Arc<Mutex<EnforcementState>>)>>,
369 impl EntropySource for KeyProvider {
370 fn get_secure_random_bytes(&self) -> [u8; 32] {
371 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
373 let random_bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
374 (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8,
375 (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8];
380 impl NodeSigner for KeyProvider {
381 fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
382 let node_secret = match recipient {
383 Recipient::Node => Ok(&self.node_secret),
384 Recipient::PhantomNode => Err(()),
386 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
390 &self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>,
391 ) -> Result<SharedSecret, ()> {
392 let mut node_secret = match recipient {
393 Recipient::Node => Ok(self.node_secret.clone()),
394 Recipient::PhantomNode => Err(()),
396 if let Some(tweak) = tweak {
397 node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
399 Ok(SharedSecret::new(other_key, &node_secret))
402 fn get_inbound_payment_key_material(&self) -> KeyMaterial {
403 self.inbound_payment_key.clone()
407 &self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
408 ) -> Result<RecoverableSignature, ()> {
412 fn sign_bolt12_invoice_request(
413 &self, _invoice_request: &UnsignedInvoiceRequest,
414 ) -> Result<schnorr::Signature, ()> {
418 fn sign_bolt12_invoice(
419 &self, _invoice: &UnsignedBolt12Invoice,
420 ) -> Result<schnorr::Signature, ()> {
424 fn sign_gossip_message(
425 &self, msg: lightning::ln::msgs::UnsignedGossipMessage,
426 ) -> Result<Signature, ()> {
427 let msg_hash = Message::from_digest(Sha256dHash::hash(&msg.encode()[..]).to_byte_array());
428 let secp_ctx = Secp256k1::signing_only();
429 Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
433 impl SignerProvider for KeyProvider {
434 type EcdsaSigner = TestChannelSigner;
436 type TaprootSigner = TestChannelSigner;
438 fn generate_channel_keys_id(
439 &self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128,
441 let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
444 .insert(ctr, (inbound, Arc::new(Mutex::new(EnforcementState::new()))));
448 fn derive_channel_signer(
449 &self, channel_value_satoshis: u64, channel_keys_id: [u8; 32],
450 ) -> Self::EcdsaSigner {
451 let secp_ctx = Secp256k1::signing_only();
452 let ctr = channel_keys_id[0];
453 let (inbound, state) = self.signer_state.borrow().get(&ctr).unwrap().clone();
454 TestChannelSigner::new_with_revoked(
458 SecretKey::from_slice(&[
459 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
460 0, 0, 0, 0, 0, 1, ctr,
463 SecretKey::from_slice(&[
464 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
465 0, 0, 0, 0, 0, 2, ctr,
468 SecretKey::from_slice(&[
469 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
470 0, 0, 0, 0, 0, 3, ctr,
473 SecretKey::from_slice(&[
474 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
475 0, 0, 0, 0, 0, 4, ctr,
478 SecretKey::from_slice(&[
479 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
480 0, 0, 0, 0, 0, 5, ctr,
484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
485 0, 0, 0, 0, 0, 6, ctr,
487 channel_value_satoshis,
494 SecretKey::from_slice(&[
495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
496 0, 0, 0, 0, 0, 7, ctr,
499 SecretKey::from_slice(&[
500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
501 0, 0, 0, 0, 0, 8, ctr,
504 SecretKey::from_slice(&[
505 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
506 0, 0, 0, 0, 0, 9, ctr,
509 SecretKey::from_slice(&[
510 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
511 0, 0, 0, 0, 0, 10, ctr,
514 SecretKey::from_slice(&[
515 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
516 0, 0, 0, 0, 0, 11, ctr,
520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
521 0, 0, 0, 0, 0, 12, ctr,
523 channel_value_satoshis,
533 fn read_chan_signer(&self, mut data: &[u8]) -> Result<TestChannelSigner, DecodeError> {
534 let inner: InMemorySigner = ReadableArgs::read(&mut data, self)?;
535 let state = Arc::new(Mutex::new(EnforcementState::new()));
537 Ok(TestChannelSigner::new_with_revoked(inner, state, false))
540 fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {
541 let secp_ctx = Secp256k1::signing_only();
542 let channel_monitor_claim_key = SecretKey::from_slice(
543 &<Vec<u8>>::from_hex(
544 "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
549 let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(
550 &PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize(),
553 .push_opcode(opcodes::all::OP_PUSHBYTES_0)
554 .push_slice(our_channel_monitor_claim_key_hash)
558 fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
559 let secp_ctx = Secp256k1::signing_only();
560 let secret_key = SecretKey::from_slice(&[
561 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
566 WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
567 Ok(ShutdownScript::new_p2wpkh(&pubkey_hash))
572 pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
577 let our_network_key = match SecretKey::from_slice(&data[..32]) {
583 let config: UserConfig = if let Ok(config) = Readable::read(&mut data) {
589 let input = Arc::new(InputData {
591 read_pos: AtomicUsize::new(0),
592 halt_fee_est_reads: AtomicBool::new(false),
594 let fee_est = Arc::new(FuzzEstimator { input: input.clone() });
595 let router = FuzzRouter {};
597 macro_rules! get_slice {
599 match input.get_slice($len as usize) {
600 Some(slice) => slice,
606 macro_rules! get_bytes {
608 let mut res = [0; $len];
609 match input.get_slice($len as usize) {
610 Some(slice) => res.copy_from_slice(slice),
617 macro_rules! get_pubkey {
619 match PublicKey::from_slice(get_slice!(33)) {
626 let inbound_payment_key = [
627 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
631 let broadcast = Arc::new(TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()) });
632 let monitor = Arc::new(chainmonitor::ChainMonitor::new(
637 Arc::new(TestPersister { update_ret: Mutex::new(ChannelMonitorUpdateStatus::Completed) }),
640 let keys_manager = Arc::new(KeyProvider {
641 node_secret: our_network_key.clone(),
642 inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()),
643 counter: AtomicU64::new(0),
644 signer_state: RefCell::new(new_hash_map()),
646 let network = Network::Bitcoin;
647 let best_block_timestamp = genesis_block(network).header.time;
648 let params = ChainParameters { network, best_block: BestBlock::from_network(network) };
649 let channelmanager = Arc::new(ChannelManager::new(
655 keys_manager.clone(),
656 keys_manager.clone(),
657 keys_manager.clone(),
660 best_block_timestamp,
662 // Adding new calls to `EntropySource::get_secure_random_bytes` during startup can change all the
663 // keys subsequently generated in this test. Rather than regenerating all the messages manually,
664 // it's easier to just increment the counter here so the keys don't change.
665 keys_manager.counter.fetch_sub(3, Ordering::AcqRel);
666 let network_graph = Arc::new(NetworkGraph::new(network, Arc::clone(&logger)));
668 Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
670 let peers = RefCell::new([false; 256]);
671 let message_handler = MessageHandler {
672 chan_handler: channelmanager.clone(),
673 route_handler: gossip_sync.clone(),
674 onion_message_handler: IgnoringMessageHandler {},
675 custom_message_handler: IgnoringMessageHandler {},
678 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
681 let peer_manager = PeerManager::new(
686 keys_manager.clone(),
688 let mut loss_detector =
689 MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), peer_manager);
691 let mut should_forward = false;
692 let mut payments_received: Vec<PaymentHash> = Vec::new();
693 let mut intercepted_htlcs: Vec<InterceptId> = Vec::new();
694 let mut payments_sent: u16 = 0;
695 let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, ScriptBuf)> = Vec::new();
696 let mut pending_funding_signatures = new_hash_map();
699 match get_slice!(1)[0] {
703 if !peers.borrow()[i - 1] {
711 let peer = Peer { id: (new_id - 1) as u8, peers_connected: &peers };
712 loss_detector.handler.new_outbound_connection(get_pubkey!(), peer, None).unwrap();
713 peers.borrow_mut()[new_id - 1] = true;
718 if !peers.borrow()[i - 1] {
726 let peer = Peer { id: (new_id - 1) as u8, peers_connected: &peers };
727 loss_detector.handler.new_inbound_connection(peer, None).unwrap();
728 peers.borrow_mut()[new_id - 1] = true;
731 let peer_id = get_slice!(1)[0];
732 if !peers.borrow()[peer_id as usize] {
735 let peer = Peer { id: peer_id, peers_connected: &peers };
736 loss_detector.handler.socket_disconnected(&peer);
737 peers.borrow_mut()[peer_id as usize] = false;
740 let peer_id = get_slice!(1)[0];
741 if !peers.borrow()[peer_id as usize] {
744 let mut peer = Peer { id: peer_id, peers_connected: &peers };
745 match loss_detector.handler.read_event(&mut peer, get_slice!(get_slice!(1)[0])) {
746 Ok(res) => assert!(!res),
748 peers.borrow_mut()[peer_id as usize] = false;
753 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
754 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
755 let params = RouteParameters::from_payment_params_and_value(
759 let mut payment_hash = PaymentHash([0; 32]);
760 payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
761 payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
763 let _ = channelmanager.send_payment(
765 RecipientOnionFields::spontaneous_empty(),
766 PaymentId(payment_hash.0),
772 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
773 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
774 let params = RouteParameters::from_payment_params_and_value(
778 let mut payment_hash = PaymentHash([0; 32]);
779 payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
780 payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
782 let mut payment_secret = PaymentSecret([0; 32]);
783 payment_secret.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
785 let _ = channelmanager.send_payment(
787 RecipientOnionFields::secret_only(payment_secret),
788 PaymentId(payment_hash.0),
794 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
795 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
796 let params = RouteParameters::from_payment_params_and_value(
800 let _ = channelmanager.send_preflight_probes(params, None);
803 let idx = u16::from_be_bytes(get_bytes!(2)) % cmp::max(payments_sent, 1);
804 let mut payment_id = PaymentId([0; 32]);
805 payment_id.0[0..2].copy_from_slice(&idx.to_be_bytes());
806 channelmanager.abandon_payment(payment_id);
809 let peer_id = get_slice!(1)[0];
810 if !peers.borrow()[peer_id as usize] {
813 let their_key = get_pubkey!();
814 let chan_value = slice_to_be24(get_slice!(3)) as u64;
815 let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
817 .create_channel(their_key, chan_value, push_msat_value, 0, None, None)
824 let mut channels = channelmanager.list_channels();
825 let channel_id = get_slice!(1)[0] as usize;
826 if channel_id >= channels.len() {
829 channels.sort_by(|a, b| a.channel_id.cmp(&b.channel_id));
832 &channels[channel_id].channel_id,
833 &channels[channel_id].counterparty.node_id,
842 channelmanager.process_pending_htlc_forwards();
843 should_forward = false;
847 for payment in payments_received.drain(..) {
848 // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
849 // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
850 // fulfill this HTLC, but if they are, we can just take the first byte and
851 // place that anywhere in our preimage.
852 if &payment.0[1..] != &[0; 31] {
853 channelmanager.fail_htlc_backwards(&payment);
855 let mut payment_preimage = PaymentPreimage([0; 32]);
856 payment_preimage.0[0] = payment.0[0];
857 channelmanager.claim_funds(payment_preimage);
862 let payment_preimage = PaymentPreimage(keys_manager.get_secure_random_bytes());
864 PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
865 // Note that this may fail - our hashes may collide and we'll end up trying to
866 // double-register the same payment_hash.
867 let _ = channelmanager.create_inbound_payment_for_hash(payment_hash, None, 1, None);
870 for payment in payments_received.drain(..) {
871 channelmanager.fail_htlc_backwards(&payment);
875 let mut tx = Transaction {
877 lock_time: LockTime::ZERO,
881 let mut channels = Vec::new();
882 for funding_generation in pending_funding_generation.drain(..) {
884 value: Amount::from_sat(funding_generation.2),
885 script_pubkey: funding_generation.3,
887 if !tx.output.contains(&txout) {
888 tx.output.push(txout);
889 channels.push((funding_generation.0, funding_generation.1));
892 // Once we switch to V2 channel opens we should be able to drop this entirely as
893 // channel_ids no longer change when we set the funding tx.
895 if tx.version.0 > 0xff {
898 let funding_txid = tx.txid();
899 if loss_detector.txids_confirmed.get(&funding_txid).is_none() {
900 let outpoint = OutPoint { txid: funding_txid, index: 0 };
901 for chan in channelmanager.list_channels() {
902 if chan.channel_id == ChannelId::v1_from_funding_outpoint(outpoint) {
903 tx.version = Version(tx.version.0 + 1);
904 continue 'search_loop;
909 tx.version = Version(tx.version.0 + 1);
911 if tx.version.0 <= 0xff && !channels.is_empty() {
912 let chans = channels.iter().map(|(a, b)| (a, b)).collect::<Vec<_>>();
914 channelmanager.batch_funding_transaction_generated(&chans, tx.clone())
916 // It's possible the channel has been closed in the mean time, but any other
917 // failure may be a bug.
918 if let APIError::ChannelUnavailable { .. } = e {
923 let funding_txid = tx.txid();
924 for idx in 0..tx.output.len() {
925 let outpoint = OutPoint { txid: funding_txid, index: idx as u16 };
926 pending_funding_signatures.insert(outpoint, tx.clone());
931 let mut txn = broadcast.txn_broadcasted.lock().unwrap().split_off(0);
933 input.halt_fee_est_reads.store(true, Ordering::Release);
934 loss_detector.connect_block(&txn[..]);
936 loss_detector.connect_block(&[]);
938 input.halt_fee_est_reads.store(false, Ordering::Release);
940 for tx in txn.drain(..) {
941 loss_detector.funding_txn.push(tx);
945 let txlen = u16::from_be_bytes(get_bytes!(2));
947 loss_detector.connect_block(&[]);
949 let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
950 if let Ok(tx) = txres {
951 let mut output_val = Amount::ZERO;
952 for out in tx.output.iter() {
953 if out.value > Amount::MAX_MONEY {
956 output_val += out.value;
957 if output_val > Amount::MAX_MONEY {
961 loss_detector.connect_block(&[tx]);
968 loss_detector.disconnect_block();
971 let mut channels = channelmanager.list_channels();
972 let channel_id = get_slice!(1)[0] as usize;
973 let error_message = "Channel force-closed";
974 if channel_id >= channels.len() {
977 channels.sort_by(|a, b| a.channel_id.cmp(&b.channel_id));
979 .force_close_broadcasting_latest_txn(
980 &channels[channel_id].channel_id,
981 &channels[channel_id].counterparty.node_id,
982 error_message.to_string(),
986 // 15, 16, 17, 18 is above
988 let mut list = loss_detector.handler.list_peers();
989 list.sort_by_key(|v| v.counterparty_node_id);
990 if let Some(peer_details) = list.get(0) {
991 loss_detector.handler.disconnect_by_node_id(peer_details.counterparty_node_id);
994 20 => loss_detector.handler.disconnect_all_peers(),
995 21 => loss_detector.handler.timer_tick_occurred(),
996 22 => loss_detector.handler.broadcast_node_announcement([42; 3], [43; 32], Vec::new()),
997 32 => channelmanager.timer_tick_occurred(),
999 for id in intercepted_htlcs.drain(..) {
1000 channelmanager.fail_intercepted_htlc(id).unwrap();
1004 let amt = u64::from_be_bytes(get_bytes!(8));
1005 let chans = channelmanager.list_channels();
1006 for id in intercepted_htlcs.drain(..) {
1007 if chans.is_empty() {
1008 channelmanager.fail_intercepted_htlc(id).unwrap();
1010 let chan = &chans[amt as usize % chans.len()];
1012 .forward_intercepted_htlc(
1015 chan.counterparty.node_id,
1023 let config: ChannelConfig = if let Ok(c) = Readable::read(&mut &*input) {
1028 let chans = channelmanager.list_channels();
1029 if let Some(chan) = chans.get(0) {
1030 let _ = channelmanager.update_channel_config(
1031 &chan.counterparty.node_id,
1039 loss_detector.handler.process_events();
1040 for event in loss_detector.manager.get_and_clear_pending_events() {
1042 Event::FundingGenerationReady {
1043 temporary_channel_id,
1044 counterparty_node_id,
1045 channel_value_satoshis,
1049 pending_funding_generation.push((
1050 temporary_channel_id,
1051 counterparty_node_id,
1052 channel_value_satoshis,
1056 Event::PaymentClaimable { payment_hash, .. } => {
1057 //TODO: enhance by fetching random amounts from fuzz input?
1058 payments_received.push(payment_hash);
1060 Event::PendingHTLCsForwardable { .. } => {
1061 should_forward = true;
1063 Event::HTLCIntercepted { intercept_id, .. } => {
1064 if !intercepted_htlcs.contains(&intercept_id) {
1065 intercepted_htlcs.push(intercept_id);
1074 pub fn full_stack_test<Out: test_logger::Output>(data: &[u8], out: Out) {
1075 let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
1076 do_test(data, &logger);
1080 pub extern "C" fn full_stack_run(data: *const u8, datalen: usize) {
1081 let logger: Arc<dyn Logger> =
1082 Arc::new(test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {}));
1083 do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
1088 use bitcoin::hashes::hex::FromHex;
1089 use lightning::util::logger::{Logger, Record};
1090 use std::collections::HashMap;
1091 use std::sync::{Arc, Mutex};
1093 struct TrackingLogger {
1094 /// (module, message) -> count
1095 pub lines: Mutex<HashMap<(String, String), usize>>,
1097 impl Logger for TrackingLogger {
1098 fn log(&self, record: Record) {
1103 .entry((record.module_path.to_string(), format!("{}", record.args)))
1106 "{:<5} [{} : {}, {}] {}",
1107 record.level.to_string(),
1116 fn ext_from_hex(hex_with_spaces: &str, out: &mut Vec<u8>) {
1117 for hex in hex_with_spaces.split(" ") {
1118 out.append(&mut <Vec<u8>>::from_hex(hex).unwrap());
1123 fn test_no_existing_test_breakage() {
1124 // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
1125 // changes (such as requesting feerate info in a new place), we run a pretty full
1126 // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky,
1127 // so this should be updated pretty liberally, but at least we'll know when changes occur.
1128 // If nothing else, this test serves as a pretty great initial full_stack_target seed.
1130 // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length +
1131 // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
1132 // of the Lightning message
1133 // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
1134 // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
1136 // Writing new code generating transactions and see a new failure ? Don't forget to add input for the FuzzEstimator !
1138 let mut test = Vec::new();
1140 ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
1142 ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
1144 // new outbound connection with id 0
1145 ext_from_hex("00", &mut test);
1148 "030000000000000000000000000000000000000000000000000000000000000002",
1151 // inbound read from peer id 0 of len 50
1152 ext_from_hex("030032", &mut test);
1153 // noise act two (0||pubkey||mac)
1154 ext_from_hex("00 030000000000000000000000000000000000000000000000000000000000000002 03000000000000000000000000000000", &mut test);
1156 // inbound read from peer id 0 of len 18
1157 ext_from_hex("030012", &mut test);
1158 // message header indicating message length 16
1159 ext_from_hex("0010 03000000000000000000000000000000", &mut test);
1160 // inbound read from peer id 0 of len 32
1161 ext_from_hex("030020", &mut test);
1162 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1164 "0010 00021aaa 0008aaa20aaa2a0a9aaa 03000000000000000000000000000000",
1168 // inbound read from peer id 0 of len 18
1169 ext_from_hex("030012", &mut test);
1170 // message header indicating message length 327
1171 ext_from_hex("0147 03000000000000000000000000000000", &mut test);
1172 // inbound read from peer id 0 of len 254
1173 ext_from_hex("0300fe", &mut test);
1174 // beginning of open_channel message
1175 ext_from_hex("0020 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 000000000000c350 0000000000000000 0000000000000162 ffffffffffffffff 0000000000000222 0000000000000000 000000fd 0006 01e3 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 030000000000000000000000000000000000000000000000000000000000000004", &mut test);
1176 // inbound read from peer id 0 of len 89
1177 ext_from_hex("030059", &mut test);
1178 // rest of open_channel and mac
1179 ext_from_hex("030000000000000000000000000000000000000000000000000000000000000005 020900000000000000000000000000000000000000000000000000000000000000 01 0000 01021000 03000000000000000000000000000000", &mut test);
1181 // One feerate request returning min feerate, which our open_channel also uses (ingested by FuzzEstimator)
1182 ext_from_hex("00fd", &mut test);
1183 // client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
1185 // inbound read from peer id 0 of len 18
1186 ext_from_hex("030012", &mut test);
1187 // message header indicating message length 132
1188 ext_from_hex("0084 03000000000000000000000000000000", &mut test);
1189 // inbound read from peer id 0 of len 148
1190 ext_from_hex("030094", &mut test);
1191 // funding_created and mac
1192 ext_from_hex("0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 3d00000000000000000000000000000000000000000000000000000000000000 0000 00000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1193 // client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000)
1195 // connect a block with one transaction of len 94
1196 ext_from_hex("0c005e", &mut test);
1197 // the funding transaction
1198 ext_from_hex("020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000", &mut test);
1199 // Two feerate requests during block connection
1200 ext_from_hex("00fd00fd", &mut test);
1201 // connect a block with no transactions, one per line
1202 ext_from_hex("0c0000", &mut test);
1203 // Two feerate requests during block connection
1204 ext_from_hex("00fd00fd", &mut test);
1205 ext_from_hex("0c0000", &mut test);
1206 // Two feerate requests during block connection
1207 ext_from_hex("00fd00fd", &mut test);
1208 ext_from_hex("0c0000", &mut test);
1209 // Two feerate requests during block connection
1210 ext_from_hex("00fd00fd", &mut test);
1211 ext_from_hex("0c0000", &mut test);
1212 // Two feerate requests during block connection
1213 ext_from_hex("00fd00fd", &mut test);
1214 ext_from_hex("0c0000", &mut test);
1215 // Two feerate requests during block connection
1216 ext_from_hex("00fd00fd", &mut test);
1217 ext_from_hex("0c0000", &mut test);
1218 // Two feerate requests during block connection
1219 ext_from_hex("00fd00fd", &mut test);
1220 ext_from_hex("0c0000", &mut test);
1221 // Two feerate requests during block connection
1222 ext_from_hex("00fd00fd", &mut test);
1223 ext_from_hex("0c0000", &mut test);
1224 // Two feerate requests during block connection
1225 ext_from_hex("00fd00fd", &mut test);
1226 ext_from_hex("0c0000", &mut test);
1227 // Two feerate requests during block connection
1228 ext_from_hex("00fd00fd", &mut test);
1229 ext_from_hex("0c0000", &mut test);
1230 // Two feerate requests during block connection
1231 ext_from_hex("00fd00fd", &mut test);
1232 ext_from_hex("0c0000", &mut test);
1233 // Two feerate requests during block connection
1234 ext_from_hex("00fd00fd", &mut test);
1235 ext_from_hex("0c0000", &mut test);
1236 // Two feerate requests during block connection
1237 ext_from_hex("00fd00fd", &mut test);
1238 // by now client should have sent a channel_ready (CHECK 3: SendChannelReady to 03000000 for chan 3d000000)
1240 // inbound read from peer id 0 of len 18
1241 ext_from_hex("030012", &mut test);
1242 // message header indicating message length 67
1243 ext_from_hex("0043 03000000000000000000000000000000", &mut test);
1244 // inbound read from peer id 0 of len 83
1245 ext_from_hex("030053", &mut test);
1246 // channel_ready and mac
1247 ext_from_hex("0024 3d00000000000000000000000000000000000000000000000000000000000000 020800000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1249 // new inbound connection with id 1
1250 ext_from_hex("01", &mut test);
1251 // inbound read from peer id 1 of len 50
1252 ext_from_hex("030132", &mut test);
1253 // inbound noise act 1
1254 ext_from_hex("0003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000", &mut test);
1255 // inbound read from peer id 1 of len 66
1256 ext_from_hex("030142", &mut test);
1257 // inbound noise act 3
1258 ext_from_hex("000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000", &mut test);
1260 // inbound read from peer id 1 of len 18
1261 ext_from_hex("030112", &mut test);
1262 // message header indicating message length 16
1263 ext_from_hex("0010 01000000000000000000000000000000", &mut test);
1264 // inbound read from peer id 1 of len 32
1265 ext_from_hex("030120", &mut test);
1266 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1268 "0010 00021aaa 0008aaa20aaa2a0a9aaa 01000000000000000000000000000000",
1272 // create outbound channel to peer 1 for 50k sat
1273 ext_from_hex("05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8", &mut test);
1274 // One feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
1275 ext_from_hex("00fd", &mut test);
1277 // inbound read from peer id 1 of len 18
1278 ext_from_hex("030112", &mut test);
1279 // message header indicating message length 274
1280 ext_from_hex("0112 01000000000000000000000000000000", &mut test);
1281 // inbound read from peer id 1 of len 255
1282 ext_from_hex("0301ff", &mut test);
1283 // beginning of accept_channel
1284 ext_from_hex("0021 0000000000000000000000000000000000000000000000000000000000000e05 0000000000000162 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 02660000000000000000000000000000", &mut test);
1285 // inbound read from peer id 1 of len 35
1286 ext_from_hex("030123", &mut test);
1287 // rest of accept_channel and mac
1289 "0000000000000000000000000000000000 0000 01000000000000000000000000000000",
1293 // create the funding transaction (client should send funding_created now)
1294 ext_from_hex("0a", &mut test);
1295 // Two feerate requests to check the dust exposure on the initial commitment tx
1296 ext_from_hex("00fd00fd", &mut test);
1298 // inbound read from peer id 1 of len 18
1299 ext_from_hex("030112", &mut test);
1300 // message header indicating message length 98
1301 ext_from_hex("0062 01000000000000000000000000000000", &mut test);
1302 // inbound read from peer id 1 of len 114
1303 ext_from_hex("030172", &mut test);
1304 // funding_signed message and mac
1305 ext_from_hex("0023 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1307 // broadcast funding transaction
1308 ext_from_hex("0b", &mut test);
1309 // by now client should have sent a channel_ready (CHECK 4: SendChannelReady to 03020000 for chan 3f000000)
1311 // inbound read from peer id 1 of len 18
1312 ext_from_hex("030112", &mut test);
1313 // message header indicating message length 67
1314 ext_from_hex("0043 01000000000000000000000000000000", &mut test);
1315 // inbound read from peer id 1 of len 83
1316 ext_from_hex("030153", &mut test);
1317 // channel_ready and mac
1318 ext_from_hex("0024 3a00000000000000000000000000000000000000000000000000000000000000 026700000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1320 // inbound read from peer id 0 of len 18
1321 ext_from_hex("030012", &mut test);
1322 // message header indicating message length 1452
1323 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1324 // inbound read from peer id 0 of len 255
1325 ext_from_hex("0300ff", &mut test);
1326 // beginning of update_add_htlc from 0 to 1 via client
1327 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1328 // inbound read from peer id 0 of len 255
1329 ext_from_hex("0300ff", &mut test);
1330 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1331 // inbound read from peer id 0 of len 255
1332 ext_from_hex("0300ff", &mut test);
1333 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1334 // inbound read from peer id 0 of len 255
1335 ext_from_hex("0300ff", &mut test);
1336 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1337 // inbound read from peer id 0 of len 255
1338 ext_from_hex("0300ff", &mut test);
1339 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1340 // inbound read from peer id 0 of len 193
1341 ext_from_hex("0300c1", &mut test);
1342 // end of update_add_htlc from 0 to 1 via client and mac
1343 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1345 // Two feerate requests to check dust exposure
1346 ext_from_hex("00fd00fd", &mut test);
1348 // inbound read from peer id 0 of len 18
1349 ext_from_hex("030012", &mut test);
1350 // message header indicating message length 100
1351 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1352 // inbound read from peer id 0 of len 116
1353 ext_from_hex("030074", &mut test);
1354 // commitment_signed and mac
1355 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000300100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1356 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000)
1358 // inbound read from peer id 0 of len 18
1359 ext_from_hex("030012", &mut test);
1360 // message header indicating message length 99
1361 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1362 // inbound read from peer id 0 of len 115
1363 ext_from_hex("030073", &mut test);
1364 // revoke_and_ack and mac
1365 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0900000000000000000000000000000000000000000000000000000000000000 020b00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1367 // process the now-pending HTLC forward
1368 ext_from_hex("07", &mut test);
1369 // Three feerate requests to check dust exposure
1370 ext_from_hex("00fd00fd00fd", &mut test);
1371 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: UpdateHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
1373 // we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
1374 // inbound read from peer id 1 of len 18
1375 ext_from_hex("030112", &mut test);
1376 // message header indicating message length 100
1377 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1378 // inbound read from peer id 1 of len 116
1379 ext_from_hex("030174", &mut test);
1380 // commitment_signed and mac
1381 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000006a0001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1383 // inbound read from peer id 1 of len 18
1384 ext_from_hex("030112", &mut test);
1385 // message header indicating message length 99
1386 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1387 // inbound read from peer id 1 of len 115
1388 ext_from_hex("030173", &mut test);
1389 // revoke_and_ack and mac
1390 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6600000000000000000000000000000000000000000000000000000000000000 026400000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1392 // inbound read from peer id 1 of len 18
1393 ext_from_hex("030112", &mut test);
1394 // message header indicating message length 74
1395 ext_from_hex("004a 01000000000000000000000000000000", &mut test);
1396 // inbound read from peer id 1 of len 90
1397 ext_from_hex("03015a", &mut test);
1398 // update_fulfill_htlc and mac
1399 ext_from_hex("0082 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000", &mut test);
1400 // client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000)
1402 // inbound read from peer id 1 of len 18
1403 ext_from_hex("030112", &mut test);
1404 // message header indicating message length 100
1405 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1406 // inbound read from peer id 1 of len 116
1407 ext_from_hex("030174", &mut test);
1408 // commitment_signed and mac
1409 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000100001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1411 // inbound read from peer id 1 of len 18
1412 ext_from_hex("030112", &mut test);
1413 // message header indicating message length 99
1414 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1415 // inbound read from peer id 1 of len 115
1416 ext_from_hex("030173", &mut test);
1417 // revoke_and_ack and mac
1418 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6700000000000000000000000000000000000000000000000000000000000000 026500000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1420 // before responding to the commitment_signed generated above, send a new HTLC
1421 // inbound read from peer id 0 of len 18
1422 ext_from_hex("030012", &mut test);
1423 // message header indicating message length 1452
1424 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1425 // inbound read from peer id 0 of len 255
1426 ext_from_hex("0300ff", &mut test);
1427 // beginning of update_add_htlc from 0 to 1 via client
1428 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1429 // inbound read from peer id 0 of len 255
1430 ext_from_hex("0300ff", &mut test);
1431 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1432 // inbound read from peer id 0 of len 255
1433 ext_from_hex("0300ff", &mut test);
1434 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1435 // inbound read from peer id 0 of len 255
1436 ext_from_hex("0300ff", &mut test);
1437 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1438 // inbound read from peer id 0 of len 255
1439 ext_from_hex("0300ff", &mut test);
1440 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1441 // inbound read from peer id 0 of len 193
1442 ext_from_hex("0300c1", &mut test);
1443 // end of update_add_htlc from 0 to 1 via client and mac
1444 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1446 // Two feerate requests to check dust exposure
1447 ext_from_hex("00fd00fd", &mut test);
1449 // now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
1450 // inbound read from peer id 0 of len 18
1451 ext_from_hex("030012", &mut test);
1452 // message header indicating message length 99
1453 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1454 // inbound read from peer id 0 of len 115
1455 ext_from_hex("030073", &mut test);
1456 // revoke_and_ack and mac
1457 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0800000000000000000000000000000000000000000000000000000000000000 020a00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1458 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
1460 // inbound read from peer id 0 of len 18
1461 ext_from_hex("030012", &mut test);
1462 // message header indicating message length 100
1463 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1464 // inbound read from peer id 0 of len 116
1465 ext_from_hex("030074", &mut test);
1466 // commitment_signed and mac
1467 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000c30100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1469 // inbound read from peer id 0 of len 18
1470 ext_from_hex("030012", &mut test);
1471 // message header indicating message length 99
1472 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1473 // inbound read from peer id 0 of len 115
1474 ext_from_hex("030073", &mut test);
1475 // revoke_and_ack and mac
1476 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0b00000000000000000000000000000000000000000000000000000000000000 020d00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1478 // process the now-pending HTLC forward
1479 ext_from_hex("07", &mut test);
1481 // Three feerate requests to check dust exposure
1482 ext_from_hex("00fd00fd00fd", &mut test);
1484 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
1485 // we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
1487 // inbound read from peer id 1 of len 18
1488 ext_from_hex("030112", &mut test);
1489 // message header indicating message length 100
1490 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1491 // inbound read from peer id 1 of len 116
1492 ext_from_hex("030174", &mut test);
1493 // commitment_signed and mac
1494 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1496 // inbound read from peer id 1 of len 18
1497 ext_from_hex("030112", &mut test);
1498 // message header indicating message length 99
1499 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1500 // inbound read from peer id 1 of len 115
1501 ext_from_hex("030173", &mut test);
1502 // revoke_and_ack and mac
1503 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6400000000000000000000000000000000000000000000000000000000000000 027000000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1505 // inbound read from peer id 1 of len 18
1506 ext_from_hex("030112", &mut test);
1507 // message header indicating message length 44
1508 ext_from_hex("002c 01000000000000000000000000000000", &mut test);
1509 // inbound read from peer id 1 of len 60
1510 ext_from_hex("03013c", &mut test);
1511 // update_fail_htlc and mac
1512 ext_from_hex("0083 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000", &mut test);
1514 // inbound read from peer id 1 of len 18
1515 ext_from_hex("030112", &mut test);
1516 // message header indicating message length 100
1517 ext_from_hex("0064 01000000000000000000000000000000", &mut test);
1518 // inbound read from peer id 1 of len 116
1519 ext_from_hex("030174", &mut test);
1520 // commitment_signed and mac
1521 ext_from_hex("0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000", &mut test);
1523 // inbound read from peer id 1 of len 18
1524 ext_from_hex("030112", &mut test);
1525 // message header indicating message length 99
1526 ext_from_hex("0063 01000000000000000000000000000000", &mut test);
1527 // inbound read from peer id 1 of len 115
1528 ext_from_hex("030173", &mut test);
1529 // revoke_and_ack and mac
1530 ext_from_hex("0085 3a00000000000000000000000000000000000000000000000000000000000000 6500000000000000000000000000000000000000000000000000000000000000 027100000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000", &mut test);
1532 // process the now-pending HTLC forward
1533 ext_from_hex("07", &mut test);
1534 // client now sends id 0 update_fail_htlc and commitment_signed (CHECK 9)
1535 // now respond to the update_fail_htlc+commitment_signed messages the client sent to peer 0
1537 // inbound read from peer id 0 of len 18
1538 ext_from_hex("030012", &mut test);
1539 // message header indicating message length 99
1540 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1541 // inbound read from peer id 0 of len 115
1542 ext_from_hex("030073", &mut test);
1543 // revoke_and_ack and mac
1544 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0a00000000000000000000000000000000000000000000000000000000000000 020c00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1546 // inbound read from peer id 0 of len 18
1547 ext_from_hex("030012", &mut test);
1548 // message header indicating message length 100
1549 ext_from_hex("0064 03000000000000000000000000000000", &mut test);
1550 // inbound read from peer id 0 of len 116
1551 ext_from_hex("030074", &mut test);
1552 // commitment_signed and mac
1553 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000320100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000", &mut test);
1554 // client should now respond with revoke_and_ack (CHECK 5 duplicate)
1556 // inbound read from peer id 0 of len 18
1557 ext_from_hex("030012", &mut test);
1558 // message header indicating message length 1452
1559 ext_from_hex("05ac 03000000000000000000000000000000", &mut test);
1560 // inbound read from peer id 0 of len 255
1561 ext_from_hex("0300ff", &mut test);
1562 // beginning of update_add_htlc from 0 to 1 via client
1563 ext_from_hex("0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000002 00000000000b0838 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 12 02030927c0 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1564 // inbound read from peer id 0 of len 255
1565 ext_from_hex("0300ff", &mut test);
1566 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1567 // inbound read from peer id 0 of len 255
1568 ext_from_hex("0300ff", &mut test);
1569 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1570 // inbound read from peer id 0 of len 255
1571 ext_from_hex("0300ff", &mut test);
1572 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1573 // inbound read from peer id 0 of len 255
1574 ext_from_hex("0300ff", &mut test);
1575 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &mut test);
1576 // inbound read from peer id 0 of len 193
1577 ext_from_hex("0300c1", &mut test);
1578 // end of update_add_htlc from 0 to 1 via client and mac
1579 ext_from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1581 // Two feerate requests to check dust exposure
1582 ext_from_hex("00fd00fd", &mut test);
1584 // inbound read from peer id 0 of len 18
1585 ext_from_hex("030012", &mut test);
1586 // message header indicating message length 164
1587 ext_from_hex("00a4 03000000000000000000000000000000", &mut test);
1588 // inbound read from peer id 0 of len 180
1589 ext_from_hex("0300b4", &mut test);
1590 // commitment_signed and mac
1591 ext_from_hex("0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000750100000000000000000000000000000000000000000000000000000000000000 0001 00000000000000000000000000000000000000000000000000000000000000670500000000000000000000000000000000000000000000000000000000000006 03000000000000000000000000000000", &mut test);
1592 // client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
1594 // inbound read from peer id 0 of len 18
1595 ext_from_hex("030012", &mut test);
1596 // message header indicating message length 99
1597 ext_from_hex("0063 03000000000000000000000000000000", &mut test);
1598 // inbound read from peer id 0 of len 115
1599 ext_from_hex("030073", &mut test);
1600 // revoke_and_ack and mac
1601 ext_from_hex("0085 3d00000000000000000000000000000000000000000000000000000000000000 0d00000000000000000000000000000000000000000000000000000000000000 020f00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1603 // process the now-pending HTLC forward
1604 ext_from_hex("07", &mut test);
1605 // Three feerate requests to check dust exposure
1606 ext_from_hex("00fd00fd00fd", &mut test);
1607 // client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
1609 // connect a block with one transaction of len 125
1610 ext_from_hex("0c007d", &mut test);
1611 // the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
1612 ext_from_hex("02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c0000000000000160014280000000000000000000000000000000000000005000020", &mut test);
1613 // Two feerate requests during block connection
1614 ext_from_hex("00fd00fd", &mut test);
1616 // connect a block with one transaction of len 94
1617 ext_from_hex("0c005e", &mut test);
1618 // the HTLC timeout transaction
1619 ext_from_hex("0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b20000000000000000000000000000000000000000000000000000000000000000000000", &mut test);
1620 // Two feerate requests during block connection
1621 ext_from_hex("00fd00fd", &mut test);
1622 // connect a block with no transactions
1623 ext_from_hex("0c0000", &mut test);
1624 // Two feerate requests during block connection
1625 ext_from_hex("00fd00fd", &mut test);
1626 // connect a block with no transactions
1627 ext_from_hex("0c0000", &mut test);
1628 // Two feerate requests during block connection
1629 ext_from_hex("00fd00fd", &mut test);
1630 // connect a block with no transactions
1631 ext_from_hex("0c0000", &mut test);
1632 // Two feerate requests during block connection
1633 ext_from_hex("00fd00fd", &mut test);
1634 // connect a block with no transactions
1635 ext_from_hex("0c0000", &mut test);
1636 // Two feerate requests during block connection
1637 ext_from_hex("00fd00fd", &mut test);
1638 // connect a block with no transactions
1639 ext_from_hex("0c0000", &mut test);
1640 // Two feerate requests during block connection
1641 ext_from_hex("00fd00fd", &mut test);
1643 // process the now-pending HTLC forward
1644 ext_from_hex("07", &mut test);
1645 // client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)
1647 let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
1648 super::do_test(&test, &(Arc::clone(&logger) as Arc<dyn Logger>));
1650 let log_entries = logger.lines.lock().unwrap();
1652 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendAcceptChannel event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1));
1654 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingSigned event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1656 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendChannelReady event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1658 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendChannelReady event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3a00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1660 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendRevokeAndACK event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&4));
1662 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 0 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3));
1664 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3a00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3));
1666 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1));
1668 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000002 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2));
1670 assert_eq!(log_entries.get(&("lightning::chain::channelmonitor".to_string(), "Input spending counterparty commitment tx (0000000000000000000000000000000000000000000000000000000000000073:0) in 0000000000000000000000000000000000000000000000000000000000000067 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1));
1674 fn test_gossip_exchange_breakage() {
1675 // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
1676 // changes (such as requesting feerate info in a new place), we exchange some gossip
1677 // messages. Obviously this is pretty finicky, so this should be updated pretty liberally,
1678 // but at least we'll know when changes occur.
1679 // This test serves as a pretty good full_stack_target seed.
1681 // What each byte represents is broken down below, and then everything is concatenated into
1682 // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
1684 // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length +
1685 // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
1686 // of the Lightning message
1687 // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
1688 // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
1690 // Writing new code generating transactions and see a new failure ? Don't forget to add input for the FuzzEstimator !
1692 let mut test = Vec::new();
1695 ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
1697 ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
1699 // new outbound connection with id 0
1700 ext_from_hex("00", &mut test);
1703 "030000000000000000000000000000000000000000000000000000000000000002",
1706 // inbound read from peer id 0 of len 50
1707 ext_from_hex("030032", &mut test);
1708 // noise act two (0||pubkey||mac)
1709 ext_from_hex("00 030000000000000000000000000000000000000000000000000000000000000002 03000000000000000000000000000000", &mut test);
1711 // inbound read from peer id 0 of len 18
1712 ext_from_hex("030012", &mut test);
1713 // message header indicating message length 16
1714 ext_from_hex("0010 03000000000000000000000000000000", &mut test);
1715 // inbound read from peer id 0 of len 32
1716 ext_from_hex("030020", &mut test);
1717 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1719 "0010 00021aaa 0008aaa20aaa2a0a9aaa 03000000000000000000000000000000",
1723 // new inbound connection with id 1
1724 ext_from_hex("01", &mut test);
1725 // inbound read from peer id 1 of len 50
1726 ext_from_hex("030132", &mut test);
1727 // inbound noise act 1
1728 ext_from_hex("0003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000", &mut test);
1729 // inbound read from peer id 1 of len 66
1730 ext_from_hex("030142", &mut test);
1731 // inbound noise act 3
1732 ext_from_hex("000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000", &mut test);
1734 // inbound read from peer id 1 of len 18
1735 ext_from_hex("030112", &mut test);
1736 // message header indicating message length 16
1737 ext_from_hex("0010 01000000000000000000000000000000", &mut test);
1738 // inbound read from peer id 1 of len 32
1739 ext_from_hex("030120", &mut test);
1740 // init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1742 "0010 00021aaa 0008aaa20aaa2a0a9aaa 01000000000000000000000000000000",
1746 // inbound read from peer id 0 of len 18
1747 ext_from_hex("030012", &mut test);
1748 // message header indicating message length 432
1749 ext_from_hex("01b0 03000000000000000000000000000000", &mut test);
1750 // inbound read from peer id 0 of len 255
1751 ext_from_hex("0300ff", &mut test);
1752 // First part of channel_announcement (type 256)
1753 ext_from_hex("0100 00000000000000000000000000000000000000000000000000000000000000b20303030303030303030303030303030303030303030303030303030303030303 00000000000000000000000000000000000000000000000000000000000000b20202020202020202020202020202020202020202020202020202020202020202 00000000000000000000000000000000000000000000000000000000000000b20303030303030303030303030303030303030303030303030303030303030303 00000000000000000000000000000000000000000000000000000000000000b20202020202020202020202020202020202020202020202020202020202", &mut test);
1754 // inbound read from peer id 0 of len 193
1755 ext_from_hex("0300c1", &mut test);
1756 // Last part of channel_announcement and mac
1757 ext_from_hex("020202 00006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000000000000000002a030303030303030303030303030303030303030303030303030303030303030303020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030303020202020202020202020202020202020202020202020202020202020202020202 03000000000000000000000000000000", &mut test);
1759 // inbound read from peer id 0 of len 18
1760 ext_from_hex("030012", &mut test);
1761 // message header indicating message length 138
1762 ext_from_hex("008a 03000000000000000000000000000000", &mut test);
1763 // inbound read from peer id 0 of len 154
1764 ext_from_hex("03009a", &mut test);
1765 // channel_update (type 258) and mac
1766 ext_from_hex("0102 00000000000000000000000000000000000000000000000000000000000000a60303030303030303030303030303030303030303030303030303030303030303 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 000000000000002a0000002c01000028000000000000000000000000000000000000000005f5e100 03000000000000000000000000000000", &mut test);
1768 // inbound read from peer id 0 of len 18
1769 ext_from_hex("030012", &mut test);
1770 // message header indicating message length 142
1771 ext_from_hex("008e 03000000000000000000000000000000", &mut test);
1772 // inbound read from peer id 0 of len 158
1773 ext_from_hex("03009e", &mut test);
1774 // node_announcement (type 257) and mac
1775 ext_from_hex("0101 00000000000000000000000000000000000000000000000000000000000000280303030303030303030303030303030303030303030303030303030303030303 00000000002b03030303030303030303030303030303030303030303030303030303030303030300000000000000000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000", &mut test);
1777 let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
1778 super::do_test(&test, &(Arc::clone(&logger) as Arc<dyn Logger>));
1780 let log_entries = logger.lines.lock().unwrap();
1781 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)) or the announced channel's counterparties: ChannelAnnouncement { node_signature_1: 3026020200b202200303030303030303030303030303030303030303030303030303030303030303, node_signature_2: 3026020200b202200202020202020202020202020202020202020202020202020202020202020202, bitcoin_signature_1: 3026020200b202200303030303030303030303030303030303030303030303030303030303030303, bitcoin_signature_2: 3026020200b202200202020202020202020202020202020202020202020202020202020202020202, contents: UnsignedChannelAnnouncement { features: [], chain_hash: 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000, short_channel_id: 42, node_id_1: NodeId(030303030303030303030303030303030303030303030303030303030303030303), node_id_2: NodeId(020202020202020202020202020202020202020202020202020202020202020202), bitcoin_key_1: NodeId(030303030303030303030303030303030303030303030303030303030303030303), bitcoin_key_2: NodeId(020202020202020202020202020202020202020202020202020202020202020202), excess_data: [] } }".to_string())), Some(&1));
1782 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)): ChannelUpdate { signature: 3026020200a602200303030303030303030303030303030303030303030303030303030303030303, contents: UnsignedChannelUpdate { chain_hash: 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000, short_channel_id: 42, timestamp: 44, flags: 0, cltv_expiry_delta: 40, htlc_minimum_msat: 0, htlc_maximum_msat: 100000000, fee_base_msat: 0, fee_proportional_millionths: 0, excess_data: [] } }".to_string())), Some(&1));
1783 assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Sending message to all peers except Some(PublicKey(0000000000000000000000000000000000000000000000000000000000000002ff00000000000000000000000000000000000000000000000000000000000002)) or the announced node: NodeAnnouncement { signature: 302502012802200303030303030303030303030303030303030303030303030303030303030303, contents: UnsignedNodeAnnouncement { features: [], timestamp: 43, node_id: NodeId(030303030303030303030303030303030303030303030303030303030303030303), rgb: [0, 0, 0], alias: NodeAlias([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), addresses: [], excess_address_data: [], excess_data: [] } }".to_string())), Some(&1));