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