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;
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) {
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 chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
61 pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
62 pub chan_monitor: Arc<test_utils::TestChannelMonitor>,
63 pub keys_manager: Arc<test_utils::TestKeysInterface>,
64 pub node: Arc<ChannelManager>,
66 pub node_seed: [u8; 32],
67 pub network_payment_count: Rc<RefCell<u8>>,
68 pub network_chan_count: Rc<RefCell<u32>>,
72 if !::std::thread::panicking() {
73 // Check that we processed all pending events
74 assert!(self.node.get_and_clear_pending_msg_events().is_empty());
75 assert!(self.node.get_and_clear_pending_events().is_empty());
76 assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
81 pub fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
82 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001)
85 pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
86 let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat);
87 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
88 (announcement, as_update, bs_update, channel_id, tx)
91 macro_rules! get_revoke_commit_msgs {
92 ($node: expr, $node_id: expr) => {
94 let events = $node.node.get_and_clear_pending_msg_events();
95 assert_eq!(events.len(), 2);
97 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
98 assert_eq!(*node_id, $node_id);
101 _ => panic!("Unexpected event"),
103 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
104 assert_eq!(*node_id, $node_id);
105 assert!(updates.update_add_htlcs.is_empty());
106 assert!(updates.update_fulfill_htlcs.is_empty());
107 assert!(updates.update_fail_htlcs.is_empty());
108 assert!(updates.update_fail_malformed_htlcs.is_empty());
109 assert!(updates.update_fee.is_none());
110 updates.commitment_signed.clone()
112 _ => panic!("Unexpected event"),
118 macro_rules! get_event_msg {
119 ($node: expr, $event_type: path, $node_id: expr) => {
121 let events = $node.node.get_and_clear_pending_msg_events();
122 assert_eq!(events.len(), 1);
124 $event_type { ref node_id, ref msg } => {
125 assert_eq!(*node_id, $node_id);
128 _ => panic!("Unexpected event"),
134 macro_rules! get_htlc_update_msgs {
135 ($node: expr, $node_id: expr) => {
137 let events = $node.node.get_and_clear_pending_msg_events();
138 assert_eq!(events.len(), 1);
140 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
141 assert_eq!(*node_id, $node_id);
144 _ => panic!("Unexpected event"),
150 macro_rules! get_feerate {
151 ($node: expr, $channel_id: expr) => {
153 let chan_lock = $node.node.channel_state.lock().unwrap();
154 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
161 pub fn create_chan_between_nodes_with_value_init(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> Transaction {
162 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
163 node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
164 node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
166 let chan_id = *node_a.network_chan_count.borrow();
170 let events_2 = node_a.node.get_and_clear_pending_events();
171 assert_eq!(events_2.len(), 1);
173 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
174 assert_eq!(*channel_value_satoshis, channel_value);
175 assert_eq!(user_channel_id, 42);
177 tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
178 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
180 funding_output = OutPoint::new(tx.txid(), 0);
182 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
183 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
184 assert_eq!(added_monitors.len(), 1);
185 assert_eq!(added_monitors[0].0, funding_output);
186 added_monitors.clear();
188 _ => panic!("Unexpected event"),
191 node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id())).unwrap();
193 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
194 assert_eq!(added_monitors.len(), 1);
195 assert_eq!(added_monitors[0].0, funding_output);
196 added_monitors.clear();
199 node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id())).unwrap();
201 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
202 assert_eq!(added_monitors.len(), 1);
203 assert_eq!(added_monitors[0].0, funding_output);
204 added_monitors.clear();
207 let events_4 = node_a.node.get_and_clear_pending_events();
208 assert_eq!(events_4.len(), 1);
210 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
211 assert_eq!(user_channel_id, 42);
212 assert_eq!(*funding_txo, funding_output);
214 _ => panic!("Unexpected event"),
220 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
221 confirm_transaction(&node_b.chain_monitor, &tx, tx.version);
222 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingLocked, node_a.node.get_our_node_id())).unwrap();
226 confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
227 let events_6 = node_a.node.get_and_clear_pending_msg_events();
228 assert_eq!(events_6.len(), 2);
229 ((match events_6[0] {
230 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
231 channel_id = msg.channel_id.clone();
232 assert_eq!(*node_id, node_b.node.get_our_node_id());
235 _ => panic!("Unexpected event"),
236 }, match events_6[1] {
237 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
238 assert_eq!(*node_id, node_b.node.get_our_node_id());
241 _ => panic!("Unexpected event"),
245 pub fn create_chan_between_nodes_with_value_a(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
246 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat);
247 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
251 pub fn create_chan_between_nodes_with_value_b(node_a: &Node, node_b: &Node, as_funding_msgs: &(msgs::FundingLocked, msgs::AnnouncementSignatures)) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
252 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
253 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
254 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
256 let events_7 = node_b.node.get_and_clear_pending_msg_events();
257 assert_eq!(events_7.len(), 1);
258 let (announcement, bs_update) = match events_7[0] {
259 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
262 _ => panic!("Unexpected event"),
265 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
266 let events_8 = node_a.node.get_and_clear_pending_msg_events();
267 assert_eq!(events_8.len(), 1);
268 let as_update = match events_8[0] {
269 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
270 assert!(*announcement == *msg);
271 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
272 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
275 _ => panic!("Unexpected event"),
278 *node_a.network_chan_count.borrow_mut() += 1;
280 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
283 pub fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
284 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001)
287 pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
288 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat);
290 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
291 node.router.handle_channel_update(&chan_announcement.1).unwrap();
292 node.router.handle_channel_update(&chan_announcement.2).unwrap();
294 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
297 macro_rules! check_spends {
298 ($tx: expr, $spends_tx: expr) => {
300 let mut funding_tx_map = HashMap::new();
301 let spends_tx = $spends_tx;
302 funding_tx_map.insert(spends_tx.txid(), spends_tx);
303 $tx.verify(&funding_tx_map).unwrap();
308 macro_rules! get_closing_signed_broadcast {
309 ($node: expr, $dest_pubkey: expr) => {
311 let events = $node.get_and_clear_pending_msg_events();
312 assert!(events.len() == 1 || events.len() == 2);
313 (match events[events.len() - 1] {
314 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
315 assert_eq!(msg.contents.flags & 2, 2);
318 _ => panic!("Unexpected event"),
319 }, if events.len() == 2 {
321 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
322 assert_eq!(*node_id, $dest_pubkey);
325 _ => panic!("Unexpected event"),
332 macro_rules! check_closed_broadcast {
334 let events = $node.node.get_and_clear_pending_msg_events();
335 assert_eq!(events.len(), 1);
337 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
338 assert_eq!(msg.contents.flags & 2, 2);
340 _ => panic!("Unexpected event"),
345 pub fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
346 let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) };
347 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
350 node_a.close_channel(channel_id).unwrap();
351 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
353 let events_1 = node_b.get_and_clear_pending_msg_events();
354 assert!(events_1.len() >= 1);
355 let shutdown_b = match events_1[0] {
356 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
357 assert_eq!(node_id, &node_a.get_our_node_id());
360 _ => panic!("Unexpected event"),
363 let closing_signed_b = if !close_inbound_first {
364 assert_eq!(events_1.len(), 1);
367 Some(match events_1[1] {
368 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
369 assert_eq!(node_id, &node_a.get_our_node_id());
372 _ => panic!("Unexpected event"),
376 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
377 let (as_update, bs_update) = if close_inbound_first {
378 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
379 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
380 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
381 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
382 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
384 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
385 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
386 assert!(none_b.is_none());
387 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
388 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
389 (as_update, bs_update)
391 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
393 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
394 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
395 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
396 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
398 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
399 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
400 assert!(none_a.is_none());
401 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
402 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
403 (as_update, bs_update)
405 assert_eq!(tx_a, tx_b);
406 check_spends!(tx_a, funding_tx);
408 (as_update, bs_update, tx_a)
411 pub struct SendEvent {
412 pub node_id: PublicKey,
413 pub msgs: Vec<msgs::UpdateAddHTLC>,
414 pub commitment_msg: msgs::CommitmentSigned,
417 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
418 assert!(updates.update_fulfill_htlcs.is_empty());
419 assert!(updates.update_fail_htlcs.is_empty());
420 assert!(updates.update_fail_malformed_htlcs.is_empty());
421 assert!(updates.update_fee.is_none());
422 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
425 pub fn from_event(event: MessageSendEvent) -> SendEvent {
427 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
428 _ => panic!("Unexpected event type!"),
432 pub fn from_node(node: &Node) -> SendEvent {
433 let mut events = node.node.get_and_clear_pending_msg_events();
434 assert_eq!(events.len(), 1);
435 SendEvent::from_event(events.pop().unwrap())
439 macro_rules! check_added_monitors {
440 ($node: expr, $count: expr) => {
442 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
443 assert_eq!(added_monitors.len(), $count);
444 added_monitors.clear();
449 macro_rules! commitment_signed_dance {
450 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
452 check_added_monitors!($node_a, 0);
453 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
454 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
455 check_added_monitors!($node_a, 1);
456 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
459 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
461 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
462 check_added_monitors!($node_b, 0);
463 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
464 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
465 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
466 check_added_monitors!($node_b, 1);
467 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
468 let (bs_revoke_and_ack, extra_msg_option) = {
469 let events = $node_b.node.get_and_clear_pending_msg_events();
470 assert!(events.len() <= 2);
472 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
473 assert_eq!(*node_id, $node_a.node.get_our_node_id());
476 _ => panic!("Unexpected event"),
477 }, events.get(1).map(|e| e.clone()))
479 check_added_monitors!($node_b, 1);
481 assert!($node_a.node.get_and_clear_pending_events().is_empty());
482 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
484 (extra_msg_option, bs_revoke_and_ack)
487 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
489 check_added_monitors!($node_a, 0);
490 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
491 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
492 check_added_monitors!($node_a, 1);
493 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
494 assert!(extra_msg_option.is_none());
498 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
500 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
501 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
502 check_added_monitors!($node_a, 1);
506 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
508 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
511 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
513 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
515 expect_pending_htlcs_forwardable!($node_a);
516 check_added_monitors!($node_a, 1);
518 let channel_state = $node_a.node.channel_state.lock().unwrap();
519 assert_eq!(channel_state.pending_msg_events.len(), 1);
520 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
521 assert_ne!(*node_id, $node_b.node.get_our_node_id());
522 } else { panic!("Unexpected event"); }
524 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
530 macro_rules! get_payment_preimage_hash {
533 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
534 *$node.network_payment_count.borrow_mut() += 1;
535 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
536 (payment_preimage, payment_hash)
541 macro_rules! expect_pending_htlcs_forwardable {
543 let events = $node.node.get_and_clear_pending_events();
544 assert_eq!(events.len(), 1);
546 Event::PendingHTLCsForwardable { .. } => { },
547 _ => panic!("Unexpected event"),
549 let node_ref: &Node = &$node;
550 node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now();
551 $node.node.process_pending_htlc_forwards();
555 macro_rules! expect_payment_received {
556 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
557 let events = $node.node.get_and_clear_pending_events();
558 assert_eq!(events.len(), 1);
560 Event::PaymentReceived { ref payment_hash, amt } => {
561 assert_eq!($expected_payment_hash, *payment_hash);
562 assert_eq!($expected_recv_value, amt);
564 _ => panic!("Unexpected event"),
569 macro_rules! expect_payment_sent {
570 ($node: expr, $expected_payment_preimage: expr) => {
571 let events = $node.node.get_and_clear_pending_events();
572 assert_eq!(events.len(), 1);
574 Event::PaymentSent { ref payment_preimage } => {
575 assert_eq!($expected_payment_preimage, *payment_preimage);
577 _ => panic!("Unexpected event"),
582 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
583 let mut payment_event = {
584 origin_node.node.send_payment(route, our_payment_hash).unwrap();
585 check_added_monitors!(origin_node, 1);
587 let mut events = origin_node.node.get_and_clear_pending_msg_events();
588 assert_eq!(events.len(), 1);
589 SendEvent::from_event(events.remove(0))
591 let mut prev_node = origin_node;
593 for (idx, &node) in expected_route.iter().enumerate() {
594 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
596 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
597 check_added_monitors!(node, 0);
598 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
600 expect_pending_htlcs_forwardable!(node);
602 if idx == expected_route.len() - 1 {
603 let events_2 = node.node.get_and_clear_pending_events();
604 assert_eq!(events_2.len(), 1);
606 Event::PaymentReceived { ref payment_hash, amt } => {
607 assert_eq!(our_payment_hash, *payment_hash);
608 assert_eq!(amt, recv_value);
610 _ => panic!("Unexpected event"),
613 let mut events_2 = node.node.get_and_clear_pending_msg_events();
614 assert_eq!(events_2.len(), 1);
615 check_added_monitors!(node, 1);
616 payment_event = SendEvent::from_event(events_2.remove(0));
617 assert_eq!(payment_event.msgs.len(), 1);
624 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
625 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
626 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
627 (our_payment_preimage, our_payment_hash)
630 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
631 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
632 check_added_monitors!(expected_route.last().unwrap(), 1);
634 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
635 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
636 macro_rules! get_next_msgs {
639 let events = $node.node.get_and_clear_pending_msg_events();
640 assert_eq!(events.len(), 1);
642 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
643 assert!(update_add_htlcs.is_empty());
644 assert_eq!(update_fulfill_htlcs.len(), 1);
645 assert!(update_fail_htlcs.is_empty());
646 assert!(update_fail_malformed_htlcs.is_empty());
647 assert!(update_fee.is_none());
648 expected_next_node = node_id.clone();
649 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
651 _ => panic!("Unexpected event"),
657 macro_rules! last_update_fulfill_dance {
658 ($node: expr, $prev_node: expr) => {
660 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
661 check_added_monitors!($node, 0);
662 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
663 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
667 macro_rules! mid_update_fulfill_dance {
668 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
670 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
671 check_added_monitors!($node, 1);
672 let new_next_msgs = if $new_msgs {
673 get_next_msgs!($node)
675 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);
679 next_msgs = new_next_msgs;
684 let mut prev_node = expected_route.last().unwrap();
685 for (idx, node) in expected_route.iter().rev().enumerate() {
686 assert_eq!(expected_next_node, node.node.get_our_node_id());
687 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
688 if next_msgs.is_some() {
689 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
690 } else if update_next_msgs {
691 next_msgs = get_next_msgs!(node);
693 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
695 if !skip_last && idx == expected_route.len() - 1 {
696 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
703 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
704 expect_payment_sent!(origin_node, our_payment_preimage);
708 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
709 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
712 pub const TEST_FINAL_CLTV: u32 = 32;
714 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
715 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
716 assert_eq!(route.hops.len(), expected_route.len());
717 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
718 assert_eq!(hop.pubkey, node.node.get_our_node_id());
721 send_along_route(origin_node, route, expected_route, recv_value)
724 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
725 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
726 assert_eq!(route.hops.len(), expected_route.len());
727 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
728 assert_eq!(hop.pubkey, node.node.get_our_node_id());
731 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
733 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
735 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
736 _ => panic!("Unknown error variants"),
740 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
741 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
742 claim_payment(&origin, expected_route, our_payment_preimage);
745 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
746 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
747 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
748 check_added_monitors!(expected_route.last().unwrap(), 1);
750 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
751 macro_rules! update_fail_dance {
752 ($node: expr, $prev_node: expr, $last_node: expr) => {
754 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
755 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
756 if skip_last && $last_node {
757 expect_pending_htlcs_forwardable!($node);
763 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
764 let mut prev_node = expected_route.last().unwrap();
765 for (idx, node) in expected_route.iter().rev().enumerate() {
766 assert_eq!(expected_next_node, node.node.get_our_node_id());
767 if next_msgs.is_some() {
768 // We may be the "last node" for the purpose of the commitment dance if we're
769 // skipping the last node (implying it is disconnected) and we're the
770 // second-to-last node!
771 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
774 let events = node.node.get_and_clear_pending_msg_events();
775 if !skip_last || idx != expected_route.len() - 1 {
776 assert_eq!(events.len(), 1);
778 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
779 assert!(update_add_htlcs.is_empty());
780 assert!(update_fulfill_htlcs.is_empty());
781 assert_eq!(update_fail_htlcs.len(), 1);
782 assert!(update_fail_malformed_htlcs.is_empty());
783 assert!(update_fee.is_none());
784 expected_next_node = node_id.clone();
785 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
787 _ => panic!("Unexpected event"),
790 assert!(events.is_empty());
792 if !skip_last && idx == expected_route.len() - 1 {
793 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
800 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
802 let events = origin_node.node.get_and_clear_pending_events();
803 assert_eq!(events.len(), 1);
805 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
806 assert_eq!(payment_hash, our_payment_hash);
807 assert!(rejected_by_dest);
809 _ => panic!("Unexpected event"),
814 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
815 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
818 pub fn create_network(node_count: usize) -> Vec<Node> {
819 let mut nodes = Vec::new();
820 let mut rng = thread_rng();
821 let secp_ctx = Secp256k1::new();
823 let chan_count = Rc::new(RefCell::new(0));
824 let payment_count = Rc::new(RefCell::new(0));
826 for i in 0..node_count {
827 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
828 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
829 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
830 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
831 let mut seed = [0; 32];
832 rng.fill_bytes(&mut seed);
833 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
834 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
835 let mut config = UserConfig::new();
836 config.channel_options.announced_channel = true;
837 config.peer_channel_config_limits.force_announced_channel_preference = false;
838 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), config).unwrap();
839 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
840 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
841 network_payment_count: payment_count.clone(),
842 network_chan_count: chan_count.clone(),
850 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
851 /// Tests that the given node has broadcast transactions for the given Channel
853 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
854 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
855 /// broadcast and the revoked outputs were claimed.
857 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
858 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
860 /// All broadcast transactions must be accounted for in one of the above three types of we'll
862 pub fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
863 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
864 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
866 let mut res = Vec::with_capacity(2);
867 node_txn.retain(|tx| {
868 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
869 check_spends!(tx, chan.3.clone());
870 if commitment_tx.is_none() {
871 res.push(tx.clone());
876 if let Some(explicit_tx) = commitment_tx {
877 res.push(explicit_tx.clone());
880 assert_eq!(res.len(), 1);
882 if has_htlc_tx != HTLCType::NONE {
883 node_txn.retain(|tx| {
884 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
885 check_spends!(tx, res[0].clone());
886 if has_htlc_tx == HTLCType::TIMEOUT {
887 assert!(tx.lock_time != 0);
889 assert!(tx.lock_time == 0);
891 res.push(tx.clone());
895 assert!(res.len() == 2 || res.len() == 3);
897 assert_eq!(res[1], res[2]);
901 assert!(node_txn.is_empty());
905 /// Tests that the given node has broadcast a claim transaction against the provided revoked
906 /// HTLC transaction.
907 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
908 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
909 assert_eq!(node_txn.len(), 1);
910 node_txn.retain(|tx| {
911 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
912 check_spends!(tx, revoked_tx.clone());
916 assert!(node_txn.is_empty());
919 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
920 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
922 assert!(node_txn.len() >= 1);
923 assert_eq!(node_txn[0].input.len(), 1);
924 let mut found_prev = false;
927 if node_txn[0].input[0].previous_output.txid == tx.txid() {
928 check_spends!(node_txn[0], tx.clone());
929 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
930 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
938 let mut res = Vec::new();
939 mem::swap(&mut *node_txn, &mut res);
943 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
944 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
945 assert_eq!(events_1.len(), 1);
946 let as_update = match events_1[0] {
947 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
950 _ => panic!("Unexpected event"),
953 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
954 assert_eq!(events_2.len(), 1);
955 let bs_update = match events_2[0] {
956 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
959 _ => panic!("Unexpected event"),
963 node.router.handle_channel_update(&as_update).unwrap();
964 node.router.handle_channel_update(&bs_update).unwrap();
968 macro_rules! get_channel_value_stat {
969 ($node: expr, $channel_id: expr) => {{
970 let chan_lock = $node.node.channel_state.lock().unwrap();
971 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
972 chan.get_value_stat()
976 macro_rules! get_chan_reestablish_msgs {
977 ($src_node: expr, $dst_node: expr) => {
979 let mut res = Vec::with_capacity(1);
980 for msg in $src_node.node.get_and_clear_pending_msg_events() {
981 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
982 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
983 res.push(msg.clone());
985 panic!("Unexpected event")
993 macro_rules! handle_chan_reestablish_msgs {
994 ($src_node: expr, $dst_node: expr) => {
996 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
998 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1000 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1006 let mut revoke_and_ack = None;
1007 let mut commitment_update = None;
1008 let order = if let Some(ev) = msg_events.get(idx) {
1011 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1012 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1013 revoke_and_ack = Some(msg.clone());
1014 RAACommitmentOrder::RevokeAndACKFirst
1016 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1017 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1018 commitment_update = Some(updates.clone());
1019 RAACommitmentOrder::CommitmentFirst
1021 _ => panic!("Unexpected event"),
1024 RAACommitmentOrder::CommitmentFirst
1027 if let Some(ev) = msg_events.get(idx) {
1029 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1030 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1031 assert!(revoke_and_ack.is_none());
1032 revoke_and_ack = Some(msg.clone());
1034 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1035 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1036 assert!(commitment_update.is_none());
1037 commitment_update = Some(updates.clone());
1039 _ => panic!("Unexpected event"),
1043 (funding_locked, revoke_and_ack, commitment_update, order)
1048 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1049 /// for claims/fails they are separated out.
1050 pub fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
1051 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1052 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1053 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1054 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1056 if send_funding_locked.0 {
1057 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1059 for reestablish in reestablish_1.iter() {
1060 assert_eq!(reestablish.next_remote_commitment_number, 0);
1063 if send_funding_locked.1 {
1064 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1066 for reestablish in reestablish_2.iter() {
1067 assert_eq!(reestablish.next_remote_commitment_number, 0);
1070 if send_funding_locked.0 || send_funding_locked.1 {
1071 // If we expect any funding_locked's, both sides better have set
1072 // next_local_commitment_number to 1
1073 for reestablish in reestablish_1.iter() {
1074 assert_eq!(reestablish.next_local_commitment_number, 1);
1076 for reestablish in reestablish_2.iter() {
1077 assert_eq!(reestablish.next_local_commitment_number, 1);
1081 let mut resp_1 = Vec::new();
1082 for msg in reestablish_1 {
1083 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1084 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1086 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1087 check_added_monitors!(node_b, 1);
1089 check_added_monitors!(node_b, 0);
1092 let mut resp_2 = Vec::new();
1093 for msg in reestablish_2 {
1094 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1095 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1097 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1098 check_added_monitors!(node_a, 1);
1100 check_added_monitors!(node_a, 0);
1103 // We don't yet support both needing updates, as that would require a different commitment dance:
1104 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1105 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1107 for chan_msgs in resp_1.drain(..) {
1108 if send_funding_locked.0 {
1109 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1110 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1111 if !announcement_event.is_empty() {
1112 assert_eq!(announcement_event.len(), 1);
1113 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1114 //TODO: Test announcement_sigs re-sending
1115 } else { panic!("Unexpected event!"); }
1118 assert!(chan_msgs.0.is_none());
1121 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1122 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1123 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1124 check_added_monitors!(node_a, 1);
1126 assert!(chan_msgs.1.is_none());
1128 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1129 let commitment_update = chan_msgs.2.unwrap();
1130 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1131 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1133 assert!(commitment_update.update_add_htlcs.is_empty());
1135 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1136 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1137 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1138 for update_add in commitment_update.update_add_htlcs {
1139 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1141 for update_fulfill in commitment_update.update_fulfill_htlcs {
1142 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1144 for update_fail in commitment_update.update_fail_htlcs {
1145 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1148 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1149 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1151 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1152 check_added_monitors!(node_a, 1);
1153 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1154 // No commitment_signed so get_event_msg's assert(len == 1) passes
1155 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1156 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1157 check_added_monitors!(node_b, 1);
1160 assert!(chan_msgs.2.is_none());
1164 for chan_msgs in resp_2.drain(..) {
1165 if send_funding_locked.1 {
1166 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1167 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1168 if !announcement_event.is_empty() {
1169 assert_eq!(announcement_event.len(), 1);
1170 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1171 //TODO: Test announcement_sigs re-sending
1172 } else { panic!("Unexpected event!"); }
1175 assert!(chan_msgs.0.is_none());
1178 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1179 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1180 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1181 check_added_monitors!(node_b, 1);
1183 assert!(chan_msgs.1.is_none());
1185 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1186 let commitment_update = chan_msgs.2.unwrap();
1187 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1188 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1190 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1191 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1192 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1193 for update_add in commitment_update.update_add_htlcs {
1194 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1196 for update_fulfill in commitment_update.update_fulfill_htlcs {
1197 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1199 for update_fail in commitment_update.update_fail_htlcs {
1200 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1203 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1204 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1206 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1207 check_added_monitors!(node_b, 1);
1208 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1209 // No commitment_signed so get_event_msg's assert(len == 1) passes
1210 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1211 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1212 check_added_monitors!(node_a, 1);
1215 assert!(chan_msgs.2.is_none());