c0e33432611bd4aacc29f15b3affdea8fb1c7e98
[rust-lightning] / lightning / src / ln / functional_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Tests that test standing up a network of ChannelManagers, creating channels, sending
11 //! payments/messages between them, and often checking the resulting ChannelMonitors are able to
12 //! claim outputs on-chain.
13
14 use crate::chain;
15 use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
16 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
17 use crate::chain::channelmonitor;
18 use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
19 use crate::chain::transaction::OutPoint;
20 use crate::sign::{EcdsaChannelSigner, EntropySource, SignerProvider};
21 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
22 use crate::ln::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash};
23 use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY, ChannelPhase};
24 use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
25 use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError};
26 use crate::ln::{chan_utils, onion_utils};
27 use crate::ln::chan_utils::{OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
28 use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
29 use crate::routing::router::{Path, PaymentParameters, Route, RouteHop, get_route, RouteParameters};
30 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
31 use crate::ln::msgs;
32 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
33 use crate::util::test_channel_signer::TestChannelSigner;
34 use crate::util::test_utils::{self, WatchtowerPersister};
35 use crate::util::errors::APIError;
36 use crate::util::ser::{Writeable, ReadableArgs};
37 use crate::util::string::UntrustedString;
38 use crate::util::config::{UserConfig, MaxDustHTLCExposure};
39
40 use bitcoin::hash_types::BlockHash;
41 use bitcoin::blockdata::script::{Builder, Script};
42 use bitcoin::blockdata::opcodes;
43 use bitcoin::blockdata::constants::genesis_block;
44 use bitcoin::network::constants::Network;
45 use bitcoin::{PackedLockTime, Sequence, Transaction, TxIn, TxOut, Witness};
46 use bitcoin::OutPoint as BitcoinOutPoint;
47
48 use bitcoin::secp256k1::Secp256k1;
49 use bitcoin::secp256k1::{PublicKey,SecretKey};
50
51 use regex;
52
53 use crate::io;
54 use crate::prelude::*;
55 use alloc::collections::BTreeSet;
56 use core::default::Default;
57 use core::iter::repeat;
58 use bitcoin::hashes::Hash;
59 use crate::sync::{Arc, Mutex, RwLock};
60
61 use crate::ln::functional_test_utils::*;
62 use crate::ln::chan_utils::CommitmentTransaction;
63
64 use super::channel::UNFUNDED_CHANNEL_AGE_LIMIT_TICKS;
65
66 #[test]
67 fn test_insane_channel_opens() {
68         // Stand up a network of 2 nodes
69         use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
70         let mut cfg = UserConfig::default();
71         cfg.channel_handshake_limits.max_funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1;
72         let chanmon_cfgs = create_chanmon_cfgs(2);
73         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
74         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(cfg)]);
75         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
76
77         // Instantiate channel parameters where we push the maximum msats given our
78         // funding satoshis
79         let channel_value_sat = 31337; // same as funding satoshis
80         let channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
81         let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
82
83         // Have node0 initiate a channel to node1 with aforementioned parameters
84         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42, None).unwrap();
85
86         // Extract the channel open message from node0 to node1
87         let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
88
89         // Test helper that asserts we get the correct error string given a mutator
90         // that supposedly makes the channel open message insane
91         let insane_open_helper = |expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
92                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &message_mutator(open_channel_message.clone()));
93                 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
94                 assert_eq!(msg_events.len(), 1);
95                 let expected_regex = regex::Regex::new(expected_error_str).unwrap();
96                 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
97                         match action {
98                                 &ErrorAction::SendErrorMessage { .. } => {
99                                         nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", expected_regex, 1);
100                                 },
101                                 _ => panic!("unexpected event!"),
102                         }
103                 } else { assert!(false); }
104         };
105
106         use crate::ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
107
108         // Test all mutations that would make the channel open message insane
109         insane_open_helper(format!("Per our config, funding must be at most {}. It was {}", TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1, TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2).as_str(), |mut msg| { msg.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2; msg });
110         insane_open_helper(format!("Funding must be smaller than the total bitcoin supply. It was {}", TOTAL_BITCOIN_SUPPLY_SATOSHIS).as_str(), |mut msg| { msg.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS; msg });
111
112         insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
113
114         insane_open_helper(r"push_msat \d+ was larger than channel amount minus reserve \(\d+\)", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
115
116         insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
117
118         insane_open_helper(r"Minimum htlc value \(\d+\) was larger than full channel value \(\d+\)", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg });
119
120         insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg });
121
122         insane_open_helper("0 max_accepted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
123
124         insane_open_helper("max_accepted_htlcs was 484. It must not be larger than 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
125 }
126
127 #[test]
128 fn test_funding_exceeds_no_wumbo_limit() {
129         // Test that if a peer does not support wumbo channels, we'll refuse to open a wumbo channel to
130         // them.
131         use crate::ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
132         let chanmon_cfgs = create_chanmon_cfgs(2);
133         let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
134         *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_wumbo());
135         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
136         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
137
138         match nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), MAX_FUNDING_SATOSHIS_NO_WUMBO + 1, 0, 42, None) {
139                 Err(APIError::APIMisuseError { err }) => {
140                         assert_eq!(format!("funding_value must not exceed {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, MAX_FUNDING_SATOSHIS_NO_WUMBO + 1), err);
141                 },
142                 _ => panic!()
143         }
144 }
145
146 fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
147         // A peer providing a channel_reserve_satoshis of 0 (or less than our dust limit) is insecure,
148         // but only for them. Because some LSPs do it with some level of trust of the clients (for a
149         // substantial UX improvement), we explicitly allow it. Because it's unlikely to happen often
150         // in normal testing, we test it explicitly here.
151         let chanmon_cfgs = create_chanmon_cfgs(2);
152         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
153         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
154         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
155         let default_config = UserConfig::default();
156
157         // Have node0 initiate a channel to node1 with aforementioned parameters
158         let mut push_amt = 100_000_000;
159         let feerate_per_kw = 253;
160         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
161         push_amt -= feerate_per_kw as u64 * (commitment_tx_base_weight(&channel_type_features) + 4 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
162         push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
163
164         let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, if send_from_initiator { 0 } else { push_amt }, 42, None).unwrap();
165         let mut open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
166         if !send_from_initiator {
167                 open_channel_message.channel_reserve_satoshis = 0;
168                 open_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
169         }
170         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
171
172         // Extract the channel accept message from node1 to node0
173         let mut accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
174         if send_from_initiator {
175                 accept_channel_message.channel_reserve_satoshis = 0;
176                 accept_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
177         }
178         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
179         {
180                 let sender_node = if send_from_initiator { &nodes[1] } else { &nodes[0] };
181                 let counterparty_node = if send_from_initiator { &nodes[0] } else { &nodes[1] };
182                 let mut sender_node_per_peer_lock;
183                 let mut sender_node_peer_state_lock;
184
185                 let channel_phase = get_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
186                 match channel_phase {
187                         ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
188                                 let chan_context = channel_phase.context_mut();
189                                 chan_context.holder_selected_channel_reserve_satoshis = 0;
190                                 chan_context.holder_max_htlc_value_in_flight_msat = 100_000_000;
191                         },
192                         ChannelPhase::Funded(_) => assert!(false),
193                 }
194         }
195
196         let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id);
197         let funding_msgs = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &funding_tx);
198         create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_msgs.0);
199
200         // nodes[0] should now be able to send the full balance to nodes[1], violating nodes[1]'s
201         // security model if it ever tries to send funds back to nodes[0] (but that's not our problem).
202         if send_from_initiator {
203                 send_payment(&nodes[0], &[&nodes[1]], 100_000_000
204                         // Note that for outbound channels we have to consider the commitment tx fee and the
205                         // "fee spike buffer", which is currently a multiple of the total commitment tx fee as
206                         // well as an additional HTLC.
207                         - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * commit_tx_fee_msat(feerate_per_kw, 2, &channel_type_features));
208         } else {
209                 send_payment(&nodes[1], &[&nodes[0]], push_amt);
210         }
211 }
212
213 #[test]
214 fn test_counterparty_no_reserve() {
215         do_test_counterparty_no_reserve(true);
216         do_test_counterparty_no_reserve(false);
217 }
218
219 #[test]
220 fn test_async_inbound_update_fee() {
221         let chanmon_cfgs = create_chanmon_cfgs(2);
222         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
223         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
224         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
225         create_announced_chan_between_nodes(&nodes, 0, 1);
226
227         // balancing
228         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
229
230         // A                                        B
231         // update_fee                            ->
232         // send (1) commitment_signed            -.
233         //                                       <- update_add_htlc/commitment_signed
234         // send (2) RAA (awaiting remote revoke) -.
235         // (1) commitment_signed is delivered    ->
236         //                                       .- send (3) RAA (awaiting remote revoke)
237         // (2) RAA is delivered                  ->
238         //                                       .- send (4) commitment_signed
239         //                                       <- (3) RAA is delivered
240         // send (5) commitment_signed            -.
241         //                                       <- (4) commitment_signed is delivered
242         // send (6) RAA                          -.
243         // (5) commitment_signed is delivered    ->
244         //                                       <- RAA
245         // (6) RAA is delivered                  ->
246
247         // First nodes[0] generates an update_fee
248         {
249                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
250                 *feerate_lock += 20;
251         }
252         nodes[0].node.timer_tick_occurred();
253         check_added_monitors!(nodes[0], 1);
254
255         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
256         assert_eq!(events_0.len(), 1);
257         let (update_msg, commitment_signed) = match events_0[0] { // (1)
258                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
259                         (update_fee.as_ref(), commitment_signed)
260                 },
261                 _ => panic!("Unexpected event"),
262         };
263
264         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
265
266         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
267         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
268         nodes[1].node.send_payment_with_route(&route, our_payment_hash,
269                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
270         check_added_monitors!(nodes[1], 1);
271
272         let payment_event = {
273                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
274                 assert_eq!(events_1.len(), 1);
275                 SendEvent::from_event(events_1.remove(0))
276         };
277         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
278         assert_eq!(payment_event.msgs.len(), 1);
279
280         // ...now when the messages get delivered everyone should be happy
281         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
282         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
283         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
284         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
285         check_added_monitors!(nodes[0], 1);
286
287         // deliver(1), generate (3):
288         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
289         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
290         // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
291         check_added_monitors!(nodes[1], 1);
292
293         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack); // deliver (2)
294         let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
295         assert!(bs_update.update_add_htlcs.is_empty()); // (4)
296         assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
297         assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
298         assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
299         assert!(bs_update.update_fee.is_none()); // (4)
300         check_added_monitors!(nodes[1], 1);
301
302         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack); // deliver (3)
303         let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
304         assert!(as_update.update_add_htlcs.is_empty()); // (5)
305         assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
306         assert!(as_update.update_fail_htlcs.is_empty()); // (5)
307         assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
308         assert!(as_update.update_fee.is_none()); // (5)
309         check_added_monitors!(nodes[0], 1);
310
311         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed); // deliver (4)
312         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
313         // only (6) so get_event_msg's assert(len == 1) passes
314         check_added_monitors!(nodes[0], 1);
315
316         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed); // deliver (5)
317         let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
318         check_added_monitors!(nodes[1], 1);
319
320         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
321         check_added_monitors!(nodes[0], 1);
322
323         let events_2 = nodes[0].node.get_and_clear_pending_events();
324         assert_eq!(events_2.len(), 1);
325         match events_2[0] {
326                 Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
327                 _ => panic!("Unexpected event"),
328         }
329
330         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke); // deliver (6)
331         check_added_monitors!(nodes[1], 1);
332 }
333
334 #[test]
335 fn test_update_fee_unordered_raa() {
336         // Just the intro to the previous test followed by an out-of-order RAA (which caused a
337         // crash in an earlier version of the update_fee patch)
338         let chanmon_cfgs = create_chanmon_cfgs(2);
339         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
340         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
341         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
342         create_announced_chan_between_nodes(&nodes, 0, 1);
343
344         // balancing
345         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
346
347         // First nodes[0] generates an update_fee
348         {
349                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
350                 *feerate_lock += 20;
351         }
352         nodes[0].node.timer_tick_occurred();
353         check_added_monitors!(nodes[0], 1);
354
355         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
356         assert_eq!(events_0.len(), 1);
357         let update_msg = match events_0[0] { // (1)
358                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
359                         update_fee.as_ref()
360                 },
361                 _ => panic!("Unexpected event"),
362         };
363
364         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
365
366         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
367         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
368         nodes[1].node.send_payment_with_route(&route, our_payment_hash,
369                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
370         check_added_monitors!(nodes[1], 1);
371
372         let payment_event = {
373                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
374                 assert_eq!(events_1.len(), 1);
375                 SendEvent::from_event(events_1.remove(0))
376         };
377         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
378         assert_eq!(payment_event.msgs.len(), 1);
379
380         // ...now when the messages get delivered everyone should be happy
381         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
382         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
383         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
384         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
385         check_added_monitors!(nodes[0], 1);
386
387         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg); // deliver (2)
388         check_added_monitors!(nodes[1], 1);
389
390         // We can't continue, sadly, because our (1) now has a bogus signature
391 }
392
393 #[test]
394 fn test_multi_flight_update_fee() {
395         let chanmon_cfgs = create_chanmon_cfgs(2);
396         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
397         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
398         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
399         create_announced_chan_between_nodes(&nodes, 0, 1);
400
401         // A                                        B
402         // update_fee/commitment_signed          ->
403         //                                       .- send (1) RAA and (2) commitment_signed
404         // update_fee (never committed)          ->
405         // (3) update_fee                        ->
406         // We have to manually generate the above update_fee, it is allowed by the protocol but we
407         // don't track which updates correspond to which revoke_and_ack responses so we're in
408         // AwaitingRAA mode and will not generate the update_fee yet.
409         //                                       <- (1) RAA delivered
410         // (3) is generated and send (4) CS      -.
411         // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
412         // know the per_commitment_point to use for it.
413         //                                       <- (2) commitment_signed delivered
414         // revoke_and_ack                        ->
415         //                                          B should send no response here
416         // (4) commitment_signed delivered       ->
417         //                                       <- RAA/commitment_signed delivered
418         // revoke_and_ack                        ->
419
420         // First nodes[0] generates an update_fee
421         let initial_feerate;
422         {
423                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
424                 initial_feerate = *feerate_lock;
425                 *feerate_lock = initial_feerate + 20;
426         }
427         nodes[0].node.timer_tick_occurred();
428         check_added_monitors!(nodes[0], 1);
429
430         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
431         assert_eq!(events_0.len(), 1);
432         let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
433                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
434                         (update_fee.as_ref().unwrap(), commitment_signed)
435                 },
436                 _ => panic!("Unexpected event"),
437         };
438
439         // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
440         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1);
441         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1);
442         let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
443         check_added_monitors!(nodes[1], 1);
444
445         // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
446         // transaction:
447         {
448                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
449                 *feerate_lock = initial_feerate + 40;
450         }
451         nodes[0].node.timer_tick_occurred();
452         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
453         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
454
455         // Create the (3) update_fee message that nodes[0] will generate before it does...
456         let mut update_msg_2 = msgs::UpdateFee {
457                 channel_id: update_msg_1.channel_id.clone(),
458                 feerate_per_kw: (initial_feerate + 30) as u32,
459         };
460
461         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
462
463         update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
464         // Deliver (3)
465         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
466
467         // Deliver (1), generating (3) and (4)
468         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg);
469         let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
470         check_added_monitors!(nodes[0], 1);
471         assert!(as_second_update.update_add_htlcs.is_empty());
472         assert!(as_second_update.update_fulfill_htlcs.is_empty());
473         assert!(as_second_update.update_fail_htlcs.is_empty());
474         assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
475         // Check that the update_fee newly generated matches what we delivered:
476         assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
477         assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
478
479         // Deliver (2) commitment_signed
480         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
481         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
482         check_added_monitors!(nodes[0], 1);
483         // No commitment_signed so get_event_msg's assert(len == 1) passes
484
485         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg);
486         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
487         check_added_monitors!(nodes[1], 1);
488
489         // Delever (4)
490         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed);
491         let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
492         check_added_monitors!(nodes[1], 1);
493
494         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
495         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
496         check_added_monitors!(nodes[0], 1);
497
498         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment);
499         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
500         // No commitment_signed so get_event_msg's assert(len == 1) passes
501         check_added_monitors!(nodes[0], 1);
502
503         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke);
504         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
505         check_added_monitors!(nodes[1], 1);
506 }
507
508 fn do_test_sanity_on_in_flight_opens(steps: u8) {
509         // Previously, we had issues deserializing channels when we hadn't connected the first block
510         // after creation. To catch that and similar issues, we lean on the Node::drop impl to test
511         // serialization round-trips and simply do steps towards opening a channel and then drop the
512         // Node objects.
513
514         let chanmon_cfgs = create_chanmon_cfgs(2);
515         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
516         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
517         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
518
519         if steps & 0b1000_0000 != 0{
520                 let block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
521                 connect_block(&nodes[0], &block);
522                 connect_block(&nodes[1], &block);
523         }
524
525         if steps & 0x0f == 0 { return; }
526         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
527         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
528
529         if steps & 0x0f == 1 { return; }
530         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
531         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
532
533         if steps & 0x0f == 2 { return; }
534         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
535
536         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
537
538         if steps & 0x0f == 3 { return; }
539         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
540         check_added_monitors!(nodes[0], 0);
541         let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
542
543         if steps & 0x0f == 4 { return; }
544         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
545         {
546                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
547                 assert_eq!(added_monitors.len(), 1);
548                 assert_eq!(added_monitors[0].0, funding_output);
549                 added_monitors.clear();
550         }
551         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
552
553         let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
554
555         if steps & 0x0f == 5 { return; }
556         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
557         {
558                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
559                 assert_eq!(added_monitors.len(), 1);
560                 assert_eq!(added_monitors[0].0, funding_output);
561                 added_monitors.clear();
562         }
563
564         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
565         let events_4 = nodes[0].node.get_and_clear_pending_events();
566         assert_eq!(events_4.len(), 0);
567
568         if steps & 0x0f == 6 { return; }
569         create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);
570
571         if steps & 0x0f == 7 { return; }
572         confirm_transaction_at(&nodes[0], &tx, 2);
573         connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
574         create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
575         expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
576 }
577
578 #[test]
579 fn test_sanity_on_in_flight_opens() {
580         do_test_sanity_on_in_flight_opens(0);
581         do_test_sanity_on_in_flight_opens(0 | 0b1000_0000);
582         do_test_sanity_on_in_flight_opens(1);
583         do_test_sanity_on_in_flight_opens(1 | 0b1000_0000);
584         do_test_sanity_on_in_flight_opens(2);
585         do_test_sanity_on_in_flight_opens(2 | 0b1000_0000);
586         do_test_sanity_on_in_flight_opens(3);
587         do_test_sanity_on_in_flight_opens(3 | 0b1000_0000);
588         do_test_sanity_on_in_flight_opens(4);
589         do_test_sanity_on_in_flight_opens(4 | 0b1000_0000);
590         do_test_sanity_on_in_flight_opens(5);
591         do_test_sanity_on_in_flight_opens(5 | 0b1000_0000);
592         do_test_sanity_on_in_flight_opens(6);
593         do_test_sanity_on_in_flight_opens(6 | 0b1000_0000);
594         do_test_sanity_on_in_flight_opens(7);
595         do_test_sanity_on_in_flight_opens(7 | 0b1000_0000);
596         do_test_sanity_on_in_flight_opens(8);
597         do_test_sanity_on_in_flight_opens(8 | 0b1000_0000);
598 }
599
600 #[test]
601 fn test_update_fee_vanilla() {
602         let chanmon_cfgs = create_chanmon_cfgs(2);
603         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
604         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
605         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
606         create_announced_chan_between_nodes(&nodes, 0, 1);
607
608         {
609                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
610                 *feerate_lock += 25;
611         }
612         nodes[0].node.timer_tick_occurred();
613         check_added_monitors!(nodes[0], 1);
614
615         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
616         assert_eq!(events_0.len(), 1);
617         let (update_msg, commitment_signed) = match events_0[0] {
618                         MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
619                         (update_fee.as_ref(), commitment_signed)
620                 },
621                 _ => panic!("Unexpected event"),
622         };
623         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
624
625         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
626         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
627         check_added_monitors!(nodes[1], 1);
628
629         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
630         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
631         check_added_monitors!(nodes[0], 1);
632
633         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
634         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
635         // No commitment_signed so get_event_msg's assert(len == 1) passes
636         check_added_monitors!(nodes[0], 1);
637
638         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
639         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
640         check_added_monitors!(nodes[1], 1);
641 }
642
643 #[test]
644 fn test_update_fee_that_funder_cannot_afford() {
645         let chanmon_cfgs = create_chanmon_cfgs(2);
646         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
647         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
648         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
649         let channel_value = 5000;
650         let push_sats = 700;
651         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, push_sats * 1000);
652         let channel_id = chan.2;
653         let secp_ctx = Secp256k1::new();
654         let default_config = UserConfig::default();
655         let bs_channel_reserve_sats = get_holder_selected_channel_reserve_satoshis(channel_value, &default_config);
656
657         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
658
659         // Calculate the maximum feerate that A can afford. Note that we don't send an update_fee
660         // CONCURRENT_INBOUND_HTLC_FEE_BUFFER HTLCs before actually running out of local balance, so we
661         // calculate two different feerates here - the expected local limit as well as the expected
662         // remote limit.
663         let feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / (commitment_tx_base_weight(&channel_type_features) + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)) as u32;
664         let non_buffer_feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / commitment_tx_base_weight(&channel_type_features)) as u32;
665         {
666                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
667                 *feerate_lock = feerate;
668         }
669         nodes[0].node.timer_tick_occurred();
670         check_added_monitors!(nodes[0], 1);
671         let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
672
673         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap());
674
675         commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
676
677         // Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate set above.
678         {
679                 let commitment_tx = get_local_commitment_txn!(nodes[1], channel_id)[0].clone();
680
681                 //We made sure neither party's funds are below the dust limit and there are no HTLCs here
682                 assert_eq!(commitment_tx.output.len(), 2);
683                 let total_fee: u64 = commit_tx_fee_msat(feerate, 0, &channel_type_features) / 1000;
684                 let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
685                 actual_fee = channel_value - actual_fee;
686                 assert_eq!(total_fee, actual_fee);
687         }
688
689         {
690                 // Increment the feerate by a small constant, accounting for rounding errors
691                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
692                 *feerate_lock += 4;
693         }
694         nodes[0].node.timer_tick_occurred();
695         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot afford to send new feerate at {}", feerate + 4), 1);
696         check_added_monitors!(nodes[0], 0);
697
698         const INITIAL_COMMITMENT_NUMBER: u64 = 281474976710654;
699
700         // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
701         // needed to sign the new commitment tx and (2) sign the new commitment tx.
702         let (local_revocation_basepoint, local_htlc_basepoint, local_funding) = {
703                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
704                 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
705                 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
706                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
707                 ).flatten().unwrap();
708                 let chan_signer = local_chan.get_signer();
709                 let pubkeys = chan_signer.as_ref().pubkeys();
710                 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
711                  pubkeys.funding_pubkey)
712         };
713         let (remote_delayed_payment_basepoint, remote_htlc_basepoint,remote_point, remote_funding) = {
714                 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
715                 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
716                 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
717                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
718                 ).flatten().unwrap();
719                 let chan_signer = remote_chan.get_signer();
720                 let pubkeys = chan_signer.as_ref().pubkeys();
721                 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
722                  chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
723                  pubkeys.funding_pubkey)
724         };
725
726         // Assemble the set of keys we can use for signatures for our commitment_signed message.
727         let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
728                 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
729
730         let res = {
731                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
732                 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
733                 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
734                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
735                 ).flatten().unwrap();
736                 let local_chan_signer = local_chan.get_signer();
737                 let mut htlcs: Vec<(HTLCOutputInCommitment, ())> = vec![];
738                 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
739                         INITIAL_COMMITMENT_NUMBER - 1,
740                         push_sats,
741                         channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
742                         local_funding, remote_funding,
743                         commit_tx_keys.clone(),
744                         non_buffer_feerate + 4,
745                         &mut htlcs,
746                         &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
747                 );
748                 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
749         };
750
751         let commit_signed_msg = msgs::CommitmentSigned {
752                 channel_id: chan.2,
753                 signature: res.0,
754                 htlc_signatures: res.1,
755                 #[cfg(taproot)]
756                 partial_signature_with_nonce: None,
757         };
758
759         let update_fee = msgs::UpdateFee {
760                 channel_id: chan.2,
761                 feerate_per_kw: non_buffer_feerate + 4,
762         };
763
764         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_fee);
765
766         //While producing the commitment_signed response after handling a received update_fee request the
767         //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
768         //Should produce and error.
769         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
770         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Funding remote cannot afford proposed new fee".to_string(), 1);
771         check_added_monitors!(nodes[1], 1);
772         check_closed_broadcast!(nodes[1], true);
773         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: String::from("Funding remote cannot afford proposed new fee") },
774                 [nodes[0].node.get_our_node_id()], channel_value);
775 }
776
777 #[test]
778 fn test_update_fee_with_fundee_update_add_htlc() {
779         let chanmon_cfgs = create_chanmon_cfgs(2);
780         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
781         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
782         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
783         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
784
785         // balancing
786         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
787
788         {
789                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
790                 *feerate_lock += 20;
791         }
792         nodes[0].node.timer_tick_occurred();
793         check_added_monitors!(nodes[0], 1);
794
795         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
796         assert_eq!(events_0.len(), 1);
797         let (update_msg, commitment_signed) = match events_0[0] {
798                         MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
799                         (update_fee.as_ref(), commitment_signed)
800                 },
801                 _ => panic!("Unexpected event"),
802         };
803         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
804         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
805         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
806         check_added_monitors!(nodes[1], 1);
807
808         let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 800000);
809
810         // nothing happens since node[1] is in AwaitingRemoteRevoke
811         nodes[1].node.send_payment_with_route(&route, our_payment_hash,
812                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
813         {
814                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
815                 assert_eq!(added_monitors.len(), 0);
816                 added_monitors.clear();
817         }
818         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
819         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
820         // node[1] has nothing to do
821
822         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
823         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
824         check_added_monitors!(nodes[0], 1);
825
826         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
827         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
828         // No commitment_signed so get_event_msg's assert(len == 1) passes
829         check_added_monitors!(nodes[0], 1);
830         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
831         check_added_monitors!(nodes[1], 1);
832         // AwaitingRemoteRevoke ends here
833
834         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
835         assert_eq!(commitment_update.update_add_htlcs.len(), 1);
836         assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
837         assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
838         assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
839         assert_eq!(commitment_update.update_fee.is_none(), true);
840
841         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]);
842         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
843         check_added_monitors!(nodes[0], 1);
844         let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
845
846         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke);
847         check_added_monitors!(nodes[1], 1);
848         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
849
850         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
851         check_added_monitors!(nodes[1], 1);
852         let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
853         // No commitment_signed so get_event_msg's assert(len == 1) passes
854
855         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke);
856         check_added_monitors!(nodes[0], 1);
857         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
858
859         expect_pending_htlcs_forwardable!(nodes[0]);
860
861         let events = nodes[0].node.get_and_clear_pending_events();
862         assert_eq!(events.len(), 1);
863         match events[0] {
864                 Event::PaymentClaimable { .. } => { },
865                 _ => panic!("Unexpected event"),
866         };
867
868         claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
869
870         send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
871         send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
872         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
873         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
874         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
875 }
876
877 #[test]
878 fn test_update_fee() {
879         let chanmon_cfgs = create_chanmon_cfgs(2);
880         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
881         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
882         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
883         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
884         let channel_id = chan.2;
885
886         // A                                        B
887         // (1) update_fee/commitment_signed      ->
888         //                                       <- (2) revoke_and_ack
889         //                                       .- send (3) commitment_signed
890         // (4) update_fee/commitment_signed      ->
891         //                                       .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
892         //                                       <- (3) commitment_signed delivered
893         // send (6) revoke_and_ack               -.
894         //                                       <- (5) deliver revoke_and_ack
895         // (6) deliver revoke_and_ack            ->
896         //                                       .- send (7) commitment_signed in response to (4)
897         //                                       <- (7) deliver commitment_signed
898         // revoke_and_ack                        ->
899
900         // Create and deliver (1)...
901         let feerate;
902         {
903                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
904                 feerate = *feerate_lock;
905                 *feerate_lock = feerate + 20;
906         }
907         nodes[0].node.timer_tick_occurred();
908         check_added_monitors!(nodes[0], 1);
909
910         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
911         assert_eq!(events_0.len(), 1);
912         let (update_msg, commitment_signed) = match events_0[0] {
913                         MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
914                         (update_fee.as_ref(), commitment_signed)
915                 },
916                 _ => panic!("Unexpected event"),
917         };
918         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
919
920         // Generate (2) and (3):
921         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
922         let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
923         check_added_monitors!(nodes[1], 1);
924
925         // Deliver (2):
926         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
927         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
928         check_added_monitors!(nodes[0], 1);
929
930         // Create and deliver (4)...
931         {
932                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
933                 *feerate_lock = feerate + 30;
934         }
935         nodes[0].node.timer_tick_occurred();
936         check_added_monitors!(nodes[0], 1);
937         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
938         assert_eq!(events_0.len(), 1);
939         let (update_msg, commitment_signed) = match events_0[0] {
940                         MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
941                         (update_fee.as_ref(), commitment_signed)
942                 },
943                 _ => panic!("Unexpected event"),
944         };
945
946         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
947         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
948         check_added_monitors!(nodes[1], 1);
949         // ... creating (5)
950         let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
951         // No commitment_signed so get_event_msg's assert(len == 1) passes
952
953         // Handle (3), creating (6):
954         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0);
955         check_added_monitors!(nodes[0], 1);
956         let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
957         // No commitment_signed so get_event_msg's assert(len == 1) passes
958
959         // Deliver (5):
960         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
961         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
962         check_added_monitors!(nodes[0], 1);
963
964         // Deliver (6), creating (7):
965         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0);
966         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
967         assert!(commitment_update.update_add_htlcs.is_empty());
968         assert!(commitment_update.update_fulfill_htlcs.is_empty());
969         assert!(commitment_update.update_fail_htlcs.is_empty());
970         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
971         assert!(commitment_update.update_fee.is_none());
972         check_added_monitors!(nodes[1], 1);
973
974         // Deliver (7)
975         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
976         check_added_monitors!(nodes[0], 1);
977         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
978         // No commitment_signed so get_event_msg's assert(len == 1) passes
979
980         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
981         check_added_monitors!(nodes[1], 1);
982         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
983
984         assert_eq!(get_feerate!(nodes[0], nodes[1], channel_id), feerate + 30);
985         assert_eq!(get_feerate!(nodes[1], nodes[0], channel_id), feerate + 30);
986         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
987         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
988         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
989 }
990
991 #[test]
992 fn fake_network_test() {
993         // Simple test which builds a network of ChannelManagers, connects them to each other, and
994         // tests that payments get routed and transactions broadcast in semi-reasonable ways.
995         let chanmon_cfgs = create_chanmon_cfgs(4);
996         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
997         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
998         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
999
1000         // Create some initial channels
1001         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1002         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1003         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
1004
1005         // Rebalance the network a bit by relaying one payment through all the channels...
1006         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1007         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1008         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1009         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1010
1011         // Send some more payments
1012         send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
1013         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
1014         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
1015
1016         // Test failure packets
1017         let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
1018         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
1019
1020         // Add a new channel that skips 3
1021         let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3);
1022
1023         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
1024         send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
1025         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1026         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1027         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1028         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1029         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1030
1031         // Do some rebalance loop payments, simultaneously
1032         let mut hops = Vec::with_capacity(3);
1033         hops.push(RouteHop {
1034                 pubkey: nodes[2].node.get_our_node_id(),
1035                 node_features: NodeFeatures::empty(),
1036                 short_channel_id: chan_2.0.contents.short_channel_id,
1037                 channel_features: ChannelFeatures::empty(),
1038                 fee_msat: 0,
1039                 cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
1040         });
1041         hops.push(RouteHop {
1042                 pubkey: nodes[3].node.get_our_node_id(),
1043                 node_features: NodeFeatures::empty(),
1044                 short_channel_id: chan_3.0.contents.short_channel_id,
1045                 channel_features: ChannelFeatures::empty(),
1046                 fee_msat: 0,
1047                 cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
1048         });
1049         hops.push(RouteHop {
1050                 pubkey: nodes[1].node.get_our_node_id(),
1051                 node_features: nodes[1].node.node_features(),
1052                 short_channel_id: chan_4.0.contents.short_channel_id,
1053                 channel_features: nodes[1].node.channel_features(),
1054                 fee_msat: 1000000,
1055                 cltv_expiry_delta: TEST_FINAL_CLTV,
1056         });
1057         hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
1058         hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
1059         let payment_preimage_1 = send_along_route(&nodes[1],
1060                 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1061                         &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
1062
1063         let mut hops = Vec::with_capacity(3);
1064         hops.push(RouteHop {
1065                 pubkey: nodes[3].node.get_our_node_id(),
1066                 node_features: NodeFeatures::empty(),
1067                 short_channel_id: chan_4.0.contents.short_channel_id,
1068                 channel_features: ChannelFeatures::empty(),
1069                 fee_msat: 0,
1070                 cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
1071         });
1072         hops.push(RouteHop {
1073                 pubkey: nodes[2].node.get_our_node_id(),
1074                 node_features: NodeFeatures::empty(),
1075                 short_channel_id: chan_3.0.contents.short_channel_id,
1076                 channel_features: ChannelFeatures::empty(),
1077                 fee_msat: 0,
1078                 cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
1079         });
1080         hops.push(RouteHop {
1081                 pubkey: nodes[1].node.get_our_node_id(),
1082                 node_features: nodes[1].node.node_features(),
1083                 short_channel_id: chan_2.0.contents.short_channel_id,
1084                 channel_features: nodes[1].node.channel_features(),
1085                 fee_msat: 1000000,
1086                 cltv_expiry_delta: TEST_FINAL_CLTV,
1087         });
1088         hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
1089         hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
1090         let payment_hash_2 = send_along_route(&nodes[1],
1091                 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1092                         &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
1093
1094         // Claim the rebalances...
1095         fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
1096         claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
1097
1098         // Close down the channels...
1099         close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1100         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1101         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1102         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1103         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1104         check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1105         close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1106         check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1107         check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1108         close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1109         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1110         check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1111 }
1112
1113 #[test]
1114 fn holding_cell_htlc_counting() {
1115         // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1116         // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1117         // commitment dance rounds.
1118         let chanmon_cfgs = create_chanmon_cfgs(3);
1119         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1120         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1121         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1122         create_announced_chan_between_nodes(&nodes, 0, 1);
1123         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1124
1125         // Fetch a route in advance as we will be unable to once we're unable to send.
1126         let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1127
1128         let mut payments = Vec::new();
1129         for _ in 0..50 {
1130                 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1131                 nodes[1].node.send_payment_with_route(&route, payment_hash,
1132                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1133                 payments.push((payment_preimage, payment_hash));
1134         }
1135         check_added_monitors!(nodes[1], 1);
1136
1137         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1138         assert_eq!(events.len(), 1);
1139         let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1140         assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1141
1142         // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1143         // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1144         // another HTLC.
1145         {
1146                 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, payment_hash_1,
1147                                 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)
1148                         ), true, APIError::ChannelUnavailable { .. }, {});
1149                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1150         }
1151
1152         // This should also be true if we try to forward a payment.
1153         let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
1154         {
1155                 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
1156                         RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1157                 check_added_monitors!(nodes[0], 1);
1158         }
1159
1160         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1161         assert_eq!(events.len(), 1);
1162         let payment_event = SendEvent::from_event(events.pop().unwrap());
1163         assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1164
1165         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1166         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1167         // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1168         // fails), the second will process the resulting failure and fail the HTLC backward.
1169         expect_pending_htlcs_forwardable!(nodes[1]);
1170         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
1171         check_added_monitors!(nodes[1], 1);
1172
1173         let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1174         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
1175         commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1176
1177         expect_payment_failed_with_update!(nodes[0], payment_hash_2, false, chan_2.0.contents.short_channel_id, false);
1178
1179         // Now forward all the pending HTLCs and claim them back
1180         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]);
1181         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg);
1182         check_added_monitors!(nodes[2], 1);
1183
1184         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1185         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1186         check_added_monitors!(nodes[1], 1);
1187         let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1188
1189         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1190         check_added_monitors!(nodes[1], 1);
1191         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1192
1193         for ref update in as_updates.update_add_htlcs.iter() {
1194                 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update);
1195         }
1196         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed);
1197         check_added_monitors!(nodes[2], 1);
1198         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
1199         check_added_monitors!(nodes[2], 1);
1200         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1201
1202         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1203         check_added_monitors!(nodes[1], 1);
1204         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1205         check_added_monitors!(nodes[1], 1);
1206         let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1207
1208         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa);
1209         check_added_monitors!(nodes[2], 1);
1210
1211         expect_pending_htlcs_forwardable!(nodes[2]);
1212
1213         let events = nodes[2].node.get_and_clear_pending_events();
1214         assert_eq!(events.len(), payments.len());
1215         for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1216                 match event {
1217                         &Event::PaymentClaimable { ref payment_hash, .. } => {
1218                                 assert_eq!(*payment_hash, *hash);
1219                         },
1220                         _ => panic!("Unexpected event"),
1221                 };
1222         }
1223
1224         for (preimage, _) in payments.drain(..) {
1225                 claim_payment(&nodes[1], &[&nodes[2]], preimage);
1226         }
1227
1228         send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1229 }
1230
1231 #[test]
1232 fn duplicate_htlc_test() {
1233         // Test that we accept duplicate payment_hash HTLCs across the network and that
1234         // claiming/failing them are all separate and don't affect each other
1235         let chanmon_cfgs = create_chanmon_cfgs(6);
1236         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1237         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1238         let mut nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1239
1240         // Create some initial channels to route via 3 to 4/5 from 0/1/2
1241         create_announced_chan_between_nodes(&nodes, 0, 3);
1242         create_announced_chan_between_nodes(&nodes, 1, 3);
1243         create_announced_chan_between_nodes(&nodes, 2, 3);
1244         create_announced_chan_between_nodes(&nodes, 3, 4);
1245         create_announced_chan_between_nodes(&nodes, 3, 5);
1246
1247         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
1248
1249         *nodes[0].network_payment_count.borrow_mut() -= 1;
1250         assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
1251
1252         *nodes[0].network_payment_count.borrow_mut() -= 1;
1253         assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
1254
1255         claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
1256         fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
1257         claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
1258 }
1259
1260 #[test]
1261 fn test_duplicate_htlc_different_direction_onchain() {
1262         // Test that ChannelMonitor doesn't generate 2 preimage txn
1263         // when we have 2 HTLCs with same preimage that go across a node
1264         // in opposite directions, even with the same payment secret.
1265         let chanmon_cfgs = create_chanmon_cfgs(2);
1266         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1267         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1268         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1269
1270         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1271
1272         // balancing
1273         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1274
1275         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
1276
1277         let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 800_000);
1278         let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
1279         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
1280
1281         // Provide preimage to node 0 by claiming payment
1282         nodes[0].node.claim_funds(payment_preimage);
1283         expect_payment_claimed!(nodes[0], payment_hash, 800_000);
1284         check_added_monitors!(nodes[0], 1);
1285
1286         // Broadcast node 1 commitment txn
1287         let remote_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
1288
1289         assert_eq!(remote_txn[0].output.len(), 4); // 1 local, 1 remote, 1 htlc inbound, 1 htlc outbound
1290         let mut has_both_htlcs = 0; // check htlcs match ones committed
1291         for outp in remote_txn[0].output.iter() {
1292                 if outp.value == 800_000 / 1000 {
1293                         has_both_htlcs += 1;
1294                 } else if outp.value == 900_000 / 1000 {
1295                         has_both_htlcs += 1;
1296                 }
1297         }
1298         assert_eq!(has_both_htlcs, 2);
1299
1300         mine_transaction(&nodes[0], &remote_txn[0]);
1301         check_added_monitors!(nodes[0], 1);
1302         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
1303         connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
1304
1305         let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1306         assert_eq!(claim_txn.len(), 3);
1307
1308         check_spends!(claim_txn[0], remote_txn[0]); // Immediate HTLC claim with preimage
1309         check_spends!(claim_txn[1], remote_txn[0]);
1310         check_spends!(claim_txn[2], remote_txn[0]);
1311         let preimage_tx = &claim_txn[0];
1312         let (preimage_bump_tx, timeout_tx) = if claim_txn[1].input[0].previous_output == preimage_tx.input[0].previous_output {
1313                 (&claim_txn[1], &claim_txn[2])
1314         } else {
1315                 (&claim_txn[2], &claim_txn[1])
1316         };
1317
1318         assert_eq!(preimage_tx.input.len(), 1);
1319         assert_eq!(preimage_bump_tx.input.len(), 1);
1320
1321         assert_eq!(preimage_tx.input.len(), 1);
1322         assert_eq!(preimage_tx.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC 1 <--> 0, preimage tx
1323         assert_eq!(remote_txn[0].output[preimage_tx.input[0].previous_output.vout as usize].value, 800);
1324
1325         assert_eq!(timeout_tx.input.len(), 1);
1326         assert_eq!(timeout_tx.input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
1327         check_spends!(timeout_tx, remote_txn[0]);
1328         assert_eq!(remote_txn[0].output[timeout_tx.input[0].previous_output.vout as usize].value, 900);
1329
1330         let events = nodes[0].node.get_and_clear_pending_msg_events();
1331         assert_eq!(events.len(), 3);
1332         for e in events {
1333                 match e {
1334                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1335                         MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
1336                                 assert_eq!(node_id, nodes[1].node.get_our_node_id());
1337                                 assert_eq!(msg.data, "Channel closed because commitment or closing transaction was confirmed on chain.");
1338                         },
1339                         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, .. } } => {
1340                                 assert!(update_add_htlcs.is_empty());
1341                                 assert!(update_fail_htlcs.is_empty());
1342                                 assert_eq!(update_fulfill_htlcs.len(), 1);
1343                                 assert!(update_fail_malformed_htlcs.is_empty());
1344                                 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
1345                         },
1346                         _ => panic!("Unexpected event"),
1347                 }
1348         }
1349 }
1350
1351 #[test]
1352 fn test_basic_channel_reserve() {
1353         let chanmon_cfgs = create_chanmon_cfgs(2);
1354         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1355         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1356         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1357         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1358
1359         let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1360         let channel_reserve = chan_stat.channel_reserve_msat;
1361
1362         // The 2* and +1 are for the fee spike reserve.
1363         let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], nodes[1], chan.2), 1 + 1, &get_channel_type_features!(nodes[0], nodes[1], chan.2));
1364         let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
1365         let (mut route, our_payment_hash, _, our_payment_secret) =
1366                 get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
1367         route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1368         let err = nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1369                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).err().unwrap();
1370         match err {
1371                 PaymentSendFailure::AllFailedResendSafe(ref fails) => {
1372                         if let &APIError::ChannelUnavailable { .. } = &fails[0] {}
1373                         else { panic!("Unexpected error variant"); }
1374                 },
1375                 _ => panic!("Unexpected error variant"),
1376         }
1377         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1378
1379         send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
1380 }
1381
1382 #[test]
1383 fn test_fee_spike_violation_fails_htlc() {
1384         let chanmon_cfgs = create_chanmon_cfgs(2);
1385         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1386         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1387         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1388         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1389
1390         let (mut route, payment_hash, _, payment_secret) =
1391                 get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
1392         route.paths[0].hops[0].fee_msat += 1;
1393         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1394         let secp_ctx = Secp256k1::new();
1395         let session_priv = SecretKey::from_slice(&[42; 32]).expect("RNG is bad!");
1396
1397         let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1398
1399         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1400         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1401                 3460001, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1402         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1403         let msg = msgs::UpdateAddHTLC {
1404                 channel_id: chan.2,
1405                 htlc_id: 0,
1406                 amount_msat: htlc_msat,
1407                 payment_hash: payment_hash,
1408                 cltv_expiry: htlc_cltv,
1409                 onion_routing_packet: onion_packet,
1410                 skimmed_fee_msat: None,
1411         };
1412
1413         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1414
1415         // Now manually create the commitment_signed message corresponding to the update_add
1416         // nodes[0] just sent. In the code for construction of this message, "local" refers
1417         // to the sender of the message, and "remote" refers to the receiver.
1418
1419         let feerate_per_kw = get_feerate!(nodes[0], nodes[1], chan.2);
1420
1421         const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1422
1423         // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
1424         // needed to sign the new commitment tx and (2) sign the new commitment tx.
1425         let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1426                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1427                 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1428                 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
1429                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1430                 ).flatten().unwrap();
1431                 let chan_signer = local_chan.get_signer();
1432                 // Make the signer believe we validated another commitment, so we can release the secret
1433                 chan_signer.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
1434
1435                 let pubkeys = chan_signer.as_ref().pubkeys();
1436                 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1437                  chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1438                  chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx),
1439                  chan_signer.as_ref().pubkeys().funding_pubkey)
1440         };
1441         let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1442                 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
1443                 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
1444                 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
1445                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1446                 ).flatten().unwrap();
1447                 let chan_signer = remote_chan.get_signer();
1448                 let pubkeys = chan_signer.as_ref().pubkeys();
1449                 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1450                  chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
1451                  chan_signer.as_ref().pubkeys().funding_pubkey)
1452         };
1453
1454         // Assemble the set of keys we can use for signatures for our commitment_signed message.
1455         let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
1456                 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
1457
1458         // Build the remote commitment transaction so we can sign it, and then later use the
1459         // signature for the commitment_signed message.
1460         let local_chan_balance = 1313;
1461
1462         let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
1463                 offered: false,
1464                 amount_msat: 3460001,
1465                 cltv_expiry: htlc_cltv,
1466                 payment_hash,
1467                 transaction_output_index: Some(1),
1468         };
1469
1470         let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
1471
1472         let res = {
1473                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1474                 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1475                 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
1476                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1477                 ).flatten().unwrap();
1478                 let local_chan_signer = local_chan.get_signer();
1479                 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
1480                         commitment_number,
1481                         95000,
1482                         local_chan_balance,
1483                         local_funding, remote_funding,
1484                         commit_tx_keys.clone(),
1485                         feerate_per_kw,
1486                         &mut vec![(accepted_htlc_info, ())],
1487                         &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
1488                 );
1489                 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
1490         };
1491
1492         let commit_signed_msg = msgs::CommitmentSigned {
1493                 channel_id: chan.2,
1494                 signature: res.0,
1495                 htlc_signatures: res.1,
1496                 #[cfg(taproot)]
1497                 partial_signature_with_nonce: None,
1498         };
1499
1500         // Send the commitment_signed message to the nodes[1].
1501         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
1502         let _ = nodes[1].node.get_and_clear_pending_msg_events();
1503
1504         // Send the RAA to nodes[1].
1505         let raa_msg = msgs::RevokeAndACK {
1506                 channel_id: chan.2,
1507                 per_commitment_secret: local_secret,
1508                 next_per_commitment_point: next_local_point,
1509                 #[cfg(taproot)]
1510                 next_local_nonce: None,
1511         };
1512         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
1513
1514         let events = nodes[1].node.get_and_clear_pending_msg_events();
1515         assert_eq!(events.len(), 1);
1516         // Make sure the HTLC failed in the way we expect.
1517         match events[0] {
1518                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. }, .. } => {
1519                         assert_eq!(update_fail_htlcs.len(), 1);
1520                         update_fail_htlcs[0].clone()
1521                 },
1522                 _ => panic!("Unexpected event"),
1523         };
1524         nodes[1].logger.assert_log("lightning::ln::channel".to_string(),
1525                 format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", raa_msg.channel_id), 1);
1526
1527         check_added_monitors!(nodes[1], 2);
1528 }
1529
1530 #[test]
1531 fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
1532         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1533         // Set the fee rate for the channel very high, to the point where the fundee
1534         // sending any above-dust amount would result in a channel reserve violation.
1535         // In this test we check that we would be prevented from sending an HTLC in
1536         // this situation.
1537         let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1538         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1539         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1540         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1541         let default_config = UserConfig::default();
1542         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1543
1544         let mut push_amt = 100_000_000;
1545         push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1546
1547         push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1548
1549         let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1550
1551         // Fetch a route in advance as we will be unable to once we're unable to send.
1552         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
1553         // Sending exactly enough to hit the reserve amount should be accepted
1554         for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1555                 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1556         }
1557
1558         // However one more HTLC should be significantly over the reserve amount and fail.
1559         unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1560                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1561                 ), true, APIError::ChannelUnavailable { .. }, {});
1562         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1563 }
1564
1565 #[test]
1566 fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
1567         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1568         let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1569         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1570         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1571         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1572         let default_config = UserConfig::default();
1573         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1574
1575         // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1576         // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1577         // transaction fee with 0 HTLCs (183 sats)).
1578         let mut push_amt = 100_000_000;
1579         push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1580         push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1581         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1582
1583         // Send four HTLCs to cover the initial push_msat buffer we're required to include
1584         for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1585                 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1586         }
1587
1588         let (mut route, payment_hash, _, payment_secret) =
1589                 get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
1590         route.paths[0].hops[0].fee_msat = 700_000;
1591         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1592         let secp_ctx = Secp256k1::new();
1593         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1594         let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1595         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1596         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1597                 700_000, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1598         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1599         let msg = msgs::UpdateAddHTLC {
1600                 channel_id: chan.2,
1601                 htlc_id: MIN_AFFORDABLE_HTLC_COUNT as u64,
1602                 amount_msat: htlc_msat,
1603                 payment_hash: payment_hash,
1604                 cltv_expiry: htlc_cltv,
1605                 onion_routing_packet: onion_packet,
1606                 skimmed_fee_msat: None,
1607         };
1608
1609         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
1610         // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1611         nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
1612         assert_eq!(nodes[0].node.list_channels().len(), 0);
1613         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
1614         assert_eq!(err_msg.data, "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value");
1615         check_added_monitors!(nodes[0], 1);
1616         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string() },
1617                 [nodes[1].node.get_our_node_id()], 100000);
1618 }
1619
1620 #[test]
1621 fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
1622         // Test that if we receive many dust HTLCs over an outbound channel, they don't count when
1623         // calculating our commitment transaction fee (this was previously broken).
1624         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1625         let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1626
1627         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1628         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1629         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1630         let default_config = UserConfig::default();
1631         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1632
1633         // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1634         // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1635         // transaction fee with 0 HTLCs (183 sats)).
1636         let mut push_amt = 100_000_000;
1637         push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1638         push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1639         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt);
1640
1641         let dust_amt = crate::ln::channel::MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000
1642                 + feerate_per_kw as u64 * htlc_success_tx_weight(&channel_type_features) / 1000 * 1000 - 1;
1643         // In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
1644         // reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
1645         // commitment transaction fee.
1646         route_payment(&nodes[1], &[&nodes[0]], dust_amt);
1647
1648         // Send four HTLCs to cover the initial push_msat buffer we're required to include
1649         for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1650                 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1651         }
1652
1653         // One more than the dust amt should fail, however.
1654         let (mut route, our_payment_hash, _, our_payment_secret) =
1655                 get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt);
1656         route.paths[0].hops[0].fee_msat += 1;
1657         unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1658                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1659                 ), true, APIError::ChannelUnavailable { .. }, {});
1660 }
1661
1662 #[test]
1663 fn test_chan_init_feerate_unaffordability() {
1664         // Test that we will reject channel opens which do not leave enough to pay for any HTLCs due to
1665         // channel reserve and feerate requirements.
1666         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1667         let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1668         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1669         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1670         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1671         let default_config = UserConfig::default();
1672         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1673
1674         // Set the push_msat amount such that nodes[0] will not be able to afford to add even a single
1675         // HTLC.
1676         let mut push_amt = 100_000_000;
1677         push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1678         assert_eq!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt + 1, 42, None).unwrap_err(),
1679                 APIError::APIMisuseError { err: "Funding amount (356) can't even pay fee for initial commitment transaction fee of 357.".to_string() });
1680
1681         // During open, we don't have a "counterparty channel reserve" to check against, so that
1682         // requirement only comes into play on the open_channel handling side.
1683         push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1684         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt, 42, None).unwrap();
1685         let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
1686         open_channel_msg.push_msat += 1;
1687         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
1688
1689         let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
1690         assert_eq!(msg_events.len(), 1);
1691         match msg_events[0] {
1692                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
1693                         assert_eq!(msg.data, "Insufficient funding amount for initial reserve");
1694                 },
1695                 _ => panic!("Unexpected event"),
1696         }
1697 }
1698
1699 #[test]
1700 fn test_chan_reserve_dust_inbound_htlcs_inbound_chan() {
1701         // Test that if we receive many dust HTLCs over an inbound channel, they don't count when
1702         // calculating our counterparty's commitment transaction fee (this was previously broken).
1703         let chanmon_cfgs = create_chanmon_cfgs(2);
1704         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1705         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1706         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1707         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 98000000);
1708
1709         let payment_amt = 46000; // Dust amount
1710         // In the previous code, these first four payments would succeed.
1711         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1712         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1713         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1714         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1715
1716         // Then these next 5 would be interpreted by nodes[1] as violating the fee spike buffer.
1717         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1718         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1719         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1720         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1721         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1722
1723         // And this last payment previously resulted in nodes[1] closing on its inbound-channel
1724         // counterparty, because it counted all the previous dust HTLCs against nodes[0]'s commitment
1725         // transaction fee and therefore perceived this next payment as a channel reserve violation.
1726         route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1727 }
1728
1729 #[test]
1730 fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1731         let chanmon_cfgs = create_chanmon_cfgs(3);
1732         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1733         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1734         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1735         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1736         let _ = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
1737
1738         let feemsat = 239;
1739         let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1740         let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1741         let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
1742         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
1743
1744         // Add a 2* and +1 for the fee spike reserve.
1745         let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1746         let recv_value_1 = (chan_stat.value_to_self_msat - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlc)/2;
1747         let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
1748
1749         // Add a pending HTLC.
1750         let (route_1, our_payment_hash_1, _, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_1);
1751         let payment_event_1 = {
1752                 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1753                         RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1754                 check_added_monitors!(nodes[0], 1);
1755
1756                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1757                 assert_eq!(events.len(), 1);
1758                 SendEvent::from_event(events.remove(0))
1759         };
1760         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1761
1762         // Attempt to trigger a channel reserve violation --> payment failure.
1763         let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2, &channel_type_features);
1764         let recv_value_2 = chan_stat.value_to_self_msat - amt_msat_1 - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlcs + 1;
1765         let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1766         let mut route_2 = route_1.clone();
1767         route_2.paths[0].hops.last_mut().unwrap().fee_msat = amt_msat_2;
1768
1769         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1770         let secp_ctx = Secp256k1::new();
1771         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1772         let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
1773         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route_2.paths[0], &session_priv).unwrap();
1774         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
1775                 &route_2.paths[0], recv_value_2, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
1776         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash_1).unwrap();
1777         let msg = msgs::UpdateAddHTLC {
1778                 channel_id: chan.2,
1779                 htlc_id: 1,
1780                 amount_msat: htlc_msat + 1,
1781                 payment_hash: our_payment_hash_1,
1782                 cltv_expiry: htlc_cltv,
1783                 onion_routing_packet: onion_packet,
1784                 skimmed_fee_msat: None,
1785         };
1786
1787         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1788         // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1789         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote HTLC add would put them under remote reserve value".to_string(), 1);
1790         assert_eq!(nodes[1].node.list_channels().len(), 1);
1791         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
1792         assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
1793         check_added_monitors!(nodes[1], 1);
1794         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote HTLC add would put them under remote reserve value".to_string() },
1795                 [nodes[0].node.get_our_node_id()], 100000);
1796 }
1797
1798 #[test]
1799 fn test_inbound_outbound_capacity_is_not_zero() {
1800         let chanmon_cfgs = create_chanmon_cfgs(2);
1801         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1802         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1803         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1804         let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1805         let channels0 = node_chanmgrs[0].list_channels();
1806         let channels1 = node_chanmgrs[1].list_channels();
1807         let default_config = UserConfig::default();
1808         assert_eq!(channels0.len(), 1);
1809         assert_eq!(channels1.len(), 1);
1810
1811         let reserve = get_holder_selected_channel_reserve_satoshis(100_000, &default_config);
1812         assert_eq!(channels0[0].inbound_capacity_msat, 95000000 - reserve*1000);
1813         assert_eq!(channels1[0].outbound_capacity_msat, 95000000 - reserve*1000);
1814
1815         assert_eq!(channels0[0].outbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1816         assert_eq!(channels1[0].inbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1817 }
1818
1819 fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64, channel_type_features: &ChannelTypeFeatures) -> u64 {
1820         (commitment_tx_base_weight(channel_type_features) + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
1821 }
1822
1823 #[test]
1824 fn test_channel_reserve_holding_cell_htlcs() {
1825         let chanmon_cfgs = create_chanmon_cfgs(3);
1826         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1827         // When this test was written, the default base fee floated based on the HTLC count.
1828         // It is now fixed, so we simply set the fee to the expected value here.
1829         let mut config = test_default_channel_config();
1830         config.channel_config.forwarding_fee_base_msat = 239;
1831         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
1832         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1833         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 190000, 1001);
1834         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 190000, 1001);
1835
1836         let mut stat01 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1837         let mut stat11 = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
1838
1839         let mut stat12 = get_channel_value_stat!(nodes[1], nodes[2], chan_2.2);
1840         let mut stat22 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
1841
1842         macro_rules! expect_forward {
1843                 ($node: expr) => {{
1844                         let mut events = $node.node.get_and_clear_pending_msg_events();
1845                         assert_eq!(events.len(), 1);
1846                         check_added_monitors!($node, 1);
1847                         let payment_event = SendEvent::from_event(events.remove(0));
1848                         payment_event
1849                 }}
1850         }
1851
1852         let feemsat = 239; // set above
1853         let total_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1854         let feerate = get_feerate!(nodes[0], nodes[1], chan_1.2);
1855         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_1.2);
1856
1857         let recv_value_0 = stat01.counterparty_max_htlc_value_in_flight_msat - total_fee_msat;
1858
1859         // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
1860         {
1861                 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1862                         .with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1863                 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0);
1864                 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1865                 assert!(route.paths[0].hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
1866
1867                 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1868                                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1869                         ), true, APIError::ChannelUnavailable { .. }, {});
1870                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1871         }
1872
1873         // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
1874         // nodes[0]'s wealth
1875         loop {
1876                 let amt_msat = recv_value_0 + total_fee_msat;
1877                 // 3 for the 3 HTLCs that will be sent, 2* and +1 for the fee spike reserve.
1878                 // Also, ensure that each payment has enough to be over the dust limit to
1879                 // ensure it'll be included in each commit tx fee calculation.
1880                 let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1881                 let ensure_htlc_amounts_above_dust_buffer = 3 * (stat01.counterparty_dust_limit_msat + 1000);
1882                 if stat01.value_to_self_msat < stat01.channel_reserve_msat + commit_tx_fee_all_htlcs + ensure_htlc_amounts_above_dust_buffer + amt_msat {
1883                         break;
1884                 }
1885
1886                 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1887                         .with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1888                 let route = get_route!(nodes[0], payment_params, recv_value_0).unwrap();
1889                 let (payment_preimage, ..) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], recv_value_0);
1890                 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
1891
1892                 let (stat01_, stat11_, stat12_, stat22_) = (
1893                         get_channel_value_stat!(nodes[0], nodes[1], chan_1.2),
1894                         get_channel_value_stat!(nodes[1], nodes[0], chan_1.2),
1895                         get_channel_value_stat!(nodes[1], nodes[2], chan_2.2),
1896                         get_channel_value_stat!(nodes[2], nodes[1], chan_2.2),
1897                 );
1898
1899                 assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
1900                 assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
1901                 assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
1902                 assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
1903                 stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
1904         }
1905
1906         // adding pending output.
1907         // 2* and +1 HTLCs on the commit tx fee for the fee spike reserve.
1908         // The reason we're dividing by two here is as follows: the dividend is the total outbound liquidity
1909         // after fees, the channel reserve, and the fee spike buffer are removed. We eventually want to
1910         // divide this quantity into 3 portions, that will each be sent in an HTLC. This allows us
1911         // to test channel channel reserve policy at the edges of what amount is sendable, i.e.
1912         // cases where 1 msat over X amount will cause a payment failure, but anything less than
1913         // that can be sent successfully. So, dividing by two is a somewhat arbitrary way of getting
1914         // the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
1915         // is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
1916         // policy.
1917         let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1918         let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
1919         let amt_msat_1 = recv_value_1 + total_fee_msat;
1920
1921         let (route_1, our_payment_hash_1, our_payment_preimage_1, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_1);
1922         let payment_event_1 = {
1923                 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1924                         RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1925                 check_added_monitors!(nodes[0], 1);
1926
1927                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1928                 assert_eq!(events.len(), 1);
1929                 SendEvent::from_event(events.remove(0))
1930         };
1931         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1932
1933         // channel reserve test with htlc pending output > 0
1934         let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs;
1935         {
1936                 let mut route = route_1.clone();
1937                 route.paths[0].hops.last_mut().unwrap().fee_msat = recv_value_2 + 1;
1938                 let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
1939                 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1940                                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1941                         ), true, APIError::ChannelUnavailable { .. }, {});
1942                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1943         }
1944
1945         // split the rest to test holding cell
1946         let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1947         let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
1948         let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2;
1949         let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
1950         {
1951                 let stat = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1952                 assert_eq!(stat.value_to_self_msat - (stat.pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat + commit_tx_fee_3_htlcs), stat.channel_reserve_msat);
1953         }
1954
1955         // now see if they go through on both sides
1956         let (route_21, our_payment_hash_21, our_payment_preimage_21, our_payment_secret_21) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_21);
1957         // but this will stuck in the holding cell
1958         nodes[0].node.send_payment_with_route(&route_21, our_payment_hash_21,
1959                 RecipientOnionFields::secret_only(our_payment_secret_21), PaymentId(our_payment_hash_21.0)).unwrap();
1960         check_added_monitors!(nodes[0], 0);
1961         let events = nodes[0].node.get_and_clear_pending_events();
1962         assert_eq!(events.len(), 0);
1963
1964         // test with outbound holding cell amount > 0
1965         {
1966                 let (mut route, our_payment_hash, _, our_payment_secret) =
1967                         get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
1968                 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1969                 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1970                                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1971                         ), true, APIError::ChannelUnavailable { .. }, {});
1972                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1973         }
1974
1975         let (route_22, our_payment_hash_22, our_payment_preimage_22, our_payment_secret_22) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
1976         // this will also stuck in the holding cell
1977         nodes[0].node.send_payment_with_route(&route_22, our_payment_hash_22,
1978                 RecipientOnionFields::secret_only(our_payment_secret_22), PaymentId(our_payment_hash_22.0)).unwrap();
1979         check_added_monitors!(nodes[0], 0);
1980         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1981         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1982
1983         // flush the pending htlc
1984         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg);
1985         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1986         check_added_monitors!(nodes[1], 1);
1987
1988         // the pending htlc should be promoted to committed
1989         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
1990         check_added_monitors!(nodes[0], 1);
1991         let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1992
1993         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed);
1994         let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1995         // No commitment_signed so get_event_msg's assert(len == 1) passes
1996         check_added_monitors!(nodes[0], 1);
1997
1998         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack);
1999         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2000         check_added_monitors!(nodes[1], 1);
2001
2002         expect_pending_htlcs_forwardable!(nodes[1]);
2003
2004         let ref payment_event_11 = expect_forward!(nodes[1]);
2005         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]);
2006         commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
2007
2008         expect_pending_htlcs_forwardable!(nodes[2]);
2009         expect_payment_claimable!(nodes[2], our_payment_hash_1, our_payment_secret_1, recv_value_1);
2010
2011         // flush the htlcs in the holding cell
2012         assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
2013         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]);
2014         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]);
2015         commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
2016         expect_pending_htlcs_forwardable!(nodes[1]);
2017
2018         let ref payment_event_3 = expect_forward!(nodes[1]);
2019         assert_eq!(payment_event_3.msgs.len(), 2);
2020         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]);
2021         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]);
2022
2023         commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
2024         expect_pending_htlcs_forwardable!(nodes[2]);
2025
2026         let events = nodes[2].node.get_and_clear_pending_events();
2027         assert_eq!(events.len(), 2);
2028         match events[0] {
2029                 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2030                         assert_eq!(our_payment_hash_21, *payment_hash);
2031                         assert_eq!(recv_value_21, amount_msat);
2032                         assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2033                         assert_eq!(via_channel_id, Some(chan_2.2));
2034                         match &purpose {
2035                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2036                                         assert!(payment_preimage.is_none());
2037                                         assert_eq!(our_payment_secret_21, *payment_secret);
2038                                 },
2039                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
2040                         }
2041                 },
2042                 _ => panic!("Unexpected event"),
2043         }
2044         match events[1] {
2045                 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2046                         assert_eq!(our_payment_hash_22, *payment_hash);
2047                         assert_eq!(recv_value_22, amount_msat);
2048                         assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2049                         assert_eq!(via_channel_id, Some(chan_2.2));
2050                         match &purpose {
2051                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2052                                         assert!(payment_preimage.is_none());
2053                                         assert_eq!(our_payment_secret_22, *payment_secret);
2054                                 },
2055                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
2056                         }
2057                 },
2058                 _ => panic!("Unexpected event"),
2059         }
2060
2061         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
2062         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
2063         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
2064
2065         let commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1, &channel_type_features);
2066         let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
2067         send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_3);
2068
2069         let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
2070         let expected_value_to_self = stat01.value_to_self_msat - (recv_value_1 + total_fee_msat) - (recv_value_21 + total_fee_msat) - (recv_value_22 + total_fee_msat) - (recv_value_3 + total_fee_msat);
2071         let stat0 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
2072         assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
2073         assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat + commit_tx_fee_1_htlc);
2074
2075         let stat2 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
2076         assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3);
2077 }
2078
2079 #[test]
2080 fn channel_reserve_in_flight_removes() {
2081         // In cases where one side claims an HTLC, it thinks it has additional available funds that it
2082         // can send to its counterparty, but due to update ordering, the other side may not yet have
2083         // considered those HTLCs fully removed.
2084         // This tests that we don't count HTLCs which will not be included in the next remote
2085         // commitment transaction towards the reserve value (as it implies no commitment transaction
2086         // will be generated which violates the remote reserve value).
2087         // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
2088         // To test this we:
2089         //  * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
2090         //    you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
2091         //    you only consider the value of the first HTLC, it may not),
2092         //  * start routing a third HTLC from A to B,
2093         //  * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
2094         //    the other claim in its holding cell, as it immediately goes into AwaitingRAA),
2095         //  * deliver the first fulfill from B
2096         //  * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
2097         //    claim,
2098         //  * deliver A's response CS and RAA.
2099         //    This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
2100         //    removed it fully. B now has the push_msat plus the first two HTLCs in value.
2101         //  * Now B happily sends another HTLC, potentially violating its reserve value from A's point
2102         //    of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
2103         let chanmon_cfgs = create_chanmon_cfgs(2);
2104         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2105         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2106         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2107         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2108
2109         let b_chan_values = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
2110         // Route the first two HTLCs.
2111         let payment_value_1 = b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000;
2112         let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], payment_value_1);
2113         let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], 20_000);
2114
2115         // Start routing the third HTLC (this is just used to get everyone in the right state).
2116         let (route, payment_hash_3, payment_preimage_3, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
2117         let send_1 = {
2118                 nodes[0].node.send_payment_with_route(&route, payment_hash_3,
2119                         RecipientOnionFields::secret_only(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
2120                 check_added_monitors!(nodes[0], 1);
2121                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2122                 assert_eq!(events.len(), 1);
2123                 SendEvent::from_event(events.remove(0))
2124         };
2125
2126         // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
2127         // initial fulfill/CS.
2128         nodes[1].node.claim_funds(payment_preimage_1);
2129         expect_payment_claimed!(nodes[1], payment_hash_1, payment_value_1);
2130         check_added_monitors!(nodes[1], 1);
2131         let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2132
2133         // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
2134         // remove the second HTLC when we send the HTLC back from B to A.
2135         nodes[1].node.claim_funds(payment_preimage_2);
2136         expect_payment_claimed!(nodes[1], payment_hash_2, 20_000);
2137         check_added_monitors!(nodes[1], 1);
2138         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2139
2140         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]);
2141         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed);
2142         check_added_monitors!(nodes[0], 1);
2143         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2144         expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
2145
2146         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]);
2147         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg);
2148         check_added_monitors!(nodes[1], 1);
2149         // B is already AwaitingRAA, so cant generate a CS here
2150         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2151
2152         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2153         check_added_monitors!(nodes[1], 1);
2154         let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2155
2156         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2157         check_added_monitors!(nodes[0], 1);
2158         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2159
2160         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2161         check_added_monitors!(nodes[1], 1);
2162         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2163
2164         // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
2165         // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
2166         // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
2167         // can no longer broadcast a commitment transaction with it and B has the preimage so can go
2168         // on-chain as necessary).
2169         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]);
2170         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed);
2171         check_added_monitors!(nodes[0], 1);
2172         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2173         expect_payment_sent(&nodes[0], payment_preimage_2, None, false, false);
2174
2175         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2176         check_added_monitors!(nodes[1], 1);
2177         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2178
2179         expect_pending_htlcs_forwardable!(nodes[1]);
2180         expect_payment_claimable!(nodes[1], payment_hash_3, payment_secret_3, 100000);
2181
2182         // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
2183         // resolve the second HTLC from A's point of view.
2184         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2185         check_added_monitors!(nodes[0], 1);
2186         expect_payment_path_successful!(nodes[0]);
2187         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2188
2189         // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
2190         // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
2191         let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[1], nodes[0], 10000);
2192         let send_2 = {
2193                 nodes[1].node.send_payment_with_route(&route, payment_hash_4,
2194                         RecipientOnionFields::secret_only(payment_secret_4), PaymentId(payment_hash_4.0)).unwrap();
2195                 check_added_monitors!(nodes[1], 1);
2196                 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2197                 assert_eq!(events.len(), 1);
2198                 SendEvent::from_event(events.remove(0))
2199         };
2200
2201         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]);
2202         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg);
2203         check_added_monitors!(nodes[0], 1);
2204         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2205
2206         // Now just resolve all the outstanding messages/HTLCs for completeness...
2207
2208         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2209         check_added_monitors!(nodes[1], 1);
2210         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2211
2212         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2213         check_added_monitors!(nodes[1], 1);
2214
2215         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2216         check_added_monitors!(nodes[0], 1);
2217         expect_payment_path_successful!(nodes[0]);
2218         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2219
2220         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2221         check_added_monitors!(nodes[1], 1);
2222         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2223
2224         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2225         check_added_monitors!(nodes[0], 1);
2226
2227         expect_pending_htlcs_forwardable!(nodes[0]);
2228         expect_payment_claimable!(nodes[0], payment_hash_4, payment_secret_4, 10000);
2229
2230         claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
2231         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
2232 }
2233
2234 #[test]
2235 fn channel_monitor_network_test() {
2236         // Simple test which builds a network of ChannelManagers, connects them to each other, and
2237         // tests that ChannelMonitor is able to recover from various states.
2238         let chanmon_cfgs = create_chanmon_cfgs(5);
2239         let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
2240         let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
2241         let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
2242
2243         // Create some initial channels
2244         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2245         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2246         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
2247         let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
2248
2249         // Make sure all nodes are at the same starting height
2250         connect_blocks(&nodes[0], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
2251         connect_blocks(&nodes[1], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
2252         connect_blocks(&nodes[2], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
2253         connect_blocks(&nodes[3], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);
2254         connect_blocks(&nodes[4], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[4].best_block_info().1);
2255
2256         // Rebalance the network a bit by relaying one payment through all the channels...
2257         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2258         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2259         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2260         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2261
2262         // Simple case with no pending HTLCs:
2263         nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
2264         check_added_monitors!(nodes[1], 1);
2265         check_closed_broadcast!(nodes[1], true);
2266         {
2267                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
2268                 assert_eq!(node_txn.len(), 1);
2269                 mine_transaction(&nodes[0], &node_txn[0]);
2270                 check_added_monitors!(nodes[0], 1);
2271                 test_txn_broadcast(&nodes[0], &chan_1, Some(node_txn[0].clone()), HTLCType::NONE);
2272         }
2273         check_closed_broadcast!(nodes[0], true);
2274         assert_eq!(nodes[0].node.list_channels().len(), 0);
2275         assert_eq!(nodes[1].node.list_channels().len(), 1);
2276         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2277         check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
2278
2279         // One pending HTLC is discarded by the force-close:
2280         let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[1], &[&nodes[2], &nodes[3]], 3_000_000);
2281
2282         // Simple case of one pending HTLC to HTLC-Timeout (note that the HTLC-Timeout is not
2283         // broadcasted until we reach the timelock time).
2284         nodes[1].node.force_close_broadcasting_latest_txn(&chan_2.2, &nodes[2].node.get_our_node_id()).unwrap();
2285         check_closed_broadcast!(nodes[1], true);
2286         check_added_monitors!(nodes[1], 1);
2287         {
2288                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::NONE);
2289                 connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2290                 test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
2291                 mine_transaction(&nodes[2], &node_txn[0]);
2292                 check_added_monitors!(nodes[2], 1);
2293                 test_txn_broadcast(&nodes[2], &chan_2, Some(node_txn[0].clone()), HTLCType::NONE);
2294         }
2295         check_closed_broadcast!(nodes[2], true);
2296         assert_eq!(nodes[1].node.list_channels().len(), 0);
2297         assert_eq!(nodes[2].node.list_channels().len(), 1);
2298         check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
2299         check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2300
2301         macro_rules! claim_funds {
2302                 ($node: expr, $prev_node: expr, $preimage: expr, $payment_hash: expr) => {
2303                         {
2304                                 $node.node.claim_funds($preimage);
2305                                 expect_payment_claimed!($node, $payment_hash, 3_000_000);
2306                                 check_added_monitors!($node, 1);
2307
2308                                 let events = $node.node.get_and_clear_pending_msg_events();
2309                                 assert_eq!(events.len(), 1);
2310                                 match events[0] {
2311                                         MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
2312                                                 assert!(update_add_htlcs.is_empty());
2313                                                 assert!(update_fail_htlcs.is_empty());
2314                                                 assert_eq!(*node_id, $prev_node.node.get_our_node_id());
2315                                         },
2316                                         _ => panic!("Unexpected event"),
2317                                 };
2318                         }
2319                 }
2320         }
2321
2322         // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
2323         // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
2324         nodes[2].node.force_close_broadcasting_latest_txn(&chan_3.2, &nodes[3].node.get_our_node_id()).unwrap();
2325         check_added_monitors!(nodes[2], 1);
2326         check_closed_broadcast!(nodes[2], true);
2327         let node2_commitment_txid;
2328         {
2329                 let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::NONE);
2330                 connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2331                 test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
2332                 node2_commitment_txid = node_txn[0].txid();
2333
2334                 // Claim the payment on nodes[3], giving it knowledge of the preimage
2335                 claim_funds!(nodes[3], nodes[2], payment_preimage_1, payment_hash_1);
2336                 mine_transaction(&nodes[3], &node_txn[0]);
2337                 check_added_monitors!(nodes[3], 1);
2338                 check_preimage_claim(&nodes[3], &node_txn);
2339         }
2340         check_closed_broadcast!(nodes[3], true);
2341         assert_eq!(nodes[2].node.list_channels().len(), 0);
2342         assert_eq!(nodes[3].node.list_channels().len(), 1);
2343         check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[3].node.get_our_node_id()], 100000);
2344         check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
2345
2346         // Drop the ChannelMonitor for the previous channel to avoid it broadcasting transactions and
2347         // confusing us in the following tests.
2348         let chan_3_mon = nodes[3].chain_monitor.chain_monitor.remove_monitor(&OutPoint { txid: chan_3.3.txid(), index: 0 });
2349
2350         // One pending HTLC to time out:
2351         let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[3], &[&nodes[4]], 3_000_000);
2352         // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
2353         // buffer space).
2354
2355         let (close_chan_update_1, close_chan_update_2) = {
2356                 connect_blocks(&nodes[3], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
2357                 let events = nodes[3].node.get_and_clear_pending_msg_events();
2358                 assert_eq!(events.len(), 2);
2359                 let close_chan_update_1 = match events[0] {
2360                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2361                                 msg.clone()
2362                         },
2363                         _ => panic!("Unexpected event"),
2364                 };
2365                 match events[1] {
2366                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2367                                 assert_eq!(node_id, nodes[4].node.get_our_node_id());
2368                         },
2369                         _ => panic!("Unexpected event"),
2370                 }
2371                 check_added_monitors!(nodes[3], 1);
2372
2373                 // Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer.
2374                 {
2375                         let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap();
2376                         node_txn.retain(|tx| {
2377                                 if tx.input[0].previous_output.txid == node2_commitment_txid {
2378                                         false
2379                                 } else { true }
2380                         });
2381                 }
2382
2383                 let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
2384
2385                 // Claim the payment on nodes[4], giving it knowledge of the preimage
2386                 claim_funds!(nodes[4], nodes[3], payment_preimage_2, payment_hash_2);
2387
2388                 connect_blocks(&nodes[4], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2389                 let events = nodes[4].node.get_and_clear_pending_msg_events();
2390                 assert_eq!(events.len(), 2);
2391                 let close_chan_update_2 = match events[0] {
2392                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2393                                 msg.clone()
2394                         },
2395                         _ => panic!("Unexpected event"),
2396                 };
2397                 match events[1] {
2398                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2399                                 assert_eq!(node_id, nodes[3].node.get_our_node_id());
2400                         },
2401                         _ => panic!("Unexpected event"),
2402                 }
2403                 check_added_monitors!(nodes[4], 1);
2404                 test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
2405
2406                 mine_transaction(&nodes[4], &node_txn[0]);
2407                 check_preimage_claim(&nodes[4], &node_txn);
2408                 (close_chan_update_1, close_chan_update_2)
2409         };
2410         nodes[3].gossip_sync.handle_channel_update(&close_chan_update_2).unwrap();
2411         nodes[4].gossip_sync.handle_channel_update(&close_chan_update_1).unwrap();
2412         assert_eq!(nodes[3].node.list_channels().len(), 0);
2413         assert_eq!(nodes[4].node.list_channels().len(), 0);
2414
2415         assert_eq!(nodes[3].chain_monitor.chain_monitor.watch_channel(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon),
2416                 ChannelMonitorUpdateStatus::Completed);
2417         check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed, [nodes[4].node.get_our_node_id()], 100000);
2418         check_closed_event!(nodes[4], 1, ClosureReason::CommitmentTxConfirmed, [nodes[3].node.get_our_node_id()], 100000);
2419 }
2420
2421 #[test]
2422 fn test_justice_tx_htlc_timeout() {
2423         // Test justice txn built on revoked HTLC-Timeout tx, against both sides
2424         let mut alice_config = UserConfig::default();
2425         alice_config.channel_handshake_config.announced_channel = true;
2426         alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2427         alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2428         let mut bob_config = UserConfig::default();
2429         bob_config.channel_handshake_config.announced_channel = true;
2430         bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2431         bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2432         let user_cfgs = [Some(alice_config), Some(bob_config)];
2433         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2434         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2435         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2436         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2437         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2438         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2439         // Create some new channels:
2440         let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1);
2441
2442         // A pending HTLC which will be revoked:
2443         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2444         // Get the will-be-revoked local txn from nodes[0]
2445         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_5.2);
2446         assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
2447         assert_eq!(revoked_local_txn[0].input.len(), 1);
2448         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
2449         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
2450         assert_eq!(revoked_local_txn[1].input.len(), 1);
2451         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2452         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2453         // Revoke the old state
2454         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
2455
2456         {
2457                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2458                 {
2459                         let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2460                         assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2461                         assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
2462                         check_spends!(node_txn[0], revoked_local_txn[0]);
2463                         node_txn.swap_remove(0);
2464                 }
2465                 check_added_monitors!(nodes[1], 1);
2466                 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2467                 test_txn_broadcast(&nodes[1], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2468
2469                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2470                 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2471                 // Verify broadcast of revoked HTLC-timeout
2472                 let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
2473                 check_added_monitors!(nodes[0], 1);
2474                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2475                 // Broadcast revoked HTLC-timeout on node 1
2476                 mine_transaction(&nodes[1], &node_txn[1]);
2477                 test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone());
2478         }
2479         get_announce_close_broadcast_events(&nodes, 0, 1);
2480         assert_eq!(nodes[0].node.list_channels().len(), 0);
2481         assert_eq!(nodes[1].node.list_channels().len(), 0);
2482 }
2483
2484 #[test]
2485 fn test_justice_tx_htlc_success() {
2486         // Test justice txn built on revoked HTLC-Success tx, against both sides
2487         let mut alice_config = UserConfig::default();
2488         alice_config.channel_handshake_config.announced_channel = true;
2489         alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2490         alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2491         let mut bob_config = UserConfig::default();
2492         bob_config.channel_handshake_config.announced_channel = true;
2493         bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2494         bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2495         let user_cfgs = [Some(alice_config), Some(bob_config)];
2496         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2497         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2498         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2499         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2500         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2501         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2502         // Create some new channels:
2503         let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1);
2504
2505         // A pending HTLC which will be revoked:
2506         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2507         // Get the will-be-revoked local txn from B
2508         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_6.2);
2509         assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
2510         assert_eq!(revoked_local_txn[0].input.len(), 1);
2511         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
2512         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
2513         // Revoke the old state
2514         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
2515         {
2516                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2517                 {
2518                         let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
2519                         assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2520                         assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
2521
2522                         check_spends!(node_txn[0], revoked_local_txn[0]);
2523                         node_txn.swap_remove(0);
2524                 }
2525                 check_added_monitors!(nodes[0], 1);
2526                 test_txn_broadcast(&nodes[0], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2527
2528                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2529                 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2530                 let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
2531                 check_added_monitors!(nodes[1], 1);
2532                 mine_transaction(&nodes[0], &node_txn[1]);
2533                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2534                 test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone(), revoked_local_txn[0].clone());
2535         }
2536         get_announce_close_broadcast_events(&nodes, 0, 1);
2537         assert_eq!(nodes[0].node.list_channels().len(), 0);
2538         assert_eq!(nodes[1].node.list_channels().len(), 0);
2539 }
2540
2541 #[test]
2542 fn revoked_output_claim() {
2543         // Simple test to ensure a node will claim a revoked output when a stale remote commitment
2544         // transaction is broadcast by its counterparty
2545         let chanmon_cfgs = create_chanmon_cfgs(2);
2546         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2547         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2548         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2549         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2550         // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
2551         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2552         assert_eq!(revoked_local_txn.len(), 1);
2553         // Only output is the full channel value back to nodes[0]:
2554         assert_eq!(revoked_local_txn[0].output.len(), 1);
2555         // Send a payment through, updating everyone's latest commitment txn
2556         send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
2557
2558         // Inform nodes[1] that nodes[0] broadcast a stale tx
2559         mine_transaction(&nodes[1], &revoked_local_txn[0]);
2560         check_added_monitors!(nodes[1], 1);
2561         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2562         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2563         assert_eq!(node_txn.len(), 1); // ChannelMonitor: justice tx against revoked to_local output
2564
2565         check_spends!(node_txn[0], revoked_local_txn[0]);
2566
2567         // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
2568         mine_transaction(&nodes[0], &revoked_local_txn[0]);
2569         get_announce_close_broadcast_events(&nodes, 0, 1);
2570         check_added_monitors!(nodes[0], 1);
2571         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2572 }
2573
2574 #[test]
2575 fn test_forming_justice_tx_from_monitor_updates() {
2576         do_test_forming_justice_tx_from_monitor_updates(true);
2577         do_test_forming_justice_tx_from_monitor_updates(false);
2578 }
2579
2580 fn do_test_forming_justice_tx_from_monitor_updates(broadcast_initial_commitment: bool) {
2581         // Simple test to make sure that the justice tx formed in WatchtowerPersister
2582         // is properly formed and can be broadcasted/confirmed successfully in the event
2583         // that a revoked commitment transaction is broadcasted
2584         // (Similar to `revoked_output_claim` test but we get the justice tx + broadcast manually)
2585         let chanmon_cfgs = create_chanmon_cfgs(2);
2586         let destination_script0 = chanmon_cfgs[0].keys_manager.get_destination_script().unwrap();
2587         let destination_script1 = chanmon_cfgs[1].keys_manager.get_destination_script().unwrap();
2588         let persisters = vec![WatchtowerPersister::new(destination_script0),
2589                 WatchtowerPersister::new(destination_script1)];
2590         let node_cfgs = create_node_cfgs_with_persisters(2, &chanmon_cfgs, persisters.iter().collect());
2591         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2592         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2593         let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
2594         let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
2595
2596         if !broadcast_initial_commitment {
2597                 // Send a payment to move the channel forward
2598                 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2599         }
2600
2601         // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output.
2602         // We'll keep this commitment transaction to broadcast once it's revoked.
2603         let revoked_local_txn = get_local_commitment_txn!(nodes[0], channel_id);
2604         assert_eq!(revoked_local_txn.len(), 1);
2605         let revoked_commitment_tx = &revoked_local_txn[0];
2606
2607         // Send another payment, now revoking the previous commitment tx
2608         send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2609
2610         let justice_tx = persisters[1].justice_tx(funding_txo, &revoked_commitment_tx.txid()).unwrap();
2611         check_spends!(justice_tx, revoked_commitment_tx);
2612
2613         mine_transactions(&nodes[1], &[revoked_commitment_tx, &justice_tx]);
2614         mine_transactions(&nodes[0], &[revoked_commitment_tx, &justice_tx]);
2615
2616         check_added_monitors!(nodes[1], 1);
2617         check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false,
2618                 &[nodes[0].node.get_our_node_id()], 100_000);
2619         get_announce_close_broadcast_events(&nodes, 1, 0);
2620
2621         check_added_monitors!(nodes[0], 1);
2622         check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false,
2623                 &[nodes[1].node.get_our_node_id()], 100_000);
2624
2625         // Check that the justice tx has sent the revoked output value to nodes[1]
2626         let monitor = get_monitor!(nodes[1], channel_id);
2627         let total_claimable_balance = monitor.get_claimable_balances().iter().fold(0, |sum, balance| {
2628                 match balance {
2629                         channelmonitor::Balance::ClaimableAwaitingConfirmations { amount_satoshis, .. } => sum + amount_satoshis,
2630                         _ => panic!("Unexpected balance type"),
2631                 }
2632         });
2633         // On the first commitment, node[1]'s balance was below dust so it didn't have an output
2634         let node1_channel_balance = if broadcast_initial_commitment { 0 } else { revoked_commitment_tx.output[0].value };
2635         let expected_claimable_balance = node1_channel_balance + justice_tx.output[0].value;
2636         assert_eq!(total_claimable_balance, expected_claimable_balance);
2637 }
2638
2639
2640 #[test]
2641 fn claim_htlc_outputs_shared_tx() {
2642         // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
2643         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2644         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2645         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2646         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2647         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2648
2649         // Create some new channel:
2650         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2651
2652         // Rebalance the network to generate htlc in the two directions
2653         send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2654         // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx
2655         let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2656         let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2657
2658         // Get the will-be-revoked local txn from node[0]
2659         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2660         assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
2661         assert_eq!(revoked_local_txn[0].input.len(), 1);
2662         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
2663         assert_eq!(revoked_local_txn[1].input.len(), 1);
2664         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2665         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2666         check_spends!(revoked_local_txn[1], revoked_local_txn[0]);
2667
2668         //Revoke the old state
2669         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2670
2671         {
2672                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2673                 check_added_monitors!(nodes[0], 1);
2674                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2675                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2676                 check_added_monitors!(nodes[1], 1);
2677                 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2678                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2679                 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2680
2681                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2682                 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2683
2684                 assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
2685                 check_spends!(node_txn[0], revoked_local_txn[0]);
2686
2687                 let mut witness_lens = BTreeSet::new();
2688                 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2689                 witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
2690                 witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
2691                 assert_eq!(witness_lens.len(), 3);
2692                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2693                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2694                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2695
2696                 // Finally, mine the penalty transaction and check that we get an HTLC failure after
2697                 // ANTI_REORG_DELAY confirmations.
2698                 mine_transaction(&nodes[1], &node_txn[0]);
2699                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2700                 expect_payment_failed!(nodes[1], payment_hash_2, false);
2701         }
2702         get_announce_close_broadcast_events(&nodes, 0, 1);
2703         assert_eq!(nodes[0].node.list_channels().len(), 0);
2704         assert_eq!(nodes[1].node.list_channels().len(), 0);
2705 }
2706
2707 #[test]
2708 fn claim_htlc_outputs_single_tx() {
2709         // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
2710         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2711         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2712         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2713         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2714         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2715
2716         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2717
2718         // Rebalance the network to generate htlc in the two directions
2719         send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2720         // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx, but this
2721         // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
2722         let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2723         let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2724
2725         // Get the will-be-revoked local txn from node[0]
2726         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2727
2728         //Revoke the old state
2729         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2730
2731         {
2732                 confirm_transaction_at(&nodes[0], &revoked_local_txn[0], 100);
2733                 check_added_monitors!(nodes[0], 1);
2734                 confirm_transaction_at(&nodes[1], &revoked_local_txn[0], 100);
2735                 check_added_monitors!(nodes[1], 1);
2736                 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2737                 let mut events = nodes[0].node.get_and_clear_pending_events();
2738                 expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
2739                 match events.last().unwrap() {
2740                         Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2741                         _ => panic!("Unexpected event"),
2742                 }
2743
2744                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2745                 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2746
2747                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcast();
2748
2749                 // Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
2750                 assert_eq!(node_txn[0].input.len(), 1);
2751                 check_spends!(node_txn[0], chan_1.3);
2752                 assert_eq!(node_txn[1].input.len(), 1);
2753                 let witness_script = node_txn[1].input[0].witness.last().unwrap();
2754                 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
2755                 check_spends!(node_txn[1], node_txn[0]);
2756
2757                 // Filter out any non justice transactions.
2758                 node_txn.retain(|tx| tx.input[0].previous_output.txid == revoked_local_txn[0].txid());
2759                 assert!(node_txn.len() > 3);
2760
2761                 assert_eq!(node_txn[0].input.len(), 1);
2762                 assert_eq!(node_txn[1].input.len(), 1);
2763                 assert_eq!(node_txn[2].input.len(), 1);
2764
2765                 check_spends!(node_txn[0], revoked_local_txn[0]);
2766                 check_spends!(node_txn[1], revoked_local_txn[0]);
2767                 check_spends!(node_txn[2], revoked_local_txn[0]);
2768
2769                 let mut witness_lens = BTreeSet::new();
2770                 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2771                 witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
2772                 witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
2773                 assert_eq!(witness_lens.len(), 3);
2774                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2775                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2776                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2777
2778                 // Finally, mine the penalty transactions and check that we get an HTLC failure after
2779                 // ANTI_REORG_DELAY confirmations.
2780                 mine_transaction(&nodes[1], &node_txn[0]);
2781                 mine_transaction(&nodes[1], &node_txn[1]);
2782                 mine_transaction(&nodes[1], &node_txn[2]);
2783                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2784                 expect_payment_failed!(nodes[1], payment_hash_2, false);
2785         }
2786         get_announce_close_broadcast_events(&nodes, 0, 1);
2787         assert_eq!(nodes[0].node.list_channels().len(), 0);
2788         assert_eq!(nodes[1].node.list_channels().len(), 0);
2789 }
2790
2791 #[test]
2792 fn test_htlc_on_chain_success() {
2793         // Test that in case of a unilateral close onchain, we detect the state of output and pass
2794         // the preimage backward accordingly. So here we test that ChannelManager is
2795         // broadcasting the right event to other nodes in payment path.
2796         // We test with two HTLCs simultaneously as that was not handled correctly in the past.
2797         // A --------------------> B ----------------------> C (preimage)
2798         // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
2799         // commitment transaction was broadcast.
2800         // Then, B should learn the preimage from said transactions, attempting to claim backwards
2801         // towards B.
2802         // B should be able to claim via preimage if A then broadcasts its local tx.
2803         // Finally, when A sees B's latest local commitment transaction it should be able to claim
2804         // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
2805         // PaymentSent event).
2806
2807         let chanmon_cfgs = create_chanmon_cfgs(3);
2808         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2809         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2810         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2811
2812         // Create some initial channels
2813         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2814         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2815
2816         // Ensure all nodes are at the same height
2817         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
2818         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
2819         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
2820         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
2821
2822         // Rebalance the network a bit by relaying one payment through all the channels...
2823         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2824         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2825
2826         let (our_payment_preimage, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2827         let (our_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2828
2829         // Broadcast legit commitment tx from C on B's chain
2830         // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
2831         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
2832         assert_eq!(commitment_tx.len(), 1);
2833         check_spends!(commitment_tx[0], chan_2.3);
2834         nodes[2].node.claim_funds(our_payment_preimage);
2835         expect_payment_claimed!(nodes[2], payment_hash_1, 3_000_000);
2836         nodes[2].node.claim_funds(our_payment_preimage_2);
2837         expect_payment_claimed!(nodes[2], payment_hash_2, 3_000_000);
2838         check_added_monitors!(nodes[2], 2);
2839         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2840         assert!(updates.update_add_htlcs.is_empty());
2841         assert!(updates.update_fail_htlcs.is_empty());
2842         assert!(updates.update_fail_malformed_htlcs.is_empty());
2843         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2844
2845         mine_transaction(&nodes[2], &commitment_tx[0]);
2846         check_closed_broadcast!(nodes[2], true);
2847         check_added_monitors!(nodes[2], 1);
2848         check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2849         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 2 (2 * HTLC-Success tx)
2850         assert_eq!(node_txn.len(), 2);
2851         check_spends!(node_txn[0], commitment_tx[0]);
2852         check_spends!(node_txn[1], commitment_tx[0]);
2853         assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2854         assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2855         assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2856         assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2857         assert_eq!(node_txn[0].lock_time.0, 0);
2858         assert_eq!(node_txn[1].lock_time.0, 0);
2859
2860         // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
2861         connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![commitment_tx[0].clone(), node_txn[0].clone(), node_txn[1].clone()]));
2862         connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2863         {
2864                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2865                 assert_eq!(added_monitors.len(), 1);
2866                 assert_eq!(added_monitors[0].0.txid, chan_2.3.txid());
2867                 added_monitors.clear();
2868         }
2869         let forwarded_events = nodes[1].node.get_and_clear_pending_events();
2870         assert_eq!(forwarded_events.len(), 3);
2871         match forwarded_events[0] {
2872                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2873                 _ => panic!("Unexpected event"),
2874         }
2875         let chan_id = Some(chan_1.2);
2876         match forwarded_events[1] {
2877                 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2878                         assert_eq!(fee_earned_msat, Some(1000));
2879                         assert_eq!(prev_channel_id, chan_id);
2880                         assert_eq!(claim_from_onchain_tx, true);
2881                         assert_eq!(next_channel_id, Some(chan_2.2));
2882                         assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2883                 },
2884                 _ => panic!()
2885         }
2886         match forwarded_events[2] {
2887                 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2888                         assert_eq!(fee_earned_msat, Some(1000));
2889                         assert_eq!(prev_channel_id, chan_id);
2890                         assert_eq!(claim_from_onchain_tx, true);
2891                         assert_eq!(next_channel_id, Some(chan_2.2));
2892                         assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2893                 },
2894                 _ => panic!()
2895         }
2896         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2897         {
2898                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2899                 assert_eq!(added_monitors.len(), 2);
2900                 assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
2901                 assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
2902                 added_monitors.clear();
2903         }
2904         assert_eq!(events.len(), 3);
2905
2906         let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
2907         let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
2908
2909         match nodes_2_event {
2910                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
2911                 _ => panic!("Unexpected event"),
2912         }
2913
2914         match nodes_0_event {
2915                 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
2916                         assert!(update_add_htlcs.is_empty());
2917                         assert!(update_fail_htlcs.is_empty());
2918                         assert_eq!(update_fulfill_htlcs.len(), 1);
2919                         assert!(update_fail_malformed_htlcs.is_empty());
2920                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2921                 },
2922                 _ => panic!("Unexpected event"),
2923         };
2924
2925         // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
2926         match events[0] {
2927                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
2928                 _ => panic!("Unexpected event"),
2929         }
2930
2931         macro_rules! check_tx_local_broadcast {
2932                 ($node: expr, $htlc_offered: expr, $commitment_tx: expr) => { {
2933                         let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
2934                         assert_eq!(node_txn.len(), 2);
2935                         // Node[1]: 2 * HTLC-timeout tx
2936                         // Node[0]: 2 * HTLC-timeout tx
2937                         check_spends!(node_txn[0], $commitment_tx);
2938                         check_spends!(node_txn[1], $commitment_tx);
2939                         assert_ne!(node_txn[0].lock_time.0, 0);
2940                         assert_ne!(node_txn[1].lock_time.0, 0);
2941                         if $htlc_offered {
2942                                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2943                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2944                                 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2945                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2946                         } else {
2947                                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2948                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2949                                 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2950                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2951                         }
2952                         node_txn.clear();
2953                 } }
2954         }
2955         // nodes[1] now broadcasts its own timeout-claim of the output that nodes[2] just claimed via success.
2956         check_tx_local_broadcast!(nodes[1], false, commitment_tx[0]);
2957
2958         // Broadcast legit commitment tx from A on B's chain
2959         // Broadcast preimage tx by B on offered output from A commitment tx  on A's chain
2960         let node_a_commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
2961         check_spends!(node_a_commitment_tx[0], chan_1.3);
2962         mine_transaction(&nodes[1], &node_a_commitment_tx[0]);
2963         check_closed_broadcast!(nodes[1], true);
2964         check_added_monitors!(nodes[1], 1);
2965         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2966         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2967         assert!(node_txn.len() == 1 || node_txn.len() == 3); // HTLC-Success, 2* RBF bumps of above HTLC txn
2968         let commitment_spend =
2969                 if node_txn.len() == 1 {
2970                         &node_txn[0]
2971                 } else {
2972                         // Certain `ConnectStyle`s will cause RBF bumps of the previous HTLC transaction to be broadcast.
2973                         // FullBlockViaListen
2974                         if node_txn[0].input[0].previous_output.txid == node_a_commitment_tx[0].txid() {
2975                                 check_spends!(node_txn[1], commitment_tx[0]);
2976                                 check_spends!(node_txn[2], commitment_tx[0]);
2977                                 assert_ne!(node_txn[1].input[0].previous_output.vout, node_txn[2].input[0].previous_output.vout);
2978                                 &node_txn[0]
2979                         } else {
2980                                 check_spends!(node_txn[0], commitment_tx[0]);
2981                                 check_spends!(node_txn[1], commitment_tx[0]);
2982                                 assert_ne!(node_txn[0].input[0].previous_output.vout, node_txn[1].input[0].previous_output.vout);
2983                                 &node_txn[2]
2984                         }
2985                 };
2986
2987         check_spends!(commitment_spend, node_a_commitment_tx[0]);
2988         assert_eq!(commitment_spend.input.len(), 2);
2989         assert_eq!(commitment_spend.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2990         assert_eq!(commitment_spend.input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2991         assert_eq!(commitment_spend.lock_time.0, nodes[1].best_block_info().1);
2992         assert!(commitment_spend.output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2993         // We don't bother to check that B can claim the HTLC output on its commitment tx here as
2994         // we already checked the same situation with A.
2995
2996         // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
2997         connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![node_a_commitment_tx[0].clone(), commitment_spend.clone()]));
2998         connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
2999         check_closed_broadcast!(nodes[0], true);
3000         check_added_monitors!(nodes[0], 1);
3001         let events = nodes[0].node.get_and_clear_pending_events();
3002         assert_eq!(events.len(), 5);
3003         let mut first_claimed = false;
3004         for event in events {
3005                 match event {
3006                         Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3007                                 if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
3008                                         assert!(!first_claimed);
3009                                         first_claimed = true;
3010                                 } else {
3011                                         assert_eq!(payment_preimage, our_payment_preimage_2);
3012                                         assert_eq!(payment_hash, payment_hash_2);
3013                                 }
3014                         },
3015                         Event::PaymentPathSuccessful { .. } => {},
3016                         Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
3017                         _ => panic!("Unexpected event"),
3018                 }
3019         }
3020         check_tx_local_broadcast!(nodes[0], true, node_a_commitment_tx[0]);
3021 }
3022
3023 fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
3024         // Test that in case of a unilateral close onchain, we detect the state of output and
3025         // timeout the HTLC backward accordingly. So here we test that ChannelManager is
3026         // broadcasting the right event to other nodes in payment path.
3027         // A ------------------> B ----------------------> C (timeout)
3028         //    B's commitment tx                 C's commitment tx
3029         //            \                                  \
3030         //         B's HTLC timeout tx               B's timeout tx
3031
3032         let chanmon_cfgs = create_chanmon_cfgs(3);
3033         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3034         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3035         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3036         *nodes[0].connect_style.borrow_mut() = connect_style;
3037         *nodes[1].connect_style.borrow_mut() = connect_style;
3038         *nodes[2].connect_style.borrow_mut() = connect_style;
3039
3040         // Create some intial channels
3041         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
3042         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3043
3044         // Rebalance the network a bit by relaying one payment thorugh all the channels...
3045         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3046         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3047
3048         let (_payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
3049
3050         // Broadcast legit commitment tx from C on B's chain
3051         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
3052         check_spends!(commitment_tx[0], chan_2.3);
3053         nodes[2].node.fail_htlc_backwards(&payment_hash);
3054         check_added_monitors!(nodes[2], 0);
3055         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }]);
3056         check_added_monitors!(nodes[2], 1);
3057
3058         let events = nodes[2].node.get_and_clear_pending_msg_events();
3059         assert_eq!(events.len(), 1);
3060         match events[0] {
3061                 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, .. } } => {
3062                         assert!(update_add_htlcs.is_empty());
3063                         assert!(!update_fail_htlcs.is_empty());
3064                         assert!(update_fulfill_htlcs.is_empty());
3065                         assert!(update_fail_malformed_htlcs.is_empty());
3066                         assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
3067                 },
3068                 _ => panic!("Unexpected event"),
3069         };
3070         mine_transaction(&nodes[2], &commitment_tx[0]);
3071         check_closed_broadcast!(nodes[2], true);
3072         check_added_monitors!(nodes[2], 1);
3073         check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3074         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
3075         assert_eq!(node_txn.len(), 0);
3076
3077         // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
3078         // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
3079         mine_transaction(&nodes[1], &commitment_tx[0]);
3080         check_closed_event!(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false
3081                 , [nodes[2].node.get_our_node_id()], 100000);
3082         connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
3083         let timeout_tx = {
3084                 let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
3085                 if nodes[1].connect_style.borrow().skips_blocks() {
3086                         assert_eq!(txn.len(), 1);
3087                 } else {
3088                         assert_eq!(txn.len(), 3); // Two extra fee bumps for timeout transaction
3089                 }
3090                 txn.iter().for_each(|tx| check_spends!(tx, commitment_tx[0]));
3091                 assert_eq!(txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3092                 txn.remove(0)
3093         };
3094
3095         mine_transaction(&nodes[1], &timeout_tx);
3096         check_added_monitors!(nodes[1], 1);
3097         check_closed_broadcast!(nodes[1], true);
3098
3099         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3100
3101         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
3102         check_added_monitors!(nodes[1], 1);
3103         let events = nodes[1].node.get_and_clear_pending_msg_events();
3104         assert_eq!(events.len(), 1);
3105         match events[0] {
3106                 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
3107                         assert!(update_add_htlcs.is_empty());
3108                         assert!(!update_fail_htlcs.is_empty());
3109                         assert!(update_fulfill_htlcs.is_empty());
3110                         assert!(update_fail_malformed_htlcs.is_empty());
3111                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3112                 },
3113                 _ => panic!("Unexpected event"),
3114         };
3115
3116         // Broadcast legit commitment tx from B on A's chain
3117         let commitment_tx = get_local_commitment_txn!(nodes[1], chan_1.2);
3118         check_spends!(commitment_tx[0], chan_1.3);
3119
3120         mine_transaction(&nodes[0], &commitment_tx[0]);
3121         connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
3122
3123         check_closed_broadcast!(nodes[0], true);
3124         check_added_monitors!(nodes[0], 1);
3125         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3126         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // 1 timeout tx
3127         assert_eq!(node_txn.len(), 1);
3128         check_spends!(node_txn[0], commitment_tx[0]);
3129         assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3130 }
3131
3132 #[test]
3133 fn test_htlc_on_chain_timeout() {
3134         do_test_htlc_on_chain_timeout(ConnectStyle::BestBlockFirstSkippingBlocks);
3135         do_test_htlc_on_chain_timeout(ConnectStyle::TransactionsFirstSkippingBlocks);
3136         do_test_htlc_on_chain_timeout(ConnectStyle::FullBlockViaListen);
3137 }
3138
3139 #[test]
3140 fn test_simple_commitment_revoked_fail_backward() {
3141         // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
3142         // and fail backward accordingly.
3143
3144         let chanmon_cfgs = create_chanmon_cfgs(3);
3145         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3146         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3147         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3148
3149         // Create some initial channels
3150         create_announced_chan_between_nodes(&nodes, 0, 1);
3151         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3152
3153         let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3154         // Get the will-be-revoked local txn from nodes[2]
3155         let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3156         // Revoke the old state
3157         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3158
3159         let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3160
3161         mine_transaction(&nodes[1], &revoked_local_txn[0]);
3162         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3163         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3164         check_added_monitors!(nodes[1], 1);
3165         check_closed_broadcast!(nodes[1], true);
3166
3167         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
3168         check_added_monitors!(nodes[1], 1);
3169         let events = nodes[1].node.get_and_clear_pending_msg_events();
3170         assert_eq!(events.len(), 1);
3171         match events[0] {
3172                 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
3173                         assert!(update_add_htlcs.is_empty());
3174                         assert_eq!(update_fail_htlcs.len(), 1);
3175                         assert!(update_fulfill_htlcs.is_empty());
3176                         assert!(update_fail_malformed_htlcs.is_empty());
3177                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3178
3179                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3180                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3181                         expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_2.0.contents.short_channel_id, true);
3182                 },
3183                 _ => panic!("Unexpected event"),
3184         }
3185 }
3186
3187 fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
3188         // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
3189         // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
3190         // commitment transaction anymore.
3191         // To do this, we have the peer which will broadcast a revoked commitment transaction send
3192         // a number of update_fail/commitment_signed updates without ever sending the RAA in
3193         // response to our commitment_signed. This is somewhat misbehavior-y, though not
3194         // technically disallowed and we should probably handle it reasonably.
3195         // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
3196         // failed/fulfilled backwards must be in at least one of the latest two remote commitment
3197         // transactions:
3198         // * Once we move it out of our holding cell/add it, we will immediately include it in a
3199         //   commitment_signed (implying it will be in the latest remote commitment transaction).
3200         // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
3201         //   and once they revoke the previous commitment transaction (allowing us to send a new
3202         //   commitment_signed) we will be free to fail/fulfill the HTLC backwards.
3203         let chanmon_cfgs = create_chanmon_cfgs(3);
3204         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3205         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3206         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3207
3208         // Create some initial channels
3209         create_announced_chan_between_nodes(&nodes, 0, 1);
3210         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3211
3212         let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
3213         // Get the will-be-revoked local txn from nodes[2]
3214         let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3215         assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
3216         // Revoke the old state
3217         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3218
3219         let value = if use_dust {
3220                 // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
3221                 // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
3222                 nodes[2].node.per_peer_state.read().unwrap().get(&nodes[1].node.get_our_node_id())
3223                         .unwrap().lock().unwrap().channel_by_id.get(&chan_2.2).unwrap().context().holder_dust_limit_satoshis * 1000
3224         } else { 3000000 };
3225
3226         let (_, first_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3227         let (_, second_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3228         let (_, third_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3229
3230         nodes[2].node.fail_htlc_backwards(&first_payment_hash);
3231         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
3232         check_added_monitors!(nodes[2], 1);
3233         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3234         assert!(updates.update_add_htlcs.is_empty());
3235         assert!(updates.update_fulfill_htlcs.is_empty());
3236         assert!(updates.update_fail_malformed_htlcs.is_empty());
3237         assert_eq!(updates.update_fail_htlcs.len(), 1);
3238         assert!(updates.update_fee.is_none());
3239         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3240         let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
3241         // Drop the last RAA from 3 -> 2
3242
3243         nodes[2].node.fail_htlc_backwards(&second_payment_hash);
3244         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: second_payment_hash }]);
3245         check_added_monitors!(nodes[2], 1);
3246         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3247         assert!(updates.update_add_htlcs.is_empty());
3248         assert!(updates.update_fulfill_htlcs.is_empty());
3249         assert!(updates.update_fail_malformed_htlcs.is_empty());
3250         assert_eq!(updates.update_fail_htlcs.len(), 1);
3251         assert!(updates.update_fee.is_none());
3252         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3253         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3254         check_added_monitors!(nodes[1], 1);
3255         // Note that nodes[1] is in AwaitingRAA, so won't send a CS
3256         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3257         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3258         check_added_monitors!(nodes[2], 1);
3259
3260         nodes[2].node.fail_htlc_backwards(&third_payment_hash);
3261         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: third_payment_hash }]);
3262         check_added_monitors!(nodes[2], 1);
3263         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3264         assert!(updates.update_add_htlcs.is_empty());
3265         assert!(updates.update_fulfill_htlcs.is_empty());
3266         assert!(updates.update_fail_malformed_htlcs.is_empty());
3267         assert_eq!(updates.update_fail_htlcs.len(), 1);
3268         assert!(updates.update_fee.is_none());
3269         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3270         // At this point first_payment_hash has dropped out of the latest two commitment
3271         // transactions that nodes[1] is tracking...
3272         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3273         check_added_monitors!(nodes[1], 1);
3274         // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
3275         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3276         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3277         check_added_monitors!(nodes[2], 1);
3278
3279         // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
3280         // on nodes[2]'s RAA.
3281         let (route, fourth_payment_hash, _, fourth_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 1000000);
3282         nodes[1].node.send_payment_with_route(&route, fourth_payment_hash,
3283                 RecipientOnionFields::secret_only(fourth_payment_secret), PaymentId(fourth_payment_hash.0)).unwrap();
3284         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3285         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3286         check_added_monitors!(nodes[1], 0);
3287
3288         if deliver_bs_raa {
3289                 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa);
3290                 // One monitor for the new revocation preimage, no second on as we won't generate a new
3291                 // commitment transaction for nodes[0] until process_pending_htlc_forwards().
3292                 check_added_monitors!(nodes[1], 1);
3293                 let events = nodes[1].node.get_and_clear_pending_events();
3294                 assert_eq!(events.len(), 2);
3295                 match events[0] {
3296                         Event::PendingHTLCsForwardable { .. } => { },
3297                         _ => panic!("Unexpected event"),
3298                 };
3299                 match events[1] {
3300                         Event::HTLCHandlingFailed { .. } => { },
3301                         _ => panic!("Unexpected event"),
3302                 }
3303                 // Deliberately don't process the pending fail-back so they all fail back at once after
3304                 // block connection just like the !deliver_bs_raa case
3305         }
3306
3307         let mut failed_htlcs = HashSet::new();
3308         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3309
3310         mine_transaction(&nodes[1], &revoked_local_txn[0]);
3311         check_added_monitors!(nodes[1], 1);
3312         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3313
3314         let events = nodes[1].node.get_and_clear_pending_events();
3315         assert_eq!(events.len(), if deliver_bs_raa { 3 + nodes.len() - 1 } else { 4 + nodes.len() });
3316         match events[0] {
3317                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => { },
3318                 _ => panic!("Unexepected event"),
3319         }
3320         match events[1] {
3321                 Event::PaymentPathFailed { ref payment_hash, .. } => {
3322                         assert_eq!(*payment_hash, fourth_payment_hash);
3323                 },
3324                 _ => panic!("Unexpected event"),
3325         }
3326         match events[2] {
3327                 Event::PaymentFailed { ref payment_hash, .. } => {
3328                         assert_eq!(*payment_hash, fourth_payment_hash);
3329                 },
3330                 _ => panic!("Unexpected event"),
3331         }
3332
3333         nodes[1].node.process_pending_htlc_forwards();
3334         check_added_monitors!(nodes[1], 1);
3335
3336         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
3337         assert_eq!(events.len(), if deliver_bs_raa { 4 } else { 3 });
3338
3339         if deliver_bs_raa {
3340                 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3341                 match nodes_2_event {
3342                         MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
3343                                 assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
3344                                 assert_eq!(update_add_htlcs.len(), 1);
3345                                 assert!(update_fulfill_htlcs.is_empty());
3346                                 assert!(update_fail_htlcs.is_empty());
3347                                 assert!(update_fail_malformed_htlcs.is_empty());
3348                         },
3349                         _ => panic!("Unexpected event"),
3350                 }
3351         }
3352
3353         let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3354         match nodes_2_event {
3355                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id, ref data } }, node_id: _ } => {
3356                         assert_eq!(channel_id, chan_2.2);
3357                         assert_eq!(data.as_str(), "Channel closed because commitment or closing transaction was confirmed on chain.");
3358                 },
3359                 _ => panic!("Unexpected event"),
3360         }
3361
3362         let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
3363         match nodes_0_event {
3364                 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
3365                         assert!(update_add_htlcs.is_empty());
3366                         assert_eq!(update_fail_htlcs.len(), 3);
3367                         assert!(update_fulfill_htlcs.is_empty());
3368                         assert!(update_fail_malformed_htlcs.is_empty());
3369                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3370
3371                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3372                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]);
3373                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]);
3374
3375                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3376
3377                         let events = nodes[0].node.get_and_clear_pending_events();
3378                         assert_eq!(events.len(), 6);
3379                         match events[0] {
3380                                 Event::PaymentPathFailed { ref payment_hash, ref failure, .. } => {
3381                                         assert!(failed_htlcs.insert(payment_hash.0));
3382                                         // If we delivered B's RAA we got an unknown preimage error, not something
3383                                         // that we should update our routing table for.
3384                                         if !deliver_bs_raa {
3385                                                 if let PathFailure::OnPath { network_update: Some(_) } = failure { } else { panic!("Unexpected path failure") }
3386                                         }
3387                                 },
3388                                 _ => panic!("Unexpected event"),
3389                         }
3390                         match events[1] {
3391                                 Event::PaymentFailed { ref payment_hash, .. } => {
3392                                         assert_eq!(*payment_hash, first_payment_hash);
3393                                 },
3394                                 _ => panic!("Unexpected event"),
3395                         }
3396                         match events[2] {
3397                                 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3398                                         assert!(failed_htlcs.insert(payment_hash.0));
3399                                 },
3400                                 _ => panic!("Unexpected event"),
3401                         }
3402                         match events[3] {
3403                                 Event::PaymentFailed { ref payment_hash, .. } => {
3404                                         assert_eq!(*payment_hash, second_payment_hash);
3405                                 },
3406                                 _ => panic!("Unexpected event"),
3407                         }
3408                         match events[4] {
3409                                 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3410                                         assert!(failed_htlcs.insert(payment_hash.0));
3411                                 },
3412                                 _ => panic!("Unexpected event"),
3413                         }
3414                         match events[5] {
3415                                 Event::PaymentFailed { ref payment_hash, .. } => {
3416                                         assert_eq!(*payment_hash, third_payment_hash);
3417                                 },
3418                                 _ => panic!("Unexpected event"),
3419                         }
3420                 },
3421                 _ => panic!("Unexpected event"),
3422         }
3423
3424         // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
3425         match events[0] {
3426                 MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
3427                 _ => panic!("Unexpected event"),
3428         }
3429
3430         assert!(failed_htlcs.contains(&first_payment_hash.0));
3431         assert!(failed_htlcs.contains(&second_payment_hash.0));
3432         assert!(failed_htlcs.contains(&third_payment_hash.0));
3433 }
3434
3435 #[test]
3436 fn test_commitment_revoked_fail_backward_exhaustive_a() {
3437         do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
3438         do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
3439         do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
3440         do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
3441 }
3442
3443 #[test]
3444 fn test_commitment_revoked_fail_backward_exhaustive_b() {
3445         do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
3446         do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
3447         do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
3448         do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
3449 }
3450
3451 #[test]
3452 fn fail_backward_pending_htlc_upon_channel_failure() {
3453         let chanmon_cfgs = create_chanmon_cfgs(2);
3454         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3455         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3456         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3457         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
3458
3459         // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack.
3460         {
3461                 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3462                 nodes[0].node.send_payment_with_route(&route, payment_hash, RecipientOnionFields::secret_only(payment_secret),
3463                         PaymentId(payment_hash.0)).unwrap();
3464                 check_added_monitors!(nodes[0], 1);
3465
3466                 let payment_event = {
3467                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3468                         assert_eq!(events.len(), 1);
3469                         SendEvent::from_event(events.remove(0))
3470                 };
3471                 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
3472                 assert_eq!(payment_event.msgs.len(), 1);
3473         }
3474
3475         // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack.
3476         let (route, failed_payment_hash, _, failed_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3477         {
3478                 nodes[0].node.send_payment_with_route(&route, failed_payment_hash,
3479                         RecipientOnionFields::secret_only(failed_payment_secret), PaymentId(failed_payment_hash.0)).unwrap();
3480                 check_added_monitors!(nodes[0], 0);
3481
3482                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3483         }
3484
3485         // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel.
3486         {
3487                 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 50_000);
3488
3489                 let secp_ctx = Secp256k1::new();
3490                 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
3491                 let current_height = nodes[1].node.best_block.read().unwrap().height() + 1;
3492                 let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(
3493                         &route.paths[0], 50_000, RecipientOnionFields::secret_only(payment_secret), current_height, &None).unwrap();
3494                 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
3495                 let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
3496
3497                 // Send a 0-msat update_add_htlc to fail the channel.
3498                 let update_add_htlc = msgs::UpdateAddHTLC {
3499                         channel_id: chan.2,
3500                         htlc_id: 0,
3501                         amount_msat: 0,
3502                         payment_hash,
3503                         cltv_expiry,
3504                         onion_routing_packet,
3505                         skimmed_fee_msat: None,
3506                 };
3507                 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc);
3508         }
3509         let events = nodes[0].node.get_and_clear_pending_events();
3510         assert_eq!(events.len(), 3);
3511         // Check that Alice fails backward the pending HTLC from the second payment.
3512         match events[0] {
3513                 Event::PaymentPathFailed { payment_hash, .. } => {
3514                         assert_eq!(payment_hash, failed_payment_hash);
3515                 },
3516                 _ => panic!("Unexpected event"),
3517         }
3518         match events[1] {
3519                 Event::PaymentFailed { payment_hash, .. } => {
3520                         assert_eq!(payment_hash, failed_payment_hash);
3521                 },
3522                 _ => panic!("Unexpected event"),
3523         }
3524         match events[2] {
3525                 Event::ChannelClosed { reason: ClosureReason::ProcessingError { ref err }, .. } => {
3526                         assert_eq!(err, "Remote side tried to send a 0-msat HTLC");
3527                 },
3528                 _ => panic!("Unexpected event {:?}", events[1]),
3529         }
3530         check_closed_broadcast!(nodes[0], true);
3531         check_added_monitors!(nodes[0], 1);
3532 }
3533
3534 #[test]
3535 fn test_htlc_ignore_latest_remote_commitment() {
3536         // Test that HTLC transactions spending the latest remote commitment transaction are simply
3537         // ignored if we cannot claim them. This originally tickled an invalid unwrap().
3538         let chanmon_cfgs = create_chanmon_cfgs(2);
3539         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3540         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3541         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3542         if *nodes[1].connect_style.borrow() == ConnectStyle::FullBlockViaListen {
3543                 // We rely on the ability to connect a block redundantly, which isn't allowed via
3544                 // `chain::Listen`, so we never run the test if we randomly get assigned that
3545                 // connect_style.
3546                 return;
3547         }
3548         create_announced_chan_between_nodes(&nodes, 0, 1);
3549
3550         route_payment(&nodes[0], &[&nodes[1]], 10000000);
3551         nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3552         connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
3553         check_closed_broadcast!(nodes[0], true);
3554         check_added_monitors!(nodes[0], 1);
3555         check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3556
3557         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3558         assert_eq!(node_txn.len(), 3);
3559         assert_eq!(node_txn[0].txid(), node_txn[1].txid());
3560
3561         let block = create_dummy_block(nodes[1].best_block_hash(), 42, vec![node_txn[0].clone(), node_txn[1].clone()]);
3562         connect_block(&nodes[1], &block);
3563         check_closed_broadcast!(nodes[1], true);
3564         check_added_monitors!(nodes[1], 1);
3565         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
3566
3567         // Duplicate the connect_block call since this may happen due to other listeners
3568         // registering new transactions
3569         connect_block(&nodes[1], &block);
3570 }
3571
3572 #[test]
3573 fn test_force_close_fail_back() {
3574         // Check which HTLCs are failed-backwards on channel force-closure
3575         let chanmon_cfgs = create_chanmon_cfgs(3);
3576         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3577         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3578         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3579         create_announced_chan_between_nodes(&nodes, 0, 1);
3580         create_announced_chan_between_nodes(&nodes, 1, 2);
3581
3582         let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
3583
3584         let mut payment_event = {
3585                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
3586                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
3587                 check_added_monitors!(nodes[0], 1);
3588
3589                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3590                 assert_eq!(events.len(), 1);
3591                 SendEvent::from_event(events.remove(0))
3592         };
3593
3594         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3595         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3596
3597         expect_pending_htlcs_forwardable!(nodes[1]);
3598
3599         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3600         assert_eq!(events_2.len(), 1);
3601         payment_event = SendEvent::from_event(events_2.remove(0));
3602         assert_eq!(payment_event.msgs.len(), 1);
3603
3604         check_added_monitors!(nodes[1], 1);
3605         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
3606         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
3607         check_added_monitors!(nodes[2], 1);
3608         let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3609
3610         // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
3611         // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
3612         // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
3613
3614         nodes[2].node.force_close_broadcasting_latest_txn(&payment_event.commitment_msg.channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3615         check_closed_broadcast!(nodes[2], true);
3616         check_added_monitors!(nodes[2], 1);
3617         check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3618         let tx = {
3619                 let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3620                 // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
3621                 // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
3622                 // back to nodes[1] upon timeout otherwise.
3623                 assert_eq!(node_txn.len(), 1);
3624                 node_txn.remove(0)
3625         };
3626
3627         mine_transaction(&nodes[1], &tx);
3628
3629         // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
3630         check_closed_broadcast!(nodes[1], true);
3631         check_added_monitors!(nodes[1], 1);
3632         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3633
3634         // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
3635         {
3636                 get_monitor!(nodes[2], payment_event.commitment_msg.channel_id)
3637                         .provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[2].fee_estimator), &node_cfgs[2].logger);
3638         }
3639         mine_transaction(&nodes[2], &tx);
3640         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3641         assert_eq!(node_txn.len(), 1);
3642         assert_eq!(node_txn[0].input.len(), 1);
3643         assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
3644         assert_eq!(node_txn[0].lock_time.0, 0); // Must be an HTLC-Success
3645         assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
3646
3647         check_spends!(node_txn[0], tx);
3648 }
3649
3650 #[test]
3651 fn test_dup_events_on_peer_disconnect() {
3652         // Test that if we receive a duplicative update_fulfill_htlc message after a reconnect we do
3653         // not generate a corresponding duplicative PaymentSent event. This did not use to be the case
3654         // as we used to generate the event immediately upon receipt of the payment preimage in the
3655         // update_fulfill_htlc message.
3656
3657         let chanmon_cfgs = create_chanmon_cfgs(2);
3658         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3659         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3660         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3661         create_announced_chan_between_nodes(&nodes, 0, 1);
3662
3663         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
3664
3665         nodes[1].node.claim_funds(payment_preimage);
3666         expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
3667         check_added_monitors!(nodes[1], 1);
3668         let claim_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3669         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &claim_msgs.update_fulfill_htlcs[0]);
3670         expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
3671
3672         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3673         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3674
3675         let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3676         reconnect_args.pending_htlc_claims.0 = 1;
3677         reconnect_nodes(reconnect_args);
3678         expect_payment_path_successful!(nodes[0]);
3679 }
3680
3681 #[test]
3682 fn test_peer_disconnected_before_funding_broadcasted() {
3683         // Test that channels are closed with `ClosureReason::DisconnectedPeer` if the peer disconnects
3684         // before the funding transaction has been broadcasted.
3685         let chanmon_cfgs = create_chanmon_cfgs(2);
3686         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3687         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3688         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3689
3690         // Open a channel between `nodes[0]` and `nodes[1]`, for which the funding transaction is never
3691         // broadcasted, even though it's created by `nodes[0]`.
3692         let expected_temporary_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
3693         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
3694         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
3695         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
3696         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
3697
3698         let (temporary_channel_id, tx, _funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
3699         assert_eq!(temporary_channel_id, expected_temporary_channel_id);
3700
3701         assert!(nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
3702
3703         let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
3704         assert_eq!(funding_created_msg.temporary_channel_id, expected_temporary_channel_id);
3705
3706         // Even though the funding transaction is created by `nodes[0]`, the `FundingCreated` msg is
3707         // never sent to `nodes[1]`, and therefore the tx is never signed by either party nor
3708         // broadcasted.
3709         {
3710                 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
3711         }
3712
3713         // Ensure that the channel is closed with `ClosureReason::DisconnectedPeer` when the peers are
3714         // disconnected before the funding transaction was broadcasted.
3715         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3716         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3717
3718         check_closed_event!(&nodes[0], 1, ClosureReason::DisconnectedPeer, false
3719                 , [nodes[1].node.get_our_node_id()], 1000000);
3720         check_closed_event!(&nodes[1], 1, ClosureReason::DisconnectedPeer, false
3721                 , [nodes[0].node.get_our_node_id()], 1000000);
3722 }
3723
3724 #[test]
3725 fn test_simple_peer_disconnect() {
3726         // Test that we can reconnect when there are no lost messages
3727         let chanmon_cfgs = create_chanmon_cfgs(3);
3728         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3729         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3730         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3731         create_announced_chan_between_nodes(&nodes, 0, 1);
3732         create_announced_chan_between_nodes(&nodes, 1, 2);
3733
3734         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3735         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3736         let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3737         reconnect_args.send_channel_ready = (true, true);
3738         reconnect_nodes(reconnect_args);
3739
3740         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3741         let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3742         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
3743         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
3744
3745         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3746         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3747         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3748
3749         let (payment_preimage_3, payment_hash_3, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000);
3750         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3751         let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3752         let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3753
3754         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3755         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3756
3757         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_preimage_3);
3758         fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_hash_5);
3759
3760         let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3761         reconnect_args.pending_cell_htlc_fails.0 = 1;
3762         reconnect_args.pending_cell_htlc_claims.0 = 1;
3763         reconnect_nodes(reconnect_args);
3764         {
3765                 let events = nodes[0].node.get_and_clear_pending_events();
3766                 assert_eq!(events.len(), 4);
3767                 match events[0] {
3768                         Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3769                                 assert_eq!(payment_preimage, payment_preimage_3);
3770                                 assert_eq!(payment_hash, payment_hash_3);
3771                         },
3772                         _ => panic!("Unexpected event"),
3773                 }
3774                 match events[1] {
3775                         Event::PaymentPathSuccessful { .. } => {},
3776                         _ => panic!("Unexpected event"),
3777                 }
3778                 match events[2] {
3779                         Event::PaymentPathFailed { payment_hash, payment_failed_permanently, .. } => {
3780                                 assert_eq!(payment_hash, payment_hash_5);
3781                                 assert!(payment_failed_permanently);
3782                         },
3783                         _ => panic!("Unexpected event"),
3784                 }
3785                 match events[3] {
3786                         Event::PaymentFailed { payment_hash, .. } => {
3787                                 assert_eq!(payment_hash, payment_hash_5);
3788                         },
3789                         _ => panic!("Unexpected event"),
3790                 }
3791         }
3792         check_added_monitors(&nodes[0], 1);
3793
3794         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
3795         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
3796 }
3797
3798 fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken_lnd: bool) {
3799         // Test that we can reconnect when in-flight HTLC updates get dropped
3800         let chanmon_cfgs = create_chanmon_cfgs(2);
3801         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3802         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3803         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3804
3805         let mut as_channel_ready = None;
3806         let channel_id = if messages_delivered == 0 {
3807                 let (channel_ready, chan_id, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001);
3808                 as_channel_ready = Some(channel_ready);
3809                 // nodes[1] doesn't receive the channel_ready message (it'll be re-sent on reconnect)
3810                 // Note that we store it so that if we're running with `simulate_broken_lnd` we can deliver
3811                 // it before the channel_reestablish message.
3812                 chan_id
3813         } else {
3814                 create_announced_chan_between_nodes(&nodes, 0, 1).2
3815         };
3816
3817         let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
3818
3819         let payment_event = {
3820                 nodes[0].node.send_payment_with_route(&route, payment_hash_1,
3821                         RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
3822                 check_added_monitors!(nodes[0], 1);
3823
3824                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3825                 assert_eq!(events.len(), 1);
3826                 SendEvent::from_event(events.remove(0))
3827         };
3828         assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
3829
3830         if messages_delivered < 2 {
3831                 // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
3832         } else {
3833                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3834                 if messages_delivered >= 3 {
3835                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
3836                         check_added_monitors!(nodes[1], 1);
3837                         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3838
3839                         if messages_delivered >= 4 {
3840                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3841                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3842                                 check_added_monitors!(nodes[0], 1);
3843
3844                                 if messages_delivered >= 5 {
3845                                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
3846                                         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3847                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
3848                                         check_added_monitors!(nodes[0], 1);
3849
3850                                         if messages_delivered >= 6 {
3851                                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3852                                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3853                                                 check_added_monitors!(nodes[1], 1);
3854                                         }
3855                                 }
3856                         }
3857                 }
3858         }
3859
3860         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3861         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3862         if messages_delivered < 3 {
3863                 if simulate_broken_lnd {
3864                         // lnd has a long-standing bug where they send a channel_ready prior to a
3865                         // channel_reestablish if you reconnect prior to channel_ready time.
3866                         //
3867                         // Here we simulate that behavior, delivering a channel_ready immediately on
3868                         // reconnect. Note that we don't bother skipping the now-duplicate channel_ready sent
3869                         // in `reconnect_nodes` but we currently don't fail based on that.
3870                         //
3871                         // See-also <https://github.com/lightningnetwork/lnd/issues/4006>
3872                         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready.as_ref().unwrap().0);
3873                 }
3874                 // Even if the channel_ready messages get exchanged, as long as nothing further was
3875                 // received on either side, both sides will need to resend them.
3876                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3877                 reconnect_args.send_channel_ready = (true, true);
3878                 reconnect_args.pending_htlc_adds.1 = 1;
3879                 reconnect_nodes(reconnect_args);
3880         } else if messages_delivered == 3 {
3881                 // nodes[0] still wants its RAA + commitment_signed
3882                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3883                 reconnect_args.pending_responding_commitment_signed.0 = true;
3884                 reconnect_args.pending_raa.0 = true;
3885                 reconnect_nodes(reconnect_args);
3886         } else if messages_delivered == 4 {
3887                 // nodes[0] still wants its commitment_signed
3888                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3889                 reconnect_args.pending_responding_commitment_signed.0 = true;
3890                 reconnect_nodes(reconnect_args);
3891         } else if messages_delivered == 5 {
3892                 // nodes[1] still wants its final RAA
3893                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3894                 reconnect_args.pending_raa.1 = true;
3895                 reconnect_nodes(reconnect_args);
3896         } else if messages_delivered == 6 {
3897                 // Everything was delivered...
3898                 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3899         }
3900
3901         let events_1 = nodes[1].node.get_and_clear_pending_events();
3902         if messages_delivered == 0 {
3903                 assert_eq!(events_1.len(), 2);
3904                 match events_1[0] {
3905                         Event::ChannelReady { .. } => { },
3906                         _ => panic!("Unexpected event"),
3907                 };
3908                 match events_1[1] {
3909                         Event::PendingHTLCsForwardable { .. } => { },
3910                         _ => panic!("Unexpected event"),
3911                 };
3912         } else {
3913                 assert_eq!(events_1.len(), 1);
3914                 match events_1[0] {
3915                         Event::PendingHTLCsForwardable { .. } => { },
3916                         _ => panic!("Unexpected event"),
3917                 };
3918         }
3919
3920         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3921         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3922         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3923
3924         nodes[1].node.process_pending_htlc_forwards();
3925
3926         let events_2 = nodes[1].node.get_and_clear_pending_events();
3927         assert_eq!(events_2.len(), 1);
3928         match events_2[0] {
3929                 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
3930                         assert_eq!(payment_hash_1, *payment_hash);
3931                         assert_eq!(amount_msat, 1_000_000);
3932                         assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
3933                         assert_eq!(via_channel_id, Some(channel_id));
3934                         match &purpose {
3935                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
3936                                         assert!(payment_preimage.is_none());
3937                                         assert_eq!(payment_secret_1, *payment_secret);
3938                                 },
3939                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
3940                         }
3941                 },
3942                 _ => panic!("Unexpected event"),
3943         }
3944
3945         nodes[1].node.claim_funds(payment_preimage_1);
3946         check_added_monitors!(nodes[1], 1);
3947         expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
3948
3949         let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
3950         assert_eq!(events_3.len(), 1);
3951         let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
3952                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
3953                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3954                         assert!(updates.update_add_htlcs.is_empty());
3955                         assert!(updates.update_fail_htlcs.is_empty());
3956                         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3957                         assert!(updates.update_fail_malformed_htlcs.is_empty());
3958                         assert!(updates.update_fee.is_none());
3959                         (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
3960                 },
3961                 _ => panic!("Unexpected event"),
3962         };
3963
3964         if messages_delivered >= 1 {
3965                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc);
3966
3967                 let events_4 = nodes[0].node.get_and_clear_pending_events();
3968                 assert_eq!(events_4.len(), 1);
3969                 match events_4[0] {
3970                         Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
3971                                 assert_eq!(payment_preimage_1, *payment_preimage);
3972                                 assert_eq!(payment_hash_1, *payment_hash);
3973                         },
3974                         _ => panic!("Unexpected event"),
3975                 }
3976
3977                 if messages_delivered >= 2 {
3978                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
3979                         check_added_monitors!(nodes[0], 1);
3980                         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3981
3982                         if messages_delivered >= 3 {
3983                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3984                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3985                                 check_added_monitors!(nodes[1], 1);
3986
3987                                 if messages_delivered >= 4 {
3988                                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed);
3989                                         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3990                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
3991                                         check_added_monitors!(nodes[1], 1);
3992
3993                                         if messages_delivered >= 5 {
3994                                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3995                                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3996                                                 check_added_monitors!(nodes[0], 1);
3997                                         }
3998                                 }
3999                         }
4000                 }
4001         }
4002
4003         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4004         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4005         if messages_delivered < 2 {
4006                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4007                 reconnect_args.pending_htlc_claims.0 = 1;
4008                 reconnect_nodes(reconnect_args);
4009                 if messages_delivered < 1 {
4010                         expect_payment_sent!(nodes[0], payment_preimage_1);
4011                 } else {
4012                         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4013                 }
4014         } else if messages_delivered == 2 {
4015                 // nodes[0] still wants its RAA + commitment_signed
4016                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4017                 reconnect_args.pending_responding_commitment_signed.1 = true;
4018                 reconnect_args.pending_raa.1 = true;
4019                 reconnect_nodes(reconnect_args);
4020         } else if messages_delivered == 3 {
4021                 // nodes[0] still wants its commitment_signed
4022                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4023                 reconnect_args.pending_responding_commitment_signed.1 = true;
4024                 reconnect_nodes(reconnect_args);
4025         } else if messages_delivered == 4 {
4026                 // nodes[1] still wants its final RAA
4027                 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4028                 reconnect_args.pending_raa.0 = true;
4029                 reconnect_nodes(reconnect_args);
4030         } else if messages_delivered == 5 {
4031                 // Everything was delivered...
4032                 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4033         }
4034
4035         if messages_delivered == 1 || messages_delivered == 2 {
4036                 expect_payment_path_successful!(nodes[0]);
4037         }
4038         if messages_delivered <= 5 {
4039                 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4040                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4041         }
4042         reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4043
4044         if messages_delivered > 2 {
4045                 expect_payment_path_successful!(nodes[0]);
4046         }
4047
4048         // Channel should still work fine...
4049         let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4050         let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
4051         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4052 }
4053
4054 #[test]
4055 fn test_drop_messages_peer_disconnect_a() {
4056         do_test_drop_messages_peer_disconnect(0, true);
4057         do_test_drop_messages_peer_disconnect(0, false);
4058         do_test_drop_messages_peer_disconnect(1, false);
4059         do_test_drop_messages_peer_disconnect(2, false);
4060 }
4061
4062 #[test]
4063 fn test_drop_messages_peer_disconnect_b() {
4064         do_test_drop_messages_peer_disconnect(3, false);
4065         do_test_drop_messages_peer_disconnect(4, false);
4066         do_test_drop_messages_peer_disconnect(5, false);
4067         do_test_drop_messages_peer_disconnect(6, false);
4068 }
4069
4070 #[test]
4071 fn test_channel_ready_without_best_block_updated() {
4072         // Previously, if we were offline when a funding transaction was locked in, and then we came
4073         // back online, calling best_block_updated once followed by transactions_confirmed, we'd not
4074         // generate a channel_ready until a later best_block_updated. This tests that we generate the
4075         // channel_ready immediately instead.
4076         let chanmon_cfgs = create_chanmon_cfgs(2);
4077         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4078         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4079         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4080         *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
4081
4082         let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4083
4084         let conf_height = nodes[0].best_block_info().1 + 1;
4085         connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4086         let block_txn = [funding_tx];
4087         let conf_txn: Vec<_> = block_txn.iter().enumerate().collect();
4088         let conf_block_header = nodes[0].get_block_header(conf_height);
4089         nodes[0].node.transactions_confirmed(&conf_block_header, &conf_txn[..], conf_height);
4090
4091         // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4092         let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4093         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4094 }
4095
4096 #[test]
4097 fn test_channel_monitor_skipping_block_when_channel_manager_is_leading() {
4098         let chanmon_cfgs = create_chanmon_cfgs(2);
4099         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4100         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4101         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4102
4103         // Let channel_manager get ahead of chain_monitor by 1 block.
4104         // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4105         // in case where client calls block_connect on channel_manager first and then on chain_monitor.
4106         let height_1 = nodes[0].best_block_info().1 + 1;
4107         let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4108
4109         nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4110         nodes[0].node.block_connected(&block_1, height_1);
4111
4112         // Create channel, and it gets added to chain_monitor in funding_created.
4113         let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4114
4115         // Now, newly added channel_monitor in chain_monitor hasn't processed block_1,
4116         // but it's best_block is block_1, since that was populated by channel_manager, and channel_manager
4117         // was running ahead of chain_monitor at the time of funding_created.
4118         // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4119         // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4120         confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4121         connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4122
4123         // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4124         let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4125         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4126 }
4127
4128 #[test]
4129 fn test_channel_monitor_skipping_block_when_channel_manager_is_lagging() {
4130         let chanmon_cfgs = create_chanmon_cfgs(2);
4131         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4132         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4133         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4134
4135         // Let chain_monitor get ahead of channel_manager by 1 block.
4136         // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4137         // in case where client calls block_connect on chain_monitor first and then on channel_manager.
4138         let height_1 = nodes[0].best_block_info().1 + 1;
4139         let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4140
4141         nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4142         nodes[0].chain_monitor.chain_monitor.block_connected(&block_1, height_1);
4143
4144         // Create channel, and it gets added to chain_monitor in funding_created.
4145         let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4146
4147         // channel_manager can't really skip block_1, it should get it eventually.
4148         nodes[0].node.block_connected(&block_1, height_1);
4149
4150         // Now, newly added channel_monitor in chain_monitor hasn't processed block_1, it's best_block is
4151         // the block before block_1, since that was populated by channel_manager, and channel_manager was
4152         // running behind at the time of funding_created.
4153         // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4154         // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4155         confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4156         connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4157
4158         // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4159         let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4160         nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4161 }
4162
4163 #[test]
4164 fn test_drop_messages_peer_disconnect_dual_htlc() {
4165         // Test that we can handle reconnecting when both sides of a channel have pending
4166         // commitment_updates when we disconnect.
4167         let chanmon_cfgs = create_chanmon_cfgs(2);
4168         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4169         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4170         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4171         create_announced_chan_between_nodes(&nodes, 0, 1);
4172
4173         let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
4174
4175         // Now try to send a second payment which will fail to send
4176         let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4177         nodes[0].node.send_payment_with_route(&route, payment_hash_2,
4178                 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
4179         check_added_monitors!(nodes[0], 1);
4180
4181         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
4182         assert_eq!(events_1.len(), 1);
4183         match events_1[0] {
4184                 MessageSendEvent::UpdateHTLCs { .. } => {},
4185                 _ => panic!("Unexpected event"),
4186         }
4187
4188         nodes[1].node.claim_funds(payment_preimage_1);
4189         expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
4190         check_added_monitors!(nodes[1], 1);
4191
4192         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
4193         assert_eq!(events_2.len(), 1);
4194         match events_2[0] {
4195                 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 } } => {
4196                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4197                         assert!(update_add_htlcs.is_empty());
4198                         assert_eq!(update_fulfill_htlcs.len(), 1);
4199                         assert!(update_fail_htlcs.is_empty());
4200                         assert!(update_fail_malformed_htlcs.is_empty());
4201                         assert!(update_fee.is_none());
4202
4203                         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
4204                         let events_3 = nodes[0].node.get_and_clear_pending_events();
4205                         assert_eq!(events_3.len(), 1);
4206                         match events_3[0] {
4207                                 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
4208                                         assert_eq!(*payment_preimage, payment_preimage_1);
4209                                         assert_eq!(*payment_hash, payment_hash_1);
4210                                 },
4211                                 _ => panic!("Unexpected event"),
4212                         }
4213
4214                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
4215                         let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4216                         // No commitment_signed so get_event_msg's assert(len == 1) passes
4217                         check_added_monitors!(nodes[0], 1);
4218                 },
4219                 _ => panic!("Unexpected event"),
4220         }
4221
4222         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4223         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4224
4225         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
4226                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
4227         }, true).unwrap();
4228         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4229         assert_eq!(reestablish_1.len(), 1);
4230         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
4231                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
4232         }, false).unwrap();
4233         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4234         assert_eq!(reestablish_2.len(), 1);
4235
4236         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4237         let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
4238         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4239         let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
4240
4241         assert!(as_resp.0.is_none());
4242         assert!(bs_resp.0.is_none());
4243
4244         assert!(bs_resp.1.is_none());
4245         assert!(bs_resp.2.is_none());
4246
4247         assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
4248
4249         assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
4250         assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
4251         assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
4252         assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
4253         assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
4254         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]);
4255         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed);
4256         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4257         // No commitment_signed so get_event_msg's assert(len == 1) passes
4258         check_added_monitors!(nodes[1], 1);
4259
4260         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap());
4261         let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4262         assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
4263         assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
4264         assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
4265         assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
4266         assert!(bs_second_commitment_signed.update_fee.is_none());
4267         check_added_monitors!(nodes[1], 1);
4268
4269         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
4270         let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4271         assert!(as_commitment_signed.update_add_htlcs.is_empty());
4272         assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
4273         assert!(as_commitment_signed.update_fail_htlcs.is_empty());
4274         assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
4275         assert!(as_commitment_signed.update_fee.is_none());
4276         check_added_monitors!(nodes[0], 1);
4277
4278         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed);
4279         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4280         // No commitment_signed so get_event_msg's assert(len == 1) passes
4281         check_added_monitors!(nodes[0], 1);
4282
4283         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed);
4284         let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4285         // No commitment_signed so get_event_msg's assert(len == 1) passes
4286         check_added_monitors!(nodes[1], 1);
4287
4288         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
4289         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4290         check_added_monitors!(nodes[1], 1);
4291
4292         expect_pending_htlcs_forwardable!(nodes[1]);
4293
4294         let events_5 = nodes[1].node.get_and_clear_pending_events();
4295         assert_eq!(events_5.len(), 1);
4296         match events_5[0] {
4297                 Event::PaymentClaimable { ref payment_hash, ref purpose, .. } => {
4298                         assert_eq!(payment_hash_2, *payment_hash);
4299                         match &purpose {
4300                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
4301                                         assert!(payment_preimage.is_none());
4302                                         assert_eq!(payment_secret_2, *payment_secret);
4303                                 },
4304                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
4305                         }
4306                 },
4307                 _ => panic!("Unexpected event"),
4308         }
4309
4310         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack);
4311         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4312         check_added_monitors!(nodes[0], 1);
4313
4314         expect_payment_path_successful!(nodes[0]);
4315         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4316 }
4317
4318 fn do_test_htlc_timeout(send_partial_mpp: bool) {
4319         // If the user fails to claim/fail an HTLC within the HTLC CLTV timeout we fail it for them
4320         // to avoid our counterparty failing the channel.
4321         let chanmon_cfgs = create_chanmon_cfgs(2);
4322         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4323         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4324         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4325
4326         create_announced_chan_between_nodes(&nodes, 0, 1);
4327
4328         let our_payment_hash = if send_partial_mpp {
4329                 let (route, our_payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
4330                 // Use the utility function send_payment_along_path to send the payment with MPP data which
4331                 // indicates there are more HTLCs coming.
4332                 let cur_height = CHAN_CONFIRM_DEPTH + 1; // route_payment calls send_payment, which adds 1 to the current height. So we do the same here to match.
4333                 let payment_id = PaymentId([42; 32]);
4334                 let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
4335                         RecipientOnionFields::secret_only(payment_secret), payment_id, &route).unwrap();
4336                 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
4337                         RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id,
4338                         &None, session_privs[0]).unwrap();
4339                 check_added_monitors!(nodes[0], 1);
4340                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4341                 assert_eq!(events.len(), 1);
4342                 // Now do the relevant commitment_signed/RAA dances along the path, noting that the final
4343                 // hop should *not* yet generate any PaymentClaimable event(s).
4344                 pass_along_path(&nodes[0], &[&nodes[1]], 100000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
4345                 our_payment_hash
4346         } else {
4347                 route_payment(&nodes[0], &[&nodes[1]], 100000).1
4348         };
4349
4350         let mut block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
4351         connect_block(&nodes[0], &block);
4352         connect_block(&nodes[1], &block);
4353         let block_count = TEST_FINAL_CLTV + CHAN_CONFIRM_DEPTH + 2 - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS;
4354         for _ in CHAN_CONFIRM_DEPTH + 2..block_count {
4355                 block.header.prev_blockhash = block.block_hash();
4356                 connect_block(&nodes[0], &block);
4357                 connect_block(&nodes[1], &block);
4358         }
4359
4360         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
4361
4362         check_added_monitors!(nodes[1], 1);
4363         let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4364         assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
4365         assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
4366         assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
4367         assert!(htlc_timeout_updates.update_fee.is_none());
4368
4369         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
4370         commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
4371         // 100_000 msat as u64, followed by the height at which we failed back above
4372         let mut expected_failure_data = (100_000 as u64).to_be_bytes().to_vec();
4373         expected_failure_data.extend_from_slice(&(block_count - 1).to_be_bytes());
4374         expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000 | 15, &expected_failure_data[..]);
4375 }
4376
4377 #[test]
4378 fn test_htlc_timeout() {
4379         do_test_htlc_timeout(true);
4380         do_test_htlc_timeout(false);
4381 }
4382
4383 fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
4384         // Tests that HTLCs in the holding cell are timed out after the requisite number of blocks.
4385         let chanmon_cfgs = create_chanmon_cfgs(3);
4386         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4387         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4388         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4389         create_announced_chan_between_nodes(&nodes, 0, 1);
4390         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4391
4392         // Make sure all nodes are at the same starting height
4393         connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
4394         connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
4395         connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
4396
4397         // Route a first payment to get the 1 -> 2 channel in awaiting_raa...
4398         let (route, first_payment_hash, _, first_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
4399         nodes[1].node.send_payment_with_route(&route, first_payment_hash,
4400                 RecipientOnionFields::secret_only(first_payment_secret), PaymentId(first_payment_hash.0)).unwrap();
4401         assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
4402         check_added_monitors!(nodes[1], 1);
4403
4404         // Now attempt to route a second payment, which should be placed in the holding cell
4405         let sending_node = if forwarded_htlc { &nodes[0] } else { &nodes[1] };
4406         let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(sending_node, nodes[2], 100000);
4407         sending_node.node.send_payment_with_route(&route, second_payment_hash,
4408                 RecipientOnionFields::secret_only(second_payment_secret), PaymentId(second_payment_hash.0)).unwrap();
4409         if forwarded_htlc {
4410                 check_added_monitors!(nodes[0], 1);
4411                 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
4412                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
4413                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4414                 expect_pending_htlcs_forwardable!(nodes[1]);
4415         }
4416         check_added_monitors!(nodes[1], 0);
4417
4418         connect_blocks(&nodes[1], TEST_FINAL_CLTV - LATENCY_GRACE_PERIOD_BLOCKS);
4419         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4420         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
4421         connect_blocks(&nodes[1], 1);
4422
4423         if forwarded_htlc {
4424                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
4425                 check_added_monitors!(nodes[1], 1);
4426                 let fail_commit = nodes[1].node.get_and_clear_pending_msg_events();
4427                 assert_eq!(fail_commit.len(), 1);
4428                 match fail_commit[0] {
4429                         MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, ref commitment_signed, .. }, .. } => {
4430                                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
4431                                 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, true, true);
4432                         },
4433                         _ => unreachable!(),
4434                 }
4435                 expect_payment_failed_with_update!(nodes[0], second_payment_hash, false, chan_2.0.contents.short_channel_id, false);
4436         } else {
4437                 expect_payment_failed!(nodes[1], second_payment_hash, false);
4438         }
4439 }
4440
4441 #[test]
4442 fn test_holding_cell_htlc_add_timeouts() {
4443         do_test_holding_cell_htlc_add_timeouts(false);
4444         do_test_holding_cell_htlc_add_timeouts(true);
4445 }
4446
4447 macro_rules! check_spendable_outputs {
4448         ($node: expr, $keysinterface: expr) => {
4449                 {
4450                         let mut events = $node.chain_monitor.chain_monitor.get_and_clear_pending_events();
4451                         let mut txn = Vec::new();
4452                         let mut all_outputs = Vec::new();
4453                         let secp_ctx = Secp256k1::new();
4454                         for event in events.drain(..) {
4455                                 match event {
4456                                         Event::SpendableOutputs { mut outputs, channel_id: _ } => {
4457                                                 for outp in outputs.drain(..) {
4458                                                         txn.push($keysinterface.backing.spend_spendable_outputs(&[&outp], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &secp_ctx).unwrap());
4459                                                         all_outputs.push(outp);
4460                                                 }
4461                                         },
4462                                         _ => panic!("Unexpected event"),
4463                                 };
4464                         }
4465                         if all_outputs.len() > 1 {
4466                                 if let Ok(tx) = $keysinterface.backing.spend_spendable_outputs(&all_outputs.iter().map(|a| a).collect::<Vec<_>>(), Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &secp_ctx) {
4467                                         txn.push(tx);
4468                                 }
4469                         }
4470                         txn
4471                 }
4472         }
4473 }
4474
4475 #[test]
4476 fn test_claim_sizeable_push_msat() {
4477         // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
4478         let chanmon_cfgs = create_chanmon_cfgs(2);
4479         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4480         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4481         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4482
4483         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4484         nodes[1].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[0].node.get_our_node_id()).unwrap();
4485         check_closed_broadcast!(nodes[1], true);
4486         check_added_monitors!(nodes[1], 1);
4487         check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
4488         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4489         assert_eq!(node_txn.len(), 1);
4490         check_spends!(node_txn[0], chan.3);
4491         assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
4492
4493         mine_transaction(&nodes[1], &node_txn[0]);
4494         connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
4495
4496         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4497         assert_eq!(spend_txn.len(), 1);
4498         assert_eq!(spend_txn[0].input.len(), 1);
4499         check_spends!(spend_txn[0], node_txn[0]);
4500         assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
4501 }
4502
4503 #[test]
4504 fn test_claim_on_remote_sizeable_push_msat() {
4505         // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4506         // to_remote output is encumbered by a P2WPKH
4507         let chanmon_cfgs = create_chanmon_cfgs(2);
4508         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4509         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4510         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4511
4512         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4513         nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
4514         check_closed_broadcast!(nodes[0], true);
4515         check_added_monitors!(nodes[0], 1);
4516         check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
4517
4518         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4519         assert_eq!(node_txn.len(), 1);
4520         check_spends!(node_txn[0], chan.3);
4521         assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
4522
4523         mine_transaction(&nodes[1], &node_txn[0]);
4524         check_closed_broadcast!(nodes[1], true);
4525         check_added_monitors!(nodes[1], 1);
4526         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4527         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4528
4529         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4530         assert_eq!(spend_txn.len(), 1);
4531         check_spends!(spend_txn[0], node_txn[0]);
4532 }
4533
4534 #[test]
4535 fn test_claim_on_remote_revoked_sizeable_push_msat() {
4536         // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4537         // to_remote output is encumbered by a P2WPKH
4538
4539         let chanmon_cfgs = create_chanmon_cfgs(2);
4540         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4541         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4542         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4543
4544         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000);
4545         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4546         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
4547         assert_eq!(revoked_local_txn[0].input.len(), 1);
4548         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
4549
4550         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4551         mine_transaction(&nodes[1], &revoked_local_txn[0]);
4552         check_closed_broadcast!(nodes[1], true);
4553         check_added_monitors!(nodes[1], 1);
4554         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4555
4556         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4557         mine_transaction(&nodes[1], &node_txn[0]);
4558         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4559
4560         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4561         assert_eq!(spend_txn.len(), 3);
4562         check_spends!(spend_txn[0], revoked_local_txn[0]); // to_remote output on revoked remote commitment_tx
4563         check_spends!(spend_txn[1], node_txn[0]);
4564         check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[0]); // Both outputs
4565 }
4566
4567 #[test]
4568 fn test_static_spendable_outputs_preimage_tx() {
4569         let chanmon_cfgs = create_chanmon_cfgs(2);
4570         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4571         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4572         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4573
4574         // Create some initial channels
4575         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4576
4577         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
4578
4579         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4580         assert_eq!(commitment_tx[0].input.len(), 1);
4581         assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4582
4583         // Settle A's commitment tx on B's chain
4584         nodes[1].node.claim_funds(payment_preimage);
4585         expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
4586         check_added_monitors!(nodes[1], 1);
4587         mine_transaction(&nodes[1], &commitment_tx[0]);
4588         check_added_monitors!(nodes[1], 1);
4589         let events = nodes[1].node.get_and_clear_pending_msg_events();
4590         match events[0] {
4591                 MessageSendEvent::UpdateHTLCs { .. } => {},
4592                 _ => panic!("Unexpected event"),
4593         }
4594         match events[1] {
4595                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4596                 _ => panic!("Unexepected event"),
4597         }
4598
4599         // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
4600         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: preimage tx
4601         assert_eq!(node_txn.len(), 1);
4602         check_spends!(node_txn[0], commitment_tx[0]);
4603         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4604
4605         mine_transaction(&nodes[1], &node_txn[0]);
4606         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4607         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4608
4609         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4610         assert_eq!(spend_txn.len(), 1);
4611         check_spends!(spend_txn[0], node_txn[0]);
4612 }
4613
4614 #[test]
4615 fn test_static_spendable_outputs_timeout_tx() {
4616         let chanmon_cfgs = create_chanmon_cfgs(2);
4617         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4618         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4619         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4620
4621         // Create some initial channels
4622         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4623
4624         // Rebalance the network a bit by relaying one payment through all the channels ...
4625         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
4626
4627         let (_, our_payment_hash, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000);
4628
4629         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4630         assert_eq!(commitment_tx[0].input.len(), 1);
4631         assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4632
4633         // Settle A's commitment tx on B' chain
4634         mine_transaction(&nodes[1], &commitment_tx[0]);
4635         check_added_monitors!(nodes[1], 1);
4636         let events = nodes[1].node.get_and_clear_pending_msg_events();
4637         match events[0] {
4638                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4639                 _ => panic!("Unexpected event"),
4640         }
4641         connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4642
4643         // Check B's monitor was able to send back output descriptor event for timeout tx on A's commitment tx
4644         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4645         assert_eq!(node_txn.len(), 1); // ChannelMonitor: timeout tx
4646         check_spends!(node_txn[0],  commitment_tx[0].clone());
4647         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4648
4649         mine_transaction(&nodes[1], &node_txn[0]);
4650         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4651         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4652         expect_payment_failed!(nodes[1], our_payment_hash, false);
4653
4654         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4655         assert_eq!(spend_txn.len(), 3); // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
4656         check_spends!(spend_txn[0], commitment_tx[0]);
4657         check_spends!(spend_txn[1], node_txn[0]);
4658         check_spends!(spend_txn[2], node_txn[0], commitment_tx[0]); // All outputs
4659 }
4660
4661 #[test]
4662 fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4663         let chanmon_cfgs = create_chanmon_cfgs(2);
4664         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4665         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4666         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4667
4668         // Create some initial channels
4669         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4670
4671         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4672         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4673         assert_eq!(revoked_local_txn[0].input.len(), 1);
4674         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4675
4676         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4677
4678         mine_transaction(&nodes[1], &revoked_local_txn[0]);
4679         check_closed_broadcast!(nodes[1], true);
4680         check_added_monitors!(nodes[1], 1);
4681         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4682
4683         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4684         assert_eq!(node_txn.len(), 1);
4685         assert_eq!(node_txn[0].input.len(), 2);
4686         check_spends!(node_txn[0], revoked_local_txn[0]);
4687
4688         mine_transaction(&nodes[1], &node_txn[0]);
4689         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4690
4691         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4692         assert_eq!(spend_txn.len(), 1);
4693         check_spends!(spend_txn[0], node_txn[0]);
4694 }
4695
4696 #[test]
4697 fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
4698         let mut chanmon_cfgs = create_chanmon_cfgs(2);
4699         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
4700         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4701         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4702         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4703
4704         // Create some initial channels
4705         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4706
4707         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4708         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4709         assert_eq!(revoked_local_txn[0].input.len(), 1);
4710         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4711
4712         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4713
4714         // A will generate HTLC-Timeout from revoked commitment tx
4715         mine_transaction(&nodes[0], &revoked_local_txn[0]);
4716         check_closed_broadcast!(nodes[0], true);
4717         check_added_monitors!(nodes[0], 1);
4718         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4719         connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4720
4721         let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4722         assert_eq!(revoked_htlc_txn.len(), 1);
4723         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4724         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4725         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4726         assert_ne!(revoked_htlc_txn[0].lock_time.0, 0); // HTLC-Timeout
4727
4728         // B will generate justice tx from A's revoked commitment/HTLC tx
4729         connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4730         check_closed_broadcast!(nodes[1], true);
4731         check_added_monitors!(nodes[1], 1);
4732         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4733
4734         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4735         assert_eq!(node_txn.len(), 2); // ChannelMonitor: bogus justice tx, justice tx on revoked outputs
4736         // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4737         // including the one already spent by revoked_htlc_txn[1]. That's OK, we'll spend with valid
4738         // transactions next...
4739         assert_eq!(node_txn[0].input.len(), 3);
4740         check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4741
4742         assert_eq!(node_txn[1].input.len(), 2);
4743         check_spends!(node_txn[1], revoked_local_txn[0], revoked_htlc_txn[0]);
4744         if node_txn[1].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4745                 assert_ne!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4746         } else {
4747                 assert_eq!(node_txn[1].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4748                 assert_ne!(node_txn[1].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4749         }
4750
4751         mine_transaction(&nodes[1], &node_txn[1]);
4752         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4753
4754         // Check B's ChannelMonitor was able to generate the right spendable output descriptor
4755         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4756         assert_eq!(spend_txn.len(), 1);
4757         assert_eq!(spend_txn[0].input.len(), 1);
4758         check_spends!(spend_txn[0], node_txn[1]);
4759 }
4760
4761 #[test]
4762 fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
4763         let mut chanmon_cfgs = create_chanmon_cfgs(2);
4764         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
4765         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4766         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4767         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4768
4769         // Create some initial channels
4770         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4771
4772         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4773         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
4774         assert_eq!(revoked_local_txn[0].input.len(), 1);
4775         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4776
4777         // The to-be-revoked commitment tx should have one HTLC and one to_remote output
4778         assert_eq!(revoked_local_txn[0].output.len(), 2);
4779
4780         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4781
4782         // B will generate HTLC-Success from revoked commitment tx
4783         mine_transaction(&nodes[1], &revoked_local_txn[0]);
4784         check_closed_broadcast!(nodes[1], true);
4785         check_added_monitors!(nodes[1], 1);
4786         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4787         let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4788
4789         assert_eq!(revoked_htlc_txn.len(), 1);
4790         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4791         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4792         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4793
4794         // Check that the unspent (of two) outputs on revoked_local_txn[0] is a P2WPKH:
4795         let unspent_local_txn_output = revoked_htlc_txn[0].input[0].previous_output.vout as usize ^ 1;
4796         assert_eq!(revoked_local_txn[0].output[unspent_local_txn_output].script_pubkey.len(), 2 + 20); // P2WPKH
4797
4798         // A will generate justice tx from B's revoked commitment/HTLC tx
4799         connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4800         check_closed_broadcast!(nodes[0], true);
4801         check_added_monitors!(nodes[0], 1);
4802         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4803
4804         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4805         assert_eq!(node_txn.len(), 2); // ChannelMonitor: justice tx on revoked commitment, justice tx on revoked HTLC-success
4806
4807         // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4808         // including the one already spent by revoked_htlc_txn[0]. That's OK, we'll spend with valid
4809         // transactions next...
4810         assert_eq!(node_txn[0].input.len(), 2);
4811         check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4812         if node_txn[0].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4813                 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4814         } else {
4815                 assert_eq!(node_txn[0].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4816                 assert_eq!(node_txn[0].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4817         }
4818
4819         assert_eq!(node_txn[1].input.len(), 1);
4820         check_spends!(node_txn[1], revoked_htlc_txn[0]);
4821
4822         mine_transaction(&nodes[0], &node_txn[1]);
4823         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
4824
4825         // Note that nodes[0]'s tx_broadcaster is still locked, so if we get here the channelmonitor
4826         // didn't try to generate any new transactions.
4827
4828         // Check A's ChannelMonitor was able to generate the right spendable output descriptor
4829         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
4830         assert_eq!(spend_txn.len(), 3);
4831         assert_eq!(spend_txn[0].input.len(), 1);
4832         check_spends!(spend_txn[0], revoked_local_txn[0]); // spending to_remote output from revoked local tx
4833         assert_ne!(spend_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4834         check_spends!(spend_txn[1], node_txn[1]); // spending justice tx output on the htlc success tx
4835         check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[1]); // Both outputs
4836 }
4837
4838 #[test]
4839 fn test_onchain_to_onchain_claim() {
4840         // Test that in case of channel closure, we detect the state of output and claim HTLC
4841         // on downstream peer's remote commitment tx.
4842         // First, have C claim an HTLC against its own latest commitment transaction.
4843         // Then, broadcast these to B, which should update the monitor downstream on the A<->B
4844         // channel.
4845         // Finally, check that B will claim the HTLC output if A's latest commitment transaction
4846         // gets broadcast.
4847
4848         let chanmon_cfgs = create_chanmon_cfgs(3);
4849         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4850         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4851         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4852
4853         // Create some initial channels
4854         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4855         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4856
4857         // Ensure all nodes are at the same height
4858         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4859         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4860         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4861         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4862
4863         // Rebalance the network a bit by relaying one payment through all the channels ...
4864         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4865         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4866
4867         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
4868         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
4869         check_spends!(commitment_tx[0], chan_2.3);
4870         nodes[2].node.claim_funds(payment_preimage);
4871         expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
4872         check_added_monitors!(nodes[2], 1);
4873         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
4874         assert!(updates.update_add_htlcs.is_empty());
4875         assert!(updates.update_fail_htlcs.is_empty());
4876         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
4877         assert!(updates.update_fail_malformed_htlcs.is_empty());
4878
4879         mine_transaction(&nodes[2], &commitment_tx[0]);
4880         check_closed_broadcast!(nodes[2], true);
4881         check_added_monitors!(nodes[2], 1);
4882         check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4883
4884         let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 1 (HTLC-Success tx)
4885         assert_eq!(c_txn.len(), 1);
4886         check_spends!(c_txn[0], commitment_tx[0]);
4887         assert_eq!(c_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4888         assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
4889         assert_eq!(c_txn[0].lock_time.0, 0); // Success tx
4890
4891         // So we broadcast C's commitment tx and HTLC-Success on B's chain, we should successfully be able to extract preimage and update downstream monitor
4892         connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![commitment_tx[0].clone(), c_txn[0].clone()]));
4893         check_added_monitors!(nodes[1], 1);
4894         let events = nodes[1].node.get_and_clear_pending_events();
4895         assert_eq!(events.len(), 2);
4896         match events[0] {
4897                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
4898                 _ => panic!("Unexpected event"),
4899         }
4900         match events[1] {
4901                 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
4902                         assert_eq!(fee_earned_msat, Some(1000));
4903                         assert_eq!(prev_channel_id, Some(chan_1.2));
4904                         assert_eq!(claim_from_onchain_tx, true);
4905                         assert_eq!(next_channel_id, Some(chan_2.2));
4906                         assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
4907                 },
4908                 _ => panic!("Unexpected event"),
4909         }
4910         check_added_monitors!(nodes[1], 1);
4911         let mut msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4912         assert_eq!(msg_events.len(), 3);
4913         let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut msg_events);
4914         let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut msg_events);
4915
4916         match nodes_2_event {
4917                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
4918                 _ => panic!("Unexpected event"),
4919         }
4920
4921         match nodes_0_event {
4922                 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, .. } } => {
4923                         assert!(update_add_htlcs.is_empty());
4924                         assert!(update_fail_htlcs.is_empty());
4925                         assert_eq!(update_fulfill_htlcs.len(), 1);
4926                         assert!(update_fail_malformed_htlcs.is_empty());
4927                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
4928                 },
4929                 _ => panic!("Unexpected event"),
4930         };
4931
4932         // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
4933         match msg_events[0] {
4934                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4935                 _ => panic!("Unexpected event"),
4936         }
4937
4938         // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
4939         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4940         mine_transaction(&nodes[1], &commitment_tx[0]);
4941         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4942         let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4943         // ChannelMonitor: HTLC-Success tx
4944         assert_eq!(b_txn.len(), 1);
4945         check_spends!(b_txn[0], commitment_tx[0]);
4946         assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4947         assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
4948         assert_eq!(b_txn[0].lock_time.0, nodes[1].best_block_info().1); // Success tx
4949
4950         check_closed_broadcast!(nodes[1], true);
4951         check_added_monitors!(nodes[1], 1);
4952 }
4953
4954 #[test]
4955 fn test_duplicate_payment_hash_one_failure_one_success() {
4956         // Topology : A --> B --> C --> D
4957         // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
4958         // Note that because C will refuse to generate two payment secrets for the same payment hash,
4959         // we forward one of the payments onwards to D.
4960         let chanmon_cfgs = create_chanmon_cfgs(4);
4961         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
4962         // When this test was written, the default base fee floated based on the HTLC count.
4963         // It is now fixed, so we simply set the fee to the expected value here.
4964         let mut config = test_default_channel_config();
4965         config.channel_config.forwarding_fee_base_msat = 196;
4966         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs,
4967                 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
4968         let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
4969
4970         create_announced_chan_between_nodes(&nodes, 0, 1);
4971         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4972         create_announced_chan_between_nodes(&nodes, 2, 3);
4973
4974         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4975         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4976         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4977         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4978         connect_blocks(&nodes[3], node_max_height - nodes[3].best_block_info().1);
4979
4980         let (our_payment_preimage, duplicate_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 900_000);
4981
4982         let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200, None).unwrap();
4983         // We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
4984         // script push size limit so that the below script length checks match
4985         // ACCEPTED_HTLC_SCRIPT_WEIGHT.
4986         let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV - 40)
4987                 .with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
4988         let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000);
4989         send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 800_000, duplicate_payment_hash, payment_secret);
4990
4991         let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
4992         assert_eq!(commitment_txn[0].input.len(), 1);
4993         check_spends!(commitment_txn[0], chan_2.3);
4994
4995         mine_transaction(&nodes[1], &commitment_txn[0]);
4996         check_closed_broadcast!(nodes[1], true);
4997         check_added_monitors!(nodes[1], 1);
4998         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
4999         connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
5000
5001         let htlc_timeout_tx;
5002         { // Extract one of the two HTLC-Timeout transaction
5003                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5004                 // ChannelMonitor: timeout tx * 2-or-3
5005                 assert!(node_txn.len() == 2 || node_txn.len() == 3);
5006
5007                 check_spends!(node_txn[0], commitment_txn[0]);
5008                 assert_eq!(node_txn[0].input.len(), 1);
5009                 assert_eq!(node_txn[0].output.len(), 1);
5010
5011                 if node_txn.len() > 2 {
5012                         check_spends!(node_txn[1], commitment_txn[0]);
5013                         assert_eq!(node_txn[1].input.len(), 1);
5014                         assert_eq!(node_txn[1].output.len(), 1);
5015                         assert_eq!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5016
5017                         check_spends!(node_txn[2], commitment_txn[0]);
5018                         assert_eq!(node_txn[2].input.len(), 1);
5019                         assert_eq!(node_txn[2].output.len(), 1);
5020                         assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
5021                 } else {
5022                         check_spends!(node_txn[1], commitment_txn[0]);
5023                         assert_eq!(node_txn[1].input.len(), 1);
5024                         assert_eq!(node_txn[1].output.len(), 1);
5025                         assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5026                 }
5027
5028                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5029                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5030                 // Assign htlc_timeout_tx to the forwarded HTLC (with value ~800 sats). The received HTLC
5031                 // (with value 900 sats) will be claimed in the below `claim_funds` call.
5032                 if node_txn.len() > 2 {
5033                         assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5034                         htlc_timeout_tx = if node_txn[2].output[0].value < 900 { node_txn[2].clone() } else { node_txn[0].clone() };
5035                 } else {
5036                         htlc_timeout_tx = if node_txn[0].output[0].value < 900 { node_txn[1].clone() } else { node_txn[0].clone() };
5037                 }
5038         }
5039
5040         nodes[2].node.claim_funds(our_payment_preimage);
5041         expect_payment_claimed!(nodes[2], duplicate_payment_hash, 900_000);
5042
5043         mine_transaction(&nodes[2], &commitment_txn[0]);
5044         check_added_monitors!(nodes[2], 2);
5045         check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5046         let events = nodes[2].node.get_and_clear_pending_msg_events();
5047         match events[0] {
5048                 MessageSendEvent::UpdateHTLCs { .. } => {},
5049                 _ => panic!("Unexpected event"),
5050         }
5051         match events[1] {
5052                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5053                 _ => panic!("Unexepected event"),
5054         }
5055         let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
5056         assert_eq!(htlc_success_txn.len(), 2); // ChannelMonitor: HTLC-Success txn (*2 due to 2-HTLC outputs)
5057         check_spends!(htlc_success_txn[0], commitment_txn[0]);
5058         check_spends!(htlc_success_txn[1], commitment_txn[0]);
5059         assert_eq!(htlc_success_txn[0].input.len(), 1);
5060         assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5061         assert_eq!(htlc_success_txn[1].input.len(), 1);
5062         assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5063         assert_ne!(htlc_success_txn[0].input[0].previous_output, htlc_success_txn[1].input[0].previous_output);
5064         assert_ne!(htlc_success_txn[1].input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
5065
5066         mine_transaction(&nodes[1], &htlc_timeout_tx);
5067         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5068         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
5069         let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5070         assert!(htlc_updates.update_add_htlcs.is_empty());
5071         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
5072         let first_htlc_id = htlc_updates.update_fail_htlcs[0].htlc_id;
5073         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
5074         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
5075         check_added_monitors!(nodes[1], 1);
5076
5077         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
5078         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5079         {
5080                 commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
5081         }
5082         expect_payment_failed_with_update!(nodes[0], duplicate_payment_hash, false, chan_2.0.contents.short_channel_id, true);
5083
5084         // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
5085         mine_transaction(&nodes[1], &htlc_success_txn[1]);
5086         expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(196), true, true);
5087         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5088         assert!(updates.update_add_htlcs.is_empty());
5089         assert!(updates.update_fail_htlcs.is_empty());
5090         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
5091         assert_ne!(updates.update_fulfill_htlcs[0].htlc_id, first_htlc_id);
5092         assert!(updates.update_fail_malformed_htlcs.is_empty());
5093         check_added_monitors!(nodes[1], 1);
5094
5095         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
5096         commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
5097         expect_payment_sent(&nodes[0], our_payment_preimage, None, true, true);
5098 }
5099
5100 #[test]
5101 fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5102         let chanmon_cfgs = create_chanmon_cfgs(2);
5103         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5104         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5105         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5106
5107         // Create some initial channels
5108         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5109
5110         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
5111         let local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
5112         assert_eq!(local_txn.len(), 1);
5113         assert_eq!(local_txn[0].input.len(), 1);
5114         check_spends!(local_txn[0], chan_1.3);
5115
5116         // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
5117         nodes[1].node.claim_funds(payment_preimage);
5118         expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
5119         check_added_monitors!(nodes[1], 1);
5120
5121         mine_transaction(&nodes[1], &local_txn[0]);
5122         check_added_monitors!(nodes[1], 1);
5123         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
5124         let events = nodes[1].node.get_and_clear_pending_msg_events();
5125         match events[0] {
5126                 MessageSendEvent::UpdateHTLCs { .. } => {},
5127                 _ => panic!("Unexpected event"),
5128         }
5129         match events[1] {
5130                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5131                 _ => panic!("Unexepected event"),
5132         }
5133         let node_tx = {
5134                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5135                 assert_eq!(node_txn.len(), 1);
5136                 assert_eq!(node_txn[0].input.len(), 1);
5137                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5138                 check_spends!(node_txn[0], local_txn[0]);
5139                 node_txn[0].clone()
5140         };
5141
5142         mine_transaction(&nodes[1], &node_tx);
5143         connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
5144
5145         // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
5146         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5147         assert_eq!(spend_txn.len(), 1);
5148         assert_eq!(spend_txn[0].input.len(), 1);
5149         check_spends!(spend_txn[0], node_tx);
5150         assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5151 }
5152
5153 fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
5154         // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
5155         // unrevoked commitment transaction.
5156         // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
5157         // a remote RAA before they could be failed backwards (and combinations thereof).
5158         // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
5159         // use the same payment hashes.
5160         // Thus, we use a six-node network:
5161         //
5162         // A \         / E
5163         //    - C - D -
5164         // B /         \ F
5165         // And test where C fails back to A/B when D announces its latest commitment transaction
5166         let chanmon_cfgs = create_chanmon_cfgs(6);
5167         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
5168         // When this test was written, the default base fee floated based on the HTLC count.
5169         // It is now fixed, so we simply set the fee to the expected value here.
5170         let mut config = test_default_channel_config();
5171         config.channel_config.forwarding_fee_base_msat = 196;
5172         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs,
5173                 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
5174         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
5175
5176         let _chan_0_2 = create_announced_chan_between_nodes(&nodes, 0, 2);
5177         let _chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
5178         let chan_2_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
5179         let chan_3_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
5180         let chan_3_5  = create_announced_chan_between_nodes(&nodes, 3, 5);
5181
5182         // Rebalance and check output sanity...
5183         send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
5184         send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
5185         assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 2);
5186
5187         let ds_dust_limit = nodes[3].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
5188                 .unwrap().lock().unwrap().channel_by_id.get(&chan_2_3.2).unwrap().context().holder_dust_limit_satoshis;
5189         // 0th HTLC:
5190         let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5191         // 1st HTLC:
5192         let (_, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5193         let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5194         // 2nd HTLC:
5195         send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_1, nodes[5].node.create_inbound_payment_for_hash(payment_hash_1, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5196         // 3rd HTLC:
5197         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_2, nodes[5].node.create_inbound_payment_for_hash(payment_hash_2, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5198         // 4th HTLC:
5199         let (_, payment_hash_3, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5200         // 5th HTLC:
5201         let (_, payment_hash_4, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5202         let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5203         // 6th HTLC:
5204         send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_3, nodes[5].node.create_inbound_payment_for_hash(payment_hash_3, None, 7200, None).unwrap());
5205         // 7th HTLC:
5206         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_4, nodes[5].node.create_inbound_payment_for_hash(payment_hash_4, None, 7200, None).unwrap());
5207
5208         // 8th HTLC:
5209         let (_, payment_hash_5, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5210         // 9th HTLC:
5211         let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5212         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_5, nodes[5].node.create_inbound_payment_for_hash(payment_hash_5, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5213
5214         // 10th HTLC:
5215         let (_, payment_hash_6, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5216         // 11th HTLC:
5217         let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5218         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_6, nodes[5].node.create_inbound_payment_for_hash(payment_hash_6, None, 7200, None).unwrap());
5219
5220         // Double-check that six of the new HTLC were added
5221         // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
5222         // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
5223         assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2).len(), 1);
5224         assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 8);
5225
5226         // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
5227         // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5228         nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5229         nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5230         nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5231         nodes[4].node.fail_htlc_backwards(&payment_hash_6);
5232         check_added_monitors!(nodes[4], 0);
5233
5234         let failed_destinations = vec![
5235                 HTLCDestination::FailedPayment { payment_hash: payment_hash_1 },
5236                 HTLCDestination::FailedPayment { payment_hash: payment_hash_3 },
5237                 HTLCDestination::FailedPayment { payment_hash: payment_hash_5 },
5238                 HTLCDestination::FailedPayment { payment_hash: payment_hash_6 },
5239         ];
5240         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[4], failed_destinations);
5241         check_added_monitors!(nodes[4], 1);
5242
5243         let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
5244         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]);
5245         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]);
5246         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]);
5247         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]);
5248         commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
5249
5250         // Fail 3rd below-dust and 7th above-dust HTLCs
5251         nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5252         nodes[5].node.fail_htlc_backwards(&payment_hash_4);
5253         check_added_monitors!(nodes[5], 0);
5254
5255         let failed_destinations_2 = vec![
5256                 HTLCDestination::FailedPayment { payment_hash: payment_hash_2 },
5257                 HTLCDestination::FailedPayment { payment_hash: payment_hash_4 },
5258         ];
5259         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[5], failed_destinations_2);
5260         check_added_monitors!(nodes[5], 1);
5261
5262         let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
5263         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]);
5264         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]);
5265         commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
5266
5267         let ds_prev_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5268
5269         // After 4 and 2 removes respectively above in nodes[4] and nodes[5], nodes[3] should receive 6 PaymentForwardedFailed events
5270         let failed_destinations_3 = vec![
5271                 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5272                 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5273                 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5274                 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5275                 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5276                 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5277         ];
5278         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations_3);
5279         check_added_monitors!(nodes[3], 1);
5280         let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
5281         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]);
5282         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]);
5283         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]);
5284         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]);
5285         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]);
5286         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]);
5287         if deliver_last_raa {
5288                 commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
5289         } else {
5290                 let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
5291         }
5292
5293         // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
5294         // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
5295         // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
5296         // propagated back to A/B yet (and D has two unrevoked commitment transactions).
5297         //
5298         // We now broadcast the latest commitment transaction, which *should* result in failures for
5299         // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
5300         // the non-broadcast above-dust HTLCs.
5301         //
5302         // Alternatively, we may broadcast the previous commitment transaction, which should only
5303         // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
5304         let ds_last_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5305
5306         if announce_latest {
5307                 mine_transaction(&nodes[2], &ds_last_commitment_tx[0]);
5308         } else {
5309                 mine_transaction(&nodes[2], &ds_prev_commitment_tx[0]);
5310         }
5311         let events = nodes[2].node.get_and_clear_pending_events();
5312         let close_event = if deliver_last_raa {
5313                 assert_eq!(events.len(), 2 + 6);
5314                 events.last().clone().unwrap()
5315         } else {
5316                 assert_eq!(events.len(), 1);
5317                 events.last().clone().unwrap()
5318         };
5319         match close_event {
5320                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
5321                 _ => panic!("Unexpected event"),
5322         }
5323
5324         connect_blocks(&nodes[2], ANTI_REORG_DELAY - 1);
5325         check_closed_broadcast!(nodes[2], true);
5326         if deliver_last_raa {
5327                 expect_pending_htlcs_forwardable_from_events!(nodes[2], events[0..1], true);
5328
5329                 let expected_destinations: Vec<HTLCDestination> = repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(3).collect();
5330                 expect_htlc_handling_failed_destinations!(nodes[2].node.get_and_clear_pending_events(), expected_destinations);
5331         } else {
5332                 let expected_destinations: Vec<HTLCDestination> = if announce_latest {
5333                         repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(9).collect()
5334                 } else {
5335                         repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(6).collect()
5336                 };
5337
5338                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], expected_destinations);
5339         }
5340         check_added_monitors!(nodes[2], 3);
5341
5342         let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
5343         assert_eq!(cs_msgs.len(), 2);
5344         let mut a_done = false;
5345         for msg in cs_msgs {
5346                 match msg {
5347                         MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
5348                                 // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
5349                                 // should be failed-backwards here.
5350                                 let target = if *node_id == nodes[0].node.get_our_node_id() {
5351                                         // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
5352                                         for htlc in &updates.update_fail_htlcs {
5353                                                 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 6 || if announce_latest { htlc.htlc_id == 3 || htlc.htlc_id == 5 } else { false });
5354                                         }
5355                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
5356                                         assert!(!a_done);
5357                                         a_done = true;
5358                                         &nodes[0]
5359                                 } else {
5360                                         // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
5361                                         for htlc in &updates.update_fail_htlcs {
5362                                                 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
5363                                         }
5364                                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
5365                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
5366                                         &nodes[1]
5367                                 };
5368                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
5369                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]);
5370                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]);
5371                                 if announce_latest {
5372                                         target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]);
5373                                         if *node_id == nodes[0].node.get_our_node_id() {
5374                                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]);
5375                                         }
5376                                 }
5377                                 commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
5378                         },
5379                         _ => panic!("Unexpected event"),
5380                 }
5381         }
5382
5383         let as_events = nodes[0].node.get_and_clear_pending_events();
5384         assert_eq!(as_events.len(), if announce_latest { 10 } else { 6 });
5385         let mut as_failds = HashSet::new();
5386         let mut as_updates = 0;
5387         for event in as_events.iter() {
5388                 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5389                         assert!(as_failds.insert(*payment_hash));
5390                         if *payment_hash != payment_hash_2 {
5391                                 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5392                         } else {
5393                                 assert!(!payment_failed_permanently);
5394                         }
5395                         if let PathFailure::OnPath { network_update: Some(_) } = failure {
5396                                 as_updates += 1;
5397                         }
5398                 } else if let &Event::PaymentFailed { .. } = event {
5399                 } else { panic!("Unexpected event"); }
5400         }
5401         assert!(as_failds.contains(&payment_hash_1));
5402         assert!(as_failds.contains(&payment_hash_2));
5403         if announce_latest {
5404                 assert!(as_failds.contains(&payment_hash_3));
5405                 assert!(as_failds.contains(&payment_hash_5));
5406         }
5407         assert!(as_failds.contains(&payment_hash_6));
5408
5409         let bs_events = nodes[1].node.get_and_clear_pending_events();
5410         assert_eq!(bs_events.len(), if announce_latest { 8 } else { 6 });
5411         let mut bs_failds = HashSet::new();
5412         let mut bs_updates = 0;
5413         for event in bs_events.iter() {
5414                 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5415                         assert!(bs_failds.insert(*payment_hash));
5416                         if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
5417                                 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5418                         } else {
5419                                 assert!(!payment_failed_permanently);
5420                         }
5421                         if let PathFailure::OnPath { network_update: Some(_) } = failure {
5422                                 bs_updates += 1;
5423                         }
5424                 } else if let &Event::PaymentFailed { .. } = event {
5425                 } else { panic!("Unexpected event"); }
5426         }
5427         assert!(bs_failds.contains(&payment_hash_1));
5428         assert!(bs_failds.contains(&payment_hash_2));
5429         if announce_latest {
5430                 assert!(bs_failds.contains(&payment_hash_4));
5431         }
5432         assert!(bs_failds.contains(&payment_hash_5));
5433
5434         // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
5435         // get a NetworkUpdate. A should have gotten 4 HTLCs which were failed-back due to
5436         // unknown-preimage-etc, B should have gotten 2. Thus, in the
5437         // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2 NetworkUpdates.
5438         assert_eq!(as_updates, if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
5439         assert_eq!(bs_updates, if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
5440 }
5441
5442 #[test]
5443 fn test_fail_backwards_latest_remote_announce_a() {
5444         do_test_fail_backwards_unrevoked_remote_announce(false, true);
5445 }
5446
5447 #[test]
5448 fn test_fail_backwards_latest_remote_announce_b() {
5449         do_test_fail_backwards_unrevoked_remote_announce(true, true);
5450 }
5451
5452 #[test]
5453 fn test_fail_backwards_previous_remote_announce() {
5454         do_test_fail_backwards_unrevoked_remote_announce(false, false);
5455         // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
5456         // tested for in test_commitment_revoked_fail_backward_exhaustive()
5457 }
5458
5459 #[test]
5460 fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
5461         let chanmon_cfgs = create_chanmon_cfgs(2);
5462         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5463         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5464         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5465
5466         // Create some initial channels
5467         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5468
5469         let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5470         let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5471         assert_eq!(local_txn[0].input.len(), 1);
5472         check_spends!(local_txn[0], chan_1.3);
5473
5474         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5475         mine_transaction(&nodes[0], &local_txn[0]);
5476         check_closed_broadcast!(nodes[0], true);
5477         check_added_monitors!(nodes[0], 1);
5478         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5479         connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5480
5481         let htlc_timeout = {
5482                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5483                 assert_eq!(node_txn.len(), 1);
5484                 assert_eq!(node_txn[0].input.len(), 1);
5485                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5486                 check_spends!(node_txn[0], local_txn[0]);
5487                 node_txn[0].clone()
5488         };
5489
5490         mine_transaction(&nodes[0], &htlc_timeout);
5491         connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5492         expect_payment_failed!(nodes[0], our_payment_hash, false);
5493
5494         // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5495         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5496         assert_eq!(spend_txn.len(), 3);
5497         check_spends!(spend_txn[0], local_txn[0]);
5498         assert_eq!(spend_txn[1].input.len(), 1);
5499         check_spends!(spend_txn[1], htlc_timeout);
5500         assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5501         assert_eq!(spend_txn[2].input.len(), 2);
5502         check_spends!(spend_txn[2], local_txn[0], htlc_timeout);
5503         assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5504                 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5505 }
5506
5507 #[test]
5508 fn test_key_derivation_params() {
5509         // This test is a copy of test_dynamic_spendable_outputs_local_htlc_timeout_tx, with a key
5510         // manager rotation to test that `channel_keys_id` returned in
5511         // [`SpendableOutputDescriptor::DelayedPaymentOutput`] let us re-derive the channel key set to
5512         // then derive a `delayed_payment_key`.
5513
5514         let chanmon_cfgs = create_chanmon_cfgs(3);
5515
5516         // We manually create the node configuration to backup the seed.
5517         let seed = [42; 32];
5518         let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5519         let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &chanmon_cfgs[0].persister, &keys_manager);
5520         let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &chanmon_cfgs[0].logger));
5521         let scorer = RwLock::new(test_utils::TestScorer::new());
5522         let router = test_utils::TestRouter::new(network_graph.clone(), &scorer);
5523         let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, router, chain_monitor, keys_manager: &keys_manager, network_graph, node_seed: seed, override_init_features: alloc::rc::Rc::new(core::cell::RefCell::new(None)) };
5524         let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5525         node_cfgs.remove(0);
5526         node_cfgs.insert(0, node);
5527
5528         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5529         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5530
5531         // Create some initial channels
5532         // Create a dummy channel to advance index by one and thus test re-derivation correctness
5533         // for node 0
5534         let chan_0 = create_announced_chan_between_nodes(&nodes, 0, 2);
5535         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5536         assert_ne!(chan_0.3.output[0].script_pubkey, chan_1.3.output[0].script_pubkey);
5537
5538         // Ensure all nodes are at the same height
5539         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5540         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5541         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5542         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5543
5544         let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5545         let local_txn_0 = get_local_commitment_txn!(nodes[0], chan_0.2);
5546         let local_txn_1 = get_local_commitment_txn!(nodes[0], chan_1.2);
5547         assert_eq!(local_txn_1[0].input.len(), 1);
5548         check_spends!(local_txn_1[0], chan_1.3);
5549
5550         // We check funding pubkey are unique
5551         let (from_0_funding_key_0, from_0_funding_key_1) = (PublicKey::from_slice(&local_txn_0[0].input[0].witness.to_vec()[3][2..35]), PublicKey::from_slice(&local_txn_0[0].input[0].witness.to_vec()[3][36..69]));
5552         let (from_1_funding_key_0, from_1_funding_key_1) = (PublicKey::from_slice(&local_txn_1[0].input[0].witness.to_vec()[3][2..35]), PublicKey::from_slice(&local_txn_1[0].input[0].witness.to_vec()[3][36..69]));
5553         if from_0_funding_key_0 == from_1_funding_key_0
5554             || from_0_funding_key_0 == from_1_funding_key_1
5555             || from_0_funding_key_1 == from_1_funding_key_0
5556             || from_0_funding_key_1 == from_1_funding_key_1 {
5557                 panic!("Funding pubkeys aren't unique");
5558         }
5559
5560         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5561         mine_transaction(&nodes[0], &local_txn_1[0]);
5562         connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5563         check_closed_broadcast!(nodes[0], true);
5564         check_added_monitors!(nodes[0], 1);
5565         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5566
5567         let htlc_timeout = {
5568                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5569                 assert_eq!(node_txn.len(), 1);
5570                 assert_eq!(node_txn[0].input.len(), 1);
5571                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5572                 check_spends!(node_txn[0], local_txn_1[0]);
5573                 node_txn[0].clone()
5574         };
5575
5576         mine_transaction(&nodes[0], &htlc_timeout);
5577         connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5578         expect_payment_failed!(nodes[0], our_payment_hash, false);
5579
5580         // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5581         let new_keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5582         let spend_txn = check_spendable_outputs!(nodes[0], new_keys_manager);
5583         assert_eq!(spend_txn.len(), 3);
5584         check_spends!(spend_txn[0], local_txn_1[0]);
5585         assert_eq!(spend_txn[1].input.len(), 1);
5586         check_spends!(spend_txn[1], htlc_timeout);
5587         assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5588         assert_eq!(spend_txn[2].input.len(), 2);
5589         check_spends!(spend_txn[2], local_txn_1[0], htlc_timeout);
5590         assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5591                 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5592 }
5593
5594 #[test]
5595 fn test_static_output_closing_tx() {
5596         let chanmon_cfgs = create_chanmon_cfgs(2);
5597         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5598         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5599         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5600
5601         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5602
5603         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5604         let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
5605
5606         mine_transaction(&nodes[0], &closing_tx);
5607         check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
5608         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5609
5610         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5611         assert_eq!(spend_txn.len(), 1);
5612         check_spends!(spend_txn[0], closing_tx);
5613
5614         mine_transaction(&nodes[1], &closing_tx);
5615         check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
5616         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5617
5618         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5619         assert_eq!(spend_txn.len(), 1);
5620         check_spends!(spend_txn[0], closing_tx);
5621 }
5622
5623 fn do_htlc_claim_local_commitment_only(use_dust: bool) {
5624         let chanmon_cfgs = create_chanmon_cfgs(2);
5625         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5626         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5627         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5628         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5629
5630         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3_000_000 });
5631
5632         // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
5633         // present in B's local commitment transaction, but none of A's commitment transactions.
5634         nodes[1].node.claim_funds(payment_preimage);
5635         check_added_monitors!(nodes[1], 1);
5636         expect_payment_claimed!(nodes[1], payment_hash, if use_dust { 50000 } else { 3_000_000 });
5637
5638         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5639         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
5640         expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
5641
5642         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5643         check_added_monitors!(nodes[0], 1);
5644         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5645         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5646         check_added_monitors!(nodes[1], 1);
5647
5648         let starting_block = nodes[1].best_block_info();
5649         let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5650         for _ in starting_block.1 + 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + starting_block.1 + 2 {
5651                 connect_block(&nodes[1], &block);
5652                 block.header.prev_blockhash = block.block_hash();
5653         }
5654         test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
5655         check_closed_broadcast!(nodes[1], true);
5656         check_added_monitors!(nodes[1], 1);
5657         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
5658 }
5659
5660 fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
5661         let chanmon_cfgs = create_chanmon_cfgs(2);
5662         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5663         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5664         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5665         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5666
5667         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if use_dust { 50000 } else { 3000000 });
5668         nodes[0].node.send_payment_with_route(&route, payment_hash,
5669                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
5670         check_added_monitors!(nodes[0], 1);
5671
5672         let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5673
5674         // As far as A is concerned, the HTLC is now present only in the latest remote commitment
5675         // transaction, however it is not in A's latest local commitment, so we can just broadcast that
5676         // to "time out" the HTLC.
5677
5678         let starting_block = nodes[1].best_block_info();
5679         let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5680
5681         for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + starting_block.1 + 2 {
5682                 connect_block(&nodes[0], &block);
5683                 block.header.prev_blockhash = block.block_hash();
5684         }
5685         test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5686         check_closed_broadcast!(nodes[0], true);
5687         check_added_monitors!(nodes[0], 1);
5688         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5689 }
5690
5691 fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
5692         let chanmon_cfgs = create_chanmon_cfgs(3);
5693         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5694         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5695         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5696         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5697
5698         // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
5699         // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
5700         // Also optionally test that we *don't* fail the channel in case the commitment transaction was
5701         // actually revoked.
5702         let htlc_value = if use_dust { 50000 } else { 3000000 };
5703         let (_, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5704         nodes[1].node.fail_htlc_backwards(&our_payment_hash);
5705         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
5706         check_added_monitors!(nodes[1], 1);
5707
5708         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5709         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
5710         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5711         check_added_monitors!(nodes[0], 1);
5712         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5713         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5714         check_added_monitors!(nodes[1], 1);
5715         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1);
5716         check_added_monitors!(nodes[1], 1);
5717         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
5718
5719         if check_revoke_no_close {
5720                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
5721                 check_added_monitors!(nodes[0], 1);
5722         }
5723
5724         let starting_block = nodes[1].best_block_info();
5725         let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5726         for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 2 {
5727                 connect_block(&nodes[0], &block);
5728                 block.header.prev_blockhash = block.block_hash();
5729         }
5730         if !check_revoke_no_close {
5731                 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5732                 check_closed_broadcast!(nodes[0], true);
5733                 check_added_monitors!(nodes[0], 1);
5734                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5735         } else {
5736                 expect_payment_failed!(nodes[0], our_payment_hash, true);
5737         }
5738 }
5739
5740 // Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
5741 // There are only a few cases to test here:
5742 //  * its not really normative behavior, but we test that below-dust HTLCs "included" in
5743 //    broadcastable commitment transactions result in channel closure,
5744 //  * its included in an unrevoked-but-previous remote commitment transaction,
5745 //  * its included in the latest remote or local commitment transactions.
5746 // We test each of the three possible commitment transactions individually and use both dust and
5747 // non-dust HTLCs.
5748 // Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
5749 // assume they are handled the same across all six cases, as both outbound and inbound failures are
5750 // tested for at least one of the cases in other tests.
5751 #[test]
5752 fn htlc_claim_single_commitment_only_a() {
5753         do_htlc_claim_local_commitment_only(true);
5754         do_htlc_claim_local_commitment_only(false);
5755
5756         do_htlc_claim_current_remote_commitment_only(true);
5757         do_htlc_claim_current_remote_commitment_only(false);
5758 }
5759
5760 #[test]
5761 fn htlc_claim_single_commitment_only_b() {
5762         do_htlc_claim_previous_remote_commitment_only(true, false);
5763         do_htlc_claim_previous_remote_commitment_only(false, false);
5764         do_htlc_claim_previous_remote_commitment_only(true, true);
5765         do_htlc_claim_previous_remote_commitment_only(false, true);
5766 }
5767
5768 #[test]
5769 #[should_panic]
5770 fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
5771         let chanmon_cfgs = create_chanmon_cfgs(2);
5772         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5773         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5774         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5775         // Force duplicate randomness for every get-random call
5776         for node in nodes.iter() {
5777                 *node.keys_manager.override_random_bytes.lock().unwrap() = Some([0; 32]);
5778         }
5779
5780         // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
5781         let channel_value_satoshis=10000;
5782         let push_msat=10001;
5783         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
5784         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5785         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5786         get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
5787
5788         // Create a second channel with the same random values. This used to panic due to a colliding
5789         // channel_id, but now panics due to a colliding outbound SCID alias.
5790         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5791 }
5792
5793 #[test]
5794 fn bolt2_open_channel_sending_node_checks_part2() {
5795         let chanmon_cfgs = create_chanmon_cfgs(2);
5796         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5797         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5798         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5799
5800         // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
5801         let channel_value_satoshis=2^24;
5802         let push_msat=10001;
5803         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5804
5805         // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
5806         let channel_value_satoshis=10000;
5807         // Test when push_msat is equal to 1000 * funding_satoshis.
5808         let push_msat=1000*channel_value_satoshis+1;
5809         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5810
5811         // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
5812         let channel_value_satoshis=10000;
5813         let push_msat=10001;
5814         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_ok()); //Create a valid channel
5815         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5816         assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
5817
5818         // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
5819         // Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
5820         assert!(node0_to_1_send_open_channel.channel_flags<=1);
5821
5822         // BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
5823         assert!(BREAKDOWN_TIMEOUT>0);
5824         assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
5825
5826         // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
5827         let chain_hash=genesis_block(Network::Testnet).header.block_hash();
5828         assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
5829
5830         // BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
5831         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
5832         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
5833         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
5834         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_point.serialize()).is_ok());
5835         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
5836 }
5837
5838 #[test]
5839 fn bolt2_open_channel_sane_dust_limit() {
5840         let chanmon_cfgs = create_chanmon_cfgs(2);
5841         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5842         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5843         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5844
5845         let channel_value_satoshis=1000000;
5846         let push_msat=10001;
5847         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
5848         let mut node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5849         node0_to_1_send_open_channel.dust_limit_satoshis = 547;
5850         node0_to_1_send_open_channel.channel_reserve_satoshis = 100001;
5851
5852         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5853         let events = nodes[1].node.get_and_clear_pending_msg_events();
5854         let err_msg = match events[0] {
5855                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
5856                         msg.clone()
5857                 },
5858                 _ => panic!("Unexpected event"),
5859         };
5860         assert_eq!(err_msg.data, "dust_limit_satoshis (547) is greater than the implementation limit (546)");
5861 }
5862
5863 // Test that if we fail to send an HTLC that is being freed from the holding cell, and the HTLC
5864 // originated from our node, its failure is surfaced to the user. We trigger this failure to
5865 // free the HTLC by increasing our fee while the HTLC is in the holding cell such that the HTLC
5866 // is no longer affordable once it's freed.
5867 #[test]
5868 fn test_fail_holding_cell_htlc_upon_free() {
5869         let chanmon_cfgs = create_chanmon_cfgs(2);
5870         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5871         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5872         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5873         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5874
5875         // First nodes[0] generates an update_fee, setting the channel's
5876         // pending_update_fee.
5877         {
5878                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5879                 *feerate_lock += 20;
5880         }
5881         nodes[0].node.timer_tick_occurred();
5882         check_added_monitors!(nodes[0], 1);
5883
5884         let events = nodes[0].node.get_and_clear_pending_msg_events();
5885         assert_eq!(events.len(), 1);
5886         let (update_msg, commitment_signed) = match events[0] {
5887                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5888                         (update_fee.as_ref(), commitment_signed)
5889                 },
5890                 _ => panic!("Unexpected event"),
5891         };
5892
5893         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5894
5895         let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5896         let channel_reserve = chan_stat.channel_reserve_msat;
5897         let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5898         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5899
5900         // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5901         let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
5902         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
5903
5904         // Send a payment which passes reserve checks but gets stuck in the holding cell.
5905         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
5906                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
5907         chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5908         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
5909
5910         // Flush the pending fee update.
5911         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5912         let (as_revoke_and_ack, _) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5913         check_added_monitors!(nodes[1], 1);
5914         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
5915         check_added_monitors!(nodes[0], 1);
5916
5917         // Upon receipt of the RAA, there will be an attempt to resend the holding cell
5918         // HTLC, but now that the fee has been raised the payment will now fail, causing
5919         // us to surface its failure to the user.
5920         chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5921         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
5922         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", chan.2), 1);
5923
5924         // Check that the payment failed to be sent out.
5925         let events = nodes[0].node.get_and_clear_pending_events();
5926         assert_eq!(events.len(), 2);
5927         match &events[0] {
5928                 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
5929                         assert_eq!(PaymentId(our_payment_hash.0), *payment_id.as_ref().unwrap());
5930                         assert_eq!(our_payment_hash.clone(), *payment_hash);
5931                         assert_eq!(*payment_failed_permanently, false);
5932                         assert_eq!(*short_channel_id, Some(route.paths[0].hops[0].short_channel_id));
5933                 },
5934                 _ => panic!("Unexpected event"),
5935         }
5936         match &events[1] {
5937                 &Event::PaymentFailed { ref payment_hash, .. } => {
5938                         assert_eq!(our_payment_hash.clone(), *payment_hash);
5939                 },
5940                 _ => panic!("Unexpected event"),
5941         }
5942 }
5943
5944 // Test that if multiple HTLCs are released from the holding cell and one is
5945 // valid but the other is no longer valid upon release, the valid HTLC can be
5946 // successfully completed while the other one fails as expected.
5947 #[test]
5948 fn test_free_and_fail_holding_cell_htlcs() {
5949         let chanmon_cfgs = create_chanmon_cfgs(2);
5950         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5951         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5952         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5953         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5954
5955         // First nodes[0] generates an update_fee, setting the channel's
5956         // pending_update_fee.
5957         {
5958                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5959                 *feerate_lock += 200;
5960         }
5961         nodes[0].node.timer_tick_occurred();
5962         check_added_monitors!(nodes[0], 1);
5963
5964         let events = nodes[0].node.get_and_clear_pending_msg_events();
5965         assert_eq!(events.len(), 1);
5966         let (update_msg, commitment_signed) = match events[0] {
5967                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5968                         (update_fee.as_ref(), commitment_signed)
5969                 },
5970                 _ => panic!("Unexpected event"),
5971         };
5972
5973         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5974
5975         let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5976         let channel_reserve = chan_stat.channel_reserve_msat;
5977         let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5978         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5979
5980         // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5981         let amt_1 = 20000;
5982         let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features) - amt_1;
5983         let (route_1, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_1);
5984         let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_2);
5985
5986         // Send 2 payments which pass reserve checks but get stuck in the holding cell.
5987         nodes[0].node.send_payment_with_route(&route_1, payment_hash_1,
5988                 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
5989         chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5990         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1);
5991         let payment_id_2 = PaymentId(nodes[0].keys_manager.get_secure_random_bytes());
5992         nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
5993                 RecipientOnionFields::secret_only(payment_secret_2), payment_id_2).unwrap();
5994         chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5995         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1 + amt_2);
5996
5997         // Flush the pending fee update.
5998         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5999         let (revoke_and_ack, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6000         check_added_monitors!(nodes[1], 1);
6001         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_and_ack);
6002         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6003         check_added_monitors!(nodes[0], 2);
6004
6005         // Upon receipt of the RAA, there will be an attempt to resend the holding cell HTLCs,
6006         // but now that the fee has been raised the second payment will now fail, causing us
6007         // to surface its failure to the user. The first payment should succeed.
6008         chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6009         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6010         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", chan.2), 1);
6011
6012         // Check that the second payment failed to be sent out.
6013         let events = nodes[0].node.get_and_clear_pending_events();
6014         assert_eq!(events.len(), 2);
6015         match &events[0] {
6016                 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
6017                         assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
6018                         assert_eq!(payment_hash_2.clone(), *payment_hash);
6019                         assert_eq!(*payment_failed_permanently, false);
6020                         assert_eq!(*short_channel_id, Some(route_2.paths[0].hops[0].short_channel_id));
6021                 },
6022                 _ => panic!("Unexpected event"),
6023         }
6024         match &events[1] {
6025                 &Event::PaymentFailed { ref payment_hash, .. } => {
6026                         assert_eq!(payment_hash_2.clone(), *payment_hash);
6027                 },
6028                 _ => panic!("Unexpected event"),
6029         }
6030
6031         // Complete the first payment and the RAA from the fee update.
6032         let (payment_event, send_raa_event) = {
6033                 let mut msgs = nodes[0].node.get_and_clear_pending_msg_events();
6034                 assert_eq!(msgs.len(), 2);
6035                 (SendEvent::from_event(msgs.remove(0)), msgs.remove(0))
6036         };
6037         let raa = match send_raa_event {
6038                 MessageSendEvent::SendRevokeAndACK { msg, .. } => msg,
6039                 _ => panic!("Unexpected event"),
6040         };
6041         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6042         check_added_monitors!(nodes[1], 1);
6043         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6044         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6045         let events = nodes[1].node.get_and_clear_pending_events();
6046         assert_eq!(events.len(), 1);
6047         match events[0] {
6048                 Event::PendingHTLCsForwardable { .. } => {},
6049                 _ => panic!("Unexpected event"),
6050         }
6051         nodes[1].node.process_pending_htlc_forwards();
6052         let events = nodes[1].node.get_and_clear_pending_events();
6053         assert_eq!(events.len(), 1);
6054         match events[0] {
6055                 Event::PaymentClaimable { .. } => {},
6056                 _ => panic!("Unexpected event"),
6057         }
6058         nodes[1].node.claim_funds(payment_preimage_1);
6059         check_added_monitors!(nodes[1], 1);
6060         expect_payment_claimed!(nodes[1], payment_hash_1, amt_1);
6061
6062         let update_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6063         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msgs.update_fulfill_htlcs[0]);
6064         commitment_signed_dance!(nodes[0], nodes[1], update_msgs.commitment_signed, false, true);
6065         expect_payment_sent!(nodes[0], payment_preimage_1);
6066 }
6067
6068 // Test that if we fail to forward an HTLC that is being freed from the holding cell that the
6069 // HTLC is failed backwards. We trigger this failure to forward the freed HTLC by increasing
6070 // our fee while the HTLC is in the holding cell such that the HTLC is no longer affordable
6071 // once it's freed.
6072 #[test]
6073 fn test_fail_holding_cell_htlc_upon_free_multihop() {
6074         let chanmon_cfgs = create_chanmon_cfgs(3);
6075         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6076         // Avoid having to include routing fees in calculations
6077         let mut config = test_default_channel_config();
6078         config.channel_config.forwarding_fee_base_msat = 0;
6079         config.channel_config.forwarding_fee_proportional_millionths = 0;
6080         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
6081         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6082         let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6083         let chan_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
6084
6085         // First nodes[1] generates an update_fee, setting the channel's
6086         // pending_update_fee.
6087         {
6088                 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
6089                 *feerate_lock += 20;
6090         }
6091         nodes[1].node.timer_tick_occurred();
6092         check_added_monitors!(nodes[1], 1);
6093
6094         let events = nodes[1].node.get_and_clear_pending_msg_events();
6095         assert_eq!(events.len(), 1);
6096         let (update_msg, commitment_signed) = match events[0] {
6097                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6098                         (update_fee.as_ref(), commitment_signed)
6099                 },
6100                 _ => panic!("Unexpected event"),
6101         };
6102
6103         nodes[2].node.handle_update_fee(&nodes[1].node.get_our_node_id(), update_msg.unwrap());
6104
6105         let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan_0_1.2);
6106         let channel_reserve = chan_stat.channel_reserve_msat;
6107         let feerate = get_feerate!(nodes[0], nodes[1], chan_0_1.2);
6108         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_0_1.2);
6109
6110         // Send a payment which passes reserve checks but gets stuck in the holding cell.
6111         let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6112         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], max_can_send);
6113         let payment_event = {
6114                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6115                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6116                 check_added_monitors!(nodes[0], 1);
6117
6118                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6119                 assert_eq!(events.len(), 1);
6120
6121                 SendEvent::from_event(events.remove(0))
6122         };
6123         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6124         check_added_monitors!(nodes[1], 0);
6125         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6126         expect_pending_htlcs_forwardable!(nodes[1]);
6127
6128         chan_stat = get_channel_value_stat!(nodes[1], nodes[2], chan_1_2.2);
6129         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
6130
6131         // Flush the pending fee update.
6132         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
6133         let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6134         check_added_monitors!(nodes[2], 1);
6135         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &raa);
6136         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &commitment_signed);
6137         check_added_monitors!(nodes[1], 2);
6138
6139         // A final RAA message is generated to finalize the fee update.
6140         let events = nodes[1].node.get_and_clear_pending_msg_events();
6141         assert_eq!(events.len(), 1);
6142
6143         let raa_msg = match &events[0] {
6144                 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => {
6145                         msg.clone()
6146                 },
6147                 _ => panic!("Unexpected event"),
6148         };
6149
6150         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa_msg);
6151         check_added_monitors!(nodes[2], 1);
6152         assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
6153
6154         // nodes[1]'s ChannelManager will now signal that we have HTLC forwards to process.
6155         let process_htlc_forwards_event = nodes[1].node.get_and_clear_pending_events();
6156         assert_eq!(process_htlc_forwards_event.len(), 2);
6157         match &process_htlc_forwards_event[0] {
6158                 &Event::PendingHTLCsForwardable { .. } => {},
6159                 _ => panic!("Unexpected event"),
6160         }
6161
6162         // In response, we call ChannelManager's process_pending_htlc_forwards
6163         nodes[1].node.process_pending_htlc_forwards();
6164         check_added_monitors!(nodes[1], 1);
6165
6166         // This causes the HTLC to be failed backwards.
6167         let fail_event = nodes[1].node.get_and_clear_pending_msg_events();
6168         assert_eq!(fail_event.len(), 1);
6169         let (fail_msg, commitment_signed) = match &fail_event[0] {
6170                 &MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6171                         assert_eq!(updates.update_add_htlcs.len(), 0);
6172                         assert_eq!(updates.update_fulfill_htlcs.len(), 0);
6173                         assert_eq!(updates.update_fail_malformed_htlcs.len(), 0);
6174                         assert_eq!(updates.update_fail_htlcs.len(), 1);
6175                         (updates.update_fail_htlcs[0].clone(), updates.commitment_signed.clone())
6176                 },
6177                 _ => panic!("Unexpected event"),
6178         };
6179
6180         // Pass the failure messages back to nodes[0].
6181         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
6182         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6183
6184         // Complete the HTLC failure+removal process.
6185         let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6186         check_added_monitors!(nodes[0], 1);
6187         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6188         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
6189         check_added_monitors!(nodes[1], 2);
6190         let final_raa_event = nodes[1].node.get_and_clear_pending_msg_events();
6191         assert_eq!(final_raa_event.len(), 1);
6192         let raa = match &final_raa_event[0] {
6193                 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => msg.clone(),
6194                 _ => panic!("Unexpected event"),
6195         };
6196         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
6197         expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_1_2.0.contents.short_channel_id, false);
6198         check_added_monitors!(nodes[0], 1);
6199 }
6200
6201 // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6202 // BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
6203 //TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.
6204
6205 #[test]
6206 fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
6207         //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
6208         let chanmon_cfgs = create_chanmon_cfgs(2);
6209         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6210         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6211         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6212         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6213
6214         let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6215         route.paths[0].hops[0].fee_msat = 100;
6216
6217         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6218                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6219                 ), true, APIError::ChannelUnavailable { .. }, {});
6220         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6221 }
6222
6223 #[test]
6224 fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
6225         //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6226         let chanmon_cfgs = create_chanmon_cfgs(2);
6227         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6228         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6229         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6230         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6231
6232         let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6233         route.paths[0].hops[0].fee_msat = 0;
6234         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6235                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)),
6236                 true, APIError::ChannelUnavailable { ref err },
6237                 assert_eq!(err, "Cannot send 0-msat HTLC"));
6238
6239         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6240         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send 0-msat HTLC", 1);
6241 }
6242
6243 #[test]
6244 fn test_update_add_htlc_bolt2_receiver_zero_value_msat() {
6245         //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6246         let chanmon_cfgs = create_chanmon_cfgs(2);
6247         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6248         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6249         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6250         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6251
6252         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6253         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6254                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6255         check_added_monitors!(nodes[0], 1);
6256         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6257         updates.update_add_htlcs[0].amount_msat = 0;
6258
6259         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6260         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote side tried to send a 0-msat HTLC".to_string(), 1);
6261         check_closed_broadcast!(nodes[1], true).unwrap();
6262         check_added_monitors!(nodes[1], 1);
6263         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote side tried to send a 0-msat HTLC".to_string() },
6264                 [nodes[0].node.get_our_node_id()], 100000);
6265 }
6266
6267 #[test]
6268 fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
6269         //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
6270         //It is enforced when constructing a route.
6271         let chanmon_cfgs = create_chanmon_cfgs(2);
6272         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6273         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6274         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6275         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6276
6277         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
6278                 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
6279         let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
6280         route.paths[0].hops.last_mut().unwrap().cltv_expiry_delta = 500000001;
6281         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6282                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6283                 ), true, APIError::InvalidRoute { ref err },
6284                 assert_eq!(err, &"Channel CLTV overflowed?"));
6285 }
6286
6287 #[test]
6288 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
6289         //BOLT 2 Requirement: if result would be offering more than the remote's max_accepted_htlcs HTLCs, in the remote commitment transaction: MUST NOT add an HTLC.
6290         //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
6291         //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6292         let chanmon_cfgs = create_chanmon_cfgs(2);
6293         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6294         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6295         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6296         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6297         let max_accepted_htlcs = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
6298                 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().counterparty_max_accepted_htlcs as u64;
6299
6300         // Fetch a route in advance as we will be unable to once we're unable to send.
6301         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6302         for i in 0..max_accepted_htlcs {
6303                 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6304                 let payment_event = {
6305                         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6306                                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6307                         check_added_monitors!(nodes[0], 1);
6308
6309                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6310                         assert_eq!(events.len(), 1);
6311                         if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
6312                                 assert_eq!(htlcs[0].htlc_id, i);
6313                         } else {
6314                                 assert!(false);
6315                         }
6316                         SendEvent::from_event(events.remove(0))
6317                 };
6318                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6319                 check_added_monitors!(nodes[1], 0);
6320                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6321
6322                 expect_pending_htlcs_forwardable!(nodes[1]);
6323                 expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 100000);
6324         }
6325         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6326                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6327                 ), true, APIError::ChannelUnavailable { .. }, {});
6328
6329         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6330 }
6331
6332 #[test]
6333 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
6334         //BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
6335         let chanmon_cfgs = create_chanmon_cfgs(2);
6336         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6337         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6338         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6339         let channel_value = 100000;
6340         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0);
6341         let max_in_flight = get_channel_value_stat!(nodes[0], nodes[1], chan.2).counterparty_max_htlc_value_in_flight_msat;
6342
6343         send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
6344
6345         let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_in_flight);
6346         // Manually create a route over our max in flight (which our router normally automatically
6347         // limits us to.
6348         route.paths[0].hops[0].fee_msat =  max_in_flight + 1;
6349         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6350                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6351                 ), true, APIError::ChannelUnavailable { .. }, {});
6352         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6353
6354         send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
6355 }
6356
6357 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
6358 #[test]
6359 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
6360         //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
6361         let chanmon_cfgs = create_chanmon_cfgs(2);
6362         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6363         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6364         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6365         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6366         let htlc_minimum_msat: u64;
6367         {
6368                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
6369                 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
6370                 let channel = chan_lock.channel_by_id.get(&chan.2).unwrap();
6371                 htlc_minimum_msat = channel.context().get_holder_htlc_minimum_msat();
6372         }
6373
6374         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], htlc_minimum_msat);
6375         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6376                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6377         check_added_monitors!(nodes[0], 1);
6378         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6379         updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
6380         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6381         assert!(nodes[1].node.list_channels().is_empty());
6382         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6383         assert!(regex::Regex::new(r"Remote side tried to send less than our minimum HTLC value\. Lower limit: \(\d+\)\. Actual: \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6384         check_added_monitors!(nodes[1], 1);
6385         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6386 }
6387
6388 #[test]
6389 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
6390         //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
6391         let chanmon_cfgs = create_chanmon_cfgs(2);
6392         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6393         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6394         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6395         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6396
6397         let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6398         let channel_reserve = chan_stat.channel_reserve_msat;
6399         let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
6400         let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
6401         // The 2* and +1 are for the fee spike reserve.
6402         let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6403
6404         let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
6405         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
6406         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6407                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6408         check_added_monitors!(nodes[0], 1);
6409         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6410
6411         // Even though channel-initiator senders are required to respect the fee_spike_reserve,
6412         // at this time channel-initiatee receivers are not required to enforce that senders
6413         // respect the fee_spike_reserve.
6414         updates.update_add_htlcs[0].amount_msat = max_can_send + commit_tx_fee_outbound + 1;
6415         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6416
6417         assert!(nodes[1].node.list_channels().is_empty());
6418         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6419         assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
6420         check_added_monitors!(nodes[1], 1);
6421         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6422 }
6423
6424 #[test]
6425 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
6426         //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
6427         //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
6428         let chanmon_cfgs = create_chanmon_cfgs(2);
6429         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6430         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6431         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6432         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6433
6434         let send_amt = 3999999;
6435         let (mut route, our_payment_hash, _, our_payment_secret) =
6436                 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
6437         route.paths[0].hops[0].fee_msat = send_amt;
6438         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
6439         let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
6440         let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
6441         let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
6442                 &route.paths[0], send_amt, RecipientOnionFields::secret_only(our_payment_secret), cur_height, &None).unwrap();
6443         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
6444
6445         let mut msg = msgs::UpdateAddHTLC {
6446                 channel_id: chan.2,
6447                 htlc_id: 0,
6448                 amount_msat: 1000,
6449                 payment_hash: our_payment_hash,
6450                 cltv_expiry: htlc_cltv,
6451                 onion_routing_packet: onion_packet.clone(),
6452                 skimmed_fee_msat: None,
6453         };
6454
6455         for i in 0..50 {
6456                 msg.htlc_id = i as u64;
6457                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6458         }
6459         msg.htlc_id = (50) as u64;
6460         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6461
6462         assert!(nodes[1].node.list_channels().is_empty());
6463         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6464         assert!(regex::Regex::new(r"Remote tried to push more than our max accepted HTLCs \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6465         check_added_monitors!(nodes[1], 1);
6466         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6467 }
6468
6469 #[test]
6470 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
6471         //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
6472         let chanmon_cfgs = create_chanmon_cfgs(2);
6473         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6474         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6475         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6476         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6477
6478         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6479         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6480                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6481         check_added_monitors!(nodes[0], 1);
6482         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6483         updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], nodes[0], chan.2).counterparty_max_htlc_value_in_flight_msat + 1;
6484         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6485
6486         assert!(nodes[1].node.list_channels().is_empty());
6487         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6488         assert!(regex::Regex::new("Remote HTLC add would put them over our max HTLC value").unwrap().is_match(err_msg.data.as_str()));
6489         check_added_monitors!(nodes[1], 1);
6490         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 1000000);
6491 }
6492
6493 #[test]
6494 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
6495         //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
6496         let chanmon_cfgs = create_chanmon_cfgs(2);
6497         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6498         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6499         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6500
6501         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6502         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6503         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6504                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6505         check_added_monitors!(nodes[0], 1);
6506         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6507         updates.update_add_htlcs[0].cltv_expiry = 500000000;
6508         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6509
6510         assert!(nodes[1].node.list_channels().is_empty());
6511         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6512         assert_eq!(err_msg.data,"Remote provided CLTV expiry in seconds instead of block height");
6513         check_added_monitors!(nodes[1], 1);
6514         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6515 }
6516
6517 #[test]
6518 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
6519         //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
6520         // We test this by first testing that that repeated HTLCs pass commitment signature checks
6521         // after disconnect and that non-sequential htlc_ids result in a channel failure.
6522         let chanmon_cfgs = create_chanmon_cfgs(2);
6523         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6524         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6525         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6526
6527         create_announced_chan_between_nodes(&nodes, 0, 1);
6528         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6529         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6530                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6531         check_added_monitors!(nodes[0], 1);
6532         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6533         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6534
6535         //Disconnect and Reconnect
6536         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
6537         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
6538         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
6539                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
6540         }, true).unwrap();
6541         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
6542         assert_eq!(reestablish_1.len(), 1);
6543         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
6544                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
6545         }, false).unwrap();
6546         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
6547         assert_eq!(reestablish_2.len(), 1);
6548         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
6549         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
6550         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
6551         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
6552
6553         //Resend HTLC
6554         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6555         assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
6556         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
6557         check_added_monitors!(nodes[1], 1);
6558         let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6559
6560         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6561
6562         assert!(nodes[1].node.list_channels().is_empty());
6563         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6564         assert!(regex::Regex::new(r"Remote skipped HTLC ID \(skipped ID: \d+\)").unwrap().is_match(err_msg.data.as_str()));
6565         check_added_monitors!(nodes[1], 1);
6566         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6567 }
6568
6569 #[test]
6570 fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
6571         //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6572
6573         let chanmon_cfgs = create_chanmon_cfgs(2);
6574         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6575         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6576         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6577         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6578         let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6579         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6580                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6581
6582         check_added_monitors!(nodes[0], 1);
6583         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6584         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6585
6586         let update_msg = msgs::UpdateFulfillHTLC{
6587                 channel_id: chan.2,
6588                 htlc_id: 0,
6589                 payment_preimage: our_payment_preimage,
6590         };
6591
6592         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6593
6594         assert!(nodes[0].node.list_channels().is_empty());
6595         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6596         assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6597         check_added_monitors!(nodes[0], 1);
6598         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6599 }
6600
6601 #[test]
6602 fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
6603         //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6604
6605         let chanmon_cfgs = create_chanmon_cfgs(2);
6606         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6607         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6608         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6609         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6610
6611         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6612         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6613                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6614         check_added_monitors!(nodes[0], 1);
6615         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6616         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6617
6618         let update_msg = msgs::UpdateFailHTLC{
6619                 channel_id: chan.2,
6620                 htlc_id: 0,
6621                 reason: msgs::OnionErrorPacket { data: Vec::new()},
6622         };
6623
6624         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6625
6626         assert!(nodes[0].node.list_channels().is_empty());
6627         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6628         assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6629         check_added_monitors!(nodes[0], 1);
6630         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6631 }
6632
6633 #[test]
6634 fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
6635         //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions:     MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6636
6637         let chanmon_cfgs = create_chanmon_cfgs(2);
6638         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6639         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6640         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6641         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6642
6643         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6644         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6645                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6646         check_added_monitors!(nodes[0], 1);
6647         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6648         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6649         let update_msg = msgs::UpdateFailMalformedHTLC{
6650                 channel_id: chan.2,
6651                 htlc_id: 0,
6652                 sha256_of_onion: [1; 32],
6653                 failure_code: 0x8000,
6654         };
6655
6656         nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6657
6658         assert!(nodes[0].node.list_channels().is_empty());
6659         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6660         assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6661         check_added_monitors!(nodes[0], 1);
6662         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6663 }
6664
6665 #[test]
6666 fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
6667         //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
6668
6669         let chanmon_cfgs = create_chanmon_cfgs(2);
6670         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6671         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6672         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6673         create_announced_chan_between_nodes(&nodes, 0, 1);
6674
6675         let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6676
6677         nodes[1].node.claim_funds(our_payment_preimage);
6678         check_added_monitors!(nodes[1], 1);
6679         expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6680
6681         let events = nodes[1].node.get_and_clear_pending_msg_events();
6682         assert_eq!(events.len(), 1);
6683         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6684                 match events[0] {
6685                         MessageSendEvent::UpdateHTLCs { 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, .. } } => {
6686                                 assert!(update_add_htlcs.is_empty());
6687                                 assert_eq!(update_fulfill_htlcs.len(), 1);
6688                                 assert!(update_fail_htlcs.is_empty());
6689                                 assert!(update_fail_malformed_htlcs.is_empty());
6690                                 assert!(update_fee.is_none());
6691                                 update_fulfill_htlcs[0].clone()
6692                         },
6693                         _ => panic!("Unexpected event"),
6694                 }
6695         };
6696
6697         update_fulfill_msg.htlc_id = 1;
6698
6699         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6700
6701         assert!(nodes[0].node.list_channels().is_empty());
6702         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6703         assert_eq!(err_msg.data, "Remote tried to fulfill/fail an HTLC we couldn't find");
6704         check_added_monitors!(nodes[0], 1);
6705         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6706 }
6707
6708 #[test]
6709 fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
6710         //BOLT 2 Requirement: A receiving node: if the payment_preimage value in update_fulfill_htlc doesn't SHA256 hash to the corresponding HTLC payment_hash MUST fail the channel.
6711
6712         let chanmon_cfgs = create_chanmon_cfgs(2);
6713         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6714         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6715         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6716         create_announced_chan_between_nodes(&nodes, 0, 1);
6717
6718         let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6719
6720         nodes[1].node.claim_funds(our_payment_preimage);
6721         check_added_monitors!(nodes[1], 1);
6722         expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6723
6724         let events = nodes[1].node.get_and_clear_pending_msg_events();
6725         assert_eq!(events.len(), 1);
6726         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6727                 match events[0] {
6728                         MessageSendEvent::UpdateHTLCs { 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, .. } } => {
6729                                 assert!(update_add_htlcs.is_empty());
6730                                 assert_eq!(update_fulfill_htlcs.len(), 1);
6731                                 assert!(update_fail_htlcs.is_empty());
6732                                 assert!(update_fail_malformed_htlcs.is_empty());
6733                                 assert!(update_fee.is_none());
6734                                 update_fulfill_htlcs[0].clone()
6735                         },
6736                         _ => panic!("Unexpected event"),
6737                 }
6738         };
6739
6740         update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
6741
6742         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6743
6744         assert!(nodes[0].node.list_channels().is_empty());
6745         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6746         assert!(regex::Regex::new(r"Remote tried to fulfill HTLC \(\d+\) with an incorrect preimage").unwrap().is_match(err_msg.data.as_str()));
6747         check_added_monitors!(nodes[0], 1);
6748         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6749 }
6750
6751 #[test]
6752 fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
6753         //BOLT 2 Requirement: A receiving node: if the BADONION bit in failure_code is not set for update_fail_malformed_htlc MUST fail the channel.
6754
6755         let chanmon_cfgs = create_chanmon_cfgs(2);
6756         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6757         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6758         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6759         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6760
6761         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6762         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6763                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6764         check_added_monitors!(nodes[0], 1);
6765
6766         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6767         updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6768
6769         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6770         check_added_monitors!(nodes[1], 0);
6771         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
6772
6773         let events = nodes[1].node.get_and_clear_pending_msg_events();
6774
6775         let mut update_msg: msgs::UpdateFailMalformedHTLC = {
6776                 match events[0] {
6777                         MessageSendEvent::UpdateHTLCs { 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, .. } } => {
6778                                 assert!(update_add_htlcs.is_empty());
6779                                 assert!(update_fulfill_htlcs.is_empty());
6780                                 assert!(update_fail_htlcs.is_empty());
6781                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6782                                 assert!(update_fee.is_none());
6783                                 update_fail_malformed_htlcs[0].clone()
6784                         },
6785                         _ => panic!("Unexpected event"),
6786                 }
6787         };
6788         update_msg.failure_code &= !0x8000;
6789         nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6790
6791         assert!(nodes[0].node.list_channels().is_empty());
6792         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6793         assert_eq!(err_msg.data, "Got update_fail_malformed_htlc with BADONION not set");
6794         check_added_monitors!(nodes[0], 1);
6795         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 1000000);
6796 }
6797
6798 #[test]
6799 fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
6800         //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
6801         //    * MUST return an error in the update_fail_htlc sent to the link which originally sent the HTLC, using the failure_code given and setting the data to sha256_of_onion.
6802
6803         let chanmon_cfgs = create_chanmon_cfgs(3);
6804         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6805         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6806         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6807         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6808         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000);
6809
6810         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
6811
6812         //First hop
6813         let mut payment_event = {
6814                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6815                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6816                 check_added_monitors!(nodes[0], 1);
6817                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6818                 assert_eq!(events.len(), 1);
6819                 SendEvent::from_event(events.remove(0))
6820         };
6821         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6822         check_added_monitors!(nodes[1], 0);
6823         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6824         expect_pending_htlcs_forwardable!(nodes[1]);
6825         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
6826         assert_eq!(events_2.len(), 1);
6827         check_added_monitors!(nodes[1], 1);
6828         payment_event = SendEvent::from_event(events_2.remove(0));
6829         assert_eq!(payment_event.msgs.len(), 1);
6830
6831         //Second Hop
6832         payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6833         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6834         check_added_monitors!(nodes[2], 0);
6835         commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6836
6837         let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6838         assert_eq!(events_3.len(), 1);
6839         let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
6840                 match events_3[0] {
6841                         MessageSendEvent::UpdateHTLCs { 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 } } => {
6842                                 assert!(update_add_htlcs.is_empty());
6843                                 assert!(update_fulfill_htlcs.is_empty());
6844                                 assert!(update_fail_htlcs.is_empty());
6845                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6846                                 assert!(update_fee.is_none());
6847                                 (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
6848                         },
6849                         _ => panic!("Unexpected event"),
6850                 }
6851         };
6852
6853         nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0);
6854
6855         check_added_monitors!(nodes[1], 0);
6856         commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
6857         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
6858         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6859         assert_eq!(events_4.len(), 1);
6860
6861         //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
6862         match events_4[0] {
6863                 MessageSendEvent::UpdateHTLCs { 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, .. } } => {
6864                         assert!(update_add_htlcs.is_empty());
6865                         assert!(update_fulfill_htlcs.is_empty());
6866                         assert_eq!(update_fail_htlcs.len(), 1);
6867                         assert!(update_fail_malformed_htlcs.is_empty());
6868                         assert!(update_fee.is_none());
6869                 },
6870                 _ => panic!("Unexpected event"),
6871         };
6872
6873         check_added_monitors!(nodes[1], 1);
6874 }
6875
6876 #[test]
6877 fn test_channel_failed_after_message_with_badonion_node_perm_bits_set() {
6878         let chanmon_cfgs = create_chanmon_cfgs(3);
6879         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6880         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6881         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6882         create_announced_chan_between_nodes(&nodes, 0, 1);
6883         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
6884
6885         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
6886
6887         // First hop
6888         let mut payment_event = {
6889                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6890                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6891                 check_added_monitors!(nodes[0], 1);
6892                 SendEvent::from_node(&nodes[0])
6893         };
6894
6895         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6896         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6897         expect_pending_htlcs_forwardable!(nodes[1]);
6898         check_added_monitors!(nodes[1], 1);
6899         payment_event = SendEvent::from_node(&nodes[1]);
6900         assert_eq!(payment_event.msgs.len(), 1);
6901
6902         // Second Hop
6903         payment_event.msgs[0].onion_routing_packet.version = 1; // Trigger an invalid_onion_version error
6904         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6905         check_added_monitors!(nodes[2], 0);
6906         commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6907
6908         let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6909         assert_eq!(events_3.len(), 1);
6910         match events_3[0] {
6911                 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6912                         let mut update_msg = updates.update_fail_malformed_htlcs[0].clone();
6913                         // Set the NODE bit (BADONION and PERM already set in invalid_onion_version error)
6914                         update_msg.failure_code |= 0x2000;
6915
6916                         nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg);
6917                         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true);
6918                 },
6919                 _ => panic!("Unexpected event"),
6920         }
6921
6922         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
6923                 vec![HTLCDestination::NextHopChannel {
6924                         node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
6925         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6926         assert_eq!(events_4.len(), 1);
6927         check_added_monitors!(nodes[1], 1);
6928
6929         match events_4[0] {
6930                 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6931                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
6932                         commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
6933                 },
6934                 _ => panic!("Unexpected event"),
6935         }
6936
6937         let events_5 = nodes[0].node.get_and_clear_pending_events();
6938         assert_eq!(events_5.len(), 2);
6939
6940         // Expect a PaymentPathFailed event with a ChannelFailure network update for the channel between
6941         // the node originating the error to its next hop.
6942         match events_5[0] {
6943                 Event::PaymentPathFailed { error_code, failure: PathFailure::OnPath { network_update: Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }) }, ..
6944                 } => {
6945                         assert_eq!(short_channel_id, chan_2.0.contents.short_channel_id);
6946                         assert!(is_permanent);
6947                         assert_eq!(error_code, Some(0x8000|0x4000|0x2000|4));
6948                 },
6949                 _ => panic!("Unexpected event"),
6950         }
6951         match events_5[1] {
6952                 Event::PaymentFailed { payment_hash, .. } => {
6953                         assert_eq!(payment_hash, our_payment_hash);
6954                 },
6955                 _ => panic!("Unexpected event"),
6956         }
6957
6958         // TODO: Test actual removal of channel from NetworkGraph when it's implemented.
6959 }
6960
6961 fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
6962         // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
6963         // We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
6964         // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
6965
6966         let mut chanmon_cfgs = create_chanmon_cfgs(2);
6967         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
6968         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6969         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6970         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6971         let chan =create_announced_chan_between_nodes(&nodes, 0, 1);
6972
6973         let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
6974                 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
6975
6976         // We route 2 dust-HTLCs between A and B
6977         let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
6978         let (_, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
6979         route_payment(&nodes[0], &[&nodes[1]], 1000000);
6980
6981         // Cache one local commitment tx as previous
6982         let as_prev_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
6983
6984         // Fail one HTLC to prune it in the will-be-latest-local commitment tx
6985         nodes[1].node.fail_htlc_backwards(&payment_hash_2);
6986         check_added_monitors!(nodes[1], 0);
6987         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
6988         check_added_monitors!(nodes[1], 1);
6989
6990         let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6991         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]);
6992         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed);
6993         check_added_monitors!(nodes[0], 1);
6994
6995         // Cache one local commitment tx as lastest
6996         let as_last_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
6997
6998         let events = nodes[0].node.get_and_clear_pending_msg_events();
6999         match events[0] {
7000                 MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
7001                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7002                 },
7003                 _ => panic!("Unexpected event"),
7004         }
7005         match events[1] {
7006                 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
7007                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7008                 },
7009                 _ => panic!("Unexpected event"),
7010         }
7011
7012         assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
7013         // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
7014         if announce_latest {
7015                 mine_transaction(&nodes[0], &as_last_commitment_tx[0]);
7016         } else {
7017                 mine_transaction(&nodes[0], &as_prev_commitment_tx[0]);
7018         }
7019
7020         check_closed_broadcast!(nodes[0], true);
7021         check_added_monitors!(nodes[0], 1);
7022         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7023
7024         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7025         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7026         let events = nodes[0].node.get_and_clear_pending_events();
7027         // Only 2 PaymentPathFailed events should show up, over-dust HTLC has to be failed by timeout tx
7028         assert_eq!(events.len(), 4);
7029         let mut first_failed = false;
7030         for event in events {
7031                 match event {
7032                         Event::PaymentPathFailed { payment_hash, .. } => {
7033                                 if payment_hash == payment_hash_1 {
7034                                         assert!(!first_failed);
7035                                         first_failed = true;
7036                                 } else {
7037                                         assert_eq!(payment_hash, payment_hash_2);
7038                                 }
7039                         },
7040                         Event::PaymentFailed { .. } => {}
7041                         _ => panic!("Unexpected event"),
7042                 }
7043         }
7044 }
7045
7046 #[test]
7047 fn test_failure_delay_dust_htlc_local_commitment() {
7048         do_test_failure_delay_dust_htlc_local_commitment(true);
7049         do_test_failure_delay_dust_htlc_local_commitment(false);
7050 }
7051
7052 fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
7053         // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
7054         // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
7055         // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
7056         // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
7057         // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
7058         // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
7059
7060         let chanmon_cfgs = create_chanmon_cfgs(3);
7061         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7062         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7063         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7064         let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
7065
7066         let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
7067                 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
7068
7069         let (_payment_preimage_1, dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7070         let (_payment_preimage_2, non_dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7071
7072         let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7073         let bs_commitment_tx = get_local_commitment_txn!(nodes[1], chan.2);
7074
7075         // We revoked bs_commitment_tx
7076         if revoked {
7077                 let (payment_preimage_3, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7078                 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
7079         }
7080
7081         let mut timeout_tx = Vec::new();
7082         if local {
7083                 // We fail dust-HTLC 1 by broadcast of local commitment tx
7084                 mine_transaction(&nodes[0], &as_commitment_tx[0]);
7085                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7086                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7087                 expect_payment_failed!(nodes[0], dust_hash, false);
7088
7089                 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS - ANTI_REORG_DELAY);
7090                 check_closed_broadcast!(nodes[0], true);
7091                 check_added_monitors!(nodes[0], 1);
7092                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7093                 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
7094                 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7095                 // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
7096                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7097                 mine_transaction(&nodes[0], &timeout_tx[0]);
7098                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7099                 expect_payment_failed!(nodes[0], non_dust_hash, false);
7100         } else {
7101                 // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
7102                 mine_transaction(&nodes[0], &bs_commitment_tx[0]);
7103                 check_closed_broadcast!(nodes[0], true);
7104                 check_added_monitors!(nodes[0], 1);
7105                 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7106                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7107
7108                 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7109                 timeout_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..)
7110                         .filter(|tx| tx.input[0].previous_output.txid == bs_commitment_tx[0].txid()).collect();
7111                 check_spends!(timeout_tx[0], bs_commitment_tx[0]);
7112                 // For both a revoked or non-revoked commitment transaction, after ANTI_REORG_DELAY the
7113                 // dust HTLC should have been failed.
7114                 expect_payment_failed!(nodes[0], dust_hash, false);
7115
7116                 if !revoked {
7117                         assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7118                 } else {
7119                         assert_eq!(timeout_tx[0].lock_time.0, 11);
7120                 }
7121                 // We fail non-dust-HTLC 2 by broadcast of local timeout/revocation-claim tx
7122                 mine_transaction(&nodes[0], &timeout_tx[0]);
7123                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7124                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7125                 expect_payment_failed!(nodes[0], non_dust_hash, false);
7126         }
7127 }
7128
7129 #[test]
7130 fn test_sweep_outbound_htlc_failure_update() {
7131         do_test_sweep_outbound_htlc_failure_update(false, true);
7132         do_test_sweep_outbound_htlc_failure_update(false, false);
7133         do_test_sweep_outbound_htlc_failure_update(true, false);
7134 }
7135
7136 #[test]
7137 fn test_user_configurable_csv_delay() {
7138         // We test our channel constructors yield errors when we pass them absurd csv delay
7139
7140         let mut low_our_to_self_config = UserConfig::default();
7141         low_our_to_self_config.channel_handshake_config.our_to_self_delay = 6;
7142         let mut high_their_to_self_config = UserConfig::default();
7143         high_their_to_self_config.channel_handshake_limits.their_to_self_delay = 100;
7144         let user_cfgs = [Some(high_their_to_self_config.clone()), None];
7145         let chanmon_cfgs = create_chanmon_cfgs(2);
7146         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7147         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
7148         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7149
7150         // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
7151         if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7152                 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
7153                 &low_our_to_self_config, 0, 42)
7154         {
7155                 match error {
7156                         APIError::APIMisuseError { err } => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },
7157                         _ => panic!("Unexpected event"),
7158                 }
7159         } else { assert!(false) }
7160
7161         // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in InboundV1Channel::new()
7162         nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7163         let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7164         open_channel.to_self_delay = 200;
7165         if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7166                 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
7167                 &low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7168         {
7169                 match error {
7170                         ChannelError::Close(err) => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str()));  },
7171                         _ => panic!("Unexpected event"),
7172                 }
7173         } else { assert!(false); }
7174
7175         // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
7176         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7177         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
7178         let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7179         accept_channel.to_self_delay = 200;
7180         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
7181         let reason_msg;
7182         if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[0] {
7183                 match action {
7184                         &ErrorAction::SendErrorMessage { ref msg } => {
7185                                 assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(msg.data.as_str()));
7186                                 reason_msg = msg.data.clone();
7187                         },
7188                         _ => { panic!(); }
7189                 }
7190         } else { panic!(); }
7191         check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: reason_msg }, [nodes[1].node.get_our_node_id()], 1000000);
7192
7193         // We test msg.to_self_delay <= config.their_to_self_delay is enforced in InboundV1Channel::new()
7194         nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7195         let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7196         open_channel.to_self_delay = 200;
7197         if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7198                 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
7199                 &high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7200         {
7201                 match error {
7202                         ChannelError::Close(err) => { assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(err.as_str())); },
7203                         _ => panic!("Unexpected event"),
7204                 }
7205         } else { assert!(false); }
7206 }
7207
7208 #[test]
7209 fn test_check_htlc_underpaying() {
7210         // Send payment through A -> B but A is maliciously
7211         // sending a probe payment (i.e less than expected value0
7212         // to B, B should refuse payment.
7213
7214         let chanmon_cfgs = create_chanmon_cfgs(2);
7215         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7216         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7217         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7218
7219         // Create some initial channels
7220         create_announced_chan_between_nodes(&nodes, 0, 1);
7221
7222         let scorer = test_utils::TestScorer::new();
7223         let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7224         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(),
7225                 TEST_FINAL_CLTV).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
7226         let route_params = RouteParameters::from_payment_params_and_value(payment_params, 10_000);
7227         let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(),
7228                 None, nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7229         let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
7230         let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, None).unwrap();
7231         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
7232                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
7233         check_added_monitors!(nodes[0], 1);
7234
7235         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
7236         assert_eq!(events.len(), 1);
7237         let mut payment_event = SendEvent::from_event(events.pop().unwrap());
7238         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7239         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7240
7241         // Note that we first have to wait a random delay before processing the receipt of the HTLC,
7242         // and then will wait a second random delay before failing the HTLC back:
7243         expect_pending_htlcs_forwardable!(nodes[1]);
7244         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
7245
7246         // Node 3 is expecting payment of 100_000 but received 10_000,
7247         // it should fail htlc like we didn't know the preimage.
7248         nodes[1].node.process_pending_htlc_forwards();
7249
7250         let events = nodes[1].node.get_and_clear_pending_msg_events();
7251         assert_eq!(events.len(), 1);
7252         let (update_fail_htlc, commitment_signed) = match events[0] {
7253                 MessageSendEvent::UpdateHTLCs { 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 } } => {
7254                         assert!(update_add_htlcs.is_empty());
7255                         assert!(update_fulfill_htlcs.is_empty());
7256                         assert_eq!(update_fail_htlcs.len(), 1);
7257                         assert!(update_fail_malformed_htlcs.is_empty());
7258                         assert!(update_fee.is_none());
7259                         (update_fail_htlcs[0].clone(), commitment_signed)
7260                 },
7261                 _ => panic!("Unexpected event"),
7262         };
7263         check_added_monitors!(nodes[1], 1);
7264
7265         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlc);
7266         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
7267
7268         // 10_000 msat as u64, followed by a height of CHAN_CONFIRM_DEPTH as u32
7269         let mut expected_failure_data = (10_000 as u64).to_be_bytes().to_vec();
7270         expected_failure_data.extend_from_slice(&CHAN_CONFIRM_DEPTH.to_be_bytes());
7271         expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000|15, &expected_failure_data[..]);
7272 }
7273
7274 #[test]
7275 fn test_announce_disable_channels() {
7276         // Create 2 channels between A and B. Disconnect B. Call timer_tick_occurred and check for generated
7277         // ChannelUpdate. Reconnect B, reestablish and check there is non-generated ChannelUpdate.
7278
7279         let chanmon_cfgs = create_chanmon_cfgs(2);
7280         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7281         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7282         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7283
7284         create_announced_chan_between_nodes(&nodes, 0, 1);
7285         create_announced_chan_between_nodes(&nodes, 1, 0);
7286         create_announced_chan_between_nodes(&nodes, 0, 1);
7287
7288         // Disconnect peers
7289         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
7290         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
7291
7292         for _ in 0..DISABLE_GOSSIP_TICKS + 1 {
7293                 nodes[0].node.timer_tick_occurred();
7294         }
7295         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7296         assert_eq!(msg_events.len(), 3);
7297         let mut chans_disabled = HashMap::new();
7298         for e in msg_events {
7299                 match e {
7300                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7301                                 assert_eq!(msg.contents.flags & (1<<1), 1<<1); // The "channel disabled" bit should be set
7302                                 // Check that each channel gets updated exactly once
7303                                 if chans_disabled.insert(msg.contents.short_channel_id, msg.contents.timestamp).is_some() {
7304                                         panic!("Generated ChannelUpdate for wrong chan!");
7305                                 }
7306                         },
7307                         _ => panic!("Unexpected event"),
7308                 }
7309         }
7310         // Reconnect peers
7311         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
7312                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
7313         }, true).unwrap();
7314         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
7315         assert_eq!(reestablish_1.len(), 3);
7316         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
7317                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
7318         }, false).unwrap();
7319         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
7320         assert_eq!(reestablish_2.len(), 3);
7321
7322         // Reestablish chan_1
7323         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
7324         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7325         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
7326         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7327         // Reestablish chan_2
7328         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[1]);
7329         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7330         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[1]);
7331         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7332         // Reestablish chan_3
7333         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[2]);
7334         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7335         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[2]);
7336         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7337
7338         for _ in 0..ENABLE_GOSSIP_TICKS {
7339                 nodes[0].node.timer_tick_occurred();
7340         }
7341         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7342         nodes[0].node.timer_tick_occurred();
7343         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7344         assert_eq!(msg_events.len(), 3);
7345         for e in msg_events {
7346                 match e {
7347                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7348                                 assert_eq!(msg.contents.flags & (1<<1), 0); // The "channel disabled" bit should be off
7349                                 match chans_disabled.remove(&msg.contents.short_channel_id) {
7350                                         // Each update should have a higher timestamp than the previous one, replacing
7351                                         // the old one.
7352                                         Some(prev_timestamp) => assert!(msg.contents.timestamp > prev_timestamp),
7353                                         None => panic!("Generated ChannelUpdate for wrong chan!"),
7354                                 }
7355                         },
7356                         _ => panic!("Unexpected event"),
7357                 }
7358         }
7359         // Check that each channel gets updated exactly once
7360         assert!(chans_disabled.is_empty());
7361 }
7362
7363 #[test]
7364 fn test_bump_penalty_txn_on_revoked_commitment() {
7365         // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
7366         // we're able to claim outputs on revoked commitment transaction before timelocks expiration
7367
7368         let chanmon_cfgs = create_chanmon_cfgs(2);
7369         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7370         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7371         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7372
7373         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7374
7375         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
7376         let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 30)
7377                 .with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
7378         let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000);
7379         send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
7380
7381         let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
7382         // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7383         assert_eq!(revoked_txn[0].output.len(), 4);
7384         assert_eq!(revoked_txn[0].input.len(), 1);
7385         assert_eq!(revoked_txn[0].input[0].previous_output.txid, chan.3.txid());
7386         let revoked_txid = revoked_txn[0].txid();
7387
7388         let mut penalty_sum = 0;
7389         for outp in revoked_txn[0].output.iter() {
7390                 if outp.script_pubkey.is_v0_p2wsh() {
7391                         penalty_sum += outp.value;
7392                 }
7393         }
7394
7395         // Connect blocks to change height_timer range to see if we use right soonest_timelock
7396         let header_114 = connect_blocks(&nodes[1], 14);
7397
7398         // Actually revoke tx by claiming a HTLC
7399         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7400         connect_block(&nodes[1], &create_dummy_block(header_114, 42, vec![revoked_txn[0].clone()]));
7401         check_added_monitors!(nodes[1], 1);
7402
7403         // One or more justice tx should have been broadcast, check it
7404         let penalty_1;
7405         let feerate_1;
7406         {
7407                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7408                 assert_eq!(node_txn.len(), 1); // justice tx (broadcasted from ChannelMonitor)
7409                 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7410                 assert_eq!(node_txn[0].output.len(), 1);
7411                 check_spends!(node_txn[0], revoked_txn[0]);
7412                 let fee_1 = penalty_sum - node_txn[0].output[0].value;
7413                 feerate_1 = fee_1 * 1000 / node_txn[0].weight() as u64;
7414                 penalty_1 = node_txn[0].txid();
7415                 node_txn.clear();
7416         };
7417
7418         // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it
7419         connect_blocks(&nodes[1], 15);
7420         let mut penalty_2 = penalty_1;
7421         let mut feerate_2 = 0;
7422         {
7423                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7424                 assert_eq!(node_txn.len(), 1);
7425                 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7426                         assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7427                         assert_eq!(node_txn[0].output.len(), 1);
7428                         check_spends!(node_txn[0], revoked_txn[0]);
7429                         penalty_2 = node_txn[0].txid();
7430                         // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7431                         assert_ne!(penalty_2, penalty_1);
7432                         let fee_2 = penalty_sum - node_txn[0].output[0].value;
7433                         feerate_2 = fee_2 * 1000 / node_txn[0].weight() as u64;
7434                         // Verify 25% bump heuristic
7435                         assert!(feerate_2 * 100 >= feerate_1 * 125);
7436                         node_txn.clear();
7437                 }
7438         }
7439         assert_ne!(feerate_2, 0);
7440
7441         // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it
7442         connect_blocks(&nodes[1], 1);
7443         let penalty_3;
7444         let mut feerate_3 = 0;
7445         {
7446                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7447                 assert_eq!(node_txn.len(), 1);
7448                 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7449                         assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7450                         assert_eq!(node_txn[0].output.len(), 1);
7451                         check_spends!(node_txn[0], revoked_txn[0]);
7452                         penalty_3 = node_txn[0].txid();
7453                         // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7454                         assert_ne!(penalty_3, penalty_2);
7455                         let fee_3 = penalty_sum - node_txn[0].output[0].value;
7456                         feerate_3 = fee_3 * 1000 / node_txn[0].weight() as u64;
7457                         // Verify 25% bump heuristic
7458                         assert!(feerate_3 * 100 >= feerate_2 * 125);
7459                         node_txn.clear();
7460                 }
7461         }
7462         assert_ne!(feerate_3, 0);
7463
7464         nodes[1].node.get_and_clear_pending_events();
7465         nodes[1].node.get_and_clear_pending_msg_events();
7466 }
7467
7468 #[test]
7469 fn test_bump_penalty_txn_on_revoked_htlcs() {
7470         // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure
7471         // we're able to claim outputs on revoked HTLC transactions before timelocks expiration
7472
7473         let mut chanmon_cfgs = create_chanmon_cfgs(2);
7474         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
7475         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7476         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7477         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7478
7479         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7480         // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
7481         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 50).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
7482         let scorer = test_utils::TestScorer::new();
7483         let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7484         let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7485         let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(), None,
7486                 nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7487         let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
7488         let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50).with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
7489         let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7490         let route = get_route(&nodes[1].node.get_our_node_id(), &route_params, &nodes[1].network_graph.read_only(), None,
7491                 nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7492         send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
7493
7494         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7495         assert_eq!(revoked_local_txn[0].input.len(), 1);
7496         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7497
7498         // Revoke local commitment tx
7499         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7500
7501         // B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx
7502         connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone()]));
7503         check_closed_broadcast!(nodes[1], true);
7504         check_added_monitors!(nodes[1], 1);
7505         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 1000000);
7506         connect_blocks(&nodes[1], 50); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
7507
7508         let revoked_htlc_txn = {
7509                 let txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
7510                 assert_eq!(txn.len(), 2);
7511
7512                 assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7513                 assert_eq!(txn[0].input.len(), 1);
7514                 check_spends!(txn[0], revoked_local_txn[0]);
7515
7516                 assert_eq!(txn[1].input.len(), 1);
7517                 assert_eq!(txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7518                 assert_eq!(txn[1].output.len(), 1);
7519                 check_spends!(txn[1], revoked_local_txn[0]);
7520
7521                 txn
7522         };
7523
7524         // Broadcast set of revoked txn on A
7525         let hash_128 = connect_blocks(&nodes[0], 40);
7526         let block_11 = create_dummy_block(hash_128, 42, vec![revoked_local_txn[0].clone()]);
7527         connect_block(&nodes[0], &block_11);
7528         let block_129 = create_dummy_block(block_11.block_hash(), 42, vec![revoked_htlc_txn[0].clone(), revoked_htlc_txn[1].clone()]);
7529         connect_block(&nodes[0], &block_129);
7530         let events = nodes[0].node.get_and_clear_pending_events();
7531         expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
7532         match events.last().unwrap() {
7533                 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
7534                 _ => panic!("Unexpected event"),
7535         }
7536         let first;
7537         let feerate_1;
7538         let penalty_txn;
7539         {
7540                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7541                 assert_eq!(node_txn.len(), 4); // 3 penalty txn on revoked commitment tx + 1 penalty tnx on revoked HTLC txn
7542                 // Verify claim tx are spending revoked HTLC txn
7543
7544                 // node_txn 0-2 each spend a separate revoked output from revoked_local_txn[0]
7545                 // Note that node_txn[0] and node_txn[1] are bogus - they double spend the revoked_htlc_txn
7546                 // which are included in the same block (they are broadcasted because we scan the
7547                 // transactions linearly and generate claims as we go, they likely should be removed in the
7548                 // future).
7549                 assert_eq!(node_txn[0].input.len(), 1);
7550                 check_spends!(node_txn[0], revoked_local_txn[0]);
7551                 assert_eq!(node_txn[1].input.len(), 1);
7552                 check_spends!(node_txn[1], revoked_local_txn[0]);
7553                 assert_eq!(node_txn[2].input.len(), 1);
7554                 check_spends!(node_txn[2], revoked_local_txn[0]);
7555
7556                 // Each of the three justice transactions claim a separate (single) output of the three
7557                 // available, which we check here:
7558                 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
7559                 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
7560                 assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
7561
7562                 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[1].input[0].previous_output);
7563                 assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
7564
7565                 // node_txn[3] spends the revoked outputs from the revoked_htlc_txn (which only have one
7566                 // output, checked above).
7567                 assert_eq!(node_txn[3].input.len(), 2);
7568                 assert_eq!(node_txn[3].output.len(), 1);
7569                 check_spends!(node_txn[3], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7570
7571                 first = node_txn[3].txid();
7572                 // Store both feerates for later comparison
7573                 let fee_1 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[3].output[0].value;
7574                 feerate_1 = fee_1 * 1000 / node_txn[3].weight() as u64;
7575                 penalty_txn = vec![node_txn[2].clone()];
7576                 node_txn.clear();
7577         }
7578
7579         // Connect one more block to see if bumped penalty are issued for HTLC txn
7580         let block_130 = create_dummy_block(block_129.block_hash(), 42, penalty_txn);
7581         connect_block(&nodes[0], &block_130);
7582         let block_131 = create_dummy_block(block_130.block_hash(), 42, Vec::new());
7583         connect_block(&nodes[0], &block_131);
7584
7585         // Few more blocks to confirm penalty txn
7586         connect_blocks(&nodes[0], 4);
7587         assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
7588         let header_144 = connect_blocks(&nodes[0], 9);
7589         let node_txn = {
7590                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7591                 assert_eq!(node_txn.len(), 1);
7592
7593                 assert_eq!(node_txn[0].input.len(), 2);
7594                 check_spends!(node_txn[0], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7595                 // Verify bumped tx is different and 25% bump heuristic
7596                 assert_ne!(first, node_txn[0].txid());
7597                 let fee_2 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[0].output[0].value;
7598                 let feerate_2 = fee_2 * 1000 / node_txn[0].weight() as u64;
7599                 assert!(feerate_2 * 100 > feerate_1 * 125);
7600                 let txn = vec![node_txn[0].clone()];
7601                 node_txn.clear();
7602                 txn
7603         };
7604         // Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
7605         connect_block(&nodes[0], &create_dummy_block(header_144, 42, node_txn));
7606         connect_blocks(&nodes[0], 20);
7607         {
7608                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7609                 // We verify than no new transaction has been broadcast because previously
7610                 // we were buggy on this exact behavior by not tracking for monitoring remote HTLC outputs (see #411)
7611                 // which means we wouldn't see a spend of them by a justice tx and bumped justice tx
7612                 // were generated forever instead of safe cleaning after confirmation and ANTI_REORG_SAFE_DELAY blocks.
7613                 // Enforce spending of revoked htlc output by claiming transaction remove request as expected and dry
7614                 // up bumped justice generation.
7615                 assert_eq!(node_txn.len(), 0);
7616                 node_txn.clear();
7617         }
7618         check_closed_broadcast!(nodes[0], true);
7619         check_added_monitors!(nodes[0], 1);
7620 }
7621
7622 #[test]
7623 fn test_bump_penalty_txn_on_remote_commitment() {
7624         // In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure
7625         // we're able to claim outputs on remote commitment transaction before timelocks expiration
7626
7627         // Create 2 HTLCs
7628         // Provide preimage for one
7629         // Check aggregation
7630
7631         let chanmon_cfgs = create_chanmon_cfgs(2);
7632         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7633         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7634         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7635
7636         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7637         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
7638         route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0;
7639
7640         // Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7641         let remote_txn = get_local_commitment_txn!(nodes[0], chan.2);
7642         assert_eq!(remote_txn[0].output.len(), 4);
7643         assert_eq!(remote_txn[0].input.len(), 1);
7644         assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid());
7645
7646         // Claim a HTLC without revocation (provide B monitor with preimage)
7647         nodes[1].node.claim_funds(payment_preimage);
7648         expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
7649         mine_transaction(&nodes[1], &remote_txn[0]);
7650         check_added_monitors!(nodes[1], 2);
7651         connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7652
7653         // One or more claim tx should have been broadcast, check it
7654         let timeout;
7655         let preimage;
7656         let preimage_bump;
7657         let feerate_timeout;
7658         let feerate_preimage;
7659         {
7660                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7661                 // 3 transactions including:
7662                 //   preimage and timeout sweeps from remote commitment + preimage sweep bump
7663                 assert_eq!(node_txn.len(), 3);
7664                 assert_eq!(node_txn[0].input.len(), 1);
7665                 assert_eq!(node_txn[1].input.len(), 1);
7666                 assert_eq!(node_txn[2].input.len(), 1);
7667                 check_spends!(node_txn[0], remote_txn[0]);
7668                 check_spends!(node_txn[1], remote_txn[0]);
7669                 check_spends!(node_txn[2], remote_txn[0]);
7670
7671                 preimage = node_txn[0].txid();
7672                 let index = node_txn[0].input[0].previous_output.vout;
7673                 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7674                 feerate_preimage = fee * 1000 / node_txn[0].weight() as u64;
7675
7676                 let (preimage_bump_tx, timeout_tx) = if node_txn[2].input[0].previous_output == node_txn[0].input[0].previous_output {
7677                         (node_txn[2].clone(), node_txn[1].clone())
7678                 } else {
7679                         (node_txn[1].clone(), node_txn[2].clone())
7680                 };
7681
7682                 preimage_bump = preimage_bump_tx;
7683                 check_spends!(preimage_bump, remote_txn[0]);
7684                 assert_eq!(node_txn[0].input[0].previous_output, preimage_bump.input[0].previous_output);
7685
7686                 timeout = timeout_tx.txid();
7687                 let index = timeout_tx.input[0].previous_output.vout;
7688                 let fee = remote_txn[0].output[index as usize].value - timeout_tx.output[0].value;
7689                 feerate_timeout = fee * 1000 / timeout_tx.weight() as u64;
7690
7691                 node_txn.clear();
7692         };
7693         assert_ne!(feerate_timeout, 0);
7694         assert_ne!(feerate_preimage, 0);
7695
7696         // After exhaustion of height timer, new bumped claim txn should have been broadcast, check it
7697         connect_blocks(&nodes[1], 1);
7698         {
7699                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7700                 assert_eq!(node_txn.len(), 1);
7701                 assert_eq!(node_txn[0].input.len(), 1);
7702                 assert_eq!(preimage_bump.input.len(), 1);
7703                 check_spends!(node_txn[0], remote_txn[0]);
7704                 check_spends!(preimage_bump, remote_txn[0]);
7705
7706                 let index = preimage_bump.input[0].previous_output.vout;
7707                 let fee = remote_txn[0].output[index as usize].value - preimage_bump.output[0].value;
7708                 let new_feerate = fee * 1000 / preimage_bump.weight() as u64;
7709                 assert!(new_feerate * 100 > feerate_timeout * 125);
7710                 assert_ne!(timeout, preimage_bump.txid());
7711
7712                 let index = node_txn[0].input[0].previous_output.vout;
7713                 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7714                 let new_feerate = fee * 1000 / node_txn[0].weight() as u64;
7715                 assert!(new_feerate * 100 > feerate_preimage * 125);
7716                 assert_ne!(preimage, node_txn[0].txid());
7717
7718                 node_txn.clear();
7719         }
7720
7721         nodes[1].node.get_and_clear_pending_events();
7722         nodes[1].node.get_and_clear_pending_msg_events();
7723 }
7724
7725 #[test]
7726 fn test_counterparty_raa_skip_no_crash() {
7727         // Previously, if our counterparty sent two RAAs in a row without us having provided a
7728         // commitment transaction, we would have happily carried on and provided them the next
7729         // commitment transaction based on one RAA forward. This would probably eventually have led to
7730         // channel closure, but it would not have resulted in funds loss. Still, our
7731         // TestChannelSigner would have panicked as it doesn't like jumps into the future. Here, we
7732         // check simply that the channel is closed in response to such an RAA, but don't check whether
7733         // we decide to punish our counterparty for revoking their funds (as we don't currently
7734         // implement that).
7735         let chanmon_cfgs = create_chanmon_cfgs(2);
7736         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7737         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7738         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7739         let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
7740
7741         let per_commitment_secret;
7742         let next_per_commitment_point;
7743         {
7744                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
7745                 let mut guard = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
7746                 let keys = guard.channel_by_id.get_mut(&channel_id).map(
7747                         |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
7748                 ).flatten().unwrap().get_signer();
7749
7750                 const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
7751
7752                 // Make signer believe we got a counterparty signature, so that it allows the revocation
7753                 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7754                 per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
7755
7756                 // Must revoke without gaps
7757                 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7758                 keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
7759
7760                 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7761                 next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
7762                         &SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
7763         }
7764
7765         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
7766                 &msgs::RevokeAndACK {
7767                         channel_id,
7768                         per_commitment_secret,
7769                         next_per_commitment_point,
7770                         #[cfg(taproot)]
7771                         next_local_nonce: None,
7772                 });
7773         assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
7774         check_added_monitors!(nodes[1], 1);
7775         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() }
7776                 , [nodes[0].node.get_our_node_id()], 100000);
7777 }
7778
7779 #[test]
7780 fn test_bump_txn_sanitize_tracking_maps() {
7781         // Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,
7782         // verify we clean then right after expiration of ANTI_REORG_DELAY.
7783
7784         let chanmon_cfgs = create_chanmon_cfgs(2);
7785         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7786         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7787         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7788
7789         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7790         // Lock HTLC in both directions
7791         let (payment_preimage_1, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000);
7792         let (_, payment_hash_2, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 9_000_000);
7793
7794         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7795         assert_eq!(revoked_local_txn[0].input.len(), 1);
7796         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7797
7798         // Revoke local commitment tx
7799         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
7800
7801         // Broadcast set of revoked txn on A
7802         connect_blocks(&nodes[0], TEST_FINAL_CLTV + 2 - CHAN_CONFIRM_DEPTH);
7803         expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[0], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
7804         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
7805
7806         mine_transaction(&nodes[0], &revoked_local_txn[0]);
7807         check_closed_broadcast!(nodes[0], true);
7808         check_added_monitors!(nodes[0], 1);
7809         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 1000000);
7810         let penalty_txn = {
7811                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7812                 assert_eq!(node_txn.len(), 3); //ChannelMonitor: justice txn * 3
7813                 check_spends!(node_txn[0], revoked_local_txn[0]);
7814                 check_spends!(node_txn[1], revoked_local_txn[0]);
7815                 check_spends!(node_txn[2], revoked_local_txn[0]);
7816                 let penalty_txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone()];
7817                 node_txn.clear();
7818                 penalty_txn
7819         };
7820         connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, penalty_txn));
7821         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7822         {
7823                 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(OutPoint { txid: chan.3.txid(), index: 0 }).unwrap();
7824                 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
7825                 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
7826         }
7827 }
7828
7829 #[test]
7830 fn test_channel_conf_timeout() {
7831         // Tests that, for inbound channels, we give up on them if the funding transaction does not
7832         // confirm within 2016 blocks, as recommended by BOLT 2.
7833         let chanmon_cfgs = create_chanmon_cfgs(2);
7834         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7835         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7836         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7837
7838         let _funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 100_000);
7839
7840         // The outbound node should wait forever for confirmation:
7841         // This matches `channel::FUNDING_CONF_DEADLINE_BLOCKS` and BOLT 2's suggested timeout, thus is
7842         // copied here instead of directly referencing the constant.
7843         connect_blocks(&nodes[0], 2016);
7844         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7845
7846         // The inbound node should fail the channel after exactly 2016 blocks
7847         connect_blocks(&nodes[1], 2015);
7848         check_added_monitors!(nodes[1], 0);
7849         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7850
7851         connect_blocks(&nodes[1], 1);
7852         check_added_monitors!(nodes[1], 1);
7853         check_closed_event!(nodes[1], 1, ClosureReason::FundingTimedOut, [nodes[0].node.get_our_node_id()], 1000000);
7854         let close_ev = nodes[1].node.get_and_clear_pending_msg_events();
7855         assert_eq!(close_ev.len(), 1);
7856         match close_ev[0] {
7857                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id } => {
7858                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
7859                         assert_eq!(msg.data, "Channel closed because funding transaction failed to confirm within 2016 blocks");
7860                 },
7861                 _ => panic!("Unexpected event"),
7862         }
7863 }
7864
7865 #[test]
7866 fn test_override_channel_config() {
7867         let chanmon_cfgs = create_chanmon_cfgs(2);
7868         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7869         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7870         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7871
7872         // Node0 initiates a channel to node1 using the override config.
7873         let mut override_config = UserConfig::default();
7874         override_config.channel_handshake_config.our_to_self_delay = 200;
7875
7876         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(override_config)).unwrap();
7877
7878         // Assert the channel created by node0 is using the override config.
7879         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7880         assert_eq!(res.channel_flags, 0);
7881         assert_eq!(res.to_self_delay, 200);
7882 }
7883
7884 #[test]
7885 fn test_override_0msat_htlc_minimum() {
7886         let mut zero_config = UserConfig::default();
7887         zero_config.channel_handshake_config.our_htlc_minimum_msat = 0;
7888         let chanmon_cfgs = create_chanmon_cfgs(2);
7889         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7890         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(zero_config.clone())]);
7891         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7892
7893         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(zero_config)).unwrap();
7894         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7895         assert_eq!(res.htlc_minimum_msat, 1);
7896
7897         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
7898         let res = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7899         assert_eq!(res.htlc_minimum_msat, 1);
7900 }
7901
7902 #[test]
7903 fn test_channel_update_has_correct_htlc_maximum_msat() {
7904         // Tests that the `ChannelUpdate` message has the correct values for `htlc_maximum_msat` set.
7905         // Bolt 7 specifies that if present `htlc_maximum_msat`:
7906         // 1. MUST be set to less than or equal to the channel capacity. In LDK, this is capped to
7907         // 90% of the `channel_value`.
7908         // 2. MUST be set to less than or equal to the `max_htlc_value_in_flight_msat` received from the peer.
7909
7910         let mut config_30_percent = UserConfig::default();
7911         config_30_percent.channel_handshake_config.announced_channel = true;
7912         config_30_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 30;
7913         let mut config_50_percent = UserConfig::default();
7914         config_50_percent.channel_handshake_config.announced_channel = true;
7915         config_50_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 50;
7916         let mut config_95_percent = UserConfig::default();
7917         config_95_percent.channel_handshake_config.announced_channel = true;
7918         config_95_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 95;
7919         let mut config_100_percent = UserConfig::default();
7920         config_100_percent.channel_handshake_config.announced_channel = true;
7921         config_100_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 100;
7922
7923         let chanmon_cfgs = create_chanmon_cfgs(4);
7924         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
7925         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[Some(config_30_percent), Some(config_50_percent), Some(config_95_percent), Some(config_100_percent)]);
7926         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
7927
7928         let channel_value_satoshis = 100000;
7929         let channel_value_msat = channel_value_satoshis * 1000;
7930         let channel_value_30_percent_msat = (channel_value_msat as f64 * 0.3) as u64;
7931         let channel_value_50_percent_msat = (channel_value_msat as f64 * 0.5) as u64;
7932         let channel_value_90_percent_msat = (channel_value_msat as f64 * 0.9) as u64;
7933
7934         let (node_0_chan_update, node_1_chan_update, _, _)  = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value_satoshis, 10001);
7935         let (node_2_chan_update, node_3_chan_update, _, _)  = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, channel_value_satoshis, 10001);
7936
7937         // Assert that `node[0]`'s `ChannelUpdate` is capped at 50 percent of the `channel_value`, as
7938         // that's the value of `node[1]`'s `holder_max_htlc_value_in_flight_msat`.
7939         assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, channel_value_50_percent_msat);
7940         // Assert that `node[1]`'s `ChannelUpdate` is capped at 30 percent of the `channel_value`, as
7941         // that's the value of `node[0]`'s `holder_max_htlc_value_in_flight_msat`.
7942         assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, channel_value_30_percent_msat);
7943
7944         // Assert that `node[2]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7945         // the value of `node[3]`'s `holder_max_htlc_value_in_flight_msat` (100%), exceeds 90% of the
7946         // `channel_value`.
7947         assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7948         // Assert that `node[3]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7949         // the value of `node[2]`'s `holder_max_htlc_value_in_flight_msat` (95%), exceeds 90% of the
7950         // `channel_value`.
7951         assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7952 }
7953
7954 #[test]
7955 fn test_manually_accept_inbound_channel_request() {
7956         let mut manually_accept_conf = UserConfig::default();
7957         manually_accept_conf.manually_accept_inbound_channels = true;
7958         let chanmon_cfgs = create_chanmon_cfgs(2);
7959         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7960         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
7961         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7962
7963         let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
7964         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7965
7966         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
7967
7968         // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
7969         // accepting the inbound channel request.
7970         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
7971
7972         let events = nodes[1].node.get_and_clear_pending_events();
7973         match events[0] {
7974                 Event::OpenChannelRequest { temporary_channel_id, .. } => {
7975                         nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap();
7976                 }
7977                 _ => panic!("Unexpected event"),
7978         }
7979
7980         let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
7981         assert_eq!(accept_msg_ev.len(), 1);
7982
7983         match accept_msg_ev[0] {
7984                 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
7985                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
7986                 }
7987                 _ => panic!("Unexpected event"),
7988         }
7989
7990         nodes[1].node.force_close_broadcasting_latest_txn(&temp_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
7991
7992         let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
7993         assert_eq!(close_msg_ev.len(), 1);
7994
7995         let events = nodes[1].node.get_and_clear_pending_events();
7996         match events[0] {
7997                 Event::ChannelClosed { user_channel_id, .. } => {
7998                         assert_eq!(user_channel_id, 23);
7999                 }
8000                 _ => panic!("Unexpected event"),
8001         }
8002 }
8003
8004 #[test]
8005 fn test_manually_reject_inbound_channel_request() {
8006         let mut manually_accept_conf = UserConfig::default();
8007         manually_accept_conf.manually_accept_inbound_channels = true;
8008         let chanmon_cfgs = create_chanmon_cfgs(2);
8009         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8010         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8011         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8012
8013         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8014         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8015
8016         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8017
8018         // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8019         // rejecting the inbound channel request.
8020         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8021
8022         let events = nodes[1].node.get_and_clear_pending_events();
8023         match events[0] {
8024                 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8025                         nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
8026                 }
8027                 _ => panic!("Unexpected event"),
8028         }
8029
8030         let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8031         assert_eq!(close_msg_ev.len(), 1);
8032
8033         match close_msg_ev[0] {
8034                 MessageSendEvent::HandleError { ref node_id, .. } => {
8035                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8036                 }
8037                 _ => panic!("Unexpected event"),
8038         }
8039
8040         // There should be no more events to process, as the channel was never opened.
8041         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
8042 }
8043
8044 #[test]
8045 fn test_can_not_accept_inbound_channel_twice() {
8046         let mut manually_accept_conf = UserConfig::default();
8047         manually_accept_conf.manually_accept_inbound_channels = true;
8048         let chanmon_cfgs = create_chanmon_cfgs(2);
8049         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8050         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8051         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8052
8053         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8054         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8055
8056         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8057
8058         // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8059         // accepting the inbound channel request.
8060         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8061
8062         let events = nodes[1].node.get_and_clear_pending_events();
8063         match events[0] {
8064                 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8065                         nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
8066                         let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0);
8067                         match api_res {
8068                                 Err(APIError::APIMisuseError { err }) => {
8069                                         assert_eq!(err, "No such channel awaiting to be accepted.");
8070                                 },
8071                                 Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
8072                                 Err(e) => panic!("Unexpected Error {:?}", e),
8073                         }
8074                 }
8075                 _ => panic!("Unexpected event"),
8076         }
8077
8078         // Ensure that the channel wasn't closed after attempting to accept it twice.
8079         let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8080         assert_eq!(accept_msg_ev.len(), 1);
8081
8082         match accept_msg_ev[0] {
8083                 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8084                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8085                 }
8086                 _ => panic!("Unexpected event"),
8087         }
8088 }
8089
8090 #[test]
8091 fn test_can_not_accept_unknown_inbound_channel() {
8092         let chanmon_cfg = create_chanmon_cfgs(2);
8093         let node_cfg = create_node_cfgs(2, &chanmon_cfg);
8094         let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
8095         let nodes = create_network(2, &node_cfg, &node_chanmgr);
8096
8097         let unknown_channel_id = ChannelId::new_zero();
8098         let api_res = nodes[0].node.accept_inbound_channel(&unknown_channel_id, &nodes[1].node.get_our_node_id(), 0);
8099         match api_res {
8100                 Err(APIError::APIMisuseError { err }) => {
8101                         assert_eq!(err, "No such channel awaiting to be accepted.");
8102                 },
8103                 Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
8104                 Err(e) => panic!("Unexpected Error: {:?}", e),
8105         }
8106 }
8107
8108 #[test]
8109 fn test_onion_value_mpp_set_calculation() {
8110         // Test that we use the onion value `amt_to_forward` when
8111         // calculating whether we've reached the `total_msat` of an MPP
8112         // by having a routing node forward more than `amt_to_forward`
8113         // and checking that the receiving node doesn't generate
8114         // a PaymentClaimable event too early
8115         let node_count = 4;
8116         let chanmon_cfgs = create_chanmon_cfgs(node_count);
8117         let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8118         let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8119         let mut nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8120
8121         let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8122         let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8123         let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8124         let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8125
8126         let total_msat = 100_000;
8127         let expected_paths: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
8128         let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], total_msat);
8129         let sample_path = route.paths.pop().unwrap();
8130
8131         let mut path_1 = sample_path.clone();
8132         path_1.hops[0].pubkey = nodes[1].node.get_our_node_id();
8133         path_1.hops[0].short_channel_id = chan_1_id;
8134         path_1.hops[1].pubkey = nodes[3].node.get_our_node_id();
8135         path_1.hops[1].short_channel_id = chan_3_id;
8136         path_1.hops[1].fee_msat = 100_000;
8137         route.paths.push(path_1);
8138
8139         let mut path_2 = sample_path.clone();
8140         path_2.hops[0].pubkey = nodes[2].node.get_our_node_id();
8141         path_2.hops[0].short_channel_id = chan_2_id;
8142         path_2.hops[1].pubkey = nodes[3].node.get_our_node_id();
8143         path_2.hops[1].short_channel_id = chan_4_id;
8144         path_2.hops[1].fee_msat = 1_000;
8145         route.paths.push(path_2);
8146
8147         // Send payment
8148         let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
8149         let onion_session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
8150                 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8151         nodes[0].node.test_send_payment_internal(&route, our_payment_hash,
8152                 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8153         check_added_monitors!(nodes[0], expected_paths.len());
8154
8155         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8156         assert_eq!(events.len(), expected_paths.len());
8157
8158         // First path
8159         let ev = remove_first_msg_event_to_node(&expected_paths[0][0].node.get_our_node_id(), &mut events);
8160         let mut payment_event = SendEvent::from_event(ev);
8161         let mut prev_node = &nodes[0];
8162
8163         for (idx, &node) in expected_paths[0].iter().enumerate() {
8164                 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
8165
8166                 if idx == 0 { // routing node
8167                         let session_priv = [3; 32];
8168                         let height = nodes[0].best_block_info().1;
8169                         let session_priv = SecretKey::from_slice(&session_priv).unwrap();
8170                         let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
8171                         let (mut onion_payloads, _, _) = onion_utils::build_onion_payloads(&route.paths[0], 100_000,
8172                                 RecipientOnionFields::secret_only(our_payment_secret), height + 1, &None).unwrap();
8173                         // Edit amt_to_forward to simulate the sender having set
8174                         // the final amount and the routing node taking less fee
8175                         if let msgs::OutboundOnionPayload::Receive { ref mut amt_msat, .. } = onion_payloads[1] {
8176                                 *amt_msat = 99_000;
8177                         } else { panic!() }
8178                         let new_onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
8179                         payment_event.msgs[0].onion_routing_packet = new_onion_packet;
8180                 }
8181
8182                 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]);
8183                 check_added_monitors!(node, 0);
8184                 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
8185                 expect_pending_htlcs_forwardable!(node);
8186
8187                 if idx == 0 {
8188                         let mut events_2 = node.node.get_and_clear_pending_msg_events();
8189                         assert_eq!(events_2.len(), 1);
8190                         check_added_monitors!(node, 1);
8191                         payment_event = SendEvent::from_event(events_2.remove(0));
8192                         assert_eq!(payment_event.msgs.len(), 1);
8193                 } else {
8194                         let events_2 = node.node.get_and_clear_pending_events();
8195                         assert!(events_2.is_empty());
8196                 }
8197
8198                 prev_node = node;
8199         }
8200
8201         // Second path
8202         let ev = remove_first_msg_event_to_node(&expected_paths[1][0].node.get_our_node_id(), &mut events);
8203         pass_along_path(&nodes[0], expected_paths[1], 101_000, our_payment_hash.clone(), Some(our_payment_secret), ev, true, None);
8204
8205         claim_payment_along_route(&nodes[0], expected_paths, false, our_payment_preimage);
8206 }
8207
8208 fn do_test_overshoot_mpp(msat_amounts: &[u64], total_msat: u64) {
8209
8210         let routing_node_count = msat_amounts.len();
8211         let node_count = routing_node_count + 2;
8212
8213         let chanmon_cfgs = create_chanmon_cfgs(node_count);
8214         let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8215         let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8216         let nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8217
8218         let src_idx = 0;
8219         let dst_idx = 1;
8220
8221         // Create channels for each amount
8222         let mut expected_paths = Vec::with_capacity(routing_node_count);
8223         let mut src_chan_ids = Vec::with_capacity(routing_node_count);
8224         let mut dst_chan_ids = Vec::with_capacity(routing_node_count);
8225         for i in 0..routing_node_count {
8226                 let routing_node = 2 + i;
8227                 let src_chan_id = create_announced_chan_between_nodes(&nodes, src_idx, routing_node).0.contents.short_channel_id;
8228                 src_chan_ids.push(src_chan_id);
8229                 let dst_chan_id = create_announced_chan_between_nodes(&nodes, routing_node, dst_idx).0.contents.short_channel_id;
8230                 dst_chan_ids.push(dst_chan_id);
8231                 let path = vec![&nodes[routing_node], &nodes[dst_idx]];
8232                 expected_paths.push(path);
8233         }
8234         let expected_paths: Vec<&[&Node]> = expected_paths.iter().map(|route| route.as_slice()).collect();
8235
8236         // Create a route for each amount
8237         let example_amount = 100000;
8238         let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[src_idx], nodes[dst_idx], example_amount);
8239         let sample_path = route.paths.pop().unwrap();
8240         for i in 0..routing_node_count {
8241                 let routing_node = 2 + i;
8242                 let mut path = sample_path.clone();
8243                 path.hops[0].pubkey = nodes[routing_node].node.get_our_node_id();
8244                 path.hops[0].short_channel_id = src_chan_ids[i];
8245                 path.hops[1].pubkey = nodes[dst_idx].node.get_our_node_id();
8246                 path.hops[1].short_channel_id = dst_chan_ids[i];
8247                 path.hops[1].fee_msat = msat_amounts[i];
8248                 route.paths.push(path);
8249         }
8250
8251         // Send payment with manually set total_msat
8252         let payment_id = PaymentId(nodes[src_idx].keys_manager.backing.get_secure_random_bytes());
8253         let onion_session_privs = nodes[src_idx].node.test_add_new_pending_payment(our_payment_hash,
8254                 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8255         nodes[src_idx].node.test_send_payment_internal(&route, our_payment_hash,
8256                 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8257         check_added_monitors!(nodes[src_idx], expected_paths.len());
8258
8259         let mut events = nodes[src_idx].node.get_and_clear_pending_msg_events();
8260         assert_eq!(events.len(), expected_paths.len());
8261         let mut amount_received = 0;
8262         for (path_idx, expected_path) in expected_paths.iter().enumerate() {
8263                 let ev = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &mut events);
8264
8265                 let current_path_amount = msat_amounts[path_idx];
8266                 amount_received += current_path_amount;
8267                 let became_claimable_now = amount_received >= total_msat && amount_received - current_path_amount < total_msat;
8268                 pass_along_path(&nodes[src_idx], expected_path, amount_received, our_payment_hash.clone(), Some(our_payment_secret), ev, became_claimable_now, None);
8269         }
8270
8271         claim_payment_along_route(&nodes[src_idx], &expected_paths, false, our_payment_preimage);
8272 }
8273
8274 #[test]
8275 fn test_overshoot_mpp() {
8276         do_test_overshoot_mpp(&[100_000, 101_000], 200_000);
8277         do_test_overshoot_mpp(&[100_000, 10_000, 100_000], 200_000);
8278 }
8279
8280 #[test]
8281 fn test_simple_mpp() {
8282         // Simple test of sending a multi-path payment.
8283         let chanmon_cfgs = create_chanmon_cfgs(4);
8284         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
8285         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
8286         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
8287
8288         let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8289         let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8290         let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8291         let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8292
8293         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
8294         let path = route.paths[0].clone();
8295         route.paths.push(path);
8296         route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
8297         route.paths[0].hops[0].short_channel_id = chan_1_id;
8298         route.paths[0].hops[1].short_channel_id = chan_3_id;
8299         route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
8300         route.paths[1].hops[0].short_channel_id = chan_2_id;
8301         route.paths[1].hops[1].short_channel_id = chan_4_id;
8302         send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
8303         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
8304 }
8305
8306 #[test]
8307 fn test_preimage_storage() {
8308         // Simple test of payment preimage storage allowing no client-side storage to claim payments
8309         let chanmon_cfgs = create_chanmon_cfgs(2);
8310         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8311         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8312         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8313
8314         create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8315
8316         {
8317                 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 7200, None).unwrap();
8318                 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8319                 nodes[0].node.send_payment_with_route(&route, payment_hash,
8320                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8321                 check_added_monitors!(nodes[0], 1);
8322                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8323                 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8324                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8325                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8326         }
8327         // Note that after leaving the above scope we have no knowledge of any arguments or return
8328         // values from previous calls.
8329         expect_pending_htlcs_forwardable!(nodes[1]);
8330         let events = nodes[1].node.get_and_clear_pending_events();
8331         assert_eq!(events.len(), 1);
8332         match events[0] {
8333                 Event::PaymentClaimable { ref purpose, .. } => {
8334                         match &purpose {
8335                                 PaymentPurpose::InvoicePayment { payment_preimage, .. } => {
8336                                         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8337                                 },
8338                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
8339                         }
8340                 },
8341                 _ => panic!("Unexpected event"),
8342         }
8343 }
8344
8345 #[test]
8346 fn test_bad_secret_hash() {
8347         // Simple test of unregistered payment hash/invalid payment secret handling
8348         let chanmon_cfgs = create_chanmon_cfgs(2);
8349         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8350         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8351         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8352
8353         create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8354
8355         let random_payment_hash = PaymentHash([42; 32]);
8356         let random_payment_secret = PaymentSecret([43; 32]);
8357         let (our_payment_hash, our_payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 2, None).unwrap();
8358         let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8359
8360         // All the below cases should end up being handled exactly identically, so we macro the
8361         // resulting events.
8362         macro_rules! handle_unknown_invalid_payment_data {
8363                 ($payment_hash: expr) => {
8364                         check_added_monitors!(nodes[0], 1);
8365                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8366                         let payment_event = SendEvent::from_event(events.pop().unwrap());
8367                         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8368                         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8369
8370                         // We have to forward pending HTLCs once to process the receipt of the HTLC and then
8371                         // again to process the pending backwards-failure of the HTLC
8372                         expect_pending_htlcs_forwardable!(nodes[1]);
8373                         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment{ payment_hash: $payment_hash }]);
8374                         check_added_monitors!(nodes[1], 1);
8375
8376                         // We should fail the payment back
8377                         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
8378                         match events.pop().unwrap() {
8379                                 MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8380                                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
8381                                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
8382                                 },
8383                                 _ => panic!("Unexpected event"),
8384                         }
8385                 }
8386         }
8387
8388         let expected_error_code = 0x4000|15; // incorrect_or_unknown_payment_details
8389         // Error data is the HTLC value (100,000) and current block height
8390         let expected_error_data = [0, 0, 0, 0, 0, 1, 0x86, 0xa0, 0, 0, 0, CHAN_CONFIRM_DEPTH as u8];
8391
8392         // Send a payment with the right payment hash but the wrong payment secret
8393         nodes[0].node.send_payment_with_route(&route, our_payment_hash,
8394                 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
8395         handle_unknown_invalid_payment_data!(our_payment_hash);
8396         expect_payment_failed!(nodes[0], our_payment_hash, true, expected_error_code, expected_error_data);
8397
8398         // Send a payment with a random payment hash, but the right payment secret
8399         nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8400                 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8401         handle_unknown_invalid_payment_data!(random_payment_hash);
8402         expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8403
8404         // Send a payment with a random payment hash and random payment secret
8405         nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8406                 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8407         handle_unknown_invalid_payment_data!(random_payment_hash);
8408         expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8409 }
8410
8411 #[test]
8412 fn test_update_err_monitor_lockdown() {
8413         // Our monitor will lock update of local commitment transaction if a broadcastion condition
8414         // has been fulfilled (either force-close from Channel or block height requiring a HTLC-
8415         // timeout). Trying to update monitor after lockdown should return a ChannelMonitorUpdateStatus
8416         // error.
8417         //
8418         // This scenario may happen in a watchtower setup, where watchtower process a block height
8419         // triggering a timeout while a slow-block-processing ChannelManager receives a local signed
8420         // commitment at same time.
8421
8422         let chanmon_cfgs = create_chanmon_cfgs(2);
8423         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8424         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8425         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8426
8427         // Create some initial channel
8428         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8429         let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8430
8431         // Rebalance the network to generate htlc in the two directions
8432         send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8433
8434         // Route a HTLC from node 0 to node 1 (but don't settle)
8435         let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
8436
8437         // Copy ChainMonitor to simulate a watchtower and update block height of node 0 until its ChannelMonitor timeout HTLC onchain
8438         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8439         let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
8440         let persister = test_utils::TestPersister::new();
8441         let watchtower = {
8442                 let new_monitor = {
8443                         let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8444                         let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8445                                         &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8446                         assert!(new_monitor == *monitor);
8447                         new_monitor
8448                 };
8449                 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8450                 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8451                 watchtower
8452         };
8453         let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8454         // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8455         // transaction lock time requirements here.
8456         chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 200));
8457         watchtower.chain_monitor.block_connected(&block, 200);
8458
8459         // Try to update ChannelMonitor
8460         nodes[1].node.claim_funds(preimage);
8461         check_added_monitors!(nodes[1], 1);
8462         expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
8463
8464         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8465         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
8466         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
8467         {
8468                 let mut node_0_per_peer_lock;
8469                 let mut node_0_peer_state_lock;
8470                 if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
8471                         if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8472                                 assert_eq!(watchtower.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8473                                 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8474                         } else { assert!(false); }
8475                 } else {
8476                         assert!(false);
8477                 }
8478         }
8479         // Our local monitor is in-sync and hasn't processed yet timeout
8480         check_added_monitors!(nodes[0], 1);
8481         let events = nodes[0].node.get_and_clear_pending_events();
8482         assert_eq!(events.len(), 1);
8483 }
8484
8485 #[test]
8486 fn test_concurrent_monitor_claim() {
8487         // Watchtower A receives block, broadcasts state N, then channel receives new state N+1,
8488         // sending it to both watchtowers, Bob accepts N+1, then receives block and broadcasts
8489         // the latest state N+1, Alice rejects state N+1, but Bob has already broadcast it,
8490         // state N+1 confirms. Alice claims output from state N+1.
8491
8492         let chanmon_cfgs = create_chanmon_cfgs(2);
8493         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8494         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8495         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8496
8497         // Create some initial channel
8498         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8499         let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8500
8501         // Rebalance the network to generate htlc in the two directions
8502         send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8503
8504         // Route a HTLC from node 0 to node 1 (but don't settle)
8505         route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
8506
8507         // Copy ChainMonitor to simulate watchtower Alice and update block height her ChannelMonitor timeout HTLC onchain
8508         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8509         let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
8510         let persister = test_utils::TestPersister::new();
8511         let alice_broadcaster = test_utils::TestBroadcaster::with_blocks(
8512                 Arc::new(Mutex::new(nodes[0].blocks.lock().unwrap().clone())),
8513         );
8514         let watchtower_alice = {
8515                 let new_monitor = {
8516                         let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8517                         let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8518                                         &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8519                         assert!(new_monitor == *monitor);
8520                         new_monitor
8521                 };
8522                 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &alice_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8523                 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8524                 watchtower
8525         };
8526         let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8527         // Make Alice aware of enough blocks that it doesn't think we're violating transaction lock time
8528         // requirements here.
8529         const HTLC_TIMEOUT_BROADCAST: u32 = CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS;
8530         alice_broadcaster.blocks.lock().unwrap().resize((HTLC_TIMEOUT_BROADCAST) as usize, (block.clone(), HTLC_TIMEOUT_BROADCAST));
8531         watchtower_alice.chain_monitor.block_connected(&block, HTLC_TIMEOUT_BROADCAST);
8532
8533         // Watchtower Alice should have broadcast a commitment/HTLC-timeout
8534         let alice_state = {
8535                 let mut txn = alice_broadcaster.txn_broadcast();
8536                 assert_eq!(txn.len(), 2);
8537                 txn.remove(0)
8538         };
8539
8540         // Copy ChainMonitor to simulate watchtower Bob and make it receive a commitment update first.
8541         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8542         let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
8543         let persister = test_utils::TestPersister::new();
8544         let bob_broadcaster = test_utils::TestBroadcaster::with_blocks(Arc::clone(&alice_broadcaster.blocks));
8545         let watchtower_bob = {
8546                 let new_monitor = {
8547                         let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8548                         let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8549                                         &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8550                         assert!(new_monitor == *monitor);
8551                         new_monitor
8552                 };
8553                 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &bob_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8554                 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8555                 watchtower
8556         };
8557         watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST - 1);
8558
8559         // Route another payment to generate another update with still previous HTLC pending
8560         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
8561         nodes[1].node.send_payment_with_route(&route, payment_hash,
8562                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8563         check_added_monitors!(nodes[1], 1);
8564
8565         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8566         assert_eq!(updates.update_add_htlcs.len(), 1);
8567         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
8568         {
8569                 let mut node_0_per_peer_lock;
8570                 let mut node_0_peer_state_lock;
8571                 if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
8572                         if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8573                                 // Watchtower Alice should already have seen the block and reject the update
8574                                 assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8575                                 assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8576                                 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8577                         } else { assert!(false); }
8578                 } else {
8579                         assert!(false);
8580                 }
8581         }
8582         // Our local monitor is in-sync and hasn't processed yet timeout
8583         check_added_monitors!(nodes[0], 1);
8584
8585         //// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
8586         watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST);
8587
8588         // Watchtower Bob should have broadcast a commitment/HTLC-timeout
8589         let bob_state_y;
8590         {
8591                 let mut txn = bob_broadcaster.txn_broadcast();
8592                 assert_eq!(txn.len(), 2);
8593                 bob_state_y = txn.remove(0);
8594         };
8595
8596         // We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
8597         let height = HTLC_TIMEOUT_BROADCAST + 1;
8598         connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
8599         check_closed_broadcast(&nodes[0], 1, true);
8600         check_closed_event!(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false,
8601                 [nodes[1].node.get_our_node_id()], 100000);
8602         watchtower_alice.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, vec![bob_state_y.clone()]), height);
8603         check_added_monitors(&nodes[0], 1);
8604         {
8605                 let htlc_txn = alice_broadcaster.txn_broadcast();
8606                 assert_eq!(htlc_txn.len(), 2);
8607                 check_spends!(htlc_txn[0], bob_state_y);
8608                 // Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for
8609                 // it. However, she should, because it now has an invalid parent.
8610                 check_spends!(htlc_txn[1], alice_state);
8611         }
8612 }
8613
8614 #[test]
8615 fn test_pre_lockin_no_chan_closed_update() {
8616         // Test that if a peer closes a channel in response to a funding_created message we don't
8617         // generate a channel update (as the channel cannot appear on chain without a funding_signed
8618         // message).
8619         //
8620         // Doing so would imply a channel monitor update before the initial channel monitor
8621         // registration, violating our API guarantees.
8622         //
8623         // Previously, full_stack_target managed to hit this case by opening then closing a channel,
8624         // then opening a second channel with the same funding output as the first (which is not
8625         // rejected because the first channel does not exist in the ChannelManager) and closing it
8626         // before receiving funding_signed.
8627         let chanmon_cfgs = create_chanmon_cfgs(2);
8628         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8629         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8630         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8631
8632         // Create an initial channel
8633         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8634         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8635         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8636         let accept_chan_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
8637         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_chan_msg);
8638
8639         // Move the first channel through the funding flow...
8640         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
8641
8642         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
8643         check_added_monitors!(nodes[0], 0);
8644
8645         let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8646         let channel_id = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
8647         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
8648         assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
8649         check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("Hi".to_string()) }, true,
8650                 [nodes[1].node.get_our_node_id(); 2], 100000);
8651 }
8652
8653 #[test]
8654 fn test_htlc_no_detection() {
8655         // This test is a mutation to underscore the detection logic bug we had
8656         // before #653. HTLC value routed is above the remaining balance, thus
8657         // inverting HTLC and `to_remote` output. HTLC will come second and
8658         // it wouldn't be seen by pre-#653 detection as we were enumerate()'ing
8659         // on a watched outputs vector (Vec<TxOut>) thus implicitly relying on
8660         // outputs order detection for correct spending children filtring.
8661
8662         let chanmon_cfgs = create_chanmon_cfgs(2);
8663         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8664         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8665         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8666
8667         // Create some initial channels
8668         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8669
8670         send_payment(&nodes[0], &vec!(&nodes[1])[..], 1_000_000);
8671         let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 2_000_000);
8672         let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
8673         assert_eq!(local_txn[0].input.len(), 1);
8674         assert_eq!(local_txn[0].output.len(), 3);
8675         check_spends!(local_txn[0], chan_1.3);
8676
8677         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
8678         let block = create_dummy_block(nodes[0].best_block_hash(), 42, vec![local_txn[0].clone()]);
8679         connect_block(&nodes[0], &block);
8680         // We deliberately connect the local tx twice as this should provoke a failure calling
8681         // this test before #653 fix.
8682         chain::Listen::block_connected(&nodes[0].chain_monitor.chain_monitor, &block, nodes[0].best_block_info().1 + 1);
8683         check_closed_broadcast!(nodes[0], true);
8684         check_added_monitors!(nodes[0], 1);
8685         check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
8686         connect_blocks(&nodes[0], TEST_FINAL_CLTV);
8687
8688         let htlc_timeout = {
8689                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8690                 assert_eq!(node_txn.len(), 1);
8691                 assert_eq!(node_txn[0].input.len(), 1);
8692                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8693                 check_spends!(node_txn[0], local_txn[0]);
8694                 node_txn[0].clone()
8695         };
8696
8697         connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![htlc_timeout.clone()]));
8698         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
8699         expect_payment_failed!(nodes[0], our_payment_hash, false);
8700 }
8701
8702 fn do_test_onchain_htlc_settlement_after_close(broadcast_alice: bool, go_onchain_before_fulfill: bool) {
8703         // If we route an HTLC, then learn the HTLC's preimage after the upstream channel has been
8704         // force-closed, we must claim that HTLC on-chain. (Given an HTLC forwarded from Alice --> Bob -->
8705         // Carol, Alice would be the upstream node, and Carol the downstream.)
8706         //
8707         // Steps of the test:
8708         // 1) Alice sends a HTLC to Carol through Bob.
8709         // 2) Carol doesn't settle the HTLC.
8710         // 3) If broadcast_alice is true, Alice force-closes her channel with Bob. Else Bob force closes.
8711         // Steps 4 and 5 may be reordered depending on go_onchain_before_fulfill.
8712         // 4) Bob sees the Alice's commitment on his chain or vice versa. An offered output is present
8713         //    but can't be claimed as Bob doesn't have yet knowledge of the preimage.
8714         // 5) Carol release the preimage to Bob off-chain.
8715         // 6) Bob claims the offered output on the broadcasted commitment.
8716         let chanmon_cfgs = create_chanmon_cfgs(3);
8717         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8718         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8719         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8720
8721         // Create some initial channels
8722         let chan_ab = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8723         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001);
8724
8725         // Steps (1) and (2):
8726         // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
8727         let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
8728
8729         // Check that Alice's commitment transaction now contains an output for this HTLC.
8730         let alice_txn = get_local_commitment_txn!(nodes[0], chan_ab.2);
8731         check_spends!(alice_txn[0], chan_ab.3);
8732         assert_eq!(alice_txn[0].output.len(), 2);
8733         check_spends!(alice_txn[1], alice_txn[0]); // 2nd transaction is a non-final HTLC-timeout
8734         assert_eq!(alice_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8735         assert_eq!(alice_txn.len(), 2);
8736
8737         // Steps (3) and (4):
8738         // If `go_onchain_before_fufill`, broadcast the relevant commitment transaction and check that Bob
8739         // responds by (1) broadcasting a channel update and (2) adding a new ChannelMonitor.
8740         let mut force_closing_node = 0; // Alice force-closes
8741         let mut counterparty_node = 1; // Bob if Alice force-closes
8742
8743         // Bob force-closes
8744         if !broadcast_alice {
8745                 force_closing_node = 1;
8746                 counterparty_node = 0;
8747         }
8748         nodes[force_closing_node].node.force_close_broadcasting_latest_txn(&chan_ab.2, &nodes[counterparty_node].node.get_our_node_id()).unwrap();
8749         check_closed_broadcast!(nodes[force_closing_node], true);
8750         check_added_monitors!(nodes[force_closing_node], 1);
8751         check_closed_event!(nodes[force_closing_node], 1, ClosureReason::HolderForceClosed, [nodes[counterparty_node].node.get_our_node_id()], 100000);
8752         if go_onchain_before_fulfill {
8753                 let txn_to_broadcast = match broadcast_alice {
8754                         true => alice_txn.clone(),
8755                         false => get_local_commitment_txn!(nodes[1], chan_ab.2)
8756                 };
8757                 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8758                 if broadcast_alice {
8759                         check_closed_broadcast!(nodes[1], true);
8760                         check_added_monitors!(nodes[1], 1);
8761                         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8762                 }
8763         }
8764
8765         // Step (5):
8766         // Carol then claims the funds and sends an update_fulfill message to Bob, and they go through the
8767         // process of removing the HTLC from their commitment transactions.
8768         nodes[2].node.claim_funds(payment_preimage);
8769         check_added_monitors!(nodes[2], 1);
8770         expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
8771
8772         let carol_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
8773         assert!(carol_updates.update_add_htlcs.is_empty());
8774         assert!(carol_updates.update_fail_htlcs.is_empty());
8775         assert!(carol_updates.update_fail_malformed_htlcs.is_empty());
8776         assert!(carol_updates.update_fee.is_none());
8777         assert_eq!(carol_updates.update_fulfill_htlcs.len(), 1);
8778
8779         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &carol_updates.update_fulfill_htlcs[0]);
8780         let went_onchain = go_onchain_before_fulfill || force_closing_node == 1;
8781         expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], if went_onchain { None } else { Some(1000) }, went_onchain, false);
8782         // If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
8783         if !go_onchain_before_fulfill && broadcast_alice {
8784                 let events = nodes[1].node.get_and_clear_pending_msg_events();
8785                 assert_eq!(events.len(), 1);
8786                 match events[0] {
8787                         MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
8788                                 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8789                         },
8790                         _ => panic!("Unexpected event"),
8791                 };
8792         }
8793         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &carol_updates.commitment_signed);
8794         // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
8795         // Carol<->Bob's updated commitment transaction info.
8796         check_added_monitors!(nodes[1], 2);
8797
8798         let events = nodes[1].node.get_and_clear_pending_msg_events();
8799         assert_eq!(events.len(), 2);
8800         let bob_revocation = match events[0] {
8801                 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8802                         assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8803                         (*msg).clone()
8804                 },
8805                 _ => panic!("Unexpected event"),
8806         };
8807         let bob_updates = match events[1] {
8808                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
8809                         assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8810                         (*updates).clone()
8811                 },
8812                 _ => panic!("Unexpected event"),
8813         };
8814
8815         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revocation);
8816         check_added_monitors!(nodes[2], 1);
8817         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_updates.commitment_signed);
8818         check_added_monitors!(nodes[2], 1);
8819
8820         let events = nodes[2].node.get_and_clear_pending_msg_events();
8821         assert_eq!(events.len(), 1);
8822         let carol_revocation = match events[0] {
8823                 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8824                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
8825                         (*msg).clone()
8826                 },
8827                 _ => panic!("Unexpected event"),
8828         };
8829         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &carol_revocation);
8830         check_added_monitors!(nodes[1], 1);
8831
8832         // If this test requires the force-closed channel to not be on-chain until after the fulfill,
8833         // here's where we put said channel's commitment tx on-chain.
8834         let mut txn_to_broadcast = alice_txn.clone();
8835         if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn!(nodes[1], chan_ab.2); }
8836         if !go_onchain_before_fulfill {
8837                 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8838                 // If Bob was the one to force-close, he will have already passed these checks earlier.
8839                 if broadcast_alice {
8840                         check_closed_broadcast!(nodes[1], true);
8841                         check_added_monitors!(nodes[1], 1);
8842                         check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8843                 }
8844                 let mut bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8845                 if broadcast_alice {
8846                         assert_eq!(bob_txn.len(), 1);
8847                         check_spends!(bob_txn[0], txn_to_broadcast[0]);
8848                 } else {
8849                         assert_eq!(bob_txn.len(), 2);
8850                         check_spends!(bob_txn[0], chan_ab.3);
8851                 }
8852         }
8853
8854         // Step (6):
8855         // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
8856         // broadcasted commitment transaction.
8857         {
8858                 let script_weight = match broadcast_alice {
8859                         true => OFFERED_HTLC_SCRIPT_WEIGHT,
8860                         false => ACCEPTED_HTLC_SCRIPT_WEIGHT
8861                 };
8862                 // If Alice force-closed, Bob only broadcasts a HTLC-output-claiming transaction. Otherwise,
8863                 // Bob force-closed and broadcasts the commitment transaction along with a
8864                 // HTLC-output-claiming transaction.
8865                 let bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
8866                 if broadcast_alice {
8867                         assert_eq!(bob_txn.len(), 1);
8868                         check_spends!(bob_txn[0], txn_to_broadcast[0]);
8869                         assert_eq!(bob_txn[0].input[0].witness.last().unwrap().len(), script_weight);
8870                 } else {
8871                         assert_eq!(bob_txn.len(), 2);
8872                         check_spends!(bob_txn[1], txn_to_broadcast[0]);
8873                         assert_eq!(bob_txn[1].input[0].witness.last().unwrap().len(), script_weight);
8874                 }
8875         }
8876 }
8877
8878 #[test]
8879 fn test_onchain_htlc_settlement_after_close() {
8880         do_test_onchain_htlc_settlement_after_close(true, true);
8881         do_test_onchain_htlc_settlement_after_close(false, true); // Technically redundant, but may as well
8882         do_test_onchain_htlc_settlement_after_close(true, false);
8883         do_test_onchain_htlc_settlement_after_close(false, false);
8884 }
8885
8886 #[test]
8887 fn test_duplicate_temporary_channel_id_from_different_peers() {
8888         // Tests that we can accept two different `OpenChannel` requests with the same
8889         // `temporary_channel_id`, as long as they are from different peers.
8890         let chanmon_cfgs = create_chanmon_cfgs(3);
8891         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8892         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8893         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8894
8895         // Create an first channel channel
8896         nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8897         let mut open_chan_msg_chan_1_0 = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8898
8899         // Create an second channel
8900         nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 43, None).unwrap();
8901         let mut open_chan_msg_chan_2_0 = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8902
8903         // Modify the `OpenChannel` from `nodes[2]` to `nodes[0]` to ensure that it uses the same
8904         // `temporary_channel_id` as the `OpenChannel` from nodes[1] to nodes[0].
8905         open_chan_msg_chan_2_0.temporary_channel_id = open_chan_msg_chan_1_0.temporary_channel_id;
8906
8907         // Assert that `nodes[0]` can accept both `OpenChannel` requests, even though they use the same
8908         // `temporary_channel_id` as they are from different peers.
8909         nodes[0].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_chan_msg_chan_1_0);
8910         {
8911                 let events = nodes[0].node.get_and_clear_pending_msg_events();
8912                 assert_eq!(events.len(), 1);
8913                 match &events[0] {
8914                         MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8915                                 assert_eq!(node_id, &nodes[1].node.get_our_node_id());
8916                                 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8917                         },
8918                         _ => panic!("Unexpected event"),
8919                 }
8920         }
8921
8922         nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), &open_chan_msg_chan_2_0);
8923         {
8924                 let events = nodes[0].node.get_and_clear_pending_msg_events();
8925                 assert_eq!(events.len(), 1);
8926                 match &events[0] {
8927                         MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8928                                 assert_eq!(node_id, &nodes[2].node.get_our_node_id());
8929                                 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8930                         },
8931                         _ => panic!("Unexpected event"),
8932                 }
8933         }
8934 }
8935
8936 #[test]
8937 fn test_duplicate_chan_id() {
8938         // Test that if a given peer tries to open a channel with the same channel_id as one that is
8939         // already open we reject it and keep the old channel.
8940         //
8941         // Previously, full_stack_target managed to figure out that if you tried to open two channels
8942         // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
8943         // the existing channel when we detect the duplicate new channel, screwing up our monitor
8944         // updating logic for the existing channel.
8945         let chanmon_cfgs = create_chanmon_cfgs(2);
8946         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8947         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8948         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8949
8950         // Create an initial channel
8951         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8952         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8953         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8954         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
8955
8956         // Try to create a second channel with the same temporary_channel_id as the first and check
8957         // that it is rejected.
8958         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8959         {
8960                 let events = nodes[1].node.get_and_clear_pending_msg_events();
8961                 assert_eq!(events.len(), 1);
8962                 match events[0] {
8963                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
8964                                 // Technically, at this point, nodes[1] would be justified in thinking both the
8965                                 // first (valid) and second (invalid) channels are closed, given they both have
8966                                 // the same non-temporary channel_id. However, currently we do not, so we just
8967                                 // move forward with it.
8968                                 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
8969                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
8970                         },
8971                         _ => panic!("Unexpected event"),
8972                 }
8973         }
8974
8975         // Move the first channel through the funding flow...
8976         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
8977
8978         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
8979         check_added_monitors!(nodes[0], 0);
8980
8981         let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8982         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
8983         {
8984                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
8985                 assert_eq!(added_monitors.len(), 1);
8986                 assert_eq!(added_monitors[0].0, funding_output);
8987                 added_monitors.clear();
8988         }
8989         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
8990
8991         let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
8992
8993         let funding_outpoint = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index };
8994         let channel_id = funding_outpoint.to_channel_id();
8995
8996         // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
8997         // temporary one).
8998
8999         // First try to open a second channel with a temporary channel id equal to the txid-based one.
9000         // Technically this is allowed by the spec, but we don't support it and there's little reason
9001         // to. Still, it shouldn't cause any other issues.
9002         open_chan_msg.temporary_channel_id = channel_id;
9003         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
9004         {
9005                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9006                 assert_eq!(events.len(), 1);
9007                 match events[0] {
9008                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9009                                 // Technically, at this point, nodes[1] would be justified in thinking both
9010                                 // channels are closed, but currently we do not, so we just move forward with it.
9011                                 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9012                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9013                         },
9014                         _ => panic!("Unexpected event"),
9015                 }
9016         }
9017
9018         // Now try to create a second channel which has a duplicate funding output.
9019         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9020         let open_chan_2_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9021         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_2_msg);
9022         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9023         create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); // Get and check the FundingGenerationReady event
9024
9025         let (_, funding_created) = {
9026                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9027                 let mut a_peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9028                 // Once we call `get_funding_created` the channel has a duplicate channel_id as
9029                 // another channel in the ChannelManager - an invalid state. Thus, we'd panic later when we
9030                 // try to create another channel. Instead, we drop the channel entirely here (leaving the
9031                 // channelmanager in a possibly nonsense state instead).
9032                 match a_peer_state.channel_by_id.remove(&open_chan_2_msg.temporary_channel_id).unwrap() {
9033                         ChannelPhase::UnfundedOutboundV1(chan) => {
9034                                 let logger = test_utils::TestLogger::new();
9035                                 chan.get_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap()
9036                         },
9037                         _ => panic!("Unexpected ChannelPhase variant"),
9038                 }
9039         };
9040         check_added_monitors!(nodes[0], 0);
9041         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9042         // At this point we'll look up if the channel_id is present and immediately fail the channel
9043         // without trying to persist the `ChannelMonitor`.
9044         check_added_monitors!(nodes[1], 0);
9045
9046         // ...still, nodes[1] will reject the duplicate channel.
9047         {
9048                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9049                 assert_eq!(events.len(), 1);
9050                 match events[0] {
9051                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9052                                 // Technically, at this point, nodes[1] would be justified in thinking both
9053                                 // channels are closed, but currently we do not, so we just move forward with it.
9054                                 assert_eq!(msg.channel_id, channel_id);
9055                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9056                         },
9057                         _ => panic!("Unexpected event"),
9058                 }
9059         }
9060
9061         // finally, finish creating the original channel and send a payment over it to make sure
9062         // everything is functional.
9063         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
9064         {
9065                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9066                 assert_eq!(added_monitors.len(), 1);
9067                 assert_eq!(added_monitors[0].0, funding_output);
9068                 added_monitors.clear();
9069         }
9070         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9071
9072         let events_4 = nodes[0].node.get_and_clear_pending_events();
9073         assert_eq!(events_4.len(), 0);
9074         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9075         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9076
9077         let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9078         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9079         update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9080
9081         send_payment(&nodes[0], &[&nodes[1]], 8000000);
9082 }
9083
9084 #[test]
9085 fn test_error_chans_closed() {
9086         // Test that we properly handle error messages, closing appropriate channels.
9087         //
9088         // Prior to #787 we'd allow a peer to make us force-close a channel we had with a different
9089         // peer. The "real" fix for that is to index channels with peers_ids, however in the mean time
9090         // we can test various edge cases around it to ensure we don't regress.
9091         let chanmon_cfgs = create_chanmon_cfgs(3);
9092         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9093         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9094         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9095
9096         // Create some initial channels
9097         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9098         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9099         let chan_3 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001);
9100
9101         assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9102         assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
9103         assert_eq!(nodes[2].node.list_usable_channels().len(), 1);
9104
9105         // Closing a channel from a different peer has no effect
9106         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_3.2, data: "ERR".to_owned() });
9107         assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9108
9109         // Closing one channel doesn't impact others
9110         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_2.2, data: "ERR".to_owned() });
9111         check_added_monitors!(nodes[0], 1);
9112         check_closed_broadcast!(nodes[0], false);
9113         check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9114                 [nodes[1].node.get_our_node_id()], 100000);
9115         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
9116         assert_eq!(nodes[0].node.list_usable_channels().len(), 2);
9117         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_1.2 || nodes[0].node.list_usable_channels()[1].channel_id == chan_1.2);
9118         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2 || nodes[0].node.list_usable_channels()[1].channel_id == chan_3.2);
9119
9120         // A null channel ID should close all channels
9121         let _chan_4 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9122         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: ChannelId::new_zero(), data: "ERR".to_owned() });
9123         check_added_monitors!(nodes[0], 2);
9124         check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9125                 [nodes[1].node.get_our_node_id(); 2], 100000);
9126         let events = nodes[0].node.get_and_clear_pending_msg_events();
9127         assert_eq!(events.len(), 2);
9128         match events[0] {
9129                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9130                         assert_eq!(msg.contents.flags & 2, 2);
9131                 },
9132                 _ => panic!("Unexpected event"),
9133         }
9134         match events[1] {
9135                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9136                         assert_eq!(msg.contents.flags & 2, 2);
9137                 },
9138                 _ => panic!("Unexpected event"),
9139         }
9140         // Note that at this point users of a standard PeerHandler will end up calling
9141         // peer_disconnected.
9142         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9143         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9144
9145         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9146         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9147         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9148 }
9149
9150 #[test]
9151 fn test_invalid_funding_tx() {
9152         // Test that we properly handle invalid funding transactions sent to us from a peer.
9153         //
9154         // Previously, all other major lightning implementations had failed to properly sanitize
9155         // funding transactions from their counterparties, leading to a multi-implementation critical
9156         // security vulnerability (though we always sanitized properly, we've previously had
9157         // un-released crashes in the sanitization process).
9158         //
9159         // Further, if the funding transaction is consensus-valid, confirms, and is later spent, we'd
9160         // previously have crashed in `ChannelMonitor` even though we closed the channel as bogus and
9161         // gave up on it. We test this here by generating such a transaction.
9162         let chanmon_cfgs = create_chanmon_cfgs(2);
9163         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9164         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9165         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9166
9167         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_000, 42, None).unwrap();
9168         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
9169         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9170
9171         let (temporary_channel_id, mut tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
9172
9173         // Create a witness program which can be spent by a 4-empty-stack-elements witness and which is
9174         // 136 bytes long. This matches our "accepted HTLC preimage spend" matching, previously causing
9175         // a panic as we'd try to extract a 32 byte preimage from a witness element without checking
9176         // its length.
9177         let mut wit_program: Vec<u8> = channelmonitor::deliberately_bogus_accepted_htlc_witness_program();
9178         let wit_program_script: Script = wit_program.into();
9179         for output in tx.output.iter_mut() {
9180                 // Make the confirmed funding transaction have a bogus script_pubkey
9181                 output.script_pubkey = Script::new_v0_p2wsh(&wit_program_script.wscript_hash());
9182         }
9183
9184         nodes[0].node.funding_transaction_generated_unchecked(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone(), 0).unwrap();
9185         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()));
9186         check_added_monitors!(nodes[1], 1);
9187         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9188
9189         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
9190         check_added_monitors!(nodes[0], 1);
9191         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9192
9193         let events_1 = nodes[0].node.get_and_clear_pending_events();
9194         assert_eq!(events_1.len(), 0);
9195
9196         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9197         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9198         nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
9199
9200         let expected_err = "funding tx had wrong script/value or output index";
9201         confirm_transaction_at(&nodes[1], &tx, 1);
9202         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() },
9203                 [nodes[0].node.get_our_node_id()], 100000);
9204         check_added_monitors!(nodes[1], 1);
9205         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
9206         assert_eq!(events_2.len(), 1);
9207         if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
9208                 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
9209                 if let msgs::ErrorAction::SendErrorMessage { msg } = action {
9210                         assert_eq!(msg.data, "Channel closed because of an exception: ".to_owned() + expected_err);
9211                 } else { panic!(); }
9212         } else { panic!(); }
9213         assert_eq!(nodes[1].node.list_channels().len(), 0);
9214
9215         // Now confirm a spend of the (bogus) funding transaction. As long as the witness is 5 elements
9216         // long the ChannelMonitor will try to read 32 bytes from the second-to-last element, panicing
9217         // as its not 32 bytes long.
9218         let mut spend_tx = Transaction {
9219                 version: 2i32, lock_time: PackedLockTime::ZERO,
9220                 input: tx.output.iter().enumerate().map(|(idx, _)| TxIn {
9221                         previous_output: BitcoinOutPoint {
9222                                 txid: tx.txid(),
9223                                 vout: idx as u32,
9224                         },
9225                         script_sig: Script::new(),
9226                         sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
9227                         witness: Witness::from_vec(channelmonitor::deliberately_bogus_accepted_htlc_witness())
9228                 }).collect(),
9229                 output: vec![TxOut {
9230                         value: 1000,
9231                         script_pubkey: Script::new(),
9232                 }]
9233         };
9234         check_spends!(spend_tx, tx);
9235         mine_transaction(&nodes[1], &spend_tx);
9236 }
9237
9238 #[test]
9239 fn test_coinbase_funding_tx() {
9240         // Miners are able to fund channels directly from coinbase transactions, however
9241         // by consensus rules, outputs of a coinbase transaction are encumbered by a 100
9242         // block maturity timelock. To ensure that a (non-0conf) channel like this is enforceable
9243         // on-chain, the minimum depth is updated to 100 blocks for coinbase funding transactions.
9244         //
9245         // Note that 0conf channels with coinbase funding transactions are unaffected and are
9246         // immediately operational after opening.
9247         let chanmon_cfgs = create_chanmon_cfgs(2);
9248         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9249         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9250         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9251
9252         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9253         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9254
9255         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9256         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9257
9258         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9259
9260         // Create the coinbase funding transaction.
9261         let (temporary_channel_id, tx, _) = create_coinbase_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
9262
9263         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9264         check_added_monitors!(nodes[0], 0);
9265         let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9266
9267         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9268         check_added_monitors!(nodes[1], 1);
9269         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9270
9271         let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9272
9273         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
9274         check_added_monitors!(nodes[0], 1);
9275
9276         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9277         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
9278
9279         // Starting at height 0, we "confirm" the coinbase at height 1.
9280         confirm_transaction_at(&nodes[0], &tx, 1);
9281         // We connect 98 more blocks to have 99 confirmations for the coinbase transaction.
9282         connect_blocks(&nodes[0], COINBASE_MATURITY - 2);
9283         // Check that we have no pending message events (we have not queued a `channel_ready` yet).
9284         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
9285         // Now connect one more block which results in 100 confirmations of the coinbase transaction.
9286         connect_blocks(&nodes[0], 1);
9287         // There should now be a `channel_ready` which can be handled.
9288         let _ = &nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(&nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
9289
9290         confirm_transaction_at(&nodes[1], &tx, 1);
9291         connect_blocks(&nodes[1], COINBASE_MATURITY - 2);
9292         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
9293         connect_blocks(&nodes[1], 1);
9294         expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
9295         create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
9296 }
9297
9298 fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
9299         // In the first version of the chain::Confirm interface, after a refactor was made to not
9300         // broadcast CSV-locked transactions until their CSV lock is up, we wouldn't reliably broadcast
9301         // transactions after a `transactions_confirmed` call. Specifically, if the chain, provided via
9302         // `best_block_updated` is at height N, and a transaction output which we wish to spend at
9303         // height N-1 (due to a CSV to height N-1) is provided at height N, we will not broadcast the
9304         // spending transaction until height N+1 (or greater). This was due to the way
9305         // `ChannelMonitor::transactions_confirmed` worked, only checking if we should broadcast a
9306         // spending transaction at the height the input transaction was confirmed at, not whether we
9307         // should broadcast a spending transaction at the current height.
9308         // A second, similar, issue involved failing HTLCs backwards - because we only provided the
9309         // height at which transactions were confirmed to `OnchainTx::update_claims_view`, it wasn't
9310         // aware that the anti-reorg-delay had, in fact, already expired, waiting to fail-backwards
9311         // until we learned about an additional block.
9312         //
9313         // As an additional check, if `test_height_before_timelock` is set, we instead test that we
9314         // aren't broadcasting transactions too early (ie not broadcasting them at all).
9315         let chanmon_cfgs = create_chanmon_cfgs(3);
9316         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9317         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9318         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9319         *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
9320
9321         create_announced_chan_between_nodes(&nodes, 0, 1);
9322         let (chan_announce, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
9323         let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
9324         nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
9325         nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9326
9327         nodes[1].node.force_close_broadcasting_latest_txn(&channel_id, &nodes[2].node.get_our_node_id()).unwrap();
9328         check_closed_broadcast!(nodes[1], true);
9329         check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
9330         check_added_monitors!(nodes[1], 1);
9331         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9332         assert_eq!(node_txn.len(), 1);
9333
9334         let conf_height = nodes[1].best_block_info().1;
9335         if !test_height_before_timelock {
9336                 connect_blocks(&nodes[1], 24 * 6);
9337         }
9338         nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9339                 &nodes[1].get_block_header(conf_height), &[(0, &node_txn[0])], conf_height);
9340         if test_height_before_timelock {
9341                 // If we confirmed the close transaction, but timelocks have not yet expired, we should not
9342                 // generate any events or broadcast any transactions
9343                 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
9344                 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
9345         } else {
9346                 // We should broadcast an HTLC transaction spending our funding transaction first
9347                 let spending_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9348                 assert_eq!(spending_txn.len(), 2);
9349                 assert_eq!(spending_txn[0].txid(), node_txn[0].txid());
9350                 check_spends!(spending_txn[1], node_txn[0]);
9351                 // We should also generate a SpendableOutputs event with the to_self output (as its
9352                 // timelock is up).
9353                 let descriptor_spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
9354                 assert_eq!(descriptor_spend_txn.len(), 1);
9355
9356                 // If we also discover that the HTLC-Timeout transaction was confirmed some time ago, we
9357                 // should immediately fail-backwards the HTLC to the previous hop, without waiting for an
9358                 // additional block built on top of the current chain.
9359                 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9360                         &nodes[1].get_block_header(conf_height + 1), &[(0, &spending_txn[1])], conf_height + 1);
9361                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: channel_id }]);
9362                 check_added_monitors!(nodes[1], 1);
9363
9364                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9365                 assert!(updates.update_add_htlcs.is_empty());
9366                 assert!(updates.update_fulfill_htlcs.is_empty());
9367                 assert_eq!(updates.update_fail_htlcs.len(), 1);
9368                 assert!(updates.update_fail_malformed_htlcs.is_empty());
9369                 assert!(updates.update_fee.is_none());
9370                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
9371                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
9372                 expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_announce.contents.short_channel_id, true);
9373         }
9374 }
9375
9376 #[test]
9377 fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
9378         do_test_tx_confirmed_skipping_blocks_immediate_broadcast(false);
9379         do_test_tx_confirmed_skipping_blocks_immediate_broadcast(true);
9380 }
9381
9382 fn do_test_dup_htlc_second_rejected(test_for_second_fail_panic: bool) {
9383         let chanmon_cfgs = create_chanmon_cfgs(2);
9384         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9385         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9386         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9387
9388         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9389
9390         let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
9391                 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
9392         let route = get_route!(nodes[0], payment_params, 10_000).unwrap();
9393
9394         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
9395
9396         {
9397                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9398                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
9399                 check_added_monitors!(nodes[0], 1);
9400                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9401                 assert_eq!(events.len(), 1);
9402                 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9403                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9404                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9405         }
9406         expect_pending_htlcs_forwardable!(nodes[1]);
9407         expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 10_000);
9408
9409         {
9410                 // Note that we use a different PaymentId here to allow us to duplicativly pay
9411                 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9412                         RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_secret.0)).unwrap();
9413                 check_added_monitors!(nodes[0], 1);
9414                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9415                 assert_eq!(events.len(), 1);
9416                 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9417                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9418                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9419                 // At this point, nodes[1] would notice it has too much value for the payment. It will
9420                 // assume the second is a privacy attack (no longer particularly relevant
9421                 // post-payment_secrets) and fail back the new HTLC. Previously, it'd also have failed back
9422                 // the first HTLC delivered above.
9423         }
9424
9425         expect_pending_htlcs_forwardable_ignore!(nodes[1]);
9426         nodes[1].node.process_pending_htlc_forwards();
9427
9428         if test_for_second_fail_panic {
9429                 // Now we go fail back the first HTLC from the user end.
9430                 nodes[1].node.fail_htlc_backwards(&our_payment_hash);
9431
9432                 let expected_destinations = vec![
9433                         HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9434                         HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9435                 ];
9436                 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1],  expected_destinations);
9437                 nodes[1].node.process_pending_htlc_forwards();
9438
9439                 check_added_monitors!(nodes[1], 1);
9440                 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9441                 assert_eq!(fail_updates_1.update_fail_htlcs.len(), 2);
9442
9443                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9444                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[1]);
9445                 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9446
9447                 let failure_events = nodes[0].node.get_and_clear_pending_events();
9448                 assert_eq!(failure_events.len(), 4);
9449                 if let Event::PaymentPathFailed { .. } = failure_events[0] {} else { panic!(); }
9450                 if let Event::PaymentFailed { .. } = failure_events[1] {} else { panic!(); }
9451                 if let Event::PaymentPathFailed { .. } = failure_events[2] {} else { panic!(); }
9452                 if let Event::PaymentFailed { .. } = failure_events[3] {} else { panic!(); }
9453         } else {
9454                 // Let the second HTLC fail and claim the first
9455                 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9456                 nodes[1].node.process_pending_htlc_forwards();
9457
9458                 check_added_monitors!(nodes[1], 1);
9459                 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9460                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9461                 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9462
9463                 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new());
9464
9465                 claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
9466         }
9467 }
9468
9469 #[test]
9470 fn test_dup_htlc_second_fail_panic() {
9471         // Previously, if we received two HTLCs back-to-back, where the second overran the expected
9472         // value for the payment, we'd fail back both HTLCs after generating a `PaymentClaimable` event.
9473         // Then, if the user failed the second payment, they'd hit a "tried to fail an already failed
9474         // HTLC" debug panic. This tests for this behavior, checking that only one HTLC is auto-failed.
9475         do_test_dup_htlc_second_rejected(true);
9476 }
9477
9478 #[test]
9479 fn test_dup_htlc_second_rejected() {
9480         // Test that if we receive a second HTLC for an MPP payment that overruns the payment amount we
9481         // simply reject the second HTLC but are still able to claim the first HTLC.
9482         do_test_dup_htlc_second_rejected(false);
9483 }
9484
9485 #[test]
9486 fn test_inconsistent_mpp_params() {
9487         // Test that if we recieve two HTLCs with different payment parameters we fail back the first
9488         // such HTLC and allow the second to stay.
9489         let chanmon_cfgs = create_chanmon_cfgs(4);
9490         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9491         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9492         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9493
9494         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9495         create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9496         create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9497         let chan_2_3 =create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9498
9499         let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
9500                 .with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
9501         let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
9502         assert_eq!(route.paths.len(), 2);
9503         route.paths.sort_by(|path_a, _| {
9504                 // Sort the path so that the path through nodes[1] comes first
9505                 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9506                         core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9507         });
9508
9509         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[3]);
9510
9511         let cur_height = nodes[0].best_block_info().1;
9512         let payment_id = PaymentId([42; 32]);
9513
9514         let session_privs = {
9515                 // We create a fake route here so that we start with three pending HTLCs, which we'll
9516                 // ultimately have, just not right away.
9517                 let mut dup_route = route.clone();
9518                 dup_route.paths.push(route.paths[1].clone());
9519                 nodes[0].node.test_add_new_pending_payment(our_payment_hash,
9520                         RecipientOnionFields::secret_only(our_payment_secret), payment_id, &dup_route).unwrap()
9521         };
9522         nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
9523                 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9524                 &None, session_privs[0]).unwrap();
9525         check_added_monitors!(nodes[0], 1);
9526
9527         {
9528                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9529                 assert_eq!(events.len(), 1);
9530                 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), false, None);
9531         }
9532         assert!(nodes[3].node.get_and_clear_pending_events().is_empty());
9533
9534         nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9535                 RecipientOnionFields::secret_only(our_payment_secret), 14_000_000, cur_height, payment_id, &None, session_privs[1]).unwrap();
9536         check_added_monitors!(nodes[0], 1);
9537
9538         {
9539                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9540                 assert_eq!(events.len(), 1);
9541                 let payment_event = SendEvent::from_event(events.pop().unwrap());
9542
9543                 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9544                 commitment_signed_dance!(nodes[2], nodes[0], payment_event.commitment_msg, false);
9545
9546                 expect_pending_htlcs_forwardable!(nodes[2]);
9547                 check_added_monitors!(nodes[2], 1);
9548
9549                 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
9550                 assert_eq!(events.len(), 1);
9551                 let payment_event = SendEvent::from_event(events.pop().unwrap());
9552
9553                 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
9554                 check_added_monitors!(nodes[3], 0);
9555                 commitment_signed_dance!(nodes[3], nodes[2], payment_event.commitment_msg, true, true);
9556
9557                 // At this point, nodes[3] should notice the two HTLCs don't contain the same total payment
9558                 // amount. It will assume the second is a privacy attack (no longer particularly relevant
9559                 // post-payment_secrets) and fail back the new HTLC.
9560         }
9561         expect_pending_htlcs_forwardable_ignore!(nodes[3]);
9562         nodes[3].node.process_pending_htlc_forwards();
9563         expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9564         nodes[3].node.process_pending_htlc_forwards();
9565
9566         check_added_monitors!(nodes[3], 1);
9567
9568         let fail_updates_1 = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
9569         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9570         commitment_signed_dance!(nodes[2], nodes[3], fail_updates_1.commitment_signed, false);
9571
9572         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }]);
9573         check_added_monitors!(nodes[2], 1);
9574
9575         let fail_updates_2 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
9576         nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
9577         commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
9578
9579         expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
9580
9581         nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9582                 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9583                 &None, session_privs[2]).unwrap();
9584         check_added_monitors!(nodes[0], 1);
9585
9586         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9587         assert_eq!(events.len(), 1);
9588         pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), true, None);
9589
9590         do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, our_payment_preimage);
9591         expect_payment_sent(&nodes[0], our_payment_preimage, Some(None), true, true);
9592 }
9593
9594 #[test]
9595 fn test_double_partial_claim() {
9596         // Test what happens if a node receives a payment, generates a PaymentClaimable event, the HTLCs
9597         // time out, the sender resends only some of the MPP parts, then the user processes the
9598         // PaymentClaimable event, ensuring they don't inadvertently claim only part of the full payment
9599         // amount.
9600         let chanmon_cfgs = create_chanmon_cfgs(4);
9601         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9602         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9603         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9604
9605         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9606         create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9607         create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9608         create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9609
9610         let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
9611         assert_eq!(route.paths.len(), 2);
9612         route.paths.sort_by(|path_a, _| {
9613                 // Sort the path so that the path through nodes[1] comes first
9614                 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9615                         core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9616         });
9617
9618         send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 15_000_000, payment_hash, payment_secret);
9619         // nodes[3] has now received a PaymentClaimable event...which it will take some (exorbitant)
9620         // amount of time to respond to.
9621
9622         // Connect some blocks to time out the payment
9623         connect_blocks(&nodes[3], TEST_FINAL_CLTV);
9624         connect_blocks(&nodes[0], TEST_FINAL_CLTV); // To get the same height for sending later
9625
9626         let failed_destinations = vec![
9627                 HTLCDestination::FailedPayment { payment_hash },
9628                 HTLCDestination::FailedPayment { payment_hash },
9629         ];
9630         expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations);
9631
9632         pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
9633
9634         // nodes[1] now retries one of the two paths...
9635         nodes[0].node.send_payment_with_route(&route, payment_hash,
9636                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9637         check_added_monitors!(nodes[0], 2);
9638
9639         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9640         assert_eq!(events.len(), 2);
9641         let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
9642         pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
9643
9644         // At this point nodes[3] has received one half of the payment, and the user goes to handle
9645         // that PaymentClaimable event they got hours ago and never handled...we should refuse to claim.
9646         nodes[3].node.claim_funds(payment_preimage);
9647         check_added_monitors!(nodes[3], 0);
9648         assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
9649 }
9650
9651 /// The possible events which may trigger a `max_dust_htlc_exposure` breach
9652 #[derive(Clone, Copy, PartialEq)]
9653 enum ExposureEvent {
9654         /// Breach occurs at HTLC forwarding (see `send_htlc`)
9655         AtHTLCForward,
9656         /// Breach occurs at HTLC reception (see `update_add_htlc`)
9657         AtHTLCReception,
9658         /// Breach occurs at outbound update_fee (see `send_update_fee`)
9659         AtUpdateFeeOutbound,
9660 }
9661
9662 fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool, multiplier_dust_limit: bool) {
9663         // Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
9664         // policy.
9665         //
9666         // At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9667         // trimmed-to-dust HTLC outbound balance and this new payment as included on next
9668         // counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the
9669         // update. At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC
9670         // inbound and trimmed-to-dust HTLC outbound balance and this new received HTLC as included
9671         // on next counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail
9672         // the update. Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel
9673         // might be available again for HTLC processing once the dust bandwidth has cleared up.
9674
9675         let chanmon_cfgs = create_chanmon_cfgs(2);
9676         let mut config = test_default_channel_config();
9677         config.channel_config.max_dust_htlc_exposure = if multiplier_dust_limit {
9678                 // Default test fee estimator rate is 253 sat/kw, so we set the multiplier to 5_000_000 / 253
9679                 // to get roughly the same initial value as the default setting when this test was
9680                 // originally written.
9681                 MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253)
9682         } else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000) }; // initial default setting value
9683         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9684         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
9685         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9686
9687         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
9688         let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9689         open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9690         open_channel.max_accepted_htlcs = 60;
9691         if on_holder_tx {
9692                 open_channel.dust_limit_satoshis = 546;
9693         }
9694         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9695         let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9696         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9697
9698         let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
9699
9700         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
9701
9702         if on_holder_tx {
9703                 let mut node_0_per_peer_lock;
9704                 let mut node_0_peer_state_lock;
9705                 match get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, temporary_channel_id) {
9706                         ChannelPhase::UnfundedOutboundV1(chan) => {
9707                                 chan.context.holder_dust_limit_satoshis = 546;
9708                         },
9709                         _ => panic!("Unexpected ChannelPhase variant"),
9710                 }
9711         }
9712
9713         nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9714         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()));
9715         check_added_monitors!(nodes[1], 1);
9716         expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9717
9718         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
9719         check_added_monitors!(nodes[0], 1);
9720         expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9721
9722         let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9723         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9724         update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9725
9726         // Fetch a route in advance as we will be unable to once we're unable to send.
9727         let (mut route, payment_hash, _, payment_secret) =
9728                 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
9729
9730         let (dust_buffer_feerate, max_dust_htlc_exposure_msat) = {
9731                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9732                 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9733                 let chan = chan_lock.channel_by_id.get(&channel_id).unwrap();
9734                 (chan.context().get_dust_buffer_feerate(None) as u64,
9735                 chan.context().get_max_dust_htlc_exposure_msat(&LowerBoundedFeeEstimator(nodes[0].fee_estimator)))
9736         };
9737         let dust_outbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_timeout_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9738         let dust_outbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
9739
9740         let dust_inbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_success_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9741         let dust_inbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
9742
9743         let dust_htlc_on_counterparty_tx: u64 = 4;
9744         let dust_htlc_on_counterparty_tx_msat: u64 = max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
9745
9746         if on_holder_tx {
9747                 if dust_outbound_balance {
9748                         // Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9749                         // Outbound dust balance: 4372 sats
9750                         // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9751                         for _ in 0..dust_outbound_htlc_on_holder_tx {
9752                                 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_outbound_htlc_on_holder_tx_msat);
9753                                 nodes[0].node.send_payment_with_route(&route, payment_hash,
9754                                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9755                         }
9756                 } else {
9757                         // Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9758                         // Inbound dust balance: 4372 sats
9759                         // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9760                         for _ in 0..dust_inbound_htlc_on_holder_tx {
9761                                 route_payment(&nodes[1], &[&nodes[0]], dust_inbound_htlc_on_holder_tx_msat);
9762                         }
9763                 }
9764         } else {
9765                 if dust_outbound_balance {
9766                         // Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9767                         // Outbound dust balance: 5000 sats
9768                         for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9769                                 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_htlc_on_counterparty_tx_msat);
9770                                 nodes[0].node.send_payment_with_route(&route, payment_hash,
9771                                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9772                         }
9773                 } else {
9774                         // Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9775                         // Inbound dust balance: 5000 sats
9776                         for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9777                                 route_payment(&nodes[1], &[&nodes[0]], dust_htlc_on_counterparty_tx_msat);
9778                         }
9779                 }
9780         }
9781
9782         if exposure_breach_event == ExposureEvent::AtHTLCForward {
9783                 route.paths[0].hops.last_mut().unwrap().fee_msat =
9784                         if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 1 };
9785                 // With default dust exposure: 5000 sats
9786                 if on_holder_tx {
9787                         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9788                                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9789                                 ), true, APIError::ChannelUnavailable { .. }, {});
9790                 } else {
9791                         unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9792                                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9793                                 ), true, APIError::ChannelUnavailable { .. }, {});
9794                 }
9795         } else if exposure_breach_event == ExposureEvent::AtHTLCReception {
9796                 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { dust_inbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 4 });
9797                 nodes[1].node.send_payment_with_route(&route, payment_hash,
9798                         RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9799                 check_added_monitors!(nodes[1], 1);
9800                 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
9801                 assert_eq!(events.len(), 1);
9802                 let payment_event = SendEvent::from_event(events.remove(0));
9803                 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
9804                 // With default dust exposure: 5000 sats
9805                 if on_holder_tx {
9806                         // Outbound dust balance: 6399 sats
9807                         let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
9808                         let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
9809                         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, max_dust_htlc_exposure_msat), 1);
9810                 } else {
9811                         // Outbound dust balance: 5200 sats
9812                         nodes[0].logger.assert_log("lightning::ln::channel".to_string(),
9813                                 format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
9814                                         dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx - 1) + dust_htlc_on_counterparty_tx_msat + 4,
9815                                         max_dust_htlc_exposure_msat), 1);
9816                 }
9817         } else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {
9818                 route.paths[0].hops.last_mut().unwrap().fee_msat = 2_500_000;
9819                 // For the multiplier dust exposure limit, since it scales with feerate,
9820                 // we need to add a lot of HTLCs that will become dust at the new feerate
9821                 // to cross the threshold.
9822                 for _ in 0..20 {
9823                         let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(1_000), None);
9824                         nodes[0].node.send_payment_with_route(&route, payment_hash,
9825                                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9826                 }
9827                 {
9828                         let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9829                         *feerate_lock = *feerate_lock * 10;
9830                 }
9831                 nodes[0].node.timer_tick_occurred();
9832                 check_added_monitors!(nodes[0], 1);
9833                 nodes[0].logger.assert_log_contains("lightning::ln::channel", "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure", 1);
9834         }
9835
9836         let _ = nodes[0].node.get_and_clear_pending_msg_events();
9837         let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9838         added_monitors.clear();
9839 }
9840
9841 fn do_test_max_dust_htlc_exposure_by_threshold_type(multiplier_dust_limit: bool) {
9842         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9843         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9844         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9845         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9846         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9847         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9848         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9849         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9850         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9851         do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9852         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9853         do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9854 }
9855
9856 #[test]
9857 fn test_max_dust_htlc_exposure() {
9858         do_test_max_dust_htlc_exposure_by_threshold_type(false);
9859         do_test_max_dust_htlc_exposure_by_threshold_type(true);
9860 }
9861
9862 #[test]
9863 fn test_non_final_funding_tx() {
9864         let chanmon_cfgs = create_chanmon_cfgs(2);
9865         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9866         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9867         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9868
9869         let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
9870         let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9871         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
9872         let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9873         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
9874
9875         let best_height = nodes[0].node.best_block.read().unwrap().height();
9876
9877         let chan_id = *nodes[0].network_chan_count.borrow();
9878         let events = nodes[0].node.get_and_clear_pending_events();
9879         let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::Script::new(), sequence: Sequence(1), witness: Witness::from_vec(vec!(vec!(1))) };
9880         assert_eq!(events.len(), 1);
9881         let mut tx = match events[0] {
9882                 Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
9883                         // Timelock the transaction _beyond_ the best client height + 1.
9884                         Transaction { version: chan_id as i32, lock_time: PackedLockTime(best_height + 2), input: vec![input], output: vec![TxOut {
9885                                 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
9886                         }]}
9887                 },
9888                 _ => panic!("Unexpected event"),
9889         };
9890         // Transaction should fail as it's evaluated as non-final for propagation.
9891         match nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()) {
9892                 Err(APIError::APIMisuseError { err }) => {
9893                         assert_eq!(format!("Funding transaction absolute timelock is non-final"), err);
9894                 },
9895                 _ => panic!()
9896         }
9897
9898         // However, transaction should be accepted if it's in a +1 headroom from best block.
9899         tx.lock_time = PackedLockTime(tx.lock_time.0 - 1);
9900         assert!(nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
9901         get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9902 }
9903
9904 #[test]
9905 fn accept_busted_but_better_fee() {
9906         // If a peer sends us a fee update that is too low, but higher than our previous channel
9907         // feerate, we should accept it. In the future we may want to consider closing the channel
9908         // later, but for now we only accept the update.
9909         let mut chanmon_cfgs = create_chanmon_cfgs(2);
9910         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9911         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9912         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9913
9914         create_chan_between_nodes(&nodes[0], &nodes[1]);
9915
9916         // Set nodes[1] to expect 5,000 sat/kW.
9917         {
9918                 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
9919                 *feerate_lock = 5000;
9920         }
9921
9922         // If nodes[0] increases their feerate, even if its not enough, nodes[1] should accept it.
9923         {
9924                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9925                 *feerate_lock = 1000;
9926         }
9927         nodes[0].node.timer_tick_occurred();
9928         check_added_monitors!(nodes[0], 1);
9929
9930         let events = nodes[0].node.get_and_clear_pending_msg_events();
9931         assert_eq!(events.len(), 1);
9932         match events[0] {
9933                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
9934                         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9935                         commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
9936                 },
9937                 _ => panic!("Unexpected event"),
9938         };
9939
9940         // If nodes[0] increases their feerate further, even if its not enough, nodes[1] should accept
9941         // it.
9942         {
9943                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9944                 *feerate_lock = 2000;
9945         }
9946         nodes[0].node.timer_tick_occurred();
9947         check_added_monitors!(nodes[0], 1);
9948
9949         let events = nodes[0].node.get_and_clear_pending_msg_events();
9950         assert_eq!(events.len(), 1);
9951         match events[0] {
9952                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
9953                         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9954                         commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
9955                 },
9956                 _ => panic!("Unexpected event"),
9957         };
9958
9959         // However, if nodes[0] decreases their feerate, nodes[1] should reject it and close the
9960         // channel.
9961         {
9962                 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9963                 *feerate_lock = 1000;
9964         }
9965         nodes[0].node.timer_tick_occurred();
9966         check_added_monitors!(nodes[0], 1);
9967
9968         let events = nodes[0].node.get_and_clear_pending_msg_events();
9969         assert_eq!(events.len(), 1);
9970         match events[0] {
9971                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
9972                         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9973                         check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError {
9974                                 err: "Peer's feerate much too low. Actual: 1000. Our expected lower limit: 5000 (- 250)".to_owned() },
9975                                 [nodes[0].node.get_our_node_id()], 100000);
9976                         check_closed_broadcast!(nodes[1], true);
9977                         check_added_monitors!(nodes[1], 1);
9978                 },
9979                 _ => panic!("Unexpected event"),
9980         };
9981 }
9982
9983 fn do_payment_with_custom_min_final_cltv_expiry(valid_delta: bool, use_user_hash: bool) {
9984         let mut chanmon_cfgs = create_chanmon_cfgs(2);
9985         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9986         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9987         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9988         let min_final_cltv_expiry_delta = 120;
9989         let final_cltv_expiry_delta = if valid_delta { min_final_cltv_expiry_delta + 2 } else {
9990                 min_final_cltv_expiry_delta - 2 };
9991         let recv_value = 100_000;
9992
9993         create_chan_between_nodes(&nodes[0], &nodes[1]);
9994
9995         let payment_parameters = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), final_cltv_expiry_delta as u32);
9996         let (payment_hash, payment_preimage, payment_secret) = if use_user_hash {
9997                 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1],
9998                         Some(recv_value), Some(min_final_cltv_expiry_delta));
9999                 (payment_hash, payment_preimage, payment_secret)
10000         } else {
10001                 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(recv_value), 7200, Some(min_final_cltv_expiry_delta)).unwrap();
10002                 (payment_hash, nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap(), payment_secret)
10003         };
10004         let route = get_route!(nodes[0], payment_parameters, recv_value).unwrap();
10005         nodes[0].node.send_payment_with_route(&route, payment_hash,
10006                 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
10007         check_added_monitors!(nodes[0], 1);
10008         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10009         assert_eq!(events.len(), 1);
10010         let mut payment_event = SendEvent::from_event(events.pop().unwrap());
10011         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10012         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10013         expect_pending_htlcs_forwardable!(nodes[1]);
10014
10015         if valid_delta {
10016                 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, recv_value, if use_user_hash {
10017                         None } else { Some(payment_preimage) }, nodes[1].node.get_our_node_id());
10018
10019                 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
10020         } else {
10021                 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10022
10023                 check_added_monitors!(nodes[1], 1);
10024
10025                 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10026                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
10027                 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, false, true);
10028
10029                 expect_payment_failed!(nodes[0], payment_hash, true);
10030         }
10031 }
10032
10033 #[test]
10034 fn test_payment_with_custom_min_cltv_expiry_delta() {
10035         do_payment_with_custom_min_final_cltv_expiry(false, false);
10036         do_payment_with_custom_min_final_cltv_expiry(false, true);
10037         do_payment_with_custom_min_final_cltv_expiry(true, false);
10038         do_payment_with_custom_min_final_cltv_expiry(true, true);
10039 }
10040
10041 #[test]
10042 fn test_disconnects_peer_awaiting_response_ticks() {
10043         // Tests that nodes which are awaiting on a response critical for channel responsiveness
10044         // disconnect their counterparty after `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10045         let mut chanmon_cfgs = create_chanmon_cfgs(2);
10046         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10047         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10048         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10049
10050         // Asserts a disconnect event is queued to the user.
10051         let check_disconnect_event = |node: &Node, should_disconnect: bool| {
10052                 let disconnect_event = node.node.get_and_clear_pending_msg_events().iter().find_map(|event|
10053                         if let MessageSendEvent::HandleError { action, .. } = event {
10054                                 if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
10055                                         Some(())
10056                                 } else {
10057                                         None
10058                                 }
10059                         } else {
10060                                 None
10061                         }
10062                 );
10063                 assert_eq!(disconnect_event.is_some(), should_disconnect);
10064         };
10065
10066         // Fires timer ticks ensuring we only attempt to disconnect peers after reaching
10067         // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10068         let check_disconnect = |node: &Node| {
10069                 // No disconnect without any timer ticks.
10070                 check_disconnect_event(node, false);
10071
10072                 // No disconnect with 1 timer tick less than required.
10073                 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS - 1 {
10074                         node.node.timer_tick_occurred();
10075                         check_disconnect_event(node, false);
10076                 }
10077
10078                 // Disconnect after reaching the required ticks.
10079                 node.node.timer_tick_occurred();
10080                 check_disconnect_event(node, true);
10081
10082                 // Disconnect again on the next tick if the peer hasn't been disconnected yet.
10083                 node.node.timer_tick_occurred();
10084                 check_disconnect_event(node, true);
10085         };
10086
10087         create_chan_between_nodes(&nodes[0], &nodes[1]);
10088
10089         // We'll start by performing a fee update with Alice (nodes[0]) on the channel.
10090         *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
10091         nodes[0].node.timer_tick_occurred();
10092         check_added_monitors!(&nodes[0], 1);
10093         let alice_fee_update = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10094         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), alice_fee_update.update_fee.as_ref().unwrap());
10095         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &alice_fee_update.commitment_signed);
10096         check_added_monitors!(&nodes[1], 1);
10097
10098         // This will prompt Bob (nodes[1]) to respond with his `CommitmentSigned` and `RevokeAndACK`.
10099         let (bob_revoke_and_ack, bob_commitment_signed) = get_revoke_commit_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
10100         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revoke_and_ack);
10101         check_added_monitors!(&nodes[0], 1);
10102         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_commitment_signed);
10103         check_added_monitors(&nodes[0], 1);
10104
10105         // Alice then needs to send her final `RevokeAndACK` to complete the commitment dance. We
10106         // pretend Bob hasn't received the message and check whether he'll disconnect Alice after
10107         // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10108         let alice_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
10109         check_disconnect(&nodes[1]);
10110
10111         // Now, we'll reconnect them to test awaiting a `ChannelReestablish` message.
10112         //
10113         // Note that since the commitment dance didn't complete above, Alice is expected to resend her
10114         // final `RevokeAndACK` to Bob to complete it.
10115         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10116         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10117         let bob_init = msgs::Init {
10118                 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
10119         };
10120         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &bob_init, true).unwrap();
10121         let alice_init = msgs::Init {
10122                 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
10123         };
10124         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &alice_init, true).unwrap();
10125
10126         // Upon reconnection, Alice sends her `ChannelReestablish` to Bob. Alice, however, hasn't
10127         // received Bob's yet, so she should disconnect him after reaching
10128         // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10129         let alice_channel_reestablish = get_event_msg!(
10130                 nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()
10131         );
10132         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &alice_channel_reestablish);
10133         check_disconnect(&nodes[0]);
10134
10135         // Bob now sends his `ChannelReestablish` to Alice to resume the channel and consider it "live".
10136         let bob_channel_reestablish = nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(|event|
10137                 if let MessageSendEvent::SendChannelReestablish { node_id, msg } = event {
10138                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
10139                         Some(msg.clone())
10140                 } else {
10141                         None
10142                 }
10143         ).unwrap();
10144         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bob_channel_reestablish);
10145
10146         // Sanity check that Alice won't disconnect Bob since she's no longer waiting for any messages.
10147         for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10148                 nodes[0].node.timer_tick_occurred();
10149                 check_disconnect_event(&nodes[0], false);
10150         }
10151
10152         // However, Bob is still waiting on Alice's `RevokeAndACK`, so he should disconnect her after
10153         // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10154         check_disconnect(&nodes[1]);
10155
10156         // Finally, have Bob process the last message.
10157         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &alice_revoke_and_ack);
10158         check_added_monitors(&nodes[1], 1);
10159
10160         // At this point, neither node should attempt to disconnect each other, since they aren't
10161         // waiting on any messages.
10162         for node in &nodes {
10163                 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10164                         node.node.timer_tick_occurred();
10165                         check_disconnect_event(node, false);
10166                 }
10167         }
10168 }
10169
10170 #[test]
10171 fn test_remove_expired_outbound_unfunded_channels() {
10172         let chanmon_cfgs = create_chanmon_cfgs(2);
10173         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10174         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10175         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10176
10177         let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
10178         let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10179         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10180         let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10181         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10182
10183         let events = nodes[0].node.get_and_clear_pending_events();
10184         assert_eq!(events.len(), 1);
10185         match events[0] {
10186                 Event::FundingGenerationReady { .. } => (),
10187                 _ => panic!("Unexpected event"),
10188         };
10189
10190         // Asserts the outbound channel has been removed from a nodes[0]'s peer state map.
10191         let check_outbound_channel_existence = |should_exist: bool| {
10192                 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
10193                 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
10194                 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10195         };
10196
10197         // Channel should exist without any timer ticks.
10198         check_outbound_channel_existence(true);
10199
10200         // Channel should exist with 1 timer tick less than required.
10201         for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10202                 nodes[0].node.timer_tick_occurred();
10203                 check_outbound_channel_existence(true)
10204         }
10205
10206         // Remove channel after reaching the required ticks.
10207         nodes[0].node.timer_tick_occurred();
10208         check_outbound_channel_existence(false);
10209
10210         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
10211         assert_eq!(msg_events.len(), 1);
10212         match msg_events[0] {
10213                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10214                         assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10215                 },
10216                 _ => panic!("Unexpected event"),
10217         }
10218         check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false, &[nodes[1].node.get_our_node_id()], 100000);
10219 }
10220
10221 #[test]
10222 fn test_remove_expired_inbound_unfunded_channels() {
10223         let chanmon_cfgs = create_chanmon_cfgs(2);
10224         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10225         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10226         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10227
10228         let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
10229         let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10230         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10231         let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10232         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10233
10234         let events = nodes[0].node.get_and_clear_pending_events();
10235         assert_eq!(events.len(), 1);
10236         match events[0] {
10237                 Event::FundingGenerationReady { .. } => (),
10238                 _ => panic!("Unexpected event"),
10239         };
10240
10241         // Asserts the inbound channel has been removed from a nodes[1]'s peer state map.
10242         let check_inbound_channel_existence = |should_exist: bool| {
10243                 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10244                 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
10245                 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10246         };
10247
10248         // Channel should exist without any timer ticks.
10249         check_inbound_channel_existence(true);
10250
10251         // Channel should exist with 1 timer tick less than required.
10252         for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10253                 nodes[1].node.timer_tick_occurred();
10254                 check_inbound_channel_existence(true)
10255         }
10256
10257         // Remove channel after reaching the required ticks.
10258         nodes[1].node.timer_tick_occurred();
10259         check_inbound_channel_existence(false);
10260
10261         let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
10262         assert_eq!(msg_events.len(), 1);
10263         match msg_events[0] {
10264                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10265                         assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10266                 },
10267                 _ => panic!("Unexpected event"),
10268         }
10269         check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false, &[nodes[0].node.get_our_node_id()], 100000);
10270 }
10271
10272 fn do_test_multi_post_event_actions(do_reload: bool) {
10273         // Tests handling multiple post-Event actions at once.
10274         // There is specific code in ChannelManager to handle channels where multiple post-Event
10275         // `ChannelMonitorUpdates` are pending at once. This test exercises that code.
10276         //
10277         // Specifically, we test calling `get_and_clear_pending_events` while there are two
10278         // PaymentSents from different channels and one channel has two pending `ChannelMonitorUpdate`s
10279         // - one from an RAA and one from an inbound commitment_signed.
10280         let chanmon_cfgs = create_chanmon_cfgs(3);
10281         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10282         let (persister, chain_monitor);
10283         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10284         let nodes_0_deserialized;
10285         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10286
10287         let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
10288         let chan_id_2 = create_announced_chan_between_nodes(&nodes, 0, 2).2;
10289
10290         send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10291         send_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10292
10293         let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10294         let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10295
10296         nodes[1].node.claim_funds(our_payment_preimage);
10297         check_added_monitors!(nodes[1], 1);
10298         expect_payment_claimed!(nodes[1], our_payment_hash, 1_000_000);
10299
10300         nodes[2].node.claim_funds(payment_preimage_2);
10301         check_added_monitors!(nodes[2], 1);
10302         expect_payment_claimed!(nodes[2], payment_hash_2, 1_000_000);
10303
10304         for dest in &[1, 2] {
10305                 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[*dest], nodes[0].node.get_our_node_id());
10306                 nodes[0].node.handle_update_fulfill_htlc(&nodes[*dest].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
10307                 commitment_signed_dance!(nodes[0], nodes[*dest], htlc_fulfill_updates.commitment_signed, false);
10308                 check_added_monitors(&nodes[0], 0);
10309         }
10310
10311         let (route, payment_hash_3, _, payment_secret_3) =
10312                 get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
10313         let payment_id = PaymentId(payment_hash_3.0);
10314         nodes[1].node.send_payment_with_route(&route, payment_hash_3,
10315                 RecipientOnionFields::secret_only(payment_secret_3), payment_id).unwrap();
10316         check_added_monitors(&nodes[1], 1);
10317
10318         let send_event = SendEvent::from_node(&nodes[1]);
10319         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]);
10320         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event.commitment_msg);
10321         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
10322
10323         if do_reload {
10324                 let nodes_0_serialized = nodes[0].node.encode();
10325                 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
10326                 let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_2).encode();
10327                 reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, chain_monitor, nodes_0_deserialized);
10328
10329                 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10330                 nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10331
10332                 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
10333                 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[2]));
10334         }
10335
10336         let events = nodes[0].node.get_and_clear_pending_events();
10337         assert_eq!(events.len(), 4);
10338         if let Event::PaymentSent { payment_preimage, .. } = events[0] {
10339                 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10340         } else { panic!(); }
10341         if let Event::PaymentSent { payment_preimage, .. } = events[1] {
10342                 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10343         } else { panic!(); }
10344         if let Event::PaymentPathSuccessful { .. } = events[2] {} else { panic!(); }
10345         if let Event::PaymentPathSuccessful { .. } = events[3] {} else { panic!(); }
10346
10347         // After the events are processed, the ChannelMonitorUpdates will be released and, upon their
10348         // completion, we'll respond to nodes[1] with an RAA + CS.
10349         get_revoke_commit_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10350         check_added_monitors(&nodes[0], 3);
10351 }
10352
10353 #[test]
10354 fn test_multi_post_event_actions() {
10355         do_test_multi_post_event_actions(true);
10356         do_test_multi_post_event_actions(false);
10357 }