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, PaymentSendFailure};
8 use ln::router::{Route, Router};
9 use ln::features::InitFeatures;
11 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler};
12 use util::enforcing_trait_impls::EnforcingChannelKeys;
14 use util::test_utils::TestChannelMonitor;
15 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
16 use util::errors::APIError;
17 use util::logger::Logger;
18 use util::config::UserConfig;
20 use bitcoin::util::hash::BitcoinHash;
21 use bitcoin::blockdata::block::BlockHeader;
22 use bitcoin::blockdata::transaction::{Transaction, TxOut};
23 use bitcoin::network::constants::Network;
25 use bitcoin_hashes::sha256::Hash as Sha256;
26 use bitcoin_hashes::sha256d::Hash as Sha256d;
27 use bitcoin_hashes::Hash;
29 use secp256k1::Secp256k1;
30 use secp256k1::key::PublicKey;
32 use rand::{thread_rng,Rng};
34 use std::cell::RefCell;
36 use std::sync::{Arc, Mutex};
39 pub const CHAN_CONFIRM_DEPTH: u32 = 100;
40 pub fn confirm_transaction<'a, 'b: 'a>(notifier: &'a chaininterface::BlockNotifierRef<'b>, chain: &chaininterface::ChainWatchInterfaceUtil, tx: &Transaction, chan_id: u32) {
41 assert!(chain.does_match_tx(tx));
42 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
43 notifier.block_connected_checked(&header, 1, &[tx; 1], &[chan_id; 1]);
44 for i in 2..CHAN_CONFIRM_DEPTH {
45 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
46 notifier.block_connected_checked(&header, i, &vec![], &[0; 0]);
50 pub fn connect_blocks<'a, 'b>(notifier: &'a chaininterface::BlockNotifierRef<'b>, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) -> Sha256d {
51 let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
52 notifier.block_connected_checked(&header, height + 1, &Vec::new(), &Vec::new());
53 for i in 2..depth + 1 {
54 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
55 notifier.block_connected_checked(&header, height + i, &Vec::new(), &Vec::new());
61 pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
62 pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
63 pub fee_estimator: Arc<test_utils::TestFeeEstimator>,
64 pub chan_monitor: test_utils::TestChannelMonitor,
65 pub keys_manager: Arc<test_utils::TestKeysInterface>,
66 pub logger: Arc<test_utils::TestLogger>,
67 pub node_seed: [u8; 32],
70 pub struct Node<'a, 'b: 'a> {
71 pub block_notifier: chaininterface::BlockNotifierRef<'b>,
72 pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
73 pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
74 pub chan_monitor: &'b test_utils::TestChannelMonitor,
75 pub keys_manager: Arc<test_utils::TestKeysInterface>,
76 pub node: &'a ChannelManager<EnforcingChannelKeys, &'b TestChannelMonitor>,
78 pub node_seed: [u8; 32],
79 pub network_payment_count: Rc<RefCell<u8>>,
80 pub network_chan_count: Rc<RefCell<u32>>,
81 pub logger: Arc<test_utils::TestLogger>
84 impl<'a, 'b> Drop for Node<'a, 'b> {
86 if !::std::thread::panicking() {
87 // Check that we processed all pending events
88 assert!(self.node.get_and_clear_pending_msg_events().is_empty());
89 assert!(self.node.get_and_clear_pending_events().is_empty());
90 assert!(self.chan_monitor.added_monitors.lock().unwrap().is_empty());
95 pub fn create_chan_between_nodes<'a, 'b, 'c>(node_a: &'a Node<'b, 'c>, node_b: &'a Node<'b, 'c>, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
96 create_chan_between_nodes_with_value(node_a, node_b, 100000, 10001, a_flags, b_flags)
99 pub fn create_chan_between_nodes_with_value<'a, 'b, 'c>(node_a: &'a Node<'b, 'c>, node_b: &'a Node<'b, 'c>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
100 let (funding_locked, channel_id, tx) = create_chan_between_nodes_with_value_a(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
101 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(node_a, node_b, &funding_locked);
102 (announcement, as_update, bs_update, channel_id, tx)
105 macro_rules! get_revoke_commit_msgs {
106 ($node: expr, $node_id: expr) => {
108 let events = $node.node.get_and_clear_pending_msg_events();
109 assert_eq!(events.len(), 2);
111 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
112 assert_eq!(*node_id, $node_id);
115 _ => panic!("Unexpected event"),
117 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
118 assert_eq!(*node_id, $node_id);
119 assert!(updates.update_add_htlcs.is_empty());
120 assert!(updates.update_fulfill_htlcs.is_empty());
121 assert!(updates.update_fail_htlcs.is_empty());
122 assert!(updates.update_fail_malformed_htlcs.is_empty());
123 assert!(updates.update_fee.is_none());
124 updates.commitment_signed.clone()
126 _ => panic!("Unexpected event"),
132 macro_rules! get_event_msg {
133 ($node: expr, $event_type: path, $node_id: expr) => {
135 let events = $node.node.get_and_clear_pending_msg_events();
136 assert_eq!(events.len(), 1);
138 $event_type { ref node_id, ref msg } => {
139 assert_eq!(*node_id, $node_id);
142 _ => panic!("Unexpected event"),
148 macro_rules! get_htlc_update_msgs {
149 ($node: expr, $node_id: expr) => {
151 let events = $node.node.get_and_clear_pending_msg_events();
152 assert_eq!(events.len(), 1);
154 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
155 assert_eq!(*node_id, $node_id);
158 _ => panic!("Unexpected event"),
164 macro_rules! get_feerate {
165 ($node: expr, $channel_id: expr) => {
167 let chan_lock = $node.node.channel_state.lock().unwrap();
168 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
174 macro_rules! unwrap_send_err {
175 ($res: expr, $all_failed: expr, $type: pat, $check: expr) => {
177 &Err(PaymentSendFailure::AllFailedRetrySafe(ref fails)) if $all_failed => {
178 assert_eq!(fails.len(), 1);
184 &Err(PaymentSendFailure::PartialFailure(ref fails)) if !$all_failed => {
185 assert_eq!(fails.len(), 1);
187 Err($type) => { $check },
196 pub fn create_funding_transaction<'a, 'b>(node: &Node<'a, 'b>, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
197 let chan_id = *node.network_chan_count.borrow();
199 let events = node.node.get_and_clear_pending_events();
200 assert_eq!(events.len(), 1);
202 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
203 assert_eq!(*channel_value_satoshis, expected_chan_value);
204 assert_eq!(user_channel_id, expected_user_chan_id);
206 let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
207 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
209 let funding_outpoint = OutPoint::new(tx.txid(), 0);
210 (*temporary_channel_id, tx, funding_outpoint)
212 _ => panic!("Unexpected event"),
216 pub fn create_chan_between_nodes_with_value_init<'a, 'b>(node_a: &Node<'a, 'b>, node_b: &Node<'a, 'b>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> Transaction {
217 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
218 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()));
219 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()));
221 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
224 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
225 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
226 assert_eq!(added_monitors.len(), 1);
227 assert_eq!(added_monitors[0].0, funding_output);
228 added_monitors.clear();
231 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()));
233 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
234 assert_eq!(added_monitors.len(), 1);
235 assert_eq!(added_monitors[0].0, funding_output);
236 added_monitors.clear();
239 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()));
241 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
242 assert_eq!(added_monitors.len(), 1);
243 assert_eq!(added_monitors[0].0, funding_output);
244 added_monitors.clear();
247 let events_4 = node_a.node.get_and_clear_pending_events();
248 assert_eq!(events_4.len(), 1);
250 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
251 assert_eq!(user_channel_id, 42);
252 assert_eq!(*funding_txo, funding_output);
254 _ => panic!("Unexpected event"),
260 pub fn create_chan_between_nodes_with_value_confirm_first<'a, 'b, 'c>(node_recv: &'a Node<'a, 'b>, node_conf: &'a Node<'a, 'b>, tx: &Transaction) {
261 confirm_transaction(&node_conf.block_notifier, &node_conf.chain_monitor, &tx, tx.version);
262 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()));
265 pub fn create_chan_between_nodes_with_value_confirm_second<'a, 'b>(node_recv: &Node<'a, 'b>, node_conf: &Node<'a, 'b>) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
267 let events_6 = node_conf.node.get_and_clear_pending_msg_events();
268 assert_eq!(events_6.len(), 2);
269 ((match events_6[0] {
270 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
271 channel_id = msg.channel_id.clone();
272 assert_eq!(*node_id, node_recv.node.get_our_node_id());
275 _ => panic!("Unexpected event"),
276 }, match events_6[1] {
277 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
278 assert_eq!(*node_id, node_recv.node.get_our_node_id());
281 _ => panic!("Unexpected event"),
285 pub fn create_chan_between_nodes_with_value_confirm<'a, 'b, 'c>(node_a: &'a Node<'b, 'c>, node_b: &'a Node<'b, 'c>, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
286 create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
287 confirm_transaction(&node_a.block_notifier, &node_a.chain_monitor, &tx, tx.version);
288 create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
291 pub fn create_chan_between_nodes_with_value_a<'a, 'b, 'c>(node_a: &'a Node<'b, 'c>, node_b: &'a Node<'b, 'c>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32], Transaction) {
292 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
293 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
297 pub fn create_chan_between_nodes_with_value_b<'a, 'b>(node_a: &Node<'a, 'b>, node_b: &Node<'a, 'b>, as_funding_msgs: &(msgs::FundingLocked, msgs::AnnouncementSignatures)) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
298 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0);
299 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
300 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1);
302 let events_7 = node_b.node.get_and_clear_pending_msg_events();
303 assert_eq!(events_7.len(), 1);
304 let (announcement, bs_update) = match events_7[0] {
305 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
308 _ => panic!("Unexpected event"),
311 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs);
312 let events_8 = node_a.node.get_and_clear_pending_msg_events();
313 assert_eq!(events_8.len(), 1);
314 let as_update = match events_8[0] {
315 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
316 assert!(*announcement == *msg);
317 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
318 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
321 _ => panic!("Unexpected event"),
324 *node_a.network_chan_count.borrow_mut() += 1;
326 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
329 pub fn create_announced_chan_between_nodes<'a, 'b, 'c>(nodes: &'a Vec<Node<'b, 'c>>, a: usize, b: usize, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
330 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
333 pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c>(nodes: &'a Vec<Node<'b, 'c>>, a: usize, b: usize, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
334 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
336 nodes[a].node.broadcast_node_announcement([0, 0, 0], [0; 32], msgs::NetAddressSet::new());
337 let a_events = nodes[a].node.get_and_clear_pending_msg_events();
338 assert_eq!(a_events.len(), 1);
339 let a_node_announcement = match a_events[0] {
340 MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
343 _ => panic!("Unexpected event"),
346 nodes[b].node.broadcast_node_announcement([1, 1, 1], [1; 32], msgs::NetAddressSet::new());
347 let b_events = nodes[b].node.get_and_clear_pending_msg_events();
348 assert_eq!(b_events.len(), 1);
349 let b_node_announcement = match b_events[0] {
350 MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => {
353 _ => panic!("Unexpected event"),
357 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
358 node.router.handle_channel_update(&chan_announcement.1).unwrap();
359 node.router.handle_channel_update(&chan_announcement.2).unwrap();
360 node.router.handle_node_announcement(&a_node_announcement).unwrap();
361 node.router.handle_node_announcement(&b_node_announcement).unwrap();
363 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
366 macro_rules! check_spends {
367 ($tx: expr, $spends_tx: expr) => {
369 $tx.verify(|out_point| {
370 if out_point.txid == $spends_tx.txid() {
371 $spends_tx.output.get(out_point.vout as usize).cloned()
380 macro_rules! get_closing_signed_broadcast {
381 ($node: expr, $dest_pubkey: expr) => {
383 let events = $node.get_and_clear_pending_msg_events();
384 assert!(events.len() == 1 || events.len() == 2);
385 (match events[events.len() - 1] {
386 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
387 assert_eq!(msg.contents.flags & 2, 2);
390 _ => panic!("Unexpected event"),
391 }, if events.len() == 2 {
393 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
394 assert_eq!(*node_id, $dest_pubkey);
397 _ => panic!("Unexpected event"),
404 macro_rules! check_closed_broadcast {
405 ($node: expr, $with_error_msg: expr) => {{
406 let events = $node.node.get_and_clear_pending_msg_events();
407 assert_eq!(events.len(), if $with_error_msg { 2 } else { 1 });
409 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
410 assert_eq!(msg.contents.flags & 2, 2);
412 _ => panic!("Unexpected event"),
416 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
417 // TODO: Check node_id
420 _ => panic!("Unexpected event"),
426 pub fn close_channel<'a, 'b>(outbound_node: &Node<'a, 'b>, inbound_node: &Node<'a, 'b>, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Transaction) {
427 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) };
428 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
431 node_a.close_channel(channel_id).unwrap();
432 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id()));
434 let events_1 = node_b.get_and_clear_pending_msg_events();
435 assert!(events_1.len() >= 1);
436 let shutdown_b = match events_1[0] {
437 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
438 assert_eq!(node_id, &node_a.get_our_node_id());
441 _ => panic!("Unexpected event"),
444 let closing_signed_b = if !close_inbound_first {
445 assert_eq!(events_1.len(), 1);
448 Some(match events_1[1] {
449 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
450 assert_eq!(node_id, &node_a.get_our_node_id());
453 _ => panic!("Unexpected event"),
457 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b);
458 let (as_update, bs_update) = if close_inbound_first {
459 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
460 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap());
461 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
462 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
463 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
465 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap());
466 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
467 assert!(none_b.is_none());
468 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
469 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
470 (as_update, bs_update)
472 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
474 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a);
475 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
476 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
477 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
479 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap());
480 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
481 assert!(none_a.is_none());
482 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
483 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
484 (as_update, bs_update)
486 assert_eq!(tx_a, tx_b);
487 check_spends!(tx_a, funding_tx);
489 (as_update, bs_update, tx_a)
492 pub struct SendEvent {
493 pub node_id: PublicKey,
494 pub msgs: Vec<msgs::UpdateAddHTLC>,
495 pub commitment_msg: msgs::CommitmentSigned,
498 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
499 assert!(updates.update_fulfill_htlcs.is_empty());
500 assert!(updates.update_fail_htlcs.is_empty());
501 assert!(updates.update_fail_malformed_htlcs.is_empty());
502 assert!(updates.update_fee.is_none());
503 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
506 pub fn from_event(event: MessageSendEvent) -> SendEvent {
508 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
509 _ => panic!("Unexpected event type!"),
513 pub fn from_node<'a, 'b>(node: &Node<'a, 'b>) -> SendEvent {
514 let mut events = node.node.get_and_clear_pending_msg_events();
515 assert_eq!(events.len(), 1);
516 SendEvent::from_event(events.pop().unwrap())
520 macro_rules! check_added_monitors {
521 ($node: expr, $count: expr) => {
523 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
524 assert_eq!(added_monitors.len(), $count);
525 added_monitors.clear();
530 macro_rules! commitment_signed_dance {
531 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
533 check_added_monitors!($node_a, 0);
534 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
535 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed);
536 check_added_monitors!($node_a, 1);
537 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
540 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
542 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
543 check_added_monitors!($node_b, 0);
544 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
545 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack);
546 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
547 check_added_monitors!($node_b, 1);
548 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed);
549 let (bs_revoke_and_ack, extra_msg_option) = {
550 let events = $node_b.node.get_and_clear_pending_msg_events();
551 assert!(events.len() <= 2);
553 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
554 assert_eq!(*node_id, $node_a.node.get_our_node_id());
557 _ => panic!("Unexpected event"),
558 }, events.get(1).map(|e| e.clone()))
560 check_added_monitors!($node_b, 1);
562 assert!($node_a.node.get_and_clear_pending_events().is_empty());
563 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
565 (extra_msg_option, bs_revoke_and_ack)
568 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
570 check_added_monitors!($node_a, 0);
571 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
572 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed);
573 check_added_monitors!($node_a, 1);
574 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
575 assert!(extra_msg_option.is_none());
579 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
581 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
582 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack);
583 check_added_monitors!($node_a, 1);
587 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
589 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
592 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
594 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
596 expect_pending_htlcs_forwardable!($node_a);
597 check_added_monitors!($node_a, 1);
599 let channel_state = $node_a.node.channel_state.lock().unwrap();
600 assert_eq!(channel_state.pending_msg_events.len(), 1);
601 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
602 assert_ne!(*node_id, $node_b.node.get_our_node_id());
603 } else { panic!("Unexpected event"); }
605 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
611 macro_rules! get_payment_preimage_hash {
614 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
615 *$node.network_payment_count.borrow_mut() += 1;
616 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
617 (payment_preimage, payment_hash)
622 macro_rules! expect_pending_htlcs_forwardable {
624 let events = $node.node.get_and_clear_pending_events();
625 assert_eq!(events.len(), 1);
627 Event::PendingHTLCsForwardable { .. } => { },
628 _ => panic!("Unexpected event"),
630 $node.node.process_pending_htlc_forwards();
634 macro_rules! expect_payment_received {
635 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
636 let events = $node.node.get_and_clear_pending_events();
637 assert_eq!(events.len(), 1);
639 Event::PaymentReceived { ref payment_hash, ref payment_secret, amt } => {
640 assert_eq!($expected_payment_hash, *payment_hash);
641 assert_eq!(*payment_secret, None);
642 assert_eq!($expected_recv_value, amt);
644 _ => panic!("Unexpected event"),
649 macro_rules! expect_payment_sent {
650 ($node: expr, $expected_payment_preimage: expr) => {
651 let events = $node.node.get_and_clear_pending_events();
652 assert_eq!(events.len(), 1);
654 Event::PaymentSent { ref payment_preimage } => {
655 assert_eq!($expected_payment_preimage, *payment_preimage);
657 _ => panic!("Unexpected event"),
662 pub fn send_along_route_with_secret<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64, our_payment_hash: PaymentHash, our_payment_secret: Option<[u8; 32]>) {
663 let mut payment_event = {
664 origin_node.node.send_payment(route, our_payment_hash, our_payment_secret.as_ref()).unwrap();
665 check_added_monitors!(origin_node, 1);
667 let mut events = origin_node.node.get_and_clear_pending_msg_events();
668 assert_eq!(events.len(), 1);
669 SendEvent::from_event(events.remove(0))
671 let mut prev_node = origin_node;
673 for (idx, &node) in expected_route.iter().enumerate() {
674 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
676 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]);
677 check_added_monitors!(node, 0);
678 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
680 expect_pending_htlcs_forwardable!(node);
682 if idx == expected_route.len() - 1 {
683 let events_2 = node.node.get_and_clear_pending_events();
684 assert_eq!(events_2.len(), 1);
686 Event::PaymentReceived { ref payment_hash, ref payment_secret, amt } => {
687 assert_eq!(our_payment_hash, *payment_hash);
688 assert_eq!(our_payment_secret, *payment_secret);
689 assert_eq!(amt, recv_value);
691 _ => panic!("Unexpected event"),
694 let mut events_2 = node.node.get_and_clear_pending_msg_events();
695 assert_eq!(events_2.len(), 1);
696 check_added_monitors!(node, 1);
697 payment_event = SendEvent::from_event(events_2.remove(0));
698 assert_eq!(payment_event.msgs.len(), 1);
705 pub fn send_along_route_with_hash<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64, our_payment_hash: PaymentHash) {
706 send_along_route_with_secret(origin_node, route, expected_route, recv_value, our_payment_hash, None);
709 pub fn send_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
710 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
711 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
712 (our_payment_preimage, our_payment_hash)
715 pub fn claim_payment_along_route_with_secret<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_preimage: PaymentPreimage, our_payment_secret: Option<[u8; 32]>, expected_amount: u64) {
716 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage, &our_payment_secret, expected_amount));
717 check_added_monitors!(expected_route.last().unwrap(), 1);
719 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
720 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
721 macro_rules! get_next_msgs {
724 let events = $node.node.get_and_clear_pending_msg_events();
725 assert_eq!(events.len(), 1);
727 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 } } => {
728 assert!(update_add_htlcs.is_empty());
729 assert_eq!(update_fulfill_htlcs.len(), 1);
730 assert!(update_fail_htlcs.is_empty());
731 assert!(update_fail_malformed_htlcs.is_empty());
732 assert!(update_fee.is_none());
733 expected_next_node = node_id.clone();
734 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
736 _ => panic!("Unexpected event"),
742 macro_rules! last_update_fulfill_dance {
743 ($node: expr, $prev_node: expr) => {
745 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
746 check_added_monitors!($node, 0);
747 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
748 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
752 macro_rules! mid_update_fulfill_dance {
753 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
755 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
756 check_added_monitors!($node, 1);
757 let new_next_msgs = if $new_msgs {
758 get_next_msgs!($node)
760 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
763 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
764 next_msgs = new_next_msgs;
769 let mut prev_node = expected_route.last().unwrap();
770 for (idx, node) in expected_route.iter().rev().enumerate() {
771 assert_eq!(expected_next_node, node.node.get_our_node_id());
772 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
773 if next_msgs.is_some() {
774 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
775 } else if update_next_msgs {
776 next_msgs = get_next_msgs!(node);
778 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
780 if !skip_last && idx == expected_route.len() - 1 {
781 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
788 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
789 expect_payment_sent!(origin_node, our_payment_preimage);
793 pub fn claim_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_preimage: PaymentPreimage, expected_amount: u64) {
794 claim_payment_along_route_with_secret(origin_node, expected_route, skip_last, our_payment_preimage, None, expected_amount);
797 pub fn claim_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], our_payment_preimage: PaymentPreimage, expected_amount: u64) {
798 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
801 pub const TEST_FINAL_CLTV: u32 = 32;
803 pub fn route_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
804 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();
805 assert_eq!(route.paths.len(), 1);
806 assert_eq!(route.paths[0].len(), expected_route.len());
807 for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
808 assert_eq!(hop.pubkey, node.node.get_our_node_id());
811 send_along_route(origin_node, route, expected_route, recv_value)
814 pub fn route_over_limit<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64) {
815 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();
816 assert_eq!(route.paths.len(), 1);
817 assert_eq!(route.paths[0].len(), expected_route.len());
818 for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
819 assert_eq!(hop.pubkey, node.node.get_our_node_id());
822 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
823 unwrap_send_err!(origin_node.node.send_payment(route, our_payment_hash, None), true, APIError::ChannelUnavailable { err },
824 assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"));
827 pub fn send_payment<'a, 'b>(origin: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64, expected_value: u64) {
828 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
829 claim_payment(&origin, expected_route, our_payment_preimage, expected_value);
832 pub fn fail_payment_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], skip_last: bool, our_payment_hash: PaymentHash) {
833 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash, &None));
834 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
835 check_added_monitors!(expected_route.last().unwrap(), 1);
837 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
838 macro_rules! update_fail_dance {
839 ($node: expr, $prev_node: expr, $last_node: expr) => {
841 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
842 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
843 if skip_last && $last_node {
844 expect_pending_htlcs_forwardable!($node);
850 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
851 let mut prev_node = expected_route.last().unwrap();
852 for (idx, node) in expected_route.iter().rev().enumerate() {
853 assert_eq!(expected_next_node, node.node.get_our_node_id());
854 if next_msgs.is_some() {
855 // We may be the "last node" for the purpose of the commitment dance if we're
856 // skipping the last node (implying it is disconnected) and we're the
857 // second-to-last node!
858 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
861 let events = node.node.get_and_clear_pending_msg_events();
862 if !skip_last || idx != expected_route.len() - 1 {
863 assert_eq!(events.len(), 1);
865 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 } } => {
866 assert!(update_add_htlcs.is_empty());
867 assert!(update_fulfill_htlcs.is_empty());
868 assert_eq!(update_fail_htlcs.len(), 1);
869 assert!(update_fail_malformed_htlcs.is_empty());
870 assert!(update_fee.is_none());
871 expected_next_node = node_id.clone();
872 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
874 _ => panic!("Unexpected event"),
877 assert!(events.is_empty());
879 if !skip_last && idx == expected_route.len() - 1 {
880 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
887 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
889 let events = origin_node.node.get_and_clear_pending_events();
890 assert_eq!(events.len(), 1);
892 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
893 assert_eq!(payment_hash, our_payment_hash);
894 assert!(rejected_by_dest);
896 _ => panic!("Unexpected event"),
901 pub fn fail_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], our_payment_hash: PaymentHash) {
902 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
905 pub fn create_node_cfgs(node_count: usize) -> Vec<NodeCfg> {
906 let mut nodes = Vec::new();
907 let mut rng = thread_rng();
909 for i in 0..node_count {
910 let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
911 let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
912 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, logger.clone() as Arc<Logger>));
913 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
914 let mut seed = [0; 32];
915 rng.fill_bytes(&mut seed);
916 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, logger.clone() as Arc<Logger>));
917 let chan_monitor = test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone());
918 nodes.push(NodeCfg { chain_monitor, logger, tx_broadcaster, fee_estimator, chan_monitor, keys_manager, node_seed: seed });
924 pub fn create_node_chanmgrs<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg>, node_config: &[Option<UserConfig>]) -> Vec<ChannelManager<EnforcingChannelKeys, &'a TestChannelMonitor>> {
925 let mut chanmgrs = Vec::new();
926 for i in 0..node_count {
927 let mut default_config = UserConfig::default();
928 default_config.channel_options.announced_channel = true;
929 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
930 let node = ChannelManager::new(Network::Testnet, cfgs[i].fee_estimator.clone(), &cfgs[i].chan_monitor, cfgs[i].tx_broadcaster.clone(), cfgs[i].logger.clone(), cfgs[i].keys_manager.clone(), if node_config[i].is_some() { node_config[i].clone().unwrap() } else { default_config }, 0).unwrap();
937 pub fn create_network<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg>, chan_mgrs: &'b Vec<ChannelManager<EnforcingChannelKeys, &'a TestChannelMonitor>>) -> Vec<Node<'a, 'b>> {
938 let secp_ctx = Secp256k1::new();
939 let mut nodes = Vec::new();
940 let chan_count = Rc::new(RefCell::new(0));
941 let payment_count = Rc::new(RefCell::new(0));
943 for i in 0..node_count {
944 let block_notifier = chaininterface::BlockNotifier::new(cfgs[i].chain_monitor.clone());
945 block_notifier.register_listener(&cfgs[i].chan_monitor.simple_monitor as &chaininterface::ChainListener);
946 block_notifier.register_listener(&chan_mgrs[i] as &chaininterface::ChainListener);
947 let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &cfgs[i].keys_manager.get_node_secret()), cfgs[i].chain_monitor.clone(), cfgs[i].logger.clone() as Arc<Logger>);
948 nodes.push(Node{ chain_monitor: cfgs[i].chain_monitor.clone(), block_notifier,
949 tx_broadcaster: cfgs[i].tx_broadcaster.clone(), chan_monitor: &cfgs[i].chan_monitor,
950 keys_manager: cfgs[i].keys_manager.clone(), node: &chan_mgrs[i], router,
951 node_seed: cfgs[i].node_seed, network_chan_count: chan_count.clone(),
952 network_payment_count: payment_count.clone(), logger: cfgs[i].logger.clone(),
959 pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
960 pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
963 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
964 /// Tests that the given node has broadcast transactions for the given Channel
966 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
967 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
968 /// broadcast and the revoked outputs were claimed.
970 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
971 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
973 /// All broadcast transactions must be accounted for in one of the above three types of we'll
975 pub fn test_txn_broadcast<'a, 'b>(node: &Node<'a, 'b>, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
976 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
977 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
979 let mut res = Vec::with_capacity(2);
980 node_txn.retain(|tx| {
981 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
982 check_spends!(tx, chan.3.clone());
983 if commitment_tx.is_none() {
984 res.push(tx.clone());
989 if let Some(explicit_tx) = commitment_tx {
990 res.push(explicit_tx.clone());
993 assert_eq!(res.len(), 1);
995 if has_htlc_tx != HTLCType::NONE {
996 node_txn.retain(|tx| {
997 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
998 check_spends!(tx, res[0].clone());
999 if has_htlc_tx == HTLCType::TIMEOUT {
1000 assert!(tx.lock_time != 0);
1002 assert!(tx.lock_time == 0);
1004 res.push(tx.clone());
1008 assert!(res.len() == 2 || res.len() == 3);
1010 assert_eq!(res[1], res[2]);
1014 assert!(node_txn.is_empty());
1018 /// Tests that the given node has broadcast a claim transaction against the provided revoked
1019 /// HTLC transaction.
1020 pub fn test_revoked_htlc_claim_txn_broadcast<'a, 'b>(node: &Node<'a, 'b>, revoked_tx: Transaction, commitment_revoked_tx: Transaction) {
1021 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
1022 // We should issue a 2nd transaction if one htlc is dropped from initial claiming tx
1023 // but sometimes not as feerate is too-low
1024 if node_txn.len() != 1 && node_txn.len() != 2 { assert!(false); }
1025 node_txn.retain(|tx| {
1026 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
1027 check_spends!(tx, revoked_tx);
1031 node_txn.retain(|tx| {
1032 check_spends!(tx, commitment_revoked_tx);
1035 assert!(node_txn.is_empty());
1038 pub fn check_preimage_claim<'a, 'b>(node: &Node<'a, 'b>, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
1039 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
1041 assert!(node_txn.len() >= 1);
1042 assert_eq!(node_txn[0].input.len(), 1);
1043 let mut found_prev = false;
1045 for tx in prev_txn {
1046 if node_txn[0].input[0].previous_output.txid == tx.txid() {
1047 check_spends!(node_txn[0], tx.clone());
1048 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
1049 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
1055 assert!(found_prev);
1057 let mut res = Vec::new();
1058 mem::swap(&mut *node_txn, &mut res);
1062 pub fn get_announce_close_broadcast_events<'a, 'b>(nodes: &Vec<Node<'a, 'b>>, a: usize, b: usize) {
1063 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
1064 assert_eq!(events_1.len(), 1);
1065 let as_update = match events_1[0] {
1066 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1069 _ => panic!("Unexpected event"),
1072 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
1073 assert_eq!(events_2.len(), 1);
1074 let bs_update = match events_2[0] {
1075 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1078 _ => panic!("Unexpected event"),
1082 node.router.handle_channel_update(&as_update).unwrap();
1083 node.router.handle_channel_update(&bs_update).unwrap();
1087 macro_rules! get_channel_value_stat {
1088 ($node: expr, $channel_id: expr) => {{
1089 let chan_lock = $node.node.channel_state.lock().unwrap();
1090 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
1091 chan.get_value_stat()
1095 macro_rules! get_chan_reestablish_msgs {
1096 ($src_node: expr, $dst_node: expr) => {
1098 let mut res = Vec::with_capacity(1);
1099 for msg in $src_node.node.get_and_clear_pending_msg_events() {
1100 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
1101 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1102 res.push(msg.clone());
1104 panic!("Unexpected event")
1112 macro_rules! handle_chan_reestablish_msgs {
1113 ($src_node: expr, $dst_node: expr) => {
1115 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1117 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1119 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1125 let mut revoke_and_ack = None;
1126 let mut commitment_update = None;
1127 let order = if let Some(ev) = msg_events.get(idx) {
1130 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1131 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1132 revoke_and_ack = Some(msg.clone());
1133 RAACommitmentOrder::RevokeAndACKFirst
1135 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1136 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1137 commitment_update = Some(updates.clone());
1138 RAACommitmentOrder::CommitmentFirst
1140 _ => panic!("Unexpected event"),
1143 RAACommitmentOrder::CommitmentFirst
1146 if let Some(ev) = msg_events.get(idx) {
1148 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1149 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1150 assert!(revoke_and_ack.is_none());
1151 revoke_and_ack = Some(msg.clone());
1153 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1154 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1155 assert!(commitment_update.is_none());
1156 commitment_update = Some(updates.clone());
1158 _ => panic!("Unexpected event"),
1162 (funding_locked, revoke_and_ack, commitment_update, order)
1167 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1168 /// for claims/fails they are separated out.
1169 pub fn reconnect_nodes<'a, 'b>(node_a: &Node<'a, 'b>, node_b: &Node<'a, 'b>, 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)) {
1170 node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1171 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1172 node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1173 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1175 if send_funding_locked.0 {
1176 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1178 for reestablish in reestablish_1.iter() {
1179 assert_eq!(reestablish.next_remote_commitment_number, 0);
1182 if send_funding_locked.1 {
1183 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1185 for reestablish in reestablish_2.iter() {
1186 assert_eq!(reestablish.next_remote_commitment_number, 0);
1189 if send_funding_locked.0 || send_funding_locked.1 {
1190 // If we expect any funding_locked's, both sides better have set
1191 // next_local_commitment_number to 1
1192 for reestablish in reestablish_1.iter() {
1193 assert_eq!(reestablish.next_local_commitment_number, 1);
1195 for reestablish in reestablish_2.iter() {
1196 assert_eq!(reestablish.next_local_commitment_number, 1);
1200 let mut resp_1 = Vec::new();
1201 for msg in reestablish_1 {
1202 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg);
1203 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1205 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1206 check_added_monitors!(node_b, 1);
1208 check_added_monitors!(node_b, 0);
1211 let mut resp_2 = Vec::new();
1212 for msg in reestablish_2 {
1213 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg);
1214 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1216 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1217 check_added_monitors!(node_a, 1);
1219 check_added_monitors!(node_a, 0);
1222 // We don't yet support both needing updates, as that would require a different commitment dance:
1223 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1224 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1226 for chan_msgs in resp_1.drain(..) {
1227 if send_funding_locked.0 {
1228 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap());
1229 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1230 if !announcement_event.is_empty() {
1231 assert_eq!(announcement_event.len(), 1);
1232 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1233 //TODO: Test announcement_sigs re-sending
1234 } else { panic!("Unexpected event!"); }
1237 assert!(chan_msgs.0.is_none());
1240 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1241 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap());
1242 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1243 check_added_monitors!(node_a, 1);
1245 assert!(chan_msgs.1.is_none());
1247 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1248 let commitment_update = chan_msgs.2.unwrap();
1249 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1250 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1252 assert!(commitment_update.update_add_htlcs.is_empty());
1254 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1255 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1256 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1257 for update_add in commitment_update.update_add_htlcs {
1258 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add);
1260 for update_fulfill in commitment_update.update_fulfill_htlcs {
1261 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill);
1263 for update_fail in commitment_update.update_fail_htlcs {
1264 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail);
1267 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1268 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1270 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed);
1271 check_added_monitors!(node_a, 1);
1272 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1273 // No commitment_signed so get_event_msg's assert(len == 1) passes
1274 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack);
1275 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1276 check_added_monitors!(node_b, 1);
1279 assert!(chan_msgs.2.is_none());
1283 for chan_msgs in resp_2.drain(..) {
1284 if send_funding_locked.1 {
1285 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap());
1286 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1287 if !announcement_event.is_empty() {
1288 assert_eq!(announcement_event.len(), 1);
1289 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1290 //TODO: Test announcement_sigs re-sending
1291 } else { panic!("Unexpected event!"); }
1294 assert!(chan_msgs.0.is_none());
1297 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1298 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap());
1299 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1300 check_added_monitors!(node_b, 1);
1302 assert!(chan_msgs.1.is_none());
1304 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1305 let commitment_update = chan_msgs.2.unwrap();
1306 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1307 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1309 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1310 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1311 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1312 for update_add in commitment_update.update_add_htlcs {
1313 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add);
1315 for update_fulfill in commitment_update.update_fulfill_htlcs {
1316 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill);
1318 for update_fail in commitment_update.update_fail_htlcs {
1319 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail);
1322 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1323 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1325 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed);
1326 check_added_monitors!(node_b, 1);
1327 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1328 // No commitment_signed so get_event_msg's assert(len == 1) passes
1329 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack);
1330 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1331 check_added_monitors!(node_a, 1);
1334 assert!(chan_msgs.2.is_none());