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