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