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, LocalFeatures};
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};
38 pub const CHAN_CONFIRM_DEPTH: u32 = 100;
39 pub fn confirm_transaction(chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
40 assert!(chain.does_match_tx(tx));
41 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
42 chain.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
43 for i in 2..CHAN_CONFIRM_DEPTH {
44 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
45 chain.block_connected_checked(&header, i, &[tx; 0], &[0; 0]);
49 pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
50 let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
51 chain.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
52 for i in 2..depth + 1 {
53 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
54 chain.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
59 pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
60 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 };
61 let mut blocks = Vec::new();
63 blocks.push(Block { header, txdata: Vec::new() });
64 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
66 let mut height = height;
67 for block in blocks.pop() {
68 chain.block_disconnected(&block.header, height);
74 pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
75 pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
76 pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
77 pub keys_manager: Arc<test_utils::TestKeysInterface>,
78 pub node: Arc<ChannelManager>,
80 pub node_seed: [u8; 32],
81 pub network_payment_count: Rc<RefCell<u8>>,
82 pub network_chan_count: Rc<RefCell<u32>>,
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());
95 pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
96 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
99 pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
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)
105 macro_rules! get_revoke_commit_msgs {
106 ($node: expr, $node_id: expr) => {
108 let events = $node.node.get_and_clear_pending_msg_events();
109 assert_eq!(events.len(), 2);
111 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
112 assert_eq!(*node_id, $node_id);
115 _ => panic!("Unexpected event"),
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()
126 _ => panic!("Unexpected event"),
132 macro_rules! get_event_msg {
133 ($node: expr, $event_type: path, $node_id: expr) => {
135 let events = $node.node.get_and_clear_pending_msg_events();
136 assert_eq!(events.len(), 1);
138 $event_type { ref node_id, ref msg } => {
139 assert_eq!(*node_id, $node_id);
142 _ => panic!("Unexpected event"),
148 macro_rules! get_htlc_update_msgs {
149 ($node: expr, $node_id: expr) => {
151 let events = $node.node.get_and_clear_pending_msg_events();
152 assert_eq!(events.len(), 1);
154 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
155 assert_eq!(*node_id, $node_id);
158 _ => panic!("Unexpected event"),
164 macro_rules! get_feerate {
165 ($node: expr, $channel_id: expr) => {
167 let chan_lock = $node.node.channel_state.lock().unwrap();
168 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
175 pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> Transaction {
176 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
177 node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
178 node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
180 let chan_id = *node_a.network_chan_count.borrow();
184 let events_2 = node_a.node.get_and_clear_pending_events();
185 assert_eq!(events_2.len(), 1);
187 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
188 assert_eq!(*channel_value_satoshis, channel_value);
189 assert_eq!(user_channel_id, 42);
191 tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
192 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
194 funding_output = OutPoint::new(tx.txid(), 0);
196 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
197 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
198 assert_eq!(added_monitors.len(), 1);
199 assert_eq!(added_monitors[0].0, funding_output);
200 added_monitors.clear();
202 _ => panic!("Unexpected event"),
205 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();
207 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
208 assert_eq!(added_monitors.len(), 1);
209 assert_eq!(added_monitors[0].0, funding_output);
210 added_monitors.clear();
213 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();
215 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
216 assert_eq!(added_monitors.len(), 1);
217 assert_eq!(added_monitors[0].0, funding_output);
218 added_monitors.clear();
221 let events_4 = node_a.node.get_and_clear_pending_events();
222 assert_eq!(events_4.len(), 1);
224 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
225 assert_eq!(user_channel_id, 42);
226 assert_eq!(*funding_txo, funding_output);
228 _ => panic!("Unexpected event"),
234 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
235 confirm_transaction(&node_b.chain_monitor, &tx, tx.version);
236 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();
240 confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
241 let events_6 = node_a.node.get_and_clear_pending_msg_events();
242 assert_eq!(events_6.len(), 2);
243 ((match events_6[0] {
244 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
245 channel_id = msg.channel_id.clone();
246 assert_eq!(*node_id, node_b.node.get_our_node_id());
249 _ => panic!("Unexpected event"),
250 }, match events_6[1] {
251 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
252 assert_eq!(*node_id, node_b.node.get_our_node_id());
255 _ => panic!("Unexpected event"),
259 pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
260 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
261 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
265 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) {
266 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
267 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
268 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
270 let events_7 = node_b.node.get_and_clear_pending_msg_events();
271 assert_eq!(events_7.len(), 1);
272 let (announcement, bs_update) = match events_7[0] {
273 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
276 _ => panic!("Unexpected event"),
279 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
280 let events_8 = node_a.node.get_and_clear_pending_msg_events();
281 assert_eq!(events_8.len(), 1);
282 let as_update = match events_8[0] {
283 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
284 assert!(*announcement == *msg);
285 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
286 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
289 _ => panic!("Unexpected event"),
292 *node_a.network_chan_count.borrow_mut() += 1;
294 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
297 pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
298 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
301 pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
302 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
304 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
305 node.router.handle_channel_update(&chan_announcement.1).unwrap();
306 node.router.handle_channel_update(&chan_announcement.2).unwrap();
308 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
311 macro_rules! check_spends {
312 ($tx: expr, $spends_tx: expr) => {
314 let mut funding_tx_map = HashMap::new();
315 let spends_tx = $spends_tx;
316 funding_tx_map.insert(spends_tx.txid(), spends_tx);
317 $tx.verify(&funding_tx_map).unwrap();
322 macro_rules! get_closing_signed_broadcast {
323 ($node: expr, $dest_pubkey: expr) => {
325 let events = $node.get_and_clear_pending_msg_events();
326 assert!(events.len() == 1 || events.len() == 2);
327 (match events[events.len() - 1] {
328 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
329 assert_eq!(msg.contents.flags & 2, 2);
332 _ => panic!("Unexpected event"),
333 }, if events.len() == 2 {
335 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
336 assert_eq!(*node_id, $dest_pubkey);
339 _ => panic!("Unexpected event"),
346 macro_rules! check_closed_broadcast {
348 let events = $node.node.get_and_clear_pending_msg_events();
349 assert_eq!(events.len(), 1);
351 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
352 assert_eq!(msg.contents.flags & 2, 2);
354 _ => panic!("Unexpected event"),
359 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) {
360 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) };
361 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
364 node_a.close_channel(channel_id).unwrap();
365 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
367 let events_1 = node_b.get_and_clear_pending_msg_events();
368 assert!(events_1.len() >= 1);
369 let shutdown_b = match events_1[0] {
370 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
371 assert_eq!(node_id, &node_a.get_our_node_id());
374 _ => panic!("Unexpected event"),
377 let closing_signed_b = if !close_inbound_first {
378 assert_eq!(events_1.len(), 1);
381 Some(match events_1[1] {
382 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
383 assert_eq!(node_id, &node_a.get_our_node_id());
386 _ => panic!("Unexpected event"),
390 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
391 let (as_update, bs_update) = if close_inbound_first {
392 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
393 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
394 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
395 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
396 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
398 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
399 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
400 assert!(none_b.is_none());
401 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
402 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
403 (as_update, bs_update)
405 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
407 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
408 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
409 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
410 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
412 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
413 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
414 assert!(none_a.is_none());
415 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
416 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
417 (as_update, bs_update)
419 assert_eq!(tx_a, tx_b);
420 check_spends!(tx_a, funding_tx);
422 (as_update, bs_update, tx_a)
425 pub struct SendEvent {
426 pub node_id: PublicKey,
427 pub msgs: Vec<msgs::UpdateAddHTLC>,
428 pub commitment_msg: msgs::CommitmentSigned,
431 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
432 assert!(updates.update_fulfill_htlcs.is_empty());
433 assert!(updates.update_fail_htlcs.is_empty());
434 assert!(updates.update_fail_malformed_htlcs.is_empty());
435 assert!(updates.update_fee.is_none());
436 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
439 pub fn from_event(event: MessageSendEvent) -> SendEvent {
441 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
442 _ => panic!("Unexpected event type!"),
446 pub fn from_node(node: &Node) -> SendEvent {
447 let mut events = node.node.get_and_clear_pending_msg_events();
448 assert_eq!(events.len(), 1);
449 SendEvent::from_event(events.pop().unwrap())
453 macro_rules! check_added_monitors {
454 ($node: expr, $count: expr) => {
456 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
457 assert_eq!(added_monitors.len(), $count);
458 added_monitors.clear();
463 macro_rules! commitment_signed_dance {
464 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
466 check_added_monitors!($node_a, 0);
467 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
468 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
469 check_added_monitors!($node_a, 1);
470 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
473 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
475 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
476 check_added_monitors!($node_b, 0);
477 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
478 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
479 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
480 check_added_monitors!($node_b, 1);
481 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
482 let (bs_revoke_and_ack, extra_msg_option) = {
483 let events = $node_b.node.get_and_clear_pending_msg_events();
484 assert!(events.len() <= 2);
486 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
487 assert_eq!(*node_id, $node_a.node.get_our_node_id());
490 _ => panic!("Unexpected event"),
491 }, events.get(1).map(|e| e.clone()))
493 check_added_monitors!($node_b, 1);
495 assert!($node_a.node.get_and_clear_pending_events().is_empty());
496 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
498 (extra_msg_option, bs_revoke_and_ack)
501 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
503 check_added_monitors!($node_a, 0);
504 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
505 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
506 check_added_monitors!($node_a, 1);
507 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
508 assert!(extra_msg_option.is_none());
512 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
514 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
515 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
516 check_added_monitors!($node_a, 1);
520 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
522 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
525 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
527 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
529 expect_pending_htlcs_forwardable!($node_a);
530 check_added_monitors!($node_a, 1);
532 let channel_state = $node_a.node.channel_state.lock().unwrap();
533 assert_eq!(channel_state.pending_msg_events.len(), 1);
534 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
535 assert_ne!(*node_id, $node_b.node.get_our_node_id());
536 } else { panic!("Unexpected event"); }
538 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
544 macro_rules! get_payment_preimage_hash {
547 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
548 *$node.network_payment_count.borrow_mut() += 1;
549 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
550 (payment_preimage, payment_hash)
555 macro_rules! expect_pending_htlcs_forwardable {
557 let events = $node.node.get_and_clear_pending_events();
558 assert_eq!(events.len(), 1);
560 Event::PendingHTLCsForwardable { .. } => { },
561 _ => panic!("Unexpected event"),
563 $node.node.process_pending_htlc_forwards();
567 macro_rules! expect_payment_received {
568 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
569 let events = $node.node.get_and_clear_pending_events();
570 assert_eq!(events.len(), 1);
572 Event::PaymentReceived { ref payment_hash, amt } => {
573 assert_eq!($expected_payment_hash, *payment_hash);
574 assert_eq!($expected_recv_value, amt);
576 _ => panic!("Unexpected event"),
581 macro_rules! expect_payment_sent {
582 ($node: expr, $expected_payment_preimage: expr) => {
583 let events = $node.node.get_and_clear_pending_events();
584 assert_eq!(events.len(), 1);
586 Event::PaymentSent { ref payment_preimage } => {
587 assert_eq!($expected_payment_preimage, *payment_preimage);
589 _ => panic!("Unexpected event"),
594 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
595 let mut payment_event = {
596 origin_node.node.send_payment(route, our_payment_hash).unwrap();
597 check_added_monitors!(origin_node, 1);
599 let mut events = origin_node.node.get_and_clear_pending_msg_events();
600 assert_eq!(events.len(), 1);
601 SendEvent::from_event(events.remove(0))
603 let mut prev_node = origin_node;
605 for (idx, &node) in expected_route.iter().enumerate() {
606 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
608 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
609 check_added_monitors!(node, 0);
610 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
612 expect_pending_htlcs_forwardable!(node);
614 if idx == expected_route.len() - 1 {
615 let events_2 = node.node.get_and_clear_pending_events();
616 assert_eq!(events_2.len(), 1);
618 Event::PaymentReceived { ref payment_hash, amt } => {
619 assert_eq!(our_payment_hash, *payment_hash);
620 assert_eq!(amt, recv_value);
622 _ => panic!("Unexpected event"),
625 let mut events_2 = node.node.get_and_clear_pending_msg_events();
626 assert_eq!(events_2.len(), 1);
627 check_added_monitors!(node, 1);
628 payment_event = SendEvent::from_event(events_2.remove(0));
629 assert_eq!(payment_event.msgs.len(), 1);
636 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
637 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
638 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
639 (our_payment_preimage, our_payment_hash)
642 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
643 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
644 check_added_monitors!(expected_route.last().unwrap(), 1);
646 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
647 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
648 macro_rules! get_next_msgs {
651 let events = $node.node.get_and_clear_pending_msg_events();
652 assert_eq!(events.len(), 1);
654 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 } } => {
655 assert!(update_add_htlcs.is_empty());
656 assert_eq!(update_fulfill_htlcs.len(), 1);
657 assert!(update_fail_htlcs.is_empty());
658 assert!(update_fail_malformed_htlcs.is_empty());
659 assert!(update_fee.is_none());
660 expected_next_node = node_id.clone();
661 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
663 _ => panic!("Unexpected event"),
669 macro_rules! last_update_fulfill_dance {
670 ($node: expr, $prev_node: expr) => {
672 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
673 check_added_monitors!($node, 0);
674 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
675 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
679 macro_rules! mid_update_fulfill_dance {
680 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
682 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
683 check_added_monitors!($node, 1);
684 let new_next_msgs = if $new_msgs {
685 get_next_msgs!($node)
687 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
690 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
691 next_msgs = new_next_msgs;
696 let mut prev_node = expected_route.last().unwrap();
697 for (idx, node) in expected_route.iter().rev().enumerate() {
698 assert_eq!(expected_next_node, node.node.get_our_node_id());
699 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
700 if next_msgs.is_some() {
701 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
702 } else if update_next_msgs {
703 next_msgs = get_next_msgs!(node);
705 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
707 if !skip_last && idx == expected_route.len() - 1 {
708 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
715 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
716 expect_payment_sent!(origin_node, our_payment_preimage);
720 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
721 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
724 pub const TEST_FINAL_CLTV: u32 = 32;
726 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
727 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
728 assert_eq!(route.hops.len(), expected_route.len());
729 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
730 assert_eq!(hop.pubkey, node.node.get_our_node_id());
733 send_along_route(origin_node, route, expected_route, recv_value)
736 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
737 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();
738 assert_eq!(route.hops.len(), expected_route.len());
739 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
740 assert_eq!(hop.pubkey, node.node.get_our_node_id());
743 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
745 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
747 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
748 _ => panic!("Unknown error variants"),
752 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
753 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
754 claim_payment(&origin, expected_route, our_payment_preimage);
757 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
758 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
759 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
760 check_added_monitors!(expected_route.last().unwrap(), 1);
762 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
763 macro_rules! update_fail_dance {
764 ($node: expr, $prev_node: expr, $last_node: expr) => {
766 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
767 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
768 if skip_last && $last_node {
769 expect_pending_htlcs_forwardable!($node);
775 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
776 let mut prev_node = expected_route.last().unwrap();
777 for (idx, node) in expected_route.iter().rev().enumerate() {
778 assert_eq!(expected_next_node, node.node.get_our_node_id());
779 if next_msgs.is_some() {
780 // We may be the "last node" for the purpose of the commitment dance if we're
781 // skipping the last node (implying it is disconnected) and we're the
782 // second-to-last node!
783 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
786 let events = node.node.get_and_clear_pending_msg_events();
787 if !skip_last || idx != expected_route.len() - 1 {
788 assert_eq!(events.len(), 1);
790 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 } } => {
791 assert!(update_add_htlcs.is_empty());
792 assert!(update_fulfill_htlcs.is_empty());
793 assert_eq!(update_fail_htlcs.len(), 1);
794 assert!(update_fail_malformed_htlcs.is_empty());
795 assert!(update_fee.is_none());
796 expected_next_node = node_id.clone();
797 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
799 _ => panic!("Unexpected event"),
802 assert!(events.is_empty());
804 if !skip_last && idx == expected_route.len() - 1 {
805 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
812 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
814 let events = origin_node.node.get_and_clear_pending_events();
815 assert_eq!(events.len(), 1);
817 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
818 assert_eq!(payment_hash, our_payment_hash);
819 assert!(rejected_by_dest);
821 _ => panic!("Unexpected event"),
826 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
827 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
830 pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
831 let mut nodes = Vec::new();
832 let mut rng = thread_rng();
833 let secp_ctx = Secp256k1::new();
835 let chan_count = Rc::new(RefCell::new(0));
836 let payment_count = Rc::new(RefCell::new(0));
838 for i in 0..node_count {
839 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
840 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
841 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
842 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
843 let mut seed = [0; 32];
844 rng.fill_bytes(&mut seed);
845 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
846 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
847 let mut default_config = UserConfig::new();
848 default_config.channel_options.announced_channel = true;
849 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
850 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), if node_config[i].is_some() { node_config[i].clone().unwrap() } else { default_config }).unwrap();
851 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
852 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
853 network_payment_count: payment_count.clone(),
854 network_chan_count: chan_count.clone(),
862 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
863 /// Tests that the given node has broadcast transactions for the given Channel
865 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
866 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
867 /// broadcast and the revoked outputs were claimed.
869 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
870 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
872 /// All broadcast transactions must be accounted for in one of the above three types of we'll
874 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> {
875 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
876 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
878 let mut res = Vec::with_capacity(2);
879 node_txn.retain(|tx| {
880 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
881 check_spends!(tx, chan.3.clone());
882 if commitment_tx.is_none() {
883 res.push(tx.clone());
888 if let Some(explicit_tx) = commitment_tx {
889 res.push(explicit_tx.clone());
892 assert_eq!(res.len(), 1);
894 if has_htlc_tx != HTLCType::NONE {
895 node_txn.retain(|tx| {
896 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
897 check_spends!(tx, res[0].clone());
898 if has_htlc_tx == HTLCType::TIMEOUT {
899 assert!(tx.lock_time != 0);
901 assert!(tx.lock_time == 0);
903 res.push(tx.clone());
907 assert!(res.len() == 2 || res.len() == 3);
909 assert_eq!(res[1], res[2]);
913 assert!(node_txn.is_empty());
917 /// Tests that the given node has broadcast a claim transaction against the provided revoked
918 /// HTLC transaction.
919 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
920 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
921 assert_eq!(node_txn.len(), 1);
922 node_txn.retain(|tx| {
923 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
924 check_spends!(tx, revoked_tx.clone());
928 assert!(node_txn.is_empty());
931 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
932 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
934 assert!(node_txn.len() >= 1);
935 assert_eq!(node_txn[0].input.len(), 1);
936 let mut found_prev = false;
939 if node_txn[0].input[0].previous_output.txid == tx.txid() {
940 check_spends!(node_txn[0], tx.clone());
941 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
942 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
950 let mut res = Vec::new();
951 mem::swap(&mut *node_txn, &mut res);
955 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
956 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
957 assert_eq!(events_1.len(), 1);
958 let as_update = match events_1[0] {
959 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
962 _ => panic!("Unexpected event"),
965 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
966 assert_eq!(events_2.len(), 1);
967 let bs_update = match events_2[0] {
968 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
971 _ => panic!("Unexpected event"),
975 node.router.handle_channel_update(&as_update).unwrap();
976 node.router.handle_channel_update(&bs_update).unwrap();
980 macro_rules! get_channel_value_stat {
981 ($node: expr, $channel_id: expr) => {{
982 let chan_lock = $node.node.channel_state.lock().unwrap();
983 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
984 chan.get_value_stat()
988 macro_rules! get_chan_reestablish_msgs {
989 ($src_node: expr, $dst_node: expr) => {
991 let mut res = Vec::with_capacity(1);
992 for msg in $src_node.node.get_and_clear_pending_msg_events() {
993 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
994 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
995 res.push(msg.clone());
997 panic!("Unexpected event")
1005 macro_rules! handle_chan_reestablish_msgs {
1006 ($src_node: expr, $dst_node: expr) => {
1008 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1010 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1012 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1018 let mut revoke_and_ack = None;
1019 let mut commitment_update = None;
1020 let order = if let Some(ev) = msg_events.get(idx) {
1023 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1024 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1025 revoke_and_ack = Some(msg.clone());
1026 RAACommitmentOrder::RevokeAndACKFirst
1028 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1029 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1030 commitment_update = Some(updates.clone());
1031 RAACommitmentOrder::CommitmentFirst
1033 _ => panic!("Unexpected event"),
1036 RAACommitmentOrder::CommitmentFirst
1039 if let Some(ev) = msg_events.get(idx) {
1041 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1042 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1043 assert!(revoke_and_ack.is_none());
1044 revoke_and_ack = Some(msg.clone());
1046 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1047 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1048 assert!(commitment_update.is_none());
1049 commitment_update = Some(updates.clone());
1051 _ => panic!("Unexpected event"),
1055 (funding_locked, revoke_and_ack, commitment_update, order)
1060 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1061 /// for claims/fails they are separated out.
1062 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)) {
1063 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1064 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1065 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1066 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1068 if send_funding_locked.0 {
1069 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1071 for reestablish in reestablish_1.iter() {
1072 assert_eq!(reestablish.next_remote_commitment_number, 0);
1075 if send_funding_locked.1 {
1076 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1078 for reestablish in reestablish_2.iter() {
1079 assert_eq!(reestablish.next_remote_commitment_number, 0);
1082 if send_funding_locked.0 || send_funding_locked.1 {
1083 // If we expect any funding_locked's, both sides better have set
1084 // next_local_commitment_number to 1
1085 for reestablish in reestablish_1.iter() {
1086 assert_eq!(reestablish.next_local_commitment_number, 1);
1088 for reestablish in reestablish_2.iter() {
1089 assert_eq!(reestablish.next_local_commitment_number, 1);
1093 let mut resp_1 = Vec::new();
1094 for msg in reestablish_1 {
1095 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1096 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1098 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1099 check_added_monitors!(node_b, 1);
1101 check_added_monitors!(node_b, 0);
1104 let mut resp_2 = Vec::new();
1105 for msg in reestablish_2 {
1106 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1107 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1109 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1110 check_added_monitors!(node_a, 1);
1112 check_added_monitors!(node_a, 0);
1115 // We don't yet support both needing updates, as that would require a different commitment dance:
1116 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1117 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1119 for chan_msgs in resp_1.drain(..) {
1120 if send_funding_locked.0 {
1121 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1122 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1123 if !announcement_event.is_empty() {
1124 assert_eq!(announcement_event.len(), 1);
1125 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1126 //TODO: Test announcement_sigs re-sending
1127 } else { panic!("Unexpected event!"); }
1130 assert!(chan_msgs.0.is_none());
1133 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1134 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1135 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1136 check_added_monitors!(node_a, 1);
1138 assert!(chan_msgs.1.is_none());
1140 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1141 let commitment_update = chan_msgs.2.unwrap();
1142 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1143 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1145 assert!(commitment_update.update_add_htlcs.is_empty());
1147 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1148 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1149 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1150 for update_add in commitment_update.update_add_htlcs {
1151 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1153 for update_fulfill in commitment_update.update_fulfill_htlcs {
1154 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1156 for update_fail in commitment_update.update_fail_htlcs {
1157 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1160 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1161 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1163 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1164 check_added_monitors!(node_a, 1);
1165 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1166 // No commitment_signed so get_event_msg's assert(len == 1) passes
1167 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1168 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1169 check_added_monitors!(node_b, 1);
1172 assert!(chan_msgs.2.is_none());
1176 for chan_msgs in resp_2.drain(..) {
1177 if send_funding_locked.1 {
1178 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1179 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1180 if !announcement_event.is_empty() {
1181 assert_eq!(announcement_event.len(), 1);
1182 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1183 //TODO: Test announcement_sigs re-sending
1184 } else { panic!("Unexpected event!"); }
1187 assert!(chan_msgs.0.is_none());
1190 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1191 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1192 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1193 check_added_monitors!(node_b, 1);
1195 assert!(chan_msgs.1.is_none());
1197 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1198 let commitment_update = chan_msgs.2.unwrap();
1199 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1200 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1202 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1203 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1204 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1205 for update_add in commitment_update.update_add_htlcs {
1206 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1208 for update_fulfill in commitment_update.update_fulfill_htlcs {
1209 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1211 for update_fail in commitment_update.update_fail_htlcs {
1212 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1215 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1216 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1218 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1219 check_added_monitors!(node_b, 1);
1220 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1221 // No commitment_signed so get_event_msg's assert(len == 1) passes
1222 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1223 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1224 check_added_monitors!(node_a, 1);
1227 assert!(chan_msgs.2.is_none());