1 //! A bunch of useful utilities for building networks of nodes and exchanging messages between
2 //! nodes for functional tests.
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};
10 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
12 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
13 use util::errors::APIError;
14 use util::logger::Logger;
15 use util::config::UserConfig;
17 use bitcoin::util::hash::BitcoinHash;
18 use bitcoin::blockdata::block::{BlockHeader, Block};
19 use bitcoin::blockdata::transaction::{Transaction, TxOut};
20 use bitcoin::network::constants::Network;
22 use bitcoin_hashes::sha256::Hash as Sha256;
23 use bitcoin_hashes::sha256d::Hash as Sha256d;
24 use bitcoin_hashes::Hash;
26 use secp256k1::Secp256k1;
27 use secp256k1::key::PublicKey;
29 use rand::{thread_rng,Rng};
31 use std::cell::RefCell;
32 use std::collections::HashMap;
33 use std::default::Default;
35 use std::sync::{Arc, Mutex};
36 use std::time::Instant;
39 pub const CHAN_CONFIRM_DEPTH: u32 = 100;
40 pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
41 assert!(chain.does_match_tx(tx));
42 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
43 chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
44 for i in 2..CHAN_CONFIRM_DEPTH {
45 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
46 chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
50 pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, 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 chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
53 for i in 2..depth + 1 {
54 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
55 chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
60 pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
61 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 };
62 let mut blocks = Vec::new();
64 blocks.push(Block { header, txdata: Vec::new() });
65 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
67 let mut height = height;
68 for block in blocks.pop() {
69 chain.block_disconnected(&block.header, height);
75 pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
76 pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
77 pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
78 pub keys_manager: Arc<test_utils::TestKeysInterface>,
79 pub node: Arc<ChannelManager>,
81 pub node_seed: [u8; 32],
82 pub network_payment_count: Rc<RefCell<u8>>,
83 pub network_chan_count: Rc<RefCell<u32>>,
87 if !::std::thread::panicking() {
88 // Check that we processed all pending events
89 assert!(self.node.get_and_clear_pending_msg_events().is_empty());
90 assert!(self.node.get_and_clear_pending_events().is_empty());
91 assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
96 pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
97 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001)
100 pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
101 let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat);
102 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
103 (announcement, as_update, bs_update, channel_id, tx)
106 macro_rules! get_revoke_commit_msgs {
107 ($node: expr, $node_id: expr) => {
109 let events = $node.node.get_and_clear_pending_msg_events();
110 assert_eq!(events.len(), 2);
112 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
113 assert_eq!(*node_id, $node_id);
116 _ => panic!("Unexpected event"),
118 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
119 assert_eq!(*node_id, $node_id);
120 assert!(updates.update_add_htlcs.is_empty());
121 assert!(updates.update_fulfill_htlcs.is_empty());
122 assert!(updates.update_fail_htlcs.is_empty());
123 assert!(updates.update_fail_malformed_htlcs.is_empty());
124 assert!(updates.update_fee.is_none());
125 updates.commitment_signed.clone()
127 _ => panic!("Unexpected event"),
133 macro_rules! get_event_msg {
134 ($node: expr, $event_type: path, $node_id: expr) => {
136 let events = $node.node.get_and_clear_pending_msg_events();
137 assert_eq!(events.len(), 1);
139 $event_type { ref node_id, ref msg } => {
140 assert_eq!(*node_id, $node_id);
143 _ => panic!("Unexpected event"),
149 macro_rules! get_htlc_update_msgs {
150 ($node: expr, $node_id: expr) => {
152 let events = $node.node.get_and_clear_pending_msg_events();
153 assert_eq!(events.len(), 1);
155 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
156 assert_eq!(*node_id, $node_id);
159 _ => panic!("Unexpected event"),
165 macro_rules! get_feerate {
166 ($node: expr, $channel_id: expr) => {
168 let chan_lock = $node.node.channel_state.lock().unwrap();
169 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
176 pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> Transaction {
177 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
178 node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
179 node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
181 let chan_id = *node_a.network_chan_count.borrow();
185 let events_2 = node_a.node.get_and_clear_pending_events();
186 assert_eq!(events_2.len(), 1);
188 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
189 assert_eq!(*channel_value_satoshis, channel_value);
190 assert_eq!(user_channel_id, 42);
192 tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
193 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
195 funding_output = OutPoint::new(tx.txid(), 0);
197 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
198 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
199 assert_eq!(added_monitors.len(), 1);
200 assert_eq!(added_monitors[0].0, funding_output);
201 added_monitors.clear();
203 _ => panic!("Unexpected event"),
206 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();
208 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
209 assert_eq!(added_monitors.len(), 1);
210 assert_eq!(added_monitors[0].0, funding_output);
211 added_monitors.clear();
214 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();
216 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
217 assert_eq!(added_monitors.len(), 1);
218 assert_eq!(added_monitors[0].0, funding_output);
219 added_monitors.clear();
222 let events_4 = node_a.node.get_and_clear_pending_events();
223 assert_eq!(events_4.len(), 1);
225 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
226 assert_eq!(user_channel_id, 42);
227 assert_eq!(*funding_txo, funding_output);
229 _ => panic!("Unexpected event"),
235 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
236 confirm_transaction(&node_b.chain_monitor, &tx, tx.version);
237 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();
241 confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
242 let events_6 = node_a.node.get_and_clear_pending_msg_events();
243 assert_eq!(events_6.len(), 2);
244 ((match events_6[0] {
245 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
246 channel_id = msg.channel_id.clone();
247 assert_eq!(*node_id, node_b.node.get_our_node_id());
250 _ => panic!("Unexpected event"),
251 }, match events_6[1] {
252 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
253 assert_eq!(*node_id, node_b.node.get_our_node_id());
256 _ => panic!("Unexpected event"),
260 pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
261 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat);
262 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
266 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) {
267 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
268 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
269 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
271 let events_7 = node_b.node.get_and_clear_pending_msg_events();
272 assert_eq!(events_7.len(), 1);
273 let (announcement, bs_update) = match events_7[0] {
274 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
277 _ => panic!("Unexpected event"),
280 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
281 let events_8 = node_a.node.get_and_clear_pending_msg_events();
282 assert_eq!(events_8.len(), 1);
283 let as_update = match events_8[0] {
284 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
285 assert!(*announcement == *msg);
286 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
287 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
290 _ => panic!("Unexpected event"),
293 *node_a.network_chan_count.borrow_mut() += 1;
295 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
298 pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
299 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001)
302 pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
303 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat);
305 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
306 node.router.handle_channel_update(&chan_announcement.1).unwrap();
307 node.router.handle_channel_update(&chan_announcement.2).unwrap();
309 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
312 macro_rules! check_spends {
313 ($tx: expr, $spends_tx: expr) => {
315 let mut funding_tx_map = HashMap::new();
316 let spends_tx = $spends_tx;
317 funding_tx_map.insert(spends_tx.txid(), spends_tx);
318 $tx.verify(&funding_tx_map).unwrap();
323 macro_rules! get_closing_signed_broadcast {
324 ($node: expr, $dest_pubkey: expr) => {
326 let events = $node.get_and_clear_pending_msg_events();
327 assert!(events.len() == 1 || events.len() == 2);
328 (match events[events.len() - 1] {
329 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
330 assert_eq!(msg.contents.flags & 2, 2);
333 _ => panic!("Unexpected event"),
334 }, if events.len() == 2 {
336 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
337 assert_eq!(*node_id, $dest_pubkey);
340 _ => panic!("Unexpected event"),
347 macro_rules! check_closed_broadcast {
349 let events = $node.node.get_and_clear_pending_msg_events();
350 assert_eq!(events.len(), 1);
352 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
353 assert_eq!(msg.contents.flags & 2, 2);
355 _ => panic!("Unexpected event"),
360 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) {
361 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) };
362 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
365 node_a.close_channel(channel_id).unwrap();
366 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
368 let events_1 = node_b.get_and_clear_pending_msg_events();
369 assert!(events_1.len() >= 1);
370 let shutdown_b = match events_1[0] {
371 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
372 assert_eq!(node_id, &node_a.get_our_node_id());
375 _ => panic!("Unexpected event"),
378 let closing_signed_b = if !close_inbound_first {
379 assert_eq!(events_1.len(), 1);
382 Some(match events_1[1] {
383 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
384 assert_eq!(node_id, &node_a.get_our_node_id());
387 _ => panic!("Unexpected event"),
391 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
392 let (as_update, bs_update) = if close_inbound_first {
393 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
394 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
395 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
396 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
397 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
399 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
400 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
401 assert!(none_b.is_none());
402 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
403 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
404 (as_update, bs_update)
406 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
408 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
409 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
410 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
411 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
413 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
414 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
415 assert!(none_a.is_none());
416 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
417 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
418 (as_update, bs_update)
420 assert_eq!(tx_a, tx_b);
421 check_spends!(tx_a, funding_tx);
423 (as_update, bs_update, tx_a)
426 pub struct SendEvent {
427 pub node_id: PublicKey,
428 pub msgs: Vec<msgs::UpdateAddHTLC>,
429 pub commitment_msg: msgs::CommitmentSigned,
432 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
433 assert!(updates.update_fulfill_htlcs.is_empty());
434 assert!(updates.update_fail_htlcs.is_empty());
435 assert!(updates.update_fail_malformed_htlcs.is_empty());
436 assert!(updates.update_fee.is_none());
437 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
440 pub fn from_event(event: MessageSendEvent) -> SendEvent {
442 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
443 _ => panic!("Unexpected event type!"),
447 pub fn from_node(node: &Node) -> SendEvent {
448 let mut events = node.node.get_and_clear_pending_msg_events();
449 assert_eq!(events.len(), 1);
450 SendEvent::from_event(events.pop().unwrap())
454 macro_rules! check_added_monitors {
455 ($node: expr, $count: expr) => {
457 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
458 assert_eq!(added_monitors.len(), $count);
459 added_monitors.clear();
464 macro_rules! commitment_signed_dance {
465 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
467 check_added_monitors!($node_a, 0);
468 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
469 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
470 check_added_monitors!($node_a, 1);
471 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
474 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
476 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
477 check_added_monitors!($node_b, 0);
478 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
479 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
480 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
481 check_added_monitors!($node_b, 1);
482 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
483 let (bs_revoke_and_ack, extra_msg_option) = {
484 let events = $node_b.node.get_and_clear_pending_msg_events();
485 assert!(events.len() <= 2);
487 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
488 assert_eq!(*node_id, $node_a.node.get_our_node_id());
491 _ => panic!("Unexpected event"),
492 }, events.get(1).map(|e| e.clone()))
494 check_added_monitors!($node_b, 1);
496 assert!($node_a.node.get_and_clear_pending_events().is_empty());
497 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
499 (extra_msg_option, bs_revoke_and_ack)
502 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
504 check_added_monitors!($node_a, 0);
505 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
506 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
507 check_added_monitors!($node_a, 1);
508 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
509 assert!(extra_msg_option.is_none());
513 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
515 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
516 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
517 check_added_monitors!($node_a, 1);
521 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
523 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
526 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
528 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
530 expect_pending_htlcs_forwardable!($node_a);
531 check_added_monitors!($node_a, 1);
533 let channel_state = $node_a.node.channel_state.lock().unwrap();
534 assert_eq!(channel_state.pending_msg_events.len(), 1);
535 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
536 assert_ne!(*node_id, $node_b.node.get_our_node_id());
537 } else { panic!("Unexpected event"); }
539 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
545 macro_rules! get_payment_preimage_hash {
548 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
549 *$node.network_payment_count.borrow_mut() += 1;
550 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
551 (payment_preimage, payment_hash)
556 macro_rules! expect_pending_htlcs_forwardable {
558 let events = $node.node.get_and_clear_pending_events();
559 assert_eq!(events.len(), 1);
561 Event::PendingHTLCsForwardable { .. } => { },
562 _ => panic!("Unexpected event"),
564 let node_ref: &Node = &$node;
565 node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now();
566 $node.node.process_pending_htlc_forwards();
570 macro_rules! expect_payment_received {
571 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
572 let events = $node.node.get_and_clear_pending_events();
573 assert_eq!(events.len(), 1);
575 Event::PaymentReceived { ref payment_hash, amt } => {
576 assert_eq!($expected_payment_hash, *payment_hash);
577 assert_eq!($expected_recv_value, amt);
579 _ => panic!("Unexpected event"),
584 macro_rules! expect_payment_sent {
585 ($node: expr, $expected_payment_preimage: expr) => {
586 let events = $node.node.get_and_clear_pending_events();
587 assert_eq!(events.len(), 1);
589 Event::PaymentSent { ref payment_preimage } => {
590 assert_eq!($expected_payment_preimage, *payment_preimage);
592 _ => panic!("Unexpected event"),
597 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
598 let mut payment_event = {
599 origin_node.node.send_payment(route, our_payment_hash).unwrap();
600 check_added_monitors!(origin_node, 1);
602 let mut events = origin_node.node.get_and_clear_pending_msg_events();
603 assert_eq!(events.len(), 1);
604 SendEvent::from_event(events.remove(0))
606 let mut prev_node = origin_node;
608 for (idx, &node) in expected_route.iter().enumerate() {
609 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
611 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
612 check_added_monitors!(node, 0);
613 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
615 expect_pending_htlcs_forwardable!(node);
617 if idx == expected_route.len() - 1 {
618 let events_2 = node.node.get_and_clear_pending_events();
619 assert_eq!(events_2.len(), 1);
621 Event::PaymentReceived { ref payment_hash, amt } => {
622 assert_eq!(our_payment_hash, *payment_hash);
623 assert_eq!(amt, recv_value);
625 _ => panic!("Unexpected event"),
628 let mut events_2 = node.node.get_and_clear_pending_msg_events();
629 assert_eq!(events_2.len(), 1);
630 check_added_monitors!(node, 1);
631 payment_event = SendEvent::from_event(events_2.remove(0));
632 assert_eq!(payment_event.msgs.len(), 1);
639 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
640 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
641 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
642 (our_payment_preimage, our_payment_hash)
645 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
646 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
647 check_added_monitors!(expected_route.last().unwrap(), 1);
649 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
650 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
651 macro_rules! get_next_msgs {
654 let events = $node.node.get_and_clear_pending_msg_events();
655 assert_eq!(events.len(), 1);
657 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 } } => {
658 assert!(update_add_htlcs.is_empty());
659 assert_eq!(update_fulfill_htlcs.len(), 1);
660 assert!(update_fail_htlcs.is_empty());
661 assert!(update_fail_malformed_htlcs.is_empty());
662 assert!(update_fee.is_none());
663 expected_next_node = node_id.clone();
664 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
666 _ => panic!("Unexpected event"),
672 macro_rules! last_update_fulfill_dance {
673 ($node: expr, $prev_node: expr) => {
675 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
676 check_added_monitors!($node, 0);
677 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
678 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
682 macro_rules! mid_update_fulfill_dance {
683 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
685 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
686 check_added_monitors!($node, 1);
687 let new_next_msgs = if $new_msgs {
688 get_next_msgs!($node)
690 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
693 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
694 next_msgs = new_next_msgs;
699 let mut prev_node = expected_route.last().unwrap();
700 for (idx, node) in expected_route.iter().rev().enumerate() {
701 assert_eq!(expected_next_node, node.node.get_our_node_id());
702 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
703 if next_msgs.is_some() {
704 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
705 } else if update_next_msgs {
706 next_msgs = get_next_msgs!(node);
708 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
710 if !skip_last && idx == expected_route.len() - 1 {
711 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
718 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
719 expect_payment_sent!(origin_node, our_payment_preimage);
723 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
724 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
727 pub const TEST_FINAL_CLTV: u32 = 32;
729 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
730 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();
731 assert_eq!(route.hops.len(), expected_route.len());
732 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
733 assert_eq!(hop.pubkey, node.node.get_our_node_id());
736 send_along_route(origin_node, route, expected_route, recv_value)
739 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
740 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();
741 assert_eq!(route.hops.len(), expected_route.len());
742 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
743 assert_eq!(hop.pubkey, node.node.get_our_node_id());
746 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
748 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
750 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
751 _ => panic!("Unknown error variants"),
755 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
756 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
757 claim_payment(&origin, expected_route, our_payment_preimage);
760 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
761 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
762 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
763 check_added_monitors!(expected_route.last().unwrap(), 1);
765 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
766 macro_rules! update_fail_dance {
767 ($node: expr, $prev_node: expr, $last_node: expr) => {
769 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
770 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
771 if skip_last && $last_node {
772 expect_pending_htlcs_forwardable!($node);
778 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
779 let mut prev_node = expected_route.last().unwrap();
780 for (idx, node) in expected_route.iter().rev().enumerate() {
781 assert_eq!(expected_next_node, node.node.get_our_node_id());
782 if next_msgs.is_some() {
783 // We may be the "last node" for the purpose of the commitment dance if we're
784 // skipping the last node (implying it is disconnected) and we're the
785 // second-to-last node!
786 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
789 let events = node.node.get_and_clear_pending_msg_events();
790 if !skip_last || idx != expected_route.len() - 1 {
791 assert_eq!(events.len(), 1);
793 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 } } => {
794 assert!(update_add_htlcs.is_empty());
795 assert!(update_fulfill_htlcs.is_empty());
796 assert_eq!(update_fail_htlcs.len(), 1);
797 assert!(update_fail_malformed_htlcs.is_empty());
798 assert!(update_fee.is_none());
799 expected_next_node = node_id.clone();
800 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
802 _ => panic!("Unexpected event"),
805 assert!(events.is_empty());
807 if !skip_last && idx == expected_route.len() - 1 {
808 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
815 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
817 let events = origin_node.node.get_and_clear_pending_events();
818 assert_eq!(events.len(), 1);
820 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
821 assert_eq!(payment_hash, our_payment_hash);
822 assert!(rejected_by_dest);
824 _ => panic!("Unexpected event"),
829 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
830 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
833 pub fn create_network(node_count: usize) -> Vec<Node> {
834 let mut nodes = Vec::new();
835 let mut rng = thread_rng();
836 let secp_ctx = Secp256k1::new();
838 let chan_count = Rc::new(RefCell::new(0));
839 let payment_count = Rc::new(RefCell::new(0));
841 for i in 0..node_count {
842 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
843 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
844 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
845 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
846 let mut seed = [0; 32];
847 rng.fill_bytes(&mut seed);
848 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
849 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
850 let mut config = UserConfig::new();
851 config.channel_options.announced_channel = true;
852 config.peer_channel_config_limits.force_announced_channel_preference = false;
853 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
854 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
855 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
856 network_payment_count: payment_count.clone(),
857 network_chan_count: chan_count.clone(),
865 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
866 /// Tests that the given node has broadcast transactions for the given Channel
868 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
869 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
870 /// broadcast and the revoked outputs were claimed.
872 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
873 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
875 /// All broadcast transactions must be accounted for in one of the above three types of we'll
877 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> {
878 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
879 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
881 let mut res = Vec::with_capacity(2);
882 node_txn.retain(|tx| {
883 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
884 check_spends!(tx, chan.3.clone());
885 if commitment_tx.is_none() {
886 res.push(tx.clone());
891 if let Some(explicit_tx) = commitment_tx {
892 res.push(explicit_tx.clone());
895 assert_eq!(res.len(), 1);
897 if has_htlc_tx != HTLCType::NONE {
898 node_txn.retain(|tx| {
899 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
900 check_spends!(tx, res[0].clone());
901 if has_htlc_tx == HTLCType::TIMEOUT {
902 assert!(tx.lock_time != 0);
904 assert!(tx.lock_time == 0);
906 res.push(tx.clone());
910 assert!(res.len() == 2 || res.len() == 3);
912 assert_eq!(res[1], res[2]);
916 assert!(node_txn.is_empty());
920 /// Tests that the given node has broadcast a claim transaction against the provided revoked
921 /// HTLC transaction.
922 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
923 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
924 assert_eq!(node_txn.len(), 1);
925 node_txn.retain(|tx| {
926 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
927 check_spends!(tx, revoked_tx.clone());
931 assert!(node_txn.is_empty());
934 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
935 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
937 assert!(node_txn.len() >= 1);
938 assert_eq!(node_txn[0].input.len(), 1);
939 let mut found_prev = false;
942 if node_txn[0].input[0].previous_output.txid == tx.txid() {
943 check_spends!(node_txn[0], tx.clone());
944 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
945 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
953 let mut res = Vec::new();
954 mem::swap(&mut *node_txn, &mut res);
958 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
959 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
960 assert_eq!(events_1.len(), 1);
961 let as_update = match events_1[0] {
962 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
965 _ => panic!("Unexpected event"),
968 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
969 assert_eq!(events_2.len(), 1);
970 let bs_update = match events_2[0] {
971 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
974 _ => panic!("Unexpected event"),
978 node.router.handle_channel_update(&as_update).unwrap();
979 node.router.handle_channel_update(&bs_update).unwrap();
983 macro_rules! get_channel_value_stat {
984 ($node: expr, $channel_id: expr) => {{
985 let chan_lock = $node.node.channel_state.lock().unwrap();
986 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
987 chan.get_value_stat()
991 macro_rules! get_chan_reestablish_msgs {
992 ($src_node: expr, $dst_node: expr) => {
994 let mut res = Vec::with_capacity(1);
995 for msg in $src_node.node.get_and_clear_pending_msg_events() {
996 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
997 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
998 res.push(msg.clone());
1000 panic!("Unexpected event")
1008 macro_rules! handle_chan_reestablish_msgs {
1009 ($src_node: expr, $dst_node: expr) => {
1011 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1013 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1015 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1021 let mut revoke_and_ack = None;
1022 let mut commitment_update = None;
1023 let order = if let Some(ev) = msg_events.get(idx) {
1026 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1027 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1028 revoke_and_ack = Some(msg.clone());
1029 RAACommitmentOrder::RevokeAndACKFirst
1031 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1032 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1033 commitment_update = Some(updates.clone());
1034 RAACommitmentOrder::CommitmentFirst
1036 _ => panic!("Unexpected event"),
1039 RAACommitmentOrder::CommitmentFirst
1042 if let Some(ev) = msg_events.get(idx) {
1044 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1045 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1046 assert!(revoke_and_ack.is_none());
1047 revoke_and_ack = Some(msg.clone());
1049 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1050 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1051 assert!(commitment_update.is_none());
1052 commitment_update = Some(updates.clone());
1054 _ => panic!("Unexpected event"),
1058 (funding_locked, revoke_and_ack, commitment_update, order)
1063 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1064 /// for claims/fails they are separated out.
1065 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)) {
1066 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1067 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1068 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1069 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1071 if send_funding_locked.0 {
1072 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1074 for reestablish in reestablish_1.iter() {
1075 assert_eq!(reestablish.next_remote_commitment_number, 0);
1078 if send_funding_locked.1 {
1079 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1081 for reestablish in reestablish_2.iter() {
1082 assert_eq!(reestablish.next_remote_commitment_number, 0);
1085 if send_funding_locked.0 || send_funding_locked.1 {
1086 // If we expect any funding_locked's, both sides better have set
1087 // next_local_commitment_number to 1
1088 for reestablish in reestablish_1.iter() {
1089 assert_eq!(reestablish.next_local_commitment_number, 1);
1091 for reestablish in reestablish_2.iter() {
1092 assert_eq!(reestablish.next_local_commitment_number, 1);
1096 let mut resp_1 = Vec::new();
1097 for msg in reestablish_1 {
1098 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1099 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1101 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1102 check_added_monitors!(node_b, 1);
1104 check_added_monitors!(node_b, 0);
1107 let mut resp_2 = Vec::new();
1108 for msg in reestablish_2 {
1109 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1110 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1112 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1113 check_added_monitors!(node_a, 1);
1115 check_added_monitors!(node_a, 0);
1118 // We don't yet support both needing updates, as that would require a different commitment dance:
1119 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1120 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1122 for chan_msgs in resp_1.drain(..) {
1123 if send_funding_locked.0 {
1124 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1125 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1126 if !announcement_event.is_empty() {
1127 assert_eq!(announcement_event.len(), 1);
1128 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1129 //TODO: Test announcement_sigs re-sending
1130 } else { panic!("Unexpected event!"); }
1133 assert!(chan_msgs.0.is_none());
1136 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1137 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1138 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1139 check_added_monitors!(node_a, 1);
1141 assert!(chan_msgs.1.is_none());
1143 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1144 let commitment_update = chan_msgs.2.unwrap();
1145 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1146 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1148 assert!(commitment_update.update_add_htlcs.is_empty());
1150 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1151 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1152 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1153 for update_add in commitment_update.update_add_htlcs {
1154 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1156 for update_fulfill in commitment_update.update_fulfill_htlcs {
1157 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1159 for update_fail in commitment_update.update_fail_htlcs {
1160 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1163 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1164 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1166 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1167 check_added_monitors!(node_a, 1);
1168 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1169 // No commitment_signed so get_event_msg's assert(len == 1) passes
1170 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1171 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1172 check_added_monitors!(node_b, 1);
1175 assert!(chan_msgs.2.is_none());
1179 for chan_msgs in resp_2.drain(..) {
1180 if send_funding_locked.1 {
1181 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1182 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1183 if !announcement_event.is_empty() {
1184 assert_eq!(announcement_event.len(), 1);
1185 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1186 //TODO: Test announcement_sigs re-sending
1187 } else { panic!("Unexpected event!"); }
1190 assert!(chan_msgs.0.is_none());
1193 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1194 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1195 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1196 check_added_monitors!(node_b, 1);
1198 assert!(chan_msgs.1.is_none());
1200 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1201 let commitment_update = chan_msgs.2.unwrap();
1202 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1203 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1205 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1206 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1207 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1208 for update_add in commitment_update.update_add_htlcs {
1209 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1211 for update_fulfill in commitment_update.update_fulfill_htlcs {
1212 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1214 for update_fail in commitment_update.update_fail_htlcs {
1215 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1218 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1219 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1221 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1222 check_added_monitors!(node_b, 1);
1223 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1224 // No commitment_signed so get_event_msg's assert(len == 1) passes
1225 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1226 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1227 check_added_monitors!(node_a, 1);
1230 assert!(chan_msgs.2.is_none());