]> git.bitcoin.ninja Git - rust-lightning/blob - src/ln/functional_tests.rs
Drop rng and SystemTime calls from KeysManager
[rust-lightning] / src / ln / functional_tests.rs
1 //! Tests that test standing up a network of ChannelManagers, creating channels, sending
2 //! payments/messages between them, and often checking the resulting ChannelMonitors are able to
3 //! claim outputs on-chain.
4
5 use chain::transaction::OutPoint;
6 use chain::chaininterface::{ChainListener, ChainWatchInterface};
7 use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor};
8 use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC, BREAKDOWN_TIMEOUT};
9 use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash};
10 use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
11 use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
12 use ln::onion_utils;
13 use ln::router::{Route, RouteHop};
14 use ln::msgs;
15 use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler,HTLCFailChannelUpdate, LocalFeatures, ErrorAction};
16 use util::test_utils;
17 use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
18 use util::errors::APIError;
19 use util::ser::{Writeable, ReadableArgs};
20 use util::config::UserConfig;
21 use util::rng;
22
23 use bitcoin::util::hash::BitcoinHash;
24 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
25 use bitcoin::util::bip143;
26 use bitcoin::util::address::Address;
27 use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey, ExtendedPrivKey};
28 use bitcoin::blockdata::block::{Block, BlockHeader};
29 use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn, SigHashType, OutPoint as BitcoinOutPoint};
30 use bitcoin::blockdata::script::{Builder, Script};
31 use bitcoin::blockdata::opcodes;
32 use bitcoin::blockdata::constants::genesis_block;
33 use bitcoin::network::constants::Network;
34
35 use bitcoin_hashes::sha256::Hash as Sha256;
36 use bitcoin_hashes::Hash;
37
38 use secp256k1::{Secp256k1, Message};
39 use secp256k1::key::{PublicKey,SecretKey};
40
41 use std::collections::{BTreeSet, HashMap, HashSet};
42 use std::default::Default;
43 use std::sync::Arc;
44 use std::sync::atomic::Ordering;
45 use std::mem;
46
47 use ln::functional_test_utils::*;
48
49 #[test]
50 fn test_async_inbound_update_fee() {
51         let mut nodes = create_network(2, &[None, None]);
52         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
53         let channel_id = chan.2;
54
55         // balancing
56         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
57
58         // A                                        B
59         // update_fee                            ->
60         // send (1) commitment_signed            -.
61         //                                       <- update_add_htlc/commitment_signed
62         // send (2) RAA (awaiting remote revoke) -.
63         // (1) commitment_signed is delivered    ->
64         //                                       .- send (3) RAA (awaiting remote revoke)
65         // (2) RAA is delivered                  ->
66         //                                       .- send (4) commitment_signed
67         //                                       <- (3) RAA is delivered
68         // send (5) commitment_signed            -.
69         //                                       <- (4) commitment_signed is delivered
70         // send (6) RAA                          -.
71         // (5) commitment_signed is delivered    ->
72         //                                       <- RAA
73         // (6) RAA is delivered                  ->
74
75         // First nodes[0] generates an update_fee
76         nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
77         check_added_monitors!(nodes[0], 1);
78
79         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
80         assert_eq!(events_0.len(), 1);
81         let (update_msg, commitment_signed) = match events_0[0] { // (1)
82                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
83                         (update_fee.as_ref(), commitment_signed)
84                 },
85                 _ => panic!("Unexpected event"),
86         };
87
88         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
89
90         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
91         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
92         nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
93         check_added_monitors!(nodes[1], 1);
94
95         let payment_event = {
96                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
97                 assert_eq!(events_1.len(), 1);
98                 SendEvent::from_event(events_1.remove(0))
99         };
100         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
101         assert_eq!(payment_event.msgs.len(), 1);
102
103         // ...now when the messages get delivered everyone should be happy
104         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
105         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
106         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
107         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
108         check_added_monitors!(nodes[0], 1);
109
110         // deliver(1), generate (3):
111         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
112         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
113         // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
114         check_added_monitors!(nodes[1], 1);
115
116         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap(); // deliver (2)
117         let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
118         assert!(bs_update.update_add_htlcs.is_empty()); // (4)
119         assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
120         assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
121         assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
122         assert!(bs_update.update_fee.is_none()); // (4)
123         check_added_monitors!(nodes[1], 1);
124
125         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap(); // deliver (3)
126         let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
127         assert!(as_update.update_add_htlcs.is_empty()); // (5)
128         assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
129         assert!(as_update.update_fail_htlcs.is_empty()); // (5)
130         assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
131         assert!(as_update.update_fee.is_none()); // (5)
132         check_added_monitors!(nodes[0], 1);
133
134         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed).unwrap(); // deliver (4)
135         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
136         // only (6) so get_event_msg's assert(len == 1) passes
137         check_added_monitors!(nodes[0], 1);
138
139         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed).unwrap(); // deliver (5)
140         let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
141         check_added_monitors!(nodes[1], 1);
142
143         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
144         check_added_monitors!(nodes[0], 1);
145
146         let events_2 = nodes[0].node.get_and_clear_pending_events();
147         assert_eq!(events_2.len(), 1);
148         match events_2[0] {
149                 Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
150                 _ => panic!("Unexpected event"),
151         }
152
153         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap(); // deliver (6)
154         check_added_monitors!(nodes[1], 1);
155 }
156
157 #[test]
158 fn test_update_fee_unordered_raa() {
159         // Just the intro to the previous test followed by an out-of-order RAA (which caused a
160         // crash in an earlier version of the update_fee patch)
161         let mut nodes = create_network(2, &[None, None]);
162         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
163         let channel_id = chan.2;
164
165         // balancing
166         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
167
168         // First nodes[0] generates an update_fee
169         nodes[0].node.update_fee(channel_id, get_feerate!(nodes[0], channel_id) + 20).unwrap();
170         check_added_monitors!(nodes[0], 1);
171
172         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
173         assert_eq!(events_0.len(), 1);
174         let update_msg = match events_0[0] { // (1)
175                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
176                         update_fee.as_ref()
177                 },
178                 _ => panic!("Unexpected event"),
179         };
180
181         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
182
183         // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
184         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
185         nodes[1].node.send_payment(nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(), our_payment_hash).unwrap();
186         check_added_monitors!(nodes[1], 1);
187
188         let payment_event = {
189                 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
190                 assert_eq!(events_1.len(), 1);
191                 SendEvent::from_event(events_1.remove(0))
192         };
193         assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
194         assert_eq!(payment_event.msgs.len(), 1);
195
196         // ...now when the messages get delivered everyone should be happy
197         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
198         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap(); // (2)
199         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
200         // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
201         check_added_monitors!(nodes[0], 1);
202
203         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap(); // deliver (2)
204         check_added_monitors!(nodes[1], 1);
205
206         // We can't continue, sadly, because our (1) now has a bogus signature
207 }
208
209 #[test]
210 fn test_multi_flight_update_fee() {
211         let nodes = create_network(2, &[None, None]);
212         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
213         let channel_id = chan.2;
214
215         // A                                        B
216         // update_fee/commitment_signed          ->
217         //                                       .- send (1) RAA and (2) commitment_signed
218         // update_fee (never committed)          ->
219         // (3) update_fee                        ->
220         // We have to manually generate the above update_fee, it is allowed by the protocol but we
221         // don't track which updates correspond to which revoke_and_ack responses so we're in
222         // AwaitingRAA mode and will not generate the update_fee yet.
223         //                                       <- (1) RAA delivered
224         // (3) is generated and send (4) CS      -.
225         // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
226         // know the per_commitment_point to use for it.
227         //                                       <- (2) commitment_signed delivered
228         // revoke_and_ack                        ->
229         //                                          B should send no response here
230         // (4) commitment_signed delivered       ->
231         //                                       <- RAA/commitment_signed delivered
232         // revoke_and_ack                        ->
233
234         // First nodes[0] generates an update_fee
235         let initial_feerate = get_feerate!(nodes[0], channel_id);
236         nodes[0].node.update_fee(channel_id, initial_feerate + 20).unwrap();
237         check_added_monitors!(nodes[0], 1);
238
239         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
240         assert_eq!(events_0.len(), 1);
241         let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
242                 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
243                         (update_fee.as_ref().unwrap(), commitment_signed)
244                 },
245                 _ => panic!("Unexpected event"),
246         };
247
248         // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
249         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1).unwrap();
250         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1).unwrap();
251         let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
252         check_added_monitors!(nodes[1], 1);
253
254         // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
255         // transaction:
256         nodes[0].node.update_fee(channel_id, initial_feerate + 40).unwrap();
257         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
258         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
259
260         // Create the (3) update_fee message that nodes[0] will generate before it does...
261         let mut update_msg_2 = msgs::UpdateFee {
262                 channel_id: update_msg_1.channel_id.clone(),
263                 feerate_per_kw: (initial_feerate + 30) as u32,
264         };
265
266         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
267
268         update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
269         // Deliver (3)
270         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2).unwrap();
271
272         // Deliver (1), generating (3) and (4)
273         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg).unwrap();
274         let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
275         check_added_monitors!(nodes[0], 1);
276         assert!(as_second_update.update_add_htlcs.is_empty());
277         assert!(as_second_update.update_fulfill_htlcs.is_empty());
278         assert!(as_second_update.update_fail_htlcs.is_empty());
279         assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
280         // Check that the update_fee newly generated matches what we delivered:
281         assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
282         assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
283
284         // Deliver (2) commitment_signed
285         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
286         let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
287         check_added_monitors!(nodes[0], 1);
288         // No commitment_signed so get_event_msg's assert(len == 1) passes
289
290         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg).unwrap();
291         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
292         check_added_monitors!(nodes[1], 1);
293
294         // Delever (4)
295         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed).unwrap();
296         let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
297         check_added_monitors!(nodes[1], 1);
298
299         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke).unwrap();
300         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
301         check_added_monitors!(nodes[0], 1);
302
303         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment).unwrap();
304         let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
305         // No commitment_signed so get_event_msg's assert(len == 1) passes
306         check_added_monitors!(nodes[0], 1);
307
308         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke).unwrap();
309         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
310         check_added_monitors!(nodes[1], 1);
311 }
312
313 #[test]
314 fn test_update_fee_vanilla() {
315         let nodes = create_network(2, &[None, None]);
316         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
317         let channel_id = chan.2;
318
319         let feerate = get_feerate!(nodes[0], channel_id);
320         nodes[0].node.update_fee(channel_id, feerate+25).unwrap();
321         check_added_monitors!(nodes[0], 1);
322
323         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
324         assert_eq!(events_0.len(), 1);
325         let (update_msg, commitment_signed) = match events_0[0] {
326                         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 } } => {
327                         (update_fee.as_ref(), commitment_signed)
328                 },
329                 _ => panic!("Unexpected event"),
330         };
331         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
332
333         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
334         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
335         check_added_monitors!(nodes[1], 1);
336
337         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
338         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
339         check_added_monitors!(nodes[0], 1);
340
341         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
342         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
343         // No commitment_signed so get_event_msg's assert(len == 1) passes
344         check_added_monitors!(nodes[0], 1);
345
346         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
347         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
348         check_added_monitors!(nodes[1], 1);
349 }
350
351 #[test]
352 fn test_update_fee_that_funder_cannot_afford() {
353         let nodes = create_network(2, &[None, None]);
354         let channel_value = 1888;
355         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 700000, LocalFeatures::new(), LocalFeatures::new());
356         let channel_id = chan.2;
357
358         let feerate = 260;
359         nodes[0].node.update_fee(channel_id, feerate).unwrap();
360         check_added_monitors!(nodes[0], 1);
361         let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
362
363         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap()).unwrap();
364
365         commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
366
367         //Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate of 260 set above.
368         //This value results in a fee that is exactly what the funder can afford (277 sat + 1000 sat channel reserve)
369         {
370                 let chan_lock = nodes[1].node.channel_state.lock().unwrap();
371                 let chan = chan_lock.by_id.get(&channel_id).unwrap();
372
373                 //We made sure neither party's funds are below the dust limit so -2 non-HTLC txns from number of outputs
374                 let num_htlcs = chan.last_local_commitment_txn[0].output.len() - 2;
375                 let total_fee: u64 = feerate * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
376                 let mut actual_fee = chan.last_local_commitment_txn[0].output.iter().fold(0, |acc, output| acc + output.value);
377                 actual_fee = channel_value - actual_fee;
378                 assert_eq!(total_fee, actual_fee);
379         } //drop the mutex
380
381         //Add 2 to the previous fee rate to the final fee increases by 1 (with no HTLCs the fee is essentially
382         //fee_rate*(724/1000) so the increment of 1*0.724 is rounded back down)
383         nodes[0].node.update_fee(channel_id, feerate+2).unwrap();
384         check_added_monitors!(nodes[0], 1);
385
386         let update2_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
387
388         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update2_msg.update_fee.unwrap()).unwrap();
389
390         //While producing the commitment_signed response after handling a received update_fee request the
391         //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
392         //Should produce and error.
393         let err = nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &update2_msg.commitment_signed).unwrap_err();
394
395         assert!(match err.err {
396                 "Funding remote cannot afford proposed new fee" => true,
397                 _ => false,
398         });
399
400         //clear the message we could not handle
401         nodes[1].node.get_and_clear_pending_msg_events();
402 }
403
404 #[test]
405 fn test_update_fee_with_fundee_update_add_htlc() {
406         let mut nodes = create_network(2, &[None, None]);
407         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
408         let channel_id = chan.2;
409
410         // balancing
411         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
412
413         let feerate = get_feerate!(nodes[0], channel_id);
414         nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
415         check_added_monitors!(nodes[0], 1);
416
417         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
418         assert_eq!(events_0.len(), 1);
419         let (update_msg, commitment_signed) = match events_0[0] {
420                         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 } } => {
421                         (update_fee.as_ref(), commitment_signed)
422                 },
423                 _ => panic!("Unexpected event"),
424         };
425         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
426         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
427         let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
428         check_added_monitors!(nodes[1], 1);
429
430         let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 800000, TEST_FINAL_CLTV).unwrap();
431
432         let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[1]);
433
434         // nothing happens since node[1] is in AwaitingRemoteRevoke
435         nodes[1].node.send_payment(route, our_payment_hash).unwrap();
436         {
437                 let mut added_monitors = nodes[0].chan_monitor.added_monitors.lock().unwrap();
438                 assert_eq!(added_monitors.len(), 0);
439                 added_monitors.clear();
440         }
441         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
442         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
443         // node[1] has nothing to do
444
445         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
446         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
447         check_added_monitors!(nodes[0], 1);
448
449         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
450         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
451         // No commitment_signed so get_event_msg's assert(len == 1) passes
452         check_added_monitors!(nodes[0], 1);
453         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
454         check_added_monitors!(nodes[1], 1);
455         // AwaitingRemoteRevoke ends here
456
457         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
458         assert_eq!(commitment_update.update_add_htlcs.len(), 1);
459         assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
460         assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
461         assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
462         assert_eq!(commitment_update.update_fee.is_none(), true);
463
464         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]).unwrap();
465         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
466         check_added_monitors!(nodes[0], 1);
467         let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
468
469         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke).unwrap();
470         check_added_monitors!(nodes[1], 1);
471         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
472
473         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed).unwrap();
474         check_added_monitors!(nodes[1], 1);
475         let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
476         // No commitment_signed so get_event_msg's assert(len == 1) passes
477
478         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke).unwrap();
479         check_added_monitors!(nodes[0], 1);
480         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
481
482         expect_pending_htlcs_forwardable!(nodes[0]);
483
484         let events = nodes[0].node.get_and_clear_pending_events();
485         assert_eq!(events.len(), 1);
486         match events[0] {
487                 Event::PaymentReceived { .. } => { },
488                 _ => panic!("Unexpected event"),
489         };
490
491         claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
492
493         send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
494         send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
495         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
496 }
497
498 #[test]
499 fn test_update_fee() {
500         let nodes = create_network(2, &[None, None]);
501         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
502         let channel_id = chan.2;
503
504         // A                                        B
505         // (1) update_fee/commitment_signed      ->
506         //                                       <- (2) revoke_and_ack
507         //                                       .- send (3) commitment_signed
508         // (4) update_fee/commitment_signed      ->
509         //                                       .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
510         //                                       <- (3) commitment_signed delivered
511         // send (6) revoke_and_ack               -.
512         //                                       <- (5) deliver revoke_and_ack
513         // (6) deliver revoke_and_ack            ->
514         //                                       .- send (7) commitment_signed in response to (4)
515         //                                       <- (7) deliver commitment_signed
516         // revoke_and_ack                        ->
517
518         // Create and deliver (1)...
519         let feerate = get_feerate!(nodes[0], channel_id);
520         nodes[0].node.update_fee(channel_id, feerate+20).unwrap();
521         check_added_monitors!(nodes[0], 1);
522
523         let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
524         assert_eq!(events_0.len(), 1);
525         let (update_msg, commitment_signed) = match events_0[0] {
526                         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 } } => {
527                         (update_fee.as_ref(), commitment_signed)
528                 },
529                 _ => panic!("Unexpected event"),
530         };
531         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
532
533         // Generate (2) and (3):
534         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
535         let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
536         check_added_monitors!(nodes[1], 1);
537
538         // Deliver (2):
539         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
540         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
541         check_added_monitors!(nodes[0], 1);
542
543         // Create and deliver (4)...
544         nodes[0].node.update_fee(channel_id, feerate+30).unwrap();
545         check_added_monitors!(nodes[0], 1);
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
555         nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap()).unwrap();
556         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed).unwrap();
557         check_added_monitors!(nodes[1], 1);
558         // ... creating (5)
559         let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
560         // No commitment_signed so get_event_msg's assert(len == 1) passes
561
562         // Handle (3), creating (6):
563         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0).unwrap();
564         check_added_monitors!(nodes[0], 1);
565         let revoke_msg_0 = 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
568         // Deliver (5):
569         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg).unwrap();
570         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
571         check_added_monitors!(nodes[0], 1);
572
573         // Deliver (6), creating (7):
574         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0).unwrap();
575         let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
576         assert!(commitment_update.update_add_htlcs.is_empty());
577         assert!(commitment_update.update_fulfill_htlcs.is_empty());
578         assert!(commitment_update.update_fail_htlcs.is_empty());
579         assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
580         assert!(commitment_update.update_fee.is_none());
581         check_added_monitors!(nodes[1], 1);
582
583         // Deliver (7)
584         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed).unwrap();
585         check_added_monitors!(nodes[0], 1);
586         let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
587         // No commitment_signed so get_event_msg's assert(len == 1) passes
588
589         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg).unwrap();
590         check_added_monitors!(nodes[1], 1);
591         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
592
593         assert_eq!(get_feerate!(nodes[0], channel_id), feerate + 30);
594         assert_eq!(get_feerate!(nodes[1], channel_id), feerate + 30);
595         close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
596 }
597
598 #[test]
599 fn pre_funding_lock_shutdown_test() {
600         // Test sending a shutdown prior to funding_locked after funding generation
601         let nodes = create_network(2, &[None, None]);
602         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 8000000, 0, LocalFeatures::new(), LocalFeatures::new());
603         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
604         nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
605         nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx; 1], &[1; 1]);
606
607         nodes[0].node.close_channel(&OutPoint::new(tx.txid(), 0).to_channel_id()).unwrap();
608         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
609         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
610         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
611         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
612
613         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
614         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
615         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
616         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
617         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
618         assert!(node_0_none.is_none());
619
620         assert!(nodes[0].node.list_channels().is_empty());
621         assert!(nodes[1].node.list_channels().is_empty());
622 }
623
624 #[test]
625 fn updates_shutdown_wait() {
626         // Test sending a shutdown with outstanding updates pending
627         let mut nodes = create_network(3, &[None, None, None]);
628         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
629         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
630         let route_1 = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
631         let route_2 = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
632
633         let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
634
635         nodes[0].node.close_channel(&chan_1.2).unwrap();
636         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
637         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
638         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
639         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
640
641         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
642         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
643
644         let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
645         if let Err(APIError::ChannelUnavailable {..}) = nodes[0].node.send_payment(route_1, payment_hash) {}
646         else { panic!("New sends should fail!") };
647         if let Err(APIError::ChannelUnavailable {..}) = nodes[1].node.send_payment(route_2, payment_hash) {}
648         else { panic!("New sends should fail!") };
649
650         assert!(nodes[2].node.claim_funds(our_payment_preimage));
651         check_added_monitors!(nodes[2], 1);
652         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
653         assert!(updates.update_add_htlcs.is_empty());
654         assert!(updates.update_fail_htlcs.is_empty());
655         assert!(updates.update_fail_malformed_htlcs.is_empty());
656         assert!(updates.update_fee.is_none());
657         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
658         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
659         check_added_monitors!(nodes[1], 1);
660         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
661         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
662
663         assert!(updates_2.update_add_htlcs.is_empty());
664         assert!(updates_2.update_fail_htlcs.is_empty());
665         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
666         assert!(updates_2.update_fee.is_none());
667         assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
668         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
669         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
670
671         let events = nodes[0].node.get_and_clear_pending_events();
672         assert_eq!(events.len(), 1);
673         match events[0] {
674                 Event::PaymentSent { ref payment_preimage } => {
675                         assert_eq!(our_payment_preimage, *payment_preimage);
676                 },
677                 _ => panic!("Unexpected event"),
678         }
679
680         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
681         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
682         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
683         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
684         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
685         assert!(node_0_none.is_none());
686
687         assert!(nodes[0].node.list_channels().is_empty());
688
689         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
690         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
691         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
692         assert!(nodes[1].node.list_channels().is_empty());
693         assert!(nodes[2].node.list_channels().is_empty());
694 }
695
696 #[test]
697 fn htlc_fail_async_shutdown() {
698         // Test HTLCs fail if shutdown starts even if messages are delivered out-of-order
699         let mut nodes = create_network(3, &[None, None, None]);
700         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
701         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
702
703         let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
704         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
705         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
706         check_added_monitors!(nodes[0], 1);
707         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
708         assert_eq!(updates.update_add_htlcs.len(), 1);
709         assert!(updates.update_fulfill_htlcs.is_empty());
710         assert!(updates.update_fail_htlcs.is_empty());
711         assert!(updates.update_fail_malformed_htlcs.is_empty());
712         assert!(updates.update_fee.is_none());
713
714         nodes[1].node.close_channel(&chan_1.2).unwrap();
715         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
716         nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
717         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
718
719         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
720         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
721         check_added_monitors!(nodes[1], 1);
722         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
723         commitment_signed_dance!(nodes[1], nodes[0], (), false, true, false);
724
725         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
726         assert!(updates_2.update_add_htlcs.is_empty());
727         assert!(updates_2.update_fulfill_htlcs.is_empty());
728         assert_eq!(updates_2.update_fail_htlcs.len(), 1);
729         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
730         assert!(updates_2.update_fee.is_none());
731
732         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fail_htlcs[0]).unwrap();
733         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
734
735         let events = nodes[0].node.get_and_clear_pending_events();
736         assert_eq!(events.len(), 1);
737         match events[0] {
738                 Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } => {
739                         assert_eq!(our_payment_hash, *payment_hash);
740                         assert!(!rejected_by_dest);
741                 },
742                 _ => panic!("Unexpected event"),
743         }
744
745         let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
746         assert_eq!(msg_events.len(), 2);
747         let node_0_closing_signed = match msg_events[0] {
748                 MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
749                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
750                         (*msg).clone()
751                 },
752                 _ => panic!("Unexpected event"),
753         };
754         match msg_events[1] {
755                 MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
756                         assert_eq!(msg.contents.short_channel_id, chan_1.0.contents.short_channel_id);
757                 },
758                 _ => panic!("Unexpected event"),
759         }
760
761         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
762         nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
763         let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
764         nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
765         let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
766         assert!(node_0_none.is_none());
767
768         assert!(nodes[0].node.list_channels().is_empty());
769
770         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
771         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
772         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
773         assert!(nodes[1].node.list_channels().is_empty());
774         assert!(nodes[2].node.list_channels().is_empty());
775 }
776
777 fn do_test_shutdown_rebroadcast(recv_count: u8) {
778         // Test that shutdown/closing_signed is re-sent on reconnect with a variable number of
779         // messages delivered prior to disconnect
780         let nodes = create_network(3, &[None, None, None]);
781         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
782         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
783
784         let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 100000);
785
786         nodes[1].node.close_channel(&chan_1.2).unwrap();
787         let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
788         if recv_count > 0 {
789                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_shutdown).unwrap();
790                 let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
791                 if recv_count > 1 {
792                         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown).unwrap();
793                 }
794         }
795
796         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
797         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
798
799         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
800         let node_0_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
801         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
802         let node_1_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
803
804         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish).unwrap();
805         let node_1_2nd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
806         assert!(node_1_shutdown == node_1_2nd_shutdown);
807
808         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_reestablish).unwrap();
809         let node_0_2nd_shutdown = if recv_count > 0 {
810                 let node_0_2nd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
811                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
812                 node_0_2nd_shutdown
813         } else {
814                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
815                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_2nd_shutdown).unwrap();
816                 get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id())
817         };
818         nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_2nd_shutdown).unwrap();
819
820         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
821         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
822
823         assert!(nodes[2].node.claim_funds(our_payment_preimage));
824         check_added_monitors!(nodes[2], 1);
825         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
826         assert!(updates.update_add_htlcs.is_empty());
827         assert!(updates.update_fail_htlcs.is_empty());
828         assert!(updates.update_fail_malformed_htlcs.is_empty());
829         assert!(updates.update_fee.is_none());
830         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
831         nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
832         check_added_monitors!(nodes[1], 1);
833         let updates_2 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
834         commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false);
835
836         assert!(updates_2.update_add_htlcs.is_empty());
837         assert!(updates_2.update_fail_htlcs.is_empty());
838         assert!(updates_2.update_fail_malformed_htlcs.is_empty());
839         assert!(updates_2.update_fee.is_none());
840         assert_eq!(updates_2.update_fulfill_htlcs.len(), 1);
841         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates_2.update_fulfill_htlcs[0]).unwrap();
842         commitment_signed_dance!(nodes[0], nodes[1], updates_2.commitment_signed, false, true);
843
844         let events = nodes[0].node.get_and_clear_pending_events();
845         assert_eq!(events.len(), 1);
846         match events[0] {
847                 Event::PaymentSent { ref payment_preimage } => {
848                         assert_eq!(our_payment_preimage, *payment_preimage);
849                 },
850                 _ => panic!("Unexpected event"),
851         }
852
853         let node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
854         if recv_count > 0 {
855                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed).unwrap();
856                 let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
857                 assert!(node_1_closing_signed.is_some());
858         }
859
860         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
861         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
862
863         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
864         let node_0_2nd_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id());
865         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
866         if recv_count == 0 {
867                 // If all closing_signeds weren't delivered we can just resume where we left off...
868                 let node_1_2nd_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
869
870                 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &node_1_2nd_reestablish).unwrap();
871                 let node_0_3rd_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
872                 assert!(node_0_2nd_shutdown == node_0_3rd_shutdown);
873
874                 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish).unwrap();
875                 let node_1_3rd_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
876                 assert!(node_1_3rd_shutdown == node_1_2nd_shutdown);
877
878                 nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_3rd_shutdown).unwrap();
879                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
880
881                 nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_1_3rd_shutdown).unwrap();
882                 let node_0_2nd_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
883                 assert!(node_0_closing_signed == node_0_2nd_closing_signed);
884
885                 nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed).unwrap();
886                 let (_, node_1_closing_signed) = get_closing_signed_broadcast!(nodes[1].node, nodes[0].node.get_our_node_id());
887                 nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed.unwrap()).unwrap();
888                 let (_, node_0_none) = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
889                 assert!(node_0_none.is_none());
890         } else {
891                 // If one node, however, received + responded with an identical closing_signed we end
892                 // up erroring and node[0] will try to broadcast its own latest commitment transaction.
893                 // There isn't really anything better we can do simply, but in the future we might
894                 // explore storing a set of recently-closed channels that got disconnected during
895                 // closing_signed and avoiding broadcasting local commitment txn for some timeout to
896                 // give our counterparty enough time to (potentially) broadcast a cooperative closing
897                 // transaction.
898                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
899
900                 if let Err(msgs::HandleError{action: Some(msgs::ErrorAction::SendErrorMessage{msg}), ..}) =
901                                 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_2nd_reestablish) {
902                         nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msg);
903                         let msgs::ErrorMessage {ref channel_id, ..} = msg;
904                         assert_eq!(*channel_id, chan_1.2);
905                 } else { panic!("Needed SendErrorMessage close"); }
906
907                 // get_closing_signed_broadcast usually eats the BroadcastChannelUpdate for us and
908                 // checks it, but in this case nodes[0] didn't ever get a chance to receive a
909                 // closing_signed so we do it ourselves
910                 check_closed_broadcast!(nodes[0]);
911         }
912
913         assert!(nodes[0].node.list_channels().is_empty());
914
915         assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
916         nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
917         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, true);
918         assert!(nodes[1].node.list_channels().is_empty());
919         assert!(nodes[2].node.list_channels().is_empty());
920 }
921
922 #[test]
923 fn test_shutdown_rebroadcast() {
924         do_test_shutdown_rebroadcast(0);
925         do_test_shutdown_rebroadcast(1);
926         do_test_shutdown_rebroadcast(2);
927 }
928
929 #[test]
930 fn fake_network_test() {
931         // Simple test which builds a network of ChannelManagers, connects them to each other, and
932         // tests that payments get routed and transactions broadcast in semi-reasonable ways.
933         let nodes = create_network(4, &[None, None, None, None]);
934
935         // Create some initial channels
936         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
937         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
938         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
939
940         // Rebalance the network a bit by relaying one payment through all the channels...
941         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
942         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
943         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
944         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
945
946         // Send some more payments
947         send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
948         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
949         send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
950
951         // Test failure packets
952         let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
953         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
954
955         // Add a new channel that skips 3
956         let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
957
958         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
959         send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
960         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
961         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
962         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
963         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
964         send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
965
966         // Do some rebalance loop payments, simultaneously
967         let mut hops = Vec::with_capacity(3);
968         hops.push(RouteHop {
969                 pubkey: nodes[2].node.get_our_node_id(),
970                 short_channel_id: chan_2.0.contents.short_channel_id,
971                 fee_msat: 0,
972                 cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
973         });
974         hops.push(RouteHop {
975                 pubkey: nodes[3].node.get_our_node_id(),
976                 short_channel_id: chan_3.0.contents.short_channel_id,
977                 fee_msat: 0,
978                 cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
979         });
980         hops.push(RouteHop {
981                 pubkey: nodes[1].node.get_our_node_id(),
982                 short_channel_id: chan_4.0.contents.short_channel_id,
983                 fee_msat: 1000000,
984                 cltv_expiry_delta: TEST_FINAL_CLTV,
985         });
986         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;
987         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;
988         let payment_preimage_1 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
989
990         let mut hops = Vec::with_capacity(3);
991         hops.push(RouteHop {
992                 pubkey: nodes[3].node.get_our_node_id(),
993                 short_channel_id: chan_4.0.contents.short_channel_id,
994                 fee_msat: 0,
995                 cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
996         });
997         hops.push(RouteHop {
998                 pubkey: nodes[2].node.get_our_node_id(),
999                 short_channel_id: chan_3.0.contents.short_channel_id,
1000                 fee_msat: 0,
1001                 cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
1002         });
1003         hops.push(RouteHop {
1004                 pubkey: nodes[1].node.get_our_node_id(),
1005                 short_channel_id: chan_2.0.contents.short_channel_id,
1006                 fee_msat: 1000000,
1007                 cltv_expiry_delta: TEST_FINAL_CLTV,
1008         });
1009         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;
1010         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;
1011         let payment_hash_2 = send_along_route(&nodes[1], Route { hops }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
1012
1013         // Claim the rebalances...
1014         fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
1015         claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
1016
1017         // Add a duplicate new channel from 2 to 4
1018         let chan_5 = create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
1019
1020         // Send some payments across both channels
1021         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1022         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1023         let payment_preimage_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000).0;
1024
1025         route_over_limit(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 3000000);
1026
1027         //TODO: Test that routes work again here as we've been notified that the channel is full
1028
1029         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_3);
1030         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_4);
1031         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], payment_preimage_5);
1032
1033         // Close down the channels...
1034         close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1035         close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1036         close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1037         close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1038         close_channel(&nodes[1], &nodes[3], &chan_5.2, chan_5.3, false);
1039 }
1040
1041 #[test]
1042 fn holding_cell_htlc_counting() {
1043         // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1044         // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1045         // commitment dance rounds.
1046         let mut nodes = create_network(3, &[None, None, None]);
1047         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1048         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
1049
1050         let mut payments = Vec::new();
1051         for _ in 0..::ln::channel::OUR_MAX_HTLCS {
1052                 let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1053                 let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1054                 nodes[1].node.send_payment(route, payment_hash).unwrap();
1055                 payments.push((payment_preimage, payment_hash));
1056         }
1057         check_added_monitors!(nodes[1], 1);
1058
1059         let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1060         assert_eq!(events.len(), 1);
1061         let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1062         assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1063
1064         // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1065         // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1066         // another HTLC.
1067         let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1068         let (_, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
1069         if let APIError::ChannelUnavailable { err } = nodes[1].node.send_payment(route, payment_hash_1).unwrap_err() {
1070                 assert_eq!(err, "Cannot push more than their max accepted HTLCs");
1071         } else { panic!("Unexpected event"); }
1072
1073         // This should also be true if we try to forward a payment.
1074         let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
1075         let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
1076         nodes[0].node.send_payment(route, payment_hash_2).unwrap();
1077         check_added_monitors!(nodes[0], 1);
1078
1079         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1080         assert_eq!(events.len(), 1);
1081         let payment_event = SendEvent::from_event(events.pop().unwrap());
1082         assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1083
1084         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
1085         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1086         // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1087         // fails), the second will process the resulting failure and fail the HTLC backward.
1088         expect_pending_htlcs_forwardable!(nodes[1]);
1089         expect_pending_htlcs_forwardable!(nodes[1]);
1090         check_added_monitors!(nodes[1], 1);
1091
1092         let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1093         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]).unwrap();
1094         commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1095
1096         let events = nodes[0].node.get_and_clear_pending_msg_events();
1097         assert_eq!(events.len(), 1);
1098         match events[0] {
1099                 MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg }} => {
1100                         assert_eq!(msg.contents.short_channel_id, chan_2.0.contents.short_channel_id);
1101                 },
1102                 _ => panic!("Unexpected event"),
1103         }
1104
1105         let events = nodes[0].node.get_and_clear_pending_events();
1106         assert_eq!(events.len(), 1);
1107         match events[0] {
1108                 Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1109                         assert_eq!(payment_hash, payment_hash_2);
1110                         assert!(!rejected_by_dest);
1111                 },
1112                 _ => panic!("Unexpected event"),
1113         }
1114
1115         // Now forward all the pending HTLCs and claim them back
1116         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]).unwrap();
1117         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg).unwrap();
1118         check_added_monitors!(nodes[2], 1);
1119
1120         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1121         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1122         check_added_monitors!(nodes[1], 1);
1123         let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1124
1125         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1126         check_added_monitors!(nodes[1], 1);
1127         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1128
1129         for ref update in as_updates.update_add_htlcs.iter() {
1130                 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update).unwrap();
1131         }
1132         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed).unwrap();
1133         check_added_monitors!(nodes[2], 1);
1134         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
1135         check_added_monitors!(nodes[2], 1);
1136         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1137
1138         nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1139         check_added_monitors!(nodes[1], 1);
1140         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed).unwrap();
1141         check_added_monitors!(nodes[1], 1);
1142         let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1143
1144         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa).unwrap();
1145         check_added_monitors!(nodes[2], 1);
1146
1147         expect_pending_htlcs_forwardable!(nodes[2]);
1148
1149         let events = nodes[2].node.get_and_clear_pending_events();
1150         assert_eq!(events.len(), payments.len());
1151         for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1152                 match event {
1153                         &Event::PaymentReceived { ref payment_hash, .. } => {
1154                                 assert_eq!(*payment_hash, *hash);
1155                         },
1156                         _ => panic!("Unexpected event"),
1157                 };
1158         }
1159
1160         for (preimage, _) in payments.drain(..) {
1161                 claim_payment(&nodes[1], &[&nodes[2]], preimage);
1162         }
1163
1164         send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1165 }
1166
1167 #[test]
1168 fn duplicate_htlc_test() {
1169         // Test that we accept duplicate payment_hash HTLCs across the network and that
1170         // claiming/failing them are all separate and don't affect each other
1171         let mut nodes = create_network(6, &[None, None, None, None, None, None]);
1172
1173         // Create some initial channels to route via 3 to 4/5 from 0/1/2
1174         create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
1175         create_announced_chan_between_nodes(&nodes, 1, 3, LocalFeatures::new(), LocalFeatures::new());
1176         create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
1177         create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
1178         create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
1179
1180         let (payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
1181
1182         *nodes[0].network_payment_count.borrow_mut() -= 1;
1183         assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
1184
1185         *nodes[0].network_payment_count.borrow_mut() -= 1;
1186         assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
1187
1188         claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
1189         fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
1190         claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
1191 }
1192
1193 fn do_channel_reserve_test(test_recv: bool) {
1194         use util::rng;
1195         use std::sync::atomic::Ordering;
1196         use ln::msgs::HandleError;
1197
1198         let mut nodes = create_network(3, &[None, None, None]);
1199         let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
1200         let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1900, 1001, LocalFeatures::new(), LocalFeatures::new());
1201
1202         let mut stat01 = get_channel_value_stat!(nodes[0], chan_1.2);
1203         let mut stat11 = get_channel_value_stat!(nodes[1], chan_1.2);
1204
1205         let mut stat12 = get_channel_value_stat!(nodes[1], chan_2.2);
1206         let mut stat22 = get_channel_value_stat!(nodes[2], chan_2.2);
1207
1208         macro_rules! get_route_and_payment_hash {
1209                 ($recv_value: expr) => {{
1210                         let route = nodes[0].router.get_route(&nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV).unwrap();
1211                         let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1212                         (route, payment_hash, payment_preimage)
1213                 }}
1214         };
1215
1216         macro_rules! expect_forward {
1217                 ($node: expr) => {{
1218                         let mut events = $node.node.get_and_clear_pending_msg_events();
1219                         assert_eq!(events.len(), 1);
1220                         check_added_monitors!($node, 1);
1221                         let payment_event = SendEvent::from_event(events.remove(0));
1222                         payment_event
1223                 }}
1224         }
1225
1226         let feemsat = 239; // somehow we know?
1227         let total_fee_msat = (nodes.len() - 2) as u64 * 239;
1228
1229         let recv_value_0 = stat01.their_max_htlc_value_in_flight_msat - total_fee_msat;
1230
1231         // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
1232         {
1233                 let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_0 + 1);
1234                 assert!(route.hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
1235                 let err = nodes[0].node.send_payment(route, our_payment_hash).err().unwrap();
1236                 match err {
1237                         APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight"),
1238                         _ => panic!("Unknown error variants"),
1239                 }
1240         }
1241
1242         let mut htlc_id = 0;
1243         // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
1244         // nodes[0]'s wealth
1245         loop {
1246                 let amt_msat = recv_value_0 + total_fee_msat;
1247                 if stat01.value_to_self_msat - amt_msat < stat01.channel_reserve_msat {
1248                         break;
1249                 }
1250                 send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_0);
1251                 htlc_id += 1;
1252
1253                 let (stat01_, stat11_, stat12_, stat22_) = (
1254                         get_channel_value_stat!(nodes[0], chan_1.2),
1255                         get_channel_value_stat!(nodes[1], chan_1.2),
1256                         get_channel_value_stat!(nodes[1], chan_2.2),
1257                         get_channel_value_stat!(nodes[2], chan_2.2),
1258                 );
1259
1260                 assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
1261                 assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
1262                 assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
1263                 assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
1264                 stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
1265         }
1266
1267         {
1268                 let recv_value = stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat;
1269                 // attempt to get channel_reserve violation
1270                 let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value + 1);
1271                 let err = nodes[0].node.send_payment(route.clone(), our_payment_hash).err().unwrap();
1272                 match err {
1273                         APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
1274                         _ => panic!("Unknown error variants"),
1275                 }
1276         }
1277
1278         // adding pending output
1279         let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat)/2;
1280         let amt_msat_1 = recv_value_1 + total_fee_msat;
1281
1282         let (route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash!(recv_value_1);
1283         let payment_event_1 = {
1284                 nodes[0].node.send_payment(route_1, our_payment_hash_1).unwrap();
1285                 check_added_monitors!(nodes[0], 1);
1286
1287                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1288                 assert_eq!(events.len(), 1);
1289                 SendEvent::from_event(events.remove(0))
1290         };
1291         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]).unwrap();
1292
1293         // channel reserve test with htlc pending output > 0
1294         let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat;
1295         {
1296                 let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
1297                 match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
1298                         APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
1299                         _ => panic!("Unknown error variants"),
1300                 }
1301         }
1302
1303         {
1304                 // test channel_reserve test on nodes[1] side
1305                 let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
1306
1307                 // Need to manually create update_add_htlc message to go around the channel reserve check in send_htlc()
1308                 let secp_ctx = Secp256k1::new();
1309                 let session_priv = SecretKey::from_slice(&{
1310                         let mut session_key = [0; 32];
1311                         rng::fill_bytes(&mut session_key);
1312                         session_key
1313                 }).expect("RNG is bad!");
1314
1315                 let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
1316                 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
1317                 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
1318                 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
1319                 let msg = msgs::UpdateAddHTLC {
1320                         channel_id: chan_1.2,
1321                         htlc_id,
1322                         amount_msat: htlc_msat,
1323                         payment_hash: our_payment_hash,
1324                         cltv_expiry: htlc_cltv,
1325                         onion_routing_packet: onion_packet,
1326                 };
1327
1328                 if test_recv {
1329                         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).err().unwrap();
1330                         match err {
1331                                 HandleError{err, .. } => assert_eq!(err, "Remote HTLC add would put them over their reserve value"),
1332                         }
1333                         // If we send a garbage message, the channel should get closed, making the rest of this test case fail.
1334                         assert_eq!(nodes[1].node.list_channels().len(), 1);
1335                         assert_eq!(nodes[1].node.list_channels().len(), 1);
1336                         check_closed_broadcast!(nodes[1]);
1337                         return;
1338                 }
1339         }
1340
1341         // split the rest to test holding cell
1342         let recv_value_21 = recv_value_2/2;
1343         let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat;
1344         {
1345                 let stat = get_channel_value_stat!(nodes[0], chan_1.2);
1346                 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), stat.channel_reserve_msat);
1347         }
1348
1349         // now see if they go through on both sides
1350         let (route_21, our_payment_hash_21, our_payment_preimage_21) = get_route_and_payment_hash!(recv_value_21);
1351         // but this will stuck in the holding cell
1352         nodes[0].node.send_payment(route_21, our_payment_hash_21).unwrap();
1353         check_added_monitors!(nodes[0], 0);
1354         let events = nodes[0].node.get_and_clear_pending_events();
1355         assert_eq!(events.len(), 0);
1356
1357         // test with outbound holding cell amount > 0
1358         {
1359                 let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
1360                 match nodes[0].node.send_payment(route, our_payment_hash).err().unwrap() {
1361                         APIError::ChannelUnavailable{err} => assert_eq!(err, "Cannot send value that would put us over the reserve value"),
1362                         _ => panic!("Unknown error variants"),
1363                 }
1364         }
1365
1366         let (route_22, our_payment_hash_22, our_payment_preimage_22) = get_route_and_payment_hash!(recv_value_22);
1367         // this will also stuck in the holding cell
1368         nodes[0].node.send_payment(route_22, our_payment_hash_22).unwrap();
1369         check_added_monitors!(nodes[0], 0);
1370         assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1371         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1372
1373         // flush the pending htlc
1374         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg).unwrap();
1375         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1376         check_added_monitors!(nodes[1], 1);
1377
1378         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
1379         check_added_monitors!(nodes[0], 1);
1380         let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1381
1382         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed).unwrap();
1383         let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1384         // No commitment_signed so get_event_msg's assert(len == 1) passes
1385         check_added_monitors!(nodes[0], 1);
1386
1387         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
1388         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1389         check_added_monitors!(nodes[1], 1);
1390
1391         expect_pending_htlcs_forwardable!(nodes[1]);
1392
1393         let ref payment_event_11 = expect_forward!(nodes[1]);
1394         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]).unwrap();
1395         commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
1396
1397         expect_pending_htlcs_forwardable!(nodes[2]);
1398         expect_payment_received!(nodes[2], our_payment_hash_1, recv_value_1);
1399
1400         // flush the htlcs in the holding cell
1401         assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
1402         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]).unwrap();
1403         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]).unwrap();
1404         commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
1405         expect_pending_htlcs_forwardable!(nodes[1]);
1406
1407         let ref payment_event_3 = expect_forward!(nodes[1]);
1408         assert_eq!(payment_event_3.msgs.len(), 2);
1409         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]).unwrap();
1410         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]).unwrap();
1411
1412         commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
1413         expect_pending_htlcs_forwardable!(nodes[2]);
1414
1415         let events = nodes[2].node.get_and_clear_pending_events();
1416         assert_eq!(events.len(), 2);
1417         match events[0] {
1418                 Event::PaymentReceived { ref payment_hash, amt } => {
1419                         assert_eq!(our_payment_hash_21, *payment_hash);
1420                         assert_eq!(recv_value_21, amt);
1421                 },
1422                 _ => panic!("Unexpected event"),
1423         }
1424         match events[1] {
1425                 Event::PaymentReceived { ref payment_hash, amt } => {
1426                         assert_eq!(our_payment_hash_22, *payment_hash);
1427                         assert_eq!(recv_value_22, amt);
1428                 },
1429                 _ => panic!("Unexpected event"),
1430         }
1431
1432         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
1433         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
1434         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
1435
1436         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);
1437         let stat0 = get_channel_value_stat!(nodes[0], chan_1.2);
1438         assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
1439         assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat);
1440
1441         let stat2 = get_channel_value_stat!(nodes[2], chan_2.2);
1442         assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22);
1443 }
1444
1445 #[test]
1446 fn channel_reserve_test() {
1447         do_channel_reserve_test(false);
1448         do_channel_reserve_test(true);
1449 }
1450
1451 #[test]
1452 fn channel_reserve_in_flight_removes() {
1453         // In cases where one side claims an HTLC, it thinks it has additional available funds that it
1454         // can send to its counterparty, but due to update ordering, the other side may not yet have
1455         // considered those HTLCs fully removed.
1456         // This tests that we don't count HTLCs which will not be included in the next remote
1457         // commitment transaction towards the reserve value (as it implies no commitment transaction
1458         // will be generated which violates the remote reserve value).
1459         // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
1460         // To test this we:
1461         //  * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
1462         //    you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
1463         //    you only consider the value of the first HTLC, it may not),
1464         //  * start routing a third HTLC from A to B,
1465         //  * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
1466         //    the other claim in its holding cell, as it immediately goes into AwaitingRAA),
1467         //  * deliver the first fulfill from B
1468         //  * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
1469         //    claim,
1470         //  * deliver A's response CS and RAA.
1471         //    This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
1472         //    removed it fully. B now has the push_msat plus the first two HTLCs in value.
1473         //  * Now B happily sends another HTLC, potentially violating its reserve value from A's point
1474         //    of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
1475         let mut nodes = create_network(2, &[None, None]);
1476         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1477
1478         let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2);
1479         // Route the first two HTLCs.
1480         let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000);
1481         let (payment_preimage_2, _) = route_payment(&nodes[0], &[&nodes[1]], 20000);
1482
1483         // Start routing the third HTLC (this is just used to get everyone in the right state).
1484         let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]);
1485         let send_1 = {
1486                 let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
1487                 nodes[0].node.send_payment(route, payment_hash_3).unwrap();
1488                 check_added_monitors!(nodes[0], 1);
1489                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1490                 assert_eq!(events.len(), 1);
1491                 SendEvent::from_event(events.remove(0))
1492         };
1493
1494         // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
1495         // initial fulfill/CS.
1496         assert!(nodes[1].node.claim_funds(payment_preimage_1));
1497         check_added_monitors!(nodes[1], 1);
1498         let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1499
1500         // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
1501         // remove the second HTLC when we send the HTLC back from B to A.
1502         assert!(nodes[1].node.claim_funds(payment_preimage_2));
1503         check_added_monitors!(nodes[1], 1);
1504         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1505
1506         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]).unwrap();
1507         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed).unwrap();
1508         check_added_monitors!(nodes[0], 1);
1509         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1510         expect_payment_sent!(nodes[0], payment_preimage_1);
1511
1512         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]).unwrap();
1513         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg).unwrap();
1514         check_added_monitors!(nodes[1], 1);
1515         // B is already AwaitingRAA, so cant generate a CS here
1516         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1517
1518         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
1519         check_added_monitors!(nodes[1], 1);
1520         let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1521
1522         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
1523         check_added_monitors!(nodes[0], 1);
1524         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1525
1526         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
1527         check_added_monitors!(nodes[1], 1);
1528         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1529
1530         // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
1531         // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
1532         // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
1533         // can no longer broadcast a commitment transaction with it and B has the preimage so can go
1534         // on-chain as necessary).
1535         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]).unwrap();
1536         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed).unwrap();
1537         check_added_monitors!(nodes[0], 1);
1538         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1539         expect_payment_sent!(nodes[0], payment_preimage_2);
1540
1541         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
1542         check_added_monitors!(nodes[1], 1);
1543         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1544
1545         expect_pending_htlcs_forwardable!(nodes[1]);
1546         expect_payment_received!(nodes[1], payment_hash_3, 100000);
1547
1548         // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
1549         // resolve the second HTLC from A's point of view.
1550         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
1551         check_added_monitors!(nodes[0], 1);
1552         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1553
1554         // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
1555         // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
1556         let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[1]);
1557         let send_2 = {
1558                 let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 10000, TEST_FINAL_CLTV).unwrap();
1559                 nodes[1].node.send_payment(route, payment_hash_4).unwrap();
1560                 check_added_monitors!(nodes[1], 1);
1561                 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1562                 assert_eq!(events.len(), 1);
1563                 SendEvent::from_event(events.remove(0))
1564         };
1565
1566         nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]).unwrap();
1567         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg).unwrap();
1568         check_added_monitors!(nodes[0], 1);
1569         let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1570
1571         // Now just resolve all the outstanding messages/HTLCs for completeness...
1572
1573         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
1574         check_added_monitors!(nodes[1], 1);
1575         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1576
1577         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa).unwrap();
1578         check_added_monitors!(nodes[1], 1);
1579
1580         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
1581         check_added_monitors!(nodes[0], 1);
1582         let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1583
1584         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed).unwrap();
1585         check_added_monitors!(nodes[1], 1);
1586         let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
1587
1588         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa).unwrap();
1589         check_added_monitors!(nodes[0], 1);
1590
1591         expect_pending_htlcs_forwardable!(nodes[0]);
1592         expect_payment_received!(nodes[0], payment_hash_4, 10000);
1593
1594         claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
1595         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
1596 }
1597
1598 #[test]
1599 fn channel_monitor_network_test() {
1600         // Simple test which builds a network of ChannelManagers, connects them to each other, and
1601         // tests that ChannelMonitor is able to recover from various states.
1602         let nodes = create_network(5, &[None, None, None, None, None]);
1603
1604         // Create some initial channels
1605         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1606         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
1607         let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
1608         let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
1609
1610         // Rebalance the network a bit by relaying one payment through all the channels...
1611         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
1612         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
1613         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
1614         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
1615
1616         // Simple case with no pending HTLCs:
1617         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), true);
1618         {
1619                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
1620                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1621                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
1622                 test_txn_broadcast(&nodes[0], &chan_1, None, HTLCType::NONE);
1623         }
1624         get_announce_close_broadcast_events(&nodes, 0, 1);
1625         assert_eq!(nodes[0].node.list_channels().len(), 0);
1626         assert_eq!(nodes[1].node.list_channels().len(), 1);
1627
1628         // One pending HTLC is discarded by the force-close:
1629         let payment_preimage_1 = route_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 3000000).0;
1630
1631         // Simple case of one pending HTLC to HTLC-Timeout
1632         nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), true);
1633         {
1634                 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
1635                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1636                 nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn.drain(..).next().unwrap()] }, 1);
1637                 test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::NONE);
1638         }
1639         get_announce_close_broadcast_events(&nodes, 1, 2);
1640         assert_eq!(nodes[1].node.list_channels().len(), 0);
1641         assert_eq!(nodes[2].node.list_channels().len(), 1);
1642
1643         macro_rules! claim_funds {
1644                 ($node: expr, $prev_node: expr, $preimage: expr) => {
1645                         {
1646                                 assert!($node.node.claim_funds($preimage));
1647                                 check_added_monitors!($node, 1);
1648
1649                                 let events = $node.node.get_and_clear_pending_msg_events();
1650                                 assert_eq!(events.len(), 1);
1651                                 match events[0] {
1652                                         MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
1653                                                 assert!(update_add_htlcs.is_empty());
1654                                                 assert!(update_fail_htlcs.is_empty());
1655                                                 assert_eq!(*node_id, $prev_node.node.get_our_node_id());
1656                                         },
1657                                         _ => panic!("Unexpected event"),
1658                                 };
1659                         }
1660                 }
1661         }
1662
1663         // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
1664         // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
1665         nodes[2].node.peer_disconnected(&nodes[3].node.get_our_node_id(), true);
1666         {
1667                 let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
1668
1669                 // Claim the payment on nodes[3], giving it knowledge of the preimage
1670                 claim_funds!(nodes[3], nodes[2], payment_preimage_1);
1671
1672                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1673                 nodes[3].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 1);
1674
1675                 check_preimage_claim(&nodes[3], &node_txn);
1676         }
1677         get_announce_close_broadcast_events(&nodes, 2, 3);
1678         assert_eq!(nodes[2].node.list_channels().len(), 0);
1679         assert_eq!(nodes[3].node.list_channels().len(), 1);
1680
1681         { // Cheat and reset nodes[4]'s height to 1
1682                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1683                 nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![] }, 1);
1684         }
1685
1686         assert_eq!(nodes[3].node.latest_block_height.load(Ordering::Acquire), 1);
1687         assert_eq!(nodes[4].node.latest_block_height.load(Ordering::Acquire), 1);
1688         // One pending HTLC to time out:
1689         let payment_preimage_2 = route_payment(&nodes[3], &vec!(&nodes[4])[..], 3000000).0;
1690         // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
1691         // buffer space).
1692
1693         {
1694                 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1695                 nodes[3].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
1696                 for i in 3..TEST_FINAL_CLTV + 2 + LATENCY_GRACE_PERIOD_BLOCKS + 1 {
1697                         header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1698                         nodes[3].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
1699                 }
1700
1701                 let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
1702
1703                 // Claim the payment on nodes[4], giving it knowledge of the preimage
1704                 claim_funds!(nodes[4], nodes[3], payment_preimage_2);
1705
1706                 header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1707                 nodes[4].chain_monitor.block_connected_checked(&header, 2, &Vec::new()[..], &[0; 0]);
1708                 for i in 3..TEST_FINAL_CLTV + 2 - CLTV_CLAIM_BUFFER + 1 {
1709                         header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1710                         nodes[4].chain_monitor.block_connected_checked(&header, i, &Vec::new()[..], &[0; 0]);
1711                 }
1712
1713                 test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
1714
1715                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1716                 nodes[4].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, TEST_FINAL_CLTV - 5);
1717
1718                 check_preimage_claim(&nodes[4], &node_txn);
1719         }
1720         get_announce_close_broadcast_events(&nodes, 3, 4);
1721         assert_eq!(nodes[3].node.list_channels().len(), 0);
1722         assert_eq!(nodes[4].node.list_channels().len(), 0);
1723 }
1724
1725 #[test]
1726 fn test_justice_tx() {
1727         // Test justice txn built on revoked HTLC-Success tx, against both sides
1728
1729         let nodes = create_network(2, &[None, None]);
1730         // Create some new channels:
1731         let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1732
1733         // A pending HTLC which will be revoked:
1734         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
1735         // Get the will-be-revoked local txn from nodes[0]
1736         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
1737         assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
1738         assert_eq!(revoked_local_txn[0].input.len(), 1);
1739         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
1740         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
1741         assert_eq!(revoked_local_txn[1].input.len(), 1);
1742         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
1743         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
1744         // Revoke the old state
1745         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
1746
1747         {
1748                 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1749                 nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1750                 {
1751                         let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
1752                         assert_eq!(node_txn.len(), 3);
1753                         assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
1754                         assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
1755
1756                         check_spends!(node_txn[0], revoked_local_txn[0].clone());
1757                         node_txn.swap_remove(0);
1758                 }
1759                 test_txn_broadcast(&nodes[1], &chan_5, None, HTLCType::NONE);
1760
1761                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1762                 let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
1763                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1764                 nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
1765                 test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone());
1766         }
1767         get_announce_close_broadcast_events(&nodes, 0, 1);
1768
1769         assert_eq!(nodes[0].node.list_channels().len(), 0);
1770         assert_eq!(nodes[1].node.list_channels().len(), 0);
1771
1772         // We test justice_tx build by A on B's revoked HTLC-Success tx
1773         // Create some new channels:
1774         let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1775
1776         // A pending HTLC which will be revoked:
1777         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
1778         // Get the will-be-revoked local txn from B
1779         let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
1780         assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
1781         assert_eq!(revoked_local_txn[0].input.len(), 1);
1782         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
1783         assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
1784         // Revoke the old state
1785         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
1786         {
1787                 let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1788                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1789                 {
1790                         let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
1791                         assert_eq!(node_txn.len(), 3);
1792                         assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
1793                         assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
1794
1795                         check_spends!(node_txn[0], revoked_local_txn[0].clone());
1796                         node_txn.swap_remove(0);
1797                 }
1798                 test_txn_broadcast(&nodes[0], &chan_6, None, HTLCType::NONE);
1799
1800                 nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1801                 let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
1802                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1803                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[1].clone()] }, 1);
1804                 test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone());
1805         }
1806         get_announce_close_broadcast_events(&nodes, 0, 1);
1807         assert_eq!(nodes[0].node.list_channels().len(), 0);
1808         assert_eq!(nodes[1].node.list_channels().len(), 0);
1809 }
1810
1811 #[test]
1812 fn revoked_output_claim() {
1813         // Simple test to ensure a node will claim a revoked output when a stale remote commitment
1814         // transaction is broadcast by its counterparty
1815         let nodes = create_network(2, &[None, None]);
1816         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1817         // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
1818         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
1819         assert_eq!(revoked_local_txn.len(), 1);
1820         // Only output is the full channel value back to nodes[0]:
1821         assert_eq!(revoked_local_txn[0].output.len(), 1);
1822         // Send a payment through, updating everyone's latest commitment txn
1823         send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
1824
1825         // Inform nodes[1] that nodes[0] broadcast a stale tx
1826         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1827         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1828         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
1829         assert_eq!(node_txn.len(), 3); // nodes[1] will broadcast justice tx twice, and its own local state once
1830
1831         assert_eq!(node_txn[0], node_txn[2]);
1832
1833         check_spends!(node_txn[0], revoked_local_txn[0].clone());
1834         check_spends!(node_txn[1], chan_1.3.clone());
1835
1836         // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
1837         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1838         get_announce_close_broadcast_events(&nodes, 0, 1);
1839 }
1840
1841 #[test]
1842 fn claim_htlc_outputs_shared_tx() {
1843         // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
1844         let nodes = create_network(2, &[None, None]);
1845
1846         // Create some new channel:
1847         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1848
1849         // Rebalance the network to generate htlc in the two directions
1850         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1851         // 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
1852         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
1853         let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
1854
1855         // Get the will-be-revoked local txn from node[0]
1856         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
1857         assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
1858         assert_eq!(revoked_local_txn[0].input.len(), 1);
1859         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
1860         assert_eq!(revoked_local_txn[1].input.len(), 1);
1861         assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
1862         assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
1863         check_spends!(revoked_local_txn[1], revoked_local_txn[0].clone());
1864
1865         //Revoke the old state
1866         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
1867
1868         {
1869                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1870                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1871                 nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
1872                 connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
1873
1874                 let events = nodes[1].node.get_and_clear_pending_events();
1875                 assert_eq!(events.len(), 1);
1876                 match events[0] {
1877                         Event::PaymentFailed { payment_hash, .. } => {
1878                                 assert_eq!(payment_hash, payment_hash_2);
1879                         },
1880                         _ => panic!("Unexpected event"),
1881                 }
1882
1883                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
1884                 assert_eq!(node_txn.len(), 4);
1885
1886                 assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
1887                 check_spends!(node_txn[0], revoked_local_txn[0].clone());
1888
1889                 assert_eq!(node_txn[0], node_txn[3]); // justice tx is duplicated due to block re-scanning
1890
1891                 let mut witness_lens = BTreeSet::new();
1892                 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
1893                 witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
1894                 witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
1895                 assert_eq!(witness_lens.len(), 3);
1896                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
1897                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
1898                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
1899
1900                 // Next nodes[1] broadcasts its current local tx state:
1901                 assert_eq!(node_txn[1].input.len(), 1);
1902                 assert_eq!(node_txn[1].input[0].previous_output.txid, chan_1.3.txid()); //Spending funding tx unique txouput, tx broadcasted by ChannelManager
1903
1904                 assert_eq!(node_txn[2].input.len(), 1);
1905                 let witness_script = node_txn[2].clone().input[0].witness.pop().unwrap();
1906                 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
1907                 assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
1908                 assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
1909                 assert_ne!(node_txn[2].input[0].previous_output.txid, node_txn[0].input[1].previous_output.txid);
1910         }
1911         get_announce_close_broadcast_events(&nodes, 0, 1);
1912         assert_eq!(nodes[0].node.list_channels().len(), 0);
1913         assert_eq!(nodes[1].node.list_channels().len(), 0);
1914 }
1915
1916 #[test]
1917 fn claim_htlc_outputs_single_tx() {
1918         // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
1919         let nodes = create_network(2, &[None, None]);
1920
1921         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
1922
1923         // Rebalance the network to generate htlc in the two directions
1924         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1925         // 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
1926         // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
1927         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
1928         let (_payment_preimage_2, payment_hash_2) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000);
1929
1930         // Get the will-be-revoked local txn from node[0]
1931         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
1932
1933         //Revoke the old state
1934         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
1935
1936         {
1937                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
1938                 nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
1939                 nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 200);
1940                 connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
1941
1942                 let events = nodes[1].node.get_and_clear_pending_events();
1943                 assert_eq!(events.len(), 1);
1944                 match events[0] {
1945                         Event::PaymentFailed { payment_hash, .. } => {
1946                                 assert_eq!(payment_hash, payment_hash_2);
1947                         },
1948                         _ => panic!("Unexpected event"),
1949                 }
1950
1951                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
1952                 assert_eq!(node_txn.len(), 22); // ChannelManager : 2, ChannelMontitor: 8 (1 standard revoked output, 2 revocation htlc tx, 1 local commitment tx + 1 htlc timeout tx) * 2 (block-rescan) + 5 * (1 local commitment tx + 1 htlc timeout tx)
1953
1954                 assert_eq!(node_txn[0], node_txn[7]);
1955                 assert_eq!(node_txn[1], node_txn[8]);
1956                 assert_eq!(node_txn[2], node_txn[9]);
1957                 assert_eq!(node_txn[3], node_txn[10]);
1958                 assert_eq!(node_txn[4], node_txn[11]);
1959                 assert_eq!(node_txn[3], node_txn[5]); //local commitment tx + htlc timeout tx broadcasted by ChannelManger
1960                 assert_eq!(node_txn[4], node_txn[6]);
1961
1962                 for i in 12..22 {
1963                         if i % 2 == 0 { assert_eq!(node_txn[3], node_txn[i]); } else { assert_eq!(node_txn[4], node_txn[i]); }
1964                 }
1965
1966                 assert_eq!(node_txn[0].input.len(), 1);
1967                 assert_eq!(node_txn[1].input.len(), 1);
1968                 assert_eq!(node_txn[2].input.len(), 1);
1969
1970                 let mut revoked_tx_map = HashMap::new();
1971                 revoked_tx_map.insert(revoked_local_txn[0].txid(), revoked_local_txn[0].clone());
1972                 node_txn[0].verify(&revoked_tx_map).unwrap();
1973                 node_txn[1].verify(&revoked_tx_map).unwrap();
1974                 node_txn[2].verify(&revoked_tx_map).unwrap();
1975
1976                 let mut witness_lens = BTreeSet::new();
1977                 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
1978                 witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
1979                 witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
1980                 assert_eq!(witness_lens.len(), 3);
1981                 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
1982                 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
1983                 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
1984
1985                 assert_eq!(node_txn[3].input.len(), 1);
1986                 check_spends!(node_txn[3], chan_1.3.clone());
1987
1988                 assert_eq!(node_txn[4].input.len(), 1);
1989                 let witness_script = node_txn[4].input[0].witness.last().unwrap();
1990                 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
1991                 assert_eq!(node_txn[4].input[0].previous_output.txid, node_txn[3].txid());
1992                 assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[0].input[0].previous_output.txid);
1993                 assert_ne!(node_txn[4].input[0].previous_output.txid, node_txn[1].input[0].previous_output.txid);
1994         }
1995         get_announce_close_broadcast_events(&nodes, 0, 1);
1996         assert_eq!(nodes[0].node.list_channels().len(), 0);
1997         assert_eq!(nodes[1].node.list_channels().len(), 0);
1998 }
1999
2000 #[test]
2001 fn test_htlc_on_chain_success() {
2002         // Test that in case of a unilateral close onchain, we detect the state of output thanks to
2003         // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
2004         // broadcasting the right event to other nodes in payment path.
2005         // We test with two HTLCs simultaneously as that was not handled correctly in the past.
2006         // A --------------------> B ----------------------> C (preimage)
2007         // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
2008         // commitment transaction was broadcast.
2009         // Then, B should learn the preimage from said transactions, attempting to claim backwards
2010         // towards B.
2011         // B should be able to claim via preimage if A then broadcasts its local tx.
2012         // Finally, when A sees B's latest local commitment transaction it should be able to claim
2013         // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
2014         // PaymentSent event).
2015
2016         let nodes = create_network(3, &[None, None, None]);
2017
2018         // Create some initial channels
2019         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2020         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2021
2022         // Rebalance the network a bit by relaying one payment through all the channels...
2023         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2024         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2025
2026         let (our_payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2027         let (our_payment_preimage_2, _payment_hash_2) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2028         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2029
2030         // Broadcast legit commitment tx from C on B's chain
2031         // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
2032         let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
2033         assert_eq!(commitment_tx.len(), 1);
2034         check_spends!(commitment_tx[0], chan_2.3.clone());
2035         nodes[2].node.claim_funds(our_payment_preimage);
2036         nodes[2].node.claim_funds(our_payment_preimage_2);
2037         check_added_monitors!(nodes[2], 2);
2038         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2039         assert!(updates.update_add_htlcs.is_empty());
2040         assert!(updates.update_fail_htlcs.is_empty());
2041         assert!(updates.update_fail_malformed_htlcs.is_empty());
2042         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2043
2044         nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
2045         check_closed_broadcast!(nodes[2]);
2046         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 4 (2*2 * HTLC-Success tx)
2047         assert_eq!(node_txn.len(), 5);
2048         assert_eq!(node_txn[0], node_txn[3]);
2049         assert_eq!(node_txn[1], node_txn[4]);
2050         assert_eq!(node_txn[2], commitment_tx[0]);
2051         check_spends!(node_txn[0], commitment_tx[0].clone());
2052         check_spends!(node_txn[1], commitment_tx[0].clone());
2053         assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2054         assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2055         assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2056         assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2057         assert_eq!(node_txn[0].lock_time, 0);
2058         assert_eq!(node_txn[1].lock_time, 0);
2059
2060         // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
2061         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
2062         let events = nodes[1].node.get_and_clear_pending_msg_events();
2063         {
2064                 let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
2065                 assert_eq!(added_monitors.len(), 2);
2066                 assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
2067                 assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
2068                 added_monitors.clear();
2069         }
2070         assert_eq!(events.len(), 2);
2071         match events[0] {
2072                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
2073                 _ => panic!("Unexpected event"),
2074         }
2075         match events[1] {
2076                 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, .. } } => {
2077                         assert!(update_add_htlcs.is_empty());
2078                         assert!(update_fail_htlcs.is_empty());
2079                         assert_eq!(update_fulfill_htlcs.len(), 1);
2080                         assert!(update_fail_malformed_htlcs.is_empty());
2081                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2082                 },
2083                 _ => panic!("Unexpected event"),
2084         };
2085         macro_rules! check_tx_local_broadcast {
2086                 ($node: expr, $htlc_offered: expr, $commitment_tx: expr, $chan_tx: expr) => { {
2087                         // ChannelManager : 3 (commitment tx, 2*HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 (block-rescan)
2088                         let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
2089                         assert_eq!(node_txn.len(), 7);
2090                         assert_eq!(node_txn[0], node_txn[5]);
2091                         assert_eq!(node_txn[1], node_txn[6]);
2092                         check_spends!(node_txn[0], $commitment_tx.clone());
2093                         check_spends!(node_txn[1], $commitment_tx.clone());
2094                         assert_ne!(node_txn[0].lock_time, 0);
2095                         assert_ne!(node_txn[1].lock_time, 0);
2096                         if $htlc_offered {
2097                                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2098                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2099                                 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2100                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2101                         } else {
2102                                 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2103                                 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2104                                 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2105                                 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2106                         }
2107                         check_spends!(node_txn[2], $chan_tx.clone());
2108                         check_spends!(node_txn[3], node_txn[2].clone());
2109                         check_spends!(node_txn[4], node_txn[2].clone());
2110                         assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), 71);
2111                         assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2112                         assert_eq!(node_txn[4].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2113                         assert!(node_txn[3].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2114                         assert!(node_txn[4].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2115                         assert_ne!(node_txn[3].lock_time, 0);
2116                         assert_ne!(node_txn[4].lock_time, 0);
2117                         node_txn.clear();
2118                 } }
2119         }
2120         // nodes[1] now broadcasts its own local state as a fallback, suggesting an alternate
2121         // commitment transaction with a corresponding HTLC-Timeout transactions, as well as a
2122         // timeout-claim of the output that nodes[2] just claimed via success.
2123         check_tx_local_broadcast!(nodes[1], false, commitment_tx[0], chan_2.3);
2124
2125         // Broadcast legit commitment tx from A on B's chain
2126         // Broadcast preimage tx by B on offered output from A commitment tx  on A's chain
2127         let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
2128         check_spends!(commitment_tx[0], chan_1.3.clone());
2129         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
2130         check_closed_broadcast!(nodes[1]);
2131         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
2132         assert_eq!(node_txn.len(), 3);
2133         assert_eq!(node_txn[0], node_txn[2]);
2134         check_spends!(node_txn[0], commitment_tx[0].clone());
2135         assert_eq!(node_txn[0].input.len(), 2);
2136         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2137         assert_eq!(node_txn[0].input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2138         assert_eq!(node_txn[0].lock_time, 0);
2139         assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2140         check_spends!(node_txn[1], chan_1.3.clone());
2141         assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
2142         // We don't bother to check that B can claim the HTLC output on its commitment tx here as
2143         // we already checked the same situation with A.
2144
2145         // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
2146         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone()] }, 1);
2147         check_closed_broadcast!(nodes[0]);
2148         let events = nodes[0].node.get_and_clear_pending_events();
2149         assert_eq!(events.len(), 2);
2150         let mut first_claimed = false;
2151         for event in events {
2152                 match event {
2153                         Event::PaymentSent { payment_preimage } => {
2154                                 if payment_preimage == our_payment_preimage {
2155                                         assert!(!first_claimed);
2156                                         first_claimed = true;
2157                                 } else {
2158                                         assert_eq!(payment_preimage, our_payment_preimage_2);
2159                                 }
2160                         },
2161                         _ => panic!("Unexpected event"),
2162                 }
2163         }
2164         check_tx_local_broadcast!(nodes[0], true, commitment_tx[0], chan_1.3);
2165 }
2166
2167 #[test]
2168 fn test_htlc_on_chain_timeout() {
2169         // Test that in case of a unilateral close onchain, we detect the state of output thanks to
2170         // ChainWatchInterface and timeout the HTLC backward accordingly. So here we test that ChannelManager is
2171         // broadcasting the right event to other nodes in payment path.
2172         // A ------------------> B ----------------------> C (timeout)
2173         //    B's commitment tx                 C's commitment tx
2174         //            \                                  \
2175         //         B's HTLC timeout tx               B's timeout tx
2176
2177         let nodes = create_network(3, &[None, None, None]);
2178
2179         // Create some intial channels
2180         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2181         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2182
2183         // Rebalance the network a bit by relaying one payment thorugh all the channels...
2184         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2185         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2186
2187         let (_payment_preimage, payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
2188         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2189
2190         // Broadcast legit commitment tx from C on B's chain
2191         let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
2192         check_spends!(commitment_tx[0], chan_2.3.clone());
2193         nodes[2].node.fail_htlc_backwards(&payment_hash);
2194         check_added_monitors!(nodes[2], 0);
2195         expect_pending_htlcs_forwardable!(nodes[2]);
2196         check_added_monitors!(nodes[2], 1);
2197
2198         let events = nodes[2].node.get_and_clear_pending_msg_events();
2199         assert_eq!(events.len(), 1);
2200         match events[0] {
2201                 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, .. } } => {
2202                         assert!(update_add_htlcs.is_empty());
2203                         assert!(!update_fail_htlcs.is_empty());
2204                         assert!(update_fulfill_htlcs.is_empty());
2205                         assert!(update_fail_malformed_htlcs.is_empty());
2206                         assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
2207                 },
2208                 _ => panic!("Unexpected event"),
2209         };
2210         nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
2211         check_closed_broadcast!(nodes[2]);
2212         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
2213         assert_eq!(node_txn.len(), 1);
2214         check_spends!(node_txn[0], chan_2.3.clone());
2215         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
2216
2217         // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
2218         // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
2219         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
2220         let timeout_tx;
2221         {
2222                 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2223                 assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 6 (HTLC-Timeout tx, commitment tx, timeout tx) * 2 (block-rescan)
2224                 assert_eq!(node_txn[0], node_txn[5]);
2225                 assert_eq!(node_txn[1], node_txn[6]);
2226                 assert_eq!(node_txn[2], node_txn[7]);
2227                 check_spends!(node_txn[0], commitment_tx[0].clone());
2228                 assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2229                 check_spends!(node_txn[1], chan_2.3.clone());
2230                 check_spends!(node_txn[2], node_txn[1].clone());
2231                 assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
2232                 assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2233                 check_spends!(node_txn[3], chan_2.3.clone());
2234                 check_spends!(node_txn[4], node_txn[3].clone());
2235                 assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
2236                 assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2237                 timeout_tx = node_txn[0].clone();
2238                 node_txn.clear();
2239         }
2240
2241         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
2242         connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
2243         check_added_monitors!(nodes[1], 0);
2244         check_closed_broadcast!(nodes[1]);
2245
2246         expect_pending_htlcs_forwardable!(nodes[1]);
2247         check_added_monitors!(nodes[1], 1);
2248         let events = nodes[1].node.get_and_clear_pending_msg_events();
2249         assert_eq!(events.len(), 1);
2250         match events[0] {
2251                 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, .. } } => {
2252                         assert!(update_add_htlcs.is_empty());
2253                         assert!(!update_fail_htlcs.is_empty());
2254                         assert!(update_fulfill_htlcs.is_empty());
2255                         assert!(update_fail_malformed_htlcs.is_empty());
2256                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2257                 },
2258                 _ => panic!("Unexpected event"),
2259         };
2260         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // Well... here we detect our own htlc_timeout_tx so no tx to be generated
2261         assert_eq!(node_txn.len(), 0);
2262
2263         // Broadcast legit commitment tx from B on A's chain
2264         let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
2265         check_spends!(commitment_tx[0], chan_1.3.clone());
2266
2267         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
2268         check_closed_broadcast!(nodes[0]);
2269         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 block-rescan
2270         assert_eq!(node_txn.len(), 4);
2271         assert_eq!(node_txn[0], node_txn[3]);
2272         check_spends!(node_txn[0], commitment_tx[0].clone());
2273         assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2274         check_spends!(node_txn[1], chan_1.3.clone());
2275         check_spends!(node_txn[2], node_txn[1].clone());
2276         assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
2277         assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2278 }
2279
2280 #[test]
2281 fn test_simple_commitment_revoked_fail_backward() {
2282         // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
2283         // and fail backward accordingly.
2284
2285         let nodes = create_network(3, &[None, None, None]);
2286
2287         // Create some initial channels
2288         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2289         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2290
2291         let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
2292         // Get the will-be-revoked local txn from nodes[2]
2293         let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
2294         // Revoke the old state
2295         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
2296
2297         route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
2298
2299         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2300         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
2301         connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
2302         check_added_monitors!(nodes[1], 0);
2303         check_closed_broadcast!(nodes[1]);
2304
2305         expect_pending_htlcs_forwardable!(nodes[1]);
2306         check_added_monitors!(nodes[1], 1);
2307         let events = nodes[1].node.get_and_clear_pending_msg_events();
2308         assert_eq!(events.len(), 1);
2309         match events[0] {
2310                 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, .. } } => {
2311                         assert!(update_add_htlcs.is_empty());
2312                         assert_eq!(update_fail_htlcs.len(), 1);
2313                         assert!(update_fulfill_htlcs.is_empty());
2314                         assert!(update_fail_malformed_htlcs.is_empty());
2315                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2316
2317                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
2318                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
2319
2320                         let events = nodes[0].node.get_and_clear_pending_msg_events();
2321                         assert_eq!(events.len(), 1);
2322                         match events[0] {
2323                                 MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
2324                                 _ => panic!("Unexpected event"),
2325                         }
2326                         let events = nodes[0].node.get_and_clear_pending_events();
2327                         assert_eq!(events.len(), 1);
2328                         match events[0] {
2329                                 Event::PaymentFailed { .. } => {},
2330                                 _ => panic!("Unexpected event"),
2331                         }
2332                 },
2333                 _ => panic!("Unexpected event"),
2334         }
2335 }
2336
2337 fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
2338         // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
2339         // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
2340         // commitment transaction anymore.
2341         // To do this, we have the peer which will broadcast a revoked commitment transaction send
2342         // a number of update_fail/commitment_signed updates without ever sending the RAA in
2343         // response to our commitment_signed. This is somewhat misbehavior-y, though not
2344         // technically disallowed and we should probably handle it reasonably.
2345         // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
2346         // failed/fulfilled backwards must be in at least one of the latest two remote commitment
2347         // transactions:
2348         // * Once we move it out of our holding cell/add it, we will immediately include it in a
2349         //   commitment_signed (implying it will be in the latest remote commitment transaction).
2350         // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
2351         //   and once they revoke the previous commitment transaction (allowing us to send a new
2352         //   commitment_signed) we will be free to fail/fulfill the HTLC backwards.
2353         let mut nodes = create_network(3, &[None, None, None]);
2354
2355         // Create some initial channels
2356         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2357         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2358
2359         let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
2360         // Get the will-be-revoked local txn from nodes[2]
2361         let revoked_local_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
2362         assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
2363         // Revoke the old state
2364         claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
2365
2366         let value = if use_dust {
2367                 // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
2368                 // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
2369                 nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().our_dust_limit_satoshis * 1000
2370         } else { 3000000 };
2371
2372         let (_, first_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
2373         let (_, second_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
2374         let (_, third_payment_hash) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
2375
2376         assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
2377         expect_pending_htlcs_forwardable!(nodes[2]);
2378         check_added_monitors!(nodes[2], 1);
2379         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2380         assert!(updates.update_add_htlcs.is_empty());
2381         assert!(updates.update_fulfill_htlcs.is_empty());
2382         assert!(updates.update_fail_malformed_htlcs.is_empty());
2383         assert_eq!(updates.update_fail_htlcs.len(), 1);
2384         assert!(updates.update_fee.is_none());
2385         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
2386         let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
2387         // Drop the last RAA from 3 -> 2
2388
2389         assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
2390         expect_pending_htlcs_forwardable!(nodes[2]);
2391         check_added_monitors!(nodes[2], 1);
2392         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2393         assert!(updates.update_add_htlcs.is_empty());
2394         assert!(updates.update_fulfill_htlcs.is_empty());
2395         assert!(updates.update_fail_malformed_htlcs.is_empty());
2396         assert_eq!(updates.update_fail_htlcs.len(), 1);
2397         assert!(updates.update_fee.is_none());
2398         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
2399         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
2400         check_added_monitors!(nodes[1], 1);
2401         // Note that nodes[1] is in AwaitingRAA, so won't send a CS
2402         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
2403         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
2404         check_added_monitors!(nodes[2], 1);
2405
2406         assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
2407         expect_pending_htlcs_forwardable!(nodes[2]);
2408         check_added_monitors!(nodes[2], 1);
2409         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2410         assert!(updates.update_add_htlcs.is_empty());
2411         assert!(updates.update_fulfill_htlcs.is_empty());
2412         assert!(updates.update_fail_malformed_htlcs.is_empty());
2413         assert_eq!(updates.update_fail_htlcs.len(), 1);
2414         assert!(updates.update_fee.is_none());
2415         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
2416         // At this point first_payment_hash has dropped out of the latest two commitment
2417         // transactions that nodes[1] is tracking...
2418         nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed).unwrap();
2419         check_added_monitors!(nodes[1], 1);
2420         // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
2421         let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
2422         nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa).unwrap();
2423         check_added_monitors!(nodes[2], 1);
2424
2425         // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
2426         // on nodes[2]'s RAA.
2427         let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
2428         let (_, fourth_payment_hash) = get_payment_preimage_hash!(nodes[0]);
2429         nodes[1].node.send_payment(route, fourth_payment_hash).unwrap();
2430         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2431         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2432         check_added_monitors!(nodes[1], 0);
2433
2434         if deliver_bs_raa {
2435                 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa).unwrap();
2436                 // One monitor for the new revocation preimage, no second on as we won't generate a new
2437                 // commitment transaction for nodes[0] until process_pending_htlc_forwards().
2438                 check_added_monitors!(nodes[1], 1);
2439                 let events = nodes[1].node.get_and_clear_pending_events();
2440                 assert_eq!(events.len(), 1);
2441                 match events[0] {
2442                         Event::PendingHTLCsForwardable { .. } => { },
2443                         _ => panic!("Unexpected event"),
2444                 };
2445                 // Deliberately don't process the pending fail-back so they all fail back at once after
2446                 // block connection just like the !deliver_bs_raa case
2447         }
2448
2449         let mut failed_htlcs = HashSet::new();
2450         assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2451
2452         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
2453         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
2454         connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 1, true, header.bitcoin_hash());
2455
2456         let events = nodes[1].node.get_and_clear_pending_events();
2457         assert_eq!(events.len(), if deliver_bs_raa { 1 } else { 2 });
2458         match events[0] {
2459                 Event::PaymentFailed { ref payment_hash, .. } => {
2460                         assert_eq!(*payment_hash, fourth_payment_hash);
2461                 },
2462                 _ => panic!("Unexpected event"),
2463         }
2464         if !deliver_bs_raa {
2465                 match events[1] {
2466                         Event::PendingHTLCsForwardable { .. } => { },
2467                         _ => panic!("Unexpected event"),
2468                 };
2469         }
2470         nodes[1].node.process_pending_htlc_forwards();
2471         check_added_monitors!(nodes[1], 1);
2472
2473         let events = nodes[1].node.get_and_clear_pending_msg_events();
2474         assert_eq!(events.len(), if deliver_bs_raa { 3 } else { 2 });
2475         match events[if deliver_bs_raa { 1 } else { 0 }] {
2476                 MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
2477                 _ => panic!("Unexpected event"),
2478         }
2479         if deliver_bs_raa {
2480                 match events[0] {
2481                         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, .. } } => {
2482                                 assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
2483                                 assert_eq!(update_add_htlcs.len(), 1);
2484                                 assert!(update_fulfill_htlcs.is_empty());
2485                                 assert!(update_fail_htlcs.is_empty());
2486                                 assert!(update_fail_malformed_htlcs.is_empty());
2487                         },
2488                         _ => panic!("Unexpected event"),
2489                 }
2490         }
2491         match events[if deliver_bs_raa { 2 } else { 1 }] {
2492                 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, .. } } => {
2493                         assert!(update_add_htlcs.is_empty());
2494                         assert_eq!(update_fail_htlcs.len(), 3);
2495                         assert!(update_fulfill_htlcs.is_empty());
2496                         assert!(update_fail_malformed_htlcs.is_empty());
2497                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2498
2499                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]).unwrap();
2500                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]).unwrap();
2501                         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]).unwrap();
2502
2503                         commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
2504
2505                         let events = nodes[0].node.get_and_clear_pending_msg_events();
2506                         // If we delivered B's RAA we got an unknown preimage error, not something
2507                         // that we should update our routing table for.
2508                         assert_eq!(events.len(), if deliver_bs_raa { 2 } else { 3 });
2509                         for event in events {
2510                                 match event {
2511                                         MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
2512                                         _ => panic!("Unexpected event"),
2513                                 }
2514                         }
2515                         let events = nodes[0].node.get_and_clear_pending_events();
2516                         assert_eq!(events.len(), 3);
2517                         match events[0] {
2518                                 Event::PaymentFailed { ref payment_hash, .. } => {
2519                                         assert!(failed_htlcs.insert(payment_hash.0));
2520                                 },
2521                                 _ => panic!("Unexpected event"),
2522                         }
2523                         match events[1] {
2524                                 Event::PaymentFailed { ref payment_hash, .. } => {
2525                                         assert!(failed_htlcs.insert(payment_hash.0));
2526                                 },
2527                                 _ => panic!("Unexpected event"),
2528                         }
2529                         match events[2] {
2530                                 Event::PaymentFailed { ref payment_hash, .. } => {
2531                                         assert!(failed_htlcs.insert(payment_hash.0));
2532                                 },
2533                                 _ => panic!("Unexpected event"),
2534                         }
2535                 },
2536                 _ => panic!("Unexpected event"),
2537         }
2538
2539         assert!(failed_htlcs.contains(&first_payment_hash.0));
2540         assert!(failed_htlcs.contains(&second_payment_hash.0));
2541         assert!(failed_htlcs.contains(&third_payment_hash.0));
2542 }
2543
2544 #[test]
2545 fn test_commitment_revoked_fail_backward_exhaustive_a() {
2546         do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
2547         do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
2548         do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
2549         do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
2550 }
2551
2552 #[test]
2553 fn test_commitment_revoked_fail_backward_exhaustive_b() {
2554         do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
2555         do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
2556         do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
2557         do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
2558 }
2559
2560 #[test]
2561 fn test_htlc_ignore_latest_remote_commitment() {
2562         // Test that HTLC transactions spending the latest remote commitment transaction are simply
2563         // ignored if we cannot claim them. This originally tickled an invalid unwrap().
2564         let nodes = create_network(2, &[None, None]);
2565         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2566
2567         route_payment(&nodes[0], &[&nodes[1]], 10000000);
2568         nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id);
2569         check_closed_broadcast!(nodes[0]);
2570
2571         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
2572         assert_eq!(node_txn.len(), 2);
2573
2574         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2575         nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
2576         check_closed_broadcast!(nodes[1]);
2577
2578         // Duplicate the block_connected call since this may happen due to other listeners
2579         // registering new transactions
2580         nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&node_txn[0], &node_txn[1]], &[1; 2]);
2581 }
2582
2583 #[test]
2584 fn test_force_close_fail_back() {
2585         // Check which HTLCs are failed-backwards on channel force-closure
2586         let mut nodes = create_network(3, &[None, None, None]);
2587         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2588         create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2589
2590         let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, 42).unwrap();
2591
2592         let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
2593
2594         let mut payment_event = {
2595                 nodes[0].node.send_payment(route, our_payment_hash).unwrap();
2596                 check_added_monitors!(nodes[0], 1);
2597
2598                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2599                 assert_eq!(events.len(), 1);
2600                 SendEvent::from_event(events.remove(0))
2601         };
2602
2603         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
2604         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
2605
2606         expect_pending_htlcs_forwardable!(nodes[1]);
2607
2608         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
2609         assert_eq!(events_2.len(), 1);
2610         payment_event = SendEvent::from_event(events_2.remove(0));
2611         assert_eq!(payment_event.msgs.len(), 1);
2612
2613         check_added_monitors!(nodes[1], 1);
2614         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
2615         nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
2616         check_added_monitors!(nodes[2], 1);
2617         let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2618
2619         // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
2620         // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
2621         // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
2622
2623         nodes[2].node.force_close_channel(&payment_event.commitment_msg.channel_id);
2624         check_closed_broadcast!(nodes[2]);
2625         let tx = {
2626                 let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
2627                 // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
2628                 // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
2629                 // back to nodes[1] upon timeout otherwise.
2630                 assert_eq!(node_txn.len(), 1);
2631                 node_txn.remove(0)
2632         };
2633
2634         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2635         nodes[1].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
2636
2637         // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
2638         check_closed_broadcast!(nodes[1]);
2639
2640         // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
2641         {
2642                 let mut monitors = nodes[2].chan_monitor.simple_monitor.monitors.lock().unwrap();
2643                 monitors.get_mut(&OutPoint::new(Sha256dHash::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), 0)).unwrap()
2644                         .provide_payment_preimage(&our_payment_hash, &our_payment_preimage);
2645         }
2646         nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]);
2647         let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
2648         assert_eq!(node_txn.len(), 1);
2649         assert_eq!(node_txn[0].input.len(), 1);
2650         assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
2651         assert_eq!(node_txn[0].lock_time, 0); // Must be an HTLC-Success
2652         assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
2653
2654         check_spends!(node_txn[0], tx);
2655 }
2656
2657 #[test]
2658 fn test_unconf_chan() {
2659         // After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
2660         let nodes = create_network(2, &[None, None]);
2661         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2662
2663         let channel_state = nodes[0].node.channel_state.lock().unwrap();
2664         assert_eq!(channel_state.by_id.len(), 1);
2665         assert_eq!(channel_state.short_to_id.len(), 1);
2666         mem::drop(channel_state);
2667
2668         let mut headers = Vec::new();
2669         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2670         headers.push(header.clone());
2671         for _i in 2..100 {
2672                 header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2673                 headers.push(header.clone());
2674         }
2675         let mut height = 99;
2676         while !headers.is_empty() {
2677                 nodes[0].node.block_disconnected(&headers.pop().unwrap(), height);
2678                 height -= 1;
2679         }
2680         check_closed_broadcast!(nodes[0]);
2681         let channel_state = nodes[0].node.channel_state.lock().unwrap();
2682         assert_eq!(channel_state.by_id.len(), 0);
2683         assert_eq!(channel_state.short_to_id.len(), 0);
2684 }
2685
2686 #[test]
2687 fn test_simple_peer_disconnect() {
2688         // Test that we can reconnect when there are no lost messages
2689         let nodes = create_network(3, &[None, None, None]);
2690         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2691         create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
2692
2693         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2694         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2695         reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2696
2697         let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
2698         let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
2699         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
2700         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
2701
2702         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2703         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2704         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2705
2706         let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
2707         let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
2708         let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
2709         let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
2710
2711         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2712         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2713
2714         claim_payment_along_route(&nodes[0], &vec!(&nodes[1], &nodes[2]), true, payment_preimage_3);
2715         fail_payment_along_route(&nodes[0], &[&nodes[1], &nodes[2]], true, payment_hash_5);
2716
2717         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (1, 0), (1, 0), (false, false));
2718         {
2719                 let events = nodes[0].node.get_and_clear_pending_events();
2720                 assert_eq!(events.len(), 2);
2721                 match events[0] {
2722                         Event::PaymentSent { payment_preimage } => {
2723                                 assert_eq!(payment_preimage, payment_preimage_3);
2724                         },
2725                         _ => panic!("Unexpected event"),
2726                 }
2727                 match events[1] {
2728                         Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
2729                                 assert_eq!(payment_hash, payment_hash_5);
2730                                 assert!(rejected_by_dest);
2731                         },
2732                         _ => panic!("Unexpected event"),
2733                 }
2734         }
2735
2736         claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
2737         fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
2738 }
2739
2740 fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) {
2741         // Test that we can reconnect when in-flight HTLC updates get dropped
2742         let mut nodes = create_network(2, &[None, None]);
2743         if messages_delivered == 0 {
2744                 create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
2745                 // nodes[1] doesn't receive the funding_locked message (it'll be re-sent on reconnect)
2746         } else {
2747                 create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
2748         }
2749
2750         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
2751         let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]);
2752
2753         let payment_event = {
2754                 nodes[0].node.send_payment(route.clone(), payment_hash_1).unwrap();
2755                 check_added_monitors!(nodes[0], 1);
2756
2757                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2758                 assert_eq!(events.len(), 1);
2759                 SendEvent::from_event(events.remove(0))
2760         };
2761         assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
2762
2763         if messages_delivered < 2 {
2764                 // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
2765         } else {
2766                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
2767                 if messages_delivered >= 3 {
2768                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
2769                         check_added_monitors!(nodes[1], 1);
2770                         let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2771
2772                         if messages_delivered >= 4 {
2773                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
2774                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2775                                 check_added_monitors!(nodes[0], 1);
2776
2777                                 if messages_delivered >= 5 {
2778                                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed).unwrap();
2779                                         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2780                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
2781                                         check_added_monitors!(nodes[0], 1);
2782
2783                                         if messages_delivered >= 6 {
2784                                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
2785                                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2786                                                 check_added_monitors!(nodes[1], 1);
2787                                         }
2788                                 }
2789                         }
2790                 }
2791         }
2792
2793         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2794         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2795         if messages_delivered < 3 {
2796                 // Even if the funding_locked messages get exchanged, as long as nothing further was
2797                 // received on either side, both sides will need to resend them.
2798                 reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 1), (0, 0), (0, 0), (0, 0), (false, false));
2799         } else if messages_delivered == 3 {
2800                 // nodes[0] still wants its RAA + commitment_signed
2801                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (true, false));
2802         } else if messages_delivered == 4 {
2803                 // nodes[0] still wants its commitment_signed
2804                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (-1, 0), (0, 0), (0, 0), (0, 0), (false, false));
2805         } else if messages_delivered == 5 {
2806                 // nodes[1] still wants its final RAA
2807                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, true));
2808         } else if messages_delivered == 6 {
2809                 // Everything was delivered...
2810                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2811         }
2812
2813         let events_1 = nodes[1].node.get_and_clear_pending_events();
2814         assert_eq!(events_1.len(), 1);
2815         match events_1[0] {
2816                 Event::PendingHTLCsForwardable { .. } => { },
2817                 _ => panic!("Unexpected event"),
2818         };
2819
2820         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2821         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2822         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2823
2824         nodes[1].node.process_pending_htlc_forwards();
2825
2826         let events_2 = nodes[1].node.get_and_clear_pending_events();
2827         assert_eq!(events_2.len(), 1);
2828         match events_2[0] {
2829                 Event::PaymentReceived { ref payment_hash, amt } => {
2830                         assert_eq!(payment_hash_1, *payment_hash);
2831                         assert_eq!(amt, 1000000);
2832                 },
2833                 _ => panic!("Unexpected event"),
2834         }
2835
2836         nodes[1].node.claim_funds(payment_preimage_1);
2837         check_added_monitors!(nodes[1], 1);
2838
2839         let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
2840         assert_eq!(events_3.len(), 1);
2841         let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
2842                 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
2843                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
2844                         assert!(updates.update_add_htlcs.is_empty());
2845                         assert!(updates.update_fail_htlcs.is_empty());
2846                         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2847                         assert!(updates.update_fail_malformed_htlcs.is_empty());
2848                         assert!(updates.update_fee.is_none());
2849                         (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
2850                 },
2851                 _ => panic!("Unexpected event"),
2852         };
2853
2854         if messages_delivered >= 1 {
2855                 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc).unwrap();
2856
2857                 let events_4 = nodes[0].node.get_and_clear_pending_events();
2858                 assert_eq!(events_4.len(), 1);
2859                 match events_4[0] {
2860                         Event::PaymentSent { ref payment_preimage } => {
2861                                 assert_eq!(payment_preimage_1, *payment_preimage);
2862                         },
2863                         _ => panic!("Unexpected event"),
2864                 }
2865
2866                 if messages_delivered >= 2 {
2867                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed).unwrap();
2868                         check_added_monitors!(nodes[0], 1);
2869                         let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2870
2871                         if messages_delivered >= 3 {
2872                                 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
2873                                 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2874                                 check_added_monitors!(nodes[1], 1);
2875
2876                                 if messages_delivered >= 4 {
2877                                         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed).unwrap();
2878                                         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2879                                         // No commitment_signed so get_event_msg's assert(len == 1) passes
2880                                         check_added_monitors!(nodes[1], 1);
2881
2882                                         if messages_delivered >= 5 {
2883                                                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
2884                                                 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
2885                                                 check_added_monitors!(nodes[0], 1);
2886                                         }
2887                                 }
2888                         }
2889                 }
2890         }
2891
2892         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2893         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2894         if messages_delivered < 2 {
2895                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (1, 0), (0, 0), (0, 0), (false, false));
2896                 //TODO: Deduplicate PaymentSent events, then enable this if:
2897                 //if messages_delivered < 1 {
2898                         let events_4 = nodes[0].node.get_and_clear_pending_events();
2899                         assert_eq!(events_4.len(), 1);
2900                         match events_4[0] {
2901                                 Event::PaymentSent { ref payment_preimage } => {
2902                                         assert_eq!(payment_preimage_1, *payment_preimage);
2903                                 },
2904                                 _ => panic!("Unexpected event"),
2905                         }
2906                 //}
2907         } else if messages_delivered == 2 {
2908                 // nodes[0] still wants its RAA + commitment_signed
2909                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, true));
2910         } else if messages_delivered == 3 {
2911                 // nodes[0] still wants its commitment_signed
2912                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, -1), (0, 0), (0, 0), (0, 0), (false, false));
2913         } else if messages_delivered == 4 {
2914                 // nodes[1] still wants its final RAA
2915                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (true, false));
2916         } else if messages_delivered == 5 {
2917                 // Everything was delivered...
2918                 reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2919         }
2920
2921         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2922         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2923         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2924
2925         // Channel should still work fine...
2926         let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
2927         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
2928 }
2929
2930 #[test]
2931 fn test_drop_messages_peer_disconnect_a() {
2932         do_test_drop_messages_peer_disconnect(0);
2933         do_test_drop_messages_peer_disconnect(1);
2934         do_test_drop_messages_peer_disconnect(2);
2935         do_test_drop_messages_peer_disconnect(3);
2936 }
2937
2938 #[test]
2939 fn test_drop_messages_peer_disconnect_b() {
2940         do_test_drop_messages_peer_disconnect(4);
2941         do_test_drop_messages_peer_disconnect(5);
2942         do_test_drop_messages_peer_disconnect(6);
2943 }
2944
2945 #[test]
2946 fn test_funding_peer_disconnect() {
2947         // Test that we can lock in our funding tx while disconnected
2948         let nodes = create_network(2, &[None, None]);
2949         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
2950
2951         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2952         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2953
2954         confirm_transaction(&nodes[0].chain_monitor, &tx, tx.version);
2955         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
2956         assert_eq!(events_1.len(), 1);
2957         match events_1[0] {
2958                 MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
2959                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
2960                 },
2961                 _ => panic!("Unexpected event"),
2962         }
2963
2964         reconnect_nodes(&nodes[0], &nodes[1], (false, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2965
2966         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
2967         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
2968
2969         confirm_transaction(&nodes[1].chain_monitor, &tx, tx.version);
2970         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
2971         assert_eq!(events_2.len(), 2);
2972         match events_2[0] {
2973                 MessageSendEvent::SendFundingLocked { ref node_id, msg: _ } => {
2974                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
2975                 },
2976                 _ => panic!("Unexpected event"),
2977         }
2978         match events_2[1] {
2979                 MessageSendEvent::SendAnnouncementSignatures { ref node_id, msg: _ } => {
2980                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
2981                 },
2982                 _ => panic!("Unexpected event"),
2983         }
2984
2985         reconnect_nodes(&nodes[0], &nodes[1], (true, true), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
2986
2987         // TODO: We shouldn't need to manually pass list_usable_chanels here once we support
2988         // rebroadcasting announcement_signatures upon reconnect.
2989
2990         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
2991         let (payment_preimage, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000);
2992         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
2993 }
2994
2995 #[test]
2996 fn test_drop_messages_peer_disconnect_dual_htlc() {
2997         // Test that we can handle reconnecting when both sides of a channel have pending
2998         // commitment_updates when we disconnect.
2999         let mut nodes = create_network(2, &[None, None]);
3000         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3001
3002         let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
3003
3004         // Now try to send a second payment which will fail to send
3005         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
3006         let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]);
3007
3008         nodes[0].node.send_payment(route.clone(), payment_hash_2).unwrap();
3009         check_added_monitors!(nodes[0], 1);
3010
3011         let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
3012         assert_eq!(events_1.len(), 1);
3013         match events_1[0] {
3014                 MessageSendEvent::UpdateHTLCs { .. } => {},
3015                 _ => panic!("Unexpected event"),
3016         }
3017
3018         assert!(nodes[1].node.claim_funds(payment_preimage_1));
3019         check_added_monitors!(nodes[1], 1);
3020
3021         let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3022         assert_eq!(events_2.len(), 1);
3023         match events_2[0] {
3024                 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 } } => {
3025                         assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3026                         assert!(update_add_htlcs.is_empty());
3027                         assert_eq!(update_fulfill_htlcs.len(), 1);
3028                         assert!(update_fail_htlcs.is_empty());
3029                         assert!(update_fail_malformed_htlcs.is_empty());
3030                         assert!(update_fee.is_none());
3031
3032                         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]).unwrap();
3033                         let events_3 = nodes[0].node.get_and_clear_pending_events();
3034                         assert_eq!(events_3.len(), 1);
3035                         match events_3[0] {
3036                                 Event::PaymentSent { ref payment_preimage } => {
3037                                         assert_eq!(*payment_preimage, payment_preimage_1);
3038                                 },
3039                                 _ => panic!("Unexpected event"),
3040                         }
3041
3042                         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed).unwrap();
3043                         let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3044                         // No commitment_signed so get_event_msg's assert(len == 1) passes
3045                         check_added_monitors!(nodes[0], 1);
3046                 },
3047                 _ => panic!("Unexpected event"),
3048         }
3049
3050         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
3051         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3052
3053         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
3054         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
3055         assert_eq!(reestablish_1.len(), 1);
3056         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
3057         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
3058         assert_eq!(reestablish_2.len(), 1);
3059
3060         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
3061         let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
3062         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
3063         let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
3064
3065         assert!(as_resp.0.is_none());
3066         assert!(bs_resp.0.is_none());
3067
3068         assert!(bs_resp.1.is_none());
3069         assert!(bs_resp.2.is_none());
3070
3071         assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
3072
3073         assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
3074         assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
3075         assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
3076         assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
3077         assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
3078         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]).unwrap();
3079         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed).unwrap();
3080         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3081         // No commitment_signed so get_event_msg's assert(len == 1) passes
3082         check_added_monitors!(nodes[1], 1);
3083
3084         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap()).unwrap();
3085         let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3086         assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
3087         assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
3088         assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
3089         assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
3090         assert!(bs_second_commitment_signed.update_fee.is_none());
3091         check_added_monitors!(nodes[1], 1);
3092
3093         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
3094         let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3095         assert!(as_commitment_signed.update_add_htlcs.is_empty());
3096         assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
3097         assert!(as_commitment_signed.update_fail_htlcs.is_empty());
3098         assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
3099         assert!(as_commitment_signed.update_fee.is_none());
3100         check_added_monitors!(nodes[0], 1);
3101
3102         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed).unwrap();
3103         let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3104         // No commitment_signed so get_event_msg's assert(len == 1) passes
3105         check_added_monitors!(nodes[0], 1);
3106
3107         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed).unwrap();
3108         let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3109         // No commitment_signed so get_event_msg's assert(len == 1) passes
3110         check_added_monitors!(nodes[1], 1);
3111
3112         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack).unwrap();
3113         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3114         check_added_monitors!(nodes[1], 1);
3115
3116         expect_pending_htlcs_forwardable!(nodes[1]);
3117
3118         let events_5 = nodes[1].node.get_and_clear_pending_events();
3119         assert_eq!(events_5.len(), 1);
3120         match events_5[0] {
3121                 Event::PaymentReceived { ref payment_hash, amt: _ } => {
3122                         assert_eq!(payment_hash_2, *payment_hash);
3123                 },
3124                 _ => panic!("Unexpected event"),
3125         }
3126
3127         nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack).unwrap();
3128         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3129         check_added_monitors!(nodes[0], 1);
3130
3131         claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
3132 }
3133
3134 #[test]
3135 fn test_invalid_channel_announcement() {
3136         //Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
3137         let secp_ctx = Secp256k1::new();
3138         let nodes = create_network(2, &[None, None]);
3139
3140         let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1], LocalFeatures::new(), LocalFeatures::new());
3141
3142         let a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
3143         let b_channel_lock = nodes[1].node.channel_state.lock().unwrap();
3144         let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap();
3145         let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap();
3146
3147         let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
3148
3149         let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key);
3150         let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key);
3151
3152         let as_network_key = nodes[0].node.get_our_node_id();
3153         let bs_network_key = nodes[1].node.get_our_node_id();
3154
3155         let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..];
3156
3157         let mut chan_announcement;
3158
3159         macro_rules! dummy_unsigned_msg {
3160                 () => {
3161                         msgs::UnsignedChannelAnnouncement {
3162                                 features: msgs::GlobalFeatures::new(),
3163                                 chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
3164                                 short_channel_id: as_chan.get_short_channel_id().unwrap(),
3165                                 node_id_1: if were_node_one { as_network_key } else { bs_network_key },
3166                                 node_id_2: if were_node_one { bs_network_key } else { as_network_key },
3167                                 bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key },
3168                                 bitcoin_key_2: if were_node_one { bs_bitcoin_key } else { as_bitcoin_key },
3169                                 excess_data: Vec::new(),
3170                         };
3171                 }
3172         }
3173
3174         macro_rules! sign_msg {
3175                 ($unsigned_msg: expr) => {
3176                         let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap();
3177                         let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key);
3178                         let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key);
3179                         let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret());
3180                         let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret());
3181                         chan_announcement = msgs::ChannelAnnouncement {
3182                                 node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig},
3183                                 node_signature_2 : if were_node_one { bs_node_sig } else { as_node_sig},
3184                                 bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig },
3185                                 bitcoin_signature_2 : if were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig },
3186                                 contents: $unsigned_msg
3187                         }
3188                 }
3189         }
3190
3191         let unsigned_msg = dummy_unsigned_msg!();
3192         sign_msg!(unsigned_msg);
3193         assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true);
3194         let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
3195
3196         // Configured with Network::Testnet
3197         let mut unsigned_msg = dummy_unsigned_msg!();
3198         unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash();
3199         sign_msg!(unsigned_msg);
3200         assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
3201
3202         let mut unsigned_msg = dummy_unsigned_msg!();
3203         unsigned_msg.chain_hash = Sha256dHash::hash(&[1,2,3,4,5,6,7,8,9]);
3204         sign_msg!(unsigned_msg);
3205         assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
3206 }
3207
3208 #[test]
3209 fn test_no_txn_manager_serialize_deserialize() {
3210         let mut nodes = create_network(2, &[None, None]);
3211
3212         let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, LocalFeatures::new(), LocalFeatures::new());
3213
3214         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3215
3216         let nodes_0_serialized = nodes[0].node.encode();
3217         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
3218         nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
3219
3220         nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
3221         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
3222         let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
3223         assert!(chan_0_monitor_read.is_empty());
3224
3225         let mut nodes_0_read = &nodes_0_serialized[..];
3226         let config = UserConfig::new();
3227         let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
3228         let (_, nodes_0_deserialized) = {
3229                 let mut channel_monitors = HashMap::new();
3230                 channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
3231                 <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
3232                         default_config: config,
3233                         keys_manager,
3234                         fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
3235                         monitor: nodes[0].chan_monitor.clone(),
3236                         chain_monitor: nodes[0].chain_monitor.clone(),
3237                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
3238                         logger: Arc::new(test_utils::TestLogger::new()),
3239                         channel_monitors: &channel_monitors,
3240                 }).unwrap()
3241         };
3242         assert!(nodes_0_read.is_empty());
3243
3244         assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
3245         nodes[0].node = Arc::new(nodes_0_deserialized);
3246         let nodes_0_as_listener: Arc<ChainListener> = nodes[0].node.clone();
3247         nodes[0].chain_monitor.register_listener(Arc::downgrade(&nodes_0_as_listener));
3248         assert_eq!(nodes[0].node.list_channels().len(), 1);
3249         check_added_monitors!(nodes[0], 1);
3250
3251         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
3252         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
3253         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
3254         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
3255
3256         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
3257         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3258         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
3259         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3260
3261         let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
3262         let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
3263         for node in nodes.iter() {
3264                 assert!(node.router.handle_channel_announcement(&announcement).unwrap());
3265                 node.router.handle_channel_update(&as_update).unwrap();
3266                 node.router.handle_channel_update(&bs_update).unwrap();
3267         }
3268
3269         send_payment(&nodes[0], &[&nodes[1]], 1000000);
3270 }
3271
3272 #[test]
3273 fn test_simple_manager_serialize_deserialize() {
3274         let mut nodes = create_network(2, &[None, None]);
3275         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3276
3277         let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
3278         let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
3279
3280         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3281
3282         let nodes_0_serialized = nodes[0].node.encode();
3283         let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
3284         nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
3285
3286         nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
3287         let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
3288         let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
3289         assert!(chan_0_monitor_read.is_empty());
3290
3291         let mut nodes_0_read = &nodes_0_serialized[..];
3292         let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
3293         let (_, nodes_0_deserialized) = {
3294                 let mut channel_monitors = HashMap::new();
3295                 channel_monitors.insert(chan_0_monitor.get_funding_txo().unwrap(), &chan_0_monitor);
3296                 <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
3297                         default_config: UserConfig::new(),
3298                         keys_manager,
3299                         fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
3300                         monitor: nodes[0].chan_monitor.clone(),
3301                         chain_monitor: nodes[0].chain_monitor.clone(),
3302                         tx_broadcaster: nodes[0].tx_broadcaster.clone(),
3303                         logger: Arc::new(test_utils::TestLogger::new()),
3304                         channel_monitors: &channel_monitors,
3305                 }).unwrap()
3306         };
3307         assert!(nodes_0_read.is_empty());
3308
3309         assert!(nodes[0].chan_monitor.add_update_monitor(chan_0_monitor.get_funding_txo().unwrap(), chan_0_monitor).is_ok());
3310         nodes[0].node = Arc::new(nodes_0_deserialized);
3311         check_added_monitors!(nodes[0], 1);
3312
3313         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3314
3315         fail_payment(&nodes[0], &[&nodes[1]], our_payment_hash);
3316         claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
3317 }
3318
3319 #[test]
3320 fn test_manager_serialize_deserialize_inconsistent_monitor() {
3321         // Test deserializing a ChannelManager with an out-of-date ChannelMonitor
3322         let mut nodes = create_network(4, &[None, None, None, None]);
3323         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3324         create_announced_chan_between_nodes(&nodes, 2, 0, LocalFeatures::new(), LocalFeatures::new());
3325         let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, LocalFeatures::new(), LocalFeatures::new());
3326
3327         let (our_payment_preimage, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000);
3328
3329         // Serialize the ChannelManager here, but the monitor we keep up-to-date
3330         let nodes_0_serialized = nodes[0].node.encode();
3331
3332         route_payment(&nodes[0], &[&nodes[3]], 1000000);
3333         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3334         nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3335         nodes[3].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
3336
3337         // Now the ChannelMonitor (which is now out-of-sync with ChannelManager for channel w/
3338         // nodes[3])
3339         let mut node_0_monitors_serialized = Vec::new();
3340         for monitor in nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter() {
3341                 let mut writer = test_utils::TestVecWriter(Vec::new());
3342                 monitor.1.write_for_disk(&mut writer).unwrap();
3343                 node_0_monitors_serialized.push(writer.0);
3344         }
3345
3346         nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
3347         let mut node_0_monitors = Vec::new();
3348         for serialized in node_0_monitors_serialized.iter() {
3349                 let mut read = &serialized[..];
3350                 let (_, monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap();
3351                 assert!(read.is_empty());
3352                 node_0_monitors.push(monitor);
3353         }
3354
3355         let mut nodes_0_read = &nodes_0_serialized[..];
3356         let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())));
3357         let (_, nodes_0_deserialized) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
3358                 default_config: UserConfig::new(),
3359                 keys_manager,
3360                 fee_estimator: Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }),
3361                 monitor: nodes[0].chan_monitor.clone(),
3362                 chain_monitor: nodes[0].chain_monitor.clone(),
3363                 tx_broadcaster: nodes[0].tx_broadcaster.clone(),
3364                 logger: Arc::new(test_utils::TestLogger::new()),
3365                 channel_monitors: &node_0_monitors.iter().map(|monitor| { (monitor.get_funding_txo().unwrap(), monitor) }).collect(),
3366         }).unwrap();
3367         assert!(nodes_0_read.is_empty());
3368
3369         { // Channel close should result in a commitment tx and an HTLC tx
3370                 let txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3371                 assert_eq!(txn.len(), 2);
3372                 assert_eq!(txn[0].input[0].previous_output.txid, funding_tx.txid());
3373                 assert_eq!(txn[1].input[0].previous_output.txid, txn[0].txid());
3374         }
3375
3376         for monitor in node_0_monitors.drain(..) {
3377                 assert!(nodes[0].chan_monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor).is_ok());
3378                 check_added_monitors!(nodes[0], 1);
3379         }
3380         nodes[0].node = Arc::new(nodes_0_deserialized);
3381
3382         // nodes[1] and nodes[2] have no lost state with nodes[0]...
3383         reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3384         reconnect_nodes(&nodes[0], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
3385         //... and we can even still claim the payment!
3386         claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage);
3387
3388         nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id());
3389         let reestablish = get_event_msg!(nodes[3], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id());
3390         nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id());
3391         if let Err(msgs::HandleError { action: Some(msgs::ErrorAction::SendErrorMessage { msg }), .. }) = nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish) {
3392                 assert_eq!(msg.channel_id, channel_id);
3393         } else { panic!("Unexpected result"); }
3394 }
3395
3396 macro_rules! check_spendable_outputs {
3397         ($node: expr, $der_idx: expr) => {
3398                 {
3399                         let events = $node.chan_monitor.simple_monitor.get_and_clear_pending_events();
3400                         let mut txn = Vec::new();
3401                         for event in events {
3402                                 match event {
3403                                         Event::SpendableOutputs { ref outputs } => {
3404                                                 for outp in outputs {
3405                                                         match *outp {
3406                                                                 SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
3407                                                                         let input = TxIn {
3408                                                                                 previous_output: outpoint.clone(),
3409                                                                                 script_sig: Script::new(),
3410                                                                                 sequence: 0,
3411                                                                                 witness: Vec::new(),
3412                                                                         };
3413                                                                         let outp = TxOut {
3414                                                                                 script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
3415                                                                                 value: output.value,
3416                                                                         };
3417                                                                         let mut spend_tx = Transaction {
3418                                                                                 version: 2,
3419                                                                                 lock_time: 0,
3420                                                                                 input: vec![input],
3421                                                                                 output: vec![outp],
3422                                                                         };
3423                                                                         let secp_ctx = Secp256k1::new();
3424                                                                         let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
3425                                                                         let witness_script = Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
3426                                                                         let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
3427                                                                         let remotesig = secp_ctx.sign(&sighash, key);
3428                                                                         spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec());
3429                                                                         spend_tx.input[0].witness[0].push(SigHashType::All as u8);
3430                                                                         spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());
3431                                                                         txn.push(spend_tx);
3432                                                                 },
3433                                                                 SpendableOutputDescriptor::DynamicOutputP2WSH { ref outpoint, ref key, ref witness_script, ref to_self_delay, ref output } => {
3434                                                                         let input = TxIn {
3435                                                                                 previous_output: outpoint.clone(),
3436                                                                                 script_sig: Script::new(),
3437                                                                                 sequence: *to_self_delay as u32,
3438                                                                                 witness: Vec::new(),
3439                                                                         };
3440                                                                         let outp = TxOut {
3441                                                                                 script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
3442                                                                                 value: output.value,
3443                                                                         };
3444                                                                         let mut spend_tx = Transaction {
3445                                                                                 version: 2,
3446                                                                                 lock_time: 0,
3447                                                                                 input: vec![input],
3448                                                                                 output: vec![outp],
3449                                                                         };
3450                                                                         let secp_ctx = Secp256k1::new();
3451                                                                         let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], witness_script, output.value)[..]).unwrap();
3452                                                                         let local_delaysig = secp_ctx.sign(&sighash, key);
3453                                                                         spend_tx.input[0].witness.push(local_delaysig.serialize_der().to_vec());
3454                                                                         spend_tx.input[0].witness[0].push(SigHashType::All as u8);
3455                                                                         spend_tx.input[0].witness.push(vec!(0));
3456                                                                         spend_tx.input[0].witness.push(witness_script.clone().into_bytes());
3457                                                                         txn.push(spend_tx);
3458                                                                 },
3459                                                                 SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
3460                                                                         let secp_ctx = Secp256k1::new();
3461                                                                         let input = TxIn {
3462                                                                                 previous_output: outpoint.clone(),
3463                                                                                 script_sig: Script::new(),
3464                                                                                 sequence: 0,
3465                                                                                 witness: Vec::new(),
3466                                                                         };
3467                                                                         let outp = TxOut {
3468                                                                                 script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
3469                                                                                 value: output.value,
3470                                                                         };
3471                                                                         let mut spend_tx = Transaction {
3472                                                                                 version: 2,
3473                                                                                 lock_time: 0,
3474                                                                                 input: vec![input],
3475                                                                                 output: vec![outp.clone()],
3476                                                                         };
3477                                                                         let secret = {
3478                                                                                 match ExtendedPrivKey::new_master(Network::Testnet, &$node.node_seed) {
3479                                                                                         Ok(master_key) => {
3480                                                                                                 match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx).expect("key space exhausted")) {
3481                                                                                                         Ok(key) => key,
3482                                                                                                         Err(_) => panic!("Your RNG is busted"),
3483                                                                                                 }
3484                                                                                         }
3485                                                                                         Err(_) => panic!("Your rng is busted"),
3486                                                                                 }
3487                                                                         };
3488                                                                         let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
3489                                                                         let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
3490                                                                         let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
3491                                                                         let sig = secp_ctx.sign(&sighash, &secret.private_key.key);
3492                                                                         spend_tx.input[0].witness.push(sig.serialize_der().to_vec());
3493                                                                         spend_tx.input[0].witness[0].push(SigHashType::All as u8);
3494                                                                         spend_tx.input[0].witness.push(pubkey.key.serialize().to_vec());
3495                                                                         txn.push(spend_tx);
3496                                                                 },
3497                                                         }
3498                                                 }
3499                                         },
3500                                         _ => panic!("Unexpected event"),
3501                                 };
3502                         }
3503                         txn
3504                 }
3505         }
3506 }
3507
3508 #[test]
3509 fn test_claim_sizeable_push_msat() {
3510         // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
3511         let nodes = create_network(2, &[None, None]);
3512
3513         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
3514         nodes[1].node.force_close_channel(&chan.2);
3515         check_closed_broadcast!(nodes[1]);
3516         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3517         assert_eq!(node_txn.len(), 1);
3518         check_spends!(node_txn[0], chan.3.clone());
3519         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
3520
3521         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3522         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
3523         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3524         assert_eq!(spend_txn.len(), 1);
3525         check_spends!(spend_txn[0], node_txn[0].clone());
3526 }
3527
3528 #[test]
3529 fn test_claim_on_remote_sizeable_push_msat() {
3530         // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
3531         // to_remote output is encumbered by a P2WPKH
3532
3533         let nodes = create_network(2, &[None, None]);
3534
3535         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 99000000, LocalFeatures::new(), LocalFeatures::new());
3536         nodes[0].node.force_close_channel(&chan.2);
3537         check_closed_broadcast!(nodes[0]);
3538
3539         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3540         assert_eq!(node_txn.len(), 1);
3541         check_spends!(node_txn[0], chan.3.clone());
3542         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
3543
3544         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3545         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()] }, 0);
3546         check_closed_broadcast!(nodes[1]);
3547         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3548         assert_eq!(spend_txn.len(), 2);
3549         assert_eq!(spend_txn[0], spend_txn[1]);
3550         check_spends!(spend_txn[0], node_txn[0].clone());
3551 }
3552
3553 #[test]
3554 fn test_claim_on_remote_revoked_sizeable_push_msat() {
3555         // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
3556         // to_remote output is encumbered by a P2WPKH
3557
3558         let nodes = create_network(2, &[None, None]);
3559
3560         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000, LocalFeatures::new(), LocalFeatures::new());
3561         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
3562         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
3563         assert_eq!(revoked_local_txn[0].input.len(), 1);
3564         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
3565
3566         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
3567         let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3568         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
3569         check_closed_broadcast!(nodes[1]);
3570
3571         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3572         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3573         assert_eq!(spend_txn.len(), 4);
3574         assert_eq!(spend_txn[0], spend_txn[2]); // to_remote output on revoked remote commitment_tx
3575         check_spends!(spend_txn[0], revoked_local_txn[0].clone());
3576         assert_eq!(spend_txn[1], spend_txn[3]); // to_local output on local commitment tx
3577         check_spends!(spend_txn[1], node_txn[0].clone());
3578 }
3579
3580 #[test]
3581 fn test_static_spendable_outputs_preimage_tx() {
3582         let nodes = create_network(2, &[None, None]);
3583
3584         // Create some initial channels
3585         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3586
3587         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
3588
3589         let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
3590         assert_eq!(commitment_tx[0].input.len(), 1);
3591         assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
3592
3593         // Settle A's commitment tx on B's chain
3594         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3595         assert!(nodes[1].node.claim_funds(payment_preimage));
3596         check_added_monitors!(nodes[1], 1);
3597         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()] }, 1);
3598         let events = nodes[1].node.get_and_clear_pending_msg_events();
3599         match events[0] {
3600                 MessageSendEvent::UpdateHTLCs { .. } => {},
3601                 _ => panic!("Unexpected event"),
3602         }
3603         match events[1] {
3604                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
3605                 _ => panic!("Unexepected event"),
3606         }
3607
3608         // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
3609         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap(); // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
3610         check_spends!(node_txn[0], commitment_tx[0].clone());
3611         assert_eq!(node_txn[0], node_txn[2]);
3612         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
3613         check_spends!(node_txn[1], chan_1.3.clone());
3614
3615         let spend_txn = check_spendable_outputs!(nodes[1], 1); // , 0, 0, 1, 1);
3616         assert_eq!(spend_txn.len(), 2);
3617         assert_eq!(spend_txn[0], spend_txn[1]);
3618         check_spends!(spend_txn[0], node_txn[0].clone());
3619 }
3620
3621 #[test]
3622 fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
3623         let nodes = create_network(2, &[None, None]);
3624
3625         // Create some initial channels
3626         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3627
3628         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
3629         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.iter().next().unwrap().1.last_local_commitment_txn.clone();
3630         assert_eq!(revoked_local_txn[0].input.len(), 1);
3631         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
3632
3633         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
3634
3635         let  header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3636         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
3637         check_closed_broadcast!(nodes[1]);
3638
3639         let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3640         assert_eq!(node_txn.len(), 3);
3641         assert_eq!(node_txn.pop().unwrap(), node_txn[0]);
3642         assert_eq!(node_txn[0].input.len(), 2);
3643         check_spends!(node_txn[0], revoked_local_txn[0].clone());
3644
3645         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3646         assert_eq!(spend_txn.len(), 2);
3647         assert_eq!(spend_txn[0], spend_txn[1]);
3648         check_spends!(spend_txn[0], node_txn[0].clone());
3649 }
3650
3651 #[test]
3652 fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
3653         let nodes = create_network(2, &[None, None]);
3654
3655         // Create some initial channels
3656         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3657
3658         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
3659         let revoked_local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
3660         assert_eq!(revoked_local_txn[0].input.len(), 1);
3661         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
3662
3663         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
3664
3665         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3666         // A will generate HTLC-Timeout from revoked commitment tx
3667         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
3668         check_closed_broadcast!(nodes[0]);
3669
3670         let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3671         assert_eq!(revoked_htlc_txn.len(), 3);
3672         assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
3673         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
3674         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
3675         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
3676         check_spends!(revoked_htlc_txn[1], chan_1.3.clone());
3677
3678         // B will generate justice tx from A's revoked commitment/HTLC tx
3679         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
3680         check_closed_broadcast!(nodes[1]);
3681
3682         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3683         assert_eq!(node_txn.len(), 4);
3684         assert_eq!(node_txn[3].input.len(), 1);
3685         check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
3686
3687         // Check B's ChannelMonitor was able to generate the right spendable output descriptor
3688         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3689         assert_eq!(spend_txn.len(), 3);
3690         assert_eq!(spend_txn[0], spend_txn[1]);
3691         check_spends!(spend_txn[0], node_txn[0].clone());
3692         check_spends!(spend_txn[2], node_txn[3].clone());
3693 }
3694
3695 #[test]
3696 fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
3697         let nodes = create_network(2, &[None, None]);
3698
3699         // Create some initial channels
3700         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3701
3702         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
3703         let revoked_local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
3704         assert_eq!(revoked_local_txn[0].input.len(), 1);
3705         assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
3706
3707         claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
3708
3709         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3710         // B will generate HTLC-Success from revoked commitment tx
3711         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
3712         check_closed_broadcast!(nodes[1]);
3713         let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3714
3715         assert_eq!(revoked_htlc_txn.len(), 3);
3716         assert_eq!(revoked_htlc_txn[0], revoked_htlc_txn[2]);
3717         assert_eq!(revoked_htlc_txn[0].input.len(), 1);
3718         assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3719         check_spends!(revoked_htlc_txn[0], revoked_local_txn[0].clone());
3720
3721         // A will generate justice tx from B's revoked commitment/HTLC tx
3722         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()] }, 1);
3723         check_closed_broadcast!(nodes[0]);
3724
3725         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
3726         assert_eq!(node_txn.len(), 4);
3727         assert_eq!(node_txn[3].input.len(), 1);
3728         check_spends!(node_txn[3], revoked_htlc_txn[0].clone());
3729
3730         // Check A's ChannelMonitor was able to generate the right spendable output descriptor
3731         let spend_txn = check_spendable_outputs!(nodes[0], 1);
3732         assert_eq!(spend_txn.len(), 5);
3733         assert_eq!(spend_txn[0], spend_txn[2]);
3734         assert_eq!(spend_txn[1], spend_txn[3]);
3735         check_spends!(spend_txn[0], revoked_local_txn[0].clone()); // spending to_remote output from revoked local tx
3736         check_spends!(spend_txn[1], node_txn[2].clone()); // spending justice tx output from revoked local tx htlc received output
3737         check_spends!(spend_txn[4], node_txn[3].clone()); // spending justice tx output on htlc success tx
3738 }
3739
3740 #[test]
3741 fn test_onchain_to_onchain_claim() {
3742         // Test that in case of channel closure, we detect the state of output thanks to
3743         // ChainWatchInterface and claim HTLC on downstream peer's remote commitment tx.
3744         // First, have C claim an HTLC against its own latest commitment transaction.
3745         // Then, broadcast these to B, which should update the monitor downstream on the A<->B
3746         // channel.
3747         // Finally, check that B will claim the HTLC output if A's latest commitment transaction
3748         // gets broadcast.
3749
3750         let nodes = create_network(3, &[None, None, None]);
3751
3752         // Create some initial channels
3753         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3754         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
3755
3756         // Rebalance the network a bit by relaying one payment through all the channels ...
3757         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3758         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3759
3760         let (payment_preimage, _payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
3761         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42};
3762         let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
3763         check_spends!(commitment_tx[0], chan_2.3.clone());
3764         nodes[2].node.claim_funds(payment_preimage);
3765         check_added_monitors!(nodes[2], 1);
3766         let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3767         assert!(updates.update_add_htlcs.is_empty());
3768         assert!(updates.update_fail_htlcs.is_empty());
3769         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3770         assert!(updates.update_fail_malformed_htlcs.is_empty());
3771
3772         nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
3773         check_closed_broadcast!(nodes[2]);
3774
3775         let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
3776         assert_eq!(c_txn.len(), 3);
3777         assert_eq!(c_txn[0], c_txn[2]);
3778         assert_eq!(commitment_tx[0], c_txn[1]);
3779         check_spends!(c_txn[1], chan_2.3.clone());
3780         check_spends!(c_txn[2], c_txn[1].clone());
3781         assert_eq!(c_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
3782         assert_eq!(c_txn[2].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3783         assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
3784         assert_eq!(c_txn[0].lock_time, 0); // Success tx
3785
3786         // 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
3787         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![c_txn[1].clone(), c_txn[2].clone()]}, 1);
3788         {
3789                 let mut b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3790                 assert_eq!(b_txn.len(), 4);
3791                 assert_eq!(b_txn[0], b_txn[3]);
3792                 check_spends!(b_txn[1], chan_2.3); // B local commitment tx, issued by ChannelManager
3793                 check_spends!(b_txn[2], b_txn[1].clone()); // HTLC-Timeout on B local commitment tx, issued by ChannelManager
3794                 assert_eq!(b_txn[2].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
3795                 assert!(b_txn[2].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
3796                 assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
3797                 check_spends!(b_txn[0], c_txn[1].clone()); // timeout tx on C remote commitment tx, issued by ChannelMonitor, * 2 due to block rescan
3798                 assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3799                 assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
3800                 assert_ne!(b_txn[2].lock_time, 0); // Timeout tx
3801                 b_txn.clear();
3802         }
3803         let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
3804         check_added_monitors!(nodes[1], 1);
3805         match msg_events[0] {
3806                 MessageSendEvent::BroadcastChannelUpdate {  .. } => {},
3807                 _ => panic!("Unexpected event"),
3808         }
3809         match msg_events[1] {
3810                 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, .. } } => {
3811                         assert!(update_add_htlcs.is_empty());
3812                         assert!(update_fail_htlcs.is_empty());
3813                         assert_eq!(update_fulfill_htlcs.len(), 1);
3814                         assert!(update_fail_malformed_htlcs.is_empty());
3815                         assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3816                 },
3817                 _ => panic!("Unexpected event"),
3818         };
3819         // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
3820         let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
3821         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
3822         let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3823         assert_eq!(b_txn.len(), 3);
3824         check_spends!(b_txn[1], chan_1.3); // Local commitment tx, issued by ChannelManager
3825         assert_eq!(b_txn[0], b_txn[2]); // HTLC-Success tx, issued by ChannelMonitor, * 2 due to block rescan
3826         check_spends!(b_txn[0], commitment_tx[0].clone());
3827         assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
3828         assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
3829         assert_eq!(b_txn[2].lock_time, 0); // Success tx
3830
3831         check_closed_broadcast!(nodes[1]);
3832 }
3833
3834 #[test]
3835 fn test_duplicate_payment_hash_one_failure_one_success() {
3836         // Topology : A --> B --> C
3837         // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
3838         let mut nodes = create_network(3, &[None, None, None]);
3839
3840         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3841         let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
3842
3843         let (our_payment_preimage, duplicate_payment_hash) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000);
3844         *nodes[0].network_payment_count.borrow_mut() -= 1;
3845         assert_eq!(route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 900000).1, duplicate_payment_hash);
3846
3847         let commitment_txn = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
3848         assert_eq!(commitment_txn[0].input.len(), 1);
3849         check_spends!(commitment_txn[0], chan_2.3.clone());
3850
3851         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3852         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
3853         check_closed_broadcast!(nodes[1]);
3854
3855         let htlc_timeout_tx;
3856         { // Extract one of the two HTLC-Timeout transaction
3857                 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3858                 assert_eq!(node_txn.len(), 7);
3859                 assert_eq!(node_txn[0], node_txn[5]);
3860                 assert_eq!(node_txn[1], node_txn[6]);
3861                 check_spends!(node_txn[0], commitment_txn[0].clone());
3862                 assert_eq!(node_txn[0].input.len(), 1);
3863                 check_spends!(node_txn[1], commitment_txn[0].clone());
3864                 assert_eq!(node_txn[1].input.len(), 1);
3865                 assert_ne!(node_txn[0].input[0], node_txn[1].input[0]);
3866                 check_spends!(node_txn[2], chan_2.3.clone());
3867                 check_spends!(node_txn[3], node_txn[2].clone());
3868                 check_spends!(node_txn[4], node_txn[2].clone());
3869                 htlc_timeout_tx = node_txn[1].clone();
3870         }
3871
3872         nodes[2].node.claim_funds(our_payment_preimage);
3873         nodes[2].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_txn[0].clone()] }, 1);
3874         check_added_monitors!(nodes[2], 2);
3875         let events = nodes[2].node.get_and_clear_pending_msg_events();
3876         match events[0] {
3877                 MessageSendEvent::UpdateHTLCs { .. } => {},
3878                 _ => panic!("Unexpected event"),
3879         }
3880         match events[1] {
3881                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
3882                 _ => panic!("Unexepected event"),
3883         }
3884         let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
3885         assert_eq!(htlc_success_txn.len(), 5);
3886         check_spends!(htlc_success_txn[2], chan_2.3.clone());
3887         assert_eq!(htlc_success_txn[0], htlc_success_txn[3]);
3888         assert_eq!(htlc_success_txn[0].input.len(), 1);
3889         assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3890         assert_eq!(htlc_success_txn[1], htlc_success_txn[4]);
3891         assert_eq!(htlc_success_txn[1].input.len(), 1);
3892         assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3893         assert_ne!(htlc_success_txn[0].input[0], htlc_success_txn[1].input[0]);
3894         check_spends!(htlc_success_txn[0], commitment_txn[0].clone());
3895         check_spends!(htlc_success_txn[1], commitment_txn[0].clone());
3896
3897         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_timeout_tx] }, 200);
3898         connect_blocks(&nodes[1].chain_monitor, ANTI_REORG_DELAY - 1, 200, true, header.bitcoin_hash());
3899         expect_pending_htlcs_forwardable!(nodes[1]);
3900         let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3901         assert!(htlc_updates.update_add_htlcs.is_empty());
3902         assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
3903         assert_eq!(htlc_updates.update_fail_htlcs[0].htlc_id, 1);
3904         assert!(htlc_updates.update_fulfill_htlcs.is_empty());
3905         assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
3906         check_added_monitors!(nodes[1], 1);
3907
3908         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]).unwrap();
3909         assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3910         {
3911                 commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
3912                 let events = nodes[0].node.get_and_clear_pending_msg_events();
3913                 assert_eq!(events.len(), 1);
3914                 match events[0] {
3915                         MessageSendEvent::PaymentFailureNetworkUpdate { update: msgs::HTLCFailChannelUpdate::ChannelClosed { .. }  } => {
3916                         },
3917                         _ => { panic!("Unexpected event"); }
3918                 }
3919         }
3920         let events = nodes[0].node.get_and_clear_pending_events();
3921         match events[0] {
3922                 Event::PaymentFailed { ref payment_hash, .. } => {
3923                         assert_eq!(*payment_hash, duplicate_payment_hash);
3924                 }
3925                 _ => panic!("Unexpected event"),
3926         }
3927
3928         // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
3929         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![htlc_success_txn[0].clone()] }, 200);
3930         let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3931         assert!(updates.update_add_htlcs.is_empty());
3932         assert!(updates.update_fail_htlcs.is_empty());
3933         assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3934         assert_eq!(updates.update_fulfill_htlcs[0].htlc_id, 0);
3935         assert!(updates.update_fail_malformed_htlcs.is_empty());
3936         check_added_monitors!(nodes[1], 1);
3937
3938         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]).unwrap();
3939         commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
3940
3941         let events = nodes[0].node.get_and_clear_pending_events();
3942         match events[0] {
3943                 Event::PaymentSent { ref payment_preimage } => {
3944                         assert_eq!(*payment_preimage, our_payment_preimage);
3945                 }
3946                 _ => panic!("Unexpected event"),
3947         }
3948 }
3949
3950 #[test]
3951 fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
3952         let nodes = create_network(2, &[None, None]);
3953
3954         // Create some initial channels
3955         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
3956
3957         let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
3958         let local_txn = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
3959         assert_eq!(local_txn[0].input.len(), 1);
3960         check_spends!(local_txn[0], chan_1.3.clone());
3961
3962         // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
3963         nodes[1].node.claim_funds(payment_preimage);
3964         check_added_monitors!(nodes[1], 1);
3965         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
3966         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 1);
3967         let events = nodes[1].node.get_and_clear_pending_msg_events();
3968         match events[0] {
3969                 MessageSendEvent::UpdateHTLCs { .. } => {},
3970                 _ => panic!("Unexpected event"),
3971         }
3972         match events[1] {
3973                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
3974                 _ => panic!("Unexepected event"),
3975         }
3976         let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3977         assert_eq!(node_txn[0].input.len(), 1);
3978         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3979         check_spends!(node_txn[0], local_txn[0].clone());
3980
3981         // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
3982         let spend_txn = check_spendable_outputs!(nodes[1], 1);
3983         assert_eq!(spend_txn.len(), 2);
3984         check_spends!(spend_txn[0], node_txn[0].clone());
3985         check_spends!(spend_txn[1], node_txn[2].clone());
3986 }
3987
3988 fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
3989         // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
3990         // unrevoked commitment transaction.
3991         // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
3992         // a remote RAA before they could be failed backwards (and combinations thereof).
3993         // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
3994         // use the same payment hashes.
3995         // Thus, we use a six-node network:
3996         //
3997         // A \         / E
3998         //    - C - D -
3999         // B /         \ F
4000         // And test where C fails back to A/B when D announces its latest commitment transaction
4001         let nodes = create_network(6, &[None, None, None, None, None, None]);
4002
4003         create_announced_chan_between_nodes(&nodes, 0, 2, LocalFeatures::new(), LocalFeatures::new());
4004         create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new());
4005         let chan = create_announced_chan_between_nodes(&nodes, 2, 3, LocalFeatures::new(), LocalFeatures::new());
4006         create_announced_chan_between_nodes(&nodes, 3, 4, LocalFeatures::new(), LocalFeatures::new());
4007         create_announced_chan_between_nodes(&nodes, 3, 5, LocalFeatures::new(), LocalFeatures::new());
4008
4009         // Rebalance and check output sanity...
4010         send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
4011         send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
4012         assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 2);
4013
4014         let ds_dust_limit = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
4015         // 0th HTLC:
4016         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
4017         // 1st HTLC:
4018         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
4019         let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
4020         // 2nd HTLC:
4021         send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_1); // not added < dust limit + HTLC tx fee
4022         // 3rd HTLC:
4023         send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_2); // not added < dust limit + HTLC tx fee
4024         // 4th HTLC:
4025         let (_, payment_hash_3) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
4026         // 5th HTLC:
4027         let (_, payment_hash_4) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
4028         let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
4029         // 6th HTLC:
4030         send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_3);
4031         // 7th HTLC:
4032         send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_4);
4033
4034         // 8th HTLC:
4035         let (_, payment_hash_5) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
4036         // 9th HTLC:
4037         let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap();
4038         send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_5); // not added < dust limit + HTLC tx fee
4039
4040         // 10th HTLC:
4041         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
4042         // 11th HTLC:
4043         let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap();
4044         send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_6);
4045
4046         // Double-check that six of the new HTLC were added
4047         // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
4048         // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
4049         assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.len(), 1);
4050         assert_eq!(nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn[0].output.len(), 8);
4051
4052         // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
4053         // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
4054         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
4055         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
4056         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
4057         assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
4058         check_added_monitors!(nodes[4], 0);
4059         expect_pending_htlcs_forwardable!(nodes[4]);
4060         check_added_monitors!(nodes[4], 1);
4061
4062         let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
4063         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]).unwrap();
4064         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]).unwrap();
4065         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]).unwrap();
4066         nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]).unwrap();
4067         commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
4068
4069         // Fail 3rd below-dust and 7th above-dust HTLCs
4070         assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
4071         assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
4072         check_added_monitors!(nodes[5], 0);
4073         expect_pending_htlcs_forwardable!(nodes[5]);
4074         check_added_monitors!(nodes[5], 1);
4075
4076         let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
4077         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]).unwrap();
4078         nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]).unwrap();
4079         commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
4080
4081         let ds_prev_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
4082
4083         expect_pending_htlcs_forwardable!(nodes[3]);
4084         check_added_monitors!(nodes[3], 1);
4085         let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
4086         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]).unwrap();
4087         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]).unwrap();
4088         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]).unwrap();
4089         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]).unwrap();
4090         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]).unwrap();
4091         nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]).unwrap();
4092         if deliver_last_raa {
4093                 commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
4094         } else {
4095                 let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
4096         }
4097
4098         // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
4099         // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
4100         // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
4101         // propagated back to A/B yet (and D has two unrevoked commitment transactions).
4102         //
4103         // We now broadcast the latest commitment transaction, which *should* result in failures for
4104         // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
4105         // the non-broadcast above-dust HTLCs.
4106         //
4107         // Alternatively, we may broadcast the previous commitment transaction, which should only
4108         // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
4109         let ds_last_commitment_tx = nodes[3].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
4110
4111         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4112         if announce_latest {
4113                 nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_last_commitment_tx[0]], &[1; 1]);
4114         } else {
4115                 nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&ds_prev_commitment_tx[0]], &[1; 1]);
4116         }
4117         connect_blocks(&nodes[2].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
4118         check_closed_broadcast!(nodes[2]);
4119         expect_pending_htlcs_forwardable!(nodes[2]);
4120         check_added_monitors!(nodes[2], 2);
4121
4122         let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
4123         assert_eq!(cs_msgs.len(), 2);
4124         let mut a_done = false;
4125         for msg in cs_msgs {
4126                 match msg {
4127                         MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
4128                                 // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
4129                                 // should be failed-backwards here.
4130                                 let target = if *node_id == nodes[0].node.get_our_node_id() {
4131                                         // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
4132                                         for htlc in &updates.update_fail_htlcs {
4133                                                 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 });
4134                                         }
4135                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
4136                                         assert!(!a_done);
4137                                         a_done = true;
4138                                         &nodes[0]
4139                                 } else {
4140                                         // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
4141                                         for htlc in &updates.update_fail_htlcs {
4142                                                 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
4143                                         }
4144                                         assert_eq!(*node_id, nodes[1].node.get_our_node_id());
4145                                         assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
4146                                         &nodes[1]
4147                                 };
4148                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]).unwrap();
4149                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]).unwrap();
4150                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]).unwrap();
4151                                 if announce_latest {
4152                                         target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]).unwrap();
4153                                         if *node_id == nodes[0].node.get_our_node_id() {
4154                                                 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]).unwrap();
4155                                         }
4156                                 }
4157                                 commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
4158                         },
4159                         _ => panic!("Unexpected event"),
4160                 }
4161         }
4162
4163         let as_events = nodes[0].node.get_and_clear_pending_events();
4164         assert_eq!(as_events.len(), if announce_latest { 5 } else { 3 });
4165         let mut as_failds = HashSet::new();
4166         for event in as_events.iter() {
4167                 if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
4168                         assert!(as_failds.insert(*payment_hash));
4169                         if *payment_hash != payment_hash_2 {
4170                                 assert_eq!(*rejected_by_dest, deliver_last_raa);
4171                         } else {
4172                                 assert!(!rejected_by_dest);
4173                         }
4174                 } else { panic!("Unexpected event"); }
4175         }
4176         assert!(as_failds.contains(&payment_hash_1));
4177         assert!(as_failds.contains(&payment_hash_2));
4178         if announce_latest {
4179                 assert!(as_failds.contains(&payment_hash_3));
4180                 assert!(as_failds.contains(&payment_hash_5));
4181         }
4182         assert!(as_failds.contains(&payment_hash_6));
4183
4184         let bs_events = nodes[1].node.get_and_clear_pending_events();
4185         assert_eq!(bs_events.len(), if announce_latest { 4 } else { 3 });
4186         let mut bs_failds = HashSet::new();
4187         for event in bs_events.iter() {
4188                 if let &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest, .. } = event {
4189                         assert!(bs_failds.insert(*payment_hash));
4190                         if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
4191                                 assert_eq!(*rejected_by_dest, deliver_last_raa);
4192                         } else {
4193                                 assert!(!rejected_by_dest);
4194                         }
4195                 } else { panic!("Unexpected event"); }
4196         }
4197         assert!(bs_failds.contains(&payment_hash_1));
4198         assert!(bs_failds.contains(&payment_hash_2));
4199         if announce_latest {
4200                 assert!(bs_failds.contains(&payment_hash_4));
4201         }
4202         assert!(bs_failds.contains(&payment_hash_5));
4203
4204         // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
4205         // get a PaymentFailureNetworkUpdate. A should have gotten 4 HTLCs which were failed-back due
4206         // to unknown-preimage-etc, B should have gotten 2. Thus, in the
4207         // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2
4208         // PaymentFailureNetworkUpdates.
4209         let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events();
4210         assert_eq!(as_msg_events.len(), if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
4211         let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4212         assert_eq!(bs_msg_events.len(), if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
4213         for event in as_msg_events.iter().chain(bs_msg_events.iter()) {
4214                 match event {
4215                         &MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
4216                         _ => panic!("Unexpected event"),
4217                 }
4218         }
4219 }
4220
4221 #[test]
4222 fn test_fail_backwards_latest_remote_announce_a() {
4223         do_test_fail_backwards_unrevoked_remote_announce(false, true);
4224 }
4225
4226 #[test]
4227 fn test_fail_backwards_latest_remote_announce_b() {
4228         do_test_fail_backwards_unrevoked_remote_announce(true, true);
4229 }
4230
4231 #[test]
4232 fn test_fail_backwards_previous_remote_announce() {
4233         do_test_fail_backwards_unrevoked_remote_announce(false, false);
4234         // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
4235         // tested for in test_commitment_revoked_fail_backward_exhaustive()
4236 }
4237
4238 #[test]
4239 fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
4240         let nodes = create_network(2, &[None, None]);
4241
4242         // Create some initial channels
4243         let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
4244
4245         route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000).0;
4246         let local_txn = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4247         assert_eq!(local_txn[0].input.len(), 1);
4248         check_spends!(local_txn[0], chan_1.3.clone());
4249
4250         // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
4251         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4252         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![local_txn[0].clone()] }, 200);
4253         check_closed_broadcast!(nodes[0]);
4254
4255         let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
4256         assert_eq!(node_txn[0].input.len(), 1);
4257         assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4258         check_spends!(node_txn[0], local_txn[0].clone());
4259
4260         // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
4261         let spend_txn = check_spendable_outputs!(nodes[0], 1);
4262         assert_eq!(spend_txn.len(), 8);
4263         assert_eq!(spend_txn[0], spend_txn[2]);
4264         assert_eq!(spend_txn[0], spend_txn[4]);
4265         assert_eq!(spend_txn[0], spend_txn[6]);
4266         assert_eq!(spend_txn[1], spend_txn[3]);
4267         assert_eq!(spend_txn[1], spend_txn[5]);
4268         assert_eq!(spend_txn[1], spend_txn[7]);
4269         check_spends!(spend_txn[0], local_txn[0].clone());
4270         check_spends!(spend_txn[1], node_txn[0].clone());
4271 }
4272
4273 #[test]
4274 fn test_static_output_closing_tx() {
4275         let nodes = create_network(2, &[None, None]);
4276
4277         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
4278
4279         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
4280         let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
4281
4282         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4283         nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
4284         let spend_txn = check_spendable_outputs!(nodes[0], 2);
4285         assert_eq!(spend_txn.len(), 1);
4286         check_spends!(spend_txn[0], closing_tx.clone());
4287
4288         nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![closing_tx.clone()] }, 1);
4289         let spend_txn = check_spendable_outputs!(nodes[1], 2);
4290         assert_eq!(spend_txn.len(), 1);
4291         check_spends!(spend_txn[0], closing_tx);
4292 }
4293
4294 fn do_htlc_claim_local_commitment_only(use_dust: bool) {
4295         let nodes = create_network(2, &[None, None]);
4296         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
4297
4298         let (our_payment_preimage, _) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3000000 });
4299
4300         // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
4301         // present in B's local commitment transaction, but none of A's commitment transactions.
4302         assert!(nodes[1].node.claim_funds(our_payment_preimage));
4303         check_added_monitors!(nodes[1], 1);
4304
4305         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4306         nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]).unwrap();
4307         let events = nodes[0].node.get_and_clear_pending_events();
4308         assert_eq!(events.len(), 1);
4309         match events[0] {
4310                 Event::PaymentSent { payment_preimage } => {
4311                         assert_eq!(payment_preimage, our_payment_preimage);
4312                 },
4313                 _ => panic!("Unexpected event"),
4314         }
4315
4316         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
4317         check_added_monitors!(nodes[0], 1);
4318         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4319         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
4320         check_added_monitors!(nodes[1], 1);
4321
4322         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4323         for i in 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + CHAN_CONFIRM_DEPTH + 1 {
4324                 nodes[1].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
4325                 header.prev_blockhash = header.bitcoin_hash();
4326         }
4327         test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
4328         check_closed_broadcast!(nodes[1]);
4329 }
4330
4331 fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
4332         let mut nodes = create_network(2, &[None, None]);
4333         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
4334
4335         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap();
4336         let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
4337         nodes[0].node.send_payment(route, payment_hash).unwrap();
4338         check_added_monitors!(nodes[0], 1);
4339
4340         let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4341
4342         // As far as A is concerned, the HTLC is now present only in the latest remote commitment
4343         // transaction, however it is not in A's latest local commitment, so we can just broadcast that
4344         // to "time out" the HTLC.
4345
4346         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4347         for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
4348                 nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
4349                 header.prev_blockhash = header.bitcoin_hash();
4350         }
4351         test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
4352         check_closed_broadcast!(nodes[0]);
4353 }
4354
4355 fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
4356         let nodes = create_network(3, &[None, None, None]);
4357         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
4358
4359         // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
4360         // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
4361         // Also optionally test that we *don't* fail the channel in case the commitment transaction was
4362         // actually revoked.
4363         let htlc_value = if use_dust { 50000 } else { 3000000 };
4364         let (_, our_payment_hash) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
4365         assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
4366         expect_pending_htlcs_forwardable!(nodes[1]);
4367         check_added_monitors!(nodes[1], 1);
4368
4369         let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4370         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]).unwrap();
4371         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed).unwrap();
4372         check_added_monitors!(nodes[0], 1);
4373         let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4374         nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0).unwrap();
4375         check_added_monitors!(nodes[1], 1);
4376         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1).unwrap();
4377         check_added_monitors!(nodes[1], 1);
4378         let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4379
4380         if check_revoke_no_close {
4381                 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack).unwrap();
4382                 check_added_monitors!(nodes[0], 1);
4383         }
4384
4385         let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4386         for i in 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 1 {
4387                 nodes[0].chain_monitor.block_connected_checked(&header, i, &Vec::new(), &Vec::new());
4388                 header.prev_blockhash = header.bitcoin_hash();
4389         }
4390         if !check_revoke_no_close {
4391                 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
4392                 check_closed_broadcast!(nodes[0]);
4393         } else {
4394                 let events = nodes[0].node.get_and_clear_pending_events();
4395                 assert_eq!(events.len(), 1);
4396                 match events[0] {
4397                         Event::PaymentFailed { payment_hash, rejected_by_dest, .. } => {
4398                                 assert_eq!(payment_hash, our_payment_hash);
4399                                 assert!(rejected_by_dest);
4400                         },
4401                         _ => panic!("Unexpected event"),
4402                 }
4403         }
4404 }
4405
4406 // Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
4407 // There are only a few cases to test here:
4408 //  * its not really normative behavior, but we test that below-dust HTLCs "included" in
4409 //    broadcastable commitment transactions result in channel closure,
4410 //  * its included in an unrevoked-but-previous remote commitment transaction,
4411 //  * its included in the latest remote or local commitment transactions.
4412 // We test each of the three possible commitment transactions individually and use both dust and
4413 // non-dust HTLCs.
4414 // Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
4415 // assume they are handled the same across all six cases, as both outbound and inbound failures are
4416 // tested for at least one of the cases in other tests.
4417 #[test]
4418 fn htlc_claim_single_commitment_only_a() {
4419         do_htlc_claim_local_commitment_only(true);
4420         do_htlc_claim_local_commitment_only(false);
4421
4422         do_htlc_claim_current_remote_commitment_only(true);
4423         do_htlc_claim_current_remote_commitment_only(false);
4424 }
4425
4426 #[test]
4427 fn htlc_claim_single_commitment_only_b() {
4428         do_htlc_claim_previous_remote_commitment_only(true, false);
4429         do_htlc_claim_previous_remote_commitment_only(false, false);
4430         do_htlc_claim_previous_remote_commitment_only(true, true);
4431         do_htlc_claim_previous_remote_commitment_only(false, true);
4432 }
4433
4434 fn run_onion_failure_test<F1,F2>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, callback_msg: F1, callback_node: F2, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
4435         where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
4436                                 F2: FnMut(),
4437 {
4438         run_onion_failure_test_with_fail_intercept(_name, test_case, nodes, route, payment_hash, callback_msg, |_|{}, callback_node, expected_retryable, expected_error_code, expected_channel_update);
4439 }
4440
4441 // test_case
4442 // 0: node1 fails backward
4443 // 1: final node fails backward
4444 // 2: payment completed but the user rejects the payment
4445 // 3: final node fails backward (but tamper onion payloads from node0)
4446 // 100: trigger error in the intermediate node and tamper returning fail_htlc
4447 // 200: trigger error in the final node and tamper returning fail_htlc
4448 fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case: u8, nodes: &Vec<Node>, route: &Route, payment_hash: &PaymentHash, mut callback_msg: F1, mut callback_fail: F2, mut callback_node: F3, expected_retryable: bool, expected_error_code: Option<u16>, expected_channel_update: Option<HTLCFailChannelUpdate>)
4449         where F1: for <'a> FnMut(&'a mut msgs::UpdateAddHTLC),
4450                                 F2: for <'a> FnMut(&'a mut msgs::UpdateFailHTLC),
4451                                 F3: FnMut(),
4452 {
4453         use ln::msgs::HTLCFailChannelUpdate;
4454
4455         // reset block height
4456         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4457         for ix in 0..nodes.len() {
4458                 nodes[ix].chain_monitor.block_connected_checked(&header, 1, &Vec::new()[..], &[0; 0]);
4459         }
4460
4461         macro_rules! expect_event {
4462                 ($node: expr, $event_type: path) => {{
4463                         let events = $node.node.get_and_clear_pending_events();
4464                         assert_eq!(events.len(), 1);
4465                         match events[0] {
4466                                 $event_type { .. } => {},
4467                                 _ => panic!("Unexpected event"),
4468                         }
4469                 }}
4470         }
4471
4472         macro_rules! expect_htlc_forward {
4473                 ($node: expr) => {{
4474                         expect_event!($node, Event::PendingHTLCsForwardable);
4475                         $node.node.process_pending_htlc_forwards();
4476                 }}
4477         }
4478
4479         // 0 ~~> 2 send payment
4480         nodes[0].node.send_payment(route.clone(), payment_hash.clone()).unwrap();
4481         check_added_monitors!(nodes[0], 1);
4482         let update_0 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4483         // temper update_add (0 => 1)
4484         let mut update_add_0 = update_0.update_add_htlcs[0].clone();
4485         if test_case == 0 || test_case == 3 || test_case == 100 {
4486                 callback_msg(&mut update_add_0);
4487                 callback_node();
4488         }
4489         // 0 => 1 update_add & CS
4490         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_0).unwrap();
4491         commitment_signed_dance!(nodes[1], nodes[0], &update_0.commitment_signed, false, true);
4492
4493         let update_1_0 = match test_case {
4494                 0|100 => { // intermediate node failure; fail backward to 0
4495                         let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4496                         assert!(update_1_0.update_fail_htlcs.len()+update_1_0.update_fail_malformed_htlcs.len()==1 && (update_1_0.update_fail_htlcs.len()==1 || update_1_0.update_fail_malformed_htlcs.len()==1));
4497                         update_1_0
4498                 },
4499                 1|2|3|200 => { // final node failure; forwarding to 2
4500                         assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4501                         // forwarding on 1
4502                         if test_case != 200 {
4503                                 callback_node();
4504                         }
4505                         expect_htlc_forward!(&nodes[1]);
4506
4507                         let update_1 = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
4508                         check_added_monitors!(&nodes[1], 1);
4509                         assert_eq!(update_1.update_add_htlcs.len(), 1);
4510                         // tamper update_add (1 => 2)
4511                         let mut update_add_1 = update_1.update_add_htlcs[0].clone();
4512                         if test_case != 3 && test_case != 200 {
4513                                 callback_msg(&mut update_add_1);
4514                         }
4515
4516                         // 1 => 2
4517                         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_1).unwrap();
4518                         commitment_signed_dance!(nodes[2], nodes[1], update_1.commitment_signed, false, true);
4519
4520                         if test_case == 2 || test_case == 200 {
4521                                 expect_htlc_forward!(&nodes[2]);
4522                                 expect_event!(&nodes[2], Event::PaymentReceived);
4523                                 callback_node();
4524                                 expect_pending_htlcs_forwardable!(nodes[2]);
4525                         }
4526
4527                         let update_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
4528                         if test_case == 2 || test_case == 200 {
4529                                 check_added_monitors!(&nodes[2], 1);
4530                         }
4531                         assert!(update_2_1.update_fail_htlcs.len() == 1);
4532
4533                         let mut fail_msg = update_2_1.update_fail_htlcs[0].clone();
4534                         if test_case == 200 {
4535                                 callback_fail(&mut fail_msg);
4536                         }
4537
4538                         // 2 => 1
4539                         nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_msg).unwrap();
4540                         commitment_signed_dance!(nodes[1], nodes[2], update_2_1.commitment_signed, true);
4541
4542                         // backward fail on 1
4543                         let update_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4544                         assert!(update_1_0.update_fail_htlcs.len() == 1);
4545                         update_1_0
4546                 },
4547                 _ => unreachable!(),
4548         };
4549
4550         // 1 => 0 commitment_signed_dance
4551         if update_1_0.update_fail_htlcs.len() > 0 {
4552                 let mut fail_msg = update_1_0.update_fail_htlcs[0].clone();
4553                 if test_case == 100 {
4554                         callback_fail(&mut fail_msg);
4555                 }
4556                 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg).unwrap();
4557         } else {
4558                 nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_1_0.update_fail_malformed_htlcs[0]).unwrap();
4559         };
4560
4561         commitment_signed_dance!(nodes[0], nodes[1], update_1_0.commitment_signed, false, true);
4562
4563         let events = nodes[0].node.get_and_clear_pending_events();
4564         assert_eq!(events.len(), 1);
4565         if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
4566                 assert_eq!(*rejected_by_dest, !expected_retryable);
4567                 assert_eq!(*error_code, expected_error_code);
4568         } else {
4569                 panic!("Uexpected event");
4570         }
4571
4572         let events = nodes[0].node.get_and_clear_pending_msg_events();
4573         if expected_channel_update.is_some() {
4574                 assert_eq!(events.len(), 1);
4575                 match events[0] {
4576                         MessageSendEvent::PaymentFailureNetworkUpdate { ref update } => {
4577                                 match update {
4578                                         &HTLCFailChannelUpdate::ChannelUpdateMessage { .. } => {
4579                                                 if let HTLCFailChannelUpdate::ChannelUpdateMessage { .. } = expected_channel_update.unwrap() {} else {
4580                                                         panic!("channel_update not found!");
4581                                                 }
4582                                         },
4583                                         &HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => {
4584                                                 if let HTLCFailChannelUpdate::ChannelClosed { short_channel_id: ref expected_short_channel_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
4585                                                         assert!(*short_channel_id == *expected_short_channel_id);
4586                                                         assert!(*is_permanent == *expected_is_permanent);
4587                                                 } else {
4588                                                         panic!("Unexpected message event");
4589                                                 }
4590                                         },
4591                                         &HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => {
4592                                                 if let HTLCFailChannelUpdate::NodeFailure { node_id: ref expected_node_id, is_permanent: ref expected_is_permanent } = expected_channel_update.unwrap() {
4593                                                         assert!(*node_id == *expected_node_id);
4594                                                         assert!(*is_permanent == *expected_is_permanent);
4595                                                 } else {
4596                                                         panic!("Unexpected message event");
4597                                                 }
4598                                         },
4599                                 }
4600                         },
4601                         _ => panic!("Unexpected message event"),
4602                 }
4603         } else {
4604                 assert_eq!(events.len(), 0);
4605         }
4606 }
4607
4608 impl msgs::ChannelUpdate {
4609         fn dummy() -> msgs::ChannelUpdate {
4610                 use secp256k1::ffi::Signature as FFISignature;
4611                 use secp256k1::Signature;
4612                 msgs::ChannelUpdate {
4613                         signature: Signature::from(FFISignature::new()),
4614                         contents: msgs::UnsignedChannelUpdate {
4615                                 chain_hash: Sha256dHash::hash(&vec![0u8][..]),
4616                                 short_channel_id: 0,
4617                                 timestamp: 0,
4618                                 flags: 0,
4619                                 cltv_expiry_delta: 0,
4620                                 htlc_minimum_msat: 0,
4621                                 fee_base_msat: 0,
4622                                 fee_proportional_millionths: 0,
4623                                 excess_data: vec![],
4624                         }
4625                 }
4626         }
4627 }
4628
4629 #[test]
4630 fn test_onion_failure() {
4631         use ln::msgs::ChannelUpdate;
4632         use ln::channelmanager::CLTV_FAR_FAR_AWAY;
4633         use secp256k1;
4634
4635         const BADONION: u16 = 0x8000;
4636         const PERM: u16 = 0x4000;
4637         const NODE: u16 = 0x2000;
4638         const UPDATE: u16 = 0x1000;
4639
4640         let mut nodes = create_network(3, &[None, None, None]);
4641         for node in nodes.iter() {
4642                 *node.keys_manager.override_session_priv.lock().unwrap() = Some(SecretKey::from_slice(&[3; 32]).unwrap());
4643         }
4644         let channels = [create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new()), create_announced_chan_between_nodes(&nodes, 1, 2, LocalFeatures::new(), LocalFeatures::new())];
4645         let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]);
4646         let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap();
4647         // positve case
4648         send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 40000);
4649
4650         // intermediate node failure
4651         run_onion_failure_test("invalid_realm", 0, &nodes, &route, &payment_hash, |msg| {
4652                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4653                 let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
4654                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4655                 let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
4656                 onion_payloads[0].realm = 3;
4657                 msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
4658         }, ||{}, true, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));//XXX incremented channels idx here
4659
4660         // final node failure
4661         run_onion_failure_test("invalid_realm", 3, &nodes, &route, &payment_hash, |msg| {
4662                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4663                 let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
4664                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4665                 let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
4666                 onion_payloads[1].realm = 3;
4667                 msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
4668         }, ||{}, false, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
4669
4670         // the following three with run_onion_failure_test_with_fail_intercept() test only the origin node
4671         // receiving simulated fail messages
4672         // intermediate node failure
4673         run_onion_failure_test_with_fail_intercept("temporary_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
4674                 // trigger error
4675                 msg.amount_msat -= 1;
4676         }, |msg| {
4677                 // and tamper returning error message
4678                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4679                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4680                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], NODE|2, &[0;0]);
4681         }, ||{}, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: false}));
4682
4683         // final node failure
4684         run_onion_failure_test_with_fail_intercept("temporary_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
4685                 // and tamper returning error message
4686                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4687                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4688                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], NODE|2, &[0;0]);
4689         }, ||{
4690                 nodes[2].node.fail_htlc_backwards(&payment_hash);
4691         }, true, Some(NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: false}));
4692
4693         // intermediate node failure
4694         run_onion_failure_test_with_fail_intercept("permanent_node_failure", 100, &nodes, &route, &payment_hash, |msg| {
4695                 msg.amount_msat -= 1;
4696         }, |msg| {
4697                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4698                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4699                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|2, &[0;0]);
4700         }, ||{}, true, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
4701
4702         // final node failure
4703         run_onion_failure_test_with_fail_intercept("permanent_node_failure", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
4704                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4705                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4706                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|2, &[0;0]);
4707         }, ||{
4708                 nodes[2].node.fail_htlc_backwards(&payment_hash);
4709         }, false, Some(PERM|NODE|2), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
4710
4711         // intermediate node failure
4712         run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
4713                 msg.amount_msat -= 1;
4714         }, |msg| {
4715                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4716                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4717                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|NODE|3, &[0;0]);
4718         }, ||{
4719                 nodes[2].node.fail_htlc_backwards(&payment_hash);
4720         }, true, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[0].pubkey, is_permanent: true}));
4721
4722         // final node failure
4723         run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 200, &nodes, &route, &payment_hash, |_msg| {}, |msg| {
4724                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4725                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4726                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[1].shared_secret[..], PERM|NODE|3, &[0;0]);
4727         }, ||{
4728                 nodes[2].node.fail_htlc_backwards(&payment_hash);
4729         }, false, Some(PERM|NODE|3), Some(msgs::HTLCFailChannelUpdate::NodeFailure{node_id: route.hops[1].pubkey, is_permanent: true}));
4730
4731         run_onion_failure_test("invalid_onion_version", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.version = 1; }, ||{}, true,
4732                 Some(BADONION|PERM|4), None);
4733
4734         run_onion_failure_test("invalid_onion_hmac", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.hmac = [3; 32]; }, ||{}, true,
4735                 Some(BADONION|PERM|5), None);
4736
4737         run_onion_failure_test("invalid_onion_key", 0, &nodes, &route, &payment_hash, |msg| { msg.onion_routing_packet.public_key = Err(secp256k1::Error::InvalidPublicKey);}, ||{}, true,
4738                 Some(BADONION|PERM|6), None);
4739
4740         run_onion_failure_test_with_fail_intercept("temporary_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
4741                 msg.amount_msat -= 1;
4742         }, |msg| {
4743                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4744                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4745                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], UPDATE|7, &ChannelUpdate::dummy().encode_with_len()[..]);
4746         }, ||{}, true, Some(UPDATE|7), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
4747
4748         run_onion_failure_test_with_fail_intercept("permanent_channel_failure", 100, &nodes, &route, &payment_hash, |msg| {
4749                 msg.amount_msat -= 1;
4750         }, |msg| {
4751                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4752                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4753                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|8, &[0;0]);
4754                 // short_channel_id from the processing node
4755         }, ||{}, true, Some(PERM|8), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
4756
4757         run_onion_failure_test_with_fail_intercept("required_channel_feature_missing", 100, &nodes, &route, &payment_hash, |msg| {
4758                 msg.amount_msat -= 1;
4759         }, |msg| {
4760                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4761                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4762                 msg.reason = onion_utils::build_first_hop_failure_packet(&onion_keys[0].shared_secret[..], PERM|9, &[0;0]);
4763                 // short_channel_id from the processing node
4764         }, ||{}, true, Some(PERM|9), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
4765
4766         let mut bogus_route = route.clone();
4767         bogus_route.hops[1].short_channel_id -= 1;
4768         run_onion_failure_test("unknown_next_peer", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(PERM|10),
4769           Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: bogus_route.hops[1].short_channel_id, is_permanent:true}));
4770
4771         let amt_to_forward = nodes[1].node.channel_state.lock().unwrap().by_id.get(&channels[1].2).unwrap().get_their_htlc_minimum_msat() - 1;
4772         let mut bogus_route = route.clone();
4773         let route_len = bogus_route.hops.len();
4774         bogus_route.hops[route_len-1].fee_msat = amt_to_forward;
4775         run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, |_| {}, ||{}, true, Some(UPDATE|11), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
4776
4777         //TODO: with new config API, we will be able to generate both valid and
4778         //invalid channel_update cases.
4779         run_onion_failure_test("fee_insufficient", 0, &nodes, &route, &payment_hash, |msg| {
4780                 msg.amount_msat -= 1;
4781         }, || {}, true, Some(UPDATE|12), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
4782
4783         run_onion_failure_test("incorrect_cltv_expiry", 0, &nodes, &route, &payment_hash, |msg| {
4784                 // need to violate: cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
4785                 msg.cltv_expiry -= 1;
4786         }, || {}, true, Some(UPDATE|13), Some(msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id: channels[0].0.contents.short_channel_id, is_permanent: true}));
4787
4788         run_onion_failure_test("expiry_too_soon", 0, &nodes, &route, &payment_hash, |msg| {
4789                 let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
4790                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4791                 nodes[1].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
4792         }, ||{}, true, Some(UPDATE|14), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
4793
4794         run_onion_failure_test("unknown_payment_hash", 2, &nodes, &route, &payment_hash, |_| {}, || {
4795                 nodes[2].node.fail_htlc_backwards(&payment_hash);
4796         }, false, Some(PERM|15), None);
4797
4798         run_onion_failure_test("final_expiry_too_soon", 1, &nodes, &route, &payment_hash, |msg| {
4799                 let height = msg.cltv_expiry - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS + 1;
4800                 let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
4801                 nodes[2].chain_monitor.block_connected_checked(&header, height, &Vec::new()[..], &[0; 0]);
4802         }, || {}, true, Some(17), None);
4803
4804         run_onion_failure_test("final_incorrect_cltv_expiry", 1, &nodes, &route, &payment_hash, |_| {}, || {
4805                 for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
4806                         for f in pending_forwards.iter_mut() {
4807                                 match f {
4808                                         &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
4809                                                 forward_info.outgoing_cltv_value += 1,
4810                                         _ => {},
4811                                 }
4812                         }
4813                 }
4814         }, true, Some(18), None);
4815
4816         run_onion_failure_test("final_incorrect_htlc_amount", 1, &nodes, &route, &payment_hash, |_| {}, || {
4817                 // violate amt_to_forward > msg.amount_msat
4818                 for (_, pending_forwards) in nodes[1].node.channel_state.lock().unwrap().borrow_parts().forward_htlcs.iter_mut() {
4819                         for f in pending_forwards.iter_mut() {
4820                                 match f {
4821                                         &mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
4822                                                 forward_info.amt_to_forward -= 1,
4823                                         _ => {},
4824                                 }
4825                         }
4826                 }
4827         }, true, Some(19), None);
4828
4829         run_onion_failure_test("channel_disabled", 0, &nodes, &route, &payment_hash, |_| {}, || {
4830                 // disconnect event to the channel between nodes[1] ~ nodes[2]
4831                 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id(), false);
4832                 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
4833         }, true, Some(UPDATE|20), Some(msgs::HTLCFailChannelUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy()}));
4834         reconnect_nodes(&nodes[1], &nodes[2], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
4835
4836         run_onion_failure_test("expiry_too_far", 0, &nodes, &route, &payment_hash, |msg| {
4837                 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
4838                 let mut route = route.clone();
4839                 let height = 1;
4840                 route.hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.hops[0].cltv_expiry_delta + 1;
4841                 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
4842                 let (onion_payloads, _, htlc_cltv) = onion_utils::build_onion_payloads(&route, height).unwrap();
4843                 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &payment_hash);
4844                 msg.cltv_expiry = htlc_cltv;
4845                 msg.onion_routing_packet = onion_packet;
4846         }, ||{}, true, Some(21), None);
4847 }
4848
4849 #[test]
4850 #[should_panic]
4851 fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
4852         let nodes = create_network(2, &[None, None]);
4853         //Force duplicate channel ids
4854         for node in nodes.iter() {
4855                 *node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
4856         }
4857
4858         // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
4859         let channel_value_satoshis=10000;
4860         let push_msat=10001;
4861         nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).unwrap();
4862         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
4863         nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), LocalFeatures::new(), &node0_to_1_send_open_channel).unwrap();
4864
4865         //Create a second channel with a channel_id collision
4866         assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
4867 }
4868
4869 #[test]
4870 fn bolt2_open_channel_sending_node_checks_part2() {
4871         let nodes = create_network(2, &[None, None]);
4872
4873         // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
4874         let channel_value_satoshis=2^24;
4875         let push_msat=10001;
4876         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
4877
4878         // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
4879         let channel_value_satoshis=10000;
4880         // Test when push_msat is equal to 1000 * funding_satoshis.
4881         let push_msat=1000*channel_value_satoshis+1;
4882         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
4883
4884         // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
4885         let channel_value_satoshis=10000;
4886         let push_msat=10001;
4887         assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_ok()); //Create a valid channel
4888         let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
4889         assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
4890
4891         // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
4892         // 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
4893         assert!(node0_to_1_send_open_channel.channel_flags<=1);
4894
4895         // 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.
4896         assert!(BREAKDOWN_TIMEOUT>0);
4897         assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
4898
4899         // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
4900         let chain_hash=genesis_block(Network::Testnet).header.bitcoin_hash();
4901         assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
4902
4903         // 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.
4904         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
4905         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
4906         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
4907         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_basepoint.serialize()).is_ok());
4908         assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
4909 }
4910
4911 // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
4912 // 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.
4913 //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.
4914
4915 #[test]
4916 fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
4917         //BOLT2 Requirement: MUST offer amount_msat greater than 0.
4918         //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
4919         let mut nodes = create_network(2, &[None, None]);
4920         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
4921         let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
4922         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
4923
4924         route.hops[0].fee_msat = 0;
4925
4926         let err = nodes[0].node.send_payment(route, our_payment_hash);
4927
4928         if let Err(APIError::ChannelUnavailable{err}) = err {
4929                 assert_eq!(err, "Cannot send less than their minimum HTLC value");
4930         } else {
4931                 assert!(false);
4932         }
4933 }
4934
4935 #[test]
4936 fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
4937         //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
4938         //It is enforced when constructing a route.
4939         let mut nodes = create_network(2, &[None, None]);
4940         let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 0, LocalFeatures::new(), LocalFeatures::new());
4941         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001).unwrap();
4942         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
4943
4944         let err = nodes[0].node.send_payment(route, our_payment_hash);
4945
4946         if let Err(APIError::RouteError{err}) = err {
4947                 assert_eq!(err, "Channel CLTV overflowed?!");
4948         } else {
4949                 assert!(false);
4950         }
4951 }
4952
4953 #[test]
4954 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
4955         //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.
4956         //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
4957         //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
4958         let mut nodes = create_network(2, &[None, None]);
4959         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, LocalFeatures::new(), LocalFeatures::new());
4960         let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_accepted_htlcs as u64;
4961
4962         for i in 0..max_accepted_htlcs {
4963                 let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
4964                 let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
4965                 let payment_event = {
4966                         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
4967                         check_added_monitors!(nodes[0], 1);
4968
4969                         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4970                         assert_eq!(events.len(), 1);
4971                         if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
4972                                 assert_eq!(htlcs[0].htlc_id, i);
4973                         } else {
4974                                 assert!(false);
4975                         }
4976                         SendEvent::from_event(events.remove(0))
4977                 };
4978                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
4979                 check_added_monitors!(nodes[1], 0);
4980                 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4981
4982                 expect_pending_htlcs_forwardable!(nodes[1]);
4983                 expect_payment_received!(nodes[1], our_payment_hash, 100000);
4984         }
4985         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap();
4986         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
4987         let err = nodes[0].node.send_payment(route, our_payment_hash);
4988
4989         if let Err(APIError::ChannelUnavailable{err}) = err {
4990                 assert_eq!(err, "Cannot push more than their max accepted HTLCs");
4991         } else {
4992                 assert!(false);
4993         }
4994 }
4995
4996 #[test]
4997 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
4998         //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.
4999         let mut nodes = create_network(2, &[None, None]);
5000         let channel_value = 100000;
5001         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0, LocalFeatures::new(), LocalFeatures::new());
5002         let max_in_flight = get_channel_value_stat!(nodes[0], chan.2).their_max_htlc_value_in_flight_msat;
5003
5004         send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
5005
5006         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV).unwrap();
5007         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5008         let err = nodes[0].node.send_payment(route, our_payment_hash);
5009
5010         if let Err(APIError::ChannelUnavailable{err}) = err {
5011                 assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight");
5012         } else {
5013                 assert!(false);
5014         }
5015
5016         send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
5017 }
5018
5019 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
5020 #[test]
5021 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
5022         //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
5023         let mut nodes = create_network(2, &[None, None]);
5024         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
5025         let htlc_minimum_msat: u64;
5026         {
5027                 let chan_lock = nodes[0].node.channel_state.lock().unwrap();
5028                 let channel = chan_lock.by_id.get(&chan.2).unwrap();
5029                 htlc_minimum_msat = channel.get_our_htlc_minimum_msat();
5030         }
5031         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV).unwrap();
5032         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5033         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5034         check_added_monitors!(nodes[0], 1);
5035         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5036         updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
5037         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
5038         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5039                 assert_eq!(err, "Remote side tried to send less than our minimum HTLC value");
5040         } else {
5041                 assert!(false);
5042         }
5043         assert!(nodes[1].node.list_channels().is_empty());
5044         check_closed_broadcast!(nodes[1]);
5045 }
5046
5047 #[test]
5048 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
5049         //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
5050         let mut nodes = create_network(2, &[None, None]);
5051         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
5052
5053         let their_channel_reserve = get_channel_value_stat!(nodes[0], chan.2).channel_reserve_msat;
5054
5055         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV).unwrap();
5056         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5057         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5058         check_added_monitors!(nodes[0], 1);
5059         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5060
5061         updates.update_add_htlcs[0].amount_msat = 5000000-their_channel_reserve+1;
5062         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
5063
5064         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5065                 assert_eq!(err, "Remote HTLC add would put them over their reserve value");
5066         } else {
5067                 assert!(false);
5068         }
5069
5070         assert!(nodes[1].node.list_channels().is_empty());
5071         check_closed_broadcast!(nodes[1]);
5072 }
5073
5074 #[test]
5075 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
5076         //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
5077         //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
5078         let mut nodes = create_network(2, &[None, None]);
5079         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
5080         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
5081         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5082
5083         let session_priv = SecretKey::from_slice(&{
5084                 let mut session_key = [0; 32];
5085                 rng::fill_bytes(&mut session_key);
5086                 session_key
5087         }).expect("RNG is bad!");
5088
5089         let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
5090         let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route, &session_priv).unwrap();
5091         let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
5092         let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, &our_payment_hash);
5093
5094         let mut msg = msgs::UpdateAddHTLC {
5095                 channel_id: chan.2,
5096                 htlc_id: 0,
5097                 amount_msat: 1000,
5098                 payment_hash: our_payment_hash,
5099                 cltv_expiry: htlc_cltv,
5100                 onion_routing_packet: onion_packet.clone(),
5101         };
5102
5103         for i in 0..super::channel::OUR_MAX_HTLCS {
5104                 msg.htlc_id = i as u64;
5105                 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg).unwrap();
5106         }
5107         msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
5108         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
5109
5110         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5111                 assert_eq!(err, "Remote tried to push more than our max accepted HTLCs");
5112         } else {
5113                 assert!(false);
5114         }
5115
5116         assert!(nodes[1].node.list_channels().is_empty());
5117         check_closed_broadcast!(nodes[1]);
5118 }
5119
5120 #[test]
5121 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
5122         //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
5123         let mut nodes = create_network(2, &[None, None]);
5124         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
5125         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5126         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5127         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5128         check_added_monitors!(nodes[0], 1);
5129         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5130         updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], chan.2).their_max_htlc_value_in_flight_msat + 1;
5131         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
5132
5133         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5134                 assert_eq!(err,"Remote HTLC add would put them over their max HTLC value in flight");
5135         } else {
5136                 assert!(false);
5137         }
5138
5139         assert!(nodes[1].node.list_channels().is_empty());
5140         check_closed_broadcast!(nodes[1]);
5141 }
5142
5143 #[test]
5144 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
5145         //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
5146         let mut nodes = create_network(2, &[None, None]);
5147         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, LocalFeatures::new(), LocalFeatures::new());
5148         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap();
5149         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5150         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5151         check_added_monitors!(nodes[0], 1);
5152         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5153         updates.update_add_htlcs[0].cltv_expiry = 500000000;
5154         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
5155
5156         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5157                 assert_eq!(err,"Remote provided CLTV expiry in seconds instead of block height");
5158         } else {
5159                 assert!(false);
5160         }
5161
5162         assert!(nodes[1].node.list_channels().is_empty());
5163         check_closed_broadcast!(nodes[1]);
5164 }
5165
5166 #[test]
5167 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
5168         //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
5169         // We test this by first testing that that repeated HTLCs pass commitment signature checks
5170         // after disconnect and that non-sequential htlc_ids result in a channel failure.
5171         let mut nodes = create_network(2, &[None, None]);
5172         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5173         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5174         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5175         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5176         check_added_monitors!(nodes[0], 1);
5177         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5178         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5179
5180         //Disconnect and Reconnect
5181         nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false);
5182         nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
5183         nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id());
5184         let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
5185         assert_eq!(reestablish_1.len(), 1);
5186         nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id());
5187         let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
5188         assert_eq!(reestablish_2.len(), 1);
5189         nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]).unwrap();
5190         handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
5191         nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]).unwrap();
5192         handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
5193
5194         //Resend HTLC
5195         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5196         assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
5197         nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed).unwrap();
5198         check_added_monitors!(nodes[1], 1);
5199         let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5200
5201         let err = nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
5202         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5203                 assert_eq!(err, "Remote skipped HTLC ID");
5204         } else {
5205                 assert!(false);
5206         }
5207
5208         assert!(nodes[1].node.list_channels().is_empty());
5209         check_closed_broadcast!(nodes[1]);
5210 }
5211
5212 #[test]
5213 fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
5214         //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.
5215
5216         let mut nodes = create_network(2, &[None, None]);
5217         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5218
5219         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5220         let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5221         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5222         check_added_monitors!(nodes[0], 1);
5223         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5224         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5225
5226         let update_msg = msgs::UpdateFulfillHTLC{
5227                 channel_id: chan.2,
5228                 htlc_id: 0,
5229                 payment_preimage: our_payment_preimage,
5230         };
5231
5232         let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5233
5234         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5235                 assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5236         } else {
5237                 assert!(false);
5238         }
5239
5240         assert!(nodes[0].node.list_channels().is_empty());
5241         check_closed_broadcast!(nodes[0]);
5242 }
5243
5244 #[test]
5245 fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
5246         //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.
5247
5248         let mut nodes = create_network(2, &[None, None]);
5249         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5250
5251         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5252         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5253         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5254         check_added_monitors!(nodes[0], 1);
5255         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5256         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5257
5258         let update_msg = msgs::UpdateFailHTLC{
5259                 channel_id: chan.2,
5260                 htlc_id: 0,
5261                 reason: msgs::OnionErrorPacket { data: Vec::new()},
5262         };
5263
5264         let err = nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5265
5266         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5267                 assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5268         } else {
5269                 assert!(false);
5270         }
5271
5272         assert!(nodes[0].node.list_channels().is_empty());
5273         check_closed_broadcast!(nodes[0]);
5274 }
5275
5276 #[test]
5277 fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
5278         //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.
5279
5280         let mut nodes = create_network(2, &[None, None]);
5281         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5282
5283         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5284         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5285         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5286         check_added_monitors!(nodes[0], 1);
5287         let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5288         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5289
5290         let update_msg = msgs::UpdateFailMalformedHTLC{
5291                 channel_id: chan.2,
5292                 htlc_id: 0,
5293                 sha256_of_onion: [1; 32],
5294                 failure_code: 0x8000,
5295         };
5296
5297         let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5298
5299         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5300                 assert_eq!(err, "Remote tried to fulfill/fail HTLC before it had been committed");
5301         } else {
5302                 assert!(false);
5303         }
5304
5305         assert!(nodes[0].node.list_channels().is_empty());
5306         check_closed_broadcast!(nodes[0]);
5307 }
5308
5309 #[test]
5310 fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
5311         //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
5312
5313         let nodes = create_network(2, &[None, None]);
5314         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5315
5316         let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
5317
5318         nodes[1].node.claim_funds(our_payment_preimage);
5319         check_added_monitors!(nodes[1], 1);
5320
5321         let events = nodes[1].node.get_and_clear_pending_msg_events();
5322         assert_eq!(events.len(), 1);
5323         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
5324                 match events[0] {
5325                         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, .. } } => {
5326                                 assert!(update_add_htlcs.is_empty());
5327                                 assert_eq!(update_fulfill_htlcs.len(), 1);
5328                                 assert!(update_fail_htlcs.is_empty());
5329                                 assert!(update_fail_malformed_htlcs.is_empty());
5330                                 assert!(update_fee.is_none());
5331                                 update_fulfill_htlcs[0].clone()
5332                         },
5333                         _ => panic!("Unexpected event"),
5334                 }
5335         };
5336
5337         update_fulfill_msg.htlc_id = 1;
5338
5339         let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
5340         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5341                 assert_eq!(err, "Remote tried to fulfill/fail an HTLC we couldn't find");
5342         } else {
5343                 assert!(false);
5344         }
5345
5346         assert!(nodes[0].node.list_channels().is_empty());
5347         check_closed_broadcast!(nodes[0]);
5348 }
5349
5350 #[test]
5351 fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
5352         //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.
5353
5354         let nodes = create_network(2, &[None, None]);
5355         create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5356
5357         let our_payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 100000).0;
5358
5359         nodes[1].node.claim_funds(our_payment_preimage);
5360         check_added_monitors!(nodes[1], 1);
5361
5362         let events = nodes[1].node.get_and_clear_pending_msg_events();
5363         assert_eq!(events.len(), 1);
5364         let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
5365                 match events[0] {
5366                         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, .. } } => {
5367                                 assert!(update_add_htlcs.is_empty());
5368                                 assert_eq!(update_fulfill_htlcs.len(), 1);
5369                                 assert!(update_fail_htlcs.is_empty());
5370                                 assert!(update_fail_malformed_htlcs.is_empty());
5371                                 assert!(update_fee.is_none());
5372                                 update_fulfill_htlcs[0].clone()
5373                         },
5374                         _ => panic!("Unexpected event"),
5375                 }
5376         };
5377
5378         update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
5379
5380         let err = nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
5381         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5382                 assert_eq!(err, "Remote tried to fulfill HTLC with an incorrect preimage");
5383         } else {
5384                 assert!(false);
5385         }
5386
5387         assert!(nodes[0].node.list_channels().is_empty());
5388         check_closed_broadcast!(nodes[0]);
5389 }
5390
5391
5392 #[test]
5393 fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
5394         //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.
5395
5396         let mut nodes = create_network(2, &[None, None]);
5397         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
5398         let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap();
5399         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5400         nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5401         check_added_monitors!(nodes[0], 1);
5402
5403         let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5404         updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
5405
5406         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]).unwrap();
5407         check_added_monitors!(nodes[1], 0);
5408         commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
5409
5410         let events = nodes[1].node.get_and_clear_pending_msg_events();
5411
5412         let mut update_msg: msgs::UpdateFailMalformedHTLC = {
5413                 match events[0] {
5414                         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, .. } } => {
5415                                 assert!(update_add_htlcs.is_empty());
5416                                 assert!(update_fulfill_htlcs.is_empty());
5417                                 assert!(update_fail_htlcs.is_empty());
5418                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
5419                                 assert!(update_fee.is_none());
5420                                 update_fail_malformed_htlcs[0].clone()
5421                         },
5422                         _ => panic!("Unexpected event"),
5423                 }
5424         };
5425         update_msg.failure_code &= !0x8000;
5426         let err = nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
5427         if let Err(msgs::HandleError{err, action: Some(msgs::ErrorAction::SendErrorMessage {..})}) = err {
5428                 assert_eq!(err, "Got update_fail_malformed_htlc with BADONION not set");
5429         } else {
5430                 assert!(false);
5431         }
5432
5433         assert!(nodes[0].node.list_channels().is_empty());
5434         check_closed_broadcast!(nodes[0]);
5435 }
5436
5437 #[test]
5438 fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
5439         //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
5440         //    * 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.
5441
5442         let mut nodes = create_network(3, &[None, None, None]);
5443         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
5444         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, LocalFeatures::new(), LocalFeatures::new());
5445
5446         let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap();
5447         let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
5448
5449         //First hop
5450         let mut payment_event = {
5451                 nodes[0].node.send_payment(route, our_payment_hash).unwrap();
5452                 check_added_monitors!(nodes[0], 1);
5453                 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
5454                 assert_eq!(events.len(), 1);
5455                 SendEvent::from_event(events.remove(0))
5456         };
5457         nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
5458         check_added_monitors!(nodes[1], 0);
5459         commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
5460         expect_pending_htlcs_forwardable!(nodes[1]);
5461         let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
5462         assert_eq!(events_2.len(), 1);
5463         check_added_monitors!(nodes[1], 1);
5464         payment_event = SendEvent::from_event(events_2.remove(0));
5465         assert_eq!(payment_event.msgs.len(), 1);
5466
5467         //Second Hop
5468         payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
5469         nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
5470         check_added_monitors!(nodes[2], 0);
5471         commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
5472
5473         let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
5474         assert_eq!(events_3.len(), 1);
5475         let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
5476                 match events_3[0] {
5477                         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 } } => {
5478                                 assert!(update_add_htlcs.is_empty());
5479                                 assert!(update_fulfill_htlcs.is_empty());
5480                                 assert!(update_fail_htlcs.is_empty());
5481                                 assert_eq!(update_fail_malformed_htlcs.len(), 1);
5482                                 assert!(update_fee.is_none());
5483                                 (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
5484                         },
5485                         _ => panic!("Unexpected event"),
5486                 }
5487         };
5488
5489         nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0).unwrap();
5490
5491         check_added_monitors!(nodes[1], 0);
5492         commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
5493         expect_pending_htlcs_forwardable!(nodes[1]);
5494         let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
5495         assert_eq!(events_4.len(), 1);
5496
5497         //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
5498         match events_4[0] {
5499                 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, .. } } => {
5500                         assert!(update_add_htlcs.is_empty());
5501                         assert!(update_fulfill_htlcs.is_empty());
5502                         assert_eq!(update_fail_htlcs.len(), 1);
5503                         assert!(update_fail_malformed_htlcs.is_empty());
5504                         assert!(update_fee.is_none());
5505                 },
5506                 _ => panic!("Unexpected event"),
5507         };
5508
5509         check_added_monitors!(nodes[1], 1);
5510 }
5511
5512 fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
5513         // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
5514         // 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
5515         // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
5516
5517         let nodes = create_network(2, &[None, None]);
5518         let chan =create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5519
5520         let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5521
5522         // We route 2 dust-HTLCs between A and B
5523         let (_, payment_hash_1) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5524         let (_, payment_hash_2) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5525         route_payment(&nodes[0], &[&nodes[1]], 1000000);
5526
5527         // Cache one local commitment tx as previous
5528         let as_prev_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5529
5530         // Fail one HTLC to prune it in the will-be-latest-local commitment tx
5531         assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
5532         check_added_monitors!(nodes[1], 0);
5533         expect_pending_htlcs_forwardable!(nodes[1]);
5534         check_added_monitors!(nodes[1], 1);
5535
5536         let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5537         nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]).unwrap();
5538         nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed).unwrap();
5539         check_added_monitors!(nodes[0], 1);
5540
5541         // Cache one local commitment tx as lastest
5542         let as_last_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5543
5544         let events = nodes[0].node.get_and_clear_pending_msg_events();
5545         match events[0] {
5546                 MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
5547                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
5548                 },
5549                 _ => panic!("Unexpected event"),
5550         }
5551         match events[1] {
5552                 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
5553                         assert_eq!(node_id, nodes[1].node.get_our_node_id());
5554                 },
5555                 _ => panic!("Unexpected event"),
5556         }
5557
5558         assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
5559         // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
5560         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5561         if announce_latest {
5562                 nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_last_commitment_tx[0]], &[1; 1]);
5563         } else {
5564                 nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_prev_commitment_tx[0]], &[1; 1]);
5565         }
5566
5567         let events = nodes[0].node.get_and_clear_pending_msg_events();
5568         assert_eq!(events.len(), 1);
5569         match events[0] {
5570                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5571                 _ => panic!("Unexpected event"),
5572         }
5573
5574         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5575         connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 1, true,  header.bitcoin_hash());
5576         let events = nodes[0].node.get_and_clear_pending_events();
5577         // Only 2 PaymentFailed events should show up, over-dust HTLC has to be failed by timeout tx
5578         assert_eq!(events.len(), 2);
5579         let mut first_failed = false;
5580         for event in events {
5581                 match event {
5582                         Event::PaymentFailed { payment_hash, .. } => {
5583                                 if payment_hash == payment_hash_1 {
5584                                         assert!(!first_failed);
5585                                         first_failed = true;
5586                                 } else {
5587                                         assert_eq!(payment_hash, payment_hash_2);
5588                                 }
5589                         }
5590                         _ => panic!("Unexpected event"),
5591                 }
5592         }
5593 }
5594
5595 #[test]
5596 fn test_failure_delay_dust_htlc_local_commitment() {
5597         do_test_failure_delay_dust_htlc_local_commitment(true);
5598         do_test_failure_delay_dust_htlc_local_commitment(false);
5599 }
5600
5601 #[test]
5602 fn test_no_failure_dust_htlc_local_commitment() {
5603         // Transaction filters for failing back dust htlc based on local commitment txn infos has been
5604         // prone to error, we test here that a dummy transaction don't fail them.
5605
5606         let nodes = create_network(2, &[None, None]);
5607         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5608
5609         // Rebalance a bit
5610         send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5611
5612         let as_dust_limit = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5613         let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5614
5615         // We route 2 dust-HTLCs between A and B
5616         let (preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5617         let (preimage_2, _) = route_payment(&nodes[1], &[&nodes[0]], as_dust_limit*1000);
5618
5619         // Build a dummy invalid transaction trying to spend a commitment tx
5620         let input = TxIn {
5621                 previous_output: BitcoinOutPoint { txid: chan.3.txid(), vout: 0 },
5622                 script_sig: Script::new(),
5623                 sequence: 0,
5624                 witness: Vec::new(),
5625         };
5626
5627         let outp = TxOut {
5628                 script_pubkey: Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(),
5629                 value: 10000,
5630         };
5631
5632         let dummy_tx = Transaction {
5633                 version: 2,
5634                 lock_time: 0,
5635                 input: vec![input],
5636                 output: vec![outp]
5637         };
5638
5639         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5640         nodes[0].chan_monitor.simple_monitor.block_connected(&header, 1, &[&dummy_tx], &[1;1]);
5641         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5642         assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
5643         // We broadcast a few more block to check everything is all right
5644         connect_blocks(&nodes[0].chain_monitor, 20, 1, true,  header.bitcoin_hash());
5645         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5646         assert_eq!(nodes[0].node.get_and_clear_pending_msg_events().len(), 0);
5647
5648         claim_payment(&nodes[0], &vec!(&nodes[1])[..], preimage_1);
5649         claim_payment(&nodes[1], &vec!(&nodes[0])[..], preimage_2);
5650 }
5651
5652 fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
5653         // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
5654         // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
5655         // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
5656         // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
5657         // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
5658         // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
5659
5660         let nodes = create_network(3, &[None, None, None]);
5661         let chan = create_announced_chan_between_nodes(&nodes, 0, 1, LocalFeatures::new(), LocalFeatures::new());
5662
5663         let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
5664
5665         let (payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
5666         let (payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
5667
5668         let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5669         let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
5670
5671         // We revoked bs_commitment_tx
5672         if revoked {
5673                 let (payment_preimage_3, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
5674                 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
5675         }
5676
5677         let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5678         let mut timeout_tx = Vec::new();
5679         if local {
5680                 // We fail dust-HTLC 1 by broadcast of local commitment tx
5681                 nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&as_commitment_tx[0]], &[1; 1]);
5682                 let events = nodes[0].node.get_and_clear_pending_msg_events();
5683                 assert_eq!(events.len(), 1);
5684                 match events[0] {
5685                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5686                         _ => panic!("Unexpected event"),
5687                 }
5688                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5689                 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
5690                 let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
5691                 let events = nodes[0].node.get_and_clear_pending_events();
5692                 assert_eq!(events.len(), 1);
5693                 match events[0] {
5694                         Event::PaymentFailed { payment_hash, .. } => {
5695                                 assert_eq!(payment_hash, dust_hash);
5696                         },
5697                         _ => panic!("Unexpected event"),
5698                 }
5699                 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5700                 // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
5701                 let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5702                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5703                 nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
5704                 let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5705                 connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
5706                 let events = nodes[0].node.get_and_clear_pending_events();
5707                 assert_eq!(events.len(), 1);
5708                 match events[0] {
5709                         Event::PaymentFailed { payment_hash, .. } => {
5710                                 assert_eq!(payment_hash, non_dust_hash);
5711                         },
5712                         _ => panic!("Unexpected event"),
5713                 }
5714         } else {
5715                 // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
5716                 nodes[0].chain_monitor.block_connected_checked(&header, 1, &[&bs_commitment_tx[0]], &[1; 1]);
5717                 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5718                 let events = nodes[0].node.get_and_clear_pending_msg_events();
5719                 assert_eq!(events.len(), 1);
5720                 match events[0] {
5721                         MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5722                         _ => panic!("Unexpected event"),
5723                 }
5724                 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
5725                 let parent_hash  = connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 2, true, header.bitcoin_hash());
5726                 let header_2 = BlockHeader { version: 0x20000000, prev_blockhash: parent_hash, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5727                 if !revoked {
5728                         let events = nodes[0].node.get_and_clear_pending_events();
5729                         assert_eq!(events.len(), 1);
5730                         match events[0] {
5731                                 Event::PaymentFailed { payment_hash, .. } => {
5732                                         assert_eq!(payment_hash, dust_hash);
5733                                 },
5734                                 _ => panic!("Unexpected event"),
5735                         }
5736                         assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5737                         // We fail non-dust-HTLC 2 by broadcast of local timeout tx on remote commitment tx
5738                         nodes[0].chain_monitor.block_connected_checked(&header_2, 7, &[&timeout_tx[0]], &[1; 1]);
5739                         assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
5740                         let header_3 = BlockHeader { version: 0x20000000, prev_blockhash: header_2.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
5741                         connect_blocks(&nodes[0].chain_monitor, ANTI_REORG_DELAY - 1, 8, true, header_3.bitcoin_hash());
5742                         let events = nodes[0].node.get_and_clear_pending_events();
5743                         assert_eq!(events.len(), 1);
5744                         match events[0] {
5745                                 Event::PaymentFailed { payment_hash, .. } => {
5746                                         assert_eq!(payment_hash, non_dust_hash);
5747                                 },
5748                                 _ => panic!("Unexpected event"),
5749                         }
5750                 } else {
5751                         // If revoked, both dust & non-dust HTLCs should have been failed after ANTI_REORG_DELAY confs of revoked
5752                         // commitment tx
5753                         let events = nodes[0].node.get_and_clear_pending_events();
5754                         assert_eq!(events.len(), 2);
5755                         let first;
5756                         match events[0] {
5757                                 Event::PaymentFailed { payment_hash, .. } => {
5758                                         if payment_hash == dust_hash { first = true; }
5759                                         else { first = false; }
5760                                 },
5761                                 _ => panic!("Unexpected event"),
5762                         }
5763                         match events[1] {
5764                                 Event::PaymentFailed { payment_hash, .. } => {
5765                                         if first { assert_eq!(payment_hash, non_dust_hash); }
5766                                         else { assert_eq!(payment_hash, dust_hash); }
5767                                 },
5768                                 _ => panic!("Unexpected event"),
5769                         }
5770                 }
5771         }
5772 }
5773
5774 #[test]
5775 fn test_sweep_outbound_htlc_failure_update() {
5776         do_test_sweep_outbound_htlc_failure_update(false, true);
5777         do_test_sweep_outbound_htlc_failure_update(false, false);
5778         do_test_sweep_outbound_htlc_failure_update(true, false);
5779 }
5780
5781 #[test]
5782 fn test_upfront_shutdown_script() {
5783         // BOLT 2 : Option upfront shutdown script, if peer commit its closing_script at channel opening
5784         // enforce it at shutdown message
5785
5786         let mut config = UserConfig::new();
5787         config.channel_options.announced_channel = true;
5788         config.peer_channel_config_limits.force_announced_channel_preference = false;
5789         config.channel_options.commit_upfront_shutdown_pubkey = false;
5790         let nodes = create_network(3, &[None, Some(config), None]);
5791
5792         // We test that in case of peer committing upfront to a script, if it changes at closing, we refuse to sign
5793         let flags = LocalFeatures::new();
5794         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
5795         nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
5796         let mut node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
5797         node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
5798         // Test we enforce upfront_scriptpbukey if by providing a diffrent one at closing that  we disconnect peer
5799         if let Err(error) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {
5800                 if let Some(error) = error.action {
5801                         match error {
5802                                 ErrorAction::SendErrorMessage { msg } => {
5803                                         assert_eq!(msg.data,"Got shutdown request with a scriptpubkey which did not match their previous scriptpubkey");
5804                                 },
5805                                 _ => { assert!(false); }
5806                         }
5807                 } else { assert!(false); }
5808         } else { assert!(false); }
5809         let events = nodes[2].node.get_and_clear_pending_msg_events();
5810         assert_eq!(events.len(), 1);
5811         match events[0] {
5812                 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5813                 _ => panic!("Unexpected event"),
5814         }
5815
5816         // We test that in case of peer committing upfront to a script, if it doesn't change at closing, we sign
5817         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 1000000, 1000000, flags.clone(), flags.clone());
5818         nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
5819         let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[2].node.get_our_node_id());
5820         // We test that in case of peer committing upfront to a script, if it oesn't change at closing, we sign
5821         if let Ok(_) = nodes[2].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_0_shutdown) {}
5822         else { assert!(false) }
5823         let events = nodes[2].node.get_and_clear_pending_msg_events();
5824         assert_eq!(events.len(), 1);
5825         match events[0] {
5826                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
5827                 _ => panic!("Unexpected event"),
5828         }
5829
5830         // We test that if case of peer non-signaling we don't enforce committed script at channel opening
5831         let mut flags_no = LocalFeatures::new();
5832         flags_no.unset_upfront_shutdown_script();
5833         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags_no, flags.clone());
5834         nodes[0].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
5835         let mut node_1_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
5836         node_1_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
5837         if let Ok(_) = nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &node_1_shutdown) {}
5838         else { assert!(false) }
5839         let events = nodes[1].node.get_and_clear_pending_msg_events();
5840         assert_eq!(events.len(), 1);
5841         match events[0] {
5842                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[0].node.get_our_node_id()) }
5843                 _ => panic!("Unexpected event"),
5844         }
5845
5846         // We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
5847         // channel smoothly, opt-out is from channel initiator here
5848         let chan = create_announced_chan_between_nodes_with_value(&nodes, 1, 0, 1000000, 1000000, flags.clone(), flags.clone());
5849         nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
5850         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
5851         node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
5852         if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
5853         else { assert!(false) }
5854         let events = nodes[0].node.get_and_clear_pending_msg_events();
5855         assert_eq!(events.len(), 1);
5856         match events[0] {
5857                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
5858                 _ => panic!("Unexpected event"),
5859         }
5860
5861         //// We test that if user opt-out, we provide a zero-length script at channel opening and we are able to close
5862         //// channel smoothly
5863         let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, flags.clone(), flags.clone());
5864         nodes[1].node.close_channel(&OutPoint::new(chan.3.txid(), 0).to_channel_id()).unwrap();
5865         let mut node_0_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
5866         node_0_shutdown.scriptpubkey = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script().to_p2sh();
5867         if let Ok(_) = nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &node_0_shutdown) {}
5868         else { assert!(false) }
5869         let events = nodes[0].node.get_and_clear_pending_msg_events();
5870         assert_eq!(events.len(), 2);
5871         match events[0] {
5872                 MessageSendEvent::SendShutdown { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
5873                 _ => panic!("Unexpected event"),
5874         }
5875         match events[1] {
5876                 MessageSendEvent::SendClosingSigned { node_id, .. } => { assert_eq!(node_id, nodes[1].node.get_our_node_id()) }
5877                 _ => panic!("Unexpected event"),
5878         }
5879 }