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};
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 pub fn create_funding_transaction<'a, 'b>(node: &Node<'a, 'b>, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
175 let chan_id = *node.network_chan_count.borrow();
177 let events = node.node.get_and_clear_pending_events();
178 assert_eq!(events.len(), 1);
180 Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, ref output_script, user_channel_id } => {
181 assert_eq!(*channel_value_satoshis, expected_chan_value);
182 assert_eq!(user_channel_id, expected_user_chan_id);
184 let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
185 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
187 let funding_outpoint = OutPoint::new(tx.txid(), 0);
188 (*temporary_channel_id, tx, funding_outpoint)
190 _ => panic!("Unexpected event"),
194 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 {
195 node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42).unwrap();
196 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()));
197 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()));
199 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
202 node_a.node.funding_transaction_generated(&temporary_channel_id, funding_output);
203 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
204 assert_eq!(added_monitors.len(), 1);
205 assert_eq!(added_monitors[0].0, funding_output);
206 added_monitors.clear();
209 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()));
211 let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
212 assert_eq!(added_monitors.len(), 1);
213 assert_eq!(added_monitors[0].0, funding_output);
214 added_monitors.clear();
217 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()));
219 let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
220 assert_eq!(added_monitors.len(), 1);
221 assert_eq!(added_monitors[0].0, funding_output);
222 added_monitors.clear();
225 let events_4 = node_a.node.get_and_clear_pending_events();
226 assert_eq!(events_4.len(), 1);
228 Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
229 assert_eq!(user_channel_id, 42);
230 assert_eq!(*funding_txo, funding_output);
232 _ => panic!("Unexpected event"),
238 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) {
239 confirm_transaction(&node_conf.block_notifier, &node_conf.chain_monitor, &tx, tx.version);
240 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()));
243 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]) {
245 let events_6 = node_conf.node.get_and_clear_pending_msg_events();
246 assert_eq!(events_6.len(), 2);
247 ((match events_6[0] {
248 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
249 channel_id = msg.channel_id.clone();
250 assert_eq!(*node_id, node_recv.node.get_our_node_id());
253 _ => panic!("Unexpected event"),
254 }, match events_6[1] {
255 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
256 assert_eq!(*node_id, node_recv.node.get_our_node_id());
259 _ => panic!("Unexpected event"),
263 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]) {
264 create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
265 confirm_transaction(&node_a.block_notifier, &node_a.chain_monitor, &tx, tx.version);
266 create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
269 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) {
270 let tx = create_chan_between_nodes_with_value_init(node_a, node_b, channel_value, push_msat, a_flags, b_flags);
271 let (msgs, chan_id) = create_chan_between_nodes_with_value_confirm(node_a, node_b, &tx);
275 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) {
276 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &as_funding_msgs.0);
277 let bs_announcement_sigs = get_event_msg!(node_b, MessageSendEvent::SendAnnouncementSignatures, node_a.node.get_our_node_id());
278 node_b.node.handle_announcement_signatures(&node_a.node.get_our_node_id(), &as_funding_msgs.1);
280 let events_7 = node_b.node.get_and_clear_pending_msg_events();
281 assert_eq!(events_7.len(), 1);
282 let (announcement, bs_update) = match events_7[0] {
283 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
286 _ => panic!("Unexpected event"),
289 node_a.node.handle_announcement_signatures(&node_b.node.get_our_node_id(), &bs_announcement_sigs);
290 let events_8 = node_a.node.get_and_clear_pending_msg_events();
291 assert_eq!(events_8.len(), 1);
292 let as_update = match events_8[0] {
293 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
294 assert!(*announcement == *msg);
295 assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id);
296 assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id);
299 _ => panic!("Unexpected event"),
302 *node_a.network_chan_count.borrow_mut() += 1;
304 ((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
307 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) {
308 create_announced_chan_between_nodes_with_value(nodes, a, b, 100000, 10001, a_flags, b_flags)
311 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) {
312 let chan_announcement = create_chan_between_nodes_with_value(&nodes[a], &nodes[b], channel_value, push_msat, a_flags, b_flags);
314 assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
315 node.router.handle_channel_update(&chan_announcement.1).unwrap();
316 node.router.handle_channel_update(&chan_announcement.2).unwrap();
318 (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
321 macro_rules! check_spends {
322 ($tx: expr, $spends_tx: expr) => {
324 $tx.verify(|out_point| {
325 if out_point.txid == $spends_tx.txid() {
326 $spends_tx.output.get(out_point.vout as usize).cloned()
335 macro_rules! get_closing_signed_broadcast {
336 ($node: expr, $dest_pubkey: expr) => {
338 let events = $node.get_and_clear_pending_msg_events();
339 assert!(events.len() == 1 || events.len() == 2);
340 (match events[events.len() - 1] {
341 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
342 assert_eq!(msg.contents.flags & 2, 2);
345 _ => panic!("Unexpected event"),
346 }, if events.len() == 2 {
348 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
349 assert_eq!(*node_id, $dest_pubkey);
352 _ => panic!("Unexpected event"),
359 macro_rules! check_closed_broadcast {
360 ($node: expr, $with_error_msg: expr) => {{
361 let events = $node.node.get_and_clear_pending_msg_events();
362 assert_eq!(events.len(), if $with_error_msg { 2 } else { 1 });
364 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
365 assert_eq!(msg.contents.flags & 2, 2);
367 _ => panic!("Unexpected event"),
371 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
372 // TODO: Check node_id
375 _ => panic!("Unexpected event"),
381 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) {
382 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) };
383 let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
386 node_a.close_channel(channel_id).unwrap();
387 node_b.handle_shutdown(&node_a.get_our_node_id(), &get_event_msg!(struct_a, MessageSendEvent::SendShutdown, node_b.get_our_node_id()));
389 let events_1 = node_b.get_and_clear_pending_msg_events();
390 assert!(events_1.len() >= 1);
391 let shutdown_b = match events_1[0] {
392 MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
393 assert_eq!(node_id, &node_a.get_our_node_id());
396 _ => panic!("Unexpected event"),
399 let closing_signed_b = if !close_inbound_first {
400 assert_eq!(events_1.len(), 1);
403 Some(match events_1[1] {
404 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
405 assert_eq!(node_id, &node_a.get_our_node_id());
408 _ => panic!("Unexpected event"),
412 node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b);
413 let (as_update, bs_update) = if close_inbound_first {
414 assert!(node_a.get_and_clear_pending_msg_events().is_empty());
415 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap());
416 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
417 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
418 let (as_update, closing_signed_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
420 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap());
421 let (bs_update, none_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
422 assert!(none_b.is_none());
423 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
424 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
425 (as_update, bs_update)
427 let closing_signed_a = get_event_msg!(struct_a, MessageSendEvent::SendClosingSigned, node_b.get_our_node_id());
429 node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a);
430 assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
431 tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
432 let (bs_update, closing_signed_b) = get_closing_signed_broadcast!(node_b, node_a.get_our_node_id());
434 node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap());
435 let (as_update, none_a) = get_closing_signed_broadcast!(node_a, node_b.get_our_node_id());
436 assert!(none_a.is_none());
437 assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
438 tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
439 (as_update, bs_update)
441 assert_eq!(tx_a, tx_b);
442 check_spends!(tx_a, funding_tx);
444 (as_update, bs_update, tx_a)
447 pub struct SendEvent {
448 pub node_id: PublicKey,
449 pub msgs: Vec<msgs::UpdateAddHTLC>,
450 pub commitment_msg: msgs::CommitmentSigned,
453 pub fn from_commitment_update(node_id: PublicKey, updates: msgs::CommitmentUpdate) -> SendEvent {
454 assert!(updates.update_fulfill_htlcs.is_empty());
455 assert!(updates.update_fail_htlcs.is_empty());
456 assert!(updates.update_fail_malformed_htlcs.is_empty());
457 assert!(updates.update_fee.is_none());
458 SendEvent { node_id: node_id, msgs: updates.update_add_htlcs, commitment_msg: updates.commitment_signed }
461 pub fn from_event(event: MessageSendEvent) -> SendEvent {
463 MessageSendEvent::UpdateHTLCs { node_id, updates } => SendEvent::from_commitment_update(node_id, updates),
464 _ => panic!("Unexpected event type!"),
468 pub fn from_node<'a, 'b>(node: &Node<'a, 'b>) -> SendEvent {
469 let mut events = node.node.get_and_clear_pending_msg_events();
470 assert_eq!(events.len(), 1);
471 SendEvent::from_event(events.pop().unwrap())
475 macro_rules! check_added_monitors {
476 ($node: expr, $count: expr) => {
478 let mut added_monitors = $node.chan_monitor.added_monitors.lock().unwrap();
479 assert_eq!(added_monitors.len(), $count);
480 added_monitors.clear();
485 macro_rules! commitment_signed_dance {
486 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */) => {
488 check_added_monitors!($node_a, 0);
489 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
490 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed);
491 check_added_monitors!($node_a, 1);
492 commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, false);
495 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */, true /* return last RAA */) => {
497 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!($node_a, $node_b.node.get_our_node_id());
498 check_added_monitors!($node_b, 0);
499 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
500 $node_b.node.handle_revoke_and_ack(&$node_a.node.get_our_node_id(), &as_revoke_and_ack);
501 assert!($node_b.node.get_and_clear_pending_msg_events().is_empty());
502 check_added_monitors!($node_b, 1);
503 $node_b.node.handle_commitment_signed(&$node_a.node.get_our_node_id(), &as_commitment_signed);
504 let (bs_revoke_and_ack, extra_msg_option) = {
505 let events = $node_b.node.get_and_clear_pending_msg_events();
506 assert!(events.len() <= 2);
508 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
509 assert_eq!(*node_id, $node_a.node.get_our_node_id());
512 _ => panic!("Unexpected event"),
513 }, events.get(1).map(|e| e.clone()))
515 check_added_monitors!($node_b, 1);
517 assert!($node_a.node.get_and_clear_pending_events().is_empty());
518 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
520 (extra_msg_option, bs_revoke_and_ack)
523 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr, true /* skip last step */, false /* return extra message */, true /* return last RAA */) => {
525 check_added_monitors!($node_a, 0);
526 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
527 $node_a.node.handle_commitment_signed(&$node_b.node.get_our_node_id(), &$commitment_signed);
528 check_added_monitors!($node_a, 1);
529 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
530 assert!(extra_msg_option.is_none());
534 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, true /* return extra message */) => {
536 let (extra_msg_option, bs_revoke_and_ack) = commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true, true);
537 $node_a.node.handle_revoke_and_ack(&$node_b.node.get_our_node_id(), &bs_revoke_and_ack);
538 check_added_monitors!($node_a, 1);
542 ($node_a: expr, $node_b: expr, (), $fail_backwards: expr, true /* skip last step */, false /* no extra message */) => {
544 assert!(commitment_signed_dance!($node_a, $node_b, (), $fail_backwards, true, true).is_none());
547 ($node_a: expr, $node_b: expr, $commitment_signed: expr, $fail_backwards: expr) => {
549 commitment_signed_dance!($node_a, $node_b, $commitment_signed, $fail_backwards, true);
551 expect_pending_htlcs_forwardable!($node_a);
552 check_added_monitors!($node_a, 1);
554 let channel_state = $node_a.node.channel_state.lock().unwrap();
555 assert_eq!(channel_state.pending_msg_events.len(), 1);
556 if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = channel_state.pending_msg_events[0] {
557 assert_ne!(*node_id, $node_b.node.get_our_node_id());
558 } else { panic!("Unexpected event"); }
560 assert!($node_a.node.get_and_clear_pending_msg_events().is_empty());
566 macro_rules! get_payment_preimage_hash {
569 let payment_preimage = PaymentPreimage([*$node.network_payment_count.borrow(); 32]);
570 *$node.network_payment_count.borrow_mut() += 1;
571 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner());
572 (payment_preimage, payment_hash)
577 macro_rules! expect_pending_htlcs_forwardable {
579 let events = $node.node.get_and_clear_pending_events();
580 assert_eq!(events.len(), 1);
582 Event::PendingHTLCsForwardable { .. } => { },
583 _ => panic!("Unexpected event"),
585 $node.node.process_pending_htlc_forwards();
589 macro_rules! expect_payment_received {
590 ($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
591 let events = $node.node.get_and_clear_pending_events();
592 assert_eq!(events.len(), 1);
594 Event::PaymentReceived { ref payment_hash, amt } => {
595 assert_eq!($expected_payment_hash, *payment_hash);
596 assert_eq!($expected_recv_value, amt);
598 _ => panic!("Unexpected event"),
603 macro_rules! expect_payment_sent {
604 ($node: expr, $expected_payment_preimage: expr) => {
605 let events = $node.node.get_and_clear_pending_events();
606 assert_eq!(events.len(), 1);
608 Event::PaymentSent { ref payment_preimage } => {
609 assert_eq!($expected_payment_preimage, *payment_preimage);
611 _ => panic!("Unexpected event"),
616 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) {
617 let mut payment_event = {
618 origin_node.node.send_payment(route, our_payment_hash).unwrap();
619 check_added_monitors!(origin_node, 1);
621 let mut events = origin_node.node.get_and_clear_pending_msg_events();
622 assert_eq!(events.len(), 1);
623 SendEvent::from_event(events.remove(0))
625 let mut prev_node = origin_node;
627 for (idx, &node) in expected_route.iter().enumerate() {
628 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
630 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]);
631 check_added_monitors!(node, 0);
632 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
634 expect_pending_htlcs_forwardable!(node);
636 if idx == expected_route.len() - 1 {
637 let events_2 = node.node.get_and_clear_pending_events();
638 assert_eq!(events_2.len(), 1);
640 Event::PaymentReceived { ref payment_hash, amt } => {
641 assert_eq!(our_payment_hash, *payment_hash);
642 assert_eq!(amt, recv_value);
644 _ => panic!("Unexpected event"),
647 let mut events_2 = node.node.get_and_clear_pending_msg_events();
648 assert_eq!(events_2.len(), 1);
649 check_added_monitors!(node, 1);
650 payment_event = SendEvent::from_event(events_2.remove(0));
651 assert_eq!(payment_event.msgs.len(), 1);
658 pub fn send_along_route<'a, 'b>(origin_node: &Node<'a, 'b>, route: Route, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
659 let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(origin_node);
660 send_along_route_with_hash(origin_node, route, expected_route, recv_value, our_payment_hash);
661 (our_payment_preimage, our_payment_hash)
664 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) {
665 assert!(expected_route.last().unwrap().node.claim_funds(our_payment_preimage, expected_amount));
666 check_added_monitors!(expected_route.last().unwrap(), 1);
668 let mut next_msgs: Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)> = None;
669 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
670 macro_rules! get_next_msgs {
673 let events = $node.node.get_and_clear_pending_msg_events();
674 assert_eq!(events.len(), 1);
676 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 } } => {
677 assert!(update_add_htlcs.is_empty());
678 assert_eq!(update_fulfill_htlcs.len(), 1);
679 assert!(update_fail_htlcs.is_empty());
680 assert!(update_fail_malformed_htlcs.is_empty());
681 assert!(update_fee.is_none());
682 expected_next_node = node_id.clone();
683 Some((update_fulfill_htlcs[0].clone(), commitment_signed.clone()))
685 _ => panic!("Unexpected event"),
691 macro_rules! last_update_fulfill_dance {
692 ($node: expr, $prev_node: expr) => {
694 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
695 check_added_monitors!($node, 0);
696 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
697 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
701 macro_rules! mid_update_fulfill_dance {
702 ($node: expr, $prev_node: expr, $new_msgs: expr) => {
704 $node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
705 check_added_monitors!($node, 1);
706 let new_next_msgs = if $new_msgs {
707 get_next_msgs!($node)
709 assert!($node.node.get_and_clear_pending_msg_events().is_empty());
712 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, false);
713 next_msgs = new_next_msgs;
718 let mut prev_node = expected_route.last().unwrap();
719 for (idx, node) in expected_route.iter().rev().enumerate() {
720 assert_eq!(expected_next_node, node.node.get_our_node_id());
721 let update_next_msgs = !skip_last || idx != expected_route.len() - 1;
722 if next_msgs.is_some() {
723 mid_update_fulfill_dance!(node, prev_node, update_next_msgs);
724 } else if update_next_msgs {
725 next_msgs = get_next_msgs!(node);
727 assert!(node.node.get_and_clear_pending_msg_events().is_empty());
729 if !skip_last && idx == expected_route.len() - 1 {
730 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
737 last_update_fulfill_dance!(origin_node, expected_route.first().unwrap());
738 expect_payment_sent!(origin_node, our_payment_preimage);
742 pub fn claim_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], our_payment_preimage: PaymentPreimage, expected_amount: u64) {
743 claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
746 pub const TEST_FINAL_CLTV: u32 = 32;
748 pub fn route_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
749 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();
750 assert_eq!(route.hops.len(), expected_route.len());
751 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
752 assert_eq!(hop.pubkey, node.node.get_our_node_id());
755 send_along_route(origin_node, route, expected_route, recv_value)
758 pub fn route_over_limit<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64) {
759 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();
760 assert_eq!(route.hops.len(), expected_route.len());
761 for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
762 assert_eq!(hop.pubkey, node.node.get_our_node_id());
765 let (_, our_payment_hash) = get_payment_preimage_hash!(origin_node);
767 let err = origin_node.node.send_payment(route, our_payment_hash).err().unwrap();
769 APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept"),
770 _ => panic!("Unknown error variants"),
774 pub fn send_payment<'a, 'b>(origin: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], recv_value: u64, expected_value: u64) {
775 let our_payment_preimage = route_payment(&origin, expected_route, recv_value).0;
776 claim_payment(&origin, expected_route, our_payment_preimage, expected_value);
779 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) {
780 assert!(expected_route.last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
781 expect_pending_htlcs_forwardable!(expected_route.last().unwrap());
782 check_added_monitors!(expected_route.last().unwrap(), 1);
784 let mut next_msgs: Option<(msgs::UpdateFailHTLC, msgs::CommitmentSigned)> = None;
785 macro_rules! update_fail_dance {
786 ($node: expr, $prev_node: expr, $last_node: expr) => {
788 $node.node.handle_update_fail_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
789 commitment_signed_dance!($node, $prev_node, next_msgs.as_ref().unwrap().1, !$last_node);
790 if skip_last && $last_node {
791 expect_pending_htlcs_forwardable!($node);
797 let mut expected_next_node = expected_route.last().unwrap().node.get_our_node_id();
798 let mut prev_node = expected_route.last().unwrap();
799 for (idx, node) in expected_route.iter().rev().enumerate() {
800 assert_eq!(expected_next_node, node.node.get_our_node_id());
801 if next_msgs.is_some() {
802 // We may be the "last node" for the purpose of the commitment dance if we're
803 // skipping the last node (implying it is disconnected) and we're the
804 // second-to-last node!
805 update_fail_dance!(node, prev_node, skip_last && idx == expected_route.len() - 1);
808 let events = node.node.get_and_clear_pending_msg_events();
809 if !skip_last || idx != expected_route.len() - 1 {
810 assert_eq!(events.len(), 1);
812 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 } } => {
813 assert!(update_add_htlcs.is_empty());
814 assert!(update_fulfill_htlcs.is_empty());
815 assert_eq!(update_fail_htlcs.len(), 1);
816 assert!(update_fail_malformed_htlcs.is_empty());
817 assert!(update_fee.is_none());
818 expected_next_node = node_id.clone();
819 next_msgs = Some((update_fail_htlcs[0].clone(), commitment_signed.clone()));
821 _ => panic!("Unexpected event"),
824 assert!(events.is_empty());
826 if !skip_last && idx == expected_route.len() - 1 {
827 assert_eq!(expected_next_node, origin_node.node.get_our_node_id());
834 update_fail_dance!(origin_node, expected_route.first().unwrap(), true);
836 let events = origin_node.node.get_and_clear_pending_events();
837 assert_eq!(events.len(), 1);
839 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
840 assert_eq!(payment_hash, our_payment_hash);
841 assert!(rejected_by_dest);
843 _ => panic!("Unexpected event"),
848 pub fn fail_payment<'a, 'b>(origin_node: &Node<'a, 'b>, expected_route: &[&Node<'a, 'b>], our_payment_hash: PaymentHash) {
849 fail_payment_along_route(origin_node, expected_route, false, our_payment_hash);
852 pub fn create_node_cfgs(node_count: usize) -> Vec<NodeCfg> {
853 let mut nodes = Vec::new();
854 let mut rng = thread_rng();
856 for i in 0..node_count {
857 let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
858 let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
859 let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, logger.clone() as Arc<Logger>));
860 let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
861 let mut seed = [0; 32];
862 rng.fill_bytes(&mut seed);
863 let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, logger.clone() as Arc<Logger>));
864 let chan_monitor = test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone());
865 nodes.push(NodeCfg { chain_monitor, logger, tx_broadcaster, fee_estimator, chan_monitor, keys_manager, node_seed: seed });
871 pub fn create_node_chanmgrs<'a, 'b>(node_count: usize, cfgs: &'a Vec<NodeCfg>, node_config: &[Option<UserConfig>]) -> Vec<ChannelManager<EnforcingChannelKeys, &'a TestChannelMonitor>> {
872 let mut chanmgrs = Vec::new();
873 for i in 0..node_count {
874 let mut default_config = UserConfig::default();
875 default_config.channel_options.announced_channel = true;
876 default_config.peer_channel_config_limits.force_announced_channel_preference = false;
877 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();
884 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>> {
885 let secp_ctx = Secp256k1::new();
886 let mut nodes = Vec::new();
887 let chan_count = Rc::new(RefCell::new(0));
888 let payment_count = Rc::new(RefCell::new(0));
890 for i in 0..node_count {
891 let block_notifier = chaininterface::BlockNotifier::new(cfgs[i].chain_monitor.clone());
892 block_notifier.register_listener(&cfgs[i].chan_monitor.simple_monitor as &chaininterface::ChainListener);
893 block_notifier.register_listener(&chan_mgrs[i] as &chaininterface::ChainListener);
894 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>);
895 nodes.push(Node{ chain_monitor: cfgs[i].chain_monitor.clone(), block_notifier,
896 tx_broadcaster: cfgs[i].tx_broadcaster.clone(), chan_monitor: &cfgs[i].chan_monitor,
897 keys_manager: cfgs[i].keys_manager.clone(), node: &chan_mgrs[i], router,
898 node_seed: cfgs[i].node_seed, network_chan_count: chan_count.clone(),
899 network_payment_count: payment_count.clone(), logger: cfgs[i].logger.clone(),
906 pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
907 pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
910 pub enum HTLCType { NONE, TIMEOUT, SUCCESS }
911 /// Tests that the given node has broadcast transactions for the given Channel
913 /// First checks that the latest local commitment tx has been broadcast, unless an explicit
914 /// commitment_tx is provided, which may be used to test that a remote commitment tx was
915 /// broadcast and the revoked outputs were claimed.
917 /// Next tests that there is (or is not) a transaction that spends the commitment transaction
918 /// that appears to be the type of HTLC transaction specified in has_htlc_tx.
920 /// All broadcast transactions must be accounted for in one of the above three types of we'll
922 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> {
923 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
924 assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
926 let mut res = Vec::with_capacity(2);
927 node_txn.retain(|tx| {
928 if tx.input.len() == 1 && tx.input[0].previous_output.txid == chan.3.txid() {
929 check_spends!(tx, chan.3.clone());
930 if commitment_tx.is_none() {
931 res.push(tx.clone());
936 if let Some(explicit_tx) = commitment_tx {
937 res.push(explicit_tx.clone());
940 assert_eq!(res.len(), 1);
942 if has_htlc_tx != HTLCType::NONE {
943 node_txn.retain(|tx| {
944 if tx.input.len() == 1 && tx.input[0].previous_output.txid == res[0].txid() {
945 check_spends!(tx, res[0].clone());
946 if has_htlc_tx == HTLCType::TIMEOUT {
947 assert!(tx.lock_time != 0);
949 assert!(tx.lock_time == 0);
951 res.push(tx.clone());
955 assert!(res.len() == 2 || res.len() == 3);
957 assert_eq!(res[1], res[2]);
961 assert!(node_txn.is_empty());
965 /// Tests that the given node has broadcast a claim transaction against the provided revoked
966 /// HTLC transaction.
967 pub fn test_revoked_htlc_claim_txn_broadcast<'a, 'b>(node: &Node<'a, 'b>, revoked_tx: Transaction, commitment_revoked_tx: Transaction) {
968 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
969 // We should issue a 2nd transaction if one htlc is dropped from initial claiming tx
970 // but sometimes not as feerate is too-low
971 if node_txn.len() != 1 && node_txn.len() != 2 { assert!(false); }
972 node_txn.retain(|tx| {
973 if tx.input.len() == 1 && tx.input[0].previous_output.txid == revoked_tx.txid() {
974 check_spends!(tx, revoked_tx);
978 node_txn.retain(|tx| {
979 check_spends!(tx, commitment_revoked_tx);
982 assert!(node_txn.is_empty());
985 pub fn check_preimage_claim<'a, 'b>(node: &Node<'a, 'b>, prev_txn: &Vec<Transaction>) -> Vec<Transaction> {
986 let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
988 assert!(node_txn.len() >= 1);
989 assert_eq!(node_txn[0].input.len(), 1);
990 let mut found_prev = false;
993 if node_txn[0].input[0].previous_output.txid == tx.txid() {
994 check_spends!(node_txn[0], tx.clone());
995 assert!(node_txn[0].input[0].witness[2].len() > 106); // must spend an htlc output
996 assert_eq!(tx.input.len(), 1); // must spend a commitment tx
1002 assert!(found_prev);
1004 let mut res = Vec::new();
1005 mem::swap(&mut *node_txn, &mut res);
1009 pub fn get_announce_close_broadcast_events<'a, 'b>(nodes: &Vec<Node<'a, 'b>>, a: usize, b: usize) {
1010 let events_1 = nodes[a].node.get_and_clear_pending_msg_events();
1011 assert_eq!(events_1.len(), 1);
1012 let as_update = match events_1[0] {
1013 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1016 _ => panic!("Unexpected event"),
1019 let events_2 = nodes[b].node.get_and_clear_pending_msg_events();
1020 assert_eq!(events_2.len(), 1);
1021 let bs_update = match events_2[0] {
1022 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
1025 _ => panic!("Unexpected event"),
1029 node.router.handle_channel_update(&as_update).unwrap();
1030 node.router.handle_channel_update(&bs_update).unwrap();
1034 macro_rules! get_channel_value_stat {
1035 ($node: expr, $channel_id: expr) => {{
1036 let chan_lock = $node.node.channel_state.lock().unwrap();
1037 let chan = chan_lock.by_id.get(&$channel_id).unwrap();
1038 chan.get_value_stat()
1042 macro_rules! get_chan_reestablish_msgs {
1043 ($src_node: expr, $dst_node: expr) => {
1045 let mut res = Vec::with_capacity(1);
1046 for msg in $src_node.node.get_and_clear_pending_msg_events() {
1047 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
1048 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1049 res.push(msg.clone());
1051 panic!("Unexpected event")
1059 macro_rules! handle_chan_reestablish_msgs {
1060 ($src_node: expr, $dst_node: expr) => {
1062 let msg_events = $src_node.node.get_and_clear_pending_msg_events();
1064 let funding_locked = if let Some(&MessageSendEvent::SendFundingLocked { ref node_id, ref msg }) = msg_events.get(0) {
1066 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1072 let mut revoke_and_ack = None;
1073 let mut commitment_update = None;
1074 let order = if let Some(ev) = msg_events.get(idx) {
1077 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1078 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1079 revoke_and_ack = Some(msg.clone());
1080 RAACommitmentOrder::RevokeAndACKFirst
1082 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1083 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1084 commitment_update = Some(updates.clone());
1085 RAACommitmentOrder::CommitmentFirst
1087 _ => panic!("Unexpected event"),
1090 RAACommitmentOrder::CommitmentFirst
1093 if let Some(ev) = msg_events.get(idx) {
1095 &MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
1096 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1097 assert!(revoke_and_ack.is_none());
1098 revoke_and_ack = Some(msg.clone());
1100 &MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
1101 assert_eq!(*node_id, $dst_node.node.get_our_node_id());
1102 assert!(commitment_update.is_none());
1103 commitment_update = Some(updates.clone());
1105 _ => panic!("Unexpected event"),
1109 (funding_locked, revoke_and_ack, commitment_update, order)
1114 /// pending_htlc_adds includes both the holding cell and in-flight update_add_htlcs, whereas
1115 /// for claims/fails they are separated out.
1116 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)) {
1117 node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1118 let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b);
1119 node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1120 let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a);
1122 if send_funding_locked.0 {
1123 // If a expects a funding_locked, it better not think it has received a revoke_and_ack
1125 for reestablish in reestablish_1.iter() {
1126 assert_eq!(reestablish.next_remote_commitment_number, 0);
1129 if send_funding_locked.1 {
1130 // If b expects a funding_locked, it better not think it has received a revoke_and_ack
1132 for reestablish in reestablish_2.iter() {
1133 assert_eq!(reestablish.next_remote_commitment_number, 0);
1136 if send_funding_locked.0 || send_funding_locked.1 {
1137 // If we expect any funding_locked's, both sides better have set
1138 // next_local_commitment_number to 1
1139 for reestablish in reestablish_1.iter() {
1140 assert_eq!(reestablish.next_local_commitment_number, 1);
1142 for reestablish in reestablish_2.iter() {
1143 assert_eq!(reestablish.next_local_commitment_number, 1);
1147 let mut resp_1 = Vec::new();
1148 for msg in reestablish_1 {
1149 node_b.node.handle_channel_reestablish(&node_a.node.get_our_node_id(), &msg);
1150 resp_1.push(handle_chan_reestablish_msgs!(node_b, node_a));
1152 if pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1153 check_added_monitors!(node_b, 1);
1155 check_added_monitors!(node_b, 0);
1158 let mut resp_2 = Vec::new();
1159 for msg in reestablish_2 {
1160 node_a.node.handle_channel_reestablish(&node_b.node.get_our_node_id(), &msg);
1161 resp_2.push(handle_chan_reestablish_msgs!(node_a, node_b));
1163 if pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1164 check_added_monitors!(node_a, 1);
1166 check_added_monitors!(node_a, 0);
1169 // We don't yet support both needing updates, as that would require a different commitment dance:
1170 assert!((pending_htlc_adds.0 == 0 && pending_htlc_claims.0 == 0 && pending_cell_htlc_claims.0 == 0 && pending_cell_htlc_fails.0 == 0) ||
1171 (pending_htlc_adds.1 == 0 && pending_htlc_claims.1 == 0 && pending_cell_htlc_claims.1 == 0 && pending_cell_htlc_fails.1 == 0));
1173 for chan_msgs in resp_1.drain(..) {
1174 if send_funding_locked.0 {
1175 node_a.node.handle_funding_locked(&node_b.node.get_our_node_id(), &chan_msgs.0.unwrap());
1176 let announcement_event = node_a.node.get_and_clear_pending_msg_events();
1177 if !announcement_event.is_empty() {
1178 assert_eq!(announcement_event.len(), 1);
1179 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1180 //TODO: Test announcement_sigs re-sending
1181 } else { panic!("Unexpected event!"); }
1184 assert!(chan_msgs.0.is_none());
1187 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1188 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &chan_msgs.1.unwrap());
1189 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1190 check_added_monitors!(node_a, 1);
1192 assert!(chan_msgs.1.is_none());
1194 if pending_htlc_adds.0 != 0 || pending_htlc_claims.0 != 0 || pending_cell_htlc_claims.0 != 0 || pending_cell_htlc_fails.0 != 0 {
1195 let commitment_update = chan_msgs.2.unwrap();
1196 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1197 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.0 as usize);
1199 assert!(commitment_update.update_add_htlcs.is_empty());
1201 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1202 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1203 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1204 for update_add in commitment_update.update_add_htlcs {
1205 node_a.node.handle_update_add_htlc(&node_b.node.get_our_node_id(), &update_add);
1207 for update_fulfill in commitment_update.update_fulfill_htlcs {
1208 node_a.node.handle_update_fulfill_htlc(&node_b.node.get_our_node_id(), &update_fulfill);
1210 for update_fail in commitment_update.update_fail_htlcs {
1211 node_a.node.handle_update_fail_htlc(&node_b.node.get_our_node_id(), &update_fail);
1214 if pending_htlc_adds.0 != -1 { // We use -1 to denote a response commitment_signed
1215 commitment_signed_dance!(node_a, node_b, commitment_update.commitment_signed, false);
1217 node_a.node.handle_commitment_signed(&node_b.node.get_our_node_id(), &commitment_update.commitment_signed);
1218 check_added_monitors!(node_a, 1);
1219 let as_revoke_and_ack = get_event_msg!(node_a, MessageSendEvent::SendRevokeAndACK, node_b.node.get_our_node_id());
1220 // No commitment_signed so get_event_msg's assert(len == 1) passes
1221 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &as_revoke_and_ack);
1222 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1223 check_added_monitors!(node_b, 1);
1226 assert!(chan_msgs.2.is_none());
1230 for chan_msgs in resp_2.drain(..) {
1231 if send_funding_locked.1 {
1232 node_b.node.handle_funding_locked(&node_a.node.get_our_node_id(), &chan_msgs.0.unwrap());
1233 let announcement_event = node_b.node.get_and_clear_pending_msg_events();
1234 if !announcement_event.is_empty() {
1235 assert_eq!(announcement_event.len(), 1);
1236 if let MessageSendEvent::SendAnnouncementSignatures { .. } = announcement_event[0] {
1237 //TODO: Test announcement_sigs re-sending
1238 } else { panic!("Unexpected event!"); }
1241 assert!(chan_msgs.0.is_none());
1244 assert!(chan_msgs.3 == RAACommitmentOrder::RevokeAndACKFirst);
1245 node_b.node.handle_revoke_and_ack(&node_a.node.get_our_node_id(), &chan_msgs.1.unwrap());
1246 assert!(node_b.node.get_and_clear_pending_msg_events().is_empty());
1247 check_added_monitors!(node_b, 1);
1249 assert!(chan_msgs.1.is_none());
1251 if pending_htlc_adds.1 != 0 || pending_htlc_claims.1 != 0 || pending_cell_htlc_claims.1 != 0 || pending_cell_htlc_fails.1 != 0 {
1252 let commitment_update = chan_msgs.2.unwrap();
1253 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1254 assert_eq!(commitment_update.update_add_htlcs.len(), pending_htlc_adds.1 as usize);
1256 assert_eq!(commitment_update.update_fulfill_htlcs.len(), pending_htlc_claims.0 + pending_cell_htlc_claims.0);
1257 assert_eq!(commitment_update.update_fail_htlcs.len(), pending_cell_htlc_fails.0);
1258 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
1259 for update_add in commitment_update.update_add_htlcs {
1260 node_b.node.handle_update_add_htlc(&node_a.node.get_our_node_id(), &update_add);
1262 for update_fulfill in commitment_update.update_fulfill_htlcs {
1263 node_b.node.handle_update_fulfill_htlc(&node_a.node.get_our_node_id(), &update_fulfill);
1265 for update_fail in commitment_update.update_fail_htlcs {
1266 node_b.node.handle_update_fail_htlc(&node_a.node.get_our_node_id(), &update_fail);
1269 if pending_htlc_adds.1 != -1 { // We use -1 to denote a response commitment_signed
1270 commitment_signed_dance!(node_b, node_a, commitment_update.commitment_signed, false);
1272 node_b.node.handle_commitment_signed(&node_a.node.get_our_node_id(), &commitment_update.commitment_signed);
1273 check_added_monitors!(node_b, 1);
1274 let bs_revoke_and_ack = get_event_msg!(node_b, MessageSendEvent::SendRevokeAndACK, node_a.node.get_our_node_id());
1275 // No commitment_signed so get_event_msg's assert(len == 1) passes
1276 node_a.node.handle_revoke_and_ack(&node_b.node.get_our_node_id(), &bs_revoke_and_ack);
1277 assert!(node_a.node.get_and_clear_pending_msg_events().is_empty());
1278 check_added_monitors!(node_a, 1);
1281 assert!(chan_msgs.2.is_none());