]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/ln/functional_tests.rs
Pass InitFeatures by reference to Channel
[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 chain;
15 use chain::{Confirm, Listen, Watch};
16 use chain::channelmonitor;
17 use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
18 use chain::transaction::OutPoint;
19 use chain::keysinterface::{KeysInterface, BaseSign};
20 use ln::{PaymentPreimage, PaymentSecret, PaymentHash};
21 use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
22 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA};
23 use ln::channel::{Channel, ChannelError};
24 use ln::{chan_utils, onion_utils};
25 use ln::chan_utils::HTLC_SUCCESS_TX_WEIGHT;
26 use routing::router::{Route, RouteHop, RouteHint, RouteHintHop, get_route, get_keysend_route};
27 use routing::network_graph::RoutingFees;
28 use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
29 use ln::msgs;
30 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, ErrorAction};
31 use ln::script::ShutdownScript;
32 use util::enforcing_trait_impls::EnforcingSigner;
33 use util::{byte_utils, test_utils};
34 use util::test_utils::OnGetShutdownScriptpubkey;
35 use util::events::{Event, MessageSendEvent, MessageSendEventsProvider, PaymentPurpose};
36 use util::errors::APIError;
37 use util::ser::{Writeable, ReadableArgs};
38 use util::config::UserConfig;
39
40 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
41 use bitcoin::hash_types::{Txid, BlockHash};
42 use bitcoin::blockdata::block::{Block, BlockHeader};
43 use bitcoin::blockdata::script::Builder;
44 use bitcoin::blockdata::opcodes;
45 use bitcoin::blockdata::constants::genesis_block;
46 use bitcoin::network::constants::Network;
47
48 use bitcoin::hashes::sha256::Hash as Sha256;
49 use bitcoin::hashes::Hash;
50
51 use bitcoin::secp256k1::{Secp256k1, Message};
52 use bitcoin::secp256k1::key::{PublicKey,SecretKey};
53
54 use regex;
55
56 use io;
57 use prelude::*;
58 use alloc::collections::BTreeSet;
59 use core::default::Default;
60 use core::num::NonZeroU8;
61 use sync::{Arc, Mutex};
62
63 use ln::functional_test_utils::*;
64 use ln::chan_utils::CommitmentTransaction;
65 use ln::msgs::OptionalField::Present;
66
67 #[test]
68 fn test_insane_channel_opens() {
69         // Stand up a network of 2 nodes
70         let chanmon_cfgs = create_chanmon_cfgs(2);
71         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
72         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
73         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
74
75         // Instantiate channel parameters where we push the maximum msats given our
76         // funding satoshis
77         let channel_value_sat = 31337; // same as funding satoshis
78         let channel_reserve_satoshis = Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(channel_value_sat);
79         let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
80
81         // Have node0 initiate a channel to node1 with aforementioned parameters
82         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42, None).unwrap();
83
84         // Extract the channel open message from node0 to node1
85         let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
86
87         // Test helper that asserts we get the correct error string given a mutator
88         // that supposedly makes the channel open message insane
89         let insane_open_helper = |expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
90                 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &message_mutator(open_channel_message.clone()));
91                 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
92                 assert_eq!(msg_events.len(), 1);
93                 let expected_regex = regex::Regex::new(expected_error_str).unwrap();
94                 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
95                         match action {
96                                 &ErrorAction::SendErrorMessage { .. } => {
97                                         nodes[1].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), expected_regex, 1);
98                                 },
99                                 _ => panic!("unexpected event!"),
100                         }
101                 } else { assert!(false); }
102         };
103
104         use ln::channel::MAX_FUNDING_SATOSHIS;
105         use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
106
107         // Test all mutations that would make the channel open message insane
108         insane_open_helper(format!("Funding must be smaller than {}. It was {}", MAX_FUNDING_SATOSHIS, MAX_FUNDING_SATOSHIS).as_str(), |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg });
109
110         insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
111
112         insane_open_helper(r"push_msat \d+ was larger than funding value \d+", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
113
114         insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
115
116         insane_open_helper(r"Bogus; channel reserve \(\d+\) is less than dust limit \(\d+\)", |mut msg| { msg.dust_limit_satoshis = msg.channel_reserve_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_async_inbound_update_fee() {
129         let chanmon_cfgs = create_chanmon_cfgs(2);
130         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
131         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
132         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
133         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
134         let logger = test_utils::TestLogger::new();
135         let channel_id = chan.2;
136
137         // balancing
138         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
139
140         // A                                        B
141         // update_fee                            ->
142         // send (1) commitment_signed            -.
143         //                                       <- update_add_htlc/commitment_signed
144         // send (2) RAA (awaiting remote revoke) -.
145         // (1) commitment_signed is delivered    ->
146         //                                       .- send (3) RAA (awaiting remote revoke)
147         // (2) RAA is delivered                  ->
148         //                                       .- send (4) commitment_signed
149         //                                       <- (3) RAA is delivered
150         // send (5) commitment_signed            -.
151         //                                       <- (4) commitment_signed is delivered
152         // send (6) RAA                          -.
153         // (5) commitment_signed is delivered    ->
154         //                                       <- RAA
155         // (6) RAA is delivered                  ->
156
157         // First nodes[0] generates an update_fee
158         nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
159         check_added_monitors!(nodes[0], 1);
160
161         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
162         assert_eq!(events_0.len(), 1);
163         let (update_msg, commitment_signed) = match events_0[0] { // (1)
164                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
165                         (update_fee.as_ref(), commitment_signed)
166                 },
167                 _ => panic!("Unexpected event"),
168         };
169
170         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
171
172         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
173         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
174         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
175         nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &Some(our_payment_secret)).unwrap();
176         check_added_monitors!(nodes[1], 1);
177
178         let payment_event = {
179                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
180                 assert_eq!(events_1.len(), 1);
181                 SendEvent::from_event(events_1.remove(0))
182         };
183         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
184         assert_eq!(payment_event.msgs.len(), 1);
185
186         // ...now when the messages get delivered everyone should be happy
187         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
188         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
189         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
190         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
191         check_added_monitors!(nodes[0], 1);
192
193         // deliver(1), generate (3):
194         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
195         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
196         // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
197         check_added_monitors!(nodes[1], 1);
198
199         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack); // deliver (2)
200         let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
201         assert!(bs_update.update_add_htlcs.is_empty()); // (4)
202         assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
203         assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
204         assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
205         assert!(bs_update.update_fee.is_none()); // (4)
206         check_added_monitors!(nodes[1], 1);
207
208         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack); // deliver (3)
209         let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
210         assert!(as_update.update_add_htlcs.is_empty()); // (5)
211         assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
212         assert!(as_update.update_fail_htlcs.is_empty()); // (5)
213         assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
214         assert!(as_update.update_fee.is_none()); // (5)
215         check_added_monitors!(nodes[0], 1);
216
217         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed); // deliver (4)
218         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
219         // only (6) so get_event_msg's assert(len == 1) passes
220         check_added_monitors!(nodes[0], 1);
221
222         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed); // deliver (5)
223         let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
224         check_added_monitors!(nodes[1], 1);
225
226         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
227         check_added_monitors!(nodes[0], 1);
228
229         let events_2 = nodes[0].node.get_and_clear_pending_events();
230         assert_eq!(events_2.len(), 1);
231         match events_2[0] {
232                 Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
233                 _ => panic!("Unexpected event"),
234         }
235
236         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke); // deliver (6)
237         check_added_monitors!(nodes[1], 1);
238 }
239
240 #[test]
241 fn test_update_fee_unordered_raa() {
242         // Just the intro to the previous test followed by an out-of-order RAA (which caused a
243         // crash in an earlier version of the update_fee patch)
244         let chanmon_cfgs = create_chanmon_cfgs(2);
245         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
246         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
247         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
248         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
249         let channel_id = chan.2;
250         let logger = test_utils::TestLogger::new();
251
252         // balancing
253         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
254
255         // First nodes[0] generates an update_fee
256         nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
257         check_added_monitors!(nodes[0], 1);
258
259         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
260         assert_eq!(events_0.len(), 1);
261         let update_msg = match events_0[0] { // (1)
262                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
263                         update_fee.as_ref()
264                 },
265                 _ => panic!("Unexpected event"),
266         };
267
268         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
269
270         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
271         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
272         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
273         nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &Some(our_payment_secret)).unwrap();
274         check_added_monitors!(nodes[1], 1);
275
276         let payment_event = {
277                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
278                 assert_eq!(events_1.len(), 1);
279                 SendEvent::from_event(events_1.remove(0))
280         };
281         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
282         assert_eq!(payment_event.msgs.len(), 1);
283
284         // ...now when the messages get delivered everyone should be happy
285         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
286         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
287         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
288         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
289         check_added_monitors!(nodes[0], 1);
290
291         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg); // deliver (2)
292         check_added_monitors!(nodes[1], 1);
293
294         // We can't continue, sadly, because our (1) now has a bogus signature
295 }
296
297 #[test]
298 fn test_multi_flight_update_fee() {
299         let chanmon_cfgs = create_chanmon_cfgs(2);
300         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
301         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
302         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
303         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
304         let channel_id = chan.2;
305
306         // A                                        B
307         // update_fee/commitment_signed          ->
308         //                                       .- send (1) RAA and (2) commitment_signed
309         // update_fee (never committed)          ->
310         // (3) update_fee                        ->
311         // We have to manually generate the above update_fee, it is allowed by the protocol but we
312         // don't track which updates correspond to which revoke_and_ack responses so we're in
313         // AwaitingRAA mode and will not generate the update_fee yet.
314         //                                       <- (1) RAA delivered
315         // (3) is generated and send (4) CS      -.
316         // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
317         // know the per_commitment_point to use for it.
318         //                                       <- (2) commitment_signed delivered
319         // revoke_and_ack                        ->
320         //                                          B should send no response here
321         // (4) commitment_signed delivered       ->
322         //                                       <- RAA/commitment_signed delivered
323         // revoke_and_ack                        ->
324
325         // First nodes[0] generates an update_fee
326         let initial_feerate = get_feerate!(nodes[0], channel_id);
327         nodes[0].node.update_fee(channel_id, initial_feerate + 20).unwrap();
328         check_added_monitors!(nodes[0], 1);
329
330         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
331         assert_eq!(events_0.len(), 1);
332         let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
333                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
334                         (update_fee.as_ref().unwrap(), commitment_signed)
335                 },
336                 _ => panic!("Unexpected event"),
337         };
338
339         // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
340         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1);
341         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1);
342         let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
343         check_added_monitors!(nodes[1], 1);
344
345         // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
346         // transaction:
347         nodes[0].node.update_fee(channel_id, initial_feerate + 40).unwrap();
348         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
349         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
350
351         // Create the (3) update_fee message that nodes[0] will generate before it does...
352         let mut update_msg_2 = msgs::UpdateFee {
353                 channel_id: update_msg_1.channel_id.clone(),
354                 feerate_per_kw: (initial_feerate + 30) as u32,
355         };
356
357         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
358
359         update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
360         // Deliver (3)
361         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
362
363         // Deliver (1), generating (3) and (4)
364         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg);
365         let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
366         check_added_monitors!(nodes[0], 1);
367         assert!(as_second_update.update_add_htlcs.is_empty());
368         assert!(as_second_update.update_fulfill_htlcs.is_empty());
369         assert!(as_second_update.update_fail_htlcs.is_empty());
370         assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
371         // Check that the update_fee newly generated matches what we delivered:
372         assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
373         assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
374
375         // Deliver (2) commitment_signed
376         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
377         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
378         check_added_monitors!(nodes[0], 1);
379         // No commitment_signed so get_event_msg's assert(len == 1) passes
380
381         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg);
382         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
383         check_added_monitors!(nodes[1], 1);
384
385         // Delever (4)
386         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed);
387         let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
388         check_added_monitors!(nodes[1], 1);
389
390         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
391         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
392         check_added_monitors!(nodes[0], 1);
393
394         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment);
395         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
396         // No commitment_signed so get_event_msg's assert(len == 1) passes
397         check_added_monitors!(nodes[0], 1);
398
399         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke);
400         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
401         check_added_monitors!(nodes[1], 1);
402 }
403
404 fn do_test_1_conf_open(connect_style: ConnectStyle) {
405         // Previously, if the minium_depth config was set to 1, we'd never send a funding_locked. This
406         // tests that we properly send one in that case.
407         let mut alice_config = UserConfig::default();
408         alice_config.own_channel_config.minimum_depth = 1;
409         alice_config.channel_options.announced_channel = true;
410         alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
411         let mut bob_config = UserConfig::default();
412         bob_config.own_channel_config.minimum_depth = 1;
413         bob_config.channel_options.announced_channel = true;
414         bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
415         let chanmon_cfgs = create_chanmon_cfgs(2);
416         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
417         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
418         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
419         *nodes[0].connect_style.borrow_mut() = connect_style;
420
421         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
422         mine_transaction(&nodes[1], &tx);
423         nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
424
425         mine_transaction(&nodes[0], &tx);
426         let (funding_locked, _) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
427         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
428
429         for node in nodes {
430                 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
431                 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
432                 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
433         }
434 }
435 #[test]
436 fn test_1_conf_open() {
437         do_test_1_conf_open(ConnectStyle::BestBlockFirst);
438         do_test_1_conf_open(ConnectStyle::TransactionsFirst);
439         do_test_1_conf_open(ConnectStyle::FullBlockViaListen);
440 }
441
442 fn do_test_sanity_on_in_flight_opens(steps: u8) {
443         // Previously, we had issues deserializing channels when we hadn't connected the first block
444         // after creation. To catch that and similar issues, we lean on the Node::drop impl to test
445         // serialization round-trips and simply do steps towards opening a channel and then drop the
446         // Node objects.
447
448         let chanmon_cfgs = create_chanmon_cfgs(2);
449         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
450         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
451         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
452
453         if steps & 0b1000_0000 != 0{
454                 let block = Block {
455                         header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
456                         txdata: vec![],
457                 };
458                 connect_block(&nodes[0], &block);
459                 connect_block(&nodes[1], &block);
460         }
461
462         if steps & 0x0f == 0 { return; }
463         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
464         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
465
466         if steps & 0x0f == 1 { return; }
467         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
468         let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
469
470         if steps & 0x0f == 2 { return; }
471         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
472
473         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 42);
474
475         if steps & 0x0f == 3 { return; }
476         nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
477         check_added_monitors!(nodes[0], 0);
478         let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
479
480         if steps & 0x0f == 4 { return; }
481         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
482         {
483                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
484                 assert_eq!(added_monitors.len(), 1);
485                 assert_eq!(added_monitors[0].0, funding_output);
486                 added_monitors.clear();
487         }
488         let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
489
490         if steps & 0x0f == 5 { return; }
491         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
492         {
493                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
494                 assert_eq!(added_monitors.len(), 1);
495                 assert_eq!(added_monitors[0].0, funding_output);
496                 added_monitors.clear();
497         }
498
499         let events_4 = nodes[0].node.get_and_clear_pending_events();
500         assert_eq!(events_4.len(), 0);
501
502         if steps & 0x0f == 6 { return; }
503         create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);
504
505         if steps & 0x0f == 7 { return; }
506         confirm_transaction_at(&nodes[0], &tx, 2);
507         connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
508         create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
509 }
510
511 #[test]
512 fn test_sanity_on_in_flight_opens() {
513         do_test_sanity_on_in_flight_opens(0);
514         do_test_sanity_on_in_flight_opens(0 | 0b1000_0000);
515         do_test_sanity_on_in_flight_opens(1);
516         do_test_sanity_on_in_flight_opens(1 | 0b1000_0000);
517         do_test_sanity_on_in_flight_opens(2);
518         do_test_sanity_on_in_flight_opens(2 | 0b1000_0000);
519         do_test_sanity_on_in_flight_opens(3);
520         do_test_sanity_on_in_flight_opens(3 | 0b1000_0000);
521         do_test_sanity_on_in_flight_opens(4);
522         do_test_sanity_on_in_flight_opens(4 | 0b1000_0000);
523         do_test_sanity_on_in_flight_opens(5);
524         do_test_sanity_on_in_flight_opens(5 | 0b1000_0000);
525         do_test_sanity_on_in_flight_opens(6);
526         do_test_sanity_on_in_flight_opens(6 | 0b1000_0000);
527         do_test_sanity_on_in_flight_opens(7);
528         do_test_sanity_on_in_flight_opens(7 | 0b1000_0000);
529         do_test_sanity_on_in_flight_opens(8);
530         do_test_sanity_on_in_flight_opens(8 | 0b1000_0000);
531 }
532
533 #[test]
534 fn test_update_fee_vanilla() {
535         let chanmon_cfgs = create_chanmon_cfgs(2);
536         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
537         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
538         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
539         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
540         let channel_id = chan.2;
541
542         let feerate = get_feerate!(nodes[0], channel_id);
543         nodes[0].node.update_fee(channel_id, feerate+25).unwrap();
544         check_added_monitors!(nodes[0], 1);
545
546         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
547         assert_eq!(events_0.len(), 1);
548         let (update_msg, commitment_signed) = match events_0[0] {
549                         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 } } => {
550                         (update_fee.as_ref(), commitment_signed)
551                 },
552                 _ => panic!("Unexpected event"),
553         };
554         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
555
556         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
557         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
558         check_added_monitors!(nodes[1], 1);
559
560         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
561         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
562         check_added_monitors!(nodes[0], 1);
563
564         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
565         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
566         // No commitment_signed so get_event_msg's assert(len == 1) passes
567         check_added_monitors!(nodes[0], 1);
568
569         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
570         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
571         check_added_monitors!(nodes[1], 1);
572 }
573
574 #[test]
575 fn test_update_fee_that_funder_cannot_afford() {
576         let chanmon_cfgs = create_chanmon_cfgs(2);
577         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
578         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
579         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
580         let channel_value = 1888;
581         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 700000, InitFeatures::known(), InitFeatures::known());
582         let channel_id = chan.2;
583
584         let feerate = 260;
585         nodes[0].node.update_fee(channel_id, feerate).unwrap();
586         check_added_monitors!(nodes[0], 1);
587         let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
588
589         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap());
590
591         commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
592
593         //Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
594         //This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
595         {
596                 let commitment_tx = get_local_commitment_txn!(nodes[1], channel_id)[0].clone();
597
598                 //We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
599                 let num_htlcs = commitment_tx.output.len() - 2;
600                 let total_fee: u64 = feerate as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
601                 let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
602                 actual_fee = channel_value - actual_fee;
603                 assert_eq!(total_fee, actual_fee);
604         }
605
606         //Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
607         //fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
608         nodes[0].node.update_fee(channel_id, feerate+2).unwrap();
609         check_added_monitors!(nodes[0], 1);
610
611         let update2_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
612
613         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update2_msg.update_fee.unwrap());
614
615         //While producing the commitment_signed response after handling a received update_fee request the
616         //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
617         //Should produce and error.
618         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update2_msg.commitment_signed);
619         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Funding remote cannot afford proposed new fee".to_string(), 1);
620         check_added_monitors!(nodes[1], 1);
621         check_closed_broadcast!(nodes[1], true);
622 }
623
624 #[test]
625 fn test_update_fee_with_fundee_update_add_htlc() {
626         let chanmon_cfgs = create_chanmon_cfgs(2);
627         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
628         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
629         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
630         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
631         let channel_id = chan.2;
632         let logger = test_utils::TestLogger::new();
633
634         // balancing
635         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
636
637         let feerate = get_feerate!(nodes[0], channel_id);
638         nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
639         check_added_monitors!(nodes[0], 1);
640
641         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
642         assert_eq!(events_0.len(), 1);
643         let (update_msg, commitment_signed) = match events_0[0] {
644                         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 } } => {
645                         (update_fee.as_ref(), commitment_signed)
646                 },
647                 _ => panic!("Unexpected event"),
648         };
649         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
650         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
651         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
652         check_added_monitors!(nodes[1], 1);
653
654         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[0]);
655         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
656         let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 800000, TEST_FINAL_CLTV, &logger).unwrap();
657
658         // nothing happens since node[1] is in AwaitingRemoteRevoke
659         nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
660         {
661                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
662                 assert_eq!(added_monitors.len(), 0);
663                 added_monitors.clear();
664         }
665         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
666         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
667         // node[1] has nothing to do
668
669         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
670         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
671         check_added_monitors!(nodes[0], 1);
672
673         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
674         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
675         // No commitment_signed so get_event_msg's assert(len == 1) passes
676         check_added_monitors!(nodes[0], 1);
677         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
678         check_added_monitors!(nodes[1], 1);
679         // AwaitingRemoteRevoke ends here
680
681         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
682         assert_eq!(commitment_update.update_add_htlcs.len(), 1);
683         assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
684         assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
685         assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
686         assert_eq!(commitment_update.update_fee.is_none(), true);
687
688         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]);
689         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
690         check_added_monitors!(nodes[0], 1);
691         let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
692
693         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke);
694         check_added_monitors!(nodes[1], 1);
695         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
696
697         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
698         check_added_monitors!(nodes[1], 1);
699         let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
700         // No commitment_signed so get_event_msg's assert(len == 1) passes
701
702         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke);
703         check_added_monitors!(nodes[0], 1);
704         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
705
706         expect_pending_htlcs_forwardable!(nodes[0]);
707
708         let events = nodes[0].node.get_and_clear_pending_events();
709         assert_eq!(events.len(), 1);
710         match events[0] {
711                 Event::PaymentReceived { .. } => { },
712                 _ => panic!("Unexpected event"),
713         };
714
715         claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
716
717         send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
718         send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
719         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
720 }
721
722 #[test]
723 fn test_update_fee() {
724         let chanmon_cfgs = create_chanmon_cfgs(2);
725         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
726         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
727         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
728         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
729         let channel_id = chan.2;
730
731         // A                                        B
732         // (1) update_fee/commitment_signed      ->
733         //                                       <- (2) revoke_and_ack
734         //                                       .- send (3) commitment_signed
735         // (4) update_fee/commitment_signed      ->
736         //                                       .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
737         //                                       <- (3) commitment_signed delivered
738         // send (6) revoke_and_ack               -.
739         //                                       <- (5) deliver revoke_and_ack
740         // (6) deliver revoke_and_ack            ->
741         //                                       .- send (7) commitment_signed in response to (4)
742         //                                       <- (7) deliver commitment_signed
743         // revoke_and_ack                        ->
744
745         // Create and deliver (1)...
746         let feerate = get_feerate!(nodes[0], channel_id);
747         nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
748         check_added_monitors!(nodes[0], 1);
749
750         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
751         assert_eq!(events_0.len(), 1);
752         let (update_msg, commitment_signed) = match events_0[0] {
753                         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 } } => {
754                         (update_fee.as_ref(), commitment_signed)
755                 },
756                 _ => panic!("Unexpected event"),
757         };
758         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
759
760         // Generate (2) and (3):
761         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
762         let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
763         check_added_monitors!(nodes[1], 1);
764
765         // Deliver (2):
766         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
767         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
768         check_added_monitors!(nodes[0], 1);
769
770         // Create and deliver (4)...
771         nodes[0].node.update_fee(channel_id, feerate+30).unwrap();
772         check_added_monitors!(nodes[0], 1);
773         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
774         assert_eq!(events_0.len(), 1);
775         let (update_msg, commitment_signed) = match events_0[0] {
776                         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 } } => {
777                         (update_fee.as_ref(), commitment_signed)
778                 },
779                 _ => panic!("Unexpected event"),
780         };
781
782         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
783         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
784         check_added_monitors!(nodes[1], 1);
785         // ... creating (5)
786         let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
787         // No commitment_signed so get_event_msg's assert(len == 1) passes
788
789         // Handle (3), creating (6):
790         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0);
791         check_added_monitors!(nodes[0], 1);
792         let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
793         // No commitment_signed so get_event_msg's assert(len == 1) passes
794
795         // Deliver (5):
796         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
797         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
798         check_added_monitors!(nodes[0], 1);
799
800         // Deliver (6), creating (7):
801         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0);
802         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
803         assert!(commitment_update.update_add_htlcs.is_empty());
804         assert!(commitment_update.update_fulfill_htlcs.is_empty());
805         assert!(commitment_update.update_fail_htlcs.is_empty());
806         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
807         assert!(commitment_update.update_fee.is_none());
808         check_added_monitors!(nodes[1], 1);
809
810         // Deliver (7)
811         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
812         check_added_monitors!(nodes[0], 1);
813         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
814         // No commitment_signed so get_event_msg's assert(len == 1) passes
815
816         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
817         check_added_monitors!(nodes[1], 1);
818         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
819
820         assert_eq!(get_feerate!(nodes[0], channel_id), feerate + 30);
821         assert_eq!(get_feerate!(nodes[1], channel_id), feerate + 30);
822         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
823 }
824
825 #[test]
826 fn pre_funding_lock_shutdown_test() {
827         // Test sending a shutdown prior to funding_locked after funding generation
828         let chanmon_cfgs = create_chanmon_cfgs(2);
829         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
830         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
831         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
832         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 8000000, 0, InitFeatures::known(), InitFeatures::known());
833         mine_transaction(&nodes[0], &tx);
834         mine_transaction(&nodes[1], &tx);
835
836         nodes[0].node.close_channel(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).unwrap();
837         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
838         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
839         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
840         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
841
842         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
843         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
844         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
845         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
846         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
847         assert!(node_0_none.is_none());
848
849         assert!(nodes[0].node.list_channels().is_empty());
850         assert!(nodes[1].node.list_channels().is_empty());
851 }
852
853 #[test]
854 fn updates_shutdown_wait() {
855         // Test sending a shutdown with outstanding updates pending
856         let chanmon_cfgs = create_chanmon_cfgs(3);
857         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
858         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
859         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
860         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
861         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
862         let logger = test_utils::TestLogger::new();
863
864         let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
865
866         nodes[0].node.close_channel(&chan_1.2).unwrap();
867         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
868         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
869         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
870         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
871
872         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
873         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
874
875         let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
876
877         let net_graph_msg_handler0 = &nodes[0].net_graph_msg_handler;
878         let net_graph_msg_handler1 = &nodes[1].net_graph_msg_handler;
879         let route_1 = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler0.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
880         let route_2 = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler1.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
881         unwrap_send_err!(nodes[0].node.send_payment(&route_1, payment_hash, &Some(payment_secret)), true, APIError::ChannelUnavailable {..}, {});
882         unwrap_send_err!(nodes[1].node.send_payment(&route_2, payment_hash, &Some(payment_secret)), true, APIError::ChannelUnavailable {..}, {});
883
884         assert!(nodes[2].node.claim_funds(our_payment_preimage));
885         check_added_monitors!(nodes[2], 1);
886         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
887         assert!(updates.update_add_htlcs.is_empty());
888         assert!(updates.update_fail_htlcs.is_empty());
889         assert!(updates.update_fail_malformed_htlcs.is_empty());
890         assert!(updates.update_fee.is_none());
891         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
892         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
893         expect_payment_forwarded!(nodes[1], Some(1000), false);
894         check_added_monitors!(nodes[1], 1);
895         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
896         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
897
898         assert!(updates_2.update_add_htlcs.is_empty());
899         assert!(updates_2.update_fail_htlcs.is_empty());
900         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
901         assert!(updates_2.update_fee.is_none());
902         assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
903         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
904         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
905
906         let events = nodes[0].node.get_and_clear_pending_events();
907         assert_eq!(events.len(), 1);
908         match events[0] {
909                 Event::PaymentSent { ref payment_preimage } => {
910                         assert_eq!(our_payment_preimage, *payment_preimage);
911                 },
912                 _ => panic!("Unexpected event"),
913         }
914
915         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
916         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
917         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
918         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
919         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
920         assert!(node_0_none.is_none());
921
922         assert!(nodes[0].node.list_channels().is_empty());
923
924         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
925         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
926         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
927         assert!(nodes[1].node.list_channels().is_empty());
928         assert!(nodes[2].node.list_channels().is_empty());
929 }
930
931 #[test]
932 fn htlc_fail_async_shutdown() {
933         // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
934         let chanmon_cfgs = create_chanmon_cfgs(3);
935         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
936         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
937         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
938         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
939         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
940         let logger = test_utils::TestLogger::new();
941
942         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
943         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
944         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
945         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
946         check_added_monitors!(nodes[0], 1);
947         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
948         assert_eq!(updates.update_add_htlcs.len(), 1);
949         assert!(updates.update_fulfill_htlcs.is_empty());
950         assert!(updates.update_fail_htlcs.is_empty());
951         assert!(updates.update_fail_malformed_htlcs.is_empty());
952         assert!(updates.update_fee.is_none());
953
954         nodes[1].node.close_channel(&chan_1.2).unwrap();
955         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
956         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
957         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
958
959         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
960         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
961         check_added_monitors!(nodes[1], 1);
962         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
963         commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
964
965         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
966         assert!(updates_2.update_add_htlcs.is_empty());
967         assert!(updates_2.update_fulfill_htlcs.is_empty());
968         assert_eq!(updates_2.update_fail_htlcs.len(), 1);
969         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
970         assert!(updates_2.update_fee.is_none());
971
972         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]);
973         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
974
975         expect_payment_failed!(nodes[0], our_payment_hash, false);
976
977         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
978         assert_eq!(msg_events.len(), 2);
979         let node_0_closing_signed = match msg_events[0] {
980                 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
981                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
982                         (*msg).clone()
983                 },
984                 _ => panic!("Unexpected event"),
985         };
986         match msg_events[1] {
987                 MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
988                         assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
989                 },
990                 _ => panic!("Unexpected event"),
991         }
992
993         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
994         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
995         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
996         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
997         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
998         assert!(node_0_none.is_none());
999
1000         assert!(nodes[0].node.list_channels().is_empty());
1001
1002         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1003         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1004         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
1005         assert!(nodes[1].node.list_channels().is_empty());
1006         assert!(nodes[2].node.list_channels().is_empty());
1007 }
1008
1009 fn do_test_shutdown_rebroadcast(recv_count: u8) {
1010         // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
1011         // messages delivered prior to disconnect
1012         let chanmon_cfgs = create_chanmon_cfgs(3);
1013         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1014         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1015         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1016         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1017         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1018
1019         let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
1020
1021         nodes[1].node.close_channel(&chan_1.2).unwrap();
1022         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1023         if recv_count > 0 {
1024                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
1025                 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1026                 if recv_count > 1 {
1027                         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
1028                 }
1029         }
1030
1031         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1032         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1033
1034         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1035         let node_0_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
1036         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1037         let node_1_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
1038
1039         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish);
1040         let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1041         assert!(node_1_shutdown == node_1_2nd_shutdown);
1042
1043         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish);
1044         let node_0_2nd_shutdown = if recv_count > 0 {
1045                 let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1046                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_2nd_shutdown);
1047                 node_0_2nd_shutdown
1048         } else {
1049                 let node_0_chan_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
1050                 assert_eq!(node_0_chan_update.contents.flags & 2, 0); // "disabled" flag must not be set as we just reconnected.
1051                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_2nd_shutdown);
1052                 get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
1053         };
1054         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_2nd_shutdown);
1055
1056         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1057         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1058
1059         assert!(nodes[2].node.claim_funds(our_payment_preimage));
1060         check_added_monitors!(nodes[2], 1);
1061         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1062         assert!(updates.update_add_htlcs.is_empty());
1063         assert!(updates.update_fail_htlcs.is_empty());
1064         assert!(updates.update_fail_malformed_htlcs.is_empty());
1065         assert!(updates.update_fee.is_none());
1066         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
1067         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
1068         expect_payment_forwarded!(nodes[1], Some(1000), false);
1069         check_added_monitors!(nodes[1], 1);
1070         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1071         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
1072
1073         assert!(updates_2.update_add_htlcs.is_empty());
1074         assert!(updates_2.update_fail_htlcs.is_empty());
1075         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
1076         assert!(updates_2.update_fee.is_none());
1077         assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
1078         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]);
1079         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
1080
1081         let events = nodes[0].node.get_and_clear_pending_events();
1082         assert_eq!(events.len(), 1);
1083         match events[0] {
1084                 Event::PaymentSent { ref payment_preimage } => {
1085                         assert_eq!(our_payment_preimage, *payment_preimage);
1086                 },
1087                 _ => panic!("Unexpected event"),
1088         }
1089
1090         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1091         if recv_count > 0 {
1092                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
1093                 let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1094                 assert!(node_1_closing_signed.is_some());
1095         }
1096
1097         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
1098         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
1099
1100         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1101         let node_0_2nd_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
1102         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
1103         if recv_count == 0 {
1104                 // If all closing_signeds weren't delivered we can just resume where we left off...
1105                 let node_1_2nd_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
1106
1107                 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish);
1108                 let node_0_3rd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
1109                 assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
1110
1111                 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish);
1112                 let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
1113                 assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
1114
1115                 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_3rd_shutdown);
1116                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1117
1118                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_3rd_shutdown);
1119                 let node_0_2nd_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
1120                 assert!(node_0_closing_signed == node_0_2nd_closing_signed);
1121
1122                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed);
1123                 let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
1124                 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap());
1125                 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
1126                 assert!(node_0_none.is_none());
1127         } else {
1128                 // If one node, however, received + responded with an identical closing_signed we end
1129                 // up erroring and node[0] will try to broadcast its own latest commitment transaction.
1130                 // There isn't really anything better we can do simply, but in the future we might
1131                 // explore storing a set of recently-closed channels that got disconnected during
1132                 // closing_signed and avoiding broadcasting local commitment txn for some timeout to
1133                 // give our counterparty enough time to (potentially) broadcast a cooperative closing
1134                 // transaction.
1135                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1136
1137                 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish);
1138                 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
1139                 assert_eq!(msg_events.len(), 1);
1140                 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
1141                         match action {
1142                                 &ErrorAction::SendErrorMessage { ref msg } => {
1143                                         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msg);
1144                                         assert_eq!(msg.channel_id, chan_1.2);
1145                                 },
1146                                 _ => panic!("Unexpected event!"),
1147                         }
1148                 } else { panic!("Needed SendErrorMessage close"); }
1149
1150                 // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
1151                 // checks it, but in this case nodes[0] didn't ever get a chance to receive a
1152                 // closing_signed so we do it ourselves
1153                 check_closed_broadcast!(nodes[0], false);
1154                 check_added_monitors!(nodes[0], 1);
1155         }
1156
1157         assert!(nodes[0].node.list_channels().is_empty());
1158
1159         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
1160         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
1161         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
1162         assert!(nodes[1].node.list_channels().is_empty());
1163         assert!(nodes[2].node.list_channels().is_empty());
1164 }
1165
1166 #[test]
1167 fn test_shutdown_rebroadcast() {
1168         do_test_shutdown_rebroadcast(0);
1169         do_test_shutdown_rebroadcast(1);
1170         do_test_shutdown_rebroadcast(2);
1171 }
1172
1173 #[test]
1174 fn fake_network_test() {
1175         // Simple test which builds a network of ChannelManagers, connects them to each other, and
1176         // tests that payments get routed and transactions broadcast in semi-reasonable ways.
1177         let chanmon_cfgs = create_chanmon_cfgs(4);
1178         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
1179         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
1180         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1181
1182         // Create some initial channels
1183         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1184         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1185         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
1186
1187         // Rebalance the network a bit by relaying one payment through all the channels...
1188         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1189         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1190         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1191         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1192
1193         // Send some more payments
1194         send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
1195         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
1196         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
1197
1198         // Test failure packets
1199         let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
1200         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
1201
1202         // Add a new channel that skips 3
1203         let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known());
1204
1205         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
1206         send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
1207         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1208         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1209         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1210         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1211         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1212
1213         // Do some rebalance loop payments, simultaneously
1214         let mut hops = Vec::with_capacity(3);
1215         hops.push(RouteHop {
1216                 pubkey: nodes[2].node.get_our_node_id(),
1217                 node_features: NodeFeatures::empty(),
1218                 short_channel_id: chan_2.0.contents.short_channel_id,
1219                 channel_features: ChannelFeatures::empty(),
1220                 fee_msat: 0,
1221                 cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
1222         });
1223         hops.push(RouteHop {
1224                 pubkey: nodes[3].node.get_our_node_id(),
1225                 node_features: NodeFeatures::empty(),
1226                 short_channel_id: chan_3.0.contents.short_channel_id,
1227                 channel_features: ChannelFeatures::empty(),
1228                 fee_msat: 0,
1229                 cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
1230         });
1231         hops.push(RouteHop {
1232                 pubkey: nodes[1].node.get_our_node_id(),
1233                 node_features: NodeFeatures::known(),
1234                 short_channel_id: chan_4.0.contents.short_channel_id,
1235                 channel_features: ChannelFeatures::known(),
1236                 fee_msat: 1000000,
1237                 cltv_expiry_delta: TEST_FINAL_CLTV,
1238         });
1239         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;
1240         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;
1241         let payment_preimage_1 = send_along_route(&nodes[1], Route { paths: vec![hops] }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
1242
1243         let mut hops = Vec::with_capacity(3);
1244         hops.push(RouteHop {
1245                 pubkey: nodes[3].node.get_our_node_id(),
1246                 node_features: NodeFeatures::empty(),
1247                 short_channel_id: chan_4.0.contents.short_channel_id,
1248                 channel_features: ChannelFeatures::empty(),
1249                 fee_msat: 0,
1250                 cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
1251         });
1252         hops.push(RouteHop {
1253                 pubkey: nodes[2].node.get_our_node_id(),
1254                 node_features: NodeFeatures::empty(),
1255                 short_channel_id: chan_3.0.contents.short_channel_id,
1256                 channel_features: ChannelFeatures::empty(),
1257                 fee_msat: 0,
1258                 cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
1259         });
1260         hops.push(RouteHop {
1261                 pubkey: nodes[1].node.get_our_node_id(),
1262                 node_features: NodeFeatures::known(),
1263                 short_channel_id: chan_2.0.contents.short_channel_id,
1264                 channel_features: ChannelFeatures::known(),
1265                 fee_msat: 1000000,
1266                 cltv_expiry_delta: TEST_FINAL_CLTV,
1267         });
1268         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;
1269         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;
1270         let payment_hash_2 = send_along_route(&nodes[1], Route { paths: vec![hops] }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
1271
1272         // Claim the rebalances...
1273         fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
1274         claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
1275
1276         // Add a duplicate new channel from 2 to 4
1277         let chan_5 = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known());
1278
1279         // Send some payments across both channels
1280         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1281         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1282         let payment_preimage_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1283
1284
1285         route_over_limit(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000);
1286         let events = nodes[0].node.get_and_clear_pending_msg_events();
1287         assert_eq!(events.len(), 0);
1288         nodes[0].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap(), 1);
1289
1290         //TODO: Test that routes work again here as we've been notified that the channel is full
1291
1292         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_3);
1293         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_4);
1294         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_5);
1295
1296         // Close down the channels...
1297         close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1298         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1299         close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1300         close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1301         close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
1302 }
1303
1304 #[test]
1305 fn holding_cell_htlc_counting() {
1306         // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1307         // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1308         // commitment dance rounds.
1309         let chanmon_cfgs = create_chanmon_cfgs(3);
1310         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1311         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1312         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1313         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1314         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
1315         let logger = test_utils::TestLogger::new();
1316
1317         let mut payments = Vec::new();
1318         for _ in 0..::ln::channel::OUR_MAX_HTLCS {
1319                 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
1320                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
1321                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
1322                 nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
1323                 payments.push((payment_preimage, payment_hash));
1324         }
1325         check_added_monitors!(nodes[1], 1);
1326
1327         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1328         assert_eq!(events.len(), 1);
1329         let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1330         assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1331
1332         // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1333         // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1334         // another HTLC.
1335         let (_, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[2]);
1336         {
1337                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
1338                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
1339                 unwrap_send_err!(nodes[1].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)), true, APIError::ChannelUnavailable { ref err },
1340                         assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
1341                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1342                 nodes[1].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot push more than their max accepted HTLCs".to_string(), 1);
1343         }
1344
1345         // This should also be true if we try to forward a payment.
1346         let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[2]);
1347         {
1348                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
1349                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
1350                 nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
1351                 check_added_monitors!(nodes[0], 1);
1352         }
1353
1354         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1355         assert_eq!(events.len(), 1);
1356         let payment_event = SendEvent::from_event(events.pop().unwrap());
1357         assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1358
1359         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1360         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1361         // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1362         // fails), the second will process the resulting failure and fail the HTLC backward.
1363         expect_pending_htlcs_forwardable!(nodes[1]);
1364         expect_pending_htlcs_forwardable!(nodes[1]);
1365         check_added_monitors!(nodes[1], 1);
1366
1367         let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1368         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
1369         commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1370
1371         expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, false);
1372         expect_payment_failed!(nodes[0], payment_hash_2, false);
1373
1374         // Now forward all the pending HTLCs and claim them back
1375         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]);
1376         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg);
1377         check_added_monitors!(nodes[2], 1);
1378
1379         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1380         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1381         check_added_monitors!(nodes[1], 1);
1382         let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1383
1384         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1385         check_added_monitors!(nodes[1], 1);
1386         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1387
1388         for ref update in as_updates.update_add_htlcs.iter() {
1389                 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update);
1390         }
1391         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed);
1392         check_added_monitors!(nodes[2], 1);
1393         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
1394         check_added_monitors!(nodes[2], 1);
1395         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1396
1397         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1398         check_added_monitors!(nodes[1], 1);
1399         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1400         check_added_monitors!(nodes[1], 1);
1401         let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1402
1403         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa);
1404         check_added_monitors!(nodes[2], 1);
1405
1406         expect_pending_htlcs_forwardable!(nodes[2]);
1407
1408         let events = nodes[2].node.get_and_clear_pending_events();
1409         assert_eq!(events.len(), payments.len());
1410         for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1411                 match event {
1412                         &Event::PaymentReceived { ref payment_hash, .. } => {
1413                                 assert_eq!(*payment_hash, *hash);
1414                         },
1415                         _ => panic!("Unexpected event"),
1416                 };
1417         }
1418
1419         for (preimage, _) in payments.drain(..) {
1420                 claim_payment(&nodes[1], &[&nodes[2]], preimage);
1421         }
1422
1423         send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1424 }
1425
1426 #[test]
1427 fn duplicate_htlc_test() {
1428         // Test that we accept duplicate payment_hash HTLCs across the network and that
1429         // claiming/failing them are all separate and don't affect each other
1430         let chanmon_cfgs = create_chanmon_cfgs(6);
1431         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1432         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1433         let mut nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1434
1435         // Create some initial channels to route via 3 to 4/5 from 0/1/2
1436         create_announced_chan_between_nodes(&nodes, 0, 3, InitFeatures::known(), InitFeatures::known());
1437         create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known());
1438         create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
1439         create_announced_chan_between_nodes(&nodes, 3, 4, InitFeatures::known(), InitFeatures::known());
1440         create_announced_chan_between_nodes(&nodes, 3, 5, InitFeatures::known(), InitFeatures::known());
1441
1442         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
1443
1444         *nodes[0].network_payment_count.borrow_mut() -= 1;
1445         assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
1446
1447         *nodes[0].network_payment_count.borrow_mut() -= 1;
1448         assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
1449
1450         claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
1451         fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
1452         claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
1453 }
1454
1455 #[test]
1456 fn test_duplicate_htlc_different_direction_onchain() {
1457         // Test that ChannelMonitor doesn't generate 2 preimage txn
1458         // when we have 2 HTLCs with same preimage that go across a node
1459         // in opposite directions, even with the same payment secret.
1460         let chanmon_cfgs = create_chanmon_cfgs(2);
1461         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1462         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1463         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1464
1465         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
1466         let logger = test_utils::TestLogger::new();
1467
1468         // balancing
1469         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1470
1471         let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
1472
1473         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
1474         let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 800_000, TEST_FINAL_CLTV, &logger).unwrap();
1475         let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200, 0).unwrap();
1476         send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
1477
1478         // Provide preimage to node 0 by claiming payment
1479         nodes[0].node.claim_funds(payment_preimage);
1480         check_added_monitors!(nodes[0], 1);
1481
1482         // Broadcast node 1 commitment txn
1483         let remote_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
1484
1485         assert_eq!(remote_txn[0].output.len(), 4); // 1 local, 1 remote, 1 htlc inbound, 1 htlc outbound
1486         let mut has_both_htlcs = 0; // check htlcs match ones committed
1487         for outp in remote_txn[0].output.iter() {
1488                 if outp.value == 800_000 / 1000 {
1489                         has_both_htlcs += 1;
1490                 } else if outp.value == 900_000 / 1000 {
1491                         has_both_htlcs += 1;
1492                 }
1493         }
1494         assert_eq!(has_both_htlcs, 2);
1495
1496         mine_transaction(&nodes[0], &remote_txn[0]);
1497         check_added_monitors!(nodes[0], 1);
1498         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
1499
1500         // Check we only broadcast 1 timeout tx
1501         let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1502         assert_eq!(claim_txn.len(), 8);
1503         assert_eq!(claim_txn[1], claim_txn[4]);
1504         assert_eq!(claim_txn[2], claim_txn[5]);
1505         check_spends!(claim_txn[1], chan_1.3);
1506         check_spends!(claim_txn[2], claim_txn[1]);
1507         check_spends!(claim_txn[7], claim_txn[1]);
1508
1509         assert_eq!(claim_txn[0].input.len(), 1);
1510         assert_eq!(claim_txn[3].input.len(), 1);
1511         assert_eq!(claim_txn[0].input[0].previous_output, claim_txn[3].input[0].previous_output);
1512
1513         assert_eq!(claim_txn[0].input.len(), 1);
1514         assert_eq!(claim_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC 1 <--> 0, preimage tx
1515         check_spends!(claim_txn[0], remote_txn[0]);
1516         assert_eq!(remote_txn[0].output[claim_txn[0].input[0].previous_output.vout as usize].value, 800);
1517         assert_eq!(claim_txn[6].input.len(), 1);
1518         assert_eq!(claim_txn[6].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
1519         check_spends!(claim_txn[6], remote_txn[0]);
1520         assert_eq!(remote_txn[0].output[claim_txn[6].input[0].previous_output.vout as usize].value, 900);
1521
1522         let events = nodes[0].node.get_and_clear_pending_msg_events();
1523         assert_eq!(events.len(), 3);
1524         for e in events {
1525                 match e {
1526                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1527                         MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
1528                                 assert_eq!(node_id, nodes[1].node.get_our_node_id());
1529                                 assert_eq!(msg.data, "Commitment or closing transaction was confirmed on chain.");
1530                         },
1531                         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, .. } } => {
1532                                 assert!(update_add_htlcs.is_empty());
1533                                 assert!(update_fail_htlcs.is_empty());
1534                                 assert_eq!(update_fulfill_htlcs.len(), 1);
1535                                 assert!(update_fail_malformed_htlcs.is_empty());
1536                                 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
1537                         },
1538                         _ => panic!("Unexpected event"),
1539                 }
1540         }
1541 }
1542
1543 #[test]
1544 fn test_basic_channel_reserve() {
1545         let chanmon_cfgs = create_chanmon_cfgs(2);
1546         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1547         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1548         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1549         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1550         let logger = test_utils::TestLogger::new();
1551
1552         let chan_stat = get_channel_value_stat!(nodes[0], chan.2);
1553         let channel_reserve = chan_stat.channel_reserve_msat;
1554
1555         // The 2* and +1 are for the fee spike reserve.
1556         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
1557         let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], chan.2), 1 + 1);
1558         let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
1559         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
1560         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), max_can_send + 1, TEST_FINAL_CLTV, &logger).unwrap();
1561         let err = nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).err().unwrap();
1562         match err {
1563                 PaymentSendFailure::AllFailedRetrySafe(ref fails) => {
1564                         match &fails[0] {
1565                                 &APIError::ChannelUnavailable{ref err} =>
1566                                         assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)),
1567                                 _ => panic!("Unexpected error variant"),
1568                         }
1569                 },
1570                 _ => panic!("Unexpected error variant"),
1571         }
1572         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1573         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
1574
1575         send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
1576 }
1577
1578 #[test]
1579 fn test_fee_spike_violation_fails_htlc() {
1580         let chanmon_cfgs = create_chanmon_cfgs(2);
1581         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1582         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1583         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1584         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1585
1586         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 3460001);
1587         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1588         let secp_ctx = Secp256k1::new();
1589         let session_priv = SecretKey::from_slice(&[42; 32]).expect("RNG is bad!");
1590
1591         let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1592
1593         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1594         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0], 3460001, &Some(payment_secret), cur_height, &None).unwrap();
1595         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
1596         let msg = msgs::UpdateAddHTLC {
1597                 channel_id: chan.2,
1598                 htlc_id: 0,
1599                 amount_msat: htlc_msat,
1600                 payment_hash: payment_hash,
1601                 cltv_expiry: htlc_cltv,
1602                 onion_routing_packet: onion_packet,
1603         };
1604
1605         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1606
1607         // Now manually create the commitment_signed message corresponding to the update_add
1608         // nodes[0] just sent. In the code for construction of this message, "local" refers
1609         // to the sender of the message, and "remote" refers to the receiver.
1610
1611         let feerate_per_kw = get_feerate!(nodes[0], chan.2);
1612
1613         const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1614
1615         // Get the EnforcingSigner for each channel, which will be used to (1) get the keys
1616         // needed to sign the new commitment tx and (2) sign the new commitment tx.
1617         let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point) = {
1618                 let chan_lock = nodes[0].node.channel_state.lock().unwrap();
1619                 let local_chan = chan_lock.by_id.get(&chan.2).unwrap();
1620                 let chan_signer = local_chan.get_signer();
1621                 let pubkeys = chan_signer.pubkeys();
1622                 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1623                  chan_signer.release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1624                  chan_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx))
1625         };
1626         let (remote_delayed_payment_basepoint, remote_htlc_basepoint,remote_point) = {
1627                 let chan_lock = nodes[1].node.channel_state.lock().unwrap();
1628                 let remote_chan = chan_lock.by_id.get(&chan.2).unwrap();
1629                 let chan_signer = remote_chan.get_signer();
1630                 let pubkeys = chan_signer.pubkeys();
1631                 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1632                  chan_signer.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx))
1633         };
1634
1635         // Assemble the set of keys we can use for signatures for our commitment_signed message.
1636         let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
1637                 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint).unwrap();
1638
1639         // Build the remote commitment transaction so we can sign it, and then later use the
1640         // signature for the commitment_signed message.
1641         let local_chan_balance = 1313;
1642
1643         let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
1644                 offered: false,
1645                 amount_msat: 3460001,
1646                 cltv_expiry: htlc_cltv,
1647                 payment_hash,
1648                 transaction_output_index: Some(1),
1649         };
1650
1651         let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
1652
1653         let res = {
1654                 let local_chan_lock = nodes[0].node.channel_state.lock().unwrap();
1655                 let local_chan = local_chan_lock.by_id.get(&chan.2).unwrap();
1656                 let local_chan_signer = local_chan.get_signer();
1657                 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
1658                         commitment_number,
1659                         95000,
1660                         local_chan_balance,
1661                         commit_tx_keys.clone(),
1662                         feerate_per_kw,
1663                         &mut vec![(accepted_htlc_info, ())],
1664                         &local_chan.channel_transaction_parameters.as_counterparty_broadcastable()
1665                 );
1666                 local_chan_signer.sign_counterparty_commitment(&commitment_tx, &secp_ctx).unwrap()
1667         };
1668
1669         let commit_signed_msg = msgs::CommitmentSigned {
1670                 channel_id: chan.2,
1671                 signature: res.0,
1672                 htlc_signatures: res.1
1673         };
1674
1675         // Send the commitment_signed message to the nodes[1].
1676         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
1677         let _ = nodes[1].node.get_and_clear_pending_msg_events();
1678
1679         // Send the RAA to nodes[1].
1680         let raa_msg = msgs::RevokeAndACK {
1681                 channel_id: chan.2,
1682                 per_commitment_secret: local_secret,
1683                 next_per_commitment_point: next_local_point
1684         };
1685         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
1686
1687         let events = nodes[1].node.get_and_clear_pending_msg_events();
1688         assert_eq!(events.len(), 1);
1689         // Make sure the HTLC failed in the way we expect.
1690         match events[0] {
1691                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. }, .. } => {
1692                         assert_eq!(update_fail_htlcs.len(), 1);
1693                         update_fail_htlcs[0].clone()
1694                 },
1695                 _ => panic!("Unexpected event"),
1696         };
1697         nodes[1].logger.assert_log("lightning::ln::channel".to_string(),
1698                 format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", ::hex::encode(raa_msg.channel_id)), 1);
1699
1700         check_added_monitors!(nodes[1], 2);
1701 }
1702
1703 #[test]
1704 fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
1705         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1706         // Set the fee rate for the channel very high, to the point where the fundee
1707         // sending any above-dust amount would result in a channel reserve violation.
1708         // In this test we check that we would be prevented from sending an HTLC in
1709         // this situation.
1710         let feerate_per_kw = 253;
1711         chanmon_cfgs[0].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(feerate_per_kw) };
1712         chanmon_cfgs[1].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(feerate_per_kw) };
1713         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1714         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1715         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1716
1717         let mut push_amt = 100_000_000;
1718         push_amt -= feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
1719         push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
1720
1721         let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt, InitFeatures::known(), InitFeatures::known());
1722
1723         // Sending exactly enough to hit the reserve amount should be accepted
1724         let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1725
1726         // However one more HTLC should be significantly over the reserve amount and fail.
1727         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
1728         unwrap_send_err!(nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
1729                 assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
1730         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1731         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send value that would put counterparty balance under holder-announced channel reserve value".to_string(), 1);
1732 }
1733
1734 #[test]
1735 fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
1736         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1737         // Set the fee rate for the channel very high, to the point where the funder
1738         // receiving 1 update_add_htlc would result in them closing the channel due
1739         // to channel reserve violation. This close could also happen if the fee went
1740         // up a more realistic amount, but many HTLCs were outstanding at the time of
1741         // the update_add_htlc.
1742         chanmon_cfgs[0].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(6000) };
1743         chanmon_cfgs[1].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(6000) };
1744         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1745         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1746         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1747         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1748
1749         let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
1750         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1751         let secp_ctx = Secp256k1::new();
1752         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1753         let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1754         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1755         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0], 1000, &Some(payment_secret), cur_height, &None).unwrap();
1756         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
1757         let msg = msgs::UpdateAddHTLC {
1758                 channel_id: chan.2,
1759                 htlc_id: 1,
1760                 amount_msat: htlc_msat + 1,
1761                 payment_hash: payment_hash,
1762                 cltv_expiry: htlc_cltv,
1763                 onion_routing_packet: onion_packet,
1764         };
1765
1766         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
1767         // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1768         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);
1769         assert_eq!(nodes[0].node.list_channels().len(), 0);
1770         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
1771         assert_eq!(err_msg.data, "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value");
1772         check_added_monitors!(nodes[0], 1);
1773 }
1774
1775 #[test]
1776 fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
1777         // Test that if we receive many dust HTLCs over an outbound channel, they don't count when
1778         // calculating our commitment transaction fee (this was previously broken).
1779         let mut chanmon_cfgs = create_chanmon_cfgs(2);
1780         let feerate_per_kw = 253;
1781         chanmon_cfgs[0].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(feerate_per_kw) };
1782         chanmon_cfgs[1].fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(feerate_per_kw) };
1783
1784         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1785         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1786         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1787
1788         // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1789         // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1790         // transaction fee with 0 HTLCs (183 sats)).
1791         let mut push_amt = 100_000_000;
1792         push_amt -= feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT) / 1000 * 1000;
1793         push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
1794         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt, InitFeatures::known(), InitFeatures::known());
1795
1796         let dust_amt = crate::ln::channel::MIN_DUST_LIMIT_SATOSHIS * 1000
1797                 + feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000 * 1000 - 1;
1798         // In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
1799         // reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
1800         // commitment transaction fee.
1801         let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], dust_amt);
1802
1803         // One more than the dust amt should fail, however.
1804         let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt + 1);
1805         unwrap_send_err!(nodes[1].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
1806                 assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
1807 }
1808
1809 #[test]
1810 fn test_chan_reserve_dust_inbound_htlcs_inbound_chan() {
1811         // Test that if we receive many dust HTLCs over an inbound channel, they don't count when
1812         // calculating our counterparty's commitment transaction fee (this was previously broken).
1813         let chanmon_cfgs = create_chanmon_cfgs(2);
1814         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1815         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1816         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1817         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 98000000, InitFeatures::known(), InitFeatures::known());
1818
1819         let payment_amt = 46000; // Dust amount
1820         // In the previous code, these first four payments would succeed.
1821         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1822         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1823         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1824         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1825
1826         // Then these next 5 would be interpreted by nodes[1] as violating the fee spike buffer.
1827         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1828         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1829         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1830         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1831         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1832
1833         // And this last payment previously resulted in nodes[1] closing on its inbound-channel
1834         // counterparty, because it counted all the previous dust HTLCs against nodes[0]'s commitment
1835         // transaction fee and therefore perceived this next payment as a channel reserve violation.
1836         let (_, _, _) = route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1837 }
1838
1839 #[test]
1840 fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1841         let chanmon_cfgs = create_chanmon_cfgs(3);
1842         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1843         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1844         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1845         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1846         let _ = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1847
1848         let feemsat = 239;
1849         let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1850         let chan_stat = get_channel_value_stat!(nodes[0], chan.2);
1851         let feerate = get_feerate!(nodes[0], chan.2);
1852
1853         // Add a 2* and +1 for the fee spike reserve.
1854         let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1);
1855         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;
1856         let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
1857
1858         // Add a pending HTLC.
1859         let (route_1, our_payment_hash_1, _, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_1);
1860         let payment_event_1 = {
1861                 nodes[0].node.send_payment(&route_1, our_payment_hash_1, &Some(our_payment_secret_1)).unwrap();
1862                 check_added_monitors!(nodes[0], 1);
1863
1864                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1865                 assert_eq!(events.len(), 1);
1866                 SendEvent::from_event(events.remove(0))
1867         };
1868         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1869
1870         // Attempt to trigger a channel reserve violation --> payment failure.
1871         let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2);
1872         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;
1873         let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1874         let (route_2, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_2);
1875
1876         // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1877         let secp_ctx = Secp256k1::new();
1878         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1879         let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
1880         let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route_2.paths[0], &session_priv).unwrap();
1881         let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route_2.paths[0], recv_value_2, &None, cur_height, &None).unwrap();
1882         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash_1);
1883         let msg = msgs::UpdateAddHTLC {
1884                 channel_id: chan.2,
1885                 htlc_id: 1,
1886                 amount_msat: htlc_msat + 1,
1887                 payment_hash: our_payment_hash_1,
1888                 cltv_expiry: htlc_cltv,
1889                 onion_routing_packet: onion_packet,
1890         };
1891
1892         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1893         // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1894         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote HTLC add would put them under remote reserve value".to_string(), 1);
1895         assert_eq!(nodes[1].node.list_channels().len(), 1);
1896         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
1897         assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
1898         check_added_monitors!(nodes[1], 1);
1899 }
1900
1901 #[test]
1902 fn test_inbound_outbound_capacity_is_not_zero() {
1903         let chanmon_cfgs = create_chanmon_cfgs(2);
1904         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1905         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1906         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1907         let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1908         let channels0 = node_chanmgrs[0].list_channels();
1909         let channels1 = node_chanmgrs[1].list_channels();
1910         assert_eq!(channels0.len(), 1);
1911         assert_eq!(channels1.len(), 1);
1912
1913         let reserve = Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100000);
1914         assert_eq!(channels0[0].inbound_capacity_msat, 95000000 - reserve*1000);
1915         assert_eq!(channels1[0].outbound_capacity_msat, 95000000 - reserve*1000);
1916
1917         assert_eq!(channels0[0].outbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1918         assert_eq!(channels1[0].inbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1919 }
1920
1921 fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64) -> u64 {
1922         (COMMITMENT_TX_BASE_WEIGHT + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
1923 }
1924
1925 #[test]
1926 fn test_channel_reserve_holding_cell_htlcs() {
1927         let chanmon_cfgs = create_chanmon_cfgs(3);
1928         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1929         // When this test was written, the default base fee floated based on the HTLC count.
1930         // It is now fixed, so we simply set the fee to the expected value here.
1931         let mut config = test_default_channel_config();
1932         config.channel_options.forwarding_fee_base_msat = 239;
1933         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
1934         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1935         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 190000, 1001, InitFeatures::known(), InitFeatures::known());
1936         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 190000, 1001, InitFeatures::known(), InitFeatures::known());
1937
1938         let mut stat01 = get_channel_value_stat!(nodes[0], chan_1.2);
1939         let mut stat11 = get_channel_value_stat!(nodes[1], chan_1.2);
1940
1941         let mut stat12 = get_channel_value_stat!(nodes[1], chan_2.2);
1942         let mut stat22 = get_channel_value_stat!(nodes[2], chan_2.2);
1943
1944         macro_rules! expect_forward {
1945                 ($node: expr) => {{
1946                         let mut events = $node.node.get_and_clear_pending_msg_events();
1947                         assert_eq!(events.len(), 1);
1948                         check_added_monitors!($node, 1);
1949                         let payment_event = SendEvent::from_event(events.remove(0));
1950                         payment_event
1951                 }}
1952         }
1953
1954         let feemsat = 239; // set above
1955         let total_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1956         let feerate = get_feerate!(nodes[0], chan_1.2);
1957
1958         let recv_value_0 = stat01.counterparty_max_htlc_value_in_flight_msat - total_fee_msat;
1959
1960         // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
1961         {
1962                 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_0);
1963                 route.paths[0].last_mut().unwrap().fee_msat += 1;
1964                 assert!(route.paths[0].iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
1965                 unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
1966                         assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
1967                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1968                 nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put us over the max HTLC value in flight our peer will accept".to_string(), 1);
1969         }
1970
1971         // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
1972         // nodes[0]'s wealth
1973         loop {
1974                 let amt_msat = recv_value_0 + total_fee_msat;
1975                 // 3 for the 3 HTLCs that will be sent, 2* and +1 for the fee spike reserve.
1976                 // Also, ensure that each payment has enough to be over the dust limit to
1977                 // ensure it'll be included in each commit tx fee calculation.
1978                 let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1);
1979                 let ensure_htlc_amounts_above_dust_buffer = 3 * (stat01.counterparty_dust_limit_msat + 1000);
1980                 if stat01.value_to_self_msat < stat01.channel_reserve_msat + commit_tx_fee_all_htlcs + ensure_htlc_amounts_above_dust_buffer + amt_msat {
1981                         break;
1982                 }
1983                 send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_0);
1984
1985                 let (stat01_, stat11_, stat12_, stat22_) = (
1986                         get_channel_value_stat!(nodes[0], chan_1.2),
1987                         get_channel_value_stat!(nodes[1], chan_1.2),
1988                         get_channel_value_stat!(nodes[1], chan_2.2),
1989                         get_channel_value_stat!(nodes[2], chan_2.2),
1990                 );
1991
1992                 assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
1993                 assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
1994                 assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
1995                 assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
1996                 stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
1997         }
1998
1999         // adding pending output.
2000         // 2* and +1 HTLCs on the commit tx fee for the fee spike reserve.
2001         // The reason we're dividing by two here is as follows: the dividend is the total outbound liquidity
2002         // after fees, the channel reserve, and the fee spike buffer are removed. We eventually want to
2003         // divide this quantity into 3 portions, that will each be sent in an HTLC. This allows us
2004         // to test channel channel reserve policy at the edges of what amount is sendable, i.e.
2005         // cases where 1 msat over X amount will cause a payment failure, but anything less than
2006         // that can be sent successfully. So, dividing by two is a somewhat arbitrary way of getting
2007         // the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
2008         // is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
2009         // policy.
2010         let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1);
2011         let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
2012         let amt_msat_1 = recv_value_1 + total_fee_msat;
2013
2014         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);
2015         let payment_event_1 = {
2016                 nodes[0].node.send_payment(&route_1, our_payment_hash_1, &Some(our_payment_secret_1)).unwrap();
2017                 check_added_monitors!(nodes[0], 1);
2018
2019                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2020                 assert_eq!(events.len(), 1);
2021                 SendEvent::from_event(events.remove(0))
2022         };
2023         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
2024
2025         // channel reserve test with htlc pending output > 0
2026         let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs;
2027         {
2028                 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_2 + 1);
2029                 unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
2030                         assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
2031                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2032         }
2033
2034         // split the rest to test holding cell
2035         let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1);
2036         let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
2037         let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2;
2038         let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
2039         {
2040                 let stat = get_channel_value_stat!(nodes[0], chan_1.2);
2041                 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);
2042         }
2043
2044         // now see if they go through on both sides
2045         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);
2046         // but this will stuck in the holding cell
2047         nodes[0].node.send_payment(&route_21, our_payment_hash_21, &Some(our_payment_secret_21)).unwrap();
2048         check_added_monitors!(nodes[0], 0);
2049         let events = nodes[0].node.get_and_clear_pending_events();
2050         assert_eq!(events.len(), 0);
2051
2052         // test with outbound holding cell amount > 0
2053         {
2054                 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22+1);
2055                 unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
2056                         assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
2057                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2058                 nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put our balance under counterparty-announced channel reserve value".to_string(), 2);
2059         }
2060
2061         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);
2062         // this will also stuck in the holding cell
2063         nodes[0].node.send_payment(&route_22, our_payment_hash_22, &Some(our_payment_secret_22)).unwrap();
2064         check_added_monitors!(nodes[0], 0);
2065         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
2066         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2067
2068         // flush the pending htlc
2069         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg);
2070         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2071         check_added_monitors!(nodes[1], 1);
2072
2073         // the pending htlc should be promoted to committed
2074         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
2075         check_added_monitors!(nodes[0], 1);
2076         let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2077
2078         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed);
2079         let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2080         // No commitment_signed so get_event_msg's assert(len == 1) passes
2081         check_added_monitors!(nodes[0], 1);
2082
2083         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack);
2084         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2085         check_added_monitors!(nodes[1], 1);
2086
2087         expect_pending_htlcs_forwardable!(nodes[1]);
2088
2089         let ref payment_event_11 = expect_forward!(nodes[1]);
2090         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]);
2091         commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
2092
2093         expect_pending_htlcs_forwardable!(nodes[2]);
2094         expect_payment_received!(nodes[2], our_payment_hash_1, our_payment_secret_1, recv_value_1);
2095
2096         // flush the htlcs in the holding cell
2097         assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
2098         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]);
2099         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]);
2100         commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
2101         expect_pending_htlcs_forwardable!(nodes[1]);
2102
2103         let ref payment_event_3 = expect_forward!(nodes[1]);
2104         assert_eq!(payment_event_3.msgs.len(), 2);
2105         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]);
2106         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]);
2107
2108         commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
2109         expect_pending_htlcs_forwardable!(nodes[2]);
2110
2111         let events = nodes[2].node.get_and_clear_pending_events();
2112         assert_eq!(events.len(), 2);
2113         match events[0] {
2114                 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
2115                         assert_eq!(our_payment_hash_21, *payment_hash);
2116                         assert_eq!(recv_value_21, amt);
2117                         match &purpose {
2118                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2119                                         assert!(payment_preimage.is_none());
2120                                         assert_eq!(our_payment_secret_21, *payment_secret);
2121                                 },
2122                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
2123                         }
2124                 },
2125                 _ => panic!("Unexpected event"),
2126         }
2127         match events[1] {
2128                 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
2129                         assert_eq!(our_payment_hash_22, *payment_hash);
2130                         assert_eq!(recv_value_22, amt);
2131                         match &purpose {
2132                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2133                                         assert!(payment_preimage.is_none());
2134                                         assert_eq!(our_payment_secret_22, *payment_secret);
2135                                 },
2136                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
2137                         }
2138                 },
2139                 _ => panic!("Unexpected event"),
2140         }
2141
2142         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
2143         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
2144         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
2145
2146         let commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1);
2147         let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
2148         send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_3);
2149
2150         let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1);
2151         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);
2152         let stat0 = get_channel_value_stat!(nodes[0], chan_1.2);
2153         assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
2154         assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat + commit_tx_fee_1_htlc);
2155
2156         let stat2 = get_channel_value_stat!(nodes[2], chan_2.2);
2157         assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3);
2158 }
2159
2160 #[test]
2161 fn channel_reserve_in_flight_removes() {
2162         // In cases where one side claims an HTLC, it thinks it has additional available funds that it
2163         // can send to its counterparty, but due to update ordering, the other side may not yet have
2164         // considered those HTLCs fully removed.
2165         // This tests that we don't count HTLCs which will not be included in the next remote
2166         // commitment transaction towards the reserve value (as it implies no commitment transaction
2167         // will be generated which violates the remote reserve value).
2168         // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
2169         // To test this we:
2170         //  * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
2171         //    you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
2172         //    you only consider the value of the first HTLC, it may not),
2173         //  * start routing a third HTLC from A to B,
2174         //  * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
2175         //    the other claim in its holding cell, as it immediately goes into AwaitingRAA),
2176         //  * deliver the first fulfill from B
2177         //  * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
2178         //    claim,
2179         //  * deliver A's response CS and RAA.
2180         //    This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
2181         //    removed it fully. B now has the push_msat plus the first two HTLCs in value.
2182         //  * Now B happily sends another HTLC, potentially violating its reserve value from A's point
2183         //    of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
2184         let chanmon_cfgs = create_chanmon_cfgs(2);
2185         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2186         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2187         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2188         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2189         let logger = test_utils::TestLogger::new();
2190
2191         let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2);
2192         // Route the first two HTLCs.
2193         let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000);
2194         let (payment_preimage_2, _, _) = route_payment(&nodes[0], &[&nodes[1]], 20000);
2195
2196         // Start routing the third HTLC (this is just used to get everyone in the right state).
2197         let (payment_preimage_3, payment_hash_3, payment_secret_3) = get_payment_preimage_hash!(nodes[1]);
2198         let send_1 = {
2199                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
2200                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
2201                 nodes[0].node.send_payment(&route, payment_hash_3, &Some(payment_secret_3)).unwrap();
2202                 check_added_monitors!(nodes[0], 1);
2203                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2204                 assert_eq!(events.len(), 1);
2205                 SendEvent::from_event(events.remove(0))
2206         };
2207
2208         // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
2209         // initial fulfill/CS.
2210         assert!(nodes[1].node.claim_funds(payment_preimage_1));
2211         check_added_monitors!(nodes[1], 1);
2212         let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2213
2214         // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
2215         // remove the second HTLC when we send the HTLC back from B to A.
2216         assert!(nodes[1].node.claim_funds(payment_preimage_2));
2217         check_added_monitors!(nodes[1], 1);
2218         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2219
2220         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]);
2221         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed);
2222         check_added_monitors!(nodes[0], 1);
2223         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2224         expect_payment_sent!(nodes[0], payment_preimage_1);
2225
2226         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]);
2227         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg);
2228         check_added_monitors!(nodes[1], 1);
2229         // B is already AwaitingRAA, so cant generate a CS here
2230         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2231
2232         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2233         check_added_monitors!(nodes[1], 1);
2234         let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2235
2236         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2237         check_added_monitors!(nodes[0], 1);
2238         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2239
2240         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2241         check_added_monitors!(nodes[1], 1);
2242         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2243
2244         // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
2245         // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
2246         // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
2247         // can no longer broadcast a commitment transaction with it and B has the preimage so can go
2248         // on-chain as necessary).
2249         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]);
2250         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed);
2251         check_added_monitors!(nodes[0], 1);
2252         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2253         expect_payment_sent!(nodes[0], payment_preimage_2);
2254
2255         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2256         check_added_monitors!(nodes[1], 1);
2257         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2258
2259         expect_pending_htlcs_forwardable!(nodes[1]);
2260         expect_payment_received!(nodes[1], payment_hash_3, payment_secret_3, 100000);
2261
2262         // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
2263         // resolve the second HTLC from A's point of view.
2264         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2265         check_added_monitors!(nodes[0], 1);
2266         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2267
2268         // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
2269         // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
2270         let (payment_preimage_4, payment_hash_4, payment_secret_4) = get_payment_preimage_hash!(nodes[0]);
2271         let send_2 = {
2272                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
2273                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 10000, TEST_FINAL_CLTV, &logger).unwrap();
2274                 nodes[1].node.send_payment(&route, payment_hash_4, &Some(payment_secret_4)).unwrap();
2275                 check_added_monitors!(nodes[1], 1);
2276                 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2277                 assert_eq!(events.len(), 1);
2278                 SendEvent::from_event(events.remove(0))
2279         };
2280
2281         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]);
2282         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg);
2283         check_added_monitors!(nodes[0], 1);
2284         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2285
2286         // Now just resolve all the outstanding messages/HTLCs for completeness...
2287
2288         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2289         check_added_monitors!(nodes[1], 1);
2290         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2291
2292         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2293         check_added_monitors!(nodes[1], 1);
2294
2295         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2296         check_added_monitors!(nodes[0], 1);
2297         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2298
2299         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2300         check_added_monitors!(nodes[1], 1);
2301         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2302
2303         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2304         check_added_monitors!(nodes[0], 1);
2305
2306         expect_pending_htlcs_forwardable!(nodes[0]);
2307         expect_payment_received!(nodes[0], payment_hash_4, payment_secret_4, 10000);
2308
2309         claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
2310         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
2311 }
2312
2313 #[test]
2314 fn channel_monitor_network_test() {
2315         // Simple test which builds a network of ChannelManagers, connects them to each other, and
2316         // tests that ChannelMonitor is able to recover from various states.
2317         let chanmon_cfgs = create_chanmon_cfgs(5);
2318         let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
2319         let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
2320         let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
2321
2322         // Create some initial channels
2323         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2324         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
2325         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
2326         let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4, InitFeatures::known(), InitFeatures::known());
2327
2328         // Make sure all nodes are at the same starting height
2329         connect_blocks(&nodes[0], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
2330         connect_blocks(&nodes[1], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
2331         connect_blocks(&nodes[2], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
2332         connect_blocks(&nodes[3], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);
2333         connect_blocks(&nodes[4], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[4].best_block_info().1);
2334
2335         // Rebalance the network a bit by relaying one payment through all the channels...
2336         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2337         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2338         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2339         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2340
2341         // Simple case with no pending HTLCs:
2342         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
2343         check_added_monitors!(nodes[1], 1);
2344         check_closed_broadcast!(nodes[1], false);
2345         {
2346                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
2347                 assert_eq!(node_txn.len(), 1);
2348                 mine_transaction(&nodes[0], &node_txn[0]);
2349                 check_added_monitors!(nodes[0], 1);
2350                 test_txn_broadcast(&nodes[0], &chan_1, None, HTLCType::NONE);
2351         }
2352         check_closed_broadcast!(nodes[0], true);
2353         assert_eq!(nodes[0].node.list_channels().len(), 0);
2354         assert_eq!(nodes[1].node.list_channels().len(), 1);
2355
2356         // One pending HTLC is discarded by the force-close:
2357         let payment_preimage_1 = route_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 3000000).0;
2358
2359         // Simple case of one pending HTLC to HTLC-Timeout (note that the HTLC-Timeout is not
2360         // broadcasted until we reach the timelock time).
2361         nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
2362         check_closed_broadcast!(nodes[1], false);
2363         check_added_monitors!(nodes[1], 1);
2364         {
2365                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::NONE);
2366                 connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2367                 test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
2368                 mine_transaction(&nodes[2], &node_txn[0]);
2369                 check_added_monitors!(nodes[2], 1);
2370                 test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::NONE);
2371         }
2372         check_closed_broadcast!(nodes[2], true);
2373         assert_eq!(nodes[1].node.list_channels().len(), 0);
2374         assert_eq!(nodes[2].node.list_channels().len(), 1);
2375
2376         macro_rules! claim_funds {
2377                 ($node: expr, $prev_node: expr, $preimage: expr) => {
2378                         {
2379                                 assert!($node.node.claim_funds($preimage));
2380                                 check_added_monitors!($node, 1);
2381
2382                                 let events = $node.node.get_and_clear_pending_msg_events();
2383                                 assert_eq!(events.len(), 1);
2384                                 match events[0] {
2385                                         MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
2386                                                 assert!(update_add_htlcs.is_empty());
2387                                                 assert!(update_fail_htlcs.is_empty());
2388                                                 assert_eq!(*node_id, $prev_node.node.get_our_node_id());
2389                                         },
2390                                         _ => panic!("Unexpected event"),
2391                                 };
2392                         }
2393                 }
2394         }
2395
2396         // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
2397         // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
2398         nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
2399         check_added_monitors!(nodes[2], 1);
2400         check_closed_broadcast!(nodes[2], false);
2401         let node2_commitment_txid;
2402         {
2403                 let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::NONE);
2404                 connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2405                 test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
2406                 node2_commitment_txid = node_txn[0].txid();
2407
2408                 // Claim the payment on nodes[3], giving it knowledge of the preimage
2409                 claim_funds!(nodes[3], nodes[2], payment_preimage_1);
2410                 mine_transaction(&nodes[3], &node_txn[0]);
2411                 check_added_monitors!(nodes[3], 1);
2412                 check_preimage_claim(&nodes[3], &node_txn);
2413         }
2414         check_closed_broadcast!(nodes[3], true);
2415         assert_eq!(nodes[2].node.list_channels().len(), 0);
2416         assert_eq!(nodes[3].node.list_channels().len(), 1);
2417
2418         // Drop the ChannelMonitor for the previous channel to avoid it broadcasting transactions and
2419         // confusing us in the following tests.
2420         let chan_3_mon = nodes[3].chain_monitor.chain_monitor.monitors.write().unwrap().remove(&OutPoint { txid: chan_3.3.txid(), index: 0 }).unwrap();
2421
2422         // One pending HTLC to time out:
2423         let payment_preimage_2 = route_payment(&nodes[3], &vec!(&nodes[4])[..], 3000000).0;
2424         // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
2425         // buffer space).
2426
2427         let (close_chan_update_1, close_chan_update_2) = {
2428                 connect_blocks(&nodes[3], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
2429                 let events = nodes[3].node.get_and_clear_pending_msg_events();
2430                 assert_eq!(events.len(), 2);
2431                 let close_chan_update_1 = match events[0] {
2432                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2433                                 msg.clone()
2434                         },
2435                         _ => panic!("Unexpected event"),
2436                 };
2437                 match events[1] {
2438                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2439                                 assert_eq!(node_id, nodes[4].node.get_our_node_id());
2440                         },
2441                         _ => panic!("Unexpected event"),
2442                 }
2443                 check_added_monitors!(nodes[3], 1);
2444
2445                 // Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer.
2446                 {
2447                         let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap();
2448                         node_txn.retain(|tx| {
2449                                 if tx.input[0].previous_output.txid == node2_commitment_txid {
2450                                         false
2451                                 } else { true }
2452                         });
2453                 }
2454
2455                 let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
2456
2457                 // Claim the payment on nodes[4], giving it knowledge of the preimage
2458                 claim_funds!(nodes[4], nodes[3], payment_preimage_2);
2459
2460                 connect_blocks(&nodes[4], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2461                 let events = nodes[4].node.get_and_clear_pending_msg_events();
2462                 assert_eq!(events.len(), 2);
2463                 let close_chan_update_2 = match events[0] {
2464                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2465                                 msg.clone()
2466                         },
2467                         _ => panic!("Unexpected event"),
2468                 };
2469                 match events[1] {
2470                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2471                                 assert_eq!(node_id, nodes[3].node.get_our_node_id());
2472                         },
2473                         _ => panic!("Unexpected event"),
2474                 }
2475                 check_added_monitors!(nodes[4], 1);
2476                 test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
2477
2478                 mine_transaction(&nodes[4], &node_txn[0]);
2479                 check_preimage_claim(&nodes[4], &node_txn);
2480                 (close_chan_update_1, close_chan_update_2)
2481         };
2482         nodes[3].net_graph_msg_handler.handle_channel_update(&close_chan_update_2).unwrap();
2483         nodes[4].net_graph_msg_handler.handle_channel_update(&close_chan_update_1).unwrap();
2484         assert_eq!(nodes[3].node.list_channels().len(), 0);
2485         assert_eq!(nodes[4].node.list_channels().len(), 0);
2486
2487         nodes[3].chain_monitor.chain_monitor.monitors.write().unwrap().insert(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon);
2488 }
2489
2490 #[test]
2491 fn test_justice_tx() {
2492         // Test justice txn built on revoked HTLC-Success tx, against both sides
2493         let mut alice_config = UserConfig::default();
2494         alice_config.channel_options.announced_channel = true;
2495         alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
2496         alice_config.own_channel_config.our_to_self_delay = 6 * 24 * 5;
2497         let mut bob_config = UserConfig::default();
2498         bob_config.channel_options.announced_channel = true;
2499         bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
2500         bob_config.own_channel_config.our_to_self_delay = 6 * 24 * 3;
2501         let user_cfgs = [Some(alice_config), Some(bob_config)];
2502         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2503         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2504         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2505         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2506         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2507         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2508         // Create some new channels:
2509         let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2510
2511         // A pending HTLC which will be revoked:
2512         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2513         // Get the will-be-revoked local txn from nodes[0]
2514         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_5.2);
2515         assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
2516         assert_eq!(revoked_local_txn[0].input.len(), 1);
2517         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
2518         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
2519         assert_eq!(revoked_local_txn[1].input.len(), 1);
2520         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2521         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2522         // Revoke the old state
2523         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
2524
2525         {
2526                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2527                 {
2528                         let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2529                         assert_eq!(node_txn.len(), 2); // ChannelMonitor: penalty tx, ChannelManager: local commitment tx
2530                         assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
2531
2532                         check_spends!(node_txn[0], revoked_local_txn[0]);
2533                         node_txn.swap_remove(0);
2534                         node_txn.truncate(1);
2535                 }
2536                 check_added_monitors!(nodes[1], 1);
2537                 test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
2538
2539                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2540                 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
2541                 // Verify broadcast of revoked HTLC-timeout
2542                 let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
2543                 check_added_monitors!(nodes[0], 1);
2544                 // Broadcast revoked HTLC-timeout on node 1
2545                 mine_transaction(&nodes[1], &node_txn[1]);
2546                 test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone());
2547         }
2548         get_announce_close_broadcast_events(&nodes, 0, 1);
2549
2550         assert_eq!(nodes[0].node.list_channels().len(), 0);
2551         assert_eq!(nodes[1].node.list_channels().len(), 0);
2552
2553         // We test justice_tx build by A on B's revoked HTLC-Success tx
2554         // Create some new channels:
2555         let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2556         {
2557                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2558                 node_txn.clear();
2559         }
2560
2561         // A pending HTLC which will be revoked:
2562         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2563         // Get the will-be-revoked local txn from B
2564         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_6.2);
2565         assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
2566         assert_eq!(revoked_local_txn[0].input.len(), 1);
2567         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
2568         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
2569         // Revoke the old state
2570         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
2571         {
2572                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2573                 {
2574                         let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
2575                         assert_eq!(node_txn.len(), 2); //ChannelMonitor: penalty tx, ChannelManager: local commitment tx
2576                         assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
2577
2578                         check_spends!(node_txn[0], revoked_local_txn[0]);
2579                         node_txn.swap_remove(0);
2580                 }
2581                 check_added_monitors!(nodes[0], 1);
2582                 test_txn_broadcast(&nodes[0], &chan_6, None, HTLCType::NONE);
2583
2584                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2585                 let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
2586                 check_added_monitors!(nodes[1], 1);
2587                 mine_transaction(&nodes[0], &node_txn[1]);
2588                 test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone(), revoked_local_txn[0].clone());
2589         }
2590         get_announce_close_broadcast_events(&nodes, 0, 1);
2591         assert_eq!(nodes[0].node.list_channels().len(), 0);
2592         assert_eq!(nodes[1].node.list_channels().len(), 0);
2593 }
2594
2595 #[test]
2596 fn revoked_output_claim() {
2597         // Simple test to ensure a node will claim a revoked output when a stale remote commitment
2598         // transaction is broadcast by its counterparty
2599         let chanmon_cfgs = create_chanmon_cfgs(2);
2600         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2601         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2602         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2603         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2604         // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
2605         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2606         assert_eq!(revoked_local_txn.len(), 1);
2607         // Only output is the full channel value back to nodes[0]:
2608         assert_eq!(revoked_local_txn[0].output.len(), 1);
2609         // Send a payment through, updating everyone's latest commitment txn
2610         send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
2611
2612         // Inform nodes[1] that nodes[0] broadcast a stale tx
2613         mine_transaction(&nodes[1], &revoked_local_txn[0]);
2614         check_added_monitors!(nodes[1], 1);
2615         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2616         assert_eq!(node_txn.len(), 2); // ChannelMonitor: justice tx against revoked to_local output, ChannelManager: local commitment tx
2617
2618         check_spends!(node_txn[0], revoked_local_txn[0]);
2619         check_spends!(node_txn[1], chan_1.3);
2620
2621         // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
2622         mine_transaction(&nodes[0], &revoked_local_txn[0]);
2623         get_announce_close_broadcast_events(&nodes, 0, 1);
2624         check_added_monitors!(nodes[0], 1)
2625 }
2626
2627 #[test]
2628 fn claim_htlc_outputs_shared_tx() {
2629         // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
2630         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2631         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2632         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2633         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2634         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2635
2636         // Create some new channel:
2637         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2638
2639         // Rebalance the network to generate htlc in the two directions
2640         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
2641         // 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
2642         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2643         let (_payment_preimage_2, payment_hash_2, _) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
2644
2645         // Get the will-be-revoked local txn from node[0]
2646         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2647         assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
2648         assert_eq!(revoked_local_txn[0].input.len(), 1);
2649         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
2650         assert_eq!(revoked_local_txn[1].input.len(), 1);
2651         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2652         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2653         check_spends!(revoked_local_txn[1], revoked_local_txn[0]);
2654
2655         //Revoke the old state
2656         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2657
2658         {
2659                 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2660                 check_added_monitors!(nodes[0], 1);
2661                 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2662                 check_added_monitors!(nodes[1], 1);
2663                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2664                 expect_payment_failed!(nodes[1], payment_hash_2, true);
2665
2666                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2667                 assert_eq!(node_txn.len(), 2); // ChannelMonitor: penalty tx, ChannelManager: local commitment
2668
2669                 assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
2670                 check_spends!(node_txn[0], revoked_local_txn[0]);
2671
2672                 let mut witness_lens = BTreeSet::new();
2673                 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2674                 witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
2675                 witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
2676                 assert_eq!(witness_lens.len(), 3);
2677                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2678                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2679                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2680
2681                 // Next nodes[1] broadcasts its current local tx state:
2682                 assert_eq!(node_txn[1].input.len(), 1);
2683                 assert_eq!(node_txn[1].input[0].previous_output.txid, chan_1.3.txid()); //Spending funding tx unique txouput, tx broadcasted by ChannelManager
2684         }
2685         get_announce_close_broadcast_events(&nodes, 0, 1);
2686         assert_eq!(nodes[0].node.list_channels().len(), 0);
2687         assert_eq!(nodes[1].node.list_channels().len(), 0);
2688 }
2689
2690 #[test]
2691 fn claim_htlc_outputs_single_tx() {
2692         // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
2693         let mut chanmon_cfgs = create_chanmon_cfgs(2);
2694         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2695         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2696         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2697         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2698
2699         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2700
2701         // Rebalance the network to generate htlc in the two directions
2702         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
2703         // 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
2704         // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
2705         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2706         let (_payment_preimage_2, payment_hash_2, _payment_secret_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
2707
2708         // Get the will-be-revoked local txn from node[0]
2709         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2710
2711         //Revoke the old state
2712         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2713
2714         {
2715                 confirm_transaction_at(&nodes[0], &revoked_local_txn[0], 100);
2716                 check_added_monitors!(nodes[0], 1);
2717                 confirm_transaction_at(&nodes[1], &revoked_local_txn[0], 100);
2718                 check_added_monitors!(nodes[1], 1);
2719                 expect_pending_htlcs_forwardable_ignore!(nodes[0]);
2720
2721                 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2722                 expect_payment_failed!(nodes[1], payment_hash_2, true);
2723
2724                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2725                 assert_eq!(node_txn.len(), 9);
2726                 // ChannelMonitor: justice tx revoked offered htlc, justice tx revoked received htlc, justice tx revoked to_local (3)
2727                 // ChannelManager: local commmitment + local HTLC-timeout (2)
2728                 // ChannelMonitor: bumped justice tx, after one increase, bumps on HTLC aren't generated not being substantial anymore, bump on revoked to_local isn't generated due to more room for expiration (2)
2729                 // ChannelMonitor: local commitment + local HTLC-timeout (2)
2730
2731                 // Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
2732                 assert_eq!(node_txn[0].input.len(), 1);
2733                 check_spends!(node_txn[0], chan_1.3);
2734                 assert_eq!(node_txn[1].input.len(), 1);
2735                 let witness_script = node_txn[1].input[0].witness.last().unwrap();
2736                 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
2737                 check_spends!(node_txn[1], node_txn[0]);
2738
2739                 // Justice transactions are indices 1-2-4
2740                 assert_eq!(node_txn[2].input.len(), 1);
2741                 assert_eq!(node_txn[3].input.len(), 1);
2742                 assert_eq!(node_txn[4].input.len(), 1);
2743
2744                 check_spends!(node_txn[2], revoked_local_txn[0]);
2745                 check_spends!(node_txn[3], revoked_local_txn[0]);
2746                 check_spends!(node_txn[4], revoked_local_txn[0]);
2747
2748                 let mut witness_lens = BTreeSet::new();
2749                 witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
2750                 witness_lens.insert(node_txn[3].input[0].witness.last().unwrap().len());
2751                 witness_lens.insert(node_txn[4].input[0].witness.last().unwrap().len());
2752                 assert_eq!(witness_lens.len(), 3);
2753                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2754                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2755                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2756         }
2757         get_announce_close_broadcast_events(&nodes, 0, 1);
2758         assert_eq!(nodes[0].node.list_channels().len(), 0);
2759         assert_eq!(nodes[1].node.list_channels().len(), 0);
2760 }
2761
2762 #[test]
2763 fn test_htlc_on_chain_success() {
2764         // Test that in case of a unilateral close onchain, we detect the state of output and pass
2765         // the preimage backward accordingly. So here we test that ChannelManager is
2766         // broadcasting the right event to other nodes in payment path.
2767         // We test with two HTLCs simultaneously as that was not handled correctly in the past.
2768         // A --------------------> B ----------------------> C (preimage)
2769         // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
2770         // commitment transaction was broadcast.
2771         // Then, B should learn the preimage from said transactions, attempting to claim backwards
2772         // towards B.
2773         // B should be able to claim via preimage if A then broadcasts its local tx.
2774         // Finally, when A sees B's latest local commitment transaction it should be able to claim
2775         // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
2776         // PaymentSent event).
2777
2778         let chanmon_cfgs = create_chanmon_cfgs(3);
2779         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2780         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2781         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2782
2783         // Create some initial channels
2784         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2785         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
2786
2787         // Ensure all nodes are at the same height
2788         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
2789         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
2790         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
2791         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
2792
2793         // Rebalance the network a bit by relaying one payment through all the channels...
2794         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2795         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2796
2797         let (our_payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2798         let (our_payment_preimage_2, _payment_hash_2, _payment_secret_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2799
2800         // Broadcast legit commitment tx from C on B's chain
2801         // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
2802         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
2803         assert_eq!(commitment_tx.len(), 1);
2804         check_spends!(commitment_tx[0], chan_2.3);
2805         nodes[2].node.claim_funds(our_payment_preimage);
2806         nodes[2].node.claim_funds(our_payment_preimage_2);
2807         check_added_monitors!(nodes[2], 2);
2808         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2809         assert!(updates.update_add_htlcs.is_empty());
2810         assert!(updates.update_fail_htlcs.is_empty());
2811         assert!(updates.update_fail_malformed_htlcs.is_empty());
2812         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2813
2814         mine_transaction(&nodes[2], &commitment_tx[0]);
2815         check_closed_broadcast!(nodes[2], true);
2816         check_added_monitors!(nodes[2], 1);
2817         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 3 (commitment tx, 2*htlc-success tx), ChannelMonitor : 2 (2 * HTLC-Success tx)
2818         assert_eq!(node_txn.len(), 5);
2819         assert_eq!(node_txn[0], node_txn[3]);
2820         assert_eq!(node_txn[1], node_txn[4]);
2821         assert_eq!(node_txn[2], commitment_tx[0]);
2822         check_spends!(node_txn[0], commitment_tx[0]);
2823         check_spends!(node_txn[1], commitment_tx[0]);
2824         assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2825         assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2826         assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2827         assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2828         assert_eq!(node_txn[0].lock_time, 0);
2829         assert_eq!(node_txn[1].lock_time, 0);
2830
2831         // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
2832         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2833         connect_block(&nodes[1], &Block { header, txdata: node_txn});
2834         connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
2835         {
2836                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2837                 assert_eq!(added_monitors.len(), 1);
2838                 assert_eq!(added_monitors[0].0.txid, chan_2.3.txid());
2839                 added_monitors.clear();
2840         }
2841         let forwarded_events = nodes[1].node.get_and_clear_pending_events();
2842         assert_eq!(forwarded_events.len(), 2);
2843         if let Event::PaymentForwarded { fee_earned_msat: Some(1000), claim_from_onchain_tx: true } = forwarded_events[0] {
2844                 } else { panic!(); }
2845         if let Event::PaymentForwarded { fee_earned_msat: Some(1000), claim_from_onchain_tx: true } = forwarded_events[1] {
2846                 } else { panic!(); }
2847         let events = nodes[1].node.get_and_clear_pending_msg_events();
2848         {
2849                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2850                 assert_eq!(added_monitors.len(), 2);
2851                 assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
2852                 assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
2853                 added_monitors.clear();
2854         }
2855         assert_eq!(events.len(), 3);
2856         match events[0] {
2857                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
2858                 _ => panic!("Unexpected event"),
2859         }
2860         match events[1] {
2861                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
2862                 _ => panic!("Unexpected event"),
2863         }
2864
2865         match events[2] {
2866                 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, .. } } => {
2867                         assert!(update_add_htlcs.is_empty());
2868                         assert!(update_fail_htlcs.is_empty());
2869                         assert_eq!(update_fulfill_htlcs.len(), 1);
2870                         assert!(update_fail_malformed_htlcs.is_empty());
2871                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2872                 },
2873                 _ => panic!("Unexpected event"),
2874         };
2875         macro_rules! check_tx_local_broadcast {
2876                 ($node: expr, $htlc_offered: expr, $commitment_tx: expr, $chan_tx: expr) => { {
2877                         let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
2878                         assert_eq!(node_txn.len(), 3);
2879                         // Node[1]: ChannelManager: 3 (commitment tx, 2*HTLC-Timeout tx), ChannelMonitor: 2 (timeout tx)
2880                         // Node[0]: ChannelManager: 3 (commtiemtn tx, 2*HTLC-Timeout tx), ChannelMonitor: 2 HTLC-timeout
2881                         check_spends!(node_txn[1], $commitment_tx);
2882                         check_spends!(node_txn[2], $commitment_tx);
2883                         assert_ne!(node_txn[1].lock_time, 0);
2884                         assert_ne!(node_txn[2].lock_time, 0);
2885                         if $htlc_offered {
2886                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2887                                 assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2888                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2889                                 assert!(node_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2890                         } else {
2891                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2892                                 assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2893                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2894                                 assert!(node_txn[2].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2895                         }
2896                         check_spends!(node_txn[0], $chan_tx);
2897                         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
2898                         node_txn.clear();
2899                 } }
2900         }
2901         // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate
2902         // commitment transaction with a corresponding HTLC-Timeout transactions, as well as a
2903         // timeout-claim of the output that nodes[2] just claimed via success.
2904         check_tx_local_broadcast!(nodes[1], false, commitment_tx[0], chan_2.3);
2905
2906         // Broadcast legit commitment tx from A on B's chain
2907         // Broadcast preimage tx by B on offered output from A commitment tx  on A's chain
2908         let node_a_commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
2909         check_spends!(node_a_commitment_tx[0], chan_1.3);
2910         mine_transaction(&nodes[1], &node_a_commitment_tx[0]);
2911         check_closed_broadcast!(nodes[1], true);
2912         check_added_monitors!(nodes[1], 1);
2913         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2914         assert_eq!(node_txn.len(), 6); // ChannelManager : 3 (commitment tx + HTLC-Sucess * 2), ChannelMonitor : 3 (HTLC-Success, 2* RBF bumps of above HTLC txn)
2915         let commitment_spend =
2916                 if node_txn[0].input[0].previous_output.txid == node_a_commitment_tx[0].txid() {
2917                         check_spends!(node_txn[1], commitment_tx[0]);
2918                         check_spends!(node_txn[2], commitment_tx[0]);
2919                         assert_ne!(node_txn[1].input[0].previous_output.vout, node_txn[2].input[0].previous_output.vout);
2920                         &node_txn[0]
2921                 } else {
2922                         check_spends!(node_txn[0], commitment_tx[0]);
2923                         check_spends!(node_txn[1], commitment_tx[0]);
2924                         assert_ne!(node_txn[0].input[0].previous_output.vout, node_txn[1].input[0].previous_output.vout);
2925                         &node_txn[2]
2926                 };
2927
2928         check_spends!(commitment_spend, node_a_commitment_tx[0]);
2929         assert_eq!(commitment_spend.input.len(), 2);
2930         assert_eq!(commitment_spend.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2931         assert_eq!(commitment_spend.input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2932         assert_eq!(commitment_spend.lock_time, 0);
2933         assert!(commitment_spend.output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2934         check_spends!(node_txn[3], chan_1.3);
2935         assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
2936         check_spends!(node_txn[4], node_txn[3]);
2937         check_spends!(node_txn[5], node_txn[3]);
2938         // We don't bother to check that B can claim the HTLC output on its commitment tx here as
2939         // we already checked the same situation with A.
2940
2941         // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
2942         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2943         connect_block(&nodes[0], &Block { header, txdata: vec![node_a_commitment_tx[0].clone(), commitment_spend.clone()] });
2944         connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
2945         check_closed_broadcast!(nodes[0], true);
2946         check_added_monitors!(nodes[0], 1);
2947         let events = nodes[0].node.get_and_clear_pending_events();
2948         assert_eq!(events.len(), 2);
2949         let mut first_claimed = false;
2950         for event in events {
2951                 match event {
2952                         Event::PaymentSent { payment_preimage } => {
2953                                 if payment_preimage == our_payment_preimage {
2954                                         assert!(!first_claimed);
2955                                         first_claimed = true;
2956                                 } else {
2957                                         assert_eq!(payment_preimage, our_payment_preimage_2);
2958                                 }
2959                         },
2960                         _ => panic!("Unexpected event"),
2961                 }
2962         }
2963         check_tx_local_broadcast!(nodes[0], true, node_a_commitment_tx[0], chan_1.3);
2964 }
2965
2966 fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
2967         // Test that in case of a unilateral close onchain, we detect the state of output and
2968         // timeout the HTLC backward accordingly. So here we test that ChannelManager is
2969         // broadcasting the right event to other nodes in payment path.
2970         // A ------------------> B ----------------------> C (timeout)
2971         //    B's commitment tx                 C's commitment tx
2972         //            \                                  \
2973         //         B's HTLC timeout tx               B's timeout tx
2974
2975         let chanmon_cfgs = create_chanmon_cfgs(3);
2976         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2977         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2978         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2979         *nodes[0].connect_style.borrow_mut() = connect_style;
2980         *nodes[1].connect_style.borrow_mut() = connect_style;
2981         *nodes[2].connect_style.borrow_mut() = connect_style;
2982
2983         // Create some intial channels
2984         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
2985         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
2986
2987         // Rebalance the network a bit by relaying one payment thorugh all the channels...
2988         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2989         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2990
2991         let (_payment_preimage, payment_hash, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2992
2993         // Broadcast legit commitment tx from C on B's chain
2994         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
2995         check_spends!(commitment_tx[0], chan_2.3);
2996         nodes[2].node.fail_htlc_backwards(&payment_hash);
2997         check_added_monitors!(nodes[2], 0);
2998         expect_pending_htlcs_forwardable!(nodes[2]);
2999         check_added_monitors!(nodes[2], 1);
3000
3001         let events = nodes[2].node.get_and_clear_pending_msg_events();
3002         assert_eq!(events.len(), 1);
3003         match events[0] {
3004                 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, .. } } => {
3005                         assert!(update_add_htlcs.is_empty());
3006                         assert!(!update_fail_htlcs.is_empty());
3007                         assert!(update_fulfill_htlcs.is_empty());
3008                         assert!(update_fail_malformed_htlcs.is_empty());
3009                         assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
3010                 },
3011                 _ => panic!("Unexpected event"),
3012         };
3013         mine_transaction(&nodes[2], &commitment_tx[0]);
3014         check_closed_broadcast!(nodes[2], true);
3015         check_added_monitors!(nodes[2], 1);
3016         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
3017         assert_eq!(node_txn.len(), 1);
3018         check_spends!(node_txn[0], chan_2.3);
3019         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
3020
3021         // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
3022         // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
3023         connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
3024         mine_transaction(&nodes[1], &commitment_tx[0]);
3025         let timeout_tx;
3026         {
3027                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3028                 assert_eq!(node_txn.len(), 5); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (local commitment tx + HTLC-timeout), 1 timeout tx
3029                 assert_eq!(node_txn[0], node_txn[3]);
3030                 assert_eq!(node_txn[1], node_txn[4]);
3031
3032                 check_spends!(node_txn[2], commitment_tx[0]);
3033                 assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3034
3035                 check_spends!(node_txn[0], chan_2.3);
3036                 check_spends!(node_txn[1], node_txn[0]);
3037                 assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 71);
3038                 assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
3039
3040                 timeout_tx = node_txn[2].clone();
3041                 node_txn.clear();
3042         }
3043
3044         mine_transaction(&nodes[1], &timeout_tx);
3045         check_added_monitors!(nodes[1], 1);
3046         check_closed_broadcast!(nodes[1], true);
3047         {
3048                 // B will rebroadcast a fee-bumped timeout transaction here.
3049                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3050                 assert_eq!(node_txn.len(), 1);
3051                 check_spends!(node_txn[0], commitment_tx[0]);
3052         }
3053
3054         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3055         {
3056                 // B may rebroadcast its own holder commitment transaction here, as a safeguard against
3057                 // some incredibly unlikely partial-eclipse-attack scenarios. That said, because the
3058                 // original commitment_tx[0] (also spending chan_2.3) has reached ANTI_REORG_DELAY B really
3059                 // shouldn't broadcast anything here, and in some connect style scenarios we do not.
3060                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3061                 if node_txn.len() == 1 {
3062                         check_spends!(node_txn[0], chan_2.3);
3063                 } else {
3064                         assert_eq!(node_txn.len(), 0);
3065                 }
3066         }
3067
3068         expect_pending_htlcs_forwardable!(nodes[1]);
3069         check_added_monitors!(nodes[1], 1);
3070         let events = nodes[1].node.get_and_clear_pending_msg_events();
3071         assert_eq!(events.len(), 1);
3072         match events[0] {
3073                 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, .. } } => {
3074                         assert!(update_add_htlcs.is_empty());
3075                         assert!(!update_fail_htlcs.is_empty());
3076                         assert!(update_fulfill_htlcs.is_empty());
3077                         assert!(update_fail_malformed_htlcs.is_empty());
3078                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3079                 },
3080                 _ => panic!("Unexpected event"),
3081         };
3082
3083         // Broadcast legit commitment tx from B on A's chain
3084         let commitment_tx = get_local_commitment_txn!(nodes[1], chan_1.2);
3085         check_spends!(commitment_tx[0], chan_1.3);
3086
3087         mine_transaction(&nodes[0], &commitment_tx[0]);
3088         connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
3089
3090         check_closed_broadcast!(nodes[0], true);
3091         check_added_monitors!(nodes[0], 1);
3092         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 commitment tx, ChannelMonitor : 1 timeout tx
3093         assert_eq!(node_txn.len(), 2);
3094         check_spends!(node_txn[0], chan_1.3);
3095         assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 71);
3096         check_spends!(node_txn[1], commitment_tx[0]);
3097         assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3098 }
3099
3100 #[test]
3101 fn test_htlc_on_chain_timeout() {
3102         do_test_htlc_on_chain_timeout(ConnectStyle::BestBlockFirstSkippingBlocks);
3103         do_test_htlc_on_chain_timeout(ConnectStyle::TransactionsFirstSkippingBlocks);
3104         do_test_htlc_on_chain_timeout(ConnectStyle::FullBlockViaListen);
3105 }
3106
3107 #[test]
3108 fn test_simple_commitment_revoked_fail_backward() {
3109         // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
3110         // and fail backward accordingly.
3111
3112         let chanmon_cfgs = create_chanmon_cfgs(3);
3113         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3114         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3115         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3116
3117         // Create some initial channels
3118         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3119         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
3120
3121         let (payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3122         // Get the will-be-revoked local txn from nodes[2]
3123         let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3124         // Revoke the old state
3125         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3126
3127         let (_, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3128
3129         mine_transaction(&nodes[1], &revoked_local_txn[0]);
3130         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3131         check_added_monitors!(nodes[1], 1);
3132         check_closed_broadcast!(nodes[1], true);
3133
3134         expect_pending_htlcs_forwardable!(nodes[1]);
3135         check_added_monitors!(nodes[1], 1);
3136         let events = nodes[1].node.get_and_clear_pending_msg_events();
3137         assert_eq!(events.len(), 1);
3138         match events[0] {
3139                 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, .. } } => {
3140                         assert!(update_add_htlcs.is_empty());
3141                         assert_eq!(update_fail_htlcs.len(), 1);
3142                         assert!(update_fulfill_htlcs.is_empty());
3143                         assert!(update_fail_malformed_htlcs.is_empty());
3144                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3145
3146                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3147                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3148                         expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, true);
3149                         expect_payment_failed!(nodes[0], payment_hash, false);
3150                 },
3151                 _ => panic!("Unexpected event"),
3152         }
3153 }
3154
3155 fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
3156         // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
3157         // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
3158         // commitment transaction anymore.
3159         // To do this, we have the peer which will broadcast a revoked commitment transaction send
3160         // a number of update_fail/commitment_signed updates without ever sending the RAA in
3161         // response to our commitment_signed. This is somewhat misbehavior-y, though not
3162         // technically disallowed and we should probably handle it reasonably.
3163         // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
3164         // failed/fulfilled backwards must be in at least one of the latest two remote commitment
3165         // transactions:
3166         // * Once we move it out of our holding cell/add it, we will immediately include it in a
3167         //   commitment_signed (implying it will be in the latest remote commitment transaction).
3168         // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
3169         //   and once they revoke the previous commitment transaction (allowing us to send a new
3170         //   commitment_signed) we will be free to fail/fulfill the HTLC backwards.
3171         let chanmon_cfgs = create_chanmon_cfgs(3);
3172         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3173         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3174         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3175
3176         // Create some initial channels
3177         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3178         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
3179
3180         let (payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
3181         // Get the will-be-revoked local txn from nodes[2]
3182         let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3183         assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
3184         // Revoke the old state
3185         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3186
3187         let value = if use_dust {
3188                 // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
3189                 // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
3190                 nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().holder_dust_limit_satoshis * 1000
3191         } else { 3000000 };
3192
3193         let (_, first_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3194         let (_, second_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3195         let (_, third_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3196
3197         assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
3198         expect_pending_htlcs_forwardable!(nodes[2]);
3199         check_added_monitors!(nodes[2], 1);
3200         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3201         assert!(updates.update_add_htlcs.is_empty());
3202         assert!(updates.update_fulfill_htlcs.is_empty());
3203         assert!(updates.update_fail_malformed_htlcs.is_empty());
3204         assert_eq!(updates.update_fail_htlcs.len(), 1);
3205         assert!(updates.update_fee.is_none());
3206         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3207         let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
3208         // Drop the last RAA from 3 -> 2
3209
3210         assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
3211         expect_pending_htlcs_forwardable!(nodes[2]);
3212         check_added_monitors!(nodes[2], 1);
3213         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3214         assert!(updates.update_add_htlcs.is_empty());
3215         assert!(updates.update_fulfill_htlcs.is_empty());
3216         assert!(updates.update_fail_malformed_htlcs.is_empty());
3217         assert_eq!(updates.update_fail_htlcs.len(), 1);
3218         assert!(updates.update_fee.is_none());
3219         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3220         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3221         check_added_monitors!(nodes[1], 1);
3222         // Note that nodes[1] is in AwaitingRAA, so won't send a CS
3223         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3224         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3225         check_added_monitors!(nodes[2], 1);
3226
3227         assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
3228         expect_pending_htlcs_forwardable!(nodes[2]);
3229         check_added_monitors!(nodes[2], 1);
3230         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3231         assert!(updates.update_add_htlcs.is_empty());
3232         assert!(updates.update_fulfill_htlcs.is_empty());
3233         assert!(updates.update_fail_malformed_htlcs.is_empty());
3234         assert_eq!(updates.update_fail_htlcs.len(), 1);
3235         assert!(updates.update_fee.is_none());
3236         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3237         // At this point first_payment_hash has dropped out of the latest two commitment
3238         // transactions that nodes[1] is tracking...
3239         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3240         check_added_monitors!(nodes[1], 1);
3241         // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
3242         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3243         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3244         check_added_monitors!(nodes[2], 1);
3245
3246         // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
3247         // on nodes[2]'s RAA.
3248         let (_, fourth_payment_hash, fourth_payment_secret) = get_payment_preimage_hash!(nodes[2]);
3249         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
3250         let logger = test_utils::TestLogger::new();
3251         let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
3252         nodes[1].node.send_payment(&route, fourth_payment_hash, &Some(fourth_payment_secret)).unwrap();
3253         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3254         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3255         check_added_monitors!(nodes[1], 0);
3256
3257         if deliver_bs_raa {
3258                 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa);
3259                 // One monitor for the new revocation preimage, no second on as we won't generate a new
3260                 // commitment transaction for nodes[0] until process_pending_htlc_forwards().
3261                 check_added_monitors!(nodes[1], 1);
3262                 let events = nodes[1].node.get_and_clear_pending_events();
3263                 assert_eq!(events.len(), 1);
3264                 match events[0] {
3265                         Event::PendingHTLCsForwardable { .. } => { },
3266                         _ => panic!("Unexpected event"),
3267                 };
3268                 // Deliberately don't process the pending fail-back so they all fail back at once after
3269                 // block connection just like the !deliver_bs_raa case
3270         }
3271
3272         let mut failed_htlcs = HashSet::new();
3273         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3274
3275         mine_transaction(&nodes[1], &revoked_local_txn[0]);
3276         check_added_monitors!(nodes[1], 1);
3277         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3278
3279         let events = nodes[1].node.get_and_clear_pending_events();
3280         assert_eq!(events.len(), if deliver_bs_raa { 1 } else { 2 });
3281         match events[0] {
3282                 Event::PaymentFailed { ref payment_hash, .. } => {
3283                         assert_eq!(*payment_hash, fourth_payment_hash);
3284                 },
3285                 _ => panic!("Unexpected event"),
3286         }
3287         if !deliver_bs_raa {
3288                 match events[1] {
3289                         Event::PendingHTLCsForwardable { .. } => { },
3290                         _ => panic!("Unexpected event"),
3291                 };
3292         }
3293         nodes[1].node.process_pending_htlc_forwards();
3294         check_added_monitors!(nodes[1], 1);
3295
3296         let events = nodes[1].node.get_and_clear_pending_msg_events();
3297         assert_eq!(events.len(), if deliver_bs_raa { 4 } else { 3 });
3298         match events[if deliver_bs_raa { 1 } else { 0 }] {
3299                 MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
3300                 _ => panic!("Unexpected event"),
3301         }
3302         match events[if deliver_bs_raa { 2 } else { 1 }] {
3303                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id, ref data } }, node_id: _ } => {
3304                         assert_eq!(channel_id, chan_2.2);
3305                         assert_eq!(data.as_str(), "Commitment or closing transaction was confirmed on chain.");
3306                 },
3307                 _ => panic!("Unexpected event"),
3308         }
3309         if deliver_bs_raa {
3310                 match events[0] {
3311                         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, .. } } => {
3312                                 assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
3313                                 assert_eq!(update_add_htlcs.len(), 1);
3314                                 assert!(update_fulfill_htlcs.is_empty());
3315                                 assert!(update_fail_htlcs.is_empty());
3316                                 assert!(update_fail_malformed_htlcs.is_empty());
3317                         },
3318                         _ => panic!("Unexpected event"),
3319                 }
3320         }
3321         match events[if deliver_bs_raa { 3 } else { 2 }] {
3322                 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, .. } } => {
3323                         assert!(update_add_htlcs.is_empty());
3324                         assert_eq!(update_fail_htlcs.len(), 3);
3325                         assert!(update_fulfill_htlcs.is_empty());
3326                         assert!(update_fail_malformed_htlcs.is_empty());
3327                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3328
3329                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3330                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]);
3331                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]);
3332
3333                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3334
3335                         let events = nodes[0].node.get_and_clear_pending_msg_events();
3336                         // If we delivered B's RAA we got an unknown preimage error, not something
3337                         // that we should update our routing table for.
3338                         assert_eq!(events.len(), if deliver_bs_raa { 2 } else { 3 });
3339                         for event in events {
3340                                 match event {
3341                                         MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
3342                                         _ => panic!("Unexpected event"),
3343                                 }
3344                         }
3345                         let events = nodes[0].node.get_and_clear_pending_events();
3346                         assert_eq!(events.len(), 3);
3347                         match events[0] {
3348                                 Event::PaymentFailed { ref payment_hash, .. } => {
3349                                         assert!(failed_htlcs.insert(payment_hash.0));
3350                                 },
3351                                 _ => panic!("Unexpected event"),
3352                         }
3353                         match events[1] {
3354                                 Event::PaymentFailed { ref payment_hash, .. } => {
3355                                         assert!(failed_htlcs.insert(payment_hash.0));
3356                                 },
3357                                 _ => panic!("Unexpected event"),
3358                         }
3359                         match events[2] {
3360                                 Event::PaymentFailed { ref payment_hash, .. } => {
3361                                         assert!(failed_htlcs.insert(payment_hash.0));
3362                                 },
3363                                 _ => panic!("Unexpected event"),
3364                         }
3365                 },
3366                 _ => panic!("Unexpected event"),
3367         }
3368
3369         assert!(failed_htlcs.contains(&first_payment_hash.0));
3370         assert!(failed_htlcs.contains(&second_payment_hash.0));
3371         assert!(failed_htlcs.contains(&third_payment_hash.0));
3372 }
3373
3374 #[test]
3375 fn test_commitment_revoked_fail_backward_exhaustive_a() {
3376         do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
3377         do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
3378         do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
3379         do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
3380 }
3381
3382 #[test]
3383 fn test_commitment_revoked_fail_backward_exhaustive_b() {
3384         do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
3385         do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
3386         do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
3387         do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
3388 }
3389
3390 #[test]
3391 fn fail_backward_pending_htlc_upon_channel_failure() {
3392         let chanmon_cfgs = create_chanmon_cfgs(2);
3393         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3394         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3395         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3396         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
3397         let logger = test_utils::TestLogger::new();
3398
3399         // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack.
3400         {
3401                 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1]);
3402                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3403                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
3404                 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
3405                 check_added_monitors!(nodes[0], 1);
3406
3407                 let payment_event = {
3408                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3409                         assert_eq!(events.len(), 1);
3410                         SendEvent::from_event(events.remove(0))
3411                 };
3412                 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
3413                 assert_eq!(payment_event.msgs.len(), 1);
3414         }
3415
3416         // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack.
3417         let (_, failed_payment_hash, failed_payment_secret) = get_payment_preimage_hash!(nodes[1]);
3418         {
3419                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3420                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
3421                 nodes[0].node.send_payment(&route, failed_payment_hash, &Some(failed_payment_secret)).unwrap();
3422                 check_added_monitors!(nodes[0], 0);
3423
3424                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3425         }
3426
3427         // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel.
3428         {
3429                 let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
3430
3431                 let secp_ctx = Secp256k1::new();
3432                 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
3433                 let current_height = nodes[1].node.best_block.read().unwrap().height() + 1;
3434                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
3435                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, &logger).unwrap();
3436                 let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(&route.paths[0], 50_000, &Some(payment_secret), current_height, &None).unwrap();
3437                 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
3438                 let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
3439
3440                 // Send a 0-msat update_add_htlc to fail the channel.
3441                 let update_add_htlc = msgs::UpdateAddHTLC {
3442                         channel_id: chan.2,
3443                         htlc_id: 0,
3444                         amount_msat: 0,
3445                         payment_hash,
3446                         cltv_expiry,
3447                         onion_routing_packet,
3448                 };
3449                 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc);
3450         }
3451
3452         // Check that Alice fails backward the pending HTLC from the second payment.
3453         expect_payment_failed!(nodes[0], failed_payment_hash, true);
3454         check_closed_broadcast!(nodes[0], true);
3455         check_added_monitors!(nodes[0], 1);
3456 }
3457
3458 #[test]
3459 fn test_htlc_ignore_latest_remote_commitment() {
3460         // Test that HTLC transactions spending the latest remote commitment transaction are simply
3461         // ignored if we cannot claim them. This originally tickled an invalid unwrap().
3462         let chanmon_cfgs = create_chanmon_cfgs(2);
3463         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3464         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3465         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3466         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3467
3468         route_payment(&nodes[0], &[&nodes[1]], 10000000);
3469         nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
3470         connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
3471         check_closed_broadcast!(nodes[0], true);
3472         check_added_monitors!(nodes[0], 1);
3473
3474         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3475         assert_eq!(node_txn.len(), 3);
3476         assert_eq!(node_txn[0], node_txn[1]);
3477
3478         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3479         connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[1].clone()]});
3480         check_closed_broadcast!(nodes[1], true);
3481         check_added_monitors!(nodes[1], 1);
3482
3483         // Duplicate the connect_block call since this may happen due to other listeners
3484         // registering new transactions
3485         header.prev_blockhash = header.block_hash();
3486         connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[2].clone()]});
3487 }
3488
3489 #[test]
3490 fn test_force_close_fail_back() {
3491         // Check which HTLCs are failed-backwards on channel force-closure
3492         let chanmon_cfgs = create_chanmon_cfgs(3);
3493         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3494         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3495         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3496         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3497         create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
3498         let logger = test_utils::TestLogger::new();
3499
3500         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
3501
3502         let mut payment_event = {
3503                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3504                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, 42, &logger).unwrap();
3505                 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
3506                 check_added_monitors!(nodes[0], 1);
3507
3508                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3509                 assert_eq!(events.len(), 1);
3510                 SendEvent::from_event(events.remove(0))
3511         };
3512
3513         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3514         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3515
3516         expect_pending_htlcs_forwardable!(nodes[1]);
3517
3518         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3519         assert_eq!(events_2.len(), 1);
3520         payment_event = SendEvent::from_event(events_2.remove(0));
3521         assert_eq!(payment_event.msgs.len(), 1);
3522
3523         check_added_monitors!(nodes[1], 1);
3524         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
3525         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
3526         check_added_monitors!(nodes[2], 1);
3527         let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3528
3529         // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
3530         // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
3531         // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
3532
3533         nodes[2].node.force_close_channel(&payment_event.commitment_msg.channel_id).unwrap();
3534         check_closed_broadcast!(nodes[2], true);
3535         check_added_monitors!(nodes[2], 1);
3536         let tx = {
3537                 let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3538                 // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
3539                 // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
3540                 // back to nodes[1] upon timeout otherwise.
3541                 assert_eq!(node_txn.len(), 1);
3542                 node_txn.remove(0)
3543         };
3544
3545         mine_transaction(&nodes[1], &tx);
3546
3547         // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
3548         check_closed_broadcast!(nodes[1], true);
3549         check_added_monitors!(nodes[1], 1);
3550
3551         // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
3552         {
3553                 let mut monitors = nodes[2].chain_monitor.chain_monitor.monitors.read().unwrap();
3554                 monitors.get(&OutPoint{ txid: Txid::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), index: 0 }).unwrap()
3555                         .provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &node_cfgs[2].fee_estimator, &&logger);
3556         }
3557         mine_transaction(&nodes[2], &tx);
3558         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3559         assert_eq!(node_txn.len(), 1);
3560         assert_eq!(node_txn[0].input.len(), 1);
3561         assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
3562         assert_eq!(node_txn[0].lock_time, 0); // Must be an HTLC-Success
3563         assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
3564
3565         check_spends!(node_txn[0], tx);
3566 }
3567
3568 #[test]
3569 fn test_dup_events_on_peer_disconnect() {
3570         // Test that if we receive a duplicative update_fulfill_htlc message after a reconnect we do
3571         // not generate a corresponding duplicative PaymentSent event. This did not use to be the case
3572         // as we used to generate the event immediately upon receipt of the payment preimage in the
3573         // update_fulfill_htlc message.
3574
3575         let chanmon_cfgs = create_chanmon_cfgs(2);
3576         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3577         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3578         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3579         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3580
3581         let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1000000).0;
3582
3583         assert!(nodes[1].node.claim_funds(payment_preimage));
3584         check_added_monitors!(nodes[1], 1);
3585         let claim_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3586         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &claim_msgs.update_fulfill_htlcs[0]);
3587         expect_payment_sent!(nodes[0], payment_preimage);
3588
3589         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3590         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3591
3592         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (0, 0), (false, false));
3593         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
3594 }
3595
3596 #[test]
3597 fn test_simple_peer_disconnect() {
3598         // Test that we can reconnect when there are no lost messages
3599         let chanmon_cfgs = create_chanmon_cfgs(3);
3600         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3601         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3602         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3603         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3604         create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
3605
3606         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3607         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3608         reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3609
3610         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3611         let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3612         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
3613         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
3614
3615         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3616         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3617         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3618
3619         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3620         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3621         let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3622         let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3623
3624         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3625         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3626
3627         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_preimage_3);
3628         fail_payment_along_route(&nodes[0], &[&nodes[1], &nodes[2]], true, payment_hash_5);
3629
3630         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (1, 0), (1, 0), (false, false));
3631         {
3632                 let events = nodes[0].node.get_and_clear_pending_events();
3633                 assert_eq!(events.len(), 2);
3634                 match events[0] {
3635                         Event::PaymentSent { payment_preimage } => {
3636                                 assert_eq!(payment_preimage, payment_preimage_3);
3637                         },
3638                         _ => panic!("Unexpected event"),
3639                 }
3640                 match events[1] {
3641                         Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
3642                                 assert_eq!(payment_hash, payment_hash_5);
3643                                 assert!(rejected_by_dest);
3644                         },
3645                         _ => panic!("Unexpected event"),
3646                 }
3647         }
3648
3649         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
3650         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
3651 }
3652
3653 fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken_lnd: bool) {
3654         // Test that we can reconnect when in-flight HTLC updates get dropped
3655         let chanmon_cfgs = create_chanmon_cfgs(2);
3656         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3657         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3658         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3659
3660         let mut as_funding_locked = None;
3661         if messages_delivered == 0 {
3662                 let (funding_locked, _, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
3663                 as_funding_locked = Some(funding_locked);
3664                 // nodes[1] doesn't receive the funding_locked message (it'll be re-sent on reconnect)
3665                 // Note that we store it so that if we're running with `simulate_broken_lnd` we can deliver
3666                 // it before the channel_reestablish message.
3667         } else {
3668                 create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
3669         }
3670
3671         let (payment_preimage_1, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[1]);
3672
3673         let logger = test_utils::TestLogger::new();
3674         let payment_event = {
3675                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3676                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(),
3677                         &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
3678                         &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
3679                 nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1)).unwrap();
3680                 check_added_monitors!(nodes[0], 1);
3681
3682                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3683                 assert_eq!(events.len(), 1);
3684                 SendEvent::from_event(events.remove(0))
3685         };
3686         assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
3687
3688         if messages_delivered < 2 {
3689                 // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
3690         } else {
3691                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3692                 if messages_delivered >= 3 {
3693                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
3694                         check_added_monitors!(nodes[1], 1);
3695                         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3696
3697                         if messages_delivered >= 4 {
3698                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3699                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3700                                 check_added_monitors!(nodes[0], 1);
3701
3702                                 if messages_delivered >= 5 {
3703                                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
3704                                         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3705                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
3706                                         check_added_monitors!(nodes[0], 1);
3707
3708                                         if messages_delivered >= 6 {
3709                                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3710                                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3711                                                 check_added_monitors!(nodes[1], 1);
3712                                         }
3713                                 }
3714                         }
3715                 }
3716         }
3717
3718         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3719         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3720         if messages_delivered < 3 {
3721                 if simulate_broken_lnd {
3722                         // lnd has a long-standing bug where they send a funding_locked prior to a
3723                         // channel_reestablish if you reconnect prior to funding_locked time.
3724                         //
3725                         // Here we simulate that behavior, delivering a funding_locked immediately on
3726                         // reconnect. Note that we don't bother skipping the now-duplicate funding_locked sent
3727                         // in `reconnect_nodes` but we currently don't fail based on that.
3728                         //
3729                         // See-also <https://github.com/lightningnetwork/lnd/issues/4006>
3730                         nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked.as_ref().unwrap().0);
3731                 }
3732                 // Even if the funding_locked messages get exchanged, as long as nothing further was
3733                 // received on either side, both sides will need to resend them.
3734                 reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 1), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3735         } else if messages_delivered == 3 {
3736                 // nodes[0] still wants its RAA + commitment_signed
3737                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (0, 0), (true, false));
3738         } else if messages_delivered == 4 {
3739                 // nodes[0] still wants its commitment_signed
3740                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3741         } else if messages_delivered == 5 {
3742                 // nodes[1] still wants its final RAA
3743                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3744         } else if messages_delivered == 6 {
3745                 // Everything was delivered...
3746                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3747         }
3748
3749         let events_1 = nodes[1].node.get_and_clear_pending_events();
3750         assert_eq!(events_1.len(), 1);
3751         match events_1[0] {
3752                 Event::PendingHTLCsForwardable { .. } => { },
3753                 _ => panic!("Unexpected event"),
3754         };
3755
3756         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3757         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3758         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3759
3760         nodes[1].node.process_pending_htlc_forwards();
3761
3762         let events_2 = nodes[1].node.get_and_clear_pending_events();
3763         assert_eq!(events_2.len(), 1);
3764         match events_2[0] {
3765                 Event::PaymentReceived { ref payment_hash, ref purpose, amt } => {
3766                         assert_eq!(payment_hash_1, *payment_hash);
3767                         assert_eq!(amt, 1000000);
3768                         match &purpose {
3769                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
3770                                         assert!(payment_preimage.is_none());
3771                                         assert_eq!(payment_secret_1, *payment_secret);
3772                                 },
3773                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
3774                         }
3775                 },
3776                 _ => panic!("Unexpected event"),
3777         }
3778
3779         nodes[1].node.claim_funds(payment_preimage_1);
3780         check_added_monitors!(nodes[1], 1);
3781
3782         let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
3783         assert_eq!(events_3.len(), 1);
3784         let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
3785                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
3786                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3787                         assert!(updates.update_add_htlcs.is_empty());
3788                         assert!(updates.update_fail_htlcs.is_empty());
3789                         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3790                         assert!(updates.update_fail_malformed_htlcs.is_empty());
3791                         assert!(updates.update_fee.is_none());
3792                         (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
3793                 },
3794                 _ => panic!("Unexpected event"),
3795         };
3796
3797         if messages_delivered >= 1 {
3798                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc);
3799
3800                 let events_4 = nodes[0].node.get_and_clear_pending_events();
3801                 assert_eq!(events_4.len(), 1);
3802                 match events_4[0] {
3803                         Event::PaymentSent { ref payment_preimage } => {
3804                                 assert_eq!(payment_preimage_1, *payment_preimage);
3805                         },
3806                         _ => panic!("Unexpected event"),
3807                 }
3808
3809                 if messages_delivered >= 2 {
3810                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
3811                         check_added_monitors!(nodes[0], 1);
3812                         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3813
3814                         if messages_delivered >= 3 {
3815                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3816                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3817                                 check_added_monitors!(nodes[1], 1);
3818
3819                                 if messages_delivered >= 4 {
3820                                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed);
3821                                         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3822                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
3823                                         check_added_monitors!(nodes[1], 1);
3824
3825                                         if messages_delivered >= 5 {
3826                                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3827                                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3828                                                 check_added_monitors!(nodes[0], 1);
3829                                         }
3830                                 }
3831                         }
3832                 }
3833         }
3834
3835         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3836         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3837         if messages_delivered < 2 {
3838                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (0, 0), (false, false));
3839                 if messages_delivered < 1 {
3840                         let events_4 = nodes[0].node.get_and_clear_pending_events();
3841                         assert_eq!(events_4.len(), 1);
3842                         match events_4[0] {
3843                                 Event::PaymentSent { ref payment_preimage } => {
3844                                         assert_eq!(payment_preimage_1, *payment_preimage);
3845                                 },
3846                                 _ => panic!("Unexpected event"),
3847                         }
3848                 } else {
3849                         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3850                 }
3851         } else if messages_delivered == 2 {
3852                 // nodes[0] still wants its RAA + commitment_signed
3853                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
3854         } else if messages_delivered == 3 {
3855                 // nodes[0] still wants its commitment_signed
3856                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3857         } else if messages_delivered == 4 {
3858                 // nodes[1] still wants its final RAA
3859                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (true, false));
3860         } else if messages_delivered == 5 {
3861                 // Everything was delivered...
3862                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3863         }
3864
3865         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3866         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3867         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3868
3869         // Channel should still work fine...
3870         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3871         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(),
3872                 &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
3873                 &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
3874         let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
3875         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
3876 }
3877
3878 #[test]
3879 fn test_drop_messages_peer_disconnect_a() {
3880         do_test_drop_messages_peer_disconnect(0, true);
3881         do_test_drop_messages_peer_disconnect(0, false);
3882         do_test_drop_messages_peer_disconnect(1, false);
3883         do_test_drop_messages_peer_disconnect(2, false);
3884 }
3885
3886 #[test]
3887 fn test_drop_messages_peer_disconnect_b() {
3888         do_test_drop_messages_peer_disconnect(3, false);
3889         do_test_drop_messages_peer_disconnect(4, false);
3890         do_test_drop_messages_peer_disconnect(5, false);
3891         do_test_drop_messages_peer_disconnect(6, false);
3892 }
3893
3894 #[test]
3895 fn test_funding_peer_disconnect() {
3896         // Test that we can lock in our funding tx while disconnected
3897         let chanmon_cfgs = create_chanmon_cfgs(2);
3898         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3899         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3900         let persister: test_utils::TestPersister;
3901         let new_chain_monitor: test_utils::TestChainMonitor;
3902         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
3903         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3904         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
3905
3906         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3907         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3908
3909         confirm_transaction(&nodes[0], &tx);
3910         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
3911         assert_eq!(events_1.len(), 1);
3912         match events_1[0] {
3913                 MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
3914                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
3915                 },
3916                 _ => panic!("Unexpected event"),
3917         }
3918
3919         reconnect_nodes(&nodes[0], &nodes[1], (false, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3920
3921         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3922         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3923
3924         confirm_transaction(&nodes[1], &tx);
3925         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3926         assert_eq!(events_2.len(), 2);
3927         let funding_locked = match events_2[0] {
3928                 MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
3929                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3930                         msg.clone()
3931                 },
3932                 _ => panic!("Unexpected event"),
3933         };
3934         let bs_announcement_sigs = match events_2[1] {
3935                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
3936                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3937                         msg.clone()
3938                 },
3939                 _ => panic!("Unexpected event"),
3940         };
3941
3942         reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3943
3944         nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &funding_locked);
3945         nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
3946         let events_3 = nodes[0].node.get_and_clear_pending_msg_events();
3947         assert_eq!(events_3.len(), 2);
3948         let as_announcement_sigs = match events_3[0] {
3949                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
3950                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
3951                         msg.clone()
3952                 },
3953                 _ => panic!("Unexpected event"),
3954         };
3955         let (as_announcement, as_update) = match events_3[1] {
3956                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
3957                         (msg.clone(), update_msg.clone())
3958                 },
3959                 _ => panic!("Unexpected event"),
3960         };
3961
3962         nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs);
3963         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
3964         assert_eq!(events_4.len(), 1);
3965         let (_, bs_update) = match events_4[0] {
3966                 MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
3967                         (msg.clone(), update_msg.clone())
3968                 },
3969                 _ => panic!("Unexpected event"),
3970         };
3971
3972         nodes[0].net_graph_msg_handler.handle_channel_announcement(&as_announcement).unwrap();
3973         nodes[0].net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
3974         nodes[0].net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
3975
3976         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
3977         let logger = test_utils::TestLogger::new();
3978         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
3979         let (payment_preimage, _, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000);
3980         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
3981
3982         // Check that after deserialization and reconnection we can still generate an identical
3983         // channel_announcement from the cached signatures.
3984         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3985
3986         let nodes_0_serialized = nodes[0].node.encode();
3987         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
3988         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
3989
3990         persister = test_utils::TestPersister::new();
3991         let keys_manager = &chanmon_cfgs[0].keys_manager;
3992         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
3993         nodes[0].chain_monitor = &new_chain_monitor;
3994         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
3995         let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
3996                 &mut chan_0_monitor_read, keys_manager).unwrap();
3997         assert!(chan_0_monitor_read.is_empty());
3998
3999         let mut nodes_0_read = &nodes_0_serialized[..];
4000         let (_, nodes_0_deserialized_tmp) = {
4001                 let mut channel_monitors = HashMap::new();
4002                 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4003                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4004                         default_config: UserConfig::default(),
4005                         keys_manager,
4006                         fee_estimator: node_cfgs[0].fee_estimator,
4007                         chain_monitor: nodes[0].chain_monitor,
4008                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4009                         logger: nodes[0].logger,
4010                         channel_monitors,
4011                 }).unwrap()
4012         };
4013         nodes_0_deserialized = nodes_0_deserialized_tmp;
4014         assert!(nodes_0_read.is_empty());
4015
4016         assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4017         nodes[0].node = &nodes_0_deserialized;
4018         check_added_monitors!(nodes[0], 1);
4019
4020         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4021
4022         // as_announcement should be re-generated exactly by broadcast_node_announcement.
4023         nodes[0].node.broadcast_node_announcement([0, 0, 0], [0; 32], Vec::new());
4024         let msgs = nodes[0].node.get_and_clear_pending_msg_events();
4025         let mut found_announcement = false;
4026         for event in msgs.iter() {
4027                 match event {
4028                         MessageSendEvent::BroadcastChannelAnnouncement { ref msg, .. } => {
4029                                 if *msg == as_announcement { found_announcement = true; }
4030                         },
4031                         MessageSendEvent::BroadcastNodeAnnouncement { .. } => {},
4032                         _ => panic!("Unexpected event"),
4033                 }
4034         }
4035         assert!(found_announcement);
4036 }
4037
4038 #[test]
4039 fn test_drop_messages_peer_disconnect_dual_htlc() {
4040         // Test that we can handle reconnecting when both sides of a channel have pending
4041         // commitment_updates when we disconnect.
4042         let chanmon_cfgs = create_chanmon_cfgs(2);
4043         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4044         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4045         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4046         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4047         let logger = test_utils::TestLogger::new();
4048
4049         let (payment_preimage_1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
4050
4051         // Now try to send a second payment which will fail to send
4052         let (payment_preimage_2, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
4053         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
4054         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
4055         nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2)).unwrap();
4056         check_added_monitors!(nodes[0], 1);
4057
4058         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
4059         assert_eq!(events_1.len(), 1);
4060         match events_1[0] {
4061                 MessageSendEvent::UpdateHTLCs { .. } => {},
4062                 _ => panic!("Unexpected event"),
4063         }
4064
4065         assert!(nodes[1].node.claim_funds(payment_preimage_1));
4066         check_added_monitors!(nodes[1], 1);
4067
4068         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
4069         assert_eq!(events_2.len(), 1);
4070         match events_2[0] {
4071                 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 } } => {
4072                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4073                         assert!(update_add_htlcs.is_empty());
4074                         assert_eq!(update_fulfill_htlcs.len(), 1);
4075                         assert!(update_fail_htlcs.is_empty());
4076                         assert!(update_fail_malformed_htlcs.is_empty());
4077                         assert!(update_fee.is_none());
4078
4079                         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
4080                         let events_3 = nodes[0].node.get_and_clear_pending_events();
4081                         assert_eq!(events_3.len(), 1);
4082                         match events_3[0] {
4083                                 Event::PaymentSent { ref payment_preimage } => {
4084                                         assert_eq!(*payment_preimage, payment_preimage_1);
4085                                 },
4086                                 _ => panic!("Unexpected event"),
4087                         }
4088
4089                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
4090                         let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4091                         // No commitment_signed so get_event_msg's assert(len == 1) passes
4092                         check_added_monitors!(nodes[0], 1);
4093                 },
4094                 _ => panic!("Unexpected event"),
4095         }
4096
4097         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
4098         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4099
4100         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4101         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4102         assert_eq!(reestablish_1.len(), 1);
4103         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4104         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4105         assert_eq!(reestablish_2.len(), 1);
4106
4107         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4108         let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
4109         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4110         let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
4111
4112         assert!(as_resp.0.is_none());
4113         assert!(bs_resp.0.is_none());
4114
4115         assert!(bs_resp.1.is_none());
4116         assert!(bs_resp.2.is_none());
4117
4118         assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
4119
4120         assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
4121         assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
4122         assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
4123         assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
4124         assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
4125         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]);
4126         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed);
4127         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4128         // No commitment_signed so get_event_msg's assert(len == 1) passes
4129         check_added_monitors!(nodes[1], 1);
4130
4131         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap());
4132         let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4133         assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
4134         assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
4135         assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
4136         assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
4137         assert!(bs_second_commitment_signed.update_fee.is_none());
4138         check_added_monitors!(nodes[1], 1);
4139
4140         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
4141         let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4142         assert!(as_commitment_signed.update_add_htlcs.is_empty());
4143         assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
4144         assert!(as_commitment_signed.update_fail_htlcs.is_empty());
4145         assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
4146         assert!(as_commitment_signed.update_fee.is_none());
4147         check_added_monitors!(nodes[0], 1);
4148
4149         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed);
4150         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4151         // No commitment_signed so get_event_msg's assert(len == 1) passes
4152         check_added_monitors!(nodes[0], 1);
4153
4154         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed);
4155         let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4156         // No commitment_signed so get_event_msg's assert(len == 1) passes
4157         check_added_monitors!(nodes[1], 1);
4158
4159         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
4160         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4161         check_added_monitors!(nodes[1], 1);
4162
4163         expect_pending_htlcs_forwardable!(nodes[1]);
4164
4165         let events_5 = nodes[1].node.get_and_clear_pending_events();
4166         assert_eq!(events_5.len(), 1);
4167         match events_5[0] {
4168                 Event::PaymentReceived { ref payment_hash, ref purpose, .. } => {
4169                         assert_eq!(payment_hash_2, *payment_hash);
4170                         match &purpose {
4171                                 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
4172                                         assert!(payment_preimage.is_none());
4173                                         assert_eq!(payment_secret_2, *payment_secret);
4174                                 },
4175                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
4176                         }
4177                 },
4178                 _ => panic!("Unexpected event"),
4179         }
4180
4181         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack);
4182         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4183         check_added_monitors!(nodes[0], 1);
4184
4185         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4186 }
4187
4188 fn do_test_htlc_timeout(send_partial_mpp: bool) {
4189         // If the user fails to claim/fail an HTLC within the HTLC CLTV timeout we fail it for them
4190         // to avoid our counterparty failing the channel.
4191         let chanmon_cfgs = create_chanmon_cfgs(2);
4192         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4193         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4194         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4195
4196         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4197         let logger = test_utils::TestLogger::new();
4198
4199         let our_payment_hash = if send_partial_mpp {
4200                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
4201                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
4202                 let (_, our_payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[1]);
4203                 // Use the utility function send_payment_along_path to send the payment with MPP data which
4204                 // indicates there are more HTLCs coming.
4205                 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.
4206                 nodes[0].node.send_payment_along_path(&route.paths[0], &our_payment_hash, &Some(payment_secret), 200000, cur_height, &None).unwrap();
4207                 check_added_monitors!(nodes[0], 1);
4208                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4209                 assert_eq!(events.len(), 1);
4210                 // Now do the relevant commitment_signed/RAA dances along the path, noting that the final
4211                 // hop should *not* yet generate any PaymentReceived event(s).
4212                 pass_along_path(&nodes[0], &[&nodes[1]], 100000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
4213                 our_payment_hash
4214         } else {
4215                 route_payment(&nodes[0], &[&nodes[1]], 100000).1
4216         };
4217
4218         let mut block = Block {
4219                 header: BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
4220                 txdata: vec![],
4221         };
4222         connect_block(&nodes[0], &block);
4223         connect_block(&nodes[1], &block);
4224         let block_count = TEST_FINAL_CLTV + CHAN_CONFIRM_DEPTH + 2 - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS;
4225         for _ in CHAN_CONFIRM_DEPTH + 2..block_count {
4226                 block.header.prev_blockhash = block.block_hash();
4227                 connect_block(&nodes[0], &block);
4228                 connect_block(&nodes[1], &block);
4229         }
4230
4231         expect_pending_htlcs_forwardable!(nodes[1]);
4232
4233         check_added_monitors!(nodes[1], 1);
4234         let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4235         assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
4236         assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
4237         assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
4238         assert!(htlc_timeout_updates.update_fee.is_none());
4239
4240         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
4241         commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
4242         // 100_000 msat as u64, followed by the height at which we failed back above
4243         let mut expected_failure_data = byte_utils::be64_to_array(100_000).to_vec();
4244         expected_failure_data.extend_from_slice(&byte_utils::be32_to_array(block_count - 1));
4245         expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000 | 15, &expected_failure_data[..]);
4246 }
4247
4248 #[test]
4249 fn test_htlc_timeout() {
4250         do_test_htlc_timeout(true);
4251         do_test_htlc_timeout(false);
4252 }
4253
4254 fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
4255         // Tests that HTLCs in the holding cell are timed out after the requisite number of blocks.
4256         let chanmon_cfgs = create_chanmon_cfgs(3);
4257         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4258         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4259         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4260         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4261         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
4262
4263         // Make sure all nodes are at the same starting height
4264         connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
4265         connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
4266         connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
4267
4268         let logger = test_utils::TestLogger::new();
4269
4270         // Route a first payment to get the 1 -> 2 channel in awaiting_raa...
4271         let (_, first_payment_hash, first_payment_secret) = get_payment_preimage_hash!(nodes[2]);
4272         {
4273                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
4274                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
4275                 nodes[1].node.send_payment(&route, first_payment_hash, &Some(first_payment_secret)).unwrap();
4276         }
4277         assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
4278         check_added_monitors!(nodes[1], 1);
4279
4280         // Now attempt to route a second payment, which should be placed in the holding cell
4281         let (_, second_payment_hash, second_payment_secret) = get_payment_preimage_hash!(nodes[2]);
4282         if forwarded_htlc {
4283                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
4284                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
4285                 nodes[0].node.send_payment(&route, second_payment_hash, &Some(first_payment_secret)).unwrap();
4286                 check_added_monitors!(nodes[0], 1);
4287                 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
4288                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
4289                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4290                 expect_pending_htlcs_forwardable!(nodes[1]);
4291                 check_added_monitors!(nodes[1], 0);
4292         } else {
4293                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
4294                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
4295                 nodes[1].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)).unwrap();
4296                 check_added_monitors!(nodes[1], 0);
4297         }
4298
4299         connect_blocks(&nodes[1], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS);
4300         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4301         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
4302         connect_blocks(&nodes[1], 1);
4303
4304         if forwarded_htlc {
4305                 expect_pending_htlcs_forwardable!(nodes[1]);
4306                 check_added_monitors!(nodes[1], 1);
4307                 let fail_commit = nodes[1].node.get_and_clear_pending_msg_events();
4308                 assert_eq!(fail_commit.len(), 1);
4309                 match fail_commit[0] {
4310                         MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, ref commitment_signed, .. }, .. } => {
4311                                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
4312                                 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, true, true);
4313                         },
4314                         _ => unreachable!(),
4315                 }
4316                 expect_payment_failed!(nodes[0], second_payment_hash, false);
4317                 expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, false);
4318         } else {
4319                 expect_payment_failed!(nodes[1], second_payment_hash, true);
4320         }
4321 }
4322
4323 #[test]
4324 fn test_holding_cell_htlc_add_timeouts() {
4325         do_test_holding_cell_htlc_add_timeouts(false);
4326         do_test_holding_cell_htlc_add_timeouts(true);
4327 }
4328
4329 #[test]
4330 fn test_invalid_channel_announcement() {
4331         //Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
4332         let secp_ctx = Secp256k1::new();
4333         let chanmon_cfgs = create_chanmon_cfgs(2);
4334         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4335         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4336         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4337
4338         let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known());
4339
4340         let a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
4341         let b_channel_lock = nodes[1].node.channel_state.lock().unwrap();
4342         let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap();
4343         let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap();
4344
4345         nodes[0].net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
4346
4347         let as_bitcoin_key = as_chan.get_signer().inner.holder_channel_pubkeys.funding_pubkey;
4348         let bs_bitcoin_key = bs_chan.get_signer().inner.holder_channel_pubkeys.funding_pubkey;
4349
4350         let as_network_key = nodes[0].node.get_our_node_id();
4351         let bs_network_key = nodes[1].node.get_our_node_id();
4352
4353         let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..];
4354
4355         let mut chan_announcement;
4356
4357         macro_rules! dummy_unsigned_msg {
4358                 () => {
4359                         msgs::UnsignedChannelAnnouncement {
4360                                 features: ChannelFeatures::known(),
4361                                 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
4362                                 short_channel_id: as_chan.get_short_channel_id().unwrap(),
4363                                 node_id_1: if were_node_one { as_network_key } else { bs_network_key },
4364                                 node_id_2: if were_node_one { bs_network_key } else { as_network_key },
4365                                 bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key },
4366                                 bitcoin_key_2: if were_node_one { bs_bitcoin_key } else { as_bitcoin_key },
4367                                 excess_data: Vec::new(),
4368                         };
4369                 }
4370         }
4371
4372         macro_rules! sign_msg {
4373                 ($unsigned_msg: expr) => {
4374                         let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap();
4375                         let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_signer().inner.funding_key);
4376                         let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_signer().inner.funding_key);
4377                         let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret());
4378                         let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret());
4379                         chan_announcement = msgs::ChannelAnnouncement {
4380                                 node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig},
4381                                 node_signature_2 : if were_node_one { bs_node_sig } else { as_node_sig},
4382                                 bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig },
4383                                 bitcoin_signature_2 : if were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig },
4384                                 contents: $unsigned_msg
4385                         }
4386                 }
4387         }
4388
4389         let unsigned_msg = dummy_unsigned_msg!();
4390         sign_msg!(unsigned_msg);
4391         assert_eq!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).unwrap(), true);
4392         let _ = nodes[0].net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
4393
4394         // Configured with Network::Testnet
4395         let mut unsigned_msg = dummy_unsigned_msg!();
4396         unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.block_hash();
4397         sign_msg!(unsigned_msg);
4398         assert!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).is_err());
4399
4400         let mut unsigned_msg = dummy_unsigned_msg!();
4401         unsigned_msg.chain_hash = BlockHash::hash(&[1,2,3,4,5,6,7,8,9]);
4402         sign_msg!(unsigned_msg);
4403         assert!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).is_err());
4404 }
4405
4406 #[test]
4407 fn test_no_txn_manager_serialize_deserialize() {
4408         let chanmon_cfgs = create_chanmon_cfgs(2);
4409         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4410         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4411         let logger: test_utils::TestLogger;
4412         let fee_estimator: test_utils::TestFeeEstimator;
4413         let persister: test_utils::TestPersister;
4414         let new_chain_monitor: test_utils::TestChainMonitor;
4415         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4416         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4417
4418         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known());
4419
4420         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4421
4422         let nodes_0_serialized = nodes[0].node.encode();
4423         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4424         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
4425
4426         logger = test_utils::TestLogger::new();
4427         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
4428         persister = test_utils::TestPersister::new();
4429         let keys_manager = &chanmon_cfgs[0].keys_manager;
4430         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister, keys_manager);
4431         nodes[0].chain_monitor = &new_chain_monitor;
4432         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
4433         let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
4434                 &mut chan_0_monitor_read, keys_manager).unwrap();
4435         assert!(chan_0_monitor_read.is_empty());
4436
4437         let mut nodes_0_read = &nodes_0_serialized[..];
4438         let config = UserConfig::default();
4439         let (_, nodes_0_deserialized_tmp) = {
4440                 let mut channel_monitors = HashMap::new();
4441                 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4442                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4443                         default_config: config,
4444                         keys_manager,
4445                         fee_estimator: &fee_estimator,
4446                         chain_monitor: nodes[0].chain_monitor,
4447                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4448                         logger: &logger,
4449                         channel_monitors,
4450                 }).unwrap()
4451         };
4452         nodes_0_deserialized = nodes_0_deserialized_tmp;
4453         assert!(nodes_0_read.is_empty());
4454
4455         assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4456         nodes[0].node = &nodes_0_deserialized;
4457         assert_eq!(nodes[0].node.list_channels().len(), 1);
4458         check_added_monitors!(nodes[0], 1);
4459
4460         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4461         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4462         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4463         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4464
4465         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4466         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4467         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4468         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4469
4470         let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
4471         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
4472         for node in nodes.iter() {
4473                 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
4474                 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
4475                 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
4476         }
4477
4478         send_payment(&nodes[0], &[&nodes[1]], 1000000);
4479 }
4480
4481 #[test]
4482 fn test_dup_htlc_onchain_fails_on_reload() {
4483         // When a Channel is closed, any outbound HTLCs which were relayed through it are simply
4484         // dropped when the Channel is. From there, the ChannelManager relies on the ChannelMonitor
4485         // having a copy of the relevant fail-/claim-back data and processes the HTLC fail/claim when
4486         // the ChannelMonitor tells it to.
4487         //
4488         // If, due to an on-chain event, an HTLC is failed/claimed, and then we serialize the
4489         // ChannelManager, we generally expect there not to be a duplicate HTLC fail/claim (eg via a
4490         // PaymentFailed event appearing). However, because we may not serialize the relevant
4491         // ChannelMonitor at the same time, this isn't strictly guaranteed. In order to provide this
4492         // consistency, the ChannelManager explicitly tracks pending-onchain-resolution outbound HTLCs
4493         // and de-duplicates ChannelMonitor events.
4494         //
4495         // This tests that explicit tracking behavior.
4496         let chanmon_cfgs = create_chanmon_cfgs(2);
4497         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4498         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4499         let persister: test_utils::TestPersister;
4500         let new_chain_monitor: test_utils::TestChainMonitor;
4501         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4502         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4503
4504         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4505
4506         // Route a payment, but force-close the channel before the HTLC fulfill message arrives at
4507         // nodes[0].
4508         let (payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 10000000);
4509         nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
4510         check_closed_broadcast!(nodes[0], true);
4511         check_added_monitors!(nodes[0], 1);
4512
4513         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
4514         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4515
4516         // Connect blocks until the CLTV timeout is up so that we get an HTLC-Timeout transaction
4517         connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
4518         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4519         assert_eq!(node_txn.len(), 3);
4520         assert_eq!(node_txn[0], node_txn[1]);
4521
4522         assert!(nodes[1].node.claim_funds(payment_preimage));
4523         check_added_monitors!(nodes[1], 1);
4524
4525         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4526         connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[1].clone(), node_txn[2].clone()]});
4527         check_closed_broadcast!(nodes[1], true);
4528         check_added_monitors!(nodes[1], 1);
4529         let claim_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4530
4531         header.prev_blockhash = nodes[0].best_block_hash();
4532         connect_block(&nodes[0], &Block { header, txdata: vec![node_txn[1].clone(), node_txn[2].clone()]});
4533
4534         // Serialize out the ChannelMonitor before connecting the on-chain claim transactions. This is
4535         // fairly normal behavior as ChannelMonitor(s) are often not re-serialized when on-chain events
4536         // happen, unlike ChannelManager which tends to be re-serialized after any relevant event(s).
4537         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4538         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
4539
4540         header.prev_blockhash = nodes[0].best_block_hash();
4541         let claim_block = Block { header, txdata: claim_txn};
4542         connect_block(&nodes[0], &claim_block);
4543         expect_payment_sent!(nodes[0], payment_preimage);
4544
4545         // ChannelManagers generally get re-serialized after any relevant event(s). Since we just
4546         // connected a highly-relevant block, it likely gets serialized out now.
4547         let mut chan_manager_serialized = test_utils::TestVecWriter(Vec::new());
4548         nodes[0].node.write(&mut chan_manager_serialized).unwrap();
4549
4550         // Now reload nodes[0]...
4551         persister = test_utils::TestPersister::new();
4552         let keys_manager = &chanmon_cfgs[0].keys_manager;
4553         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
4554         nodes[0].chain_monitor = &new_chain_monitor;
4555         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
4556         let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
4557                 &mut chan_0_monitor_read, keys_manager).unwrap();
4558         assert!(chan_0_monitor_read.is_empty());
4559
4560         let (_, nodes_0_deserialized_tmp) = {
4561                 let mut channel_monitors = HashMap::new();
4562                 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4563                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>
4564                         ::read(&mut io::Cursor::new(&chan_manager_serialized.0[..]), ChannelManagerReadArgs {
4565                                 default_config: Default::default(),
4566                                 keys_manager,
4567                                 fee_estimator: node_cfgs[0].fee_estimator,
4568                                 chain_monitor: nodes[0].chain_monitor,
4569                                 tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4570                                 logger: nodes[0].logger,
4571                                 channel_monitors,
4572                         }).unwrap()
4573         };
4574         nodes_0_deserialized = nodes_0_deserialized_tmp;
4575
4576         assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4577         check_added_monitors!(nodes[0], 1);
4578         nodes[0].node = &nodes_0_deserialized;
4579
4580         // Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
4581         // which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
4582         // payment events should kick in, leaving us with no pending events here.
4583         let height = nodes[0].blocks.lock().unwrap().len() as u32 - 1;
4584         nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, height);
4585         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
4586 }
4587
4588 #[test]
4589 fn test_manager_serialize_deserialize_events() {
4590         // This test makes sure the events field in ChannelManager survives de/serialization
4591         let chanmon_cfgs = create_chanmon_cfgs(2);
4592         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4593         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4594         let fee_estimator: test_utils::TestFeeEstimator;
4595         let persister: test_utils::TestPersister;
4596         let logger: test_utils::TestLogger;
4597         let new_chain_monitor: test_utils::TestChainMonitor;
4598         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4599         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4600
4601         // Start creating a channel, but stop right before broadcasting the funding transaction
4602         let channel_value = 100000;
4603         let push_msat = 10001;
4604         let a_flags = InitFeatures::known();
4605         let b_flags = InitFeatures::known();
4606         let node_a = nodes.remove(0);
4607         let node_b = nodes.remove(0);
4608         node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
4609         node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id()));
4610         node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id()));
4611
4612         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&node_a, channel_value, 42);
4613
4614         node_a.node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
4615         check_added_monitors!(node_a, 0);
4616
4617         node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id()));
4618         {
4619                 let mut added_monitors = node_b.chain_monitor.added_monitors.lock().unwrap();
4620                 assert_eq!(added_monitors.len(), 1);
4621                 assert_eq!(added_monitors[0].0, funding_output);
4622                 added_monitors.clear();
4623         }
4624
4625         node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id()));
4626         {
4627                 let mut added_monitors = node_a.chain_monitor.added_monitors.lock().unwrap();
4628                 assert_eq!(added_monitors.len(), 1);
4629                 assert_eq!(added_monitors[0].0, funding_output);
4630                 added_monitors.clear();
4631         }
4632         // Normally, this is where node_a would broadcast the funding transaction, but the test de/serializes first instead
4633
4634         nodes.push(node_a);
4635         nodes.push(node_b);
4636
4637         // Start the de/seriailization process mid-channel creation to check that the channel manager will hold onto events that are serialized
4638         let nodes_0_serialized = nodes[0].node.encode();
4639         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4640         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
4641
4642         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
4643         logger = test_utils::TestLogger::new();
4644         persister = test_utils::TestPersister::new();
4645         let keys_manager = &chanmon_cfgs[0].keys_manager;
4646         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister, keys_manager);
4647         nodes[0].chain_monitor = &new_chain_monitor;
4648         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
4649         let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
4650                 &mut chan_0_monitor_read, keys_manager).unwrap();
4651         assert!(chan_0_monitor_read.is_empty());
4652
4653         let mut nodes_0_read = &nodes_0_serialized[..];
4654         let config = UserConfig::default();
4655         let (_, nodes_0_deserialized_tmp) = {
4656                 let mut channel_monitors = HashMap::new();
4657                 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4658                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4659                         default_config: config,
4660                         keys_manager,
4661                         fee_estimator: &fee_estimator,
4662                         chain_monitor: nodes[0].chain_monitor,
4663                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4664                         logger: &logger,
4665                         channel_monitors,
4666                 }).unwrap()
4667         };
4668         nodes_0_deserialized = nodes_0_deserialized_tmp;
4669         assert!(nodes_0_read.is_empty());
4670
4671         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4672
4673         assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4674         nodes[0].node = &nodes_0_deserialized;
4675
4676         // After deserializing, make sure the funding_transaction is still held by the channel manager
4677         let events_4 = nodes[0].node.get_and_clear_pending_events();
4678         assert_eq!(events_4.len(), 0);
4679         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
4680         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].txid(), funding_output.txid);
4681
4682         // Make sure the channel is functioning as though the de/serialization never happened
4683         assert_eq!(nodes[0].node.list_channels().len(), 1);
4684         check_added_monitors!(nodes[0], 1);
4685
4686         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4687         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4688         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4689         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4690
4691         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4692         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4693         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4694         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4695
4696         let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
4697         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
4698         for node in nodes.iter() {
4699                 assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap());
4700                 node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap();
4701                 node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap();
4702         }
4703
4704         send_payment(&nodes[0], &[&nodes[1]], 1000000);
4705 }
4706
4707 #[test]
4708 fn test_simple_manager_serialize_deserialize() {
4709         let chanmon_cfgs = create_chanmon_cfgs(2);
4710         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4711         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4712         let logger: test_utils::TestLogger;
4713         let fee_estimator: test_utils::TestFeeEstimator;
4714         let persister: test_utils::TestPersister;
4715         let new_chain_monitor: test_utils::TestChainMonitor;
4716         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4717         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4718         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4719
4720         let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
4721         let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
4722
4723         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4724
4725         let nodes_0_serialized = nodes[0].node.encode();
4726         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
4727         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
4728
4729         logger = test_utils::TestLogger::new();
4730         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
4731         persister = test_utils::TestPersister::new();
4732         let keys_manager = &chanmon_cfgs[0].keys_manager;
4733         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister, keys_manager);
4734         nodes[0].chain_monitor = &new_chain_monitor;
4735         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
4736         let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
4737                 &mut chan_0_monitor_read, keys_manager).unwrap();
4738         assert!(chan_0_monitor_read.is_empty());
4739
4740         let mut nodes_0_read = &nodes_0_serialized[..];
4741         let (_, nodes_0_deserialized_tmp) = {
4742                 let mut channel_monitors = HashMap::new();
4743                 channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
4744                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4745                         default_config: UserConfig::default(),
4746                         keys_manager,
4747                         fee_estimator: &fee_estimator,
4748                         chain_monitor: nodes[0].chain_monitor,
4749                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4750                         logger: &logger,
4751                         channel_monitors,
4752                 }).unwrap()
4753         };
4754         nodes_0_deserialized = nodes_0_deserialized_tmp;
4755         assert!(nodes_0_read.is_empty());
4756
4757         assert!(nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0, chan_0_monitor).is_ok());
4758         nodes[0].node = &nodes_0_deserialized;
4759         check_added_monitors!(nodes[0], 1);
4760
4761         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4762
4763         fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
4764         claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
4765 }
4766
4767 #[test]
4768 fn test_manager_serialize_deserialize_inconsistent_monitor() {
4769         // Test deserializing a ChannelManager with an out-of-date ChannelMonitor
4770         let chanmon_cfgs = create_chanmon_cfgs(4);
4771         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
4772         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
4773         let logger: test_utils::TestLogger;
4774         let fee_estimator: test_utils::TestFeeEstimator;
4775         let persister: test_utils::TestPersister;
4776         let new_chain_monitor: test_utils::TestChainMonitor;
4777         let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
4778         let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
4779         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4780         create_announced_chan_between_nodes(&nodes, 2, 0, InitFeatures::known(), InitFeatures::known());
4781         let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, InitFeatures::known(), InitFeatures::known());
4782
4783         let mut node_0_stale_monitors_serialized = Vec::new();
4784         for monitor in nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter() {
4785                 let mut writer = test_utils::TestVecWriter(Vec::new());
4786                 monitor.1.write(&mut writer).unwrap();
4787                 node_0_stale_monitors_serialized.push(writer.0);
4788         }
4789
4790         let (our_payment_preimage, _, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);
4791
4792         // Serialize the ChannelManager here, but the monitor we keep up-to-date
4793         let nodes_0_serialized = nodes[0].node.encode();
4794
4795         route_payment(&nodes[0], &[&nodes[3]], 1000000);
4796         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4797         nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4798         nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
4799
4800         // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
4801         // nodes[3])
4802         let mut node_0_monitors_serialized = Vec::new();
4803         for monitor in nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter() {
4804                 let mut writer = test_utils::TestVecWriter(Vec::new());
4805                 monitor.1.write(&mut writer).unwrap();
4806                 node_0_monitors_serialized.push(writer.0);
4807         }
4808
4809         logger = test_utils::TestLogger::new();
4810         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
4811         persister = test_utils::TestPersister::new();
4812         let keys_manager = &chanmon_cfgs[0].keys_manager;
4813         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), &logger, &fee_estimator, &persister, keys_manager);
4814         nodes[0].chain_monitor = &new_chain_monitor;
4815
4816
4817         let mut node_0_stale_monitors = Vec::new();
4818         for serialized in node_0_stale_monitors_serialized.iter() {
4819                 let mut read = &serialized[..];
4820                 let (_, monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut read, keys_manager).unwrap();
4821                 assert!(read.is_empty());
4822                 node_0_stale_monitors.push(monitor);
4823         }
4824
4825         let mut node_0_monitors = Vec::new();
4826         for serialized in node_0_monitors_serialized.iter() {
4827                 let mut read = &serialized[..];
4828                 let (_, monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut read, keys_manager).unwrap();
4829                 assert!(read.is_empty());
4830                 node_0_monitors.push(monitor);
4831         }
4832
4833         let mut nodes_0_read = &nodes_0_serialized[..];
4834         if let Err(msgs::DecodeError::InvalidValue) =
4835                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4836                 default_config: UserConfig::default(),
4837                 keys_manager,
4838                 fee_estimator: &fee_estimator,
4839                 chain_monitor: nodes[0].chain_monitor,
4840                 tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4841                 logger: &logger,
4842                 channel_monitors: node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4843         }) { } else {
4844                 panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
4845         };
4846
4847         let mut nodes_0_read = &nodes_0_serialized[..];
4848         let (_, nodes_0_deserialized_tmp) =
4849                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
4850                 default_config: UserConfig::default(),
4851                 keys_manager,
4852                 fee_estimator: &fee_estimator,
4853                 chain_monitor: nodes[0].chain_monitor,
4854                 tx_broadcaster: nodes[0].tx_broadcaster.clone(),
4855                 logger: &logger,
4856                 channel_monitors: node_0_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().0, monitor) }).collect(),
4857         }).unwrap();
4858         nodes_0_deserialized = nodes_0_deserialized_tmp;
4859         assert!(nodes_0_read.is_empty());
4860
4861         { // Channel close should result in a commitment tx
4862                 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
4863                 assert_eq!(txn.len(), 1);
4864                 check_spends!(txn[0], funding_tx);
4865                 assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
4866         }
4867
4868         for monitor in node_0_monitors.drain(..) {
4869                 assert!(nodes[0].chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor).is_ok());
4870                 check_added_monitors!(nodes[0], 1);
4871         }
4872         nodes[0].node = &nodes_0_deserialized;
4873
4874         // nodes[1] and nodes[2] have no lost state with nodes[0]...
4875         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4876         reconnect_nodes(&nodes[0], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4877         //... and we can even still claim the payment!
4878         claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);
4879
4880         nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4881         let reestablish = get_event_msg!(nodes[3], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
4882         nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
4883         nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish);
4884         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
4885         assert_eq!(msg_events.len(), 1);
4886         if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
4887                 match action {
4888                         &ErrorAction::SendErrorMessage { ref msg } => {
4889                                 assert_eq!(msg.channel_id, channel_id);
4890                         },
4891                         _ => panic!("Unexpected event!"),
4892                 }
4893         }
4894 }
4895
4896 macro_rules! check_spendable_outputs {
4897         ($node: expr, $keysinterface: expr) => {
4898                 {
4899                         let mut events = $node.chain_monitor.chain_monitor.get_and_clear_pending_events();
4900                         let mut txn = Vec::new();
4901                         let mut all_outputs = Vec::new();
4902                         let secp_ctx = Secp256k1::new();
4903                         for event in events.drain(..) {
4904                                 match event {
4905                                         Event::SpendableOutputs { mut outputs } => {
4906                                                 for outp in outputs.drain(..) {
4907                                                         txn.push($keysinterface.backing.spend_spendable_outputs(&[&outp], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, &secp_ctx).unwrap());
4908                                                         all_outputs.push(outp);
4909                                                 }
4910                                         },
4911                                         _ => panic!("Unexpected event"),
4912                                 };
4913                         }
4914                         if all_outputs.len() > 1 {
4915                                 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, &secp_ctx) {
4916                                         txn.push(tx);
4917                                 }
4918                         }
4919                         txn
4920                 }
4921         }
4922 }
4923
4924 #[test]
4925 fn test_claim_sizeable_push_msat() {
4926         // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
4927         let chanmon_cfgs = create_chanmon_cfgs(2);
4928         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4929         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4930         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4931
4932         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, InitFeatures::known(), InitFeatures::known());
4933         nodes[1].node.force_close_channel(&chan.2).unwrap();
4934         check_closed_broadcast!(nodes[1], true);
4935         check_added_monitors!(nodes[1], 1);
4936         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
4937         assert_eq!(node_txn.len(), 1);
4938         check_spends!(node_txn[0], chan.3);
4939         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
4940
4941         mine_transaction(&nodes[1], &node_txn[0]);
4942         connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
4943
4944         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4945         assert_eq!(spend_txn.len(), 1);
4946         assert_eq!(spend_txn[0].input.len(), 1);
4947         check_spends!(spend_txn[0], node_txn[0]);
4948         assert_eq!(spend_txn[0].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
4949 }
4950
4951 #[test]
4952 fn test_claim_on_remote_sizeable_push_msat() {
4953         // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4954         // to_remote output is encumbered by a P2WPKH
4955         let chanmon_cfgs = create_chanmon_cfgs(2);
4956         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4957         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4958         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4959
4960         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, InitFeatures::known(), InitFeatures::known());
4961         nodes[0].node.force_close_channel(&chan.2).unwrap();
4962         check_closed_broadcast!(nodes[0], true);
4963         check_added_monitors!(nodes[0], 1);
4964
4965         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
4966         assert_eq!(node_txn.len(), 1);
4967         check_spends!(node_txn[0], chan.3);
4968         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
4969
4970         mine_transaction(&nodes[1], &node_txn[0]);
4971         check_closed_broadcast!(nodes[1], true);
4972         check_added_monitors!(nodes[1], 1);
4973         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4974
4975         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4976         assert_eq!(spend_txn.len(), 1);
4977         check_spends!(spend_txn[0], node_txn[0]);
4978 }
4979
4980 #[test]
4981 fn test_claim_on_remote_revoked_sizeable_push_msat() {
4982         // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4983         // to_remote output is encumbered by a P2WPKH
4984
4985         let chanmon_cfgs = create_chanmon_cfgs(2);
4986         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4987         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4988         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4989
4990         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000, InitFeatures::known(), InitFeatures::known());
4991         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4992         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
4993         assert_eq!(revoked_local_txn[0].input.len(), 1);
4994         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
4995
4996         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4997         mine_transaction(&nodes[1], &revoked_local_txn[0]);
4998         check_closed_broadcast!(nodes[1], true);
4999         check_added_monitors!(nodes[1], 1);
5000
5001         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5002         mine_transaction(&nodes[1], &node_txn[0]);
5003         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5004
5005         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5006         assert_eq!(spend_txn.len(), 3);
5007         check_spends!(spend_txn[0], revoked_local_txn[0]); // to_remote output on revoked remote commitment_tx
5008         check_spends!(spend_txn[1], node_txn[0]);
5009         check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[0]); // Both outputs
5010 }
5011
5012 #[test]
5013 fn test_static_spendable_outputs_preimage_tx() {
5014         let chanmon_cfgs = create_chanmon_cfgs(2);
5015         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5016         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5017         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5018
5019         // Create some initial channels
5020         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5021
5022         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
5023
5024         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
5025         assert_eq!(commitment_tx[0].input.len(), 1);
5026         assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
5027
5028         // Settle A's commitment tx on B's chain
5029         assert!(nodes[1].node.claim_funds(payment_preimage));
5030         check_added_monitors!(nodes[1], 1);
5031         mine_transaction(&nodes[1], &commitment_tx[0]);
5032         check_added_monitors!(nodes[1], 1);
5033         let events = nodes[1].node.get_and_clear_pending_msg_events();
5034         match events[0] {
5035                 MessageSendEvent::UpdateHTLCs { .. } => {},
5036                 _ => panic!("Unexpected event"),
5037         }
5038         match events[1] {
5039                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5040                 _ => panic!("Unexepected event"),
5041         }
5042
5043         // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
5044         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 2 (local commitment tx + HTLC-Success), ChannelMonitor: preimage tx
5045         assert_eq!(node_txn.len(), 3);
5046         check_spends!(node_txn[0], commitment_tx[0]);
5047         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5048         check_spends!(node_txn[1], chan_1.3);
5049         check_spends!(node_txn[2], node_txn[1]);
5050
5051         mine_transaction(&nodes[1], &node_txn[0]);
5052         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5053
5054         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5055         assert_eq!(spend_txn.len(), 1);
5056         check_spends!(spend_txn[0], node_txn[0]);
5057 }
5058
5059 #[test]
5060 fn test_static_spendable_outputs_timeout_tx() {
5061         let chanmon_cfgs = create_chanmon_cfgs(2);
5062         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5063         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5064         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5065
5066         // Create some initial channels
5067         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5068
5069         // Rebalance the network a bit by relaying one payment through all the channels ...
5070         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5071
5072         let (_, our_payment_hash, _) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000);
5073
5074         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
5075         assert_eq!(commitment_tx[0].input.len(), 1);
5076         assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
5077
5078         // Settle A's commitment tx on B' chain
5079         mine_transaction(&nodes[1], &commitment_tx[0]);
5080         check_added_monitors!(nodes[1], 1);
5081         let events = nodes[1].node.get_and_clear_pending_msg_events();
5082         match events[0] {
5083                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5084                 _ => panic!("Unexpected event"),
5085         }
5086         connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
5087
5088         // Check B's monitor was able to send back output descriptor event for timeout tx on A's commitment tx
5089         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
5090         assert_eq!(node_txn.len(), 2); // ChannelManager : 1 local commitent tx, ChannelMonitor: timeout tx
5091         check_spends!(node_txn[0], chan_1.3.clone());
5092         check_spends!(node_txn[1],  commitment_tx[0].clone());
5093         assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5094
5095         mine_transaction(&nodes[1], &node_txn[1]);
5096         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5097         expect_payment_failed!(nodes[1], our_payment_hash, true);
5098
5099         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5100         assert_eq!(spend_txn.len(), 3); // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
5101         check_spends!(spend_txn[0], commitment_tx[0]);
5102         check_spends!(spend_txn[1], node_txn[1]);
5103         check_spends!(spend_txn[2], node_txn[1], commitment_tx[0]); // All outputs
5104 }
5105
5106 #[test]
5107 fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
5108         let chanmon_cfgs = create_chanmon_cfgs(2);
5109         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5110         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5111         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5112
5113         // Create some initial channels
5114         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5115
5116         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
5117         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5118         assert_eq!(revoked_local_txn[0].input.len(), 1);
5119         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
5120
5121         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
5122
5123         mine_transaction(&nodes[1], &revoked_local_txn[0]);
5124         check_closed_broadcast!(nodes[1], true);
5125         check_added_monitors!(nodes[1], 1);
5126
5127         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5128         assert_eq!(node_txn.len(), 2);
5129         assert_eq!(node_txn[0].input.len(), 2);
5130         check_spends!(node_txn[0], revoked_local_txn[0]);
5131
5132         mine_transaction(&nodes[1], &node_txn[0]);
5133         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5134
5135         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5136         assert_eq!(spend_txn.len(), 1);
5137         check_spends!(spend_txn[0], node_txn[0]);
5138 }
5139
5140 #[test]
5141 fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
5142         let mut chanmon_cfgs = create_chanmon_cfgs(2);
5143         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
5144         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5145         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5146         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5147
5148         // Create some initial channels
5149         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5150
5151         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
5152         let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5153         assert_eq!(revoked_local_txn[0].input.len(), 1);
5154         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
5155
5156         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
5157
5158         // A will generate HTLC-Timeout from revoked commitment tx
5159         mine_transaction(&nodes[0], &revoked_local_txn[0]);
5160         check_closed_broadcast!(nodes[0], true);
5161         check_added_monitors!(nodes[0], 1);
5162         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
5163
5164         let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5165         assert_eq!(revoked_htlc_txn.len(), 2);
5166         check_spends!(revoked_htlc_txn[0], chan_1.3);
5167         assert_eq!(revoked_htlc_txn[1].input.len(), 1);
5168         assert_eq!(revoked_htlc_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5169         check_spends!(revoked_htlc_txn[1], revoked_local_txn[0]);
5170         assert_ne!(revoked_htlc_txn[1].lock_time, 0); // HTLC-Timeout
5171
5172         // B will generate justice tx from A's revoked commitment/HTLC tx
5173         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5174         connect_block(&nodes[1], &Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[1].clone()] });
5175         check_closed_broadcast!(nodes[1], true);
5176         check_added_monitors!(nodes[1], 1);
5177
5178         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5179         assert_eq!(node_txn.len(), 3); // ChannelMonitor: bogus justice tx, justice tx on revoked outputs, ChannelManager: local commitment tx
5180         // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
5181         // including the one already spent by revoked_htlc_txn[1]. That's OK, we'll spend with valid
5182         // transactions next...
5183         assert_eq!(node_txn[0].input.len(), 3);
5184         check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[1]);
5185
5186         assert_eq!(node_txn[1].input.len(), 2);
5187         check_spends!(node_txn[1], revoked_local_txn[0], revoked_htlc_txn[1]);
5188         if node_txn[1].input[1].previous_output.txid == revoked_htlc_txn[1].txid() {
5189                 assert_ne!(node_txn[1].input[0].previous_output, revoked_htlc_txn[1].input[0].previous_output);
5190         } else {
5191                 assert_eq!(node_txn[1].input[0].previous_output.txid, revoked_htlc_txn[1].txid());
5192                 assert_ne!(node_txn[1].input[1].previous_output, revoked_htlc_txn[1].input[0].previous_output);
5193         }
5194
5195         assert_eq!(node_txn[2].input.len(), 1);
5196         check_spends!(node_txn[2], chan_1.3);
5197
5198         mine_transaction(&nodes[1], &node_txn[1]);
5199         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5200
5201         // Check B's ChannelMonitor was able to generate the right spendable output descriptor
5202         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5203         assert_eq!(spend_txn.len(), 1);
5204         assert_eq!(spend_txn[0].input.len(), 1);
5205         check_spends!(spend_txn[0], node_txn[1]);
5206 }
5207
5208 #[test]
5209 fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
5210         let mut chanmon_cfgs = create_chanmon_cfgs(2);
5211         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
5212         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5213         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5214         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5215
5216         // Create some initial channels
5217         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5218
5219         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
5220         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
5221         assert_eq!(revoked_local_txn[0].input.len(), 1);
5222         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
5223
5224         // The to-be-revoked commitment tx should have one HTLC and one to_remote output
5225         assert_eq!(revoked_local_txn[0].output.len(), 2);
5226
5227         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
5228
5229         // B will generate HTLC-Success from revoked commitment tx
5230         mine_transaction(&nodes[1], &revoked_local_txn[0]);
5231         check_closed_broadcast!(nodes[1], true);
5232         check_added_monitors!(nodes[1], 1);
5233         let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5234
5235         assert_eq!(revoked_htlc_txn.len(), 2);
5236         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
5237         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5238         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
5239
5240         // Check that the unspent (of two) outputs on revoked_local_txn[0] is a P2WPKH:
5241         let unspent_local_txn_output = revoked_htlc_txn[0].input[0].previous_output.vout as usize ^ 1;
5242         assert_eq!(revoked_local_txn[0].output[unspent_local_txn_output].script_pubkey.len(), 2 + 20); // P2WPKH
5243
5244         // A will generate justice tx from B's revoked commitment/HTLC tx
5245         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5246         connect_block(&nodes[0], &Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] });
5247         check_closed_broadcast!(nodes[0], true);
5248         check_added_monitors!(nodes[0], 1);
5249
5250         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5251         assert_eq!(node_txn.len(), 3); // ChannelMonitor: justice tx on revoked commitment, justice tx on revoked HTLC-success, ChannelManager: local commitment tx
5252
5253         // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
5254         // including the one already spent by revoked_htlc_txn[0]. That's OK, we'll spend with valid
5255         // transactions next...
5256         assert_eq!(node_txn[0].input.len(), 2);
5257         check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
5258         if node_txn[0].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
5259                 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
5260         } else {
5261                 assert_eq!(node_txn[0].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
5262                 assert_eq!(node_txn[0].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
5263         }
5264
5265         assert_eq!(node_txn[1].input.len(), 1);
5266         check_spends!(node_txn[1], revoked_htlc_txn[0]);
5267
5268         check_spends!(node_txn[2], chan_1.3);
5269
5270         mine_transaction(&nodes[0], &node_txn[1]);
5271         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5272
5273         // Note that nodes[0]'s tx_broadcaster is still locked, so if we get here the channelmonitor
5274         // didn't try to generate any new transactions.
5275
5276         // Check A's ChannelMonitor was able to generate the right spendable output descriptor
5277         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5278         assert_eq!(spend_txn.len(), 3);
5279         assert_eq!(spend_txn[0].input.len(), 1);
5280         check_spends!(spend_txn[0], revoked_local_txn[0]); // spending to_remote output from revoked local tx
5281         assert_ne!(spend_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
5282         check_spends!(spend_txn[1], node_txn[1]); // spending justice tx output on the htlc success tx
5283         check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[1]); // Both outputs
5284 }
5285
5286 #[test]
5287 fn test_onchain_to_onchain_claim() {
5288         // Test that in case of channel closure, we detect the state of output and claim HTLC
5289         // on downstream peer's remote commitment tx.
5290         // First, have C claim an HTLC against its own latest commitment transaction.
5291         // Then, broadcast these to B, which should update the monitor downstream on the A<->B
5292         // channel.
5293         // Finally, check that B will claim the HTLC output if A's latest commitment transaction
5294         // gets broadcast.
5295
5296         let chanmon_cfgs = create_chanmon_cfgs(3);
5297         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5298         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5299         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5300
5301         // Create some initial channels
5302         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5303         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
5304
5305         // Ensure all nodes are at the same height
5306         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5307         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5308         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5309         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5310
5311         // Rebalance the network a bit by relaying one payment through all the channels ...
5312         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
5313         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
5314
5315         let (payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
5316         let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
5317         check_spends!(commitment_tx[0], chan_2.3);
5318         nodes[2].node.claim_funds(payment_preimage);
5319         check_added_monitors!(nodes[2], 1);
5320         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
5321         assert!(updates.update_add_htlcs.is_empty());
5322         assert!(updates.update_fail_htlcs.is_empty());
5323         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
5324         assert!(updates.update_fail_malformed_htlcs.is_empty());
5325
5326         mine_transaction(&nodes[2], &commitment_tx[0]);
5327         check_closed_broadcast!(nodes[2], true);
5328         check_added_monitors!(nodes[2], 1);
5329
5330         let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
5331         assert_eq!(c_txn.len(), 3);
5332         assert_eq!(c_txn[0], c_txn[2]);
5333         assert_eq!(commitment_tx[0], c_txn[1]);
5334         check_spends!(c_txn[1], chan_2.3);
5335         check_spends!(c_txn[2], c_txn[1]);
5336         assert_eq!(c_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
5337         assert_eq!(c_txn[2].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5338         assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
5339         assert_eq!(c_txn[0].lock_time, 0); // Success tx
5340
5341         // 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
5342         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
5343         connect_block(&nodes[1], &Block { header, txdata: vec![c_txn[1].clone(), c_txn[2].clone()]});
5344         check_added_monitors!(nodes[1], 1);
5345         expect_payment_forwarded!(nodes[1], Some(1000), true);
5346         {
5347                 let mut b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5348                 // ChannelMonitor: claim tx
5349                 assert_eq!(b_txn.len(), 1);
5350                 check_spends!(b_txn[0], chan_2.3); // B local commitment tx, issued by ChannelManager
5351                 b_txn.clear();
5352         }
5353         let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
5354         assert_eq!(msg_events.len(), 3);
5355         check_added_monitors!(nodes[1], 1);
5356         match msg_events[0] {
5357                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5358                 _ => panic!("Unexpected event"),
5359         }
5360         match msg_events[1] {
5361                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
5362                 _ => panic!("Unexpected event"),
5363         }
5364         match msg_events[2] {
5365                 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, .. } } => {
5366                         assert!(update_add_htlcs.is_empty());
5367                         assert!(update_fail_htlcs.is_empty());
5368                         assert_eq!(update_fulfill_htlcs.len(), 1);
5369                         assert!(update_fail_malformed_htlcs.is_empty());
5370                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
5371                 },
5372                 _ => panic!("Unexpected event"),
5373         };
5374         // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
5375         let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
5376         mine_transaction(&nodes[1], &commitment_tx[0]);
5377         let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5378         // ChannelMonitor: HTLC-Success tx, ChannelManager: local commitment tx + HTLC-Success tx
5379         assert_eq!(b_txn.len(), 3);
5380         check_spends!(b_txn[1], chan_1.3);
5381         check_spends!(b_txn[2], b_txn[1]);
5382         check_spends!(b_txn[0], commitment_tx[0]);
5383         assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5384         assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
5385         assert_eq!(b_txn[0].lock_time, 0); // Success tx
5386
5387         check_closed_broadcast!(nodes[1], true);
5388         check_added_monitors!(nodes[1], 1);
5389 }
5390
5391 #[test]
5392 fn test_duplicate_payment_hash_one_failure_one_success() {
5393         // Topology : A --> B --> C --> D
5394         // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
5395         // Note that because C will refuse to generate two payment secrets for the same payment hash,
5396         // we forward one of the payments onwards to D.
5397         let chanmon_cfgs = create_chanmon_cfgs(4);
5398         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
5399         // When this test was written, the default base fee floated based on the HTLC count.
5400         // It is now fixed, so we simply set the fee to the expected value here.
5401         let mut config = test_default_channel_config();
5402         config.channel_options.forwarding_fee_base_msat = 196;
5403         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs,
5404                 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
5405         let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
5406
5407         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5408         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
5409         create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
5410
5411         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5412         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5413         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5414         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5415         connect_blocks(&nodes[3], node_max_height - nodes[3].best_block_info().1);
5416
5417         let (our_payment_preimage, duplicate_payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000);
5418
5419         let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200, 0).unwrap();
5420         // We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
5421         // script push size limit so that the below script length checks match
5422         // ACCEPTED_HTLC_SCRIPT_WEIGHT.
5423         let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph.read().unwrap(),
5424                 &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 900000, TEST_FINAL_CLTV - 40, nodes[0].logger).unwrap();
5425         send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 900000, duplicate_payment_hash, payment_secret);
5426
5427         let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
5428         assert_eq!(commitment_txn[0].input.len(), 1);
5429         check_spends!(commitment_txn[0], chan_2.3);
5430
5431         mine_transaction(&nodes[1], &commitment_txn[0]);
5432         check_closed_broadcast!(nodes[1], true);
5433         check_added_monitors!(nodes[1], 1);
5434         connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
5435
5436         let htlc_timeout_tx;
5437         { // Extract one of the two HTLC-Timeout transaction
5438                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5439                 // ChannelMonitor: timeout tx * 3, ChannelManager: local commitment tx
5440                 assert_eq!(node_txn.len(), 4);
5441                 check_spends!(node_txn[0], chan_2.3);
5442
5443                 check_spends!(node_txn[1], commitment_txn[0]);
5444                 assert_eq!(node_txn[1].input.len(), 1);
5445                 check_spends!(node_txn[2], commitment_txn[0]);
5446                 assert_eq!(node_txn[2].input.len(), 1);
5447                 assert_eq!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5448                 check_spends!(node_txn[3], commitment_txn[0]);
5449                 assert_ne!(node_txn[1].input[0].previous_output, node_txn[3].input[0].previous_output);
5450
5451                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5452                 assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5453                 assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5454                 htlc_timeout_tx = node_txn[1].clone();
5455         }
5456
5457         nodes[2].node.claim_funds(our_payment_preimage);
5458         mine_transaction(&nodes[2], &commitment_txn[0]);
5459         check_added_monitors!(nodes[2], 2);
5460         let events = nodes[2].node.get_and_clear_pending_msg_events();
5461         match events[0] {
5462                 MessageSendEvent::UpdateHTLCs { .. } => {},
5463                 _ => panic!("Unexpected event"),
5464         }
5465         match events[1] {
5466                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5467                 _ => panic!("Unexepected event"),
5468         }
5469         let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
5470         assert_eq!(htlc_success_txn.len(), 5); // ChannelMonitor: HTLC-Success txn (*2 due to 2-HTLC outputs), ChannelManager: local commitment tx + HTLC-Success txn (*2 due to 2-HTLC outputs)
5471         check_spends!(htlc_success_txn[0], commitment_txn[0]);
5472         check_spends!(htlc_success_txn[1], commitment_txn[0]);
5473         assert_eq!(htlc_success_txn[0].input.len(), 1);
5474         assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5475         assert_eq!(htlc_success_txn[1].input.len(), 1);
5476         assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5477         assert_ne!(htlc_success_txn[0].input[0].previous_output, htlc_success_txn[1].input[0].previous_output);
5478         assert_eq!(htlc_success_txn[2], commitment_txn[0]);
5479         assert_eq!(htlc_success_txn[3], htlc_success_txn[0]);
5480         assert_eq!(htlc_success_txn[4], htlc_success_txn[1]);
5481         assert_ne!(htlc_success_txn[0].input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
5482
5483         mine_transaction(&nodes[1], &htlc_timeout_tx);
5484         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5485         expect_pending_htlcs_forwardable!(nodes[1]);
5486         let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5487         assert!(htlc_updates.update_add_htlcs.is_empty());
5488         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
5489         let first_htlc_id = htlc_updates.update_fail_htlcs[0].htlc_id;
5490         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
5491         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
5492         check_added_monitors!(nodes[1], 1);
5493
5494         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
5495         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5496         {
5497                 commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
5498                 expect_payment_failure_chan_update!(nodes[0], chan_2.0.contents.short_channel_id, true);
5499         }
5500         expect_payment_failed!(nodes[0], duplicate_payment_hash, false);
5501
5502         // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
5503         // Note that the fee paid is effectively double as the HTLC value (including the nodes[1] fee
5504         // and nodes[2] fee) is rounded down and then claimed in full.
5505         mine_transaction(&nodes[1], &htlc_success_txn[0]);
5506         expect_payment_forwarded!(nodes[1], Some(196*2), true);
5507         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5508         assert!(updates.update_add_htlcs.is_empty());
5509         assert!(updates.update_fail_htlcs.is_empty());
5510         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
5511         assert_ne!(updates.update_fulfill_htlcs[0].htlc_id, first_htlc_id);
5512         assert!(updates.update_fail_malformed_htlcs.is_empty());
5513         check_added_monitors!(nodes[1], 1);
5514
5515         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
5516         commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
5517
5518         let events = nodes[0].node.get_and_clear_pending_events();
5519         match events[0] {
5520                 Event::PaymentSent { ref payment_preimage } => {
5521                         assert_eq!(*payment_preimage, our_payment_preimage);
5522                 }
5523                 _ => panic!("Unexpected event"),
5524         }
5525 }
5526
5527 #[test]
5528 fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5529         let chanmon_cfgs = create_chanmon_cfgs(2);
5530         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5531         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5532         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5533
5534         // Create some initial channels
5535         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5536
5537         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
5538         let local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
5539         assert_eq!(local_txn.len(), 1);
5540         assert_eq!(local_txn[0].input.len(), 1);
5541         check_spends!(local_txn[0], chan_1.3);
5542
5543         // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
5544         nodes[1].node.claim_funds(payment_preimage);
5545         check_added_monitors!(nodes[1], 1);
5546         mine_transaction(&nodes[1], &local_txn[0]);
5547         check_added_monitors!(nodes[1], 1);
5548         let events = nodes[1].node.get_and_clear_pending_msg_events();
5549         match events[0] {
5550                 MessageSendEvent::UpdateHTLCs { .. } => {},
5551                 _ => panic!("Unexpected event"),
5552         }
5553         match events[1] {
5554                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5555                 _ => panic!("Unexepected event"),
5556         }
5557         let node_tx = {
5558                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5559                 assert_eq!(node_txn.len(), 3);
5560                 assert_eq!(node_txn[0], node_txn[2]);
5561                 assert_eq!(node_txn[1], local_txn[0]);
5562                 assert_eq!(node_txn[0].input.len(), 1);
5563                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5564                 check_spends!(node_txn[0], local_txn[0]);
5565                 node_txn[0].clone()
5566         };
5567
5568         mine_transaction(&nodes[1], &node_tx);
5569         connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
5570
5571         // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
5572         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5573         assert_eq!(spend_txn.len(), 1);
5574         assert_eq!(spend_txn[0].input.len(), 1);
5575         check_spends!(spend_txn[0], node_tx);
5576         assert_eq!(spend_txn[0].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
5577 }
5578
5579 fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
5580         // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
5581         // unrevoked commitment transaction.
5582         // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
5583         // a remote RAA before they could be failed backwards (and combinations thereof).
5584         // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
5585         // use the same payment hashes.
5586         // Thus, we use a six-node network:
5587         //
5588         // A \         / E
5589         //    - C - D -
5590         // B /         \ F
5591         // And test where C fails back to A/B when D announces its latest commitment transaction
5592         let chanmon_cfgs = create_chanmon_cfgs(6);
5593         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
5594         // When this test was written, the default base fee floated based on the HTLC count.
5595         // It is now fixed, so we simply set the fee to the expected value here.
5596         let mut config = test_default_channel_config();
5597         config.channel_options.forwarding_fee_base_msat = 196;
5598         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs,
5599                 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
5600         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
5601         let logger = test_utils::TestLogger::new();
5602
5603         create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known());
5604         create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
5605         let chan = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
5606         create_announced_chan_between_nodes(&nodes, 3, 4, InitFeatures::known(), InitFeatures::known());
5607         create_announced_chan_between_nodes(&nodes, 3, 5, InitFeatures::known(), InitFeatures::known());
5608
5609         // Rebalance and check output sanity...
5610         send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
5611         send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
5612         assert_eq!(get_local_commitment_txn!(nodes[3], chan.2)[0].output.len(), 2);
5613
5614         let ds_dust_limit = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().holder_dust_limit_satoshis;
5615         // 0th HTLC:
5616         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
5617         // 1st HTLC:
5618         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
5619         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
5620         let our_node_id = &nodes[1].node.get_our_node_id();
5621         let route = get_route(our_node_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, &logger).unwrap();
5622         // 2nd HTLC:
5623         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, 0).unwrap()); // not added < dust limit + HTLC tx fee
5624         // 3rd HTLC:
5625         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, 0).unwrap()); // not added < dust limit + HTLC tx fee
5626         // 4th HTLC:
5627         let (_, payment_hash_3, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5628         // 5th HTLC:
5629         let (_, payment_hash_4, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5630         let route = get_route(our_node_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
5631         // 6th HTLC:
5632         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, 0).unwrap());
5633         // 7th HTLC:
5634         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, 0).unwrap());
5635
5636         // 8th HTLC:
5637         let (_, payment_hash_5, _) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5638         // 9th HTLC:
5639         let route = get_route(our_node_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, &logger).unwrap();
5640         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, 0).unwrap()); // not added < dust limit + HTLC tx fee
5641
5642         // 10th HTLC:
5643         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
5644         // 11th HTLC:
5645         let route = get_route(our_node_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[5].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, &logger).unwrap();
5646         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, 0).unwrap());
5647
5648         // Double-check that six of the new HTLC were added
5649         // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
5650         // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
5651         assert_eq!(get_local_commitment_txn!(nodes[3], chan.2).len(), 1);
5652         assert_eq!(get_local_commitment_txn!(nodes[3], chan.2)[0].output.len(), 8);
5653
5654         // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
5655         // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5656         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
5657         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
5658         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
5659         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
5660         check_added_monitors!(nodes[4], 0);
5661         expect_pending_htlcs_forwardable!(nodes[4]);
5662         check_added_monitors!(nodes[4], 1);
5663
5664         let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
5665         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]);
5666         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]);
5667         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]);
5668         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]);
5669         commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
5670
5671         // Fail 3rd below-dust and 7th above-dust HTLCs
5672         assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
5673         assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
5674         check_added_monitors!(nodes[5], 0);
5675         expect_pending_htlcs_forwardable!(nodes[5]);
5676         check_added_monitors!(nodes[5], 1);
5677
5678         let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
5679         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]);
5680         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]);
5681         commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
5682
5683         let ds_prev_commitment_tx = get_local_commitment_txn!(nodes[3], chan.2);
5684
5685         expect_pending_htlcs_forwardable!(nodes[3]);
5686         check_added_monitors!(nodes[3], 1);
5687         let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
5688         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]);
5689         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]);
5690         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]);
5691         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]);
5692         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]);
5693         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]);
5694         if deliver_last_raa {
5695                 commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
5696         } else {
5697                 let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
5698         }
5699
5700         // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
5701         // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
5702         // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
5703         // propagated back to A/B yet (and D has two unrevoked commitment transactions).
5704         //
5705         // We now broadcast the latest commitment transaction, which *should* result in failures for
5706         // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
5707         // the non-broadcast above-dust HTLCs.
5708         //
5709         // Alternatively, we may broadcast the previous commitment transaction, which should only
5710         // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
5711         let ds_last_commitment_tx = get_local_commitment_txn!(nodes[3], chan.2);
5712
5713         if announce_latest {
5714                 mine_transaction(&nodes[2], &ds_last_commitment_tx[0]);
5715         } else {
5716                 mine_transaction(&nodes[2], &ds_prev_commitment_tx[0]);
5717         }
5718         connect_blocks(&nodes[2], ANTI_REORG_DELAY - 1);
5719         check_closed_broadcast!(nodes[2], true);
5720         expect_pending_htlcs_forwardable!(nodes[2]);
5721         check_added_monitors!(nodes[2], 3);
5722
5723         let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
5724         assert_eq!(cs_msgs.len(), 2);
5725         let mut a_done = false;
5726         for msg in cs_msgs {
5727                 match msg {
5728                         MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
5729                                 // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
5730                                 // should be failed-backwards here.
5731                                 let target = if *node_id == nodes[0].node.get_our_node_id() {
5732                                         // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
5733                                         for htlc in &updates.update_fail_htlcs {
5734                                                 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 });
5735                                         }
5736                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
5737                                         assert!(!a_done);
5738                                         a_done = true;
5739                                         &nodes[0]
5740                                 } else {
5741                                         // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
5742                                         for htlc in &updates.update_fail_htlcs {
5743                                                 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
5744                                         }
5745                                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
5746                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
5747                                         &nodes[1]
5748                                 };
5749                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
5750                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]);
5751                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]);
5752                                 if announce_latest {
5753                                         target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]);
5754                                         if *node_id == nodes[0].node.get_our_node_id() {
5755                                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]);
5756                                         }
5757                                 }
5758                                 commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
5759                         },
5760                         _ => panic!("Unexpected event"),
5761                 }
5762         }
5763
5764         let as_events = nodes[0].node.get_and_clear_pending_events();
5765         assert_eq!(as_events.len(), if announce_latest { 5 } else { 3 });
5766         let mut as_failds = HashSet::new();
5767         for event in as_events.iter() {
5768                 if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
5769                         assert!(as_failds.insert(*payment_hash));
5770                         if *payment_hash != payment_hash_2 {
5771                                 assert_eq!(*rejected_by_dest, deliver_last_raa);
5772                         } else {
5773                                 assert!(!rejected_by_dest);
5774                         }
5775                 } else { panic!("Unexpected event"); }
5776         }
5777         assert!(as_failds.contains(&payment_hash_1));
5778         assert!(as_failds.contains(&payment_hash_2));
5779         if announce_latest {
5780                 assert!(as_failds.contains(&payment_hash_3));
5781                 assert!(as_failds.contains(&payment_hash_5));
5782         }
5783         assert!(as_failds.contains(&payment_hash_6));
5784
5785         let bs_events = nodes[1].node.get_and_clear_pending_events();
5786         assert_eq!(bs_events.len(), if announce_latest { 4 } else { 3 });
5787         let mut bs_failds = HashSet::new();
5788         for event in bs_events.iter() {
5789                 if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
5790                         assert!(bs_failds.insert(*payment_hash));
5791                         if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
5792                                 assert_eq!(*rejected_by_dest, deliver_last_raa);
5793                         } else {
5794                                 assert!(!rejected_by_dest);
5795                         }
5796                 } else { panic!("Unexpected event"); }
5797         }
5798         assert!(bs_failds.contains(&payment_hash_1));
5799         assert!(bs_failds.contains(&payment_hash_2));
5800         if announce_latest {
5801                 assert!(bs_failds.contains(&payment_hash_4));
5802         }
5803         assert!(bs_failds.contains(&payment_hash_5));
5804
5805         // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
5806         // get a PaymentFailureNetworkUpdate. A should have gotten 4 HTLCs which were failed-back due
5807         // to unknown-preimage-etc, B should have gotten 2. Thus, in the
5808         // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2
5809         // PaymentFailureNetworkUpdates.
5810         let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
5811         assert_eq!(as_msg_events.len(), if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
5812         let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
5813         assert_eq!(bs_msg_events.len(), if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
5814         for event in as_msg_events.iter().chain(bs_msg_events.iter()) {
5815                 match event {
5816                         &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
5817                         _ => panic!("Unexpected event"),
5818                 }
5819         }
5820 }
5821
5822 #[test]
5823 fn test_fail_backwards_latest_remote_announce_a() {
5824         do_test_fail_backwards_unrevoked_remote_announce(false, true);
5825 }
5826
5827 #[test]
5828 fn test_fail_backwards_latest_remote_announce_b() {
5829         do_test_fail_backwards_unrevoked_remote_announce(true, true);
5830 }
5831
5832 #[test]
5833 fn test_fail_backwards_previous_remote_announce() {
5834         do_test_fail_backwards_unrevoked_remote_announce(false, false);
5835         // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
5836         // tested for in test_commitment_revoked_fail_backward_exhaustive()
5837 }
5838
5839 #[test]
5840 fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
5841         let chanmon_cfgs = create_chanmon_cfgs(2);
5842         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5843         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5844         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5845
5846         // Create some initial channels
5847         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5848
5849         let (_, our_payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5850         let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5851         assert_eq!(local_txn[0].input.len(), 1);
5852         check_spends!(local_txn[0], chan_1.3);
5853
5854         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5855         mine_transaction(&nodes[0], &local_txn[0]);
5856         check_closed_broadcast!(nodes[0], true);
5857         check_added_monitors!(nodes[0], 1);
5858         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
5859
5860         let htlc_timeout = {
5861                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5862                 assert_eq!(node_txn.len(), 2);
5863                 check_spends!(node_txn[0], chan_1.3);
5864                 assert_eq!(node_txn[1].input.len(), 1);
5865                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5866                 check_spends!(node_txn[1], local_txn[0]);
5867                 node_txn[1].clone()
5868         };
5869
5870         mine_transaction(&nodes[0], &htlc_timeout);
5871         connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5872         expect_payment_failed!(nodes[0], our_payment_hash, true);
5873
5874         // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5875         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5876         assert_eq!(spend_txn.len(), 3);
5877         check_spends!(spend_txn[0], local_txn[0]);
5878         assert_eq!(spend_txn[1].input.len(), 1);
5879         check_spends!(spend_txn[1], htlc_timeout);
5880         assert_eq!(spend_txn[1].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
5881         assert_eq!(spend_txn[2].input.len(), 2);
5882         check_spends!(spend_txn[2], local_txn[0], htlc_timeout);
5883         assert!(spend_txn[2].input[0].sequence == BREAKDOWN_TIMEOUT as u32 ||
5884                 spend_txn[2].input[1].sequence == BREAKDOWN_TIMEOUT as u32);
5885 }
5886
5887 #[test]
5888 fn test_key_derivation_params() {
5889         // This test is a copy of test_dynamic_spendable_outputs_local_htlc_timeout_tx, with
5890         // a key manager rotation to test that key_derivation_params returned in DynamicOutputP2WSH
5891         // let us re-derive the channel key set to then derive a delayed_payment_key.
5892
5893         let chanmon_cfgs = create_chanmon_cfgs(3);
5894
5895         // We manually create the node configuration to backup the seed.
5896         let seed = [42; 32];
5897         let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5898         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);
5899         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, chain_monitor, keys_manager: &keys_manager, node_seed: seed, features: InitFeatures::known() };
5900         let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5901         node_cfgs.remove(0);
5902         node_cfgs.insert(0, node);
5903
5904         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5905         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5906
5907         // Create some initial channels
5908         // Create a dummy channel to advance index by one and thus test re-derivation correctness
5909         // for node 0
5910         let chan_0 = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known());
5911         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5912         assert_ne!(chan_0.3.output[0].script_pubkey, chan_1.3.output[0].script_pubkey);
5913
5914         // Ensure all nodes are at the same height
5915         let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5916         connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5917         connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5918         connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5919
5920         let (_, our_payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5921         let local_txn_0 = get_local_commitment_txn!(nodes[0], chan_0.2);
5922         let local_txn_1 = get_local_commitment_txn!(nodes[0], chan_1.2);
5923         assert_eq!(local_txn_1[0].input.len(), 1);
5924         check_spends!(local_txn_1[0], chan_1.3);
5925
5926         // We check funding pubkey are unique
5927         let (from_0_funding_key_0, from_0_funding_key_1) = (PublicKey::from_slice(&local_txn_0[0].input[0].witness[3][2..35]), PublicKey::from_slice(&local_txn_0[0].input[0].witness[3][36..69]));
5928         let (from_1_funding_key_0, from_1_funding_key_1) = (PublicKey::from_slice(&local_txn_1[0].input[0].witness[3][2..35]), PublicKey::from_slice(&local_txn_1[0].input[0].witness[3][36..69]));
5929         if from_0_funding_key_0 == from_1_funding_key_0
5930             || from_0_funding_key_0 == from_1_funding_key_1
5931             || from_0_funding_key_1 == from_1_funding_key_0
5932             || from_0_funding_key_1 == from_1_funding_key_1 {
5933                 panic!("Funding pubkeys aren't unique");
5934         }
5935
5936         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5937         mine_transaction(&nodes[0], &local_txn_1[0]);
5938         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
5939         check_closed_broadcast!(nodes[0], true);
5940         check_added_monitors!(nodes[0], 1);
5941
5942         let htlc_timeout = {
5943                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5944                 assert_eq!(node_txn[1].input.len(), 1);
5945                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5946                 check_spends!(node_txn[1], local_txn_1[0]);
5947                 node_txn[1].clone()
5948         };
5949
5950         mine_transaction(&nodes[0], &htlc_timeout);
5951         connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5952         expect_payment_failed!(nodes[0], our_payment_hash, true);
5953
5954         // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5955         let new_keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5956         let spend_txn = check_spendable_outputs!(nodes[0], new_keys_manager);
5957         assert_eq!(spend_txn.len(), 3);
5958         check_spends!(spend_txn[0], local_txn_1[0]);
5959         assert_eq!(spend_txn[1].input.len(), 1);
5960         check_spends!(spend_txn[1], htlc_timeout);
5961         assert_eq!(spend_txn[1].input[0].sequence, BREAKDOWN_TIMEOUT as u32);
5962         assert_eq!(spend_txn[2].input.len(), 2);
5963         check_spends!(spend_txn[2], local_txn_1[0], htlc_timeout);
5964         assert!(spend_txn[2].input[0].sequence == BREAKDOWN_TIMEOUT as u32 ||
5965                 spend_txn[2].input[1].sequence == BREAKDOWN_TIMEOUT as u32);
5966 }
5967
5968 #[test]
5969 fn test_static_output_closing_tx() {
5970         let chanmon_cfgs = create_chanmon_cfgs(2);
5971         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5972         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5973         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5974
5975         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
5976
5977         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5978         let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
5979
5980         mine_transaction(&nodes[0], &closing_tx);
5981         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5982
5983         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5984         assert_eq!(spend_txn.len(), 1);
5985         check_spends!(spend_txn[0], closing_tx);
5986
5987         mine_transaction(&nodes[1], &closing_tx);
5988         connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5989
5990         let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5991         assert_eq!(spend_txn.len(), 1);
5992         check_spends!(spend_txn[0], closing_tx);
5993 }
5994
5995 fn do_htlc_claim_local_commitment_only(use_dust: bool) {
5996         let chanmon_cfgs = create_chanmon_cfgs(2);
5997         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5998         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5999         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6000         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6001
6002         let (our_payment_preimage, _, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
6003
6004         // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
6005         // present in B's local commitment transaction, but none of A's commitment transactions.
6006         assert!(nodes[1].node.claim_funds(our_payment_preimage));
6007         check_added_monitors!(nodes[1], 1);
6008
6009         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6010         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
6011         let events = nodes[0].node.get_and_clear_pending_events();
6012         assert_eq!(events.len(), 1);
6013         match events[0] {
6014                 Event::PaymentSent { payment_preimage } => {
6015                         assert_eq!(payment_preimage, our_payment_preimage);
6016                 },
6017                 _ => panic!("Unexpected event"),
6018         }
6019
6020         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
6021         check_added_monitors!(nodes[0], 1);
6022         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6023         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
6024         check_added_monitors!(nodes[1], 1);
6025
6026         let starting_block = nodes[1].best_block_info();
6027         let mut block = Block {
6028                 header: BlockHeader { version: 0x20000000, prev_blockhash: starting_block.0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
6029                 txdata: vec![],
6030         };
6031         for _ in starting_block.1 + 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + starting_block.1 + 2 {
6032                 connect_block(&nodes[1], &block);
6033                 block.header.prev_blockhash = block.block_hash();
6034         }
6035         test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
6036         check_closed_broadcast!(nodes[1], true);
6037         check_added_monitors!(nodes[1], 1);
6038 }
6039
6040 fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
6041         let chanmon_cfgs = create_chanmon_cfgs(2);
6042         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6043         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6044         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6045         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6046         let logger = test_utils::TestLogger::new();
6047
6048         let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1]);
6049         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6050         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV, &logger).unwrap();
6051         nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
6052         check_added_monitors!(nodes[0], 1);
6053
6054         let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6055
6056         // As far as A is concerned, the HTLC is now present only in the latest remote commitment
6057         // transaction, however it is not in A's latest local commitment, so we can just broadcast that
6058         // to "time out" the HTLC.
6059
6060         let starting_block = nodes[1].best_block_info();
6061         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: starting_block.0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
6062
6063         for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + starting_block.1 + 2 {
6064                 connect_block(&nodes[0], &Block { header, txdata: Vec::new()});
6065                 header.prev_blockhash = header.block_hash();
6066         }
6067         test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
6068         check_closed_broadcast!(nodes[0], true);
6069         check_added_monitors!(nodes[0], 1);
6070 }
6071
6072 fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
6073         let chanmon_cfgs = create_chanmon_cfgs(3);
6074         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6075         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6076         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6077         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6078
6079         // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
6080         // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
6081         // Also optionally test that we *don't* fail the channel in case the commitment transaction was
6082         // actually revoked.
6083         let htlc_value = if use_dust { 50000 } else { 3000000 };
6084         let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
6085         assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
6086         expect_pending_htlcs_forwardable!(nodes[1]);
6087         check_added_monitors!(nodes[1], 1);
6088
6089         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6090         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
6091         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
6092         check_added_monitors!(nodes[0], 1);
6093         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6094         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
6095         check_added_monitors!(nodes[1], 1);
6096         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1);
6097         check_added_monitors!(nodes[1], 1);
6098         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
6099
6100         if check_revoke_no_close {
6101                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
6102                 check_added_monitors!(nodes[0], 1);
6103         }
6104
6105         let starting_block = nodes[1].best_block_info();
6106         let mut block = Block {
6107                 header: BlockHeader { version: 0x20000000, prev_blockhash: starting_block.0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
6108                 txdata: vec![],
6109         };
6110         for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 2 {
6111                 connect_block(&nodes[0], &block);
6112                 block.header.prev_blockhash = block.block_hash();
6113         }
6114         if !check_revoke_no_close {
6115                 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
6116                 check_closed_broadcast!(nodes[0], true);
6117                 check_added_monitors!(nodes[0], 1);
6118         } else {
6119                 expect_payment_failed!(nodes[0], our_payment_hash, true);
6120         }
6121 }
6122
6123 // Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
6124 // There are only a few cases to test here:
6125 //  * its not really normative behavior, but we test that below-dust HTLCs "included" in
6126 //    broadcastable commitment transactions result in channel closure,
6127 //  * its included in an unrevoked-but-previous remote commitment transaction,
6128 //  * its included in the latest remote or local commitment transactions.
6129 // We test each of the three possible commitment transactions individually and use both dust and
6130 // non-dust HTLCs.
6131 // Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
6132 // assume they are handled the same across all six cases, as both outbound and inbound failures are
6133 // tested for at least one of the cases in other tests.
6134 #[test]
6135 fn htlc_claim_single_commitment_only_a() {
6136         do_htlc_claim_local_commitment_only(true);
6137         do_htlc_claim_local_commitment_only(false);
6138
6139         do_htlc_claim_current_remote_commitment_only(true);
6140         do_htlc_claim_current_remote_commitment_only(false);
6141 }
6142
6143 #[test]
6144 fn htlc_claim_single_commitment_only_b() {
6145         do_htlc_claim_previous_remote_commitment_only(true, false);
6146         do_htlc_claim_previous_remote_commitment_only(false, false);
6147         do_htlc_claim_previous_remote_commitment_only(true, true);
6148         do_htlc_claim_previous_remote_commitment_only(false, true);
6149 }
6150
6151 #[test]
6152 #[should_panic]
6153 fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
6154         let chanmon_cfgs = create_chanmon_cfgs(2);
6155         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6156         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6157         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6158         //Force duplicate channel ids
6159         for node in nodes.iter() {
6160                 *node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
6161         }
6162
6163         // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
6164         let channel_value_satoshis=10000;
6165         let push_msat=10001;
6166         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
6167         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
6168         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &node0_to_1_send_open_channel);
6169
6170         //Create a second channel with a channel_id collision
6171         assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
6172 }
6173
6174 #[test]
6175 fn bolt2_open_channel_sending_node_checks_part2() {
6176         let chanmon_cfgs = create_chanmon_cfgs(2);
6177         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6178         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6179         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6180
6181         // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
6182         let channel_value_satoshis=2^24;
6183         let push_msat=10001;
6184         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
6185
6186         // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
6187         let channel_value_satoshis=10000;
6188         // Test when push_msat is equal to 1000 * funding_satoshis.
6189         let push_msat=1000*channel_value_satoshis+1;
6190         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
6191
6192         // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
6193         let channel_value_satoshis=10000;
6194         let push_msat=10001;
6195         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
6196         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
6197         assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
6198
6199         // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
6200         // 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
6201         assert!(node0_to_1_send_open_channel.channel_flags<=1);
6202
6203         // 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.
6204         assert!(BREAKDOWN_TIMEOUT>0);
6205         assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
6206
6207         // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
6208         let chain_hash=genesis_block(Network::Testnet).header.block_hash();
6209         assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
6210
6211         // 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.
6212         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
6213         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
6214         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
6215         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_point.serialize()).is_ok());
6216         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
6217 }
6218
6219 #[test]
6220 fn bolt2_open_channel_sane_dust_limit() {
6221         let chanmon_cfgs = create_chanmon_cfgs(2);
6222         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6223         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6224         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6225
6226         let channel_value_satoshis=1000000;
6227         let push_msat=10001;
6228         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
6229         let mut node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
6230         node0_to_1_send_open_channel.dust_limit_satoshis = 661;
6231         node0_to_1_send_open_channel.channel_reserve_satoshis = 100001;
6232
6233         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &node0_to_1_send_open_channel);
6234         let events = nodes[1].node.get_and_clear_pending_msg_events();
6235         let err_msg = match events[0] {
6236                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
6237                         msg.clone()
6238                 },
6239                 _ => panic!("Unexpected event"),
6240         };
6241         assert_eq!(err_msg.data, "dust_limit_satoshis (661) is greater than the implementation limit (660)");
6242 }
6243
6244 // Test that if we fail to send an HTLC that is being freed from the holding cell, and the HTLC
6245 // originated from our node, its failure is surfaced to the user. We trigger this failure to
6246 // free the HTLC by increasing our fee while the HTLC is in the holding cell such that the HTLC
6247 // is no longer affordable once it's freed.
6248 #[test]
6249 fn test_fail_holding_cell_htlc_upon_free() {
6250         let chanmon_cfgs = create_chanmon_cfgs(2);
6251         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6252         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6253         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6254         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6255         let logger = test_utils::TestLogger::new();
6256
6257         // First nodes[0] generates an update_fee, setting the channel's
6258         // pending_update_fee.
6259         nodes[0].node.update_fee(chan.2, get_feerate!(nodes[0], chan.2) + 20).unwrap();
6260         check_added_monitors!(nodes[0], 1);
6261
6262         let events = nodes[0].node.get_and_clear_pending_msg_events();
6263         assert_eq!(events.len(), 1);
6264         let (update_msg, commitment_signed) = match events[0] {
6265                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6266                         (update_fee.as_ref(), commitment_signed)
6267                 },
6268                 _ => panic!("Unexpected event"),
6269         };
6270
6271         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
6272
6273         let mut chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6274         let channel_reserve = chan_stat.channel_reserve_msat;
6275         let feerate = get_feerate!(nodes[0], chan.2);
6276
6277         // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
6278         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6279         let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1);
6280         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6281         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
6282
6283         // Send a payment which passes reserve checks but gets stuck in the holding cell.
6284         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6285         chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6286         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
6287
6288         // Flush the pending fee update.
6289         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
6290         let (as_revoke_and_ack, _) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6291         check_added_monitors!(nodes[1], 1);
6292         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
6293         check_added_monitors!(nodes[0], 1);
6294
6295         // Upon receipt of the RAA, there will be an attempt to resend the holding cell
6296         // HTLC, but now that the fee has been raised the payment will now fail, causing
6297         // us to surface its failure to the user.
6298         chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6299         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6300         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", hex::encode(chan.2)), 1);
6301         let failure_log = format!("Failed to send HTLC with payment_hash {} due to Cannot send value that would put our balance under counterparty-announced channel reserve value ({}) in channel {}",
6302                 hex::encode(our_payment_hash.0), chan_stat.channel_reserve_msat, hex::encode(chan.2));
6303         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), failure_log.to_string(), 1);
6304
6305         // Check that the payment failed to be sent out.
6306         let events = nodes[0].node.get_and_clear_pending_events();
6307         assert_eq!(events.len(), 1);
6308         match &events[0] {
6309                 &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, ref error_code, ref error_data } => {
6310                         assert_eq!(our_payment_hash.clone(), *payment_hash);
6311                         assert_eq!(*rejected_by_dest, false);
6312                         assert_eq!(*error_code, None);
6313                         assert_eq!(*error_data, None);
6314                 },
6315                 _ => panic!("Unexpected event"),
6316         }
6317 }
6318
6319 // Test that if multiple HTLCs are released from the holding cell and one is
6320 // valid but the other is no longer valid upon release, the valid HTLC can be
6321 // successfully completed while the other one fails as expected.
6322 #[test]
6323 fn test_free_and_fail_holding_cell_htlcs() {
6324         let chanmon_cfgs = create_chanmon_cfgs(2);
6325         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6326         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6327         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6328         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6329         let logger = test_utils::TestLogger::new();
6330
6331         // First nodes[0] generates an update_fee, setting the channel's
6332         // pending_update_fee.
6333         nodes[0].node.update_fee(chan.2, get_feerate!(nodes[0], chan.2) + 200).unwrap();
6334         check_added_monitors!(nodes[0], 1);
6335
6336         let events = nodes[0].node.get_and_clear_pending_msg_events();
6337         assert_eq!(events.len(), 1);
6338         let (update_msg, commitment_signed) = match events[0] {
6339                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6340                         (update_fee.as_ref(), commitment_signed)
6341                 },
6342                 _ => panic!("Unexpected event"),
6343         };
6344
6345         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
6346
6347         let mut chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6348         let channel_reserve = chan_stat.channel_reserve_msat;
6349         let feerate = get_feerate!(nodes[0], chan.2);
6350
6351         // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
6352         let (payment_preimage_1, payment_hash_1, payment_secret_1) = get_payment_preimage_hash!(nodes[1]);
6353         let amt_1 = 20000;
6354         let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
6355         let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1) - amt_1;
6356         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6357         let route_1 = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], amt_1, TEST_FINAL_CLTV, &logger).unwrap();
6358         let route_2 = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], amt_2, TEST_FINAL_CLTV, &logger).unwrap();
6359
6360         // Send 2 payments which pass reserve checks but get stuck in the holding cell.
6361         nodes[0].node.send_payment(&route_1, payment_hash_1, &Some(payment_secret_1)).unwrap();
6362         chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6363         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1);
6364         nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2)).unwrap();
6365         chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6366         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1 + amt_2);
6367
6368         // Flush the pending fee update.
6369         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
6370         let (revoke_and_ack, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6371         check_added_monitors!(nodes[1], 1);
6372         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_and_ack);
6373         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6374         check_added_monitors!(nodes[0], 2);
6375
6376         // Upon receipt of the RAA, there will be an attempt to resend the holding cell HTLCs,
6377         // but now that the fee has been raised the second payment will now fail, causing us
6378         // to surface its failure to the user. The first payment should succeed.
6379         chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6380         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6381         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", hex::encode(chan.2)), 1);
6382         let failure_log = format!("Failed to send HTLC with payment_hash {} due to Cannot send value that would put our balance under counterparty-announced channel reserve value ({}) in channel {}",
6383                 hex::encode(payment_hash_2.0), chan_stat.channel_reserve_msat, hex::encode(chan.2));
6384         nodes[0].logger.assert_log("lightning::ln::channel".to_string(), failure_log.to_string(), 1);
6385
6386         // Check that the second payment failed to be sent out.
6387         let events = nodes[0].node.get_and_clear_pending_events();
6388         assert_eq!(events.len(), 1);
6389         match &events[0] {
6390                 &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, ref error_code, ref error_data } => {
6391                         assert_eq!(payment_hash_2.clone(), *payment_hash);
6392                         assert_eq!(*rejected_by_dest, false);
6393                         assert_eq!(*error_code, None);
6394                         assert_eq!(*error_data, None);
6395                 },
6396                 _ => panic!("Unexpected event"),
6397         }
6398
6399         // Complete the first payment and the RAA from the fee update.
6400         let (payment_event, send_raa_event) = {
6401                 let mut msgs = nodes[0].node.get_and_clear_pending_msg_events();
6402                 assert_eq!(msgs.len(), 2);
6403                 (SendEvent::from_event(msgs.remove(0)), msgs.remove(0))
6404         };
6405         let raa = match send_raa_event {
6406                 MessageSendEvent::SendRevokeAndACK { msg, .. } => msg,
6407                 _ => panic!("Unexpected event"),
6408         };
6409         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6410         check_added_monitors!(nodes[1], 1);
6411         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6412         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6413         let events = nodes[1].node.get_and_clear_pending_events();
6414         assert_eq!(events.len(), 1);
6415         match events[0] {
6416                 Event::PendingHTLCsForwardable { .. } => {},
6417                 _ => panic!("Unexpected event"),
6418         }
6419         nodes[1].node.process_pending_htlc_forwards();
6420         let events = nodes[1].node.get_and_clear_pending_events();
6421         assert_eq!(events.len(), 1);
6422         match events[0] {
6423                 Event::PaymentReceived { .. } => {},
6424                 _ => panic!("Unexpected event"),
6425         }
6426         nodes[1].node.claim_funds(payment_preimage_1);
6427         check_added_monitors!(nodes[1], 1);
6428         let update_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6429         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msgs.update_fulfill_htlcs[0]);
6430         commitment_signed_dance!(nodes[0], nodes[1], update_msgs.commitment_signed, false, true);
6431         let events = nodes[0].node.get_and_clear_pending_events();
6432         assert_eq!(events.len(), 1);
6433         match events[0] {
6434                 Event::PaymentSent { ref payment_preimage } => {
6435                         assert_eq!(*payment_preimage, payment_preimage_1);
6436                 }
6437                 _ => panic!("Unexpected event"),
6438         }
6439 }
6440
6441 // Test that if we fail to forward an HTLC that is being freed from the holding cell that the
6442 // HTLC is failed backwards. We trigger this failure to forward the freed HTLC by increasing
6443 // our fee while the HTLC is in the holding cell such that the HTLC is no longer affordable
6444 // once it's freed.
6445 #[test]
6446 fn test_fail_holding_cell_htlc_upon_free_multihop() {
6447         let chanmon_cfgs = create_chanmon_cfgs(3);
6448         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6449         // When this test was written, the default base fee floated based on the HTLC count.
6450         // It is now fixed, so we simply set the fee to the expected value here.
6451         let mut config = test_default_channel_config();
6452         config.channel_options.forwarding_fee_base_msat = 196;
6453         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
6454         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6455         let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6456         let chan_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6457         let logger = test_utils::TestLogger::new();
6458
6459         // First nodes[1] generates an update_fee, setting the channel's
6460         // pending_update_fee.
6461         nodes[1].node.update_fee(chan_1_2.2, get_feerate!(nodes[1], chan_1_2.2) + 20).unwrap();
6462         check_added_monitors!(nodes[1], 1);
6463
6464         let events = nodes[1].node.get_and_clear_pending_msg_events();
6465         assert_eq!(events.len(), 1);
6466         let (update_msg, commitment_signed) = match events[0] {
6467                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6468                         (update_fee.as_ref(), commitment_signed)
6469                 },
6470                 _ => panic!("Unexpected event"),
6471         };
6472
6473         nodes[2].node.handle_update_fee(&nodes[1].node.get_our_node_id(), update_msg.unwrap());
6474
6475         let mut chan_stat = get_channel_value_stat!(nodes[0], chan_0_1.2);
6476         let channel_reserve = chan_stat.channel_reserve_msat;
6477         let feerate = get_feerate!(nodes[0], chan_0_1.2);
6478
6479         // Send a payment which passes reserve checks but gets stuck in the holding cell.
6480         let feemsat = 239;
6481         let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
6482         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
6483         let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1) - total_routing_fee_msat;
6484         let payment_event = {
6485                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6486                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
6487                 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6488                 check_added_monitors!(nodes[0], 1);
6489
6490                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6491                 assert_eq!(events.len(), 1);
6492
6493                 SendEvent::from_event(events.remove(0))
6494         };
6495         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6496         check_added_monitors!(nodes[1], 0);
6497         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6498         expect_pending_htlcs_forwardable!(nodes[1]);
6499
6500         chan_stat = get_channel_value_stat!(nodes[1], chan_1_2.2);
6501         assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
6502
6503         // Flush the pending fee update.
6504         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
6505         let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6506         check_added_monitors!(nodes[2], 1);
6507         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &raa);
6508         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &commitment_signed);
6509         check_added_monitors!(nodes[1], 2);
6510
6511         // A final RAA message is generated to finalize the fee update.
6512         let events = nodes[1].node.get_and_clear_pending_msg_events();
6513         assert_eq!(events.len(), 1);
6514
6515         let raa_msg = match &events[0] {
6516                 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => {
6517                         msg.clone()
6518                 },
6519                 _ => panic!("Unexpected event"),
6520         };
6521
6522         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa_msg);
6523         check_added_monitors!(nodes[2], 1);
6524         assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
6525
6526         // nodes[1]'s ChannelManager will now signal that we have HTLC forwards to process.
6527         let process_htlc_forwards_event = nodes[1].node.get_and_clear_pending_events();
6528         assert_eq!(process_htlc_forwards_event.len(), 1);
6529         match &process_htlc_forwards_event[0] {
6530                 &Event::PendingHTLCsForwardable { .. } => {},
6531                 _ => panic!("Unexpected event"),
6532         }
6533
6534         // In response, we call ChannelManager's process_pending_htlc_forwards
6535         nodes[1].node.process_pending_htlc_forwards();
6536         check_added_monitors!(nodes[1], 1);
6537
6538         // This causes the HTLC to be failed backwards.
6539         let fail_event = nodes[1].node.get_and_clear_pending_msg_events();
6540         assert_eq!(fail_event.len(), 1);
6541         let (fail_msg, commitment_signed) = match &fail_event[0] {
6542                 &MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6543                         assert_eq!(updates.update_add_htlcs.len(), 0);
6544                         assert_eq!(updates.update_fulfill_htlcs.len(), 0);
6545                         assert_eq!(updates.update_fail_malformed_htlcs.len(), 0);
6546                         assert_eq!(updates.update_fail_htlcs.len(), 1);
6547                         (updates.update_fail_htlcs[0].clone(), updates.commitment_signed.clone())
6548                 },
6549                 _ => panic!("Unexpected event"),
6550         };
6551
6552         // Pass the failure messages back to nodes[0].
6553         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
6554         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6555
6556         // Complete the HTLC failure+removal process.
6557         let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6558         check_added_monitors!(nodes[0], 1);
6559         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6560         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
6561         check_added_monitors!(nodes[1], 2);
6562         let final_raa_event = nodes[1].node.get_and_clear_pending_msg_events();
6563         assert_eq!(final_raa_event.len(), 1);
6564         let raa = match &final_raa_event[0] {
6565                 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => msg.clone(),
6566                 _ => panic!("Unexpected event"),
6567         };
6568         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
6569         expect_payment_failure_chan_update!(nodes[0], chan_1_2.0.contents.short_channel_id, false);
6570         expect_payment_failed!(nodes[0], our_payment_hash, false);
6571         check_added_monitors!(nodes[0], 1);
6572 }
6573
6574 // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6575 // 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.
6576 //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.
6577
6578 #[test]
6579 fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
6580         //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
6581         let chanmon_cfgs = create_chanmon_cfgs(2);
6582         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6583         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6584         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6585         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6586
6587         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6588         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6589         let logger = test_utils::TestLogger::new();
6590         let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
6591         route.paths[0][0].fee_msat = 100;
6592
6593         unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
6594                 assert!(regex::Regex::new(r"Cannot send less than their minimum HTLC value \(\d+\)").unwrap().is_match(err)));
6595         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6596         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send less than their minimum HTLC value".to_string(), 1);
6597 }
6598
6599 #[test]
6600 fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
6601         //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6602         let chanmon_cfgs = create_chanmon_cfgs(2);
6603         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6604         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6605         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6606         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6607         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6608
6609         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6610         let logger = test_utils::TestLogger::new();
6611         let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
6612         route.paths[0][0].fee_msat = 0;
6613         unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
6614                 assert_eq!(err, "Cannot send 0-msat HTLC"));
6615
6616         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6617         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send 0-msat HTLC".to_string(), 1);
6618 }
6619
6620 #[test]
6621 fn test_update_add_htlc_bolt2_receiver_zero_value_msat() {
6622         //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6623         let chanmon_cfgs = create_chanmon_cfgs(2);
6624         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6625         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6626         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6627         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6628
6629         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6630         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6631         let logger = test_utils::TestLogger::new();
6632         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
6633         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6634         check_added_monitors!(nodes[0], 1);
6635         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6636         updates.update_add_htlcs[0].amount_msat = 0;
6637
6638         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6639         nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote side tried to send a 0-msat HTLC".to_string(), 1);
6640         check_closed_broadcast!(nodes[1], true).unwrap();
6641         check_added_monitors!(nodes[1], 1);
6642 }
6643
6644 #[test]
6645 fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
6646         //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
6647         //It is enforced when constructing a route.
6648         let chanmon_cfgs = create_chanmon_cfgs(2);
6649         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6650         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6651         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6652         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known());
6653         let logger = test_utils::TestLogger::new();
6654
6655         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6656
6657         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6658         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000000, 500000001, &logger).unwrap();
6659         unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::RouteError { ref err },
6660                 assert_eq!(err, &"Channel CLTV overflowed?"));
6661 }
6662
6663 #[test]
6664 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
6665         //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.
6666         //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
6667         //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6668         let chanmon_cfgs = create_chanmon_cfgs(2);
6669         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6670         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6671         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6672         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known());
6673         let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().counterparty_max_accepted_htlcs as u64;
6674
6675         let logger = test_utils::TestLogger::new();
6676         for i in 0..max_accepted_htlcs {
6677                 let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6678                 let payment_event = {
6679                         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6680                         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
6681                         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6682                         check_added_monitors!(nodes[0], 1);
6683
6684                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6685                         assert_eq!(events.len(), 1);
6686                         if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
6687                                 assert_eq!(htlcs[0].htlc_id, i);
6688                         } else {
6689                                 assert!(false);
6690                         }
6691                         SendEvent::from_event(events.remove(0))
6692                 };
6693                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6694                 check_added_monitors!(nodes[1], 0);
6695                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6696
6697                 expect_pending_htlcs_forwardable!(nodes[1]);
6698                 expect_payment_received!(nodes[1], our_payment_hash, our_payment_secret, 100000);
6699         }
6700         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6701         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6702         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
6703         unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
6704                 assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err)));
6705
6706         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6707         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot push more than their max accepted HTLCs".to_string(), 1);
6708 }
6709
6710 #[test]
6711 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
6712         //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.
6713         let chanmon_cfgs = create_chanmon_cfgs(2);
6714         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6715         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6716         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6717         let channel_value = 100000;
6718         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0, InitFeatures::known(), InitFeatures::known());
6719         let max_in_flight = get_channel_value_stat!(nodes[0], chan.2).counterparty_max_htlc_value_in_flight_msat;
6720
6721         send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
6722
6723         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6724         // Manually create a route over our max in flight (which our router normally automatically
6725         // limits us to.
6726         let route = Route { paths: vec![vec![RouteHop {
6727            pubkey: nodes[1].node.get_our_node_id(), node_features: NodeFeatures::known(), channel_features: ChannelFeatures::known(),
6728            short_channel_id: nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(),
6729            fee_msat: max_in_flight + 1, cltv_expiry_delta: TEST_FINAL_CLTV
6730         }]] };
6731         unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err },
6732                 assert!(regex::Regex::new(r"Cannot send value that would put us over the max HTLC value in flight our peer will accept \(\d+\)").unwrap().is_match(err)));
6733
6734         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6735         nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put us over the max HTLC value in flight our peer will accept".to_string(), 1);
6736
6737         send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
6738 }
6739
6740 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
6741 #[test]
6742 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
6743         //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
6744         let chanmon_cfgs = create_chanmon_cfgs(2);
6745         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6746         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6747         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6748         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6749         let htlc_minimum_msat: u64;
6750         {
6751                 let chan_lock = nodes[0].node.channel_state.lock().unwrap();
6752                 let channel = chan_lock.by_id.get(&chan.2).unwrap();
6753                 htlc_minimum_msat = channel.get_holder_htlc_minimum_msat();
6754         }
6755
6756         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6757         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6758         let logger = test_utils::TestLogger::new();
6759         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV, &logger).unwrap();
6760         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6761         check_added_monitors!(nodes[0], 1);
6762         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6763         updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
6764         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6765         assert!(nodes[1].node.list_channels().is_empty());
6766         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6767         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()));
6768         check_added_monitors!(nodes[1], 1);
6769 }
6770
6771 #[test]
6772 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
6773         //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
6774         let chanmon_cfgs = create_chanmon_cfgs(2);
6775         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6776         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6777         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6778         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6779         let logger = test_utils::TestLogger::new();
6780
6781         let chan_stat = get_channel_value_stat!(nodes[0], chan.2);
6782         let channel_reserve = chan_stat.channel_reserve_msat;
6783         let feerate = get_feerate!(nodes[0], chan.2);
6784         // The 2* and +1 are for the fee spike reserve.
6785         let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1);
6786
6787         let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
6788         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6789         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6790         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], max_can_send, TEST_FINAL_CLTV, &logger).unwrap();
6791         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6792         check_added_monitors!(nodes[0], 1);
6793         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6794
6795         // Even though channel-initiator senders are required to respect the fee_spike_reserve,
6796         // at this time channel-initiatee receivers are not required to enforce that senders
6797         // respect the fee_spike_reserve.
6798         updates.update_add_htlcs[0].amount_msat = max_can_send + commit_tx_fee_outbound + 1;
6799         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6800
6801         assert!(nodes[1].node.list_channels().is_empty());
6802         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6803         assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
6804         check_added_monitors!(nodes[1], 1);
6805 }
6806
6807 #[test]
6808 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
6809         //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
6810         //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
6811         let chanmon_cfgs = create_chanmon_cfgs(2);
6812         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6813         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6814         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6815         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6816         let logger = test_utils::TestLogger::new();
6817
6818         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6819         let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
6820
6821         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6822         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 3999999, TEST_FINAL_CLTV, &logger).unwrap();
6823
6824         let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
6825         let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
6826         let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0], 3999999, &Some(our_payment_secret), cur_height, &None).unwrap();
6827         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash);
6828
6829         let mut msg = msgs::UpdateAddHTLC {
6830                 channel_id: chan.2,
6831                 htlc_id: 0,
6832                 amount_msat: 1000,
6833                 payment_hash: our_payment_hash,
6834                 cltv_expiry: htlc_cltv,
6835                 onion_routing_packet: onion_packet.clone(),
6836         };
6837
6838         for i in 0..super::channel::OUR_MAX_HTLCS {
6839                 msg.htlc_id = i as u64;
6840                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6841         }
6842         msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
6843         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6844
6845         assert!(nodes[1].node.list_channels().is_empty());
6846         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6847         assert!(regex::Regex::new(r"Remote tried to push more than our max accepted HTLCs \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6848         check_added_monitors!(nodes[1], 1);
6849 }
6850
6851 #[test]
6852 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
6853         //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
6854         let chanmon_cfgs = create_chanmon_cfgs(2);
6855         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6856         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6857         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6858         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
6859         let logger = test_utils::TestLogger::new();
6860
6861         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6862         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6863         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
6864         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6865         check_added_monitors!(nodes[0], 1);
6866         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6867         updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], chan.2).counterparty_max_htlc_value_in_flight_msat + 1;
6868         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6869
6870         assert!(nodes[1].node.list_channels().is_empty());
6871         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6872         assert!(regex::Regex::new("Remote HTLC add would put them over our max HTLC value").unwrap().is_match(err_msg.data.as_str()));
6873         check_added_monitors!(nodes[1], 1);
6874 }
6875
6876 #[test]
6877 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
6878         //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
6879         let chanmon_cfgs = create_chanmon_cfgs(2);
6880         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6881         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6882         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6883         let logger = test_utils::TestLogger::new();
6884
6885         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
6886         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6887         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6888         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
6889         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6890         check_added_monitors!(nodes[0], 1);
6891         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6892         updates.update_add_htlcs[0].cltv_expiry = 500000000;
6893         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6894
6895         assert!(nodes[1].node.list_channels().is_empty());
6896         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6897         assert_eq!(err_msg.data,"Remote provided CLTV expiry in seconds instead of block height");
6898         check_added_monitors!(nodes[1], 1);
6899 }
6900
6901 #[test]
6902 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
6903         //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
6904         // We test this by first testing that that repeated HTLCs pass commitment signature checks
6905         // after disconnect and that non-sequential htlc_ids result in a channel failure.
6906         let chanmon_cfgs = create_chanmon_cfgs(2);
6907         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6908         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6909         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6910         let logger = test_utils::TestLogger::new();
6911
6912         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6913         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6914         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6915         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
6916         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6917         check_added_monitors!(nodes[0], 1);
6918         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6919         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6920
6921         //Disconnect and Reconnect
6922         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
6923         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
6924         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
6925         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
6926         assert_eq!(reestablish_1.len(), 1);
6927         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
6928         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
6929         assert_eq!(reestablish_2.len(), 1);
6930         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
6931         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
6932         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
6933         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
6934
6935         //Resend HTLC
6936         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6937         assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
6938         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
6939         check_added_monitors!(nodes[1], 1);
6940         let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6941
6942         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6943
6944         assert!(nodes[1].node.list_channels().is_empty());
6945         let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6946         assert!(regex::Regex::new(r"Remote skipped HTLC ID \(skipped ID: \d+\)").unwrap().is_match(err_msg.data.as_str()));
6947         check_added_monitors!(nodes[1], 1);
6948 }
6949
6950 #[test]
6951 fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
6952         //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.
6953
6954         let chanmon_cfgs = create_chanmon_cfgs(2);
6955         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6956         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6957         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6958         let logger = test_utils::TestLogger::new();
6959         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6960         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6961         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6962         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
6963         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6964
6965         check_added_monitors!(nodes[0], 1);
6966         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6967         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6968
6969         let update_msg = msgs::UpdateFulfillHTLC{
6970                 channel_id: chan.2,
6971                 htlc_id: 0,
6972                 payment_preimage: our_payment_preimage,
6973         };
6974
6975         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6976
6977         assert!(nodes[0].node.list_channels().is_empty());
6978         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6979         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()));
6980         check_added_monitors!(nodes[0], 1);
6981 }
6982
6983 #[test]
6984 fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
6985         //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.
6986
6987         let chanmon_cfgs = create_chanmon_cfgs(2);
6988         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6989         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6990         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6991         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
6992         let logger = test_utils::TestLogger::new();
6993
6994         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
6995         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
6996         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
6997         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
6998         check_added_monitors!(nodes[0], 1);
6999         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
7000         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
7001
7002         let update_msg = msgs::UpdateFailHTLC{
7003                 channel_id: chan.2,
7004                 htlc_id: 0,
7005                 reason: msgs::OnionErrorPacket { data: Vec::new()},
7006         };
7007
7008         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
7009
7010         assert!(nodes[0].node.list_channels().is_empty());
7011         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
7012         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()));
7013         check_added_monitors!(nodes[0], 1);
7014 }
7015
7016 #[test]
7017 fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
7018         //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.
7019
7020         let chanmon_cfgs = create_chanmon_cfgs(2);
7021         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7022         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7023         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7024         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7025         let logger = test_utils::TestLogger::new();
7026
7027         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
7028         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
7029         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
7030         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7031         check_added_monitors!(nodes[0], 1);
7032         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
7033         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
7034         let update_msg = msgs::UpdateFailMalformedHTLC{
7035                 channel_id: chan.2,
7036                 htlc_id: 0,
7037                 sha256_of_onion: [1; 32],
7038                 failure_code: 0x8000,
7039         };
7040
7041         nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
7042
7043         assert!(nodes[0].node.list_channels().is_empty());
7044         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
7045         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()));
7046         check_added_monitors!(nodes[0], 1);
7047 }
7048
7049 #[test]
7050 fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
7051         //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
7052
7053         let chanmon_cfgs = create_chanmon_cfgs(2);
7054         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7055         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7056         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7057         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7058
7059         let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
7060
7061         nodes[1].node.claim_funds(our_payment_preimage);
7062         check_added_monitors!(nodes[1], 1);
7063
7064         let events = nodes[1].node.get_and_clear_pending_msg_events();
7065         assert_eq!(events.len(), 1);
7066         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
7067                 match events[0] {
7068                         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, .. } } => {
7069                                 assert!(update_add_htlcs.is_empty());
7070                                 assert_eq!(update_fulfill_htlcs.len(), 1);
7071                                 assert!(update_fail_htlcs.is_empty());
7072                                 assert!(update_fail_malformed_htlcs.is_empty());
7073                                 assert!(update_fee.is_none());
7074                                 update_fulfill_htlcs[0].clone()
7075                         },
7076                         _ => panic!("Unexpected event"),
7077                 }
7078         };
7079
7080         update_fulfill_msg.htlc_id = 1;
7081
7082         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
7083
7084         assert!(nodes[0].node.list_channels().is_empty());
7085         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
7086         assert_eq!(err_msg.data, "Remote tried to fulfill/fail an HTLC we couldn't find");
7087         check_added_monitors!(nodes[0], 1);
7088 }
7089
7090 #[test]
7091 fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
7092         //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.
7093
7094         let chanmon_cfgs = create_chanmon_cfgs(2);
7095         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7096         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7097         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7098         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7099
7100         let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
7101
7102         nodes[1].node.claim_funds(our_payment_preimage);
7103         check_added_monitors!(nodes[1], 1);
7104
7105         let events = nodes[1].node.get_and_clear_pending_msg_events();
7106         assert_eq!(events.len(), 1);
7107         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
7108                 match events[0] {
7109                         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, .. } } => {
7110                                 assert!(update_add_htlcs.is_empty());
7111                                 assert_eq!(update_fulfill_htlcs.len(), 1);
7112                                 assert!(update_fail_htlcs.is_empty());
7113                                 assert!(update_fail_malformed_htlcs.is_empty());
7114                                 assert!(update_fee.is_none());
7115                                 update_fulfill_htlcs[0].clone()
7116                         },
7117                         _ => panic!("Unexpected event"),
7118                 }
7119         };
7120
7121         update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
7122
7123         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
7124
7125         assert!(nodes[0].node.list_channels().is_empty());
7126         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
7127         assert!(regex::Regex::new(r"Remote tried to fulfill HTLC \(\d+\) with an incorrect preimage").unwrap().is_match(err_msg.data.as_str()));
7128         check_added_monitors!(nodes[0], 1);
7129 }
7130
7131 #[test]
7132 fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
7133         //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.
7134
7135         let chanmon_cfgs = create_chanmon_cfgs(2);
7136         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7137         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7138         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7139         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7140         let logger = test_utils::TestLogger::new();
7141
7142         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[1]);
7143         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
7144         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1000000, TEST_FINAL_CLTV, &logger).unwrap();
7145         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7146         check_added_monitors!(nodes[0], 1);
7147
7148         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
7149         updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
7150
7151         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
7152         check_added_monitors!(nodes[1], 0);
7153         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
7154
7155         let events = nodes[1].node.get_and_clear_pending_msg_events();
7156
7157         let mut update_msg: msgs::UpdateFailMalformedHTLC = {
7158                 match events[0] {
7159                         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, .. } } => {
7160                                 assert!(update_add_htlcs.is_empty());
7161                                 assert!(update_fulfill_htlcs.is_empty());
7162                                 assert!(update_fail_htlcs.is_empty());
7163                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
7164                                 assert!(update_fee.is_none());
7165                                 update_fail_malformed_htlcs[0].clone()
7166                         },
7167                         _ => panic!("Unexpected event"),
7168                 }
7169         };
7170         update_msg.failure_code &= !0x8000;
7171         nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
7172
7173         assert!(nodes[0].node.list_channels().is_empty());
7174         let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
7175         assert_eq!(err_msg.data, "Got update_fail_malformed_htlc with BADONION not set");
7176         check_added_monitors!(nodes[0], 1);
7177 }
7178
7179 #[test]
7180 fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
7181         //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
7182         //    * 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.
7183
7184         let chanmon_cfgs = create_chanmon_cfgs(3);
7185         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7186         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7187         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7188         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7189         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7190         let logger = test_utils::TestLogger::new();
7191
7192         let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
7193
7194         //First hop
7195         let mut payment_event = {
7196                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
7197                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100000, TEST_FINAL_CLTV, &logger).unwrap();
7198                 nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7199                 check_added_monitors!(nodes[0], 1);
7200                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
7201                 assert_eq!(events.len(), 1);
7202                 SendEvent::from_event(events.remove(0))
7203         };
7204         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7205         check_added_monitors!(nodes[1], 0);
7206         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7207         expect_pending_htlcs_forwardable!(nodes[1]);
7208         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
7209         assert_eq!(events_2.len(), 1);
7210         check_added_monitors!(nodes[1], 1);
7211         payment_event = SendEvent::from_event(events_2.remove(0));
7212         assert_eq!(payment_event.msgs.len(), 1);
7213
7214         //Second Hop
7215         payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
7216         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
7217         check_added_monitors!(nodes[2], 0);
7218         commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
7219
7220         let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
7221         assert_eq!(events_3.len(), 1);
7222         let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
7223                 match events_3[0] {
7224                         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 } } => {
7225                                 assert!(update_add_htlcs.is_empty());
7226                                 assert!(update_fulfill_htlcs.is_empty());
7227                                 assert!(update_fail_htlcs.is_empty());
7228                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
7229                                 assert!(update_fee.is_none());
7230                                 (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
7231                         },
7232                         _ => panic!("Unexpected event"),
7233                 }
7234         };
7235
7236         nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0);
7237
7238         check_added_monitors!(nodes[1], 0);
7239         commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
7240         expect_pending_htlcs_forwardable!(nodes[1]);
7241         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
7242         assert_eq!(events_4.len(), 1);
7243
7244         //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
7245         match events_4[0] {
7246                 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, .. } } => {
7247                         assert!(update_add_htlcs.is_empty());
7248                         assert!(update_fulfill_htlcs.is_empty());
7249                         assert_eq!(update_fail_htlcs.len(), 1);
7250                         assert!(update_fail_malformed_htlcs.is_empty());
7251                         assert!(update_fee.is_none());
7252                 },
7253                 _ => panic!("Unexpected event"),
7254         };
7255
7256         check_added_monitors!(nodes[1], 1);
7257 }
7258
7259 fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
7260         // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
7261         // 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
7262         // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
7263
7264         let mut chanmon_cfgs = create_chanmon_cfgs(2);
7265         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
7266         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7267         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7268         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7269         let chan =create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7270
7271         let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().holder_dust_limit_satoshis;
7272
7273         // We route 2 dust-HTLCs between A and B
7274         let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7275         let (_, payment_hash_2, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7276         route_payment(&nodes[0], &[&nodes[1]], 1000000);
7277
7278         // Cache one local commitment tx as previous
7279         let as_prev_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7280
7281         // Fail one HTLC to prune it in the will-be-latest-local commitment tx
7282         assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
7283         check_added_monitors!(nodes[1], 0);
7284         expect_pending_htlcs_forwardable!(nodes[1]);
7285         check_added_monitors!(nodes[1], 1);
7286
7287         let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
7288         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]);
7289         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed);
7290         check_added_monitors!(nodes[0], 1);
7291
7292         // Cache one local commitment tx as lastest
7293         let as_last_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7294
7295         let events = nodes[0].node.get_and_clear_pending_msg_events();
7296         match events[0] {
7297                 MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
7298                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7299                 },
7300                 _ => panic!("Unexpected event"),
7301         }
7302         match events[1] {
7303                 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
7304                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7305                 },
7306                 _ => panic!("Unexpected event"),
7307         }
7308
7309         assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
7310         // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
7311         if announce_latest {
7312                 mine_transaction(&nodes[0], &as_last_commitment_tx[0]);
7313         } else {
7314                 mine_transaction(&nodes[0], &as_prev_commitment_tx[0]);
7315         }
7316
7317         check_closed_broadcast!(nodes[0], true);
7318         check_added_monitors!(nodes[0], 1);
7319
7320         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7321         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7322         let events = nodes[0].node.get_and_clear_pending_events();
7323         // Only 2 PaymentFailed events should show up, over-dust HTLC has to be failed by timeout tx
7324         assert_eq!(events.len(), 2);
7325         let mut first_failed = false;
7326         for event in events {
7327                 match event {
7328                         Event::PaymentFailed { payment_hash, .. } => {
7329                                 if payment_hash == payment_hash_1 {
7330                                         assert!(!first_failed);
7331                                         first_failed = true;
7332                                 } else {
7333                                         assert_eq!(payment_hash, payment_hash_2);
7334                                 }
7335                         }
7336                         _ => panic!("Unexpected event"),
7337                 }
7338         }
7339 }
7340
7341 #[test]
7342 fn test_failure_delay_dust_htlc_local_commitment() {
7343         do_test_failure_delay_dust_htlc_local_commitment(true);
7344         do_test_failure_delay_dust_htlc_local_commitment(false);
7345 }
7346
7347 fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
7348         // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
7349         // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
7350         // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
7351         // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
7352         // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
7353         // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
7354
7355         let chanmon_cfgs = create_chanmon_cfgs(3);
7356         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7357         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7358         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7359         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7360
7361         let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().holder_dust_limit_satoshis;
7362
7363         let (_payment_preimage_1, dust_hash, _payment_secret_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7364         let (_payment_preimage_2, non_dust_hash, _payment_secret_2) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7365
7366         let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7367         let bs_commitment_tx = get_local_commitment_txn!(nodes[1], chan.2);
7368
7369         // We revoked bs_commitment_tx
7370         if revoked {
7371                 let (payment_preimage_3, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7372                 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
7373         }
7374
7375         let mut timeout_tx = Vec::new();
7376         if local {
7377                 // We fail dust-HTLC 1 by broadcast of local commitment tx
7378                 mine_transaction(&nodes[0], &as_commitment_tx[0]);
7379                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7380                 expect_payment_failed!(nodes[0], dust_hash, true);
7381
7382                 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS - ANTI_REORG_DELAY);
7383                 check_closed_broadcast!(nodes[0], true);
7384                 check_added_monitors!(nodes[0], 1);
7385                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7386                 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[1].clone());
7387                 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7388                 // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
7389                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7390                 mine_transaction(&nodes[0], &timeout_tx[0]);
7391                 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7392                 expect_payment_failed!(nodes[0], non_dust_hash, true);
7393         } else {
7394                 // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
7395                 mine_transaction(&nodes[0], &bs_commitment_tx[0]);
7396                 check_closed_broadcast!(nodes[0], true);
7397                 check_added_monitors!(nodes[0], 1);
7398                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7399                 connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
7400                 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[1].clone());
7401                 if !revoked {
7402                         expect_payment_failed!(nodes[0], dust_hash, true);
7403                         assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7404                         // We fail non-dust-HTLC 2 by broadcast of local timeout tx on remote commitment tx
7405                         mine_transaction(&nodes[0], &timeout_tx[0]);
7406                         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7407                         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7408                         expect_payment_failed!(nodes[0], non_dust_hash, true);
7409                 } else {
7410                         // If revoked, both dust & non-dust HTLCs should have been failed after ANTI_REORG_DELAY confs of revoked
7411                         // commitment tx
7412                         let events = nodes[0].node.get_and_clear_pending_events();
7413                         assert_eq!(events.len(), 2);
7414                         let first;
7415                         match events[0] {
7416                                 Event::PaymentFailed { payment_hash, .. } => {
7417                                         if payment_hash == dust_hash { first = true; }
7418                                         else { first = false; }
7419                                 },
7420                                 _ => panic!("Unexpected event"),
7421                         }
7422                         match events[1] {
7423                                 Event::PaymentFailed { payment_hash, .. } => {
7424                                         if first { assert_eq!(payment_hash, non_dust_hash); }
7425                                         else { assert_eq!(payment_hash, dust_hash); }
7426                                 },
7427                                 _ => panic!("Unexpected event"),
7428                         }
7429                 }
7430         }
7431 }
7432
7433 #[test]
7434 fn test_sweep_outbound_htlc_failure_update() {
7435         do_test_sweep_outbound_htlc_failure_update(false, true);
7436         do_test_sweep_outbound_htlc_failure_update(false, false);
7437         do_test_sweep_outbound_htlc_failure_update(true, false);
7438 }
7439
7440 #[test]
7441 fn test_upfront_shutdown_script() {
7442         // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
7443         // enforce it at shutdown message
7444
7445         let mut config = UserConfig::default();
7446         config.channel_options.announced_channel = true;
7447         config.peer_channel_config_limits.force_announced_channel_preference = false;
7448         config.channel_options.commit_upfront_shutdown_pubkey = false;
7449         let user_cfgs = [None, Some(config), None];
7450         let chanmon_cfgs = create_chanmon_cfgs(3);
7451         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7452         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7453         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7454
7455         // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
7456         let flags = InitFeatures::known();
7457         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
7458         nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7459         let mut node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
7460         node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
7461         // Test we enforce upfront_scriptpbukey if by providing a diffrent one at closing that  we disconnect peer
7462         nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7463     assert!(regex::Regex::new(r"Got shutdown request with a scriptpubkey \([A-Fa-f0-9]+\) which did not match their previous scriptpubkey.").unwrap().is_match(check_closed_broadcast!(nodes[2], true).unwrap().data.as_str()));
7464         check_added_monitors!(nodes[2], 1);
7465
7466         // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
7467         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
7468         nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7469         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
7470         // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
7471         nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7472         let events = nodes[2].node.get_and_clear_pending_msg_events();
7473         assert_eq!(events.len(), 1);
7474         match events[0] {
7475                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
7476                 _ => panic!("Unexpected event"),
7477         }
7478
7479         // We test that if case of peer non-signaling we don't enforce committed script at channel opening
7480         let flags_no = InitFeatures::known().clear_upfront_shutdown_script();
7481         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags_no, flags.clone());
7482         nodes[0].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7483         let node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
7484         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
7485         check_added_monitors!(nodes[1], 1);
7486         let events = nodes[1].node.get_and_clear_pending_msg_events();
7487         assert_eq!(events.len(), 1);
7488         match events[0] {
7489                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
7490                 _ => panic!("Unexpected event"),
7491         }
7492
7493         // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
7494         // channel smoothly, opt-out is from channel initiator here
7495         let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000, flags.clone(), flags.clone());
7496         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7497         check_added_monitors!(nodes[1], 1);
7498         let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7499         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7500         let events = nodes[0].node.get_and_clear_pending_msg_events();
7501         assert_eq!(events.len(), 1);
7502         match events[0] {
7503                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7504                 _ => panic!("Unexpected event"),
7505         }
7506
7507         //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
7508         //// channel smoothly
7509         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags.clone(), flags.clone());
7510         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7511         check_added_monitors!(nodes[1], 1);
7512         let node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7513         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7514         let events = nodes[0].node.get_and_clear_pending_msg_events();
7515         assert_eq!(events.len(), 2);
7516         match events[0] {
7517                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7518                 _ => panic!("Unexpected event"),
7519         }
7520         match events[1] {
7521                 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7522                 _ => panic!("Unexpected event"),
7523         }
7524 }
7525
7526 #[test]
7527 fn test_unsupported_anysegwit_upfront_shutdown_script() {
7528         let chanmon_cfgs = create_chanmon_cfgs(2);
7529         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7530         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7531         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7532
7533         // Use a non-v0 segwit script supported by option_shutdown_anysegwit
7534         let node_features = InitFeatures::known().clear_shutdown_anysegwit();
7535         let anysegwit_shutdown_script = Builder::new()
7536                 .push_int(16)
7537                 .push_slice(&[0, 40])
7538                 .into_script();
7539
7540         // Check script when handling an open_channel message
7541         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7542         let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7543         open_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
7544         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), node_features.clone(), &open_channel);
7545
7546         let events = nodes[1].node.get_and_clear_pending_msg_events();
7547         assert_eq!(events.len(), 1);
7548         match events[0] {
7549                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7550                         assert_eq!(node_id, nodes[0].node.get_our_node_id());
7551                         assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
7552                 },
7553                 _ => panic!("Unexpected event"),
7554         }
7555
7556         // Check script when handling an accept_channel message
7557         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7558         let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7559         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
7560         let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7561         accept_channel.shutdown_scriptpubkey = Present(anysegwit_shutdown_script.clone());
7562         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), node_features, &accept_channel);
7563
7564         let events = nodes[0].node.get_and_clear_pending_msg_events();
7565         assert_eq!(events.len(), 1);
7566         match events[0] {
7567                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7568                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7569                         assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
7570                 },
7571                 _ => panic!("Unexpected event"),
7572         }
7573 }
7574
7575 #[test]
7576 fn test_invalid_upfront_shutdown_script() {
7577         let chanmon_cfgs = create_chanmon_cfgs(2);
7578         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7579         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7580         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7581
7582         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
7583
7584         // Use a segwit v0 script with an unsupported witness program
7585         let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7586         open_channel.shutdown_scriptpubkey = Present(Builder::new().push_int(0)
7587                 .push_slice(&[0, 0])
7588                 .into_script());
7589         nodes[0].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
7590
7591         let events = nodes[0].node.get_and_clear_pending_msg_events();
7592         assert_eq!(events.len(), 1);
7593         match events[0] {
7594                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7595                         assert_eq!(node_id, nodes[0].node.get_our_node_id());
7596                         assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided an unacceptable scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
7597                 },
7598                 _ => panic!("Unexpected event"),
7599         }
7600 }
7601
7602 #[test]
7603 fn test_segwit_v0_shutdown_script() {
7604         let mut config = UserConfig::default();
7605         config.channel_options.announced_channel = true;
7606         config.peer_channel_config_limits.force_announced_channel_preference = false;
7607         config.channel_options.commit_upfront_shutdown_pubkey = false;
7608         let user_cfgs = [None, Some(config), None];
7609         let chanmon_cfgs = create_chanmon_cfgs(3);
7610         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7611         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7612         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7613
7614         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7615         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7616         check_added_monitors!(nodes[1], 1);
7617
7618         // Use a segwit v0 script supported even without option_shutdown_anysegwit
7619         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7620         node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7621                 .push_slice(&[0; 20])
7622                 .into_script();
7623         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7624
7625         let events = nodes[0].node.get_and_clear_pending_msg_events();
7626         assert_eq!(events.len(), 2);
7627         match events[0] {
7628                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7629                 _ => panic!("Unexpected event"),
7630         }
7631         match events[1] {
7632                 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7633                 _ => panic!("Unexpected event"),
7634         }
7635 }
7636
7637 #[test]
7638 fn test_anysegwit_shutdown_script() {
7639         let mut config = UserConfig::default();
7640         config.channel_options.announced_channel = true;
7641         config.peer_channel_config_limits.force_announced_channel_preference = false;
7642         config.channel_options.commit_upfront_shutdown_pubkey = false;
7643         let user_cfgs = [None, Some(config), None];
7644         let chanmon_cfgs = create_chanmon_cfgs(3);
7645         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7646         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7647         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7648
7649         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7650         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7651         check_added_monitors!(nodes[1], 1);
7652
7653         // Use a non-v0 segwit script supported by option_shutdown_anysegwit
7654         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7655         node_0_shutdown.scriptpubkey = Builder::new().push_int(16)
7656                 .push_slice(&[0, 0])
7657                 .into_script();
7658         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7659
7660         let events = nodes[0].node.get_and_clear_pending_msg_events();
7661         assert_eq!(events.len(), 2);
7662         match events[0] {
7663                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7664                 _ => panic!("Unexpected event"),
7665         }
7666         match events[1] {
7667                 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
7668                 _ => panic!("Unexpected event"),
7669         }
7670 }
7671
7672 #[test]
7673 fn test_unsupported_anysegwit_shutdown_script() {
7674         let mut config = UserConfig::default();
7675         config.channel_options.announced_channel = true;
7676         config.peer_channel_config_limits.force_announced_channel_preference = false;
7677         config.channel_options.commit_upfront_shutdown_pubkey = false;
7678         let user_cfgs = [None, Some(config), None];
7679         let chanmon_cfgs = create_chanmon_cfgs(3);
7680         let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7681         node_cfgs[0].features = InitFeatures::known().clear_shutdown_anysegwit();
7682         node_cfgs[1].features = InitFeatures::known().clear_shutdown_anysegwit();
7683         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7684         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7685
7686         // Check that using an unsupported shutdown script fails and a supported one succeeds.
7687         let supported_shutdown_script = chanmon_cfgs[1].keys_manager.get_shutdown_scriptpubkey();
7688         let unsupported_shutdown_script =
7689                 ShutdownScript::new_witness_program(NonZeroU8::new(16).unwrap(), &[0, 40]).unwrap();
7690         chanmon_cfgs[1].keys_manager
7691                 .expect(OnGetShutdownScriptpubkey { returns: unsupported_shutdown_script.clone() })
7692                 .expect(OnGetShutdownScriptpubkey { returns: supported_shutdown_script });
7693
7694         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, node_cfgs[0].features.clone(), node_cfgs[1].features.clone());
7695         match nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()) {
7696                 Err(APIError::APIMisuseError { err }) => assert_eq!(err, "Provided a scriptpubkey format not accepted by peer. script: (60020028)"),
7697                 Err(e) => panic!("Unexpected error: {:?}", e),
7698                 Ok(_) => panic!("Expected error"),
7699         }
7700         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7701         check_added_monitors!(nodes[1], 1);
7702
7703         // Use a non-v0 segwit script unsupported without option_shutdown_anysegwit
7704         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7705         node_0_shutdown.scriptpubkey = unsupported_shutdown_script.into_inner();
7706         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_cfgs[1].features, &node_0_shutdown);
7707
7708         let events = nodes[0].node.get_and_clear_pending_msg_events();
7709         assert_eq!(events.len(), 2);
7710         match events[1] {
7711                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7712                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7713                         assert_eq!(msg.data, "Got a nonstandard scriptpubkey (60020028) from remote peer".to_owned());
7714                 },
7715                 _ => panic!("Unexpected event"),
7716         }
7717         check_added_monitors!(nodes[0], 1);
7718 }
7719
7720 #[test]
7721 fn test_invalid_shutdown_script() {
7722         let mut config = UserConfig::default();
7723         config.channel_options.announced_channel = true;
7724         config.peer_channel_config_limits.force_announced_channel_preference = false;
7725         config.channel_options.commit_upfront_shutdown_pubkey = false;
7726         let user_cfgs = [None, Some(config), None];
7727         let chanmon_cfgs = create_chanmon_cfgs(3);
7728         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7729         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &user_cfgs);
7730         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7731
7732         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7733         nodes[1].node.close_channel(&OutPoint { txid: chan.3.txid(), index: 0 }.to_channel_id()).unwrap();
7734         check_added_monitors!(nodes[1], 1);
7735
7736         // Use a segwit v0 script with an unsupported witness program
7737         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
7738         node_0_shutdown.scriptpubkey = Builder::new().push_int(0)
7739                 .push_slice(&[0, 0])
7740                 .into_script();
7741         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
7742
7743         let events = nodes[0].node.get_and_clear_pending_msg_events();
7744         assert_eq!(events.len(), 2);
7745         match events[1] {
7746                 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
7747                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
7748                         assert_eq!(msg.data, "Got a nonstandard scriptpubkey (00020000) from remote peer".to_owned())
7749                 },
7750                 _ => panic!("Unexpected event"),
7751         }
7752         check_added_monitors!(nodes[0], 1);
7753 }
7754
7755 #[test]
7756 fn test_user_configurable_csv_delay() {
7757         // We test our channel constructors yield errors when we pass them absurd csv delay
7758
7759         let mut low_our_to_self_config = UserConfig::default();
7760         low_our_to_self_config.own_channel_config.our_to_self_delay = 6;
7761         let mut high_their_to_self_config = UserConfig::default();
7762         high_their_to_self_config.peer_channel_config_limits.their_to_self_delay = 100;
7763         let user_cfgs = [Some(high_their_to_self_config.clone()), None];
7764         let chanmon_cfgs = create_chanmon_cfgs(2);
7765         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7766         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
7767         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7768
7769         // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
7770         if let Err(error) = Channel::new_outbound(&&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &InitFeatures::known(), 1000000, 1000000, 0, &low_our_to_self_config) {
7771                 match error {
7772                         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())); },
7773                         _ => panic!("Unexpected event"),
7774                 }
7775         } else { assert!(false) }
7776
7777         // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_from_req()
7778         nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7779         let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7780         open_channel.to_self_delay = 200;
7781         if let Err(error) = Channel::new_from_req(&&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &InitFeatures::known(), &open_channel, 0, &low_our_to_self_config) {
7782                 match error {
7783                         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()));  },
7784                         _ => panic!("Unexpected event"),
7785                 }
7786         } else { assert!(false); }
7787
7788         // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
7789         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7790         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
7791         let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7792         accept_channel.to_self_delay = 200;
7793         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
7794         if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[0] {
7795                 match action {
7796                         &ErrorAction::SendErrorMessage { ref msg } => {
7797                                 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()));
7798                         },
7799                         _ => { assert!(false); }
7800                 }
7801         } else { assert!(false); }
7802
7803         // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Channel::new_from_req()
7804         nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7805         let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7806         open_channel.to_self_delay = 200;
7807         if let Err(error) = Channel::new_from_req(&&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &InitFeatures::known(), &open_channel, 0, &high_their_to_self_config) {
7808                 match error {
7809                         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())); },
7810                         _ => panic!("Unexpected event"),
7811                 }
7812         } else { assert!(false); }
7813 }
7814
7815 #[test]
7816 fn test_data_loss_protect() {
7817         // We want to be sure that :
7818         // * we don't broadcast our Local Commitment Tx in case of fallen behind
7819         //   (but this is not quite true - we broadcast during Drop because chanmon is out of sync with chanmgr)
7820         // * we close channel in case of detecting other being fallen behind
7821         // * we are able to claim our own outputs thanks to to_remote being static
7822         // TODO: this test is incomplete and the data_loss_protect implementation is incomplete - see issue #775
7823         let persister;
7824         let logger;
7825         let fee_estimator;
7826         let tx_broadcaster;
7827         let chain_source;
7828         let mut chanmon_cfgs = create_chanmon_cfgs(2);
7829         // We broadcast during Drop because chanmon is out of sync with chanmgr, which would cause a panic
7830         // during signing due to revoked tx
7831         chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
7832         let keys_manager = &chanmon_cfgs[0].keys_manager;
7833         let monitor;
7834         let node_state_0;
7835         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7836         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7837         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7838
7839         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known());
7840
7841         // Cache node A state before any channel update
7842         let previous_node_state = nodes[0].node.encode();
7843         let mut previous_chain_monitor_state = test_utils::TestVecWriter(Vec::new());
7844         nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap().iter().next().unwrap().1.write(&mut previous_chain_monitor_state).unwrap();
7845
7846         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
7847         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
7848
7849         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
7850         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
7851
7852         // Restore node A from previous state
7853         logger = test_utils::TestLogger::with_id(format!("node {}", 0));
7854         let mut chain_monitor = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut io::Cursor::new(previous_chain_monitor_state.0), keys_manager).unwrap().1;
7855         chain_source = test_utils::TestChainSource::new(Network::Testnet);
7856         tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
7857         fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
7858         persister = test_utils::TestPersister::new();
7859         monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
7860         node_state_0 = {
7861                 let mut channel_monitors = HashMap::new();
7862                 channel_monitors.insert(OutPoint { txid: chan.3.txid(), index: 0 }, &mut chain_monitor);
7863                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut io::Cursor::new(previous_node_state), ChannelManagerReadArgs {
7864                         keys_manager: keys_manager,
7865                         fee_estimator: &fee_estimator,
7866                         chain_monitor: &monitor,
7867                         logger: &logger,
7868                         tx_broadcaster: &tx_broadcaster,
7869                         default_config: UserConfig::default(),
7870                         channel_monitors,
7871                 }).unwrap().1
7872         };
7873         nodes[0].node = &node_state_0;
7874         assert!(monitor.watch_channel(OutPoint { txid: chan.3.txid(), index: 0 }, chain_monitor).is_ok());
7875         nodes[0].chain_monitor = &monitor;
7876         nodes[0].chain_source = &chain_source;
7877
7878         check_added_monitors!(nodes[0], 1);
7879
7880         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
7881         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
7882
7883         let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
7884
7885         // Check we don't broadcast any transactions following learning of per_commitment_point from B
7886         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_0[0]);
7887         check_added_monitors!(nodes[0], 1);
7888
7889         {
7890                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
7891                 assert_eq!(node_txn.len(), 0);
7892         }
7893
7894         let mut reestablish_1 = Vec::with_capacity(1);
7895         for msg in nodes[0].node.get_and_clear_pending_msg_events() {
7896                 if let MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } = msg {
7897                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
7898                         reestablish_1.push(msg.clone());
7899                 } else if let MessageSendEvent::BroadcastChannelUpdate { .. } = msg {
7900                 } else if let MessageSendEvent::HandleError { ref action, .. } = msg {
7901                         match action {
7902                                 &ErrorAction::SendErrorMessage { ref msg } => {
7903                                         assert_eq!(msg.data, "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting");
7904                                 },
7905                                 _ => panic!("Unexpected event!"),
7906                         }
7907                 } else {
7908                         panic!("Unexpected event")
7909                 }
7910         }
7911
7912         // Check we close channel detecting A is fallen-behind
7913         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
7914         assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Peer attempted to reestablish channel with a very old local commitment transaction");
7915         check_added_monitors!(nodes[1], 1);
7916
7917
7918         // Check A is able to claim to_remote output
7919         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
7920         assert_eq!(node_txn.len(), 1);
7921         check_spends!(node_txn[0], chan.3);
7922         assert_eq!(node_txn[0].output.len(), 2);
7923         mine_transaction(&nodes[0], &node_txn[0]);
7924         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7925         let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
7926         assert_eq!(spend_txn.len(), 1);
7927         check_spends!(spend_txn[0], node_txn[0]);
7928 }
7929
7930 #[test]
7931 fn test_check_htlc_underpaying() {
7932         // Send payment through A -> B but A is maliciously
7933         // sending a probe payment (i.e less than expected value0
7934         // to B, B should refuse payment.
7935
7936         let chanmon_cfgs = create_chanmon_cfgs(2);
7937         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7938         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7939         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7940
7941         // Create some initial channels
7942         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
7943
7944         let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 10_000, TEST_FINAL_CLTV, nodes[0].logger).unwrap();
7945         let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
7946         let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, 0).unwrap();
7947         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
7948         check_added_monitors!(nodes[0], 1);
7949
7950         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
7951         assert_eq!(events.len(), 1);
7952         let mut payment_event = SendEvent::from_event(events.pop().unwrap());
7953         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7954         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7955
7956         // Note that we first have to wait a random delay before processing the receipt of the HTLC,
7957         // and then will wait a second random delay before failing the HTLC back:
7958         expect_pending_htlcs_forwardable!(nodes[1]);
7959         expect_pending_htlcs_forwardable!(nodes[1]);
7960
7961         // Node 3 is expecting payment of 100_000 but received 10_000,
7962         // it should fail htlc like we didn't know the preimage.
7963         nodes[1].node.process_pending_htlc_forwards();
7964
7965         let events = nodes[1].node.get_and_clear_pending_msg_events();
7966         assert_eq!(events.len(), 1);
7967         let (update_fail_htlc, commitment_signed) = match events[0] {
7968                 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 } } => {
7969                         assert!(update_add_htlcs.is_empty());
7970                         assert!(update_fulfill_htlcs.is_empty());
7971                         assert_eq!(update_fail_htlcs.len(), 1);
7972                         assert!(update_fail_malformed_htlcs.is_empty());
7973                         assert!(update_fee.is_none());
7974                         (update_fail_htlcs[0].clone(), commitment_signed)
7975                 },
7976                 _ => panic!("Unexpected event"),
7977         };
7978         check_added_monitors!(nodes[1], 1);
7979
7980         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlc);
7981         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
7982
7983         // 10_000 msat as u64, followed by a height of CHAN_CONFIRM_DEPTH as u32
7984         let mut expected_failure_data = byte_utils::be64_to_array(10_000).to_vec();
7985         expected_failure_data.extend_from_slice(&byte_utils::be32_to_array(CHAN_CONFIRM_DEPTH));
7986         expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000|15, &expected_failure_data[..]);
7987 }
7988
7989 #[test]
7990 fn test_announce_disable_channels() {
7991         // Create 2 channels between A and B. Disconnect B. Call timer_tick_occurred and check for generated
7992         // ChannelUpdate. Reconnect B, reestablish and check there is non-generated ChannelUpdate.
7993
7994         let chanmon_cfgs = create_chanmon_cfgs(2);
7995         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7996         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7997         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7998
7999         let short_id_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8000         let short_id_2 = create_announced_chan_between_nodes(&nodes, 1, 0, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8001         let short_id_3 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8002
8003         // Disconnect peers
8004         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
8005         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
8006
8007         nodes[0].node.timer_tick_occurred(); // Enabled -> DisabledStaged
8008         nodes[0].node.timer_tick_occurred(); // DisabledStaged -> Disabled
8009         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
8010         assert_eq!(msg_events.len(), 3);
8011         let mut chans_disabled: HashSet<u64> = [short_id_1, short_id_2, short_id_3].iter().map(|a| *a).collect();
8012         for e in msg_events {
8013                 match e {
8014                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
8015                                 assert_eq!(msg.contents.flags & (1<<1), 1<<1); // The "channel disabled" bit should be set
8016                                 // Check that each channel gets updated exactly once
8017                                 if !chans_disabled.remove(&msg.contents.short_channel_id) {
8018                                         panic!("Generated ChannelUpdate for wrong chan!");
8019                                 }
8020                         },
8021                         _ => panic!("Unexpected event"),
8022                 }
8023         }
8024         // Reconnect peers
8025         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
8026         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
8027         assert_eq!(reestablish_1.len(), 3);
8028         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
8029         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
8030         assert_eq!(reestablish_2.len(), 3);
8031
8032         // Reestablish chan_1
8033         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
8034         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
8035         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
8036         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
8037         // Reestablish chan_2
8038         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[1]);
8039         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
8040         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[1]);
8041         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
8042         // Reestablish chan_3
8043         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[2]);
8044         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
8045         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[2]);
8046         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
8047
8048         nodes[0].node.timer_tick_occurred();
8049         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
8050         nodes[0].node.timer_tick_occurred();
8051         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
8052         assert_eq!(msg_events.len(), 3);
8053         chans_disabled = [short_id_1, short_id_2, short_id_3].iter().map(|a| *a).collect();
8054         for e in msg_events {
8055                 match e {
8056                         MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
8057                                 assert_eq!(msg.contents.flags & (1<<1), 0); // The "channel disabled" bit should be off
8058                                 // Check that each channel gets updated exactly once
8059                                 if !chans_disabled.remove(&msg.contents.short_channel_id) {
8060                                         panic!("Generated ChannelUpdate for wrong chan!");
8061                                 }
8062                         },
8063                         _ => panic!("Unexpected event"),
8064                 }
8065         }
8066 }
8067
8068 #[test]
8069 fn test_priv_forwarding_rejection() {
8070         // If we have a private channel with outbound liquidity, and
8071         // UserConfig::accept_forwards_to_priv_channels is set to false, we should reject any attempts
8072         // to forward through that channel.
8073         let chanmon_cfgs = create_chanmon_cfgs(3);
8074         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8075         let mut no_announce_cfg = test_default_channel_config();
8076         no_announce_cfg.channel_options.announced_channel = false;
8077         no_announce_cfg.accept_forwards_to_priv_channels = false;
8078         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
8079         let persister: test_utils::TestPersister;
8080         let new_chain_monitor: test_utils::TestChainMonitor;
8081         let nodes_1_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
8082         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8083
8084         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known());
8085
8086         // Note that the create_*_chan functions in utils requires announcement_signatures, which we do
8087         // not send for private channels.
8088         nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
8089         let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[2].node.get_our_node_id());
8090         nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel);
8091         let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
8092         nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
8093
8094         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], 1_000_000, 42);
8095         nodes[1].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
8096         nodes[2].node.handle_funding_created(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingCreated, nodes[2].node.get_our_node_id()));
8097         check_added_monitors!(nodes[2], 1);
8098
8099         nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id()));
8100         check_added_monitors!(nodes[1], 1);
8101
8102         let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1);
8103         confirm_transaction_at(&nodes[1], &tx, conf_height);
8104         connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1);
8105         confirm_transaction_at(&nodes[2], &tx, conf_height);
8106         connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1);
8107         let as_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id());
8108         nodes[1].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id()));
8109         get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
8110         nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked);
8111         get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
8112
8113         assert!(nodes[0].node.list_usable_channels()[0].is_public);
8114         assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
8115         assert!(!nodes[2].node.list_usable_channels()[0].is_public);
8116
8117         // We should always be able to forward through nodes[1] as long as its out through a public
8118         // channel:
8119         send_payment(&nodes[2], &[&nodes[1], &nodes[0]], 10_000);
8120
8121         // ... however, if we send to nodes[2], we will have to pass the private channel from nodes[1]
8122         // to nodes[2], which should be rejected:
8123         let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
8124         let route = get_route(&nodes[0].node.get_our_node_id(),
8125                 &nodes[0].net_graph_msg_handler.network_graph.read().unwrap(),
8126                 &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None,
8127                 &[&RouteHint(vec![RouteHintHop {
8128                         src_node_id: nodes[1].node.get_our_node_id(),
8129                         short_channel_id: nodes[2].node.list_channels()[0].short_channel_id.unwrap(),
8130                         fees: RoutingFees { base_msat: 1000, proportional_millionths: 0 },
8131                         cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
8132                         htlc_minimum_msat: None,
8133                         htlc_maximum_msat: None,
8134                 }])], 10_000, TEST_FINAL_CLTV, nodes[0].logger).unwrap();
8135
8136         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
8137         check_added_monitors!(nodes[0], 1);
8138         let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
8139         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8140         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false, true);
8141
8142         let htlc_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8143         assert!(htlc_fail_updates.update_add_htlcs.is_empty());
8144         assert_eq!(htlc_fail_updates.update_fail_htlcs.len(), 1);
8145         assert!(htlc_fail_updates.update_fail_malformed_htlcs.is_empty());
8146         assert!(htlc_fail_updates.update_fee.is_none());
8147
8148         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_fail_updates.update_fail_htlcs[0]);
8149         commitment_signed_dance!(nodes[0], nodes[1], htlc_fail_updates.commitment_signed, true, true);
8150         expect_payment_failed!(nodes[0], our_payment_hash, false);
8151         expect_payment_failure_chan_update!(nodes[0], nodes[2].node.list_channels()[0].short_channel_id.unwrap(), true);
8152
8153         // Now disconnect nodes[1] from its peers and restart with accept_forwards_to_priv_channels set
8154         // to true. Sadly there is currently no way to change it at runtime.
8155
8156         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
8157         nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
8158
8159         let nodes_1_serialized = nodes[1].node.encode();
8160         let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new());
8161         let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new());
8162         {
8163                 let mons = nodes[1].chain_monitor.chain_monitor.monitors.read().unwrap();
8164                 let mut mon_iter = mons.iter();
8165                 mon_iter.next().unwrap().1.write(&mut monitor_a_serialized).unwrap();
8166                 mon_iter.next().unwrap().1.write(&mut monitor_b_serialized).unwrap();
8167         }
8168
8169         persister = test_utils::TestPersister::new();
8170         let keys_manager = &chanmon_cfgs[1].keys_manager;
8171         new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[1].chain_source), nodes[1].tx_broadcaster.clone(), nodes[1].logger, node_cfgs[1].fee_estimator, &persister, keys_manager);
8172         nodes[1].chain_monitor = &new_chain_monitor;
8173
8174         let mut monitor_a_read = &monitor_a_serialized.0[..];
8175         let mut monitor_b_read = &monitor_b_serialized.0[..];
8176         let (_, mut monitor_a) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_a_read, keys_manager).unwrap();
8177         let (_, mut monitor_b) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut monitor_b_read, keys_manager).unwrap();
8178         assert!(monitor_a_read.is_empty());
8179         assert!(monitor_b_read.is_empty());
8180
8181         no_announce_cfg.accept_forwards_to_priv_channels = true;
8182
8183         let mut nodes_1_read = &nodes_1_serialized[..];
8184         let (_, nodes_1_deserialized_tmp) = {
8185                 let mut channel_monitors = HashMap::new();
8186                 channel_monitors.insert(monitor_a.get_funding_txo().0, &mut monitor_a);
8187                 channel_monitors.insert(monitor_b.get_funding_txo().0, &mut monitor_b);
8188                 <(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs {
8189                         default_config: no_announce_cfg,
8190                         keys_manager,
8191                         fee_estimator: node_cfgs[1].fee_estimator,
8192                         chain_monitor: nodes[1].chain_monitor,
8193                         tx_broadcaster: nodes[1].tx_broadcaster.clone(),
8194                         logger: nodes[1].logger,
8195                         channel_monitors,
8196                 }).unwrap()
8197         };
8198         assert!(nodes_1_read.is_empty());
8199         nodes_1_deserialized = nodes_1_deserialized_tmp;
8200
8201         assert!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a).is_ok());
8202         assert!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b).is_ok());
8203         check_added_monitors!(nodes[1], 2);
8204         nodes[1].node = &nodes_1_deserialized;
8205
8206         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
8207         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
8208         let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
8209         let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
8210         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish);
8211         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
8212         get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
8213         get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
8214
8215         nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() });
8216         nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() });
8217         let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[2].node.get_our_node_id());
8218         let cs_reestablish = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
8219         nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish);
8220         nodes[1].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &cs_reestablish);
8221         get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id());
8222         get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
8223
8224         nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)).unwrap();
8225         check_added_monitors!(nodes[0], 1);
8226         pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret);
8227         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage);
8228 }
8229
8230 #[test]
8231 fn test_bump_penalty_txn_on_revoked_commitment() {
8232         // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
8233         // we're able to claim outputs on revoked commitment transaction before timelocks expiration
8234
8235         let chanmon_cfgs = create_chanmon_cfgs(2);
8236         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8237         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8238         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8239
8240         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
8241         let logger = test_utils::TestLogger::new();
8242
8243         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
8244         let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
8245         let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3000000, 30, &logger).unwrap();
8246         send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
8247
8248         let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
8249         // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
8250         assert_eq!(revoked_txn[0].output.len(), 4);
8251         assert_eq!(revoked_txn[0].input.len(), 1);
8252         assert_eq!(revoked_txn[0].input[0].previous_output.txid, chan.3.txid());
8253         let revoked_txid = revoked_txn[0].txid();
8254
8255         let mut penalty_sum = 0;
8256         for outp in revoked_txn[0].output.iter() {
8257                 if outp.script_pubkey.is_v0_p2wsh() {
8258                         penalty_sum += outp.value;
8259                 }
8260         }
8261
8262         // Connect blocks to change height_timer range to see if we use right soonest_timelock
8263         let header_114 = connect_blocks(&nodes[1], 14);
8264
8265         // Actually revoke tx by claiming a HTLC
8266         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
8267         let header = BlockHeader { version: 0x20000000, prev_blockhash: header_114, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8268         connect_block(&nodes[1], &Block { header, txdata: vec![revoked_txn[0].clone()] });
8269         check_added_monitors!(nodes[1], 1);
8270
8271         // One or more justice tx should have been broadcast, check it
8272         let penalty_1;
8273         let feerate_1;
8274         {
8275                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8276                 assert_eq!(node_txn.len(), 2); // justice tx (broadcasted from ChannelMonitor) + local commitment tx
8277                 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
8278                 assert_eq!(node_txn[0].output.len(), 1);
8279                 check_spends!(node_txn[0], revoked_txn[0]);
8280                 let fee_1 = penalty_sum - node_txn[0].output[0].value;
8281                 feerate_1 = fee_1 * 1000 / node_txn[0].get_weight() as u64;
8282                 penalty_1 = node_txn[0].txid();
8283                 node_txn.clear();
8284         };
8285
8286         // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it
8287         connect_blocks(&nodes[1], 15);
8288         let mut penalty_2 = penalty_1;
8289         let mut feerate_2 = 0;
8290         {
8291                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8292                 assert_eq!(node_txn.len(), 1);
8293                 if node_txn[0].input[0].previous_output.txid == revoked_txid {
8294                         assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
8295                         assert_eq!(node_txn[0].output.len(), 1);
8296                         check_spends!(node_txn[0], revoked_txn[0]);
8297                         penalty_2 = node_txn[0].txid();
8298                         // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
8299                         assert_ne!(penalty_2, penalty_1);
8300                         let fee_2 = penalty_sum - node_txn[0].output[0].value;
8301                         feerate_2 = fee_2 * 1000 / node_txn[0].get_weight() as u64;
8302                         // Verify 25% bump heuristic
8303                         assert!(feerate_2 * 100 >= feerate_1 * 125);
8304                         node_txn.clear();
8305                 }
8306         }
8307         assert_ne!(feerate_2, 0);
8308
8309         // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it
8310         connect_blocks(&nodes[1], 1);
8311         let penalty_3;
8312         let mut feerate_3 = 0;
8313         {
8314                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8315                 assert_eq!(node_txn.len(), 1);
8316                 if node_txn[0].input[0].previous_output.txid == revoked_txid {
8317                         assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
8318                         assert_eq!(node_txn[0].output.len(), 1);
8319                         check_spends!(node_txn[0], revoked_txn[0]);
8320                         penalty_3 = node_txn[0].txid();
8321                         // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
8322                         assert_ne!(penalty_3, penalty_2);
8323                         let fee_3 = penalty_sum - node_txn[0].output[0].value;
8324                         feerate_3 = fee_3 * 1000 / node_txn[0].get_weight() as u64;
8325                         // Verify 25% bump heuristic
8326                         assert!(feerate_3 * 100 >= feerate_2 * 125);
8327                         node_txn.clear();
8328                 }
8329         }
8330         assert_ne!(feerate_3, 0);
8331
8332         nodes[1].node.get_and_clear_pending_events();
8333         nodes[1].node.get_and_clear_pending_msg_events();
8334 }
8335
8336 #[test]
8337 fn test_bump_penalty_txn_on_revoked_htlcs() {
8338         // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure
8339         // we're able to claim outputs on revoked HTLC transactions before timelocks expiration
8340
8341         let mut chanmon_cfgs = create_chanmon_cfgs(2);
8342         chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
8343         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8344         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8345         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8346
8347         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
8348         // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
8349         let route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler.network_graph.read().unwrap(),
8350                 &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3_000_000, 50, nodes[0].logger).unwrap();
8351         let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
8352         let route = get_route(&nodes[1].node.get_our_node_id(), &nodes[1].net_graph_msg_handler.network_graph.read().unwrap(),
8353                 &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3_000_000, 50, nodes[0].logger).unwrap();
8354         send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
8355
8356         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
8357         assert_eq!(revoked_local_txn[0].input.len(), 1);
8358         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
8359
8360         // Revoke local commitment tx
8361         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
8362
8363         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8364         // B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx
8365         connect_block(&nodes[1], &Block { header, txdata: vec![revoked_local_txn[0].clone()] });
8366         check_closed_broadcast!(nodes[1], true);
8367         check_added_monitors!(nodes[1], 1);
8368         connect_blocks(&nodes[1], 49); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
8369
8370         let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8371         assert_eq!(revoked_htlc_txn.len(), 3);
8372         check_spends!(revoked_htlc_txn[1], chan.3);
8373
8374         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
8375         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
8376         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
8377
8378         assert_eq!(revoked_htlc_txn[2].input.len(), 1);
8379         assert_eq!(revoked_htlc_txn[2].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8380         assert_eq!(revoked_htlc_txn[2].output.len(), 1);
8381         check_spends!(revoked_htlc_txn[2], revoked_local_txn[0]);
8382
8383         // Broadcast set of revoked txn on A
8384         let hash_128 = connect_blocks(&nodes[0], 40);
8385         let header_11 = BlockHeader { version: 0x20000000, prev_blockhash: hash_128, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8386         connect_block(&nodes[0], &Block { header: header_11, txdata: vec![revoked_local_txn[0].clone()] });
8387         let header_129 = BlockHeader { version: 0x20000000, prev_blockhash: header_11.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8388         connect_block(&nodes[0], &Block { header: header_129, txdata: vec![revoked_htlc_txn[0].clone(), revoked_htlc_txn[2].clone()] });
8389         expect_pending_htlcs_forwardable_ignore!(nodes[0]);
8390         let first;
8391         let feerate_1;
8392         let penalty_txn;
8393         {
8394                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8395                 assert_eq!(node_txn.len(), 5); // 3 penalty txn on revoked commitment tx + A commitment tx + 1 penalty tnx on revoked HTLC txn
8396                 // Verify claim tx are spending revoked HTLC txn
8397
8398                 // node_txn 0-2 each spend a separate revoked output from revoked_local_txn[0]
8399                 // Note that node_txn[0] and node_txn[1] are bogus - they double spend the revoked_htlc_txn
8400                 // which are included in the same block (they are broadcasted because we scan the
8401                 // transactions linearly and generate claims as we go, they likely should be removed in the
8402                 // future).
8403                 assert_eq!(node_txn[0].input.len(), 1);
8404                 check_spends!(node_txn[0], revoked_local_txn[0]);
8405                 assert_eq!(node_txn[1].input.len(), 1);
8406                 check_spends!(node_txn[1], revoked_local_txn[0]);
8407                 assert_eq!(node_txn[2].input.len(), 1);
8408                 check_spends!(node_txn[2], revoked_local_txn[0]);
8409
8410                 // Each of the three justice transactions claim a separate (single) output of the three
8411                 // available, which we check here:
8412                 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
8413                 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
8414                 assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
8415
8416                 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
8417                 assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[2].input[0].previous_output);
8418
8419                 // node_txn[3] is the local commitment tx broadcast just because (and somewhat in case of
8420                 // reorgs, though its not clear its ever worth broadcasting conflicting txn like this when
8421                 // a remote commitment tx has already been confirmed).
8422                 check_spends!(node_txn[3], chan.3);
8423
8424                 // node_txn[4] spends the revoked outputs from the revoked_htlc_txn (which only have one
8425                 // output, checked above).
8426                 assert_eq!(node_txn[4].input.len(), 2);
8427                 assert_eq!(node_txn[4].output.len(), 1);
8428                 check_spends!(node_txn[4], revoked_htlc_txn[0], revoked_htlc_txn[2]);
8429
8430                 first = node_txn[4].txid();
8431                 // Store both feerates for later comparison
8432                 let fee_1 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[2].output[0].value - node_txn[4].output[0].value;
8433                 feerate_1 = fee_1 * 1000 / node_txn[4].get_weight() as u64;
8434                 penalty_txn = vec![node_txn[2].clone()];
8435                 node_txn.clear();
8436         }
8437
8438         // Connect one more block to see if bumped penalty are issued for HTLC txn
8439         let header_130 = BlockHeader { version: 0x20000000, prev_blockhash: header_129.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8440         connect_block(&nodes[0], &Block { header: header_130, txdata: penalty_txn });
8441         let header_131 = BlockHeader { version: 0x20000000, prev_blockhash: header_130.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8442         connect_block(&nodes[0], &Block { header: header_131, txdata: Vec::new() });
8443         {
8444                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8445                 assert_eq!(node_txn.len(), 2); // 2 bumped penalty txn on revoked commitment tx
8446
8447                 check_spends!(node_txn[0], revoked_local_txn[0]);
8448                 check_spends!(node_txn[1], revoked_local_txn[0]);
8449                 // Note that these are both bogus - they spend outputs already claimed in block 129:
8450                 if node_txn[0].input[0].previous_output == revoked_htlc_txn[0].input[0].previous_output  {
8451                         assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[2].input[0].previous_output);
8452                 } else {
8453                         assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[2].input[0].previous_output);
8454                         assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
8455                 }
8456
8457                 node_txn.clear();
8458         };
8459
8460         // Few more blocks to confirm penalty txn
8461         connect_blocks(&nodes[0], 4);
8462         assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
8463         let header_144 = connect_blocks(&nodes[0], 9);
8464         let node_txn = {
8465                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8466                 assert_eq!(node_txn.len(), 1);
8467
8468                 assert_eq!(node_txn[0].input.len(), 2);
8469                 check_spends!(node_txn[0], revoked_htlc_txn[0], revoked_htlc_txn[2]);
8470                 // Verify bumped tx is different and 25% bump heuristic
8471                 assert_ne!(first, node_txn[0].txid());
8472                 let fee_2 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[2].output[0].value - node_txn[0].output[0].value;
8473                 let feerate_2 = fee_2 * 1000 / node_txn[0].get_weight() as u64;
8474                 assert!(feerate_2 * 100 > feerate_1 * 125);
8475                 let txn = vec![node_txn[0].clone()];
8476                 node_txn.clear();
8477                 txn
8478         };
8479         // Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
8480         let header_145 = BlockHeader { version: 0x20000000, prev_blockhash: header_144, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8481         connect_block(&nodes[0], &Block { header: header_145, txdata: node_txn });
8482         connect_blocks(&nodes[0], 20);
8483         {
8484                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8485                 // We verify than no new transaction has been broadcast because previously
8486                 // we were buggy on this exact behavior by not tracking for monitoring remote HTLC outputs (see #411)
8487                 // which means we wouldn't see a spend of them by a justice tx and bumped justice tx
8488                 // were generated forever instead of safe cleaning after confirmation and ANTI_REORG_SAFE_DELAY blocks.
8489                 // Enforce spending of revoked htlc output by claiming transaction remove request as expected and dry
8490                 // up bumped justice generation.
8491                 assert_eq!(node_txn.len(), 0);
8492                 node_txn.clear();
8493         }
8494         check_closed_broadcast!(nodes[0], true);
8495         check_added_monitors!(nodes[0], 1);
8496 }
8497
8498 #[test]
8499 fn test_bump_penalty_txn_on_remote_commitment() {
8500         // In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure
8501         // we're able to claim outputs on remote commitment transaction before timelocks expiration
8502
8503         // Create 2 HTLCs
8504         // Provide preimage for one
8505         // Check aggregation
8506
8507         let chanmon_cfgs = create_chanmon_cfgs(2);
8508         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8509         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8510         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8511
8512         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
8513         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
8514         route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0;
8515
8516         // Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
8517         let remote_txn = get_local_commitment_txn!(nodes[0], chan.2);
8518         assert_eq!(remote_txn[0].output.len(), 4);
8519         assert_eq!(remote_txn[0].input.len(), 1);
8520         assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid());
8521
8522         // Claim a HTLC without revocation (provide B monitor with preimage)
8523         nodes[1].node.claim_funds(payment_preimage);
8524         mine_transaction(&nodes[1], &remote_txn[0]);
8525         check_added_monitors!(nodes[1], 2);
8526         connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
8527
8528         // One or more claim tx should have been broadcast, check it
8529         let timeout;
8530         let preimage;
8531         let preimage_bump;
8532         let feerate_timeout;
8533         let feerate_preimage;
8534         {
8535                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8536                 // 9 transactions including:
8537                 // 1*2 ChannelManager local broadcasts of commitment + HTLC-Success
8538                 // 1*3 ChannelManager local broadcasts of commitment + HTLC-Success + HTLC-Timeout
8539                 // 2 * HTLC-Success (one RBF bump we'll check later)
8540                 // 1 * HTLC-Timeout
8541                 assert_eq!(node_txn.len(), 8);
8542                 assert_eq!(node_txn[0].input.len(), 1);
8543                 assert_eq!(node_txn[6].input.len(), 1);
8544                 check_spends!(node_txn[0], remote_txn[0]);
8545                 check_spends!(node_txn[6], remote_txn[0]);
8546                 assert_eq!(node_txn[0].input[0].previous_output, node_txn[3].input[0].previous_output);
8547                 preimage_bump = node_txn[3].clone();
8548
8549                 check_spends!(node_txn[1], chan.3);
8550                 check_spends!(node_txn[2], node_txn[1]);
8551                 assert_eq!(node_txn[1], node_txn[4]);
8552                 assert_eq!(node_txn[2], node_txn[5]);
8553
8554                 timeout = node_txn[6].txid();
8555                 let index = node_txn[6].input[0].previous_output.vout;
8556                 let fee = remote_txn[0].output[index as usize].value - node_txn[6].output[0].value;
8557                 feerate_timeout = fee * 1000 / node_txn[6].get_weight() as u64;
8558
8559                 preimage = node_txn[0].txid();
8560                 let index = node_txn[0].input[0].previous_output.vout;
8561                 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
8562                 feerate_preimage = fee * 1000 / node_txn[0].get_weight() as u64;
8563
8564                 node_txn.clear();
8565         };
8566         assert_ne!(feerate_timeout, 0);
8567         assert_ne!(feerate_preimage, 0);
8568
8569         // After exhaustion of height timer, new bumped claim txn should have been broadcast, check it
8570         connect_blocks(&nodes[1], 15);
8571         {
8572                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8573                 assert_eq!(node_txn.len(), 1);
8574                 assert_eq!(node_txn[0].input.len(), 1);
8575                 assert_eq!(preimage_bump.input.len(), 1);
8576                 check_spends!(node_txn[0], remote_txn[0]);
8577                 check_spends!(preimage_bump, remote_txn[0]);
8578
8579                 let index = preimage_bump.input[0].previous_output.vout;
8580                 let fee = remote_txn[0].output[index as usize].value - preimage_bump.output[0].value;
8581                 let new_feerate = fee * 1000 / preimage_bump.get_weight() as u64;
8582                 assert!(new_feerate * 100 > feerate_timeout * 125);
8583                 assert_ne!(timeout, preimage_bump.txid());
8584
8585                 let index = node_txn[0].input[0].previous_output.vout;
8586                 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
8587                 let new_feerate = fee * 1000 / node_txn[0].get_weight() as u64;
8588                 assert!(new_feerate * 100 > feerate_preimage * 125);
8589                 assert_ne!(preimage, node_txn[0].txid());
8590
8591                 node_txn.clear();
8592         }
8593
8594         nodes[1].node.get_and_clear_pending_events();
8595         nodes[1].node.get_and_clear_pending_msg_events();
8596 }
8597
8598 #[test]
8599 fn test_counterparty_raa_skip_no_crash() {
8600         // Previously, if our counterparty sent two RAAs in a row without us having provided a
8601         // commitment transaction, we would have happily carried on and provided them the next
8602         // commitment transaction based on one RAA forward. This would probably eventually have led to
8603         // channel closure, but it would not have resulted in funds loss. Still, our
8604         // EnforcingSigner would have paniced as it doesn't like jumps into the future. Here, we
8605         // check simply that the channel is closed in response to such an RAA, but don't check whether
8606         // we decide to punish our counterparty for revoking their funds (as we don't currently
8607         // implement that).
8608         let chanmon_cfgs = create_chanmon_cfgs(2);
8609         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8610         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8611         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8612         let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
8613
8614         let mut guard = nodes[0].node.channel_state.lock().unwrap();
8615         let keys = &guard.by_id.get_mut(&channel_id).unwrap().get_signer();
8616         const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
8617         let per_commitment_secret = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
8618         // Must revoke without gaps
8619         keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
8620         let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
8621                 &SecretKey::from_slice(&keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8622
8623         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
8624                 &msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });
8625         assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
8626         check_added_monitors!(nodes[1], 1);
8627 }
8628
8629 #[test]
8630 fn test_bump_txn_sanitize_tracking_maps() {
8631         // Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,
8632         // verify we clean then right after expiration of ANTI_REORG_DELAY.
8633
8634         let chanmon_cfgs = create_chanmon_cfgs(2);
8635         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8636         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8637         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8638
8639         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known());
8640         // Lock HTLC in both directions
8641         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
8642         route_payment(&nodes[1], &vec!(&nodes[0])[..], 9_000_000).0;
8643
8644         let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
8645         assert_eq!(revoked_local_txn[0].input.len(), 1);
8646         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
8647
8648         // Revoke local commitment tx
8649         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
8650
8651         // Broadcast set of revoked txn on A
8652         connect_blocks(&nodes[0], TEST_FINAL_CLTV + 2 - CHAN_CONFIRM_DEPTH);
8653         expect_pending_htlcs_forwardable_ignore!(nodes[0]);
8654         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
8655
8656         mine_transaction(&nodes[0], &revoked_local_txn[0]);
8657         check_closed_broadcast!(nodes[0], true);
8658         check_added_monitors!(nodes[0], 1);
8659         let penalty_txn = {
8660                 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8661                 assert_eq!(node_txn.len(), 4); //ChannelMonitor: justice txn * 3, ChannelManager: local commitment tx
8662                 check_spends!(node_txn[0], revoked_local_txn[0]);
8663                 check_spends!(node_txn[1], revoked_local_txn[0]);
8664                 check_spends!(node_txn[2], revoked_local_txn[0]);
8665                 let penalty_txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone()];
8666                 node_txn.clear();
8667                 penalty_txn
8668         };
8669         let header_130 = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8670         connect_block(&nodes[0], &Block { header: header_130, txdata: penalty_txn });
8671         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
8672         {
8673                 let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
8674                 if let Some(monitor) = monitors.get(&OutPoint { txid: chan.3.txid(), index: 0 }) {
8675                         assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
8676                         assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
8677                 }
8678         }
8679 }
8680
8681 #[test]
8682 fn test_override_channel_config() {
8683         let chanmon_cfgs = create_chanmon_cfgs(2);
8684         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8685         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8686         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8687
8688         // Node0 initiates a channel to node1 using the override config.
8689         let mut override_config = UserConfig::default();
8690         override_config.own_channel_config.our_to_self_delay = 200;
8691
8692         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(override_config)).unwrap();
8693
8694         // Assert the channel created by node0 is using the override config.
8695         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8696         assert_eq!(res.channel_flags, 0);
8697         assert_eq!(res.to_self_delay, 200);
8698 }
8699
8700 #[test]
8701 fn test_override_0msat_htlc_minimum() {
8702         let mut zero_config = UserConfig::default();
8703         zero_config.own_channel_config.our_htlc_minimum_msat = 0;
8704         let chanmon_cfgs = create_chanmon_cfgs(2);
8705         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8706         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(zero_config.clone())]);
8707         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8708
8709         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(zero_config)).unwrap();
8710         let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8711         assert_eq!(res.htlc_minimum_msat, 1);
8712
8713         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &res);
8714         let res = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
8715         assert_eq!(res.htlc_minimum_msat, 1);
8716 }
8717
8718 #[test]
8719 fn test_simple_mpp() {
8720         // Simple test of sending a multi-path payment.
8721         let chanmon_cfgs = create_chanmon_cfgs(4);
8722         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
8723         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
8724         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
8725
8726         let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8727         let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8728         let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8729         let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8730         let logger = test_utils::TestLogger::new();
8731
8732         let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[3]);
8733         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8734         let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100000, TEST_FINAL_CLTV, &logger).unwrap();
8735         let path = route.paths[0].clone();
8736         route.paths.push(path);
8737         route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
8738         route.paths[0][0].short_channel_id = chan_1_id;
8739         route.paths[0][1].short_channel_id = chan_3_id;
8740         route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
8741         route.paths[1][0].short_channel_id = chan_2_id;
8742         route.paths[1][1].short_channel_id = chan_4_id;
8743         send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
8744         claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
8745 }
8746
8747 #[test]
8748 fn test_preimage_storage() {
8749         // Simple test of payment preimage storage allowing no client-side storage to claim payments
8750         let chanmon_cfgs = create_chanmon_cfgs(2);
8751         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8752         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8753         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8754
8755         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8756
8757         {
8758                 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 7200, 42);
8759
8760                 let logger = test_utils::TestLogger::new();
8761                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8762                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8763                 nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
8764                 check_added_monitors!(nodes[0], 1);
8765                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8766                 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8767                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8768                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8769         }
8770         // Note that after leaving the above scope we have no knowledge of any arguments or return
8771         // values from previous calls.
8772         expect_pending_htlcs_forwardable!(nodes[1]);
8773         let events = nodes[1].node.get_and_clear_pending_events();
8774         assert_eq!(events.len(), 1);
8775         match events[0] {
8776                 Event::PaymentReceived { ref purpose, .. } => {
8777                         match &purpose {
8778                                 PaymentPurpose::InvoicePayment { payment_preimage, user_payment_id, .. } => {
8779                                         assert_eq!(*user_payment_id, 42);
8780                                         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8781                                 },
8782                                 _ => panic!("expected PaymentPurpose::InvoicePayment")
8783                         }
8784                 },
8785                 _ => panic!("Unexpected event"),
8786         }
8787 }
8788
8789 #[test]
8790 fn test_secret_timeout() {
8791         // Simple test of payment secret storage time outs
8792         let chanmon_cfgs = create_chanmon_cfgs(2);
8793         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8794         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8795         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8796
8797         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8798
8799         let (payment_hash, payment_secret_1) = nodes[1].node.create_inbound_payment(Some(100_000), 2, 0);
8800
8801         // We should fail to register the same payment hash twice, at least until we've connected a
8802         // block with time 7200 + CHAN_CONFIRM_DEPTH + 1.
8803         if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(100_000), 2, 0) {
8804                 assert_eq!(err, "Duplicate payment hash");
8805         } else { panic!(); }
8806         let mut block = {
8807                 let node_1_blocks = nodes[1].blocks.lock().unwrap();
8808                 Block {
8809                         header: BlockHeader {
8810                                 version: 0x2000000,
8811                                 prev_blockhash: node_1_blocks.last().unwrap().0.block_hash(),
8812                                 merkle_root: Default::default(),
8813                                 time: node_1_blocks.len() as u32 + 7200, bits: 42, nonce: 42 },
8814                         txdata: vec![],
8815                 }
8816         };
8817         connect_block(&nodes[1], &block);
8818         if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(100_000), 2, 0) {
8819                 assert_eq!(err, "Duplicate payment hash");
8820         } else { panic!(); }
8821
8822         // If we then connect the second block, we should be able to register the same payment hash
8823         // again with a different user_payment_id (this time getting a new payment secret).
8824         block.header.prev_blockhash = block.header.block_hash();
8825         block.header.time += 1;
8826         connect_block(&nodes[1], &block);
8827         let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(100_000), 2, 42).unwrap();
8828         assert_ne!(payment_secret_1, our_payment_secret);
8829
8830         {
8831                 let logger = test_utils::TestLogger::new();
8832                 let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8833                 let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8834                 nodes[0].node.send_payment(&route, payment_hash, &Some(our_payment_secret)).unwrap();
8835                 check_added_monitors!(nodes[0], 1);
8836                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8837                 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8838                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8839                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8840         }
8841         // Note that after leaving the above scope we have no knowledge of any arguments or return
8842         // values from previous calls.
8843         expect_pending_htlcs_forwardable!(nodes[1]);
8844         let events = nodes[1].node.get_and_clear_pending_events();
8845         assert_eq!(events.len(), 1);
8846         match events[0] {
8847                 Event::PaymentReceived { purpose: PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, user_payment_id }, .. } => {
8848                         assert!(payment_preimage.is_none());
8849                         assert_eq!(user_payment_id, 42);
8850                         assert_eq!(payment_secret, our_payment_secret);
8851                         // We don't actually have the payment preimage with which to claim this payment!
8852                 },
8853                 _ => panic!("Unexpected event"),
8854         }
8855 }
8856
8857 #[test]
8858 fn test_bad_secret_hash() {
8859         // Simple test of unregistered payment hash/invalid payment secret handling
8860         let chanmon_cfgs = create_chanmon_cfgs(2);
8861         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8862         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8863         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8864
8865         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
8866
8867         let random_payment_hash = PaymentHash([42; 32]);
8868         let random_payment_secret = PaymentSecret([43; 32]);
8869         let (our_payment_hash, our_payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 2, 0);
8870
8871         let logger = test_utils::TestLogger::new();
8872         let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
8873         let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[1].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 100_000, TEST_FINAL_CLTV, &logger).unwrap();
8874
8875         // All the below cases should end up being handled exactly identically, so we macro the
8876         // resulting events.
8877         macro_rules! handle_unknown_invalid_payment_data {
8878                 () => {
8879                         check_added_monitors!(nodes[0], 1);
8880                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8881                         let payment_event = SendEvent::from_event(events.pop().unwrap());
8882                         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8883                         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8884
8885                         // We have to forward pending HTLCs once to process the receipt of the HTLC and then
8886                         // again to process the pending backwards-failure of the HTLC
8887                         expect_pending_htlcs_forwardable!(nodes[1]);
8888                         expect_pending_htlcs_forwardable!(nodes[1]);
8889                         check_added_monitors!(nodes[1], 1);
8890
8891                         // We should fail the payment back
8892                         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
8893                         match events.pop().unwrap() {
8894                                 MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8895                                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
8896                                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
8897                                 },
8898                                 _ => panic!("Unexpected event"),
8899                         }
8900                 }
8901         }
8902
8903         let expected_error_code = 0x4000|15; // incorrect_or_unknown_payment_details
8904         // Error data is the HTLC value (100,000) and current block height
8905         let expected_error_data = [0, 0, 0, 0, 0, 1, 0x86, 0xa0, 0, 0, 0, CHAN_CONFIRM_DEPTH as u8];
8906
8907         // Send a payment with the right payment hash but the wrong payment secret
8908         nodes[0].node.send_payment(&route, our_payment_hash, &Some(random_payment_secret)).unwrap();
8909         handle_unknown_invalid_payment_data!();
8910         expect_payment_failed!(nodes[0], our_payment_hash, true, expected_error_code, expected_error_data);
8911
8912         // Send a payment with a random payment hash, but the right payment secret
8913         nodes[0].node.send_payment(&route, random_payment_hash, &Some(our_payment_secret)).unwrap();
8914         handle_unknown_invalid_payment_data!();
8915         expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8916
8917         // Send a payment with a random payment hash and random payment secret
8918         nodes[0].node.send_payment(&route, random_payment_hash, &Some(random_payment_secret)).unwrap();
8919         handle_unknown_invalid_payment_data!();
8920         expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8921 }
8922
8923 #[test]
8924 fn test_update_err_monitor_lockdown() {
8925         // Our monitor will lock update of local commitment transaction if a broadcastion condition
8926         // has been fulfilled (either force-close from Channel or block height requiring a HTLC-
8927         // timeout). Trying to update monitor after lockdown should return a ChannelMonitorUpdateErr.
8928         //
8929         // This scenario may happen in a watchtower setup, where watchtower process a block height
8930         // triggering a timeout while a slow-block-processing ChannelManager receives a local signed
8931         // commitment at same time.
8932
8933         let chanmon_cfgs = create_chanmon_cfgs(2);
8934         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8935         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8936         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8937
8938         // Create some initial channel
8939         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
8940         let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8941
8942         // Rebalance the network to generate htlc in the two directions
8943         send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8944
8945         // Route a HTLC from node 0 to node 1 (but don't settle)
8946         let preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
8947
8948         // Copy ChainMonitor to simulate a watchtower and update block height of node 0 until its ChannelMonitor timeout HTLC onchain
8949         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8950         let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
8951         let persister = test_utils::TestPersister::new();
8952         let watchtower = {
8953                 let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
8954                 let monitor = monitors.get(&outpoint).unwrap();
8955                 let mut w = test_utils::TestVecWriter(Vec::new());
8956                 monitor.write(&mut w).unwrap();
8957                 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
8958                                 &mut io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1;
8959                 assert!(new_monitor == *monitor);
8960                 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);
8961                 assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok());
8962                 watchtower
8963         };
8964         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8965         // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8966         // transaction lock time requirements here.
8967         chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (header, 0));
8968         watchtower.chain_monitor.block_connected(&Block { header, txdata: vec![] }, 200);
8969
8970         // Try to update ChannelMonitor
8971         assert!(nodes[1].node.claim_funds(preimage));
8972         check_added_monitors!(nodes[1], 1);
8973         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8974         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
8975         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
8976         if let Some(ref mut channel) = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&chan_1.2) {
8977                 if let Ok((_, _, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].fee_estimator, &node_cfgs[0].logger) {
8978                         if let Err(_) =  watchtower.chain_monitor.update_channel(outpoint, update.clone()) {} else { assert!(false); }
8979                         if let Ok(_) = nodes[0].chain_monitor.update_channel(outpoint, update) {} else { assert!(false); }
8980                 } else { assert!(false); }
8981         } else { assert!(false); };
8982         // Our local monitor is in-sync and hasn't processed yet timeout
8983         check_added_monitors!(nodes[0], 1);
8984         let events = nodes[0].node.get_and_clear_pending_events();
8985         assert_eq!(events.len(), 1);
8986 }
8987
8988 #[test]
8989 fn test_concurrent_monitor_claim() {
8990         // Watchtower A receives block, broadcasts state N, then channel receives new state N+1,
8991         // sending it to both watchtowers, Bob accepts N+1, then receives block and broadcasts
8992         // the latest state N+1, Alice rejects state N+1, but Bob has already broadcast it,
8993         // state N+1 confirms. Alice claims output from state N+1.
8994
8995         let chanmon_cfgs = create_chanmon_cfgs(2);
8996         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8997         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8998         let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8999
9000         // Create some initial channel
9001         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
9002         let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
9003
9004         // Rebalance the network to generate htlc in the two directions
9005         send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
9006
9007         // Route a HTLC from node 0 to node 1 (but don't settle)
9008         route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
9009
9010         // Copy ChainMonitor to simulate watchtower Alice and update block height her ChannelMonitor timeout HTLC onchain
9011         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
9012         let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
9013         let persister = test_utils::TestPersister::new();
9014         let watchtower_alice = {
9015                 let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
9016                 let monitor = monitors.get(&outpoint).unwrap();
9017                 let mut w = test_utils::TestVecWriter(Vec::new());
9018                 monitor.write(&mut w).unwrap();
9019                 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
9020                                 &mut io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1;
9021                 assert!(new_monitor == *monitor);
9022                 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);
9023                 assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok());
9024                 watchtower
9025         };
9026         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9027         // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
9028         // transaction lock time requirements here.
9029         chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (header, 0));
9030         watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
9031
9032         // Watchtower Alice should have broadcast a commitment/HTLC-timeout
9033         {
9034                 let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
9035                 assert_eq!(txn.len(), 2);
9036                 txn.clear();
9037         }
9038
9039         // Copy ChainMonitor to simulate watchtower Bob and make it receive a commitment update first.
9040         let chain_source = test_utils::TestChainSource::new(Network::Testnet);
9041         let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
9042         let persister = test_utils::TestPersister::new();
9043         let watchtower_bob = {
9044                 let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
9045                 let monitor = monitors.get(&outpoint).unwrap();
9046                 let mut w = test_utils::TestVecWriter(Vec::new());
9047                 monitor.write(&mut w).unwrap();
9048                 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<EnforcingSigner>)>::read(
9049                                 &mut io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1;
9050                 assert!(new_monitor == *monitor);
9051                 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);
9052                 assert!(watchtower.watch_channel(outpoint, new_monitor).is_ok());
9053                 watchtower
9054         };
9055         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9056         watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
9057
9058         // Route another payment to generate another update with still previous HTLC pending
9059         let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
9060         {
9061                 let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
9062                 let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 3000000 , TEST_FINAL_CLTV, &logger).unwrap();
9063                 nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
9064         }
9065         check_added_monitors!(nodes[1], 1);
9066
9067         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9068         assert_eq!(updates.update_add_htlcs.len(), 1);
9069         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
9070         if let Some(ref mut channel) = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&chan_1.2) {
9071                 if let Ok((_, _, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].fee_estimator, &node_cfgs[0].logger) {
9072                         // Watchtower Alice should already have seen the block and reject the update
9073                         if let Err(_) =  watchtower_alice.chain_monitor.update_channel(outpoint, update.clone()) {} else { assert!(false); }
9074                         if let Ok(_) = watchtower_bob.chain_monitor.update_channel(outpoint, update.clone()) {} else { assert!(false); }
9075                         if let Ok(_) = nodes[0].chain_monitor.update_channel(outpoint, update) {} else { assert!(false); }
9076                 } else { assert!(false); }
9077         } else { assert!(false); };
9078         // Our local monitor is in-sync and hasn't processed yet timeout
9079         check_added_monitors!(nodes[0], 1);
9080
9081         //// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
9082         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9083         watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
9084
9085         // Watchtower Bob should have broadcast a commitment/HTLC-timeout
9086         let bob_state_y;
9087         {
9088                 let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
9089                 assert_eq!(txn.len(), 2);
9090                 bob_state_y = txn[0].clone();
9091                 txn.clear();
9092         };
9093
9094         // We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
9095         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9096         watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, CHAN_CONFIRM_DEPTH + 2 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
9097         {
9098                 let htlc_txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
9099                 // We broadcast twice the transaction, once due to the HTLC-timeout, once due
9100                 // the onchain detection of the HTLC output
9101                 assert_eq!(htlc_txn.len(), 2);
9102                 check_spends!(htlc_txn[0], bob_state_y);
9103                 check_spends!(htlc_txn[1], bob_state_y);
9104         }
9105 }
9106
9107 #[test]
9108 fn test_pre_lockin_no_chan_closed_update() {
9109         // Test that if a peer closes a channel in response to a funding_created message we don't
9110         // generate a channel update (as the channel cannot appear on chain without a funding_signed
9111         // message).
9112         //
9113         // Doing so would imply a channel monitor update before the initial channel monitor
9114         // registration, violating our API guarantees.
9115         //
9116         // Previously, full_stack_target managed to hit this case by opening then closing a channel,
9117         // then opening a second channel with the same funding output as the first (which is not
9118         // rejected because the first channel does not exist in the ChannelManager) and closing it
9119         // before receiving funding_signed.
9120         let chanmon_cfgs = create_chanmon_cfgs(2);
9121         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9122         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9123         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9124
9125         // Create an initial channel
9126         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9127         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9128         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_chan_msg);
9129         let accept_chan_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9130         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_chan_msg);
9131
9132         // Move the first channel through the funding flow...
9133         let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 100000, 42);
9134
9135         nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
9136         check_added_monitors!(nodes[0], 0);
9137
9138         let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9139         let channel_id = ::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
9140         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
9141         assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
9142 }
9143
9144 #[test]
9145 fn test_htlc_no_detection() {
9146         // This test is a mutation to underscore the detection logic bug we had
9147         // before #653. HTLC value routed is above the remaining balance, thus
9148         // inverting HTLC and `to_remote` output. HTLC will come second and
9149         // it wouldn't be seen by pre-#653 detection as we were enumerate()'ing
9150         // on a watched outputs vector (Vec<TxOut>) thus implicitly relying on
9151         // outputs order detection for correct spending children filtring.
9152
9153         let chanmon_cfgs = create_chanmon_cfgs(2);
9154         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9155         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9156         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9157
9158         // Create some initial channels
9159         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9160
9161         send_payment(&nodes[0], &vec!(&nodes[1])[..], 1_000_000);
9162         let (_, our_payment_hash, _) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 2_000_000);
9163         let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
9164         assert_eq!(local_txn[0].input.len(), 1);
9165         assert_eq!(local_txn[0].output.len(), 3);
9166         check_spends!(local_txn[0], chan_1.3);
9167
9168         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
9169         let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9170         connect_block(&nodes[0], &Block { header, txdata: vec![local_txn[0].clone()] });
9171         // We deliberately connect the local tx twice as this should provoke a failure calling
9172         // this test before #653 fix.
9173         chain::Listen::block_connected(&nodes[0].chain_monitor.chain_monitor, &Block { header, txdata: vec![local_txn[0].clone()] }, nodes[0].best_block_info().1 + 1);
9174         check_closed_broadcast!(nodes[0], true);
9175         check_added_monitors!(nodes[0], 1);
9176         connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
9177
9178         let htlc_timeout = {
9179                 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
9180                 assert_eq!(node_txn[1].input.len(), 1);
9181                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
9182                 check_spends!(node_txn[1], local_txn[0]);
9183                 node_txn[1].clone()
9184         };
9185
9186         let header_201 = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
9187         connect_block(&nodes[0], &Block { header: header_201, txdata: vec![htlc_timeout.clone()] });
9188         connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
9189         expect_payment_failed!(nodes[0], our_payment_hash, true);
9190 }
9191
9192 fn do_test_onchain_htlc_settlement_after_close(broadcast_alice: bool, go_onchain_before_fulfill: bool) {
9193         // If we route an HTLC, then learn the HTLC's preimage after the upstream channel has been
9194         // force-closed, we must claim that HTLC on-chain. (Given an HTLC forwarded from Alice --> Bob -->
9195         // Carol, Alice would be the upstream node, and Carol the downstream.)
9196         //
9197         // Steps of the test:
9198         // 1) Alice sends a HTLC to Carol through Bob.
9199         // 2) Carol doesn't settle the HTLC.
9200         // 3) If broadcast_alice is true, Alice force-closes her channel with Bob. Else Bob force closes.
9201         // Steps 4 and 5 may be reordered depending on go_onchain_before_fulfill.
9202         // 4) Bob sees the Alice's commitment on his chain or vice versa. An offered output is present
9203         //    but can't be claimed as Bob doesn't have yet knowledge of the preimage.
9204         // 5) Carol release the preimage to Bob off-chain.
9205         // 6) Bob claims the offered output on the broadcasted commitment.
9206         let chanmon_cfgs = create_chanmon_cfgs(3);
9207         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9208         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9209         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9210
9211         // Create some initial channels
9212         let chan_ab = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9213         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9214
9215         // Steps (1) and (2):
9216         // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
9217         let (payment_preimage, _payment_hash, _payment_secret) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3_000_000);
9218
9219         // Check that Alice's commitment transaction now contains an output for this HTLC.
9220         let alice_txn = get_local_commitment_txn!(nodes[0], chan_ab.2);
9221         check_spends!(alice_txn[0], chan_ab.3);
9222         assert_eq!(alice_txn[0].output.len(), 2);
9223         check_spends!(alice_txn[1], alice_txn[0]); // 2nd transaction is a non-final HTLC-timeout
9224         assert_eq!(alice_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
9225         assert_eq!(alice_txn.len(), 2);
9226
9227         // Steps (3) and (4):
9228         // If `go_onchain_before_fufill`, broadcast the relevant commitment transaction and check that Bob
9229         // responds by (1) broadcasting a channel update and (2) adding a new ChannelMonitor.
9230         let mut force_closing_node = 0; // Alice force-closes
9231         if !broadcast_alice { force_closing_node = 1; } // Bob force-closes
9232         nodes[force_closing_node].node.force_close_channel(&chan_ab.2).unwrap();
9233         check_closed_broadcast!(nodes[force_closing_node], true);
9234         check_added_monitors!(nodes[force_closing_node], 1);
9235         if go_onchain_before_fulfill {
9236                 let txn_to_broadcast = match broadcast_alice {
9237                         true => alice_txn.clone(),
9238                         false => get_local_commitment_txn!(nodes[1], chan_ab.2)
9239                 };
9240                 let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
9241                 connect_block(&nodes[1], &Block { header, txdata: vec![txn_to_broadcast[0].clone()]});
9242                 let mut bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
9243                 if broadcast_alice {
9244                         check_closed_broadcast!(nodes[1], true);
9245                         check_added_monitors!(nodes[1], 1);
9246                 }
9247                 assert_eq!(bob_txn.len(), 1);
9248                 check_spends!(bob_txn[0], chan_ab.3);
9249         }
9250
9251         // Step (5):
9252         // Carol then claims the funds and sends an update_fulfill message to Bob, and they go through the
9253         // process of removing the HTLC from their commitment transactions.
9254         assert!(nodes[2].node.claim_funds(payment_preimage));
9255         check_added_monitors!(nodes[2], 1);
9256         let carol_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
9257         assert!(carol_updates.update_add_htlcs.is_empty());
9258         assert!(carol_updates.update_fail_htlcs.is_empty());
9259         assert!(carol_updates.update_fail_malformed_htlcs.is_empty());
9260         assert!(carol_updates.update_fee.is_none());
9261         assert_eq!(carol_updates.update_fulfill_htlcs.len(), 1);
9262
9263         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &carol_updates.update_fulfill_htlcs[0]);
9264         expect_payment_forwarded!(nodes[1], if go_onchain_before_fulfill || force_closing_node == 1 { None } else { Some(1000) }, false);
9265         // If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
9266         if !go_onchain_before_fulfill && broadcast_alice {
9267                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9268                 assert_eq!(events.len(), 1);
9269                 match events[0] {
9270                         MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
9271                                 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
9272                         },
9273                         _ => panic!("Unexpected event"),
9274                 };
9275         }
9276         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &carol_updates.commitment_signed);
9277         // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
9278         // Carol<->Bob's updated commitment transaction info.
9279         check_added_monitors!(nodes[1], 2);
9280
9281         let events = nodes[1].node.get_and_clear_pending_msg_events();
9282         assert_eq!(events.len(), 2);
9283         let bob_revocation = match events[0] {
9284                 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
9285                         assert_eq!(*node_id, nodes[2].node.get_our_node_id());
9286                         (*msg).clone()
9287                 },
9288                 _ => panic!("Unexpected event"),
9289         };
9290         let bob_updates = match events[1] {
9291                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
9292                         assert_eq!(*node_id, nodes[2].node.get_our_node_id());
9293                         (*updates).clone()
9294                 },
9295                 _ => panic!("Unexpected event"),
9296         };
9297
9298         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revocation);
9299         check_added_monitors!(nodes[2], 1);
9300         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_updates.commitment_signed);
9301         check_added_monitors!(nodes[2], 1);
9302
9303         let events = nodes[2].node.get_and_clear_pending_msg_events();
9304         assert_eq!(events.len(), 1);
9305         let carol_revocation = match events[0] {
9306                 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
9307                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
9308                         (*msg).clone()
9309                 },
9310                 _ => panic!("Unexpected event"),
9311         };
9312         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &carol_revocation);
9313         check_added_monitors!(nodes[1], 1);
9314
9315         // If this test requires the force-closed channel to not be on-chain until after the fulfill,
9316         // here's where we put said channel's commitment tx on-chain.
9317         let mut txn_to_broadcast = alice_txn.clone();
9318         if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn!(nodes[1], chan_ab.2); }
9319         if !go_onchain_before_fulfill {
9320                 let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
9321                 connect_block(&nodes[1], &Block { header, txdata: vec![txn_to_broadcast[0].clone()]});
9322                 // If Bob was the one to force-close, he will have already passed these checks earlier.
9323                 if broadcast_alice {
9324                         check_closed_broadcast!(nodes[1], true);
9325                         check_added_monitors!(nodes[1], 1);
9326                 }
9327                 let mut bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
9328                 if broadcast_alice {
9329                         // In `connect_block()`, the ChainMonitor and ChannelManager are separately notified about a
9330                         // new block being connected. The ChannelManager being notified triggers a monitor update,
9331                         // which triggers broadcasting our commitment tx and an HTLC-claiming tx. The ChainMonitor
9332                         // being notified triggers the HTLC-claiming tx redundantly, resulting in 3 total txs being
9333                         // broadcasted.
9334                         assert_eq!(bob_txn.len(), 3);
9335                         check_spends!(bob_txn[1], chan_ab.3);
9336                 } else {
9337                         assert_eq!(bob_txn.len(), 2);
9338                         check_spends!(bob_txn[0], chan_ab.3);
9339                 }
9340         }
9341
9342         // Step (6):
9343         // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
9344         // broadcasted commitment transaction.
9345         {
9346                 let bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
9347                 if go_onchain_before_fulfill {
9348                         // Bob should now have an extra broadcasted tx, for the preimage-claiming transaction.
9349                         assert_eq!(bob_txn.len(), 2);
9350                 }
9351                 let script_weight = match broadcast_alice {
9352                         true => OFFERED_HTLC_SCRIPT_WEIGHT,
9353                         false => ACCEPTED_HTLC_SCRIPT_WEIGHT
9354                 };
9355                 // If Alice force-closed and Bob didn't receive her commitment transaction until after he
9356                 // received Carol's fulfill, he broadcasts the HTLC-output-claiming transaction first. Else if
9357                 // Bob force closed or if he found out about Alice's commitment tx before receiving Carol's
9358                 // fulfill, then he broadcasts the HTLC-output-claiming transaction second.
9359                 if broadcast_alice && !go_onchain_before_fulfill {
9360                         check_spends!(bob_txn[0], txn_to_broadcast[0]);
9361                         assert_eq!(bob_txn[0].input[0].witness.last().unwrap().len(), script_weight);
9362                 } else {
9363                         check_spends!(bob_txn[1], txn_to_broadcast[0]);
9364                         assert_eq!(bob_txn[1].input[0].witness.last().unwrap().len(), script_weight);
9365                 }
9366         }
9367 }
9368
9369 #[test]
9370 fn test_onchain_htlc_settlement_after_close() {
9371         do_test_onchain_htlc_settlement_after_close(true, true);
9372         do_test_onchain_htlc_settlement_after_close(false, true); // Technically redundant, but may as well
9373         do_test_onchain_htlc_settlement_after_close(true, false);
9374         do_test_onchain_htlc_settlement_after_close(false, false);
9375 }
9376
9377 #[test]
9378 fn test_duplicate_chan_id() {
9379         // Test that if a given peer tries to open a channel with the same channel_id as one that is
9380         // already open we reject it and keep the old channel.
9381         //
9382         // Previously, full_stack_target managed to figure out that if you tried to open two channels
9383         // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
9384         // the existing channel when we detect the duplicate new channel, screwing up our monitor
9385         // updating logic for the existing channel.
9386         let chanmon_cfgs = create_chanmon_cfgs(2);
9387         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9388         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9389         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9390
9391         // Create an initial channel
9392         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9393         let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9394         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_chan_msg);
9395         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9396
9397         // Try to create a second channel with the same temporary_channel_id as the first and check
9398         // that it is rejected.
9399         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_chan_msg);
9400         {
9401                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9402                 assert_eq!(events.len(), 1);
9403                 match events[0] {
9404                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9405                                 // Technically, at this point, nodes[1] would be justified in thinking both the
9406                                 // first (valid) and second (invalid) channels are closed, given they both have
9407                                 // the same non-temporary channel_id. However, currently we do not, so we just
9408                                 // move forward with it.
9409                                 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9410                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9411                         },
9412                         _ => panic!("Unexpected event"),
9413                 }
9414         }
9415
9416         // Move the first channel through the funding flow...
9417         let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], 100000, 42);
9418
9419         nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
9420         check_added_monitors!(nodes[0], 0);
9421
9422         let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9423         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
9424         {
9425                 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
9426                 assert_eq!(added_monitors.len(), 1);
9427                 assert_eq!(added_monitors[0].0, funding_output);
9428                 added_monitors.clear();
9429         }
9430         let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9431
9432         let funding_outpoint = ::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index };
9433         let channel_id = funding_outpoint.to_channel_id();
9434
9435         // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
9436         // temporary one).
9437
9438         // First try to open a second channel with a temporary channel id equal to the txid-based one.
9439         // Technically this is allowed by the spec, but we don't support it and there's little reason
9440         // to. Still, it shouldn't cause any other issues.
9441         open_chan_msg.temporary_channel_id = channel_id;
9442         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_chan_msg);
9443         {
9444                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9445                 assert_eq!(events.len(), 1);
9446                 match events[0] {
9447                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9448                                 // Technically, at this point, nodes[1] would be justified in thinking both
9449                                 // channels are closed, but currently we do not, so we just move forward with it.
9450                                 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9451                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9452                         },
9453                         _ => panic!("Unexpected event"),
9454                 }
9455         }
9456
9457         // Now try to create a second channel which has a duplicate funding output.
9458         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9459         let open_chan_2_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9460         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_chan_2_msg);
9461         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9462         create_funding_transaction(&nodes[0], 100000, 42); // Get and check the FundingGenerationReady event
9463
9464         let funding_created = {
9465                 let mut a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
9466                 let mut as_chan = a_channel_lock.by_id.get_mut(&open_chan_2_msg.temporary_channel_id).unwrap();
9467                 let logger = test_utils::TestLogger::new();
9468                 as_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap()
9469         };
9470         check_added_monitors!(nodes[0], 0);
9471         nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9472         // At this point we'll try to add a duplicate channel monitor, which will be rejected, but
9473         // still needs to be cleared here.
9474         check_added_monitors!(nodes[1], 1);
9475
9476         // ...still, nodes[1] will reject the duplicate channel.
9477         {
9478                 let events = nodes[1].node.get_and_clear_pending_msg_events();
9479                 assert_eq!(events.len(), 1);
9480                 match events[0] {
9481                         MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9482                                 // Technically, at this point, nodes[1] would be justified in thinking both
9483                                 // channels are closed, but currently we do not, so we just move forward with it.
9484                                 assert_eq!(msg.channel_id, channel_id);
9485                                 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9486                         },
9487                         _ => panic!("Unexpected event"),
9488                 }
9489         }
9490
9491         // finally, finish creating the original channel and send a payment over it to make sure
9492         // everything is functional.
9493         nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
9494         {
9495                 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9496                 assert_eq!(added_monitors.len(), 1);
9497                 assert_eq!(added_monitors[0].0, funding_output);
9498                 added_monitors.clear();
9499         }
9500
9501         let events_4 = nodes[0].node.get_and_clear_pending_events();
9502         assert_eq!(events_4.len(), 0);
9503         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9504         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].txid(), funding_output.txid);
9505
9506         let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9507         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
9508         update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9509         send_payment(&nodes[0], &[&nodes[1]], 8000000);
9510 }
9511
9512 #[test]
9513 fn test_error_chans_closed() {
9514         // Test that we properly handle error messages, closing appropriate channels.
9515         //
9516         // Prior to #787 we'd allow a peer to make us force-close a channel we had with a different
9517         // peer. The "real" fix for that is to index channels with peers_ids, however in the mean time
9518         // we can test various edge cases around it to ensure we don't regress.
9519         let chanmon_cfgs = create_chanmon_cfgs(3);
9520         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9521         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9522         let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9523
9524         // Create some initial channels
9525         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9526         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9527         let chan_3 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9528
9529         assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9530         assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
9531         assert_eq!(nodes[2].node.list_usable_channels().len(), 1);
9532
9533         // Closing a channel from a different peer has no effect
9534         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_3.2, data: "ERR".to_owned() });
9535         assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9536
9537         // Closing one channel doesn't impact others
9538         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_2.2, data: "ERR".to_owned() });
9539         check_added_monitors!(nodes[0], 1);
9540         check_closed_broadcast!(nodes[0], false);
9541         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
9542         assert_eq!(nodes[0].node.list_usable_channels().len(), 2);
9543         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);
9544         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);
9545
9546         // A null channel ID should close all channels
9547         let _chan_4 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9548         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: [0; 32], data: "ERR".to_owned() });
9549         check_added_monitors!(nodes[0], 2);
9550         let events = nodes[0].node.get_and_clear_pending_msg_events();
9551         assert_eq!(events.len(), 2);
9552         match events[0] {
9553                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9554                         assert_eq!(msg.contents.flags & 2, 2);
9555                 },
9556                 _ => panic!("Unexpected event"),
9557         }
9558         match events[1] {
9559                 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9560                         assert_eq!(msg.contents.flags & 2, 2);
9561                 },
9562                 _ => panic!("Unexpected event"),
9563         }
9564         // Note that at this point users of a standard PeerHandler will end up calling
9565         // peer_disconnected with no_connection_possible set to false, duplicating the
9566         // close-all-channels logic. That's OK, we don't want to end up not force-closing channels for
9567         // users with their own peer handling logic. We duplicate the call here, however.
9568         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9569         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9570
9571         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), true);
9572         assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9573         assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9574 }
9575
9576 #[test]
9577 fn test_invalid_funding_tx() {
9578         // Test that we properly handle invalid funding transactions sent to us from a peer.
9579         //
9580         // Previously, all other major lightning implementations had failed to properly sanitize
9581         // funding transactions from their counterparties, leading to a multi-implementation critical
9582         // security vulnerability (though we always sanitized properly, we've previously had
9583         // un-released crashes in the sanitization process).
9584         let chanmon_cfgs = create_chanmon_cfgs(2);
9585         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9586         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9587         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9588
9589         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_000, 42, None).unwrap();
9590         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
9591         nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9592
9593         let (temporary_channel_id, mut tx, _) = create_funding_transaction(&nodes[0], 100_000, 42);
9594         for output in tx.output.iter_mut() {
9595                 // Make the confirmed funding transaction have a bogus script_pubkey
9596                 output.script_pubkey = bitcoin::Script::new();
9597         }
9598
9599         nodes[0].node.funding_transaction_generated_unchecked(&temporary_channel_id, tx.clone(), 0).unwrap();
9600         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()));
9601         check_added_monitors!(nodes[1], 1);
9602
9603         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()));
9604         check_added_monitors!(nodes[0], 1);
9605
9606         let events_1 = nodes[0].node.get_and_clear_pending_events();
9607         assert_eq!(events_1.len(), 0);
9608
9609         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9610         assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9611         nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
9612
9613         confirm_transaction_at(&nodes[1], &tx, 1);
9614         check_added_monitors!(nodes[1], 1);
9615         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
9616         assert_eq!(events_2.len(), 1);
9617         if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
9618                 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
9619                 if let msgs::ErrorAction::SendErrorMessage { msg } = action {
9620                         assert_eq!(msg.data, "funding tx had wrong script/value or output index");
9621                 } else { panic!(); }
9622         } else { panic!(); }
9623         assert_eq!(nodes[1].node.list_channels().len(), 0);
9624 }
9625
9626 fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
9627         // In the first version of the chain::Confirm interface, after a refactor was made to not
9628         // broadcast CSV-locked transactions until their CSV lock is up, we wouldn't reliably broadcast
9629         // transactions after a `transactions_confirmed` call. Specifically, if the chain, provided via
9630         // `best_block_updated` is at height N, and a transaction output which we wish to spend at
9631         // height N-1 (due to a CSV to height N-1) is provided at height N, we will not broadcast the
9632         // spending transaction until height N+1 (or greater). This was due to the way
9633         // `ChannelMonitor::transactions_confirmed` worked, only checking if we should broadcast a
9634         // spending transaction at the height the input transaction was confirmed at, not whether we
9635         // should broadcast a spending transaction at the current height.
9636         // A second, similar, issue involved failing HTLCs backwards - because we only provided the
9637         // height at which transactions were confirmed to `OnchainTx::update_claims_view`, it wasn't
9638         // aware that the anti-reorg-delay had, in fact, already expired, waiting to fail-backwards
9639         // until we learned about an additional block.
9640         //
9641         // As an additional check, if `test_height_before_timelock` is set, we instead test that we
9642         // aren't broadcasting transactions too early (ie not broadcasting them at all).
9643         let chanmon_cfgs = create_chanmon_cfgs(3);
9644         let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9645         let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9646         let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9647         *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
9648
9649         create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
9650         let (chan_announce, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
9651         let (_, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
9652         nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
9653         nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
9654
9655         nodes[1].node.force_close_channel(&channel_id).unwrap();
9656         check_closed_broadcast!(nodes[1], true);
9657         check_added_monitors!(nodes[1], 1);
9658         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9659         assert_eq!(node_txn.len(), 1);
9660
9661         let conf_height = nodes[1].best_block_info().1;
9662         if !test_height_before_timelock {
9663                 connect_blocks(&nodes[1], 24 * 6);
9664         }
9665         nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9666                 &nodes[1].get_block_header(conf_height), &[(0, &node_txn[0])], conf_height);
9667         if test_height_before_timelock {
9668                 // If we confirmed the close transaction, but timelocks have not yet expired, we should not
9669                 // generate any events or broadcast any transactions
9670                 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
9671                 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
9672         } else {
9673                 // We should broadcast an HTLC transaction spending our funding transaction first
9674                 let spending_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9675                 assert_eq!(spending_txn.len(), 2);
9676                 assert_eq!(spending_txn[0], node_txn[0]);
9677                 check_spends!(spending_txn[1], node_txn[0]);
9678                 // We should also generate a SpendableOutputs event with the to_self output (as its
9679                 // timelock is up).
9680                 let descriptor_spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
9681                 assert_eq!(descriptor_spend_txn.len(), 1);
9682
9683                 // If we also discover that the HTLC-Timeout transaction was confirmed some time ago, we
9684                 // should immediately fail-backwards the HTLC to the previous hop, without waiting for an
9685                 // additional block built on top of the current chain.
9686                 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9687                         &nodes[1].get_block_header(conf_height + 1), &[(0, &spending_txn[1])], conf_height + 1);
9688                 expect_pending_htlcs_forwardable!(nodes[1]);
9689                 check_added_monitors!(nodes[1], 1);
9690
9691                 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9692                 assert!(updates.update_add_htlcs.is_empty());
9693                 assert!(updates.update_fulfill_htlcs.is_empty());
9694                 assert_eq!(updates.update_fail_htlcs.len(), 1);
9695                 assert!(updates.update_fail_malformed_htlcs.is_empty());
9696                 assert!(updates.update_fee.is_none());
9697                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
9698                 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
9699                 expect_payment_failed!(nodes[0], payment_hash, false);
9700                 expect_payment_failure_chan_update!(nodes[0], chan_announce.contents.short_channel_id, true);
9701         }
9702 }
9703
9704 #[test]
9705 fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
9706         do_test_tx_confirmed_skipping_blocks_immediate_broadcast(false);
9707         do_test_tx_confirmed_skipping_blocks_immediate_broadcast(true);
9708 }
9709
9710 #[test]
9711 fn test_keysend_payments_to_public_node() {
9712         let chanmon_cfgs = create_chanmon_cfgs(2);
9713         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9714         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9715         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9716
9717         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
9718         let network_graph = nodes[0].net_graph_msg_handler.network_graph.read().unwrap();
9719         let payer_pubkey = nodes[0].node.get_our_node_id();
9720         let payee_pubkey = nodes[1].node.get_our_node_id();
9721         let route = get_route(&payer_pubkey, &network_graph, &payee_pubkey, None,
9722                         None, &vec![], 10000, 40,
9723                         nodes[0].logger).unwrap();
9724
9725         let test_preimage = PaymentPreimage([42; 32]);
9726         let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage)).unwrap();
9727         check_added_monitors!(nodes[0], 1);
9728         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9729         assert_eq!(events.len(), 1);
9730         let event = events.pop().unwrap();
9731         let path = vec![&nodes[1]];
9732         pass_along_path(&nodes[0], &path, 10000, payment_hash, None, event, true, Some(test_preimage));
9733         claim_payment(&nodes[0], &path, test_preimage);
9734 }
9735
9736 #[test]
9737 fn test_keysend_payments_to_private_node() {
9738         let chanmon_cfgs = create_chanmon_cfgs(2);
9739         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9740         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9741         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9742
9743         let payer_pubkey = nodes[0].node.get_our_node_id();
9744         let payee_pubkey = nodes[1].node.get_our_node_id();
9745         nodes[0].node.peer_connected(&payee_pubkey, &msgs::Init { features: InitFeatures::known() });
9746         nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known() });
9747
9748         let _chan = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known());
9749         let network_graph = nodes[0].net_graph_msg_handler.network_graph.read().unwrap();
9750         let first_hops = nodes[0].node.list_usable_channels();
9751         let route = get_keysend_route(&payer_pubkey, &network_graph, &payee_pubkey,
9752                                 Some(&first_hops.iter().collect::<Vec<_>>()), &vec![], 10000, 40,
9753                                 nodes[0].logger).unwrap();
9754
9755         let test_preimage = PaymentPreimage([42; 32]);
9756         let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(test_preimage)).unwrap();
9757         check_added_monitors!(nodes[0], 1);
9758         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9759         assert_eq!(events.len(), 1);
9760         let event = events.pop().unwrap();
9761         let path = vec![&nodes[1]];
9762         pass_along_path(&nodes[0], &path, 10000, payment_hash, None, event, true, Some(test_preimage));
9763         claim_payment(&nodes[0], &path, test_preimage);
9764 }