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();
160 pub fn create_funding_transaction(node: &Node, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
161 let chan_id = *node.network_chan_count.borrow();
163 let events = node.node.get_and_clear_pending_events();
164 assert_eq!(events.len(), 1);
166 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
167 assert_eq!(*channel_value_satoshis, expected_chan_value);
168 assert_eq!(user_channel_id, expected_user_chan_id);
170 let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
171 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
173 let funding_outpoint = OutPoint::new(tx.txid(), 0);
174 (*temporary_channel_id, tx, funding_outpoint)
176 _ => panic!("Unexpected event"),
180 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 {
181 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
182 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();
183 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();
185 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
188 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
189 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
190 assert_eq!(added_monitors.len(), 1);
191 assert_eq!(added_monitors[0].0, funding_output);
192 added_monitors.clear();
195 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();
197 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
198 assert_eq!(added_monitors.len(), 1);
199 assert_eq!(added_monitors[0].0, funding_output);
200 added_monitors.clear();
203 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();
205 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
206 assert_eq!(added_monitors.len(), 1);
207 assert_eq!(added_monitors[0].0, funding_output);
208 added_monitors.clear();
211 let events_4 = node_a.node.get_and_clear_pending_events();
212 assert_eq!(events_4.len(), 1);
214 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
215 assert_eq!(user_channel_id, 42);
216 assert_eq!(*funding_txo, funding_output);
218 _ => panic!("Unexpected event"),
224 pub fn create_chan_between_nodes_with_value_confirm_first(node_recv: &Node, node_conf: &Node, tx: &Transaction) {
225 confirm_transaction(&node_conf.chain_monitor, &tx, tx.version);
226 node_recv.node.handle_funding_locked(&node_conf.node.get_our_node_id(), &get_event_msg!(node_conf, MessageSendEvent::SendFundingLocked, node_recv.node.get_our_node_id())).unwrap();
229 pub fn create_chan_between_nodes_with_value_confirm_second(node_recv: &Node, node_conf: &Node) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
231 let events_6 = node_conf.node.get_and_clear_pending_msg_events();
232 assert_eq!(events_6.len(), 2);
233 ((match events_6[0] {
234 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
235 channel_id = msg.channel_id.clone();
236 assert_eq!(*node_id, node_recv.node.get_our_node_id());
239 _ => panic!("Unexpected event"),
240 }, match events_6[1] {
241 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
242 assert_eq!(*node_id, node_recv.node.get_our_node_id());
245 _ => panic!("Unexpected event"),
249 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
250 create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
251 confirm_transaction(&node_a.chain_monitor, &tx, tx.version);
252 create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
255 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) {
256 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
257 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
261 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) {
262 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
263 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
264 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
266 let events_7 = node_b.node.get_and_clear_pending_msg_events();
267 assert_eq!(events_7.len(), 1);
268 let (announcement, bs_update) = match events_7[0] {
269 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
272 _ => panic!("Unexpected event"),
275 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
276 let events_8 = node_a.node.get_and_clear_pending_msg_events();
277 assert_eq!(events_8.len(), 1);
278 let as_update = match events_8[0] {
279 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
280 assert!(*announcement == *msg);
281 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
282 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
285 _ => panic!("Unexpected event"),
288 *node_a.network_chan_count.borrow_mut() += 1;
290 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
293 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) {
294 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
297 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) {
298 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
300 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
301 node.router.handle_channel_update(&chan_announcement.1).unwrap();
302 node.router.handle_channel_update(&chan_announcement.2).unwrap();
304 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
307 macro_rules! check_spends {
308 ($tx: expr, $spends_tx: expr) => {
310 let mut funding_tx_map = HashMap::new();
311 let spends_tx = $spends_tx;
312 funding_tx_map.insert(spends_tx.txid(), spends_tx);
313 $tx.verify(&funding_tx_map).unwrap();
318 macro_rules! get_closing_signed_broadcast {
319 ($node: expr, $dest_pubkey: expr) => {
321 let events = $node.get_and_clear_pending_msg_events();
322 assert!(events.len() == 1 || events.len() == 2);
323 (match events[events.len() - 1] {
324 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
325 assert_eq!(msg.contents.flags & 2, 2);
328 _ => panic!("Unexpected event"),
329 }, if events.len() == 2 {
331 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
332 assert_eq!(*node_id, $dest_pubkey);
335 _ => panic!("Unexpected event"),
342 macro_rules! check_closed_broadcast {
344 let events = $node.node.get_and_clear_pending_msg_events();
345 assert_eq!(events.len(), 1);
347 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
348 assert_eq!(msg.contents.flags & 2, 2);
350 _ => panic!("Unexpected event"),
355 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) {
356 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) };
357 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
360 node_a.close_channel(channel_id).unwrap();
361 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
363 let events_1 = node_b.get_and_clear_pending_msg_events();
364 assert!(events_1.len() >= 1);
365 let shutdown_b = match events_1[0] {
366 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
367 assert_eq!(node_id, &node_a.get_our_node_id());
370 _ => panic!("Unexpected event"),
373 let closing_signed_b = if !close_inbound_first {
374 assert_eq!(events_1.len(), 1);
377 Some(match events_1[1] {
378 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
379 assert_eq!(node_id, &node_a.get_our_node_id());
382 _ => panic!("Unexpected event"),
386 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
387 let (as_update, bs_update) = if close_inbound_first {
388 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
389 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
390 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
391 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
392 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
394 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
395 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
396 assert!(none_b.is_none());
397 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
398 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
399 (as_update, bs_update)
401 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
403 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
404 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
405 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
406 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
408 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
409 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
410 assert!(none_a.is_none());
411 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
412 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
413 (as_update, bs_update)
415 assert_eq!(tx_a, tx_b);
416 check_spends!(tx_a, funding_tx);
418 (as_update, bs_update, tx_a)
421 pub struct SendEvent {
422 pub node_id: PublicKey,
423 pub msgs: Vec<msgs::UpdateAddHTLC>,
424 pub commitment_msg: msgs::CommitmentSigned,
427 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
428 assert!(updates.update_fulfill_htlcs.is_empty());
429 assert!(updates.update_fail_htlcs.is_empty());
430 assert!(updates.update_fail_malformed_htlcs.is_empty());
431 assert!(updates.update_fee.is_none());
432 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
435 pub fn from_event(event: MessageSendEvent) -> SendEvent {
437 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
438 _ => panic!("Unexpected event type!"),
442 pub fn from_node(node: &Node) -> SendEvent {
443 let mut events = node.node.get_and_clear_pending_msg_events();
444 assert_eq!(events.len(), 1);
445 SendEvent::from_event(events.pop().unwrap())
449 macro_rules! check_added_monitors {
450 ($node: expr, $count: expr) => {
452 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
453 assert_eq!(added_monitors.len(), $count);
454 added_monitors.clear();
459 macro_rules! commitment_signed_dance {
460 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
462 check_added_monitors!($node_a, 0);
463 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
464 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
465 check_added_monitors!($node_a, 1);
466 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
469 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
471 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
472 check_added_monitors!($node_b, 0);
473 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
474 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
475 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
476 check_added_monitors!($node_b, 1);
477 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
478 let (bs_revoke_and_ack, extra_msg_option) = {
479 let events = $node_b.node.get_and_clear_pending_msg_events();
480 assert!(events.len() <= 2);
482 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
483 assert_eq!(*node_id, $node_a.node.get_our_node_id());
486 _ => panic!("Unexpected event"),
487 }, events.get(1).map(|e| e.clone()))
489 check_added_monitors!($node_b, 1);
491 assert!($node_a.node.get_and_clear_pending_events().is_empty());
492 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
494 (extra_msg_option, bs_revoke_and_ack)
497 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
499 check_added_monitors!($node_a, 0);
500 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
501 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
502 check_added_monitors!($node_a, 1);
503 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
504 assert!(extra_msg_option.is_none());
508 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
510 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
511 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
512 check_added_monitors!($node_a, 1);
516 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
518 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
521 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
523 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
525 expect_pending_htlcs_forwardable!($node_a);
526 check_added_monitors!($node_a, 1);
528 let channel_state = $node_a.node.channel_state.lock().unwrap();
529 assert_eq!(channel_state.pending_msg_events.len(), 1);
530 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
531 assert_ne!(*node_id, $node_b.node.get_our_node_id());
532 } else { panic!("Unexpected event"); }
534 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
540 macro_rules! get_payment_preimage_hash {
543 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
544 *$node.network_payment_count.borrow_mut() += 1;
545 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
546 (payment_preimage, payment_hash)
551 macro_rules! expect_pending_htlcs_forwardable {
553 let events = $node.node.get_and_clear_pending_events();
554 assert_eq!(events.len(), 1);
556 Event::PendingHTLCsForwardable { .. } => { },
557 _ => panic!("Unexpected event"),
559 $node.node.process_pending_htlc_forwards();
563 macro_rules! expect_payment_received {
564 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
565 let events = $node.node.get_and_clear_pending_events();
566 assert_eq!(events.len(), 1);
568 Event::PaymentReceived { ref payment_hash, amt } => {
569 assert_eq!($expected_payment_hash, *payment_hash);
570 assert_eq!($expected_recv_value, amt);
572 _ => panic!("Unexpected event"),
577 macro_rules! expect_payment_sent {
578 ($node: expr, $expected_payment_preimage: expr) => {
579 let events = $node.node.get_and_clear_pending_events();
580 assert_eq!(events.len(), 1);
582 Event::PaymentSent { ref payment_preimage } => {
583 assert_eq!($expected_payment_preimage, *payment_preimage);
585 _ => panic!("Unexpected event"),
590 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
591 let mut payment_event = {
592 origin_node.node.send_payment(route, our_payment_hash).unwrap();
593 check_added_monitors!(origin_node, 1);
595 let mut events = origin_node.node.get_and_clear_pending_msg_events();
596 assert_eq!(events.len(), 1);
597 SendEvent::from_event(events.remove(0))
599 let mut prev_node = origin_node;
601 for (idx, &node) in expected_route.iter().enumerate() {
602 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
604 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
605 check_added_monitors!(node, 0);
606 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
608 expect_pending_htlcs_forwardable!(node);
610 if idx == expected_route.len() - 1 {
611 let events_2 = node.node.get_and_clear_pending_events();
612 assert_eq!(events_2.len(), 1);
614 Event::PaymentReceived { ref payment_hash, amt } => {
615 assert_eq!(our_payment_hash, *payment_hash);
616 assert_eq!(amt, recv_value);
618 _ => panic!("Unexpected event"),
621 let mut events_2 = node.node.get_and_clear_pending_msg_events();
622 assert_eq!(events_2.len(), 1);
623 check_added_monitors!(node, 1);
624 payment_event = SendEvent::from_event(events_2.remove(0));
625 assert_eq!(payment_event.msgs.len(), 1);
632 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
633 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
634 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
635 (our_payment_preimage, our_payment_hash)
638 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage) {
639 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage));
640 check_added_monitors!(expected_route.last().unwrap(), 1);
642 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
643 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
644 macro_rules! get_next_msgs {
647 let events = $node.node.get_and_clear_pending_msg_events();
648 assert_eq!(events.len(), 1);
650 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 } } => {
651 assert!(update_add_htlcs.is_empty());
652 assert_eq!(update_fulfill_htlcs.len(), 1);
653 assert!(update_fail_htlcs.is_empty());
654 assert!(update_fail_malformed_htlcs.is_empty());
655 assert!(update_fee.is_none());
656 expected_next_node = node_id.clone();
657 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
659 _ => panic!("Unexpected event"),
665 macro_rules! last_update_fulfill_dance {
666 ($node: expr, $prev_node: 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, 0);
670 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
671 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
675 macro_rules! mid_update_fulfill_dance {
676 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
678 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
679 check_added_monitors!($node, 1);
680 let new_next_msgs = if $new_msgs {
681 get_next_msgs!($node)
683 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
686 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
687 next_msgs = new_next_msgs;
692 let mut prev_node = expected_route.last().unwrap();
693 for (idx, node) in expected_route.iter().rev().enumerate() {
694 assert_eq!(expected_next_node, node.node.get_our_node_id());
695 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
696 if next_msgs.is_some() {
697 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
698 } else if update_next_msgs {
699 next_msgs = get_next_msgs!(node);
701 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
703 if !skip_last && idx == expected_route.len() - 1 {
704 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
711 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
712 expect_payment_sent!(origin_node, our_payment_preimage);
716 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage) {
717 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage);
720 pub const TEST_FINAL_CLTV: u32 = 32;
722 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
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 send_along_route(origin_node, route, expected_route, recv_value)
732 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
733 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();
734 assert_eq!(route.hops.len(), expected_route.len());
735 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
736 assert_eq!(hop.pubkey, node.node.get_our_node_id());
739 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
741 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
743 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
744 _ => panic!("Unknown error variants"),
748 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64) {
749 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
750 claim_payment(&origin, expected_route, our_payment_preimage);
753 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
754 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
755 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
756 check_added_monitors!(expected_route.last().unwrap(), 1);
758 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
759 macro_rules! update_fail_dance {
760 ($node: expr, $prev_node: expr, $last_node: expr) => {
762 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
763 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
764 if skip_last && $last_node {
765 expect_pending_htlcs_forwardable!($node);
771 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
772 let mut prev_node = expected_route.last().unwrap();
773 for (idx, node) in expected_route.iter().rev().enumerate() {
774 assert_eq!(expected_next_node, node.node.get_our_node_id());
775 if next_msgs.is_some() {
776 // We may be the "last node" for the purpose of the commitment dance if we're
777 // skipping the last node (implying it is disconnected) and we're the
778 // second-to-last node!
779 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
782 let events = node.node.get_and_clear_pending_msg_events();
783 if !skip_last || idx != expected_route.len() - 1 {
784 assert_eq!(events.len(), 1);
786 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 } } => {
787 assert!(update_add_htlcs.is_empty());
788 assert!(update_fulfill_htlcs.is_empty());
789 assert_eq!(update_fail_htlcs.len(), 1);
790 assert!(update_fail_malformed_htlcs.is_empty());
791 assert!(update_fee.is_none());
792 expected_next_node = node_id.clone();
793 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
795 _ => panic!("Unexpected event"),
798 assert!(events.is_empty());
800 if !skip_last && idx == expected_route.len() - 1 {
801 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
808 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
810 let events = origin_node.node.get_and_clear_pending_events();
811 assert_eq!(events.len(), 1);
813 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
814 assert_eq!(payment_hash, our_payment_hash);
815 assert!(rejected_by_dest);
817 _ => panic!("Unexpected event"),
822 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
823 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
826 pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
827 let mut nodes = Vec::new();
828 let mut rng = thread_rng();
829 let secp_ctx = Secp256k1::new();
831 let chan_count = Rc::new(RefCell::new(0));
832 let payment_count = Rc::new(RefCell::new(0));
834 for i in 0..node_count {
835 let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
836 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
837 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
838 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
839 let mut seed = [0; 32];
840 rng.fill_bytes(&mut seed);
841 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
842 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
843 let mut default_config = UserConfig::new();
844 default_config.channel_options.announced_channel = true;
845 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
846 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();
847 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
848 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
849 network_payment_count: payment_count.clone(),
850 network_chan_count: chan_count.clone(),
858 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
859 /// Tests that the given node has broadcast transactions for the given Channel
861 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
862 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
863 /// broadcast and the revoked outputs were claimed.
865 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
866 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
868 /// All broadcast transactions must be accounted for in one of the above three types of we'll
870 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> {
871 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
872 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
874 let mut res = Vec::with_capacity(2);
875 node_txn.retain(|tx| {
876 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
877 check_spends!(tx, chan.3.clone());
878 if commitment_tx.is_none() {
879 res.push(tx.clone());
884 if let Some(explicit_tx) = commitment_tx {
885 res.push(explicit_tx.clone());
888 assert_eq!(res.len(), 1);
890 if has_htlc_tx != HTLCType::NONE {
891 node_txn.retain(|tx| {
892 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
893 check_spends!(tx, res[0].clone());
894 if has_htlc_tx == HTLCType::TIMEOUT {
895 assert!(tx.lock_time != 0);
897 assert!(tx.lock_time == 0);
899 res.push(tx.clone());
903 assert!(res.len() == 2 || res.len() == 3);
905 assert_eq!(res[1], res[2]);
909 assert!(node_txn.is_empty());
913 /// Tests that the given node has broadcast a claim transaction against the provided revoked
914 /// HTLC transaction.
915 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction) {
916 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
917 assert_eq!(node_txn.len(), 1);
918 node_txn.retain(|tx| {
919 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
920 check_spends!(tx, revoked_tx.clone());
924 assert!(node_txn.is_empty());
927 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
928 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
930 assert!(node_txn.len() >= 1);
931 assert_eq!(node_txn[0].input.len(), 1);
932 let mut found_prev = false;
935 if node_txn[0].input[0].previous_output.txid == tx.txid() {
936 check_spends!(node_txn[0], tx.clone());
937 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
938 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
946 let mut res = Vec::new();
947 mem::swap(&mut *node_txn, &mut res);
951 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
952 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
953 assert_eq!(events_1.len(), 1);
954 let as_update = match events_1[0] {
955 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
958 _ => panic!("Unexpected event"),
961 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
962 assert_eq!(events_2.len(), 1);
963 let bs_update = match events_2[0] {
964 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
967 _ => panic!("Unexpected event"),
971 node.router.handle_channel_update(&as_update).unwrap();
972 node.router.handle_channel_update(&bs_update).unwrap();
976 macro_rules! get_channel_value_stat {
977 ($node: expr, $channel_id: expr) => {{
978 let chan_lock = $node.node.channel_state.lock().unwrap();
979 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
980 chan.get_value_stat()
984 macro_rules! get_chan_reestablish_msgs {
985 ($src_node: expr, $dst_node: expr) => {
987 let mut res = Vec::with_capacity(1);
988 for msg in $src_node.node.get_and_clear_pending_msg_events() {
989 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
990 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
991 res.push(msg.clone());
993 panic!("Unexpected event")
1001 macro_rules! handle_chan_reestablish_msgs {
1002 ($src_node: expr, $dst_node: expr) => {
1004 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1006 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1008 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1014 let mut revoke_and_ack = None;
1015 let mut commitment_update = None;
1016 let order = if let Some(ev) = msg_events.get(idx) {
1019 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1020 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1021 revoke_and_ack = Some(msg.clone());
1022 RAACommitmentOrder::RevokeAndACKFirst
1024 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1025 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1026 commitment_update = Some(updates.clone());
1027 RAACommitmentOrder::CommitmentFirst
1029 _ => panic!("Unexpected event"),
1032 RAACommitmentOrder::CommitmentFirst
1035 if let Some(ev) = msg_events.get(idx) {
1037 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1038 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1039 assert!(revoke_and_ack.is_none());
1040 revoke_and_ack = Some(msg.clone());
1042 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1043 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1044 assert!(commitment_update.is_none());
1045 commitment_update = Some(updates.clone());
1047 _ => panic!("Unexpected event"),
1051 (funding_locked, revoke_and_ack, commitment_update, order)
1056 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1057 /// for claims/fails they are separated out.
1058 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)) {
1059 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1060 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1061 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1062 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1064 if send_funding_locked.0 {
1065 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1067 for reestablish in reestablish_1.iter() {
1068 assert_eq!(reestablish.next_remote_commitment_number, 0);
1071 if send_funding_locked.1 {
1072 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1074 for reestablish in reestablish_2.iter() {
1075 assert_eq!(reestablish.next_remote_commitment_number, 0);
1078 if send_funding_locked.0 || send_funding_locked.1 {
1079 // If we expect any funding_locked's, both sides better have set
1080 // next_local_commitment_number to 1
1081 for reestablish in reestablish_1.iter() {
1082 assert_eq!(reestablish.next_local_commitment_number, 1);
1084 for reestablish in reestablish_2.iter() {
1085 assert_eq!(reestablish.next_local_commitment_number, 1);
1089 let mut resp_1 = Vec::new();
1090 for msg in reestablish_1 {
1091 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1092 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1094 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1095 check_added_monitors!(node_b, 1);
1097 check_added_monitors!(node_b, 0);
1100 let mut resp_2 = Vec::new();
1101 for msg in reestablish_2 {
1102 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1103 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1105 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1106 check_added_monitors!(node_a, 1);
1108 check_added_monitors!(node_a, 0);
1111 // We don't yet support both needing updates, as that would require a different commitment dance:
1112 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1113 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1115 for chan_msgs in resp_1.drain(..) {
1116 if send_funding_locked.0 {
1117 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1118 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1119 if !announcement_event.is_empty() {
1120 assert_eq!(announcement_event.len(), 1);
1121 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1122 //TODO: Test announcement_sigs re-sending
1123 } else { panic!("Unexpected event!"); }
1126 assert!(chan_msgs.0.is_none());
1129 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1130 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1131 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1132 check_added_monitors!(node_a, 1);
1134 assert!(chan_msgs.1.is_none());
1136 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1137 let commitment_update = chan_msgs.2.unwrap();
1138 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1139 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1141 assert!(commitment_update.update_add_htlcs.is_empty());
1143 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1144 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1145 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1146 for update_add in commitment_update.update_add_htlcs {
1147 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1149 for update_fulfill in commitment_update.update_fulfill_htlcs {
1150 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1152 for update_fail in commitment_update.update_fail_htlcs {
1153 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1156 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1157 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1159 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1160 check_added_monitors!(node_a, 1);
1161 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1162 // No commitment_signed so get_event_msg's assert(len == 1) passes
1163 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1164 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1165 check_added_monitors!(node_b, 1);
1168 assert!(chan_msgs.2.is_none());
1172 for chan_msgs in resp_2.drain(..) {
1173 if send_funding_locked.1 {
1174 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1175 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1176 if !announcement_event.is_empty() {
1177 assert_eq!(announcement_event.len(), 1);
1178 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1179 //TODO: Test announcement_sigs re-sending
1180 } else { panic!("Unexpected event!"); }
1183 assert!(chan_msgs.0.is_none());
1186 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1187 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1188 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1189 check_added_monitors!(node_b, 1);
1191 assert!(chan_msgs.1.is_none());
1193 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1194 let commitment_update = chan_msgs.2.unwrap();
1195 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1196 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1198 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1199 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1200 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1201 for update_add in commitment_update.update_add_htlcs {
1202 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1204 for update_fulfill in commitment_update.update_fulfill_htlcs {
1205 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1207 for update_fail in commitment_update.update_fail_htlcs {
1208 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1211 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1212 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1214 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1215 check_added_monitors!(node_b, 1);
1216 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1217 // No commitment_signed so get_event_msg's assert(len == 1) passes
1218 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1219 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1220 check_added_monitors!(node_a, 1);
1223 assert!(chan_msgs.2.is_none());