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};
11 use util::enforcing_trait_impls::EnforcingChannelKeys;
13 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
14 use util::errors::APIError;
15 use util::logger::Logger;
16 use util::config::UserConfig;
18 use bitcoin::util::hash::BitcoinHash;
19 use bitcoin::blockdata::block::BlockHeader;
20 use bitcoin::blockdata::transaction::{Transaction, TxOut};
21 use bitcoin::network::constants::Network;
23 use bitcoin_hashes::sha256::Hash as Sha256;
24 use bitcoin_hashes::sha256d::Hash as Sha256d;
25 use bitcoin_hashes::Hash;
27 use secp256k1::Secp256k1;
28 use secp256k1::key::PublicKey;
30 use rand::{thread_rng,Rng};
32 use std::cell::RefCell;
34 use std::sync::{Arc, Mutex};
37 pub const CHAN_CONFIRM_DEPTH: u32 = 100;
38 pub fn confirm_transaction(notifier: &chaininterface::BlockNotifier, chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
39 assert!(chain.does_match_tx(tx));
40 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
41 notifier.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
42 for i in 2..CHAN_CONFIRM_DEPTH {
43 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
44 notifier.block_connected_checked(&header, i, &vec![], &[0; 0]);
48 pub fn connect_blocks(notifier: &chaininterface::BlockNotifier, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
49 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 };
50 notifier.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
51 for i in 2..depth + 1 {
52 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
53 notifier.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
59 pub block_notifier: Arc<chaininterface::BlockNotifier>,
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<EnforcingChannelKeys>>,
66 pub node_seed: [u8; 32],
67 pub network_payment_count: Rc<RefCell<u8>>,
68 pub network_chan_count: Rc<RefCell<u32>>,
69 pub logger: Arc<test_utils::TestLogger>
73 if !::std::thread::panicking() {
74 // Check that we processed all pending events
75 assert!(self.node.get_and_clear_pending_msg_events().is_empty());
76 assert!(self.node.get_and_clear_pending_events().is_empty());
77 assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
82 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) {
83 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
86 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) {
87 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);
88 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
89 (announcement, as_update, bs_update, channel_id, tx)
92 macro_rules! get_revoke_commit_msgs {
93 ($node: expr, $node_id: expr) => {
95 let events = $node.node.get_and_clear_pending_msg_events();
96 assert_eq!(events.len(), 2);
98 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
99 assert_eq!(*node_id, $node_id);
102 _ => panic!("Unexpected event"),
104 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
105 assert_eq!(*node_id, $node_id);
106 assert!(updates.update_add_htlcs.is_empty());
107 assert!(updates.update_fulfill_htlcs.is_empty());
108 assert!(updates.update_fail_htlcs.is_empty());
109 assert!(updates.update_fail_malformed_htlcs.is_empty());
110 assert!(updates.update_fee.is_none());
111 updates.commitment_signed.clone()
113 _ => panic!("Unexpected event"),
119 macro_rules! get_event_msg {
120 ($node: expr, $event_type: path, $node_id: expr) => {
122 let events = $node.node.get_and_clear_pending_msg_events();
123 assert_eq!(events.len(), 1);
125 $event_type { ref node_id, ref msg } => {
126 assert_eq!(*node_id, $node_id);
129 _ => panic!("Unexpected event"),
135 macro_rules! get_htlc_update_msgs {
136 ($node: expr, $node_id: expr) => {
138 let events = $node.node.get_and_clear_pending_msg_events();
139 assert_eq!(events.len(), 1);
141 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
142 assert_eq!(*node_id, $node_id);
145 _ => panic!("Unexpected event"),
151 macro_rules! get_feerate {
152 ($node: expr, $channel_id: expr) => {
154 let chan_lock = $node.node.channel_state.lock().unwrap();
155 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
161 pub fn create_funding_transaction(node: &Node, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
162 let chan_id = *node.network_chan_count.borrow();
164 let events = node.node.get_and_clear_pending_events();
165 assert_eq!(events.len(), 1);
167 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
168 assert_eq!(*channel_value_satoshis, expected_chan_value);
169 assert_eq!(user_channel_id, expected_user_chan_id);
171 let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
172 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
174 let funding_outpoint = OutPoint::new(tx.txid(), 0);
175 (*temporary_channel_id, tx, funding_outpoint)
177 _ => panic!("Unexpected event"),
181 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 {
182 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
183 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();
184 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();
186 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
189 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
190 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
191 assert_eq!(added_monitors.len(), 1);
192 assert_eq!(added_monitors[0].0, funding_output);
193 added_monitors.clear();
196 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();
198 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
199 assert_eq!(added_monitors.len(), 1);
200 assert_eq!(added_monitors[0].0, funding_output);
201 added_monitors.clear();
204 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();
206 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
207 assert_eq!(added_monitors.len(), 1);
208 assert_eq!(added_monitors[0].0, funding_output);
209 added_monitors.clear();
212 let events_4 = node_a.node.get_and_clear_pending_events();
213 assert_eq!(events_4.len(), 1);
215 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
216 assert_eq!(user_channel_id, 42);
217 assert_eq!(*funding_txo, funding_output);
219 _ => panic!("Unexpected event"),
225 pub fn create_chan_between_nodes_with_value_confirm_first(node_recv: &Node, node_conf: &Node, tx: &Transaction) {
226 confirm_transaction(&node_conf.block_notifier, &node_conf.chain_monitor, &tx, tx.version);
227 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();
230 pub fn create_chan_between_nodes_with_value_confirm_second(node_recv: &Node, node_conf: &Node) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
232 let events_6 = node_conf.node.get_and_clear_pending_msg_events();
233 assert_eq!(events_6.len(), 2);
234 ((match events_6[0] {
235 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
236 channel_id = msg.channel_id.clone();
237 assert_eq!(*node_id, node_recv.node.get_our_node_id());
240 _ => panic!("Unexpected event"),
241 }, match events_6[1] {
242 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
243 assert_eq!(*node_id, node_recv.node.get_our_node_id());
246 _ => panic!("Unexpected event"),
250 pub fn create_chan_between_nodes_with_value_confirm(node_a: &Node, node_b: &Node, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
251 create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
252 confirm_transaction(&node_a.block_notifier, &node_a.chain_monitor, &tx, tx.version);
253 create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
256 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) {
257 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
258 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
262 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) {
263 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0).unwrap();
264 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
265 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1).unwrap();
267 let events_7 = node_b.node.get_and_clear_pending_msg_events();
268 assert_eq!(events_7.len(), 1);
269 let (announcement, bs_update) = match events_7[0] {
270 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
273 _ => panic!("Unexpected event"),
276 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs).unwrap();
277 let events_8 = node_a.node.get_and_clear_pending_msg_events();
278 assert_eq!(events_8.len(), 1);
279 let as_update = match events_8[0] {
280 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
281 assert!(*announcement == *msg);
282 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
283 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
286 _ => panic!("Unexpected event"),
289 *node_a.network_chan_count.borrow_mut() += 1;
291 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
294 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) {
295 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
298 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) {
299 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
301 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
302 node.router.handle_channel_update(&chan_announcement.1).unwrap();
303 node.router.handle_channel_update(&chan_announcement.2).unwrap();
305 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
308 macro_rules! check_spends {
309 ($tx: expr, $spends_tx: expr) => {
311 $tx.verify(|out_point| {
312 if out_point.txid == $spends_tx.txid() {
313 $spends_tx.output.get(out_point.vout as usize).cloned()
322 macro_rules! get_closing_signed_broadcast {
323 ($node: expr, $dest_pubkey: expr) => {
325 let events = $node.get_and_clear_pending_msg_events();
326 assert!(events.len() == 1 || events.len() == 2);
327 (match events[events.len() - 1] {
328 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
329 assert_eq!(msg.contents.flags & 2, 2);
332 _ => panic!("Unexpected event"),
333 }, if events.len() == 2 {
335 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
336 assert_eq!(*node_id, $dest_pubkey);
339 _ => panic!("Unexpected event"),
346 macro_rules! check_closed_broadcast {
348 let events = $node.node.get_and_clear_pending_msg_events();
349 assert_eq!(events.len(), 1);
351 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
352 assert_eq!(msg.contents.flags & 2, 2);
354 _ => panic!("Unexpected event"),
359 pub fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
360 let (node_a, broadcaster_a, struct_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster, inbound_node) } else { (&outbound_node.node, &outbound_node.tx_broadcaster, outbound_node) };
361 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
364 node_a.close_channel(channel_id).unwrap();
365 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id())).unwrap();
367 let events_1 = node_b.get_and_clear_pending_msg_events();
368 assert!(events_1.len() >= 1);
369 let shutdown_b = match events_1[0] {
370 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
371 assert_eq!(node_id, &node_a.get_our_node_id());
374 _ => panic!("Unexpected event"),
377 let closing_signed_b = if !close_inbound_first {
378 assert_eq!(events_1.len(), 1);
381 Some(match events_1[1] {
382 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
383 assert_eq!(node_id, &node_a.get_our_node_id());
386 _ => panic!("Unexpected event"),
390 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b).unwrap();
391 let (as_update, bs_update) = if close_inbound_first {
392 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
393 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
394 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
395 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
396 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
398 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
399 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
400 assert!(none_b.is_none());
401 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
402 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
403 (as_update, bs_update)
405 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
407 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a).unwrap();
408 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
409 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
410 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
412 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
413 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
414 assert!(none_a.is_none());
415 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
416 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
417 (as_update, bs_update)
419 assert_eq!(tx_a, tx_b);
420 check_spends!(tx_a, funding_tx);
422 (as_update, bs_update, tx_a)
425 pub struct SendEvent {
426 pub node_id: PublicKey,
427 pub msgs: Vec<msgs::UpdateAddHTLC>,
428 pub commitment_msg: msgs::CommitmentSigned,
431 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
432 assert!(updates.update_fulfill_htlcs.is_empty());
433 assert!(updates.update_fail_htlcs.is_empty());
434 assert!(updates.update_fail_malformed_htlcs.is_empty());
435 assert!(updates.update_fee.is_none());
436 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
439 pub fn from_event(event: MessageSendEvent) -> SendEvent {
441 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
442 _ => panic!("Unexpected event type!"),
446 pub fn from_node(node: &Node) -> SendEvent {
447 let mut events = node.node.get_and_clear_pending_msg_events();
448 assert_eq!(events.len(), 1);
449 SendEvent::from_event(events.pop().unwrap())
453 macro_rules! check_added_monitors {
454 ($node: expr, $count: expr) => {
456 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
457 assert_eq!(added_monitors.len(), $count);
458 added_monitors.clear();
463 macro_rules! commitment_signed_dance {
464 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
466 check_added_monitors!($node_a, 0);
467 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
468 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
469 check_added_monitors!($node_a, 1);
470 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
473 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
475 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
476 check_added_monitors!($node_b, 0);
477 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
478 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
479 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
480 check_added_monitors!($node_b, 1);
481 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed).unwrap();
482 let (bs_revoke_and_ack, extra_msg_option) = {
483 let events = $node_b.node.get_and_clear_pending_msg_events();
484 assert!(events.len() <= 2);
486 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
487 assert_eq!(*node_id, $node_a.node.get_our_node_id());
490 _ => panic!("Unexpected event"),
491 }, events.get(1).map(|e| e.clone()))
493 check_added_monitors!($node_b, 1);
495 assert!($node_a.node.get_and_clear_pending_events().is_empty());
496 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
498 (extra_msg_option, bs_revoke_and_ack)
501 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
503 check_added_monitors!($node_a, 0);
504 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
505 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed).unwrap();
506 check_added_monitors!($node_a, 1);
507 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
508 assert!(extra_msg_option.is_none());
512 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
514 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
515 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
516 check_added_monitors!($node_a, 1);
520 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
522 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
525 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
527 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
529 expect_pending_htlcs_forwardable!($node_a);
530 check_added_monitors!($node_a, 1);
532 let channel_state = $node_a.node.channel_state.lock().unwrap();
533 assert_eq!(channel_state.pending_msg_events.len(), 1);
534 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
535 assert_ne!(*node_id, $node_b.node.get_our_node_id());
536 } else { panic!("Unexpected event"); }
538 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
544 macro_rules! get_payment_preimage_hash {
547 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
548 *$node.network_payment_count.borrow_mut() += 1;
549 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
550 (payment_preimage, payment_hash)
555 macro_rules! expect_pending_htlcs_forwardable {
557 let events = $node.node.get_and_clear_pending_events();
558 assert_eq!(events.len(), 1);
560 Event::PendingHTLCsForwardable { .. } => { },
561 _ => panic!("Unexpected event"),
563 $node.node.process_pending_htlc_forwards();
567 macro_rules! expect_payment_received {
568 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
569 let events = $node.node.get_and_clear_pending_events();
570 assert_eq!(events.len(), 1);
572 Event::PaymentReceived { ref payment_hash, amt } => {
573 assert_eq!($expected_payment_hash, *payment_hash);
574 assert_eq!($expected_recv_value, amt);
576 _ => panic!("Unexpected event"),
581 macro_rules! expect_payment_sent {
582 ($node: expr, $expected_payment_preimage: expr) => {
583 let events = $node.node.get_and_clear_pending_events();
584 assert_eq!(events.len(), 1);
586 Event::PaymentSent { ref payment_preimage } => {
587 assert_eq!($expected_payment_preimage, *payment_preimage);
589 _ => panic!("Unexpected event"),
594 pub fn send_along_route_with_hash(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64, our_payment_hash: PaymentHash) {
595 let mut payment_event = {
596 origin_node.node.send_payment(route, our_payment_hash).unwrap();
597 check_added_monitors!(origin_node, 1);
599 let mut events = origin_node.node.get_and_clear_pending_msg_events();
600 assert_eq!(events.len(), 1);
601 SendEvent::from_event(events.remove(0))
603 let mut prev_node = origin_node;
605 for (idx, &node) in expected_route.iter().enumerate() {
606 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
608 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
609 check_added_monitors!(node, 0);
610 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
612 expect_pending_htlcs_forwardable!(node);
614 if idx == expected_route.len() - 1 {
615 let events_2 = node.node.get_and_clear_pending_events();
616 assert_eq!(events_2.len(), 1);
618 Event::PaymentReceived { ref payment_hash, amt } => {
619 assert_eq!(our_payment_hash, *payment_hash);
620 assert_eq!(amt, recv_value);
622 _ => panic!("Unexpected event"),
625 let mut events_2 = node.node.get_and_clear_pending_msg_events();
626 assert_eq!(events_2.len(), 1);
627 check_added_monitors!(node, 1);
628 payment_event = SendEvent::from_event(events_2.remove(0));
629 assert_eq!(payment_event.msgs.len(), 1);
636 pub fn send_along_route(origin_node: &Node, route: Route, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
637 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
638 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
639 (our_payment_preimage, our_payment_hash)
642 pub fn claim_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_preimage: PaymentPreimage, expected_amount: u64) {
643 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage, expected_amount));
644 check_added_monitors!(expected_route.last().unwrap(), 1);
646 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
647 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
648 macro_rules! get_next_msgs {
651 let events = $node.node.get_and_clear_pending_msg_events();
652 assert_eq!(events.len(), 1);
654 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
655 assert!(update_add_htlcs.is_empty());
656 assert_eq!(update_fulfill_htlcs.len(), 1);
657 assert!(update_fail_htlcs.is_empty());
658 assert!(update_fail_malformed_htlcs.is_empty());
659 assert!(update_fee.is_none());
660 expected_next_node = node_id.clone();
661 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
663 _ => panic!("Unexpected event"),
669 macro_rules! last_update_fulfill_dance {
670 ($node: expr, $prev_node: expr) => {
672 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
673 check_added_monitors!($node, 0);
674 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
675 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
679 macro_rules! mid_update_fulfill_dance {
680 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
682 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
683 check_added_monitors!($node, 1);
684 let new_next_msgs = if $new_msgs {
685 get_next_msgs!($node)
687 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
690 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
691 next_msgs = new_next_msgs;
696 let mut prev_node = expected_route.last().unwrap();
697 for (idx, node) in expected_route.iter().rev().enumerate() {
698 assert_eq!(expected_next_node, node.node.get_our_node_id());
699 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
700 if next_msgs.is_some() {
701 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
702 } else if update_next_msgs {
703 next_msgs = get_next_msgs!(node);
705 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
707 if !skip_last && idx == expected_route.len() - 1 {
708 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
715 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
716 expect_payment_sent!(origin_node, our_payment_preimage);
720 pub fn claim_payment(origin_node: &Node, expected_route: &[&Node], our_payment_preimage: PaymentPreimage, expected_amount: u64) {
721 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
724 pub const TEST_FINAL_CLTV: u32 = 32;
726 pub fn route_payment(origin_node: &Node, expected_route: &[&Node], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
727 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
728 assert_eq!(route.hops.len(), expected_route.len());
729 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
730 assert_eq!(hop.pubkey, node.node.get_our_node_id());
733 send_along_route(origin_node, route, expected_route, recv_value)
736 pub fn route_over_limit(origin_node: &Node, expected_route: &[&Node], recv_value: u64) {
737 let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap();
738 assert_eq!(route.hops.len(), expected_route.len());
739 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
740 assert_eq!(hop.pubkey, node.node.get_our_node_id());
743 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
745 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
747 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
748 _ => panic!("Unknown error variants"),
752 pub fn send_payment(origin: &Node, expected_route: &[&Node], recv_value: u64, expected_value: u64) {
753 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
754 claim_payment(&origin, expected_route, our_payment_preimage, expected_value);
757 pub fn fail_payment_along_route(origin_node: &Node, expected_route: &[&Node], skip_last: bool, our_payment_hash: PaymentHash) {
758 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
759 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
760 check_added_monitors!(expected_route.last().unwrap(), 1);
762 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
763 macro_rules! update_fail_dance {
764 ($node: expr, $prev_node: expr, $last_node: expr) => {
766 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0).unwrap();
767 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
768 if skip_last && $last_node {
769 expect_pending_htlcs_forwardable!($node);
775 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
776 let mut prev_node = expected_route.last().unwrap();
777 for (idx, node) in expected_route.iter().rev().enumerate() {
778 assert_eq!(expected_next_node, node.node.get_our_node_id());
779 if next_msgs.is_some() {
780 // We may be the "last node" for the purpose of the commitment dance if we're
781 // skipping the last node (implying it is disconnected) and we're the
782 // second-to-last node!
783 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
786 let events = node.node.get_and_clear_pending_msg_events();
787 if !skip_last || idx != expected_route.len() - 1 {
788 assert_eq!(events.len(), 1);
790 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
791 assert!(update_add_htlcs.is_empty());
792 assert!(update_fulfill_htlcs.is_empty());
793 assert_eq!(update_fail_htlcs.len(), 1);
794 assert!(update_fail_malformed_htlcs.is_empty());
795 assert!(update_fee.is_none());
796 expected_next_node = node_id.clone();
797 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
799 _ => panic!("Unexpected event"),
802 assert!(events.is_empty());
804 if !skip_last && idx == expected_route.len() - 1 {
805 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
812 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
814 let events = origin_node.node.get_and_clear_pending_events();
815 assert_eq!(events.len(), 1);
817 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
818 assert_eq!(payment_hash, our_payment_hash);
819 assert!(rejected_by_dest);
821 _ => panic!("Unexpected event"),
826 pub fn fail_payment(origin_node: &Node, expected_route: &[&Node], our_payment_hash: PaymentHash) {
827 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
830 pub fn create_network(node_count: usize, node_config: &[Option<UserConfig>]) -> Vec<Node> {
831 let mut nodes = Vec::new();
832 let mut rng = thread_rng();
833 let secp_ctx = Secp256k1::new();
835 let chan_count = Rc::new(RefCell::new(0));
836 let payment_count = Rc::new(RefCell::new(0));
838 for i in 0..node_count {
839 let test_logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
840 let logger = &(Arc::clone(&test_logger) as Arc<Logger>);
841 let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
842 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
843 let block_notifier = Arc::new(chaininterface::BlockNotifier::new(chain_monitor.clone()));
844 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
845 let mut seed = [0; 32];
846 rng.fill_bytes(&mut seed);
847 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
848 let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
849 let weak_res = Arc::downgrade(&chan_monitor.simple_monitor);
850 block_notifier.register_listener(weak_res);
851 let mut default_config = UserConfig::default();
852 default_config.channel_options.announced_channel = true;
853 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
854 let node = ChannelManager::new(Network::Testnet, feeest.clone(), chan_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 }, 0).unwrap();
855 let weak_res = Arc::downgrade(&node);
856 block_notifier.register_listener(weak_res);
857 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()), chain_monitor.clone(), Arc::clone(&logger));
858 nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router, keys_manager, node_seed: seed,
859 network_payment_count: payment_count.clone(),
860 network_chan_count: chan_count.clone(),
870 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
871 /// Tests that the given node has broadcast transactions for the given Channel
873 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
874 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
875 /// broadcast and the revoked outputs were claimed.
877 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
878 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
880 /// All broadcast transactions must be accounted for in one of the above three types of we'll
882 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> {
883 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
884 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
886 let mut res = Vec::with_capacity(2);
887 node_txn.retain(|tx| {
888 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
889 check_spends!(tx, chan.3.clone());
890 if commitment_tx.is_none() {
891 res.push(tx.clone());
896 if let Some(explicit_tx) = commitment_tx {
897 res.push(explicit_tx.clone());
900 assert_eq!(res.len(), 1);
902 if has_htlc_tx != HTLCType::NONE {
903 node_txn.retain(|tx| {
904 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
905 check_spends!(tx, res[0].clone());
906 if has_htlc_tx == HTLCType::TIMEOUT {
907 assert!(tx.lock_time != 0);
909 assert!(tx.lock_time == 0);
911 res.push(tx.clone());
915 assert!(res.len() == 2 || res.len() == 3);
917 assert_eq!(res[1], res[2]);
921 assert!(node_txn.is_empty());
925 /// Tests that the given node has broadcast a claim transaction against the provided revoked
926 /// HTLC transaction.
927 pub fn test_revoked_htlc_claim_txn_broadcast(node: &Node, revoked_tx: Transaction, commitment_revoked_tx: Transaction) {
928 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
929 // We should issue a 2nd transaction if one htlc is dropped from initial claiming tx
930 // but sometimes not as feerate is too-low
931 if node_txn.len() != 1 && node_txn.len() != 2 { assert!(false); }
932 node_txn.retain(|tx| {
933 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
934 check_spends!(tx, revoked_tx);
938 node_txn.retain(|tx| {
939 check_spends!(tx, commitment_revoked_tx);
942 assert!(node_txn.is_empty());
945 pub fn check_preimage_claim(node: &Node, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
946 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
948 assert!(node_txn.len() >= 1);
949 assert_eq!(node_txn[0].input.len(), 1);
950 let mut found_prev = false;
953 if node_txn[0].input[0].previous_output.txid == tx.txid() {
954 check_spends!(node_txn[0], tx.clone());
955 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
956 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
964 let mut res = Vec::new();
965 mem::swap(&mut *node_txn, &mut res);
969 pub fn get_announce_close_broadcast_events(nodes: &Vec<Node>, a: usize, b: usize) {
970 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
971 assert_eq!(events_1.len(), 1);
972 let as_update = match events_1[0] {
973 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
976 _ => panic!("Unexpected event"),
979 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
980 assert_eq!(events_2.len(), 1);
981 let bs_update = match events_2[0] {
982 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
985 _ => panic!("Unexpected event"),
989 node.router.handle_channel_update(&as_update).unwrap();
990 node.router.handle_channel_update(&bs_update).unwrap();
994 macro_rules! get_channel_value_stat {
995 ($node: expr, $channel_id: expr) => {{
996 let chan_lock = $node.node.channel_state.lock().unwrap();
997 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
998 chan.get_value_stat()
1002 macro_rules! get_chan_reestablish_msgs {
1003 ($src_node: expr, $dst_node: expr) => {
1005 let mut res = Vec::with_capacity(1);
1006 for msg in $src_node.node.get_and_clear_pending_msg_events() {
1007 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
1008 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1009 res.push(msg.clone());
1011 panic!("Unexpected event")
1019 macro_rules! handle_chan_reestablish_msgs {
1020 ($src_node: expr, $dst_node: expr) => {
1022 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1024 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1026 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1032 let mut revoke_and_ack = None;
1033 let mut commitment_update = None;
1034 let order = 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 revoke_and_ack = Some(msg.clone());
1040 RAACommitmentOrder::RevokeAndACKFirst
1042 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1043 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1044 commitment_update = Some(updates.clone());
1045 RAACommitmentOrder::CommitmentFirst
1047 _ => panic!("Unexpected event"),
1050 RAACommitmentOrder::CommitmentFirst
1053 if let Some(ev) = msg_events.get(idx) {
1055 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1056 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1057 assert!(revoke_and_ack.is_none());
1058 revoke_and_ack = Some(msg.clone());
1060 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1061 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1062 assert!(commitment_update.is_none());
1063 commitment_update = Some(updates.clone());
1065 _ => panic!("Unexpected event"),
1069 (funding_locked, revoke_and_ack, commitment_update, order)
1074 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1075 /// for claims/fails they are separated out.
1076 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)) {
1077 node_a.node.peer_connected(&node_b.node.get_our_node_id());
1078 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1079 node_b.node.peer_connected(&node_a.node.get_our_node_id());
1080 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1082 if send_funding_locked.0 {
1083 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1085 for reestablish in reestablish_1.iter() {
1086 assert_eq!(reestablish.next_remote_commitment_number, 0);
1089 if send_funding_locked.1 {
1090 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1092 for reestablish in reestablish_2.iter() {
1093 assert_eq!(reestablish.next_remote_commitment_number, 0);
1096 if send_funding_locked.0 || send_funding_locked.1 {
1097 // If we expect any funding_locked's, both sides better have set
1098 // next_local_commitment_number to 1
1099 for reestablish in reestablish_1.iter() {
1100 assert_eq!(reestablish.next_local_commitment_number, 1);
1102 for reestablish in reestablish_2.iter() {
1103 assert_eq!(reestablish.next_local_commitment_number, 1);
1107 let mut resp_1 = Vec::new();
1108 for msg in reestablish_1 {
1109 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg).unwrap();
1110 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1112 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1113 check_added_monitors!(node_b, 1);
1115 check_added_monitors!(node_b, 0);
1118 let mut resp_2 = Vec::new();
1119 for msg in reestablish_2 {
1120 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg).unwrap();
1121 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1123 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1124 check_added_monitors!(node_a, 1);
1126 check_added_monitors!(node_a, 0);
1129 // We don't yet support both needing updates, as that would require a different commitment dance:
1130 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1131 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1133 for chan_msgs in resp_1.drain(..) {
1134 if send_funding_locked.0 {
1135 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1136 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1137 if !announcement_event.is_empty() {
1138 assert_eq!(announcement_event.len(), 1);
1139 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1140 //TODO: Test announcement_sigs re-sending
1141 } else { panic!("Unexpected event!"); }
1144 assert!(chan_msgs.0.is_none());
1147 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1148 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1149 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1150 check_added_monitors!(node_a, 1);
1152 assert!(chan_msgs.1.is_none());
1154 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1155 let commitment_update = chan_msgs.2.unwrap();
1156 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1157 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1159 assert!(commitment_update.update_add_htlcs.is_empty());
1161 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1162 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1163 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1164 for update_add in commitment_update.update_add_htlcs {
1165 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add).unwrap();
1167 for update_fulfill in commitment_update.update_fulfill_htlcs {
1168 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill).unwrap();
1170 for update_fail in commitment_update.update_fail_htlcs {
1171 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail).unwrap();
1174 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1175 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1177 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1178 check_added_monitors!(node_a, 1);
1179 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1180 // No commitment_signed so get_event_msg's assert(len == 1) passes
1181 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1182 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1183 check_added_monitors!(node_b, 1);
1186 assert!(chan_msgs.2.is_none());
1190 for chan_msgs in resp_2.drain(..) {
1191 if send_funding_locked.1 {
1192 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap()).unwrap();
1193 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1194 if !announcement_event.is_empty() {
1195 assert_eq!(announcement_event.len(), 1);
1196 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1197 //TODO: Test announcement_sigs re-sending
1198 } else { panic!("Unexpected event!"); }
1201 assert!(chan_msgs.0.is_none());
1204 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1205 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap()).unwrap();
1206 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1207 check_added_monitors!(node_b, 1);
1209 assert!(chan_msgs.1.is_none());
1211 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1212 let commitment_update = chan_msgs.2.unwrap();
1213 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1214 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1216 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1217 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1218 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1219 for update_add in commitment_update.update_add_htlcs {
1220 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add).unwrap();
1222 for update_fulfill in commitment_update.update_fulfill_htlcs {
1223 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill).unwrap();
1225 for update_fail in commitment_update.update_fail_htlcs {
1226 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail).unwrap();
1229 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1230 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1232 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
1233 check_added_monitors!(node_b, 1);
1234 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1235 // No commitment_signed so get_event_msg's assert(len == 1) passes
1236 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1237 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1238 check_added_monitors!(node_a, 1);
1241 assert!(chan_msgs.2.is_none());