Merge pull request #356 from TheBlueMatt/2019-07-more-chanmon-fuzz
[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
161 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 {
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(), a_flags, &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(), b_flags, &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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> ((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, a_flags, b_flags);
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
284         create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (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, a_flags, b_flags);
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                 $node.node.process_pending_htlc_forwards();
550         }}
551 }
552
553 macro_rules! expect_payment_received {
554         ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
555                 let events = $node.node.get_and_clear_pending_events();
556                 assert_eq!(events.len(), 1);
557                 match events[0] {
558                         Event::PaymentReceived { ref payment_hash, amt } => {
559                                 assert_eq!($expected_payment_hash, *payment_hash);
560                                 assert_eq!($expected_recv_value, amt);
561                         },
562                         _ => panic!("Unexpected event"),
563                 }
564         }
565 }
566
567 macro_rules! expect_payment_sent {
568         ($node: expr, $expected_payment_preimage: expr) => {
569                 let events = $node.node.get_and_clear_pending_events();
570                 assert_eq!(events.len(), 1);
571                 match events[0] {
572                         Event::PaymentSent { ref payment_preimage } => {
573                                 assert_eq!($expected_payment_preimage, *payment_preimage);
574                         },
575                         _ => panic!("Unexpected event"),
576                 }
577         }
578 }
579
580 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
581         let mut payment_event = {
582                 origin_node.node.send_payment(route, our_payment_hash).unwrap();
583                 check_added_monitors!(origin_node, 1);
584
585                 let mut events = origin_node.node.get_and_clear_pending_msg_events();
586                 assert_eq!(events.len(), 1);
587                 SendEvent::from_event(events.remove(0))
588         };
589         let mut prev_node = origin_node;
590
591         for (idx, &node) in expected_route.iter().enumerate() {
592                 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
593
594                 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
595                 check_added_monitors!(node, 0);
596                 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
597
598                 expect_pending_htlcs_forwardable!(node);
599
600                 if idx == expected_route.len() - 1 {
601                         let events_2 = node.node.get_and_clear_pending_events();
602                         assert_eq!(events_2.len(), 1);
603                         match events_2[0] {
604                                 Event::PaymentReceived { ref payment_hash, amt } => {
605                                         assert_eq!(our_payment_hash, *payment_hash);
606                                         assert_eq!(amt, recv_value);
607                                 },
608                                 _ => panic!("Unexpected event"),
609                         }
610                 } else {
611                         let mut events_2 = node.node.get_and_clear_pending_msg_events();
612                         assert_eq!(events_2.len(), 1);
613                         check_added_monitors!(node, 1);
614                         payment_event = SendEvent::from_event(events_2.remove(0));
615                         assert_eq!(payment_event.msgs.len(), 1);
616                 }
617
618                 prev_node = node;
619         }
620 }
621
622 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
623         let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
624         send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
625         (our_payment_preimage, our_payment_hash)
626 }
627
628 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
629         assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
630         check_added_monitors!(expected_route.last().unwrap(), 1);
631
632         let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
633         let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
634         macro_rules! get_next_msgs {
635                 ($node: expr) => {
636                         {
637                                 let events = $node.node.get_and_clear_pending_msg_events();
638                                 assert_eq!(events.len(), 1);
639                                 match events[0] {
640                                         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 } } => {
641                                                 assert!(update_add_htlcs.is_empty());
642                                                 assert_eq!(update_fulfill_htlcs.len(), 1);
643                                                 assert!(update_fail_htlcs.is_empty());
644                                                 assert!(update_fail_malformed_htlcs.is_empty());
645                                                 assert!(update_fee.is_none());
646                                                 expected_next_node = node_id.clone();
647                                                 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
648                                         },
649                                         _ => panic!("Unexpected event"),
650                                 }
651                         }
652                 }
653         }
654
655         macro_rules! last_update_fulfill_dance {
656                 ($node: expr, $prev_node: expr) => {
657                         {
658                                 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
659                                 check_added_monitors!($node, 0);
660                                 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
661                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
662                         }
663                 }
664         }
665         macro_rules! mid_update_fulfill_dance {
666                 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
667                         {
668                                 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
669                                 check_added_monitors!($node, 1);
670                                 let new_next_msgs = if $new_msgs {
671                                         get_next_msgs!($node)
672                                 } else {
673                                         assert!($node.node.get_and_clear_pending_msg_events().is_empty());
674                                         None
675                                 };
676                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
677                                 next_msgs = new_next_msgs;
678                         }
679                 }
680         }
681
682         let mut prev_node = expected_route.last().unwrap();
683         for (idx, node) in expected_route.iter().rev().enumerate() {
684                 assert_eq!(expected_next_node, node.node.get_our_node_id());
685                 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
686                 if next_msgs.is_some() {
687                         mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
688                 } else if update_next_msgs {
689                         next_msgs = get_next_msgs!(node);
690                 } else {
691                         assert!(node.node.get_and_clear_pending_msg_events().is_empty());
692                 }
693                 if !skip_last && idx == expected_route.len() - 1 {
694                         assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
695                 }
696
697                 prev_node = node;
698         }
699
700         if !skip_last {
701                 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
702                 expect_payment_sent!(origin_node, our_payment_preimage);
703         }
704 }
705
706 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
707         claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
708 }
709
710 pub const TEST_FINAL_CLTV: u32 = 32;
711
712 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
713         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();
714         assert_eq!(route.hops.len(), expected_route.len());
715         for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
716                 assert_eq!(hop.pubkey, node.node.get_our_node_id());
717         }
718
719         send_along_route(origin_node, route, expected_route, recv_value)
720 }
721
722 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
723         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();
724         assert_eq!(route.hops.len(), expected_route.len());
725         for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
726                 assert_eq!(hop.pubkey, node.node.get_our_node_id());
727         }
728
729         let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
730
731         let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
732         match err {
733                 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
734                 _ => panic!("Unknown error variants"),
735         };
736 }
737
738 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
739         let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
740         claim_payment(&origin, expected_route, our_payment_preimage);
741 }
742
743 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
744         assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
745         expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
746         check_added_monitors!(expected_route.last().unwrap(), 1);
747
748         let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
749         macro_rules! update_fail_dance {
750                 ($node: expr, $prev_node: expr, $last_node: expr) => {
751                         {
752                                 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
753                                 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
754                                 if skip_last && $last_node {
755                                         expect_pending_htlcs_forwardable!($node);
756                                 }
757                         }
758                 }
759         }
760
761         let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
762         let mut prev_node = expected_route.last().unwrap();
763         for (idx, node) in expected_route.iter().rev().enumerate() {
764                 assert_eq!(expected_next_node, node.node.get_our_node_id());
765                 if next_msgs.is_some() {
766                         // We may be the "last node" for the purpose of the commitment dance if we're
767                         // skipping the last node (implying it is disconnected) and we're the
768                         // second-to-last node!
769                         update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
770                 }
771
772                 let events = node.node.get_and_clear_pending_msg_events();
773                 if !skip_last || idx != expected_route.len() - 1 {
774                         assert_eq!(events.len(), 1);
775                         match events[0] {
776                                 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 } } => {
777                                         assert!(update_add_htlcs.is_empty());
778                                         assert!(update_fulfill_htlcs.is_empty());
779                                         assert_eq!(update_fail_htlcs.len(), 1);
780                                         assert!(update_fail_malformed_htlcs.is_empty());
781                                         assert!(update_fee.is_none());
782                                         expected_next_node = node_id.clone();
783                                         next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
784                                 },
785                                 _ => panic!("Unexpected event"),
786                         }
787                 } else {
788                         assert!(events.is_empty());
789                 }
790                 if !skip_last && idx == expected_route.len() - 1 {
791                         assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
792                 }
793
794                 prev_node = node;
795         }
796
797         if !skip_last {
798                 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
799
800                 let events = origin_node.node.get_and_clear_pending_events();
801                 assert_eq!(events.len(), 1);
802                 match events[0] {
803                         Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
804                                 assert_eq!(payment_hash, our_payment_hash);
805                                 assert!(rejected_by_dest);
806                         },
807                         _ => panic!("Unexpected event"),
808                 }
809         }
810 }
811
812 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
813         fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
814 }
815
816 pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
817         let mut nodes = Vec::new();
818         let mut rng = thread_rng();
819         let secp_ctx = Secp256k1::new();
820
821         let chan_count = Rc::new(RefCell::new(0));
822         let payment_count = Rc::new(RefCell::new(0));
823
824         for i in 0..node_count {
825                 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
826                 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
827                 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
828                 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
829                 let mut seed = [0; 32];
830                 rng.fill_bytes(&mut seed);
831                 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
832                 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
833                 let mut default_config = UserConfig::new();
834                 default_config.channel_options.announced_channel = true;
835                 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
836                 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();
837                 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
838                 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
839                         network_payment_count: payment_count.clone(),
840                         network_chan_count: chan_count.clone(),
841                 });
842         }
843
844         nodes
845 }
846
847 #[derive(PartialEq)]
848 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
849 /// Tests that the given node has broadcast transactions for the given Channel
850 ///
851 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
852 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
853 /// broadcast and the revoked outputs were claimed.
854 ///
855 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
856 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
857 ///
858 /// All broadcast transactions must be accounted for in one of the above three types of we'll
859 /// also fail.
860 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> {
861         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
862         assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
863
864         let mut res = Vec::with_capacity(2);
865         node_txn.retain(|tx| {
866                 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
867                         check_spends!(tx, chan.3.clone());
868                         if commitment_tx.is_none() {
869                                 res.push(tx.clone());
870                         }
871                         false
872                 } else { true }
873         });
874         if let Some(explicit_tx) = commitment_tx {
875                 res.push(explicit_tx.clone());
876         }
877
878         assert_eq!(res.len(), 1);
879
880         if has_htlc_tx != HTLCType::NONE {
881                 node_txn.retain(|tx| {
882                         if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
883                                 check_spends!(tx, res[0].clone());
884                                 if has_htlc_tx == HTLCType::TIMEOUT {
885                                         assert!(tx.lock_time != 0);
886                                 } else {
887                                         assert!(tx.lock_time == 0);
888                                 }
889                                 res.push(tx.clone());
890                                 false
891                         } else { true }
892                 });
893                 assert!(res.len() == 2 || res.len() == 3);
894                 if res.len() == 3 {
895                         assert_eq!(res[1], res[2]);
896                 }
897         }
898
899         assert!(node_txn.is_empty());
900         res
901 }
902
903 /// Tests that the given node has broadcast a claim transaction against the provided revoked
904 /// HTLC transaction.
905 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
906         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
907         assert_eq!(node_txn.len(), 1);
908         node_txn.retain(|tx| {
909                 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
910                         check_spends!(tx, revoked_tx.clone());
911                         false
912                 } else { true }
913         });
914         assert!(node_txn.is_empty());
915 }
916
917 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
918         let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
919
920         assert!(node_txn.len() >= 1);
921         assert_eq!(node_txn[0].input.len(), 1);
922         let mut found_prev = false;
923
924         for tx in prev_txn {
925                 if node_txn[0].input[0].previous_output.txid == tx.txid() {
926                         check_spends!(node_txn[0], tx.clone());
927                         assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
928                         assert_eq!(tx.input.len(), 1); // must spend a commitment tx
929
930                         found_prev = true;
931                         break;
932                 }
933         }
934         assert!(found_prev);
935
936         let mut res = Vec::new();
937         mem::swap(&mut *node_txn, &mut res);
938         res
939 }
940
941 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
942         let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
943         assert_eq!(events_1.len(), 1);
944         let as_update = match events_1[0] {
945                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
946                         msg.clone()
947                 },
948                 _ => panic!("Unexpected event"),
949         };
950
951         let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
952         assert_eq!(events_2.len(), 1);
953         let bs_update = match events_2[0] {
954                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
955                         msg.clone()
956                 },
957                 _ => panic!("Unexpected event"),
958         };
959
960         for node in nodes {
961                 node.router.handle_channel_update(&as_update).unwrap();
962                 node.router.handle_channel_update(&bs_update).unwrap();
963         }
964 }
965
966 macro_rules! get_channel_value_stat {
967         ($node: expr, $channel_id: expr) => {{
968                 let chan_lock = $node.node.channel_state.lock().unwrap();
969                 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
970                 chan.get_value_stat()
971         }}
972 }
973
974 macro_rules! get_chan_reestablish_msgs {
975         ($src_node: expr, $dst_node: expr) => {
976                 {
977                         let mut res = Vec::with_capacity(1);
978                         for msg in $src_node.node.get_and_clear_pending_msg_events() {
979                                 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
980                                         assert_eq!(*node_id, $dst_node.node.get_our_node_id());
981                                         res.push(msg.clone());
982                                 } else {
983                                         panic!("Unexpected event")
984                                 }
985                         }
986                         res
987                 }
988         }
989 }
990
991 macro_rules! handle_chan_reestablish_msgs {
992         ($src_node: expr, $dst_node: expr) => {
993                 {
994                         let msg_events = $src_node.node.get_and_clear_pending_msg_events();
995                         let mut idx = 0;
996                         let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
997                                 idx += 1;
998                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
999                                 Some(msg.clone())
1000                         } else {
1001                                 None
1002                         };
1003
1004                         let mut revoke_and_ack = None;
1005                         let mut commitment_update = None;
1006                         let order = if let Some(ev) = msg_events.get(idx) {
1007                                 idx += 1;
1008                                 match ev {
1009                                         &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1010                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1011                                                 revoke_and_ack = Some(msg.clone());
1012                                                 RAACommitmentOrder::RevokeAndACKFirst
1013                                         },
1014                                         &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1015                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1016                                                 commitment_update = Some(updates.clone());
1017                                                 RAACommitmentOrder::CommitmentFirst
1018                                         },
1019                                         _ => panic!("Unexpected event"),
1020                                 }
1021                         } else {
1022                                 RAACommitmentOrder::CommitmentFirst
1023                         };
1024
1025                         if let Some(ev) = msg_events.get(idx) {
1026                                 match ev {
1027                                         &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1028                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1029                                                 assert!(revoke_and_ack.is_none());
1030                                                 revoke_and_ack = Some(msg.clone());
1031                                         },
1032                                         &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1033                                                 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1034                                                 assert!(commitment_update.is_none());
1035                                                 commitment_update = Some(updates.clone());
1036                                         },
1037                                         _ => panic!("Unexpected event"),
1038                                 }
1039                         }
1040
1041                         (funding_locked, revoke_and_ack, commitment_update, order)
1042                 }
1043         }
1044 }
1045
1046 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1047 /// for claims/fails they are separated out.
1048 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)) {
1049         node_a.node.peer_connected(&node_b.node.get_our_node_id());
1050         let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1051         node_b.node.peer_connected(&node_a.node.get_our_node_id());
1052         let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1053
1054         if send_funding_locked.0 {
1055                 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1056                 // from b
1057                 for reestablish in reestablish_1.iter() {
1058                         assert_eq!(reestablish.next_remote_commitment_number, 0);
1059                 }
1060         }
1061         if send_funding_locked.1 {
1062                 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1063                 // from a
1064                 for reestablish in reestablish_2.iter() {
1065                         assert_eq!(reestablish.next_remote_commitment_number, 0);
1066                 }
1067         }
1068         if send_funding_locked.0 || send_funding_locked.1 {
1069                 // If we expect any funding_locked's, both sides better have set
1070                 // next_local_commitment_number to 1
1071                 for reestablish in reestablish_1.iter() {
1072                         assert_eq!(reestablish.next_local_commitment_number, 1);
1073                 }
1074                 for reestablish in reestablish_2.iter() {
1075                         assert_eq!(reestablish.next_local_commitment_number, 1);
1076                 }
1077         }
1078
1079         let mut resp_1 = Vec::new();
1080         for msg in reestablish_1 {
1081                 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1082                 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1083         }
1084         if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1085                 check_added_monitors!(node_b, 1);
1086         } else {
1087                 check_added_monitors!(node_b, 0);
1088         }
1089
1090         let mut resp_2 = Vec::new();
1091         for msg in reestablish_2 {
1092                 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1093                 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1094         }
1095         if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1096                 check_added_monitors!(node_a, 1);
1097         } else {
1098                 check_added_monitors!(node_a, 0);
1099         }
1100
1101         // We don't yet support both needing updates, as that would require a different commitment dance:
1102         assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1103                         (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1104
1105         for chan_msgs in resp_1.drain(..) {
1106                 if send_funding_locked.0 {
1107                         node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1108                         let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1109                         if !announcement_event.is_empty() {
1110                                 assert_eq!(announcement_event.len(), 1);
1111                                 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1112                                         //TODO: Test announcement_sigs re-sending
1113                                 } else { panic!("Unexpected event!"); }
1114                         }
1115                 } else {
1116                         assert!(chan_msgs.0.is_none());
1117                 }
1118                 if pending_raa.0 {
1119                         assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1120                         node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1121                         assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1122                         check_added_monitors!(node_a, 1);
1123                 } else {
1124                         assert!(chan_msgs.1.is_none());
1125                 }
1126                 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1127                         let commitment_update = chan_msgs.2.unwrap();
1128                         if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1129                                 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1130                         } else {
1131                                 assert!(commitment_update.update_add_htlcs.is_empty());
1132                         }
1133                         assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1134                         assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1135                         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1136                         for update_add in commitment_update.update_add_htlcs {
1137                                 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1138                         }
1139                         for update_fulfill in commitment_update.update_fulfill_htlcs {
1140                                 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1141                         }
1142                         for update_fail in commitment_update.update_fail_htlcs {
1143                                 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1144                         }
1145
1146                         if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1147                                 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1148                         } else {
1149                                 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1150                                 check_added_monitors!(node_a, 1);
1151                                 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1152                                 // No commitment_signed so get_event_msg's assert(len == 1) passes
1153                                 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1154                                 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1155                                 check_added_monitors!(node_b, 1);
1156                         }
1157                 } else {
1158                         assert!(chan_msgs.2.is_none());
1159                 }
1160         }
1161
1162         for chan_msgs in resp_2.drain(..) {
1163                 if send_funding_locked.1 {
1164                         node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1165                         let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1166                         if !announcement_event.is_empty() {
1167                                 assert_eq!(announcement_event.len(), 1);
1168                                 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1169                                         //TODO: Test announcement_sigs re-sending
1170                                 } else { panic!("Unexpected event!"); }
1171                         }
1172                 } else {
1173                         assert!(chan_msgs.0.is_none());
1174                 }
1175                 if pending_raa.1 {
1176                         assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1177                         node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1178                         assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1179                         check_added_monitors!(node_b, 1);
1180                 } else {
1181                         assert!(chan_msgs.1.is_none());
1182                 }
1183                 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1184                         let commitment_update = chan_msgs.2.unwrap();
1185                         if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1186                                 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1187                         }
1188                         assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1189                         assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1190                         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1191                         for update_add in commitment_update.update_add_htlcs {
1192                                 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1193                         }
1194                         for update_fulfill in commitment_update.update_fulfill_htlcs {
1195                                 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1196                         }
1197                         for update_fail in commitment_update.update_fail_htlcs {
1198                                 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1199                         }
1200
1201                         if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1202                                 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1203                         } else {
1204                                 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1205                                 check_added_monitors!(node_b, 1);
1206                                 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1207                                 // No commitment_signed so get_event_msg's assert(len == 1) passes
1208                                 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1209                                 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1210                                 check_added_monitors!(node_a, 1);
1211                         }
1212                 } else {
1213                         assert!(chan_msgs.2.is_none());
1214                 }
1215         }
1216 }