Move the final CLTV delta to `PaymentParameters` from `RouteParams`
[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::TxMerkleNode;
17 use bitcoin::blockdata::block::BlockHeader;
18 use bitcoin::blockdata::constants::genesis_block;
19 use bitcoin::blockdata::transaction::{Transaction, TxOut};
20 use bitcoin::blockdata::script::{Builder, Script};
21 use bitcoin::blockdata::opcodes;
22 use bitcoin::blockdata::locktime::PackedLockTime;
23 use bitcoin::consensus::encode::deserialize;
24 use bitcoin::network::constants::Network;
25
26 use bitcoin::hashes::Hash as TraitImport;
27 use bitcoin::hashes::HashEngine as TraitImportEngine;
28 use bitcoin::hashes::sha256::Hash as Sha256;
29 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
30 use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
31
32 use lightning::chain;
33 use lightning::chain::{BestBlock, ChannelMonitorUpdateStatus, Confirm, Listen};
34 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
35 use lightning::chain::chainmonitor;
36 use lightning::chain::transaction::OutPoint;
37 use lightning::chain::keysinterface::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
38 use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
39 use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId};
40 use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
41 use lightning::ln::msgs::{self, DecodeError};
42 use lightning::ln::script::ShutdownScript;
43 use lightning::routing::gossip::{P2PGossipSync, NetworkGraph};
44 use lightning::routing::router::{find_route, InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, Router};
45 use lightning::routing::scoring::FixedPenaltyScorer;
46 use lightning::util::config::UserConfig;
47 use lightning::util::errors::APIError;
48 use lightning::util::events::Event;
49 use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
50 use lightning::util::logger::Logger;
51 use lightning::util::ser::{Readable, Writeable};
52
53 use crate::utils::test_logger;
54 use crate::utils::test_persister::TestPersister;
55
56 use bitcoin::secp256k1::{Message, PublicKey, SecretKey, Scalar, Secp256k1};
57 use bitcoin::secp256k1::ecdh::SharedSecret;
58 use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
59
60 use std::cell::RefCell;
61 use hashbrown::{HashMap, hash_map};
62 use std::convert::TryInto;
63 use std::cmp;
64 use std::sync::{Arc, Mutex};
65 use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
66 use bitcoin::bech32::u5;
67
68 #[inline]
69 pub fn slice_to_be16(v: &[u8]) -> u16 {
70         ((v[0] as u16) << 8*1) |
71         ((v[1] as u16) << 8*0)
72 }
73
74 #[inline]
75 pub fn slice_to_be24(v: &[u8]) -> u32 {
76         ((v[0] as u32) << 8*2) |
77         ((v[1] as u32) << 8*1) |
78         ((v[2] as u32) << 8*0)
79 }
80
81 #[inline]
82 pub fn slice_to_be32(v: &[u8]) -> u32 {
83         ((v[0] as u32) << 8*3) |
84         ((v[1] as u32) << 8*2) |
85         ((v[2] as u32) << 8*1) |
86         ((v[3] as u32) << 8*0)
87 }
88
89 #[inline]
90 pub fn be64_to_array(u: u64) -> [u8; 8] {
91         let mut v = [0; 8];
92         v[0] = ((u >> 8*7) & 0xff) as u8;
93         v[1] = ((u >> 8*6) & 0xff) as u8;
94         v[2] = ((u >> 8*5) & 0xff) as u8;
95         v[3] = ((u >> 8*4) & 0xff) as u8;
96         v[4] = ((u >> 8*3) & 0xff) as u8;
97         v[5] = ((u >> 8*2) & 0xff) as u8;
98         v[6] = ((u >> 8*1) & 0xff) as u8;
99         v[7] = ((u >> 8*0) & 0xff) as u8;
100         v
101 }
102
103 struct InputData {
104         data: Vec<u8>,
105         read_pos: AtomicUsize,
106 }
107 impl InputData {
108         fn get_slice(&self, len: usize) -> Option<&[u8]> {
109                 let old_pos = self.read_pos.fetch_add(len, Ordering::AcqRel);
110                 if self.data.len() < old_pos + len {
111                         return None;
112                 }
113                 Some(&self.data[old_pos..old_pos + len])
114         }
115 }
116
117 struct FuzzEstimator {
118         input: Arc<InputData>,
119 }
120 impl FeeEstimator for FuzzEstimator {
121         fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 {
122                 //TODO: We should actually be testing at least much more than 64k...
123                 match self.input.get_slice(2) {
124                         Some(slice) => cmp::max(slice_to_be16(slice) as u32, 253),
125                         None => 253
126                 }
127         }
128 }
129
130 struct FuzzRouter {}
131
132 impl Router for FuzzRouter {
133         fn find_route(
134                 &self, _payer: &PublicKey, _params: &RouteParameters, _first_hops: Option<&[&ChannelDetails]>,
135                 _inflight_htlcs: &InFlightHtlcs
136         ) -> Result<Route, msgs::LightningError> {
137                 Err(msgs::LightningError {
138                         err: String::from("Not implemented"),
139                         action: msgs::ErrorAction::IgnoreError
140                 })
141         }
142         fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}
143         fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {}
144         fn notify_payment_probe_successful(&self, _path: &[&RouteHop]) {}
145         fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {}
146 }
147
148 struct TestBroadcaster {
149         txn_broadcasted: Mutex<Vec<Transaction>>,
150 }
151 impl BroadcasterInterface for TestBroadcaster {
152         fn broadcast_transaction(&self, tx: &Transaction) {
153                 self.txn_broadcasted.lock().unwrap().push(tx.clone());
154         }
155 }
156
157 #[derive(Clone)]
158 struct Peer<'a> {
159         id: u8,
160         peers_connected: &'a RefCell<[bool; 256]>,
161 }
162 impl<'a> SocketDescriptor for Peer<'a> {
163         fn send_data(&mut self, data: &[u8], _resume_read: bool) -> usize {
164                 data.len()
165         }
166         fn disconnect_socket(&mut self) {
167                 assert!(self.peers_connected.borrow()[self.id as usize]);
168                 self.peers_connected.borrow_mut()[self.id as usize] = false;
169         }
170 }
171 impl<'a> PartialEq for Peer<'a> {
172         fn eq(&self, other: &Self) -> bool {
173                 self.id == other.id
174         }
175 }
176 impl<'a> Eq for Peer<'a> {}
177 impl<'a> std::hash::Hash for Peer<'a> {
178         fn hash<H : std::hash::Hasher>(&self, h: &mut H) {
179                 self.id.hash(h)
180         }
181 }
182
183 type ChannelMan<'a> = ChannelManager<
184         Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
185         Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<FuzzEstimator>, &'a FuzzRouter, Arc<dyn Logger>>;
186 type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan<'a>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, IgnoringMessageHandler, Arc<dyn Logger>, IgnoringMessageHandler, Arc<KeyProvider>>;
187
188 struct MoneyLossDetector<'a> {
189         manager: Arc<ChannelMan<'a>>,
190         monitor: Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
191         handler: PeerMan<'a>,
192
193         peers: &'a RefCell<[bool; 256]>,
194         funding_txn: Vec<Transaction>,
195         txids_confirmed: HashMap<Txid, usize>,
196         header_hashes: Vec<(BlockHash, u32)>,
197         height: usize,
198         max_height: usize,
199         blocks_connected: u32,
200 }
201 impl<'a> MoneyLossDetector<'a> {
202         pub fn new(peers: &'a RefCell<[bool; 256]>,
203                    manager: Arc<ChannelMan<'a>>,
204                    monitor: Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
205                    handler: PeerMan<'a>) -> Self {
206                 MoneyLossDetector {
207                         manager,
208                         monitor,
209                         handler,
210
211                         peers,
212                         funding_txn: Vec::new(),
213                         txids_confirmed: HashMap::new(),
214                         header_hashes: vec![(genesis_block(Network::Bitcoin).block_hash(), 0)],
215                         height: 0,
216                         max_height: 0,
217                         blocks_connected: 0,
218                 }
219         }
220
221         fn connect_block(&mut self, all_txn: &[Transaction]) {
222                 let mut txdata = Vec::with_capacity(all_txn.len());
223                 for (idx, tx) in all_txn.iter().enumerate() {
224                         let txid = tx.txid();
225                         match self.txids_confirmed.entry(txid) {
226                                 hash_map::Entry::Vacant(e) => {
227                                         e.insert(self.height);
228                                         txdata.push((idx + 1, tx));
229                                 },
230                                 _ => {},
231                         }
232                 }
233
234                 self.blocks_connected += 1;
235                 let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height].0, merkle_root: TxMerkleNode::all_zeros(), time: self.blocks_connected, bits: 42, nonce: 42 };
236                 self.height += 1;
237                 self.manager.transactions_confirmed(&header, &txdata, self.height as u32);
238                 self.manager.best_block_updated(&header, self.height as u32);
239                 (*self.monitor).transactions_confirmed(&header, &txdata, self.height as u32);
240                 (*self.monitor).best_block_updated(&header, self.height as u32);
241                 if self.header_hashes.len() > self.height {
242                         self.header_hashes[self.height] = (header.block_hash(), self.blocks_connected);
243                 } else {
244                         assert_eq!(self.header_hashes.len(), self.height);
245                         self.header_hashes.push((header.block_hash(), self.blocks_connected));
246                 }
247                 self.max_height = cmp::max(self.height, self.max_height);
248         }
249
250         fn disconnect_block(&mut self) {
251                 if self.height > 0 && (self.max_height < 6 || self.height >= self.max_height - 6) {
252                         let header = BlockHeader { version: 0x20000000, prev_blockhash: self.header_hashes[self.height - 1].0, merkle_root: TxMerkleNode::all_zeros(), time: self.header_hashes[self.height].1, bits: 42, nonce: 42 };
253                         self.manager.block_disconnected(&header, self.height as u32);
254                         self.monitor.block_disconnected(&header, self.height as u32);
255                         self.height -= 1;
256                         let removal_height = self.height;
257                         self.txids_confirmed.retain(|_, height| {
258                                 removal_height != *height
259                         });
260                 }
261         }
262 }
263
264 impl<'a> Drop for MoneyLossDetector<'a> {
265         fn drop(&mut self) {
266                 if !::std::thread::panicking() {
267                         // Disconnect all peers
268                         for (idx, peer) in self.peers.borrow().iter().enumerate() {
269                                 if *peer {
270                                         self.handler.socket_disconnected(&Peer{id: idx as u8, peers_connected: &self.peers});
271                                 }
272                         }
273
274                         // Force all channels onto the chain (and time out claim txn)
275                         self.manager.force_close_all_channels_broadcasting_latest_txn();
276                 }
277         }
278 }
279
280 struct KeyProvider {
281         node_secret: SecretKey,
282         inbound_payment_key: KeyMaterial,
283         counter: AtomicU64,
284         signer_state: RefCell<HashMap<u8, (bool, Arc<Mutex<EnforcementState>>)>>
285 }
286
287 impl EntropySource for KeyProvider {
288         fn get_secure_random_bytes(&self) -> [u8; 32] {
289                 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
290                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
291                         (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]
292         }
293 }
294
295 impl NodeSigner for KeyProvider {
296         fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
297                 let node_secret = match recipient {
298                         Recipient::Node => Ok(&self.node_secret),
299                         Recipient::PhantomNode => Err(())
300                 }?;
301                 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
302         }
303
304         fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
305                 let mut node_secret = match recipient {
306                         Recipient::Node => Ok(self.node_secret.clone()),
307                         Recipient::PhantomNode => Err(())
308                 }?;
309                 if let Some(tweak) = tweak {
310                         node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
311                 }
312                 Ok(SharedSecret::new(other_key, &node_secret))
313         }
314
315         fn get_inbound_payment_key_material(&self) -> KeyMaterial {
316                 self.inbound_payment_key.clone()
317         }
318
319         fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
320                 unreachable!()
321         }
322
323         fn sign_gossip_message(&self, msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
324                 let msg_hash = Message::from_slice(&Sha256dHash::hash(&msg.encode()[..])[..]).map_err(|_| ())?;
325                 let secp_ctx = Secp256k1::signing_only();
326                 Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
327         }
328 }
329
330 impl SignerProvider for KeyProvider {
331         type Signer = EnforcingSigner;
332
333         fn generate_channel_keys_id(&self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
334                 let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
335                 self.signer_state.borrow_mut().insert(ctr, (inbound, Arc::new(Mutex::new(EnforcementState::new()))));
336                 [ctr; 32]
337         }
338
339         fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> Self::Signer {
340                 let secp_ctx = Secp256k1::signing_only();
341                 let ctr = channel_keys_id[0];
342                 let (inbound, state) = self.signer_state.borrow().get(&ctr).unwrap().clone();
343                 EnforcingSigner::new_with_revoked(if inbound {
344                         InMemorySigner::new(
345                                 &secp_ctx,
346                                 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(),
347                                 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(),
348                                 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(),
349                                 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(),
350                                 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(),
351                                 [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],
352                                 channel_value_satoshis,
353                                 channel_keys_id,
354                         )
355                 } else {
356                         InMemorySigner::new(
357                                 &secp_ctx,
358                                 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(),
359                                 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(),
360                                 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(),
361                                 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(),
362                                 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(),
363                                 [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],
364                                 channel_value_satoshis,
365                                 channel_keys_id,
366                         )
367                 }, state, false)
368         }
369
370         fn read_chan_signer(&self, mut data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
371                 let inner: InMemorySigner = Readable::read(&mut data)?;
372                 let state = Arc::new(Mutex::new(EnforcementState::new()));
373
374                 Ok(EnforcingSigner::new_with_revoked(
375                         inner,
376                         state,
377                         false
378                 ))
379         }
380
381         fn get_destination_script(&self) -> Script {
382                 let secp_ctx = Secp256k1::signing_only();
383                 let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap();
384                 let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
385                 Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
386         }
387
388         fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
389                 let secp_ctx = Secp256k1::signing_only();
390                 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();
391                 let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
392                 ShutdownScript::new_p2wpkh(&pubkey_hash)
393         }
394 }
395
396 #[inline]
397 pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
398         let input = Arc::new(InputData {
399                 data: data.to_vec(),
400                 read_pos: AtomicUsize::new(0),
401         });
402         let fee_est = Arc::new(FuzzEstimator {
403                 input: input.clone(),
404         });
405         let router = FuzzRouter {};
406
407         macro_rules! get_slice {
408                 ($len: expr) => {
409                         match input.get_slice($len as usize) {
410                                 Some(slice) => slice,
411                                 None => return,
412                         }
413                 }
414         }
415
416         macro_rules! get_pubkey {
417                 () => {
418                         match PublicKey::from_slice(get_slice!(33)) {
419                                 Ok(key) => key,
420                                 Err(_) => return,
421                         }
422                 }
423         }
424
425         let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
426                 Ok(key) => key,
427                 Err(_) => return,
428         };
429
430         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];
431
432         let broadcast = Arc::new(TestBroadcaster{ txn_broadcasted: Mutex::new(Vec::new()) });
433         let monitor = Arc::new(chainmonitor::ChainMonitor::new(None, broadcast.clone(), Arc::clone(&logger), fee_est.clone(),
434                 Arc::new(TestPersister { update_ret: Mutex::new(ChannelMonitorUpdateStatus::Completed) })));
435
436         let keys_manager = Arc::new(KeyProvider {
437                 node_secret: our_network_key.clone(),
438                 inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()),
439                 counter: AtomicU64::new(0),
440                 signer_state: RefCell::new(HashMap::new())
441         });
442         let mut config = UserConfig::default();
443         config.channel_config.forwarding_fee_proportional_millionths =  slice_to_be32(get_slice!(4));
444         config.channel_handshake_config.announced_channel = get_slice!(1)[0] != 0;
445         let network = Network::Bitcoin;
446         let params = ChainParameters {
447                 network,
448                 best_block: BestBlock::from_genesis(network),
449         };
450         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));
451         // Adding new calls to `EntropySource::get_secure_random_bytes` during startup can change all the
452         // keys subsequently generated in this test. Rather than regenerating all the messages manually,
453         // it's easier to just increment the counter here so the keys don't change.
454         keys_manager.counter.fetch_sub(3, Ordering::AcqRel);
455         let our_id = &keys_manager.get_node_id(Recipient::Node).unwrap();
456         let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash(), Arc::clone(&logger)));
457         let gossip_sync = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
458         let scorer = FixedPenaltyScorer::with_penalty(0);
459
460         let peers = RefCell::new([false; 256]);
461         let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler {
462                 chan_handler: channelmanager.clone(),
463                 route_handler: gossip_sync.clone(),
464                 onion_message_handler: IgnoringMessageHandler {},
465         }, 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), IgnoringMessageHandler{}, keys_manager.clone()));
466
467         let mut should_forward = false;
468         let mut payments_received: Vec<PaymentHash> = Vec::new();
469         let mut payments_sent = 0;
470         let mut pending_funding_generation: Vec<([u8; 32], PublicKey, u64, Script)> = Vec::new();
471         let mut pending_funding_signatures = HashMap::new();
472
473         loop {
474                 match get_slice!(1)[0] {
475                         0 => {
476                                 let mut new_id = 0;
477                                 for i in 1..256 {
478                                         if !peers.borrow()[i-1] {
479                                                 new_id = i;
480                                                 break;
481                                         }
482                                 }
483                                 if new_id == 0 { return; }
484                                 loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}, None).unwrap();
485                                 peers.borrow_mut()[new_id - 1] = true;
486                         },
487                         1 => {
488                                 let mut new_id = 0;
489                                 for i in 1..256 {
490                                         if !peers.borrow()[i-1] {
491                                                 new_id = i;
492                                                 break;
493                                         }
494                                 }
495                                 if new_id == 0 { return; }
496                                 loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}, None).unwrap();
497                                 peers.borrow_mut()[new_id - 1] = true;
498                         },
499                         2 => {
500                                 let peer_id = get_slice!(1)[0];
501                                 if !peers.borrow()[peer_id as usize] { return; }
502                                 loss_detector.handler.socket_disconnected(&Peer{id: peer_id, peers_connected: &peers});
503                                 peers.borrow_mut()[peer_id as usize] = false;
504                         },
505                         3 => {
506                                 let peer_id = get_slice!(1)[0];
507                                 if !peers.borrow()[peer_id as usize] { return; }
508                                 match loss_detector.handler.read_event(&mut Peer{id: peer_id, peers_connected: &peers}, get_slice!(get_slice!(1)[0])) {
509                                         Ok(res) => assert!(!res),
510                                         Err(_) => { peers.borrow_mut()[peer_id as usize] = false; }
511                                 }
512                         },
513                         4 => {
514                                 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
515                                 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
516                                 let params = RouteParameters {
517                                         payment_params,
518                                         final_value_msat,
519                                         final_cltv_expiry_delta: 42,
520                                 };
521                                 let random_seed_bytes: [u8; 32] = keys_manager.get_secure_random_bytes();
522                                 let route = match find_route(&our_id, &params, &network_graph, None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
523                                         Ok(route) => route,
524                                         Err(_) => return,
525                                 };
526                                 let mut payment_hash = PaymentHash([0; 32]);
527                                 payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
528                                 let mut sha = Sha256::engine();
529                                 sha.input(&payment_hash.0[..]);
530                                 payment_hash.0 = Sha256::from_engine(sha).into_inner();
531                                 payments_sent += 1;
532                                 match channelmanager.send_payment(&route, payment_hash, &None, PaymentId(payment_hash.0)) {
533                                         Ok(_) => {},
534                                         Err(_) => return,
535                                 }
536                         },
537                         15 => {
538                                 let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
539                                 let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
540                                 let params = RouteParameters {
541                                         payment_params,
542                                         final_value_msat,
543                                         final_cltv_expiry_delta: 42,
544                                 };
545                                 let random_seed_bytes: [u8; 32] = keys_manager.get_secure_random_bytes();
546                                 let mut route = match find_route(&our_id, &params, &network_graph, None, Arc::clone(&logger), &scorer, &random_seed_bytes) {
547                                         Ok(route) => route,
548                                         Err(_) => return,
549                                 };
550                                 route.paths.push(route.paths[0].clone());
551                                 let mut payment_hash = PaymentHash([0; 32]);
552                                 payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
553                                 let mut sha = Sha256::engine();
554                                 sha.input(&payment_hash.0[..]);
555                                 payment_hash.0 = Sha256::from_engine(sha).into_inner();
556                                 payments_sent += 1;
557                                 let mut payment_secret = PaymentSecret([0; 32]);
558                                 payment_secret.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
559                                 payments_sent += 1;
560                                 match channelmanager.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)) {
561                                         Ok(_) => {},
562                                         Err(_) => return,
563                                 }
564                         },
565                         5 => {
566                                 let peer_id = get_slice!(1)[0];
567                                 if !peers.borrow()[peer_id as usize] { return; }
568                                 let their_key = get_pubkey!();
569                                 let chan_value = slice_to_be24(get_slice!(3)) as u64;
570                                 let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
571                                 if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0, None).is_err() { return; }
572                         },
573                         6 => {
574                                 let mut channels = channelmanager.list_channels();
575                                 let channel_id = get_slice!(1)[0] as usize;
576                                 if channel_id >= channels.len() { return; }
577                                 channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
578                                 if channelmanager.close_channel(&channels[channel_id].channel_id, &channels[channel_id].counterparty.node_id).is_err() { return; }
579                         },
580                         7 => {
581                                 if should_forward {
582                                         channelmanager.process_pending_htlc_forwards();
583                                         should_forward = false;
584                                 }
585                         },
586                         8 => {
587                                 for payment in payments_received.drain(..) {
588                                         // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
589                                         // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
590                                         // fulfill this HTLC, but if they are, we can just take the first byte and
591                                         // place that anywhere in our preimage.
592                                         if &payment.0[1..] != &[0; 31] {
593                                                 channelmanager.fail_htlc_backwards(&payment);
594                                         } else {
595                                                 let mut payment_preimage = PaymentPreimage([0; 32]);
596                                                 payment_preimage.0[0] = payment.0[0];
597                                                 channelmanager.claim_funds(payment_preimage);
598                                         }
599                                 }
600                         },
601                         16 => {
602                                 let payment_preimage = PaymentPreimage(keys_manager.get_secure_random_bytes());
603                                 let mut sha = Sha256::engine();
604                                 sha.input(&payment_preimage.0[..]);
605                                 let payment_hash = PaymentHash(Sha256::from_engine(sha).into_inner());
606                                 // Note that this may fail - our hashes may collide and we'll end up trying to
607                                 // double-register the same payment_hash.
608                                 let _ = channelmanager.create_inbound_payment_for_hash(payment_hash, None, 1, None);
609                         },
610                         9 => {
611                                 for payment in payments_received.drain(..) {
612                                         channelmanager.fail_htlc_backwards(&payment);
613                                 }
614                         },
615                         10 => {
616                                 'outer_loop: for funding_generation in pending_funding_generation.drain(..) {
617                                         let mut tx = Transaction { version: 0, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
618                                                         value: funding_generation.2, script_pubkey: funding_generation.3,
619                                                 }] };
620                                         let funding_output = 'search_loop: loop {
621                                                 let funding_txid = tx.txid();
622                                                 if let None = loss_detector.txids_confirmed.get(&funding_txid) {
623                                                         let outpoint = OutPoint { txid: funding_txid, index: 0 };
624                                                         for chan in channelmanager.list_channels() {
625                                                                 if chan.channel_id == outpoint.to_channel_id() {
626                                                                         tx.version += 1;
627                                                                         continue 'search_loop;
628                                                                 }
629                                                         }
630                                                         break outpoint;
631                                                 }
632                                                 tx.version += 1;
633                                                 if tx.version > 0xff {
634                                                         continue 'outer_loop;
635                                                 }
636                                         };
637                                         if let Err(e) = channelmanager.funding_transaction_generated(&funding_generation.0, &funding_generation.1, tx.clone()) {
638                                                 // It's possible the channel has been closed in the mean time, but any other
639                                                 // failure may be a bug.
640                                                 if let APIError::ChannelUnavailable { err } = e {
641                                                         if !err.starts_with("Can't find a peer matching the passed counterparty node_id ") {
642                                                                 assert_eq!(err, "No such channel");
643                                                         }
644                                                 } else { panic!(); }
645                                         }
646                                         pending_funding_signatures.insert(funding_output, tx);
647                                 }
648                         },
649                         11 => {
650                                 let mut txn = broadcast.txn_broadcasted.lock().unwrap().split_off(0);
651                                 if !txn.is_empty() {
652                                         loss_detector.connect_block(&txn[..]);
653                                         for _ in 2..100 {
654                                                 loss_detector.connect_block(&[]);
655                                         }
656                                 }
657                                 for tx in txn.drain(..) {
658                                         loss_detector.funding_txn.push(tx);
659                                 }
660                         },
661                         12 => {
662                                 let txlen = slice_to_be16(get_slice!(2));
663                                 if txlen == 0 {
664                                         loss_detector.connect_block(&[]);
665                                 } else {
666                                         let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
667                                         if let Ok(tx) = txres {
668                                                 let mut output_val = 0;
669                                                 for out in tx.output.iter() {
670                                                         if out.value > 21_000_000_0000_0000 { return; }
671                                                         output_val += out.value;
672                                                         if output_val > 21_000_000_0000_0000 { return; }
673                                                 }
674                                                 loss_detector.connect_block(&[tx]);
675                                         } else {
676                                                 return;
677                                         }
678                                 }
679                         },
680                         13 => {
681                                 loss_detector.disconnect_block();
682                         },
683                         14 => {
684                                 let mut channels = channelmanager.list_channels();
685                                 let channel_id = get_slice!(1)[0] as usize;
686                                 if channel_id >= channels.len() { return; }
687                                 channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
688                                 channelmanager.force_close_broadcasting_latest_txn(&channels[channel_id].channel_id, &channels[channel_id].counterparty.node_id).unwrap();
689                         },
690                         // 15 is above
691                         _ => return,
692                 }
693                 loss_detector.handler.process_events();
694                 for event in loss_detector.manager.get_and_clear_pending_events() {
695                         match event {
696                                 Event::FundingGenerationReady { temporary_channel_id, counterparty_node_id, channel_value_satoshis, output_script, .. } => {
697                                         pending_funding_generation.push((temporary_channel_id, counterparty_node_id, channel_value_satoshis, output_script));
698                                 },
699                                 Event::PaymentClaimable { payment_hash, .. } => {
700                                         //TODO: enhance by fetching random amounts from fuzz input?
701                                         payments_received.push(payment_hash);
702                                 },
703                                 Event::PendingHTLCsForwardable {..} => {
704                                         should_forward = true;
705                                 },
706                                 _ => {},
707                         }
708                 }
709         }
710 }
711
712 pub fn full_stack_test<Out: test_logger::Output>(data: &[u8], out: Out) {
713         let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), out));
714         do_test(data, &logger);
715 }
716
717 #[no_mangle]
718 pub extern "C" fn full_stack_run(data: *const u8, datalen: usize) {
719         let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {}));
720         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
721 }
722
723 #[cfg(test)]
724 mod tests {
725         use lightning::util::logger::{Logger, Record};
726         use std::collections::HashMap;
727         use std::sync::{Arc, Mutex};
728
729         struct TrackingLogger {
730                 /// (module, message) -> count
731                 pub lines: Mutex<HashMap<(String, String), usize>>,
732         }
733         impl Logger for TrackingLogger {
734                 fn log(&self, record: &Record) {
735                         *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
736                         println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
737                 }
738         }
739
740         #[test]
741         fn test_no_existing_test_breakage() {
742                 // To avoid accidentally causing all existing fuzz test cases to be useless by making minor
743                 // changes (such as requesting feerate info in a new place), we run a pretty full
744                 // step-through with two peers and HTLC forwarding here. Obviously this is pretty finicky,
745                 // so this should be updated pretty liberally, but at least we'll know when changes occur.
746                 // If nothing else, this test serves as a pretty great initial full_stack_target seed.
747
748                 // What each byte represents is broken down below, and then everything is concatenated into
749                 // one large test at the end (you want %s/ -.*//g %s/\n\| \|\t\|\///g).
750
751                 // Following BOLT 8, lightning message on the wire are: 2-byte encrypted message length +
752                 // 16-byte MAC of the encrypted message length + encrypted Lightning message + 16-byte MAC
753                 // of the Lightning message
754                 // I.e 2nd inbound read, len 18 : 0006 (encrypted message length) + 03000000000000000000000000000000 (MAC of the encrypted message length)
755                 // Len 22 : 0010 00000000 (encrypted lightning message) + 03000000000000000000000000000000 (MAC of the Lightning message)
756
757                 // Writing new code generating transactions and see a new failure ? Don't forget to add input for the FuzzEstimator !
758
759                 // 0100000000000000000000000000000000000000000000000000000000000000 - our network key
760                 // 00000000 - fee_proportional_millionths
761                 // 01 - announce_channels_publicly
762                 //
763                 // 00 - new outbound connection with id 0
764                 // 030000000000000000000000000000000000000000000000000000000000000002 - peer's pubkey
765                 // 030032 - inbound read from peer id 0 of len 50
766                 // 00 030000000000000000000000000000000000000000000000000000000000000002 03000000000000000000000000000000 - noise act two (0||pubkey||mac)
767                 //
768                 // 030012 - inbound read from peer id 0 of len 18
769                 // 000a 03000000000000000000000000000000 - message header indicating message length 10
770                 // 03001a - inbound read from peer id 0 of len 26
771                 // 0010 00022000 00022000 03000000000000000000000000000000 - init message (type 16) with static_remotekey (0x2000) and mac
772                 //
773                 // 030012 - inbound read from peer id 0 of len 18
774                 // 0141 03000000000000000000000000000000 - message header indicating message length 321
775                 // 0300fe - inbound read from peer id 0 of len 254
776                 // 0020 7500000000000000000000000000000000000000000000000000000000000000 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 000000000000c350 0000000000000000 0000000000000162 ffffffffffffffff 0000000000000222 0000000000000000 000000fd 0006 01e3 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 030000000000000000000000000000000000000000000000000000000000000004 - beginning of open_channel message
777                 // 030053 - inbound read from peer id 0 of len 83
778                 // 030000000000000000000000000000000000000000000000000000000000000005 020900000000000000000000000000000000000000000000000000000000000000 01 03000000000000000000000000000000 - rest of open_channel and mac
779                 //
780                 // 00fd00fd - Two feerate requests (all returning min feerate, which our open_channel also uses) (gonna be ingested by FuzzEstimator)
781                 // - client should now respond with accept_channel (CHECK 1: type 33 to peer 03000000)
782                 //
783                 // 030012 - inbound read from peer id 0 of len 18
784                 // 0084 03000000000000000000000000000000 - message header indicating message length 132
785                 // 030094 - inbound read from peer id 0 of len 148
786                 // 0022 ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679 3d00000000000000000000000000000000000000000000000000000000000000 0000 00000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - funding_created and mac
787                 // - client should now respond with funding_signed (CHECK 2: type 35 to peer 03000000)
788                 //
789                 // 0c005e - connect a block with one transaction of len 94
790                 // 020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae0000000000000000000000000000000000000000000000000000000000000000000000 - the funding transaction
791                 // 0c0000 - connect a block with no transactions
792                 // 0c0000 - connect a block with no transactions
793                 // 0c0000 - connect a block with no transactions
794                 // 0c0000 - connect a block with no transactions
795                 // 0c0000 - connect a block with no transactions
796                 // 0c0000 - connect a block with no transactions
797                 // 0c0000 - connect a block with no transactions
798                 // 0c0000 - connect a block with no transactions
799                 // 0c0000 - connect a block with no transactions
800                 // 0c0000 - connect a block with no transactions
801                 // 0c0000 - connect a block with no transactions
802                 // 0c0000 - connect a block with no transactions
803                 // - by now client should have sent a channel_ready (CHECK 3: SendChannelReady to 03000000 for chan 3d000000)
804                 //
805                 // 030012 - inbound read from peer id 0 of len 18
806                 // 0043 03000000000000000000000000000000 - message header indicating message length 67
807                 // 030053 - inbound read from peer id 0 of len 83
808                 // 0024 3d00000000000000000000000000000000000000000000000000000000000000 020800000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - channel_ready and mac
809                 //
810                 // 01 - new inbound connection with id 1
811                 // 030132 - inbound read from peer id 1 of len 50
812                 // 0003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000 - inbound noise act 1
813                 // 030142 - inbound read from peer id 1 of len 66
814                 // 000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000 - inbound noise act 3
815                 //
816                 // 030112 - inbound read from peer id 1 of len 18
817                 // 000a 01000000000000000000000000000000 - message header indicating message length 10
818                 // 03011a - inbound read from peer id 1 of len 26
819                 // 0010 00022000 00022000 01000000000000000000000000000000 - init message (type 16) with static_remotekey (0x2000) and mac
820                 //
821                 // 05 01 030200000000000000000000000000000000000000000000000000000000000000 00c350 0003e8 - create outbound channel to peer 1 for 50k sat
822                 // 00fd - One feerate requests (all returning min feerate) (gonna be ingested by FuzzEstimator)
823                 //
824                 // 030112 - inbound read from peer id 1 of len 18
825                 // 0110 01000000000000000000000000000000 - message header indicating message length 272
826                 // 0301ff - inbound read from peer id 1 of len 255
827                 // 0021 0000000000000000000000000000000000000000000000000000000000000e05 0000000000000162 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 02660000000000000000000000000000 - beginning of accept_channel
828                 // 030121 - inbound read from peer id 1 of len 33
829                 // 0000000000000000000000000000000000 01000000000000000000000000000000 - rest of accept_channel and mac
830                 //
831                 // 0a - create the funding transaction (client should send funding_created now)
832                 //
833                 // 030112 - inbound read from peer id 1 of len 18
834                 // 0062 01000000000000000000000000000000 - message header indicating message length 98
835                 // 030172 - inbound read from peer id 1 of len 114
836                 // 0023 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - funding_signed message and mac
837                 //
838                 // 0b - broadcast funding transaction
839                 // - by now client should have sent a channel_ready (CHECK 4: SendChannelReady to 03020000 for chan 3f000000)
840                 //
841                 // 030112 - inbound read from peer id 1 of len 18
842                 // 0043 01000000000000000000000000000000 - message header indicating message length 67
843                 // 030153 - inbound read from peer id 1 of len 83
844                 // 0024 3a00000000000000000000000000000000000000000000000000000000000000 026700000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - channel_ready and mac
845                 //
846                 // 030012 - inbound read from peer id 0 of len 18
847                 // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
848                 // 0300ff - inbound read from peer id 0 of len 255
849                 // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000000 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
850                 // 0300ff - inbound read from peer id 0 of len 255
851                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
852                 // 0300ff - inbound read from peer id 0 of len 255
853                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
854                 // 0300ff - inbound read from peer id 0 of len 255
855                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
856                 // 0300ff - inbound read from peer id 0 of len 255
857                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
858                 // 0300c1 - inbound read from peer id 0 of len 193
859                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
860                 //
861                 // 030012 - inbound read from peer id 0 of len 18
862                 // 0064 03000000000000000000000000000000 - message header indicating message length 100
863                 // 030074 - inbound read from peer id 0 of len 116
864                 // 0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000300100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000 - commitment_signed and mac
865                 // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6: types 133 and 132 to peer 03000000)
866                 //
867                 // 030012 - inbound read from peer id 0 of len 18
868                 // 0063 03000000000000000000000000000000 - message header indicating message length 99
869                 // 030073 - inbound read from peer id 0 of len 115
870                 // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0900000000000000000000000000000000000000000000000000000000000000 020b00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
871                 //
872                 // 07 - process the now-pending HTLC forward
873                 // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7: SendHTLCs event for node 03020000 with 1 HTLCs for channel 3f000000)
874                 //
875                 // - we respond with commitment_signed then revoke_and_ack (a weird, but valid, order)
876                 // 030112 - inbound read from peer id 1 of len 18
877                 // 0064 01000000000000000000000000000000 - message header indicating message length 100
878                 // 030174 - inbound read from peer id 1 of len 116
879                 // 0084 3a00000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000006a0001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000 - commitment_signed and mac
880                 //
881                 // 030112 - inbound read from peer id 1 of len 18
882                 // 0063 01000000000000000000000000000000 - message header indicating message length 99
883                 // 030173 - inbound read from peer id 1 of len 115
884                 // 0085 3a00000000000000000000000000000000000000000000000000000000000000 6600000000000000000000000000000000000000000000000000000000000000 026400000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
885                 //
886                 // 030112 - inbound read from peer id 1 of len 18
887                 // 004a 01000000000000000000000000000000 - message header indicating message length 74
888                 // 03015a - inbound read from peer id 1 of len 90
889                 // 0082 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000000 ff00888888888888888888888888888888888888888888888888888888888888 01000000000000000000000000000000 - update_fulfill_htlc and mac
890                 // - client should immediately claim the pending HTLC from peer 0 (CHECK 8: SendFulfillHTLCs for node 03000000 with preimage ff00888888 for channel 3d000000)
891                 //
892                 // 030112 - inbound read from peer id 1 of len 18
893                 // 0064 01000000000000000000000000000000 - message header indicating message length 100
894                 // 030174 - inbound read from peer id 1 of len 116
895                 // 0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000100001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000 - commitment_signed and mac
896                 //
897                 // 030112 - inbound read from peer id 1 of len 18
898                 // 0063 01000000000000000000000000000000 - message header indicating message length 99
899                 // 030173 - inbound read from peer id 1 of len 115
900                 // 0085 3a00000000000000000000000000000000000000000000000000000000000000 6700000000000000000000000000000000000000000000000000000000000000 026500000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
901                 //
902                 // - before responding to the commitment_signed generated above, send a new HTLC
903                 // 030012 - inbound read from peer id 0 of len 18
904                 // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
905                 // 0300ff - inbound read from peer id 0 of len 255
906                 // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000000000003e80 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 11 020203e8 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
907                 // 0300ff - inbound read from peer id 0 of len 255
908                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
909                 // 0300ff - inbound read from peer id 0 of len 255
910                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
911                 // 0300ff - inbound read from peer id 0 of len 255
912                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
913                 // 0300ff - inbound read from peer id 0 of len 255
914                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
915                 // 0300c1 - inbound read from peer id 0 of len 193
916                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ab00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
917                 //
918                 // - now respond to the update_fulfill_htlc+commitment_signed messages the client sent to peer 0
919                 // 030012 - inbound read from peer id 0 of len 18
920                 // 0063 03000000000000000000000000000000 - message header indicating message length 99
921                 // 030073 - inbound read from peer id 0 of len 115
922                 // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0800000000000000000000000000000000000000000000000000000000000000 020a00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
923                 // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
924                 //
925                 // 030012 - inbound read from peer id 0 of len 18
926                 // 0064 03000000000000000000000000000000 - message header indicating message length 100
927                 // 030074 - inbound read from peer id 0 of len 116
928                 // 0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000c30100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000 - commitment_signed and mac
929                 //
930                 // 030012 - inbound read from peer id 0 of len 18
931                 // 0063 03000000000000000000000000000000 - message header indicating message length 99
932                 // 030073 - inbound read from peer id 0 of len 115
933                 // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0b00000000000000000000000000000000000000000000000000000000000000 020d00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
934                 //
935                 // 07 - process the now-pending HTLC forward
936                 // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
937                 // - we respond with revoke_and_ack, then commitment_signed, then update_fail_htlc
938                 //
939                 // 030112 - inbound read from peer id 1 of len 18
940                 // 0064 01000000000000000000000000000000 - message header indicating message length 100
941                 // 030174 - inbound read from peer id 1 of len 116
942                 // 0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000 - commitment_signed and mac
943                 //
944                 // 030112 - inbound read from peer id 1 of len 18
945                 // 0063 01000000000000000000000000000000 - message header indicating message length 99
946                 // 030173 - inbound read from peer id 1 of len 115
947                 // 0085 3a00000000000000000000000000000000000000000000000000000000000000 6400000000000000000000000000000000000000000000000000000000000000 027000000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
948                 //
949                 // 030112 - inbound read from peer id 1 of len 18
950                 // 002c 01000000000000000000000000000000 - message header indicating message length 44
951                 // 03013c - inbound read from peer id 1 of len 60
952                 // 0083 3a00000000000000000000000000000000000000000000000000000000000000 0000000000000001 0000 01000000000000000000000000000000 - update_fail_htlc and mac
953                 //
954                 // 030112 - inbound read from peer id 1 of len 18
955                 // 0064 01000000000000000000000000000000 - message header indicating message length 100
956                 // 030174 - inbound read from peer id 1 of len 116
957                 // 0084 3a00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000390001000000000000000000000000000000000000000000000000000000000000 0000 01000000000000000000000000000000 - commitment_signed and mac
958                 //
959                 // 030112 - inbound read from peer id 1 of len 18
960                 // 0063 01000000000000000000000000000000 - message header indicating message length 99
961                 // 030173 - inbound read from peer id 1 of len 115
962                 // 0085 3a00000000000000000000000000000000000000000000000000000000000000 6500000000000000000000000000000000000000000000000000000000000000 027100000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000 - revoke_and_ack and mac
963                 //
964                 // 07 - process the now-pending HTLC forward
965                 // - client now sends id 0 update_fail_htlc and commitment_signed (CHECK 9)
966                 // - now respond to the update_fail_htlc+commitment_signed messages the client sent to peer 0
967                 //
968                 // 030012 - inbound read from peer id 0 of len 18
969                 // 0063 03000000000000000000000000000000 - message header indicating message length 99
970                 // 030073 - inbound read from peer id 0 of len 115
971                 // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0a00000000000000000000000000000000000000000000000000000000000000 020c00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
972                 //
973                 // 030012 - inbound read from peer id 0 of len 18
974                 // 0064 03000000000000000000000000000000 - message header indicating message length 100
975                 // 030074 - inbound read from peer id 0 of len 116
976                 // 0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000320100000000000000000000000000000000000000000000000000000000000000 0000 03000000000000000000000000000000 - commitment_signed and mac
977                 // - client should now respond with revoke_and_ack (CHECK 5 duplicate)
978                 //
979                 // 030012 - inbound read from peer id 0 of len 18
980                 // 05ac 03000000000000000000000000000000 - message header indicating message length 1452
981                 // 0300ff - inbound read from peer id 0 of len 255
982                 // 0080 3d00000000000000000000000000000000000000000000000000000000000000 0000000000000002 00000000000b0838 ff00000000000000000000000000000000000000000000000000000000000000 000003f0 00 030000000000000000000000000000000000000000000000000000000000000555 12 02030927c0 0401a0 060800000e0000010000 0a00000000000000000000000000000000000000000000000000000000000000 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - beginning of update_add_htlc from 0 to 1 via client
983                 // 0300ff - inbound read from peer id 0 of len 255
984                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
985                 // 0300ff - inbound read from peer id 0 of len 255
986                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
987                 // 0300ff - inbound read from peer id 0 of len 255
988                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
989                 // 0300ff - inbound read from peer id 0 of len 255
990                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
991                 // 0300c1 - inbound read from peer id 0 of len 193
992                 // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 5300000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - end of update_add_htlc from 0 to 1 via client and mac
993                 //
994                 // 030012 - inbound read from peer id 0 of len 18
995                 // 00a4 03000000000000000000000000000000 - message header indicating message length 164
996                 // 0300b4 - inbound read from peer id 0 of len 180
997                 // 0084 3d00000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000750100000000000000000000000000000000000000000000000000000000000000 0001 00000000000000000000000000000000000000000000000000000000000000670500000000000000000000000000000000000000000000000000000000000006 03000000000000000000000000000000 - commitment_signed and mac
998                 // - client should now respond with revoke_and_ack and commitment_signed (CHECK 5/6 duplicates)
999                 //
1000                 // 030012 - inbound read from peer id 0 of len 18
1001                 // 0063 03000000000000000000000000000000 - message header indicating message length 99
1002                 // 030073 - inbound read from peer id 0 of len 115
1003                 // 0085 3d00000000000000000000000000000000000000000000000000000000000000 0d00000000000000000000000000000000000000000000000000000000000000 020f00000000000000000000000000000000000000000000000000000000000000 03000000000000000000000000000000 - revoke_and_ack and mac
1004                 //
1005                 // 07 - process the now-pending HTLC forward
1006                 // - client now sends id 1 update_add_htlc and commitment_signed (CHECK 7 duplicate)
1007                 //
1008                 // 0c007d - connect a block with one transaction of len 125
1009                 // 02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c0000000000000160014280000000000000000000000000000000000000005000020 - the commitment transaction for channel 3f00000000000000000000000000000000000000000000000000000000000000
1010                 //
1011                 // 0c005e - connect a block with one transaction of len 94
1012                 // 0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b20000000000000000000000000000000000000000000000000000000000000000000000 - the HTLC timeout transaction
1013                 // 0c0000 - connect a block with no transactions
1014                 // 0c0000 - connect a block with no transactions
1015                 // 0c0000 - connect a block with no transactions
1016                 // 0c0000 - connect a block with no transactions
1017                 // 0c0000 - connect a block with no transactions
1018                 //
1019                 // 07 - process the now-pending HTLC forward
1020                 // - client now fails the HTLC backwards as it was unable to extract the payment preimage (CHECK 9 duplicate and CHECK 10)
1021
1022                 let logger = Arc::new(TrackingLogger { lines: Mutex::new(HashMap::new()) });
1023                 super::do_test(&::hex::decode("01000000000000000000000000000000000000000000000000000000000000000000000001000300000000000000000000000000000000000000000000000000000000000000020300320003000000000000000000000000000000000000000000000000000000000000000203000000000000000000000000000000030012000a0300000000000000000000000000000003001a00100002200000022000030000000000000000000000000000000300120141030000000000000000000000000000000300fe00207500000000000000000000000000000000000000000000000000000000000000ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679000000000000c35000000000000000000000000000000162ffffffffffffffff00000000000002220000000000000000000000fd000601e3030000000000000000000000000000000000000000000000000000000000000001030000000000000000000000000000000000000000000000000000000000000002030000000000000000000000000000000000000000000000000000000000000003030000000000000000000000000000000000000000000000000000000000000004030053030000000000000000000000000000000000000000000000000000000000000005020900000000000000000000000000000000000000000000000000000000000000010300000000000000000000000000000000fd00fd0300120084030000000000000000000000000000000300940022ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb1819096793d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000210100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000c005e020000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0150c3000000000000220020ae00000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c00000c000003001200430300000000000000000000000000000003005300243d0000000000000000000000000000000000000000000000000000000000000002080000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000010301320003000000000000000000000000000000000000000000000000000000000000000703000000000000000000000000000000030142000302000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003000000000000000000000000000000030112000a0100000000000000000000000000000003011a0010000220000002200001000000000000000000000000000000050103020000000000000000000000000000000000000000000000000000000000000000c3500003e800fd0301120110010000000000000000000000000000000301ff00210000000000000000000000000000000000000000000000000000000000000e05000000000000016200000000004c4b4000000000000003e800000000000003e80000000203f00005030000000000000000000000000000000000000000000000000000000000000100030000000000000000000000000000000000000000000000000000000000000200030000000000000000000000000000000000000000000000000000000000000300030000000000000000000000000000000000000000000000000000000000000400030000000000000000000000000000000000000000000000000000000000000500026600000000000000000000000000000301210000000000000000000000000000000000010000000000000000000000000000000a03011200620100000000000000000000000000000003017200233a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007c0001000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000b03011200430100000000000000000000000000000003015300243a000000000000000000000000000000000000000000000000000000000000000267000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000020b00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000660000000000000000000000000000000000000000000000000000000000000002640000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112004a0100000000000000000000000000000003015a00823a000000000000000000000000000000000000000000000000000000000000000000000000000000ff008888888888888888888888888888888888888888888888888888888888880100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a0000000000000000000000000000000000000000000000000000000000000067000000000000000000000000000000000000000000000000000000000000000265000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d0000000000000000000000000000000000000000000000000000000000000000000000000000010000000000003e80ff00000000000000000000000000000000000000000000000000000000000000000003f00003000000000000000000000000000000000000000000000000000000000000055511020203e80401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffab000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020a000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c3010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000000000000000000000000020d00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000703011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000002700000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000030112002c0100000000000000000000000000000003013c00833a00000000000000000000000000000000000000000000000000000000000000000000000000000100000100000000000000000000000000000003011200640100000000000000000000000000000003017400843a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000003011200630100000000000000000000000000000003017300853a000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000703001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020c000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200640300000000000000000000000000000003007400843d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032010000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001205ac030000000000000000000000000000000300ff00803d00000000000000000000000000000000000000000000000000000000000000000000000000000200000000000b0838ff00000000000000000000000000000000000000000000000000000000000000000003f0000300000000000000000000000000000000000000000000000000000000000005551202030927c00401a0060800000e00000100000a00000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0300c1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff53000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000003001200a4030000000000000000000000000000000300b400843d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007501000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006705000000000000000000000000000000000000000000000000000000000000060300000000000000000000000000000003001200630300000000000000000000000000000003007300853d000000000000000000000000000000000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000020f0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000070c007d02000000013a000000000000000000000000000000000000000000000000000000000000000000000000000000800258020000000000002200204b0000000000000000000000000000000000000000000000000000000000000014c00000000000001600142800000000000000000000000000000000000000050000200c005e0200000001730000000000000000000000000000000000000000000000000000000000000000000000000000000001a701000000000000220020b200000000000000000000000000000000000000000000000000000000000000000000000c00000c00000c00000c00000c000007").unwrap(), &(Arc::clone(&logger) as Arc<dyn Logger>));
1024
1025                 let log_entries = logger.lines.lock().unwrap();
1026                 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
1027                 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
1028                 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
1029                 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
1030                 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
1031                 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
1032                 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
1033                 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
1034                 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
1035                 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
1036         }
1037 }