Add block_disconnecting tests to cancel HTLC failure updates
[rust-lightning] / src / ln / functional_test_utils.rs
1 //! A bunch of useful utilities for building networks of nodes and exchanging messages between
2 //! nodes for functional tests.
3
4 use chain::chaininterface;
5 use chain::transaction::OutPoint;
6 use chain::keysinterface::KeysInterface;
7 use ln::channelmanager::{ChannelManager,RAACommitmentOrder, PaymentPreimage, PaymentHash};
8 use ln::router::{Route, Router};
9 use ln::msgs;
10 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
11 use util::test_utils;
12 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
13 use util::errors::APIError;
14 use util::logger::Logger;
15 use util::config::UserConfig;
16
17 use bitcoin::util::hash::BitcoinHash;
18 use bitcoin::blockdata::block::{BlockHeader, Block};
19 use bitcoin::blockdata::transaction::{Transaction, TxOut};
20 use bitcoin::network::constants::Network;
21
22 use bitcoin_hashes::sha256::Hash as Sha256;
23 use bitcoin_hashes::sha256d::Hash as Sha256d;
24 use bitcoin_hashes::Hash;
25
26 use secp256k1::Secp256k1;
27 use secp256k1::key::PublicKey;
28
29 use rand::{thread_rng,Rng};
30
31 use std::cell::RefCell;
32 use std::collections::HashMap;
33 use std::default::Default;
34 use std::rc::Rc;
35 use std::sync::{Arc, Mutex};
36 use std::time::Instant;
37 use std::mem;
38
39 pub const CHAN_CONFIRM_DEPTH: u32 = 100;
40 pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
41         assert!(chain.does_match_tx(tx));
42         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
43         chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
44         for i in 2..CHAN_CONFIRM_DEPTH {
45                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
46                 chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
47         }
48 }
49
50 pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
51         let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
52         chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
53         for i in 2..depth + 1 {
54                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
55                 chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
56         }
57         header.bitcoin_hash()
58 }
59
60 pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
61         let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
62         let mut blocks = Vec::new();
63         for _ in 0..depth {
64                 blocks.push(Block { header, txdata: Vec::new() });
65                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
66         }
67         let mut height = height;
68         for block in blocks.pop() {
69                 chain.block_disconnected(&block.header, height);
70                 height -= 1;
71         }
72 }
73
74 pub struct Node {
75         pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
76         pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
77         pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
78         pub keys_manager: Arc<test_utils::TestKeysInterface>,
79         pub node: Arc<ChannelManager>,
80         pub router: Router,
81         pub node_seed: [u8; 32],
82         pub network_payment_count: Rc<RefCell<u8>>,
83         pub network_chan_count: Rc<RefCell<u32>>,
84 }
85 impl Drop for Node {
86         fn drop(&mut self) {
87                 if !::std::thread::panicking() {
88                         // Check that we processed all pending events
89                         assert!(self.node.get_and_clear_pending_msg_events().is_empty());
90                         assert!(self.node.get_and_clear_pending_events().is_empty());
91                         assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
92                 }
93         }
94 }
95
96 pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
97         create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001)
98 }
99
100 pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
101         let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat);
102         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
103         (announcement, as_update, bs_update, channel_id, tx)
104 }
105
106 macro_rules! get_revoke_commit_msgs {
107         ($node: expr, $node_id: expr) => {
108                 {
109                         let events = $node.node.get_and_clear_pending_msg_events();
110                         assert_eq!(events.len(), 2);
111                         (match events[0] {
112                                 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
113                                         assert_eq!(*node_id, $node_id);
114                                         (*msg).clone()
115                                 },
116                                 _ => panic!("Unexpected event"),
117                         }, match events[1] {
118                                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
119                                         assert_eq!(*node_id, $node_id);
120                                         assert!(updates.update_add_htlcs.is_empty());
121                                         assert!(updates.update_fulfill_htlcs.is_empty());
122                                         assert!(updates.update_fail_htlcs.is_empty());
123                                         assert!(updates.update_fail_malformed_htlcs.is_empty());
124                                         assert!(updates.update_fee.is_none());
125                                         updates.commitment_signed.clone()
126                                 },
127                                 _ => panic!("Unexpected event"),
128                         })
129                 }
130         }
131 }
132
133 macro_rules! get_event_msg {
134         ($node: expr, $event_type: path, $node_id: expr) => {
135                 {
136                         let events = $node.node.get_and_clear_pending_msg_events();
137                         assert_eq!(events.len(), 1);
138                         match events[0] {
139                                 $event_type { ref node_id, ref msg } => {
140                                         assert_eq!(*node_id, $node_id);
141                                         (*msg).clone()
142                                 },
143                                 _ => panic!("Unexpected event"),
144                         }
145                 }
146         }
147 }
148
149 macro_rules! get_htlc_update_msgs {
150         ($node: expr, $node_id: expr) => {
151                 {
152                         let events = $node.node.get_and_clear_pending_msg_events();
153                         assert_eq!(events.len(), 1);
154                         match events[0] {
155                                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
156                                         assert_eq!(*node_id, $node_id);
157                                         (*updates).clone()
158                                 },
159                                 _ => panic!("Unexpected event"),
160                         }
161                 }
162         }
163 }
164
165 macro_rules! get_feerate {
166         ($node: expr, $channel_id: expr) => {
167                 {
168                         let chan_lock = $node.node.channel_state.lock().unwrap();
169                         let chan = chan_lock.by_id.get(&$channel_id).unwrap();
170                         chan.get_feerate()
171                 }
172         }
173 }
174
175
176 pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> Transaction {
177         node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
178         node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
179         node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
180
181         let chan_id = *node_a.network_chan_count.borrow();
182         let tx;
183         let funding_output;
184
185         let events_2 = node_a.node.get_and_clear_pending_events();
186         assert_eq!(events_2.len(), 1);
187         match events_2[0] {
188                 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
189                         assert_eq!(*channel_value_satoshis, channel_value);
190                         assert_eq!(user_channel_id, 42);
191
192                         tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
193                                 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
194                         }]};
195                         funding_output = OutPoint::new(tx.txid(), 0);
196
197                         node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
198                         let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
199                         assert_eq!(added_monitors.len(), 1);
200                         assert_eq!(added_monitors[0].0, funding_output);
201                         added_monitors.clear();
202                 },
203                 _ => panic!("Unexpected event"),
204         }
205
206         node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id())).unwrap();
207         {
208                 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
209                 assert_eq!(added_monitors.len(), 1);
210                 assert_eq!(added_monitors[0].0, funding_output);
211                 added_monitors.clear();
212         }
213
214         node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id())).unwrap();
215         {
216                 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
217                 assert_eq!(added_monitors.len(), 1);
218                 assert_eq!(added_monitors[0].0, funding_output);
219                 added_monitors.clear();
220         }
221
222         let events_4 = node_a.node.get_and_clear_pending_events();
223         assert_eq!(events_4.len(), 1);
224         match events_4[0] {
225                 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
226                         assert_eq!(user_channel_id, 42);
227                         assert_eq!(*funding_txo, funding_output);
228                 },
229                 _ => panic!("Unexpected event"),
230         };
231
232         tx
233 }
234
235 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
236         confirm_transaction(&node_b.chain_monitor, &tx, tx.version);
237         node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingLocked, node_a.node.get_our_node_id())).unwrap();
238
239         let channel_id;
240
241         confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
242         let events_6 = node_a.node.get_and_clear_pending_msg_events();
243         assert_eq!(events_6.len(), 2);
244         ((match events_6[0] {
245                 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
246                         channel_id = msg.channel_id.clone();
247                         assert_eq!(*node_id, node_b.node.get_our_node_id());
248                         msg.clone()
249                 },
250                 _ => panic!("Unexpected event"),
251         }, match events_6[1] {
252                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
253                         assert_eq!(*node_id, node_b.node.get_our_node_id());
254                         msg.clone()
255                 },
256                 _ => panic!("Unexpected event"),
257         }), channel_id)
258 }
259
260 pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
261         let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat);
262         let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
263         (msgs, chan_id, tx)
264 }
265
266 pub fn create_chan_between_nodes_with_value_b(node_a: &Node, node_b: &Node, as_funding_msgs: &(msgs::FundingLocked, msgs::AnnouncementSignatures)) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
267         node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
268         let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
269         node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
270
271         let events_7 = node_b.node.get_and_clear_pending_msg_events();
272         assert_eq!(events_7.len(), 1);
273         let (announcement, bs_update) = match events_7[0] {
274                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
275                         (msg, update_msg)
276                 },
277                 _ => panic!("Unexpected event"),
278         };
279
280         node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
281         let events_8 = node_a.node.get_and_clear_pending_msg_events();
282         assert_eq!(events_8.len(), 1);
283         let as_update = match events_8[0] {
284                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
285                         assert!(*announcement == *msg);
286                         assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
287                         assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
288                         update_msg
289                 },
290                 _ => panic!("Unexpected event"),
291         };
292
293         *node_a.network_chan_count.borrow_mut() += 1;
294
295         ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
296 }
297
298 pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
299         create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001)
300 }
301
302 pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
303         let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat);
304         for node in nodes {
305                 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
306                 node.router.handle_channel_update(&chan_announcement.1).unwrap();
307                 node.router.handle_channel_update(&chan_announcement.2).unwrap();
308         }
309         (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
310 }
311
312 macro_rules! check_spends {
313         ($tx: expr, $spends_tx: expr) => {
314                 {
315                         let mut funding_tx_map = HashMap::new();
316                         let spends_tx = $spends_tx;
317                         funding_tx_map.insert(spends_tx.txid(), spends_tx);
318                         $tx.verify(&funding_tx_map).unwrap();
319                 }
320         }
321 }
322
323 macro_rules! get_closing_signed_broadcast {
324         ($node: expr, $dest_pubkey: expr) => {
325                 {
326                         let events = $node.get_and_clear_pending_msg_events();
327                         assert!(events.len() == 1 || events.len() == 2);
328                         (match events[events.len() - 1] {
329                                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
330                                         assert_eq!(msg.contents.flags & 2, 2);
331                                         msg.clone()
332                                 },
333                                 _ => panic!("Unexpected event"),
334                         }, if events.len() == 2 {
335                                 match events[0] {
336                                         MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
337                                                 assert_eq!(*node_id, $dest_pubkey);
338                                                 Some(msg.clone())
339                                         },
340                                         _ => panic!("Unexpected event"),
341                                 }
342                         } else { None })
343                 }
344         }
345 }
346
347 macro_rules! check_closed_broadcast {
348         ($node: expr) => {{
349                 let events = $node.node.get_and_clear_pending_msg_events();
350                 assert_eq!(events.len(), 1);
351                 match events[0] {
352                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
353                                 assert_eq!(msg.contents.flags & 2, 2);
354                         },
355                         _ => panic!("Unexpected event"),
356                 }
357         }}
358 }
359
360 pub fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
361         let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) };
362         let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
363         let (tx_a, tx_b);
364
365         node_a.close_channel(channel_id).unwrap();
366         node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
367
368         let events_1 = node_b.get_and_clear_pending_msg_events();
369         assert!(events_1.len() >= 1);
370         let shutdown_b = match events_1[0] {
371                 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
372                         assert_eq!(node_id, &node_a.get_our_node_id());
373                         msg.clone()
374                 },
375                 _ => panic!("Unexpected event"),
376         };
377
378         let closing_signed_b = if !close_inbound_first {
379                 assert_eq!(events_1.len(), 1);
380                 None
381         } else {
382                 Some(match events_1[1] {
383                         MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
384                                 assert_eq!(node_id, &node_a.get_our_node_id());
385                                 msg.clone()
386                         },
387                         _ => panic!("Unexpected event"),
388                 })
389         };
390
391         node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
392         let (as_update, bs_update) = if close_inbound_first {
393                 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
394                 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
395                 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
396                 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
397                 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
398
399                 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
400                 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
401                 assert!(none_b.is_none());
402                 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
403                 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
404                 (as_update, bs_update)
405         } else {
406                 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
407
408                 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
409                 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
410                 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
411                 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
412
413                 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
414                 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
415                 assert!(none_a.is_none());
416                 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
417                 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
418                 (as_update, bs_update)
419         };
420         assert_eq!(tx_a, tx_b);
421         check_spends!(tx_a, funding_tx);
422
423         (as_update, bs_update, tx_a)
424 }
425
426 pub struct SendEvent {
427         pub node_id: PublicKey,
428         pub msgs: Vec<msgs::UpdateAddHTLC>,
429         pub commitment_msg: msgs::CommitmentSigned,
430 }
431 impl SendEvent {
432         pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
433                 assert!(updates.update_fulfill_htlcs.is_empty());
434                 assert!(updates.update_fail_htlcs.is_empty());
435                 assert!(updates.update_fail_malformed_htlcs.is_empty());
436                 assert!(updates.update_fee.is_none());
437                 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
438         }
439
440         pub fn from_event(event: MessageSendEvent) -> SendEvent {
441                 match event {
442                         MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
443                         _ => panic!("Unexpected event type!"),
444                 }
445         }
446
447         pub fn from_node(node: &Node) -> SendEvent {
448                 let mut events = node.node.get_and_clear_pending_msg_events();
449                 assert_eq!(events.len(), 1);
450                 SendEvent::from_event(events.pop().unwrap())
451         }
452 }
453
454 macro_rules! check_added_monitors {
455         ($node: expr, $count: expr) => {
456                 {
457                         let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
458                         assert_eq!(added_monitors.len(), $count);
459                         added_monitors.clear();
460                 }
461         }
462 }
463
464 macro_rules! commitment_signed_dance {
465         ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
466                 {
467                         check_added_monitors!($node_a, 0);
468                         assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
469                         $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
470                         check_added_monitors!($node_a, 1);
471                         commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
472                 }
473         };
474         ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
475                 {
476                         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
477                         check_added_monitors!($node_b, 0);
478                         assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
479                         $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
480                         assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
481                         check_added_monitors!($node_b, 1);
482                         $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
483                         let (bs_revoke_and_ack, extra_msg_option) = {
484                                 let events = $node_b.node.get_and_clear_pending_msg_events();
485                                 assert!(events.len() <= 2);
486                                 (match events[0] {
487                                         MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
488                                                 assert_eq!(*node_id, $node_a.node.get_our_node_id());
489                                                 (*msg).clone()
490                                         },
491                                         _ => panic!("Unexpected event"),
492                                 }, events.get(1).map(|e| e.clone()))
493                         };
494                         check_added_monitors!($node_b, 1);
495                         if $fail_backwards {
496                                 assert!($node_a.node.get_and_clear_pending_events().is_empty());
497                                 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
498                         }
499                         (extra_msg_option, bs_revoke_and_ack)
500                 }
501         };
502         ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
503                 {
504                         check_added_monitors!($node_a, 0);
505                         assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
506                         $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
507                         check_added_monitors!($node_a, 1);
508                         let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
509                         assert!(extra_msg_option.is_none());
510                         bs_revoke_and_ack
511                 }
512         };
513         ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
514                 {
515                         let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
516                         $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
517                         check_added_monitors!($node_a, 1);
518                         extra_msg_option
519                 }
520         };
521         ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
522                 {
523                         assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
524                 }
525         };
526         ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
527                 {
528                         commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
529                         if $fail_backwards {
530                                 expect_pending_htlcs_forwardable!($node_a);
531                                 check_added_monitors!($node_a, 1);
532
533                                 let channel_state = $node_a.node.channel_state.lock().unwrap();
534                                 assert_eq!(channel_state.pending_msg_events.len(), 1);
535                                 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
536                                         assert_ne!(*node_id, $node_b.node.get_our_node_id());
537                                 } else { panic!("Unexpected event"); }
538                         } else {
539                                 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
540                         }
541                 }
542         }
543 }
544
545 macro_rules! get_payment_preimage_hash {
546         ($node: expr) => {
547                 {
548                         let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
549                         *$node.network_payment_count.borrow_mut() += 1;
550                         let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
551                         (payment_preimage, payment_hash)
552                 }
553         }
554 }
555
556 macro_rules! expect_pending_htlcs_forwardable {
557         ($node: expr) => {{
558                 let events = $node.node.get_and_clear_pending_events();
559                 assert_eq!(events.len(), 1);
560                 match events[0] {
561                         Event::PendingHTLCsForwardable { .. } => { },
562                         _ => panic!("Unexpected event"),
563                 };
564                 let node_ref: &Node = &$node;
565                 node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now();
566                 $node.node.process_pending_htlc_forwards();
567         }}
568 }
569
570 macro_rules! expect_payment_received {
571         ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
572                 let events = $node.node.get_and_clear_pending_events();
573                 assert_eq!(events.len(), 1);
574                 match events[0] {
575                         Event::PaymentReceived { ref payment_hash, amt } => {
576                                 assert_eq!($expected_payment_hash, *payment_hash);
577                                 assert_eq!($expected_recv_value, amt);
578                         },
579                         _ => panic!("Unexpected event"),
580                 }
581         }
582 }
583
584 macro_rules! expect_payment_sent {
585         ($node: expr, $expected_payment_preimage: expr) => {
586                 let events = $node.node.get_and_clear_pending_events();
587                 assert_eq!(events.len(), 1);
588                 match events[0] {
589                         Event::PaymentSent { ref payment_preimage } => {
590                                 assert_eq!($expected_payment_preimage, *payment_preimage);
591                         },
592                         _ => panic!("Unexpected event"),
593                 }
594         }
595 }
596
597 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
598         let mut payment_event = {
599                 origin_node.node.send_payment(route, our_payment_hash).unwrap();
600                 check_added_monitors!(origin_node, 1);
601
602                 let mut events = origin_node.node.get_and_clear_pending_msg_events();
603                 assert_eq!(events.len(), 1);
604                 SendEvent::from_event(events.remove(0))
605         };
606         let mut prev_node = origin_node;
607
608         for (idx, &node) in expected_route.iter().enumerate() {
609                 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
610
611                 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
612                 check_added_monitors!(node, 0);
613                 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
614
615                 expect_pending_htlcs_forwardable!(node);
616
617                 if idx == expected_route.len() - 1 {
618                         let events_2 = node.node.get_and_clear_pending_events();
619                         assert_eq!(events_2.len(), 1);
620                         match events_2[0] {
621                                 Event::PaymentReceived { ref payment_hash, amt } => {
622                                         assert_eq!(our_payment_hash, *payment_hash);
623                                         assert_eq!(amt, recv_value);
624                                 },
625                                 _ => panic!("Unexpected event"),
626                         }
627                 } else {
628                         let mut events_2 = node.node.get_and_clear_pending_msg_events();
629                         assert_eq!(events_2.len(), 1);
630                         check_added_monitors!(node, 1);
631                         payment_event = SendEvent::from_event(events_2.remove(0));
632                         assert_eq!(payment_event.msgs.len(), 1);
633                 }
634
635                 prev_node = node;
636         }
637 }
638
639 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
640         let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
641         send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
642         (our_payment_preimage, our_payment_hash)
643 }
644
645 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
646         assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
647         check_added_monitors!(expected_route.last().unwrap(), 1);
648
649         let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
650         let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
651         macro_rules! get_next_msgs {
652                 ($node: expr) => {
653                         {
654                                 let events = $node.node.get_and_clear_pending_msg_events();
655                                 assert_eq!(events.len(), 1);
656                                 match events[0] {
657                                         MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
658                                                 assert!(update_add_htlcs.is_empty());
659                                                 assert_eq!(update_fulfill_htlcs.len(), 1);
660                                                 assert!(update_fail_htlcs.is_empty());
661                                                 assert!(update_fail_malformed_htlcs.is_empty());
662                                                 assert!(update_fee.is_none());
663                                                 expected_next_node = node_id.clone();
664                                                 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
665                                         },
666                                         _ => panic!("Unexpected event"),
667                                 }
668                         }
669                 }
670         }
671
672         macro_rules! last_update_fulfill_dance {
673                 ($node: expr, $prev_node: expr) => {
674                         {
675                                 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
676                                 check_added_monitors!($node, 0);
677                                 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
678                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
679                         }
680                 }
681         }
682         macro_rules! mid_update_fulfill_dance {
683                 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
684                         {
685                                 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
686                                 check_added_monitors!($node, 1);
687                                 let new_next_msgs = if $new_msgs {
688                                         get_next_msgs!($node)
689                                 } else {
690                                         assert!($node.node.get_and_clear_pending_msg_events().is_empty());
691                                         None
692                                 };
693                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
694                                 next_msgs = new_next_msgs;
695                         }
696                 }
697         }
698
699         let mut prev_node = expected_route.last().unwrap();
700         for (idx, node) in expected_route.iter().rev().enumerate() {
701                 assert_eq!(expected_next_node, node.node.get_our_node_id());
702                 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
703                 if next_msgs.is_some() {
704                         mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
705                 } else if update_next_msgs {
706                         next_msgs = get_next_msgs!(node);
707                 } else {
708                         assert!(node.node.get_and_clear_pending_msg_events().is_empty());
709                 }
710                 if !skip_last && idx == expected_route.len() - 1 {
711                         assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
712                 }
713
714                 prev_node = node;
715         }
716
717         if !skip_last {
718                 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
719                 expect_payment_sent!(origin_node, our_payment_preimage);
720         }
721 }
722
723 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
724         claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
725 }
726
727 pub const TEST_FINAL_CLTV: u32 = 32;
728
729 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
730         let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
731         assert_eq!(route.hops.len(), expected_route.len());
732         for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
733                 assert_eq!(hop.pubkey, node.node.get_our_node_id());
734         }
735
736         send_along_route(origin_node, route, expected_route, recv_value)
737 }
738
739 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
740         let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
741         assert_eq!(route.hops.len(), expected_route.len());
742         for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
743                 assert_eq!(hop.pubkey, node.node.get_our_node_id());
744         }
745
746         let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
747
748         let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
749         match err {
750                 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
751                 _ => panic!("Unknown error variants"),
752         };
753 }
754
755 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
756         let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
757         claim_payment(&origin, expected_route, our_payment_preimage);
758 }
759
760 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
761         assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
762         expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
763         check_added_monitors!(expected_route.last().unwrap(), 1);
764
765         let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
766         macro_rules! update_fail_dance {
767                 ($node: expr, $prev_node: expr, $last_node: expr) => {
768                         {
769                                 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
770                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
771                                 if skip_last && $last_node {
772                                         expect_pending_htlcs_forwardable!($node);
773                                 }
774                         }
775                 }
776         }
777
778         let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
779         let mut prev_node = expected_route.last().unwrap();
780         for (idx, node) in expected_route.iter().rev().enumerate() {
781                 assert_eq!(expected_next_node, node.node.get_our_node_id());
782                 if next_msgs.is_some() {
783                         // We may be the "last node" for the purpose of the commitment dance if we're
784                         // skipping the last node (implying it is disconnected) and we're the
785                         // second-to-last node!
786                         update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
787                 }
788
789                 let events = node.node.get_and_clear_pending_msg_events();
790                 if !skip_last || idx != expected_route.len() - 1 {
791                         assert_eq!(events.len(), 1);
792                         match events[0] {
793                                 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
794                                         assert!(update_add_htlcs.is_empty());
795                                         assert!(update_fulfill_htlcs.is_empty());
796                                         assert_eq!(update_fail_htlcs.len(), 1);
797                                         assert!(update_fail_malformed_htlcs.is_empty());
798                                         assert!(update_fee.is_none());
799                                         expected_next_node = node_id.clone();
800                                         next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
801                                 },
802                                 _ => panic!("Unexpected event"),
803                         }
804                 } else {
805                         assert!(events.is_empty());
806                 }
807                 if !skip_last && idx == expected_route.len() - 1 {
808                         assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
809                 }
810
811                 prev_node = node;
812         }
813
814         if !skip_last {
815                 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
816
817                 let events = origin_node.node.get_and_clear_pending_events();
818                 assert_eq!(events.len(), 1);
819                 match events[0] {
820                         Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
821                                 assert_eq!(payment_hash, our_payment_hash);
822                                 assert!(rejected_by_dest);
823                         },
824                         _ => panic!("Unexpected event"),
825                 }
826         }
827 }
828
829 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
830         fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
831 }
832
833 pub fn create_network(node_count: usize) -> Vec<Node> {
834         let mut nodes = Vec::new();
835         let mut rng = thread_rng();
836         let secp_ctx = Secp256k1::new();
837
838         let chan_count = Rc::new(RefCell::new(0));
839         let payment_count = Rc::new(RefCell::new(0));
840
841         for i in 0..node_count {
842                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
843                 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
844                 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
845                 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
846                 let mut seed = [0; 32];
847                 rng.fill_bytes(&mut seed);
848                 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
849                 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
850                 let mut config = UserConfig::new();
851                 config.channel_options.announced_channel = true;
852                 config.peer_channel_config_limits.force_announced_channel_preference = false;
853                 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
854                 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
855                 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
856                         network_payment_count: payment_count.clone(),
857                         network_chan_count: chan_count.clone(),
858                 });
859         }
860
861         nodes
862 }
863
864 #[derive(PartialEq)]
865 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
866 /// Tests that the given node has broadcast transactions for the given Channel
867 ///
868 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
869 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
870 /// broadcast and the revoked outputs were claimed.
871 ///
872 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
873 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
874 ///
875 /// All broadcast transactions must be accounted for in one of the above three types of we'll
876 /// also fail.
877 pub fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
878         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
879         assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
880
881         let mut res = Vec::with_capacity(2);
882         node_txn.retain(|tx| {
883                 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
884                         check_spends!(tx, chan.3.clone());
885                         if commitment_tx.is_none() {
886                                 res.push(tx.clone());
887                         }
888                         false
889                 } else { true }
890         });
891         if let Some(explicit_tx) = commitment_tx {
892                 res.push(explicit_tx.clone());
893         }
894
895         assert_eq!(res.len(), 1);
896
897         if has_htlc_tx != HTLCType::NONE {
898                 node_txn.retain(|tx| {
899                         if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
900                                 check_spends!(tx, res[0].clone());
901                                 if has_htlc_tx == HTLCType::TIMEOUT {
902                                         assert!(tx.lock_time != 0);
903                                 } else {
904                                         assert!(tx.lock_time == 0);
905                                 }
906                                 res.push(tx.clone());
907                                 false
908                         } else { true }
909                 });
910                 assert!(res.len() == 2 || res.len() == 3);
911                 if res.len() == 3 {
912                         assert_eq!(res[1], res[2]);
913                 }
914         }
915
916         assert!(node_txn.is_empty());
917         res
918 }
919
920 /// Tests that the given node has broadcast a claim transaction against the provided revoked
921 /// HTLC transaction.
922 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
923         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
924         assert_eq!(node_txn.len(), 1);
925         node_txn.retain(|tx| {
926                 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
927                         check_spends!(tx, revoked_tx.clone());
928                         false
929                 } else { true }
930         });
931         assert!(node_txn.is_empty());
932 }
933
934 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
935         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
936
937         assert!(node_txn.len() >= 1);
938         assert_eq!(node_txn[0].input.len(), 1);
939         let mut found_prev = false;
940
941         for tx in prev_txn {
942                 if node_txn[0].input[0].previous_output.txid == tx.txid() {
943                         check_spends!(node_txn[0], tx.clone());
944                         assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
945                         assert_eq!(tx.input.len(), 1); // must spend a commitment tx
946
947                         found_prev = true;
948                         break;
949                 }
950         }
951         assert!(found_prev);
952
953         let mut res = Vec::new();
954         mem::swap(&mut *node_txn, &mut res);
955         res
956 }
957
958 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
959         let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
960         assert_eq!(events_1.len(), 1);
961         let as_update = match events_1[0] {
962                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
963                         msg.clone()
964                 },
965                 _ => panic!("Unexpected event"),
966         };
967
968         let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
969         assert_eq!(events_2.len(), 1);
970         let bs_update = match events_2[0] {
971                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
972                         msg.clone()
973                 },
974                 _ => panic!("Unexpected event"),
975         };
976
977         for node in nodes {
978                 node.router.handle_channel_update(&as_update).unwrap();
979                 node.router.handle_channel_update(&bs_update).unwrap();
980         }
981 }
982
983 macro_rules! get_channel_value_stat {
984         ($node: expr, $channel_id: expr) => {{
985                 let chan_lock = $node.node.channel_state.lock().unwrap();
986                 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
987                 chan.get_value_stat()
988         }}
989 }
990
991 macro_rules! get_chan_reestablish_msgs {
992         ($src_node: expr, $dst_node: expr) => {
993                 {
994                         let mut res = Vec::with_capacity(1);
995                         for msg in $src_node.node.get_and_clear_pending_msg_events() {
996                                 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
997                                         assert_eq!(*node_id, $dst_node.node.get_our_node_id());
998                                         res.push(msg.clone());
999                                 } else {
1000                                         panic!("Unexpected event")
1001                                 }
1002                         }
1003                         res
1004                 }
1005         }
1006 }
1007
1008 macro_rules! handle_chan_reestablish_msgs {
1009         ($src_node: expr, $dst_node: expr) => {
1010                 {
1011                         let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1012                         let mut idx = 0;
1013                         let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1014                                 idx += 1;
1015                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1016                                 Some(msg.clone())
1017                         } else {
1018                                 None
1019                         };
1020
1021                         let mut revoke_and_ack = None;
1022                         let mut commitment_update = None;
1023                         let order = if let Some(ev) = msg_events.get(idx) {
1024                                 idx += 1;
1025                                 match ev {
1026                                         &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1027                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1028                                                 revoke_and_ack = Some(msg.clone());
1029                                                 RAACommitmentOrder::RevokeAndACKFirst
1030                                         },
1031                                         &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1032                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1033                                                 commitment_update = Some(updates.clone());
1034                                                 RAACommitmentOrder::CommitmentFirst
1035                                         },
1036                                         _ => panic!("Unexpected event"),
1037                                 }
1038                         } else {
1039                                 RAACommitmentOrder::CommitmentFirst
1040                         };
1041
1042                         if let Some(ev) = msg_events.get(idx) {
1043                                 match ev {
1044                                         &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1045                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1046                                                 assert!(revoke_and_ack.is_none());
1047                                                 revoke_and_ack = Some(msg.clone());
1048                                         },
1049                                         &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1050                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1051                                                 assert!(commitment_update.is_none());
1052                                                 commitment_update = Some(updates.clone());
1053                                         },
1054                                         _ => panic!("Unexpected event"),
1055                                 }
1056                         }
1057
1058                         (funding_locked, revoke_and_ack, commitment_update, order)
1059                 }
1060         }
1061 }
1062
1063 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1064 /// for claims/fails they are separated out.
1065 pub fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
1066         node_a.node.peer_connected(&node_b.node.get_our_node_id());
1067         let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1068         node_b.node.peer_connected(&node_a.node.get_our_node_id());
1069         let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1070
1071         if send_funding_locked.0 {
1072                 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1073                 // from b
1074                 for reestablish in reestablish_1.iter() {
1075                         assert_eq!(reestablish.next_remote_commitment_number, 0);
1076                 }
1077         }
1078         if send_funding_locked.1 {
1079                 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1080                 // from a
1081                 for reestablish in reestablish_2.iter() {
1082                         assert_eq!(reestablish.next_remote_commitment_number, 0);
1083                 }
1084         }
1085         if send_funding_locked.0 || send_funding_locked.1 {
1086                 // If we expect any funding_locked's, both sides better have set
1087                 // next_local_commitment_number to 1
1088                 for reestablish in reestablish_1.iter() {
1089                         assert_eq!(reestablish.next_local_commitment_number, 1);
1090                 }
1091                 for reestablish in reestablish_2.iter() {
1092                         assert_eq!(reestablish.next_local_commitment_number, 1);
1093                 }
1094         }
1095
1096         let mut resp_1 = Vec::new();
1097         for msg in reestablish_1 {
1098                 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1099                 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1100         }
1101         if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1102                 check_added_monitors!(node_b, 1);
1103         } else {
1104                 check_added_monitors!(node_b, 0);
1105         }
1106
1107         let mut resp_2 = Vec::new();
1108         for msg in reestablish_2 {
1109                 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1110                 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1111         }
1112         if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1113                 check_added_monitors!(node_a, 1);
1114         } else {
1115                 check_added_monitors!(node_a, 0);
1116         }
1117
1118         // We don't yet support both needing updates, as that would require a different commitment dance:
1119         assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1120                         (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1121
1122         for chan_msgs in resp_1.drain(..) {
1123                 if send_funding_locked.0 {
1124                         node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1125                         let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1126                         if !announcement_event.is_empty() {
1127                                 assert_eq!(announcement_event.len(), 1);
1128                                 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1129                                         //TODO: Test announcement_sigs re-sending
1130                                 } else { panic!("Unexpected event!"); }
1131                         }
1132                 } else {
1133                         assert!(chan_msgs.0.is_none());
1134                 }
1135                 if pending_raa.0 {
1136                         assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1137                         node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1138                         assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1139                         check_added_monitors!(node_a, 1);
1140                 } else {
1141                         assert!(chan_msgs.1.is_none());
1142                 }
1143                 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1144                         let commitment_update = chan_msgs.2.unwrap();
1145                         if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1146                                 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1147                         } else {
1148                                 assert!(commitment_update.update_add_htlcs.is_empty());
1149                         }
1150                         assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1151                         assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1152                         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1153                         for update_add in commitment_update.update_add_htlcs {
1154                                 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1155                         }
1156                         for update_fulfill in commitment_update.update_fulfill_htlcs {
1157                                 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1158                         }
1159                         for update_fail in commitment_update.update_fail_htlcs {
1160                                 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1161                         }
1162
1163                         if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1164                                 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1165                         } else {
1166                                 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1167                                 check_added_monitors!(node_a, 1);
1168                                 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1169                                 // No commitment_signed so get_event_msg's assert(len == 1) passes
1170                                 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1171                                 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1172                                 check_added_monitors!(node_b, 1);
1173                         }
1174                 } else {
1175                         assert!(chan_msgs.2.is_none());
1176                 }
1177         }
1178
1179         for chan_msgs in resp_2.drain(..) {
1180                 if send_funding_locked.1 {
1181                         node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1182                         let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1183                         if !announcement_event.is_empty() {
1184                                 assert_eq!(announcement_event.len(), 1);
1185                                 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1186                                         //TODO: Test announcement_sigs re-sending
1187                                 } else { panic!("Unexpected event!"); }
1188                         }
1189                 } else {
1190                         assert!(chan_msgs.0.is_none());
1191                 }
1192                 if pending_raa.1 {
1193                         assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1194                         node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1195                         assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1196                         check_added_monitors!(node_b, 1);
1197                 } else {
1198                         assert!(chan_msgs.1.is_none());
1199                 }
1200                 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1201                         let commitment_update = chan_msgs.2.unwrap();
1202                         if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1203                                 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1204                         }
1205                         assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1206                         assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1207                         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1208                         for update_add in commitment_update.update_add_htlcs {
1209                                 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1210                         }
1211                         for update_fulfill in commitment_update.update_fulfill_htlcs {
1212                                 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1213                         }
1214                         for update_fail in commitment_update.update_fail_htlcs {
1215                                 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1216                         }
1217
1218                         if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1219                                 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1220                         } else {
1221                                 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1222                                 check_added_monitors!(node_b, 1);
1223                                 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1224                                 // No commitment_signed so get_event_msg's assert(len == 1) passes
1225                                 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1226                                 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1227                                 check_added_monitors!(node_a, 1);
1228                         }
1229                 } else {
1230                         assert!(chan_msgs.2.is_none());
1231                 }
1232         }
1233 }