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