1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Tests that test standing up a network of ChannelManagers, creating channels, sending
11 //! payments/messages between them, and often checking the resulting ChannelMonitors are able to
12 //! claim outputs on-chain.
15 use crate::chain::{ChannelMonitorUpdateStatus, Confirm, Listen, Watch};
16 use crate::chain::chaininterface::LowerBoundedFeeEstimator;
17 use crate::chain::channelmonitor;
18 use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
19 use crate::chain::transaction::OutPoint;
20 use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource, SignerProvider};
21 use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
22 use crate::ln::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash};
23 use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY, ChannelPhase};
24 use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
25 use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError};
26 use crate::ln::{chan_utils, onion_utils};
27 use crate::ln::chan_utils::{OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
28 use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
29 use crate::routing::router::{Path, PaymentParameters, Route, RouteHop, get_route, RouteParameters};
30 use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, NodeFeatures};
32 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
33 use crate::util::test_channel_signer::TestChannelSigner;
34 use crate::util::test_utils::{self, WatchtowerPersister};
35 use crate::util::errors::APIError;
36 use crate::util::ser::{Writeable, ReadableArgs};
37 use crate::util::string::UntrustedString;
38 use crate::util::config::{UserConfig, MaxDustHTLCExposure};
40 use bitcoin::hash_types::BlockHash;
41 use bitcoin::blockdata::script::{Builder, Script};
42 use bitcoin::blockdata::opcodes;
43 use bitcoin::blockdata::constants::genesis_block;
44 use bitcoin::network::constants::Network;
45 use bitcoin::{PackedLockTime, Sequence, Transaction, TxIn, TxOut, Witness};
46 use bitcoin::OutPoint as BitcoinOutPoint;
48 use bitcoin::secp256k1::Secp256k1;
49 use bitcoin::secp256k1::{PublicKey,SecretKey};
54 use crate::prelude::*;
55 use alloc::collections::BTreeSet;
56 use core::default::Default;
57 use core::iter::repeat;
58 use bitcoin::hashes::Hash;
59 use crate::sync::{Arc, Mutex, RwLock};
61 use crate::ln::functional_test_utils::*;
62 use crate::ln::chan_utils::CommitmentTransaction;
64 use super::channel::UNFUNDED_CHANNEL_AGE_LIMIT_TICKS;
67 fn test_insane_channel_opens() {
68 // Stand up a network of 2 nodes
69 use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
70 let mut cfg = UserConfig::default();
71 cfg.channel_handshake_limits.max_funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1;
72 let chanmon_cfgs = create_chanmon_cfgs(2);
73 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
74 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(cfg)]);
75 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
77 // Instantiate channel parameters where we push the maximum msats given our
79 let channel_value_sat = 31337; // same as funding satoshis
80 let channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
81 let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
83 // Have node0 initiate a channel to node1 with aforementioned parameters
84 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42, None).unwrap();
86 // Extract the channel open message from node0 to node1
87 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
89 // Test helper that asserts we get the correct error string given a mutator
90 // that supposedly makes the channel open message insane
91 let insane_open_helper = |expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
92 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &message_mutator(open_channel_message.clone()));
93 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
94 assert_eq!(msg_events.len(), 1);
95 let expected_regex = regex::Regex::new(expected_error_str).unwrap();
96 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
98 &ErrorAction::SendErrorMessage { .. } => {
99 nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", expected_regex, 1);
101 _ => panic!("unexpected event!"),
103 } else { assert!(false); }
106 use crate::ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
108 // Test all mutations that would make the channel open message insane
109 insane_open_helper(format!("Per our config, funding must be at most {}. It was {}", TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1, TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2).as_str(), |mut msg| { msg.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 2; msg });
110 insane_open_helper(format!("Funding must be smaller than the total bitcoin supply. It was {}", TOTAL_BITCOIN_SUPPLY_SATOSHIS).as_str(), |mut msg| { msg.funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS; msg });
112 insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
114 insane_open_helper(r"push_msat \d+ was larger than channel amount minus reserve \(\d+\)", |mut msg| { msg.push_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 + 1; msg });
116 insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
118 insane_open_helper(r"Minimum htlc value \(\d+\) was larger than full channel value \(\d+\)", |mut msg| { msg.htlc_minimum_msat = (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000; msg });
120 insane_open_helper("They wanted our payments to be delayed by a needlessly long period", |mut msg| { msg.to_self_delay = MAX_LOCAL_BREAKDOWN_TIMEOUT + 1; msg });
122 insane_open_helper("0 max_accepted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
124 insane_open_helper("max_accepted_htlcs was 484. It must not be larger than 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
128 fn test_funding_exceeds_no_wumbo_limit() {
129 // Test that if a peer does not support wumbo channels, we'll refuse to open a wumbo channel to
131 use crate::ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
132 let chanmon_cfgs = create_chanmon_cfgs(2);
133 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
134 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_wumbo());
135 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
136 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
138 match nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), MAX_FUNDING_SATOSHIS_NO_WUMBO + 1, 0, 42, None) {
139 Err(APIError::APIMisuseError { err }) => {
140 assert_eq!(format!("funding_value must not exceed {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, MAX_FUNDING_SATOSHIS_NO_WUMBO + 1), err);
146 fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
147 // A peer providing a channel_reserve_satoshis of 0 (or less than our dust limit) is insecure,
148 // but only for them. Because some LSPs do it with some level of trust of the clients (for a
149 // substantial UX improvement), we explicitly allow it. Because it's unlikely to happen often
150 // in normal testing, we test it explicitly here.
151 let chanmon_cfgs = create_chanmon_cfgs(2);
152 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
153 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
154 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
155 let default_config = UserConfig::default();
157 // Have node0 initiate a channel to node1 with aforementioned parameters
158 let mut push_amt = 100_000_000;
159 let feerate_per_kw = 253;
160 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
161 push_amt -= feerate_per_kw as u64 * (commitment_tx_base_weight(&channel_type_features) + 4 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
162 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
164 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, if send_from_initiator { 0 } else { push_amt }, 42, None).unwrap();
165 let mut open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
166 if !send_from_initiator {
167 open_channel_message.channel_reserve_satoshis = 0;
168 open_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
170 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
172 // Extract the channel accept message from node1 to node0
173 let mut accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
174 if send_from_initiator {
175 accept_channel_message.channel_reserve_satoshis = 0;
176 accept_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
178 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
180 let sender_node = if send_from_initiator { &nodes[1] } else { &nodes[0] };
181 let counterparty_node = if send_from_initiator { &nodes[0] } else { &nodes[1] };
182 let mut sender_node_per_peer_lock;
183 let mut sender_node_peer_state_lock;
185 let channel_phase = get_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
186 match channel_phase {
187 ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
188 let chan_context = channel_phase.context_mut();
189 chan_context.holder_selected_channel_reserve_satoshis = 0;
190 chan_context.holder_max_htlc_value_in_flight_msat = 100_000_000;
192 ChannelPhase::Funded(_) => assert!(false),
196 let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id);
197 let funding_msgs = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &funding_tx);
198 create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_msgs.0);
200 // nodes[0] should now be able to send the full balance to nodes[1], violating nodes[1]'s
201 // security model if it ever tries to send funds back to nodes[0] (but that's not our problem).
202 if send_from_initiator {
203 send_payment(&nodes[0], &[&nodes[1]], 100_000_000
204 // Note that for outbound channels we have to consider the commitment tx fee and the
205 // "fee spike buffer", which is currently a multiple of the total commitment tx fee as
206 // well as an additional HTLC.
207 - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * commit_tx_fee_msat(feerate_per_kw, 2, &channel_type_features));
209 send_payment(&nodes[1], &[&nodes[0]], push_amt);
214 fn test_counterparty_no_reserve() {
215 do_test_counterparty_no_reserve(true);
216 do_test_counterparty_no_reserve(false);
220 fn test_async_inbound_update_fee() {
221 let chanmon_cfgs = create_chanmon_cfgs(2);
222 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
223 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
224 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
225 create_announced_chan_between_nodes(&nodes, 0, 1);
228 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
232 // send (1) commitment_signed -.
233 // <- update_add_htlc/commitment_signed
234 // send (2) RAA (awaiting remote revoke) -.
235 // (1) commitment_signed is delivered ->
236 // .- send (3) RAA (awaiting remote revoke)
237 // (2) RAA is delivered ->
238 // .- send (4) commitment_signed
239 // <- (3) RAA is delivered
240 // send (5) commitment_signed -.
241 // <- (4) commitment_signed is delivered
243 // (5) commitment_signed is delivered ->
245 // (6) RAA is delivered ->
247 // First nodes[0] generates an update_fee
249 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
252 nodes[0].node.timer_tick_occurred();
253 check_added_monitors!(nodes[0], 1);
255 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
256 assert_eq!(events_0.len(), 1);
257 let (update_msg, commitment_signed) = match events_0[0] { // (1)
258 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
259 (update_fee.as_ref(), commitment_signed)
261 _ => panic!("Unexpected event"),
264 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
266 // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
267 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
268 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
269 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
270 check_added_monitors!(nodes[1], 1);
272 let payment_event = {
273 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
274 assert_eq!(events_1.len(), 1);
275 SendEvent::from_event(events_1.remove(0))
277 assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
278 assert_eq!(payment_event.msgs.len(), 1);
280 // ...now when the messages get delivered everyone should be happy
281 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
282 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
283 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
284 // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
285 check_added_monitors!(nodes[0], 1);
287 // deliver(1), generate (3):
288 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
289 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
290 // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
291 check_added_monitors!(nodes[1], 1);
293 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack); // deliver (2)
294 let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
295 assert!(bs_update.update_add_htlcs.is_empty()); // (4)
296 assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
297 assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
298 assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
299 assert!(bs_update.update_fee.is_none()); // (4)
300 check_added_monitors!(nodes[1], 1);
302 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack); // deliver (3)
303 let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
304 assert!(as_update.update_add_htlcs.is_empty()); // (5)
305 assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
306 assert!(as_update.update_fail_htlcs.is_empty()); // (5)
307 assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
308 assert!(as_update.update_fee.is_none()); // (5)
309 check_added_monitors!(nodes[0], 1);
311 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed); // deliver (4)
312 let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
313 // only (6) so get_event_msg's assert(len == 1) passes
314 check_added_monitors!(nodes[0], 1);
316 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed); // deliver (5)
317 let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
318 check_added_monitors!(nodes[1], 1);
320 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
321 check_added_monitors!(nodes[0], 1);
323 let events_2 = nodes[0].node.get_and_clear_pending_events();
324 assert_eq!(events_2.len(), 1);
326 Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
327 _ => panic!("Unexpected event"),
330 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke); // deliver (6)
331 check_added_monitors!(nodes[1], 1);
335 fn test_update_fee_unordered_raa() {
336 // Just the intro to the previous test followed by an out-of-order RAA (which caused a
337 // crash in an earlier version of the update_fee patch)
338 let chanmon_cfgs = create_chanmon_cfgs(2);
339 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
340 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
341 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
342 create_announced_chan_between_nodes(&nodes, 0, 1);
345 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
347 // First nodes[0] generates an update_fee
349 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
352 nodes[0].node.timer_tick_occurred();
353 check_added_monitors!(nodes[0], 1);
355 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
356 assert_eq!(events_0.len(), 1);
357 let update_msg = match events_0[0] { // (1)
358 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
361 _ => panic!("Unexpected event"),
364 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
366 // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
367 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
368 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
369 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
370 check_added_monitors!(nodes[1], 1);
372 let payment_event = {
373 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
374 assert_eq!(events_1.len(), 1);
375 SendEvent::from_event(events_1.remove(0))
377 assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
378 assert_eq!(payment_event.msgs.len(), 1);
380 // ...now when the messages get delivered everyone should be happy
381 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
382 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
383 let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
384 // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
385 check_added_monitors!(nodes[0], 1);
387 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg); // deliver (2)
388 check_added_monitors!(nodes[1], 1);
390 // We can't continue, sadly, because our (1) now has a bogus signature
394 fn test_multi_flight_update_fee() {
395 let chanmon_cfgs = create_chanmon_cfgs(2);
396 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
397 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
398 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
399 create_announced_chan_between_nodes(&nodes, 0, 1);
402 // update_fee/commitment_signed ->
403 // .- send (1) RAA and (2) commitment_signed
404 // update_fee (never committed) ->
406 // We have to manually generate the above update_fee, it is allowed by the protocol but we
407 // don't track which updates correspond to which revoke_and_ack responses so we're in
408 // AwaitingRAA mode and will not generate the update_fee yet.
409 // <- (1) RAA delivered
410 // (3) is generated and send (4) CS -.
411 // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
412 // know the per_commitment_point to use for it.
413 // <- (2) commitment_signed delivered
415 // B should send no response here
416 // (4) commitment_signed delivered ->
417 // <- RAA/commitment_signed delivered
420 // First nodes[0] generates an update_fee
423 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
424 initial_feerate = *feerate_lock;
425 *feerate_lock = initial_feerate + 20;
427 nodes[0].node.timer_tick_occurred();
428 check_added_monitors!(nodes[0], 1);
430 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
431 assert_eq!(events_0.len(), 1);
432 let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
433 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
434 (update_fee.as_ref().unwrap(), commitment_signed)
436 _ => panic!("Unexpected event"),
439 // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
440 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1);
441 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1);
442 let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
443 check_added_monitors!(nodes[1], 1);
445 // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
448 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
449 *feerate_lock = initial_feerate + 40;
451 nodes[0].node.timer_tick_occurred();
452 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
453 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
455 // Create the (3) update_fee message that nodes[0] will generate before it does...
456 let mut update_msg_2 = msgs::UpdateFee {
457 channel_id: update_msg_1.channel_id.clone(),
458 feerate_per_kw: (initial_feerate + 30) as u32,
461 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
463 update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
465 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
467 // Deliver (1), generating (3) and (4)
468 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg);
469 let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
470 check_added_monitors!(nodes[0], 1);
471 assert!(as_second_update.update_add_htlcs.is_empty());
472 assert!(as_second_update.update_fulfill_htlcs.is_empty());
473 assert!(as_second_update.update_fail_htlcs.is_empty());
474 assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
475 // Check that the update_fee newly generated matches what we delivered:
476 assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
477 assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
479 // Deliver (2) commitment_signed
480 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
481 let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
482 check_added_monitors!(nodes[0], 1);
483 // No commitment_signed so get_event_msg's assert(len == 1) passes
485 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg);
486 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
487 check_added_monitors!(nodes[1], 1);
490 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed);
491 let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
492 check_added_monitors!(nodes[1], 1);
494 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
495 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
496 check_added_monitors!(nodes[0], 1);
498 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment);
499 let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
500 // No commitment_signed so get_event_msg's assert(len == 1) passes
501 check_added_monitors!(nodes[0], 1);
503 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke);
504 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
505 check_added_monitors!(nodes[1], 1);
508 fn do_test_sanity_on_in_flight_opens(steps: u8) {
509 // Previously, we had issues deserializing channels when we hadn't connected the first block
510 // after creation. To catch that and similar issues, we lean on the Node::drop impl to test
511 // serialization round-trips and simply do steps towards opening a channel and then drop the
514 let chanmon_cfgs = create_chanmon_cfgs(2);
515 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
516 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
517 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
519 if steps & 0b1000_0000 != 0{
520 let block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
521 connect_block(&nodes[0], &block);
522 connect_block(&nodes[1], &block);
525 if steps & 0x0f == 0 { return; }
526 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
527 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
529 if steps & 0x0f == 1 { return; }
530 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
531 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
533 if steps & 0x0f == 2 { return; }
534 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
536 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
538 if steps & 0x0f == 3 { return; }
539 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
540 check_added_monitors!(nodes[0], 0);
541 let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
543 if steps & 0x0f == 4 { return; }
544 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
546 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
547 assert_eq!(added_monitors.len(), 1);
548 assert_eq!(added_monitors[0].0, funding_output);
549 added_monitors.clear();
551 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
553 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
555 if steps & 0x0f == 5 { return; }
556 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
558 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
559 assert_eq!(added_monitors.len(), 1);
560 assert_eq!(added_monitors[0].0, funding_output);
561 added_monitors.clear();
564 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
565 let events_4 = nodes[0].node.get_and_clear_pending_events();
566 assert_eq!(events_4.len(), 0);
568 if steps & 0x0f == 6 { return; }
569 create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);
571 if steps & 0x0f == 7 { return; }
572 confirm_transaction_at(&nodes[0], &tx, 2);
573 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
574 create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
575 expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
579 fn test_sanity_on_in_flight_opens() {
580 do_test_sanity_on_in_flight_opens(0);
581 do_test_sanity_on_in_flight_opens(0 | 0b1000_0000);
582 do_test_sanity_on_in_flight_opens(1);
583 do_test_sanity_on_in_flight_opens(1 | 0b1000_0000);
584 do_test_sanity_on_in_flight_opens(2);
585 do_test_sanity_on_in_flight_opens(2 | 0b1000_0000);
586 do_test_sanity_on_in_flight_opens(3);
587 do_test_sanity_on_in_flight_opens(3 | 0b1000_0000);
588 do_test_sanity_on_in_flight_opens(4);
589 do_test_sanity_on_in_flight_opens(4 | 0b1000_0000);
590 do_test_sanity_on_in_flight_opens(5);
591 do_test_sanity_on_in_flight_opens(5 | 0b1000_0000);
592 do_test_sanity_on_in_flight_opens(6);
593 do_test_sanity_on_in_flight_opens(6 | 0b1000_0000);
594 do_test_sanity_on_in_flight_opens(7);
595 do_test_sanity_on_in_flight_opens(7 | 0b1000_0000);
596 do_test_sanity_on_in_flight_opens(8);
597 do_test_sanity_on_in_flight_opens(8 | 0b1000_0000);
601 fn test_update_fee_vanilla() {
602 let chanmon_cfgs = create_chanmon_cfgs(2);
603 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
604 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
605 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
606 create_announced_chan_between_nodes(&nodes, 0, 1);
609 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
612 nodes[0].node.timer_tick_occurred();
613 check_added_monitors!(nodes[0], 1);
615 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
616 assert_eq!(events_0.len(), 1);
617 let (update_msg, commitment_signed) = match events_0[0] {
618 MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
619 (update_fee.as_ref(), commitment_signed)
621 _ => panic!("Unexpected event"),
623 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
625 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
626 let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
627 check_added_monitors!(nodes[1], 1);
629 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
630 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
631 check_added_monitors!(nodes[0], 1);
633 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
634 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
635 // No commitment_signed so get_event_msg's assert(len == 1) passes
636 check_added_monitors!(nodes[0], 1);
638 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
639 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
640 check_added_monitors!(nodes[1], 1);
644 fn test_update_fee_that_funder_cannot_afford() {
645 let chanmon_cfgs = create_chanmon_cfgs(2);
646 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
647 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
648 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
649 let channel_value = 5000;
651 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, push_sats * 1000);
652 let channel_id = chan.2;
653 let secp_ctx = Secp256k1::new();
654 let default_config = UserConfig::default();
655 let bs_channel_reserve_sats = get_holder_selected_channel_reserve_satoshis(channel_value, &default_config);
657 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
659 // Calculate the maximum feerate that A can afford. Note that we don't send an update_fee
660 // CONCURRENT_INBOUND_HTLC_FEE_BUFFER HTLCs before actually running out of local balance, so we
661 // calculate two different feerates here - the expected local limit as well as the expected
663 let feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / (commitment_tx_base_weight(&channel_type_features) + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as u64 * COMMITMENT_TX_WEIGHT_PER_HTLC)) as u32;
664 let non_buffer_feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / commitment_tx_base_weight(&channel_type_features)) as u32;
666 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
667 *feerate_lock = feerate;
669 nodes[0].node.timer_tick_occurred();
670 check_added_monitors!(nodes[0], 1);
671 let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
673 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap());
675 commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
677 // Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate set above.
679 let commitment_tx = get_local_commitment_txn!(nodes[1], channel_id)[0].clone();
681 //We made sure neither party's funds are below the dust limit and there are no HTLCs here
682 assert_eq!(commitment_tx.output.len(), 2);
683 let total_fee: u64 = commit_tx_fee_msat(feerate, 0, &channel_type_features) / 1000;
684 let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
685 actual_fee = channel_value - actual_fee;
686 assert_eq!(total_fee, actual_fee);
690 // Increment the feerate by a small constant, accounting for rounding errors
691 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
694 nodes[0].node.timer_tick_occurred();
695 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot afford to send new feerate at {}", feerate + 4), 1);
696 check_added_monitors!(nodes[0], 0);
698 const INITIAL_COMMITMENT_NUMBER: u64 = 281474976710654;
700 // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
701 // needed to sign the new commitment tx and (2) sign the new commitment tx.
702 let (local_revocation_basepoint, local_htlc_basepoint, local_funding) = {
703 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
704 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
705 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
706 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
707 ).flatten().unwrap();
708 let chan_signer = local_chan.get_signer();
709 let pubkeys = chan_signer.as_ref().pubkeys();
710 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
711 pubkeys.funding_pubkey)
713 let (remote_delayed_payment_basepoint, remote_htlc_basepoint,remote_point, remote_funding) = {
714 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
715 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
716 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
717 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
718 ).flatten().unwrap();
719 let chan_signer = remote_chan.get_signer();
720 let pubkeys = chan_signer.as_ref().pubkeys();
721 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
722 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
723 pubkeys.funding_pubkey)
726 // Assemble the set of keys we can use for signatures for our commitment_signed message.
727 let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
728 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
731 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
732 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
733 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
734 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
735 ).flatten().unwrap();
736 let local_chan_signer = local_chan.get_signer();
737 let mut htlcs: Vec<(HTLCOutputInCommitment, ())> = vec![];
738 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
739 INITIAL_COMMITMENT_NUMBER - 1,
741 channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
742 local_funding, remote_funding,
743 commit_tx_keys.clone(),
744 non_buffer_feerate + 4,
746 &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
748 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
751 let commit_signed_msg = msgs::CommitmentSigned {
754 htlc_signatures: res.1,
756 partial_signature_with_nonce: None,
759 let update_fee = msgs::UpdateFee {
761 feerate_per_kw: non_buffer_feerate + 4,
764 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_fee);
766 //While producing the commitment_signed response after handling a received update_fee request the
767 //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
768 //Should produce and error.
769 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
770 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Funding remote cannot afford proposed new fee".to_string(), 1);
771 check_added_monitors!(nodes[1], 1);
772 check_closed_broadcast!(nodes[1], true);
773 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: String::from("Funding remote cannot afford proposed new fee") },
774 [nodes[0].node.get_our_node_id()], channel_value);
778 fn test_update_fee_with_fundee_update_add_htlc() {
779 let chanmon_cfgs = create_chanmon_cfgs(2);
780 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
781 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
782 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
783 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
786 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
789 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
792 nodes[0].node.timer_tick_occurred();
793 check_added_monitors!(nodes[0], 1);
795 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
796 assert_eq!(events_0.len(), 1);
797 let (update_msg, commitment_signed) = match events_0[0] {
798 MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
799 (update_fee.as_ref(), commitment_signed)
801 _ => panic!("Unexpected event"),
803 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
804 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
805 let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
806 check_added_monitors!(nodes[1], 1);
808 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 800000);
810 // nothing happens since node[1] is in AwaitingRemoteRevoke
811 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
812 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
814 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
815 assert_eq!(added_monitors.len(), 0);
816 added_monitors.clear();
818 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
819 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
820 // node[1] has nothing to do
822 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
823 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
824 check_added_monitors!(nodes[0], 1);
826 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
827 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
828 // No commitment_signed so get_event_msg's assert(len == 1) passes
829 check_added_monitors!(nodes[0], 1);
830 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
831 check_added_monitors!(nodes[1], 1);
832 // AwaitingRemoteRevoke ends here
834 let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
835 assert_eq!(commitment_update.update_add_htlcs.len(), 1);
836 assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
837 assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
838 assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
839 assert_eq!(commitment_update.update_fee.is_none(), true);
841 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]);
842 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
843 check_added_monitors!(nodes[0], 1);
844 let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
846 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke);
847 check_added_monitors!(nodes[1], 1);
848 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
850 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
851 check_added_monitors!(nodes[1], 1);
852 let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
853 // No commitment_signed so get_event_msg's assert(len == 1) passes
855 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke);
856 check_added_monitors!(nodes[0], 1);
857 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
859 expect_pending_htlcs_forwardable!(nodes[0]);
861 let events = nodes[0].node.get_and_clear_pending_events();
862 assert_eq!(events.len(), 1);
864 Event::PaymentClaimable { .. } => { },
865 _ => panic!("Unexpected event"),
868 claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
870 send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
871 send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
872 close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
873 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
874 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
878 fn test_update_fee() {
879 let chanmon_cfgs = create_chanmon_cfgs(2);
880 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
881 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
882 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
883 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
884 let channel_id = chan.2;
887 // (1) update_fee/commitment_signed ->
888 // <- (2) revoke_and_ack
889 // .- send (3) commitment_signed
890 // (4) update_fee/commitment_signed ->
891 // .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
892 // <- (3) commitment_signed delivered
893 // send (6) revoke_and_ack -.
894 // <- (5) deliver revoke_and_ack
895 // (6) deliver revoke_and_ack ->
896 // .- send (7) commitment_signed in response to (4)
897 // <- (7) deliver commitment_signed
900 // Create and deliver (1)...
903 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
904 feerate = *feerate_lock;
905 *feerate_lock = feerate + 20;
907 nodes[0].node.timer_tick_occurred();
908 check_added_monitors!(nodes[0], 1);
910 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
911 assert_eq!(events_0.len(), 1);
912 let (update_msg, commitment_signed) = match events_0[0] {
913 MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
914 (update_fee.as_ref(), commitment_signed)
916 _ => panic!("Unexpected event"),
918 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
920 // Generate (2) and (3):
921 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
922 let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
923 check_added_monitors!(nodes[1], 1);
926 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
927 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
928 check_added_monitors!(nodes[0], 1);
930 // Create and deliver (4)...
932 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
933 *feerate_lock = feerate + 30;
935 nodes[0].node.timer_tick_occurred();
936 check_added_monitors!(nodes[0], 1);
937 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
938 assert_eq!(events_0.len(), 1);
939 let (update_msg, commitment_signed) = match events_0[0] {
940 MessageSendEvent::UpdateHTLCs { node_id:_, updates: msgs::CommitmentUpdate { update_add_htlcs:_, update_fulfill_htlcs:_, update_fail_htlcs:_, update_fail_malformed_htlcs:_, ref update_fee, ref commitment_signed } } => {
941 (update_fee.as_ref(), commitment_signed)
943 _ => panic!("Unexpected event"),
946 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
947 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
948 check_added_monitors!(nodes[1], 1);
950 let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
951 // No commitment_signed so get_event_msg's assert(len == 1) passes
953 // Handle (3), creating (6):
954 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0);
955 check_added_monitors!(nodes[0], 1);
956 let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
957 // No commitment_signed so get_event_msg's assert(len == 1) passes
960 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
961 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
962 check_added_monitors!(nodes[0], 1);
964 // Deliver (6), creating (7):
965 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0);
966 let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
967 assert!(commitment_update.update_add_htlcs.is_empty());
968 assert!(commitment_update.update_fulfill_htlcs.is_empty());
969 assert!(commitment_update.update_fail_htlcs.is_empty());
970 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
971 assert!(commitment_update.update_fee.is_none());
972 check_added_monitors!(nodes[1], 1);
975 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
976 check_added_monitors!(nodes[0], 1);
977 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
978 // No commitment_signed so get_event_msg's assert(len == 1) passes
980 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
981 check_added_monitors!(nodes[1], 1);
982 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
984 assert_eq!(get_feerate!(nodes[0], nodes[1], channel_id), feerate + 30);
985 assert_eq!(get_feerate!(nodes[1], nodes[0], channel_id), feerate + 30);
986 close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
987 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
988 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
992 fn fake_network_test() {
993 // Simple test which builds a network of ChannelManagers, connects them to each other, and
994 // tests that payments get routed and transactions broadcast in semi-reasonable ways.
995 let chanmon_cfgs = create_chanmon_cfgs(4);
996 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
997 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
998 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1000 // Create some initial channels
1001 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1002 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1003 let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
1005 // Rebalance the network a bit by relaying one payment through all the channels...
1006 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1007 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1008 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1009 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1011 // Send some more payments
1012 send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
1013 send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
1014 send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
1016 // Test failure packets
1017 let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
1018 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
1020 // Add a new channel that skips 3
1021 let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3);
1023 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
1024 send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
1025 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1026 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1027 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1028 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1029 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1031 // Do some rebalance loop payments, simultaneously
1032 let mut hops = Vec::with_capacity(3);
1033 hops.push(RouteHop {
1034 pubkey: nodes[2].node.get_our_node_id(),
1035 node_features: NodeFeatures::empty(),
1036 short_channel_id: chan_2.0.contents.short_channel_id,
1037 channel_features: ChannelFeatures::empty(),
1039 cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32
1041 hops.push(RouteHop {
1042 pubkey: nodes[3].node.get_our_node_id(),
1043 node_features: NodeFeatures::empty(),
1044 short_channel_id: chan_3.0.contents.short_channel_id,
1045 channel_features: ChannelFeatures::empty(),
1047 cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32
1049 hops.push(RouteHop {
1050 pubkey: nodes[1].node.get_our_node_id(),
1051 node_features: nodes[1].node.node_features(),
1052 short_channel_id: chan_4.0.contents.short_channel_id,
1053 channel_features: nodes[1].node.channel_features(),
1055 cltv_expiry_delta: TEST_FINAL_CLTV,
1057 hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
1058 hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
1059 let payment_preimage_1 = send_along_route(&nodes[1],
1060 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1061 &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
1063 let mut hops = Vec::with_capacity(3);
1064 hops.push(RouteHop {
1065 pubkey: nodes[3].node.get_our_node_id(),
1066 node_features: NodeFeatures::empty(),
1067 short_channel_id: chan_4.0.contents.short_channel_id,
1068 channel_features: ChannelFeatures::empty(),
1070 cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32
1072 hops.push(RouteHop {
1073 pubkey: nodes[2].node.get_our_node_id(),
1074 node_features: NodeFeatures::empty(),
1075 short_channel_id: chan_3.0.contents.short_channel_id,
1076 channel_features: ChannelFeatures::empty(),
1078 cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32
1080 hops.push(RouteHop {
1081 pubkey: nodes[1].node.get_our_node_id(),
1082 node_features: nodes[1].node.node_features(),
1083 short_channel_id: chan_2.0.contents.short_channel_id,
1084 channel_features: nodes[1].node.channel_features(),
1086 cltv_expiry_delta: TEST_FINAL_CLTV,
1088 hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
1089 hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
1090 let payment_hash_2 = send_along_route(&nodes[1],
1091 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1092 &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
1094 // Claim the rebalances...
1095 fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
1096 claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
1098 // Close down the channels...
1099 close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1100 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1101 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1102 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1103 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1104 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1105 close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1106 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1107 check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1108 close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1109 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1110 check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1114 fn holding_cell_htlc_counting() {
1115 // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1116 // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1117 // commitment dance rounds.
1118 let chanmon_cfgs = create_chanmon_cfgs(3);
1119 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1120 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1121 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1122 create_announced_chan_between_nodes(&nodes, 0, 1);
1123 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1125 // Fetch a route in advance as we will be unable to once we're unable to send.
1126 let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1128 let mut payments = Vec::new();
1130 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1131 nodes[1].node.send_payment_with_route(&route, payment_hash,
1132 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1133 payments.push((payment_preimage, payment_hash));
1135 check_added_monitors!(nodes[1], 1);
1137 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1138 assert_eq!(events.len(), 1);
1139 let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1140 assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1142 // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1143 // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1146 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, payment_hash_1,
1147 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)
1148 ), true, APIError::ChannelUnavailable { .. }, {});
1149 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1152 // This should also be true if we try to forward a payment.
1153 let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
1155 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
1156 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1157 check_added_monitors!(nodes[0], 1);
1160 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1161 assert_eq!(events.len(), 1);
1162 let payment_event = SendEvent::from_event(events.pop().unwrap());
1163 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1165 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1166 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1167 // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1168 // fails), the second will process the resulting failure and fail the HTLC backward.
1169 expect_pending_htlcs_forwardable!(nodes[1]);
1170 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
1171 check_added_monitors!(nodes[1], 1);
1173 let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1174 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
1175 commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1177 expect_payment_failed_with_update!(nodes[0], payment_hash_2, false, chan_2.0.contents.short_channel_id, false);
1179 // Now forward all the pending HTLCs and claim them back
1180 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]);
1181 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg);
1182 check_added_monitors!(nodes[2], 1);
1184 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1185 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1186 check_added_monitors!(nodes[1], 1);
1187 let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1189 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1190 check_added_monitors!(nodes[1], 1);
1191 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1193 for ref update in as_updates.update_add_htlcs.iter() {
1194 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update);
1196 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed);
1197 check_added_monitors!(nodes[2], 1);
1198 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
1199 check_added_monitors!(nodes[2], 1);
1200 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1202 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1203 check_added_monitors!(nodes[1], 1);
1204 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1205 check_added_monitors!(nodes[1], 1);
1206 let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1208 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa);
1209 check_added_monitors!(nodes[2], 1);
1211 expect_pending_htlcs_forwardable!(nodes[2]);
1213 let events = nodes[2].node.get_and_clear_pending_events();
1214 assert_eq!(events.len(), payments.len());
1215 for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1217 &Event::PaymentClaimable { ref payment_hash, .. } => {
1218 assert_eq!(*payment_hash, *hash);
1220 _ => panic!("Unexpected event"),
1224 for (preimage, _) in payments.drain(..) {
1225 claim_payment(&nodes[1], &[&nodes[2]], preimage);
1228 send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1232 fn duplicate_htlc_test() {
1233 // Test that we accept duplicate payment_hash HTLCs across the network and that
1234 // claiming/failing them are all separate and don't affect each other
1235 let chanmon_cfgs = create_chanmon_cfgs(6);
1236 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1237 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1238 let mut nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1240 // Create some initial channels to route via 3 to 4/5 from 0/1/2
1241 create_announced_chan_between_nodes(&nodes, 0, 3);
1242 create_announced_chan_between_nodes(&nodes, 1, 3);
1243 create_announced_chan_between_nodes(&nodes, 2, 3);
1244 create_announced_chan_between_nodes(&nodes, 3, 4);
1245 create_announced_chan_between_nodes(&nodes, 3, 5);
1247 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
1249 *nodes[0].network_payment_count.borrow_mut() -= 1;
1250 assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
1252 *nodes[0].network_payment_count.borrow_mut() -= 1;
1253 assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
1255 claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
1256 fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
1257 claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
1261 fn test_duplicate_htlc_different_direction_onchain() {
1262 // Test that ChannelMonitor doesn't generate 2 preimage txn
1263 // when we have 2 HTLCs with same preimage that go across a node
1264 // in opposite directions, even with the same payment secret.
1265 let chanmon_cfgs = create_chanmon_cfgs(2);
1266 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1267 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1268 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1270 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1273 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1275 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
1277 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 800_000);
1278 let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
1279 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
1281 // Provide preimage to node 0 by claiming payment
1282 nodes[0].node.claim_funds(payment_preimage);
1283 expect_payment_claimed!(nodes[0], payment_hash, 800_000);
1284 check_added_monitors!(nodes[0], 1);
1286 // Broadcast node 1 commitment txn
1287 let remote_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
1289 assert_eq!(remote_txn[0].output.len(), 4); // 1 local, 1 remote, 1 htlc inbound, 1 htlc outbound
1290 let mut has_both_htlcs = 0; // check htlcs match ones committed
1291 for outp in remote_txn[0].output.iter() {
1292 if outp.value == 800_000 / 1000 {
1293 has_both_htlcs += 1;
1294 } else if outp.value == 900_000 / 1000 {
1295 has_both_htlcs += 1;
1298 assert_eq!(has_both_htlcs, 2);
1300 mine_transaction(&nodes[0], &remote_txn[0]);
1301 check_added_monitors!(nodes[0], 1);
1302 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
1303 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
1305 let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1306 assert_eq!(claim_txn.len(), 3);
1308 check_spends!(claim_txn[0], remote_txn[0]); // Immediate HTLC claim with preimage
1309 check_spends!(claim_txn[1], remote_txn[0]);
1310 check_spends!(claim_txn[2], remote_txn[0]);
1311 let preimage_tx = &claim_txn[0];
1312 let (preimage_bump_tx, timeout_tx) = if claim_txn[1].input[0].previous_output == preimage_tx.input[0].previous_output {
1313 (&claim_txn[1], &claim_txn[2])
1315 (&claim_txn[2], &claim_txn[1])
1318 assert_eq!(preimage_tx.input.len(), 1);
1319 assert_eq!(preimage_bump_tx.input.len(), 1);
1321 assert_eq!(preimage_tx.input.len(), 1);
1322 assert_eq!(preimage_tx.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC 1 <--> 0, preimage tx
1323 assert_eq!(remote_txn[0].output[preimage_tx.input[0].previous_output.vout as usize].value, 800);
1325 assert_eq!(timeout_tx.input.len(), 1);
1326 assert_eq!(timeout_tx.input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
1327 check_spends!(timeout_tx, remote_txn[0]);
1328 assert_eq!(remote_txn[0].output[timeout_tx.input[0].previous_output.vout as usize].value, 900);
1330 let events = nodes[0].node.get_and_clear_pending_msg_events();
1331 assert_eq!(events.len(), 3);
1334 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1335 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
1336 assert_eq!(node_id, nodes[1].node.get_our_node_id());
1337 assert_eq!(msg.data, "Channel closed because commitment or closing transaction was confirmed on chain.");
1339 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
1340 assert!(update_add_htlcs.is_empty());
1341 assert!(update_fail_htlcs.is_empty());
1342 assert_eq!(update_fulfill_htlcs.len(), 1);
1343 assert!(update_fail_malformed_htlcs.is_empty());
1344 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
1346 _ => panic!("Unexpected event"),
1352 fn test_basic_channel_reserve() {
1353 let chanmon_cfgs = create_chanmon_cfgs(2);
1354 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1355 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1356 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1357 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1359 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1360 let channel_reserve = chan_stat.channel_reserve_msat;
1362 // The 2* and +1 are for the fee spike reserve.
1363 let commit_tx_fee = 2 * commit_tx_fee_msat(get_feerate!(nodes[0], nodes[1], chan.2), 1 + 1, &get_channel_type_features!(nodes[0], nodes[1], chan.2));
1364 let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
1365 let (mut route, our_payment_hash, _, our_payment_secret) =
1366 get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
1367 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1368 let err = nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1369 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).err().unwrap();
1371 PaymentSendFailure::AllFailedResendSafe(ref fails) => {
1372 if let &APIError::ChannelUnavailable { .. } = &fails[0] {}
1373 else { panic!("Unexpected error variant"); }
1375 _ => panic!("Unexpected error variant"),
1377 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1379 send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
1383 fn test_fee_spike_violation_fails_htlc() {
1384 let chanmon_cfgs = create_chanmon_cfgs(2);
1385 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1386 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1387 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1388 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1390 let (mut route, payment_hash, _, payment_secret) =
1391 get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
1392 route.paths[0].hops[0].fee_msat += 1;
1393 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1394 let secp_ctx = Secp256k1::new();
1395 let session_priv = SecretKey::from_slice(&[42; 32]).expect("RNG is bad!");
1397 let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1399 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1400 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1401 3460001, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1402 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1403 let msg = msgs::UpdateAddHTLC {
1406 amount_msat: htlc_msat,
1407 payment_hash: payment_hash,
1408 cltv_expiry: htlc_cltv,
1409 onion_routing_packet: onion_packet,
1410 skimmed_fee_msat: None,
1413 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1415 // Now manually create the commitment_signed message corresponding to the update_add
1416 // nodes[0] just sent. In the code for construction of this message, "local" refers
1417 // to the sender of the message, and "remote" refers to the receiver.
1419 let feerate_per_kw = get_feerate!(nodes[0], nodes[1], chan.2);
1421 const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1423 // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
1424 // needed to sign the new commitment tx and (2) sign the new commitment tx.
1425 let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1426 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1427 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1428 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
1429 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1430 ).flatten().unwrap();
1431 let chan_signer = local_chan.get_signer();
1432 // Make the signer believe we validated another commitment, so we can release the secret
1433 chan_signer.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
1435 let pubkeys = chan_signer.as_ref().pubkeys();
1436 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1437 chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1438 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx),
1439 chan_signer.as_ref().pubkeys().funding_pubkey)
1441 let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1442 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
1443 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
1444 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
1445 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1446 ).flatten().unwrap();
1447 let chan_signer = remote_chan.get_signer();
1448 let pubkeys = chan_signer.as_ref().pubkeys();
1449 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1450 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
1451 chan_signer.as_ref().pubkeys().funding_pubkey)
1454 // Assemble the set of keys we can use for signatures for our commitment_signed message.
1455 let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
1456 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
1458 // Build the remote commitment transaction so we can sign it, and then later use the
1459 // signature for the commitment_signed message.
1460 let local_chan_balance = 1313;
1462 let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
1464 amount_msat: 3460001,
1465 cltv_expiry: htlc_cltv,
1467 transaction_output_index: Some(1),
1470 let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
1473 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1474 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1475 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
1476 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1477 ).flatten().unwrap();
1478 let local_chan_signer = local_chan.get_signer();
1479 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
1483 local_funding, remote_funding,
1484 commit_tx_keys.clone(),
1486 &mut vec![(accepted_htlc_info, ())],
1487 &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
1489 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
1492 let commit_signed_msg = msgs::CommitmentSigned {
1495 htlc_signatures: res.1,
1497 partial_signature_with_nonce: None,
1500 // Send the commitment_signed message to the nodes[1].
1501 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
1502 let _ = nodes[1].node.get_and_clear_pending_msg_events();
1504 // Send the RAA to nodes[1].
1505 let raa_msg = msgs::RevokeAndACK {
1507 per_commitment_secret: local_secret,
1508 next_per_commitment_point: next_local_point,
1510 next_local_nonce: None,
1512 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
1514 let events = nodes[1].node.get_and_clear_pending_msg_events();
1515 assert_eq!(events.len(), 1);
1516 // Make sure the HTLC failed in the way we expect.
1518 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. }, .. } => {
1519 assert_eq!(update_fail_htlcs.len(), 1);
1520 update_fail_htlcs[0].clone()
1522 _ => panic!("Unexpected event"),
1524 nodes[1].logger.assert_log("lightning::ln::channel".to_string(),
1525 format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", raa_msg.channel_id), 1);
1527 check_added_monitors!(nodes[1], 2);
1531 fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
1532 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1533 // Set the fee rate for the channel very high, to the point where the fundee
1534 // sending any above-dust amount would result in a channel reserve violation.
1535 // In this test we check that we would be prevented from sending an HTLC in
1537 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1538 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1539 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1540 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1541 let default_config = UserConfig::default();
1542 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1544 let mut push_amt = 100_000_000;
1545 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1547 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1549 let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1551 // Fetch a route in advance as we will be unable to once we're unable to send.
1552 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
1553 // Sending exactly enough to hit the reserve amount should be accepted
1554 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1555 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1558 // However one more HTLC should be significantly over the reserve amount and fail.
1559 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1560 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1561 ), true, APIError::ChannelUnavailable { .. }, {});
1562 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1566 fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
1567 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1568 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1569 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1570 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1571 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1572 let default_config = UserConfig::default();
1573 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1575 // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1576 // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1577 // transaction fee with 0 HTLCs (183 sats)).
1578 let mut push_amt = 100_000_000;
1579 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1580 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1581 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1583 // Send four HTLCs to cover the initial push_msat buffer we're required to include
1584 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1585 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1588 let (mut route, payment_hash, _, payment_secret) =
1589 get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
1590 route.paths[0].hops[0].fee_msat = 700_000;
1591 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1592 let secp_ctx = Secp256k1::new();
1593 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1594 let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1595 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1596 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1597 700_000, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1598 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1599 let msg = msgs::UpdateAddHTLC {
1601 htlc_id: MIN_AFFORDABLE_HTLC_COUNT as u64,
1602 amount_msat: htlc_msat,
1603 payment_hash: payment_hash,
1604 cltv_expiry: htlc_cltv,
1605 onion_routing_packet: onion_packet,
1606 skimmed_fee_msat: None,
1609 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
1610 // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1611 nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string(), 1);
1612 assert_eq!(nodes[0].node.list_channels().len(), 0);
1613 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
1614 assert_eq!(err_msg.data, "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value");
1615 check_added_monitors!(nodes[0], 1);
1616 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value".to_string() },
1617 [nodes[1].node.get_our_node_id()], 100000);
1621 fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
1622 // Test that if we receive many dust HTLCs over an outbound channel, they don't count when
1623 // calculating our commitment transaction fee (this was previously broken).
1624 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1625 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1627 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1628 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1629 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1630 let default_config = UserConfig::default();
1631 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1633 // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1634 // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1635 // transaction fee with 0 HTLCs (183 sats)).
1636 let mut push_amt = 100_000_000;
1637 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1638 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1639 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt);
1641 let dust_amt = crate::ln::channel::MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000
1642 + feerate_per_kw as u64 * htlc_success_tx_weight(&channel_type_features) / 1000 * 1000 - 1;
1643 // In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
1644 // reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
1645 // commitment transaction fee.
1646 route_payment(&nodes[1], &[&nodes[0]], dust_amt);
1648 // Send four HTLCs to cover the initial push_msat buffer we're required to include
1649 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1650 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1653 // One more than the dust amt should fail, however.
1654 let (mut route, our_payment_hash, _, our_payment_secret) =
1655 get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt);
1656 route.paths[0].hops[0].fee_msat += 1;
1657 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1658 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1659 ), true, APIError::ChannelUnavailable { .. }, {});
1663 fn test_chan_init_feerate_unaffordability() {
1664 // Test that we will reject channel opens which do not leave enough to pay for any HTLCs due to
1665 // channel reserve and feerate requirements.
1666 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1667 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1668 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1669 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1670 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1671 let default_config = UserConfig::default();
1672 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1674 // Set the push_msat amount such that nodes[0] will not be able to afford to add even a single
1676 let mut push_amt = 100_000_000;
1677 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1678 assert_eq!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt + 1, 42, None).unwrap_err(),
1679 APIError::APIMisuseError { err: "Funding amount (356) can't even pay fee for initial commitment transaction fee of 357.".to_string() });
1681 // During open, we don't have a "counterparty channel reserve" to check against, so that
1682 // requirement only comes into play on the open_channel handling side.
1683 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1684 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt, 42, None).unwrap();
1685 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
1686 open_channel_msg.push_msat += 1;
1687 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
1689 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
1690 assert_eq!(msg_events.len(), 1);
1691 match msg_events[0] {
1692 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
1693 assert_eq!(msg.data, "Insufficient funding amount for initial reserve");
1695 _ => panic!("Unexpected event"),
1700 fn test_chan_reserve_dust_inbound_htlcs_inbound_chan() {
1701 // Test that if we receive many dust HTLCs over an inbound channel, they don't count when
1702 // calculating our counterparty's commitment transaction fee (this was previously broken).
1703 let chanmon_cfgs = create_chanmon_cfgs(2);
1704 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1705 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1706 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1707 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 98000000);
1709 let payment_amt = 46000; // Dust amount
1710 // In the previous code, these first four payments would succeed.
1711 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1712 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1713 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1714 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1716 // Then these next 5 would be interpreted by nodes[1] as violating the fee spike buffer.
1717 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1718 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1719 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1720 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1721 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1723 // And this last payment previously resulted in nodes[1] closing on its inbound-channel
1724 // counterparty, because it counted all the previous dust HTLCs against nodes[0]'s commitment
1725 // transaction fee and therefore perceived this next payment as a channel reserve violation.
1726 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1730 fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1731 let chanmon_cfgs = create_chanmon_cfgs(3);
1732 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1733 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1734 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1735 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1736 let _ = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
1739 let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1740 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1741 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
1742 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
1744 // Add a 2* and +1 for the fee spike reserve.
1745 let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1746 let recv_value_1 = (chan_stat.value_to_self_msat - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlc)/2;
1747 let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
1749 // Add a pending HTLC.
1750 let (route_1, our_payment_hash_1, _, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_1);
1751 let payment_event_1 = {
1752 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1753 RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1754 check_added_monitors!(nodes[0], 1);
1756 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1757 assert_eq!(events.len(), 1);
1758 SendEvent::from_event(events.remove(0))
1760 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1762 // Attempt to trigger a channel reserve violation --> payment failure.
1763 let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2, &channel_type_features);
1764 let recv_value_2 = chan_stat.value_to_self_msat - amt_msat_1 - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlcs + 1;
1765 let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1766 let mut route_2 = route_1.clone();
1767 route_2.paths[0].hops.last_mut().unwrap().fee_msat = amt_msat_2;
1769 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1770 let secp_ctx = Secp256k1::new();
1771 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1772 let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
1773 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route_2.paths[0], &session_priv).unwrap();
1774 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
1775 &route_2.paths[0], recv_value_2, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
1776 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash_1).unwrap();
1777 let msg = msgs::UpdateAddHTLC {
1780 amount_msat: htlc_msat + 1,
1781 payment_hash: our_payment_hash_1,
1782 cltv_expiry: htlc_cltv,
1783 onion_routing_packet: onion_packet,
1784 skimmed_fee_msat: None,
1787 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1788 // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1789 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote HTLC add would put them under remote reserve value".to_string(), 1);
1790 assert_eq!(nodes[1].node.list_channels().len(), 1);
1791 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
1792 assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
1793 check_added_monitors!(nodes[1], 1);
1794 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote HTLC add would put them under remote reserve value".to_string() },
1795 [nodes[0].node.get_our_node_id()], 100000);
1799 fn test_inbound_outbound_capacity_is_not_zero() {
1800 let chanmon_cfgs = create_chanmon_cfgs(2);
1801 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1802 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1803 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1804 let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1805 let channels0 = node_chanmgrs[0].list_channels();
1806 let channels1 = node_chanmgrs[1].list_channels();
1807 let default_config = UserConfig::default();
1808 assert_eq!(channels0.len(), 1);
1809 assert_eq!(channels1.len(), 1);
1811 let reserve = get_holder_selected_channel_reserve_satoshis(100_000, &default_config);
1812 assert_eq!(channels0[0].inbound_capacity_msat, 95000000 - reserve*1000);
1813 assert_eq!(channels1[0].outbound_capacity_msat, 95000000 - reserve*1000);
1815 assert_eq!(channels0[0].outbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1816 assert_eq!(channels1[0].inbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1819 fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64, channel_type_features: &ChannelTypeFeatures) -> u64 {
1820 (commitment_tx_base_weight(channel_type_features) + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
1824 fn test_channel_reserve_holding_cell_htlcs() {
1825 let chanmon_cfgs = create_chanmon_cfgs(3);
1826 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1827 // When this test was written, the default base fee floated based on the HTLC count.
1828 // It is now fixed, so we simply set the fee to the expected value here.
1829 let mut config = test_default_channel_config();
1830 config.channel_config.forwarding_fee_base_msat = 239;
1831 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
1832 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1833 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 190000, 1001);
1834 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 190000, 1001);
1836 let mut stat01 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1837 let mut stat11 = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
1839 let mut stat12 = get_channel_value_stat!(nodes[1], nodes[2], chan_2.2);
1840 let mut stat22 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
1842 macro_rules! expect_forward {
1844 let mut events = $node.node.get_and_clear_pending_msg_events();
1845 assert_eq!(events.len(), 1);
1846 check_added_monitors!($node, 1);
1847 let payment_event = SendEvent::from_event(events.remove(0));
1852 let feemsat = 239; // set above
1853 let total_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1854 let feerate = get_feerate!(nodes[0], nodes[1], chan_1.2);
1855 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_1.2);
1857 let recv_value_0 = stat01.counterparty_max_htlc_value_in_flight_msat - total_fee_msat;
1859 // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
1861 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1862 .with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1863 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0);
1864 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1865 assert!(route.paths[0].hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
1867 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1868 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1869 ), true, APIError::ChannelUnavailable { .. }, {});
1870 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1873 // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
1874 // nodes[0]'s wealth
1876 let amt_msat = recv_value_0 + total_fee_msat;
1877 // 3 for the 3 HTLCs that will be sent, 2* and +1 for the fee spike reserve.
1878 // Also, ensure that each payment has enough to be over the dust limit to
1879 // ensure it'll be included in each commit tx fee calculation.
1880 let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1881 let ensure_htlc_amounts_above_dust_buffer = 3 * (stat01.counterparty_dust_limit_msat + 1000);
1882 if stat01.value_to_self_msat < stat01.channel_reserve_msat + commit_tx_fee_all_htlcs + ensure_htlc_amounts_above_dust_buffer + amt_msat {
1886 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1887 .with_bolt11_features(nodes[2].node.invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1888 let route = get_route!(nodes[0], payment_params, recv_value_0).unwrap();
1889 let (payment_preimage, ..) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], recv_value_0);
1890 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
1892 let (stat01_, stat11_, stat12_, stat22_) = (
1893 get_channel_value_stat!(nodes[0], nodes[1], chan_1.2),
1894 get_channel_value_stat!(nodes[1], nodes[0], chan_1.2),
1895 get_channel_value_stat!(nodes[1], nodes[2], chan_2.2),
1896 get_channel_value_stat!(nodes[2], nodes[1], chan_2.2),
1899 assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
1900 assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
1901 assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
1902 assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
1903 stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
1906 // adding pending output.
1907 // 2* and +1 HTLCs on the commit tx fee for the fee spike reserve.
1908 // The reason we're dividing by two here is as follows: the dividend is the total outbound liquidity
1909 // after fees, the channel reserve, and the fee spike buffer are removed. We eventually want to
1910 // divide this quantity into 3 portions, that will each be sent in an HTLC. This allows us
1911 // to test channel channel reserve policy at the edges of what amount is sendable, i.e.
1912 // cases where 1 msat over X amount will cause a payment failure, but anything less than
1913 // that can be sent successfully. So, dividing by two is a somewhat arbitrary way of getting
1914 // the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
1915 // is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
1917 let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1918 let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
1919 let amt_msat_1 = recv_value_1 + total_fee_msat;
1921 let (route_1, our_payment_hash_1, our_payment_preimage_1, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_1);
1922 let payment_event_1 = {
1923 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1924 RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1925 check_added_monitors!(nodes[0], 1);
1927 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1928 assert_eq!(events.len(), 1);
1929 SendEvent::from_event(events.remove(0))
1931 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1933 // channel reserve test with htlc pending output > 0
1934 let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs;
1936 let mut route = route_1.clone();
1937 route.paths[0].hops.last_mut().unwrap().fee_msat = recv_value_2 + 1;
1938 let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
1939 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1940 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1941 ), true, APIError::ChannelUnavailable { .. }, {});
1942 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1945 // split the rest to test holding cell
1946 let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1947 let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
1948 let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2;
1949 let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
1951 let stat = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1952 assert_eq!(stat.value_to_self_msat - (stat.pending_outbound_htlcs_amount_msat + recv_value_21 + recv_value_22 + total_fee_msat + total_fee_msat + commit_tx_fee_3_htlcs), stat.channel_reserve_msat);
1955 // now see if they go through on both sides
1956 let (route_21, our_payment_hash_21, our_payment_preimage_21, our_payment_secret_21) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_21);
1957 // but this will stuck in the holding cell
1958 nodes[0].node.send_payment_with_route(&route_21, our_payment_hash_21,
1959 RecipientOnionFields::secret_only(our_payment_secret_21), PaymentId(our_payment_hash_21.0)).unwrap();
1960 check_added_monitors!(nodes[0], 0);
1961 let events = nodes[0].node.get_and_clear_pending_events();
1962 assert_eq!(events.len(), 0);
1964 // test with outbound holding cell amount > 0
1966 let (mut route, our_payment_hash, _, our_payment_secret) =
1967 get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
1968 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1969 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1970 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1971 ), true, APIError::ChannelUnavailable { .. }, {});
1972 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1975 let (route_22, our_payment_hash_22, our_payment_preimage_22, our_payment_secret_22) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
1976 // this will also stuck in the holding cell
1977 nodes[0].node.send_payment_with_route(&route_22, our_payment_hash_22,
1978 RecipientOnionFields::secret_only(our_payment_secret_22), PaymentId(our_payment_hash_22.0)).unwrap();
1979 check_added_monitors!(nodes[0], 0);
1980 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1981 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1983 // flush the pending htlc
1984 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg);
1985 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1986 check_added_monitors!(nodes[1], 1);
1988 // the pending htlc should be promoted to committed
1989 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
1990 check_added_monitors!(nodes[0], 1);
1991 let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
1993 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed);
1994 let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
1995 // No commitment_signed so get_event_msg's assert(len == 1) passes
1996 check_added_monitors!(nodes[0], 1);
1998 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack);
1999 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2000 check_added_monitors!(nodes[1], 1);
2002 expect_pending_htlcs_forwardable!(nodes[1]);
2004 let ref payment_event_11 = expect_forward!(nodes[1]);
2005 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]);
2006 commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
2008 expect_pending_htlcs_forwardable!(nodes[2]);
2009 expect_payment_claimable!(nodes[2], our_payment_hash_1, our_payment_secret_1, recv_value_1);
2011 // flush the htlcs in the holding cell
2012 assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
2013 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]);
2014 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]);
2015 commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
2016 expect_pending_htlcs_forwardable!(nodes[1]);
2018 let ref payment_event_3 = expect_forward!(nodes[1]);
2019 assert_eq!(payment_event_3.msgs.len(), 2);
2020 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]);
2021 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]);
2023 commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
2024 expect_pending_htlcs_forwardable!(nodes[2]);
2026 let events = nodes[2].node.get_and_clear_pending_events();
2027 assert_eq!(events.len(), 2);
2029 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2030 assert_eq!(our_payment_hash_21, *payment_hash);
2031 assert_eq!(recv_value_21, amount_msat);
2032 assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2033 assert_eq!(via_channel_id, Some(chan_2.2));
2035 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2036 assert!(payment_preimage.is_none());
2037 assert_eq!(our_payment_secret_21, *payment_secret);
2039 _ => panic!("expected PaymentPurpose::InvoicePayment")
2042 _ => panic!("Unexpected event"),
2045 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2046 assert_eq!(our_payment_hash_22, *payment_hash);
2047 assert_eq!(recv_value_22, amount_msat);
2048 assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2049 assert_eq!(via_channel_id, Some(chan_2.2));
2051 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2052 assert!(payment_preimage.is_none());
2053 assert_eq!(our_payment_secret_22, *payment_secret);
2055 _ => panic!("expected PaymentPurpose::InvoicePayment")
2058 _ => panic!("Unexpected event"),
2061 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
2062 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
2063 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
2065 let commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1, &channel_type_features);
2066 let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
2067 send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_3);
2069 let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
2070 let expected_value_to_self = stat01.value_to_self_msat - (recv_value_1 + total_fee_msat) - (recv_value_21 + total_fee_msat) - (recv_value_22 + total_fee_msat) - (recv_value_3 + total_fee_msat);
2071 let stat0 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
2072 assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
2073 assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat + commit_tx_fee_1_htlc);
2075 let stat2 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
2076 assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3);
2080 fn channel_reserve_in_flight_removes() {
2081 // In cases where one side claims an HTLC, it thinks it has additional available funds that it
2082 // can send to its counterparty, but due to update ordering, the other side may not yet have
2083 // considered those HTLCs fully removed.
2084 // This tests that we don't count HTLCs which will not be included in the next remote
2085 // commitment transaction towards the reserve value (as it implies no commitment transaction
2086 // will be generated which violates the remote reserve value).
2087 // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
2089 // * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
2090 // you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
2091 // you only consider the value of the first HTLC, it may not),
2092 // * start routing a third HTLC from A to B,
2093 // * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
2094 // the other claim in its holding cell, as it immediately goes into AwaitingRAA),
2095 // * deliver the first fulfill from B
2096 // * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
2098 // * deliver A's response CS and RAA.
2099 // This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
2100 // removed it fully. B now has the push_msat plus the first two HTLCs in value.
2101 // * Now B happily sends another HTLC, potentially violating its reserve value from A's point
2102 // of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
2103 let chanmon_cfgs = create_chanmon_cfgs(2);
2104 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2105 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2106 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2107 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2109 let b_chan_values = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
2110 // Route the first two HTLCs.
2111 let payment_value_1 = b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000;
2112 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], payment_value_1);
2113 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], 20_000);
2115 // Start routing the third HTLC (this is just used to get everyone in the right state).
2116 let (route, payment_hash_3, payment_preimage_3, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
2118 nodes[0].node.send_payment_with_route(&route, payment_hash_3,
2119 RecipientOnionFields::secret_only(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
2120 check_added_monitors!(nodes[0], 1);
2121 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2122 assert_eq!(events.len(), 1);
2123 SendEvent::from_event(events.remove(0))
2126 // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
2127 // initial fulfill/CS.
2128 nodes[1].node.claim_funds(payment_preimage_1);
2129 expect_payment_claimed!(nodes[1], payment_hash_1, payment_value_1);
2130 check_added_monitors!(nodes[1], 1);
2131 let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2133 // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
2134 // remove the second HTLC when we send the HTLC back from B to A.
2135 nodes[1].node.claim_funds(payment_preimage_2);
2136 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000);
2137 check_added_monitors!(nodes[1], 1);
2138 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2140 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]);
2141 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed);
2142 check_added_monitors!(nodes[0], 1);
2143 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2144 expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
2146 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]);
2147 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg);
2148 check_added_monitors!(nodes[1], 1);
2149 // B is already AwaitingRAA, so cant generate a CS here
2150 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2152 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2153 check_added_monitors!(nodes[1], 1);
2154 let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2156 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2157 check_added_monitors!(nodes[0], 1);
2158 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2160 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2161 check_added_monitors!(nodes[1], 1);
2162 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2164 // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
2165 // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
2166 // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
2167 // can no longer broadcast a commitment transaction with it and B has the preimage so can go
2168 // on-chain as necessary).
2169 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]);
2170 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed);
2171 check_added_monitors!(nodes[0], 1);
2172 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2173 expect_payment_sent(&nodes[0], payment_preimage_2, None, false, false);
2175 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2176 check_added_monitors!(nodes[1], 1);
2177 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2179 expect_pending_htlcs_forwardable!(nodes[1]);
2180 expect_payment_claimable!(nodes[1], payment_hash_3, payment_secret_3, 100000);
2182 // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
2183 // resolve the second HTLC from A's point of view.
2184 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2185 check_added_monitors!(nodes[0], 1);
2186 expect_payment_path_successful!(nodes[0]);
2187 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2189 // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
2190 // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
2191 let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[1], nodes[0], 10000);
2193 nodes[1].node.send_payment_with_route(&route, payment_hash_4,
2194 RecipientOnionFields::secret_only(payment_secret_4), PaymentId(payment_hash_4.0)).unwrap();
2195 check_added_monitors!(nodes[1], 1);
2196 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2197 assert_eq!(events.len(), 1);
2198 SendEvent::from_event(events.remove(0))
2201 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]);
2202 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg);
2203 check_added_monitors!(nodes[0], 1);
2204 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2206 // Now just resolve all the outstanding messages/HTLCs for completeness...
2208 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2209 check_added_monitors!(nodes[1], 1);
2210 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2212 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2213 check_added_monitors!(nodes[1], 1);
2215 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2216 check_added_monitors!(nodes[0], 1);
2217 expect_payment_path_successful!(nodes[0]);
2218 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2220 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2221 check_added_monitors!(nodes[1], 1);
2222 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2224 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2225 check_added_monitors!(nodes[0], 1);
2227 expect_pending_htlcs_forwardable!(nodes[0]);
2228 expect_payment_claimable!(nodes[0], payment_hash_4, payment_secret_4, 10000);
2230 claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
2231 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
2235 fn channel_monitor_network_test() {
2236 // Simple test which builds a network of ChannelManagers, connects them to each other, and
2237 // tests that ChannelMonitor is able to recover from various states.
2238 let chanmon_cfgs = create_chanmon_cfgs(5);
2239 let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
2240 let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
2241 let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
2243 // Create some initial channels
2244 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2245 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2246 let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
2247 let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
2249 // Make sure all nodes are at the same starting height
2250 connect_blocks(&nodes[0], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
2251 connect_blocks(&nodes[1], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
2252 connect_blocks(&nodes[2], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
2253 connect_blocks(&nodes[3], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);
2254 connect_blocks(&nodes[4], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[4].best_block_info().1);
2256 // Rebalance the network a bit by relaying one payment through all the channels...
2257 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2258 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2259 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2260 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2262 // Simple case with no pending HTLCs:
2263 nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
2264 check_added_monitors!(nodes[1], 1);
2265 check_closed_broadcast!(nodes[1], true);
2267 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
2268 assert_eq!(node_txn.len(), 1);
2269 mine_transaction(&nodes[0], &node_txn[0]);
2270 check_added_monitors!(nodes[0], 1);
2271 test_txn_broadcast(&nodes[0], &chan_1, Some(node_txn[0].clone()), HTLCType::NONE);
2273 check_closed_broadcast!(nodes[0], true);
2274 assert_eq!(nodes[0].node.list_channels().len(), 0);
2275 assert_eq!(nodes[1].node.list_channels().len(), 1);
2276 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2277 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
2279 // One pending HTLC is discarded by the force-close:
2280 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[1], &[&nodes[2], &nodes[3]], 3_000_000);
2282 // Simple case of one pending HTLC to HTLC-Timeout (note that the HTLC-Timeout is not
2283 // broadcasted until we reach the timelock time).
2284 nodes[1].node.force_close_broadcasting_latest_txn(&chan_2.2, &nodes[2].node.get_our_node_id()).unwrap();
2285 check_closed_broadcast!(nodes[1], true);
2286 check_added_monitors!(nodes[1], 1);
2288 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::NONE);
2289 connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2290 test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
2291 mine_transaction(&nodes[2], &node_txn[0]);
2292 check_added_monitors!(nodes[2], 1);
2293 test_txn_broadcast(&nodes[2], &chan_2, Some(node_txn[0].clone()), HTLCType::NONE);
2295 check_closed_broadcast!(nodes[2], true);
2296 assert_eq!(nodes[1].node.list_channels().len(), 0);
2297 assert_eq!(nodes[2].node.list_channels().len(), 1);
2298 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
2299 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2301 macro_rules! claim_funds {
2302 ($node: expr, $prev_node: expr, $preimage: expr, $payment_hash: expr) => {
2304 $node.node.claim_funds($preimage);
2305 expect_payment_claimed!($node, $payment_hash, 3_000_000);
2306 check_added_monitors!($node, 1);
2308 let events = $node.node.get_and_clear_pending_msg_events();
2309 assert_eq!(events.len(), 1);
2311 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
2312 assert!(update_add_htlcs.is_empty());
2313 assert!(update_fail_htlcs.is_empty());
2314 assert_eq!(*node_id, $prev_node.node.get_our_node_id());
2316 _ => panic!("Unexpected event"),
2322 // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
2323 // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
2324 nodes[2].node.force_close_broadcasting_latest_txn(&chan_3.2, &nodes[3].node.get_our_node_id()).unwrap();
2325 check_added_monitors!(nodes[2], 1);
2326 check_closed_broadcast!(nodes[2], true);
2327 let node2_commitment_txid;
2329 let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::NONE);
2330 connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2331 test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
2332 node2_commitment_txid = node_txn[0].txid();
2334 // Claim the payment on nodes[3], giving it knowledge of the preimage
2335 claim_funds!(nodes[3], nodes[2], payment_preimage_1, payment_hash_1);
2336 mine_transaction(&nodes[3], &node_txn[0]);
2337 check_added_monitors!(nodes[3], 1);
2338 check_preimage_claim(&nodes[3], &node_txn);
2340 check_closed_broadcast!(nodes[3], true);
2341 assert_eq!(nodes[2].node.list_channels().len(), 0);
2342 assert_eq!(nodes[3].node.list_channels().len(), 1);
2343 check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[3].node.get_our_node_id()], 100000);
2344 check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
2346 // Drop the ChannelMonitor for the previous channel to avoid it broadcasting transactions and
2347 // confusing us in the following tests.
2348 let chan_3_mon = nodes[3].chain_monitor.chain_monitor.remove_monitor(&OutPoint { txid: chan_3.3.txid(), index: 0 });
2350 // One pending HTLC to time out:
2351 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[3], &[&nodes[4]], 3_000_000);
2352 // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
2355 let (close_chan_update_1, close_chan_update_2) = {
2356 connect_blocks(&nodes[3], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
2357 let events = nodes[3].node.get_and_clear_pending_msg_events();
2358 assert_eq!(events.len(), 2);
2359 let close_chan_update_1 = match events[0] {
2360 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2363 _ => panic!("Unexpected event"),
2366 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2367 assert_eq!(node_id, nodes[4].node.get_our_node_id());
2369 _ => panic!("Unexpected event"),
2371 check_added_monitors!(nodes[3], 1);
2373 // Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer.
2375 let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap();
2376 node_txn.retain(|tx| {
2377 if tx.input[0].previous_output.txid == node2_commitment_txid {
2383 let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
2385 // Claim the payment on nodes[4], giving it knowledge of the preimage
2386 claim_funds!(nodes[4], nodes[3], payment_preimage_2, payment_hash_2);
2388 connect_blocks(&nodes[4], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2389 let events = nodes[4].node.get_and_clear_pending_msg_events();
2390 assert_eq!(events.len(), 2);
2391 let close_chan_update_2 = match events[0] {
2392 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2395 _ => panic!("Unexpected event"),
2398 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id } => {
2399 assert_eq!(node_id, nodes[3].node.get_our_node_id());
2401 _ => panic!("Unexpected event"),
2403 check_added_monitors!(nodes[4], 1);
2404 test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
2406 mine_transaction(&nodes[4], &node_txn[0]);
2407 check_preimage_claim(&nodes[4], &node_txn);
2408 (close_chan_update_1, close_chan_update_2)
2410 nodes[3].gossip_sync.handle_channel_update(&close_chan_update_2).unwrap();
2411 nodes[4].gossip_sync.handle_channel_update(&close_chan_update_1).unwrap();
2412 assert_eq!(nodes[3].node.list_channels().len(), 0);
2413 assert_eq!(nodes[4].node.list_channels().len(), 0);
2415 assert_eq!(nodes[3].chain_monitor.chain_monitor.watch_channel(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon),
2416 ChannelMonitorUpdateStatus::Completed);
2417 check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed, [nodes[4].node.get_our_node_id()], 100000);
2418 check_closed_event!(nodes[4], 1, ClosureReason::CommitmentTxConfirmed, [nodes[3].node.get_our_node_id()], 100000);
2422 fn test_justice_tx_htlc_timeout() {
2423 // Test justice txn built on revoked HTLC-Timeout tx, against both sides
2424 let mut alice_config = UserConfig::default();
2425 alice_config.channel_handshake_config.announced_channel = true;
2426 alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2427 alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2428 let mut bob_config = UserConfig::default();
2429 bob_config.channel_handshake_config.announced_channel = true;
2430 bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2431 bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2432 let user_cfgs = [Some(alice_config), Some(bob_config)];
2433 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2434 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2435 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2436 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2437 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2438 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2439 // Create some new channels:
2440 let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1);
2442 // A pending HTLC which will be revoked:
2443 let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2444 // Get the will-be-revoked local txn from nodes[0]
2445 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_5.2);
2446 assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
2447 assert_eq!(revoked_local_txn[0].input.len(), 1);
2448 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
2449 assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
2450 assert_eq!(revoked_local_txn[1].input.len(), 1);
2451 assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2452 assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2453 // Revoke the old state
2454 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
2457 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2459 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2460 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2461 assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
2462 check_spends!(node_txn[0], revoked_local_txn[0]);
2463 node_txn.swap_remove(0);
2465 check_added_monitors!(nodes[1], 1);
2466 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2467 test_txn_broadcast(&nodes[1], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2469 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2470 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2471 // Verify broadcast of revoked HTLC-timeout
2472 let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
2473 check_added_monitors!(nodes[0], 1);
2474 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2475 // Broadcast revoked HTLC-timeout on node 1
2476 mine_transaction(&nodes[1], &node_txn[1]);
2477 test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone());
2479 get_announce_close_broadcast_events(&nodes, 0, 1);
2480 assert_eq!(nodes[0].node.list_channels().len(), 0);
2481 assert_eq!(nodes[1].node.list_channels().len(), 0);
2485 fn test_justice_tx_htlc_success() {
2486 // Test justice txn built on revoked HTLC-Success tx, against both sides
2487 let mut alice_config = UserConfig::default();
2488 alice_config.channel_handshake_config.announced_channel = true;
2489 alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2490 alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2491 let mut bob_config = UserConfig::default();
2492 bob_config.channel_handshake_config.announced_channel = true;
2493 bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2494 bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2495 let user_cfgs = [Some(alice_config), Some(bob_config)];
2496 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2497 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2498 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2499 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2500 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2501 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2502 // Create some new channels:
2503 let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1);
2505 // A pending HTLC which will be revoked:
2506 let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2507 // Get the will-be-revoked local txn from B
2508 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_6.2);
2509 assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
2510 assert_eq!(revoked_local_txn[0].input.len(), 1);
2511 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
2512 assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
2513 // Revoke the old state
2514 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
2516 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2518 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
2519 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2520 assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
2522 check_spends!(node_txn[0], revoked_local_txn[0]);
2523 node_txn.swap_remove(0);
2525 check_added_monitors!(nodes[0], 1);
2526 test_txn_broadcast(&nodes[0], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2528 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2529 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2530 let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
2531 check_added_monitors!(nodes[1], 1);
2532 mine_transaction(&nodes[0], &node_txn[1]);
2533 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2534 test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone(), revoked_local_txn[0].clone());
2536 get_announce_close_broadcast_events(&nodes, 0, 1);
2537 assert_eq!(nodes[0].node.list_channels().len(), 0);
2538 assert_eq!(nodes[1].node.list_channels().len(), 0);
2542 fn revoked_output_claim() {
2543 // Simple test to ensure a node will claim a revoked output when a stale remote commitment
2544 // transaction is broadcast by its counterparty
2545 let chanmon_cfgs = create_chanmon_cfgs(2);
2546 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2547 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2548 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2549 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2550 // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
2551 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2552 assert_eq!(revoked_local_txn.len(), 1);
2553 // Only output is the full channel value back to nodes[0]:
2554 assert_eq!(revoked_local_txn[0].output.len(), 1);
2555 // Send a payment through, updating everyone's latest commitment txn
2556 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
2558 // Inform nodes[1] that nodes[0] broadcast a stale tx
2559 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2560 check_added_monitors!(nodes[1], 1);
2561 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2562 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2563 assert_eq!(node_txn.len(), 1); // ChannelMonitor: justice tx against revoked to_local output
2565 check_spends!(node_txn[0], revoked_local_txn[0]);
2567 // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
2568 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2569 get_announce_close_broadcast_events(&nodes, 0, 1);
2570 check_added_monitors!(nodes[0], 1);
2571 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2575 fn test_forming_justice_tx_from_monitor_updates() {
2576 do_test_forming_justice_tx_from_monitor_updates(true);
2577 do_test_forming_justice_tx_from_monitor_updates(false);
2580 fn do_test_forming_justice_tx_from_monitor_updates(broadcast_initial_commitment: bool) {
2581 // Simple test to make sure that the justice tx formed in WatchtowerPersister
2582 // is properly formed and can be broadcasted/confirmed successfully in the event
2583 // that a revoked commitment transaction is broadcasted
2584 // (Similar to `revoked_output_claim` test but we get the justice tx + broadcast manually)
2585 let chanmon_cfgs = create_chanmon_cfgs(2);
2586 let destination_script0 = chanmon_cfgs[0].keys_manager.get_destination_script().unwrap();
2587 let destination_script1 = chanmon_cfgs[1].keys_manager.get_destination_script().unwrap();
2588 let persisters = vec![WatchtowerPersister::new(destination_script0),
2589 WatchtowerPersister::new(destination_script1)];
2590 let node_cfgs = create_node_cfgs_with_persisters(2, &chanmon_cfgs, persisters.iter().collect());
2591 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2592 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2593 let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
2594 let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
2596 if !broadcast_initial_commitment {
2597 // Send a payment to move the channel forward
2598 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2601 // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output.
2602 // We'll keep this commitment transaction to broadcast once it's revoked.
2603 let revoked_local_txn = get_local_commitment_txn!(nodes[0], channel_id);
2604 assert_eq!(revoked_local_txn.len(), 1);
2605 let revoked_commitment_tx = &revoked_local_txn[0];
2607 // Send another payment, now revoking the previous commitment tx
2608 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2610 let justice_tx = persisters[1].justice_tx(funding_txo, &revoked_commitment_tx.txid()).unwrap();
2611 check_spends!(justice_tx, revoked_commitment_tx);
2613 mine_transactions(&nodes[1], &[revoked_commitment_tx, &justice_tx]);
2614 mine_transactions(&nodes[0], &[revoked_commitment_tx, &justice_tx]);
2616 check_added_monitors!(nodes[1], 1);
2617 check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false,
2618 &[nodes[0].node.get_our_node_id()], 100_000);
2619 get_announce_close_broadcast_events(&nodes, 1, 0);
2621 check_added_monitors!(nodes[0], 1);
2622 check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false,
2623 &[nodes[1].node.get_our_node_id()], 100_000);
2625 // Check that the justice tx has sent the revoked output value to nodes[1]
2626 let monitor = get_monitor!(nodes[1], channel_id);
2627 let total_claimable_balance = monitor.get_claimable_balances().iter().fold(0, |sum, balance| {
2629 channelmonitor::Balance::ClaimableAwaitingConfirmations { amount_satoshis, .. } => sum + amount_satoshis,
2630 _ => panic!("Unexpected balance type"),
2633 // On the first commitment, node[1]'s balance was below dust so it didn't have an output
2634 let node1_channel_balance = if broadcast_initial_commitment { 0 } else { revoked_commitment_tx.output[0].value };
2635 let expected_claimable_balance = node1_channel_balance + justice_tx.output[0].value;
2636 assert_eq!(total_claimable_balance, expected_claimable_balance);
2641 fn claim_htlc_outputs_shared_tx() {
2642 // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
2643 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2644 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2645 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2646 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2647 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2649 // Create some new channel:
2650 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2652 // Rebalance the network to generate htlc in the two directions
2653 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2654 // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx
2655 let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2656 let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2658 // Get the will-be-revoked local txn from node[0]
2659 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2660 assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
2661 assert_eq!(revoked_local_txn[0].input.len(), 1);
2662 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
2663 assert_eq!(revoked_local_txn[1].input.len(), 1);
2664 assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2665 assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2666 check_spends!(revoked_local_txn[1], revoked_local_txn[0]);
2668 //Revoke the old state
2669 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2672 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2673 check_added_monitors!(nodes[0], 1);
2674 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2675 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2676 check_added_monitors!(nodes[1], 1);
2677 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2678 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2679 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2681 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2682 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2684 assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
2685 check_spends!(node_txn[0], revoked_local_txn[0]);
2687 let mut witness_lens = BTreeSet::new();
2688 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2689 witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
2690 witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
2691 assert_eq!(witness_lens.len(), 3);
2692 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2693 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2694 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2696 // Finally, mine the penalty transaction and check that we get an HTLC failure after
2697 // ANTI_REORG_DELAY confirmations.
2698 mine_transaction(&nodes[1], &node_txn[0]);
2699 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2700 expect_payment_failed!(nodes[1], payment_hash_2, false);
2702 get_announce_close_broadcast_events(&nodes, 0, 1);
2703 assert_eq!(nodes[0].node.list_channels().len(), 0);
2704 assert_eq!(nodes[1].node.list_channels().len(), 0);
2708 fn claim_htlc_outputs_single_tx() {
2709 // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
2710 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2711 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2712 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2713 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2714 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2716 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2718 // Rebalance the network to generate htlc in the two directions
2719 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2720 // node[0] is gonna to revoke an old state thus node[1] should be able to claim both offered/received HTLC outputs on top of commitment tx, but this
2721 // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
2722 let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2723 let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2725 // Get the will-be-revoked local txn from node[0]
2726 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2728 //Revoke the old state
2729 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2732 confirm_transaction_at(&nodes[0], &revoked_local_txn[0], 100);
2733 check_added_monitors!(nodes[0], 1);
2734 confirm_transaction_at(&nodes[1], &revoked_local_txn[0], 100);
2735 check_added_monitors!(nodes[1], 1);
2736 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2737 let mut events = nodes[0].node.get_and_clear_pending_events();
2738 expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
2739 match events.last().unwrap() {
2740 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2741 _ => panic!("Unexpected event"),
2744 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2745 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2747 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcast();
2749 // Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
2750 assert_eq!(node_txn[0].input.len(), 1);
2751 check_spends!(node_txn[0], chan_1.3);
2752 assert_eq!(node_txn[1].input.len(), 1);
2753 let witness_script = node_txn[1].input[0].witness.last().unwrap();
2754 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
2755 check_spends!(node_txn[1], node_txn[0]);
2757 // Filter out any non justice transactions.
2758 node_txn.retain(|tx| tx.input[0].previous_output.txid == revoked_local_txn[0].txid());
2759 assert!(node_txn.len() > 3);
2761 assert_eq!(node_txn[0].input.len(), 1);
2762 assert_eq!(node_txn[1].input.len(), 1);
2763 assert_eq!(node_txn[2].input.len(), 1);
2765 check_spends!(node_txn[0], revoked_local_txn[0]);
2766 check_spends!(node_txn[1], revoked_local_txn[0]);
2767 check_spends!(node_txn[2], revoked_local_txn[0]);
2769 let mut witness_lens = BTreeSet::new();
2770 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2771 witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
2772 witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
2773 assert_eq!(witness_lens.len(), 3);
2774 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2775 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2776 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2778 // Finally, mine the penalty transactions and check that we get an HTLC failure after
2779 // ANTI_REORG_DELAY confirmations.
2780 mine_transaction(&nodes[1], &node_txn[0]);
2781 mine_transaction(&nodes[1], &node_txn[1]);
2782 mine_transaction(&nodes[1], &node_txn[2]);
2783 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2784 expect_payment_failed!(nodes[1], payment_hash_2, false);
2786 get_announce_close_broadcast_events(&nodes, 0, 1);
2787 assert_eq!(nodes[0].node.list_channels().len(), 0);
2788 assert_eq!(nodes[1].node.list_channels().len(), 0);
2792 fn test_htlc_on_chain_success() {
2793 // Test that in case of a unilateral close onchain, we detect the state of output and pass
2794 // the preimage backward accordingly. So here we test that ChannelManager is
2795 // broadcasting the right event to other nodes in payment path.
2796 // We test with two HTLCs simultaneously as that was not handled correctly in the past.
2797 // A --------------------> B ----------------------> C (preimage)
2798 // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
2799 // commitment transaction was broadcast.
2800 // Then, B should learn the preimage from said transactions, attempting to claim backwards
2802 // B should be able to claim via preimage if A then broadcasts its local tx.
2803 // Finally, when A sees B's latest local commitment transaction it should be able to claim
2804 // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
2805 // PaymentSent event).
2807 let chanmon_cfgs = create_chanmon_cfgs(3);
2808 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2809 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2810 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2812 // Create some initial channels
2813 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2814 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2816 // Ensure all nodes are at the same height
2817 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
2818 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
2819 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
2820 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
2822 // Rebalance the network a bit by relaying one payment through all the channels...
2823 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2824 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2826 let (our_payment_preimage, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2827 let (our_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2829 // Broadcast legit commitment tx from C on B's chain
2830 // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
2831 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
2832 assert_eq!(commitment_tx.len(), 1);
2833 check_spends!(commitment_tx[0], chan_2.3);
2834 nodes[2].node.claim_funds(our_payment_preimage);
2835 expect_payment_claimed!(nodes[2], payment_hash_1, 3_000_000);
2836 nodes[2].node.claim_funds(our_payment_preimage_2);
2837 expect_payment_claimed!(nodes[2], payment_hash_2, 3_000_000);
2838 check_added_monitors!(nodes[2], 2);
2839 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2840 assert!(updates.update_add_htlcs.is_empty());
2841 assert!(updates.update_fail_htlcs.is_empty());
2842 assert!(updates.update_fail_malformed_htlcs.is_empty());
2843 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2845 mine_transaction(&nodes[2], &commitment_tx[0]);
2846 check_closed_broadcast!(nodes[2], true);
2847 check_added_monitors!(nodes[2], 1);
2848 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2849 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 2 (2 * HTLC-Success tx)
2850 assert_eq!(node_txn.len(), 2);
2851 check_spends!(node_txn[0], commitment_tx[0]);
2852 check_spends!(node_txn[1], commitment_tx[0]);
2853 assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2854 assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2855 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2856 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2857 assert_eq!(node_txn[0].lock_time.0, 0);
2858 assert_eq!(node_txn[1].lock_time.0, 0);
2860 // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
2861 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![commitment_tx[0].clone(), node_txn[0].clone(), node_txn[1].clone()]));
2862 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2864 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2865 assert_eq!(added_monitors.len(), 1);
2866 assert_eq!(added_monitors[0].0.txid, chan_2.3.txid());
2867 added_monitors.clear();
2869 let forwarded_events = nodes[1].node.get_and_clear_pending_events();
2870 assert_eq!(forwarded_events.len(), 3);
2871 match forwarded_events[0] {
2872 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2873 _ => panic!("Unexpected event"),
2875 let chan_id = Some(chan_1.2);
2876 match forwarded_events[1] {
2877 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2878 assert_eq!(fee_earned_msat, Some(1000));
2879 assert_eq!(prev_channel_id, chan_id);
2880 assert_eq!(claim_from_onchain_tx, true);
2881 assert_eq!(next_channel_id, Some(chan_2.2));
2882 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2886 match forwarded_events[2] {
2887 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2888 assert_eq!(fee_earned_msat, Some(1000));
2889 assert_eq!(prev_channel_id, chan_id);
2890 assert_eq!(claim_from_onchain_tx, true);
2891 assert_eq!(next_channel_id, Some(chan_2.2));
2892 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2896 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2898 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2899 assert_eq!(added_monitors.len(), 2);
2900 assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
2901 assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
2902 added_monitors.clear();
2904 assert_eq!(events.len(), 3);
2906 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
2907 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
2909 match nodes_2_event {
2910 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
2911 _ => panic!("Unexpected event"),
2914 match nodes_0_event {
2915 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
2916 assert!(update_add_htlcs.is_empty());
2917 assert!(update_fail_htlcs.is_empty());
2918 assert_eq!(update_fulfill_htlcs.len(), 1);
2919 assert!(update_fail_malformed_htlcs.is_empty());
2920 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2922 _ => panic!("Unexpected event"),
2925 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
2927 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
2928 _ => panic!("Unexpected event"),
2931 macro_rules! check_tx_local_broadcast {
2932 ($node: expr, $htlc_offered: expr, $commitment_tx: expr) => { {
2933 let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
2934 assert_eq!(node_txn.len(), 2);
2935 // Node[1]: 2 * HTLC-timeout tx
2936 // Node[0]: 2 * HTLC-timeout tx
2937 check_spends!(node_txn[0], $commitment_tx);
2938 check_spends!(node_txn[1], $commitment_tx);
2939 assert_ne!(node_txn[0].lock_time.0, 0);
2940 assert_ne!(node_txn[1].lock_time.0, 0);
2942 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2943 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2944 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2945 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2947 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2948 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2949 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2950 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2955 // nodes[1] now broadcasts its own timeout-claim of the output that nodes[2] just claimed via success.
2956 check_tx_local_broadcast!(nodes[1], false, commitment_tx[0]);
2958 // Broadcast legit commitment tx from A on B's chain
2959 // Broadcast preimage tx by B on offered output from A commitment tx on A's chain
2960 let node_a_commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
2961 check_spends!(node_a_commitment_tx[0], chan_1.3);
2962 mine_transaction(&nodes[1], &node_a_commitment_tx[0]);
2963 check_closed_broadcast!(nodes[1], true);
2964 check_added_monitors!(nodes[1], 1);
2965 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2966 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2967 assert!(node_txn.len() == 1 || node_txn.len() == 3); // HTLC-Success, 2* RBF bumps of above HTLC txn
2968 let commitment_spend =
2969 if node_txn.len() == 1 {
2972 // Certain `ConnectStyle`s will cause RBF bumps of the previous HTLC transaction to be broadcast.
2973 // FullBlockViaListen
2974 if node_txn[0].input[0].previous_output.txid == node_a_commitment_tx[0].txid() {
2975 check_spends!(node_txn[1], commitment_tx[0]);
2976 check_spends!(node_txn[2], commitment_tx[0]);
2977 assert_ne!(node_txn[1].input[0].previous_output.vout, node_txn[2].input[0].previous_output.vout);
2980 check_spends!(node_txn[0], commitment_tx[0]);
2981 check_spends!(node_txn[1], commitment_tx[0]);
2982 assert_ne!(node_txn[0].input[0].previous_output.vout, node_txn[1].input[0].previous_output.vout);
2987 check_spends!(commitment_spend, node_a_commitment_tx[0]);
2988 assert_eq!(commitment_spend.input.len(), 2);
2989 assert_eq!(commitment_spend.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2990 assert_eq!(commitment_spend.input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2991 assert_eq!(commitment_spend.lock_time.0, nodes[1].best_block_info().1);
2992 assert!(commitment_spend.output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2993 // We don't bother to check that B can claim the HTLC output on its commitment tx here as
2994 // we already checked the same situation with A.
2996 // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
2997 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![node_a_commitment_tx[0].clone(), commitment_spend.clone()]));
2998 connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
2999 check_closed_broadcast!(nodes[0], true);
3000 check_added_monitors!(nodes[0], 1);
3001 let events = nodes[0].node.get_and_clear_pending_events();
3002 assert_eq!(events.len(), 5);
3003 let mut first_claimed = false;
3004 for event in events {
3006 Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3007 if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
3008 assert!(!first_claimed);
3009 first_claimed = true;
3011 assert_eq!(payment_preimage, our_payment_preimage_2);
3012 assert_eq!(payment_hash, payment_hash_2);
3015 Event::PaymentPathSuccessful { .. } => {},
3016 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
3017 _ => panic!("Unexpected event"),
3020 check_tx_local_broadcast!(nodes[0], true, node_a_commitment_tx[0]);
3023 fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
3024 // Test that in case of a unilateral close onchain, we detect the state of output and
3025 // timeout the HTLC backward accordingly. So here we test that ChannelManager is
3026 // broadcasting the right event to other nodes in payment path.
3027 // A ------------------> B ----------------------> C (timeout)
3028 // B's commitment tx C's commitment tx
3030 // B's HTLC timeout tx B's timeout tx
3032 let chanmon_cfgs = create_chanmon_cfgs(3);
3033 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3034 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3035 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3036 *nodes[0].connect_style.borrow_mut() = connect_style;
3037 *nodes[1].connect_style.borrow_mut() = connect_style;
3038 *nodes[2].connect_style.borrow_mut() = connect_style;
3040 // Create some intial channels
3041 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
3042 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3044 // Rebalance the network a bit by relaying one payment thorugh all the channels...
3045 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3046 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3048 let (_payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
3050 // Broadcast legit commitment tx from C on B's chain
3051 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
3052 check_spends!(commitment_tx[0], chan_2.3);
3053 nodes[2].node.fail_htlc_backwards(&payment_hash);
3054 check_added_monitors!(nodes[2], 0);
3055 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }]);
3056 check_added_monitors!(nodes[2], 1);
3058 let events = nodes[2].node.get_and_clear_pending_msg_events();
3059 assert_eq!(events.len(), 1);
3061 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
3062 assert!(update_add_htlcs.is_empty());
3063 assert!(!update_fail_htlcs.is_empty());
3064 assert!(update_fulfill_htlcs.is_empty());
3065 assert!(update_fail_malformed_htlcs.is_empty());
3066 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
3068 _ => panic!("Unexpected event"),
3070 mine_transaction(&nodes[2], &commitment_tx[0]);
3071 check_closed_broadcast!(nodes[2], true);
3072 check_added_monitors!(nodes[2], 1);
3073 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3074 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
3075 assert_eq!(node_txn.len(), 0);
3077 // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
3078 // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
3079 mine_transaction(&nodes[1], &commitment_tx[0]);
3080 check_closed_event!(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false
3081 , [nodes[2].node.get_our_node_id()], 100000);
3082 connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
3084 let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
3085 if nodes[1].connect_style.borrow().skips_blocks() {
3086 assert_eq!(txn.len(), 1);
3088 assert_eq!(txn.len(), 3); // Two extra fee bumps for timeout transaction
3090 txn.iter().for_each(|tx| check_spends!(tx, commitment_tx[0]));
3091 assert_eq!(txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3095 mine_transaction(&nodes[1], &timeout_tx);
3096 check_added_monitors!(nodes[1], 1);
3097 check_closed_broadcast!(nodes[1], true);
3099 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3101 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
3102 check_added_monitors!(nodes[1], 1);
3103 let events = nodes[1].node.get_and_clear_pending_msg_events();
3104 assert_eq!(events.len(), 1);
3106 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
3107 assert!(update_add_htlcs.is_empty());
3108 assert!(!update_fail_htlcs.is_empty());
3109 assert!(update_fulfill_htlcs.is_empty());
3110 assert!(update_fail_malformed_htlcs.is_empty());
3111 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3113 _ => panic!("Unexpected event"),
3116 // Broadcast legit commitment tx from B on A's chain
3117 let commitment_tx = get_local_commitment_txn!(nodes[1], chan_1.2);
3118 check_spends!(commitment_tx[0], chan_1.3);
3120 mine_transaction(&nodes[0], &commitment_tx[0]);
3121 connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
3123 check_closed_broadcast!(nodes[0], true);
3124 check_added_monitors!(nodes[0], 1);
3125 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3126 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // 1 timeout tx
3127 assert_eq!(node_txn.len(), 1);
3128 check_spends!(node_txn[0], commitment_tx[0]);
3129 assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3133 fn test_htlc_on_chain_timeout() {
3134 do_test_htlc_on_chain_timeout(ConnectStyle::BestBlockFirstSkippingBlocks);
3135 do_test_htlc_on_chain_timeout(ConnectStyle::TransactionsFirstSkippingBlocks);
3136 do_test_htlc_on_chain_timeout(ConnectStyle::FullBlockViaListen);
3140 fn test_simple_commitment_revoked_fail_backward() {
3141 // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
3142 // and fail backward accordingly.
3144 let chanmon_cfgs = create_chanmon_cfgs(3);
3145 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3146 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3147 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3149 // Create some initial channels
3150 create_announced_chan_between_nodes(&nodes, 0, 1);
3151 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3153 let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3154 // Get the will-be-revoked local txn from nodes[2]
3155 let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3156 // Revoke the old state
3157 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3159 let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3161 mine_transaction(&nodes[1], &revoked_local_txn[0]);
3162 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3163 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3164 check_added_monitors!(nodes[1], 1);
3165 check_closed_broadcast!(nodes[1], true);
3167 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
3168 check_added_monitors!(nodes[1], 1);
3169 let events = nodes[1].node.get_and_clear_pending_msg_events();
3170 assert_eq!(events.len(), 1);
3172 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
3173 assert!(update_add_htlcs.is_empty());
3174 assert_eq!(update_fail_htlcs.len(), 1);
3175 assert!(update_fulfill_htlcs.is_empty());
3176 assert!(update_fail_malformed_htlcs.is_empty());
3177 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3179 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3180 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3181 expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_2.0.contents.short_channel_id, true);
3183 _ => panic!("Unexpected event"),
3187 fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
3188 // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
3189 // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
3190 // commitment transaction anymore.
3191 // To do this, we have the peer which will broadcast a revoked commitment transaction send
3192 // a number of update_fail/commitment_signed updates without ever sending the RAA in
3193 // response to our commitment_signed. This is somewhat misbehavior-y, though not
3194 // technically disallowed and we should probably handle it reasonably.
3195 // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
3196 // failed/fulfilled backwards must be in at least one of the latest two remote commitment
3198 // * Once we move it out of our holding cell/add it, we will immediately include it in a
3199 // commitment_signed (implying it will be in the latest remote commitment transaction).
3200 // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
3201 // and once they revoke the previous commitment transaction (allowing us to send a new
3202 // commitment_signed) we will be free to fail/fulfill the HTLC backwards.
3203 let chanmon_cfgs = create_chanmon_cfgs(3);
3204 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3205 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3206 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3208 // Create some initial channels
3209 create_announced_chan_between_nodes(&nodes, 0, 1);
3210 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3212 let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
3213 // Get the will-be-revoked local txn from nodes[2]
3214 let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3215 assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
3216 // Revoke the old state
3217 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3219 let value = if use_dust {
3220 // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
3221 // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
3222 nodes[2].node.per_peer_state.read().unwrap().get(&nodes[1].node.get_our_node_id())
3223 .unwrap().lock().unwrap().channel_by_id.get(&chan_2.2).unwrap().context().holder_dust_limit_satoshis * 1000
3226 let (_, first_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3227 let (_, second_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3228 let (_, third_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3230 nodes[2].node.fail_htlc_backwards(&first_payment_hash);
3231 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
3232 check_added_monitors!(nodes[2], 1);
3233 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3234 assert!(updates.update_add_htlcs.is_empty());
3235 assert!(updates.update_fulfill_htlcs.is_empty());
3236 assert!(updates.update_fail_malformed_htlcs.is_empty());
3237 assert_eq!(updates.update_fail_htlcs.len(), 1);
3238 assert!(updates.update_fee.is_none());
3239 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3240 let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
3241 // Drop the last RAA from 3 -> 2
3243 nodes[2].node.fail_htlc_backwards(&second_payment_hash);
3244 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: second_payment_hash }]);
3245 check_added_monitors!(nodes[2], 1);
3246 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3247 assert!(updates.update_add_htlcs.is_empty());
3248 assert!(updates.update_fulfill_htlcs.is_empty());
3249 assert!(updates.update_fail_malformed_htlcs.is_empty());
3250 assert_eq!(updates.update_fail_htlcs.len(), 1);
3251 assert!(updates.update_fee.is_none());
3252 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3253 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3254 check_added_monitors!(nodes[1], 1);
3255 // Note that nodes[1] is in AwaitingRAA, so won't send a CS
3256 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3257 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3258 check_added_monitors!(nodes[2], 1);
3260 nodes[2].node.fail_htlc_backwards(&third_payment_hash);
3261 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: third_payment_hash }]);
3262 check_added_monitors!(nodes[2], 1);
3263 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3264 assert!(updates.update_add_htlcs.is_empty());
3265 assert!(updates.update_fulfill_htlcs.is_empty());
3266 assert!(updates.update_fail_malformed_htlcs.is_empty());
3267 assert_eq!(updates.update_fail_htlcs.len(), 1);
3268 assert!(updates.update_fee.is_none());
3269 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3270 // At this point first_payment_hash has dropped out of the latest two commitment
3271 // transactions that nodes[1] is tracking...
3272 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3273 check_added_monitors!(nodes[1], 1);
3274 // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
3275 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3276 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3277 check_added_monitors!(nodes[2], 1);
3279 // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
3280 // on nodes[2]'s RAA.
3281 let (route, fourth_payment_hash, _, fourth_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 1000000);
3282 nodes[1].node.send_payment_with_route(&route, fourth_payment_hash,
3283 RecipientOnionFields::secret_only(fourth_payment_secret), PaymentId(fourth_payment_hash.0)).unwrap();
3284 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3285 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3286 check_added_monitors!(nodes[1], 0);
3289 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa);
3290 // One monitor for the new revocation preimage, no second on as we won't generate a new
3291 // commitment transaction for nodes[0] until process_pending_htlc_forwards().
3292 check_added_monitors!(nodes[1], 1);
3293 let events = nodes[1].node.get_and_clear_pending_events();
3294 assert_eq!(events.len(), 2);
3296 Event::PendingHTLCsForwardable { .. } => { },
3297 _ => panic!("Unexpected event"),
3300 Event::HTLCHandlingFailed { .. } => { },
3301 _ => panic!("Unexpected event"),
3303 // Deliberately don't process the pending fail-back so they all fail back at once after
3304 // block connection just like the !deliver_bs_raa case
3307 let mut failed_htlcs = HashSet::new();
3308 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3310 mine_transaction(&nodes[1], &revoked_local_txn[0]);
3311 check_added_monitors!(nodes[1], 1);
3312 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3314 let events = nodes[1].node.get_and_clear_pending_events();
3315 assert_eq!(events.len(), if deliver_bs_raa { 3 + nodes.len() - 1 } else { 4 + nodes.len() });
3317 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => { },
3318 _ => panic!("Unexepected event"),
3321 Event::PaymentPathFailed { ref payment_hash, .. } => {
3322 assert_eq!(*payment_hash, fourth_payment_hash);
3324 _ => panic!("Unexpected event"),
3327 Event::PaymentFailed { ref payment_hash, .. } => {
3328 assert_eq!(*payment_hash, fourth_payment_hash);
3330 _ => panic!("Unexpected event"),
3333 nodes[1].node.process_pending_htlc_forwards();
3334 check_added_monitors!(nodes[1], 1);
3336 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
3337 assert_eq!(events.len(), if deliver_bs_raa { 4 } else { 3 });
3340 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3341 match nodes_2_event {
3342 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
3343 assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
3344 assert_eq!(update_add_htlcs.len(), 1);
3345 assert!(update_fulfill_htlcs.is_empty());
3346 assert!(update_fail_htlcs.is_empty());
3347 assert!(update_fail_malformed_htlcs.is_empty());
3349 _ => panic!("Unexpected event"),
3353 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3354 match nodes_2_event {
3355 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id, ref data } }, node_id: _ } => {
3356 assert_eq!(channel_id, chan_2.2);
3357 assert_eq!(data.as_str(), "Channel closed because commitment or closing transaction was confirmed on chain.");
3359 _ => panic!("Unexpected event"),
3362 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
3363 match nodes_0_event {
3364 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref commitment_signed, .. } } => {
3365 assert!(update_add_htlcs.is_empty());
3366 assert_eq!(update_fail_htlcs.len(), 3);
3367 assert!(update_fulfill_htlcs.is_empty());
3368 assert!(update_fail_malformed_htlcs.is_empty());
3369 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3371 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3372 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]);
3373 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]);
3375 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3377 let events = nodes[0].node.get_and_clear_pending_events();
3378 assert_eq!(events.len(), 6);
3380 Event::PaymentPathFailed { ref payment_hash, ref failure, .. } => {
3381 assert!(failed_htlcs.insert(payment_hash.0));
3382 // If we delivered B's RAA we got an unknown preimage error, not something
3383 // that we should update our routing table for.
3384 if !deliver_bs_raa {
3385 if let PathFailure::OnPath { network_update: Some(_) } = failure { } else { panic!("Unexpected path failure") }
3388 _ => panic!("Unexpected event"),
3391 Event::PaymentFailed { ref payment_hash, .. } => {
3392 assert_eq!(*payment_hash, first_payment_hash);
3394 _ => panic!("Unexpected event"),
3397 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3398 assert!(failed_htlcs.insert(payment_hash.0));
3400 _ => panic!("Unexpected event"),
3403 Event::PaymentFailed { ref payment_hash, .. } => {
3404 assert_eq!(*payment_hash, second_payment_hash);
3406 _ => panic!("Unexpected event"),
3409 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3410 assert!(failed_htlcs.insert(payment_hash.0));
3412 _ => panic!("Unexpected event"),
3415 Event::PaymentFailed { ref payment_hash, .. } => {
3416 assert_eq!(*payment_hash, third_payment_hash);
3418 _ => panic!("Unexpected event"),
3421 _ => panic!("Unexpected event"),
3424 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
3426 MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
3427 _ => panic!("Unexpected event"),
3430 assert!(failed_htlcs.contains(&first_payment_hash.0));
3431 assert!(failed_htlcs.contains(&second_payment_hash.0));
3432 assert!(failed_htlcs.contains(&third_payment_hash.0));
3436 fn test_commitment_revoked_fail_backward_exhaustive_a() {
3437 do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
3438 do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
3439 do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
3440 do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
3444 fn test_commitment_revoked_fail_backward_exhaustive_b() {
3445 do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
3446 do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
3447 do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
3448 do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
3452 fn fail_backward_pending_htlc_upon_channel_failure() {
3453 let chanmon_cfgs = create_chanmon_cfgs(2);
3454 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3455 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3456 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3457 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
3459 // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack.
3461 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3462 nodes[0].node.send_payment_with_route(&route, payment_hash, RecipientOnionFields::secret_only(payment_secret),
3463 PaymentId(payment_hash.0)).unwrap();
3464 check_added_monitors!(nodes[0], 1);
3466 let payment_event = {
3467 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3468 assert_eq!(events.len(), 1);
3469 SendEvent::from_event(events.remove(0))
3471 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
3472 assert_eq!(payment_event.msgs.len(), 1);
3475 // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack.
3476 let (route, failed_payment_hash, _, failed_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3478 nodes[0].node.send_payment_with_route(&route, failed_payment_hash,
3479 RecipientOnionFields::secret_only(failed_payment_secret), PaymentId(failed_payment_hash.0)).unwrap();
3480 check_added_monitors!(nodes[0], 0);
3482 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3485 // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel.
3487 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 50_000);
3489 let secp_ctx = Secp256k1::new();
3490 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
3491 let current_height = nodes[1].node.best_block.read().unwrap().height() + 1;
3492 let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(
3493 &route.paths[0], 50_000, RecipientOnionFields::secret_only(payment_secret), current_height, &None).unwrap();
3494 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
3495 let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
3497 // Send a 0-msat update_add_htlc to fail the channel.
3498 let update_add_htlc = msgs::UpdateAddHTLC {
3504 onion_routing_packet,
3505 skimmed_fee_msat: None,
3507 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc);
3509 let events = nodes[0].node.get_and_clear_pending_events();
3510 assert_eq!(events.len(), 3);
3511 // Check that Alice fails backward the pending HTLC from the second payment.
3513 Event::PaymentPathFailed { payment_hash, .. } => {
3514 assert_eq!(payment_hash, failed_payment_hash);
3516 _ => panic!("Unexpected event"),
3519 Event::PaymentFailed { payment_hash, .. } => {
3520 assert_eq!(payment_hash, failed_payment_hash);
3522 _ => panic!("Unexpected event"),
3525 Event::ChannelClosed { reason: ClosureReason::ProcessingError { ref err }, .. } => {
3526 assert_eq!(err, "Remote side tried to send a 0-msat HTLC");
3528 _ => panic!("Unexpected event {:?}", events[1]),
3530 check_closed_broadcast!(nodes[0], true);
3531 check_added_monitors!(nodes[0], 1);
3535 fn test_htlc_ignore_latest_remote_commitment() {
3536 // Test that HTLC transactions spending the latest remote commitment transaction are simply
3537 // ignored if we cannot claim them. This originally tickled an invalid unwrap().
3538 let chanmon_cfgs = create_chanmon_cfgs(2);
3539 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3540 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3541 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3542 if *nodes[1].connect_style.borrow() == ConnectStyle::FullBlockViaListen {
3543 // We rely on the ability to connect a block redundantly, which isn't allowed via
3544 // `chain::Listen`, so we never run the test if we randomly get assigned that
3548 create_announced_chan_between_nodes(&nodes, 0, 1);
3550 route_payment(&nodes[0], &[&nodes[1]], 10000000);
3551 nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3552 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
3553 check_closed_broadcast!(nodes[0], true);
3554 check_added_monitors!(nodes[0], 1);
3555 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3557 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3558 assert_eq!(node_txn.len(), 3);
3559 assert_eq!(node_txn[0].txid(), node_txn[1].txid());
3561 let block = create_dummy_block(nodes[1].best_block_hash(), 42, vec![node_txn[0].clone(), node_txn[1].clone()]);
3562 connect_block(&nodes[1], &block);
3563 check_closed_broadcast!(nodes[1], true);
3564 check_added_monitors!(nodes[1], 1);
3565 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
3567 // Duplicate the connect_block call since this may happen due to other listeners
3568 // registering new transactions
3569 connect_block(&nodes[1], &block);
3573 fn test_force_close_fail_back() {
3574 // Check which HTLCs are failed-backwards on channel force-closure
3575 let chanmon_cfgs = create_chanmon_cfgs(3);
3576 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3577 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3578 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3579 create_announced_chan_between_nodes(&nodes, 0, 1);
3580 create_announced_chan_between_nodes(&nodes, 1, 2);
3582 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
3584 let mut payment_event = {
3585 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
3586 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
3587 check_added_monitors!(nodes[0], 1);
3589 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3590 assert_eq!(events.len(), 1);
3591 SendEvent::from_event(events.remove(0))
3594 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3595 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3597 expect_pending_htlcs_forwardable!(nodes[1]);
3599 let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3600 assert_eq!(events_2.len(), 1);
3601 payment_event = SendEvent::from_event(events_2.remove(0));
3602 assert_eq!(payment_event.msgs.len(), 1);
3604 check_added_monitors!(nodes[1], 1);
3605 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
3606 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
3607 check_added_monitors!(nodes[2], 1);
3608 let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3610 // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
3611 // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
3612 // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
3614 nodes[2].node.force_close_broadcasting_latest_txn(&payment_event.commitment_msg.channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3615 check_closed_broadcast!(nodes[2], true);
3616 check_added_monitors!(nodes[2], 1);
3617 check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3619 let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3620 // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
3621 // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
3622 // back to nodes[1] upon timeout otherwise.
3623 assert_eq!(node_txn.len(), 1);
3627 mine_transaction(&nodes[1], &tx);
3629 // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
3630 check_closed_broadcast!(nodes[1], true);
3631 check_added_monitors!(nodes[1], 1);
3632 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3634 // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
3636 get_monitor!(nodes[2], payment_event.commitment_msg.channel_id)
3637 .provide_payment_preimage(&our_payment_hash, &our_payment_preimage, &node_cfgs[2].tx_broadcaster, &LowerBoundedFeeEstimator::new(node_cfgs[2].fee_estimator), &node_cfgs[2].logger);
3639 mine_transaction(&nodes[2], &tx);
3640 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3641 assert_eq!(node_txn.len(), 1);
3642 assert_eq!(node_txn[0].input.len(), 1);
3643 assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
3644 assert_eq!(node_txn[0].lock_time.0, 0); // Must be an HTLC-Success
3645 assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
3647 check_spends!(node_txn[0], tx);
3651 fn test_dup_events_on_peer_disconnect() {
3652 // Test that if we receive a duplicative update_fulfill_htlc message after a reconnect we do
3653 // not generate a corresponding duplicative PaymentSent event. This did not use to be the case
3654 // as we used to generate the event immediately upon receipt of the payment preimage in the
3655 // update_fulfill_htlc message.
3657 let chanmon_cfgs = create_chanmon_cfgs(2);
3658 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3659 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3660 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3661 create_announced_chan_between_nodes(&nodes, 0, 1);
3663 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
3665 nodes[1].node.claim_funds(payment_preimage);
3666 expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
3667 check_added_monitors!(nodes[1], 1);
3668 let claim_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3669 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &claim_msgs.update_fulfill_htlcs[0]);
3670 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
3672 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3673 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3675 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3676 reconnect_args.pending_htlc_claims.0 = 1;
3677 reconnect_nodes(reconnect_args);
3678 expect_payment_path_successful!(nodes[0]);
3682 fn test_peer_disconnected_before_funding_broadcasted() {
3683 // Test that channels are closed with `ClosureReason::DisconnectedPeer` if the peer disconnects
3684 // before the funding transaction has been broadcasted.
3685 let chanmon_cfgs = create_chanmon_cfgs(2);
3686 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3687 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3688 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3690 // Open a channel between `nodes[0]` and `nodes[1]`, for which the funding transaction is never
3691 // broadcasted, even though it's created by `nodes[0]`.
3692 let expected_temporary_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
3693 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
3694 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
3695 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
3696 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
3698 let (temporary_channel_id, tx, _funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
3699 assert_eq!(temporary_channel_id, expected_temporary_channel_id);
3701 assert!(nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
3703 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
3704 assert_eq!(funding_created_msg.temporary_channel_id, expected_temporary_channel_id);
3706 // Even though the funding transaction is created by `nodes[0]`, the `FundingCreated` msg is
3707 // never sent to `nodes[1]`, and therefore the tx is never signed by either party nor
3710 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
3713 // Ensure that the channel is closed with `ClosureReason::DisconnectedPeer` when the peers are
3714 // disconnected before the funding transaction was broadcasted.
3715 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3716 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3718 check_closed_event!(&nodes[0], 1, ClosureReason::DisconnectedPeer, false
3719 , [nodes[1].node.get_our_node_id()], 1000000);
3720 check_closed_event!(&nodes[1], 1, ClosureReason::DisconnectedPeer, false
3721 , [nodes[0].node.get_our_node_id()], 1000000);
3725 fn test_simple_peer_disconnect() {
3726 // Test that we can reconnect when there are no lost messages
3727 let chanmon_cfgs = create_chanmon_cfgs(3);
3728 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3729 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3730 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3731 create_announced_chan_between_nodes(&nodes, 0, 1);
3732 create_announced_chan_between_nodes(&nodes, 1, 2);
3734 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3735 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3736 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3737 reconnect_args.send_channel_ready = (true, true);
3738 reconnect_nodes(reconnect_args);
3740 let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3741 let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3742 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
3743 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
3745 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3746 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3747 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3749 let (payment_preimage_3, payment_hash_3, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000);
3750 let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3751 let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3752 let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3754 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3755 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3757 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_preimage_3);
3758 fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_hash_5);
3760 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3761 reconnect_args.pending_cell_htlc_fails.0 = 1;
3762 reconnect_args.pending_cell_htlc_claims.0 = 1;
3763 reconnect_nodes(reconnect_args);
3765 let events = nodes[0].node.get_and_clear_pending_events();
3766 assert_eq!(events.len(), 4);
3768 Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3769 assert_eq!(payment_preimage, payment_preimage_3);
3770 assert_eq!(payment_hash, payment_hash_3);
3772 _ => panic!("Unexpected event"),
3775 Event::PaymentPathSuccessful { .. } => {},
3776 _ => panic!("Unexpected event"),
3779 Event::PaymentPathFailed { payment_hash, payment_failed_permanently, .. } => {
3780 assert_eq!(payment_hash, payment_hash_5);
3781 assert!(payment_failed_permanently);
3783 _ => panic!("Unexpected event"),
3786 Event::PaymentFailed { payment_hash, .. } => {
3787 assert_eq!(payment_hash, payment_hash_5);
3789 _ => panic!("Unexpected event"),
3792 check_added_monitors(&nodes[0], 1);
3794 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
3795 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
3798 fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken_lnd: bool) {
3799 // Test that we can reconnect when in-flight HTLC updates get dropped
3800 let chanmon_cfgs = create_chanmon_cfgs(2);
3801 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3802 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3803 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3805 let mut as_channel_ready = None;
3806 let channel_id = if messages_delivered == 0 {
3807 let (channel_ready, chan_id, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001);
3808 as_channel_ready = Some(channel_ready);
3809 // nodes[1] doesn't receive the channel_ready message (it'll be re-sent on reconnect)
3810 // Note that we store it so that if we're running with `simulate_broken_lnd` we can deliver
3811 // it before the channel_reestablish message.
3814 create_announced_chan_between_nodes(&nodes, 0, 1).2
3817 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
3819 let payment_event = {
3820 nodes[0].node.send_payment_with_route(&route, payment_hash_1,
3821 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
3822 check_added_monitors!(nodes[0], 1);
3824 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3825 assert_eq!(events.len(), 1);
3826 SendEvent::from_event(events.remove(0))
3828 assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
3830 if messages_delivered < 2 {
3831 // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
3833 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3834 if messages_delivered >= 3 {
3835 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
3836 check_added_monitors!(nodes[1], 1);
3837 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3839 if messages_delivered >= 4 {
3840 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3841 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3842 check_added_monitors!(nodes[0], 1);
3844 if messages_delivered >= 5 {
3845 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
3846 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3847 // No commitment_signed so get_event_msg's assert(len == 1) passes
3848 check_added_monitors!(nodes[0], 1);
3850 if messages_delivered >= 6 {
3851 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3852 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3853 check_added_monitors!(nodes[1], 1);
3860 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3861 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3862 if messages_delivered < 3 {
3863 if simulate_broken_lnd {
3864 // lnd has a long-standing bug where they send a channel_ready prior to a
3865 // channel_reestablish if you reconnect prior to channel_ready time.
3867 // Here we simulate that behavior, delivering a channel_ready immediately on
3868 // reconnect. Note that we don't bother skipping the now-duplicate channel_ready sent
3869 // in `reconnect_nodes` but we currently don't fail based on that.
3871 // See-also <https://github.com/lightningnetwork/lnd/issues/4006>
3872 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready.as_ref().unwrap().0);
3874 // Even if the channel_ready messages get exchanged, as long as nothing further was
3875 // received on either side, both sides will need to resend them.
3876 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3877 reconnect_args.send_channel_ready = (true, true);
3878 reconnect_args.pending_htlc_adds.1 = 1;
3879 reconnect_nodes(reconnect_args);
3880 } else if messages_delivered == 3 {
3881 // nodes[0] still wants its RAA + commitment_signed
3882 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3883 reconnect_args.pending_htlc_adds.0 = -1;
3884 reconnect_args.pending_raa.0 = true;
3885 reconnect_nodes(reconnect_args);
3886 } else if messages_delivered == 4 {
3887 // nodes[0] still wants its commitment_signed
3888 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3889 reconnect_args.pending_htlc_adds.0 = -1;
3890 reconnect_nodes(reconnect_args);
3891 } else if messages_delivered == 5 {
3892 // nodes[1] still wants its final RAA
3893 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3894 reconnect_args.pending_raa.1 = true;
3895 reconnect_nodes(reconnect_args);
3896 } else if messages_delivered == 6 {
3897 // Everything was delivered...
3898 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3901 let events_1 = nodes[1].node.get_and_clear_pending_events();
3902 if messages_delivered == 0 {
3903 assert_eq!(events_1.len(), 2);
3905 Event::ChannelReady { .. } => { },
3906 _ => panic!("Unexpected event"),
3909 Event::PendingHTLCsForwardable { .. } => { },
3910 _ => panic!("Unexpected event"),
3913 assert_eq!(events_1.len(), 1);
3915 Event::PendingHTLCsForwardable { .. } => { },
3916 _ => panic!("Unexpected event"),
3920 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3921 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3922 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3924 nodes[1].node.process_pending_htlc_forwards();
3926 let events_2 = nodes[1].node.get_and_clear_pending_events();
3927 assert_eq!(events_2.len(), 1);
3929 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
3930 assert_eq!(payment_hash_1, *payment_hash);
3931 assert_eq!(amount_msat, 1_000_000);
3932 assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
3933 assert_eq!(via_channel_id, Some(channel_id));
3935 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
3936 assert!(payment_preimage.is_none());
3937 assert_eq!(payment_secret_1, *payment_secret);
3939 _ => panic!("expected PaymentPurpose::InvoicePayment")
3942 _ => panic!("Unexpected event"),
3945 nodes[1].node.claim_funds(payment_preimage_1);
3946 check_added_monitors!(nodes[1], 1);
3947 expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
3949 let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
3950 assert_eq!(events_3.len(), 1);
3951 let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
3952 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
3953 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3954 assert!(updates.update_add_htlcs.is_empty());
3955 assert!(updates.update_fail_htlcs.is_empty());
3956 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3957 assert!(updates.update_fail_malformed_htlcs.is_empty());
3958 assert!(updates.update_fee.is_none());
3959 (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
3961 _ => panic!("Unexpected event"),
3964 if messages_delivered >= 1 {
3965 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc);
3967 let events_4 = nodes[0].node.get_and_clear_pending_events();
3968 assert_eq!(events_4.len(), 1);
3970 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
3971 assert_eq!(payment_preimage_1, *payment_preimage);
3972 assert_eq!(payment_hash_1, *payment_hash);
3974 _ => panic!("Unexpected event"),
3977 if messages_delivered >= 2 {
3978 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
3979 check_added_monitors!(nodes[0], 1);
3980 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3982 if messages_delivered >= 3 {
3983 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3984 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3985 check_added_monitors!(nodes[1], 1);
3987 if messages_delivered >= 4 {
3988 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed);
3989 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3990 // No commitment_signed so get_event_msg's assert(len == 1) passes
3991 check_added_monitors!(nodes[1], 1);
3993 if messages_delivered >= 5 {
3994 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3995 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3996 check_added_monitors!(nodes[0], 1);
4003 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4004 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4005 if messages_delivered < 2 {
4006 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4007 reconnect_args.pending_htlc_claims.0 = 1;
4008 reconnect_nodes(reconnect_args);
4009 if messages_delivered < 1 {
4010 expect_payment_sent!(nodes[0], payment_preimage_1);
4012 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4014 } else if messages_delivered == 2 {
4015 // nodes[0] still wants its RAA + commitment_signed
4016 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4017 reconnect_args.pending_htlc_adds.1 = -1;
4018 reconnect_args.pending_raa.1 = true;
4019 reconnect_nodes(reconnect_args);
4020 } else if messages_delivered == 3 {
4021 // nodes[0] still wants its commitment_signed
4022 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4023 reconnect_args.pending_htlc_adds.1 = -1;
4024 reconnect_nodes(reconnect_args);
4025 } else if messages_delivered == 4 {
4026 // nodes[1] still wants its final RAA
4027 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4028 reconnect_args.pending_raa.0 = true;
4029 reconnect_nodes(reconnect_args);
4030 } else if messages_delivered == 5 {
4031 // Everything was delivered...
4032 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4035 if messages_delivered == 1 || messages_delivered == 2 {
4036 expect_payment_path_successful!(nodes[0]);
4038 if messages_delivered <= 5 {
4039 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4040 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4042 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4044 if messages_delivered > 2 {
4045 expect_payment_path_successful!(nodes[0]);
4048 // Channel should still work fine...
4049 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4050 let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
4051 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4055 fn test_drop_messages_peer_disconnect_a() {
4056 do_test_drop_messages_peer_disconnect(0, true);
4057 do_test_drop_messages_peer_disconnect(0, false);
4058 do_test_drop_messages_peer_disconnect(1, false);
4059 do_test_drop_messages_peer_disconnect(2, false);
4063 fn test_drop_messages_peer_disconnect_b() {
4064 do_test_drop_messages_peer_disconnect(3, false);
4065 do_test_drop_messages_peer_disconnect(4, false);
4066 do_test_drop_messages_peer_disconnect(5, false);
4067 do_test_drop_messages_peer_disconnect(6, false);
4071 fn test_channel_ready_without_best_block_updated() {
4072 // Previously, if we were offline when a funding transaction was locked in, and then we came
4073 // back online, calling best_block_updated once followed by transactions_confirmed, we'd not
4074 // generate a channel_ready until a later best_block_updated. This tests that we generate the
4075 // channel_ready immediately instead.
4076 let chanmon_cfgs = create_chanmon_cfgs(2);
4077 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4078 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4079 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4080 *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
4082 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4084 let conf_height = nodes[0].best_block_info().1 + 1;
4085 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4086 let block_txn = [funding_tx];
4087 let conf_txn: Vec<_> = block_txn.iter().enumerate().collect();
4088 let conf_block_header = nodes[0].get_block_header(conf_height);
4089 nodes[0].node.transactions_confirmed(&conf_block_header, &conf_txn[..], conf_height);
4091 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4092 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4093 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4097 fn test_channel_monitor_skipping_block_when_channel_manager_is_leading() {
4098 let chanmon_cfgs = create_chanmon_cfgs(2);
4099 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4100 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4101 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4103 // Let channel_manager get ahead of chain_monitor by 1 block.
4104 // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4105 // in case where client calls block_connect on channel_manager first and then on chain_monitor.
4106 let height_1 = nodes[0].best_block_info().1 + 1;
4107 let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4109 nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4110 nodes[0].node.block_connected(&block_1, height_1);
4112 // Create channel, and it gets added to chain_monitor in funding_created.
4113 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4115 // Now, newly added channel_monitor in chain_monitor hasn't processed block_1,
4116 // but it's best_block is block_1, since that was populated by channel_manager, and channel_manager
4117 // was running ahead of chain_monitor at the time of funding_created.
4118 // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4119 // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4120 confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4121 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4123 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4124 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4125 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4129 fn test_channel_monitor_skipping_block_when_channel_manager_is_lagging() {
4130 let chanmon_cfgs = create_chanmon_cfgs(2);
4131 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4132 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4133 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4135 // Let chain_monitor get ahead of channel_manager by 1 block.
4136 // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4137 // in case where client calls block_connect on chain_monitor first and then on channel_manager.
4138 let height_1 = nodes[0].best_block_info().1 + 1;
4139 let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4141 nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4142 nodes[0].chain_monitor.chain_monitor.block_connected(&block_1, height_1);
4144 // Create channel, and it gets added to chain_monitor in funding_created.
4145 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4147 // channel_manager can't really skip block_1, it should get it eventually.
4148 nodes[0].node.block_connected(&block_1, height_1);
4150 // Now, newly added channel_monitor in chain_monitor hasn't processed block_1, it's best_block is
4151 // the block before block_1, since that was populated by channel_manager, and channel_manager was
4152 // running behind at the time of funding_created.
4153 // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4154 // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4155 confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4156 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4158 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4159 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4160 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4164 fn test_drop_messages_peer_disconnect_dual_htlc() {
4165 // Test that we can handle reconnecting when both sides of a channel have pending
4166 // commitment_updates when we disconnect.
4167 let chanmon_cfgs = create_chanmon_cfgs(2);
4168 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4169 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4170 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4171 create_announced_chan_between_nodes(&nodes, 0, 1);
4173 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
4175 // Now try to send a second payment which will fail to send
4176 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4177 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
4178 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
4179 check_added_monitors!(nodes[0], 1);
4181 let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
4182 assert_eq!(events_1.len(), 1);
4184 MessageSendEvent::UpdateHTLCs { .. } => {},
4185 _ => panic!("Unexpected event"),
4188 nodes[1].node.claim_funds(payment_preimage_1);
4189 expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
4190 check_added_monitors!(nodes[1], 1);
4192 let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
4193 assert_eq!(events_2.len(), 1);
4195 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
4196 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4197 assert!(update_add_htlcs.is_empty());
4198 assert_eq!(update_fulfill_htlcs.len(), 1);
4199 assert!(update_fail_htlcs.is_empty());
4200 assert!(update_fail_malformed_htlcs.is_empty());
4201 assert!(update_fee.is_none());
4203 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
4204 let events_3 = nodes[0].node.get_and_clear_pending_events();
4205 assert_eq!(events_3.len(), 1);
4207 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
4208 assert_eq!(*payment_preimage, payment_preimage_1);
4209 assert_eq!(*payment_hash, payment_hash_1);
4211 _ => panic!("Unexpected event"),
4214 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
4215 let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4216 // No commitment_signed so get_event_msg's assert(len == 1) passes
4217 check_added_monitors!(nodes[0], 1);
4219 _ => panic!("Unexpected event"),
4222 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4223 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4225 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
4226 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
4228 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4229 assert_eq!(reestablish_1.len(), 1);
4230 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
4231 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
4233 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4234 assert_eq!(reestablish_2.len(), 1);
4236 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4237 let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
4238 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4239 let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
4241 assert!(as_resp.0.is_none());
4242 assert!(bs_resp.0.is_none());
4244 assert!(bs_resp.1.is_none());
4245 assert!(bs_resp.2.is_none());
4247 assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
4249 assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
4250 assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
4251 assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
4252 assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
4253 assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
4254 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]);
4255 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed);
4256 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4257 // No commitment_signed so get_event_msg's assert(len == 1) passes
4258 check_added_monitors!(nodes[1], 1);
4260 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap());
4261 let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4262 assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
4263 assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
4264 assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
4265 assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
4266 assert!(bs_second_commitment_signed.update_fee.is_none());
4267 check_added_monitors!(nodes[1], 1);
4269 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
4270 let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4271 assert!(as_commitment_signed.update_add_htlcs.is_empty());
4272 assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
4273 assert!(as_commitment_signed.update_fail_htlcs.is_empty());
4274 assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
4275 assert!(as_commitment_signed.update_fee.is_none());
4276 check_added_monitors!(nodes[0], 1);
4278 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed);
4279 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4280 // No commitment_signed so get_event_msg's assert(len == 1) passes
4281 check_added_monitors!(nodes[0], 1);
4283 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed);
4284 let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4285 // No commitment_signed so get_event_msg's assert(len == 1) passes
4286 check_added_monitors!(nodes[1], 1);
4288 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
4289 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4290 check_added_monitors!(nodes[1], 1);
4292 expect_pending_htlcs_forwardable!(nodes[1]);
4294 let events_5 = nodes[1].node.get_and_clear_pending_events();
4295 assert_eq!(events_5.len(), 1);
4297 Event::PaymentClaimable { ref payment_hash, ref purpose, .. } => {
4298 assert_eq!(payment_hash_2, *payment_hash);
4300 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
4301 assert!(payment_preimage.is_none());
4302 assert_eq!(payment_secret_2, *payment_secret);
4304 _ => panic!("expected PaymentPurpose::InvoicePayment")
4307 _ => panic!("Unexpected event"),
4310 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack);
4311 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4312 check_added_monitors!(nodes[0], 1);
4314 expect_payment_path_successful!(nodes[0]);
4315 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4318 fn do_test_htlc_timeout(send_partial_mpp: bool) {
4319 // If the user fails to claim/fail an HTLC within the HTLC CLTV timeout we fail it for them
4320 // to avoid our counterparty failing the channel.
4321 let chanmon_cfgs = create_chanmon_cfgs(2);
4322 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4323 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4324 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4326 create_announced_chan_between_nodes(&nodes, 0, 1);
4328 let our_payment_hash = if send_partial_mpp {
4329 let (route, our_payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
4330 // Use the utility function send_payment_along_path to send the payment with MPP data which
4331 // indicates there are more HTLCs coming.
4332 let cur_height = CHAN_CONFIRM_DEPTH + 1; // route_payment calls send_payment, which adds 1 to the current height. So we do the same here to match.
4333 let payment_id = PaymentId([42; 32]);
4334 let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
4335 RecipientOnionFields::secret_only(payment_secret), payment_id, &route).unwrap();
4336 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
4337 RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id,
4338 &None, session_privs[0]).unwrap();
4339 check_added_monitors!(nodes[0], 1);
4340 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4341 assert_eq!(events.len(), 1);
4342 // Now do the relevant commitment_signed/RAA dances along the path, noting that the final
4343 // hop should *not* yet generate any PaymentClaimable event(s).
4344 pass_along_path(&nodes[0], &[&nodes[1]], 100000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
4347 route_payment(&nodes[0], &[&nodes[1]], 100000).1
4350 let mut block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
4351 connect_block(&nodes[0], &block);
4352 connect_block(&nodes[1], &block);
4353 let block_count = TEST_FINAL_CLTV + CHAN_CONFIRM_DEPTH + 2 - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS;
4354 for _ in CHAN_CONFIRM_DEPTH + 2..block_count {
4355 block.header.prev_blockhash = block.block_hash();
4356 connect_block(&nodes[0], &block);
4357 connect_block(&nodes[1], &block);
4360 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
4362 check_added_monitors!(nodes[1], 1);
4363 let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4364 assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
4365 assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
4366 assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
4367 assert!(htlc_timeout_updates.update_fee.is_none());
4369 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
4370 commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
4371 // 100_000 msat as u64, followed by the height at which we failed back above
4372 let mut expected_failure_data = (100_000 as u64).to_be_bytes().to_vec();
4373 expected_failure_data.extend_from_slice(&(block_count - 1).to_be_bytes());
4374 expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000 | 15, &expected_failure_data[..]);
4378 fn test_htlc_timeout() {
4379 do_test_htlc_timeout(true);
4380 do_test_htlc_timeout(false);
4383 fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
4384 // Tests that HTLCs in the holding cell are timed out after the requisite number of blocks.
4385 let chanmon_cfgs = create_chanmon_cfgs(3);
4386 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4387 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4388 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4389 create_announced_chan_between_nodes(&nodes, 0, 1);
4390 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4392 // Make sure all nodes are at the same starting height
4393 connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
4394 connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
4395 connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
4397 // Route a first payment to get the 1 -> 2 channel in awaiting_raa...
4398 let (route, first_payment_hash, _, first_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
4399 nodes[1].node.send_payment_with_route(&route, first_payment_hash,
4400 RecipientOnionFields::secret_only(first_payment_secret), PaymentId(first_payment_hash.0)).unwrap();
4401 assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
4402 check_added_monitors!(nodes[1], 1);
4404 // Now attempt to route a second payment, which should be placed in the holding cell
4405 let sending_node = if forwarded_htlc { &nodes[0] } else { &nodes[1] };
4406 let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(sending_node, nodes[2], 100000);
4407 sending_node.node.send_payment_with_route(&route, second_payment_hash,
4408 RecipientOnionFields::secret_only(second_payment_secret), PaymentId(second_payment_hash.0)).unwrap();
4410 check_added_monitors!(nodes[0], 1);
4411 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
4412 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
4413 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4414 expect_pending_htlcs_forwardable!(nodes[1]);
4416 check_added_monitors!(nodes[1], 0);
4418 connect_blocks(&nodes[1], TEST_FINAL_CLTV - LATENCY_GRACE_PERIOD_BLOCKS);
4419 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4420 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
4421 connect_blocks(&nodes[1], 1);
4424 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
4425 check_added_monitors!(nodes[1], 1);
4426 let fail_commit = nodes[1].node.get_and_clear_pending_msg_events();
4427 assert_eq!(fail_commit.len(), 1);
4428 match fail_commit[0] {
4429 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, ref commitment_signed, .. }, .. } => {
4430 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
4431 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, true, true);
4433 _ => unreachable!(),
4435 expect_payment_failed_with_update!(nodes[0], second_payment_hash, false, chan_2.0.contents.short_channel_id, false);
4437 expect_payment_failed!(nodes[1], second_payment_hash, false);
4442 fn test_holding_cell_htlc_add_timeouts() {
4443 do_test_holding_cell_htlc_add_timeouts(false);
4444 do_test_holding_cell_htlc_add_timeouts(true);
4447 macro_rules! check_spendable_outputs {
4448 ($node: expr, $keysinterface: expr) => {
4450 let mut events = $node.chain_monitor.chain_monitor.get_and_clear_pending_events();
4451 let mut txn = Vec::new();
4452 let mut all_outputs = Vec::new();
4453 let secp_ctx = Secp256k1::new();
4454 for event in events.drain(..) {
4456 Event::SpendableOutputs { mut outputs, channel_id: _ } => {
4457 for outp in outputs.drain(..) {
4458 txn.push($keysinterface.backing.spend_spendable_outputs(&[&outp], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &secp_ctx).unwrap());
4459 all_outputs.push(outp);
4462 _ => panic!("Unexpected event"),
4465 if all_outputs.len() > 1 {
4466 if let Ok(tx) = $keysinterface.backing.spend_spendable_outputs(&all_outputs.iter().map(|a| a).collect::<Vec<_>>(), Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &secp_ctx) {
4476 fn test_claim_sizeable_push_msat() {
4477 // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
4478 let chanmon_cfgs = create_chanmon_cfgs(2);
4479 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4480 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4481 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4483 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4484 nodes[1].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[0].node.get_our_node_id()).unwrap();
4485 check_closed_broadcast!(nodes[1], true);
4486 check_added_monitors!(nodes[1], 1);
4487 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
4488 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4489 assert_eq!(node_txn.len(), 1);
4490 check_spends!(node_txn[0], chan.3);
4491 assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
4493 mine_transaction(&nodes[1], &node_txn[0]);
4494 connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
4496 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4497 assert_eq!(spend_txn.len(), 1);
4498 assert_eq!(spend_txn[0].input.len(), 1);
4499 check_spends!(spend_txn[0], node_txn[0]);
4500 assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
4504 fn test_claim_on_remote_sizeable_push_msat() {
4505 // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4506 // to_remote output is encumbered by a P2WPKH
4507 let chanmon_cfgs = create_chanmon_cfgs(2);
4508 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4509 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4510 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4512 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4513 nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
4514 check_closed_broadcast!(nodes[0], true);
4515 check_added_monitors!(nodes[0], 1);
4516 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
4518 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4519 assert_eq!(node_txn.len(), 1);
4520 check_spends!(node_txn[0], chan.3);
4521 assert_eq!(node_txn[0].output.len(), 2); // We can't force trimming of to_remote output as channel_reserve_satoshis block us to do so at channel opening
4523 mine_transaction(&nodes[1], &node_txn[0]);
4524 check_closed_broadcast!(nodes[1], true);
4525 check_added_monitors!(nodes[1], 1);
4526 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4527 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4529 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4530 assert_eq!(spend_txn.len(), 1);
4531 check_spends!(spend_txn[0], node_txn[0]);
4535 fn test_claim_on_remote_revoked_sizeable_push_msat() {
4536 // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4537 // to_remote output is encumbered by a P2WPKH
4539 let chanmon_cfgs = create_chanmon_cfgs(2);
4540 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4541 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4542 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4544 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000);
4545 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4546 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
4547 assert_eq!(revoked_local_txn[0].input.len(), 1);
4548 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
4550 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4551 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4552 check_closed_broadcast!(nodes[1], true);
4553 check_added_monitors!(nodes[1], 1);
4554 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4556 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4557 mine_transaction(&nodes[1], &node_txn[0]);
4558 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4560 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4561 assert_eq!(spend_txn.len(), 3);
4562 check_spends!(spend_txn[0], revoked_local_txn[0]); // to_remote output on revoked remote commitment_tx
4563 check_spends!(spend_txn[1], node_txn[0]);
4564 check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[0]); // Both outputs
4568 fn test_static_spendable_outputs_preimage_tx() {
4569 let chanmon_cfgs = create_chanmon_cfgs(2);
4570 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4571 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4572 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4574 // Create some initial channels
4575 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4577 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
4579 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4580 assert_eq!(commitment_tx[0].input.len(), 1);
4581 assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4583 // Settle A's commitment tx on B's chain
4584 nodes[1].node.claim_funds(payment_preimage);
4585 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
4586 check_added_monitors!(nodes[1], 1);
4587 mine_transaction(&nodes[1], &commitment_tx[0]);
4588 check_added_monitors!(nodes[1], 1);
4589 let events = nodes[1].node.get_and_clear_pending_msg_events();
4591 MessageSendEvent::UpdateHTLCs { .. } => {},
4592 _ => panic!("Unexpected event"),
4595 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4596 _ => panic!("Unexepected event"),
4599 // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
4600 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: preimage tx
4601 assert_eq!(node_txn.len(), 1);
4602 check_spends!(node_txn[0], commitment_tx[0]);
4603 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4605 mine_transaction(&nodes[1], &node_txn[0]);
4606 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4607 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4609 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4610 assert_eq!(spend_txn.len(), 1);
4611 check_spends!(spend_txn[0], node_txn[0]);
4615 fn test_static_spendable_outputs_timeout_tx() {
4616 let chanmon_cfgs = create_chanmon_cfgs(2);
4617 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4618 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4619 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4621 // Create some initial channels
4622 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4624 // Rebalance the network a bit by relaying one payment through all the channels ...
4625 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
4627 let (_, our_payment_hash, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000);
4629 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4630 assert_eq!(commitment_tx[0].input.len(), 1);
4631 assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4633 // Settle A's commitment tx on B' chain
4634 mine_transaction(&nodes[1], &commitment_tx[0]);
4635 check_added_monitors!(nodes[1], 1);
4636 let events = nodes[1].node.get_and_clear_pending_msg_events();
4638 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4639 _ => panic!("Unexpected event"),
4641 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4643 // Check B's monitor was able to send back output descriptor event for timeout tx on A's commitment tx
4644 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4645 assert_eq!(node_txn.len(), 1); // ChannelMonitor: timeout tx
4646 check_spends!(node_txn[0], commitment_tx[0].clone());
4647 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4649 mine_transaction(&nodes[1], &node_txn[0]);
4650 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4651 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4652 expect_payment_failed!(nodes[1], our_payment_hash, false);
4654 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4655 assert_eq!(spend_txn.len(), 3); // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
4656 check_spends!(spend_txn[0], commitment_tx[0]);
4657 check_spends!(spend_txn[1], node_txn[0]);
4658 check_spends!(spend_txn[2], node_txn[0], commitment_tx[0]); // All outputs
4662 fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4663 let chanmon_cfgs = create_chanmon_cfgs(2);
4664 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4665 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4666 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4668 // Create some initial channels
4669 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4671 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4672 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4673 assert_eq!(revoked_local_txn[0].input.len(), 1);
4674 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4676 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4678 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4679 check_closed_broadcast!(nodes[1], true);
4680 check_added_monitors!(nodes[1], 1);
4681 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4683 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4684 assert_eq!(node_txn.len(), 1);
4685 assert_eq!(node_txn[0].input.len(), 2);
4686 check_spends!(node_txn[0], revoked_local_txn[0]);
4688 mine_transaction(&nodes[1], &node_txn[0]);
4689 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4691 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4692 assert_eq!(spend_txn.len(), 1);
4693 check_spends!(spend_txn[0], node_txn[0]);
4697 fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
4698 let mut chanmon_cfgs = create_chanmon_cfgs(2);
4699 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
4700 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4701 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4702 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4704 // Create some initial channels
4705 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4707 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4708 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4709 assert_eq!(revoked_local_txn[0].input.len(), 1);
4710 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4712 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4714 // A will generate HTLC-Timeout from revoked commitment tx
4715 mine_transaction(&nodes[0], &revoked_local_txn[0]);
4716 check_closed_broadcast!(nodes[0], true);
4717 check_added_monitors!(nodes[0], 1);
4718 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4719 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4721 let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4722 assert_eq!(revoked_htlc_txn.len(), 1);
4723 assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4724 assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4725 check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4726 assert_ne!(revoked_htlc_txn[0].lock_time.0, 0); // HTLC-Timeout
4728 // B will generate justice tx from A's revoked commitment/HTLC tx
4729 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4730 check_closed_broadcast!(nodes[1], true);
4731 check_added_monitors!(nodes[1], 1);
4732 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4734 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4735 assert_eq!(node_txn.len(), 2); // ChannelMonitor: bogus justice tx, justice tx on revoked outputs
4736 // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4737 // including the one already spent by revoked_htlc_txn[1]. That's OK, we'll spend with valid
4738 // transactions next...
4739 assert_eq!(node_txn[0].input.len(), 3);
4740 check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4742 assert_eq!(node_txn[1].input.len(), 2);
4743 check_spends!(node_txn[1], revoked_local_txn[0], revoked_htlc_txn[0]);
4744 if node_txn[1].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4745 assert_ne!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4747 assert_eq!(node_txn[1].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4748 assert_ne!(node_txn[1].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4751 mine_transaction(&nodes[1], &node_txn[1]);
4752 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4754 // Check B's ChannelMonitor was able to generate the right spendable output descriptor
4755 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4756 assert_eq!(spend_txn.len(), 1);
4757 assert_eq!(spend_txn[0].input.len(), 1);
4758 check_spends!(spend_txn[0], node_txn[1]);
4762 fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
4763 let mut chanmon_cfgs = create_chanmon_cfgs(2);
4764 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
4765 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4766 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4767 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4769 // Create some initial channels
4770 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4772 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4773 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
4774 assert_eq!(revoked_local_txn[0].input.len(), 1);
4775 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4777 // The to-be-revoked commitment tx should have one HTLC and one to_remote output
4778 assert_eq!(revoked_local_txn[0].output.len(), 2);
4780 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4782 // B will generate HTLC-Success from revoked commitment tx
4783 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4784 check_closed_broadcast!(nodes[1], true);
4785 check_added_monitors!(nodes[1], 1);
4786 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4787 let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4789 assert_eq!(revoked_htlc_txn.len(), 1);
4790 assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4791 assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4792 check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4794 // Check that the unspent (of two) outputs on revoked_local_txn[0] is a P2WPKH:
4795 let unspent_local_txn_output = revoked_htlc_txn[0].input[0].previous_output.vout as usize ^ 1;
4796 assert_eq!(revoked_local_txn[0].output[unspent_local_txn_output].script_pubkey.len(), 2 + 20); // P2WPKH
4798 // A will generate justice tx from B's revoked commitment/HTLC tx
4799 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4800 check_closed_broadcast!(nodes[0], true);
4801 check_added_monitors!(nodes[0], 1);
4802 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4804 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4805 assert_eq!(node_txn.len(), 2); // ChannelMonitor: justice tx on revoked commitment, justice tx on revoked HTLC-success
4807 // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4808 // including the one already spent by revoked_htlc_txn[0]. That's OK, we'll spend with valid
4809 // transactions next...
4810 assert_eq!(node_txn[0].input.len(), 2);
4811 check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4812 if node_txn[0].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4813 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4815 assert_eq!(node_txn[0].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4816 assert_eq!(node_txn[0].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4819 assert_eq!(node_txn[1].input.len(), 1);
4820 check_spends!(node_txn[1], revoked_htlc_txn[0]);
4822 mine_transaction(&nodes[0], &node_txn[1]);
4823 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
4825 // Note that nodes[0]'s tx_broadcaster is still locked, so if we get here the channelmonitor
4826 // didn't try to generate any new transactions.
4828 // Check A's ChannelMonitor was able to generate the right spendable output descriptor
4829 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
4830 assert_eq!(spend_txn.len(), 3);
4831 assert_eq!(spend_txn[0].input.len(), 1);
4832 check_spends!(spend_txn[0], revoked_local_txn[0]); // spending to_remote output from revoked local tx
4833 assert_ne!(spend_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4834 check_spends!(spend_txn[1], node_txn[1]); // spending justice tx output on the htlc success tx
4835 check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[1]); // Both outputs
4839 fn test_onchain_to_onchain_claim() {
4840 // Test that in case of channel closure, we detect the state of output and claim HTLC
4841 // on downstream peer's remote commitment tx.
4842 // First, have C claim an HTLC against its own latest commitment transaction.
4843 // Then, broadcast these to B, which should update the monitor downstream on the A<->B
4845 // Finally, check that B will claim the HTLC output if A's latest commitment transaction
4848 let chanmon_cfgs = create_chanmon_cfgs(3);
4849 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4850 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4851 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4853 // Create some initial channels
4854 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4855 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4857 // Ensure all nodes are at the same height
4858 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4859 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4860 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4861 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4863 // Rebalance the network a bit by relaying one payment through all the channels ...
4864 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4865 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4867 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
4868 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
4869 check_spends!(commitment_tx[0], chan_2.3);
4870 nodes[2].node.claim_funds(payment_preimage);
4871 expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
4872 check_added_monitors!(nodes[2], 1);
4873 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
4874 assert!(updates.update_add_htlcs.is_empty());
4875 assert!(updates.update_fail_htlcs.is_empty());
4876 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
4877 assert!(updates.update_fail_malformed_htlcs.is_empty());
4879 mine_transaction(&nodes[2], &commitment_tx[0]);
4880 check_closed_broadcast!(nodes[2], true);
4881 check_added_monitors!(nodes[2], 1);
4882 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4884 let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 1 (HTLC-Success tx)
4885 assert_eq!(c_txn.len(), 1);
4886 check_spends!(c_txn[0], commitment_tx[0]);
4887 assert_eq!(c_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4888 assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
4889 assert_eq!(c_txn[0].lock_time.0, 0); // Success tx
4891 // So we broadcast C's commitment tx and HTLC-Success on B's chain, we should successfully be able to extract preimage and update downstream monitor
4892 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![commitment_tx[0].clone(), c_txn[0].clone()]));
4893 check_added_monitors!(nodes[1], 1);
4894 let events = nodes[1].node.get_and_clear_pending_events();
4895 assert_eq!(events.len(), 2);
4897 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
4898 _ => panic!("Unexpected event"),
4901 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
4902 assert_eq!(fee_earned_msat, Some(1000));
4903 assert_eq!(prev_channel_id, Some(chan_1.2));
4904 assert_eq!(claim_from_onchain_tx, true);
4905 assert_eq!(next_channel_id, Some(chan_2.2));
4906 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
4908 _ => panic!("Unexpected event"),
4910 check_added_monitors!(nodes[1], 1);
4911 let mut msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4912 assert_eq!(msg_events.len(), 3);
4913 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut msg_events);
4914 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut msg_events);
4916 match nodes_2_event {
4917 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { .. }, node_id: _ } => {},
4918 _ => panic!("Unexpected event"),
4921 match nodes_0_event {
4922 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
4923 assert!(update_add_htlcs.is_empty());
4924 assert!(update_fail_htlcs.is_empty());
4925 assert_eq!(update_fulfill_htlcs.len(), 1);
4926 assert!(update_fail_malformed_htlcs.is_empty());
4927 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
4929 _ => panic!("Unexpected event"),
4932 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
4933 match msg_events[0] {
4934 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4935 _ => panic!("Unexpected event"),
4938 // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
4939 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4940 mine_transaction(&nodes[1], &commitment_tx[0]);
4941 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4942 let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4943 // ChannelMonitor: HTLC-Success tx
4944 assert_eq!(b_txn.len(), 1);
4945 check_spends!(b_txn[0], commitment_tx[0]);
4946 assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4947 assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
4948 assert_eq!(b_txn[0].lock_time.0, nodes[1].best_block_info().1); // Success tx
4950 check_closed_broadcast!(nodes[1], true);
4951 check_added_monitors!(nodes[1], 1);
4955 fn test_duplicate_payment_hash_one_failure_one_success() {
4956 // Topology : A --> B --> C --> D
4957 // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
4958 // Note that because C will refuse to generate two payment secrets for the same payment hash,
4959 // we forward one of the payments onwards to D.
4960 let chanmon_cfgs = create_chanmon_cfgs(4);
4961 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
4962 // When this test was written, the default base fee floated based on the HTLC count.
4963 // It is now fixed, so we simply set the fee to the expected value here.
4964 let mut config = test_default_channel_config();
4965 config.channel_config.forwarding_fee_base_msat = 196;
4966 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs,
4967 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
4968 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
4970 create_announced_chan_between_nodes(&nodes, 0, 1);
4971 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4972 create_announced_chan_between_nodes(&nodes, 2, 3);
4974 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4975 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4976 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4977 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4978 connect_blocks(&nodes[3], node_max_height - nodes[3].best_block_info().1);
4980 let (our_payment_preimage, duplicate_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 900_000);
4982 let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200, None).unwrap();
4983 // We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
4984 // script push size limit so that the below script length checks match
4985 // ACCEPTED_HTLC_SCRIPT_WEIGHT.
4986 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV - 40)
4987 .with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
4988 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000);
4989 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 800_000, duplicate_payment_hash, payment_secret);
4991 let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
4992 assert_eq!(commitment_txn[0].input.len(), 1);
4993 check_spends!(commitment_txn[0], chan_2.3);
4995 mine_transaction(&nodes[1], &commitment_txn[0]);
4996 check_closed_broadcast!(nodes[1], true);
4997 check_added_monitors!(nodes[1], 1);
4998 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
4999 connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
5001 let htlc_timeout_tx;
5002 { // Extract one of the two HTLC-Timeout transaction
5003 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5004 // ChannelMonitor: timeout tx * 2-or-3
5005 assert!(node_txn.len() == 2 || node_txn.len() == 3);
5007 check_spends!(node_txn[0], commitment_txn[0]);
5008 assert_eq!(node_txn[0].input.len(), 1);
5009 assert_eq!(node_txn[0].output.len(), 1);
5011 if node_txn.len() > 2 {
5012 check_spends!(node_txn[1], commitment_txn[0]);
5013 assert_eq!(node_txn[1].input.len(), 1);
5014 assert_eq!(node_txn[1].output.len(), 1);
5015 assert_eq!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5017 check_spends!(node_txn[2], commitment_txn[0]);
5018 assert_eq!(node_txn[2].input.len(), 1);
5019 assert_eq!(node_txn[2].output.len(), 1);
5020 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
5022 check_spends!(node_txn[1], commitment_txn[0]);
5023 assert_eq!(node_txn[1].input.len(), 1);
5024 assert_eq!(node_txn[1].output.len(), 1);
5025 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5028 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5029 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5030 // Assign htlc_timeout_tx to the forwarded HTLC (with value ~800 sats). The received HTLC
5031 // (with value 900 sats) will be claimed in the below `claim_funds` call.
5032 if node_txn.len() > 2 {
5033 assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5034 htlc_timeout_tx = if node_txn[2].output[0].value < 900 { node_txn[2].clone() } else { node_txn[0].clone() };
5036 htlc_timeout_tx = if node_txn[0].output[0].value < 900 { node_txn[1].clone() } else { node_txn[0].clone() };
5040 nodes[2].node.claim_funds(our_payment_preimage);
5041 expect_payment_claimed!(nodes[2], duplicate_payment_hash, 900_000);
5043 mine_transaction(&nodes[2], &commitment_txn[0]);
5044 check_added_monitors!(nodes[2], 2);
5045 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5046 let events = nodes[2].node.get_and_clear_pending_msg_events();
5048 MessageSendEvent::UpdateHTLCs { .. } => {},
5049 _ => panic!("Unexpected event"),
5052 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5053 _ => panic!("Unexepected event"),
5055 let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
5056 assert_eq!(htlc_success_txn.len(), 2); // ChannelMonitor: HTLC-Success txn (*2 due to 2-HTLC outputs)
5057 check_spends!(htlc_success_txn[0], commitment_txn[0]);
5058 check_spends!(htlc_success_txn[1], commitment_txn[0]);
5059 assert_eq!(htlc_success_txn[0].input.len(), 1);
5060 assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5061 assert_eq!(htlc_success_txn[1].input.len(), 1);
5062 assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5063 assert_ne!(htlc_success_txn[0].input[0].previous_output, htlc_success_txn[1].input[0].previous_output);
5064 assert_ne!(htlc_success_txn[1].input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
5066 mine_transaction(&nodes[1], &htlc_timeout_tx);
5067 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5068 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
5069 let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5070 assert!(htlc_updates.update_add_htlcs.is_empty());
5071 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
5072 let first_htlc_id = htlc_updates.update_fail_htlcs[0].htlc_id;
5073 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
5074 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
5075 check_added_monitors!(nodes[1], 1);
5077 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
5078 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5080 commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
5082 expect_payment_failed_with_update!(nodes[0], duplicate_payment_hash, false, chan_2.0.contents.short_channel_id, true);
5084 // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
5085 mine_transaction(&nodes[1], &htlc_success_txn[1]);
5086 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(196), true, true);
5087 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5088 assert!(updates.update_add_htlcs.is_empty());
5089 assert!(updates.update_fail_htlcs.is_empty());
5090 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
5091 assert_ne!(updates.update_fulfill_htlcs[0].htlc_id, first_htlc_id);
5092 assert!(updates.update_fail_malformed_htlcs.is_empty());
5093 check_added_monitors!(nodes[1], 1);
5095 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
5096 commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
5097 expect_payment_sent(&nodes[0], our_payment_preimage, None, true, true);
5101 fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5102 let chanmon_cfgs = create_chanmon_cfgs(2);
5103 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5104 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5105 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5107 // Create some initial channels
5108 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5110 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
5111 let local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
5112 assert_eq!(local_txn.len(), 1);
5113 assert_eq!(local_txn[0].input.len(), 1);
5114 check_spends!(local_txn[0], chan_1.3);
5116 // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
5117 nodes[1].node.claim_funds(payment_preimage);
5118 expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
5119 check_added_monitors!(nodes[1], 1);
5121 mine_transaction(&nodes[1], &local_txn[0]);
5122 check_added_monitors!(nodes[1], 1);
5123 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
5124 let events = nodes[1].node.get_and_clear_pending_msg_events();
5126 MessageSendEvent::UpdateHTLCs { .. } => {},
5127 _ => panic!("Unexpected event"),
5130 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5131 _ => panic!("Unexepected event"),
5134 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5135 assert_eq!(node_txn.len(), 1);
5136 assert_eq!(node_txn[0].input.len(), 1);
5137 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5138 check_spends!(node_txn[0], local_txn[0]);
5142 mine_transaction(&nodes[1], &node_tx);
5143 connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
5145 // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
5146 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5147 assert_eq!(spend_txn.len(), 1);
5148 assert_eq!(spend_txn[0].input.len(), 1);
5149 check_spends!(spend_txn[0], node_tx);
5150 assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5153 fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
5154 // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
5155 // unrevoked commitment transaction.
5156 // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
5157 // a remote RAA before they could be failed backwards (and combinations thereof).
5158 // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
5159 // use the same payment hashes.
5160 // Thus, we use a six-node network:
5165 // And test where C fails back to A/B when D announces its latest commitment transaction
5166 let chanmon_cfgs = create_chanmon_cfgs(6);
5167 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
5168 // When this test was written, the default base fee floated based on the HTLC count.
5169 // It is now fixed, so we simply set the fee to the expected value here.
5170 let mut config = test_default_channel_config();
5171 config.channel_config.forwarding_fee_base_msat = 196;
5172 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs,
5173 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
5174 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
5176 let _chan_0_2 = create_announced_chan_between_nodes(&nodes, 0, 2);
5177 let _chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
5178 let chan_2_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
5179 let chan_3_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
5180 let chan_3_5 = create_announced_chan_between_nodes(&nodes, 3, 5);
5182 // Rebalance and check output sanity...
5183 send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
5184 send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
5185 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 2);
5187 let ds_dust_limit = nodes[3].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
5188 .unwrap().lock().unwrap().channel_by_id.get(&chan_2_3.2).unwrap().context().holder_dust_limit_satoshis;
5190 let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5192 let (_, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5193 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5195 send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_1, nodes[5].node.create_inbound_payment_for_hash(payment_hash_1, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5197 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_2, nodes[5].node.create_inbound_payment_for_hash(payment_hash_2, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5199 let (_, payment_hash_3, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5201 let (_, payment_hash_4, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5202 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5204 send_along_route_with_secret(&nodes[1], route.clone(), &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_3, nodes[5].node.create_inbound_payment_for_hash(payment_hash_3, None, 7200, None).unwrap());
5206 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_4, nodes[5].node.create_inbound_payment_for_hash(payment_hash_4, None, 7200, None).unwrap());
5209 let (_, payment_hash_5, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5211 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5212 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], ds_dust_limit*1000, payment_hash_5, nodes[5].node.create_inbound_payment_for_hash(payment_hash_5, None, 7200, None).unwrap()); // not added < dust limit + HTLC tx fee
5215 let (_, payment_hash_6, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], ds_dust_limit*1000); // not added < dust limit + HTLC tx fee
5217 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5218 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[2], &nodes[3], &nodes[5]]], 1000000, payment_hash_6, nodes[5].node.create_inbound_payment_for_hash(payment_hash_6, None, 7200, None).unwrap());
5220 // Double-check that six of the new HTLC were added
5221 // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
5222 // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
5223 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2).len(), 1);
5224 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 8);
5226 // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
5227 // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5228 nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5229 nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5230 nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5231 nodes[4].node.fail_htlc_backwards(&payment_hash_6);
5232 check_added_monitors!(nodes[4], 0);
5234 let failed_destinations = vec![
5235 HTLCDestination::FailedPayment { payment_hash: payment_hash_1 },
5236 HTLCDestination::FailedPayment { payment_hash: payment_hash_3 },
5237 HTLCDestination::FailedPayment { payment_hash: payment_hash_5 },
5238 HTLCDestination::FailedPayment { payment_hash: payment_hash_6 },
5240 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[4], failed_destinations);
5241 check_added_monitors!(nodes[4], 1);
5243 let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
5244 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]);
5245 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]);
5246 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]);
5247 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]);
5248 commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
5250 // Fail 3rd below-dust and 7th above-dust HTLCs
5251 nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5252 nodes[5].node.fail_htlc_backwards(&payment_hash_4);
5253 check_added_monitors!(nodes[5], 0);
5255 let failed_destinations_2 = vec![
5256 HTLCDestination::FailedPayment { payment_hash: payment_hash_2 },
5257 HTLCDestination::FailedPayment { payment_hash: payment_hash_4 },
5259 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[5], failed_destinations_2);
5260 check_added_monitors!(nodes[5], 1);
5262 let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
5263 nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]);
5264 nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]);
5265 commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
5267 let ds_prev_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5269 // After 4 and 2 removes respectively above in nodes[4] and nodes[5], nodes[3] should receive 6 PaymentForwardedFailed events
5270 let failed_destinations_3 = vec![
5271 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5272 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5273 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5274 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5275 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5276 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5278 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations_3);
5279 check_added_monitors!(nodes[3], 1);
5280 let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
5281 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]);
5282 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]);
5283 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]);
5284 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]);
5285 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]);
5286 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]);
5287 if deliver_last_raa {
5288 commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
5290 let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
5293 // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
5294 // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
5295 // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
5296 // propagated back to A/B yet (and D has two unrevoked commitment transactions).
5298 // We now broadcast the latest commitment transaction, which *should* result in failures for
5299 // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
5300 // the non-broadcast above-dust HTLCs.
5302 // Alternatively, we may broadcast the previous commitment transaction, which should only
5303 // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
5304 let ds_last_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5306 if announce_latest {
5307 mine_transaction(&nodes[2], &ds_last_commitment_tx[0]);
5309 mine_transaction(&nodes[2], &ds_prev_commitment_tx[0]);
5311 let events = nodes[2].node.get_and_clear_pending_events();
5312 let close_event = if deliver_last_raa {
5313 assert_eq!(events.len(), 2 + 6);
5314 events.last().clone().unwrap()
5316 assert_eq!(events.len(), 1);
5317 events.last().clone().unwrap()
5320 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
5321 _ => panic!("Unexpected event"),
5324 connect_blocks(&nodes[2], ANTI_REORG_DELAY - 1);
5325 check_closed_broadcast!(nodes[2], true);
5326 if deliver_last_raa {
5327 expect_pending_htlcs_forwardable_from_events!(nodes[2], events[0..1], true);
5329 let expected_destinations: Vec<HTLCDestination> = repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(3).collect();
5330 expect_htlc_handling_failed_destinations!(nodes[2].node.get_and_clear_pending_events(), expected_destinations);
5332 let expected_destinations: Vec<HTLCDestination> = if announce_latest {
5333 repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(9).collect()
5335 repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(6).collect()
5338 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], expected_destinations);
5340 check_added_monitors!(nodes[2], 3);
5342 let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
5343 assert_eq!(cs_msgs.len(), 2);
5344 let mut a_done = false;
5345 for msg in cs_msgs {
5347 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
5348 // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
5349 // should be failed-backwards here.
5350 let target = if *node_id == nodes[0].node.get_our_node_id() {
5351 // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
5352 for htlc in &updates.update_fail_htlcs {
5353 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 6 || if announce_latest { htlc.htlc_id == 3 || htlc.htlc_id == 5 } else { false });
5355 assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
5360 // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
5361 for htlc in &updates.update_fail_htlcs {
5362 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
5364 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
5365 assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
5368 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
5369 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]);
5370 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]);
5371 if announce_latest {
5372 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]);
5373 if *node_id == nodes[0].node.get_our_node_id() {
5374 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]);
5377 commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
5379 _ => panic!("Unexpected event"),
5383 let as_events = nodes[0].node.get_and_clear_pending_events();
5384 assert_eq!(as_events.len(), if announce_latest { 10 } else { 6 });
5385 let mut as_failds = HashSet::new();
5386 let mut as_updates = 0;
5387 for event in as_events.iter() {
5388 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5389 assert!(as_failds.insert(*payment_hash));
5390 if *payment_hash != payment_hash_2 {
5391 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5393 assert!(!payment_failed_permanently);
5395 if let PathFailure::OnPath { network_update: Some(_) } = failure {
5398 } else if let &Event::PaymentFailed { .. } = event {
5399 } else { panic!("Unexpected event"); }
5401 assert!(as_failds.contains(&payment_hash_1));
5402 assert!(as_failds.contains(&payment_hash_2));
5403 if announce_latest {
5404 assert!(as_failds.contains(&payment_hash_3));
5405 assert!(as_failds.contains(&payment_hash_5));
5407 assert!(as_failds.contains(&payment_hash_6));
5409 let bs_events = nodes[1].node.get_and_clear_pending_events();
5410 assert_eq!(bs_events.len(), if announce_latest { 8 } else { 6 });
5411 let mut bs_failds = HashSet::new();
5412 let mut bs_updates = 0;
5413 for event in bs_events.iter() {
5414 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5415 assert!(bs_failds.insert(*payment_hash));
5416 if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
5417 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5419 assert!(!payment_failed_permanently);
5421 if let PathFailure::OnPath { network_update: Some(_) } = failure {
5424 } else if let &Event::PaymentFailed { .. } = event {
5425 } else { panic!("Unexpected event"); }
5427 assert!(bs_failds.contains(&payment_hash_1));
5428 assert!(bs_failds.contains(&payment_hash_2));
5429 if announce_latest {
5430 assert!(bs_failds.contains(&payment_hash_4));
5432 assert!(bs_failds.contains(&payment_hash_5));
5434 // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
5435 // get a NetworkUpdate. A should have gotten 4 HTLCs which were failed-back due to
5436 // unknown-preimage-etc, B should have gotten 2. Thus, in the
5437 // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2 NetworkUpdates.
5438 assert_eq!(as_updates, if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
5439 assert_eq!(bs_updates, if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
5443 fn test_fail_backwards_latest_remote_announce_a() {
5444 do_test_fail_backwards_unrevoked_remote_announce(false, true);
5448 fn test_fail_backwards_latest_remote_announce_b() {
5449 do_test_fail_backwards_unrevoked_remote_announce(true, true);
5453 fn test_fail_backwards_previous_remote_announce() {
5454 do_test_fail_backwards_unrevoked_remote_announce(false, false);
5455 // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
5456 // tested for in test_commitment_revoked_fail_backward_exhaustive()
5460 fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
5461 let chanmon_cfgs = create_chanmon_cfgs(2);
5462 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5463 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5464 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5466 // Create some initial channels
5467 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5469 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5470 let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5471 assert_eq!(local_txn[0].input.len(), 1);
5472 check_spends!(local_txn[0], chan_1.3);
5474 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5475 mine_transaction(&nodes[0], &local_txn[0]);
5476 check_closed_broadcast!(nodes[0], true);
5477 check_added_monitors!(nodes[0], 1);
5478 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5479 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5481 let htlc_timeout = {
5482 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5483 assert_eq!(node_txn.len(), 1);
5484 assert_eq!(node_txn[0].input.len(), 1);
5485 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5486 check_spends!(node_txn[0], local_txn[0]);
5490 mine_transaction(&nodes[0], &htlc_timeout);
5491 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5492 expect_payment_failed!(nodes[0], our_payment_hash, false);
5494 // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5495 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5496 assert_eq!(spend_txn.len(), 3);
5497 check_spends!(spend_txn[0], local_txn[0]);
5498 assert_eq!(spend_txn[1].input.len(), 1);
5499 check_spends!(spend_txn[1], htlc_timeout);
5500 assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5501 assert_eq!(spend_txn[2].input.len(), 2);
5502 check_spends!(spend_txn[2], local_txn[0], htlc_timeout);
5503 assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5504 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5508 fn test_key_derivation_params() {
5509 // This test is a copy of test_dynamic_spendable_outputs_local_htlc_timeout_tx, with a key
5510 // manager rotation to test that `channel_keys_id` returned in
5511 // [`SpendableOutputDescriptor::DelayedPaymentOutput`] let us re-derive the channel key set to
5512 // then derive a `delayed_payment_key`.
5514 let chanmon_cfgs = create_chanmon_cfgs(3);
5516 // We manually create the node configuration to backup the seed.
5517 let seed = [42; 32];
5518 let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5519 let chain_monitor = test_utils::TestChainMonitor::new(Some(&chanmon_cfgs[0].chain_source), &chanmon_cfgs[0].tx_broadcaster, &chanmon_cfgs[0].logger, &chanmon_cfgs[0].fee_estimator, &chanmon_cfgs[0].persister, &keys_manager);
5520 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &chanmon_cfgs[0].logger));
5521 let scorer = RwLock::new(test_utils::TestScorer::new());
5522 let router = test_utils::TestRouter::new(network_graph.clone(), &scorer);
5523 let node = NodeCfg { chain_source: &chanmon_cfgs[0].chain_source, logger: &chanmon_cfgs[0].logger, tx_broadcaster: &chanmon_cfgs[0].tx_broadcaster, fee_estimator: &chanmon_cfgs[0].fee_estimator, router, chain_monitor, keys_manager: &keys_manager, network_graph, node_seed: seed, override_init_features: alloc::rc::Rc::new(core::cell::RefCell::new(None)) };
5524 let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5525 node_cfgs.remove(0);
5526 node_cfgs.insert(0, node);
5528 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5529 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5531 // Create some initial channels
5532 // Create a dummy channel to advance index by one and thus test re-derivation correctness
5534 let chan_0 = create_announced_chan_between_nodes(&nodes, 0, 2);
5535 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5536 assert_ne!(chan_0.3.output[0].script_pubkey, chan_1.3.output[0].script_pubkey);
5538 // Ensure all nodes are at the same height
5539 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5540 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5541 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5542 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5544 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5545 let local_txn_0 = get_local_commitment_txn!(nodes[0], chan_0.2);
5546 let local_txn_1 = get_local_commitment_txn!(nodes[0], chan_1.2);
5547 assert_eq!(local_txn_1[0].input.len(), 1);
5548 check_spends!(local_txn_1[0], chan_1.3);
5550 // We check funding pubkey are unique
5551 let (from_0_funding_key_0, from_0_funding_key_1) = (PublicKey::from_slice(&local_txn_0[0].input[0].witness.to_vec()[3][2..35]), PublicKey::from_slice(&local_txn_0[0].input[0].witness.to_vec()[3][36..69]));
5552 let (from_1_funding_key_0, from_1_funding_key_1) = (PublicKey::from_slice(&local_txn_1[0].input[0].witness.to_vec()[3][2..35]), PublicKey::from_slice(&local_txn_1[0].input[0].witness.to_vec()[3][36..69]));
5553 if from_0_funding_key_0 == from_1_funding_key_0
5554 || from_0_funding_key_0 == from_1_funding_key_1
5555 || from_0_funding_key_1 == from_1_funding_key_0
5556 || from_0_funding_key_1 == from_1_funding_key_1 {
5557 panic!("Funding pubkeys aren't unique");
5560 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5561 mine_transaction(&nodes[0], &local_txn_1[0]);
5562 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5563 check_closed_broadcast!(nodes[0], true);
5564 check_added_monitors!(nodes[0], 1);
5565 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5567 let htlc_timeout = {
5568 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5569 assert_eq!(node_txn.len(), 1);
5570 assert_eq!(node_txn[0].input.len(), 1);
5571 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5572 check_spends!(node_txn[0], local_txn_1[0]);
5576 mine_transaction(&nodes[0], &htlc_timeout);
5577 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5578 expect_payment_failed!(nodes[0], our_payment_hash, false);
5580 // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5581 let new_keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5582 let spend_txn = check_spendable_outputs!(nodes[0], new_keys_manager);
5583 assert_eq!(spend_txn.len(), 3);
5584 check_spends!(spend_txn[0], local_txn_1[0]);
5585 assert_eq!(spend_txn[1].input.len(), 1);
5586 check_spends!(spend_txn[1], htlc_timeout);
5587 assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5588 assert_eq!(spend_txn[2].input.len(), 2);
5589 check_spends!(spend_txn[2], local_txn_1[0], htlc_timeout);
5590 assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5591 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5595 fn test_static_output_closing_tx() {
5596 let chanmon_cfgs = create_chanmon_cfgs(2);
5597 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5598 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5599 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5601 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5603 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5604 let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
5606 mine_transaction(&nodes[0], &closing_tx);
5607 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
5608 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5610 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5611 assert_eq!(spend_txn.len(), 1);
5612 check_spends!(spend_txn[0], closing_tx);
5614 mine_transaction(&nodes[1], &closing_tx);
5615 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
5616 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5618 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5619 assert_eq!(spend_txn.len(), 1);
5620 check_spends!(spend_txn[0], closing_tx);
5623 fn do_htlc_claim_local_commitment_only(use_dust: bool) {
5624 let chanmon_cfgs = create_chanmon_cfgs(2);
5625 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5626 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5627 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5628 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5630 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3_000_000 });
5632 // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
5633 // present in B's local commitment transaction, but none of A's commitment transactions.
5634 nodes[1].node.claim_funds(payment_preimage);
5635 check_added_monitors!(nodes[1], 1);
5636 expect_payment_claimed!(nodes[1], payment_hash, if use_dust { 50000 } else { 3_000_000 });
5638 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5639 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
5640 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
5642 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5643 check_added_monitors!(nodes[0], 1);
5644 let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5645 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5646 check_added_monitors!(nodes[1], 1);
5648 let starting_block = nodes[1].best_block_info();
5649 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5650 for _ in starting_block.1 + 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + starting_block.1 + 2 {
5651 connect_block(&nodes[1], &block);
5652 block.header.prev_blockhash = block.block_hash();
5654 test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
5655 check_closed_broadcast!(nodes[1], true);
5656 check_added_monitors!(nodes[1], 1);
5657 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
5660 fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
5661 let chanmon_cfgs = create_chanmon_cfgs(2);
5662 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5663 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5664 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5665 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5667 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if use_dust { 50000 } else { 3000000 });
5668 nodes[0].node.send_payment_with_route(&route, payment_hash,
5669 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
5670 check_added_monitors!(nodes[0], 1);
5672 let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5674 // As far as A is concerned, the HTLC is now present only in the latest remote commitment
5675 // transaction, however it is not in A's latest local commitment, so we can just broadcast that
5676 // to "time out" the HTLC.
5678 let starting_block = nodes[1].best_block_info();
5679 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5681 for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + starting_block.1 + 2 {
5682 connect_block(&nodes[0], &block);
5683 block.header.prev_blockhash = block.block_hash();
5685 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5686 check_closed_broadcast!(nodes[0], true);
5687 check_added_monitors!(nodes[0], 1);
5688 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5691 fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
5692 let chanmon_cfgs = create_chanmon_cfgs(3);
5693 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5694 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5695 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5696 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5698 // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
5699 // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
5700 // Also optionally test that we *don't* fail the channel in case the commitment transaction was
5701 // actually revoked.
5702 let htlc_value = if use_dust { 50000 } else { 3000000 };
5703 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5704 nodes[1].node.fail_htlc_backwards(&our_payment_hash);
5705 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
5706 check_added_monitors!(nodes[1], 1);
5708 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5709 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
5710 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5711 check_added_monitors!(nodes[0], 1);
5712 let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5713 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5714 check_added_monitors!(nodes[1], 1);
5715 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1);
5716 check_added_monitors!(nodes[1], 1);
5717 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
5719 if check_revoke_no_close {
5720 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
5721 check_added_monitors!(nodes[0], 1);
5724 let starting_block = nodes[1].best_block_info();
5725 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5726 for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 2 {
5727 connect_block(&nodes[0], &block);
5728 block.header.prev_blockhash = block.block_hash();
5730 if !check_revoke_no_close {
5731 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5732 check_closed_broadcast!(nodes[0], true);
5733 check_added_monitors!(nodes[0], 1);
5734 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5736 expect_payment_failed!(nodes[0], our_payment_hash, true);
5740 // Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
5741 // There are only a few cases to test here:
5742 // * its not really normative behavior, but we test that below-dust HTLCs "included" in
5743 // broadcastable commitment transactions result in channel closure,
5744 // * its included in an unrevoked-but-previous remote commitment transaction,
5745 // * its included in the latest remote or local commitment transactions.
5746 // We test each of the three possible commitment transactions individually and use both dust and
5748 // Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
5749 // assume they are handled the same across all six cases, as both outbound and inbound failures are
5750 // tested for at least one of the cases in other tests.
5752 fn htlc_claim_single_commitment_only_a() {
5753 do_htlc_claim_local_commitment_only(true);
5754 do_htlc_claim_local_commitment_only(false);
5756 do_htlc_claim_current_remote_commitment_only(true);
5757 do_htlc_claim_current_remote_commitment_only(false);
5761 fn htlc_claim_single_commitment_only_b() {
5762 do_htlc_claim_previous_remote_commitment_only(true, false);
5763 do_htlc_claim_previous_remote_commitment_only(false, false);
5764 do_htlc_claim_previous_remote_commitment_only(true, true);
5765 do_htlc_claim_previous_remote_commitment_only(false, true);
5770 fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
5771 let chanmon_cfgs = create_chanmon_cfgs(2);
5772 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5773 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5774 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5775 // Force duplicate randomness for every get-random call
5776 for node in nodes.iter() {
5777 *node.keys_manager.override_random_bytes.lock().unwrap() = Some([0; 32]);
5780 // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
5781 let channel_value_satoshis=10000;
5782 let push_msat=10001;
5783 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
5784 let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5785 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5786 get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
5788 // Create a second channel with the same random values. This used to panic due to a colliding
5789 // channel_id, but now panics due to a colliding outbound SCID alias.
5790 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5794 fn bolt2_open_channel_sending_node_checks_part2() {
5795 let chanmon_cfgs = create_chanmon_cfgs(2);
5796 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5797 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5798 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5800 // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
5801 let channel_value_satoshis=2^24;
5802 let push_msat=10001;
5803 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5805 // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
5806 let channel_value_satoshis=10000;
5807 // Test when push_msat is equal to 1000 * funding_satoshis.
5808 let push_msat=1000*channel_value_satoshis+1;
5809 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_err());
5811 // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
5812 let channel_value_satoshis=10000;
5813 let push_msat=10001;
5814 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).is_ok()); //Create a valid channel
5815 let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5816 assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
5818 // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
5819 // Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
5820 assert!(node0_to_1_send_open_channel.channel_flags<=1);
5822 // BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
5823 assert!(BREAKDOWN_TIMEOUT>0);
5824 assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
5826 // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
5827 let chain_hash=genesis_block(Network::Testnet).header.block_hash();
5828 assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
5830 // BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
5831 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
5832 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
5833 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
5834 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_point.serialize()).is_ok());
5835 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
5839 fn bolt2_open_channel_sane_dust_limit() {
5840 let chanmon_cfgs = create_chanmon_cfgs(2);
5841 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5842 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5843 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5845 let channel_value_satoshis=1000000;
5846 let push_msat=10001;
5847 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None).unwrap();
5848 let mut node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5849 node0_to_1_send_open_channel.dust_limit_satoshis = 547;
5850 node0_to_1_send_open_channel.channel_reserve_satoshis = 100001;
5852 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5853 let events = nodes[1].node.get_and_clear_pending_msg_events();
5854 let err_msg = match events[0] {
5855 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
5858 _ => panic!("Unexpected event"),
5860 assert_eq!(err_msg.data, "dust_limit_satoshis (547) is greater than the implementation limit (546)");
5863 // Test that if we fail to send an HTLC that is being freed from the holding cell, and the HTLC
5864 // originated from our node, its failure is surfaced to the user. We trigger this failure to
5865 // free the HTLC by increasing our fee while the HTLC is in the holding cell such that the HTLC
5866 // is no longer affordable once it's freed.
5868 fn test_fail_holding_cell_htlc_upon_free() {
5869 let chanmon_cfgs = create_chanmon_cfgs(2);
5870 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5871 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5872 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5873 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5875 // First nodes[0] generates an update_fee, setting the channel's
5876 // pending_update_fee.
5878 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5879 *feerate_lock += 20;
5881 nodes[0].node.timer_tick_occurred();
5882 check_added_monitors!(nodes[0], 1);
5884 let events = nodes[0].node.get_and_clear_pending_msg_events();
5885 assert_eq!(events.len(), 1);
5886 let (update_msg, commitment_signed) = match events[0] {
5887 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5888 (update_fee.as_ref(), commitment_signed)
5890 _ => panic!("Unexpected event"),
5893 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5895 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5896 let channel_reserve = chan_stat.channel_reserve_msat;
5897 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5898 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5900 // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5901 let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
5902 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
5904 // Send a payment which passes reserve checks but gets stuck in the holding cell.
5905 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
5906 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
5907 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5908 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
5910 // Flush the pending fee update.
5911 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5912 let (as_revoke_and_ack, _) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5913 check_added_monitors!(nodes[1], 1);
5914 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
5915 check_added_monitors!(nodes[0], 1);
5917 // Upon receipt of the RAA, there will be an attempt to resend the holding cell
5918 // HTLC, but now that the fee has been raised the payment will now fail, causing
5919 // us to surface its failure to the user.
5920 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5921 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
5922 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", chan.2), 1);
5924 // Check that the payment failed to be sent out.
5925 let events = nodes[0].node.get_and_clear_pending_events();
5926 assert_eq!(events.len(), 2);
5928 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
5929 assert_eq!(PaymentId(our_payment_hash.0), *payment_id.as_ref().unwrap());
5930 assert_eq!(our_payment_hash.clone(), *payment_hash);
5931 assert_eq!(*payment_failed_permanently, false);
5932 assert_eq!(*short_channel_id, Some(route.paths[0].hops[0].short_channel_id));
5934 _ => panic!("Unexpected event"),
5937 &Event::PaymentFailed { ref payment_hash, .. } => {
5938 assert_eq!(our_payment_hash.clone(), *payment_hash);
5940 _ => panic!("Unexpected event"),
5944 // Test that if multiple HTLCs are released from the holding cell and one is
5945 // valid but the other is no longer valid upon release, the valid HTLC can be
5946 // successfully completed while the other one fails as expected.
5948 fn test_free_and_fail_holding_cell_htlcs() {
5949 let chanmon_cfgs = create_chanmon_cfgs(2);
5950 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5951 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5952 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5953 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5955 // First nodes[0] generates an update_fee, setting the channel's
5956 // pending_update_fee.
5958 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5959 *feerate_lock += 200;
5961 nodes[0].node.timer_tick_occurred();
5962 check_added_monitors!(nodes[0], 1);
5964 let events = nodes[0].node.get_and_clear_pending_msg_events();
5965 assert_eq!(events.len(), 1);
5966 let (update_msg, commitment_signed) = match events[0] {
5967 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5968 (update_fee.as_ref(), commitment_signed)
5970 _ => panic!("Unexpected event"),
5973 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5975 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5976 let channel_reserve = chan_stat.channel_reserve_msat;
5977 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5978 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5980 // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5982 let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features) - amt_1;
5983 let (route_1, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_1);
5984 let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_2);
5986 // Send 2 payments which pass reserve checks but get stuck in the holding cell.
5987 nodes[0].node.send_payment_with_route(&route_1, payment_hash_1,
5988 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
5989 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5990 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1);
5991 let payment_id_2 = PaymentId(nodes[0].keys_manager.get_secure_random_bytes());
5992 nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
5993 RecipientOnionFields::secret_only(payment_secret_2), payment_id_2).unwrap();
5994 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5995 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1 + amt_2);
5997 // Flush the pending fee update.
5998 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5999 let (revoke_and_ack, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6000 check_added_monitors!(nodes[1], 1);
6001 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_and_ack);
6002 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6003 check_added_monitors!(nodes[0], 2);
6005 // Upon receipt of the RAA, there will be an attempt to resend the holding cell HTLCs,
6006 // but now that the fee has been raised the second payment will now fail, causing us
6007 // to surface its failure to the user. The first payment should succeed.
6008 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6009 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6010 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", chan.2), 1);
6012 // Check that the second payment failed to be sent out.
6013 let events = nodes[0].node.get_and_clear_pending_events();
6014 assert_eq!(events.len(), 2);
6016 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
6017 assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
6018 assert_eq!(payment_hash_2.clone(), *payment_hash);
6019 assert_eq!(*payment_failed_permanently, false);
6020 assert_eq!(*short_channel_id, Some(route_2.paths[0].hops[0].short_channel_id));
6022 _ => panic!("Unexpected event"),
6025 &Event::PaymentFailed { ref payment_hash, .. } => {
6026 assert_eq!(payment_hash_2.clone(), *payment_hash);
6028 _ => panic!("Unexpected event"),
6031 // Complete the first payment and the RAA from the fee update.
6032 let (payment_event, send_raa_event) = {
6033 let mut msgs = nodes[0].node.get_and_clear_pending_msg_events();
6034 assert_eq!(msgs.len(), 2);
6035 (SendEvent::from_event(msgs.remove(0)), msgs.remove(0))
6037 let raa = match send_raa_event {
6038 MessageSendEvent::SendRevokeAndACK { msg, .. } => msg,
6039 _ => panic!("Unexpected event"),
6041 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6042 check_added_monitors!(nodes[1], 1);
6043 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6044 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6045 let events = nodes[1].node.get_and_clear_pending_events();
6046 assert_eq!(events.len(), 1);
6048 Event::PendingHTLCsForwardable { .. } => {},
6049 _ => panic!("Unexpected event"),
6051 nodes[1].node.process_pending_htlc_forwards();
6052 let events = nodes[1].node.get_and_clear_pending_events();
6053 assert_eq!(events.len(), 1);
6055 Event::PaymentClaimable { .. } => {},
6056 _ => panic!("Unexpected event"),
6058 nodes[1].node.claim_funds(payment_preimage_1);
6059 check_added_monitors!(nodes[1], 1);
6060 expect_payment_claimed!(nodes[1], payment_hash_1, amt_1);
6062 let update_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6063 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msgs.update_fulfill_htlcs[0]);
6064 commitment_signed_dance!(nodes[0], nodes[1], update_msgs.commitment_signed, false, true);
6065 expect_payment_sent!(nodes[0], payment_preimage_1);
6068 // Test that if we fail to forward an HTLC that is being freed from the holding cell that the
6069 // HTLC is failed backwards. We trigger this failure to forward the freed HTLC by increasing
6070 // our fee while the HTLC is in the holding cell such that the HTLC is no longer affordable
6073 fn test_fail_holding_cell_htlc_upon_free_multihop() {
6074 let chanmon_cfgs = create_chanmon_cfgs(3);
6075 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6076 // Avoid having to include routing fees in calculations
6077 let mut config = test_default_channel_config();
6078 config.channel_config.forwarding_fee_base_msat = 0;
6079 config.channel_config.forwarding_fee_proportional_millionths = 0;
6080 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
6081 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6082 let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6083 let chan_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
6085 // First nodes[1] generates an update_fee, setting the channel's
6086 // pending_update_fee.
6088 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
6089 *feerate_lock += 20;
6091 nodes[1].node.timer_tick_occurred();
6092 check_added_monitors!(nodes[1], 1);
6094 let events = nodes[1].node.get_and_clear_pending_msg_events();
6095 assert_eq!(events.len(), 1);
6096 let (update_msg, commitment_signed) = match events[0] {
6097 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6098 (update_fee.as_ref(), commitment_signed)
6100 _ => panic!("Unexpected event"),
6103 nodes[2].node.handle_update_fee(&nodes[1].node.get_our_node_id(), update_msg.unwrap());
6105 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan_0_1.2);
6106 let channel_reserve = chan_stat.channel_reserve_msat;
6107 let feerate = get_feerate!(nodes[0], nodes[1], chan_0_1.2);
6108 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_0_1.2);
6110 // Send a payment which passes reserve checks but gets stuck in the holding cell.
6111 let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6112 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], max_can_send);
6113 let payment_event = {
6114 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6115 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6116 check_added_monitors!(nodes[0], 1);
6118 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6119 assert_eq!(events.len(), 1);
6121 SendEvent::from_event(events.remove(0))
6123 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6124 check_added_monitors!(nodes[1], 0);
6125 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6126 expect_pending_htlcs_forwardable!(nodes[1]);
6128 chan_stat = get_channel_value_stat!(nodes[1], nodes[2], chan_1_2.2);
6129 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
6131 // Flush the pending fee update.
6132 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
6133 let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6134 check_added_monitors!(nodes[2], 1);
6135 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &raa);
6136 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &commitment_signed);
6137 check_added_monitors!(nodes[1], 2);
6139 // A final RAA message is generated to finalize the fee update.
6140 let events = nodes[1].node.get_and_clear_pending_msg_events();
6141 assert_eq!(events.len(), 1);
6143 let raa_msg = match &events[0] {
6144 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => {
6147 _ => panic!("Unexpected event"),
6150 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa_msg);
6151 check_added_monitors!(nodes[2], 1);
6152 assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
6154 // nodes[1]'s ChannelManager will now signal that we have HTLC forwards to process.
6155 let process_htlc_forwards_event = nodes[1].node.get_and_clear_pending_events();
6156 assert_eq!(process_htlc_forwards_event.len(), 2);
6157 match &process_htlc_forwards_event[0] {
6158 &Event::PendingHTLCsForwardable { .. } => {},
6159 _ => panic!("Unexpected event"),
6162 // In response, we call ChannelManager's process_pending_htlc_forwards
6163 nodes[1].node.process_pending_htlc_forwards();
6164 check_added_monitors!(nodes[1], 1);
6166 // This causes the HTLC to be failed backwards.
6167 let fail_event = nodes[1].node.get_and_clear_pending_msg_events();
6168 assert_eq!(fail_event.len(), 1);
6169 let (fail_msg, commitment_signed) = match &fail_event[0] {
6170 &MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6171 assert_eq!(updates.update_add_htlcs.len(), 0);
6172 assert_eq!(updates.update_fulfill_htlcs.len(), 0);
6173 assert_eq!(updates.update_fail_malformed_htlcs.len(), 0);
6174 assert_eq!(updates.update_fail_htlcs.len(), 1);
6175 (updates.update_fail_htlcs[0].clone(), updates.commitment_signed.clone())
6177 _ => panic!("Unexpected event"),
6180 // Pass the failure messages back to nodes[0].
6181 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
6182 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6184 // Complete the HTLC failure+removal process.
6185 let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6186 check_added_monitors!(nodes[0], 1);
6187 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6188 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
6189 check_added_monitors!(nodes[1], 2);
6190 let final_raa_event = nodes[1].node.get_and_clear_pending_msg_events();
6191 assert_eq!(final_raa_event.len(), 1);
6192 let raa = match &final_raa_event[0] {
6193 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => msg.clone(),
6194 _ => panic!("Unexpected event"),
6196 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
6197 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_1_2.0.contents.short_channel_id, false);
6198 check_added_monitors!(nodes[0], 1);
6201 // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6202 // BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
6203 //TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.
6206 fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
6207 //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
6208 let chanmon_cfgs = create_chanmon_cfgs(2);
6209 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6210 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6211 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6212 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6214 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6215 route.paths[0].hops[0].fee_msat = 100;
6217 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6218 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6219 ), true, APIError::ChannelUnavailable { .. }, {});
6220 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6224 fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
6225 //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6226 let chanmon_cfgs = create_chanmon_cfgs(2);
6227 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6228 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6229 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6230 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6232 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6233 route.paths[0].hops[0].fee_msat = 0;
6234 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6235 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)),
6236 true, APIError::ChannelUnavailable { ref err },
6237 assert_eq!(err, "Cannot send 0-msat HTLC"));
6239 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6240 nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send 0-msat HTLC", 1);
6244 fn test_update_add_htlc_bolt2_receiver_zero_value_msat() {
6245 //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6246 let chanmon_cfgs = create_chanmon_cfgs(2);
6247 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6248 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6249 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6250 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6252 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6253 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6254 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6255 check_added_monitors!(nodes[0], 1);
6256 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6257 updates.update_add_htlcs[0].amount_msat = 0;
6259 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6260 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote side tried to send a 0-msat HTLC".to_string(), 1);
6261 check_closed_broadcast!(nodes[1], true).unwrap();
6262 check_added_monitors!(nodes[1], 1);
6263 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote side tried to send a 0-msat HTLC".to_string() },
6264 [nodes[0].node.get_our_node_id()], 100000);
6268 fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
6269 //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
6270 //It is enforced when constructing a route.
6271 let chanmon_cfgs = create_chanmon_cfgs(2);
6272 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6273 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6274 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6275 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6277 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
6278 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
6279 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
6280 route.paths[0].hops.last_mut().unwrap().cltv_expiry_delta = 500000001;
6281 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6282 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6283 ), true, APIError::InvalidRoute { ref err },
6284 assert_eq!(err, &"Channel CLTV overflowed?"));
6288 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
6289 //BOLT 2 Requirement: if result would be offering more than the remote's max_accepted_htlcs HTLCs, in the remote commitment transaction: MUST NOT add an HTLC.
6290 //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
6291 //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6292 let chanmon_cfgs = create_chanmon_cfgs(2);
6293 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6294 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6295 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6296 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6297 let max_accepted_htlcs = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
6298 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().counterparty_max_accepted_htlcs as u64;
6300 // Fetch a route in advance as we will be unable to once we're unable to send.
6301 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6302 for i in 0..max_accepted_htlcs {
6303 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6304 let payment_event = {
6305 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6306 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6307 check_added_monitors!(nodes[0], 1);
6309 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6310 assert_eq!(events.len(), 1);
6311 if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
6312 assert_eq!(htlcs[0].htlc_id, i);
6316 SendEvent::from_event(events.remove(0))
6318 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6319 check_added_monitors!(nodes[1], 0);
6320 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6322 expect_pending_htlcs_forwardable!(nodes[1]);
6323 expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 100000);
6325 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6326 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6327 ), true, APIError::ChannelUnavailable { .. }, {});
6329 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6333 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
6334 //BOLT 2 Requirement: if the sum of total offered HTLCs would exceed the remote's max_htlc_value_in_flight_msat: MUST NOT add an HTLC.
6335 let chanmon_cfgs = create_chanmon_cfgs(2);
6336 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6337 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6338 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6339 let channel_value = 100000;
6340 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0);
6341 let max_in_flight = get_channel_value_stat!(nodes[0], nodes[1], chan.2).counterparty_max_htlc_value_in_flight_msat;
6343 send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
6345 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_in_flight);
6346 // Manually create a route over our max in flight (which our router normally automatically
6348 route.paths[0].hops[0].fee_msat = max_in_flight + 1;
6349 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6350 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6351 ), true, APIError::ChannelUnavailable { .. }, {});
6352 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6354 send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
6357 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
6359 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
6360 //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
6361 let chanmon_cfgs = create_chanmon_cfgs(2);
6362 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6363 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6364 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6365 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6366 let htlc_minimum_msat: u64;
6368 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
6369 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
6370 let channel = chan_lock.channel_by_id.get(&chan.2).unwrap();
6371 htlc_minimum_msat = channel.context().get_holder_htlc_minimum_msat();
6374 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], htlc_minimum_msat);
6375 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6376 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6377 check_added_monitors!(nodes[0], 1);
6378 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6379 updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
6380 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6381 assert!(nodes[1].node.list_channels().is_empty());
6382 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6383 assert!(regex::Regex::new(r"Remote side tried to send less than our minimum HTLC value\. Lower limit: \(\d+\)\. Actual: \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6384 check_added_monitors!(nodes[1], 1);
6385 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6389 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
6390 //BOLT2 Requirement: receiving an amount_msat that the sending node cannot afford at the current feerate_per_kw (while maintaining its channel reserve): SHOULD fail the channel
6391 let chanmon_cfgs = create_chanmon_cfgs(2);
6392 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6393 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6394 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6395 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6397 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6398 let channel_reserve = chan_stat.channel_reserve_msat;
6399 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
6400 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
6401 // The 2* and +1 are for the fee spike reserve.
6402 let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6404 let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
6405 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
6406 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6407 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6408 check_added_monitors!(nodes[0], 1);
6409 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6411 // Even though channel-initiator senders are required to respect the fee_spike_reserve,
6412 // at this time channel-initiatee receivers are not required to enforce that senders
6413 // respect the fee_spike_reserve.
6414 updates.update_add_htlcs[0].amount_msat = max_can_send + commit_tx_fee_outbound + 1;
6415 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6417 assert!(nodes[1].node.list_channels().is_empty());
6418 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6419 assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
6420 check_added_monitors!(nodes[1], 1);
6421 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6425 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
6426 //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
6427 //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
6428 let chanmon_cfgs = create_chanmon_cfgs(2);
6429 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6430 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6431 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6432 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6434 let send_amt = 3999999;
6435 let (mut route, our_payment_hash, _, our_payment_secret) =
6436 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
6437 route.paths[0].hops[0].fee_msat = send_amt;
6438 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
6439 let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
6440 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
6441 let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
6442 &route.paths[0], send_amt, RecipientOnionFields::secret_only(our_payment_secret), cur_height, &None).unwrap();
6443 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
6445 let mut msg = msgs::UpdateAddHTLC {
6449 payment_hash: our_payment_hash,
6450 cltv_expiry: htlc_cltv,
6451 onion_routing_packet: onion_packet.clone(),
6452 skimmed_fee_msat: None,
6456 msg.htlc_id = i as u64;
6457 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6459 msg.htlc_id = (50) as u64;
6460 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6462 assert!(nodes[1].node.list_channels().is_empty());
6463 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6464 assert!(regex::Regex::new(r"Remote tried to push more than our max accepted HTLCs \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6465 check_added_monitors!(nodes[1], 1);
6466 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6470 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
6471 //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
6472 let chanmon_cfgs = create_chanmon_cfgs(2);
6473 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6474 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6475 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6476 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6478 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6479 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6480 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6481 check_added_monitors!(nodes[0], 1);
6482 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6483 updates.update_add_htlcs[0].amount_msat = get_channel_value_stat!(nodes[1], nodes[0], chan.2).counterparty_max_htlc_value_in_flight_msat + 1;
6484 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6486 assert!(nodes[1].node.list_channels().is_empty());
6487 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6488 assert!(regex::Regex::new("Remote HTLC add would put them over our max HTLC value").unwrap().is_match(err_msg.data.as_str()));
6489 check_added_monitors!(nodes[1], 1);
6490 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 1000000);
6494 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
6495 //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
6496 let chanmon_cfgs = create_chanmon_cfgs(2);
6497 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6498 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6499 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6501 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6502 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6503 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6504 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6505 check_added_monitors!(nodes[0], 1);
6506 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6507 updates.update_add_htlcs[0].cltv_expiry = 500000000;
6508 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6510 assert!(nodes[1].node.list_channels().is_empty());
6511 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6512 assert_eq!(err_msg.data,"Remote provided CLTV expiry in seconds instead of block height");
6513 check_added_monitors!(nodes[1], 1);
6514 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6518 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
6519 //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
6520 // We test this by first testing that that repeated HTLCs pass commitment signature checks
6521 // after disconnect and that non-sequential htlc_ids result in a channel failure.
6522 let chanmon_cfgs = create_chanmon_cfgs(2);
6523 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6524 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6525 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6527 create_announced_chan_between_nodes(&nodes, 0, 1);
6528 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6529 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6530 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6531 check_added_monitors!(nodes[0], 1);
6532 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6533 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6535 //Disconnect and Reconnect
6536 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
6537 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
6538 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
6539 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
6541 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
6542 assert_eq!(reestablish_1.len(), 1);
6543 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
6544 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
6546 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
6547 assert_eq!(reestablish_2.len(), 1);
6548 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
6549 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
6550 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
6551 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
6554 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6555 assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
6556 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
6557 check_added_monitors!(nodes[1], 1);
6558 let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6560 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6562 assert!(nodes[1].node.list_channels().is_empty());
6563 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6564 assert!(regex::Regex::new(r"Remote skipped HTLC ID \(skipped ID: \d+\)").unwrap().is_match(err_msg.data.as_str()));
6565 check_added_monitors!(nodes[1], 1);
6566 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6570 fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
6571 //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6573 let chanmon_cfgs = create_chanmon_cfgs(2);
6574 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6575 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6576 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6577 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6578 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6579 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6580 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6582 check_added_monitors!(nodes[0], 1);
6583 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6584 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6586 let update_msg = msgs::UpdateFulfillHTLC{
6589 payment_preimage: our_payment_preimage,
6592 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6594 assert!(nodes[0].node.list_channels().is_empty());
6595 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6596 assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6597 check_added_monitors!(nodes[0], 1);
6598 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6602 fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
6603 //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6605 let chanmon_cfgs = create_chanmon_cfgs(2);
6606 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6607 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6608 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6609 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6611 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6612 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6613 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6614 check_added_monitors!(nodes[0], 1);
6615 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6616 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6618 let update_msg = msgs::UpdateFailHTLC{
6621 reason: msgs::OnionErrorPacket { data: Vec::new()},
6624 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6626 assert!(nodes[0].node.list_channels().is_empty());
6627 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6628 assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6629 check_added_monitors!(nodes[0], 1);
6630 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6634 fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
6635 //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6637 let chanmon_cfgs = create_chanmon_cfgs(2);
6638 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6639 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6640 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6641 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6643 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6644 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6645 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6646 check_added_monitors!(nodes[0], 1);
6647 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6648 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6649 let update_msg = msgs::UpdateFailMalformedHTLC{
6652 sha256_of_onion: [1; 32],
6653 failure_code: 0x8000,
6656 nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6658 assert!(nodes[0].node.list_channels().is_empty());
6659 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6660 assert!(regex::Regex::new(r"Remote tried to fulfill/fail HTLC \(\d+\) before it had been committed").unwrap().is_match(err_msg.data.as_str()));
6661 check_added_monitors!(nodes[0], 1);
6662 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6666 fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
6667 //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
6669 let chanmon_cfgs = create_chanmon_cfgs(2);
6670 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6671 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6672 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6673 create_announced_chan_between_nodes(&nodes, 0, 1);
6675 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6677 nodes[1].node.claim_funds(our_payment_preimage);
6678 check_added_monitors!(nodes[1], 1);
6679 expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6681 let events = nodes[1].node.get_and_clear_pending_msg_events();
6682 assert_eq!(events.len(), 1);
6683 let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6685 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
6686 assert!(update_add_htlcs.is_empty());
6687 assert_eq!(update_fulfill_htlcs.len(), 1);
6688 assert!(update_fail_htlcs.is_empty());
6689 assert!(update_fail_malformed_htlcs.is_empty());
6690 assert!(update_fee.is_none());
6691 update_fulfill_htlcs[0].clone()
6693 _ => panic!("Unexpected event"),
6697 update_fulfill_msg.htlc_id = 1;
6699 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6701 assert!(nodes[0].node.list_channels().is_empty());
6702 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6703 assert_eq!(err_msg.data, "Remote tried to fulfill/fail an HTLC we couldn't find");
6704 check_added_monitors!(nodes[0], 1);
6705 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6709 fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
6710 //BOLT 2 Requirement: A receiving node: if the payment_preimage value in update_fulfill_htlc doesn't SHA256 hash to the corresponding HTLC payment_hash MUST fail the channel.
6712 let chanmon_cfgs = create_chanmon_cfgs(2);
6713 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6714 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6715 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6716 create_announced_chan_between_nodes(&nodes, 0, 1);
6718 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6720 nodes[1].node.claim_funds(our_payment_preimage);
6721 check_added_monitors!(nodes[1], 1);
6722 expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6724 let events = nodes[1].node.get_and_clear_pending_msg_events();
6725 assert_eq!(events.len(), 1);
6726 let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6728 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
6729 assert!(update_add_htlcs.is_empty());
6730 assert_eq!(update_fulfill_htlcs.len(), 1);
6731 assert!(update_fail_htlcs.is_empty());
6732 assert!(update_fail_malformed_htlcs.is_empty());
6733 assert!(update_fee.is_none());
6734 update_fulfill_htlcs[0].clone()
6736 _ => panic!("Unexpected event"),
6740 update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
6742 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6744 assert!(nodes[0].node.list_channels().is_empty());
6745 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6746 assert!(regex::Regex::new(r"Remote tried to fulfill HTLC \(\d+\) with an incorrect preimage").unwrap().is_match(err_msg.data.as_str()));
6747 check_added_monitors!(nodes[0], 1);
6748 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6752 fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
6753 //BOLT 2 Requirement: A receiving node: if the BADONION bit in failure_code is not set for update_fail_malformed_htlc MUST fail the channel.
6755 let chanmon_cfgs = create_chanmon_cfgs(2);
6756 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6757 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6758 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6759 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6761 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6762 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6763 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6764 check_added_monitors!(nodes[0], 1);
6766 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6767 updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6769 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6770 check_added_monitors!(nodes[1], 0);
6771 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
6773 let events = nodes[1].node.get_and_clear_pending_msg_events();
6775 let mut update_msg: msgs::UpdateFailMalformedHTLC = {
6777 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
6778 assert!(update_add_htlcs.is_empty());
6779 assert!(update_fulfill_htlcs.is_empty());
6780 assert!(update_fail_htlcs.is_empty());
6781 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6782 assert!(update_fee.is_none());
6783 update_fail_malformed_htlcs[0].clone()
6785 _ => panic!("Unexpected event"),
6788 update_msg.failure_code &= !0x8000;
6789 nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6791 assert!(nodes[0].node.list_channels().is_empty());
6792 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6793 assert_eq!(err_msg.data, "Got update_fail_malformed_htlc with BADONION not set");
6794 check_added_monitors!(nodes[0], 1);
6795 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 1000000);
6799 fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
6800 //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
6801 // * MUST return an error in the update_fail_htlc sent to the link which originally sent the HTLC, using the failure_code given and setting the data to sha256_of_onion.
6803 let chanmon_cfgs = create_chanmon_cfgs(3);
6804 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6805 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6806 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6807 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6808 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000);
6810 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
6813 let mut payment_event = {
6814 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6815 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6816 check_added_monitors!(nodes[0], 1);
6817 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6818 assert_eq!(events.len(), 1);
6819 SendEvent::from_event(events.remove(0))
6821 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6822 check_added_monitors!(nodes[1], 0);
6823 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6824 expect_pending_htlcs_forwardable!(nodes[1]);
6825 let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
6826 assert_eq!(events_2.len(), 1);
6827 check_added_monitors!(nodes[1], 1);
6828 payment_event = SendEvent::from_event(events_2.remove(0));
6829 assert_eq!(payment_event.msgs.len(), 1);
6832 payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6833 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6834 check_added_monitors!(nodes[2], 0);
6835 commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6837 let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6838 assert_eq!(events_3.len(), 1);
6839 let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
6841 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
6842 assert!(update_add_htlcs.is_empty());
6843 assert!(update_fulfill_htlcs.is_empty());
6844 assert!(update_fail_htlcs.is_empty());
6845 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6846 assert!(update_fee.is_none());
6847 (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
6849 _ => panic!("Unexpected event"),
6853 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0);
6855 check_added_monitors!(nodes[1], 0);
6856 commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
6857 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
6858 let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6859 assert_eq!(events_4.len(), 1);
6861 //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
6863 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, .. } } => {
6864 assert!(update_add_htlcs.is_empty());
6865 assert!(update_fulfill_htlcs.is_empty());
6866 assert_eq!(update_fail_htlcs.len(), 1);
6867 assert!(update_fail_malformed_htlcs.is_empty());
6868 assert!(update_fee.is_none());
6870 _ => panic!("Unexpected event"),
6873 check_added_monitors!(nodes[1], 1);
6877 fn test_channel_failed_after_message_with_badonion_node_perm_bits_set() {
6878 let chanmon_cfgs = create_chanmon_cfgs(3);
6879 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6880 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6881 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6882 create_announced_chan_between_nodes(&nodes, 0, 1);
6883 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
6885 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
6888 let mut payment_event = {
6889 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6890 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6891 check_added_monitors!(nodes[0], 1);
6892 SendEvent::from_node(&nodes[0])
6895 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6896 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6897 expect_pending_htlcs_forwardable!(nodes[1]);
6898 check_added_monitors!(nodes[1], 1);
6899 payment_event = SendEvent::from_node(&nodes[1]);
6900 assert_eq!(payment_event.msgs.len(), 1);
6903 payment_event.msgs[0].onion_routing_packet.version = 1; // Trigger an invalid_onion_version error
6904 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6905 check_added_monitors!(nodes[2], 0);
6906 commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6908 let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6909 assert_eq!(events_3.len(), 1);
6911 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6912 let mut update_msg = updates.update_fail_malformed_htlcs[0].clone();
6913 // Set the NODE bit (BADONION and PERM already set in invalid_onion_version error)
6914 update_msg.failure_code |= 0x2000;
6916 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg);
6917 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true);
6919 _ => panic!("Unexpected event"),
6922 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
6923 vec![HTLCDestination::NextHopChannel {
6924 node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
6925 let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6926 assert_eq!(events_4.len(), 1);
6927 check_added_monitors!(nodes[1], 1);
6930 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6931 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
6932 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
6934 _ => panic!("Unexpected event"),
6937 let events_5 = nodes[0].node.get_and_clear_pending_events();
6938 assert_eq!(events_5.len(), 2);
6940 // Expect a PaymentPathFailed event with a ChannelFailure network update for the channel between
6941 // the node originating the error to its next hop.
6943 Event::PaymentPathFailed { error_code, failure: PathFailure::OnPath { network_update: Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }) }, ..
6945 assert_eq!(short_channel_id, chan_2.0.contents.short_channel_id);
6946 assert!(is_permanent);
6947 assert_eq!(error_code, Some(0x8000|0x4000|0x2000|4));
6949 _ => panic!("Unexpected event"),
6952 Event::PaymentFailed { payment_hash, .. } => {
6953 assert_eq!(payment_hash, our_payment_hash);
6955 _ => panic!("Unexpected event"),
6958 // TODO: Test actual removal of channel from NetworkGraph when it's implemented.
6961 fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
6962 // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
6963 // We can have at most two valid local commitment tx, so both cases must be covered, and both txs must be checked to get them all as
6964 // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
6966 let mut chanmon_cfgs = create_chanmon_cfgs(2);
6967 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
6968 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6969 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6970 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6971 let chan =create_announced_chan_between_nodes(&nodes, 0, 1);
6973 let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
6974 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
6976 // We route 2 dust-HTLCs between A and B
6977 let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
6978 let (_, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
6979 route_payment(&nodes[0], &[&nodes[1]], 1000000);
6981 // Cache one local commitment tx as previous
6982 let as_prev_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
6984 // Fail one HTLC to prune it in the will-be-latest-local commitment tx
6985 nodes[1].node.fail_htlc_backwards(&payment_hash_2);
6986 check_added_monitors!(nodes[1], 0);
6987 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
6988 check_added_monitors!(nodes[1], 1);
6990 let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6991 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]);
6992 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed);
6993 check_added_monitors!(nodes[0], 1);
6995 // Cache one local commitment tx as lastest
6996 let as_last_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
6998 let events = nodes[0].node.get_and_clear_pending_msg_events();
7000 MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
7001 assert_eq!(node_id, nodes[1].node.get_our_node_id());
7003 _ => panic!("Unexpected event"),
7006 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
7007 assert_eq!(node_id, nodes[1].node.get_our_node_id());
7009 _ => panic!("Unexpected event"),
7012 assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
7013 // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
7014 if announce_latest {
7015 mine_transaction(&nodes[0], &as_last_commitment_tx[0]);
7017 mine_transaction(&nodes[0], &as_prev_commitment_tx[0]);
7020 check_closed_broadcast!(nodes[0], true);
7021 check_added_monitors!(nodes[0], 1);
7022 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7024 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7025 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7026 let events = nodes[0].node.get_and_clear_pending_events();
7027 // Only 2 PaymentPathFailed events should show up, over-dust HTLC has to be failed by timeout tx
7028 assert_eq!(events.len(), 4);
7029 let mut first_failed = false;
7030 for event in events {
7032 Event::PaymentPathFailed { payment_hash, .. } => {
7033 if payment_hash == payment_hash_1 {
7034 assert!(!first_failed);
7035 first_failed = true;
7037 assert_eq!(payment_hash, payment_hash_2);
7040 Event::PaymentFailed { .. } => {}
7041 _ => panic!("Unexpected event"),
7047 fn test_failure_delay_dust_htlc_local_commitment() {
7048 do_test_failure_delay_dust_htlc_local_commitment(true);
7049 do_test_failure_delay_dust_htlc_local_commitment(false);
7052 fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
7053 // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
7054 // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
7055 // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
7056 // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
7057 // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
7058 // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
7060 let chanmon_cfgs = create_chanmon_cfgs(3);
7061 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7062 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7063 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7064 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
7066 let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
7067 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
7069 let (_payment_preimage_1, dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7070 let (_payment_preimage_2, non_dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7072 let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7073 let bs_commitment_tx = get_local_commitment_txn!(nodes[1], chan.2);
7075 // We revoked bs_commitment_tx
7077 let (payment_preimage_3, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7078 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
7081 let mut timeout_tx = Vec::new();
7083 // We fail dust-HTLC 1 by broadcast of local commitment tx
7084 mine_transaction(&nodes[0], &as_commitment_tx[0]);
7085 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7086 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7087 expect_payment_failed!(nodes[0], dust_hash, false);
7089 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS - ANTI_REORG_DELAY);
7090 check_closed_broadcast!(nodes[0], true);
7091 check_added_monitors!(nodes[0], 1);
7092 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7093 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
7094 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7095 // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
7096 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7097 mine_transaction(&nodes[0], &timeout_tx[0]);
7098 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7099 expect_payment_failed!(nodes[0], non_dust_hash, false);
7101 // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
7102 mine_transaction(&nodes[0], &bs_commitment_tx[0]);
7103 check_closed_broadcast!(nodes[0], true);
7104 check_added_monitors!(nodes[0], 1);
7105 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7106 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7108 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7109 timeout_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..)
7110 .filter(|tx| tx.input[0].previous_output.txid == bs_commitment_tx[0].txid()).collect();
7111 check_spends!(timeout_tx[0], bs_commitment_tx[0]);
7112 // For both a revoked or non-revoked commitment transaction, after ANTI_REORG_DELAY the
7113 // dust HTLC should have been failed.
7114 expect_payment_failed!(nodes[0], dust_hash, false);
7117 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7119 assert_eq!(timeout_tx[0].lock_time.0, 11);
7121 // We fail non-dust-HTLC 2 by broadcast of local timeout/revocation-claim tx
7122 mine_transaction(&nodes[0], &timeout_tx[0]);
7123 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7124 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7125 expect_payment_failed!(nodes[0], non_dust_hash, false);
7130 fn test_sweep_outbound_htlc_failure_update() {
7131 do_test_sweep_outbound_htlc_failure_update(false, true);
7132 do_test_sweep_outbound_htlc_failure_update(false, false);
7133 do_test_sweep_outbound_htlc_failure_update(true, false);
7137 fn test_user_configurable_csv_delay() {
7138 // We test our channel constructors yield errors when we pass them absurd csv delay
7140 let mut low_our_to_self_config = UserConfig::default();
7141 low_our_to_self_config.channel_handshake_config.our_to_self_delay = 6;
7142 let mut high_their_to_self_config = UserConfig::default();
7143 high_their_to_self_config.channel_handshake_limits.their_to_self_delay = 100;
7144 let user_cfgs = [Some(high_their_to_self_config.clone()), None];
7145 let chanmon_cfgs = create_chanmon_cfgs(2);
7146 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7147 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
7148 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7150 // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
7151 if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7152 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
7153 &low_our_to_self_config, 0, 42)
7156 APIError::APIMisuseError { err } => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },
7157 _ => panic!("Unexpected event"),
7159 } else { assert!(false) }
7161 // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in InboundV1Channel::new()
7162 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7163 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7164 open_channel.to_self_delay = 200;
7165 if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7166 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
7167 &low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7170 ChannelError::Close(err) => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },
7171 _ => panic!("Unexpected event"),
7173 } else { assert!(false); }
7175 // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
7176 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7177 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
7178 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7179 accept_channel.to_self_delay = 200;
7180 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
7182 if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[0] {
7184 &ErrorAction::SendErrorMessage { ref msg } => {
7185 assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(msg.data.as_str()));
7186 reason_msg = msg.data.clone();
7190 } else { panic!(); }
7191 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: reason_msg }, [nodes[1].node.get_our_node_id()], 1000000);
7193 // We test msg.to_self_delay <= config.their_to_self_delay is enforced in InboundV1Channel::new()
7194 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None).unwrap();
7195 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7196 open_channel.to_self_delay = 200;
7197 if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7198 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[0].node.channel_type_features(), &nodes[1].node.init_features(), &open_channel, 0,
7199 &high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7202 ChannelError::Close(err) => { assert!(regex::Regex::new(r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+").unwrap().is_match(err.as_str())); },
7203 _ => panic!("Unexpected event"),
7205 } else { assert!(false); }
7209 fn test_check_htlc_underpaying() {
7210 // Send payment through A -> B but A is maliciously
7211 // sending a probe payment (i.e less than expected value0
7212 // to B, B should refuse payment.
7214 let chanmon_cfgs = create_chanmon_cfgs(2);
7215 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7216 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7217 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7219 // Create some initial channels
7220 create_announced_chan_between_nodes(&nodes, 0, 1);
7222 let scorer = test_utils::TestScorer::new();
7223 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7224 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(),
7225 TEST_FINAL_CLTV).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
7226 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 10_000);
7227 let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(),
7228 None, nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7229 let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
7230 let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, None).unwrap();
7231 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
7232 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
7233 check_added_monitors!(nodes[0], 1);
7235 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
7236 assert_eq!(events.len(), 1);
7237 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
7238 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7239 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7241 // Note that we first have to wait a random delay before processing the receipt of the HTLC,
7242 // and then will wait a second random delay before failing the HTLC back:
7243 expect_pending_htlcs_forwardable!(nodes[1]);
7244 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
7246 // Node 3 is expecting payment of 100_000 but received 10_000,
7247 // it should fail htlc like we didn't know the preimage.
7248 nodes[1].node.process_pending_htlc_forwards();
7250 let events = nodes[1].node.get_and_clear_pending_msg_events();
7251 assert_eq!(events.len(), 1);
7252 let (update_fail_htlc, commitment_signed) = match events[0] {
7253 MessageSendEvent::UpdateHTLCs { node_id: _ , updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
7254 assert!(update_add_htlcs.is_empty());
7255 assert!(update_fulfill_htlcs.is_empty());
7256 assert_eq!(update_fail_htlcs.len(), 1);
7257 assert!(update_fail_malformed_htlcs.is_empty());
7258 assert!(update_fee.is_none());
7259 (update_fail_htlcs[0].clone(), commitment_signed)
7261 _ => panic!("Unexpected event"),
7263 check_added_monitors!(nodes[1], 1);
7265 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlc);
7266 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
7268 // 10_000 msat as u64, followed by a height of CHAN_CONFIRM_DEPTH as u32
7269 let mut expected_failure_data = (10_000 as u64).to_be_bytes().to_vec();
7270 expected_failure_data.extend_from_slice(&CHAN_CONFIRM_DEPTH.to_be_bytes());
7271 expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000|15, &expected_failure_data[..]);
7275 fn test_announce_disable_channels() {
7276 // Create 2 channels between A and B. Disconnect B. Call timer_tick_occurred and check for generated
7277 // ChannelUpdate. Reconnect B, reestablish and check there is non-generated ChannelUpdate.
7279 let chanmon_cfgs = create_chanmon_cfgs(2);
7280 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7281 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7282 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7284 create_announced_chan_between_nodes(&nodes, 0, 1);
7285 create_announced_chan_between_nodes(&nodes, 1, 0);
7286 create_announced_chan_between_nodes(&nodes, 0, 1);
7289 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
7290 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
7292 for _ in 0..DISABLE_GOSSIP_TICKS + 1 {
7293 nodes[0].node.timer_tick_occurred();
7295 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7296 assert_eq!(msg_events.len(), 3);
7297 let mut chans_disabled = HashMap::new();
7298 for e in msg_events {
7300 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7301 assert_eq!(msg.contents.flags & (1<<1), 1<<1); // The "channel disabled" bit should be set
7302 // Check that each channel gets updated exactly once
7303 if chans_disabled.insert(msg.contents.short_channel_id, msg.contents.timestamp).is_some() {
7304 panic!("Generated ChannelUpdate for wrong chan!");
7307 _ => panic!("Unexpected event"),
7311 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
7312 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
7314 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
7315 assert_eq!(reestablish_1.len(), 3);
7316 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
7317 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
7319 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
7320 assert_eq!(reestablish_2.len(), 3);
7322 // Reestablish chan_1
7323 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
7324 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7325 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
7326 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7327 // Reestablish chan_2
7328 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[1]);
7329 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7330 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[1]);
7331 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7332 // Reestablish chan_3
7333 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[2]);
7334 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7335 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[2]);
7336 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7338 for _ in 0..ENABLE_GOSSIP_TICKS {
7339 nodes[0].node.timer_tick_occurred();
7341 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7342 nodes[0].node.timer_tick_occurred();
7343 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7344 assert_eq!(msg_events.len(), 3);
7345 for e in msg_events {
7347 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7348 assert_eq!(msg.contents.flags & (1<<1), 0); // The "channel disabled" bit should be off
7349 match chans_disabled.remove(&msg.contents.short_channel_id) {
7350 // Each update should have a higher timestamp than the previous one, replacing
7352 Some(prev_timestamp) => assert!(msg.contents.timestamp > prev_timestamp),
7353 None => panic!("Generated ChannelUpdate for wrong chan!"),
7356 _ => panic!("Unexpected event"),
7359 // Check that each channel gets updated exactly once
7360 assert!(chans_disabled.is_empty());
7364 fn test_bump_penalty_txn_on_revoked_commitment() {
7365 // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
7366 // we're able to claim outputs on revoked commitment transaction before timelocks expiration
7368 let chanmon_cfgs = create_chanmon_cfgs(2);
7369 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7370 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7371 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7373 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7375 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
7376 let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 30)
7377 .with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
7378 let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000);
7379 send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
7381 let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
7382 // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7383 assert_eq!(revoked_txn[0].output.len(), 4);
7384 assert_eq!(revoked_txn[0].input.len(), 1);
7385 assert_eq!(revoked_txn[0].input[0].previous_output.txid, chan.3.txid());
7386 let revoked_txid = revoked_txn[0].txid();
7388 let mut penalty_sum = 0;
7389 for outp in revoked_txn[0].output.iter() {
7390 if outp.script_pubkey.is_v0_p2wsh() {
7391 penalty_sum += outp.value;
7395 // Connect blocks to change height_timer range to see if we use right soonest_timelock
7396 let header_114 = connect_blocks(&nodes[1], 14);
7398 // Actually revoke tx by claiming a HTLC
7399 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7400 connect_block(&nodes[1], &create_dummy_block(header_114, 42, vec![revoked_txn[0].clone()]));
7401 check_added_monitors!(nodes[1], 1);
7403 // One or more justice tx should have been broadcast, check it
7407 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7408 assert_eq!(node_txn.len(), 1); // justice tx (broadcasted from ChannelMonitor)
7409 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7410 assert_eq!(node_txn[0].output.len(), 1);
7411 check_spends!(node_txn[0], revoked_txn[0]);
7412 let fee_1 = penalty_sum - node_txn[0].output[0].value;
7413 feerate_1 = fee_1 * 1000 / node_txn[0].weight() as u64;
7414 penalty_1 = node_txn[0].txid();
7418 // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it
7419 connect_blocks(&nodes[1], 15);
7420 let mut penalty_2 = penalty_1;
7421 let mut feerate_2 = 0;
7423 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7424 assert_eq!(node_txn.len(), 1);
7425 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7426 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7427 assert_eq!(node_txn[0].output.len(), 1);
7428 check_spends!(node_txn[0], revoked_txn[0]);
7429 penalty_2 = node_txn[0].txid();
7430 // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7431 assert_ne!(penalty_2, penalty_1);
7432 let fee_2 = penalty_sum - node_txn[0].output[0].value;
7433 feerate_2 = fee_2 * 1000 / node_txn[0].weight() as u64;
7434 // Verify 25% bump heuristic
7435 assert!(feerate_2 * 100 >= feerate_1 * 125);
7439 assert_ne!(feerate_2, 0);
7441 // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it
7442 connect_blocks(&nodes[1], 1);
7444 let mut feerate_3 = 0;
7446 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7447 assert_eq!(node_txn.len(), 1);
7448 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7449 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7450 assert_eq!(node_txn[0].output.len(), 1);
7451 check_spends!(node_txn[0], revoked_txn[0]);
7452 penalty_3 = node_txn[0].txid();
7453 // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7454 assert_ne!(penalty_3, penalty_2);
7455 let fee_3 = penalty_sum - node_txn[0].output[0].value;
7456 feerate_3 = fee_3 * 1000 / node_txn[0].weight() as u64;
7457 // Verify 25% bump heuristic
7458 assert!(feerate_3 * 100 >= feerate_2 * 125);
7462 assert_ne!(feerate_3, 0);
7464 nodes[1].node.get_and_clear_pending_events();
7465 nodes[1].node.get_and_clear_pending_msg_events();
7469 fn test_bump_penalty_txn_on_revoked_htlcs() {
7470 // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure
7471 // we're able to claim outputs on revoked HTLC transactions before timelocks expiration
7473 let mut chanmon_cfgs = create_chanmon_cfgs(2);
7474 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
7475 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7476 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7477 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7479 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7480 // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
7481 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 50).with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
7482 let scorer = test_utils::TestScorer::new();
7483 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7484 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7485 let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(), None,
7486 nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7487 let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
7488 let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50).with_bolt11_features(nodes[0].node.invoice_features()).unwrap();
7489 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7490 let route = get_route(&nodes[1].node.get_our_node_id(), &route_params, &nodes[1].network_graph.read_only(), None,
7491 nodes[0].logger, &scorer, &(), &random_seed_bytes).unwrap();
7492 send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
7494 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7495 assert_eq!(revoked_local_txn[0].input.len(), 1);
7496 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7498 // Revoke local commitment tx
7499 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7501 // B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx
7502 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone()]));
7503 check_closed_broadcast!(nodes[1], true);
7504 check_added_monitors!(nodes[1], 1);
7505 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 1000000);
7506 connect_blocks(&nodes[1], 50); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
7508 let revoked_htlc_txn = {
7509 let txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
7510 assert_eq!(txn.len(), 2);
7512 assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7513 assert_eq!(txn[0].input.len(), 1);
7514 check_spends!(txn[0], revoked_local_txn[0]);
7516 assert_eq!(txn[1].input.len(), 1);
7517 assert_eq!(txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7518 assert_eq!(txn[1].output.len(), 1);
7519 check_spends!(txn[1], revoked_local_txn[0]);
7524 // Broadcast set of revoked txn on A
7525 let hash_128 = connect_blocks(&nodes[0], 40);
7526 let block_11 = create_dummy_block(hash_128, 42, vec![revoked_local_txn[0].clone()]);
7527 connect_block(&nodes[0], &block_11);
7528 let block_129 = create_dummy_block(block_11.block_hash(), 42, vec![revoked_htlc_txn[0].clone(), revoked_htlc_txn[1].clone()]);
7529 connect_block(&nodes[0], &block_129);
7530 let events = nodes[0].node.get_and_clear_pending_events();
7531 expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
7532 match events.last().unwrap() {
7533 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
7534 _ => panic!("Unexpected event"),
7540 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7541 assert_eq!(node_txn.len(), 4); // 3 penalty txn on revoked commitment tx + 1 penalty tnx on revoked HTLC txn
7542 // Verify claim tx are spending revoked HTLC txn
7544 // node_txn 0-2 each spend a separate revoked output from revoked_local_txn[0]
7545 // Note that node_txn[0] and node_txn[1] are bogus - they double spend the revoked_htlc_txn
7546 // which are included in the same block (they are broadcasted because we scan the
7547 // transactions linearly and generate claims as we go, they likely should be removed in the
7549 assert_eq!(node_txn[0].input.len(), 1);
7550 check_spends!(node_txn[0], revoked_local_txn[0]);
7551 assert_eq!(node_txn[1].input.len(), 1);
7552 check_spends!(node_txn[1], revoked_local_txn[0]);
7553 assert_eq!(node_txn[2].input.len(), 1);
7554 check_spends!(node_txn[2], revoked_local_txn[0]);
7556 // Each of the three justice transactions claim a separate (single) output of the three
7557 // available, which we check here:
7558 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
7559 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
7560 assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
7562 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[1].input[0].previous_output);
7563 assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
7565 // node_txn[3] spends the revoked outputs from the revoked_htlc_txn (which only have one
7566 // output, checked above).
7567 assert_eq!(node_txn[3].input.len(), 2);
7568 assert_eq!(node_txn[3].output.len(), 1);
7569 check_spends!(node_txn[3], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7571 first = node_txn[3].txid();
7572 // Store both feerates for later comparison
7573 let fee_1 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[3].output[0].value;
7574 feerate_1 = fee_1 * 1000 / node_txn[3].weight() as u64;
7575 penalty_txn = vec![node_txn[2].clone()];
7579 // Connect one more block to see if bumped penalty are issued for HTLC txn
7580 let block_130 = create_dummy_block(block_129.block_hash(), 42, penalty_txn);
7581 connect_block(&nodes[0], &block_130);
7582 let block_131 = create_dummy_block(block_130.block_hash(), 42, Vec::new());
7583 connect_block(&nodes[0], &block_131);
7585 // Few more blocks to confirm penalty txn
7586 connect_blocks(&nodes[0], 4);
7587 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
7588 let header_144 = connect_blocks(&nodes[0], 9);
7590 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7591 assert_eq!(node_txn.len(), 1);
7593 assert_eq!(node_txn[0].input.len(), 2);
7594 check_spends!(node_txn[0], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7595 // Verify bumped tx is different and 25% bump heuristic
7596 assert_ne!(first, node_txn[0].txid());
7597 let fee_2 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[0].output[0].value;
7598 let feerate_2 = fee_2 * 1000 / node_txn[0].weight() as u64;
7599 assert!(feerate_2 * 100 > feerate_1 * 125);
7600 let txn = vec![node_txn[0].clone()];
7604 // Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
7605 connect_block(&nodes[0], &create_dummy_block(header_144, 42, node_txn));
7606 connect_blocks(&nodes[0], 20);
7608 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7609 // We verify than no new transaction has been broadcast because previously
7610 // we were buggy on this exact behavior by not tracking for monitoring remote HTLC outputs (see #411)
7611 // which means we wouldn't see a spend of them by a justice tx and bumped justice tx
7612 // were generated forever instead of safe cleaning after confirmation and ANTI_REORG_SAFE_DELAY blocks.
7613 // Enforce spending of revoked htlc output by claiming transaction remove request as expected and dry
7614 // up bumped justice generation.
7615 assert_eq!(node_txn.len(), 0);
7618 check_closed_broadcast!(nodes[0], true);
7619 check_added_monitors!(nodes[0], 1);
7623 fn test_bump_penalty_txn_on_remote_commitment() {
7624 // In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure
7625 // we're able to claim outputs on remote commitment transaction before timelocks expiration
7628 // Provide preimage for one
7629 // Check aggregation
7631 let chanmon_cfgs = create_chanmon_cfgs(2);
7632 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7633 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7634 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7636 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7637 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
7638 route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0;
7640 // Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7641 let remote_txn = get_local_commitment_txn!(nodes[0], chan.2);
7642 assert_eq!(remote_txn[0].output.len(), 4);
7643 assert_eq!(remote_txn[0].input.len(), 1);
7644 assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid());
7646 // Claim a HTLC without revocation (provide B monitor with preimage)
7647 nodes[1].node.claim_funds(payment_preimage);
7648 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
7649 mine_transaction(&nodes[1], &remote_txn[0]);
7650 check_added_monitors!(nodes[1], 2);
7651 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7653 // One or more claim tx should have been broadcast, check it
7657 let feerate_timeout;
7658 let feerate_preimage;
7660 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7661 // 3 transactions including:
7662 // preimage and timeout sweeps from remote commitment + preimage sweep bump
7663 assert_eq!(node_txn.len(), 3);
7664 assert_eq!(node_txn[0].input.len(), 1);
7665 assert_eq!(node_txn[1].input.len(), 1);
7666 assert_eq!(node_txn[2].input.len(), 1);
7667 check_spends!(node_txn[0], remote_txn[0]);
7668 check_spends!(node_txn[1], remote_txn[0]);
7669 check_spends!(node_txn[2], remote_txn[0]);
7671 preimage = node_txn[0].txid();
7672 let index = node_txn[0].input[0].previous_output.vout;
7673 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7674 feerate_preimage = fee * 1000 / node_txn[0].weight() as u64;
7676 let (preimage_bump_tx, timeout_tx) = if node_txn[2].input[0].previous_output == node_txn[0].input[0].previous_output {
7677 (node_txn[2].clone(), node_txn[1].clone())
7679 (node_txn[1].clone(), node_txn[2].clone())
7682 preimage_bump = preimage_bump_tx;
7683 check_spends!(preimage_bump, remote_txn[0]);
7684 assert_eq!(node_txn[0].input[0].previous_output, preimage_bump.input[0].previous_output);
7686 timeout = timeout_tx.txid();
7687 let index = timeout_tx.input[0].previous_output.vout;
7688 let fee = remote_txn[0].output[index as usize].value - timeout_tx.output[0].value;
7689 feerate_timeout = fee * 1000 / timeout_tx.weight() as u64;
7693 assert_ne!(feerate_timeout, 0);
7694 assert_ne!(feerate_preimage, 0);
7696 // After exhaustion of height timer, new bumped claim txn should have been broadcast, check it
7697 connect_blocks(&nodes[1], 1);
7699 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7700 assert_eq!(node_txn.len(), 1);
7701 assert_eq!(node_txn[0].input.len(), 1);
7702 assert_eq!(preimage_bump.input.len(), 1);
7703 check_spends!(node_txn[0], remote_txn[0]);
7704 check_spends!(preimage_bump, remote_txn[0]);
7706 let index = preimage_bump.input[0].previous_output.vout;
7707 let fee = remote_txn[0].output[index as usize].value - preimage_bump.output[0].value;
7708 let new_feerate = fee * 1000 / preimage_bump.weight() as u64;
7709 assert!(new_feerate * 100 > feerate_timeout * 125);
7710 assert_ne!(timeout, preimage_bump.txid());
7712 let index = node_txn[0].input[0].previous_output.vout;
7713 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7714 let new_feerate = fee * 1000 / node_txn[0].weight() as u64;
7715 assert!(new_feerate * 100 > feerate_preimage * 125);
7716 assert_ne!(preimage, node_txn[0].txid());
7721 nodes[1].node.get_and_clear_pending_events();
7722 nodes[1].node.get_and_clear_pending_msg_events();
7726 fn test_counterparty_raa_skip_no_crash() {
7727 // Previously, if our counterparty sent two RAAs in a row without us having provided a
7728 // commitment transaction, we would have happily carried on and provided them the next
7729 // commitment transaction based on one RAA forward. This would probably eventually have led to
7730 // channel closure, but it would not have resulted in funds loss. Still, our
7731 // TestChannelSigner would have panicked as it doesn't like jumps into the future. Here, we
7732 // check simply that the channel is closed in response to such an RAA, but don't check whether
7733 // we decide to punish our counterparty for revoking their funds (as we don't currently
7735 let chanmon_cfgs = create_chanmon_cfgs(2);
7736 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7737 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7738 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7739 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
7741 let per_commitment_secret;
7742 let next_per_commitment_point;
7744 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
7745 let mut guard = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
7746 let keys = guard.channel_by_id.get_mut(&channel_id).map(
7747 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
7748 ).flatten().unwrap().get_signer();
7750 const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
7752 // Make signer believe we got a counterparty signature, so that it allows the revocation
7753 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7754 per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
7756 // Must revoke without gaps
7757 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7758 keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
7760 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7761 next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
7762 &SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
7765 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
7766 &msgs::RevokeAndACK {
7768 per_commitment_secret,
7769 next_per_commitment_point,
7771 next_local_nonce: None,
7773 assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
7774 check_added_monitors!(nodes[1], 1);
7775 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() }
7776 , [nodes[0].node.get_our_node_id()], 100000);
7780 fn test_bump_txn_sanitize_tracking_maps() {
7781 // Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,
7782 // verify we clean then right after expiration of ANTI_REORG_DELAY.
7784 let chanmon_cfgs = create_chanmon_cfgs(2);
7785 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7786 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7787 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7789 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7790 // Lock HTLC in both directions
7791 let (payment_preimage_1, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000);
7792 let (_, payment_hash_2, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 9_000_000);
7794 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7795 assert_eq!(revoked_local_txn[0].input.len(), 1);
7796 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7798 // Revoke local commitment tx
7799 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
7801 // Broadcast set of revoked txn on A
7802 connect_blocks(&nodes[0], TEST_FINAL_CLTV + 2 - CHAN_CONFIRM_DEPTH);
7803 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[0], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
7804 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
7806 mine_transaction(&nodes[0], &revoked_local_txn[0]);
7807 check_closed_broadcast!(nodes[0], true);
7808 check_added_monitors!(nodes[0], 1);
7809 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 1000000);
7811 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7812 assert_eq!(node_txn.len(), 3); //ChannelMonitor: justice txn * 3
7813 check_spends!(node_txn[0], revoked_local_txn[0]);
7814 check_spends!(node_txn[1], revoked_local_txn[0]);
7815 check_spends!(node_txn[2], revoked_local_txn[0]);
7816 let penalty_txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone()];
7820 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, penalty_txn));
7821 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7823 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(OutPoint { txid: chan.3.txid(), index: 0 }).unwrap();
7824 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
7825 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
7830 fn test_channel_conf_timeout() {
7831 // Tests that, for inbound channels, we give up on them if the funding transaction does not
7832 // confirm within 2016 blocks, as recommended by BOLT 2.
7833 let chanmon_cfgs = create_chanmon_cfgs(2);
7834 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7835 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7836 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7838 let _funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 100_000);
7840 // The outbound node should wait forever for confirmation:
7841 // This matches `channel::FUNDING_CONF_DEADLINE_BLOCKS` and BOLT 2's suggested timeout, thus is
7842 // copied here instead of directly referencing the constant.
7843 connect_blocks(&nodes[0], 2016);
7844 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7846 // The inbound node should fail the channel after exactly 2016 blocks
7847 connect_blocks(&nodes[1], 2015);
7848 check_added_monitors!(nodes[1], 0);
7849 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7851 connect_blocks(&nodes[1], 1);
7852 check_added_monitors!(nodes[1], 1);
7853 check_closed_event!(nodes[1], 1, ClosureReason::FundingTimedOut, [nodes[0].node.get_our_node_id()], 1000000);
7854 let close_ev = nodes[1].node.get_and_clear_pending_msg_events();
7855 assert_eq!(close_ev.len(), 1);
7857 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, ref node_id } => {
7858 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
7859 assert_eq!(msg.data, "Channel closed because funding transaction failed to confirm within 2016 blocks");
7861 _ => panic!("Unexpected event"),
7866 fn test_override_channel_config() {
7867 let chanmon_cfgs = create_chanmon_cfgs(2);
7868 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7869 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7870 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7872 // Node0 initiates a channel to node1 using the override config.
7873 let mut override_config = UserConfig::default();
7874 override_config.channel_handshake_config.our_to_self_delay = 200;
7876 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(override_config)).unwrap();
7878 // Assert the channel created by node0 is using the override config.
7879 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7880 assert_eq!(res.channel_flags, 0);
7881 assert_eq!(res.to_self_delay, 200);
7885 fn test_override_0msat_htlc_minimum() {
7886 let mut zero_config = UserConfig::default();
7887 zero_config.channel_handshake_config.our_htlc_minimum_msat = 0;
7888 let chanmon_cfgs = create_chanmon_cfgs(2);
7889 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7890 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(zero_config.clone())]);
7891 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7893 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, Some(zero_config)).unwrap();
7894 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7895 assert_eq!(res.htlc_minimum_msat, 1);
7897 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
7898 let res = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7899 assert_eq!(res.htlc_minimum_msat, 1);
7903 fn test_channel_update_has_correct_htlc_maximum_msat() {
7904 // Tests that the `ChannelUpdate` message has the correct values for `htlc_maximum_msat` set.
7905 // Bolt 7 specifies that if present `htlc_maximum_msat`:
7906 // 1. MUST be set to less than or equal to the channel capacity. In LDK, this is capped to
7907 // 90% of the `channel_value`.
7908 // 2. MUST be set to less than or equal to the `max_htlc_value_in_flight_msat` received from the peer.
7910 let mut config_30_percent = UserConfig::default();
7911 config_30_percent.channel_handshake_config.announced_channel = true;
7912 config_30_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 30;
7913 let mut config_50_percent = UserConfig::default();
7914 config_50_percent.channel_handshake_config.announced_channel = true;
7915 config_50_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 50;
7916 let mut config_95_percent = UserConfig::default();
7917 config_95_percent.channel_handshake_config.announced_channel = true;
7918 config_95_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 95;
7919 let mut config_100_percent = UserConfig::default();
7920 config_100_percent.channel_handshake_config.announced_channel = true;
7921 config_100_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 100;
7923 let chanmon_cfgs = create_chanmon_cfgs(4);
7924 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
7925 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[Some(config_30_percent), Some(config_50_percent), Some(config_95_percent), Some(config_100_percent)]);
7926 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
7928 let channel_value_satoshis = 100000;
7929 let channel_value_msat = channel_value_satoshis * 1000;
7930 let channel_value_30_percent_msat = (channel_value_msat as f64 * 0.3) as u64;
7931 let channel_value_50_percent_msat = (channel_value_msat as f64 * 0.5) as u64;
7932 let channel_value_90_percent_msat = (channel_value_msat as f64 * 0.9) as u64;
7934 let (node_0_chan_update, node_1_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value_satoshis, 10001);
7935 let (node_2_chan_update, node_3_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, channel_value_satoshis, 10001);
7937 // Assert that `node[0]`'s `ChannelUpdate` is capped at 50 percent of the `channel_value`, as
7938 // that's the value of `node[1]`'s `holder_max_htlc_value_in_flight_msat`.
7939 assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, channel_value_50_percent_msat);
7940 // Assert that `node[1]`'s `ChannelUpdate` is capped at 30 percent of the `channel_value`, as
7941 // that's the value of `node[0]`'s `holder_max_htlc_value_in_flight_msat`.
7942 assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, channel_value_30_percent_msat);
7944 // Assert that `node[2]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7945 // the value of `node[3]`'s `holder_max_htlc_value_in_flight_msat` (100%), exceeds 90% of the
7947 assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7948 // Assert that `node[3]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7949 // the value of `node[2]`'s `holder_max_htlc_value_in_flight_msat` (95%), exceeds 90% of the
7951 assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7955 fn test_manually_accept_inbound_channel_request() {
7956 let mut manually_accept_conf = UserConfig::default();
7957 manually_accept_conf.manually_accept_inbound_channels = true;
7958 let chanmon_cfgs = create_chanmon_cfgs(2);
7959 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7960 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
7961 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7963 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
7964 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7966 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
7968 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
7969 // accepting the inbound channel request.
7970 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
7972 let events = nodes[1].node.get_and_clear_pending_events();
7974 Event::OpenChannelRequest { temporary_channel_id, .. } => {
7975 nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap();
7977 _ => panic!("Unexpected event"),
7980 let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
7981 assert_eq!(accept_msg_ev.len(), 1);
7983 match accept_msg_ev[0] {
7984 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
7985 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
7987 _ => panic!("Unexpected event"),
7990 nodes[1].node.force_close_broadcasting_latest_txn(&temp_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
7992 let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
7993 assert_eq!(close_msg_ev.len(), 1);
7995 let events = nodes[1].node.get_and_clear_pending_events();
7997 Event::ChannelClosed { user_channel_id, .. } => {
7998 assert_eq!(user_channel_id, 23);
8000 _ => panic!("Unexpected event"),
8005 fn test_manually_reject_inbound_channel_request() {
8006 let mut manually_accept_conf = UserConfig::default();
8007 manually_accept_conf.manually_accept_inbound_channels = true;
8008 let chanmon_cfgs = create_chanmon_cfgs(2);
8009 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8010 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8011 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8013 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8014 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8016 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8018 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8019 // rejecting the inbound channel request.
8020 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8022 let events = nodes[1].node.get_and_clear_pending_events();
8024 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8025 nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
8027 _ => panic!("Unexpected event"),
8030 let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8031 assert_eq!(close_msg_ev.len(), 1);
8033 match close_msg_ev[0] {
8034 MessageSendEvent::HandleError { ref node_id, .. } => {
8035 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8037 _ => panic!("Unexpected event"),
8040 // There should be no more events to process, as the channel was never opened.
8041 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
8045 fn test_can_not_accept_inbound_channel_twice() {
8046 let mut manually_accept_conf = UserConfig::default();
8047 manually_accept_conf.manually_accept_inbound_channels = true;
8048 let chanmon_cfgs = create_chanmon_cfgs(2);
8049 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8050 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8051 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8053 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(manually_accept_conf)).unwrap();
8054 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8056 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8058 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8059 // accepting the inbound channel request.
8060 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8062 let events = nodes[1].node.get_and_clear_pending_events();
8064 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8065 nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
8066 let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0);
8068 Err(APIError::APIMisuseError { err }) => {
8069 assert_eq!(err, "No such channel awaiting to be accepted.");
8071 Ok(_) => panic!("Channel shouldn't be possible to be accepted twice"),
8072 Err(e) => panic!("Unexpected Error {:?}", e),
8075 _ => panic!("Unexpected event"),
8078 // Ensure that the channel wasn't closed after attempting to accept it twice.
8079 let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8080 assert_eq!(accept_msg_ev.len(), 1);
8082 match accept_msg_ev[0] {
8083 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8084 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8086 _ => panic!("Unexpected event"),
8091 fn test_can_not_accept_unknown_inbound_channel() {
8092 let chanmon_cfg = create_chanmon_cfgs(2);
8093 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
8094 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
8095 let nodes = create_network(2, &node_cfg, &node_chanmgr);
8097 let unknown_channel_id = ChannelId::new_zero();
8098 let api_res = nodes[0].node.accept_inbound_channel(&unknown_channel_id, &nodes[1].node.get_our_node_id(), 0);
8100 Err(APIError::APIMisuseError { err }) => {
8101 assert_eq!(err, "No such channel awaiting to be accepted.");
8103 Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
8104 Err(e) => panic!("Unexpected Error: {:?}", e),
8109 fn test_onion_value_mpp_set_calculation() {
8110 // Test that we use the onion value `amt_to_forward` when
8111 // calculating whether we've reached the `total_msat` of an MPP
8112 // by having a routing node forward more than `amt_to_forward`
8113 // and checking that the receiving node doesn't generate
8114 // a PaymentClaimable event too early
8116 let chanmon_cfgs = create_chanmon_cfgs(node_count);
8117 let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8118 let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8119 let mut nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8121 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8122 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8123 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8124 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8126 let total_msat = 100_000;
8127 let expected_paths: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
8128 let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], total_msat);
8129 let sample_path = route.paths.pop().unwrap();
8131 let mut path_1 = sample_path.clone();
8132 path_1.hops[0].pubkey = nodes[1].node.get_our_node_id();
8133 path_1.hops[0].short_channel_id = chan_1_id;
8134 path_1.hops[1].pubkey = nodes[3].node.get_our_node_id();
8135 path_1.hops[1].short_channel_id = chan_3_id;
8136 path_1.hops[1].fee_msat = 100_000;
8137 route.paths.push(path_1);
8139 let mut path_2 = sample_path.clone();
8140 path_2.hops[0].pubkey = nodes[2].node.get_our_node_id();
8141 path_2.hops[0].short_channel_id = chan_2_id;
8142 path_2.hops[1].pubkey = nodes[3].node.get_our_node_id();
8143 path_2.hops[1].short_channel_id = chan_4_id;
8144 path_2.hops[1].fee_msat = 1_000;
8145 route.paths.push(path_2);
8148 let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
8149 let onion_session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
8150 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8151 nodes[0].node.test_send_payment_internal(&route, our_payment_hash,
8152 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8153 check_added_monitors!(nodes[0], expected_paths.len());
8155 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8156 assert_eq!(events.len(), expected_paths.len());
8159 let ev = remove_first_msg_event_to_node(&expected_paths[0][0].node.get_our_node_id(), &mut events);
8160 let mut payment_event = SendEvent::from_event(ev);
8161 let mut prev_node = &nodes[0];
8163 for (idx, &node) in expected_paths[0].iter().enumerate() {
8164 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
8166 if idx == 0 { // routing node
8167 let session_priv = [3; 32];
8168 let height = nodes[0].best_block_info().1;
8169 let session_priv = SecretKey::from_slice(&session_priv).unwrap();
8170 let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
8171 let (mut onion_payloads, _, _) = onion_utils::build_onion_payloads(&route.paths[0], 100_000,
8172 RecipientOnionFields::secret_only(our_payment_secret), height + 1, &None).unwrap();
8173 // Edit amt_to_forward to simulate the sender having set
8174 // the final amount and the routing node taking less fee
8175 if let msgs::OutboundOnionPayload::Receive { ref mut amt_msat, .. } = onion_payloads[1] {
8178 let new_onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
8179 payment_event.msgs[0].onion_routing_packet = new_onion_packet;
8182 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]);
8183 check_added_monitors!(node, 0);
8184 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
8185 expect_pending_htlcs_forwardable!(node);
8188 let mut events_2 = node.node.get_and_clear_pending_msg_events();
8189 assert_eq!(events_2.len(), 1);
8190 check_added_monitors!(node, 1);
8191 payment_event = SendEvent::from_event(events_2.remove(0));
8192 assert_eq!(payment_event.msgs.len(), 1);
8194 let events_2 = node.node.get_and_clear_pending_events();
8195 assert!(events_2.is_empty());
8202 let ev = remove_first_msg_event_to_node(&expected_paths[1][0].node.get_our_node_id(), &mut events);
8203 pass_along_path(&nodes[0], expected_paths[1], 101_000, our_payment_hash.clone(), Some(our_payment_secret), ev, true, None);
8205 claim_payment_along_route(&nodes[0], expected_paths, false, our_payment_preimage);
8208 fn do_test_overshoot_mpp(msat_amounts: &[u64], total_msat: u64) {
8210 let routing_node_count = msat_amounts.len();
8211 let node_count = routing_node_count + 2;
8213 let chanmon_cfgs = create_chanmon_cfgs(node_count);
8214 let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8215 let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8216 let nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8221 // Create channels for each amount
8222 let mut expected_paths = Vec::with_capacity(routing_node_count);
8223 let mut src_chan_ids = Vec::with_capacity(routing_node_count);
8224 let mut dst_chan_ids = Vec::with_capacity(routing_node_count);
8225 for i in 0..routing_node_count {
8226 let routing_node = 2 + i;
8227 let src_chan_id = create_announced_chan_between_nodes(&nodes, src_idx, routing_node).0.contents.short_channel_id;
8228 src_chan_ids.push(src_chan_id);
8229 let dst_chan_id = create_announced_chan_between_nodes(&nodes, routing_node, dst_idx).0.contents.short_channel_id;
8230 dst_chan_ids.push(dst_chan_id);
8231 let path = vec![&nodes[routing_node], &nodes[dst_idx]];
8232 expected_paths.push(path);
8234 let expected_paths: Vec<&[&Node]> = expected_paths.iter().map(|route| route.as_slice()).collect();
8236 // Create a route for each amount
8237 let example_amount = 100000;
8238 let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[src_idx], nodes[dst_idx], example_amount);
8239 let sample_path = route.paths.pop().unwrap();
8240 for i in 0..routing_node_count {
8241 let routing_node = 2 + i;
8242 let mut path = sample_path.clone();
8243 path.hops[0].pubkey = nodes[routing_node].node.get_our_node_id();
8244 path.hops[0].short_channel_id = src_chan_ids[i];
8245 path.hops[1].pubkey = nodes[dst_idx].node.get_our_node_id();
8246 path.hops[1].short_channel_id = dst_chan_ids[i];
8247 path.hops[1].fee_msat = msat_amounts[i];
8248 route.paths.push(path);
8251 // Send payment with manually set total_msat
8252 let payment_id = PaymentId(nodes[src_idx].keys_manager.backing.get_secure_random_bytes());
8253 let onion_session_privs = nodes[src_idx].node.test_add_new_pending_payment(our_payment_hash,
8254 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8255 nodes[src_idx].node.test_send_payment_internal(&route, our_payment_hash,
8256 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8257 check_added_monitors!(nodes[src_idx], expected_paths.len());
8259 let mut events = nodes[src_idx].node.get_and_clear_pending_msg_events();
8260 assert_eq!(events.len(), expected_paths.len());
8261 let mut amount_received = 0;
8262 for (path_idx, expected_path) in expected_paths.iter().enumerate() {
8263 let ev = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &mut events);
8265 let current_path_amount = msat_amounts[path_idx];
8266 amount_received += current_path_amount;
8267 let became_claimable_now = amount_received >= total_msat && amount_received - current_path_amount < total_msat;
8268 pass_along_path(&nodes[src_idx], expected_path, amount_received, our_payment_hash.clone(), Some(our_payment_secret), ev, became_claimable_now, None);
8271 claim_payment_along_route(&nodes[src_idx], &expected_paths, false, our_payment_preimage);
8275 fn test_overshoot_mpp() {
8276 do_test_overshoot_mpp(&[100_000, 101_000], 200_000);
8277 do_test_overshoot_mpp(&[100_000, 10_000, 100_000], 200_000);
8281 fn test_simple_mpp() {
8282 // Simple test of sending a multi-path payment.
8283 let chanmon_cfgs = create_chanmon_cfgs(4);
8284 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
8285 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
8286 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
8288 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8289 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8290 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8291 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8293 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
8294 let path = route.paths[0].clone();
8295 route.paths.push(path);
8296 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
8297 route.paths[0].hops[0].short_channel_id = chan_1_id;
8298 route.paths[0].hops[1].short_channel_id = chan_3_id;
8299 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
8300 route.paths[1].hops[0].short_channel_id = chan_2_id;
8301 route.paths[1].hops[1].short_channel_id = chan_4_id;
8302 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
8303 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
8307 fn test_preimage_storage() {
8308 // Simple test of payment preimage storage allowing no client-side storage to claim payments
8309 let chanmon_cfgs = create_chanmon_cfgs(2);
8310 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8311 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8312 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8314 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8317 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 7200, None).unwrap();
8318 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8319 nodes[0].node.send_payment_with_route(&route, payment_hash,
8320 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8321 check_added_monitors!(nodes[0], 1);
8322 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8323 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8324 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8325 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8327 // Note that after leaving the above scope we have no knowledge of any arguments or return
8328 // values from previous calls.
8329 expect_pending_htlcs_forwardable!(nodes[1]);
8330 let events = nodes[1].node.get_and_clear_pending_events();
8331 assert_eq!(events.len(), 1);
8333 Event::PaymentClaimable { ref purpose, .. } => {
8335 PaymentPurpose::InvoicePayment { payment_preimage, .. } => {
8336 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8338 _ => panic!("expected PaymentPurpose::InvoicePayment")
8341 _ => panic!("Unexpected event"),
8346 fn test_bad_secret_hash() {
8347 // Simple test of unregistered payment hash/invalid payment secret handling
8348 let chanmon_cfgs = create_chanmon_cfgs(2);
8349 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8350 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8351 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8353 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8355 let random_payment_hash = PaymentHash([42; 32]);
8356 let random_payment_secret = PaymentSecret([43; 32]);
8357 let (our_payment_hash, our_payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 2, None).unwrap();
8358 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8360 // All the below cases should end up being handled exactly identically, so we macro the
8361 // resulting events.
8362 macro_rules! handle_unknown_invalid_payment_data {
8363 ($payment_hash: expr) => {
8364 check_added_monitors!(nodes[0], 1);
8365 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8366 let payment_event = SendEvent::from_event(events.pop().unwrap());
8367 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8368 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8370 // We have to forward pending HTLCs once to process the receipt of the HTLC and then
8371 // again to process the pending backwards-failure of the HTLC
8372 expect_pending_htlcs_forwardable!(nodes[1]);
8373 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment{ payment_hash: $payment_hash }]);
8374 check_added_monitors!(nodes[1], 1);
8376 // We should fail the payment back
8377 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
8378 match events.pop().unwrap() {
8379 MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8380 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
8381 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
8383 _ => panic!("Unexpected event"),
8388 let expected_error_code = 0x4000|15; // incorrect_or_unknown_payment_details
8389 // Error data is the HTLC value (100,000) and current block height
8390 let expected_error_data = [0, 0, 0, 0, 0, 1, 0x86, 0xa0, 0, 0, 0, CHAN_CONFIRM_DEPTH as u8];
8392 // Send a payment with the right payment hash but the wrong payment secret
8393 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
8394 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
8395 handle_unknown_invalid_payment_data!(our_payment_hash);
8396 expect_payment_failed!(nodes[0], our_payment_hash, true, expected_error_code, expected_error_data);
8398 // Send a payment with a random payment hash, but the right payment secret
8399 nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8400 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8401 handle_unknown_invalid_payment_data!(random_payment_hash);
8402 expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8404 // Send a payment with a random payment hash and random payment secret
8405 nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8406 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8407 handle_unknown_invalid_payment_data!(random_payment_hash);
8408 expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8412 fn test_update_err_monitor_lockdown() {
8413 // Our monitor will lock update of local commitment transaction if a broadcastion condition
8414 // has been fulfilled (either force-close from Channel or block height requiring a HTLC-
8415 // timeout). Trying to update monitor after lockdown should return a ChannelMonitorUpdateStatus
8418 // This scenario may happen in a watchtower setup, where watchtower process a block height
8419 // triggering a timeout while a slow-block-processing ChannelManager receives a local signed
8420 // commitment at same time.
8422 let chanmon_cfgs = create_chanmon_cfgs(2);
8423 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8424 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8425 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8427 // Create some initial channel
8428 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8429 let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8431 // Rebalance the network to generate htlc in the two directions
8432 send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8434 // Route a HTLC from node 0 to node 1 (but don't settle)
8435 let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
8437 // Copy ChainMonitor to simulate a watchtower and update block height of node 0 until its ChannelMonitor timeout HTLC onchain
8438 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8439 let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
8440 let persister = test_utils::TestPersister::new();
8443 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8444 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8445 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8446 assert!(new_monitor == *monitor);
8449 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8450 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8453 let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8454 // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8455 // transaction lock time requirements here.
8456 chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 200));
8457 watchtower.chain_monitor.block_connected(&block, 200);
8459 // Try to update ChannelMonitor
8460 nodes[1].node.claim_funds(preimage);
8461 check_added_monitors!(nodes[1], 1);
8462 expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
8464 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8465 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
8466 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
8468 let mut node_0_per_peer_lock;
8469 let mut node_0_peer_state_lock;
8470 if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
8471 if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8472 assert_eq!(watchtower.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8473 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8474 } else { assert!(false); }
8479 // Our local monitor is in-sync and hasn't processed yet timeout
8480 check_added_monitors!(nodes[0], 1);
8481 let events = nodes[0].node.get_and_clear_pending_events();
8482 assert_eq!(events.len(), 1);
8486 fn test_concurrent_monitor_claim() {
8487 // Watchtower A receives block, broadcasts state N, then channel receives new state N+1,
8488 // sending it to both watchtowers, Bob accepts N+1, then receives block and broadcasts
8489 // the latest state N+1, Alice rejects state N+1, but Bob has already broadcast it,
8490 // state N+1 confirms. Alice claims output from state N+1.
8492 let chanmon_cfgs = create_chanmon_cfgs(2);
8493 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8494 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8495 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8497 // Create some initial channel
8498 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8499 let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8501 // Rebalance the network to generate htlc in the two directions
8502 send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8504 // Route a HTLC from node 0 to node 1 (but don't settle)
8505 route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
8507 // Copy ChainMonitor to simulate watchtower Alice and update block height her ChannelMonitor timeout HTLC onchain
8508 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8509 let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
8510 let persister = test_utils::TestPersister::new();
8511 let alice_broadcaster = test_utils::TestBroadcaster::with_blocks(
8512 Arc::new(Mutex::new(nodes[0].blocks.lock().unwrap().clone())),
8514 let watchtower_alice = {
8516 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8517 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8518 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8519 assert!(new_monitor == *monitor);
8522 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &alice_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8523 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8526 let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8527 // Make Alice aware of enough blocks that it doesn't think we're violating transaction lock time
8528 // requirements here.
8529 const HTLC_TIMEOUT_BROADCAST: u32 = CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS;
8530 alice_broadcaster.blocks.lock().unwrap().resize((HTLC_TIMEOUT_BROADCAST) as usize, (block.clone(), HTLC_TIMEOUT_BROADCAST));
8531 watchtower_alice.chain_monitor.block_connected(&block, HTLC_TIMEOUT_BROADCAST);
8533 // Watchtower Alice should have broadcast a commitment/HTLC-timeout
8535 let mut txn = alice_broadcaster.txn_broadcast();
8536 assert_eq!(txn.len(), 2);
8540 // Copy ChainMonitor to simulate watchtower Bob and make it receive a commitment update first.
8541 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8542 let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
8543 let persister = test_utils::TestPersister::new();
8544 let bob_broadcaster = test_utils::TestBroadcaster::with_blocks(Arc::clone(&alice_broadcaster.blocks));
8545 let watchtower_bob = {
8547 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8548 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8549 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8550 assert!(new_monitor == *monitor);
8553 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &bob_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8554 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
8557 watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST - 1);
8559 // Route another payment to generate another update with still previous HTLC pending
8560 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
8561 nodes[1].node.send_payment_with_route(&route, payment_hash,
8562 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8563 check_added_monitors!(nodes[1], 1);
8565 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8566 assert_eq!(updates.update_add_htlcs.len(), 1);
8567 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
8569 let mut node_0_per_peer_lock;
8570 let mut node_0_peer_state_lock;
8571 if let ChannelPhase::Funded(ref mut channel) = get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_1.2) {
8572 if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8573 // Watchtower Alice should already have seen the block and reject the update
8574 assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::PermanentFailure);
8575 assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8576 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8577 } else { assert!(false); }
8582 // Our local monitor is in-sync and hasn't processed yet timeout
8583 check_added_monitors!(nodes[0], 1);
8585 //// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
8586 watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST);
8588 // Watchtower Bob should have broadcast a commitment/HTLC-timeout
8591 let mut txn = bob_broadcaster.txn_broadcast();
8592 assert_eq!(txn.len(), 2);
8593 bob_state_y = txn.remove(0);
8596 // We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
8597 let height = HTLC_TIMEOUT_BROADCAST + 1;
8598 connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
8599 check_closed_broadcast(&nodes[0], 1, true);
8600 check_closed_event!(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false,
8601 [nodes[1].node.get_our_node_id()], 100000);
8602 watchtower_alice.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, vec![bob_state_y.clone()]), height);
8603 check_added_monitors(&nodes[0], 1);
8605 let htlc_txn = alice_broadcaster.txn_broadcast();
8606 assert_eq!(htlc_txn.len(), 2);
8607 check_spends!(htlc_txn[0], bob_state_y);
8608 // Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for
8609 // it. However, she should, because it now has an invalid parent.
8610 check_spends!(htlc_txn[1], alice_state);
8615 fn test_pre_lockin_no_chan_closed_update() {
8616 // Test that if a peer closes a channel in response to a funding_created message we don't
8617 // generate a channel update (as the channel cannot appear on chain without a funding_signed
8620 // Doing so would imply a channel monitor update before the initial channel monitor
8621 // registration, violating our API guarantees.
8623 // Previously, full_stack_target managed to hit this case by opening then closing a channel,
8624 // then opening a second channel with the same funding output as the first (which is not
8625 // rejected because the first channel does not exist in the ChannelManager) and closing it
8626 // before receiving funding_signed.
8627 let chanmon_cfgs = create_chanmon_cfgs(2);
8628 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8629 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8630 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8632 // Create an initial channel
8633 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8634 let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8635 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8636 let accept_chan_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
8637 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_chan_msg);
8639 // Move the first channel through the funding flow...
8640 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
8642 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
8643 check_added_monitors!(nodes[0], 0);
8645 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8646 let channel_id = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
8647 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
8648 assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
8649 check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("Hi".to_string()) }, true,
8650 [nodes[1].node.get_our_node_id(); 2], 100000);
8654 fn test_htlc_no_detection() {
8655 // This test is a mutation to underscore the detection logic bug we had
8656 // before #653. HTLC value routed is above the remaining balance, thus
8657 // inverting HTLC and `to_remote` output. HTLC will come second and
8658 // it wouldn't be seen by pre-#653 detection as we were enumerate()'ing
8659 // on a watched outputs vector (Vec<TxOut>) thus implicitly relying on
8660 // outputs order detection for correct spending children filtring.
8662 let chanmon_cfgs = create_chanmon_cfgs(2);
8663 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8664 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8665 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8667 // Create some initial channels
8668 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8670 send_payment(&nodes[0], &vec!(&nodes[1])[..], 1_000_000);
8671 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 2_000_000);
8672 let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
8673 assert_eq!(local_txn[0].input.len(), 1);
8674 assert_eq!(local_txn[0].output.len(), 3);
8675 check_spends!(local_txn[0], chan_1.3);
8677 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
8678 let block = create_dummy_block(nodes[0].best_block_hash(), 42, vec![local_txn[0].clone()]);
8679 connect_block(&nodes[0], &block);
8680 // We deliberately connect the local tx twice as this should provoke a failure calling
8681 // this test before #653 fix.
8682 chain::Listen::block_connected(&nodes[0].chain_monitor.chain_monitor, &block, nodes[0].best_block_info().1 + 1);
8683 check_closed_broadcast!(nodes[0], true);
8684 check_added_monitors!(nodes[0], 1);
8685 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
8686 connect_blocks(&nodes[0], TEST_FINAL_CLTV);
8688 let htlc_timeout = {
8689 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8690 assert_eq!(node_txn.len(), 1);
8691 assert_eq!(node_txn[0].input.len(), 1);
8692 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8693 check_spends!(node_txn[0], local_txn[0]);
8697 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![htlc_timeout.clone()]));
8698 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
8699 expect_payment_failed!(nodes[0], our_payment_hash, false);
8702 fn do_test_onchain_htlc_settlement_after_close(broadcast_alice: bool, go_onchain_before_fulfill: bool) {
8703 // If we route an HTLC, then learn the HTLC's preimage after the upstream channel has been
8704 // force-closed, we must claim that HTLC on-chain. (Given an HTLC forwarded from Alice --> Bob -->
8705 // Carol, Alice would be the upstream node, and Carol the downstream.)
8707 // Steps of the test:
8708 // 1) Alice sends a HTLC to Carol through Bob.
8709 // 2) Carol doesn't settle the HTLC.
8710 // 3) If broadcast_alice is true, Alice force-closes her channel with Bob. Else Bob force closes.
8711 // Steps 4 and 5 may be reordered depending on go_onchain_before_fulfill.
8712 // 4) Bob sees the Alice's commitment on his chain or vice versa. An offered output is present
8713 // but can't be claimed as Bob doesn't have yet knowledge of the preimage.
8714 // 5) Carol release the preimage to Bob off-chain.
8715 // 6) Bob claims the offered output on the broadcasted commitment.
8716 let chanmon_cfgs = create_chanmon_cfgs(3);
8717 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8718 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8719 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8721 // Create some initial channels
8722 let chan_ab = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8723 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001);
8725 // Steps (1) and (2):
8726 // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
8727 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
8729 // Check that Alice's commitment transaction now contains an output for this HTLC.
8730 let alice_txn = get_local_commitment_txn!(nodes[0], chan_ab.2);
8731 check_spends!(alice_txn[0], chan_ab.3);
8732 assert_eq!(alice_txn[0].output.len(), 2);
8733 check_spends!(alice_txn[1], alice_txn[0]); // 2nd transaction is a non-final HTLC-timeout
8734 assert_eq!(alice_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8735 assert_eq!(alice_txn.len(), 2);
8737 // Steps (3) and (4):
8738 // If `go_onchain_before_fufill`, broadcast the relevant commitment transaction and check that Bob
8739 // responds by (1) broadcasting a channel update and (2) adding a new ChannelMonitor.
8740 let mut force_closing_node = 0; // Alice force-closes
8741 let mut counterparty_node = 1; // Bob if Alice force-closes
8744 if !broadcast_alice {
8745 force_closing_node = 1;
8746 counterparty_node = 0;
8748 nodes[force_closing_node].node.force_close_broadcasting_latest_txn(&chan_ab.2, &nodes[counterparty_node].node.get_our_node_id()).unwrap();
8749 check_closed_broadcast!(nodes[force_closing_node], true);
8750 check_added_monitors!(nodes[force_closing_node], 1);
8751 check_closed_event!(nodes[force_closing_node], 1, ClosureReason::HolderForceClosed, [nodes[counterparty_node].node.get_our_node_id()], 100000);
8752 if go_onchain_before_fulfill {
8753 let txn_to_broadcast = match broadcast_alice {
8754 true => alice_txn.clone(),
8755 false => get_local_commitment_txn!(nodes[1], chan_ab.2)
8757 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8758 if broadcast_alice {
8759 check_closed_broadcast!(nodes[1], true);
8760 check_added_monitors!(nodes[1], 1);
8761 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8766 // Carol then claims the funds and sends an update_fulfill message to Bob, and they go through the
8767 // process of removing the HTLC from their commitment transactions.
8768 nodes[2].node.claim_funds(payment_preimage);
8769 check_added_monitors!(nodes[2], 1);
8770 expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
8772 let carol_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
8773 assert!(carol_updates.update_add_htlcs.is_empty());
8774 assert!(carol_updates.update_fail_htlcs.is_empty());
8775 assert!(carol_updates.update_fail_malformed_htlcs.is_empty());
8776 assert!(carol_updates.update_fee.is_none());
8777 assert_eq!(carol_updates.update_fulfill_htlcs.len(), 1);
8779 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &carol_updates.update_fulfill_htlcs[0]);
8780 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], if go_onchain_before_fulfill || force_closing_node == 1 { None } else { Some(1000) }, false, false);
8781 // If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
8782 if !go_onchain_before_fulfill && broadcast_alice {
8783 let events = nodes[1].node.get_and_clear_pending_msg_events();
8784 assert_eq!(events.len(), 1);
8786 MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
8787 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8789 _ => panic!("Unexpected event"),
8792 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &carol_updates.commitment_signed);
8793 // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
8794 // Carol<->Bob's updated commitment transaction info.
8795 check_added_monitors!(nodes[1], 2);
8797 let events = nodes[1].node.get_and_clear_pending_msg_events();
8798 assert_eq!(events.len(), 2);
8799 let bob_revocation = match events[0] {
8800 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8801 assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8804 _ => panic!("Unexpected event"),
8806 let bob_updates = match events[1] {
8807 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
8808 assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8811 _ => panic!("Unexpected event"),
8814 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revocation);
8815 check_added_monitors!(nodes[2], 1);
8816 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_updates.commitment_signed);
8817 check_added_monitors!(nodes[2], 1);
8819 let events = nodes[2].node.get_and_clear_pending_msg_events();
8820 assert_eq!(events.len(), 1);
8821 let carol_revocation = match events[0] {
8822 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8823 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
8826 _ => panic!("Unexpected event"),
8828 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &carol_revocation);
8829 check_added_monitors!(nodes[1], 1);
8831 // If this test requires the force-closed channel to not be on-chain until after the fulfill,
8832 // here's where we put said channel's commitment tx on-chain.
8833 let mut txn_to_broadcast = alice_txn.clone();
8834 if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn!(nodes[1], chan_ab.2); }
8835 if !go_onchain_before_fulfill {
8836 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8837 // If Bob was the one to force-close, he will have already passed these checks earlier.
8838 if broadcast_alice {
8839 check_closed_broadcast!(nodes[1], true);
8840 check_added_monitors!(nodes[1], 1);
8841 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8843 let mut bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8844 if broadcast_alice {
8845 assert_eq!(bob_txn.len(), 1);
8846 check_spends!(bob_txn[0], txn_to_broadcast[0]);
8848 assert_eq!(bob_txn.len(), 2);
8849 check_spends!(bob_txn[0], chan_ab.3);
8854 // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
8855 // broadcasted commitment transaction.
8857 let script_weight = match broadcast_alice {
8858 true => OFFERED_HTLC_SCRIPT_WEIGHT,
8859 false => ACCEPTED_HTLC_SCRIPT_WEIGHT
8861 // If Alice force-closed, Bob only broadcasts a HTLC-output-claiming transaction. Otherwise,
8862 // Bob force-closed and broadcasts the commitment transaction along with a
8863 // HTLC-output-claiming transaction.
8864 let bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
8865 if broadcast_alice {
8866 assert_eq!(bob_txn.len(), 1);
8867 check_spends!(bob_txn[0], txn_to_broadcast[0]);
8868 assert_eq!(bob_txn[0].input[0].witness.last().unwrap().len(), script_weight);
8870 assert_eq!(bob_txn.len(), 2);
8871 check_spends!(bob_txn[1], txn_to_broadcast[0]);
8872 assert_eq!(bob_txn[1].input[0].witness.last().unwrap().len(), script_weight);
8878 fn test_onchain_htlc_settlement_after_close() {
8879 do_test_onchain_htlc_settlement_after_close(true, true);
8880 do_test_onchain_htlc_settlement_after_close(false, true); // Technically redundant, but may as well
8881 do_test_onchain_htlc_settlement_after_close(true, false);
8882 do_test_onchain_htlc_settlement_after_close(false, false);
8886 fn test_duplicate_temporary_channel_id_from_different_peers() {
8887 // Tests that we can accept two different `OpenChannel` requests with the same
8888 // `temporary_channel_id`, as long as they are from different peers.
8889 let chanmon_cfgs = create_chanmon_cfgs(3);
8890 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8891 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8892 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8894 // Create an first channel channel
8895 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8896 let mut open_chan_msg_chan_1_0 = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8898 // Create an second channel
8899 nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 43, None).unwrap();
8900 let mut open_chan_msg_chan_2_0 = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8902 // Modify the `OpenChannel` from `nodes[2]` to `nodes[0]` to ensure that it uses the same
8903 // `temporary_channel_id` as the `OpenChannel` from nodes[1] to nodes[0].
8904 open_chan_msg_chan_2_0.temporary_channel_id = open_chan_msg_chan_1_0.temporary_channel_id;
8906 // Assert that `nodes[0]` can accept both `OpenChannel` requests, even though they use the same
8907 // `temporary_channel_id` as they are from different peers.
8908 nodes[0].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_chan_msg_chan_1_0);
8910 let events = nodes[0].node.get_and_clear_pending_msg_events();
8911 assert_eq!(events.len(), 1);
8913 MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8914 assert_eq!(node_id, &nodes[1].node.get_our_node_id());
8915 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8917 _ => panic!("Unexpected event"),
8921 nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), &open_chan_msg_chan_2_0);
8923 let events = nodes[0].node.get_and_clear_pending_msg_events();
8924 assert_eq!(events.len(), 1);
8926 MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8927 assert_eq!(node_id, &nodes[2].node.get_our_node_id());
8928 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8930 _ => panic!("Unexpected event"),
8936 fn test_duplicate_chan_id() {
8937 // Test that if a given peer tries to open a channel with the same channel_id as one that is
8938 // already open we reject it and keep the old channel.
8940 // Previously, full_stack_target managed to figure out that if you tried to open two channels
8941 // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
8942 // the existing channel when we detect the duplicate new channel, screwing up our monitor
8943 // updating logic for the existing channel.
8944 let chanmon_cfgs = create_chanmon_cfgs(2);
8945 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8946 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8947 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8949 // Create an initial channel
8950 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
8951 let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8952 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8953 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
8955 // Try to create a second channel with the same temporary_channel_id as the first and check
8956 // that it is rejected.
8957 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8959 let events = nodes[1].node.get_and_clear_pending_msg_events();
8960 assert_eq!(events.len(), 1);
8962 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
8963 // Technically, at this point, nodes[1] would be justified in thinking both the
8964 // first (valid) and second (invalid) channels are closed, given they both have
8965 // the same non-temporary channel_id. However, currently we do not, so we just
8966 // move forward with it.
8967 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
8968 assert_eq!(node_id, nodes[0].node.get_our_node_id());
8970 _ => panic!("Unexpected event"),
8974 // Move the first channel through the funding flow...
8975 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
8977 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
8978 check_added_monitors!(nodes[0], 0);
8980 let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8981 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
8983 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
8984 assert_eq!(added_monitors.len(), 1);
8985 assert_eq!(added_monitors[0].0, funding_output);
8986 added_monitors.clear();
8988 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
8990 let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
8992 let funding_outpoint = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index };
8993 let channel_id = funding_outpoint.to_channel_id();
8995 // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
8998 // First try to open a second channel with a temporary channel id equal to the txid-based one.
8999 // Technically this is allowed by the spec, but we don't support it and there's little reason
9000 // to. Still, it shouldn't cause any other issues.
9001 open_chan_msg.temporary_channel_id = channel_id;
9002 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
9004 let events = nodes[1].node.get_and_clear_pending_msg_events();
9005 assert_eq!(events.len(), 1);
9007 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9008 // Technically, at this point, nodes[1] would be justified in thinking both
9009 // channels are closed, but currently we do not, so we just move forward with it.
9010 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9011 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9013 _ => panic!("Unexpected event"),
9017 // Now try to create a second channel which has a duplicate funding output.
9018 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9019 let open_chan_2_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9020 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_2_msg);
9021 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9022 create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); // Get and check the FundingGenerationReady event
9024 let (_, funding_created) = {
9025 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9026 let mut a_peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9027 // Once we call `get_funding_created` the channel has a duplicate channel_id as
9028 // another channel in the ChannelManager - an invalid state. Thus, we'd panic later when we
9029 // try to create another channel. Instead, we drop the channel entirely here (leaving the
9030 // channelmanager in a possibly nonsense state instead).
9031 match a_peer_state.channel_by_id.remove(&open_chan_2_msg.temporary_channel_id).unwrap() {
9032 ChannelPhase::UnfundedOutboundV1(chan) => {
9033 let logger = test_utils::TestLogger::new();
9034 chan.get_funding_created(tx.clone(), funding_outpoint, &&logger).map_err(|_| ()).unwrap()
9036 _ => panic!("Unexpected ChannelPhase variant"),
9039 check_added_monitors!(nodes[0], 0);
9040 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9041 // At this point we'll look up if the channel_id is present and immediately fail the channel
9042 // without trying to persist the `ChannelMonitor`.
9043 check_added_monitors!(nodes[1], 0);
9045 // ...still, nodes[1] will reject the duplicate channel.
9047 let events = nodes[1].node.get_and_clear_pending_msg_events();
9048 assert_eq!(events.len(), 1);
9050 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9051 // Technically, at this point, nodes[1] would be justified in thinking both
9052 // channels are closed, but currently we do not, so we just move forward with it.
9053 assert_eq!(msg.channel_id, channel_id);
9054 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9056 _ => panic!("Unexpected event"),
9060 // finally, finish creating the original channel and send a payment over it to make sure
9061 // everything is functional.
9062 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
9064 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9065 assert_eq!(added_monitors.len(), 1);
9066 assert_eq!(added_monitors[0].0, funding_output);
9067 added_monitors.clear();
9069 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9071 let events_4 = nodes[0].node.get_and_clear_pending_events();
9072 assert_eq!(events_4.len(), 0);
9073 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9074 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9076 let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9077 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9078 update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9080 send_payment(&nodes[0], &[&nodes[1]], 8000000);
9084 fn test_error_chans_closed() {
9085 // Test that we properly handle error messages, closing appropriate channels.
9087 // Prior to #787 we'd allow a peer to make us force-close a channel we had with a different
9088 // peer. The "real" fix for that is to index channels with peers_ids, however in the mean time
9089 // we can test various edge cases around it to ensure we don't regress.
9090 let chanmon_cfgs = create_chanmon_cfgs(3);
9091 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9092 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9093 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9095 // Create some initial channels
9096 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9097 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9098 let chan_3 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001);
9100 assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9101 assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
9102 assert_eq!(nodes[2].node.list_usable_channels().len(), 1);
9104 // Closing a channel from a different peer has no effect
9105 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_3.2, data: "ERR".to_owned() });
9106 assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9108 // Closing one channel doesn't impact others
9109 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_2.2, data: "ERR".to_owned() });
9110 check_added_monitors!(nodes[0], 1);
9111 check_closed_broadcast!(nodes[0], false);
9112 check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9113 [nodes[1].node.get_our_node_id()], 100000);
9114 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
9115 assert_eq!(nodes[0].node.list_usable_channels().len(), 2);
9116 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_1.2 || nodes[0].node.list_usable_channels()[1].channel_id == chan_1.2);
9117 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2 || nodes[0].node.list_usable_channels()[1].channel_id == chan_3.2);
9119 // A null channel ID should close all channels
9120 let _chan_4 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9121 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: ChannelId::new_zero(), data: "ERR".to_owned() });
9122 check_added_monitors!(nodes[0], 2);
9123 check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9124 [nodes[1].node.get_our_node_id(); 2], 100000);
9125 let events = nodes[0].node.get_and_clear_pending_msg_events();
9126 assert_eq!(events.len(), 2);
9128 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9129 assert_eq!(msg.contents.flags & 2, 2);
9131 _ => panic!("Unexpected event"),
9134 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9135 assert_eq!(msg.contents.flags & 2, 2);
9137 _ => panic!("Unexpected event"),
9139 // Note that at this point users of a standard PeerHandler will end up calling
9140 // peer_disconnected.
9141 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9142 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9144 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9145 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9146 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9150 fn test_invalid_funding_tx() {
9151 // Test that we properly handle invalid funding transactions sent to us from a peer.
9153 // Previously, all other major lightning implementations had failed to properly sanitize
9154 // funding transactions from their counterparties, leading to a multi-implementation critical
9155 // security vulnerability (though we always sanitized properly, we've previously had
9156 // un-released crashes in the sanitization process).
9158 // Further, if the funding transaction is consensus-valid, confirms, and is later spent, we'd
9159 // previously have crashed in `ChannelMonitor` even though we closed the channel as bogus and
9160 // gave up on it. We test this here by generating such a transaction.
9161 let chanmon_cfgs = create_chanmon_cfgs(2);
9162 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9163 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9164 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9166 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_000, 42, None).unwrap();
9167 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()));
9168 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
9170 let (temporary_channel_id, mut tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
9172 // Create a witness program which can be spent by a 4-empty-stack-elements witness and which is
9173 // 136 bytes long. This matches our "accepted HTLC preimage spend" matching, previously causing
9174 // a panic as we'd try to extract a 32 byte preimage from a witness element without checking
9176 let mut wit_program: Vec<u8> = channelmonitor::deliberately_bogus_accepted_htlc_witness_program();
9177 let wit_program_script: Script = wit_program.into();
9178 for output in tx.output.iter_mut() {
9179 // Make the confirmed funding transaction have a bogus script_pubkey
9180 output.script_pubkey = Script::new_v0_p2wsh(&wit_program_script.wscript_hash());
9183 nodes[0].node.funding_transaction_generated_unchecked(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone(), 0).unwrap();
9184 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()));
9185 check_added_monitors!(nodes[1], 1);
9186 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9188 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
9189 check_added_monitors!(nodes[0], 1);
9190 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9192 let events_1 = nodes[0].node.get_and_clear_pending_events();
9193 assert_eq!(events_1.len(), 0);
9195 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9196 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9197 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
9199 let expected_err = "funding tx had wrong script/value or output index";
9200 confirm_transaction_at(&nodes[1], &tx, 1);
9201 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() },
9202 [nodes[0].node.get_our_node_id()], 100000);
9203 check_added_monitors!(nodes[1], 1);
9204 let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
9205 assert_eq!(events_2.len(), 1);
9206 if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
9207 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
9208 if let msgs::ErrorAction::SendErrorMessage { msg } = action {
9209 assert_eq!(msg.data, "Channel closed because of an exception: ".to_owned() + expected_err);
9210 } else { panic!(); }
9211 } else { panic!(); }
9212 assert_eq!(nodes[1].node.list_channels().len(), 0);
9214 // Now confirm a spend of the (bogus) funding transaction. As long as the witness is 5 elements
9215 // long the ChannelMonitor will try to read 32 bytes from the second-to-last element, panicing
9216 // as its not 32 bytes long.
9217 let mut spend_tx = Transaction {
9218 version: 2i32, lock_time: PackedLockTime::ZERO,
9219 input: tx.output.iter().enumerate().map(|(idx, _)| TxIn {
9220 previous_output: BitcoinOutPoint {
9224 script_sig: Script::new(),
9225 sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
9226 witness: Witness::from_vec(channelmonitor::deliberately_bogus_accepted_htlc_witness())
9228 output: vec![TxOut {
9230 script_pubkey: Script::new(),
9233 check_spends!(spend_tx, tx);
9234 mine_transaction(&nodes[1], &spend_tx);
9238 fn test_coinbase_funding_tx() {
9239 // Miners are able to fund channels directly from coinbase transactions, however
9240 // by consensus rules, outputs of a coinbase transaction are encumbered by a 100
9241 // block maturity timelock. To ensure that a (non-0conf) channel like this is enforceable
9242 // on-chain, the minimum depth is updated to 100 blocks for coinbase funding transactions.
9244 // Note that 0conf channels with coinbase funding transactions are unaffected and are
9245 // immediately operational after opening.
9246 let chanmon_cfgs = create_chanmon_cfgs(2);
9247 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9248 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9249 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9251 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9252 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9254 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9255 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9257 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9259 // Create the coinbase funding transaction.
9260 let (temporary_channel_id, tx, _) = create_coinbase_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
9262 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9263 check_added_monitors!(nodes[0], 0);
9264 let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9266 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9267 check_added_monitors!(nodes[1], 1);
9268 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9270 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9272 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
9273 check_added_monitors!(nodes[0], 1);
9275 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9276 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
9278 // Starting at height 0, we "confirm" the coinbase at height 1.
9279 confirm_transaction_at(&nodes[0], &tx, 1);
9280 // We connect 98 more blocks to have 99 confirmations for the coinbase transaction.
9281 connect_blocks(&nodes[0], COINBASE_MATURITY - 2);
9282 // Check that we have no pending message events (we have not queued a `channel_ready` yet).
9283 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
9284 // Now connect one more block which results in 100 confirmations of the coinbase transaction.
9285 connect_blocks(&nodes[0], 1);
9286 // There should now be a `channel_ready` which can be handled.
9287 let _ = &nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(&nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
9289 confirm_transaction_at(&nodes[1], &tx, 1);
9290 connect_blocks(&nodes[1], COINBASE_MATURITY - 2);
9291 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
9292 connect_blocks(&nodes[1], 1);
9293 expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
9294 create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
9297 fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
9298 // In the first version of the chain::Confirm interface, after a refactor was made to not
9299 // broadcast CSV-locked transactions until their CSV lock is up, we wouldn't reliably broadcast
9300 // transactions after a `transactions_confirmed` call. Specifically, if the chain, provided via
9301 // `best_block_updated` is at height N, and a transaction output which we wish to spend at
9302 // height N-1 (due to a CSV to height N-1) is provided at height N, we will not broadcast the
9303 // spending transaction until height N+1 (or greater). This was due to the way
9304 // `ChannelMonitor::transactions_confirmed` worked, only checking if we should broadcast a
9305 // spending transaction at the height the input transaction was confirmed at, not whether we
9306 // should broadcast a spending transaction at the current height.
9307 // A second, similar, issue involved failing HTLCs backwards - because we only provided the
9308 // height at which transactions were confirmed to `OnchainTx::update_claims_view`, it wasn't
9309 // aware that the anti-reorg-delay had, in fact, already expired, waiting to fail-backwards
9310 // until we learned about an additional block.
9312 // As an additional check, if `test_height_before_timelock` is set, we instead test that we
9313 // aren't broadcasting transactions too early (ie not broadcasting them at all).
9314 let chanmon_cfgs = create_chanmon_cfgs(3);
9315 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9316 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9317 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9318 *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
9320 create_announced_chan_between_nodes(&nodes, 0, 1);
9321 let (chan_announce, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
9322 let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
9323 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
9324 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9326 nodes[1].node.force_close_broadcasting_latest_txn(&channel_id, &nodes[2].node.get_our_node_id()).unwrap();
9327 check_closed_broadcast!(nodes[1], true);
9328 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
9329 check_added_monitors!(nodes[1], 1);
9330 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9331 assert_eq!(node_txn.len(), 1);
9333 let conf_height = nodes[1].best_block_info().1;
9334 if !test_height_before_timelock {
9335 connect_blocks(&nodes[1], 24 * 6);
9337 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9338 &nodes[1].get_block_header(conf_height), &[(0, &node_txn[0])], conf_height);
9339 if test_height_before_timelock {
9340 // If we confirmed the close transaction, but timelocks have not yet expired, we should not
9341 // generate any events or broadcast any transactions
9342 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
9343 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
9345 // We should broadcast an HTLC transaction spending our funding transaction first
9346 let spending_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9347 assert_eq!(spending_txn.len(), 2);
9348 assert_eq!(spending_txn[0].txid(), node_txn[0].txid());
9349 check_spends!(spending_txn[1], node_txn[0]);
9350 // We should also generate a SpendableOutputs event with the to_self output (as its
9352 let descriptor_spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
9353 assert_eq!(descriptor_spend_txn.len(), 1);
9355 // If we also discover that the HTLC-Timeout transaction was confirmed some time ago, we
9356 // should immediately fail-backwards the HTLC to the previous hop, without waiting for an
9357 // additional block built on top of the current chain.
9358 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9359 &nodes[1].get_block_header(conf_height + 1), &[(0, &spending_txn[1])], conf_height + 1);
9360 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: channel_id }]);
9361 check_added_monitors!(nodes[1], 1);
9363 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9364 assert!(updates.update_add_htlcs.is_empty());
9365 assert!(updates.update_fulfill_htlcs.is_empty());
9366 assert_eq!(updates.update_fail_htlcs.len(), 1);
9367 assert!(updates.update_fail_malformed_htlcs.is_empty());
9368 assert!(updates.update_fee.is_none());
9369 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
9370 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
9371 expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_announce.contents.short_channel_id, true);
9376 fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
9377 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(false);
9378 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(true);
9381 fn do_test_dup_htlc_second_rejected(test_for_second_fail_panic: bool) {
9382 let chanmon_cfgs = create_chanmon_cfgs(2);
9383 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9384 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9385 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9387 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9389 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
9390 .with_bolt11_features(nodes[1].node.invoice_features()).unwrap();
9391 let route = get_route!(nodes[0], payment_params, 10_000).unwrap();
9393 let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
9396 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9397 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
9398 check_added_monitors!(nodes[0], 1);
9399 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9400 assert_eq!(events.len(), 1);
9401 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9402 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9403 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9405 expect_pending_htlcs_forwardable!(nodes[1]);
9406 expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 10_000);
9409 // Note that we use a different PaymentId here to allow us to duplicativly pay
9410 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9411 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_secret.0)).unwrap();
9412 check_added_monitors!(nodes[0], 1);
9413 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9414 assert_eq!(events.len(), 1);
9415 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9416 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9417 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9418 // At this point, nodes[1] would notice it has too much value for the payment. It will
9419 // assume the second is a privacy attack (no longer particularly relevant
9420 // post-payment_secrets) and fail back the new HTLC. Previously, it'd also have failed back
9421 // the first HTLC delivered above.
9424 expect_pending_htlcs_forwardable_ignore!(nodes[1]);
9425 nodes[1].node.process_pending_htlc_forwards();
9427 if test_for_second_fail_panic {
9428 // Now we go fail back the first HTLC from the user end.
9429 nodes[1].node.fail_htlc_backwards(&our_payment_hash);
9431 let expected_destinations = vec![
9432 HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9433 HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9435 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], expected_destinations);
9436 nodes[1].node.process_pending_htlc_forwards();
9438 check_added_monitors!(nodes[1], 1);
9439 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9440 assert_eq!(fail_updates_1.update_fail_htlcs.len(), 2);
9442 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9443 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[1]);
9444 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9446 let failure_events = nodes[0].node.get_and_clear_pending_events();
9447 assert_eq!(failure_events.len(), 4);
9448 if let Event::PaymentPathFailed { .. } = failure_events[0] {} else { panic!(); }
9449 if let Event::PaymentFailed { .. } = failure_events[1] {} else { panic!(); }
9450 if let Event::PaymentPathFailed { .. } = failure_events[2] {} else { panic!(); }
9451 if let Event::PaymentFailed { .. } = failure_events[3] {} else { panic!(); }
9453 // Let the second HTLC fail and claim the first
9454 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9455 nodes[1].node.process_pending_htlc_forwards();
9457 check_added_monitors!(nodes[1], 1);
9458 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9459 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9460 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9462 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new());
9464 claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
9469 fn test_dup_htlc_second_fail_panic() {
9470 // Previously, if we received two HTLCs back-to-back, where the second overran the expected
9471 // value for the payment, we'd fail back both HTLCs after generating a `PaymentClaimable` event.
9472 // Then, if the user failed the second payment, they'd hit a "tried to fail an already failed
9473 // HTLC" debug panic. This tests for this behavior, checking that only one HTLC is auto-failed.
9474 do_test_dup_htlc_second_rejected(true);
9478 fn test_dup_htlc_second_rejected() {
9479 // Test that if we receive a second HTLC for an MPP payment that overruns the payment amount we
9480 // simply reject the second HTLC but are still able to claim the first HTLC.
9481 do_test_dup_htlc_second_rejected(false);
9485 fn test_inconsistent_mpp_params() {
9486 // Test that if we recieve two HTLCs with different payment parameters we fail back the first
9487 // such HTLC and allow the second to stay.
9488 let chanmon_cfgs = create_chanmon_cfgs(4);
9489 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9490 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9491 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9493 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9494 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9495 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9496 let chan_2_3 =create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9498 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
9499 .with_bolt11_features(nodes[3].node.invoice_features()).unwrap();
9500 let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
9501 assert_eq!(route.paths.len(), 2);
9502 route.paths.sort_by(|path_a, _| {
9503 // Sort the path so that the path through nodes[1] comes first
9504 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9505 core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9508 let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[3]);
9510 let cur_height = nodes[0].best_block_info().1;
9511 let payment_id = PaymentId([42; 32]);
9513 let session_privs = {
9514 // We create a fake route here so that we start with three pending HTLCs, which we'll
9515 // ultimately have, just not right away.
9516 let mut dup_route = route.clone();
9517 dup_route.paths.push(route.paths[1].clone());
9518 nodes[0].node.test_add_new_pending_payment(our_payment_hash,
9519 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &dup_route).unwrap()
9521 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
9522 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9523 &None, session_privs[0]).unwrap();
9524 check_added_monitors!(nodes[0], 1);
9527 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9528 assert_eq!(events.len(), 1);
9529 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), false, None);
9531 assert!(nodes[3].node.get_and_clear_pending_events().is_empty());
9533 nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9534 RecipientOnionFields::secret_only(our_payment_secret), 14_000_000, cur_height, payment_id, &None, session_privs[1]).unwrap();
9535 check_added_monitors!(nodes[0], 1);
9538 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9539 assert_eq!(events.len(), 1);
9540 let payment_event = SendEvent::from_event(events.pop().unwrap());
9542 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9543 commitment_signed_dance!(nodes[2], nodes[0], payment_event.commitment_msg, false);
9545 expect_pending_htlcs_forwardable!(nodes[2]);
9546 check_added_monitors!(nodes[2], 1);
9548 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
9549 assert_eq!(events.len(), 1);
9550 let payment_event = SendEvent::from_event(events.pop().unwrap());
9552 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
9553 check_added_monitors!(nodes[3], 0);
9554 commitment_signed_dance!(nodes[3], nodes[2], payment_event.commitment_msg, true, true);
9556 // At this point, nodes[3] should notice the two HTLCs don't contain the same total payment
9557 // amount. It will assume the second is a privacy attack (no longer particularly relevant
9558 // post-payment_secrets) and fail back the new HTLC.
9560 expect_pending_htlcs_forwardable_ignore!(nodes[3]);
9561 nodes[3].node.process_pending_htlc_forwards();
9562 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9563 nodes[3].node.process_pending_htlc_forwards();
9565 check_added_monitors!(nodes[3], 1);
9567 let fail_updates_1 = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
9568 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9569 commitment_signed_dance!(nodes[2], nodes[3], fail_updates_1.commitment_signed, false);
9571 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }]);
9572 check_added_monitors!(nodes[2], 1);
9574 let fail_updates_2 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
9575 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
9576 commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
9578 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
9580 nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9581 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9582 &None, session_privs[2]).unwrap();
9583 check_added_monitors!(nodes[0], 1);
9585 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9586 assert_eq!(events.len(), 1);
9587 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), true, None);
9589 do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, our_payment_preimage);
9590 expect_payment_sent(&nodes[0], our_payment_preimage, Some(None), true, true);
9594 fn test_double_partial_claim() {
9595 // Test what happens if a node receives a payment, generates a PaymentClaimable event, the HTLCs
9596 // time out, the sender resends only some of the MPP parts, then the user processes the
9597 // PaymentClaimable event, ensuring they don't inadvertently claim only part of the full payment
9599 let chanmon_cfgs = create_chanmon_cfgs(4);
9600 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9601 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9602 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9604 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9605 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9606 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9607 create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9609 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
9610 assert_eq!(route.paths.len(), 2);
9611 route.paths.sort_by(|path_a, _| {
9612 // Sort the path so that the path through nodes[1] comes first
9613 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9614 core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9617 send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 15_000_000, payment_hash, payment_secret);
9618 // nodes[3] has now received a PaymentClaimable event...which it will take some (exorbitant)
9619 // amount of time to respond to.
9621 // Connect some blocks to time out the payment
9622 connect_blocks(&nodes[3], TEST_FINAL_CLTV);
9623 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // To get the same height for sending later
9625 let failed_destinations = vec![
9626 HTLCDestination::FailedPayment { payment_hash },
9627 HTLCDestination::FailedPayment { payment_hash },
9629 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations);
9631 pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
9633 // nodes[1] now retries one of the two paths...
9634 nodes[0].node.send_payment_with_route(&route, payment_hash,
9635 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9636 check_added_monitors!(nodes[0], 2);
9638 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9639 assert_eq!(events.len(), 2);
9640 let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
9641 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
9643 // At this point nodes[3] has received one half of the payment, and the user goes to handle
9644 // that PaymentClaimable event they got hours ago and never handled...we should refuse to claim.
9645 nodes[3].node.claim_funds(payment_preimage);
9646 check_added_monitors!(nodes[3], 0);
9647 assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
9650 /// The possible events which may trigger a `max_dust_htlc_exposure` breach
9651 #[derive(Clone, Copy, PartialEq)]
9652 enum ExposureEvent {
9653 /// Breach occurs at HTLC forwarding (see `send_htlc`)
9655 /// Breach occurs at HTLC reception (see `update_add_htlc`)
9657 /// Breach occurs at outbound update_fee (see `send_update_fee`)
9658 AtUpdateFeeOutbound,
9661 fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool, multiplier_dust_limit: bool) {
9662 // Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
9665 // At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9666 // trimmed-to-dust HTLC outbound balance and this new payment as included on next
9667 // counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the
9668 // update. At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC
9669 // inbound and trimmed-to-dust HTLC outbound balance and this new received HTLC as included
9670 // on next counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail
9671 // the update. Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel
9672 // might be available again for HTLC processing once the dust bandwidth has cleared up.
9674 let chanmon_cfgs = create_chanmon_cfgs(2);
9675 let mut config = test_default_channel_config();
9676 config.channel_config.max_dust_htlc_exposure = if multiplier_dust_limit {
9677 // Default test fee estimator rate is 253 sat/kw, so we set the multiplier to 5_000_000 / 253
9678 // to get roughly the same initial value as the default setting when this test was
9679 // originally written.
9680 MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253)
9681 } else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000) }; // initial default setting value
9682 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9683 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
9684 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9686 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
9687 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9688 open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9689 open_channel.max_accepted_htlcs = 60;
9691 open_channel.dust_limit_satoshis = 546;
9693 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9694 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9695 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9697 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
9699 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
9702 let mut node_0_per_peer_lock;
9703 let mut node_0_peer_state_lock;
9704 match get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, temporary_channel_id) {
9705 ChannelPhase::UnfundedOutboundV1(chan) => {
9706 chan.context.holder_dust_limit_satoshis = 546;
9708 _ => panic!("Unexpected ChannelPhase variant"),
9712 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9713 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()));
9714 check_added_monitors!(nodes[1], 1);
9715 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9717 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
9718 check_added_monitors!(nodes[0], 1);
9719 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9721 let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9722 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9723 update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9725 // Fetch a route in advance as we will be unable to once we're unable to send.
9726 let (mut route, payment_hash, _, payment_secret) =
9727 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
9729 let (dust_buffer_feerate, max_dust_htlc_exposure_msat) = {
9730 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9731 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9732 let chan = chan_lock.channel_by_id.get(&channel_id).unwrap();
9733 (chan.context().get_dust_buffer_feerate(None) as u64,
9734 chan.context().get_max_dust_htlc_exposure_msat(&LowerBoundedFeeEstimator(nodes[0].fee_estimator)))
9736 let dust_outbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_timeout_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9737 let dust_outbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
9739 let dust_inbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * htlc_success_tx_weight(&channel_type_features) / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9740 let dust_inbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
9742 let dust_htlc_on_counterparty_tx: u64 = 4;
9743 let dust_htlc_on_counterparty_tx_msat: u64 = max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
9746 if dust_outbound_balance {
9747 // Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9748 // Outbound dust balance: 4372 sats
9749 // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9750 for _ in 0..dust_outbound_htlc_on_holder_tx {
9751 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_outbound_htlc_on_holder_tx_msat);
9752 nodes[0].node.send_payment_with_route(&route, payment_hash,
9753 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9756 // Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9757 // Inbound dust balance: 4372 sats
9758 // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9759 for _ in 0..dust_inbound_htlc_on_holder_tx {
9760 route_payment(&nodes[1], &[&nodes[0]], dust_inbound_htlc_on_holder_tx_msat);
9764 if dust_outbound_balance {
9765 // Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9766 // Outbound dust balance: 5000 sats
9767 for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9768 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_htlc_on_counterparty_tx_msat);
9769 nodes[0].node.send_payment_with_route(&route, payment_hash,
9770 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9773 // Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9774 // Inbound dust balance: 5000 sats
9775 for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9776 route_payment(&nodes[1], &[&nodes[0]], dust_htlc_on_counterparty_tx_msat);
9781 if exposure_breach_event == ExposureEvent::AtHTLCForward {
9782 route.paths[0].hops.last_mut().unwrap().fee_msat =
9783 if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 1 };
9784 // With default dust exposure: 5000 sats
9786 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9787 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9788 ), true, APIError::ChannelUnavailable { .. }, {});
9790 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9791 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9792 ), true, APIError::ChannelUnavailable { .. }, {});
9794 } else if exposure_breach_event == ExposureEvent::AtHTLCReception {
9795 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { dust_inbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 4 });
9796 nodes[1].node.send_payment_with_route(&route, payment_hash,
9797 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9798 check_added_monitors!(nodes[1], 1);
9799 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
9800 assert_eq!(events.len(), 1);
9801 let payment_event = SendEvent::from_event(events.remove(0));
9802 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
9803 // With default dust exposure: 5000 sats
9805 // Outbound dust balance: 6399 sats
9806 let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
9807 let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
9808 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, max_dust_htlc_exposure_msat), 1);
9810 // Outbound dust balance: 5200 sats
9811 nodes[0].logger.assert_log("lightning::ln::channel".to_string(),
9812 format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
9813 dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx - 1) + dust_htlc_on_counterparty_tx_msat + 4,
9814 max_dust_htlc_exposure_msat), 1);
9816 } else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {
9817 route.paths[0].hops.last_mut().unwrap().fee_msat = 2_500_000;
9818 // For the multiplier dust exposure limit, since it scales with feerate,
9819 // we need to add a lot of HTLCs that will become dust at the new feerate
9820 // to cross the threshold.
9822 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(1_000), None);
9823 nodes[0].node.send_payment_with_route(&route, payment_hash,
9824 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9827 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9828 *feerate_lock = *feerate_lock * 10;
9830 nodes[0].node.timer_tick_occurred();
9831 check_added_monitors!(nodes[0], 1);
9832 nodes[0].logger.assert_log_contains("lightning::ln::channel", "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure", 1);
9835 let _ = nodes[0].node.get_and_clear_pending_msg_events();
9836 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9837 added_monitors.clear();
9840 fn do_test_max_dust_htlc_exposure_by_threshold_type(multiplier_dust_limit: bool) {
9841 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9842 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9843 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9844 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9845 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9846 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9847 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9848 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9849 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9850 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9851 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9852 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9856 fn test_max_dust_htlc_exposure() {
9857 do_test_max_dust_htlc_exposure_by_threshold_type(false);
9858 do_test_max_dust_htlc_exposure_by_threshold_type(true);
9862 fn test_non_final_funding_tx() {
9863 let chanmon_cfgs = create_chanmon_cfgs(2);
9864 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9865 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9866 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9868 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
9869 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9870 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
9871 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9872 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
9874 let best_height = nodes[0].node.best_block.read().unwrap().height();
9876 let chan_id = *nodes[0].network_chan_count.borrow();
9877 let events = nodes[0].node.get_and_clear_pending_events();
9878 let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::Script::new(), sequence: Sequence(1), witness: Witness::from_vec(vec!(vec!(1))) };
9879 assert_eq!(events.len(), 1);
9880 let mut tx = match events[0] {
9881 Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
9882 // Timelock the transaction _beyond_ the best client height + 1.
9883 Transaction { version: chan_id as i32, lock_time: PackedLockTime(best_height + 2), input: vec![input], output: vec![TxOut {
9884 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
9887 _ => panic!("Unexpected event"),
9889 // Transaction should fail as it's evaluated as non-final for propagation.
9890 match nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()) {
9891 Err(APIError::APIMisuseError { err }) => {
9892 assert_eq!(format!("Funding transaction absolute timelock is non-final"), err);
9897 // However, transaction should be accepted if it's in a +1 headroom from best block.
9898 tx.lock_time = PackedLockTime(tx.lock_time.0 - 1);
9899 assert!(nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
9900 get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9904 fn accept_busted_but_better_fee() {
9905 // If a peer sends us a fee update that is too low, but higher than our previous channel
9906 // feerate, we should accept it. In the future we may want to consider closing the channel
9907 // later, but for now we only accept the update.
9908 let mut chanmon_cfgs = create_chanmon_cfgs(2);
9909 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9910 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9911 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9913 create_chan_between_nodes(&nodes[0], &nodes[1]);
9915 // Set nodes[1] to expect 5,000 sat/kW.
9917 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
9918 *feerate_lock = 5000;
9921 // If nodes[0] increases their feerate, even if its not enough, nodes[1] should accept it.
9923 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9924 *feerate_lock = 1000;
9926 nodes[0].node.timer_tick_occurred();
9927 check_added_monitors!(nodes[0], 1);
9929 let events = nodes[0].node.get_and_clear_pending_msg_events();
9930 assert_eq!(events.len(), 1);
9932 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
9933 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9934 commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
9936 _ => panic!("Unexpected event"),
9939 // If nodes[0] increases their feerate further, even if its not enough, nodes[1] should accept
9942 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9943 *feerate_lock = 2000;
9945 nodes[0].node.timer_tick_occurred();
9946 check_added_monitors!(nodes[0], 1);
9948 let events = nodes[0].node.get_and_clear_pending_msg_events();
9949 assert_eq!(events.len(), 1);
9951 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
9952 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9953 commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
9955 _ => panic!("Unexpected event"),
9958 // However, if nodes[0] decreases their feerate, nodes[1] should reject it and close the
9961 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9962 *feerate_lock = 1000;
9964 nodes[0].node.timer_tick_occurred();
9965 check_added_monitors!(nodes[0], 1);
9967 let events = nodes[0].node.get_and_clear_pending_msg_events();
9968 assert_eq!(events.len(), 1);
9970 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
9971 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
9972 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError {
9973 err: "Peer's feerate much too low. Actual: 1000. Our expected lower limit: 5000 (- 250)".to_owned() },
9974 [nodes[0].node.get_our_node_id()], 100000);
9975 check_closed_broadcast!(nodes[1], true);
9976 check_added_monitors!(nodes[1], 1);
9978 _ => panic!("Unexpected event"),
9982 fn do_payment_with_custom_min_final_cltv_expiry(valid_delta: bool, use_user_hash: bool) {
9983 let mut chanmon_cfgs = create_chanmon_cfgs(2);
9984 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9985 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9986 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9987 let min_final_cltv_expiry_delta = 120;
9988 let final_cltv_expiry_delta = if valid_delta { min_final_cltv_expiry_delta + 2 } else {
9989 min_final_cltv_expiry_delta - 2 };
9990 let recv_value = 100_000;
9992 create_chan_between_nodes(&nodes[0], &nodes[1]);
9994 let payment_parameters = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), final_cltv_expiry_delta as u32);
9995 let (payment_hash, payment_preimage, payment_secret) = if use_user_hash {
9996 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1],
9997 Some(recv_value), Some(min_final_cltv_expiry_delta));
9998 (payment_hash, payment_preimage, payment_secret)
10000 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(recv_value), 7200, Some(min_final_cltv_expiry_delta)).unwrap();
10001 (payment_hash, nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap(), payment_secret)
10003 let route = get_route!(nodes[0], payment_parameters, recv_value).unwrap();
10004 nodes[0].node.send_payment_with_route(&route, payment_hash,
10005 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
10006 check_added_monitors!(nodes[0], 1);
10007 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10008 assert_eq!(events.len(), 1);
10009 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
10010 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10011 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10012 expect_pending_htlcs_forwardable!(nodes[1]);
10015 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, recv_value, if use_user_hash {
10016 None } else { Some(payment_preimage) }, nodes[1].node.get_our_node_id());
10018 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
10020 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10022 check_added_monitors!(nodes[1], 1);
10024 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10025 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
10026 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, false, true);
10028 expect_payment_failed!(nodes[0], payment_hash, true);
10033 fn test_payment_with_custom_min_cltv_expiry_delta() {
10034 do_payment_with_custom_min_final_cltv_expiry(false, false);
10035 do_payment_with_custom_min_final_cltv_expiry(false, true);
10036 do_payment_with_custom_min_final_cltv_expiry(true, false);
10037 do_payment_with_custom_min_final_cltv_expiry(true, true);
10041 fn test_disconnects_peer_awaiting_response_ticks() {
10042 // Tests that nodes which are awaiting on a response critical for channel responsiveness
10043 // disconnect their counterparty after `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10044 let mut chanmon_cfgs = create_chanmon_cfgs(2);
10045 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10046 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10047 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10049 // Asserts a disconnect event is queued to the user.
10050 let check_disconnect_event = |node: &Node, should_disconnect: bool| {
10051 let disconnect_event = node.node.get_and_clear_pending_msg_events().iter().find_map(|event|
10052 if let MessageSendEvent::HandleError { action, .. } = event {
10053 if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
10062 assert_eq!(disconnect_event.is_some(), should_disconnect);
10065 // Fires timer ticks ensuring we only attempt to disconnect peers after reaching
10066 // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10067 let check_disconnect = |node: &Node| {
10068 // No disconnect without any timer ticks.
10069 check_disconnect_event(node, false);
10071 // No disconnect with 1 timer tick less than required.
10072 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS - 1 {
10073 node.node.timer_tick_occurred();
10074 check_disconnect_event(node, false);
10077 // Disconnect after reaching the required ticks.
10078 node.node.timer_tick_occurred();
10079 check_disconnect_event(node, true);
10081 // Disconnect again on the next tick if the peer hasn't been disconnected yet.
10082 node.node.timer_tick_occurred();
10083 check_disconnect_event(node, true);
10086 create_chan_between_nodes(&nodes[0], &nodes[1]);
10088 // We'll start by performing a fee update with Alice (nodes[0]) on the channel.
10089 *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
10090 nodes[0].node.timer_tick_occurred();
10091 check_added_monitors!(&nodes[0], 1);
10092 let alice_fee_update = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10093 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), alice_fee_update.update_fee.as_ref().unwrap());
10094 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &alice_fee_update.commitment_signed);
10095 check_added_monitors!(&nodes[1], 1);
10097 // This will prompt Bob (nodes[1]) to respond with his `CommitmentSigned` and `RevokeAndACK`.
10098 let (bob_revoke_and_ack, bob_commitment_signed) = get_revoke_commit_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
10099 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revoke_and_ack);
10100 check_added_monitors!(&nodes[0], 1);
10101 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_commitment_signed);
10102 check_added_monitors(&nodes[0], 1);
10104 // Alice then needs to send her final `RevokeAndACK` to complete the commitment dance. We
10105 // pretend Bob hasn't received the message and check whether he'll disconnect Alice after
10106 // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10107 let alice_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
10108 check_disconnect(&nodes[1]);
10110 // Now, we'll reconnect them to test awaiting a `ChannelReestablish` message.
10112 // Note that since the commitment dance didn't complete above, Alice is expected to resend her
10113 // final `RevokeAndACK` to Bob to complete it.
10114 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10115 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10116 let bob_init = msgs::Init {
10117 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
10119 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &bob_init, true).unwrap();
10120 let alice_init = msgs::Init {
10121 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
10123 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &alice_init, true).unwrap();
10125 // Upon reconnection, Alice sends her `ChannelReestablish` to Bob. Alice, however, hasn't
10126 // received Bob's yet, so she should disconnect him after reaching
10127 // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10128 let alice_channel_reestablish = get_event_msg!(
10129 nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()
10131 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &alice_channel_reestablish);
10132 check_disconnect(&nodes[0]);
10134 // Bob now sends his `ChannelReestablish` to Alice to resume the channel and consider it "live".
10135 let bob_channel_reestablish = nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(|event|
10136 if let MessageSendEvent::SendChannelReestablish { node_id, msg } = event {
10137 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
10143 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bob_channel_reestablish);
10145 // Sanity check that Alice won't disconnect Bob since she's no longer waiting for any messages.
10146 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10147 nodes[0].node.timer_tick_occurred();
10148 check_disconnect_event(&nodes[0], false);
10151 // However, Bob is still waiting on Alice's `RevokeAndACK`, so he should disconnect her after
10152 // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10153 check_disconnect(&nodes[1]);
10155 // Finally, have Bob process the last message.
10156 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &alice_revoke_and_ack);
10157 check_added_monitors(&nodes[1], 1);
10159 // At this point, neither node should attempt to disconnect each other, since they aren't
10160 // waiting on any messages.
10161 for node in &nodes {
10162 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10163 node.node.timer_tick_occurred();
10164 check_disconnect_event(node, false);
10170 fn test_remove_expired_outbound_unfunded_channels() {
10171 let chanmon_cfgs = create_chanmon_cfgs(2);
10172 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10173 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10174 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10176 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
10177 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10178 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10179 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10180 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10182 let events = nodes[0].node.get_and_clear_pending_events();
10183 assert_eq!(events.len(), 1);
10185 Event::FundingGenerationReady { .. } => (),
10186 _ => panic!("Unexpected event"),
10189 // Asserts the outbound channel has been removed from a nodes[0]'s peer state map.
10190 let check_outbound_channel_existence = |should_exist: bool| {
10191 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
10192 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
10193 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10196 // Channel should exist without any timer ticks.
10197 check_outbound_channel_existence(true);
10199 // Channel should exist with 1 timer tick less than required.
10200 for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10201 nodes[0].node.timer_tick_occurred();
10202 check_outbound_channel_existence(true)
10205 // Remove channel after reaching the required ticks.
10206 nodes[0].node.timer_tick_occurred();
10207 check_outbound_channel_existence(false);
10209 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
10210 assert_eq!(msg_events.len(), 1);
10211 match msg_events[0] {
10212 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10213 assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10215 _ => panic!("Unexpected event"),
10217 check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false, &[nodes[1].node.get_our_node_id()], 100000);
10221 fn test_remove_expired_inbound_unfunded_channels() {
10222 let chanmon_cfgs = create_chanmon_cfgs(2);
10223 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10224 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10225 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10227 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
10228 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10229 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10230 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10231 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10233 let events = nodes[0].node.get_and_clear_pending_events();
10234 assert_eq!(events.len(), 1);
10236 Event::FundingGenerationReady { .. } => (),
10237 _ => panic!("Unexpected event"),
10240 // Asserts the inbound channel has been removed from a nodes[1]'s peer state map.
10241 let check_inbound_channel_existence = |should_exist: bool| {
10242 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10243 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
10244 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10247 // Channel should exist without any timer ticks.
10248 check_inbound_channel_existence(true);
10250 // Channel should exist with 1 timer tick less than required.
10251 for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10252 nodes[1].node.timer_tick_occurred();
10253 check_inbound_channel_existence(true)
10256 // Remove channel after reaching the required ticks.
10257 nodes[1].node.timer_tick_occurred();
10258 check_inbound_channel_existence(false);
10260 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
10261 assert_eq!(msg_events.len(), 1);
10262 match msg_events[0] {
10263 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10264 assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10266 _ => panic!("Unexpected event"),
10268 check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false, &[nodes[0].node.get_our_node_id()], 100000);
10271 fn do_test_multi_post_event_actions(do_reload: bool) {
10272 // Tests handling multiple post-Event actions at once.
10273 // There is specific code in ChannelManager to handle channels where multiple post-Event
10274 // `ChannelMonitorUpdates` are pending at once. This test exercises that code.
10276 // Specifically, we test calling `get_and_clear_pending_events` while there are two
10277 // PaymentSents from different channels and one channel has two pending `ChannelMonitorUpdate`s
10278 // - one from an RAA and one from an inbound commitment_signed.
10279 let chanmon_cfgs = create_chanmon_cfgs(3);
10280 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10281 let (persister, chain_monitor);
10282 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10283 let nodes_0_deserialized;
10284 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10286 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
10287 let chan_id_2 = create_announced_chan_between_nodes(&nodes, 0, 2).2;
10289 send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10290 send_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10292 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10293 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10295 nodes[1].node.claim_funds(our_payment_preimage);
10296 check_added_monitors!(nodes[1], 1);
10297 expect_payment_claimed!(nodes[1], our_payment_hash, 1_000_000);
10299 nodes[2].node.claim_funds(payment_preimage_2);
10300 check_added_monitors!(nodes[2], 1);
10301 expect_payment_claimed!(nodes[2], payment_hash_2, 1_000_000);
10303 for dest in &[1, 2] {
10304 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[*dest], nodes[0].node.get_our_node_id());
10305 nodes[0].node.handle_update_fulfill_htlc(&nodes[*dest].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
10306 commitment_signed_dance!(nodes[0], nodes[*dest], htlc_fulfill_updates.commitment_signed, false);
10307 check_added_monitors(&nodes[0], 0);
10310 let (route, payment_hash_3, _, payment_secret_3) =
10311 get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
10312 let payment_id = PaymentId(payment_hash_3.0);
10313 nodes[1].node.send_payment_with_route(&route, payment_hash_3,
10314 RecipientOnionFields::secret_only(payment_secret_3), payment_id).unwrap();
10315 check_added_monitors(&nodes[1], 1);
10317 let send_event = SendEvent::from_node(&nodes[1]);
10318 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]);
10319 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event.commitment_msg);
10320 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
10323 let nodes_0_serialized = nodes[0].node.encode();
10324 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
10325 let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_2).encode();
10326 reload_node!(nodes[0], test_default_channel_config(), &nodes_0_serialized, &[&chan_0_monitor_serialized, &chan_1_monitor_serialized], persister, chain_monitor, nodes_0_deserialized);
10328 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10329 nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10331 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
10332 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[2]));
10335 let events = nodes[0].node.get_and_clear_pending_events();
10336 assert_eq!(events.len(), 4);
10337 if let Event::PaymentSent { payment_preimage, .. } = events[0] {
10338 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10339 } else { panic!(); }
10340 if let Event::PaymentSent { payment_preimage, .. } = events[1] {
10341 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10342 } else { panic!(); }
10343 if let Event::PaymentPathSuccessful { .. } = events[2] {} else { panic!(); }
10344 if let Event::PaymentPathSuccessful { .. } = events[3] {} else { panic!(); }
10346 // After the events are processed, the ChannelMonitorUpdates will be released and, upon their
10347 // completion, we'll respond to nodes[1] with an RAA + CS.
10348 get_revoke_commit_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10349 check_added_monitors(&nodes[0], 3);
10353 fn test_multi_post_event_actions() {
10354 do_test_multi_post_event_actions(true);
10355 do_test_multi_post_event_actions(false);