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;
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());
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
82 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
85 pub fn create_chan_between_nodes_with_value(node_a: &Node, node_b: &Node, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
86 let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
87 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
88 (announcement, as_update, bs_update, channel_id, tx)
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> Transaction {
162 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
163 node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id())).unwrap();
164 node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id())).unwrap();
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
246 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
247 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
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, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
284 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
287 pub fn create_announced_chan_between_nodes_with_value(nodes: &Vec<Node>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: LocalFeatures, b_flags: LocalFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
288 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
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 $node.node.process_pending_htlc_forwards();
553 macro_rules! expect_payment_received {
554 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
555 let events = $node.node.get_and_clear_pending_events();
556 assert_eq!(events.len(), 1);
558 Event::PaymentReceived { ref payment_hash, amt } => {
559 assert_eq!($expected_payment_hash, *payment_hash);
560 assert_eq!($expected_recv_value, amt);
562 _ => panic!("Unexpected event"),
567 macro_rules! expect_payment_sent {
568 ($node: expr, $expected_payment_preimage: expr) => {
569 let events = $node.node.get_and_clear_pending_events();
570 assert_eq!(events.len(), 1);
572 Event::PaymentSent { ref payment_preimage } => {
573 assert_eq!($expected_payment_preimage, *payment_preimage);
575 _ => panic!("Unexpected event"),
580 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
581 let mut payment_event = {
582 origin_node.node.send_payment(route, our_payment_hash).unwrap();
583 check_added_monitors!(origin_node, 1);
585 let mut events = origin_node.node.get_and_clear_pending_msg_events();
586 assert_eq!(events.len(), 1);
587 SendEvent::from_event(events.remove(0))
589 let mut prev_node = origin_node;
591 for (idx, &node) in expected_route.iter().enumerate() {
592 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
594 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
595 check_added_monitors!(node, 0);
596 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
598 expect_pending_htlcs_forwardable!(node);
600 if idx == expected_route.len() - 1 {
601 let events_2 = node.node.get_and_clear_pending_events();
602 assert_eq!(events_2.len(), 1);
604 Event::PaymentReceived { ref payment_hash, amt } => {
605 assert_eq!(our_payment_hash, *payment_hash);
606 assert_eq!(amt, recv_value);
608 _ => panic!("Unexpected event"),
611 let mut events_2 = node.node.get_and_clear_pending_msg_events();
612 assert_eq!(events_2.len(), 1);
613 check_added_monitors!(node, 1);
614 payment_event = SendEvent::from_event(events_2.remove(0));
615 assert_eq!(payment_event.msgs.len(), 1);
622 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
623 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
624 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
625 (our_payment_preimage, our_payment_hash)
628 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
629 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
630 check_added_monitors!(expected_route.last().unwrap(), 1);
632 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
633 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
634 macro_rules! get_next_msgs {
637 let events = $node.node.get_and_clear_pending_msg_events();
638 assert_eq!(events.len(), 1);
640 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
641 assert!(update_add_htlcs.is_empty());
642 assert_eq!(update_fulfill_htlcs.len(), 1);
643 assert!(update_fail_htlcs.is_empty());
644 assert!(update_fail_malformed_htlcs.is_empty());
645 assert!(update_fee.is_none());
646 expected_next_node = node_id.clone();
647 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
649 _ => panic!("Unexpected event"),
655 macro_rules! last_update_fulfill_dance {
656 ($node: expr, $prev_node: expr) => {
658 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
659 check_added_monitors!($node, 0);
660 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
661 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
665 macro_rules! mid_update_fulfill_dance {
666 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
668 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
669 check_added_monitors!($node, 1);
670 let new_next_msgs = if $new_msgs {
671 get_next_msgs!($node)
673 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
676 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
677 next_msgs = new_next_msgs;
682 let mut prev_node = expected_route.last().unwrap();
683 for (idx, node) in expected_route.iter().rev().enumerate() {
684 assert_eq!(expected_next_node, node.node.get_our_node_id());
685 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
686 if next_msgs.is_some() {
687 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
688 } else if update_next_msgs {
689 next_msgs = get_next_msgs!(node);
691 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
693 if !skip_last && idx == expected_route.len() - 1 {
694 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
701 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
702 expect_payment_sent!(origin_node, our_payment_preimage);
706 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
707 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
710 pub const TEST_FINAL_CLTV: u32 = 32;
712 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
713 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
714 assert_eq!(route.hops.len(), expected_route.len());
715 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
716 assert_eq!(hop.pubkey, node.node.get_our_node_id());
719 send_along_route(origin_node, route, expected_route, recv_value)
722 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
723 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
724 assert_eq!(route.hops.len(), expected_route.len());
725 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
726 assert_eq!(hop.pubkey, node.node.get_our_node_id());
729 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
731 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
733 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
734 _ => panic!("Unknown error variants"),
738 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
739 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
740 claim_payment(&origin, expected_route, our_payment_preimage);
743 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
744 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
745 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
746 check_added_monitors!(expected_route.last().unwrap(), 1);
748 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
749 macro_rules! update_fail_dance {
750 ($node: expr, $prev_node: expr, $last_node: expr) => {
752 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
753 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
754 if skip_last && $last_node {
755 expect_pending_htlcs_forwardable!($node);
761 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
762 let mut prev_node = expected_route.last().unwrap();
763 for (idx, node) in expected_route.iter().rev().enumerate() {
764 assert_eq!(expected_next_node, node.node.get_our_node_id());
765 if next_msgs.is_some() {
766 // We may be the "last node" for the purpose of the commitment dance if we're
767 // skipping the last node (implying it is disconnected) and we're the
768 // second-to-last node!
769 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
772 let events = node.node.get_and_clear_pending_msg_events();
773 if !skip_last || idx != expected_route.len() - 1 {
774 assert_eq!(events.len(), 1);
776 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
777 assert!(update_add_htlcs.is_empty());
778 assert!(update_fulfill_htlcs.is_empty());
779 assert_eq!(update_fail_htlcs.len(), 1);
780 assert!(update_fail_malformed_htlcs.is_empty());
781 assert!(update_fee.is_none());
782 expected_next_node = node_id.clone();
783 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
785 _ => panic!("Unexpected event"),
788 assert!(events.is_empty());
790 if !skip_last && idx == expected_route.len() - 1 {
791 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
798 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
800 let events = origin_node.node.get_and_clear_pending_events();
801 assert_eq!(events.len(), 1);
803 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
804 assert_eq!(payment_hash, our_payment_hash);
805 assert!(rejected_by_dest);
807 _ => panic!("Unexpected event"),
812 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
813 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
816 pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
817 let mut nodes = Vec::new();
818 let mut rng = thread_rng();
819 let secp_ctx = Secp256k1::new();
821 let chan_count = Rc::new(RefCell::new(0));
822 let payment_count = Rc::new(RefCell::new(0));
824 for i in 0..node_count {
825 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
826 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
827 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
828 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
829 let mut seed = [0; 32];
830 rng.fill_bytes(&mut seed);
831 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
832 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
833 let mut default_config = UserConfig::new();
834 default_config.channel_options.announced_channel = true;
835 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
836 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger), keys_manager.clone(), if node_config[i].is_some() { node_config[i].clone().unwrap() } else { default_config }).unwrap();
837 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
838 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
839 network_payment_count: payment_count.clone(),
840 network_chan_count: chan_count.clone(),
848 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
849 /// Tests that the given node has broadcast transactions for the given Channel
851 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
852 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
853 /// broadcast and the revoked outputs were claimed.
855 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
856 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
858 /// All broadcast transactions must be accounted for in one of the above three types of we'll
860 pub fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
861 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
862 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
864 let mut res = Vec::with_capacity(2);
865 node_txn.retain(|tx| {
866 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
867 check_spends!(tx, chan.3.clone());
868 if commitment_tx.is_none() {
869 res.push(tx.clone());
874 if let Some(explicit_tx) = commitment_tx {
875 res.push(explicit_tx.clone());
878 assert_eq!(res.len(), 1);
880 if has_htlc_tx != HTLCType::NONE {
881 node_txn.retain(|tx| {
882 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
883 check_spends!(tx, res[0].clone());
884 if has_htlc_tx == HTLCType::TIMEOUT {
885 assert!(tx.lock_time != 0);
887 assert!(tx.lock_time == 0);
889 res.push(tx.clone());
893 assert!(res.len() == 2 || res.len() == 3);
895 assert_eq!(res[1], res[2]);
899 assert!(node_txn.is_empty());
903 /// Tests that the given node has broadcast a claim transaction against the provided revoked
904 /// HTLC transaction.
905 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
906 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
907 assert_eq!(node_txn.len(), 1);
908 node_txn.retain(|tx| {
909 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
910 check_spends!(tx, revoked_tx.clone());
914 assert!(node_txn.is_empty());
917 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
918 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
920 assert!(node_txn.len() >= 1);
921 assert_eq!(node_txn[0].input.len(), 1);
922 let mut found_prev = false;
925 if node_txn[0].input[0].previous_output.txid == tx.txid() {
926 check_spends!(node_txn[0], tx.clone());
927 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
928 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
936 let mut res = Vec::new();
937 mem::swap(&mut *node_txn, &mut res);
941 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
942 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
943 assert_eq!(events_1.len(), 1);
944 let as_update = match events_1[0] {
945 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
948 _ => panic!("Unexpected event"),
951 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
952 assert_eq!(events_2.len(), 1);
953 let bs_update = match events_2[0] {
954 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
957 _ => panic!("Unexpected event"),
961 node.router.handle_channel_update(&as_update).unwrap();
962 node.router.handle_channel_update(&bs_update).unwrap();
966 macro_rules! get_channel_value_stat {
967 ($node: expr, $channel_id: expr) => {{
968 let chan_lock = $node.node.channel_state.lock().unwrap();
969 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
970 chan.get_value_stat()
974 macro_rules! get_chan_reestablish_msgs {
975 ($src_node: expr, $dst_node: expr) => {
977 let mut res = Vec::with_capacity(1);
978 for msg in $src_node.node.get_and_clear_pending_msg_events() {
979 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
980 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
981 res.push(msg.clone());
983 panic!("Unexpected event")
991 macro_rules! handle_chan_reestablish_msgs {
992 ($src_node: expr, $dst_node: expr) => {
994 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
996 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
998 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1004 let mut revoke_and_ack = None;
1005 let mut commitment_update = None;
1006 let order = if let Some(ev) = msg_events.get(idx) {
1009 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1010 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1011 revoke_and_ack = Some(msg.clone());
1012 RAACommitmentOrder::RevokeAndACKFirst
1014 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1015 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1016 commitment_update = Some(updates.clone());
1017 RAACommitmentOrder::CommitmentFirst
1019 _ => panic!("Unexpected event"),
1022 RAACommitmentOrder::CommitmentFirst
1025 if let Some(ev) = msg_events.get(idx) {
1027 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1028 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1029 assert!(revoke_and_ack.is_none());
1030 revoke_and_ack = Some(msg.clone());
1032 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1033 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1034 assert!(commitment_update.is_none());
1035 commitment_update = Some(updates.clone());
1037 _ => panic!("Unexpected event"),
1041 (funding_locked, revoke_and_ack, commitment_update, order)
1046 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1047 /// for claims/fails they are separated out.
1048 pub fn reconnect_nodes(node_a: &Node, node_b: &Node, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) {
1049 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1050 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1051 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1052 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1054 if send_funding_locked.0 {
1055 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1057 for reestablish in reestablish_1.iter() {
1058 assert_eq!(reestablish.next_remote_commitment_number, 0);
1061 if send_funding_locked.1 {
1062 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1064 for reestablish in reestablish_2.iter() {
1065 assert_eq!(reestablish.next_remote_commitment_number, 0);
1068 if send_funding_locked.0 || send_funding_locked.1 {
1069 // If we expect any funding_locked's, both sides better have set
1070 // next_local_commitment_number to 1
1071 for reestablish in reestablish_1.iter() {
1072 assert_eq!(reestablish.next_local_commitment_number, 1);
1074 for reestablish in reestablish_2.iter() {
1075 assert_eq!(reestablish.next_local_commitment_number, 1);
1079 let mut resp_1 = Vec::new();
1080 for msg in reestablish_1 {
1081 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1082 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1084 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1085 check_added_monitors!(node_b, 1);
1087 check_added_monitors!(node_b, 0);
1090 let mut resp_2 = Vec::new();
1091 for msg in reestablish_2 {
1092 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1093 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1095 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1096 check_added_monitors!(node_a, 1);
1098 check_added_monitors!(node_a, 0);
1101 // We don't yet support both needing updates, as that would require a different commitment dance:
1102 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1103 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1105 for chan_msgs in resp_1.drain(..) {
1106 if send_funding_locked.0 {
1107 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1108 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1109 if !announcement_event.is_empty() {
1110 assert_eq!(announcement_event.len(), 1);
1111 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1112 //TODO: Test announcement_sigs re-sending
1113 } else { panic!("Unexpected event!"); }
1116 assert!(chan_msgs.0.is_none());
1119 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1120 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1121 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1122 check_added_monitors!(node_a, 1);
1124 assert!(chan_msgs.1.is_none());
1126 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1127 let commitment_update = chan_msgs.2.unwrap();
1128 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1129 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1131 assert!(commitment_update.update_add_htlcs.is_empty());
1133 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1134 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1135 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1136 for update_add in commitment_update.update_add_htlcs {
1137 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1139 for update_fulfill in commitment_update.update_fulfill_htlcs {
1140 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1142 for update_fail in commitment_update.update_fail_htlcs {
1143 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1146 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1147 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1149 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1150 check_added_monitors!(node_a, 1);
1151 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1152 // No commitment_signed so get_event_msg's assert(len == 1) passes
1153 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1154 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1155 check_added_monitors!(node_b, 1);
1158 assert!(chan_msgs.2.is_none());
1162 for chan_msgs in resp_2.drain(..) {
1163 if send_funding_locked.1 {
1164 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1165 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1166 if !announcement_event.is_empty() {
1167 assert_eq!(announcement_event.len(), 1);
1168 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1169 //TODO: Test announcement_sigs re-sending
1170 } else { panic!("Unexpected event!"); }
1173 assert!(chan_msgs.0.is_none());
1176 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1177 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1178 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1179 check_added_monitors!(node_b, 1);
1181 assert!(chan_msgs.1.is_none());
1183 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1184 let commitment_update = chan_msgs.2.unwrap();
1185 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1186 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1188 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1189 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1190 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1191 for update_add in commitment_update.update_add_htlcs {
1192 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1194 for update_fulfill in commitment_update.update_fulfill_htlcs {
1195 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1197 for update_fail in commitment_update.update_fail_htlcs {
1198 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1201 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1202 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1204 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1205 check_added_monitors!(node_b, 1);
1206 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1207 // No commitment_signed so get_event_msg's assert(len == 1) passes
1208 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1209 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1210 check_added_monitors!(node_a, 1);
1213 assert!(chan_msgs.2.is_none());