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::{CLOSED_CHANNEL_UPDATE_ID, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ANTI_REORG_DELAY};
19 use crate::chain::transaction::OutPoint;
20 use crate::sign::{ecdsa::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::locktime::absolute::LockTime;
42 use bitcoin::blockdata::script::{Builder, ScriptBuf};
43 use bitcoin::blockdata::opcodes;
44 use bitcoin::blockdata::constants::ChainHash;
45 use bitcoin::network::constants::Network;
46 use bitcoin::{Sequence, Transaction, TxIn, TxOut, Witness};
47 use bitcoin::OutPoint as BitcoinOutPoint;
49 use bitcoin::secp256k1::Secp256k1;
50 use bitcoin::secp256k1::{PublicKey,SecretKey};
55 use crate::prelude::*;
56 use alloc::collections::BTreeSet;
57 use core::default::Default;
58 use core::iter::repeat;
59 use bitcoin::hashes::Hash;
60 use crate::sync::{Arc, Mutex, RwLock};
62 use crate::ln::functional_test_utils::*;
63 use crate::ln::chan_utils::CommitmentTransaction;
65 use super::channel::UNFUNDED_CHANNEL_AGE_LIMIT_TICKS;
68 fn test_insane_channel_opens() {
69 // Stand up a network of 2 nodes
70 use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
71 let mut cfg = UserConfig::default();
72 cfg.channel_handshake_limits.max_funding_satoshis = TOTAL_BITCOIN_SUPPLY_SATOSHIS + 1;
73 let chanmon_cfgs = create_chanmon_cfgs(2);
74 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
75 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(cfg)]);
76 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
78 // Instantiate channel parameters where we push the maximum msats given our
80 let channel_value_sat = 31337; // same as funding satoshis
81 let channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(channel_value_sat, &cfg);
82 let push_msat = (channel_value_sat - channel_reserve_satoshis) * 1000;
84 // Have node0 initiate a channel to node1 with aforementioned parameters
85 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_sat, push_msat, 42, None, None).unwrap();
87 // Extract the channel open message from node0 to node1
88 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
90 // Test helper that asserts we get the correct error string given a mutator
91 // that supposedly makes the channel open message insane
92 let insane_open_helper = |expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
93 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &message_mutator(open_channel_message.clone()));
94 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
95 assert_eq!(msg_events.len(), 1);
96 let expected_regex = regex::Regex::new(expected_error_str).unwrap();
97 if let MessageSendEvent::HandleError { ref action, .. } = msg_events[0] {
99 &ErrorAction::SendErrorMessage { .. } => {
100 nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", expected_regex, 1);
102 _ => panic!("unexpected event!"),
104 } else { assert!(false); }
107 use crate::ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
109 // Test all mutations that would make the channel open message insane
110 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 });
111 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 });
113 insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
115 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 });
117 insane_open_helper("Peer never wants payout outputs?", |mut msg| { msg.dust_limit_satoshis = msg.funding_satoshis + 1 ; msg });
119 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 });
121 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 });
123 insane_open_helper("0 max_accepted_htlcs makes for a useless channel", |mut msg| { msg.max_accepted_htlcs = 0; msg });
125 insane_open_helper("max_accepted_htlcs was 484. It must not be larger than 483", |mut msg| { msg.max_accepted_htlcs = 484; msg });
129 fn test_funding_exceeds_no_wumbo_limit() {
130 // Test that if a peer does not support wumbo channels, we'll refuse to open a wumbo channel to
132 use crate::ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
133 let chanmon_cfgs = create_chanmon_cfgs(2);
134 let mut node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
135 *node_cfgs[1].override_init_features.borrow_mut() = Some(channelmanager::provided_init_features(&test_default_channel_config()).clear_wumbo());
136 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
137 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
139 match nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), MAX_FUNDING_SATOSHIS_NO_WUMBO + 1, 0, 42, None, None) {
140 Err(APIError::APIMisuseError { err }) => {
141 assert_eq!(format!("funding_value must not exceed {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, MAX_FUNDING_SATOSHIS_NO_WUMBO + 1), err);
147 fn do_test_counterparty_no_reserve(send_from_initiator: bool) {
148 // A peer providing a channel_reserve_satoshis of 0 (or less than our dust limit) is insecure,
149 // but only for them. Because some LSPs do it with some level of trust of the clients (for a
150 // substantial UX improvement), we explicitly allow it. Because it's unlikely to happen often
151 // in normal testing, we test it explicitly here.
152 let chanmon_cfgs = create_chanmon_cfgs(2);
153 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
154 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
155 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
156 let default_config = UserConfig::default();
158 // Have node0 initiate a channel to node1 with aforementioned parameters
159 let mut push_amt = 100_000_000;
160 let feerate_per_kw = 253;
161 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
162 push_amt -= feerate_per_kw as u64 * (commitment_tx_base_weight(&channel_type_features) + 4 * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
163 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
165 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, None).unwrap();
166 let mut open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
167 if !send_from_initiator {
168 open_channel_message.channel_reserve_satoshis = 0;
169 open_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
171 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
173 // Extract the channel accept message from node1 to node0
174 let mut accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
175 if send_from_initiator {
176 accept_channel_message.channel_reserve_satoshis = 0;
177 accept_channel_message.max_htlc_value_in_flight_msat = 100_000_000;
179 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
181 let sender_node = if send_from_initiator { &nodes[1] } else { &nodes[0] };
182 let counterparty_node = if send_from_initiator { &nodes[0] } else { &nodes[1] };
183 let mut sender_node_per_peer_lock;
184 let mut sender_node_peer_state_lock;
186 let channel_phase = get_channel_ref!(sender_node, counterparty_node, sender_node_per_peer_lock, sender_node_peer_state_lock, temp_channel_id);
187 match channel_phase {
188 ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => {
189 let chan_context = channel_phase.context_mut();
190 chan_context.holder_selected_channel_reserve_satoshis = 0;
191 chan_context.holder_max_htlc_value_in_flight_msat = 100_000_000;
193 ChannelPhase::Funded(_) => assert!(false),
197 let funding_tx = sign_funding_transaction(&nodes[0], &nodes[1], 100_000, temp_channel_id);
198 let funding_msgs = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &funding_tx);
199 create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_msgs.0);
201 // nodes[0] should now be able to send the full balance to nodes[1], violating nodes[1]'s
202 // security model if it ever tries to send funds back to nodes[0] (but that's not our problem).
203 if send_from_initiator {
204 send_payment(&nodes[0], &[&nodes[1]], 100_000_000
205 // Note that for outbound channels we have to consider the commitment tx fee and the
206 // "fee spike buffer", which is currently a multiple of the total commitment tx fee as
207 // well as an additional HTLC.
208 - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * commit_tx_fee_msat(feerate_per_kw, 2, &channel_type_features));
210 send_payment(&nodes[1], &[&nodes[0]], push_amt);
215 fn test_counterparty_no_reserve() {
216 do_test_counterparty_no_reserve(true);
217 do_test_counterparty_no_reserve(false);
221 fn test_async_inbound_update_fee() {
222 let chanmon_cfgs = create_chanmon_cfgs(2);
223 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
224 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
225 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
226 create_announced_chan_between_nodes(&nodes, 0, 1);
229 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
233 // send (1) commitment_signed -.
234 // <- update_add_htlc/commitment_signed
235 // send (2) RAA (awaiting remote revoke) -.
236 // (1) commitment_signed is delivered ->
237 // .- send (3) RAA (awaiting remote revoke)
238 // (2) RAA is delivered ->
239 // .- send (4) commitment_signed
240 // <- (3) RAA is delivered
241 // send (5) commitment_signed -.
242 // <- (4) commitment_signed is delivered
244 // (5) commitment_signed is delivered ->
246 // (6) RAA is delivered ->
248 // First nodes[0] generates an update_fee
250 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
253 nodes[0].node.timer_tick_occurred();
254 check_added_monitors!(nodes[0], 1);
256 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
257 assert_eq!(events_0.len(), 1);
258 let (update_msg, commitment_signed) = match events_0[0] { // (1)
259 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
260 (update_fee.as_ref(), commitment_signed)
262 _ => panic!("Unexpected event"),
265 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
267 // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
268 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
269 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
270 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
271 check_added_monitors!(nodes[1], 1);
273 let payment_event = {
274 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
275 assert_eq!(events_1.len(), 1);
276 SendEvent::from_event(events_1.remove(0))
278 assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
279 assert_eq!(payment_event.msgs.len(), 1);
281 // ...now when the messages get delivered everyone should be happy
282 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
283 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
284 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
285 // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
286 check_added_monitors!(nodes[0], 1);
288 // deliver(1), generate (3):
289 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
290 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
291 // nodes[1] is awaiting nodes[0] revoke_and_ack so get_event_msg's assert(len == 1) passes
292 check_added_monitors!(nodes[1], 1);
294 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack); // deliver (2)
295 let bs_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
296 assert!(bs_update.update_add_htlcs.is_empty()); // (4)
297 assert!(bs_update.update_fulfill_htlcs.is_empty()); // (4)
298 assert!(bs_update.update_fail_htlcs.is_empty()); // (4)
299 assert!(bs_update.update_fail_malformed_htlcs.is_empty()); // (4)
300 assert!(bs_update.update_fee.is_none()); // (4)
301 check_added_monitors!(nodes[1], 1);
303 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack); // deliver (3)
304 let as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
305 assert!(as_update.update_add_htlcs.is_empty()); // (5)
306 assert!(as_update.update_fulfill_htlcs.is_empty()); // (5)
307 assert!(as_update.update_fail_htlcs.is_empty()); // (5)
308 assert!(as_update.update_fail_malformed_htlcs.is_empty()); // (5)
309 assert!(as_update.update_fee.is_none()); // (5)
310 check_added_monitors!(nodes[0], 1);
312 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_update.commitment_signed); // deliver (4)
313 let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
314 // only (6) so get_event_msg's assert(len == 1) passes
315 check_added_monitors!(nodes[0], 1);
317 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_update.commitment_signed); // deliver (5)
318 let bs_second_revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
319 check_added_monitors!(nodes[1], 1);
321 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
322 check_added_monitors!(nodes[0], 1);
324 let events_2 = nodes[0].node.get_and_clear_pending_events();
325 assert_eq!(events_2.len(), 1);
327 Event::PendingHTLCsForwardable {..} => {}, // If we actually processed we'd receive the payment
328 _ => panic!("Unexpected event"),
331 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke); // deliver (6)
332 check_added_monitors!(nodes[1], 1);
336 fn test_update_fee_unordered_raa() {
337 // Just the intro to the previous test followed by an out-of-order RAA (which caused a
338 // crash in an earlier version of the update_fee patch)
339 let chanmon_cfgs = create_chanmon_cfgs(2);
340 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
341 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
342 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
343 create_announced_chan_between_nodes(&nodes, 0, 1);
346 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
348 // First nodes[0] generates an update_fee
350 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
353 nodes[0].node.timer_tick_occurred();
354 check_added_monitors!(nodes[0], 1);
356 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
357 assert_eq!(events_0.len(), 1);
358 let update_msg = match events_0[0] { // (1)
359 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
362 _ => panic!("Unexpected event"),
365 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
367 // ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
368 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 40000);
369 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
370 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
371 check_added_monitors!(nodes[1], 1);
373 let payment_event = {
374 let mut events_1 = nodes[1].node.get_and_clear_pending_msg_events();
375 assert_eq!(events_1.len(), 1);
376 SendEvent::from_event(events_1.remove(0))
378 assert_eq!(payment_event.node_id, nodes[0].node.get_our_node_id());
379 assert_eq!(payment_event.msgs.len(), 1);
381 // ...now when the messages get delivered everyone should be happy
382 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
383 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg); // (2)
384 let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
385 // nodes[0] is awaiting nodes[1] revoke_and_ack so get_event_msg's assert(len == 1) passes
386 check_added_monitors!(nodes[0], 1);
388 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg); // deliver (2)
389 check_added_monitors!(nodes[1], 1);
391 // We can't continue, sadly, because our (1) now has a bogus signature
395 fn test_multi_flight_update_fee() {
396 let chanmon_cfgs = create_chanmon_cfgs(2);
397 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
398 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
399 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
400 create_announced_chan_between_nodes(&nodes, 0, 1);
403 // update_fee/commitment_signed ->
404 // .- send (1) RAA and (2) commitment_signed
405 // update_fee (never committed) ->
407 // We have to manually generate the above update_fee, it is allowed by the protocol but we
408 // don't track which updates correspond to which revoke_and_ack responses so we're in
409 // AwaitingRAA mode and will not generate the update_fee yet.
410 // <- (1) RAA delivered
411 // (3) is generated and send (4) CS -.
412 // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
413 // know the per_commitment_point to use for it.
414 // <- (2) commitment_signed delivered
416 // B should send no response here
417 // (4) commitment_signed delivered ->
418 // <- RAA/commitment_signed delivered
421 // First nodes[0] generates an update_fee
424 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
425 initial_feerate = *feerate_lock;
426 *feerate_lock = initial_feerate + 20;
428 nodes[0].node.timer_tick_occurred();
429 check_added_monitors!(nodes[0], 1);
431 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
432 assert_eq!(events_0.len(), 1);
433 let (update_msg_1, commitment_signed_1) = match events_0[0] { // (1)
434 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
435 (update_fee.as_ref().unwrap(), commitment_signed)
437 _ => panic!("Unexpected event"),
440 // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
441 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg_1);
442 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed_1);
443 let (bs_revoke_msg, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
444 check_added_monitors!(nodes[1], 1);
446 // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
449 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
450 *feerate_lock = initial_feerate + 40;
452 nodes[0].node.timer_tick_occurred();
453 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
454 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
456 // Create the (3) update_fee message that nodes[0] will generate before it does...
457 let mut update_msg_2 = msgs::UpdateFee {
458 channel_id: update_msg_1.channel_id.clone(),
459 feerate_per_kw: (initial_feerate + 30) as u32,
462 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
464 update_msg_2.feerate_per_kw = (initial_feerate + 40) as u32;
466 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg_2);
468 // Deliver (1), generating (3) and (4)
469 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_msg);
470 let as_second_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
471 check_added_monitors!(nodes[0], 1);
472 assert!(as_second_update.update_add_htlcs.is_empty());
473 assert!(as_second_update.update_fulfill_htlcs.is_empty());
474 assert!(as_second_update.update_fail_htlcs.is_empty());
475 assert!(as_second_update.update_fail_malformed_htlcs.is_empty());
476 // Check that the update_fee newly generated matches what we delivered:
477 assert_eq!(as_second_update.update_fee.as_ref().unwrap().channel_id, update_msg_2.channel_id);
478 assert_eq!(as_second_update.update_fee.as_ref().unwrap().feerate_per_kw, update_msg_2.feerate_per_kw);
480 // Deliver (2) commitment_signed
481 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
482 let as_revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
483 check_added_monitors!(nodes[0], 1);
484 // No commitment_signed so get_event_msg's assert(len == 1) passes
486 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_msg);
487 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
488 check_added_monitors!(nodes[1], 1);
491 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_second_update.commitment_signed);
492 let (bs_second_revoke, bs_second_commitment) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
493 check_added_monitors!(nodes[1], 1);
495 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke);
496 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
497 check_added_monitors!(nodes[0], 1);
499 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment);
500 let as_second_revoke = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
501 // No commitment_signed so get_event_msg's assert(len == 1) passes
502 check_added_monitors!(nodes[0], 1);
504 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_second_revoke);
505 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
506 check_added_monitors!(nodes[1], 1);
509 fn do_test_sanity_on_in_flight_opens(steps: u8) {
510 // Previously, we had issues deserializing channels when we hadn't connected the first block
511 // after creation. To catch that and similar issues, we lean on the Node::drop impl to test
512 // serialization round-trips and simply do steps towards opening a channel and then drop the
515 let chanmon_cfgs = create_chanmon_cfgs(2);
516 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
517 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
518 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
520 if steps & 0b1000_0000 != 0{
521 let block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
522 connect_block(&nodes[0], &block);
523 connect_block(&nodes[1], &block);
526 if steps & 0x0f == 0 { return; }
527 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
528 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
530 if steps & 0x0f == 1 { return; }
531 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
532 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
534 if steps & 0x0f == 2 { return; }
535 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
537 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
539 if steps & 0x0f == 3 { return; }
540 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
541 check_added_monitors!(nodes[0], 0);
542 let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
544 if steps & 0x0f == 4 { return; }
545 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
547 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
548 assert_eq!(added_monitors.len(), 1);
549 assert_eq!(added_monitors[0].0, funding_output);
550 added_monitors.clear();
552 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
554 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
556 if steps & 0x0f == 5 { return; }
557 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
559 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
560 assert_eq!(added_monitors.len(), 1);
561 assert_eq!(added_monitors[0].0, funding_output);
562 added_monitors.clear();
565 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
566 let events_4 = nodes[0].node.get_and_clear_pending_events();
567 assert_eq!(events_4.len(), 0);
569 if steps & 0x0f == 6 { return; }
570 create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);
572 if steps & 0x0f == 7 { return; }
573 confirm_transaction_at(&nodes[0], &tx, 2);
574 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
575 create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
576 expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
580 fn test_sanity_on_in_flight_opens() {
581 do_test_sanity_on_in_flight_opens(0);
582 do_test_sanity_on_in_flight_opens(0 | 0b1000_0000);
583 do_test_sanity_on_in_flight_opens(1);
584 do_test_sanity_on_in_flight_opens(1 | 0b1000_0000);
585 do_test_sanity_on_in_flight_opens(2);
586 do_test_sanity_on_in_flight_opens(2 | 0b1000_0000);
587 do_test_sanity_on_in_flight_opens(3);
588 do_test_sanity_on_in_flight_opens(3 | 0b1000_0000);
589 do_test_sanity_on_in_flight_opens(4);
590 do_test_sanity_on_in_flight_opens(4 | 0b1000_0000);
591 do_test_sanity_on_in_flight_opens(5);
592 do_test_sanity_on_in_flight_opens(5 | 0b1000_0000);
593 do_test_sanity_on_in_flight_opens(6);
594 do_test_sanity_on_in_flight_opens(6 | 0b1000_0000);
595 do_test_sanity_on_in_flight_opens(7);
596 do_test_sanity_on_in_flight_opens(7 | 0b1000_0000);
597 do_test_sanity_on_in_flight_opens(8);
598 do_test_sanity_on_in_flight_opens(8 | 0b1000_0000);
602 fn test_update_fee_vanilla() {
603 let chanmon_cfgs = create_chanmon_cfgs(2);
604 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
605 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
606 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
607 create_announced_chan_between_nodes(&nodes, 0, 1);
610 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
613 nodes[0].node.timer_tick_occurred();
614 check_added_monitors!(nodes[0], 1);
616 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
617 assert_eq!(events_0.len(), 1);
618 let (update_msg, commitment_signed) = match events_0[0] {
619 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 } } => {
620 (update_fee.as_ref(), commitment_signed)
622 _ => panic!("Unexpected event"),
624 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
626 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
627 let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
628 check_added_monitors!(nodes[1], 1);
630 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
631 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
632 check_added_monitors!(nodes[0], 1);
634 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
635 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
636 // No commitment_signed so get_event_msg's assert(len == 1) passes
637 check_added_monitors!(nodes[0], 1);
639 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
640 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
641 check_added_monitors!(nodes[1], 1);
645 fn test_update_fee_that_funder_cannot_afford() {
646 let chanmon_cfgs = create_chanmon_cfgs(2);
647 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
648 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
649 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
650 let channel_value = 5000;
652 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, push_sats * 1000);
653 let channel_id = chan.2;
654 let secp_ctx = Secp256k1::new();
655 let default_config = UserConfig::default();
656 let bs_channel_reserve_sats = get_holder_selected_channel_reserve_satoshis(channel_value, &default_config);
658 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
660 // Calculate the maximum feerate that A can afford. Note that we don't send an update_fee
661 // CONCURRENT_INBOUND_HTLC_FEE_BUFFER HTLCs before actually running out of local balance, so we
662 // calculate two different feerates here - the expected local limit as well as the expected
664 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;
665 let non_buffer_feerate = ((channel_value - bs_channel_reserve_sats - push_sats) * 1000 / commitment_tx_base_weight(&channel_type_features)) as u32;
667 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
668 *feerate_lock = feerate;
670 nodes[0].node.timer_tick_occurred();
671 check_added_monitors!(nodes[0], 1);
672 let update_msg = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
674 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_msg.update_fee.unwrap());
676 commitment_signed_dance!(nodes[1], nodes[0], update_msg.commitment_signed, false);
678 // Confirm that the new fee based on the last local commitment txn is what we expected based on the feerate set above.
680 let commitment_tx = get_local_commitment_txn!(nodes[1], channel_id)[0].clone();
682 //We made sure neither party's funds are below the dust limit and there are no HTLCs here
683 assert_eq!(commitment_tx.output.len(), 2);
684 let total_fee: u64 = commit_tx_fee_msat(feerate, 0, &channel_type_features) / 1000;
685 let mut actual_fee = commitment_tx.output.iter().fold(0, |acc, output| acc + output.value);
686 actual_fee = channel_value - actual_fee;
687 assert_eq!(total_fee, actual_fee);
691 // Increment the feerate by a small constant, accounting for rounding errors
692 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
695 nodes[0].node.timer_tick_occurred();
696 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot afford to send new feerate at {}", feerate + 4), 1);
697 check_added_monitors!(nodes[0], 0);
699 const INITIAL_COMMITMENT_NUMBER: u64 = 281474976710654;
701 // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
702 // needed to sign the new commitment tx and (2) sign the new commitment tx.
703 let (local_revocation_basepoint, local_htlc_basepoint, local_funding) = {
704 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
705 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
706 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
707 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
708 ).flatten().unwrap();
709 let chan_signer = local_chan.get_signer();
710 let pubkeys = chan_signer.as_ref().pubkeys();
711 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
712 pubkeys.funding_pubkey)
714 let (remote_delayed_payment_basepoint, remote_htlc_basepoint,remote_point, remote_funding) = {
715 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
716 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
717 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
718 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
719 ).flatten().unwrap();
720 let chan_signer = remote_chan.get_signer();
721 let pubkeys = chan_signer.as_ref().pubkeys();
722 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
723 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
724 pubkeys.funding_pubkey)
727 // Assemble the set of keys we can use for signatures for our commitment_signed message.
728 let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
729 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
732 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
733 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
734 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
735 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
736 ).flatten().unwrap();
737 let local_chan_signer = local_chan.get_signer();
738 let mut htlcs: Vec<(HTLCOutputInCommitment, ())> = vec![];
739 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
740 INITIAL_COMMITMENT_NUMBER - 1,
742 channel_value - push_sats - commit_tx_fee_msat(non_buffer_feerate + 4, 0, &channel_type_features) / 1000,
743 local_funding, remote_funding,
744 commit_tx_keys.clone(),
745 non_buffer_feerate + 4,
747 &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
749 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
752 let commit_signed_msg = msgs::CommitmentSigned {
755 htlc_signatures: res.1,
757 partial_signature_with_nonce: None,
760 let update_fee = msgs::UpdateFee {
762 feerate_per_kw: non_buffer_feerate + 4,
765 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), &update_fee);
767 //While producing the commitment_signed response after handling a received update_fee request the
768 //check to see if the funder, who sent the update_fee request, can afford the new fee (funder_balance >= fee+channel_reserve)
769 //Should produce and error.
770 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
771 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Funding remote cannot afford proposed new fee".to_string(), 1);
772 check_added_monitors!(nodes[1], 1);
773 check_closed_broadcast!(nodes[1], true);
774 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: String::from("Funding remote cannot afford proposed new fee") },
775 [nodes[0].node.get_our_node_id()], channel_value);
779 fn test_update_fee_with_fundee_update_add_htlc() {
780 let chanmon_cfgs = create_chanmon_cfgs(2);
781 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
782 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
783 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
784 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
787 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
790 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
793 nodes[0].node.timer_tick_occurred();
794 check_added_monitors!(nodes[0], 1);
796 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
797 assert_eq!(events_0.len(), 1);
798 let (update_msg, commitment_signed) = match events_0[0] {
799 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 } } => {
800 (update_fee.as_ref(), commitment_signed)
802 _ => panic!("Unexpected event"),
804 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
805 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
806 let (revoke_msg, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
807 check_added_monitors!(nodes[1], 1);
809 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 800000);
811 // nothing happens since node[1] is in AwaitingRemoteRevoke
812 nodes[1].node.send_payment_with_route(&route, our_payment_hash,
813 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
815 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
816 assert_eq!(added_monitors.len(), 0);
817 added_monitors.clear();
819 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
820 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
821 // node[1] has nothing to do
823 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
824 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
825 check_added_monitors!(nodes[0], 1);
827 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
828 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
829 // No commitment_signed so get_event_msg's assert(len == 1) passes
830 check_added_monitors!(nodes[0], 1);
831 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
832 check_added_monitors!(nodes[1], 1);
833 // AwaitingRemoteRevoke ends here
835 let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
836 assert_eq!(commitment_update.update_add_htlcs.len(), 1);
837 assert_eq!(commitment_update.update_fulfill_htlcs.len(), 0);
838 assert_eq!(commitment_update.update_fail_htlcs.len(), 0);
839 assert_eq!(commitment_update.update_fail_malformed_htlcs.len(), 0);
840 assert_eq!(commitment_update.update_fee.is_none(), true);
842 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &commitment_update.update_add_htlcs[0]);
843 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
844 check_added_monitors!(nodes[0], 1);
845 let (revoke, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
847 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke);
848 check_added_monitors!(nodes[1], 1);
849 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
851 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
852 check_added_monitors!(nodes[1], 1);
853 let revoke = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
854 // No commitment_signed so get_event_msg's assert(len == 1) passes
856 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke);
857 check_added_monitors!(nodes[0], 1);
858 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
860 expect_pending_htlcs_forwardable!(nodes[0]);
862 let events = nodes[0].node.get_and_clear_pending_events();
863 assert_eq!(events.len(), 1);
865 Event::PaymentClaimable { .. } => { },
866 _ => panic!("Unexpected event"),
869 claim_payment(&nodes[1], &vec!(&nodes[0])[..], our_payment_preimage);
871 send_payment(&nodes[1], &vec!(&nodes[0])[..], 800000);
872 send_payment(&nodes[0], &vec!(&nodes[1])[..], 800000);
873 close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
874 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
875 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
879 fn test_update_fee() {
880 let chanmon_cfgs = create_chanmon_cfgs(2);
881 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
882 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
883 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
884 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
885 let channel_id = chan.2;
888 // (1) update_fee/commitment_signed ->
889 // <- (2) revoke_and_ack
890 // .- send (3) commitment_signed
891 // (4) update_fee/commitment_signed ->
892 // .- send (5) revoke_and_ack (no CS as we're awaiting a revoke)
893 // <- (3) commitment_signed delivered
894 // send (6) revoke_and_ack -.
895 // <- (5) deliver revoke_and_ack
896 // (6) deliver revoke_and_ack ->
897 // .- send (7) commitment_signed in response to (4)
898 // <- (7) deliver commitment_signed
901 // Create and deliver (1)...
904 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
905 feerate = *feerate_lock;
906 *feerate_lock = feerate + 20;
908 nodes[0].node.timer_tick_occurred();
909 check_added_monitors!(nodes[0], 1);
911 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
912 assert_eq!(events_0.len(), 1);
913 let (update_msg, commitment_signed) = match events_0[0] {
914 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 } } => {
915 (update_fee.as_ref(), commitment_signed)
917 _ => panic!("Unexpected event"),
919 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
921 // Generate (2) and (3):
922 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
923 let (revoke_msg, commitment_signed_0) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
924 check_added_monitors!(nodes[1], 1);
927 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
928 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
929 check_added_monitors!(nodes[0], 1);
931 // Create and deliver (4)...
933 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
934 *feerate_lock = feerate + 30;
936 nodes[0].node.timer_tick_occurred();
937 check_added_monitors!(nodes[0], 1);
938 let events_0 = nodes[0].node.get_and_clear_pending_msg_events();
939 assert_eq!(events_0.len(), 1);
940 let (update_msg, commitment_signed) = match events_0[0] {
941 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 } } => {
942 (update_fee.as_ref(), commitment_signed)
944 _ => panic!("Unexpected event"),
947 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
948 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
949 check_added_monitors!(nodes[1], 1);
951 let revoke_msg = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
952 // No commitment_signed so get_event_msg's assert(len == 1) passes
954 // Handle (3), creating (6):
955 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed_0);
956 check_added_monitors!(nodes[0], 1);
957 let revoke_msg_0 = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
958 // No commitment_signed so get_event_msg's assert(len == 1) passes
961 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_msg);
962 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
963 check_added_monitors!(nodes[0], 1);
965 // Deliver (6), creating (7):
966 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg_0);
967 let commitment_update = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
968 assert!(commitment_update.update_add_htlcs.is_empty());
969 assert!(commitment_update.update_fulfill_htlcs.is_empty());
970 assert!(commitment_update.update_fail_htlcs.is_empty());
971 assert!(commitment_update.update_fail_malformed_htlcs.is_empty());
972 assert!(commitment_update.update_fee.is_none());
973 check_added_monitors!(nodes[1], 1);
976 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_update.commitment_signed);
977 check_added_monitors!(nodes[0], 1);
978 let revoke_msg = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
979 // No commitment_signed so get_event_msg's assert(len == 1) passes
981 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &revoke_msg);
982 check_added_monitors!(nodes[1], 1);
983 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
985 assert_eq!(get_feerate!(nodes[0], nodes[1], channel_id), feerate + 30);
986 assert_eq!(get_feerate!(nodes[1], nodes[0], channel_id), feerate + 30);
987 close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true);
988 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
989 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
993 fn fake_network_test() {
994 // Simple test which builds a network of ChannelManagers, connects them to each other, and
995 // tests that payments get routed and transactions broadcast in semi-reasonable ways.
996 let chanmon_cfgs = create_chanmon_cfgs(4);
997 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
998 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
999 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
1001 // Create some initial channels
1002 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1003 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1004 let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
1006 // Rebalance the network a bit by relaying one payment through all the channels...
1007 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1008 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1009 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1010 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 8000000);
1012 // Send some more payments
1013 send_payment(&nodes[1], &vec!(&nodes[2], &nodes[3])[..], 1000000);
1014 send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1], &nodes[0])[..], 1000000);
1015 send_payment(&nodes[3], &vec!(&nodes[2], &nodes[1])[..], 1000000);
1017 // Test failure packets
1018 let payment_hash_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], 1000000).1;
1019 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3])[..], payment_hash_1);
1021 // Add a new channel that skips 3
1022 let chan_4 = create_announced_chan_between_nodes(&nodes, 1, 3);
1024 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[3])[..], 1000000);
1025 send_payment(&nodes[2], &vec!(&nodes[3])[..], 1000000);
1026 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1027 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1028 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1029 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1030 send_payment(&nodes[1], &vec!(&nodes[3])[..], 8000000);
1032 // Do some rebalance loop payments, simultaneously
1033 let mut hops = Vec::with_capacity(3);
1034 hops.push(RouteHop {
1035 pubkey: nodes[2].node.get_our_node_id(),
1036 node_features: NodeFeatures::empty(),
1037 short_channel_id: chan_2.0.contents.short_channel_id,
1038 channel_features: ChannelFeatures::empty(),
1040 cltv_expiry_delta: chan_3.0.contents.cltv_expiry_delta as u32,
1041 maybe_announced_channel: true,
1043 hops.push(RouteHop {
1044 pubkey: nodes[3].node.get_our_node_id(),
1045 node_features: NodeFeatures::empty(),
1046 short_channel_id: chan_3.0.contents.short_channel_id,
1047 channel_features: ChannelFeatures::empty(),
1049 cltv_expiry_delta: chan_4.1.contents.cltv_expiry_delta as u32,
1050 maybe_announced_channel: true,
1052 hops.push(RouteHop {
1053 pubkey: nodes[1].node.get_our_node_id(),
1054 node_features: nodes[1].node.node_features(),
1055 short_channel_id: chan_4.0.contents.short_channel_id,
1056 channel_features: nodes[1].node.channel_features(),
1058 cltv_expiry_delta: TEST_FINAL_CLTV,
1059 maybe_announced_channel: true,
1061 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;
1062 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;
1063 let payment_preimage_1 = send_along_route(&nodes[1],
1064 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1065 &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
1067 let mut hops = Vec::with_capacity(3);
1068 hops.push(RouteHop {
1069 pubkey: nodes[3].node.get_our_node_id(),
1070 node_features: NodeFeatures::empty(),
1071 short_channel_id: chan_4.0.contents.short_channel_id,
1072 channel_features: ChannelFeatures::empty(),
1074 cltv_expiry_delta: chan_3.1.contents.cltv_expiry_delta as u32,
1075 maybe_announced_channel: true,
1077 hops.push(RouteHop {
1078 pubkey: nodes[2].node.get_our_node_id(),
1079 node_features: NodeFeatures::empty(),
1080 short_channel_id: chan_3.0.contents.short_channel_id,
1081 channel_features: ChannelFeatures::empty(),
1083 cltv_expiry_delta: chan_2.1.contents.cltv_expiry_delta as u32,
1084 maybe_announced_channel: true,
1086 hops.push(RouteHop {
1087 pubkey: nodes[1].node.get_our_node_id(),
1088 node_features: nodes[1].node.node_features(),
1089 short_channel_id: chan_2.0.contents.short_channel_id,
1090 channel_features: nodes[1].node.channel_features(),
1092 cltv_expiry_delta: TEST_FINAL_CLTV,
1093 maybe_announced_channel: true,
1095 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;
1096 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;
1097 let payment_hash_2 = send_along_route(&nodes[1],
1098 Route { paths: vec![Path { hops, blinded_tail: None }], route_params: None },
1099 &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
1101 // Claim the rebalances...
1102 fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
1103 claim_payment(&nodes[1], &vec!(&nodes[2], &nodes[3], &nodes[1])[..], payment_preimage_1);
1105 // Close down the channels...
1106 close_channel(&nodes[0], &nodes[1], &chan_1.2, chan_1.3, true);
1107 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1108 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
1109 close_channel(&nodes[1], &nodes[2], &chan_2.2, chan_2.3, false);
1110 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1111 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1112 close_channel(&nodes[2], &nodes[3], &chan_3.2, chan_3.3, true);
1113 check_closed_event!(nodes[2], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1114 check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[2].node.get_our_node_id()], 100000);
1115 close_channel(&nodes[1], &nodes[3], &chan_4.2, chan_4.3, false);
1116 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[3].node.get_our_node_id()], 100000);
1117 check_closed_event!(nodes[3], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
1121 fn holding_cell_htlc_counting() {
1122 // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1123 // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1124 // commitment dance rounds.
1125 let chanmon_cfgs = create_chanmon_cfgs(3);
1126 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1127 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1128 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1129 create_announced_chan_between_nodes(&nodes, 0, 1);
1130 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
1132 // Fetch a route in advance as we will be unable to once we're unable to send.
1133 let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1135 let mut payments = Vec::new();
1137 let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
1138 nodes[1].node.send_payment_with_route(&route, payment_hash,
1139 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
1140 payments.push((payment_preimage, payment_hash));
1142 check_added_monitors!(nodes[1], 1);
1144 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1145 assert_eq!(events.len(), 1);
1146 let initial_payment_event = SendEvent::from_event(events.pop().unwrap());
1147 assert_eq!(initial_payment_event.node_id, nodes[2].node.get_our_node_id());
1149 // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1150 // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1153 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, payment_hash_1,
1154 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)
1155 ), true, APIError::ChannelUnavailable { .. }, {});
1156 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1159 // This should also be true if we try to forward a payment.
1160 let (route, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
1162 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
1163 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1164 check_added_monitors!(nodes[0], 1);
1167 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1168 assert_eq!(events.len(), 1);
1169 let payment_event = SendEvent::from_event(events.pop().unwrap());
1170 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
1172 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
1173 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
1174 // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1175 // fails), the second will process the resulting failure and fail the HTLC backward.
1176 expect_pending_htlcs_forwardable!(nodes[1]);
1177 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 }]);
1178 check_added_monitors!(nodes[1], 1);
1180 let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1181 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_fail_updates.update_fail_htlcs[0]);
1182 commitment_signed_dance!(nodes[0], nodes[1], bs_fail_updates.commitment_signed, false, true);
1184 expect_payment_failed_with_update!(nodes[0], payment_hash_2, false, chan_2.0.contents.short_channel_id, false);
1186 // Now forward all the pending HTLCs and claim them back
1187 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &initial_payment_event.msgs[0]);
1188 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &initial_payment_event.commitment_msg);
1189 check_added_monitors!(nodes[2], 1);
1191 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1192 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1193 check_added_monitors!(nodes[1], 1);
1194 let as_updates = get_htlc_update_msgs!(nodes[1], nodes[2].node.get_our_node_id());
1196 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1197 check_added_monitors!(nodes[1], 1);
1198 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1200 for ref update in as_updates.update_add_htlcs.iter() {
1201 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), update);
1203 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_updates.commitment_signed);
1204 check_added_monitors!(nodes[2], 1);
1205 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
1206 check_added_monitors!(nodes[2], 1);
1207 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1209 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_revoke_and_ack);
1210 check_added_monitors!(nodes[1], 1);
1211 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &bs_commitment_signed);
1212 check_added_monitors!(nodes[1], 1);
1213 let as_final_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
1215 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_final_raa);
1216 check_added_monitors!(nodes[2], 1);
1218 expect_pending_htlcs_forwardable!(nodes[2]);
1220 let events = nodes[2].node.get_and_clear_pending_events();
1221 assert_eq!(events.len(), payments.len());
1222 for (event, &(_, ref hash)) in events.iter().zip(payments.iter()) {
1224 &Event::PaymentClaimable { ref payment_hash, .. } => {
1225 assert_eq!(*payment_hash, *hash);
1227 _ => panic!("Unexpected event"),
1231 for (preimage, _) in payments.drain(..) {
1232 claim_payment(&nodes[1], &[&nodes[2]], preimage);
1235 send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1239 fn duplicate_htlc_test() {
1240 // Test that we accept duplicate payment_hash HTLCs across the network and that
1241 // claiming/failing them are all separate and don't affect each other
1242 let chanmon_cfgs = create_chanmon_cfgs(6);
1243 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1244 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1245 let mut nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1247 // Create some initial channels to route via 3 to 4/5 from 0/1/2
1248 create_announced_chan_between_nodes(&nodes, 0, 3);
1249 create_announced_chan_between_nodes(&nodes, 1, 3);
1250 create_announced_chan_between_nodes(&nodes, 2, 3);
1251 create_announced_chan_between_nodes(&nodes, 3, 4);
1252 create_announced_chan_between_nodes(&nodes, 3, 5);
1254 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], 1000000);
1256 *nodes[0].network_payment_count.borrow_mut() -= 1;
1257 assert_eq!(route_payment(&nodes[1], &vec!(&nodes[3])[..], 1000000).0, payment_preimage);
1259 *nodes[0].network_payment_count.borrow_mut() -= 1;
1260 assert_eq!(route_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], 1000000).0, payment_preimage);
1262 claim_payment(&nodes[0], &vec!(&nodes[3], &nodes[4])[..], payment_preimage);
1263 fail_payment(&nodes[2], &vec!(&nodes[3], &nodes[5])[..], payment_hash);
1264 claim_payment(&nodes[1], &vec!(&nodes[3])[..], payment_preimage);
1268 fn test_duplicate_htlc_different_direction_onchain() {
1269 // Test that ChannelMonitor doesn't generate 2 preimage txn
1270 // when we have 2 HTLCs with same preimage that go across a node
1271 // in opposite directions, even with the same payment secret.
1272 let chanmon_cfgs = create_chanmon_cfgs(2);
1273 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1274 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1275 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1277 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
1280 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
1282 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 900_000);
1284 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 800_000);
1285 let node_a_payment_secret = nodes[0].node.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
1286 send_along_route_with_secret(&nodes[1], route, &[&[&nodes[0]]], 800_000, payment_hash, node_a_payment_secret);
1288 // Provide preimage to node 0 by claiming payment
1289 nodes[0].node.claim_funds(payment_preimage);
1290 expect_payment_claimed!(nodes[0], payment_hash, 800_000);
1291 check_added_monitors!(nodes[0], 1);
1293 // Broadcast node 1 commitment txn
1294 let remote_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
1296 assert_eq!(remote_txn[0].output.len(), 4); // 1 local, 1 remote, 1 htlc inbound, 1 htlc outbound
1297 let mut has_both_htlcs = 0; // check htlcs match ones committed
1298 for outp in remote_txn[0].output.iter() {
1299 if outp.value == 800_000 / 1000 {
1300 has_both_htlcs += 1;
1301 } else if outp.value == 900_000 / 1000 {
1302 has_both_htlcs += 1;
1305 assert_eq!(has_both_htlcs, 2);
1307 mine_transaction(&nodes[0], &remote_txn[0]);
1308 check_added_monitors!(nodes[0], 1);
1309 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
1310 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
1312 let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
1313 assert_eq!(claim_txn.len(), 3);
1315 check_spends!(claim_txn[0], remote_txn[0]); // Immediate HTLC claim with preimage
1316 check_spends!(claim_txn[1], remote_txn[0]);
1317 check_spends!(claim_txn[2], remote_txn[0]);
1318 let preimage_tx = &claim_txn[0];
1319 let (preimage_bump_tx, timeout_tx) = if claim_txn[1].input[0].previous_output == preimage_tx.input[0].previous_output {
1320 (&claim_txn[1], &claim_txn[2])
1322 (&claim_txn[2], &claim_txn[1])
1325 assert_eq!(preimage_tx.input.len(), 1);
1326 assert_eq!(preimage_bump_tx.input.len(), 1);
1328 assert_eq!(preimage_tx.input.len(), 1);
1329 assert_eq!(preimage_tx.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC 1 <--> 0, preimage tx
1330 assert_eq!(remote_txn[0].output[preimage_tx.input[0].previous_output.vout as usize].value, 800);
1332 assert_eq!(timeout_tx.input.len(), 1);
1333 assert_eq!(timeout_tx.input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
1334 check_spends!(timeout_tx, remote_txn[0]);
1335 assert_eq!(remote_txn[0].output[timeout_tx.input[0].previous_output.vout as usize].value, 900);
1337 let events = nodes[0].node.get_and_clear_pending_msg_events();
1338 assert_eq!(events.len(), 3);
1341 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
1342 MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::DisconnectPeer { ref msg } } => {
1343 assert_eq!(node_id, nodes[1].node.get_our_node_id());
1344 assert_eq!(msg.as_ref().unwrap().data, "Channel closed because commitment or closing transaction was confirmed on chain.");
1346 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, .. } } => {
1347 assert!(update_add_htlcs.is_empty());
1348 assert!(update_fail_htlcs.is_empty());
1349 assert_eq!(update_fulfill_htlcs.len(), 1);
1350 assert!(update_fail_malformed_htlcs.is_empty());
1351 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
1353 _ => panic!("Unexpected event"),
1359 fn test_basic_channel_reserve() {
1360 let chanmon_cfgs = create_chanmon_cfgs(2);
1361 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1362 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1363 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1364 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1366 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1367 let channel_reserve = chan_stat.channel_reserve_msat;
1369 // The 2* and +1 are for the fee spike reserve.
1370 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));
1371 let max_can_send = 5000000 - channel_reserve - commit_tx_fee;
1372 let (mut route, our_payment_hash, _, our_payment_secret) =
1373 get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
1374 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1375 let err = nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1376 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).err().unwrap();
1378 PaymentSendFailure::AllFailedResendSafe(ref fails) => {
1379 if let &APIError::ChannelUnavailable { .. } = &fails[0] {}
1380 else { panic!("Unexpected error variant"); }
1382 _ => panic!("Unexpected error variant"),
1384 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1386 send_payment(&nodes[0], &vec![&nodes[1]], max_can_send);
1390 fn test_fee_spike_violation_fails_htlc() {
1391 let chanmon_cfgs = create_chanmon_cfgs(2);
1392 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1393 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1394 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1395 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1397 let (mut route, payment_hash, _, payment_secret) =
1398 get_route_and_payment_hash!(nodes[0], nodes[1], 3460000);
1399 route.paths[0].hops[0].fee_msat += 1;
1400 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1401 let secp_ctx = Secp256k1::new();
1402 let session_priv = SecretKey::from_slice(&[42; 32]).expect("RNG is bad!");
1404 let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1406 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1407 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1408 3460001, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1409 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1410 let msg = msgs::UpdateAddHTLC {
1413 amount_msat: htlc_msat,
1414 payment_hash: payment_hash,
1415 cltv_expiry: htlc_cltv,
1416 onion_routing_packet: onion_packet,
1417 skimmed_fee_msat: None,
1420 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1422 // Now manually create the commitment_signed message corresponding to the update_add
1423 // nodes[0] just sent. In the code for construction of this message, "local" refers
1424 // to the sender of the message, and "remote" refers to the receiver.
1426 let feerate_per_kw = get_feerate!(nodes[0], nodes[1], chan.2);
1428 const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
1430 // Get the TestChannelSigner for each channel, which will be used to (1) get the keys
1431 // needed to sign the new commitment tx and (2) sign the new commitment tx.
1432 let (local_revocation_basepoint, local_htlc_basepoint, local_secret, next_local_point, local_funding) = {
1433 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1434 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1435 let local_chan = chan_lock.channel_by_id.get(&chan.2).map(
1436 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1437 ).flatten().unwrap();
1438 let chan_signer = local_chan.get_signer();
1439 // Make the signer believe we validated another commitment, so we can release the secret
1440 chan_signer.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
1442 let pubkeys = chan_signer.as_ref().pubkeys();
1443 (pubkeys.revocation_basepoint, pubkeys.htlc_basepoint,
1444 chan_signer.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1445 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx),
1446 chan_signer.as_ref().pubkeys().funding_pubkey)
1448 let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_point, remote_funding) = {
1449 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
1450 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
1451 let remote_chan = chan_lock.channel_by_id.get(&chan.2).map(
1452 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1453 ).flatten().unwrap();
1454 let chan_signer = remote_chan.get_signer();
1455 let pubkeys = chan_signer.as_ref().pubkeys();
1456 (pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint,
1457 chan_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx),
1458 chan_signer.as_ref().pubkeys().funding_pubkey)
1461 // Assemble the set of keys we can use for signatures for our commitment_signed message.
1462 let commit_tx_keys = chan_utils::TxCreationKeys::derive_new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
1463 &remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint);
1465 // Build the remote commitment transaction so we can sign it, and then later use the
1466 // signature for the commitment_signed message.
1467 let local_chan_balance = 1313;
1469 let accepted_htlc_info = chan_utils::HTLCOutputInCommitment {
1471 amount_msat: 3460001,
1472 cltv_expiry: htlc_cltv,
1474 transaction_output_index: Some(1),
1477 let commitment_number = INITIAL_COMMITMENT_NUMBER - 1;
1480 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
1481 let local_chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
1482 let local_chan = local_chan_lock.channel_by_id.get(&chan.2).map(
1483 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
1484 ).flatten().unwrap();
1485 let local_chan_signer = local_chan.get_signer();
1486 let commitment_tx = CommitmentTransaction::new_with_auxiliary_htlc_data(
1490 local_funding, remote_funding,
1491 commit_tx_keys.clone(),
1493 &mut vec![(accepted_htlc_info, ())],
1494 &local_chan.context.channel_transaction_parameters.as_counterparty_broadcastable()
1496 local_chan_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_tx, Vec::new(), &secp_ctx).unwrap()
1499 let commit_signed_msg = msgs::CommitmentSigned {
1502 htlc_signatures: res.1,
1504 partial_signature_with_nonce: None,
1507 // Send the commitment_signed message to the nodes[1].
1508 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commit_signed_msg);
1509 let _ = nodes[1].node.get_and_clear_pending_msg_events();
1511 // Send the RAA to nodes[1].
1512 let raa_msg = msgs::RevokeAndACK {
1514 per_commitment_secret: local_secret,
1515 next_per_commitment_point: next_local_point,
1517 next_local_nonce: None,
1519 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
1521 let events = nodes[1].node.get_and_clear_pending_msg_events();
1522 assert_eq!(events.len(), 1);
1523 // Make sure the HTLC failed in the way we expect.
1525 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, .. }, .. } => {
1526 assert_eq!(update_fail_htlcs.len(), 1);
1527 update_fail_htlcs[0].clone()
1529 _ => panic!("Unexpected event"),
1531 nodes[1].logger.assert_log("lightning::ln::channel".to_string(),
1532 format!("Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", raa_msg.channel_id), 1);
1534 check_added_monitors!(nodes[1], 2);
1538 fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
1539 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1540 // Set the fee rate for the channel very high, to the point where the fundee
1541 // sending any above-dust amount would result in a channel reserve violation.
1542 // In this test we check that we would be prevented from sending an HTLC in
1544 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1545 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1546 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1547 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1548 let default_config = UserConfig::default();
1549 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1551 let mut push_amt = 100_000_000;
1552 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1554 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1556 let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1558 // Fetch a route in advance as we will be unable to once we're unable to send.
1559 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
1560 // Sending exactly enough to hit the reserve amount should be accepted
1561 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1562 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1565 // However one more HTLC should be significantly over the reserve amount and fail.
1566 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1567 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1568 ), true, APIError::ChannelUnavailable { .. }, {});
1569 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
1573 fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
1574 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1575 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1576 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1577 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1578 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1579 let default_config = UserConfig::default();
1580 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1582 // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1583 // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1584 // transaction fee with 0 HTLCs (183 sats)).
1585 let mut push_amt = 100_000_000;
1586 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1587 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1588 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt);
1590 // Send four HTLCs to cover the initial push_msat buffer we're required to include
1591 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1592 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1595 let (mut route, payment_hash, _, payment_secret) =
1596 get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
1597 route.paths[0].hops[0].fee_msat = 700_000;
1598 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1599 let secp_ctx = Secp256k1::new();
1600 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1601 let cur_height = nodes[1].node.best_block.read().unwrap().height() + 1;
1602 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
1603 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0],
1604 700_000, RecipientOnionFields::secret_only(payment_secret), cur_height, &None).unwrap();
1605 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
1606 let msg = msgs::UpdateAddHTLC {
1608 htlc_id: MIN_AFFORDABLE_HTLC_COUNT as u64,
1609 amount_msat: htlc_msat,
1610 payment_hash: payment_hash,
1611 cltv_expiry: htlc_cltv,
1612 onion_routing_packet: onion_packet,
1613 skimmed_fee_msat: None,
1616 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &msg);
1617 // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1618 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);
1619 assert_eq!(nodes[0].node.list_channels().len(), 0);
1620 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
1621 assert_eq!(err_msg.data, "Cannot accept HTLC that would put our balance under counterparty-announced channel reserve value");
1622 check_added_monitors!(nodes[0], 1);
1623 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() },
1624 [nodes[1].node.get_our_node_id()], 100000);
1628 fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
1629 // Test that if we receive many dust HTLCs over an outbound channel, they don't count when
1630 // calculating our commitment transaction fee (this was previously broken).
1631 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1632 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1634 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1635 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1636 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1637 let default_config = UserConfig::default();
1638 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1640 // Set nodes[0]'s balance such that they will consider any above-dust received HTLC to be a
1641 // channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
1642 // transaction fee with 0 HTLCs (183 sats)).
1643 let mut push_amt = 100_000_000;
1644 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1645 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1646 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt);
1648 let dust_amt = crate::ln::channel::MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000
1649 + feerate_per_kw as u64 * htlc_success_tx_weight(&channel_type_features) / 1000 * 1000 - 1;
1650 // In the previous code, routing this dust payment would cause nodes[0] to perceive a channel
1651 // reserve violation even though it's a dust HTLC and therefore shouldn't count towards the
1652 // commitment transaction fee.
1653 route_payment(&nodes[1], &[&nodes[0]], dust_amt);
1655 // Send four HTLCs to cover the initial push_msat buffer we're required to include
1656 for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1657 route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1660 // One more than the dust amt should fail, however.
1661 let (mut route, our_payment_hash, _, our_payment_secret) =
1662 get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt);
1663 route.paths[0].hops[0].fee_msat += 1;
1664 unwrap_send_err!(nodes[1].node.send_payment_with_route(&route, our_payment_hash,
1665 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1666 ), true, APIError::ChannelUnavailable { .. }, {});
1670 fn test_chan_init_feerate_unaffordability() {
1671 // Test that we will reject channel opens which do not leave enough to pay for any HTLCs due to
1672 // channel reserve and feerate requirements.
1673 let mut chanmon_cfgs = create_chanmon_cfgs(2);
1674 let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1675 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1676 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1677 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1678 let default_config = UserConfig::default();
1679 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
1681 // Set the push_msat amount such that nodes[0] will not be able to afford to add even a single
1683 let mut push_amt = 100_000_000;
1684 push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64, &channel_type_features);
1685 assert_eq!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt + 1, 42, None, None).unwrap_err(),
1686 APIError::APIMisuseError { err: "Funding amount (356) can't even pay fee for initial commitment transaction fee of 357.".to_string() });
1688 // During open, we don't have a "counterparty channel reserve" to check against, so that
1689 // requirement only comes into play on the open_channel handling side.
1690 push_amt -= get_holder_selected_channel_reserve_satoshis(100_000, &default_config) * 1000;
1691 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt, 42, None, None).unwrap();
1692 let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
1693 open_channel_msg.push_msat += 1;
1694 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
1696 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
1697 assert_eq!(msg_events.len(), 1);
1698 match msg_events[0] {
1699 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
1700 assert_eq!(msg.data, "Insufficient funding amount for initial reserve");
1702 _ => panic!("Unexpected event"),
1707 fn test_chan_reserve_dust_inbound_htlcs_inbound_chan() {
1708 // Test that if we receive many dust HTLCs over an inbound channel, they don't count when
1709 // calculating our counterparty's commitment transaction fee (this was previously broken).
1710 let chanmon_cfgs = create_chanmon_cfgs(2);
1711 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1712 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None, None]);
1713 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1714 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 98000000);
1716 let payment_amt = 46000; // Dust amount
1717 // In the previous code, these first four payments would succeed.
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 // Then these next 5 would be interpreted by nodes[1] as violating the fee spike buffer.
1724 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1725 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1726 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1727 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1728 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1730 // And this last payment previously resulted in nodes[1] closing on its inbound-channel
1731 // counterparty, because it counted all the previous dust HTLCs against nodes[0]'s commitment
1732 // transaction fee and therefore perceived this next payment as a channel reserve violation.
1733 route_payment(&nodes[0], &[&nodes[1]], payment_amt);
1737 fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
1738 let chanmon_cfgs = create_chanmon_cfgs(3);
1739 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1740 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
1741 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1742 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1743 let _ = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
1746 let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1747 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
1748 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
1749 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
1751 // Add a 2* and +1 for the fee spike reserve.
1752 let commit_tx_fee_2_htlc = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1753 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;
1754 let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
1756 // Add a pending HTLC.
1757 let (route_1, our_payment_hash_1, _, our_payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_1);
1758 let payment_event_1 = {
1759 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1760 RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1761 check_added_monitors!(nodes[0], 1);
1763 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1764 assert_eq!(events.len(), 1);
1765 SendEvent::from_event(events.remove(0))
1767 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1769 // Attempt to trigger a channel reserve violation --> payment failure.
1770 let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2, &channel_type_features);
1771 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;
1772 let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1773 let mut route_2 = route_1.clone();
1774 route_2.paths[0].hops.last_mut().unwrap().fee_msat = amt_msat_2;
1776 // Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
1777 let secp_ctx = Secp256k1::new();
1778 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
1779 let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
1780 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route_2.paths[0], &session_priv).unwrap();
1781 let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
1782 &route_2.paths[0], recv_value_2, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
1783 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash_1).unwrap();
1784 let msg = msgs::UpdateAddHTLC {
1787 amount_msat: htlc_msat + 1,
1788 payment_hash: our_payment_hash_1,
1789 cltv_expiry: htlc_cltv,
1790 onion_routing_packet: onion_packet,
1791 skimmed_fee_msat: None,
1794 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
1795 // Check that the payment failed and the channel is closed in response to the malicious UpdateAdd.
1796 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote HTLC add would put them under remote reserve value".to_string(), 1);
1797 assert_eq!(nodes[1].node.list_channels().len(), 1);
1798 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
1799 assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
1800 check_added_monitors!(nodes[1], 1);
1801 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote HTLC add would put them under remote reserve value".to_string() },
1802 [nodes[0].node.get_our_node_id()], 100000);
1806 fn test_inbound_outbound_capacity_is_not_zero() {
1807 let chanmon_cfgs = create_chanmon_cfgs(2);
1808 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1809 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1810 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1811 let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
1812 let channels0 = node_chanmgrs[0].list_channels();
1813 let channels1 = node_chanmgrs[1].list_channels();
1814 let default_config = UserConfig::default();
1815 assert_eq!(channels0.len(), 1);
1816 assert_eq!(channels1.len(), 1);
1818 let reserve = get_holder_selected_channel_reserve_satoshis(100_000, &default_config);
1819 assert_eq!(channels0[0].inbound_capacity_msat, 95000000 - reserve*1000);
1820 assert_eq!(channels1[0].outbound_capacity_msat, 95000000 - reserve*1000);
1822 assert_eq!(channels0[0].outbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1823 assert_eq!(channels1[0].inbound_capacity_msat, 100000 * 1000 - 95000000 - reserve*1000);
1826 fn commit_tx_fee_msat(feerate: u32, num_htlcs: u64, channel_type_features: &ChannelTypeFeatures) -> u64 {
1827 (commitment_tx_base_weight(channel_type_features) + num_htlcs * COMMITMENT_TX_WEIGHT_PER_HTLC) * feerate as u64 / 1000 * 1000
1831 fn test_channel_reserve_holding_cell_htlcs() {
1832 let chanmon_cfgs = create_chanmon_cfgs(3);
1833 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1834 // When this test was written, the default base fee floated based on the HTLC count.
1835 // It is now fixed, so we simply set the fee to the expected value here.
1836 let mut config = test_default_channel_config();
1837 config.channel_config.forwarding_fee_base_msat = 239;
1838 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
1839 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1840 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 190000, 1001);
1841 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 190000, 1001);
1843 let mut stat01 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1844 let mut stat11 = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
1846 let mut stat12 = get_channel_value_stat!(nodes[1], nodes[2], chan_2.2);
1847 let mut stat22 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
1849 macro_rules! expect_forward {
1851 let mut events = $node.node.get_and_clear_pending_msg_events();
1852 assert_eq!(events.len(), 1);
1853 check_added_monitors!($node, 1);
1854 let payment_event = SendEvent::from_event(events.remove(0));
1859 let feemsat = 239; // set above
1860 let total_fee_msat = (nodes.len() - 2) as u64 * feemsat;
1861 let feerate = get_feerate!(nodes[0], nodes[1], chan_1.2);
1862 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_1.2);
1864 let recv_value_0 = stat01.counterparty_max_htlc_value_in_flight_msat - total_fee_msat;
1866 // attempt to send amt_msat > their_max_htlc_value_in_flight_msat
1868 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1869 .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1870 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0);
1871 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1872 assert!(route.paths[0].hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
1874 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1875 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1876 ), true, APIError::ChannelUnavailable { .. }, {});
1877 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1880 // channel reserve is bigger than their_max_htlc_value_in_flight_msat so loop to deplete
1881 // nodes[0]'s wealth
1883 let amt_msat = recv_value_0 + total_fee_msat;
1884 // 3 for the 3 HTLCs that will be sent, 2* and +1 for the fee spike reserve.
1885 // Also, ensure that each payment has enough to be over the dust limit to
1886 // ensure it'll be included in each commit tx fee calculation.
1887 let commit_tx_fee_all_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1888 let ensure_htlc_amounts_above_dust_buffer = 3 * (stat01.counterparty_dust_limit_msat + 1000);
1889 if stat01.value_to_self_msat < stat01.channel_reserve_msat + commit_tx_fee_all_htlcs + ensure_htlc_amounts_above_dust_buffer + amt_msat {
1893 let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
1894 .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap().with_max_channel_saturation_power_of_half(0);
1895 let route = get_route!(nodes[0], payment_params, recv_value_0).unwrap();
1896 let (payment_preimage, ..) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], recv_value_0);
1897 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
1899 let (stat01_, stat11_, stat12_, stat22_) = (
1900 get_channel_value_stat!(nodes[0], nodes[1], chan_1.2),
1901 get_channel_value_stat!(nodes[1], nodes[0], chan_1.2),
1902 get_channel_value_stat!(nodes[1], nodes[2], chan_2.2),
1903 get_channel_value_stat!(nodes[2], nodes[1], chan_2.2),
1906 assert_eq!(stat01_.value_to_self_msat, stat01.value_to_self_msat - amt_msat);
1907 assert_eq!(stat11_.value_to_self_msat, stat11.value_to_self_msat + amt_msat);
1908 assert_eq!(stat12_.value_to_self_msat, stat12.value_to_self_msat - (amt_msat - feemsat));
1909 assert_eq!(stat22_.value_to_self_msat, stat22.value_to_self_msat + (amt_msat - feemsat));
1910 stat01 = stat01_; stat11 = stat11_; stat12 = stat12_; stat22 = stat22_;
1913 // adding pending output.
1914 // 2* and +1 HTLCs on the commit tx fee for the fee spike reserve.
1915 // The reason we're dividing by two here is as follows: the dividend is the total outbound liquidity
1916 // after fees, the channel reserve, and the fee spike buffer are removed. We eventually want to
1917 // divide this quantity into 3 portions, that will each be sent in an HTLC. This allows us
1918 // to test channel channel reserve policy at the edges of what amount is sendable, i.e.
1919 // cases where 1 msat over X amount will cause a payment failure, but anything less than
1920 // that can be sent successfully. So, dividing by two is a somewhat arbitrary way of getting
1921 // the amount of the first of these aforementioned 3 payments. The reason we split into 3 payments
1922 // is to test the behavior of the holding cell with respect to channel reserve and commit tx fee
1924 let commit_tx_fee_2_htlcs = 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features);
1925 let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
1926 let amt_msat_1 = recv_value_1 + total_fee_msat;
1928 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);
1929 let payment_event_1 = {
1930 nodes[0].node.send_payment_with_route(&route_1, our_payment_hash_1,
1931 RecipientOnionFields::secret_only(our_payment_secret_1), PaymentId(our_payment_hash_1.0)).unwrap();
1932 check_added_monitors!(nodes[0], 1);
1934 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1935 assert_eq!(events.len(), 1);
1936 SendEvent::from_event(events.remove(0))
1938 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event_1.msgs[0]);
1940 // channel reserve test with htlc pending output > 0
1941 let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs;
1943 let mut route = route_1.clone();
1944 route.paths[0].hops.last_mut().unwrap().fee_msat = recv_value_2 + 1;
1945 let (_, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(nodes[2]);
1946 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1947 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1948 ), true, APIError::ChannelUnavailable { .. }, {});
1949 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1952 // split the rest to test holding cell
1953 let commit_tx_fee_3_htlcs = 2*commit_tx_fee_msat(feerate, 3 + 1, &channel_type_features);
1954 let additional_htlc_cost_msat = commit_tx_fee_3_htlcs - commit_tx_fee_2_htlcs;
1955 let recv_value_21 = recv_value_2/2 - additional_htlc_cost_msat/2;
1956 let recv_value_22 = recv_value_2 - recv_value_21 - total_fee_msat - additional_htlc_cost_msat;
1958 let stat = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
1959 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);
1962 // now see if they go through on both sides
1963 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);
1964 // but this will stuck in the holding cell
1965 nodes[0].node.send_payment_with_route(&route_21, our_payment_hash_21,
1966 RecipientOnionFields::secret_only(our_payment_secret_21), PaymentId(our_payment_hash_21.0)).unwrap();
1967 check_added_monitors!(nodes[0], 0);
1968 let events = nodes[0].node.get_and_clear_pending_events();
1969 assert_eq!(events.len(), 0);
1971 // test with outbound holding cell amount > 0
1973 let (mut route, our_payment_hash, _, our_payment_secret) =
1974 get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
1975 route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
1976 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
1977 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
1978 ), true, APIError::ChannelUnavailable { .. }, {});
1979 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1982 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);
1983 // this will also stuck in the holding cell
1984 nodes[0].node.send_payment_with_route(&route_22, our_payment_hash_22,
1985 RecipientOnionFields::secret_only(our_payment_secret_22), PaymentId(our_payment_hash_22.0)).unwrap();
1986 check_added_monitors!(nodes[0], 0);
1987 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1988 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
1990 // flush the pending htlc
1991 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event_1.commitment_msg);
1992 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1993 check_added_monitors!(nodes[1], 1);
1995 // the pending htlc should be promoted to committed
1996 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
1997 check_added_monitors!(nodes[0], 1);
1998 let commitment_update_2 = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2000 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &as_commitment_signed);
2001 let bs_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2002 // No commitment_signed so get_event_msg's assert(len == 1) passes
2003 check_added_monitors!(nodes[0], 1);
2005 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &bs_revoke_and_ack);
2006 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2007 check_added_monitors!(nodes[1], 1);
2009 expect_pending_htlcs_forwardable!(nodes[1]);
2011 let ref payment_event_11 = expect_forward!(nodes[1]);
2012 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_11.msgs[0]);
2013 commitment_signed_dance!(nodes[2], nodes[1], payment_event_11.commitment_msg, false);
2015 expect_pending_htlcs_forwardable!(nodes[2]);
2016 expect_payment_claimable!(nodes[2], our_payment_hash_1, our_payment_secret_1, recv_value_1);
2018 // flush the htlcs in the holding cell
2019 assert_eq!(commitment_update_2.update_add_htlcs.len(), 2);
2020 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[0]);
2021 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &commitment_update_2.update_add_htlcs[1]);
2022 commitment_signed_dance!(nodes[1], nodes[0], &commitment_update_2.commitment_signed, false);
2023 expect_pending_htlcs_forwardable!(nodes[1]);
2025 let ref payment_event_3 = expect_forward!(nodes[1]);
2026 assert_eq!(payment_event_3.msgs.len(), 2);
2027 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[0]);
2028 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event_3.msgs[1]);
2030 commitment_signed_dance!(nodes[2], nodes[1], &payment_event_3.commitment_msg, false);
2031 expect_pending_htlcs_forwardable!(nodes[2]);
2033 let events = nodes[2].node.get_and_clear_pending_events();
2034 assert_eq!(events.len(), 2);
2036 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2037 assert_eq!(our_payment_hash_21, *payment_hash);
2038 assert_eq!(recv_value_21, amount_msat);
2039 assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2040 assert_eq!(via_channel_id, Some(chan_2.2));
2042 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2043 assert!(payment_preimage.is_none());
2044 assert_eq!(our_payment_secret_21, *payment_secret);
2046 _ => panic!("expected PaymentPurpose::InvoicePayment")
2049 _ => panic!("Unexpected event"),
2052 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
2053 assert_eq!(our_payment_hash_22, *payment_hash);
2054 assert_eq!(recv_value_22, amount_msat);
2055 assert_eq!(nodes[2].node.get_our_node_id(), receiver_node_id.unwrap());
2056 assert_eq!(via_channel_id, Some(chan_2.2));
2058 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
2059 assert!(payment_preimage.is_none());
2060 assert_eq!(our_payment_secret_22, *payment_secret);
2062 _ => panic!("expected PaymentPurpose::InvoicePayment")
2065 _ => panic!("Unexpected event"),
2068 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_1);
2069 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_21);
2070 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), our_payment_preimage_22);
2072 let commit_tx_fee_0_htlcs = 2*commit_tx_fee_msat(feerate, 1, &channel_type_features);
2073 let recv_value_3 = commit_tx_fee_2_htlcs - commit_tx_fee_0_htlcs - total_fee_msat;
2074 send_payment(&nodes[0], &vec![&nodes[1], &nodes[2]][..], recv_value_3);
2076 let commit_tx_fee_1_htlc = 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
2077 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);
2078 let stat0 = get_channel_value_stat!(nodes[0], nodes[1], chan_1.2);
2079 assert_eq!(stat0.value_to_self_msat, expected_value_to_self);
2080 assert_eq!(stat0.value_to_self_msat, stat0.channel_reserve_msat + commit_tx_fee_1_htlc);
2082 let stat2 = get_channel_value_stat!(nodes[2], nodes[1], chan_2.2);
2083 assert_eq!(stat2.value_to_self_msat, stat22.value_to_self_msat + recv_value_1 + recv_value_21 + recv_value_22 + recv_value_3);
2087 fn channel_reserve_in_flight_removes() {
2088 // In cases where one side claims an HTLC, it thinks it has additional available funds that it
2089 // can send to its counterparty, but due to update ordering, the other side may not yet have
2090 // considered those HTLCs fully removed.
2091 // This tests that we don't count HTLCs which will not be included in the next remote
2092 // commitment transaction towards the reserve value (as it implies no commitment transaction
2093 // will be generated which violates the remote reserve value).
2094 // This was broken previously, and discovered by the chanmon_fail_consistency fuzz test.
2096 // * route two HTLCs from A to B (note that, at a high level, this test is checking that, when
2097 // you consider the values of both of these HTLCs, B may not send an HTLC back to A, but if
2098 // you only consider the value of the first HTLC, it may not),
2099 // * start routing a third HTLC from A to B,
2100 // * claim the first two HTLCs (though B will generate an update_fulfill for one, and put
2101 // the other claim in its holding cell, as it immediately goes into AwaitingRAA),
2102 // * deliver the first fulfill from B
2103 // * deliver the update_add and an RAA from A, resulting in B freeing the second holding cell
2105 // * deliver A's response CS and RAA.
2106 // This results in A having the second HTLC in AwaitingRemovedRemoteRevoke, but B having
2107 // removed it fully. B now has the push_msat plus the first two HTLCs in value.
2108 // * Now B happily sends another HTLC, potentially violating its reserve value from A's point
2109 // of view (if A counts the AwaitingRemovedRemoteRevoke HTLC).
2110 let chanmon_cfgs = create_chanmon_cfgs(2);
2111 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2112 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2113 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2114 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2116 let b_chan_values = get_channel_value_stat!(nodes[1], nodes[0], chan_1.2);
2117 // Route the first two HTLCs.
2118 let payment_value_1 = b_chan_values.channel_reserve_msat - b_chan_values.value_to_self_msat - 10000;
2119 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], payment_value_1);
2120 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], 20_000);
2122 // Start routing the third HTLC (this is just used to get everyone in the right state).
2123 let (route, payment_hash_3, payment_preimage_3, payment_secret_3) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
2125 nodes[0].node.send_payment_with_route(&route, payment_hash_3,
2126 RecipientOnionFields::secret_only(payment_secret_3), PaymentId(payment_hash_3.0)).unwrap();
2127 check_added_monitors!(nodes[0], 1);
2128 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
2129 assert_eq!(events.len(), 1);
2130 SendEvent::from_event(events.remove(0))
2133 // Now claim both of the first two HTLCs on B's end, putting B in AwaitingRAA and generating an
2134 // initial fulfill/CS.
2135 nodes[1].node.claim_funds(payment_preimage_1);
2136 expect_payment_claimed!(nodes[1], payment_hash_1, payment_value_1);
2137 check_added_monitors!(nodes[1], 1);
2138 let bs_removes = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2140 // This claim goes in B's holding cell, allowing us to have a pending B->A RAA which does not
2141 // remove the second HTLC when we send the HTLC back from B to A.
2142 nodes[1].node.claim_funds(payment_preimage_2);
2143 expect_payment_claimed!(nodes[1], payment_hash_2, 20_000);
2144 check_added_monitors!(nodes[1], 1);
2145 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2147 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_removes.update_fulfill_htlcs[0]);
2148 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_removes.commitment_signed);
2149 check_added_monitors!(nodes[0], 1);
2150 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2151 expect_payment_sent(&nodes[0], payment_preimage_1, None, false, false);
2153 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_1.msgs[0]);
2154 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &send_1.commitment_msg);
2155 check_added_monitors!(nodes[1], 1);
2156 // B is already AwaitingRAA, so cant generate a CS here
2157 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2159 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2160 check_added_monitors!(nodes[1], 1);
2161 let bs_cs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
2163 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2164 check_added_monitors!(nodes[0], 1);
2165 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2167 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2168 check_added_monitors!(nodes[1], 1);
2169 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2171 // The second HTLCis removed, but as A is in AwaitingRAA it can't generate a CS here, so the
2172 // RAA that B generated above doesn't fully resolve the second HTLC from A's point of view.
2173 // However, the RAA A generates here *does* fully resolve the HTLC from B's point of view (as A
2174 // can no longer broadcast a commitment transaction with it and B has the preimage so can go
2175 // on-chain as necessary).
2176 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_cs.update_fulfill_htlcs[0]);
2177 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs.commitment_signed);
2178 check_added_monitors!(nodes[0], 1);
2179 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2180 expect_payment_sent(&nodes[0], payment_preimage_2, None, false, false);
2182 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2183 check_added_monitors!(nodes[1], 1);
2184 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
2186 expect_pending_htlcs_forwardable!(nodes[1]);
2187 expect_payment_claimable!(nodes[1], payment_hash_3, payment_secret_3, 100000);
2189 // Note that as this RAA was generated before the delivery of the update_fulfill it shouldn't
2190 // resolve the second HTLC from A's point of view.
2191 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2192 check_added_monitors!(nodes[0], 1);
2193 expect_payment_path_successful!(nodes[0]);
2194 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2196 // Now that B doesn't have the second RAA anymore, but A still does, send a payment from B back
2197 // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing.
2198 let (route, payment_hash_4, payment_preimage_4, payment_secret_4) = get_route_and_payment_hash!(nodes[1], nodes[0], 10000);
2200 nodes[1].node.send_payment_with_route(&route, payment_hash_4,
2201 RecipientOnionFields::secret_only(payment_secret_4), PaymentId(payment_hash_4.0)).unwrap();
2202 check_added_monitors!(nodes[1], 1);
2203 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2204 assert_eq!(events.len(), 1);
2205 SendEvent::from_event(events.remove(0))
2208 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_2.msgs[0]);
2209 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_2.commitment_msg);
2210 check_added_monitors!(nodes[0], 1);
2211 let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
2213 // Now just resolve all the outstanding messages/HTLCs for completeness...
2215 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2216 check_added_monitors!(nodes[1], 1);
2217 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2219 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_raa);
2220 check_added_monitors!(nodes[1], 1);
2222 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2223 check_added_monitors!(nodes[0], 1);
2224 expect_payment_path_successful!(nodes[0]);
2225 let as_cs = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
2227 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_cs.commitment_signed);
2228 check_added_monitors!(nodes[1], 1);
2229 let bs_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
2231 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
2232 check_added_monitors!(nodes[0], 1);
2234 expect_pending_htlcs_forwardable!(nodes[0]);
2235 expect_payment_claimable!(nodes[0], payment_hash_4, payment_secret_4, 10000);
2237 claim_payment(&nodes[1], &[&nodes[0]], payment_preimage_4);
2238 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_3);
2242 fn channel_monitor_network_test() {
2243 // Simple test which builds a network of ChannelManagers, connects them to each other, and
2244 // tests that ChannelMonitor is able to recover from various states.
2245 let chanmon_cfgs = create_chanmon_cfgs(5);
2246 let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
2247 let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
2248 let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
2250 // Create some initial channels
2251 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2252 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2253 let chan_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
2254 let chan_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
2256 // Make sure all nodes are at the same starting height
2257 connect_blocks(&nodes[0], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
2258 connect_blocks(&nodes[1], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
2259 connect_blocks(&nodes[2], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
2260 connect_blocks(&nodes[3], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);
2261 connect_blocks(&nodes[4], 4*CHAN_CONFIRM_DEPTH + 1 - nodes[4].best_block_info().1);
2263 // Rebalance the network a bit by relaying one payment through all the channels...
2264 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2265 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2266 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2267 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2], &nodes[3], &nodes[4])[..], 8000000);
2269 // Simple case with no pending HTLCs:
2270 nodes[1].node.force_close_broadcasting_latest_txn(&chan_1.2, &nodes[0].node.get_our_node_id()).unwrap();
2271 check_added_monitors!(nodes[1], 1);
2272 check_closed_broadcast!(nodes[1], true);
2274 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_1, None, HTLCType::NONE);
2275 assert_eq!(node_txn.len(), 1);
2276 mine_transaction(&nodes[0], &node_txn[0]);
2277 check_added_monitors!(nodes[0], 1);
2278 test_txn_broadcast(&nodes[0], &chan_1, Some(node_txn[0].clone()), HTLCType::NONE);
2280 check_closed_broadcast!(nodes[0], true);
2281 assert_eq!(nodes[0].node.list_channels().len(), 0);
2282 assert_eq!(nodes[1].node.list_channels().len(), 1);
2283 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2284 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
2286 // One pending HTLC is discarded by the force-close:
2287 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[1], &[&nodes[2], &nodes[3]], 3_000_000);
2289 // Simple case of one pending HTLC to HTLC-Timeout (note that the HTLC-Timeout is not
2290 // broadcasted until we reach the timelock time).
2291 nodes[1].node.force_close_broadcasting_latest_txn(&chan_2.2, &nodes[2].node.get_our_node_id()).unwrap();
2292 check_closed_broadcast!(nodes[1], true);
2293 check_added_monitors!(nodes[1], 1);
2295 let mut node_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::NONE);
2296 connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2297 test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
2298 mine_transaction(&nodes[2], &node_txn[0]);
2299 check_added_monitors!(nodes[2], 1);
2300 test_txn_broadcast(&nodes[2], &chan_2, Some(node_txn[0].clone()), HTLCType::NONE);
2302 check_closed_broadcast!(nodes[2], true);
2303 assert_eq!(nodes[1].node.list_channels().len(), 0);
2304 assert_eq!(nodes[2].node.list_channels().len(), 1);
2305 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
2306 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2308 macro_rules! claim_funds {
2309 ($node: expr, $prev_node: expr, $preimage: expr, $payment_hash: expr) => {
2311 $node.node.claim_funds($preimage);
2312 expect_payment_claimed!($node, $payment_hash, 3_000_000);
2313 check_added_monitors!($node, 1);
2315 let events = $node.node.get_and_clear_pending_msg_events();
2316 assert_eq!(events.len(), 1);
2318 MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, .. } } => {
2319 assert!(update_add_htlcs.is_empty());
2320 assert!(update_fail_htlcs.is_empty());
2321 assert_eq!(*node_id, $prev_node.node.get_our_node_id());
2323 _ => panic!("Unexpected event"),
2329 // nodes[3] gets the preimage, but nodes[2] already disconnected, resulting in a nodes[2]
2330 // HTLC-Timeout and a nodes[3] claim against it (+ its own announces)
2331 nodes[2].node.force_close_broadcasting_latest_txn(&chan_3.2, &nodes[3].node.get_our_node_id()).unwrap();
2332 check_added_monitors!(nodes[2], 1);
2333 check_closed_broadcast!(nodes[2], true);
2334 let node2_commitment_txid;
2336 let node_txn = test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::NONE);
2337 connect_blocks(&nodes[2], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + MIN_CLTV_EXPIRY_DELTA as u32 + 1);
2338 test_txn_broadcast(&nodes[2], &chan_3, None, HTLCType::TIMEOUT);
2339 node2_commitment_txid = node_txn[0].txid();
2341 // Claim the payment on nodes[3], giving it knowledge of the preimage
2342 claim_funds!(nodes[3], nodes[2], payment_preimage_1, payment_hash_1);
2343 mine_transaction(&nodes[3], &node_txn[0]);
2344 check_added_monitors!(nodes[3], 1);
2345 check_preimage_claim(&nodes[3], &node_txn);
2347 check_closed_broadcast!(nodes[3], true);
2348 assert_eq!(nodes[2].node.list_channels().len(), 0);
2349 assert_eq!(nodes[3].node.list_channels().len(), 1);
2350 check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[3].node.get_our_node_id()], 100000);
2351 check_closed_event!(nodes[3], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
2353 // Drop the ChannelMonitor for the previous channel to avoid it broadcasting transactions and
2354 // confusing us in the following tests.
2355 let chan_3_mon = nodes[3].chain_monitor.chain_monitor.remove_monitor(&OutPoint { txid: chan_3.3.txid(), index: 0 });
2357 // One pending HTLC to time out:
2358 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[3], &[&nodes[4]], 3_000_000);
2359 // CLTV expires at TEST_FINAL_CLTV + 1 (current height) + 1 (added in send_payment for
2362 let (close_chan_update_1, close_chan_update_2) = {
2363 connect_blocks(&nodes[3], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
2364 let events = nodes[3].node.get_and_clear_pending_msg_events();
2365 assert_eq!(events.len(), 2);
2366 let close_chan_update_1 = match events[0] {
2367 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2370 _ => panic!("Unexpected event"),
2373 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id } => {
2374 assert_eq!(node_id, nodes[4].node.get_our_node_id());
2376 _ => panic!("Unexpected event"),
2378 check_added_monitors!(nodes[3], 1);
2380 // Clear bumped claiming txn spending node 2 commitment tx. Bumped txn are generated after reaching some height timer.
2382 let mut node_txn = nodes[3].tx_broadcaster.txn_broadcasted.lock().unwrap();
2383 node_txn.retain(|tx| {
2384 if tx.input[0].previous_output.txid == node2_commitment_txid {
2390 let node_txn = test_txn_broadcast(&nodes[3], &chan_4, None, HTLCType::TIMEOUT);
2392 // Claim the payment on nodes[4], giving it knowledge of the preimage
2393 claim_funds!(nodes[4], nodes[3], payment_preimage_2, payment_hash_2);
2395 connect_blocks(&nodes[4], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
2396 let events = nodes[4].node.get_and_clear_pending_msg_events();
2397 assert_eq!(events.len(), 2);
2398 let close_chan_update_2 = match events[0] {
2399 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
2402 _ => panic!("Unexpected event"),
2405 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id } => {
2406 assert_eq!(node_id, nodes[3].node.get_our_node_id());
2408 _ => panic!("Unexpected event"),
2410 check_added_monitors!(nodes[4], 1);
2411 test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
2412 check_closed_event!(nodes[4], 1, ClosureReason::HolderForceClosed, [nodes[3].node.get_our_node_id()], 100000);
2414 mine_transaction(&nodes[4], &node_txn[0]);
2415 check_preimage_claim(&nodes[4], &node_txn);
2416 (close_chan_update_1, close_chan_update_2)
2418 nodes[3].gossip_sync.handle_channel_update(&close_chan_update_2).unwrap();
2419 nodes[4].gossip_sync.handle_channel_update(&close_chan_update_1).unwrap();
2420 assert_eq!(nodes[3].node.list_channels().len(), 0);
2421 assert_eq!(nodes[4].node.list_channels().len(), 0);
2423 assert_eq!(nodes[3].chain_monitor.chain_monitor.watch_channel(OutPoint { txid: chan_3.3.txid(), index: 0 }, chan_3_mon),
2424 Ok(ChannelMonitorUpdateStatus::Completed));
2425 check_closed_event!(nodes[3], 1, ClosureReason::HolderForceClosed, [nodes[4].node.get_our_node_id()], 100000);
2429 fn test_justice_tx_htlc_timeout() {
2430 // Test justice txn built on revoked HTLC-Timeout tx, against both sides
2431 let mut alice_config = UserConfig::default();
2432 alice_config.channel_handshake_config.announced_channel = true;
2433 alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2434 alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2435 let mut bob_config = UserConfig::default();
2436 bob_config.channel_handshake_config.announced_channel = true;
2437 bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2438 bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2439 let user_cfgs = [Some(alice_config), Some(bob_config)];
2440 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2441 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2442 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2443 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2444 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2445 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2446 // Create some new channels:
2447 let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1);
2449 // A pending HTLC which will be revoked:
2450 let payment_preimage_3 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2451 // Get the will-be-revoked local txn from nodes[0]
2452 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_5.2);
2453 assert_eq!(revoked_local_txn.len(), 2); // First commitment tx, then HTLC tx
2454 assert_eq!(revoked_local_txn[0].input.len(), 1);
2455 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_5.3.txid());
2456 assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to 0 are present
2457 assert_eq!(revoked_local_txn[1].input.len(), 1);
2458 assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2459 assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2460 // Revoke the old state
2461 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
2464 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2466 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2467 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2468 assert_eq!(node_txn[0].input.len(), 2); // We should claim the revoked output and the HTLC output
2469 check_spends!(node_txn[0], revoked_local_txn[0]);
2470 node_txn.swap_remove(0);
2472 check_added_monitors!(nodes[1], 1);
2473 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2474 test_txn_broadcast(&nodes[1], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2476 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2477 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2478 // Verify broadcast of revoked HTLC-timeout
2479 let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
2480 check_added_monitors!(nodes[0], 1);
2481 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2482 // Broadcast revoked HTLC-timeout on node 1
2483 mine_transaction(&nodes[1], &node_txn[1]);
2484 test_revoked_htlc_claim_txn_broadcast(&nodes[1], node_txn[1].clone(), revoked_local_txn[0].clone());
2486 get_announce_close_broadcast_events(&nodes, 0, 1);
2487 assert_eq!(nodes[0].node.list_channels().len(), 0);
2488 assert_eq!(nodes[1].node.list_channels().len(), 0);
2492 fn test_justice_tx_htlc_success() {
2493 // Test justice txn built on revoked HTLC-Success tx, against both sides
2494 let mut alice_config = UserConfig::default();
2495 alice_config.channel_handshake_config.announced_channel = true;
2496 alice_config.channel_handshake_limits.force_announced_channel_preference = false;
2497 alice_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 5;
2498 let mut bob_config = UserConfig::default();
2499 bob_config.channel_handshake_config.announced_channel = true;
2500 bob_config.channel_handshake_limits.force_announced_channel_preference = false;
2501 bob_config.channel_handshake_config.our_to_self_delay = 6 * 24 * 3;
2502 let user_cfgs = [Some(alice_config), Some(bob_config)];
2503 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2504 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2505 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
2506 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2507 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2508 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2509 // Create some new channels:
2510 let chan_6 = create_announced_chan_between_nodes(&nodes, 0, 1);
2512 // A pending HTLC which will be revoked:
2513 let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
2514 // Get the will-be-revoked local txn from B
2515 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_6.2);
2516 assert_eq!(revoked_local_txn.len(), 1); // Only commitment tx
2517 assert_eq!(revoked_local_txn[0].input.len(), 1);
2518 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_6.3.txid());
2519 assert_eq!(revoked_local_txn[0].output.len(), 2); // Only HTLC and output back to A are present
2520 // Revoke the old state
2521 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_4);
2523 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2525 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
2526 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2527 assert_eq!(node_txn[0].input.len(), 1); // We claim the received HTLC output
2529 check_spends!(node_txn[0], revoked_local_txn[0]);
2530 node_txn.swap_remove(0);
2532 check_added_monitors!(nodes[0], 1);
2533 test_txn_broadcast(&nodes[0], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
2535 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2536 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2537 let node_txn = test_txn_broadcast(&nodes[1], &chan_6, Some(revoked_local_txn[0].clone()), HTLCType::SUCCESS);
2538 check_added_monitors!(nodes[1], 1);
2539 mine_transaction(&nodes[0], &node_txn[1]);
2540 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2541 test_revoked_htlc_claim_txn_broadcast(&nodes[0], node_txn[1].clone(), revoked_local_txn[0].clone());
2543 get_announce_close_broadcast_events(&nodes, 0, 1);
2544 assert_eq!(nodes[0].node.list_channels().len(), 0);
2545 assert_eq!(nodes[1].node.list_channels().len(), 0);
2549 fn revoked_output_claim() {
2550 // Simple test to ensure a node will claim a revoked output when a stale remote commitment
2551 // transaction is broadcast by its counterparty
2552 let chanmon_cfgs = create_chanmon_cfgs(2);
2553 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2554 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2555 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2556 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2557 // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output
2558 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2559 assert_eq!(revoked_local_txn.len(), 1);
2560 // Only output is the full channel value back to nodes[0]:
2561 assert_eq!(revoked_local_txn[0].output.len(), 1);
2562 // Send a payment through, updating everyone's latest commitment txn
2563 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5000000);
2565 // Inform nodes[1] that nodes[0] broadcast a stale tx
2566 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2567 check_added_monitors!(nodes[1], 1);
2568 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2569 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2570 assert_eq!(node_txn.len(), 1); // ChannelMonitor: justice tx against revoked to_local output
2572 check_spends!(node_txn[0], revoked_local_txn[0]);
2574 // Inform nodes[0] that a watchtower cheated on its behalf, so it will force-close the chan
2575 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2576 get_announce_close_broadcast_events(&nodes, 0, 1);
2577 check_added_monitors!(nodes[0], 1);
2578 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2582 fn test_forming_justice_tx_from_monitor_updates() {
2583 do_test_forming_justice_tx_from_monitor_updates(true);
2584 do_test_forming_justice_tx_from_monitor_updates(false);
2587 fn do_test_forming_justice_tx_from_monitor_updates(broadcast_initial_commitment: bool) {
2588 // Simple test to make sure that the justice tx formed in WatchtowerPersister
2589 // is properly formed and can be broadcasted/confirmed successfully in the event
2590 // that a revoked commitment transaction is broadcasted
2591 // (Similar to `revoked_output_claim` test but we get the justice tx + broadcast manually)
2592 let chanmon_cfgs = create_chanmon_cfgs(2);
2593 let destination_script0 = chanmon_cfgs[0].keys_manager.get_destination_script([0; 32]).unwrap();
2594 let destination_script1 = chanmon_cfgs[1].keys_manager.get_destination_script([0; 32]).unwrap();
2595 let persisters = vec![WatchtowerPersister::new(destination_script0),
2596 WatchtowerPersister::new(destination_script1)];
2597 let node_cfgs = create_node_cfgs_with_persisters(2, &chanmon_cfgs, persisters.iter().collect());
2598 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2599 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2600 let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1);
2601 let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
2603 if !broadcast_initial_commitment {
2604 // Send a payment to move the channel forward
2605 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2608 // node[0] is gonna to revoke an old state thus node[1] should be able to claim the revoked output.
2609 // We'll keep this commitment transaction to broadcast once it's revoked.
2610 let revoked_local_txn = get_local_commitment_txn!(nodes[0], channel_id);
2611 assert_eq!(revoked_local_txn.len(), 1);
2612 let revoked_commitment_tx = &revoked_local_txn[0];
2614 // Send another payment, now revoking the previous commitment tx
2615 send_payment(&nodes[0], &vec!(&nodes[1])[..], 5_000_000);
2617 let justice_tx = persisters[1].justice_tx(funding_txo, &revoked_commitment_tx.txid()).unwrap();
2618 check_spends!(justice_tx, revoked_commitment_tx);
2620 mine_transactions(&nodes[1], &[revoked_commitment_tx, &justice_tx]);
2621 mine_transactions(&nodes[0], &[revoked_commitment_tx, &justice_tx]);
2623 check_added_monitors!(nodes[1], 1);
2624 check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false,
2625 &[nodes[0].node.get_our_node_id()], 100_000);
2626 get_announce_close_broadcast_events(&nodes, 1, 0);
2628 check_added_monitors!(nodes[0], 1);
2629 check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false,
2630 &[nodes[1].node.get_our_node_id()], 100_000);
2632 // Check that the justice tx has sent the revoked output value to nodes[1]
2633 let monitor = get_monitor!(nodes[1], channel_id);
2634 let total_claimable_balance = monitor.get_claimable_balances().iter().fold(0, |sum, balance| {
2636 channelmonitor::Balance::ClaimableAwaitingConfirmations { amount_satoshis, .. } => sum + amount_satoshis,
2637 _ => panic!("Unexpected balance type"),
2640 // On the first commitment, node[1]'s balance was below dust so it didn't have an output
2641 let node1_channel_balance = if broadcast_initial_commitment { 0 } else { revoked_commitment_tx.output[0].value };
2642 let expected_claimable_balance = node1_channel_balance + justice_tx.output[0].value;
2643 assert_eq!(total_claimable_balance, expected_claimable_balance);
2648 fn claim_htlc_outputs_shared_tx() {
2649 // Node revoked old state, htlcs haven't time out yet, claim them in shared justice tx
2650 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2651 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2652 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2653 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2654 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2656 // Create some new channel:
2657 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2659 // Rebalance the network to generate htlc in the two directions
2660 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2661 // 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
2662 let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2663 let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2665 // Get the will-be-revoked local txn from node[0]
2666 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2667 assert_eq!(revoked_local_txn.len(), 2); // commitment tx + 1 HTLC-Timeout tx
2668 assert_eq!(revoked_local_txn[0].input.len(), 1);
2669 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
2670 assert_eq!(revoked_local_txn[1].input.len(), 1);
2671 assert_eq!(revoked_local_txn[1].input[0].previous_output.txid, revoked_local_txn[0].txid());
2672 assert_eq!(revoked_local_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC-Timeout
2673 check_spends!(revoked_local_txn[1], revoked_local_txn[0]);
2675 //Revoke the old state
2676 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2679 mine_transaction(&nodes[0], &revoked_local_txn[0]);
2680 check_added_monitors!(nodes[0], 1);
2681 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2682 mine_transaction(&nodes[1], &revoked_local_txn[0]);
2683 check_added_monitors!(nodes[1], 1);
2684 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2685 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2686 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2688 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
2689 assert_eq!(node_txn.len(), 1); // ChannelMonitor: penalty tx
2691 assert_eq!(node_txn[0].input.len(), 3); // Claim the revoked output + both revoked HTLC outputs
2692 check_spends!(node_txn[0], revoked_local_txn[0]);
2694 let mut witness_lens = BTreeSet::new();
2695 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2696 witness_lens.insert(node_txn[0].input[1].witness.last().unwrap().len());
2697 witness_lens.insert(node_txn[0].input[2].witness.last().unwrap().len());
2698 assert_eq!(witness_lens.len(), 3);
2699 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2700 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2701 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2703 // Finally, mine the penalty transaction and check that we get an HTLC failure after
2704 // ANTI_REORG_DELAY confirmations.
2705 mine_transaction(&nodes[1], &node_txn[0]);
2706 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2707 expect_payment_failed!(nodes[1], payment_hash_2, false);
2709 get_announce_close_broadcast_events(&nodes, 0, 1);
2710 assert_eq!(nodes[0].node.list_channels().len(), 0);
2711 assert_eq!(nodes[1].node.list_channels().len(), 0);
2715 fn claim_htlc_outputs_single_tx() {
2716 // Node revoked old state, htlcs have timed out, claim each of them in separated justice tx
2717 let mut chanmon_cfgs = create_chanmon_cfgs(2);
2718 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
2719 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
2720 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
2721 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2723 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2725 // Rebalance the network to generate htlc in the two directions
2726 send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
2727 // 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
2728 // time as two different claim transactions as we're gonna to timeout htlc with given a high current height
2729 let payment_preimage_1 = route_payment(&nodes[0], &[&nodes[1]], 3_000_000).0;
2730 let (_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[1], &[&nodes[0]], 3_000_000);
2732 // Get the will-be-revoked local txn from node[0]
2733 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
2735 //Revoke the old state
2736 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
2739 confirm_transaction_at(&nodes[0], &revoked_local_txn[0], 100);
2740 check_added_monitors!(nodes[0], 1);
2741 confirm_transaction_at(&nodes[1], &revoked_local_txn[0], 100);
2742 check_added_monitors!(nodes[1], 1);
2743 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2744 let mut events = nodes[0].node.get_and_clear_pending_events();
2745 expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
2746 match events.last().unwrap() {
2747 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2748 _ => panic!("Unexpected event"),
2751 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2752 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
2754 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcast();
2756 // Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
2757 assert_eq!(node_txn[0].input.len(), 1);
2758 check_spends!(node_txn[0], chan_1.3);
2759 assert_eq!(node_txn[1].input.len(), 1);
2760 let witness_script = node_txn[1].input[0].witness.last().unwrap();
2761 assert_eq!(witness_script.len(), OFFERED_HTLC_SCRIPT_WEIGHT); //Spending an offered htlc output
2762 check_spends!(node_txn[1], node_txn[0]);
2764 // Filter out any non justice transactions.
2765 node_txn.retain(|tx| tx.input[0].previous_output.txid == revoked_local_txn[0].txid());
2766 assert!(node_txn.len() > 3);
2768 assert_eq!(node_txn[0].input.len(), 1);
2769 assert_eq!(node_txn[1].input.len(), 1);
2770 assert_eq!(node_txn[2].input.len(), 1);
2772 check_spends!(node_txn[0], revoked_local_txn[0]);
2773 check_spends!(node_txn[1], revoked_local_txn[0]);
2774 check_spends!(node_txn[2], revoked_local_txn[0]);
2776 let mut witness_lens = BTreeSet::new();
2777 witness_lens.insert(node_txn[0].input[0].witness.last().unwrap().len());
2778 witness_lens.insert(node_txn[1].input[0].witness.last().unwrap().len());
2779 witness_lens.insert(node_txn[2].input[0].witness.last().unwrap().len());
2780 assert_eq!(witness_lens.len(), 3);
2781 assert_eq!(*witness_lens.iter().skip(0).next().unwrap(), 77); // revoked to_local
2782 assert_eq!(*witness_lens.iter().skip(1).next().unwrap(), OFFERED_HTLC_SCRIPT_WEIGHT); // revoked offered HTLC
2783 assert_eq!(*witness_lens.iter().skip(2).next().unwrap(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // revoked received HTLC
2785 // Finally, mine the penalty transactions and check that we get an HTLC failure after
2786 // ANTI_REORG_DELAY confirmations.
2787 mine_transaction(&nodes[1], &node_txn[0]);
2788 mine_transaction(&nodes[1], &node_txn[1]);
2789 mine_transaction(&nodes[1], &node_txn[2]);
2790 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
2791 expect_payment_failed!(nodes[1], payment_hash_2, false);
2793 get_announce_close_broadcast_events(&nodes, 0, 1);
2794 assert_eq!(nodes[0].node.list_channels().len(), 0);
2795 assert_eq!(nodes[1].node.list_channels().len(), 0);
2799 fn test_htlc_on_chain_success() {
2800 // Test that in case of a unilateral close onchain, we detect the state of output and pass
2801 // the preimage backward accordingly. So here we test that ChannelManager is
2802 // broadcasting the right event to other nodes in payment path.
2803 // We test with two HTLCs simultaneously as that was not handled correctly in the past.
2804 // A --------------------> B ----------------------> C (preimage)
2805 // First, C should claim the HTLC outputs via HTLC-Success when its own latest local
2806 // commitment transaction was broadcast.
2807 // Then, B should learn the preimage from said transactions, attempting to claim backwards
2809 // B should be able to claim via preimage if A then broadcasts its local tx.
2810 // Finally, when A sees B's latest local commitment transaction it should be able to claim
2811 // the HTLC outputs via the preimage it learned (which, once confirmed should generate a
2812 // PaymentSent event).
2814 let chanmon_cfgs = create_chanmon_cfgs(3);
2815 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
2816 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
2817 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
2819 // Create some initial channels
2820 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
2821 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
2823 // Ensure all nodes are at the same height
2824 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
2825 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
2826 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
2827 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
2829 // Rebalance the network a bit by relaying one payment through all the channels...
2830 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2831 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
2833 let (our_payment_preimage, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2834 let (our_payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
2836 // Broadcast legit commitment tx from C on B's chain
2837 // Broadcast HTLC Success transaction by C on received output from C's commitment tx on B's chain
2838 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
2839 assert_eq!(commitment_tx.len(), 1);
2840 check_spends!(commitment_tx[0], chan_2.3);
2841 nodes[2].node.claim_funds(our_payment_preimage);
2842 expect_payment_claimed!(nodes[2], payment_hash_1, 3_000_000);
2843 nodes[2].node.claim_funds(our_payment_preimage_2);
2844 expect_payment_claimed!(nodes[2], payment_hash_2, 3_000_000);
2845 check_added_monitors!(nodes[2], 2);
2846 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
2847 assert!(updates.update_add_htlcs.is_empty());
2848 assert!(updates.update_fail_htlcs.is_empty());
2849 assert!(updates.update_fail_malformed_htlcs.is_empty());
2850 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
2852 mine_transaction(&nodes[2], &commitment_tx[0]);
2853 check_closed_broadcast!(nodes[2], true);
2854 check_added_monitors!(nodes[2], 1);
2855 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
2856 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 2 (2 * HTLC-Success tx)
2857 assert_eq!(node_txn.len(), 2);
2858 check_spends!(node_txn[0], commitment_tx[0]);
2859 check_spends!(node_txn[1], commitment_tx[0]);
2860 assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2861 assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2862 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2863 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2864 assert_eq!(node_txn[0].lock_time, LockTime::ZERO);
2865 assert_eq!(node_txn[1].lock_time, LockTime::ZERO);
2867 // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
2868 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()]));
2869 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
2871 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2872 assert_eq!(added_monitors.len(), 1);
2873 assert_eq!(added_monitors[0].0.txid, chan_2.3.txid());
2874 added_monitors.clear();
2876 let forwarded_events = nodes[1].node.get_and_clear_pending_events();
2877 assert_eq!(forwarded_events.len(), 3);
2878 match forwarded_events[0] {
2879 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
2880 _ => panic!("Unexpected event"),
2882 let chan_id = Some(chan_1.2);
2883 match forwarded_events[1] {
2884 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2885 assert_eq!(fee_earned_msat, Some(1000));
2886 assert_eq!(prev_channel_id, chan_id);
2887 assert_eq!(claim_from_onchain_tx, true);
2888 assert_eq!(next_channel_id, Some(chan_2.2));
2889 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2893 match forwarded_events[2] {
2894 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
2895 assert_eq!(fee_earned_msat, Some(1000));
2896 assert_eq!(prev_channel_id, chan_id);
2897 assert_eq!(claim_from_onchain_tx, true);
2898 assert_eq!(next_channel_id, Some(chan_2.2));
2899 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
2903 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
2905 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
2906 assert_eq!(added_monitors.len(), 2);
2907 assert_eq!(added_monitors[0].0.txid, chan_1.3.txid());
2908 assert_eq!(added_monitors[1].0.txid, chan_1.3.txid());
2909 added_monitors.clear();
2911 assert_eq!(events.len(), 3);
2913 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
2914 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
2916 match nodes_2_event {
2917 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id: _ } => {},
2918 _ => panic!("Unexpected event"),
2921 match nodes_0_event {
2922 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, .. } } => {
2923 assert!(update_add_htlcs.is_empty());
2924 assert!(update_fail_htlcs.is_empty());
2925 assert_eq!(update_fulfill_htlcs.len(), 1);
2926 assert!(update_fail_malformed_htlcs.is_empty());
2927 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
2929 _ => panic!("Unexpected event"),
2932 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
2934 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
2935 _ => panic!("Unexpected event"),
2938 macro_rules! check_tx_local_broadcast {
2939 ($node: expr, $htlc_offered: expr, $commitment_tx: expr) => { {
2940 let mut node_txn = $node.tx_broadcaster.txn_broadcasted.lock().unwrap();
2941 assert_eq!(node_txn.len(), 2);
2942 // Node[1]: 2 * HTLC-timeout tx
2943 // Node[0]: 2 * HTLC-timeout tx
2944 check_spends!(node_txn[0], $commitment_tx);
2945 check_spends!(node_txn[1], $commitment_tx);
2946 assert_ne!(node_txn[0].lock_time, LockTime::ZERO);
2947 assert_ne!(node_txn[1].lock_time, LockTime::ZERO);
2949 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2950 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2951 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2952 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
2954 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2955 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
2956 assert!(node_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2957 assert!(node_txn[1].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
2962 // nodes[1] now broadcasts its own timeout-claim of the output that nodes[2] just claimed via success.
2963 check_tx_local_broadcast!(nodes[1], false, commitment_tx[0]);
2965 // Broadcast legit commitment tx from A on B's chain
2966 // Broadcast preimage tx by B on offered output from A commitment tx on A's chain
2967 let node_a_commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
2968 check_spends!(node_a_commitment_tx[0], chan_1.3);
2969 mine_transaction(&nodes[1], &node_a_commitment_tx[0]);
2970 check_closed_broadcast!(nodes[1], true);
2971 check_added_monitors!(nodes[1], 1);
2972 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
2973 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
2974 assert!(node_txn.len() == 1 || node_txn.len() == 3); // HTLC-Success, 2* RBF bumps of above HTLC txn
2975 let commitment_spend =
2976 if node_txn.len() == 1 {
2979 // Certain `ConnectStyle`s will cause RBF bumps of the previous HTLC transaction to be broadcast.
2980 // FullBlockViaListen
2981 if node_txn[0].input[0].previous_output.txid == node_a_commitment_tx[0].txid() {
2982 check_spends!(node_txn[1], commitment_tx[0]);
2983 check_spends!(node_txn[2], commitment_tx[0]);
2984 assert_ne!(node_txn[1].input[0].previous_output.vout, node_txn[2].input[0].previous_output.vout);
2987 check_spends!(node_txn[0], commitment_tx[0]);
2988 check_spends!(node_txn[1], commitment_tx[0]);
2989 assert_ne!(node_txn[0].input[0].previous_output.vout, node_txn[1].input[0].previous_output.vout);
2994 check_spends!(commitment_spend, node_a_commitment_tx[0]);
2995 assert_eq!(commitment_spend.input.len(), 2);
2996 assert_eq!(commitment_spend.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2997 assert_eq!(commitment_spend.input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
2998 assert_eq!(commitment_spend.lock_time.to_consensus_u32(), nodes[1].best_block_info().1);
2999 assert!(commitment_spend.output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
3000 // We don't bother to check that B can claim the HTLC output on its commitment tx here as
3001 // we already checked the same situation with A.
3003 // Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
3004 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![node_a_commitment_tx[0].clone(), commitment_spend.clone()]));
3005 connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
3006 check_closed_broadcast!(nodes[0], true);
3007 check_added_monitors!(nodes[0], 1);
3008 let events = nodes[0].node.get_and_clear_pending_events();
3009 assert_eq!(events.len(), 5);
3010 let mut first_claimed = false;
3011 for event in events {
3013 Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3014 if payment_preimage == our_payment_preimage && payment_hash == payment_hash_1 {
3015 assert!(!first_claimed);
3016 first_claimed = true;
3018 assert_eq!(payment_preimage, our_payment_preimage_2);
3019 assert_eq!(payment_hash, payment_hash_2);
3022 Event::PaymentPathSuccessful { .. } => {},
3023 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {},
3024 _ => panic!("Unexpected event"),
3027 check_tx_local_broadcast!(nodes[0], true, node_a_commitment_tx[0]);
3030 fn do_test_htlc_on_chain_timeout(connect_style: ConnectStyle) {
3031 // Test that in case of a unilateral close onchain, we detect the state of output and
3032 // timeout the HTLC backward accordingly. So here we test that ChannelManager is
3033 // broadcasting the right event to other nodes in payment path.
3034 // A ------------------> B ----------------------> C (timeout)
3035 // B's commitment tx C's commitment tx
3037 // B's HTLC timeout tx B's timeout tx
3039 let chanmon_cfgs = create_chanmon_cfgs(3);
3040 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3041 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3042 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3043 *nodes[0].connect_style.borrow_mut() = connect_style;
3044 *nodes[1].connect_style.borrow_mut() = connect_style;
3045 *nodes[2].connect_style.borrow_mut() = connect_style;
3047 // Create some intial channels
3048 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
3049 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3051 // Rebalance the network a bit by relaying one payment thorugh all the channels...
3052 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3053 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
3055 let (_payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), 3000000);
3057 // Broadcast legit commitment tx from C on B's chain
3058 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
3059 check_spends!(commitment_tx[0], chan_2.3);
3060 nodes[2].node.fail_htlc_backwards(&payment_hash);
3061 check_added_monitors!(nodes[2], 0);
3062 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }]);
3063 check_added_monitors!(nodes[2], 1);
3065 let events = nodes[2].node.get_and_clear_pending_msg_events();
3066 assert_eq!(events.len(), 1);
3068 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, .. } } => {
3069 assert!(update_add_htlcs.is_empty());
3070 assert!(!update_fail_htlcs.is_empty());
3071 assert!(update_fulfill_htlcs.is_empty());
3072 assert!(update_fail_malformed_htlcs.is_empty());
3073 assert_eq!(nodes[1].node.get_our_node_id(), *node_id);
3075 _ => panic!("Unexpected event"),
3077 mine_transaction(&nodes[2], &commitment_tx[0]);
3078 check_closed_broadcast!(nodes[2], true);
3079 check_added_monitors!(nodes[2], 1);
3080 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3081 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
3082 assert_eq!(node_txn.len(), 0);
3084 // Broadcast timeout transaction by B on received output from C's commitment tx on B's chain
3085 // Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
3086 mine_transaction(&nodes[1], &commitment_tx[0]);
3087 check_closed_event!(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false
3088 , [nodes[2].node.get_our_node_id()], 100000);
3089 connect_blocks(&nodes[1], 200 - nodes[2].best_block_info().1);
3091 let mut txn = nodes[1].tx_broadcaster.txn_broadcast();
3092 if nodes[1].connect_style.borrow().skips_blocks() {
3093 assert_eq!(txn.len(), 1);
3095 assert_eq!(txn.len(), 3); // Two extra fee bumps for timeout transaction
3097 txn.iter().for_each(|tx| check_spends!(tx, commitment_tx[0]));
3098 assert_eq!(txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3102 mine_transaction(&nodes[1], &timeout_tx);
3103 check_added_monitors!(nodes[1], 1);
3104 check_closed_broadcast!(nodes[1], true);
3106 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3108 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 }]);
3109 check_added_monitors!(nodes[1], 1);
3110 let events = nodes[1].node.get_and_clear_pending_msg_events();
3111 assert_eq!(events.len(), 1);
3113 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, .. } } => {
3114 assert!(update_add_htlcs.is_empty());
3115 assert!(!update_fail_htlcs.is_empty());
3116 assert!(update_fulfill_htlcs.is_empty());
3117 assert!(update_fail_malformed_htlcs.is_empty());
3118 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3120 _ => panic!("Unexpected event"),
3123 // Broadcast legit commitment tx from B on A's chain
3124 let commitment_tx = get_local_commitment_txn!(nodes[1], chan_1.2);
3125 check_spends!(commitment_tx[0], chan_1.3);
3127 mine_transaction(&nodes[0], &commitment_tx[0]);
3128 connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
3130 check_closed_broadcast!(nodes[0], true);
3131 check_added_monitors!(nodes[0], 1);
3132 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
3133 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // 1 timeout tx
3134 assert_eq!(node_txn.len(), 1);
3135 check_spends!(node_txn[0], commitment_tx[0]);
3136 assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
3140 fn test_htlc_on_chain_timeout() {
3141 do_test_htlc_on_chain_timeout(ConnectStyle::BestBlockFirstSkippingBlocks);
3142 do_test_htlc_on_chain_timeout(ConnectStyle::TransactionsFirstSkippingBlocks);
3143 do_test_htlc_on_chain_timeout(ConnectStyle::FullBlockViaListen);
3147 fn test_simple_commitment_revoked_fail_backward() {
3148 // Test that in case of a revoked commitment tx, we detect the resolution of output by justice tx
3149 // and fail backward accordingly.
3151 let chanmon_cfgs = create_chanmon_cfgs(3);
3152 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3153 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3154 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3156 // Create some initial channels
3157 create_announced_chan_between_nodes(&nodes, 0, 1);
3158 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3160 let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3161 // Get the will-be-revoked local txn from nodes[2]
3162 let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3163 // Revoke the old state
3164 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3166 let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3000000);
3168 mine_transaction(&nodes[1], &revoked_local_txn[0]);
3169 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3170 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3171 check_added_monitors!(nodes[1], 1);
3172 check_closed_broadcast!(nodes[1], true);
3174 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 }]);
3175 check_added_monitors!(nodes[1], 1);
3176 let events = nodes[1].node.get_and_clear_pending_msg_events();
3177 assert_eq!(events.len(), 1);
3179 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, .. } } => {
3180 assert!(update_add_htlcs.is_empty());
3181 assert_eq!(update_fail_htlcs.len(), 1);
3182 assert!(update_fulfill_htlcs.is_empty());
3183 assert!(update_fail_malformed_htlcs.is_empty());
3184 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3186 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3187 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3188 expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_2.0.contents.short_channel_id, true);
3190 _ => panic!("Unexpected event"),
3194 fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use_dust: bool, no_to_remote: bool) {
3195 // Test that if our counterparty broadcasts a revoked commitment transaction we fail all
3196 // pending HTLCs on that channel backwards even if the HTLCs aren't present in our latest
3197 // commitment transaction anymore.
3198 // To do this, we have the peer which will broadcast a revoked commitment transaction send
3199 // a number of update_fail/commitment_signed updates without ever sending the RAA in
3200 // response to our commitment_signed. This is somewhat misbehavior-y, though not
3201 // technically disallowed and we should probably handle it reasonably.
3202 // Note that this is pretty exhaustive as an outbound HTLC which we haven't yet
3203 // failed/fulfilled backwards must be in at least one of the latest two remote commitment
3205 // * Once we move it out of our holding cell/add it, we will immediately include it in a
3206 // commitment_signed (implying it will be in the latest remote commitment transaction).
3207 // * Once they remove it, we will send a (the first) commitment_signed without the HTLC,
3208 // and once they revoke the previous commitment transaction (allowing us to send a new
3209 // commitment_signed) we will be free to fail/fulfill the HTLC backwards.
3210 let chanmon_cfgs = create_chanmon_cfgs(3);
3211 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3212 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3213 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3215 // Create some initial channels
3216 create_announced_chan_between_nodes(&nodes, 0, 1);
3217 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
3219 let (payment_preimage, _payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], if no_to_remote { 10_000 } else { 3_000_000 });
3220 // Get the will-be-revoked local txn from nodes[2]
3221 let revoked_local_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
3222 assert_eq!(revoked_local_txn[0].output.len(), if no_to_remote { 1 } else { 2 });
3223 // Revoke the old state
3224 claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
3226 let value = if use_dust {
3227 // The dust limit applied to HTLC outputs considers the fee of the HTLC transaction as
3228 // well, so HTLCs at exactly the dust limit will not be included in commitment txn.
3229 nodes[2].node.per_peer_state.read().unwrap().get(&nodes[1].node.get_our_node_id())
3230 .unwrap().lock().unwrap().channel_by_id.get(&chan_2.2).unwrap().context().holder_dust_limit_satoshis * 1000
3233 let (_, first_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3234 let (_, second_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3235 let (_, third_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
3237 nodes[2].node.fail_htlc_backwards(&first_payment_hash);
3238 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: first_payment_hash }]);
3239 check_added_monitors!(nodes[2], 1);
3240 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3241 assert!(updates.update_add_htlcs.is_empty());
3242 assert!(updates.update_fulfill_htlcs.is_empty());
3243 assert!(updates.update_fail_malformed_htlcs.is_empty());
3244 assert_eq!(updates.update_fail_htlcs.len(), 1);
3245 assert!(updates.update_fee.is_none());
3246 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3247 let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
3248 // Drop the last RAA from 3 -> 2
3250 nodes[2].node.fail_htlc_backwards(&second_payment_hash);
3251 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: second_payment_hash }]);
3252 check_added_monitors!(nodes[2], 1);
3253 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3254 assert!(updates.update_add_htlcs.is_empty());
3255 assert!(updates.update_fulfill_htlcs.is_empty());
3256 assert!(updates.update_fail_malformed_htlcs.is_empty());
3257 assert_eq!(updates.update_fail_htlcs.len(), 1);
3258 assert!(updates.update_fee.is_none());
3259 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3260 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3261 check_added_monitors!(nodes[1], 1);
3262 // Note that nodes[1] is in AwaitingRAA, so won't send a CS
3263 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3264 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3265 check_added_monitors!(nodes[2], 1);
3267 nodes[2].node.fail_htlc_backwards(&third_payment_hash);
3268 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::FailedPayment { payment_hash: third_payment_hash }]);
3269 check_added_monitors!(nodes[2], 1);
3270 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3271 assert!(updates.update_add_htlcs.is_empty());
3272 assert!(updates.update_fulfill_htlcs.is_empty());
3273 assert!(updates.update_fail_malformed_htlcs.is_empty());
3274 assert_eq!(updates.update_fail_htlcs.len(), 1);
3275 assert!(updates.update_fee.is_none());
3276 nodes[1].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
3277 // At this point first_payment_hash has dropped out of the latest two commitment
3278 // transactions that nodes[1] is tracking...
3279 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &updates.commitment_signed);
3280 check_added_monitors!(nodes[1], 1);
3281 // Note that nodes[1] is (still) in AwaitingRAA, so won't send a CS
3282 let as_raa = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[2].node.get_our_node_id());
3283 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
3284 check_added_monitors!(nodes[2], 1);
3286 // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting
3287 // on nodes[2]'s RAA.
3288 let (route, fourth_payment_hash, _, fourth_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 1000000);
3289 nodes[1].node.send_payment_with_route(&route, fourth_payment_hash,
3290 RecipientOnionFields::secret_only(fourth_payment_secret), PaymentId(fourth_payment_hash.0)).unwrap();
3291 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3292 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3293 check_added_monitors!(nodes[1], 0);
3296 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &bs_raa);
3297 // One monitor for the new revocation preimage, no second on as we won't generate a new
3298 // commitment transaction for nodes[0] until process_pending_htlc_forwards().
3299 check_added_monitors!(nodes[1], 1);
3300 let events = nodes[1].node.get_and_clear_pending_events();
3301 assert_eq!(events.len(), 2);
3303 Event::PendingHTLCsForwardable { .. } => { },
3304 _ => panic!("Unexpected event"),
3307 Event::HTLCHandlingFailed { .. } => { },
3308 _ => panic!("Unexpected event"),
3310 // Deliberately don't process the pending fail-back so they all fail back at once after
3311 // block connection just like the !deliver_bs_raa case
3314 let mut failed_htlcs = HashSet::new();
3315 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
3317 mine_transaction(&nodes[1], &revoked_local_txn[0]);
3318 check_added_monitors!(nodes[1], 1);
3319 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
3321 let events = nodes[1].node.get_and_clear_pending_events();
3322 assert_eq!(events.len(), if deliver_bs_raa { 3 + nodes.len() - 1 } else { 4 + nodes.len() });
3324 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => { },
3325 _ => panic!("Unexepected event"),
3328 Event::PaymentPathFailed { ref payment_hash, .. } => {
3329 assert_eq!(*payment_hash, fourth_payment_hash);
3331 _ => panic!("Unexpected event"),
3334 Event::PaymentFailed { ref payment_hash, .. } => {
3335 assert_eq!(*payment_hash, fourth_payment_hash);
3337 _ => panic!("Unexpected event"),
3340 nodes[1].node.process_pending_htlc_forwards();
3341 check_added_monitors!(nodes[1], 1);
3343 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
3344 assert_eq!(events.len(), if deliver_bs_raa { 4 } else { 3 });
3347 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3348 match nodes_2_event {
3349 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, .. } } => {
3350 assert_eq!(nodes[2].node.get_our_node_id(), *node_id);
3351 assert_eq!(update_add_htlcs.len(), 1);
3352 assert!(update_fulfill_htlcs.is_empty());
3353 assert!(update_fail_htlcs.is_empty());
3354 assert!(update_fail_malformed_htlcs.is_empty());
3356 _ => panic!("Unexpected event"),
3360 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
3361 match nodes_2_event {
3362 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { msg: Some(msgs::ErrorMessage { channel_id, ref data }) }, node_id: _ } => {
3363 assert_eq!(channel_id, chan_2.2);
3364 assert_eq!(data.as_str(), "Channel closed because commitment or closing transaction was confirmed on chain.");
3366 _ => panic!("Unexpected event"),
3369 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut events);
3370 match nodes_0_event {
3371 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, .. } } => {
3372 assert!(update_add_htlcs.is_empty());
3373 assert_eq!(update_fail_htlcs.len(), 3);
3374 assert!(update_fulfill_htlcs.is_empty());
3375 assert!(update_fail_malformed_htlcs.is_empty());
3376 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
3378 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
3379 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[1]);
3380 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[2]);
3382 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
3384 let events = nodes[0].node.get_and_clear_pending_events();
3385 assert_eq!(events.len(), 6);
3387 Event::PaymentPathFailed { ref payment_hash, ref failure, .. } => {
3388 assert!(failed_htlcs.insert(payment_hash.0));
3389 // If we delivered B's RAA we got an unknown preimage error, not something
3390 // that we should update our routing table for.
3391 if !deliver_bs_raa {
3392 if let PathFailure::OnPath { network_update: Some(_) } = failure { } else { panic!("Unexpected path failure") }
3395 _ => panic!("Unexpected event"),
3398 Event::PaymentFailed { ref payment_hash, .. } => {
3399 assert_eq!(*payment_hash, first_payment_hash);
3401 _ => panic!("Unexpected event"),
3404 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3405 assert!(failed_htlcs.insert(payment_hash.0));
3407 _ => panic!("Unexpected event"),
3410 Event::PaymentFailed { ref payment_hash, .. } => {
3411 assert_eq!(*payment_hash, second_payment_hash);
3413 _ => panic!("Unexpected event"),
3416 Event::PaymentPathFailed { ref payment_hash, failure: PathFailure::OnPath { network_update: Some(_) }, .. } => {
3417 assert!(failed_htlcs.insert(payment_hash.0));
3419 _ => panic!("Unexpected event"),
3422 Event::PaymentFailed { ref payment_hash, .. } => {
3423 assert_eq!(*payment_hash, third_payment_hash);
3425 _ => panic!("Unexpected event"),
3428 _ => panic!("Unexpected event"),
3431 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
3433 MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
3434 _ => panic!("Unexpected event"),
3437 assert!(failed_htlcs.contains(&first_payment_hash.0));
3438 assert!(failed_htlcs.contains(&second_payment_hash.0));
3439 assert!(failed_htlcs.contains(&third_payment_hash.0));
3443 fn test_commitment_revoked_fail_backward_exhaustive_a() {
3444 do_test_commitment_revoked_fail_backward_exhaustive(false, true, false);
3445 do_test_commitment_revoked_fail_backward_exhaustive(true, true, false);
3446 do_test_commitment_revoked_fail_backward_exhaustive(false, false, false);
3447 do_test_commitment_revoked_fail_backward_exhaustive(true, false, false);
3451 fn test_commitment_revoked_fail_backward_exhaustive_b() {
3452 do_test_commitment_revoked_fail_backward_exhaustive(false, true, true);
3453 do_test_commitment_revoked_fail_backward_exhaustive(true, true, true);
3454 do_test_commitment_revoked_fail_backward_exhaustive(false, false, true);
3455 do_test_commitment_revoked_fail_backward_exhaustive(true, false, true);
3459 fn fail_backward_pending_htlc_upon_channel_failure() {
3460 let chanmon_cfgs = create_chanmon_cfgs(2);
3461 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3462 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3463 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3464 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
3466 // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack.
3468 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3469 nodes[0].node.send_payment_with_route(&route, payment_hash, RecipientOnionFields::secret_only(payment_secret),
3470 PaymentId(payment_hash.0)).unwrap();
3471 check_added_monitors!(nodes[0], 1);
3473 let payment_event = {
3474 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3475 assert_eq!(events.len(), 1);
3476 SendEvent::from_event(events.remove(0))
3478 assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id());
3479 assert_eq!(payment_event.msgs.len(), 1);
3482 // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack.
3483 let (route, failed_payment_hash, _, failed_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 50_000);
3485 nodes[0].node.send_payment_with_route(&route, failed_payment_hash,
3486 RecipientOnionFields::secret_only(failed_payment_secret), PaymentId(failed_payment_hash.0)).unwrap();
3487 check_added_monitors!(nodes[0], 0);
3489 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3492 // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel.
3494 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 50_000);
3496 let secp_ctx = Secp256k1::new();
3497 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
3498 let current_height = nodes[1].node.best_block.read().unwrap().height() + 1;
3499 let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(
3500 &route.paths[0], 50_000, RecipientOnionFields::secret_only(payment_secret), current_height, &None).unwrap();
3501 let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap();
3502 let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
3504 // Send a 0-msat update_add_htlc to fail the channel.
3505 let update_add_htlc = msgs::UpdateAddHTLC {
3511 onion_routing_packet,
3512 skimmed_fee_msat: None,
3514 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc);
3516 let events = nodes[0].node.get_and_clear_pending_events();
3517 assert_eq!(events.len(), 3);
3518 // Check that Alice fails backward the pending HTLC from the second payment.
3520 Event::PaymentPathFailed { payment_hash, .. } => {
3521 assert_eq!(payment_hash, failed_payment_hash);
3523 _ => panic!("Unexpected event"),
3526 Event::PaymentFailed { payment_hash, .. } => {
3527 assert_eq!(payment_hash, failed_payment_hash);
3529 _ => panic!("Unexpected event"),
3532 Event::ChannelClosed { reason: ClosureReason::ProcessingError { ref err }, .. } => {
3533 assert_eq!(err, "Remote side tried to send a 0-msat HTLC");
3535 _ => panic!("Unexpected event {:?}", events[1]),
3537 check_closed_broadcast!(nodes[0], true);
3538 check_added_monitors!(nodes[0], 1);
3542 fn test_htlc_ignore_latest_remote_commitment() {
3543 // Test that HTLC transactions spending the latest remote commitment transaction are simply
3544 // ignored if we cannot claim them. This originally tickled an invalid unwrap().
3545 let chanmon_cfgs = create_chanmon_cfgs(2);
3546 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3547 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3548 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3549 if *nodes[1].connect_style.borrow() == ConnectStyle::FullBlockViaListen {
3550 // We rely on the ability to connect a block redundantly, which isn't allowed via
3551 // `chain::Listen`, so we never run the test if we randomly get assigned that
3555 create_announced_chan_between_nodes(&nodes, 0, 1);
3557 route_payment(&nodes[0], &[&nodes[1]], 10000000);
3558 nodes[0].node.force_close_broadcasting_latest_txn(&nodes[0].node.list_channels()[0].channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3559 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
3560 check_closed_broadcast!(nodes[0], true);
3561 check_added_monitors!(nodes[0], 1);
3562 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3564 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3565 assert_eq!(node_txn.len(), 3);
3566 assert_eq!(node_txn[0].txid(), node_txn[1].txid());
3568 let block = create_dummy_block(nodes[1].best_block_hash(), 42, vec![node_txn[0].clone(), node_txn[1].clone()]);
3569 connect_block(&nodes[1], &block);
3570 check_closed_broadcast!(nodes[1], true);
3571 check_added_monitors!(nodes[1], 1);
3572 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
3574 // Duplicate the connect_block call since this may happen due to other listeners
3575 // registering new transactions
3576 connect_block(&nodes[1], &block);
3580 fn test_force_close_fail_back() {
3581 // Check which HTLCs are failed-backwards on channel force-closure
3582 let chanmon_cfgs = create_chanmon_cfgs(3);
3583 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3584 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3585 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3586 create_announced_chan_between_nodes(&nodes, 0, 1);
3587 create_announced_chan_between_nodes(&nodes, 1, 2);
3589 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1000000);
3591 let mut payment_event = {
3592 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
3593 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
3594 check_added_monitors!(nodes[0], 1);
3596 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3597 assert_eq!(events.len(), 1);
3598 SendEvent::from_event(events.remove(0))
3601 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3602 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
3604 expect_pending_htlcs_forwardable!(nodes[1]);
3606 let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
3607 assert_eq!(events_2.len(), 1);
3608 payment_event = SendEvent::from_event(events_2.remove(0));
3609 assert_eq!(payment_event.msgs.len(), 1);
3611 check_added_monitors!(nodes[1], 1);
3612 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
3613 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &payment_event.commitment_msg);
3614 check_added_monitors!(nodes[2], 1);
3615 let (_, _) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
3617 // nodes[2] now has the latest commitment transaction, but hasn't revoked its previous
3618 // state or updated nodes[1]' state. Now force-close and broadcast that commitment/HTLC
3619 // transaction and ensure nodes[1] doesn't fail-backwards (this was originally a bug!).
3621 nodes[2].node.force_close_broadcasting_latest_txn(&payment_event.commitment_msg.channel_id, &nodes[1].node.get_our_node_id()).unwrap();
3622 check_closed_broadcast!(nodes[2], true);
3623 check_added_monitors!(nodes[2], 1);
3624 check_closed_event!(nodes[2], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
3626 let mut node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3627 // Note that we don't bother broadcasting the HTLC-Success transaction here as we don't
3628 // have a use for it unless nodes[2] learns the preimage somehow, the funds will go
3629 // back to nodes[1] upon timeout otherwise.
3630 assert_eq!(node_txn.len(), 1);
3634 mine_transaction(&nodes[1], &tx);
3636 // Note no UpdateHTLCs event here from nodes[1] to nodes[0]!
3637 check_closed_broadcast!(nodes[1], true);
3638 check_added_monitors!(nodes[1], 1);
3639 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
3641 // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success..
3643 get_monitor!(nodes[2], payment_event.commitment_msg.channel_id)
3644 .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);
3646 mine_transaction(&nodes[2], &tx);
3647 let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap();
3648 assert_eq!(node_txn.len(), 1);
3649 assert_eq!(node_txn[0].input.len(), 1);
3650 assert_eq!(node_txn[0].input[0].previous_output.txid, tx.txid());
3651 assert_eq!(node_txn[0].lock_time, LockTime::ZERO); // Must be an HTLC-Success
3652 assert_eq!(node_txn[0].input[0].witness.len(), 5); // Must be an HTLC-Success
3654 check_spends!(node_txn[0], tx);
3658 fn test_dup_events_on_peer_disconnect() {
3659 // Test that if we receive a duplicative update_fulfill_htlc message after a reconnect we do
3660 // not generate a corresponding duplicative PaymentSent event. This did not use to be the case
3661 // as we used to generate the event immediately upon receipt of the payment preimage in the
3662 // update_fulfill_htlc message.
3664 let chanmon_cfgs = create_chanmon_cfgs(2);
3665 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3666 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3667 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3668 create_announced_chan_between_nodes(&nodes, 0, 1);
3670 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
3672 nodes[1].node.claim_funds(payment_preimage);
3673 expect_payment_claimed!(nodes[1], payment_hash, 1_000_000);
3674 check_added_monitors!(nodes[1], 1);
3675 let claim_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3676 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &claim_msgs.update_fulfill_htlcs[0]);
3677 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
3679 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3680 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3682 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3683 reconnect_args.pending_htlc_claims.0 = 1;
3684 reconnect_nodes(reconnect_args);
3685 expect_payment_path_successful!(nodes[0]);
3689 fn test_peer_disconnected_before_funding_broadcasted() {
3690 // Test that channels are closed with `ClosureReason::DisconnectedPeer` if the peer disconnects
3691 // before the funding transaction has been broadcasted.
3692 let chanmon_cfgs = create_chanmon_cfgs(2);
3693 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3694 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3695 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3697 // Open a channel between `nodes[0]` and `nodes[1]`, for which the funding transaction is never
3698 // broadcasted, even though it's created by `nodes[0]`.
3699 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, None).unwrap();
3700 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
3701 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
3702 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
3703 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
3705 let (temporary_channel_id, tx, _funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
3706 assert_eq!(temporary_channel_id, expected_temporary_channel_id);
3708 assert!(nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
3710 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
3711 assert_eq!(funding_created_msg.temporary_channel_id, expected_temporary_channel_id);
3713 // Even though the funding transaction is created by `nodes[0]`, the `FundingCreated` msg is
3714 // never sent to `nodes[1]`, and therefore the tx is never signed by either party nor
3717 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
3720 // Ensure that the channel is closed with `ClosureReason::DisconnectedPeer` when the peers are
3721 // disconnected before the funding transaction was broadcasted.
3722 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3723 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3725 check_closed_event!(&nodes[0], 2, ClosureReason::DisconnectedPeer, true
3726 , [nodes[1].node.get_our_node_id()], 1000000);
3727 check_closed_event!(&nodes[1], 1, ClosureReason::DisconnectedPeer, false
3728 , [nodes[0].node.get_our_node_id()], 1000000);
3732 fn test_simple_peer_disconnect() {
3733 // Test that we can reconnect when there are no lost messages
3734 let chanmon_cfgs = create_chanmon_cfgs(3);
3735 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3736 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3737 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3738 create_announced_chan_between_nodes(&nodes, 0, 1);
3739 create_announced_chan_between_nodes(&nodes, 1, 2);
3741 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3742 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3743 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3744 reconnect_args.send_channel_ready = (true, true);
3745 reconnect_nodes(reconnect_args);
3747 let payment_preimage_1 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3748 let payment_hash_2 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3749 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_2);
3750 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_1);
3752 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3753 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3754 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3756 let (payment_preimage_3, payment_hash_3, ..) = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000);
3757 let payment_preimage_4 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).0;
3758 let payment_hash_5 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3759 let payment_hash_6 = route_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 1000000).1;
3761 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3762 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3764 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_preimage_3);
3765 fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], true, payment_hash_5);
3767 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3768 reconnect_args.pending_cell_htlc_fails.0 = 1;
3769 reconnect_args.pending_cell_htlc_claims.0 = 1;
3770 reconnect_nodes(reconnect_args);
3772 let events = nodes[0].node.get_and_clear_pending_events();
3773 assert_eq!(events.len(), 4);
3775 Event::PaymentSent { payment_preimage, payment_hash, .. } => {
3776 assert_eq!(payment_preimage, payment_preimage_3);
3777 assert_eq!(payment_hash, payment_hash_3);
3779 _ => panic!("Unexpected event"),
3782 Event::PaymentPathSuccessful { .. } => {},
3783 _ => panic!("Unexpected event"),
3786 Event::PaymentPathFailed { payment_hash, payment_failed_permanently, .. } => {
3787 assert_eq!(payment_hash, payment_hash_5);
3788 assert!(payment_failed_permanently);
3790 _ => panic!("Unexpected event"),
3793 Event::PaymentFailed { payment_hash, .. } => {
3794 assert_eq!(payment_hash, payment_hash_5);
3796 _ => panic!("Unexpected event"),
3799 check_added_monitors(&nodes[0], 1);
3801 claim_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_preimage_4);
3802 fail_payment(&nodes[0], &vec!(&nodes[1], &nodes[2]), payment_hash_6);
3805 fn do_test_drop_messages_peer_disconnect(messages_delivered: u8, simulate_broken_lnd: bool) {
3806 // Test that we can reconnect when in-flight HTLC updates get dropped
3807 let chanmon_cfgs = create_chanmon_cfgs(2);
3808 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
3809 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
3810 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
3812 let mut as_channel_ready = None;
3813 let channel_id = if messages_delivered == 0 {
3814 let (channel_ready, chan_id, _) = create_chan_between_nodes_with_value_a(&nodes[0], &nodes[1], 100000, 10001);
3815 as_channel_ready = Some(channel_ready);
3816 // nodes[1] doesn't receive the channel_ready message (it'll be re-sent on reconnect)
3817 // Note that we store it so that if we're running with `simulate_broken_lnd` we can deliver
3818 // it before the channel_reestablish message.
3821 create_announced_chan_between_nodes(&nodes, 0, 1).2
3824 let (route, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
3826 let payment_event = {
3827 nodes[0].node.send_payment_with_route(&route, payment_hash_1,
3828 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
3829 check_added_monitors!(nodes[0], 1);
3831 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
3832 assert_eq!(events.len(), 1);
3833 SendEvent::from_event(events.remove(0))
3835 assert_eq!(nodes[1].node.get_our_node_id(), payment_event.node_id);
3837 if messages_delivered < 2 {
3838 // Drop the payment_event messages, and let them get re-generated in reconnect_nodes!
3840 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
3841 if messages_delivered >= 3 {
3842 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &payment_event.commitment_msg);
3843 check_added_monitors!(nodes[1], 1);
3844 let (bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
3846 if messages_delivered >= 4 {
3847 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
3848 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
3849 check_added_monitors!(nodes[0], 1);
3851 if messages_delivered >= 5 {
3852 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed);
3853 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3854 // No commitment_signed so get_event_msg's assert(len == 1) passes
3855 check_added_monitors!(nodes[0], 1);
3857 if messages_delivered >= 6 {
3858 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3859 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3860 check_added_monitors!(nodes[1], 1);
3867 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3868 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3869 if messages_delivered < 3 {
3870 if simulate_broken_lnd {
3871 // lnd has a long-standing bug where they send a channel_ready prior to a
3872 // channel_reestablish if you reconnect prior to channel_ready time.
3874 // Here we simulate that behavior, delivering a channel_ready immediately on
3875 // reconnect. Note that we don't bother skipping the now-duplicate channel_ready sent
3876 // in `reconnect_nodes` but we currently don't fail based on that.
3878 // See-also <https://github.com/lightningnetwork/lnd/issues/4006>
3879 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready.as_ref().unwrap().0);
3881 // Even if the channel_ready messages get exchanged, as long as nothing further was
3882 // received on either side, both sides will need to resend them.
3883 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3884 reconnect_args.send_channel_ready = (true, true);
3885 reconnect_args.pending_htlc_adds.1 = 1;
3886 reconnect_nodes(reconnect_args);
3887 } else if messages_delivered == 3 {
3888 // nodes[0] still wants its RAA + commitment_signed
3889 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3890 reconnect_args.pending_responding_commitment_signed.0 = true;
3891 reconnect_args.pending_raa.0 = true;
3892 reconnect_nodes(reconnect_args);
3893 } else if messages_delivered == 4 {
3894 // nodes[0] still wants its commitment_signed
3895 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3896 reconnect_args.pending_responding_commitment_signed.0 = true;
3897 reconnect_nodes(reconnect_args);
3898 } else if messages_delivered == 5 {
3899 // nodes[1] still wants its final RAA
3900 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
3901 reconnect_args.pending_raa.1 = true;
3902 reconnect_nodes(reconnect_args);
3903 } else if messages_delivered == 6 {
3904 // Everything was delivered...
3905 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3908 let events_1 = nodes[1].node.get_and_clear_pending_events();
3909 if messages_delivered == 0 {
3910 assert_eq!(events_1.len(), 2);
3912 Event::ChannelReady { .. } => { },
3913 _ => panic!("Unexpected event"),
3916 Event::PendingHTLCsForwardable { .. } => { },
3917 _ => panic!("Unexpected event"),
3920 assert_eq!(events_1.len(), 1);
3922 Event::PendingHTLCsForwardable { .. } => { },
3923 _ => panic!("Unexpected event"),
3927 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3928 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
3929 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
3931 nodes[1].node.process_pending_htlc_forwards();
3933 let events_2 = nodes[1].node.get_and_clear_pending_events();
3934 assert_eq!(events_2.len(), 1);
3936 Event::PaymentClaimable { ref payment_hash, ref purpose, amount_msat, receiver_node_id, via_channel_id, .. } => {
3937 assert_eq!(payment_hash_1, *payment_hash);
3938 assert_eq!(amount_msat, 1_000_000);
3939 assert_eq!(receiver_node_id.unwrap(), nodes[1].node.get_our_node_id());
3940 assert_eq!(via_channel_id, Some(channel_id));
3942 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
3943 assert!(payment_preimage.is_none());
3944 assert_eq!(payment_secret_1, *payment_secret);
3946 _ => panic!("expected PaymentPurpose::InvoicePayment")
3949 _ => panic!("Unexpected event"),
3952 nodes[1].node.claim_funds(payment_preimage_1);
3953 check_added_monitors!(nodes[1], 1);
3954 expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
3956 let events_3 = nodes[1].node.get_and_clear_pending_msg_events();
3957 assert_eq!(events_3.len(), 1);
3958 let (update_fulfill_htlc, commitment_signed) = match events_3[0] {
3959 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
3960 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
3961 assert!(updates.update_add_htlcs.is_empty());
3962 assert!(updates.update_fail_htlcs.is_empty());
3963 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
3964 assert!(updates.update_fail_malformed_htlcs.is_empty());
3965 assert!(updates.update_fee.is_none());
3966 (updates.update_fulfill_htlcs[0].clone(), updates.commitment_signed.clone())
3968 _ => panic!("Unexpected event"),
3971 if messages_delivered >= 1 {
3972 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlc);
3974 let events_4 = nodes[0].node.get_and_clear_pending_events();
3975 assert_eq!(events_4.len(), 1);
3977 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
3978 assert_eq!(payment_preimage_1, *payment_preimage);
3979 assert_eq!(payment_hash_1, *payment_hash);
3981 _ => panic!("Unexpected event"),
3984 if messages_delivered >= 2 {
3985 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
3986 check_added_monitors!(nodes[0], 1);
3987 let (as_revoke_and_ack, as_commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
3989 if messages_delivered >= 3 {
3990 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
3991 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3992 check_added_monitors!(nodes[1], 1);
3994 if messages_delivered >= 4 {
3995 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed);
3996 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
3997 // No commitment_signed so get_event_msg's assert(len == 1) passes
3998 check_added_monitors!(nodes[1], 1);
4000 if messages_delivered >= 5 {
4001 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
4002 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4003 check_added_monitors!(nodes[0], 1);
4010 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4011 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4012 if messages_delivered < 2 {
4013 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4014 reconnect_args.pending_htlc_claims.0 = 1;
4015 reconnect_nodes(reconnect_args);
4016 if messages_delivered < 1 {
4017 expect_payment_sent!(nodes[0], payment_preimage_1);
4019 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4021 } else if messages_delivered == 2 {
4022 // nodes[0] still wants its RAA + commitment_signed
4023 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4024 reconnect_args.pending_responding_commitment_signed.1 = true;
4025 reconnect_args.pending_raa.1 = true;
4026 reconnect_nodes(reconnect_args);
4027 } else if messages_delivered == 3 {
4028 // nodes[0] still wants its commitment_signed
4029 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4030 reconnect_args.pending_responding_commitment_signed.1 = true;
4031 reconnect_nodes(reconnect_args);
4032 } else if messages_delivered == 4 {
4033 // nodes[1] still wants its final RAA
4034 let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
4035 reconnect_args.pending_raa.0 = true;
4036 reconnect_nodes(reconnect_args);
4037 } else if messages_delivered == 5 {
4038 // Everything was delivered...
4039 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4042 if messages_delivered == 1 || messages_delivered == 2 {
4043 expect_payment_path_successful!(nodes[0]);
4045 if messages_delivered <= 5 {
4046 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4047 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4049 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
4051 if messages_delivered > 2 {
4052 expect_payment_path_successful!(nodes[0]);
4055 // Channel should still work fine...
4056 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4057 let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0;
4058 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4062 fn test_drop_messages_peer_disconnect_a() {
4063 do_test_drop_messages_peer_disconnect(0, true);
4064 do_test_drop_messages_peer_disconnect(0, false);
4065 do_test_drop_messages_peer_disconnect(1, false);
4066 do_test_drop_messages_peer_disconnect(2, false);
4070 fn test_drop_messages_peer_disconnect_b() {
4071 do_test_drop_messages_peer_disconnect(3, false);
4072 do_test_drop_messages_peer_disconnect(4, false);
4073 do_test_drop_messages_peer_disconnect(5, false);
4074 do_test_drop_messages_peer_disconnect(6, false);
4078 fn test_channel_ready_without_best_block_updated() {
4079 // Previously, if we were offline when a funding transaction was locked in, and then we came
4080 // back online, calling best_block_updated once followed by transactions_confirmed, we'd not
4081 // generate a channel_ready until a later best_block_updated. This tests that we generate the
4082 // channel_ready immediately instead.
4083 let chanmon_cfgs = create_chanmon_cfgs(2);
4084 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4085 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4086 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4087 *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
4089 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4091 let conf_height = nodes[0].best_block_info().1 + 1;
4092 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4093 let block_txn = [funding_tx];
4094 let conf_txn: Vec<_> = block_txn.iter().enumerate().collect();
4095 let conf_block_header = nodes[0].get_block_header(conf_height);
4096 nodes[0].node.transactions_confirmed(&conf_block_header, &conf_txn[..], conf_height);
4098 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4099 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4100 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4104 fn test_channel_monitor_skipping_block_when_channel_manager_is_leading() {
4105 let chanmon_cfgs = create_chanmon_cfgs(2);
4106 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4107 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4108 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4110 // Let channel_manager get ahead of chain_monitor by 1 block.
4111 // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4112 // in case where client calls block_connect on channel_manager first and then on chain_monitor.
4113 let height_1 = nodes[0].best_block_info().1 + 1;
4114 let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4116 nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4117 nodes[0].node.block_connected(&block_1, height_1);
4119 // Create channel, and it gets added to chain_monitor in funding_created.
4120 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4122 // Now, newly added channel_monitor in chain_monitor hasn't processed block_1,
4123 // but it's best_block is block_1, since that was populated by channel_manager, and channel_manager
4124 // was running ahead of chain_monitor at the time of funding_created.
4125 // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4126 // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4127 confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4128 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4130 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4131 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4132 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4136 fn test_channel_monitor_skipping_block_when_channel_manager_is_lagging() {
4137 let chanmon_cfgs = create_chanmon_cfgs(2);
4138 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4139 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4140 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4142 // Let chain_monitor get ahead of channel_manager by 1 block.
4143 // This is to emulate race-condition where newly added channel_monitor skips processing 1 block,
4144 // in case where client calls block_connect on chain_monitor first and then on channel_manager.
4145 let height_1 = nodes[0].best_block_info().1 + 1;
4146 let mut block_1 = create_dummy_block(nodes[0].best_block_hash(), height_1, Vec::new());
4148 nodes[0].blocks.lock().unwrap().push((block_1.clone(), height_1));
4149 nodes[0].chain_monitor.chain_monitor.block_connected(&block_1, height_1);
4151 // Create channel, and it gets added to chain_monitor in funding_created.
4152 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
4154 // channel_manager can't really skip block_1, it should get it eventually.
4155 nodes[0].node.block_connected(&block_1, height_1);
4157 // Now, newly added channel_monitor in chain_monitor hasn't processed block_1, it's best_block is
4158 // the block before block_1, since that was populated by channel_manager, and channel_manager was
4159 // running behind at the time of funding_created.
4160 // Later on, subsequent blocks are connected to both channel_manager and chain_monitor.
4161 // Hence, this channel's channel_monitor skipped block_1, directly tries to process subsequent blocks.
4162 confirm_transaction_at(&nodes[0], &funding_tx, nodes[0].best_block_info().1 + 1);
4163 connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH);
4165 // Ensure nodes[0] generates a channel_ready after the transactions_confirmed
4166 let as_channel_ready = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
4167 nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready);
4171 fn test_drop_messages_peer_disconnect_dual_htlc() {
4172 // Test that we can handle reconnecting when both sides of a channel have pending
4173 // commitment_updates when we disconnect.
4174 let chanmon_cfgs = create_chanmon_cfgs(2);
4175 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4176 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4177 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4178 create_announced_chan_between_nodes(&nodes, 0, 1);
4180 let (payment_preimage_1, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
4182 // Now try to send a second payment which will fail to send
4183 let (route, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
4184 nodes[0].node.send_payment_with_route(&route, payment_hash_2,
4185 RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
4186 check_added_monitors!(nodes[0], 1);
4188 let events_1 = nodes[0].node.get_and_clear_pending_msg_events();
4189 assert_eq!(events_1.len(), 1);
4191 MessageSendEvent::UpdateHTLCs { .. } => {},
4192 _ => panic!("Unexpected event"),
4195 nodes[1].node.claim_funds(payment_preimage_1);
4196 expect_payment_claimed!(nodes[1], payment_hash_1, 1_000_000);
4197 check_added_monitors!(nodes[1], 1);
4199 let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
4200 assert_eq!(events_2.len(), 1);
4202 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 } } => {
4203 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
4204 assert!(update_add_htlcs.is_empty());
4205 assert_eq!(update_fulfill_htlcs.len(), 1);
4206 assert!(update_fail_htlcs.is_empty());
4207 assert!(update_fail_malformed_htlcs.is_empty());
4208 assert!(update_fee.is_none());
4210 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_htlcs[0]);
4211 let events_3 = nodes[0].node.get_and_clear_pending_events();
4212 assert_eq!(events_3.len(), 1);
4214 Event::PaymentSent { ref payment_preimage, ref payment_hash, .. } => {
4215 assert_eq!(*payment_preimage, payment_preimage_1);
4216 assert_eq!(*payment_hash, payment_hash_1);
4218 _ => panic!("Unexpected event"),
4221 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
4222 let _ = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4223 // No commitment_signed so get_event_msg's assert(len == 1) passes
4224 check_added_monitors!(nodes[0], 1);
4226 _ => panic!("Unexpected event"),
4229 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
4230 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
4232 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
4233 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
4235 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
4236 assert_eq!(reestablish_1.len(), 1);
4237 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
4238 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
4240 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
4241 assert_eq!(reestablish_2.len(), 1);
4243 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
4244 let as_resp = handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
4245 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
4246 let bs_resp = handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
4248 assert!(as_resp.0.is_none());
4249 assert!(bs_resp.0.is_none());
4251 assert!(bs_resp.1.is_none());
4252 assert!(bs_resp.2.is_none());
4254 assert!(as_resp.3 == RAACommitmentOrder::CommitmentFirst);
4256 assert_eq!(as_resp.2.as_ref().unwrap().update_add_htlcs.len(), 1);
4257 assert!(as_resp.2.as_ref().unwrap().update_fulfill_htlcs.is_empty());
4258 assert!(as_resp.2.as_ref().unwrap().update_fail_htlcs.is_empty());
4259 assert!(as_resp.2.as_ref().unwrap().update_fail_malformed_htlcs.is_empty());
4260 assert!(as_resp.2.as_ref().unwrap().update_fee.is_none());
4261 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().update_add_htlcs[0]);
4262 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_resp.2.as_ref().unwrap().commitment_signed);
4263 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4264 // No commitment_signed so get_event_msg's assert(len == 1) passes
4265 check_added_monitors!(nodes[1], 1);
4267 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), as_resp.1.as_ref().unwrap());
4268 let bs_second_commitment_signed = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4269 assert!(bs_second_commitment_signed.update_add_htlcs.is_empty());
4270 assert!(bs_second_commitment_signed.update_fulfill_htlcs.is_empty());
4271 assert!(bs_second_commitment_signed.update_fail_htlcs.is_empty());
4272 assert!(bs_second_commitment_signed.update_fail_malformed_htlcs.is_empty());
4273 assert!(bs_second_commitment_signed.update_fee.is_none());
4274 check_added_monitors!(nodes[1], 1);
4276 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
4277 let as_commitment_signed = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
4278 assert!(as_commitment_signed.update_add_htlcs.is_empty());
4279 assert!(as_commitment_signed.update_fulfill_htlcs.is_empty());
4280 assert!(as_commitment_signed.update_fail_htlcs.is_empty());
4281 assert!(as_commitment_signed.update_fail_malformed_htlcs.is_empty());
4282 assert!(as_commitment_signed.update_fee.is_none());
4283 check_added_monitors!(nodes[0], 1);
4285 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_second_commitment_signed.commitment_signed);
4286 let as_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
4287 // No commitment_signed so get_event_msg's assert(len == 1) passes
4288 check_added_monitors!(nodes[0], 1);
4290 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_commitment_signed.commitment_signed);
4291 let bs_second_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
4292 // No commitment_signed so get_event_msg's assert(len == 1) passes
4293 check_added_monitors!(nodes[1], 1);
4295 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_revoke_and_ack);
4296 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4297 check_added_monitors!(nodes[1], 1);
4299 expect_pending_htlcs_forwardable!(nodes[1]);
4301 let events_5 = nodes[1].node.get_and_clear_pending_events();
4302 assert_eq!(events_5.len(), 1);
4304 Event::PaymentClaimable { ref payment_hash, ref purpose, .. } => {
4305 assert_eq!(payment_hash_2, *payment_hash);
4307 PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
4308 assert!(payment_preimage.is_none());
4309 assert_eq!(payment_secret_2, *payment_secret);
4311 _ => panic!("expected PaymentPurpose::InvoicePayment")
4314 _ => panic!("Unexpected event"),
4317 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_revoke_and_ack);
4318 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
4319 check_added_monitors!(nodes[0], 1);
4321 expect_payment_path_successful!(nodes[0]);
4322 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2);
4325 fn do_test_htlc_timeout(send_partial_mpp: bool) {
4326 // If the user fails to claim/fail an HTLC within the HTLC CLTV timeout we fail it for them
4327 // to avoid our counterparty failing the channel.
4328 let chanmon_cfgs = create_chanmon_cfgs(2);
4329 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4330 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4331 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4333 create_announced_chan_between_nodes(&nodes, 0, 1);
4335 let our_payment_hash = if send_partial_mpp {
4336 let (route, our_payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100000);
4337 // Use the utility function send_payment_along_path to send the payment with MPP data which
4338 // indicates there are more HTLCs coming.
4339 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.
4340 let payment_id = PaymentId([42; 32]);
4341 let session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
4342 RecipientOnionFields::secret_only(payment_secret), payment_id, &route).unwrap();
4343 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
4344 RecipientOnionFields::secret_only(payment_secret), 200_000, cur_height, payment_id,
4345 &None, session_privs[0]).unwrap();
4346 check_added_monitors!(nodes[0], 1);
4347 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4348 assert_eq!(events.len(), 1);
4349 // Now do the relevant commitment_signed/RAA dances along the path, noting that the final
4350 // hop should *not* yet generate any PaymentClaimable event(s).
4351 pass_along_path(&nodes[0], &[&nodes[1]], 100000, our_payment_hash, Some(payment_secret), events.drain(..).next().unwrap(), false, None);
4354 route_payment(&nodes[0], &[&nodes[1]], 100000).1
4357 let mut block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
4358 connect_block(&nodes[0], &block);
4359 connect_block(&nodes[1], &block);
4360 let block_count = TEST_FINAL_CLTV + CHAN_CONFIRM_DEPTH + 2 - CLTV_CLAIM_BUFFER - LATENCY_GRACE_PERIOD_BLOCKS;
4361 for _ in CHAN_CONFIRM_DEPTH + 2..block_count {
4362 block.header.prev_blockhash = block.block_hash();
4363 connect_block(&nodes[0], &block);
4364 connect_block(&nodes[1], &block);
4367 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
4369 check_added_monitors!(nodes[1], 1);
4370 let htlc_timeout_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4371 assert!(htlc_timeout_updates.update_add_htlcs.is_empty());
4372 assert_eq!(htlc_timeout_updates.update_fail_htlcs.len(), 1);
4373 assert!(htlc_timeout_updates.update_fail_malformed_htlcs.is_empty());
4374 assert!(htlc_timeout_updates.update_fee.is_none());
4376 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_timeout_updates.update_fail_htlcs[0]);
4377 commitment_signed_dance!(nodes[0], nodes[1], htlc_timeout_updates.commitment_signed, false);
4378 // 100_000 msat as u64, followed by the height at which we failed back above
4379 let mut expected_failure_data = (100_000 as u64).to_be_bytes().to_vec();
4380 expected_failure_data.extend_from_slice(&(block_count - 1).to_be_bytes());
4381 expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000 | 15, &expected_failure_data[..]);
4385 fn test_htlc_timeout() {
4386 do_test_htlc_timeout(true);
4387 do_test_htlc_timeout(false);
4390 fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) {
4391 // Tests that HTLCs in the holding cell are timed out after the requisite number of blocks.
4392 let chanmon_cfgs = create_chanmon_cfgs(3);
4393 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4394 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4395 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4396 create_announced_chan_between_nodes(&nodes, 0, 1);
4397 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4399 // Make sure all nodes are at the same starting height
4400 connect_blocks(&nodes[0], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[0].best_block_info().1);
4401 connect_blocks(&nodes[1], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[1].best_block_info().1);
4402 connect_blocks(&nodes[2], 2*CHAN_CONFIRM_DEPTH + 1 - nodes[2].best_block_info().1);
4404 // Route a first payment to get the 1 -> 2 channel in awaiting_raa...
4405 let (route, first_payment_hash, _, first_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
4406 nodes[1].node.send_payment_with_route(&route, first_payment_hash,
4407 RecipientOnionFields::secret_only(first_payment_secret), PaymentId(first_payment_hash.0)).unwrap();
4408 assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);
4409 check_added_monitors!(nodes[1], 1);
4411 // Now attempt to route a second payment, which should be placed in the holding cell
4412 let sending_node = if forwarded_htlc { &nodes[0] } else { &nodes[1] };
4413 let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(sending_node, nodes[2], 100000);
4414 sending_node.node.send_payment_with_route(&route, second_payment_hash,
4415 RecipientOnionFields::secret_only(second_payment_secret), PaymentId(second_payment_hash.0)).unwrap();
4417 check_added_monitors!(nodes[0], 1);
4418 let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
4419 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
4420 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4421 expect_pending_htlcs_forwardable!(nodes[1]);
4423 check_added_monitors!(nodes[1], 0);
4425 connect_blocks(&nodes[1], TEST_FINAL_CLTV - LATENCY_GRACE_PERIOD_BLOCKS);
4426 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
4427 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
4428 connect_blocks(&nodes[1], 1);
4431 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 }]);
4432 check_added_monitors!(nodes[1], 1);
4433 let fail_commit = nodes[1].node.get_and_clear_pending_msg_events();
4434 assert_eq!(fail_commit.len(), 1);
4435 match fail_commit[0] {
4436 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fail_htlcs, ref commitment_signed, .. }, .. } => {
4437 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
4438 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, true, true);
4440 _ => unreachable!(),
4442 expect_payment_failed_with_update!(nodes[0], second_payment_hash, false, chan_2.0.contents.short_channel_id, false);
4444 expect_payment_failed!(nodes[1], second_payment_hash, false);
4449 fn test_holding_cell_htlc_add_timeouts() {
4450 do_test_holding_cell_htlc_add_timeouts(false);
4451 do_test_holding_cell_htlc_add_timeouts(true);
4454 macro_rules! check_spendable_outputs {
4455 ($node: expr, $keysinterface: expr) => {
4457 let mut events = $node.chain_monitor.chain_monitor.get_and_clear_pending_events();
4458 let mut txn = Vec::new();
4459 let mut all_outputs = Vec::new();
4460 let secp_ctx = Secp256k1::new();
4461 for event in events.drain(..) {
4463 Event::SpendableOutputs { mut outputs, channel_id: _ } => {
4464 for outp in outputs.drain(..) {
4465 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());
4466 all_outputs.push(outp);
4469 _ => panic!("Unexpected event"),
4472 if all_outputs.len() > 1 {
4473 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) {
4483 fn test_claim_sizeable_push_msat() {
4484 // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
4485 let chanmon_cfgs = create_chanmon_cfgs(2);
4486 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4487 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4488 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4490 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4491 nodes[1].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[0].node.get_our_node_id()).unwrap();
4492 check_closed_broadcast!(nodes[1], true);
4493 check_added_monitors!(nodes[1], 1);
4494 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
4495 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4496 assert_eq!(node_txn.len(), 1);
4497 check_spends!(node_txn[0], chan.3);
4498 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
4500 mine_transaction(&nodes[1], &node_txn[0]);
4501 connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
4503 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4504 assert_eq!(spend_txn.len(), 1);
4505 assert_eq!(spend_txn[0].input.len(), 1);
4506 check_spends!(spend_txn[0], node_txn[0]);
4507 assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
4511 fn test_claim_on_remote_sizeable_push_msat() {
4512 // Same test as previous, just test on remote commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4513 // to_remote output is encumbered by a P2WPKH
4514 let chanmon_cfgs = create_chanmon_cfgs(2);
4515 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4516 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4517 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4519 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 98_000_000);
4520 nodes[0].node.force_close_broadcasting_latest_txn(&chan.2, &nodes[1].node.get_our_node_id()).unwrap();
4521 check_closed_broadcast!(nodes[0], true);
4522 check_added_monitors!(nodes[0], 1);
4523 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
4525 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4526 assert_eq!(node_txn.len(), 1);
4527 check_spends!(node_txn[0], chan.3);
4528 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
4530 mine_transaction(&nodes[1], &node_txn[0]);
4531 check_closed_broadcast!(nodes[1], true);
4532 check_added_monitors!(nodes[1], 1);
4533 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4534 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4536 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4537 assert_eq!(spend_txn.len(), 1);
4538 check_spends!(spend_txn[0], node_txn[0]);
4542 fn test_claim_on_remote_revoked_sizeable_push_msat() {
4543 // Same test as previous, just test on remote revoked commitment tx, as per_commitment_point registration changes following you're funder/fundee and
4544 // to_remote output is encumbered by a P2WPKH
4546 let chanmon_cfgs = create_chanmon_cfgs(2);
4547 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4548 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4549 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4551 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 59000000);
4552 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4553 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan.2);
4554 assert_eq!(revoked_local_txn[0].input.len(), 1);
4555 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
4557 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4558 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4559 check_closed_broadcast!(nodes[1], true);
4560 check_added_monitors!(nodes[1], 1);
4561 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4563 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4564 mine_transaction(&nodes[1], &node_txn[0]);
4565 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4567 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4568 assert_eq!(spend_txn.len(), 3);
4569 check_spends!(spend_txn[0], revoked_local_txn[0]); // to_remote output on revoked remote commitment_tx
4570 check_spends!(spend_txn[1], node_txn[0]);
4571 check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[0]); // Both outputs
4575 fn test_static_spendable_outputs_preimage_tx() {
4576 let chanmon_cfgs = create_chanmon_cfgs(2);
4577 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4578 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4579 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4581 // Create some initial channels
4582 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4584 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
4586 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4587 assert_eq!(commitment_tx[0].input.len(), 1);
4588 assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4590 // Settle A's commitment tx on B's chain
4591 nodes[1].node.claim_funds(payment_preimage);
4592 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
4593 check_added_monitors!(nodes[1], 1);
4594 mine_transaction(&nodes[1], &commitment_tx[0]);
4595 check_added_monitors!(nodes[1], 1);
4596 let events = nodes[1].node.get_and_clear_pending_msg_events();
4598 MessageSendEvent::UpdateHTLCs { .. } => {},
4599 _ => panic!("Unexpected event"),
4602 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4603 _ => panic!("Unexepected event"),
4606 // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
4607 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: preimage tx
4608 assert_eq!(node_txn.len(), 1);
4609 check_spends!(node_txn[0], commitment_tx[0]);
4610 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4612 mine_transaction(&nodes[1], &node_txn[0]);
4613 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4614 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4616 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4617 assert_eq!(spend_txn.len(), 1);
4618 check_spends!(spend_txn[0], node_txn[0]);
4622 fn test_static_spendable_outputs_timeout_tx() {
4623 let chanmon_cfgs = create_chanmon_cfgs(2);
4624 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4625 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4626 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4628 // Create some initial channels
4629 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4631 // Rebalance the network a bit by relaying one payment through all the channels ...
4632 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
4634 let (_, our_payment_hash, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 3_000_000);
4636 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4637 assert_eq!(commitment_tx[0].input.len(), 1);
4638 assert_eq!(commitment_tx[0].input[0].previous_output.txid, chan_1.3.txid());
4640 // Settle A's commitment tx on B' chain
4641 mine_transaction(&nodes[1], &commitment_tx[0]);
4642 check_added_monitors!(nodes[1], 1);
4643 let events = nodes[1].node.get_and_clear_pending_msg_events();
4645 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4646 _ => panic!("Unexpected event"),
4648 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4650 // Check B's monitor was able to send back output descriptor event for timeout tx on A's commitment tx
4651 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4652 assert_eq!(node_txn.len(), 1); // ChannelMonitor: timeout tx
4653 check_spends!(node_txn[0], commitment_tx[0].clone());
4654 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4656 mine_transaction(&nodes[1], &node_txn[0]);
4657 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4658 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4659 expect_payment_failed!(nodes[1], our_payment_hash, false);
4661 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4662 assert_eq!(spend_txn.len(), 3); // SpendableOutput: remote_commitment_tx.to_remote, timeout_tx.output
4663 check_spends!(spend_txn[0], commitment_tx[0]);
4664 check_spends!(spend_txn[1], node_txn[0]);
4665 check_spends!(spend_txn[2], node_txn[0], commitment_tx[0]); // All outputs
4669 fn test_static_spendable_outputs_justice_tx_revoked_commitment_tx() {
4670 let chanmon_cfgs = create_chanmon_cfgs(2);
4671 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4672 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4673 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4675 // Create some initial channels
4676 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4678 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4679 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4680 assert_eq!(revoked_local_txn[0].input.len(), 1);
4681 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4683 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4685 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4686 check_closed_broadcast!(nodes[1], true);
4687 check_added_monitors!(nodes[1], 1);
4688 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4690 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4691 assert_eq!(node_txn.len(), 1);
4692 assert_eq!(node_txn[0].input.len(), 2);
4693 check_spends!(node_txn[0], revoked_local_txn[0]);
4695 mine_transaction(&nodes[1], &node_txn[0]);
4696 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4698 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4699 assert_eq!(spend_txn.len(), 1);
4700 check_spends!(spend_txn[0], node_txn[0]);
4704 fn test_static_spendable_outputs_justice_tx_revoked_htlc_timeout_tx() {
4705 let mut chanmon_cfgs = create_chanmon_cfgs(2);
4706 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
4707 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4708 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4709 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4711 // Create some initial channels
4712 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4714 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4715 let revoked_local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
4716 assert_eq!(revoked_local_txn[0].input.len(), 1);
4717 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4719 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4721 // A will generate HTLC-Timeout from revoked commitment tx
4722 mine_transaction(&nodes[0], &revoked_local_txn[0]);
4723 check_closed_broadcast!(nodes[0], true);
4724 check_added_monitors!(nodes[0], 1);
4725 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4726 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
4728 let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
4729 assert_eq!(revoked_htlc_txn.len(), 1);
4730 assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4731 assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4732 check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4733 assert_ne!(revoked_htlc_txn[0].lock_time, LockTime::ZERO); // HTLC-Timeout
4735 // B will generate justice tx from A's revoked commitment/HTLC tx
4736 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4737 check_closed_broadcast!(nodes[1], true);
4738 check_added_monitors!(nodes[1], 1);
4739 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4741 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4742 assert_eq!(node_txn.len(), 2); // ChannelMonitor: bogus justice tx, justice tx on revoked outputs
4743 // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4744 // including the one already spent by revoked_htlc_txn[1]. That's OK, we'll spend with valid
4745 // transactions next...
4746 assert_eq!(node_txn[0].input.len(), 3);
4747 check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4749 assert_eq!(node_txn[1].input.len(), 2);
4750 check_spends!(node_txn[1], revoked_local_txn[0], revoked_htlc_txn[0]);
4751 if node_txn[1].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4752 assert_ne!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4754 assert_eq!(node_txn[1].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4755 assert_ne!(node_txn[1].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4758 mine_transaction(&nodes[1], &node_txn[1]);
4759 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
4761 // Check B's ChannelMonitor was able to generate the right spendable output descriptor
4762 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
4763 assert_eq!(spend_txn.len(), 1);
4764 assert_eq!(spend_txn[0].input.len(), 1);
4765 check_spends!(spend_txn[0], node_txn[1]);
4769 fn test_static_spendable_outputs_justice_tx_revoked_htlc_success_tx() {
4770 let mut chanmon_cfgs = create_chanmon_cfgs(2);
4771 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
4772 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4773 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4774 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4776 // Create some initial channels
4777 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4779 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
4780 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
4781 assert_eq!(revoked_local_txn[0].input.len(), 1);
4782 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan_1.3.txid());
4784 // The to-be-revoked commitment tx should have one HTLC and one to_remote output
4785 assert_eq!(revoked_local_txn[0].output.len(), 2);
4787 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
4789 // B will generate HTLC-Success from revoked commitment tx
4790 mine_transaction(&nodes[1], &revoked_local_txn[0]);
4791 check_closed_broadcast!(nodes[1], true);
4792 check_added_monitors!(nodes[1], 1);
4793 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4794 let revoked_htlc_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4796 assert_eq!(revoked_htlc_txn.len(), 1);
4797 assert_eq!(revoked_htlc_txn[0].input.len(), 1);
4798 assert_eq!(revoked_htlc_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4799 check_spends!(revoked_htlc_txn[0], revoked_local_txn[0]);
4801 // Check that the unspent (of two) outputs on revoked_local_txn[0] is a P2WPKH:
4802 let unspent_local_txn_output = revoked_htlc_txn[0].input[0].previous_output.vout as usize ^ 1;
4803 assert_eq!(revoked_local_txn[0].output[unspent_local_txn_output].script_pubkey.len(), 2 + 20); // P2WPKH
4805 // A will generate justice tx from B's revoked commitment/HTLC tx
4806 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![revoked_local_txn[0].clone(), revoked_htlc_txn[0].clone()]));
4807 check_closed_broadcast!(nodes[0], true);
4808 check_added_monitors!(nodes[0], 1);
4809 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4811 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4812 assert_eq!(node_txn.len(), 2); // ChannelMonitor: justice tx on revoked commitment, justice tx on revoked HTLC-success
4814 // The first transaction generated is bogus - it spends both outputs of revoked_local_txn[0]
4815 // including the one already spent by revoked_htlc_txn[0]. That's OK, we'll spend with valid
4816 // transactions next...
4817 assert_eq!(node_txn[0].input.len(), 2);
4818 check_spends!(node_txn[0], revoked_local_txn[0], revoked_htlc_txn[0]);
4819 if node_txn[0].input[1].previous_output.txid == revoked_htlc_txn[0].txid() {
4820 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4822 assert_eq!(node_txn[0].input[0].previous_output.txid, revoked_htlc_txn[0].txid());
4823 assert_eq!(node_txn[0].input[1].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4826 assert_eq!(node_txn[1].input.len(), 1);
4827 check_spends!(node_txn[1], revoked_htlc_txn[0]);
4829 mine_transaction(&nodes[0], &node_txn[1]);
4830 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
4832 // Note that nodes[0]'s tx_broadcaster is still locked, so if we get here the channelmonitor
4833 // didn't try to generate any new transactions.
4835 // Check A's ChannelMonitor was able to generate the right spendable output descriptor
4836 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
4837 assert_eq!(spend_txn.len(), 3);
4838 assert_eq!(spend_txn[0].input.len(), 1);
4839 check_spends!(spend_txn[0], revoked_local_txn[0]); // spending to_remote output from revoked local tx
4840 assert_ne!(spend_txn[0].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
4841 check_spends!(spend_txn[1], node_txn[1]); // spending justice tx output on the htlc success tx
4842 check_spends!(spend_txn[2], revoked_local_txn[0], node_txn[1]); // Both outputs
4846 fn test_onchain_to_onchain_claim() {
4847 // Test that in case of channel closure, we detect the state of output and claim HTLC
4848 // on downstream peer's remote commitment tx.
4849 // First, have C claim an HTLC against its own latest commitment transaction.
4850 // Then, broadcast these to B, which should update the monitor downstream on the A<->B
4852 // Finally, check that B will claim the HTLC output if A's latest commitment transaction
4855 let chanmon_cfgs = create_chanmon_cfgs(3);
4856 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4857 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4858 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4860 // Create some initial channels
4861 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
4862 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4864 // Ensure all nodes are at the same height
4865 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4866 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4867 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4868 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4870 // Rebalance the network a bit by relaying one payment through all the channels ...
4871 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4872 send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 8000000);
4874 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
4875 let commitment_tx = get_local_commitment_txn!(nodes[2], chan_2.2);
4876 check_spends!(commitment_tx[0], chan_2.3);
4877 nodes[2].node.claim_funds(payment_preimage);
4878 expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
4879 check_added_monitors!(nodes[2], 1);
4880 let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
4881 assert!(updates.update_add_htlcs.is_empty());
4882 assert!(updates.update_fail_htlcs.is_empty());
4883 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
4884 assert!(updates.update_fail_malformed_htlcs.is_empty());
4886 mine_transaction(&nodes[2], &commitment_tx[0]);
4887 check_closed_broadcast!(nodes[2], true);
4888 check_added_monitors!(nodes[2], 1);
4889 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
4891 let c_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelMonitor: 1 (HTLC-Success tx)
4892 assert_eq!(c_txn.len(), 1);
4893 check_spends!(c_txn[0], commitment_tx[0]);
4894 assert_eq!(c_txn[0].input[0].witness.clone().last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
4895 assert!(c_txn[0].output[0].script_pubkey.is_v0_p2wsh()); // revokeable output
4896 assert_eq!(c_txn[0].lock_time, LockTime::ZERO); // Success tx
4898 // 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
4899 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![commitment_tx[0].clone(), c_txn[0].clone()]));
4900 check_added_monitors!(nodes[1], 1);
4901 let events = nodes[1].node.get_and_clear_pending_events();
4902 assert_eq!(events.len(), 2);
4904 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
4905 _ => panic!("Unexpected event"),
4908 Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id, outbound_amount_forwarded_msat } => {
4909 assert_eq!(fee_earned_msat, Some(1000));
4910 assert_eq!(prev_channel_id, Some(chan_1.2));
4911 assert_eq!(claim_from_onchain_tx, true);
4912 assert_eq!(next_channel_id, Some(chan_2.2));
4913 assert_eq!(outbound_amount_forwarded_msat, Some(3000000));
4915 _ => panic!("Unexpected event"),
4917 check_added_monitors!(nodes[1], 1);
4918 let mut msg_events = nodes[1].node.get_and_clear_pending_msg_events();
4919 assert_eq!(msg_events.len(), 3);
4920 let nodes_2_event = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut msg_events);
4921 let nodes_0_event = remove_first_msg_event_to_node(&nodes[0].node.get_our_node_id(), &mut msg_events);
4923 match nodes_2_event {
4924 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { .. }, node_id: _ } => {},
4925 _ => panic!("Unexpected event"),
4928 match nodes_0_event {
4929 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, .. } } => {
4930 assert!(update_add_htlcs.is_empty());
4931 assert!(update_fail_htlcs.is_empty());
4932 assert_eq!(update_fulfill_htlcs.len(), 1);
4933 assert!(update_fail_malformed_htlcs.is_empty());
4934 assert_eq!(nodes[0].node.get_our_node_id(), *node_id);
4936 _ => panic!("Unexpected event"),
4939 // Ensure that the last remaining message event is the BroadcastChannelUpdate msg for chan_2
4940 match msg_events[0] {
4941 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
4942 _ => panic!("Unexpected event"),
4945 // Broadcast A's commitment tx on B's chain to see if we are able to claim inbound HTLC with our HTLC-Success tx
4946 let commitment_tx = get_local_commitment_txn!(nodes[0], chan_1.2);
4947 mine_transaction(&nodes[1], &commitment_tx[0]);
4948 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
4949 let b_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4950 // ChannelMonitor: HTLC-Success tx
4951 assert_eq!(b_txn.len(), 1);
4952 check_spends!(b_txn[0], commitment_tx[0]);
4953 assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
4954 assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
4955 assert_eq!(b_txn[0].lock_time.to_consensus_u32(), nodes[1].best_block_info().1); // Success tx
4957 check_closed_broadcast!(nodes[1], true);
4958 check_added_monitors!(nodes[1], 1);
4962 fn test_duplicate_payment_hash_one_failure_one_success() {
4963 // Topology : A --> B --> C --> D
4964 // We route 2 payments with same hash between B and C, one will be timeout, the other successfully claim
4965 // Note that because C will refuse to generate two payment secrets for the same payment hash,
4966 // we forward one of the payments onwards to D.
4967 let chanmon_cfgs = create_chanmon_cfgs(4);
4968 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
4969 // When this test was written, the default base fee floated based on the HTLC count.
4970 // It is now fixed, so we simply set the fee to the expected value here.
4971 let mut config = test_default_channel_config();
4972 config.channel_config.forwarding_fee_base_msat = 196;
4973 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs,
4974 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
4975 let mut nodes = create_network(4, &node_cfgs, &node_chanmgrs);
4977 create_announced_chan_between_nodes(&nodes, 0, 1);
4978 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
4979 create_announced_chan_between_nodes(&nodes, 2, 3);
4981 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
4982 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
4983 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
4984 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
4985 connect_blocks(&nodes[3], node_max_height - nodes[3].best_block_info().1);
4987 let (our_payment_preimage, duplicate_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 900_000);
4989 let payment_secret = nodes[3].node.create_inbound_payment_for_hash(duplicate_payment_hash, None, 7200, None).unwrap();
4990 // We reduce the final CLTV here by a somewhat arbitrary constant to keep it under the one-byte
4991 // script push size limit so that the below script length checks match
4992 // ACCEPTED_HTLC_SCRIPT_WEIGHT.
4993 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV - 40)
4994 .with_bolt11_features(nodes[3].node.bolt11_invoice_features()).unwrap();
4995 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000);
4996 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 800_000, duplicate_payment_hash, payment_secret);
4998 let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
4999 assert_eq!(commitment_txn[0].input.len(), 1);
5000 check_spends!(commitment_txn[0], chan_2.3);
5002 mine_transaction(&nodes[1], &commitment_txn[0]);
5003 check_closed_broadcast!(nodes[1], true);
5004 check_added_monitors!(nodes[1], 1);
5005 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[2].node.get_our_node_id()], 100000);
5006 connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
5008 let htlc_timeout_tx;
5009 { // Extract one of the two HTLC-Timeout transaction
5010 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5011 // ChannelMonitor: timeout tx * 2-or-3
5012 assert!(node_txn.len() == 2 || node_txn.len() == 3);
5014 check_spends!(node_txn[0], commitment_txn[0]);
5015 assert_eq!(node_txn[0].input.len(), 1);
5016 assert_eq!(node_txn[0].output.len(), 1);
5018 if node_txn.len() > 2 {
5019 check_spends!(node_txn[1], commitment_txn[0]);
5020 assert_eq!(node_txn[1].input.len(), 1);
5021 assert_eq!(node_txn[1].output.len(), 1);
5022 assert_eq!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5024 check_spends!(node_txn[2], commitment_txn[0]);
5025 assert_eq!(node_txn[2].input.len(), 1);
5026 assert_eq!(node_txn[2].output.len(), 1);
5027 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
5029 check_spends!(node_txn[1], commitment_txn[0]);
5030 assert_eq!(node_txn[1].input.len(), 1);
5031 assert_eq!(node_txn[1].output.len(), 1);
5032 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
5035 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5036 assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5037 // Assign htlc_timeout_tx to the forwarded HTLC (with value ~800 sats). The received HTLC
5038 // (with value 900 sats) will be claimed in the below `claim_funds` call.
5039 if node_txn.len() > 2 {
5040 assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5041 htlc_timeout_tx = if node_txn[2].output[0].value < 900 { node_txn[2].clone() } else { node_txn[0].clone() };
5043 htlc_timeout_tx = if node_txn[0].output[0].value < 900 { node_txn[1].clone() } else { node_txn[0].clone() };
5047 nodes[2].node.claim_funds(our_payment_preimage);
5048 expect_payment_claimed!(nodes[2], duplicate_payment_hash, 900_000);
5050 mine_transaction(&nodes[2], &commitment_txn[0]);
5051 check_added_monitors!(nodes[2], 2);
5052 check_closed_event!(nodes[2], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5053 let events = nodes[2].node.get_and_clear_pending_msg_events();
5055 MessageSendEvent::UpdateHTLCs { .. } => {},
5056 _ => panic!("Unexpected event"),
5059 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5060 _ => panic!("Unexepected event"),
5062 let htlc_success_txn: Vec<_> = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
5063 assert_eq!(htlc_success_txn.len(), 2); // ChannelMonitor: HTLC-Success txn (*2 due to 2-HTLC outputs)
5064 check_spends!(htlc_success_txn[0], commitment_txn[0]);
5065 check_spends!(htlc_success_txn[1], commitment_txn[0]);
5066 assert_eq!(htlc_success_txn[0].input.len(), 1);
5067 assert_eq!(htlc_success_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5068 assert_eq!(htlc_success_txn[1].input.len(), 1);
5069 assert_eq!(htlc_success_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5070 assert_ne!(htlc_success_txn[0].input[0].previous_output, htlc_success_txn[1].input[0].previous_output);
5071 assert_ne!(htlc_success_txn[1].input[0].previous_output, htlc_timeout_tx.input[0].previous_output);
5073 mine_transaction(&nodes[1], &htlc_timeout_tx);
5074 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5075 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 }]);
5076 let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5077 assert!(htlc_updates.update_add_htlcs.is_empty());
5078 assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
5079 let first_htlc_id = htlc_updates.update_fail_htlcs[0].htlc_id;
5080 assert!(htlc_updates.update_fulfill_htlcs.is_empty());
5081 assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
5082 check_added_monitors!(nodes[1], 1);
5084 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
5085 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
5087 commitment_signed_dance!(nodes[0], nodes[1], &htlc_updates.commitment_signed, false, true);
5089 expect_payment_failed_with_update!(nodes[0], duplicate_payment_hash, false, chan_2.0.contents.short_channel_id, true);
5091 // Solve 2nd HTLC by broadcasting on B's chain HTLC-Success Tx from C
5092 mine_transaction(&nodes[1], &htlc_success_txn[1]);
5093 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(196), true, true);
5094 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5095 assert!(updates.update_add_htlcs.is_empty());
5096 assert!(updates.update_fail_htlcs.is_empty());
5097 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
5098 assert_ne!(updates.update_fulfill_htlcs[0].htlc_id, first_htlc_id);
5099 assert!(updates.update_fail_malformed_htlcs.is_empty());
5100 check_added_monitors!(nodes[1], 1);
5102 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
5103 commitment_signed_dance!(nodes[0], nodes[1], &updates.commitment_signed, false);
5104 expect_payment_sent(&nodes[0], our_payment_preimage, None, true, true);
5108 fn test_dynamic_spendable_outputs_local_htlc_success_tx() {
5109 let chanmon_cfgs = create_chanmon_cfgs(2);
5110 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5111 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5112 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5114 // Create some initial channels
5115 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5117 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
5118 let local_txn = get_local_commitment_txn!(nodes[1], chan_1.2);
5119 assert_eq!(local_txn.len(), 1);
5120 assert_eq!(local_txn[0].input.len(), 1);
5121 check_spends!(local_txn[0], chan_1.3);
5123 // Give B knowledge of preimage to be able to generate a local HTLC-Success Tx
5124 nodes[1].node.claim_funds(payment_preimage);
5125 expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
5126 check_added_monitors!(nodes[1], 1);
5128 mine_transaction(&nodes[1], &local_txn[0]);
5129 check_added_monitors!(nodes[1], 1);
5130 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
5131 let events = nodes[1].node.get_and_clear_pending_msg_events();
5133 MessageSendEvent::UpdateHTLCs { .. } => {},
5134 _ => panic!("Unexpected event"),
5137 MessageSendEvent::BroadcastChannelUpdate { .. } => {},
5138 _ => panic!("Unexepected event"),
5141 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5142 assert_eq!(node_txn.len(), 1);
5143 assert_eq!(node_txn[0].input.len(), 1);
5144 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5145 check_spends!(node_txn[0], local_txn[0]);
5149 mine_transaction(&nodes[1], &node_tx);
5150 connect_blocks(&nodes[1], BREAKDOWN_TIMEOUT as u32 - 1);
5152 // Verify that B is able to spend its own HTLC-Success tx thanks to spendable output event given back by its ChannelMonitor
5153 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5154 assert_eq!(spend_txn.len(), 1);
5155 assert_eq!(spend_txn[0].input.len(), 1);
5156 check_spends!(spend_txn[0], node_tx);
5157 assert_eq!(spend_txn[0].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5160 fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, announce_latest: bool) {
5161 // Test that we fail backwards the full set of HTLCs we need to when remote broadcasts an
5162 // unrevoked commitment transaction.
5163 // This includes HTLCs which were below the dust threshold as well as HTLCs which were awaiting
5164 // a remote RAA before they could be failed backwards (and combinations thereof).
5165 // We also test duplicate-hash HTLCs by adding two nodes on each side of the target nodes which
5166 // use the same payment hashes.
5167 // Thus, we use a six-node network:
5172 // And test where C fails back to A/B when D announces its latest commitment transaction
5173 let chanmon_cfgs = create_chanmon_cfgs(6);
5174 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
5175 // When this test was written, the default base fee floated based on the HTLC count.
5176 // It is now fixed, so we simply set the fee to the expected value here.
5177 let mut config = test_default_channel_config();
5178 config.channel_config.forwarding_fee_base_msat = 196;
5179 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs,
5180 &[Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone()), Some(config.clone())]);
5181 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
5183 let _chan_0_2 = create_announced_chan_between_nodes(&nodes, 0, 2);
5184 let _chan_1_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
5185 let chan_2_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
5186 let chan_3_4 = create_announced_chan_between_nodes(&nodes, 3, 4);
5187 let chan_3_5 = create_announced_chan_between_nodes(&nodes, 3, 5);
5189 // Rebalance and check output sanity...
5190 send_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 500000);
5191 send_payment(&nodes[1], &[&nodes[2], &nodes[3], &nodes[5]], 500000);
5192 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 2);
5194 let ds_dust_limit = nodes[3].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
5195 .unwrap().lock().unwrap().channel_by_id.get(&chan_2_3.2).unwrap().context().holder_dust_limit_satoshis;
5197 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
5199 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
5200 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5202 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
5204 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
5206 let (_, payment_hash_3, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5208 let (_, payment_hash_4, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5209 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5211 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());
5213 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());
5216 let (_, payment_hash_5, ..) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000);
5218 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], ds_dust_limit*1000);
5219 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
5222 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
5224 let (route, _, _, _) = get_route_and_payment_hash!(nodes[1], nodes[5], 1000000);
5225 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());
5227 // Double-check that six of the new HTLC were added
5228 // We now have six HTLCs pending over the dust limit and six HTLCs under the dust limit (ie,
5229 // with to_local and to_remote outputs, 8 outputs and 6 HTLCs not included).
5230 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2).len(), 1);
5231 assert_eq!(get_local_commitment_txn!(nodes[3], chan_2_3.2)[0].output.len(), 8);
5233 // Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
5234 // Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5235 nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5236 nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5237 nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5238 nodes[4].node.fail_htlc_backwards(&payment_hash_6);
5239 check_added_monitors!(nodes[4], 0);
5241 let failed_destinations = vec![
5242 HTLCDestination::FailedPayment { payment_hash: payment_hash_1 },
5243 HTLCDestination::FailedPayment { payment_hash: payment_hash_3 },
5244 HTLCDestination::FailedPayment { payment_hash: payment_hash_5 },
5245 HTLCDestination::FailedPayment { payment_hash: payment_hash_6 },
5247 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[4], failed_destinations);
5248 check_added_monitors!(nodes[4], 1);
5250 let four_removes = get_htlc_update_msgs!(nodes[4], nodes[3].node.get_our_node_id());
5251 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[0]);
5252 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[1]);
5253 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[2]);
5254 nodes[3].node.handle_update_fail_htlc(&nodes[4].node.get_our_node_id(), &four_removes.update_fail_htlcs[3]);
5255 commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
5257 // Fail 3rd below-dust and 7th above-dust HTLCs
5258 nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5259 nodes[5].node.fail_htlc_backwards(&payment_hash_4);
5260 check_added_monitors!(nodes[5], 0);
5262 let failed_destinations_2 = vec![
5263 HTLCDestination::FailedPayment { payment_hash: payment_hash_2 },
5264 HTLCDestination::FailedPayment { payment_hash: payment_hash_4 },
5266 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[5], failed_destinations_2);
5267 check_added_monitors!(nodes[5], 1);
5269 let two_removes = get_htlc_update_msgs!(nodes[5], nodes[3].node.get_our_node_id());
5270 nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[0]);
5271 nodes[3].node.handle_update_fail_htlc(&nodes[5].node.get_our_node_id(), &two_removes.update_fail_htlcs[1]);
5272 commitment_signed_dance!(nodes[3], nodes[5], two_removes.commitment_signed, false);
5274 let ds_prev_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5276 // After 4 and 2 removes respectively above in nodes[4] and nodes[5], nodes[3] should receive 6 PaymentForwardedFailed events
5277 let failed_destinations_3 = vec![
5278 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5279 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5280 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5281 HTLCDestination::NextHopChannel { node_id: Some(nodes[4].node.get_our_node_id()), channel_id: chan_3_4.2 },
5282 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5283 HTLCDestination::NextHopChannel { node_id: Some(nodes[5].node.get_our_node_id()), channel_id: chan_3_5.2 },
5285 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations_3);
5286 check_added_monitors!(nodes[3], 1);
5287 let six_removes = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
5288 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[0]);
5289 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[1]);
5290 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[2]);
5291 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[3]);
5292 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[4]);
5293 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &six_removes.update_fail_htlcs[5]);
5294 if deliver_last_raa {
5295 commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false);
5297 let _cs_last_raa = commitment_signed_dance!(nodes[2], nodes[3], six_removes.commitment_signed, false, true, false, true);
5300 // D's latest commitment transaction now contains 1st + 2nd + 9th HTLCs (implicitly, they're
5301 // below the dust limit) and the 5th + 6th + 11th HTLCs. It has failed back the 0th, 3rd, 4th,
5302 // 7th, 8th, and 10th, but as we haven't yet delivered the final RAA to C, the fails haven't
5303 // propagated back to A/B yet (and D has two unrevoked commitment transactions).
5305 // We now broadcast the latest commitment transaction, which *should* result in failures for
5306 // the 0th, 1st, 2nd, 3rd, 4th, 7th, 8th, 9th, and 10th HTLCs, ie all the below-dust HTLCs and
5307 // the non-broadcast above-dust HTLCs.
5309 // Alternatively, we may broadcast the previous commitment transaction, which should only
5310 // result in failures for the below-dust HTLCs, ie the 0th, 1st, 2nd, 3rd, 9th, and 10th HTLCs.
5311 let ds_last_commitment_tx = get_local_commitment_txn!(nodes[3], chan_2_3.2);
5313 if announce_latest {
5314 mine_transaction(&nodes[2], &ds_last_commitment_tx[0]);
5316 mine_transaction(&nodes[2], &ds_prev_commitment_tx[0]);
5318 let events = nodes[2].node.get_and_clear_pending_events();
5319 let close_event = if deliver_last_raa {
5320 assert_eq!(events.len(), 2 + 6);
5321 events.last().clone().unwrap()
5323 assert_eq!(events.len(), 1);
5324 events.last().clone().unwrap()
5327 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
5328 _ => panic!("Unexpected event"),
5331 connect_blocks(&nodes[2], ANTI_REORG_DELAY - 1);
5332 check_closed_broadcast!(nodes[2], true);
5333 if deliver_last_raa {
5334 expect_pending_htlcs_forwardable_from_events!(nodes[2], events[0..1], true);
5336 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();
5337 expect_htlc_handling_failed_destinations!(nodes[2].node.get_and_clear_pending_events(), expected_destinations);
5339 let expected_destinations: Vec<HTLCDestination> = if announce_latest {
5340 repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(9).collect()
5342 repeat(HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }).take(6).collect()
5345 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], expected_destinations);
5347 check_added_monitors!(nodes[2], 3);
5349 let cs_msgs = nodes[2].node.get_and_clear_pending_msg_events();
5350 assert_eq!(cs_msgs.len(), 2);
5351 let mut a_done = false;
5352 for msg in cs_msgs {
5354 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
5355 // Both under-dust HTLCs and the one above-dust HTLC that we had already failed
5356 // should be failed-backwards here.
5357 let target = if *node_id == nodes[0].node.get_our_node_id() {
5358 // If announce_latest, expect 0th, 1st, 4th, 8th, 10th HTLCs, else only 0th, 1st, 10th below-dust HTLCs
5359 for htlc in &updates.update_fail_htlcs {
5360 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 });
5362 assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 5 } else { 3 });
5367 // If announce_latest, expect 2nd, 3rd, 7th, 9th HTLCs, else only 2nd, 3rd, 9th below-dust HTLCs
5368 for htlc in &updates.update_fail_htlcs {
5369 assert!(htlc.htlc_id == 1 || htlc.htlc_id == 2 || htlc.htlc_id == 5 || if announce_latest { htlc.htlc_id == 4 } else { false });
5371 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
5372 assert_eq!(updates.update_fail_htlcs.len(), if announce_latest { 4 } else { 3 });
5375 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
5376 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[1]);
5377 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[2]);
5378 if announce_latest {
5379 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[3]);
5380 if *node_id == nodes[0].node.get_our_node_id() {
5381 target.node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &updates.update_fail_htlcs[4]);
5384 commitment_signed_dance!(target, nodes[2], updates.commitment_signed, false, true);
5386 _ => panic!("Unexpected event"),
5390 let as_events = nodes[0].node.get_and_clear_pending_events();
5391 assert_eq!(as_events.len(), if announce_latest { 10 } else { 6 });
5392 let mut as_failds = HashSet::new();
5393 let mut as_updates = 0;
5394 for event in as_events.iter() {
5395 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5396 assert!(as_failds.insert(*payment_hash));
5397 if *payment_hash != payment_hash_2 {
5398 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5400 assert!(!payment_failed_permanently);
5402 if let PathFailure::OnPath { network_update: Some(_) } = failure {
5405 } else if let &Event::PaymentFailed { .. } = event {
5406 } else { panic!("Unexpected event"); }
5408 assert!(as_failds.contains(&payment_hash_1));
5409 assert!(as_failds.contains(&payment_hash_2));
5410 if announce_latest {
5411 assert!(as_failds.contains(&payment_hash_3));
5412 assert!(as_failds.contains(&payment_hash_5));
5414 assert!(as_failds.contains(&payment_hash_6));
5416 let bs_events = nodes[1].node.get_and_clear_pending_events();
5417 assert_eq!(bs_events.len(), if announce_latest { 8 } else { 6 });
5418 let mut bs_failds = HashSet::new();
5419 let mut bs_updates = 0;
5420 for event in bs_events.iter() {
5421 if let &Event::PaymentPathFailed { ref payment_hash, ref payment_failed_permanently, ref failure, .. } = event {
5422 assert!(bs_failds.insert(*payment_hash));
5423 if *payment_hash != payment_hash_1 && *payment_hash != payment_hash_5 {
5424 assert_eq!(*payment_failed_permanently, deliver_last_raa);
5426 assert!(!payment_failed_permanently);
5428 if let PathFailure::OnPath { network_update: Some(_) } = failure {
5431 } else if let &Event::PaymentFailed { .. } = event {
5432 } else { panic!("Unexpected event"); }
5434 assert!(bs_failds.contains(&payment_hash_1));
5435 assert!(bs_failds.contains(&payment_hash_2));
5436 if announce_latest {
5437 assert!(bs_failds.contains(&payment_hash_4));
5439 assert!(bs_failds.contains(&payment_hash_5));
5441 // For each HTLC which was not failed-back by normal process (ie deliver_last_raa), we should
5442 // get a NetworkUpdate. A should have gotten 4 HTLCs which were failed-back due to
5443 // unknown-preimage-etc, B should have gotten 2. Thus, in the
5444 // announce_latest && deliver_last_raa case, we should have 5-4=1 and 4-2=2 NetworkUpdates.
5445 assert_eq!(as_updates, if deliver_last_raa { 1 } else if !announce_latest { 3 } else { 5 });
5446 assert_eq!(bs_updates, if deliver_last_raa { 2 } else if !announce_latest { 3 } else { 4 });
5450 fn test_fail_backwards_latest_remote_announce_a() {
5451 do_test_fail_backwards_unrevoked_remote_announce(false, true);
5455 fn test_fail_backwards_latest_remote_announce_b() {
5456 do_test_fail_backwards_unrevoked_remote_announce(true, true);
5460 fn test_fail_backwards_previous_remote_announce() {
5461 do_test_fail_backwards_unrevoked_remote_announce(false, false);
5462 // Note that true, true doesn't make sense as it implies we announce a revoked state, which is
5463 // tested for in test_commitment_revoked_fail_backward_exhaustive()
5467 fn test_dynamic_spendable_outputs_local_htlc_timeout_tx() {
5468 let chanmon_cfgs = create_chanmon_cfgs(2);
5469 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5470 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5471 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5473 // Create some initial channels
5474 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5476 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5477 let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
5478 assert_eq!(local_txn[0].input.len(), 1);
5479 check_spends!(local_txn[0], chan_1.3);
5481 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5482 mine_transaction(&nodes[0], &local_txn[0]);
5483 check_closed_broadcast!(nodes[0], true);
5484 check_added_monitors!(nodes[0], 1);
5485 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5486 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5488 let htlc_timeout = {
5489 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5490 assert_eq!(node_txn.len(), 1);
5491 assert_eq!(node_txn[0].input.len(), 1);
5492 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5493 check_spends!(node_txn[0], local_txn[0]);
5497 mine_transaction(&nodes[0], &htlc_timeout);
5498 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5499 expect_payment_failed!(nodes[0], our_payment_hash, false);
5501 // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5502 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5503 assert_eq!(spend_txn.len(), 3);
5504 check_spends!(spend_txn[0], local_txn[0]);
5505 assert_eq!(spend_txn[1].input.len(), 1);
5506 check_spends!(spend_txn[1], htlc_timeout);
5507 assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5508 assert_eq!(spend_txn[2].input.len(), 2);
5509 check_spends!(spend_txn[2], local_txn[0], htlc_timeout);
5510 assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5511 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5515 fn test_key_derivation_params() {
5516 // This test is a copy of test_dynamic_spendable_outputs_local_htlc_timeout_tx, with a key
5517 // manager rotation to test that `channel_keys_id` returned in
5518 // [`SpendableOutputDescriptor::DelayedPaymentOutput`] let us re-derive the channel key set to
5519 // then derive a `delayed_payment_key`.
5521 let chanmon_cfgs = create_chanmon_cfgs(3);
5523 // We manually create the node configuration to backup the seed.
5524 let seed = [42; 32];
5525 let keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5526 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);
5527 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, &chanmon_cfgs[0].logger));
5528 let scorer = RwLock::new(test_utils::TestScorer::new());
5529 let router = test_utils::TestRouter::new(network_graph.clone(), &scorer);
5530 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)) };
5531 let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5532 node_cfgs.remove(0);
5533 node_cfgs.insert(0, node);
5535 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5536 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5538 // Create some initial channels
5539 // Create a dummy channel to advance index by one and thus test re-derivation correctness
5541 let chan_0 = create_announced_chan_between_nodes(&nodes, 0, 2);
5542 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
5543 assert_ne!(chan_0.3.output[0].script_pubkey, chan_1.3.output[0].script_pubkey);
5545 // Ensure all nodes are at the same height
5546 let node_max_height = nodes.iter().map(|node| node.blocks.lock().unwrap().len()).max().unwrap() as u32;
5547 connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
5548 connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
5549 connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
5551 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9000000);
5552 let local_txn_0 = get_local_commitment_txn!(nodes[0], chan_0.2);
5553 let local_txn_1 = get_local_commitment_txn!(nodes[0], chan_1.2);
5554 assert_eq!(local_txn_1[0].input.len(), 1);
5555 check_spends!(local_txn_1[0], chan_1.3);
5557 // We check funding pubkey are unique
5558 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]));
5559 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]));
5560 if from_0_funding_key_0 == from_1_funding_key_0
5561 || from_0_funding_key_0 == from_1_funding_key_1
5562 || from_0_funding_key_1 == from_1_funding_key_0
5563 || from_0_funding_key_1 == from_1_funding_key_1 {
5564 panic!("Funding pubkeys aren't unique");
5567 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
5568 mine_transaction(&nodes[0], &local_txn_1[0]);
5569 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
5570 check_closed_broadcast!(nodes[0], true);
5571 check_added_monitors!(nodes[0], 1);
5572 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
5574 let htlc_timeout = {
5575 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
5576 assert_eq!(node_txn.len(), 1);
5577 assert_eq!(node_txn[0].input.len(), 1);
5578 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
5579 check_spends!(node_txn[0], local_txn_1[0]);
5583 mine_transaction(&nodes[0], &htlc_timeout);
5584 connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32 - 1);
5585 expect_payment_failed!(nodes[0], our_payment_hash, false);
5587 // Verify that A is able to spend its own HTLC-Timeout tx thanks to spendable output event given back by its ChannelMonitor
5588 let new_keys_manager = test_utils::TestKeysInterface::new(&seed, Network::Testnet);
5589 let spend_txn = check_spendable_outputs!(nodes[0], new_keys_manager);
5590 assert_eq!(spend_txn.len(), 3);
5591 check_spends!(spend_txn[0], local_txn_1[0]);
5592 assert_eq!(spend_txn[1].input.len(), 1);
5593 check_spends!(spend_txn[1], htlc_timeout);
5594 assert_eq!(spend_txn[1].input[0].sequence.0, BREAKDOWN_TIMEOUT as u32);
5595 assert_eq!(spend_txn[2].input.len(), 2);
5596 check_spends!(spend_txn[2], local_txn_1[0], htlc_timeout);
5597 assert!(spend_txn[2].input[0].sequence.0 == BREAKDOWN_TIMEOUT as u32 ||
5598 spend_txn[2].input[1].sequence.0 == BREAKDOWN_TIMEOUT as u32);
5602 fn test_static_output_closing_tx() {
5603 let chanmon_cfgs = create_chanmon_cfgs(2);
5604 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5605 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5606 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5608 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5610 send_payment(&nodes[0], &vec!(&nodes[1])[..], 8000000);
5611 let closing_tx = close_channel(&nodes[0], &nodes[1], &chan.2, chan.3, true).2;
5613 mine_transaction(&nodes[0], &closing_tx);
5614 check_closed_event!(nodes[0], 1, ClosureReason::CooperativeClosure, [nodes[1].node.get_our_node_id()], 100000);
5615 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
5617 let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[0].keys_manager);
5618 assert_eq!(spend_txn.len(), 1);
5619 check_spends!(spend_txn[0], closing_tx);
5621 mine_transaction(&nodes[1], &closing_tx);
5622 check_closed_event!(nodes[1], 1, ClosureReason::CooperativeClosure, [nodes[0].node.get_our_node_id()], 100000);
5623 connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
5625 let spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
5626 assert_eq!(spend_txn.len(), 1);
5627 check_spends!(spend_txn[0], closing_tx);
5630 fn do_htlc_claim_local_commitment_only(use_dust: bool) {
5631 let chanmon_cfgs = create_chanmon_cfgs(2);
5632 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5633 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5634 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5635 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5637 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], if use_dust { 50000 } else { 3_000_000 });
5639 // Claim the payment, but don't deliver A's commitment_signed, resulting in the HTLC only being
5640 // present in B's local commitment transaction, but none of A's commitment transactions.
5641 nodes[1].node.claim_funds(payment_preimage);
5642 check_added_monitors!(nodes[1], 1);
5643 expect_payment_claimed!(nodes[1], payment_hash, if use_dust { 50000 } else { 3_000_000 });
5645 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5646 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fulfill_htlcs[0]);
5647 expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
5649 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5650 check_added_monitors!(nodes[0], 1);
5651 let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5652 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5653 check_added_monitors!(nodes[1], 1);
5655 let starting_block = nodes[1].best_block_info();
5656 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5657 for _ in starting_block.1 + 1..TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + starting_block.1 + 2 {
5658 connect_block(&nodes[1], &block);
5659 block.header.prev_blockhash = block.block_hash();
5661 test_txn_broadcast(&nodes[1], &chan, None, if use_dust { HTLCType::NONE } else { HTLCType::SUCCESS });
5662 check_closed_broadcast!(nodes[1], true);
5663 check_added_monitors!(nodes[1], 1);
5664 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[0].node.get_our_node_id()], 100000);
5667 fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
5668 let chanmon_cfgs = create_chanmon_cfgs(2);
5669 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5670 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5671 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5672 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5674 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if use_dust { 50000 } else { 3000000 });
5675 nodes[0].node.send_payment_with_route(&route, payment_hash,
5676 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
5677 check_added_monitors!(nodes[0], 1);
5679 let _as_update = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5681 // As far as A is concerned, the HTLC is now present only in the latest remote commitment
5682 // transaction, however it is not in A's latest local commitment, so we can just broadcast that
5683 // to "time out" the HTLC.
5685 let starting_block = nodes[1].best_block_info();
5686 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5688 for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + starting_block.1 + 2 {
5689 connect_block(&nodes[0], &block);
5690 block.header.prev_blockhash = block.block_hash();
5692 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5693 check_closed_broadcast!(nodes[0], true);
5694 check_added_monitors!(nodes[0], 1);
5695 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
5698 fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
5699 let chanmon_cfgs = create_chanmon_cfgs(3);
5700 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
5701 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
5702 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
5703 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
5705 // Fail the payment, but don't deliver A's final RAA, resulting in the HTLC only being present
5706 // in B's previous (unrevoked) commitment transaction, but none of A's commitment transactions.
5707 // Also optionally test that we *don't* fail the channel in case the commitment transaction was
5708 // actually revoked.
5709 let htlc_value = if use_dust { 50000 } else { 3000000 };
5710 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5711 nodes[1].node.fail_htlc_backwards(&our_payment_hash);
5712 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
5713 check_added_monitors!(nodes[1], 1);
5715 let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5716 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &bs_updates.update_fail_htlcs[0]);
5717 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_updates.commitment_signed);
5718 check_added_monitors!(nodes[0], 1);
5719 let as_updates = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
5720 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &as_updates.0);
5721 check_added_monitors!(nodes[1], 1);
5722 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_updates.1);
5723 check_added_monitors!(nodes[1], 1);
5724 let bs_revoke_and_ack = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, nodes[0].node.get_our_node_id());
5726 if check_revoke_no_close {
5727 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_revoke_and_ack);
5728 check_added_monitors!(nodes[0], 1);
5731 let starting_block = nodes[1].best_block_info();
5732 let mut block = create_dummy_block(starting_block.0, 42, Vec::new());
5733 for _ in starting_block.1 + 1..TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + CHAN_CONFIRM_DEPTH + 2 {
5734 connect_block(&nodes[0], &block);
5735 block.header.prev_blockhash = block.block_hash();
5737 if !check_revoke_no_close {
5738 test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
5739 check_closed_broadcast!(nodes[0], true);
5740 check_added_monitors!(nodes[0], 1);
5741 check_closed_event!(nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
5743 expect_payment_failed!(nodes[0], our_payment_hash, true);
5747 // Test that we close channels on-chain when broadcastable HTLCs reach their timeout window.
5748 // There are only a few cases to test here:
5749 // * its not really normative behavior, but we test that below-dust HTLCs "included" in
5750 // broadcastable commitment transactions result in channel closure,
5751 // * its included in an unrevoked-but-previous remote commitment transaction,
5752 // * its included in the latest remote or local commitment transactions.
5753 // We test each of the three possible commitment transactions individually and use both dust and
5755 // Note that we don't bother testing both outbound and inbound HTLC failures for each case, and we
5756 // assume they are handled the same across all six cases, as both outbound and inbound failures are
5757 // tested for at least one of the cases in other tests.
5759 fn htlc_claim_single_commitment_only_a() {
5760 do_htlc_claim_local_commitment_only(true);
5761 do_htlc_claim_local_commitment_only(false);
5763 do_htlc_claim_current_remote_commitment_only(true);
5764 do_htlc_claim_current_remote_commitment_only(false);
5768 fn htlc_claim_single_commitment_only_b() {
5769 do_htlc_claim_previous_remote_commitment_only(true, false);
5770 do_htlc_claim_previous_remote_commitment_only(false, false);
5771 do_htlc_claim_previous_remote_commitment_only(true, true);
5772 do_htlc_claim_previous_remote_commitment_only(false, true);
5777 fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
5778 let chanmon_cfgs = create_chanmon_cfgs(2);
5779 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5780 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5781 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5782 // Force duplicate randomness for every get-random call
5783 for node in nodes.iter() {
5784 *node.keys_manager.override_random_bytes.lock().unwrap() = Some([0; 32]);
5787 // BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
5788 let channel_value_satoshis=10000;
5789 let push_msat=10001;
5790 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).unwrap();
5791 let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5792 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5793 get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
5795 // Create a second channel with the same random values. This used to panic due to a colliding
5796 // channel_id, but now panics due to a colliding outbound SCID alias.
5797 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).is_err());
5801 fn bolt2_open_channel_sending_node_checks_part2() {
5802 let chanmon_cfgs = create_chanmon_cfgs(2);
5803 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5804 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5805 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5807 // BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
5808 let channel_value_satoshis=2^24;
5809 let push_msat=10001;
5810 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).is_err());
5812 // BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
5813 let channel_value_satoshis=10000;
5814 // Test when push_msat is equal to 1000 * funding_satoshis.
5815 let push_msat=1000*channel_value_satoshis+1;
5816 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).is_err());
5818 // BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
5819 let channel_value_satoshis=10000;
5820 let push_msat=10001;
5821 assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).is_ok()); //Create a valid channel
5822 let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5823 assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
5825 // BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
5826 // 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
5827 assert!(node0_to_1_send_open_channel.channel_flags<=1);
5829 // 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.
5830 assert!(BREAKDOWN_TIMEOUT>0);
5831 assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
5833 // BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
5834 let chain_hash = ChainHash::using_genesis_block(Network::Testnet);
5835 assert_eq!(node0_to_1_send_open_channel.chain_hash, chain_hash);
5837 // 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.
5838 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
5839 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
5840 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
5841 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_point.serialize()).is_ok());
5842 assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
5846 fn bolt2_open_channel_sane_dust_limit() {
5847 let chanmon_cfgs = create_chanmon_cfgs(2);
5848 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5849 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5850 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5852 let channel_value_satoshis=1000000;
5853 let push_msat=10001;
5854 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42, None, None).unwrap();
5855 let mut node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
5856 node0_to_1_send_open_channel.dust_limit_satoshis = 547;
5857 node0_to_1_send_open_channel.channel_reserve_satoshis = 100001;
5859 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel);
5860 let events = nodes[1].node.get_and_clear_pending_msg_events();
5861 let err_msg = match events[0] {
5862 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
5865 _ => panic!("Unexpected event"),
5867 assert_eq!(err_msg.data, "dust_limit_satoshis (547) is greater than the implementation limit (546)");
5870 // Test that if we fail to send an HTLC that is being freed from the holding cell, and the HTLC
5871 // originated from our node, its failure is surfaced to the user. We trigger this failure to
5872 // free the HTLC by increasing our fee while the HTLC is in the holding cell such that the HTLC
5873 // is no longer affordable once it's freed.
5875 fn test_fail_holding_cell_htlc_upon_free() {
5876 let chanmon_cfgs = create_chanmon_cfgs(2);
5877 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5878 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5879 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5880 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5882 // First nodes[0] generates an update_fee, setting the channel's
5883 // pending_update_fee.
5885 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5886 *feerate_lock += 20;
5888 nodes[0].node.timer_tick_occurred();
5889 check_added_monitors!(nodes[0], 1);
5891 let events = nodes[0].node.get_and_clear_pending_msg_events();
5892 assert_eq!(events.len(), 1);
5893 let (update_msg, commitment_signed) = match events[0] {
5894 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5895 (update_fee.as_ref(), commitment_signed)
5897 _ => panic!("Unexpected event"),
5900 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5902 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5903 let channel_reserve = chan_stat.channel_reserve_msat;
5904 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5905 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5907 // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5908 let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
5909 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
5911 // Send a payment which passes reserve checks but gets stuck in the holding cell.
5912 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
5913 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
5914 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5915 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
5917 // Flush the pending fee update.
5918 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
5919 let (as_revoke_and_ack, _) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
5920 check_added_monitors!(nodes[1], 1);
5921 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_revoke_and_ack);
5922 check_added_monitors!(nodes[0], 1);
5924 // Upon receipt of the RAA, there will be an attempt to resend the holding cell
5925 // HTLC, but now that the fee has been raised the payment will now fail, causing
5926 // us to surface its failure to the user.
5927 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5928 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
5929 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 1 HTLC updates in channel {}", chan.2), 1);
5931 // Check that the payment failed to be sent out.
5932 let events = nodes[0].node.get_and_clear_pending_events();
5933 assert_eq!(events.len(), 2);
5935 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
5936 assert_eq!(PaymentId(our_payment_hash.0), *payment_id.as_ref().unwrap());
5937 assert_eq!(our_payment_hash.clone(), *payment_hash);
5938 assert_eq!(*payment_failed_permanently, false);
5939 assert_eq!(*short_channel_id, Some(route.paths[0].hops[0].short_channel_id));
5941 _ => panic!("Unexpected event"),
5944 &Event::PaymentFailed { ref payment_hash, .. } => {
5945 assert_eq!(our_payment_hash.clone(), *payment_hash);
5947 _ => panic!("Unexpected event"),
5951 // Test that if multiple HTLCs are released from the holding cell and one is
5952 // valid but the other is no longer valid upon release, the valid HTLC can be
5953 // successfully completed while the other one fails as expected.
5955 fn test_free_and_fail_holding_cell_htlcs() {
5956 let chanmon_cfgs = create_chanmon_cfgs(2);
5957 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
5958 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
5959 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
5960 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
5962 // First nodes[0] generates an update_fee, setting the channel's
5963 // pending_update_fee.
5965 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
5966 *feerate_lock += 200;
5968 nodes[0].node.timer_tick_occurred();
5969 check_added_monitors!(nodes[0], 1);
5971 let events = nodes[0].node.get_and_clear_pending_msg_events();
5972 assert_eq!(events.len(), 1);
5973 let (update_msg, commitment_signed) = match events[0] {
5974 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
5975 (update_fee.as_ref(), commitment_signed)
5977 _ => panic!("Unexpected event"),
5980 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_msg.unwrap());
5982 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5983 let channel_reserve = chan_stat.channel_reserve_msat;
5984 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
5985 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
5987 // 2* and +1 HTLCs on the commit tx fee calculation for the fee spike reserve.
5989 let amt_2 = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 2 + 1, &channel_type_features) - amt_1;
5990 let (route_1, payment_hash_1, payment_preimage_1, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_1);
5991 let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[1], amt_2);
5993 // Send 2 payments which pass reserve checks but get stuck in the holding cell.
5994 nodes[0].node.send_payment_with_route(&route_1, payment_hash_1,
5995 RecipientOnionFields::secret_only(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
5996 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
5997 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1);
5998 let payment_id_2 = PaymentId(nodes[0].keys_manager.get_secure_random_bytes());
5999 nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
6000 RecipientOnionFields::secret_only(payment_secret_2), payment_id_2).unwrap();
6001 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6002 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, amt_1 + amt_2);
6004 // Flush the pending fee update.
6005 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), commitment_signed);
6006 let (revoke_and_ack, commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6007 check_added_monitors!(nodes[1], 1);
6008 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &revoke_and_ack);
6009 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6010 check_added_monitors!(nodes[0], 2);
6012 // Upon receipt of the RAA, there will be an attempt to resend the holding cell HTLCs,
6013 // but now that the fee has been raised the second payment will now fail, causing us
6014 // to surface its failure to the user. The first payment should succeed.
6015 chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6016 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, 0);
6017 nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Freeing holding cell with 2 HTLC updates in channel {}", chan.2), 1);
6019 // Check that the second payment failed to be sent out.
6020 let events = nodes[0].node.get_and_clear_pending_events();
6021 assert_eq!(events.len(), 2);
6023 &Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, failure: PathFailure::OnPath { network_update: None }, ref short_channel_id, .. } => {
6024 assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
6025 assert_eq!(payment_hash_2.clone(), *payment_hash);
6026 assert_eq!(*payment_failed_permanently, false);
6027 assert_eq!(*short_channel_id, Some(route_2.paths[0].hops[0].short_channel_id));
6029 _ => panic!("Unexpected event"),
6032 &Event::PaymentFailed { ref payment_hash, .. } => {
6033 assert_eq!(payment_hash_2.clone(), *payment_hash);
6035 _ => panic!("Unexpected event"),
6038 // Complete the first payment and the RAA from the fee update.
6039 let (payment_event, send_raa_event) = {
6040 let mut msgs = nodes[0].node.get_and_clear_pending_msg_events();
6041 assert_eq!(msgs.len(), 2);
6042 (SendEvent::from_event(msgs.remove(0)), msgs.remove(0))
6044 let raa = match send_raa_event {
6045 MessageSendEvent::SendRevokeAndACK { msg, .. } => msg,
6046 _ => panic!("Unexpected event"),
6048 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6049 check_added_monitors!(nodes[1], 1);
6050 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6051 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6052 let events = nodes[1].node.get_and_clear_pending_events();
6053 assert_eq!(events.len(), 1);
6055 Event::PendingHTLCsForwardable { .. } => {},
6056 _ => panic!("Unexpected event"),
6058 nodes[1].node.process_pending_htlc_forwards();
6059 let events = nodes[1].node.get_and_clear_pending_events();
6060 assert_eq!(events.len(), 1);
6062 Event::PaymentClaimable { .. } => {},
6063 _ => panic!("Unexpected event"),
6065 nodes[1].node.claim_funds(payment_preimage_1);
6066 check_added_monitors!(nodes[1], 1);
6067 expect_payment_claimed!(nodes[1], payment_hash_1, amt_1);
6069 let update_msgs = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6070 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msgs.update_fulfill_htlcs[0]);
6071 commitment_signed_dance!(nodes[0], nodes[1], update_msgs.commitment_signed, false, true);
6072 expect_payment_sent!(nodes[0], payment_preimage_1);
6075 // Test that if we fail to forward an HTLC that is being freed from the holding cell that the
6076 // HTLC is failed backwards. We trigger this failure to forward the freed HTLC by increasing
6077 // our fee while the HTLC is in the holding cell such that the HTLC is no longer affordable
6080 fn test_fail_holding_cell_htlc_upon_free_multihop() {
6081 let chanmon_cfgs = create_chanmon_cfgs(3);
6082 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6083 // Avoid having to include routing fees in calculations
6084 let mut config = test_default_channel_config();
6085 config.channel_config.forwarding_fee_base_msat = 0;
6086 config.channel_config.forwarding_fee_proportional_millionths = 0;
6087 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(config.clone()), Some(config.clone()), Some(config.clone())]);
6088 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6089 let chan_0_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6090 let chan_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000);
6092 // First nodes[1] generates an update_fee, setting the channel's
6093 // pending_update_fee.
6095 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
6096 *feerate_lock += 20;
6098 nodes[1].node.timer_tick_occurred();
6099 check_added_monitors!(nodes[1], 1);
6101 let events = nodes[1].node.get_and_clear_pending_msg_events();
6102 assert_eq!(events.len(), 1);
6103 let (update_msg, commitment_signed) = match events[0] {
6104 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
6105 (update_fee.as_ref(), commitment_signed)
6107 _ => panic!("Unexpected event"),
6110 nodes[2].node.handle_update_fee(&nodes[1].node.get_our_node_id(), update_msg.unwrap());
6112 let mut chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan_0_1.2);
6113 let channel_reserve = chan_stat.channel_reserve_msat;
6114 let feerate = get_feerate!(nodes[0], nodes[1], chan_0_1.2);
6115 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan_0_1.2);
6117 // Send a payment which passes reserve checks but gets stuck in the holding cell.
6118 let max_can_send = 5000000 - channel_reserve - 2*commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6119 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], max_can_send);
6120 let payment_event = {
6121 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6122 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6123 check_added_monitors!(nodes[0], 1);
6125 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6126 assert_eq!(events.len(), 1);
6128 SendEvent::from_event(events.remove(0))
6130 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6131 check_added_monitors!(nodes[1], 0);
6132 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6133 expect_pending_htlcs_forwardable!(nodes[1]);
6135 chan_stat = get_channel_value_stat!(nodes[1], nodes[2], chan_1_2.2);
6136 assert_eq!(chan_stat.holding_cell_outbound_amount_msat, max_can_send);
6138 // Flush the pending fee update.
6139 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), commitment_signed);
6140 let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[2], nodes[1].node.get_our_node_id());
6141 check_added_monitors!(nodes[2], 1);
6142 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &raa);
6143 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &commitment_signed);
6144 check_added_monitors!(nodes[1], 2);
6146 // A final RAA message is generated to finalize the fee update.
6147 let events = nodes[1].node.get_and_clear_pending_msg_events();
6148 assert_eq!(events.len(), 1);
6150 let raa_msg = match &events[0] {
6151 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => {
6154 _ => panic!("Unexpected event"),
6157 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa_msg);
6158 check_added_monitors!(nodes[2], 1);
6159 assert!(nodes[2].node.get_and_clear_pending_msg_events().is_empty());
6161 // nodes[1]'s ChannelManager will now signal that we have HTLC forwards to process.
6162 let process_htlc_forwards_event = nodes[1].node.get_and_clear_pending_events();
6163 assert_eq!(process_htlc_forwards_event.len(), 2);
6164 match &process_htlc_forwards_event[0] {
6165 &Event::PendingHTLCsForwardable { .. } => {},
6166 _ => panic!("Unexpected event"),
6169 // In response, we call ChannelManager's process_pending_htlc_forwards
6170 nodes[1].node.process_pending_htlc_forwards();
6171 check_added_monitors!(nodes[1], 1);
6173 // This causes the HTLC to be failed backwards.
6174 let fail_event = nodes[1].node.get_and_clear_pending_msg_events();
6175 assert_eq!(fail_event.len(), 1);
6176 let (fail_msg, commitment_signed) = match &fail_event[0] {
6177 &MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6178 assert_eq!(updates.update_add_htlcs.len(), 0);
6179 assert_eq!(updates.update_fulfill_htlcs.len(), 0);
6180 assert_eq!(updates.update_fail_malformed_htlcs.len(), 0);
6181 assert_eq!(updates.update_fail_htlcs.len(), 1);
6182 (updates.update_fail_htlcs[0].clone(), updates.commitment_signed.clone())
6184 _ => panic!("Unexpected event"),
6187 // Pass the failure messages back to nodes[0].
6188 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
6189 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &commitment_signed);
6191 // Complete the HTLC failure+removal process.
6192 let (raa, commitment_signed) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6193 check_added_monitors!(nodes[0], 1);
6194 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa);
6195 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &commitment_signed);
6196 check_added_monitors!(nodes[1], 2);
6197 let final_raa_event = nodes[1].node.get_and_clear_pending_msg_events();
6198 assert_eq!(final_raa_event.len(), 1);
6199 let raa = match &final_raa_event[0] {
6200 &MessageSendEvent::SendRevokeAndACK { ref msg, .. } => msg.clone(),
6201 _ => panic!("Unexpected event"),
6203 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &raa);
6204 expect_payment_failed_with_update!(nodes[0], our_payment_hash, false, chan_1_2.0.contents.short_channel_id, false);
6205 check_added_monitors!(nodes[0], 1);
6209 fn test_payment_route_reaching_same_channel_twice() {
6210 //A route should not go through the same channel twice
6211 //It is enforced when constructing a route.
6212 let chanmon_cfgs = create_chanmon_cfgs(2);
6213 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6214 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6215 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6216 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6218 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
6219 .with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
6220 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
6222 // Extend the path by itself, essentially simulating route going through same channel twice
6223 let cloned_hops = route.paths[0].hops.clone();
6224 route.paths[0].hops.extend_from_slice(&cloned_hops);
6226 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6227 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6228 ), false, APIError::InvalidRoute { ref err },
6229 assert_eq!(err, &"Path went through the same channel twice"));
6232 // BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
6233 // 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.
6234 //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.
6237 fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() {
6238 //BOLT2 Requirement: MUST NOT offer amount_msat below the receiving node's htlc_minimum_msat (same validation check catches both of these)
6239 let chanmon_cfgs = create_chanmon_cfgs(2);
6240 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6241 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6242 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6243 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6245 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6246 route.paths[0].hops[0].fee_msat = 100;
6248 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6249 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6250 ), true, APIError::ChannelUnavailable { .. }, {});
6251 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6255 fn test_update_add_htlc_bolt2_sender_zero_value_msat() {
6256 //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6257 let chanmon_cfgs = create_chanmon_cfgs(2);
6258 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6259 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6260 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6261 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6263 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6264 route.paths[0].hops[0].fee_msat = 0;
6265 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6266 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)),
6267 true, APIError::ChannelUnavailable { ref err },
6268 assert_eq!(err, "Cannot send 0-msat HTLC"));
6270 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6271 nodes[0].logger.assert_log_contains("lightning::ln::channelmanager", "Cannot send 0-msat HTLC", 1);
6275 fn test_update_add_htlc_bolt2_receiver_zero_value_msat() {
6276 //BOLT2 Requirement: MUST offer amount_msat greater than 0.
6277 let chanmon_cfgs = create_chanmon_cfgs(2);
6278 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6279 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6280 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6281 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6283 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6284 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6285 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6286 check_added_monitors!(nodes[0], 1);
6287 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6288 updates.update_add_htlcs[0].amount_msat = 0;
6290 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6291 nodes[1].logger.assert_log("lightning::ln::channelmanager".to_string(), "Remote side tried to send a 0-msat HTLC".to_string(), 1);
6292 check_closed_broadcast!(nodes[1], true).unwrap();
6293 check_added_monitors!(nodes[1], 1);
6294 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Remote side tried to send a 0-msat HTLC".to_string() },
6295 [nodes[0].node.get_our_node_id()], 100000);
6299 fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() {
6300 //BOLT 2 Requirement: MUST set cltv_expiry less than 500000000.
6301 //It is enforced when constructing a route.
6302 let chanmon_cfgs = create_chanmon_cfgs(2);
6303 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6304 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6305 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6306 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6308 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
6309 .with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
6310 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
6311 route.paths[0].hops.last_mut().unwrap().cltv_expiry_delta = 500000001;
6312 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6313 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6314 ), true, APIError::InvalidRoute { ref err },
6315 assert_eq!(err, &"Channel CLTV overflowed?"));
6319 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() {
6320 //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.
6321 //BOLT 2 Requirement: for the first HTLC it offers MUST set id to 0.
6322 //BOLT 2 Requirement: MUST increase the value of id by 1 for each successive offer.
6323 let chanmon_cfgs = create_chanmon_cfgs(2);
6324 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6325 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6326 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6327 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0);
6328 let max_accepted_htlcs = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
6329 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().counterparty_max_accepted_htlcs as u64;
6331 // Fetch a route in advance as we will be unable to once we're unable to send.
6332 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6333 for i in 0..max_accepted_htlcs {
6334 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
6335 let payment_event = {
6336 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6337 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6338 check_added_monitors!(nodes[0], 1);
6340 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6341 assert_eq!(events.len(), 1);
6342 if let MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate{ update_add_htlcs: ref htlcs, .. }, } = events[0] {
6343 assert_eq!(htlcs[0].htlc_id, i);
6347 SendEvent::from_event(events.remove(0))
6349 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6350 check_added_monitors!(nodes[1], 0);
6351 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6353 expect_pending_htlcs_forwardable!(nodes[1]);
6354 expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 100000);
6356 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6357 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6358 ), true, APIError::ChannelUnavailable { .. }, {});
6360 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6364 fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() {
6365 //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.
6366 let chanmon_cfgs = create_chanmon_cfgs(2);
6367 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6368 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6369 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6370 let channel_value = 100000;
6371 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value, 0);
6372 let max_in_flight = get_channel_value_stat!(nodes[0], nodes[1], chan.2).counterparty_max_htlc_value_in_flight_msat;
6374 send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight);
6376 let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_in_flight);
6377 // Manually create a route over our max in flight (which our router normally automatically
6379 route.paths[0].hops[0].fee_msat = max_in_flight + 1;
6380 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6381 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
6382 ), true, APIError::ChannelUnavailable { .. }, {});
6383 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
6385 send_payment(&nodes[0], &[&nodes[1]], max_in_flight);
6388 // BOLT 2 Requirements for the Receiver when handling an update_add_htlc message.
6390 fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() {
6391 //BOLT2 Requirement: receiving an amount_msat equal to 0, OR less than its own htlc_minimum_msat -> SHOULD fail the channel.
6392 let chanmon_cfgs = create_chanmon_cfgs(2);
6393 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6394 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6395 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6396 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6397 let htlc_minimum_msat: u64;
6399 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
6400 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
6401 let channel = chan_lock.channel_by_id.get(&chan.2).unwrap();
6402 htlc_minimum_msat = channel.context().get_holder_htlc_minimum_msat();
6405 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], htlc_minimum_msat);
6406 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6407 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6408 check_added_monitors!(nodes[0], 1);
6409 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6410 updates.update_add_htlcs[0].amount_msat = htlc_minimum_msat-1;
6411 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6412 assert!(nodes[1].node.list_channels().is_empty());
6413 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6414 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()));
6415 check_added_monitors!(nodes[1], 1);
6416 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6420 fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() {
6421 //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
6422 let chanmon_cfgs = create_chanmon_cfgs(2);
6423 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6424 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6425 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6426 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6428 let chan_stat = get_channel_value_stat!(nodes[0], nodes[1], chan.2);
6429 let channel_reserve = chan_stat.channel_reserve_msat;
6430 let feerate = get_feerate!(nodes[0], nodes[1], chan.2);
6431 let channel_type_features = get_channel_type_features!(nodes[0], nodes[1], chan.2);
6432 // The 2* and +1 are for the fee spike reserve.
6433 let commit_tx_fee_outbound = 2 * commit_tx_fee_msat(feerate, 1 + 1, &channel_type_features);
6435 let max_can_send = 5000000 - channel_reserve - commit_tx_fee_outbound;
6436 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_can_send);
6437 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6438 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6439 check_added_monitors!(nodes[0], 1);
6440 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6442 // Even though channel-initiator senders are required to respect the fee_spike_reserve,
6443 // at this time channel-initiatee receivers are not required to enforce that senders
6444 // respect the fee_spike_reserve.
6445 updates.update_add_htlcs[0].amount_msat = max_can_send + commit_tx_fee_outbound + 1;
6446 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6448 assert!(nodes[1].node.list_channels().is_empty());
6449 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6450 assert_eq!(err_msg.data, "Remote HTLC add would put them under remote reserve value");
6451 check_added_monitors!(nodes[1], 1);
6452 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6456 fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
6457 //BOLT 2 Requirement: if a sending node adds more than its max_accepted_htlcs HTLCs to its local commitment transaction: SHOULD fail the channel
6458 //BOLT 2 Requirement: MUST allow multiple HTLCs with the same payment_hash.
6459 let chanmon_cfgs = create_chanmon_cfgs(2);
6460 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6461 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6462 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6463 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6465 let send_amt = 3999999;
6466 let (mut route, our_payment_hash, _, our_payment_secret) =
6467 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
6468 route.paths[0].hops[0].fee_msat = send_amt;
6469 let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
6470 let cur_height = nodes[0].node.best_block.read().unwrap().height() + 1;
6471 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap();
6472 let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(
6473 &route.paths[0], send_amt, RecipientOnionFields::secret_only(our_payment_secret), cur_height, &None).unwrap();
6474 let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
6476 let mut msg = msgs::UpdateAddHTLC {
6480 payment_hash: our_payment_hash,
6481 cltv_expiry: htlc_cltv,
6482 onion_routing_packet: onion_packet.clone(),
6483 skimmed_fee_msat: None,
6487 msg.htlc_id = i as u64;
6488 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6490 msg.htlc_id = (50) as u64;
6491 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
6493 assert!(nodes[1].node.list_channels().is_empty());
6494 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6495 assert!(regex::Regex::new(r"Remote tried to push more than our max accepted HTLCs \(\d+\)").unwrap().is_match(err_msg.data.as_str()));
6496 check_added_monitors!(nodes[1], 1);
6497 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6501 fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() {
6502 //OR adds more than its max_htlc_value_in_flight_msat worth of offered HTLCs to its local commitment transaction: SHOULD fail the channel
6503 let chanmon_cfgs = create_chanmon_cfgs(2);
6504 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6505 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6506 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6507 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6509 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6510 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6511 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6512 check_added_monitors!(nodes[0], 1);
6513 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6514 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;
6515 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6517 assert!(nodes[1].node.list_channels().is_empty());
6518 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6519 assert!(regex::Regex::new("Remote HTLC add would put them over our max HTLC value").unwrap().is_match(err_msg.data.as_str()));
6520 check_added_monitors!(nodes[1], 1);
6521 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 1000000);
6525 fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() {
6526 //BOLT2 Requirement: if sending node sets cltv_expiry to greater or equal to 500000000: SHOULD fail the channel.
6527 let chanmon_cfgs = create_chanmon_cfgs(2);
6528 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6529 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6530 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6532 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
6533 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6534 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6535 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6536 check_added_monitors!(nodes[0], 1);
6537 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6538 updates.update_add_htlcs[0].cltv_expiry = 500000000;
6539 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6541 assert!(nodes[1].node.list_channels().is_empty());
6542 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6543 assert_eq!(err_msg.data,"Remote provided CLTV expiry in seconds instead of block height");
6544 check_added_monitors!(nodes[1], 1);
6545 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6549 fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() {
6550 //BOLT 2 requirement: if the sender did not previously acknowledge the commitment of that HTLC: MUST ignore a repeated id value after a reconnection.
6551 // We test this by first testing that that repeated HTLCs pass commitment signature checks
6552 // after disconnect and that non-sequential htlc_ids result in a channel failure.
6553 let chanmon_cfgs = create_chanmon_cfgs(2);
6554 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6555 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6556 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6558 create_announced_chan_between_nodes(&nodes, 0, 1);
6559 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6560 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6561 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6562 check_added_monitors!(nodes[0], 1);
6563 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6564 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6566 //Disconnect and Reconnect
6567 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
6568 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
6569 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
6570 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
6572 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
6573 assert_eq!(reestablish_1.len(), 1);
6574 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
6575 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
6577 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
6578 assert_eq!(reestablish_2.len(), 1);
6579 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
6580 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
6581 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
6582 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
6585 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6586 assert_eq!(updates.commitment_signed.htlc_signatures.len(), 1);
6587 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &updates.commitment_signed);
6588 check_added_monitors!(nodes[1], 1);
6589 let _bs_responses = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id());
6591 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6593 assert!(nodes[1].node.list_channels().is_empty());
6594 let err_msg = check_closed_broadcast!(nodes[1], true).unwrap();
6595 assert!(regex::Regex::new(r"Remote skipped HTLC ID \(skipped ID: \d+\)").unwrap().is_match(err_msg.data.as_str()));
6596 check_added_monitors!(nodes[1], 1);
6597 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[0].node.get_our_node_id()], 100000);
6601 fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() {
6602 //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6604 let chanmon_cfgs = create_chanmon_cfgs(2);
6605 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6606 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6607 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6608 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6609 let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6610 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6611 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6613 check_added_monitors!(nodes[0], 1);
6614 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6615 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6617 let update_msg = msgs::UpdateFulfillHTLC{
6620 payment_preimage: our_payment_preimage,
6623 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6625 assert!(nodes[0].node.list_channels().is_empty());
6626 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6627 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()));
6628 check_added_monitors!(nodes[0], 1);
6629 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6633 fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() {
6634 //BOLT 2 Requirement: until the corresponding HTLC is irrevocably committed in both sides' commitment transactions: MUST NOT send an update_fulfill_htlc, update_fail_htlc, or update_fail_malformed_htlc.
6636 let chanmon_cfgs = create_chanmon_cfgs(2);
6637 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6638 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6639 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6640 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6642 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6643 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6644 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6645 check_added_monitors!(nodes[0], 1);
6646 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6647 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6649 let update_msg = msgs::UpdateFailHTLC{
6652 reason: msgs::OnionErrorPacket { data: Vec::new()},
6655 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6657 assert!(nodes[0].node.list_channels().is_empty());
6658 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6659 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()));
6660 check_added_monitors!(nodes[0], 1);
6661 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6665 fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() {
6666 //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.
6668 let chanmon_cfgs = create_chanmon_cfgs(2);
6669 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6670 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6671 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6672 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
6674 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6675 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6676 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6677 check_added_monitors!(nodes[0], 1);
6678 let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6679 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6680 let update_msg = msgs::UpdateFailMalformedHTLC{
6683 sha256_of_onion: [1; 32],
6684 failure_code: 0x8000,
6687 nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6689 assert!(nodes[0].node.list_channels().is_empty());
6690 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6691 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()));
6692 check_added_monitors!(nodes[0], 1);
6693 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6697 fn test_update_fulfill_htlc_bolt2_incorrect_htlc_id() {
6698 //BOLT 2 Requirement: A receiving node: if the id does not correspond to an HTLC in its current commitment transaction MUST fail the channel.
6700 let chanmon_cfgs = create_chanmon_cfgs(2);
6701 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6702 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6703 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6704 create_announced_chan_between_nodes(&nodes, 0, 1);
6706 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6708 nodes[1].node.claim_funds(our_payment_preimage);
6709 check_added_monitors!(nodes[1], 1);
6710 expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6712 let events = nodes[1].node.get_and_clear_pending_msg_events();
6713 assert_eq!(events.len(), 1);
6714 let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6716 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, .. } } => {
6717 assert!(update_add_htlcs.is_empty());
6718 assert_eq!(update_fulfill_htlcs.len(), 1);
6719 assert!(update_fail_htlcs.is_empty());
6720 assert!(update_fail_malformed_htlcs.is_empty());
6721 assert!(update_fee.is_none());
6722 update_fulfill_htlcs[0].clone()
6724 _ => panic!("Unexpected event"),
6728 update_fulfill_msg.htlc_id = 1;
6730 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6732 assert!(nodes[0].node.list_channels().is_empty());
6733 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6734 assert_eq!(err_msg.data, "Remote tried to fulfill/fail an HTLC we couldn't find");
6735 check_added_monitors!(nodes[0], 1);
6736 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6740 fn test_update_fulfill_htlc_bolt2_wrong_preimage() {
6741 //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.
6743 let chanmon_cfgs = create_chanmon_cfgs(2);
6744 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6745 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6746 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6747 create_announced_chan_between_nodes(&nodes, 0, 1);
6749 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 100_000);
6751 nodes[1].node.claim_funds(our_payment_preimage);
6752 check_added_monitors!(nodes[1], 1);
6753 expect_payment_claimed!(nodes[1], our_payment_hash, 100_000);
6755 let events = nodes[1].node.get_and_clear_pending_msg_events();
6756 assert_eq!(events.len(), 1);
6757 let mut update_fulfill_msg: msgs::UpdateFulfillHTLC = {
6759 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, .. } } => {
6760 assert!(update_add_htlcs.is_empty());
6761 assert_eq!(update_fulfill_htlcs.len(), 1);
6762 assert!(update_fail_htlcs.is_empty());
6763 assert!(update_fail_malformed_htlcs.is_empty());
6764 assert!(update_fee.is_none());
6765 update_fulfill_htlcs[0].clone()
6767 _ => panic!("Unexpected event"),
6771 update_fulfill_msg.payment_preimage = PaymentPreimage([1; 32]);
6773 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &update_fulfill_msg);
6775 assert!(nodes[0].node.list_channels().is_empty());
6776 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6777 assert!(regex::Regex::new(r"Remote tried to fulfill HTLC \(\d+\) with an incorrect preimage").unwrap().is_match(err_msg.data.as_str()));
6778 check_added_monitors!(nodes[0], 1);
6779 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 100000);
6783 fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_message() {
6784 //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.
6786 let chanmon_cfgs = create_chanmon_cfgs(2);
6787 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6788 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6789 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6790 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6792 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
6793 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6794 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6795 check_added_monitors!(nodes[0], 1);
6797 let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id());
6798 updates.update_add_htlcs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6800 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &updates.update_add_htlcs[0]);
6801 check_added_monitors!(nodes[1], 0);
6802 commitment_signed_dance!(nodes[1], nodes[0], updates.commitment_signed, false, true);
6804 let events = nodes[1].node.get_and_clear_pending_msg_events();
6806 let mut update_msg: msgs::UpdateFailMalformedHTLC = {
6808 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, .. } } => {
6809 assert!(update_add_htlcs.is_empty());
6810 assert!(update_fulfill_htlcs.is_empty());
6811 assert!(update_fail_htlcs.is_empty());
6812 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6813 assert!(update_fee.is_none());
6814 update_fail_malformed_htlcs[0].clone()
6816 _ => panic!("Unexpected event"),
6819 update_msg.failure_code &= !0x8000;
6820 nodes[0].node.handle_update_fail_malformed_htlc(&nodes[1].node.get_our_node_id(), &update_msg);
6822 assert!(nodes[0].node.list_channels().is_empty());
6823 let err_msg = check_closed_broadcast!(nodes[0], true).unwrap();
6824 assert_eq!(err_msg.data, "Got update_fail_malformed_htlc with BADONION not set");
6825 check_added_monitors!(nodes[0], 1);
6826 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: err_msg.data }, [nodes[1].node.get_our_node_id()], 1000000);
6830 fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_update_fail_htlc() {
6831 //BOLT 2 Requirement: a receiving node which has an outgoing HTLC canceled by update_fail_malformed_htlc:
6832 // * 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.
6834 let chanmon_cfgs = create_chanmon_cfgs(3);
6835 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6836 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6837 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6838 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000);
6839 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000);
6841 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100000);
6844 let mut payment_event = {
6845 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6846 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6847 check_added_monitors!(nodes[0], 1);
6848 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
6849 assert_eq!(events.len(), 1);
6850 SendEvent::from_event(events.remove(0))
6852 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6853 check_added_monitors!(nodes[1], 0);
6854 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6855 expect_pending_htlcs_forwardable!(nodes[1]);
6856 let mut events_2 = nodes[1].node.get_and_clear_pending_msg_events();
6857 assert_eq!(events_2.len(), 1);
6858 check_added_monitors!(nodes[1], 1);
6859 payment_event = SendEvent::from_event(events_2.remove(0));
6860 assert_eq!(payment_event.msgs.len(), 1);
6863 payment_event.msgs[0].onion_routing_packet.version = 1; //Produce a malformed HTLC message
6864 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6865 check_added_monitors!(nodes[2], 0);
6866 commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6868 let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6869 assert_eq!(events_3.len(), 1);
6870 let update_msg : (msgs::UpdateFailMalformedHTLC, msgs::CommitmentSigned) = {
6872 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 } } => {
6873 assert!(update_add_htlcs.is_empty());
6874 assert!(update_fulfill_htlcs.is_empty());
6875 assert!(update_fail_htlcs.is_empty());
6876 assert_eq!(update_fail_malformed_htlcs.len(), 1);
6877 assert!(update_fee.is_none());
6878 (update_fail_malformed_htlcs[0].clone(), commitment_signed.clone())
6880 _ => panic!("Unexpected event"),
6884 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg.0);
6886 check_added_monitors!(nodes[1], 0);
6887 commitment_signed_dance!(nodes[1], nodes[2], update_msg.1, false, true);
6888 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 }]);
6889 let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6890 assert_eq!(events_4.len(), 1);
6892 //Confirm that handlinge the update_malformed_htlc message produces an update_fail_htlc message to be forwarded back along the route
6894 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, .. } } => {
6895 assert!(update_add_htlcs.is_empty());
6896 assert!(update_fulfill_htlcs.is_empty());
6897 assert_eq!(update_fail_htlcs.len(), 1);
6898 assert!(update_fail_malformed_htlcs.is_empty());
6899 assert!(update_fee.is_none());
6901 _ => panic!("Unexpected event"),
6904 check_added_monitors!(nodes[1], 1);
6908 fn test_channel_failed_after_message_with_badonion_node_perm_bits_set() {
6909 let chanmon_cfgs = create_chanmon_cfgs(3);
6910 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
6911 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
6912 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
6913 create_announced_chan_between_nodes(&nodes, 0, 1);
6914 let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
6916 let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 100_000);
6919 let mut payment_event = {
6920 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
6921 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
6922 check_added_monitors!(nodes[0], 1);
6923 SendEvent::from_node(&nodes[0])
6926 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
6927 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
6928 expect_pending_htlcs_forwardable!(nodes[1]);
6929 check_added_monitors!(nodes[1], 1);
6930 payment_event = SendEvent::from_node(&nodes[1]);
6931 assert_eq!(payment_event.msgs.len(), 1);
6934 payment_event.msgs[0].onion_routing_packet.version = 1; // Trigger an invalid_onion_version error
6935 nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
6936 check_added_monitors!(nodes[2], 0);
6937 commitment_signed_dance!(nodes[2], nodes[1], payment_event.commitment_msg, false, true);
6939 let events_3 = nodes[2].node.get_and_clear_pending_msg_events();
6940 assert_eq!(events_3.len(), 1);
6942 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6943 let mut update_msg = updates.update_fail_malformed_htlcs[0].clone();
6944 // Set the NODE bit (BADONION and PERM already set in invalid_onion_version error)
6945 update_msg.failure_code |= 0x2000;
6947 nodes[1].node.handle_update_fail_malformed_htlc(&nodes[2].node.get_our_node_id(), &update_msg);
6948 commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true);
6950 _ => panic!("Unexpected event"),
6953 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1],
6954 vec![HTLCDestination::NextHopChannel {
6955 node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan_2.2 }]);
6956 let events_4 = nodes[1].node.get_and_clear_pending_msg_events();
6957 assert_eq!(events_4.len(), 1);
6958 check_added_monitors!(nodes[1], 1);
6961 MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
6962 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
6963 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false, true);
6965 _ => panic!("Unexpected event"),
6968 let events_5 = nodes[0].node.get_and_clear_pending_events();
6969 assert_eq!(events_5.len(), 2);
6971 // Expect a PaymentPathFailed event with a ChannelFailure network update for the channel between
6972 // the node originating the error to its next hop.
6974 Event::PaymentPathFailed { error_code, failure: PathFailure::OnPath { network_update: Some(NetworkUpdate::ChannelFailure { short_channel_id, is_permanent }) }, ..
6976 assert_eq!(short_channel_id, chan_2.0.contents.short_channel_id);
6977 assert!(is_permanent);
6978 assert_eq!(error_code, Some(0x8000|0x4000|0x2000|4));
6980 _ => panic!("Unexpected event"),
6983 Event::PaymentFailed { payment_hash, .. } => {
6984 assert_eq!(payment_hash, our_payment_hash);
6986 _ => panic!("Unexpected event"),
6989 // TODO: Test actual removal of channel from NetworkGraph when it's implemented.
6992 fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
6993 // Dust-HTLC failure updates must be delayed until failure-trigger tx (in this case local commitment) reach ANTI_REORG_DELAY
6994 // 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
6995 // HTLC could have been removed from lastest local commitment tx but still valid until we get remote RAA
6997 let mut chanmon_cfgs = create_chanmon_cfgs(2);
6998 chanmon_cfgs[0].keys_manager.disable_revocation_policy_check = true;
6999 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7000 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7001 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7002 let chan =create_announced_chan_between_nodes(&nodes, 0, 1);
7004 let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
7005 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
7007 // We route 2 dust-HTLCs between A and B
7008 let (_, payment_hash_1, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7009 let (_, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7010 route_payment(&nodes[0], &[&nodes[1]], 1000000);
7012 // Cache one local commitment tx as previous
7013 let as_prev_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7015 // Fail one HTLC to prune it in the will-be-latest-local commitment tx
7016 nodes[1].node.fail_htlc_backwards(&payment_hash_2);
7017 check_added_monitors!(nodes[1], 0);
7018 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
7019 check_added_monitors!(nodes[1], 1);
7021 let remove = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
7022 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &remove.update_fail_htlcs[0]);
7023 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &remove.commitment_signed);
7024 check_added_monitors!(nodes[0], 1);
7026 // Cache one local commitment tx as lastest
7027 let as_last_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7029 let events = nodes[0].node.get_and_clear_pending_msg_events();
7031 MessageSendEvent::SendRevokeAndACK { node_id, .. } => {
7032 assert_eq!(node_id, nodes[1].node.get_our_node_id());
7034 _ => panic!("Unexpected event"),
7037 MessageSendEvent::UpdateHTLCs { node_id, .. } => {
7038 assert_eq!(node_id, nodes[1].node.get_our_node_id());
7040 _ => panic!("Unexpected event"),
7043 assert_ne!(as_prev_commitment_tx, as_last_commitment_tx);
7044 // Fail the 2 dust-HTLCs, move their failure in maturation buffer (htlc_updated_waiting_threshold_conf)
7045 if announce_latest {
7046 mine_transaction(&nodes[0], &as_last_commitment_tx[0]);
7048 mine_transaction(&nodes[0], &as_prev_commitment_tx[0]);
7051 check_closed_broadcast!(nodes[0], true);
7052 check_added_monitors!(nodes[0], 1);
7053 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7055 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7056 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7057 let events = nodes[0].node.get_and_clear_pending_events();
7058 // Only 2 PaymentPathFailed events should show up, over-dust HTLC has to be failed by timeout tx
7059 assert_eq!(events.len(), 4);
7060 let mut first_failed = false;
7061 for event in events {
7063 Event::PaymentPathFailed { payment_hash, .. } => {
7064 if payment_hash == payment_hash_1 {
7065 assert!(!first_failed);
7066 first_failed = true;
7068 assert_eq!(payment_hash, payment_hash_2);
7071 Event::PaymentFailed { .. } => {}
7072 _ => panic!("Unexpected event"),
7078 fn test_failure_delay_dust_htlc_local_commitment() {
7079 do_test_failure_delay_dust_htlc_local_commitment(true);
7080 do_test_failure_delay_dust_htlc_local_commitment(false);
7083 fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
7084 // Outbound HTLC-failure updates must be cancelled if we get a reorg before we reach ANTI_REORG_DELAY.
7085 // Broadcast of revoked remote commitment tx, trigger failure-update of dust/non-dust HTLCs
7086 // Broadcast of remote commitment tx, trigger failure-update of dust-HTLCs
7087 // Broadcast of timeout tx on remote commitment tx, trigger failure-udate of non-dust HTLCs
7088 // Broadcast of local commitment tx, trigger failure-update of dust-HTLCs
7089 // Broadcast of HTLC-timeout tx on local commitment tx, trigger failure-update of non-dust HTLCs
7091 let chanmon_cfgs = create_chanmon_cfgs(3);
7092 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
7093 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
7094 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
7095 let chan = create_announced_chan_between_nodes(&nodes, 0, 1);
7097 let bs_dust_limit = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[0].node.get_our_node_id())
7098 .unwrap().lock().unwrap().channel_by_id.get(&chan.2).unwrap().context().holder_dust_limit_satoshis;
7100 let (_payment_preimage_1, dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
7101 let (_payment_preimage_2, non_dust_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7103 let as_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
7104 let bs_commitment_tx = get_local_commitment_txn!(nodes[1], chan.2);
7106 // We revoked bs_commitment_tx
7108 let (payment_preimage_3, ..) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
7109 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_3);
7112 let mut timeout_tx = Vec::new();
7114 // We fail dust-HTLC 1 by broadcast of local commitment tx
7115 mine_transaction(&nodes[0], &as_commitment_tx[0]);
7116 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7117 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7118 expect_payment_failed!(nodes[0], dust_hash, false);
7120 connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS - ANTI_REORG_DELAY);
7121 check_closed_broadcast!(nodes[0], true);
7122 check_added_monitors!(nodes[0], 1);
7123 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7124 timeout_tx.push(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0].clone());
7125 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7126 // We fail non-dust-HTLC 2 by broadcast of local HTLC-timeout tx on local commitment tx
7127 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7128 mine_transaction(&nodes[0], &timeout_tx[0]);
7129 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7130 expect_payment_failed!(nodes[0], non_dust_hash, false);
7132 // We fail dust-HTLC 1 by broadcast of remote commitment tx. If revoked, fail also non-dust HTLC
7133 mine_transaction(&nodes[0], &bs_commitment_tx[0]);
7134 check_closed_broadcast!(nodes[0], true);
7135 check_added_monitors!(nodes[0], 1);
7136 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
7137 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7139 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7140 timeout_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..)
7141 .filter(|tx| tx.input[0].previous_output.txid == bs_commitment_tx[0].txid()).collect();
7142 check_spends!(timeout_tx[0], bs_commitment_tx[0]);
7143 // For both a revoked or non-revoked commitment transaction, after ANTI_REORG_DELAY the
7144 // dust HTLC should have been failed.
7145 expect_payment_failed!(nodes[0], dust_hash, false);
7148 assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7150 assert_eq!(timeout_tx[0].lock_time.to_consensus_u32(), 11);
7152 // We fail non-dust-HTLC 2 by broadcast of local timeout/revocation-claim tx
7153 mine_transaction(&nodes[0], &timeout_tx[0]);
7154 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
7155 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7156 expect_payment_failed!(nodes[0], non_dust_hash, false);
7161 fn test_sweep_outbound_htlc_failure_update() {
7162 do_test_sweep_outbound_htlc_failure_update(false, true);
7163 do_test_sweep_outbound_htlc_failure_update(false, false);
7164 do_test_sweep_outbound_htlc_failure_update(true, false);
7168 fn test_user_configurable_csv_delay() {
7169 // We test our channel constructors yield errors when we pass them absurd csv delay
7171 let mut low_our_to_self_config = UserConfig::default();
7172 low_our_to_self_config.channel_handshake_config.our_to_self_delay = 6;
7173 let mut high_their_to_self_config = UserConfig::default();
7174 high_their_to_self_config.channel_handshake_limits.their_to_self_delay = 100;
7175 let user_cfgs = [Some(high_their_to_self_config.clone()), None];
7176 let chanmon_cfgs = create_chanmon_cfgs(2);
7177 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7178 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
7179 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7181 // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in OutboundV1Channel::new()
7182 if let Err(error) = OutboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7183 &nodes[0].keys_manager, &nodes[0].keys_manager, nodes[1].node.get_our_node_id(), &nodes[1].node.init_features(), 1000000, 1000000, 0,
7184 &low_our_to_self_config, 0, 42, None)
7187 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())); },
7188 _ => panic!("Unexpected event"),
7190 } else { assert!(false) }
7192 // We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in InboundV1Channel::new()
7193 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
7194 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7195 open_channel.to_self_delay = 200;
7196 if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7197 &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,
7198 &low_our_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7201 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())); },
7202 _ => panic!("Unexpected event"),
7204 } else { assert!(false); }
7206 // We test msg.to_self_delay <= config.their_to_self_delay is enforced in Chanel::accept_channel()
7207 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
7208 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()));
7209 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7210 accept_channel.to_self_delay = 200;
7211 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
7213 if let MessageSendEvent::HandleError { ref action, .. } = nodes[0].node.get_and_clear_pending_msg_events()[0] {
7215 &ErrorAction::SendErrorMessage { ref msg } => {
7216 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()));
7217 reason_msg = msg.data.clone();
7221 } else { panic!(); }
7222 check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: reason_msg }, [nodes[1].node.get_our_node_id()], 1000000);
7224 // We test msg.to_self_delay <= config.their_to_self_delay is enforced in InboundV1Channel::new()
7225 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 1000000, 1000000, 42, None, None).unwrap();
7226 let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
7227 open_channel.to_self_delay = 200;
7228 if let Err(error) = InboundV1Channel::new(&LowerBoundedFeeEstimator::new(&test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) }),
7229 &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,
7230 &high_their_to_self_config, 0, &nodes[0].logger, /*is_0conf=*/false)
7233 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())); },
7234 _ => panic!("Unexpected event"),
7236 } else { assert!(false); }
7240 fn test_check_htlc_underpaying() {
7241 // Send payment through A -> B but A is maliciously
7242 // sending a probe payment (i.e less than expected value0
7243 // to B, B should refuse payment.
7245 let chanmon_cfgs = create_chanmon_cfgs(2);
7246 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7247 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7248 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7250 // Create some initial channels
7251 create_announced_chan_between_nodes(&nodes, 0, 1);
7253 let scorer = test_utils::TestScorer::new();
7254 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7255 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
7256 .with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
7257 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 10_000);
7258 let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(),
7259 None, nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes).unwrap();
7260 let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
7261 let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, None).unwrap();
7262 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
7263 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
7264 check_added_monitors!(nodes[0], 1);
7266 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
7267 assert_eq!(events.len(), 1);
7268 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
7269 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
7270 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
7272 // Note that we first have to wait a random delay before processing the receipt of the HTLC,
7273 // and then will wait a second random delay before failing the HTLC back:
7274 expect_pending_htlcs_forwardable!(nodes[1]);
7275 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
7277 // Node 3 is expecting payment of 100_000 but received 10_000,
7278 // it should fail htlc like we didn't know the preimage.
7279 nodes[1].node.process_pending_htlc_forwards();
7281 let events = nodes[1].node.get_and_clear_pending_msg_events();
7282 assert_eq!(events.len(), 1);
7283 let (update_fail_htlc, commitment_signed) = match events[0] {
7284 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 } } => {
7285 assert!(update_add_htlcs.is_empty());
7286 assert!(update_fulfill_htlcs.is_empty());
7287 assert_eq!(update_fail_htlcs.len(), 1);
7288 assert!(update_fail_malformed_htlcs.is_empty());
7289 assert!(update_fee.is_none());
7290 (update_fail_htlcs[0].clone(), commitment_signed)
7292 _ => panic!("Unexpected event"),
7294 check_added_monitors!(nodes[1], 1);
7296 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlc);
7297 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false, true);
7299 // 10_000 msat as u64, followed by a height of CHAN_CONFIRM_DEPTH as u32
7300 let mut expected_failure_data = (10_000 as u64).to_be_bytes().to_vec();
7301 expected_failure_data.extend_from_slice(&CHAN_CONFIRM_DEPTH.to_be_bytes());
7302 expect_payment_failed!(nodes[0], our_payment_hash, true, 0x4000|15, &expected_failure_data[..]);
7306 fn test_announce_disable_channels() {
7307 // Create 2 channels between A and B. Disconnect B. Call timer_tick_occurred and check for generated
7308 // ChannelUpdate. Reconnect B, reestablish and check there is non-generated ChannelUpdate.
7310 let chanmon_cfgs = create_chanmon_cfgs(2);
7311 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7312 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7313 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7315 create_announced_chan_between_nodes(&nodes, 0, 1);
7316 create_announced_chan_between_nodes(&nodes, 1, 0);
7317 create_announced_chan_between_nodes(&nodes, 0, 1);
7320 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
7321 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
7323 for _ in 0..DISABLE_GOSSIP_TICKS + 1 {
7324 nodes[0].node.timer_tick_occurred();
7326 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7327 assert_eq!(msg_events.len(), 3);
7328 let mut chans_disabled = HashMap::new();
7329 for e in msg_events {
7331 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7332 assert_eq!(msg.contents.flags & (1<<1), 1<<1); // The "channel disabled" bit should be set
7333 // Check that each channel gets updated exactly once
7334 if chans_disabled.insert(msg.contents.short_channel_id, msg.contents.timestamp).is_some() {
7335 panic!("Generated ChannelUpdate for wrong chan!");
7338 _ => panic!("Unexpected event"),
7342 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init {
7343 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
7345 let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]);
7346 assert_eq!(reestablish_1.len(), 3);
7347 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init {
7348 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
7350 let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]);
7351 assert_eq!(reestablish_2.len(), 3);
7353 // Reestablish chan_1
7354 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]);
7355 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7356 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]);
7357 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7358 // Reestablish chan_2
7359 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[1]);
7360 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7361 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[1]);
7362 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7363 // Reestablish chan_3
7364 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[2]);
7365 handle_chan_reestablish_msgs!(nodes[0], nodes[1]);
7366 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[2]);
7367 handle_chan_reestablish_msgs!(nodes[1], nodes[0]);
7369 for _ in 0..ENABLE_GOSSIP_TICKS {
7370 nodes[0].node.timer_tick_occurred();
7372 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7373 nodes[0].node.timer_tick_occurred();
7374 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
7375 assert_eq!(msg_events.len(), 3);
7376 for e in msg_events {
7378 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
7379 assert_eq!(msg.contents.flags & (1<<1), 0); // The "channel disabled" bit should be off
7380 match chans_disabled.remove(&msg.contents.short_channel_id) {
7381 // Each update should have a higher timestamp than the previous one, replacing
7383 Some(prev_timestamp) => assert!(msg.contents.timestamp > prev_timestamp),
7384 None => panic!("Generated ChannelUpdate for wrong chan!"),
7387 _ => panic!("Unexpected event"),
7390 // Check that each channel gets updated exactly once
7391 assert!(chans_disabled.is_empty());
7395 fn test_bump_penalty_txn_on_revoked_commitment() {
7396 // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
7397 // we're able to claim outputs on revoked commitment transaction before timelocks expiration
7399 let chanmon_cfgs = create_chanmon_cfgs(2);
7400 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7401 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7402 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7404 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7406 let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
7407 let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 30)
7408 .with_bolt11_features(nodes[0].node.bolt11_invoice_features()).unwrap();
7409 let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000);
7410 send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
7412 let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
7413 // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7414 assert_eq!(revoked_txn[0].output.len(), 4);
7415 assert_eq!(revoked_txn[0].input.len(), 1);
7416 assert_eq!(revoked_txn[0].input[0].previous_output.txid, chan.3.txid());
7417 let revoked_txid = revoked_txn[0].txid();
7419 let mut penalty_sum = 0;
7420 for outp in revoked_txn[0].output.iter() {
7421 if outp.script_pubkey.is_v0_p2wsh() {
7422 penalty_sum += outp.value;
7426 // Connect blocks to change height_timer range to see if we use right soonest_timelock
7427 let header_114 = connect_blocks(&nodes[1], 14);
7429 // Actually revoke tx by claiming a HTLC
7430 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7431 connect_block(&nodes[1], &create_dummy_block(header_114, 42, vec![revoked_txn[0].clone()]));
7432 check_added_monitors!(nodes[1], 1);
7434 // One or more justice tx should have been broadcast, check it
7438 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7439 assert_eq!(node_txn.len(), 1); // justice tx (broadcasted from ChannelMonitor)
7440 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7441 assert_eq!(node_txn[0].output.len(), 1);
7442 check_spends!(node_txn[0], revoked_txn[0]);
7443 let fee_1 = penalty_sum - node_txn[0].output[0].value;
7444 feerate_1 = fee_1 * 1000 / node_txn[0].weight().to_wu();
7445 penalty_1 = node_txn[0].txid();
7449 // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it
7450 connect_blocks(&nodes[1], 15);
7451 let mut penalty_2 = penalty_1;
7452 let mut feerate_2 = 0;
7454 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7455 assert_eq!(node_txn.len(), 1);
7456 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7457 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7458 assert_eq!(node_txn[0].output.len(), 1);
7459 check_spends!(node_txn[0], revoked_txn[0]);
7460 penalty_2 = node_txn[0].txid();
7461 // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7462 assert_ne!(penalty_2, penalty_1);
7463 let fee_2 = penalty_sum - node_txn[0].output[0].value;
7464 feerate_2 = fee_2 * 1000 / node_txn[0].weight().to_wu();
7465 // Verify 25% bump heuristic
7466 assert!(feerate_2 * 100 >= feerate_1 * 125);
7470 assert_ne!(feerate_2, 0);
7472 // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it
7473 connect_blocks(&nodes[1], 1);
7475 let mut feerate_3 = 0;
7477 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7478 assert_eq!(node_txn.len(), 1);
7479 if node_txn[0].input[0].previous_output.txid == revoked_txid {
7480 assert_eq!(node_txn[0].input.len(), 3); // Penalty txn claims to_local, offered_htlc and received_htlc outputs
7481 assert_eq!(node_txn[0].output.len(), 1);
7482 check_spends!(node_txn[0], revoked_txn[0]);
7483 penalty_3 = node_txn[0].txid();
7484 // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
7485 assert_ne!(penalty_3, penalty_2);
7486 let fee_3 = penalty_sum - node_txn[0].output[0].value;
7487 feerate_3 = fee_3 * 1000 / node_txn[0].weight().to_wu();
7488 // Verify 25% bump heuristic
7489 assert!(feerate_3 * 100 >= feerate_2 * 125);
7493 assert_ne!(feerate_3, 0);
7495 nodes[1].node.get_and_clear_pending_events();
7496 nodes[1].node.get_and_clear_pending_msg_events();
7500 fn test_bump_penalty_txn_on_revoked_htlcs() {
7501 // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to sure
7502 // we're able to claim outputs on revoked HTLC transactions before timelocks expiration
7504 let mut chanmon_cfgs = create_chanmon_cfgs(2);
7505 chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
7506 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7507 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7508 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7510 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7511 // Lock HTLC in both directions (using a slightly lower CLTV delay to provide timely RBF bumps)
7512 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 50).with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
7513 let scorer = test_utils::TestScorer::new();
7514 let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
7515 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7516 let route = get_route(&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph.read_only(), None,
7517 nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes).unwrap();
7518 let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
7519 let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50)
7520 .with_bolt11_features(nodes[0].node.bolt11_invoice_features()).unwrap();
7521 let route_params = RouteParameters::from_payment_params_and_value(payment_params, 3_000_000);
7522 let route = get_route(&nodes[1].node.get_our_node_id(), &route_params, &nodes[1].network_graph.read_only(), None,
7523 nodes[0].logger, &scorer, &Default::default(), &random_seed_bytes).unwrap();
7524 send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
7526 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7527 assert_eq!(revoked_local_txn[0].input.len(), 1);
7528 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7530 // Revoke local commitment tx
7531 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
7533 // B will generate both revoked HTLC-timeout/HTLC-preimage txn from revoked commitment tx
7534 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone()]));
7535 check_closed_broadcast!(nodes[1], true);
7536 check_added_monitors!(nodes[1], 1);
7537 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 1000000);
7538 connect_blocks(&nodes[1], 50); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
7540 let revoked_htlc_txn = {
7541 let txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
7542 assert_eq!(txn.len(), 2);
7544 assert_eq!(txn[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
7545 assert_eq!(txn[0].input.len(), 1);
7546 check_spends!(txn[0], revoked_local_txn[0]);
7548 assert_eq!(txn[1].input.len(), 1);
7549 assert_eq!(txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
7550 assert_eq!(txn[1].output.len(), 1);
7551 check_spends!(txn[1], revoked_local_txn[0]);
7556 // Broadcast set of revoked txn on A
7557 let hash_128 = connect_blocks(&nodes[0], 40);
7558 let block_11 = create_dummy_block(hash_128, 42, vec![revoked_local_txn[0].clone()]);
7559 connect_block(&nodes[0], &block_11);
7560 let block_129 = create_dummy_block(block_11.block_hash(), 42, vec![revoked_htlc_txn[0].clone(), revoked_htlc_txn[1].clone()]);
7561 connect_block(&nodes[0], &block_129);
7562 let events = nodes[0].node.get_and_clear_pending_events();
7563 expect_pending_htlcs_forwardable_from_events!(nodes[0], events[0..1], true);
7564 match events.last().unwrap() {
7565 Event::ChannelClosed { reason: ClosureReason::CommitmentTxConfirmed, .. } => {}
7566 _ => panic!("Unexpected event"),
7572 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7573 assert_eq!(node_txn.len(), 4); // 3 penalty txn on revoked commitment tx + 1 penalty tnx on revoked HTLC txn
7574 // Verify claim tx are spending revoked HTLC txn
7576 // node_txn 0-2 each spend a separate revoked output from revoked_local_txn[0]
7577 // Note that node_txn[0] and node_txn[1] are bogus - they double spend the revoked_htlc_txn
7578 // which are included in the same block (they are broadcasted because we scan the
7579 // transactions linearly and generate claims as we go, they likely should be removed in the
7581 assert_eq!(node_txn[0].input.len(), 1);
7582 check_spends!(node_txn[0], revoked_local_txn[0]);
7583 assert_eq!(node_txn[1].input.len(), 1);
7584 check_spends!(node_txn[1], revoked_local_txn[0]);
7585 assert_eq!(node_txn[2].input.len(), 1);
7586 check_spends!(node_txn[2], revoked_local_txn[0]);
7588 // Each of the three justice transactions claim a separate (single) output of the three
7589 // available, which we check here:
7590 assert_ne!(node_txn[0].input[0].previous_output, node_txn[1].input[0].previous_output);
7591 assert_ne!(node_txn[0].input[0].previous_output, node_txn[2].input[0].previous_output);
7592 assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
7594 assert_eq!(node_txn[0].input[0].previous_output, revoked_htlc_txn[1].input[0].previous_output);
7595 assert_eq!(node_txn[1].input[0].previous_output, revoked_htlc_txn[0].input[0].previous_output);
7597 // node_txn[3] spends the revoked outputs from the revoked_htlc_txn (which only have one
7598 // output, checked above).
7599 assert_eq!(node_txn[3].input.len(), 2);
7600 assert_eq!(node_txn[3].output.len(), 1);
7601 check_spends!(node_txn[3], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7603 first = node_txn[3].txid();
7604 // Store both feerates for later comparison
7605 let fee_1 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[3].output[0].value;
7606 feerate_1 = fee_1 * 1000 / node_txn[3].weight().to_wu();
7607 penalty_txn = vec![node_txn[2].clone()];
7611 // Connect one more block to see if bumped penalty are issued for HTLC txn
7612 let block_130 = create_dummy_block(block_129.block_hash(), 42, penalty_txn);
7613 connect_block(&nodes[0], &block_130);
7614 let block_131 = create_dummy_block(block_130.block_hash(), 42, Vec::new());
7615 connect_block(&nodes[0], &block_131);
7617 // Few more blocks to confirm penalty txn
7618 connect_blocks(&nodes[0], 4);
7619 assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
7620 let header_144 = connect_blocks(&nodes[0], 9);
7622 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7623 assert_eq!(node_txn.len(), 1);
7625 assert_eq!(node_txn[0].input.len(), 2);
7626 check_spends!(node_txn[0], revoked_htlc_txn[0], revoked_htlc_txn[1]);
7627 // Verify bumped tx is different and 25% bump heuristic
7628 assert_ne!(first, node_txn[0].txid());
7629 let fee_2 = revoked_htlc_txn[0].output[0].value + revoked_htlc_txn[1].output[0].value - node_txn[0].output[0].value;
7630 let feerate_2 = fee_2 * 1000 / node_txn[0].weight().to_wu();
7631 assert!(feerate_2 * 100 > feerate_1 * 125);
7632 let txn = vec![node_txn[0].clone()];
7636 // Broadcast claim txn and confirm blocks to avoid further bumps on this outputs
7637 connect_block(&nodes[0], &create_dummy_block(header_144, 42, node_txn));
7638 connect_blocks(&nodes[0], 20);
7640 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7641 // We verify than no new transaction has been broadcast because previously
7642 // we were buggy on this exact behavior by not tracking for monitoring remote HTLC outputs (see #411)
7643 // which means we wouldn't see a spend of them by a justice tx and bumped justice tx
7644 // were generated forever instead of safe cleaning after confirmation and ANTI_REORG_SAFE_DELAY blocks.
7645 // Enforce spending of revoked htlc output by claiming transaction remove request as expected and dry
7646 // up bumped justice generation.
7647 assert_eq!(node_txn.len(), 0);
7650 check_closed_broadcast!(nodes[0], true);
7651 check_added_monitors!(nodes[0], 1);
7655 fn test_bump_penalty_txn_on_remote_commitment() {
7656 // In case of claim txn with too low feerates for getting into mempools, RBF-bump them to be sure
7657 // we're able to claim outputs on remote commitment transaction before timelocks expiration
7660 // Provide preimage for one
7661 // Check aggregation
7663 let chanmon_cfgs = create_chanmon_cfgs(2);
7664 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7665 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7666 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7668 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7669 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 3_000_000);
7670 route_payment(&nodes[1], &vec!(&nodes[0])[..], 3000000).0;
7672 // Remote commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
7673 let remote_txn = get_local_commitment_txn!(nodes[0], chan.2);
7674 assert_eq!(remote_txn[0].output.len(), 4);
7675 assert_eq!(remote_txn[0].input.len(), 1);
7676 assert_eq!(remote_txn[0].input[0].previous_output.txid, chan.3.txid());
7678 // Claim a HTLC without revocation (provide B monitor with preimage)
7679 nodes[1].node.claim_funds(payment_preimage);
7680 expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
7681 mine_transaction(&nodes[1], &remote_txn[0]);
7682 check_added_monitors!(nodes[1], 2);
7683 connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
7685 // One or more claim tx should have been broadcast, check it
7689 let feerate_timeout;
7690 let feerate_preimage;
7692 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7693 // 3 transactions including:
7694 // preimage and timeout sweeps from remote commitment + preimage sweep bump
7695 assert_eq!(node_txn.len(), 3);
7696 assert_eq!(node_txn[0].input.len(), 1);
7697 assert_eq!(node_txn[1].input.len(), 1);
7698 assert_eq!(node_txn[2].input.len(), 1);
7699 check_spends!(node_txn[0], remote_txn[0]);
7700 check_spends!(node_txn[1], remote_txn[0]);
7701 check_spends!(node_txn[2], remote_txn[0]);
7703 preimage = node_txn[0].txid();
7704 let index = node_txn[0].input[0].previous_output.vout;
7705 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7706 feerate_preimage = fee * 1000 / node_txn[0].weight().to_wu();
7708 let (preimage_bump_tx, timeout_tx) = if node_txn[2].input[0].previous_output == node_txn[0].input[0].previous_output {
7709 (node_txn[2].clone(), node_txn[1].clone())
7711 (node_txn[1].clone(), node_txn[2].clone())
7714 preimage_bump = preimage_bump_tx;
7715 check_spends!(preimage_bump, remote_txn[0]);
7716 assert_eq!(node_txn[0].input[0].previous_output, preimage_bump.input[0].previous_output);
7718 timeout = timeout_tx.txid();
7719 let index = timeout_tx.input[0].previous_output.vout;
7720 let fee = remote_txn[0].output[index as usize].value - timeout_tx.output[0].value;
7721 feerate_timeout = fee * 1000 / timeout_tx.weight().to_wu();
7725 assert_ne!(feerate_timeout, 0);
7726 assert_ne!(feerate_preimage, 0);
7728 // After exhaustion of height timer, new bumped claim txn should have been broadcast, check it
7729 connect_blocks(&nodes[1], 1);
7731 let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
7732 assert_eq!(node_txn.len(), 1);
7733 assert_eq!(node_txn[0].input.len(), 1);
7734 assert_eq!(preimage_bump.input.len(), 1);
7735 check_spends!(node_txn[0], remote_txn[0]);
7736 check_spends!(preimage_bump, remote_txn[0]);
7738 let index = preimage_bump.input[0].previous_output.vout;
7739 let fee = remote_txn[0].output[index as usize].value - preimage_bump.output[0].value;
7740 let new_feerate = fee * 1000 / preimage_bump.weight().to_wu();
7741 assert!(new_feerate * 100 > feerate_timeout * 125);
7742 assert_ne!(timeout, preimage_bump.txid());
7744 let index = node_txn[0].input[0].previous_output.vout;
7745 let fee = remote_txn[0].output[index as usize].value - node_txn[0].output[0].value;
7746 let new_feerate = fee * 1000 / node_txn[0].weight().to_wu();
7747 assert!(new_feerate * 100 > feerate_preimage * 125);
7748 assert_ne!(preimage, node_txn[0].txid());
7753 nodes[1].node.get_and_clear_pending_events();
7754 nodes[1].node.get_and_clear_pending_msg_events();
7758 fn test_counterparty_raa_skip_no_crash() {
7759 // Previously, if our counterparty sent two RAAs in a row without us having provided a
7760 // commitment transaction, we would have happily carried on and provided them the next
7761 // commitment transaction based on one RAA forward. This would probably eventually have led to
7762 // channel closure, but it would not have resulted in funds loss. Still, our
7763 // TestChannelSigner would have panicked as it doesn't like jumps into the future. Here, we
7764 // check simply that the channel is closed in response to such an RAA, but don't check whether
7765 // we decide to punish our counterparty for revoking their funds (as we don't currently
7767 let chanmon_cfgs = create_chanmon_cfgs(2);
7768 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7769 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7770 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7771 let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
7773 let per_commitment_secret;
7774 let next_per_commitment_point;
7776 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
7777 let mut guard = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
7778 let keys = guard.channel_by_id.get_mut(&channel_id).map(
7779 |phase| if let ChannelPhase::Funded(chan) = phase { Some(chan) } else { None }
7780 ).flatten().unwrap().get_signer();
7782 const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
7784 // Make signer believe we got a counterparty signature, so that it allows the revocation
7785 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7786 per_commitment_secret = keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER);
7788 // Must revoke without gaps
7789 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7790 keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
7792 keys.as_ecdsa().unwrap().get_enforcement_state().last_holder_commitment -= 1;
7793 next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
7794 &SecretKey::from_slice(&keys.as_ref().release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
7797 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
7798 &msgs::RevokeAndACK {
7800 per_commitment_secret,
7801 next_per_commitment_point,
7803 next_local_nonce: None,
7805 assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
7806 check_added_monitors!(nodes[1], 1);
7807 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() }
7808 , [nodes[0].node.get_our_node_id()], 100000);
7812 fn test_bump_txn_sanitize_tracking_maps() {
7813 // Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,
7814 // verify we clean then right after expiration of ANTI_REORG_DELAY.
7816 let chanmon_cfgs = create_chanmon_cfgs(2);
7817 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7818 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7819 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7821 let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000);
7822 // Lock HTLC in both directions
7823 let (payment_preimage_1, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000);
7824 let (_, payment_hash_2, ..) = route_payment(&nodes[1], &vec!(&nodes[0])[..], 9_000_000);
7826 let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
7827 assert_eq!(revoked_local_txn[0].input.len(), 1);
7828 assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, chan.3.txid());
7830 // Revoke local commitment tx
7831 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1);
7833 // Broadcast set of revoked txn on A
7834 connect_blocks(&nodes[0], TEST_FINAL_CLTV + 2 - CHAN_CONFIRM_DEPTH);
7835 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[0], vec![HTLCDestination::FailedPayment { payment_hash: payment_hash_2 }]);
7836 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
7838 mine_transaction(&nodes[0], &revoked_local_txn[0]);
7839 check_closed_broadcast!(nodes[0], true);
7840 check_added_monitors!(nodes[0], 1);
7841 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 1000000);
7843 let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
7844 assert_eq!(node_txn.len(), 3); //ChannelMonitor: justice txn * 3
7845 check_spends!(node_txn[0], revoked_local_txn[0]);
7846 check_spends!(node_txn[1], revoked_local_txn[0]);
7847 check_spends!(node_txn[2], revoked_local_txn[0]);
7848 let penalty_txn = vec![node_txn[0].clone(), node_txn[1].clone(), node_txn[2].clone()];
7852 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, penalty_txn));
7853 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
7855 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(OutPoint { txid: chan.3.txid(), index: 0 }).unwrap();
7856 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.pending_claim_requests.is_empty());
7857 assert!(monitor.inner.lock().unwrap().onchain_tx_handler.claimable_outpoints.is_empty());
7862 fn test_channel_conf_timeout() {
7863 // Tests that, for inbound channels, we give up on them if the funding transaction does not
7864 // confirm within 2016 blocks, as recommended by BOLT 2.
7865 let chanmon_cfgs = create_chanmon_cfgs(2);
7866 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7867 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7868 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7870 let _funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 100_000);
7872 // The outbound node should wait forever for confirmation:
7873 // This matches `channel::FUNDING_CONF_DEADLINE_BLOCKS` and BOLT 2's suggested timeout, thus is
7874 // copied here instead of directly referencing the constant.
7875 connect_blocks(&nodes[0], 2016);
7876 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7878 // The inbound node should fail the channel after exactly 2016 blocks
7879 connect_blocks(&nodes[1], 2015);
7880 check_added_monitors!(nodes[1], 0);
7881 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
7883 connect_blocks(&nodes[1], 1);
7884 check_added_monitors!(nodes[1], 1);
7885 check_closed_event!(nodes[1], 1, ClosureReason::FundingTimedOut, [nodes[0].node.get_our_node_id()], 1000000);
7886 let close_ev = nodes[1].node.get_and_clear_pending_msg_events();
7887 assert_eq!(close_ev.len(), 1);
7889 MessageSendEvent::HandleError { action: ErrorAction::DisconnectPeer { ref msg }, ref node_id } => {
7890 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
7891 assert_eq!(msg.as_ref().unwrap().data, "Channel closed because funding transaction failed to confirm within 2016 blocks");
7893 _ => panic!("Unexpected event"),
7898 fn test_override_channel_config() {
7899 let chanmon_cfgs = create_chanmon_cfgs(2);
7900 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7901 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
7902 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7904 // Node0 initiates a channel to node1 using the override config.
7905 let mut override_config = UserConfig::default();
7906 override_config.channel_handshake_config.our_to_self_delay = 200;
7908 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, None, Some(override_config)).unwrap();
7910 // Assert the channel created by node0 is using the override config.
7911 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7912 assert_eq!(res.channel_flags, 0);
7913 assert_eq!(res.to_self_delay, 200);
7917 fn test_override_0msat_htlc_minimum() {
7918 let mut zero_config = UserConfig::default();
7919 zero_config.channel_handshake_config.our_htlc_minimum_msat = 0;
7920 let chanmon_cfgs = create_chanmon_cfgs(2);
7921 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7922 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(zero_config.clone())]);
7923 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7925 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 16_000_000, 12_000_000, 42, None, Some(zero_config)).unwrap();
7926 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7927 assert_eq!(res.htlc_minimum_msat, 1);
7929 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
7930 let res = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
7931 assert_eq!(res.htlc_minimum_msat, 1);
7935 fn test_channel_update_has_correct_htlc_maximum_msat() {
7936 // Tests that the `ChannelUpdate` message has the correct values for `htlc_maximum_msat` set.
7937 // Bolt 7 specifies that if present `htlc_maximum_msat`:
7938 // 1. MUST be set to less than or equal to the channel capacity. In LDK, this is capped to
7939 // 90% of the `channel_value`.
7940 // 2. MUST be set to less than or equal to the `max_htlc_value_in_flight_msat` received from the peer.
7942 let mut config_30_percent = UserConfig::default();
7943 config_30_percent.channel_handshake_config.announced_channel = true;
7944 config_30_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 30;
7945 let mut config_50_percent = UserConfig::default();
7946 config_50_percent.channel_handshake_config.announced_channel = true;
7947 config_50_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 50;
7948 let mut config_95_percent = UserConfig::default();
7949 config_95_percent.channel_handshake_config.announced_channel = true;
7950 config_95_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 95;
7951 let mut config_100_percent = UserConfig::default();
7952 config_100_percent.channel_handshake_config.announced_channel = true;
7953 config_100_percent.channel_handshake_config.max_inbound_htlc_value_in_flight_percent_of_channel = 100;
7955 let chanmon_cfgs = create_chanmon_cfgs(4);
7956 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
7957 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)]);
7958 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
7960 let channel_value_satoshis = 100000;
7961 let channel_value_msat = channel_value_satoshis * 1000;
7962 let channel_value_30_percent_msat = (channel_value_msat as f64 * 0.3) as u64;
7963 let channel_value_50_percent_msat = (channel_value_msat as f64 * 0.5) as u64;
7964 let channel_value_90_percent_msat = (channel_value_msat as f64 * 0.9) as u64;
7966 let (node_0_chan_update, node_1_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, channel_value_satoshis, 10001);
7967 let (node_2_chan_update, node_3_chan_update, _, _) = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, channel_value_satoshis, 10001);
7969 // Assert that `node[0]`'s `ChannelUpdate` is capped at 50 percent of the `channel_value`, as
7970 // that's the value of `node[1]`'s `holder_max_htlc_value_in_flight_msat`.
7971 assert_eq!(node_0_chan_update.contents.htlc_maximum_msat, channel_value_50_percent_msat);
7972 // Assert that `node[1]`'s `ChannelUpdate` is capped at 30 percent of the `channel_value`, as
7973 // that's the value of `node[0]`'s `holder_max_htlc_value_in_flight_msat`.
7974 assert_eq!(node_1_chan_update.contents.htlc_maximum_msat, channel_value_30_percent_msat);
7976 // Assert that `node[2]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7977 // the value of `node[3]`'s `holder_max_htlc_value_in_flight_msat` (100%), exceeds 90% of the
7979 assert_eq!(node_2_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7980 // Assert that `node[3]`'s `ChannelUpdate` is capped at 90 percent of the `channel_value`, as
7981 // the value of `node[2]`'s `holder_max_htlc_value_in_flight_msat` (95%), exceeds 90% of the
7983 assert_eq!(node_3_chan_update.contents.htlc_maximum_msat, channel_value_90_percent_msat);
7987 fn test_manually_accept_inbound_channel_request() {
7988 let mut manually_accept_conf = UserConfig::default();
7989 manually_accept_conf.manually_accept_inbound_channels = true;
7990 let chanmon_cfgs = create_chanmon_cfgs(2);
7991 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
7992 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
7993 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
7995 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(manually_accept_conf)).unwrap();
7996 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
7998 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8000 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8001 // accepting the inbound channel request.
8002 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8004 let events = nodes[1].node.get_and_clear_pending_events();
8006 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8007 nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap();
8009 _ => panic!("Unexpected event"),
8012 let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8013 assert_eq!(accept_msg_ev.len(), 1);
8015 match accept_msg_ev[0] {
8016 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8017 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8019 _ => panic!("Unexpected event"),
8022 nodes[1].node.force_close_broadcasting_latest_txn(&temp_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
8024 let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8025 assert_eq!(close_msg_ev.len(), 1);
8027 let events = nodes[1].node.get_and_clear_pending_events();
8029 Event::ChannelClosed { user_channel_id, .. } => {
8030 assert_eq!(user_channel_id, 23);
8032 _ => panic!("Unexpected event"),
8037 fn test_manually_reject_inbound_channel_request() {
8038 let mut manually_accept_conf = UserConfig::default();
8039 manually_accept_conf.manually_accept_inbound_channels = true;
8040 let chanmon_cfgs = create_chanmon_cfgs(2);
8041 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8042 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8043 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8045 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(manually_accept_conf)).unwrap();
8046 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8048 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8050 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8051 // rejecting the inbound channel request.
8052 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8054 let events = nodes[1].node.get_and_clear_pending_events();
8056 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8057 nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap();
8059 _ => panic!("Unexpected event"),
8062 let close_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8063 assert_eq!(close_msg_ev.len(), 1);
8065 match close_msg_ev[0] {
8066 MessageSendEvent::HandleError { ref node_id, .. } => {
8067 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8069 _ => panic!("Unexpected event"),
8072 // There should be no more events to process, as the channel was never opened.
8073 assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
8077 fn test_can_not_accept_inbound_channel_twice() {
8078 let mut manually_accept_conf = UserConfig::default();
8079 manually_accept_conf.manually_accept_inbound_channels = true;
8080 let chanmon_cfgs = create_chanmon_cfgs(2);
8081 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8082 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf.clone())]);
8083 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8085 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(manually_accept_conf)).unwrap();
8086 let res = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8088 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &res);
8090 // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in `msg_events` before
8091 // accepting the inbound channel request.
8092 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
8094 let events = nodes[1].node.get_and_clear_pending_events();
8096 Event::OpenChannelRequest { temporary_channel_id, .. } => {
8097 nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
8098 let api_res = nodes[1].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].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!("Channel shouldn't be possible to be accepted twice"),
8104 Err(e) => panic!("Unexpected Error {:?}", e),
8107 _ => panic!("Unexpected event"),
8110 // Ensure that the channel wasn't closed after attempting to accept it twice.
8111 let accept_msg_ev = nodes[1].node.get_and_clear_pending_msg_events();
8112 assert_eq!(accept_msg_ev.len(), 1);
8114 match accept_msg_ev[0] {
8115 MessageSendEvent::SendAcceptChannel { ref node_id, .. } => {
8116 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8118 _ => panic!("Unexpected event"),
8123 fn test_can_not_accept_unknown_inbound_channel() {
8124 let chanmon_cfg = create_chanmon_cfgs(2);
8125 let node_cfg = create_node_cfgs(2, &chanmon_cfg);
8126 let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
8127 let nodes = create_network(2, &node_cfg, &node_chanmgr);
8129 let unknown_channel_id = ChannelId::new_zero();
8130 let api_res = nodes[0].node.accept_inbound_channel(&unknown_channel_id, &nodes[1].node.get_our_node_id(), 0);
8132 Err(APIError::APIMisuseError { err }) => {
8133 assert_eq!(err, "No such channel awaiting to be accepted.");
8135 Ok(_) => panic!("It shouldn't be possible to accept an unkown channel"),
8136 Err(e) => panic!("Unexpected Error: {:?}", e),
8141 fn test_onion_value_mpp_set_calculation() {
8142 // Test that we use the onion value `amt_to_forward` when
8143 // calculating whether we've reached the `total_msat` of an MPP
8144 // by having a routing node forward more than `amt_to_forward`
8145 // and checking that the receiving node doesn't generate
8146 // a PaymentClaimable event too early
8148 let chanmon_cfgs = create_chanmon_cfgs(node_count);
8149 let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8150 let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8151 let mut nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8153 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8154 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8155 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8156 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8158 let total_msat = 100_000;
8159 let expected_paths: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
8160 let (mut route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], total_msat);
8161 let sample_path = route.paths.pop().unwrap();
8163 let mut path_1 = sample_path.clone();
8164 path_1.hops[0].pubkey = nodes[1].node.get_our_node_id();
8165 path_1.hops[0].short_channel_id = chan_1_id;
8166 path_1.hops[1].pubkey = nodes[3].node.get_our_node_id();
8167 path_1.hops[1].short_channel_id = chan_3_id;
8168 path_1.hops[1].fee_msat = 100_000;
8169 route.paths.push(path_1);
8171 let mut path_2 = sample_path.clone();
8172 path_2.hops[0].pubkey = nodes[2].node.get_our_node_id();
8173 path_2.hops[0].short_channel_id = chan_2_id;
8174 path_2.hops[1].pubkey = nodes[3].node.get_our_node_id();
8175 path_2.hops[1].short_channel_id = chan_4_id;
8176 path_2.hops[1].fee_msat = 1_000;
8177 route.paths.push(path_2);
8180 let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
8181 let onion_session_privs = nodes[0].node.test_add_new_pending_payment(our_payment_hash,
8182 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8183 nodes[0].node.test_send_payment_internal(&route, our_payment_hash,
8184 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8185 check_added_monitors!(nodes[0], expected_paths.len());
8187 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8188 assert_eq!(events.len(), expected_paths.len());
8191 let ev = remove_first_msg_event_to_node(&expected_paths[0][0].node.get_our_node_id(), &mut events);
8192 let mut payment_event = SendEvent::from_event(ev);
8193 let mut prev_node = &nodes[0];
8195 for (idx, &node) in expected_paths[0].iter().enumerate() {
8196 assert_eq!(node.node.get_our_node_id(), payment_event.node_id);
8198 if idx == 0 { // routing node
8199 let session_priv = [3; 32];
8200 let height = nodes[0].best_block_info().1;
8201 let session_priv = SecretKey::from_slice(&session_priv).unwrap();
8202 let mut onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
8203 let (mut onion_payloads, _, _) = onion_utils::build_onion_payloads(&route.paths[0], 100_000,
8204 RecipientOnionFields::secret_only(our_payment_secret), height + 1, &None).unwrap();
8205 // Edit amt_to_forward to simulate the sender having set
8206 // the final amount and the routing node taking less fee
8207 if let msgs::OutboundOnionPayload::Receive { ref mut amt_msat, .. } = onion_payloads[1] {
8210 let new_onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &our_payment_hash).unwrap();
8211 payment_event.msgs[0].onion_routing_packet = new_onion_packet;
8214 node.node.handle_update_add_htlc(&prev_node.node.get_our_node_id(), &payment_event.msgs[0]);
8215 check_added_monitors!(node, 0);
8216 commitment_signed_dance!(node, prev_node, payment_event.commitment_msg, false);
8217 expect_pending_htlcs_forwardable!(node);
8220 let mut events_2 = node.node.get_and_clear_pending_msg_events();
8221 assert_eq!(events_2.len(), 1);
8222 check_added_monitors!(node, 1);
8223 payment_event = SendEvent::from_event(events_2.remove(0));
8224 assert_eq!(payment_event.msgs.len(), 1);
8226 let events_2 = node.node.get_and_clear_pending_events();
8227 assert!(events_2.is_empty());
8234 let ev = remove_first_msg_event_to_node(&expected_paths[1][0].node.get_our_node_id(), &mut events);
8235 pass_along_path(&nodes[0], expected_paths[1], 101_000, our_payment_hash.clone(), Some(our_payment_secret), ev, true, None);
8237 claim_payment_along_route(&nodes[0], expected_paths, false, our_payment_preimage);
8240 fn do_test_overshoot_mpp(msat_amounts: &[u64], total_msat: u64) {
8242 let routing_node_count = msat_amounts.len();
8243 let node_count = routing_node_count + 2;
8245 let chanmon_cfgs = create_chanmon_cfgs(node_count);
8246 let node_cfgs = create_node_cfgs(node_count, &chanmon_cfgs);
8247 let node_chanmgrs = create_node_chanmgrs(node_count, &node_cfgs, &vec![None; node_count]);
8248 let nodes = create_network(node_count, &node_cfgs, &node_chanmgrs);
8253 // Create channels for each amount
8254 let mut expected_paths = Vec::with_capacity(routing_node_count);
8255 let mut src_chan_ids = Vec::with_capacity(routing_node_count);
8256 let mut dst_chan_ids = Vec::with_capacity(routing_node_count);
8257 for i in 0..routing_node_count {
8258 let routing_node = 2 + i;
8259 let src_chan_id = create_announced_chan_between_nodes(&nodes, src_idx, routing_node).0.contents.short_channel_id;
8260 src_chan_ids.push(src_chan_id);
8261 let dst_chan_id = create_announced_chan_between_nodes(&nodes, routing_node, dst_idx).0.contents.short_channel_id;
8262 dst_chan_ids.push(dst_chan_id);
8263 let path = vec![&nodes[routing_node], &nodes[dst_idx]];
8264 expected_paths.push(path);
8266 let expected_paths: Vec<&[&Node]> = expected_paths.iter().map(|route| route.as_slice()).collect();
8268 // Create a route for each amount
8269 let example_amount = 100000;
8270 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);
8271 let sample_path = route.paths.pop().unwrap();
8272 for i in 0..routing_node_count {
8273 let routing_node = 2 + i;
8274 let mut path = sample_path.clone();
8275 path.hops[0].pubkey = nodes[routing_node].node.get_our_node_id();
8276 path.hops[0].short_channel_id = src_chan_ids[i];
8277 path.hops[1].pubkey = nodes[dst_idx].node.get_our_node_id();
8278 path.hops[1].short_channel_id = dst_chan_ids[i];
8279 path.hops[1].fee_msat = msat_amounts[i];
8280 route.paths.push(path);
8283 // Send payment with manually set total_msat
8284 let payment_id = PaymentId(nodes[src_idx].keys_manager.backing.get_secure_random_bytes());
8285 let onion_session_privs = nodes[src_idx].node.test_add_new_pending_payment(our_payment_hash,
8286 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &route).unwrap();
8287 nodes[src_idx].node.test_send_payment_internal(&route, our_payment_hash,
8288 RecipientOnionFields::secret_only(our_payment_secret), None, payment_id, Some(total_msat), onion_session_privs).unwrap();
8289 check_added_monitors!(nodes[src_idx], expected_paths.len());
8291 let mut events = nodes[src_idx].node.get_and_clear_pending_msg_events();
8292 assert_eq!(events.len(), expected_paths.len());
8293 let mut amount_received = 0;
8294 for (path_idx, expected_path) in expected_paths.iter().enumerate() {
8295 let ev = remove_first_msg_event_to_node(&expected_path[0].node.get_our_node_id(), &mut events);
8297 let current_path_amount = msat_amounts[path_idx];
8298 amount_received += current_path_amount;
8299 let became_claimable_now = amount_received >= total_msat && amount_received - current_path_amount < total_msat;
8300 pass_along_path(&nodes[src_idx], expected_path, amount_received, our_payment_hash.clone(), Some(our_payment_secret), ev, became_claimable_now, None);
8303 claim_payment_along_route(&nodes[src_idx], &expected_paths, false, our_payment_preimage);
8307 fn test_overshoot_mpp() {
8308 do_test_overshoot_mpp(&[100_000, 101_000], 200_000);
8309 do_test_overshoot_mpp(&[100_000, 10_000, 100_000], 200_000);
8313 fn test_simple_mpp() {
8314 // Simple test of sending a multi-path payment.
8315 let chanmon_cfgs = create_chanmon_cfgs(4);
8316 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
8317 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
8318 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
8320 let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8321 let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2).0.contents.short_channel_id;
8322 let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents.short_channel_id;
8323 let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3).0.contents.short_channel_id;
8325 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
8326 let path = route.paths[0].clone();
8327 route.paths.push(path);
8328 route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
8329 route.paths[0].hops[0].short_channel_id = chan_1_id;
8330 route.paths[0].hops[1].short_channel_id = chan_3_id;
8331 route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
8332 route.paths[1].hops[0].short_channel_id = chan_2_id;
8333 route.paths[1].hops[1].short_channel_id = chan_4_id;
8334 send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
8335 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
8339 fn test_preimage_storage() {
8340 // Simple test of payment preimage storage allowing no client-side storage to claim payments
8341 let chanmon_cfgs = create_chanmon_cfgs(2);
8342 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8343 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8344 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8346 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8349 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 7200, None).unwrap();
8350 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8351 nodes[0].node.send_payment_with_route(&route, payment_hash,
8352 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8353 check_added_monitors!(nodes[0], 1);
8354 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8355 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
8356 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8357 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8359 // Note that after leaving the above scope we have no knowledge of any arguments or return
8360 // values from previous calls.
8361 expect_pending_htlcs_forwardable!(nodes[1]);
8362 let events = nodes[1].node.get_and_clear_pending_events();
8363 assert_eq!(events.len(), 1);
8365 Event::PaymentClaimable { ref purpose, .. } => {
8367 PaymentPurpose::InvoicePayment { payment_preimage, .. } => {
8368 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage.unwrap());
8370 _ => panic!("expected PaymentPurpose::InvoicePayment")
8373 _ => panic!("Unexpected event"),
8378 fn test_bad_secret_hash() {
8379 // Simple test of unregistered payment hash/invalid payment secret handling
8380 let chanmon_cfgs = create_chanmon_cfgs(2);
8381 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8382 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8383 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8385 create_announced_chan_between_nodes(&nodes, 0, 1).0.contents.short_channel_id;
8387 let random_payment_hash = PaymentHash([42; 32]);
8388 let random_payment_secret = PaymentSecret([43; 32]);
8389 let (our_payment_hash, our_payment_secret) = nodes[1].node.create_inbound_payment(Some(100_000), 2, None).unwrap();
8390 let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
8392 // All the below cases should end up being handled exactly identically, so we macro the
8393 // resulting events.
8394 macro_rules! handle_unknown_invalid_payment_data {
8395 ($payment_hash: expr) => {
8396 check_added_monitors!(nodes[0], 1);
8397 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8398 let payment_event = SendEvent::from_event(events.pop().unwrap());
8399 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8400 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8402 // We have to forward pending HTLCs once to process the receipt of the HTLC and then
8403 // again to process the pending backwards-failure of the HTLC
8404 expect_pending_htlcs_forwardable!(nodes[1]);
8405 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment{ payment_hash: $payment_hash }]);
8406 check_added_monitors!(nodes[1], 1);
8408 // We should fail the payment back
8409 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
8410 match events.pop().unwrap() {
8411 MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8412 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
8413 commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
8415 _ => panic!("Unexpected event"),
8420 let expected_error_code = 0x4000|15; // incorrect_or_unknown_payment_details
8421 // Error data is the HTLC value (100,000) and current block height
8422 let expected_error_data = [0, 0, 0, 0, 0, 1, 0x86, 0xa0, 0, 0, 0, CHAN_CONFIRM_DEPTH as u8];
8424 // Send a payment with the right payment hash but the wrong payment secret
8425 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
8426 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
8427 handle_unknown_invalid_payment_data!(our_payment_hash);
8428 expect_payment_failed!(nodes[0], our_payment_hash, true, expected_error_code, expected_error_data);
8430 // Send a payment with a random payment hash, but the right payment secret
8431 nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8432 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8433 handle_unknown_invalid_payment_data!(random_payment_hash);
8434 expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8436 // Send a payment with a random payment hash and random payment secret
8437 nodes[0].node.send_payment_with_route(&route, random_payment_hash,
8438 RecipientOnionFields::secret_only(random_payment_secret), PaymentId(random_payment_hash.0)).unwrap();
8439 handle_unknown_invalid_payment_data!(random_payment_hash);
8440 expect_payment_failed!(nodes[0], random_payment_hash, true, expected_error_code, expected_error_data);
8444 fn test_update_err_monitor_lockdown() {
8445 // Our monitor will lock update of local commitment transaction if a broadcastion condition
8446 // has been fulfilled (either force-close from Channel or block height requiring a HTLC-
8447 // timeout). Trying to update monitor after lockdown should return a ChannelMonitorUpdateStatus
8450 // This scenario may happen in a watchtower setup, where watchtower process a block height
8451 // triggering a timeout while a slow-block-processing ChannelManager receives a local signed
8452 // commitment at same time.
8454 let chanmon_cfgs = create_chanmon_cfgs(2);
8455 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8456 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8457 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8459 // Create some initial channel
8460 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8461 let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8463 // Rebalance the network to generate htlc in the two directions
8464 send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8466 // Route a HTLC from node 0 to node 1 (but don't settle)
8467 let (preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 9_000_000);
8469 // Copy ChainMonitor to simulate a watchtower and update block height of node 0 until its ChannelMonitor timeout HTLC onchain
8470 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8471 let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
8472 let persister = test_utils::TestPersister::new();
8475 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8476 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8477 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8478 assert!(new_monitor == *monitor);
8481 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);
8482 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), Ok(ChannelMonitorUpdateStatus::Completed));
8485 let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8486 // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8487 // transaction lock time requirements here.
8488 chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 200));
8489 watchtower.chain_monitor.block_connected(&block, 200);
8491 // Try to update ChannelMonitor
8492 nodes[1].node.claim_funds(preimage);
8493 check_added_monitors!(nodes[1], 1);
8494 expect_payment_claimed!(nodes[1], payment_hash, 9_000_000);
8496 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8497 assert_eq!(updates.update_fulfill_htlcs.len(), 1);
8498 nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
8500 let mut node_0_per_peer_lock;
8501 let mut node_0_peer_state_lock;
8502 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) {
8503 if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8504 assert_eq!(watchtower.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::InProgress);
8505 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8506 } else { assert!(false); }
8511 // Our local monitor is in-sync and hasn't processed yet timeout
8512 check_added_monitors!(nodes[0], 1);
8513 let events = nodes[0].node.get_and_clear_pending_events();
8514 assert_eq!(events.len(), 1);
8518 fn test_concurrent_monitor_claim() {
8519 // Watchtower A receives block, broadcasts state N, then channel receives new state N+1,
8520 // sending it to both watchtowers, Bob accepts N+1, then receives block and broadcasts
8521 // the latest state N+1, Alice rejects state N+1, but Bob has already broadcast it,
8522 // state N+1 confirms. Alice claims output from state N+1.
8524 let chanmon_cfgs = create_chanmon_cfgs(2);
8525 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8526 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8527 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8529 // Create some initial channel
8530 let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1);
8531 let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 };
8533 // Rebalance the network to generate htlc in the two directions
8534 send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000);
8536 // Route a HTLC from node 0 to node 1 (but don't settle)
8537 route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0;
8539 // Copy ChainMonitor to simulate watchtower Alice and update block height her ChannelMonitor timeout HTLC onchain
8540 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8541 let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
8542 let persister = test_utils::TestPersister::new();
8543 let alice_broadcaster = test_utils::TestBroadcaster::with_blocks(
8544 Arc::new(Mutex::new(nodes[0].blocks.lock().unwrap().clone())),
8546 let watchtower_alice = {
8548 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8549 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8550 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8551 assert!(new_monitor == *monitor);
8554 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &alice_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8555 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), Ok(ChannelMonitorUpdateStatus::Completed));
8558 let block = create_dummy_block(BlockHash::all_zeros(), 42, Vec::new());
8559 // Make Alice aware of enough blocks that it doesn't think we're violating transaction lock time
8560 // requirements here.
8561 const HTLC_TIMEOUT_BROADCAST: u32 = CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS;
8562 alice_broadcaster.blocks.lock().unwrap().resize((HTLC_TIMEOUT_BROADCAST) as usize, (block.clone(), HTLC_TIMEOUT_BROADCAST));
8563 watchtower_alice.chain_monitor.block_connected(&block, HTLC_TIMEOUT_BROADCAST);
8565 // Watchtower Alice should have broadcast a commitment/HTLC-timeout
8567 let mut txn = alice_broadcaster.txn_broadcast();
8568 assert_eq!(txn.len(), 2);
8572 // Copy ChainMonitor to simulate watchtower Bob and make it receive a commitment update first.
8573 let chain_source = test_utils::TestChainSource::new(Network::Testnet);
8574 let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
8575 let persister = test_utils::TestPersister::new();
8576 let bob_broadcaster = test_utils::TestBroadcaster::with_blocks(Arc::clone(&alice_broadcaster.blocks));
8577 let watchtower_bob = {
8579 let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
8580 let new_monitor = <(BlockHash, channelmonitor::ChannelMonitor<TestChannelSigner>)>::read(
8581 &mut io::Cursor::new(&monitor.encode()), (nodes[0].keys_manager, nodes[0].keys_manager)).unwrap().1;
8582 assert!(new_monitor == *monitor);
8585 let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &bob_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8586 assert_eq!(watchtower.watch_channel(outpoint, new_monitor), Ok(ChannelMonitorUpdateStatus::Completed));
8589 watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST - 1);
8591 // Route another payment to generate another update with still previous HTLC pending
8592 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
8593 nodes[1].node.send_payment_with_route(&route, payment_hash,
8594 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
8595 check_added_monitors!(nodes[1], 1);
8597 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8598 assert_eq!(updates.update_add_htlcs.len(), 1);
8599 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &updates.update_add_htlcs[0]);
8601 let mut node_0_per_peer_lock;
8602 let mut node_0_peer_state_lock;
8603 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) {
8604 if let Ok(Some(update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].logger) {
8605 // Watchtower Alice should already have seen the block and reject the update
8606 assert_eq!(watchtower_alice.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::InProgress);
8607 assert_eq!(watchtower_bob.chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8608 assert_eq!(nodes[0].chain_monitor.update_channel(outpoint, &update), ChannelMonitorUpdateStatus::Completed);
8609 } else { assert!(false); }
8614 // Our local monitor is in-sync and hasn't processed yet timeout
8615 check_added_monitors!(nodes[0], 1);
8617 //// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
8618 watchtower_bob.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, Vec::new()), HTLC_TIMEOUT_BROADCAST);
8620 // Watchtower Bob should have broadcast a commitment/HTLC-timeout
8623 let mut txn = bob_broadcaster.txn_broadcast();
8624 assert_eq!(txn.len(), 2);
8625 bob_state_y = txn.remove(0);
8628 // We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
8629 let height = HTLC_TIMEOUT_BROADCAST + 1;
8630 connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
8631 check_closed_broadcast(&nodes[0], 1, true);
8632 check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed, false,
8633 [nodes[1].node.get_our_node_id()], 100000);
8634 watchtower_alice.chain_monitor.block_connected(&create_dummy_block(BlockHash::all_zeros(), 42, vec![bob_state_y.clone()]), height);
8635 check_added_monitors(&nodes[0], 1);
8637 let htlc_txn = alice_broadcaster.txn_broadcast();
8638 assert_eq!(htlc_txn.len(), 2);
8639 check_spends!(htlc_txn[0], bob_state_y);
8640 // Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for
8641 // it. However, she should, because it now has an invalid parent.
8642 check_spends!(htlc_txn[1], alice_state);
8647 fn test_pre_lockin_no_chan_closed_update() {
8648 // Test that if a peer closes a channel in response to a funding_created message we don't
8649 // generate a channel update (as the channel cannot appear on chain without a funding_signed
8652 // Doing so would imply a channel monitor update before the initial channel monitor
8653 // registration, violating our API guarantees.
8655 // Previously, full_stack_target managed to hit this case by opening then closing a channel,
8656 // then opening a second channel with the same funding output as the first (which is not
8657 // rejected because the first channel does not exist in the ChannelManager) and closing it
8658 // before receiving funding_signed.
8659 let chanmon_cfgs = create_chanmon_cfgs(2);
8660 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8661 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8662 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8664 // Create an initial channel
8665 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
8666 let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8667 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8668 let accept_chan_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
8669 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_chan_msg);
8671 // Move the first channel through the funding flow...
8672 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
8674 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
8675 check_added_monitors!(nodes[0], 0);
8677 let funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
8678 let channel_id = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index }.to_channel_id();
8679 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id, data: "Hi".to_owned() });
8680 assert!(nodes[0].chain_monitor.added_monitors.lock().unwrap().is_empty());
8681 check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("Hi".to_string()) }, true,
8682 [nodes[1].node.get_our_node_id()], 100000);
8686 fn test_htlc_no_detection() {
8687 // This test is a mutation to underscore the detection logic bug we had
8688 // before #653. HTLC value routed is above the remaining balance, thus
8689 // inverting HTLC and `to_remote` output. HTLC will come second and
8690 // it wouldn't be seen by pre-#653 detection as we were enumerate()'ing
8691 // on a watched outputs vector (Vec<TxOut>) thus implicitly relying on
8692 // outputs order detection for correct spending children filtring.
8694 let chanmon_cfgs = create_chanmon_cfgs(2);
8695 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8696 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8697 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8699 // Create some initial channels
8700 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8702 send_payment(&nodes[0], &vec!(&nodes[1])[..], 1_000_000);
8703 let (_, our_payment_hash, ..) = route_payment(&nodes[0], &vec!(&nodes[1])[..], 2_000_000);
8704 let local_txn = get_local_commitment_txn!(nodes[0], chan_1.2);
8705 assert_eq!(local_txn[0].input.len(), 1);
8706 assert_eq!(local_txn[0].output.len(), 3);
8707 check_spends!(local_txn[0], chan_1.3);
8709 // Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
8710 let block = create_dummy_block(nodes[0].best_block_hash(), 42, vec![local_txn[0].clone()]);
8711 connect_block(&nodes[0], &block);
8712 // We deliberately connect the local tx twice as this should provoke a failure calling
8713 // this test before #653 fix.
8714 chain::Listen::block_connected(&nodes[0].chain_monitor.chain_monitor, &block, nodes[0].best_block_info().1 + 1);
8715 check_closed_broadcast!(nodes[0], true);
8716 check_added_monitors!(nodes[0], 1);
8717 check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[1].node.get_our_node_id()], 100000);
8718 connect_blocks(&nodes[0], TEST_FINAL_CLTV);
8720 let htlc_timeout = {
8721 let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
8722 assert_eq!(node_txn.len(), 1);
8723 assert_eq!(node_txn[0].input.len(), 1);
8724 assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8725 check_spends!(node_txn[0], local_txn[0]);
8729 connect_block(&nodes[0], &create_dummy_block(nodes[0].best_block_hash(), 42, vec![htlc_timeout.clone()]));
8730 connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
8731 expect_payment_failed!(nodes[0], our_payment_hash, false);
8734 fn do_test_onchain_htlc_settlement_after_close(broadcast_alice: bool, go_onchain_before_fulfill: bool) {
8735 // If we route an HTLC, then learn the HTLC's preimage after the upstream channel has been
8736 // force-closed, we must claim that HTLC on-chain. (Given an HTLC forwarded from Alice --> Bob -->
8737 // Carol, Alice would be the upstream node, and Carol the downstream.)
8739 // Steps of the test:
8740 // 1) Alice sends a HTLC to Carol through Bob.
8741 // 2) Carol doesn't settle the HTLC.
8742 // 3) If broadcast_alice is true, Alice force-closes her channel with Bob. Else Bob force closes.
8743 // Steps 4 and 5 may be reordered depending on go_onchain_before_fulfill.
8744 // 4) Bob sees the Alice's commitment on his chain or vice versa. An offered output is present
8745 // but can't be claimed as Bob doesn't have yet knowledge of the preimage.
8746 // 5) Carol release the preimage to Bob off-chain.
8747 // 6) Bob claims the offered output on the broadcasted commitment.
8748 let chanmon_cfgs = create_chanmon_cfgs(3);
8749 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8750 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8751 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8753 // Create some initial channels
8754 let chan_ab = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
8755 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 10001);
8757 // Steps (1) and (2):
8758 // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
8759 let (payment_preimage, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 3_000_000);
8761 // Check that Alice's commitment transaction now contains an output for this HTLC.
8762 let alice_txn = get_local_commitment_txn!(nodes[0], chan_ab.2);
8763 check_spends!(alice_txn[0], chan_ab.3);
8764 assert_eq!(alice_txn[0].output.len(), 2);
8765 check_spends!(alice_txn[1], alice_txn[0]); // 2nd transaction is a non-final HTLC-timeout
8766 assert_eq!(alice_txn[1].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
8767 assert_eq!(alice_txn.len(), 2);
8769 // Steps (3) and (4):
8770 // If `go_onchain_before_fufill`, broadcast the relevant commitment transaction and check that Bob
8771 // responds by (1) broadcasting a channel update and (2) adding a new ChannelMonitor.
8772 let mut force_closing_node = 0; // Alice force-closes
8773 let mut counterparty_node = 1; // Bob if Alice force-closes
8776 if !broadcast_alice {
8777 force_closing_node = 1;
8778 counterparty_node = 0;
8780 nodes[force_closing_node].node.force_close_broadcasting_latest_txn(&chan_ab.2, &nodes[counterparty_node].node.get_our_node_id()).unwrap();
8781 check_closed_broadcast!(nodes[force_closing_node], true);
8782 check_added_monitors!(nodes[force_closing_node], 1);
8783 check_closed_event!(nodes[force_closing_node], 1, ClosureReason::HolderForceClosed, [nodes[counterparty_node].node.get_our_node_id()], 100000);
8784 if go_onchain_before_fulfill {
8785 let txn_to_broadcast = match broadcast_alice {
8786 true => alice_txn.clone(),
8787 false => get_local_commitment_txn!(nodes[1], chan_ab.2)
8789 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8790 if broadcast_alice {
8791 check_closed_broadcast!(nodes[1], true);
8792 check_added_monitors!(nodes[1], 1);
8793 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8798 // Carol then claims the funds and sends an update_fulfill message to Bob, and they go through the
8799 // process of removing the HTLC from their commitment transactions.
8800 nodes[2].node.claim_funds(payment_preimage);
8801 check_added_monitors!(nodes[2], 1);
8802 expect_payment_claimed!(nodes[2], payment_hash, 3_000_000);
8804 let carol_updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
8805 assert!(carol_updates.update_add_htlcs.is_empty());
8806 assert!(carol_updates.update_fail_htlcs.is_empty());
8807 assert!(carol_updates.update_fail_malformed_htlcs.is_empty());
8808 assert!(carol_updates.update_fee.is_none());
8809 assert_eq!(carol_updates.update_fulfill_htlcs.len(), 1);
8811 nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &carol_updates.update_fulfill_htlcs[0]);
8812 let went_onchain = go_onchain_before_fulfill || force_closing_node == 1;
8813 expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], if went_onchain { None } else { Some(1000) }, went_onchain, false);
8814 // If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
8815 if !go_onchain_before_fulfill && broadcast_alice {
8816 let events = nodes[1].node.get_and_clear_pending_msg_events();
8817 assert_eq!(events.len(), 1);
8819 MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
8820 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
8822 _ => panic!("Unexpected event"),
8825 nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &carol_updates.commitment_signed);
8826 // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
8827 // Carol<->Bob's updated commitment transaction info.
8828 check_added_monitors!(nodes[1], 2);
8830 let events = nodes[1].node.get_and_clear_pending_msg_events();
8831 assert_eq!(events.len(), 2);
8832 let bob_revocation = match events[0] {
8833 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8834 assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8837 _ => panic!("Unexpected event"),
8839 let bob_updates = match events[1] {
8840 MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } => {
8841 assert_eq!(*node_id, nodes[2].node.get_our_node_id());
8844 _ => panic!("Unexpected event"),
8847 nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revocation);
8848 check_added_monitors!(nodes[2], 1);
8849 nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_updates.commitment_signed);
8850 check_added_monitors!(nodes[2], 1);
8852 let events = nodes[2].node.get_and_clear_pending_msg_events();
8853 assert_eq!(events.len(), 1);
8854 let carol_revocation = match events[0] {
8855 MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
8856 assert_eq!(*node_id, nodes[1].node.get_our_node_id());
8859 _ => panic!("Unexpected event"),
8861 nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &carol_revocation);
8862 check_added_monitors!(nodes[1], 1);
8864 // If this test requires the force-closed channel to not be on-chain until after the fulfill,
8865 // here's where we put said channel's commitment tx on-chain.
8866 let mut txn_to_broadcast = alice_txn.clone();
8867 if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn!(nodes[1], chan_ab.2); }
8868 if !go_onchain_before_fulfill {
8869 connect_block(&nodes[1], &create_dummy_block(nodes[1].best_block_hash(), 42, vec![txn_to_broadcast[0].clone()]));
8870 // If Bob was the one to force-close, he will have already passed these checks earlier.
8871 if broadcast_alice {
8872 check_closed_broadcast!(nodes[1], true);
8873 check_added_monitors!(nodes[1], 1);
8874 check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000);
8876 let mut bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
8877 if broadcast_alice {
8878 assert_eq!(bob_txn.len(), 1);
8879 check_spends!(bob_txn[0], txn_to_broadcast[0]);
8881 assert_eq!(bob_txn.len(), 2);
8882 check_spends!(bob_txn[0], chan_ab.3);
8887 // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
8888 // broadcasted commitment transaction.
8890 let script_weight = match broadcast_alice {
8891 true => OFFERED_HTLC_SCRIPT_WEIGHT,
8892 false => ACCEPTED_HTLC_SCRIPT_WEIGHT
8894 // If Alice force-closed, Bob only broadcasts a HTLC-output-claiming transaction. Otherwise,
8895 // Bob force-closed and broadcasts the commitment transaction along with a
8896 // HTLC-output-claiming transaction.
8897 let bob_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
8898 if broadcast_alice {
8899 assert_eq!(bob_txn.len(), 1);
8900 check_spends!(bob_txn[0], txn_to_broadcast[0]);
8901 assert_eq!(bob_txn[0].input[0].witness.last().unwrap().len(), script_weight);
8903 assert_eq!(bob_txn.len(), 2);
8904 check_spends!(bob_txn[1], txn_to_broadcast[0]);
8905 assert_eq!(bob_txn[1].input[0].witness.last().unwrap().len(), script_weight);
8911 fn test_onchain_htlc_settlement_after_close() {
8912 do_test_onchain_htlc_settlement_after_close(true, true);
8913 do_test_onchain_htlc_settlement_after_close(false, true); // Technically redundant, but may as well
8914 do_test_onchain_htlc_settlement_after_close(true, false);
8915 do_test_onchain_htlc_settlement_after_close(false, false);
8919 fn test_duplicate_temporary_channel_id_from_different_peers() {
8920 // Tests that we can accept two different `OpenChannel` requests with the same
8921 // `temporary_channel_id`, as long as they are from different peers.
8922 let chanmon_cfgs = create_chanmon_cfgs(3);
8923 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
8924 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
8925 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
8927 // Create an first channel channel
8928 nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
8929 let mut open_chan_msg_chan_1_0 = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8931 // Create an second channel
8932 nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 100000, 10001, 43, None, None).unwrap();
8933 let mut open_chan_msg_chan_2_0 = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
8935 // Modify the `OpenChannel` from `nodes[2]` to `nodes[0]` to ensure that it uses the same
8936 // `temporary_channel_id` as the `OpenChannel` from nodes[1] to nodes[0].
8937 open_chan_msg_chan_2_0.temporary_channel_id = open_chan_msg_chan_1_0.temporary_channel_id;
8939 // Assert that `nodes[0]` can accept both `OpenChannel` requests, even though they use the same
8940 // `temporary_channel_id` as they are from different peers.
8941 nodes[0].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_chan_msg_chan_1_0);
8943 let events = nodes[0].node.get_and_clear_pending_msg_events();
8944 assert_eq!(events.len(), 1);
8946 MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8947 assert_eq!(node_id, &nodes[1].node.get_our_node_id());
8948 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8950 _ => panic!("Unexpected event"),
8954 nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), &open_chan_msg_chan_2_0);
8956 let events = nodes[0].node.get_and_clear_pending_msg_events();
8957 assert_eq!(events.len(), 1);
8959 MessageSendEvent::SendAcceptChannel { node_id, msg } => {
8960 assert_eq!(node_id, &nodes[2].node.get_our_node_id());
8961 assert_eq!(msg.temporary_channel_id, open_chan_msg_chan_1_0.temporary_channel_id);
8963 _ => panic!("Unexpected event"),
8969 fn test_duplicate_chan_id() {
8970 // Test that if a given peer tries to open a channel with the same channel_id as one that is
8971 // already open we reject it and keep the old channel.
8973 // Previously, full_stack_target managed to figure out that if you tried to open two channels
8974 // with the same funding output (ie post-funding channel_id), we'd create a monitor update for
8975 // the existing channel when we detect the duplicate new channel, screwing up our monitor
8976 // updating logic for the existing channel.
8977 let chanmon_cfgs = create_chanmon_cfgs(2);
8978 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8979 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8980 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
8982 // Create an initial channel
8983 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
8984 let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
8985 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8986 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()));
8988 // Try to create a second channel with the same temporary_channel_id as the first and check
8989 // that it is rejected.
8990 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
8992 let events = nodes[1].node.get_and_clear_pending_msg_events();
8993 assert_eq!(events.len(), 1);
8995 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
8996 // Technically, at this point, nodes[1] would be justified in thinking both the
8997 // first (valid) and second (invalid) channels are closed, given they both have
8998 // the same non-temporary channel_id. However, currently we do not, so we just
8999 // move forward with it.
9000 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9001 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9003 _ => panic!("Unexpected event"),
9007 // Move the first channel through the funding flow...
9008 let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
9010 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9011 check_added_monitors!(nodes[0], 0);
9013 let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9014 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
9016 let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
9017 assert_eq!(added_monitors.len(), 1);
9018 assert_eq!(added_monitors[0].0, funding_output);
9019 added_monitors.clear();
9021 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9023 let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9025 let funding_outpoint = crate::chain::transaction::OutPoint { txid: funding_created_msg.funding_txid, index: funding_created_msg.funding_output_index };
9026 let channel_id = funding_outpoint.to_channel_id();
9028 // Now we have the first channel past funding_created (ie it has a txid-based channel_id, not a
9031 // First try to open a second channel with a temporary channel id equal to the txid-based one.
9032 // Technically this is allowed by the spec, but we don't support it and there's little reason
9033 // to. Still, it shouldn't cause any other issues.
9034 open_chan_msg.temporary_channel_id = channel_id;
9035 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
9037 let events = nodes[1].node.get_and_clear_pending_msg_events();
9038 assert_eq!(events.len(), 1);
9040 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9041 // Technically, at this point, nodes[1] would be justified in thinking both
9042 // channels are closed, but currently we do not, so we just move forward with it.
9043 assert_eq!(msg.channel_id, open_chan_msg.temporary_channel_id);
9044 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9046 _ => panic!("Unexpected event"),
9050 // Now try to create a second channel which has a duplicate funding output.
9051 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
9052 let open_chan_2_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9053 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_2_msg);
9054 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()));
9055 create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); // Get and check the FundingGenerationReady event
9057 let (_, funding_created) = {
9058 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9059 let mut a_peer_state = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9060 // Once we call `get_funding_created` the channel has a duplicate channel_id as
9061 // another channel in the ChannelManager - an invalid state. Thus, we'd panic later when we
9062 // try to create another channel. Instead, we drop the channel entirely here (leaving the
9063 // channelmanager in a possibly nonsense state instead).
9064 match a_peer_state.channel_by_id.remove(&open_chan_2_msg.temporary_channel_id).unwrap() {
9065 ChannelPhase::UnfundedOutboundV1(chan) => {
9066 let logger = test_utils::TestLogger::new();
9067 chan.get_funding_created(tx.clone(), funding_outpoint, false, &&logger).map_err(|_| ()).unwrap()
9069 _ => panic!("Unexpected ChannelPhase variant"),
9072 check_added_monitors!(nodes[0], 0);
9073 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created.unwrap());
9074 // At this point we'll look up if the channel_id is present and immediately fail the channel
9075 // without trying to persist the `ChannelMonitor`.
9076 check_added_monitors!(nodes[1], 0);
9078 // ...still, nodes[1] will reject the duplicate channel.
9080 let events = nodes[1].node.get_and_clear_pending_msg_events();
9081 assert_eq!(events.len(), 1);
9083 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
9084 // Technically, at this point, nodes[1] would be justified in thinking both
9085 // channels are closed, but currently we do not, so we just move forward with it.
9086 assert_eq!(msg.channel_id, channel_id);
9087 assert_eq!(node_id, nodes[0].node.get_our_node_id());
9089 _ => panic!("Unexpected event"),
9093 // finally, finish creating the original channel and send a payment over it to make sure
9094 // everything is functional.
9095 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
9097 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9098 assert_eq!(added_monitors.len(), 1);
9099 assert_eq!(added_monitors[0].0, funding_output);
9100 added_monitors.clear();
9102 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9104 let events_4 = nodes[0].node.get_and_clear_pending_events();
9105 assert_eq!(events_4.len(), 0);
9106 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9107 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9109 let (channel_ready, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9110 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9111 update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9113 send_payment(&nodes[0], &[&nodes[1]], 8000000);
9117 fn test_error_chans_closed() {
9118 // Test that we properly handle error messages, closing appropriate channels.
9120 // Prior to #787 we'd allow a peer to make us force-close a channel we had with a different
9121 // peer. The "real" fix for that is to index channels with peers_ids, however in the mean time
9122 // we can test various edge cases around it to ensure we don't regress.
9123 let chanmon_cfgs = create_chanmon_cfgs(3);
9124 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9125 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9126 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9128 // Create some initial channels
9129 let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9130 let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9131 let chan_3 = create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001);
9133 assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9134 assert_eq!(nodes[1].node.list_usable_channels().len(), 2);
9135 assert_eq!(nodes[2].node.list_usable_channels().len(), 1);
9137 // Closing a channel from a different peer has no effect
9138 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_3.2, data: "ERR".to_owned() });
9139 assert_eq!(nodes[0].node.list_usable_channels().len(), 3);
9141 // Closing one channel doesn't impact others
9142 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: chan_2.2, data: "ERR".to_owned() });
9143 check_added_monitors!(nodes[0], 1);
9144 check_closed_broadcast!(nodes[0], false);
9145 check_closed_event!(nodes[0], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9146 [nodes[1].node.get_our_node_id()], 100000);
9147 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
9148 assert_eq!(nodes[0].node.list_usable_channels().len(), 2);
9149 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);
9150 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);
9152 // A null channel ID should close all channels
9153 let _chan_4 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9154 nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage { channel_id: ChannelId::new_zero(), data: "ERR".to_owned() });
9155 check_added_monitors!(nodes[0], 2);
9156 check_closed_event!(nodes[0], 2, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString("ERR".to_string()) },
9157 [nodes[1].node.get_our_node_id(); 2], 100000);
9158 let events = nodes[0].node.get_and_clear_pending_msg_events();
9159 assert_eq!(events.len(), 2);
9161 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9162 assert_eq!(msg.contents.flags & 2, 2);
9164 _ => panic!("Unexpected event"),
9167 MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
9168 assert_eq!(msg.contents.flags & 2, 2);
9170 _ => panic!("Unexpected event"),
9172 // Note that at this point users of a standard PeerHandler will end up calling
9173 // peer_disconnected.
9174 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9175 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9177 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9178 assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
9179 assert!(nodes[0].node.list_usable_channels()[0].channel_id == chan_3.2);
9183 fn test_invalid_funding_tx() {
9184 // Test that we properly handle invalid funding transactions sent to us from a peer.
9186 // Previously, all other major lightning implementations had failed to properly sanitize
9187 // funding transactions from their counterparties, leading to a multi-implementation critical
9188 // security vulnerability (though we always sanitized properly, we've previously had
9189 // un-released crashes in the sanitization process).
9191 // Further, if the funding transaction is consensus-valid, confirms, and is later spent, we'd
9192 // previously have crashed in `ChannelMonitor` even though we closed the channel as bogus and
9193 // gave up on it. We test this here by generating such a transaction.
9194 let chanmon_cfgs = create_chanmon_cfgs(2);
9195 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9196 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9197 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9199 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_000, 42, None, None).unwrap();
9200 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()));
9201 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()));
9203 let (temporary_channel_id, mut tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100_000, 42);
9205 // Create a witness program which can be spent by a 4-empty-stack-elements witness and which is
9206 // 136 bytes long. This matches our "accepted HTLC preimage spend" matching, previously causing
9207 // a panic as we'd try to extract a 32 byte preimage from a witness element without checking
9209 let mut wit_program: Vec<u8> = channelmonitor::deliberately_bogus_accepted_htlc_witness_program();
9210 let wit_program_script: ScriptBuf = wit_program.into();
9211 for output in tx.output.iter_mut() {
9212 // Make the confirmed funding transaction have a bogus script_pubkey
9213 output.script_pubkey = ScriptBuf::new_v0_p2wsh(&wit_program_script.wscript_hash());
9216 nodes[0].node.funding_transaction_generated_unchecked(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone(), 0).unwrap();
9217 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()));
9218 check_added_monitors!(nodes[1], 1);
9219 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9221 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()));
9222 check_added_monitors!(nodes[0], 1);
9223 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9225 let events_1 = nodes[0].node.get_and_clear_pending_events();
9226 assert_eq!(events_1.len(), 0);
9228 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
9229 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap()[0], tx);
9230 nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
9232 let expected_err = "funding tx had wrong script/value or output index";
9233 confirm_transaction_at(&nodes[1], &tx, 1);
9234 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: expected_err.to_string() },
9235 [nodes[0].node.get_our_node_id()], 100000);
9236 check_added_monitors!(nodes[1], 1);
9237 let events_2 = nodes[1].node.get_and_clear_pending_msg_events();
9238 assert_eq!(events_2.len(), 1);
9239 if let MessageSendEvent::HandleError { node_id, action } = &events_2[0] {
9240 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
9241 if let msgs::ErrorAction::DisconnectPeer { msg } = action {
9242 assert_eq!(msg.as_ref().unwrap().data, "Channel closed because of an exception: ".to_owned() + expected_err);
9243 } else { panic!(); }
9244 } else { panic!(); }
9245 assert_eq!(nodes[1].node.list_channels().len(), 0);
9247 // Now confirm a spend of the (bogus) funding transaction. As long as the witness is 5 elements
9248 // long the ChannelMonitor will try to read 32 bytes from the second-to-last element, panicing
9249 // as its not 32 bytes long.
9250 let mut spend_tx = Transaction {
9251 version: 2i32, lock_time: LockTime::ZERO,
9252 input: tx.output.iter().enumerate().map(|(idx, _)| TxIn {
9253 previous_output: BitcoinOutPoint {
9257 script_sig: ScriptBuf::new(),
9258 sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
9259 witness: Witness::from_slice(&channelmonitor::deliberately_bogus_accepted_htlc_witness())
9261 output: vec![TxOut {
9263 script_pubkey: ScriptBuf::new(),
9266 check_spends!(spend_tx, tx);
9267 mine_transaction(&nodes[1], &spend_tx);
9271 fn test_coinbase_funding_tx() {
9272 // Miners are able to fund channels directly from coinbase transactions, however
9273 // by consensus rules, outputs of a coinbase transaction are encumbered by a 100
9274 // block maturity timelock. To ensure that a (non-0conf) channel like this is enforceable
9275 // on-chain, the minimum depth is updated to 100 blocks for coinbase funding transactions.
9277 // Note that 0conf channels with coinbase funding transactions are unaffected and are
9278 // immediately operational after opening.
9279 let chanmon_cfgs = create_chanmon_cfgs(2);
9280 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9281 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9282 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9284 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
9285 let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9287 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9288 let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9290 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9292 // Create the coinbase funding transaction.
9293 let (temporary_channel_id, tx, _) = create_coinbase_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
9295 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9296 check_added_monitors!(nodes[0], 0);
9297 let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9299 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9300 check_added_monitors!(nodes[1], 1);
9301 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9303 let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9305 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
9306 check_added_monitors!(nodes[0], 1);
9308 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9309 assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
9311 // Starting at height 0, we "confirm" the coinbase at height 1.
9312 confirm_transaction_at(&nodes[0], &tx, 1);
9313 // We connect 98 more blocks to have 99 confirmations for the coinbase transaction.
9314 connect_blocks(&nodes[0], COINBASE_MATURITY - 2);
9315 // Check that we have no pending message events (we have not queued a `channel_ready` yet).
9316 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
9317 // Now connect one more block which results in 100 confirmations of the coinbase transaction.
9318 connect_blocks(&nodes[0], 1);
9319 // There should now be a `channel_ready` which can be handled.
9320 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()));
9322 confirm_transaction_at(&nodes[1], &tx, 1);
9323 connect_blocks(&nodes[1], COINBASE_MATURITY - 2);
9324 assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
9325 connect_blocks(&nodes[1], 1);
9326 expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
9327 create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
9330 fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
9331 // In the first version of the chain::Confirm interface, after a refactor was made to not
9332 // broadcast CSV-locked transactions until their CSV lock is up, we wouldn't reliably broadcast
9333 // transactions after a `transactions_confirmed` call. Specifically, if the chain, provided via
9334 // `best_block_updated` is at height N, and a transaction output which we wish to spend at
9335 // height N-1 (due to a CSV to height N-1) is provided at height N, we will not broadcast the
9336 // spending transaction until height N+1 (or greater). This was due to the way
9337 // `ChannelMonitor::transactions_confirmed` worked, only checking if we should broadcast a
9338 // spending transaction at the height the input transaction was confirmed at, not whether we
9339 // should broadcast a spending transaction at the current height.
9340 // A second, similar, issue involved failing HTLCs backwards - because we only provided the
9341 // height at which transactions were confirmed to `OnchainTx::update_claims_view`, it wasn't
9342 // aware that the anti-reorg-delay had, in fact, already expired, waiting to fail-backwards
9343 // until we learned about an additional block.
9345 // As an additional check, if `test_height_before_timelock` is set, we instead test that we
9346 // aren't broadcasting transactions too early (ie not broadcasting them at all).
9347 let chanmon_cfgs = create_chanmon_cfgs(3);
9348 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
9349 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
9350 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
9351 *nodes[0].connect_style.borrow_mut() = ConnectStyle::BestBlockFirstSkippingBlocks;
9353 create_announced_chan_between_nodes(&nodes, 0, 1);
9354 let (chan_announce, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 1, 2);
9355 let (_, payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
9356 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
9357 nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
9359 nodes[1].node.force_close_broadcasting_latest_txn(&channel_id, &nodes[2].node.get_our_node_id()).unwrap();
9360 check_closed_broadcast!(nodes[1], true);
9361 check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed, [nodes[2].node.get_our_node_id()], 100000);
9362 check_added_monitors!(nodes[1], 1);
9363 let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9364 assert_eq!(node_txn.len(), 1);
9366 let conf_height = nodes[1].best_block_info().1;
9367 if !test_height_before_timelock {
9368 connect_blocks(&nodes[1], 24 * 6);
9370 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9371 &nodes[1].get_block_header(conf_height), &[(0, &node_txn[0])], conf_height);
9372 if test_height_before_timelock {
9373 // If we confirmed the close transaction, but timelocks have not yet expired, we should not
9374 // generate any events or broadcast any transactions
9375 assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
9376 assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
9378 // We should broadcast an HTLC transaction spending our funding transaction first
9379 let spending_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
9380 assert_eq!(spending_txn.len(), 2);
9381 assert_eq!(spending_txn[0].txid(), node_txn[0].txid());
9382 check_spends!(spending_txn[1], node_txn[0]);
9383 // We should also generate a SpendableOutputs event with the to_self output (as its
9385 let descriptor_spend_txn = check_spendable_outputs!(nodes[1], node_cfgs[1].keys_manager);
9386 assert_eq!(descriptor_spend_txn.len(), 1);
9388 // If we also discover that the HTLC-Timeout transaction was confirmed some time ago, we
9389 // should immediately fail-backwards the HTLC to the previous hop, without waiting for an
9390 // additional block built on top of the current chain.
9391 nodes[1].chain_monitor.chain_monitor.transactions_confirmed(
9392 &nodes[1].get_block_header(conf_height + 1), &[(0, &spending_txn[1])], conf_height + 1);
9393 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 }]);
9394 check_added_monitors!(nodes[1], 1);
9396 let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9397 assert!(updates.update_add_htlcs.is_empty());
9398 assert!(updates.update_fulfill_htlcs.is_empty());
9399 assert_eq!(updates.update_fail_htlcs.len(), 1);
9400 assert!(updates.update_fail_malformed_htlcs.is_empty());
9401 assert!(updates.update_fee.is_none());
9402 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
9403 commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
9404 expect_payment_failed_with_update!(nodes[0], payment_hash, false, chan_announce.contents.short_channel_id, true);
9409 fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
9410 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(false);
9411 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(true);
9414 fn do_test_dup_htlc_second_rejected(test_for_second_fail_panic: bool) {
9415 let chanmon_cfgs = create_chanmon_cfgs(2);
9416 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9417 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9418 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9420 let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
9422 let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
9423 .with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
9424 let route = get_route!(nodes[0], payment_params, 10_000).unwrap();
9426 let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
9429 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9430 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
9431 check_added_monitors!(nodes[0], 1);
9432 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9433 assert_eq!(events.len(), 1);
9434 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9435 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9436 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9438 expect_pending_htlcs_forwardable!(nodes[1]);
9439 expect_payment_claimable!(nodes[1], our_payment_hash, our_payment_secret, 10_000);
9442 // Note that we use a different PaymentId here to allow us to duplicativly pay
9443 nodes[0].node.send_payment_with_route(&route, our_payment_hash,
9444 RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_secret.0)).unwrap();
9445 check_added_monitors!(nodes[0], 1);
9446 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9447 assert_eq!(events.len(), 1);
9448 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
9449 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9450 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
9451 // At this point, nodes[1] would notice it has too much value for the payment. It will
9452 // assume the second is a privacy attack (no longer particularly relevant
9453 // post-payment_secrets) and fail back the new HTLC. Previously, it'd also have failed back
9454 // the first HTLC delivered above.
9457 expect_pending_htlcs_forwardable_ignore!(nodes[1]);
9458 nodes[1].node.process_pending_htlc_forwards();
9460 if test_for_second_fail_panic {
9461 // Now we go fail back the first HTLC from the user end.
9462 nodes[1].node.fail_htlc_backwards(&our_payment_hash);
9464 let expected_destinations = vec![
9465 HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9466 HTLCDestination::FailedPayment { payment_hash: our_payment_hash },
9468 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], expected_destinations);
9469 nodes[1].node.process_pending_htlc_forwards();
9471 check_added_monitors!(nodes[1], 1);
9472 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9473 assert_eq!(fail_updates_1.update_fail_htlcs.len(), 2);
9475 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9476 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[1]);
9477 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9479 let failure_events = nodes[0].node.get_and_clear_pending_events();
9480 assert_eq!(failure_events.len(), 4);
9481 if let Event::PaymentPathFailed { .. } = failure_events[0] {} else { panic!(); }
9482 if let Event::PaymentFailed { .. } = failure_events[1] {} else { panic!(); }
9483 if let Event::PaymentPathFailed { .. } = failure_events[2] {} else { panic!(); }
9484 if let Event::PaymentFailed { .. } = failure_events[3] {} else { panic!(); }
9486 // Let the second HTLC fail and claim the first
9487 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9488 nodes[1].node.process_pending_htlc_forwards();
9490 check_added_monitors!(nodes[1], 1);
9491 let fail_updates_1 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
9492 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9493 commitment_signed_dance!(nodes[0], nodes[1], fail_updates_1.commitment_signed, false);
9495 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new());
9497 claim_payment(&nodes[0], &[&nodes[1]], our_payment_preimage);
9502 fn test_dup_htlc_second_fail_panic() {
9503 // Previously, if we received two HTLCs back-to-back, where the second overran the expected
9504 // value for the payment, we'd fail back both HTLCs after generating a `PaymentClaimable` event.
9505 // Then, if the user failed the second payment, they'd hit a "tried to fail an already failed
9506 // HTLC" debug panic. This tests for this behavior, checking that only one HTLC is auto-failed.
9507 do_test_dup_htlc_second_rejected(true);
9511 fn test_dup_htlc_second_rejected() {
9512 // Test that if we receive a second HTLC for an MPP payment that overruns the payment amount we
9513 // simply reject the second HTLC but are still able to claim the first HTLC.
9514 do_test_dup_htlc_second_rejected(false);
9518 fn test_inconsistent_mpp_params() {
9519 // Test that if we recieve two HTLCs with different payment parameters we fail back the first
9520 // such HTLC and allow the second to stay.
9521 let chanmon_cfgs = create_chanmon_cfgs(4);
9522 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9523 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9524 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9526 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9527 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9528 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9529 let chan_2_3 =create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9531 let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
9532 .with_bolt11_features(nodes[3].node.bolt11_invoice_features()).unwrap();
9533 let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
9534 assert_eq!(route.paths.len(), 2);
9535 route.paths.sort_by(|path_a, _| {
9536 // Sort the path so that the path through nodes[1] comes first
9537 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9538 core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9541 let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[3]);
9543 let cur_height = nodes[0].best_block_info().1;
9544 let payment_id = PaymentId([42; 32]);
9546 let session_privs = {
9547 // We create a fake route here so that we start with three pending HTLCs, which we'll
9548 // ultimately have, just not right away.
9549 let mut dup_route = route.clone();
9550 dup_route.paths.push(route.paths[1].clone());
9551 nodes[0].node.test_add_new_pending_payment(our_payment_hash,
9552 RecipientOnionFields::secret_only(our_payment_secret), payment_id, &dup_route).unwrap()
9554 nodes[0].node.test_send_payment_along_path(&route.paths[0], &our_payment_hash,
9555 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9556 &None, session_privs[0]).unwrap();
9557 check_added_monitors!(nodes[0], 1);
9560 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9561 assert_eq!(events.len(), 1);
9562 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), false, None);
9564 assert!(nodes[3].node.get_and_clear_pending_events().is_empty());
9566 nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9567 RecipientOnionFields::secret_only(our_payment_secret), 14_000_000, cur_height, payment_id, &None, session_privs[1]).unwrap();
9568 check_added_monitors!(nodes[0], 1);
9571 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9572 assert_eq!(events.len(), 1);
9573 let payment_event = SendEvent::from_event(events.pop().unwrap());
9575 nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9576 commitment_signed_dance!(nodes[2], nodes[0], payment_event.commitment_msg, false);
9578 expect_pending_htlcs_forwardable!(nodes[2]);
9579 check_added_monitors!(nodes[2], 1);
9581 let mut events = nodes[2].node.get_and_clear_pending_msg_events();
9582 assert_eq!(events.len(), 1);
9583 let payment_event = SendEvent::from_event(events.pop().unwrap());
9585 nodes[3].node.handle_update_add_htlc(&nodes[2].node.get_our_node_id(), &payment_event.msgs[0]);
9586 check_added_monitors!(nodes[3], 0);
9587 commitment_signed_dance!(nodes[3], nodes[2], payment_event.commitment_msg, true, true);
9589 // At this point, nodes[3] should notice the two HTLCs don't contain the same total payment
9590 // amount. It will assume the second is a privacy attack (no longer particularly relevant
9591 // post-payment_secrets) and fail back the new HTLC.
9593 expect_pending_htlcs_forwardable_ignore!(nodes[3]);
9594 nodes[3].node.process_pending_htlc_forwards();
9595 expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[3], vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }]);
9596 nodes[3].node.process_pending_htlc_forwards();
9598 check_added_monitors!(nodes[3], 1);
9600 let fail_updates_1 = get_htlc_update_msgs!(nodes[3], nodes[2].node.get_our_node_id());
9601 nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
9602 commitment_signed_dance!(nodes[2], nodes[3], fail_updates_1.commitment_signed, false);
9604 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 }]);
9605 check_added_monitors!(nodes[2], 1);
9607 let fail_updates_2 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
9608 nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
9609 commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
9611 expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
9613 nodes[0].node.test_send_payment_along_path(&route.paths[1], &our_payment_hash,
9614 RecipientOnionFields::secret_only(our_payment_secret), 15_000_000, cur_height, payment_id,
9615 &None, session_privs[2]).unwrap();
9616 check_added_monitors!(nodes[0], 1);
9618 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9619 assert_eq!(events.len(), 1);
9620 pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 15_000_000, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), true, None);
9622 do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, our_payment_preimage);
9623 expect_payment_sent(&nodes[0], our_payment_preimage, Some(None), true, true);
9627 fn test_double_partial_claim() {
9628 // Test what happens if a node receives a payment, generates a PaymentClaimable event, the HTLCs
9629 // time out, the sender resends only some of the MPP parts, then the user processes the
9630 // PaymentClaimable event, ensuring they don't inadvertently claim only part of the full payment
9632 let chanmon_cfgs = create_chanmon_cfgs(4);
9633 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
9634 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
9635 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
9637 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
9638 create_announced_chan_between_nodes_with_value(&nodes, 0, 2, 100_000, 0);
9639 create_announced_chan_between_nodes_with_value(&nodes, 1, 3, 100_000, 0);
9640 create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 100_000, 0);
9642 let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 15_000_000);
9643 assert_eq!(route.paths.len(), 2);
9644 route.paths.sort_by(|path_a, _| {
9645 // Sort the path so that the path through nodes[1] comes first
9646 if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
9647 core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
9650 send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 15_000_000, payment_hash, payment_secret);
9651 // nodes[3] has now received a PaymentClaimable event...which it will take some (exorbitant)
9652 // amount of time to respond to.
9654 // Connect some blocks to time out the payment
9655 connect_blocks(&nodes[3], TEST_FINAL_CLTV);
9656 connect_blocks(&nodes[0], TEST_FINAL_CLTV); // To get the same height for sending later
9658 let failed_destinations = vec![
9659 HTLCDestination::FailedPayment { payment_hash },
9660 HTLCDestination::FailedPayment { payment_hash },
9662 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[3], failed_destinations);
9664 pass_failed_payment_back(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash, PaymentFailureReason::RecipientRejected);
9666 // nodes[1] now retries one of the two paths...
9667 nodes[0].node.send_payment_with_route(&route, payment_hash,
9668 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9669 check_added_monitors!(nodes[0], 2);
9671 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9672 assert_eq!(events.len(), 2);
9673 let node_1_msgs = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
9674 pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 15_000_000, payment_hash, Some(payment_secret), node_1_msgs, false, None);
9676 // At this point nodes[3] has received one half of the payment, and the user goes to handle
9677 // that PaymentClaimable event they got hours ago and never handled...we should refuse to claim.
9678 nodes[3].node.claim_funds(payment_preimage);
9679 check_added_monitors!(nodes[3], 0);
9680 assert!(nodes[3].node.get_and_clear_pending_msg_events().is_empty());
9683 /// The possible events which may trigger a `max_dust_htlc_exposure` breach
9684 #[derive(Clone, Copy, PartialEq)]
9685 enum ExposureEvent {
9686 /// Breach occurs at HTLC forwarding (see `send_htlc`)
9688 /// Breach occurs at HTLC reception (see `update_add_htlc`)
9690 /// Breach occurs at outbound update_fee (see `send_update_fee`)
9691 AtUpdateFeeOutbound,
9694 fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool, multiplier_dust_limit: bool) {
9695 // Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
9698 // At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9699 // trimmed-to-dust HTLC outbound balance and this new payment as included on next
9700 // counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the
9701 // update. At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC
9702 // inbound and trimmed-to-dust HTLC outbound balance and this new received HTLC as included
9703 // on next counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail
9704 // the update. Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel
9705 // might be available again for HTLC processing once the dust bandwidth has cleared up.
9707 let chanmon_cfgs = create_chanmon_cfgs(2);
9708 let mut config = test_default_channel_config();
9709 config.channel_config.max_dust_htlc_exposure = if multiplier_dust_limit {
9710 // Default test fee estimator rate is 253 sat/kw, so we set the multiplier to 5_000_000 / 253
9711 // to get roughly the same initial value as the default setting when this test was
9712 // originally written.
9713 MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253)
9714 } else { MaxDustHTLCExposure::FixedLimitMsat(5_000_000) }; // initial default setting value
9715 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9716 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
9717 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9719 nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None, None).unwrap();
9720 let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9721 open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9722 open_channel.max_accepted_htlcs = 60;
9724 open_channel.dust_limit_satoshis = 546;
9726 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9727 let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9728 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9730 let channel_type_features = ChannelTypeFeatures::only_static_remote_key();
9732 let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 1_000_000, 42);
9735 let mut node_0_per_peer_lock;
9736 let mut node_0_peer_state_lock;
9737 match get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, temporary_channel_id) {
9738 ChannelPhase::UnfundedOutboundV1(chan) => {
9739 chan.context.holder_dust_limit_satoshis = 546;
9741 _ => panic!("Unexpected ChannelPhase variant"),
9745 nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9746 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()));
9747 check_added_monitors!(nodes[1], 1);
9748 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9750 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()));
9751 check_added_monitors!(nodes[0], 1);
9752 expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9754 let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9755 let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &channel_ready);
9756 update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9758 // Fetch a route in advance as we will be unable to once we're unable to send.
9759 let (mut route, payment_hash, _, payment_secret) =
9760 get_route_and_payment_hash!(nodes[0], nodes[1], 1000);
9762 let (dust_buffer_feerate, max_dust_htlc_exposure_msat) = {
9763 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
9764 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
9765 let chan = chan_lock.channel_by_id.get(&channel_id).unwrap();
9766 (chan.context().get_dust_buffer_feerate(None) as u64,
9767 chan.context().get_max_dust_htlc_exposure_msat(&LowerBoundedFeeEstimator(nodes[0].fee_estimator)))
9769 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;
9770 let dust_outbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
9772 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;
9773 let dust_inbound_htlc_on_holder_tx: u64 = max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
9775 let dust_htlc_on_counterparty_tx: u64 = 4;
9776 let dust_htlc_on_counterparty_tx_msat: u64 = max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
9779 if dust_outbound_balance {
9780 // Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9781 // Outbound dust balance: 4372 sats
9782 // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9783 for _ in 0..dust_outbound_htlc_on_holder_tx {
9784 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_outbound_htlc_on_holder_tx_msat);
9785 nodes[0].node.send_payment_with_route(&route, payment_hash,
9786 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9789 // Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9790 // Inbound dust balance: 4372 sats
9791 // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9792 for _ in 0..dust_inbound_htlc_on_holder_tx {
9793 route_payment(&nodes[1], &[&nodes[0]], dust_inbound_htlc_on_holder_tx_msat);
9797 if dust_outbound_balance {
9798 // Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9799 // Outbound dust balance: 5000 sats
9800 for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9801 let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_htlc_on_counterparty_tx_msat);
9802 nodes[0].node.send_payment_with_route(&route, payment_hash,
9803 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9806 // Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9807 // Inbound dust balance: 5000 sats
9808 for _ in 0..dust_htlc_on_counterparty_tx - 1 {
9809 route_payment(&nodes[1], &[&nodes[0]], dust_htlc_on_counterparty_tx_msat);
9814 if exposure_breach_event == ExposureEvent::AtHTLCForward {
9815 route.paths[0].hops.last_mut().unwrap().fee_msat =
9816 if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat + 1 };
9817 // With default dust exposure: 5000 sats
9819 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9820 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9821 ), true, APIError::ChannelUnavailable { .. }, {});
9823 unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, payment_hash,
9824 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
9825 ), true, APIError::ChannelUnavailable { .. }, {});
9827 } else if exposure_breach_event == ExposureEvent::AtHTLCReception {
9828 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 });
9829 nodes[1].node.send_payment_with_route(&route, payment_hash,
9830 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9831 check_added_monitors!(nodes[1], 1);
9832 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
9833 assert_eq!(events.len(), 1);
9834 let payment_event = SendEvent::from_event(events.remove(0));
9835 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
9836 // With default dust exposure: 5000 sats
9838 // Outbound dust balance: 6399 sats
9839 let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
9840 let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
9841 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);
9843 // Outbound dust balance: 5200 sats
9844 nodes[0].logger.assert_log("lightning::ln::channel".to_string(),
9845 format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx",
9846 dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx - 1) + dust_htlc_on_counterparty_tx_msat + 4,
9847 max_dust_htlc_exposure_msat), 1);
9849 } else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {
9850 route.paths[0].hops.last_mut().unwrap().fee_msat = 2_500_000;
9851 // For the multiplier dust exposure limit, since it scales with feerate,
9852 // we need to add a lot of HTLCs that will become dust at the new feerate
9853 // to cross the threshold.
9855 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(1_000), None);
9856 nodes[0].node.send_payment_with_route(&route, payment_hash,
9857 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
9860 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9861 *feerate_lock = *feerate_lock * 10;
9863 nodes[0].node.timer_tick_occurred();
9864 check_added_monitors!(nodes[0], 1);
9865 nodes[0].logger.assert_log_contains("lightning::ln::channel", "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure", 1);
9868 let _ = nodes[0].node.get_and_clear_pending_msg_events();
9869 let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9870 added_monitors.clear();
9873 fn do_test_max_dust_htlc_exposure_by_threshold_type(multiplier_dust_limit: bool) {
9874 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9875 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true, multiplier_dust_limit);
9876 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9877 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9878 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9879 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false, multiplier_dust_limit);
9880 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true, multiplier_dust_limit);
9881 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false, multiplier_dust_limit);
9882 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9883 do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9884 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false, multiplier_dust_limit);
9885 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true, multiplier_dust_limit);
9889 fn test_max_dust_htlc_exposure() {
9890 do_test_max_dust_htlc_exposure_by_threshold_type(false);
9891 do_test_max_dust_htlc_exposure_by_threshold_type(true);
9895 fn test_non_final_funding_tx() {
9896 let chanmon_cfgs = create_chanmon_cfgs(2);
9897 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9898 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9899 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9901 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
9902 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9903 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
9904 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9905 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
9907 let best_height = nodes[0].node.best_block.read().unwrap().height();
9909 let chan_id = *nodes[0].network_chan_count.borrow();
9910 let events = nodes[0].node.get_and_clear_pending_events();
9911 let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_slice(&[&[1]]) };
9912 assert_eq!(events.len(), 1);
9913 let mut tx = match events[0] {
9914 Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
9915 // Timelock the transaction _beyond_ the best client height + 1.
9916 Transaction { version: chan_id as i32, lock_time: LockTime::from_height(best_height + 2).unwrap(), input: vec![input], output: vec![TxOut {
9917 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
9920 _ => panic!("Unexpected event"),
9922 // Transaction should fail as it's evaluated as non-final for propagation.
9923 match nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()) {
9924 Err(APIError::APIMisuseError { err }) => {
9925 assert_eq!(format!("Funding transaction absolute timelock is non-final"), err);
9929 let events = nodes[0].node.get_and_clear_pending_events();
9930 assert_eq!(events.len(), 1);
9932 Event::ChannelClosed { channel_id, .. } => {
9933 assert_eq!(channel_id, temp_channel_id);
9935 _ => panic!("Unexpected event"),
9940 fn test_non_final_funding_tx_within_headroom() {
9941 let chanmon_cfgs = create_chanmon_cfgs(2);
9942 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9943 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9944 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9946 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
9947 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9948 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
9949 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9950 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
9952 let best_height = nodes[0].node.best_block.read().unwrap().height();
9954 let chan_id = *nodes[0].network_chan_count.borrow();
9955 let events = nodes[0].node.get_and_clear_pending_events();
9956 let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_slice(&[[1]]) };
9957 assert_eq!(events.len(), 1);
9958 let mut tx = match events[0] {
9959 Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
9960 // Timelock the transaction within a +1 headroom from the best block.
9961 Transaction { version: chan_id as i32, lock_time: LockTime::from_consensus(best_height + 1), input: vec![input], output: vec![TxOut {
9962 value: *channel_value_satoshis, script_pubkey: output_script.clone(),
9965 _ => panic!("Unexpected event"),
9968 // Transaction should be accepted if it's in a +1 headroom from best block.
9969 assert!(nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
9970 get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9974 fn accept_busted_but_better_fee() {
9975 // If a peer sends us a fee update that is too low, but higher than our previous channel
9976 // feerate, we should accept it. In the future we may want to consider closing the channel
9977 // later, but for now we only accept the update.
9978 let mut chanmon_cfgs = create_chanmon_cfgs(2);
9979 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9980 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9981 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9983 create_chan_between_nodes(&nodes[0], &nodes[1]);
9985 // Set nodes[1] to expect 5,000 sat/kW.
9987 let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
9988 *feerate_lock = 5000;
9991 // If nodes[0] increases their feerate, even if its not enough, nodes[1] should accept it.
9993 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9994 *feerate_lock = 1000;
9996 nodes[0].node.timer_tick_occurred();
9997 check_added_monitors!(nodes[0], 1);
9999 let events = nodes[0].node.get_and_clear_pending_msg_events();
10000 assert_eq!(events.len(), 1);
10002 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
10003 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
10004 commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
10006 _ => panic!("Unexpected event"),
10009 // If nodes[0] increases their feerate further, even if its not enough, nodes[1] should accept
10012 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
10013 *feerate_lock = 2000;
10015 nodes[0].node.timer_tick_occurred();
10016 check_added_monitors!(nodes[0], 1);
10018 let events = nodes[0].node.get_and_clear_pending_msg_events();
10019 assert_eq!(events.len(), 1);
10021 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, ref commitment_signed, .. }, .. } => {
10022 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
10023 commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
10025 _ => panic!("Unexpected event"),
10028 // However, if nodes[0] decreases their feerate, nodes[1] should reject it and close the
10031 let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
10032 *feerate_lock = 1000;
10034 nodes[0].node.timer_tick_occurred();
10035 check_added_monitors!(nodes[0], 1);
10037 let events = nodes[0].node.get_and_clear_pending_msg_events();
10038 assert_eq!(events.len(), 1);
10040 MessageSendEvent::UpdateHTLCs { updates: msgs::CommitmentUpdate { ref update_fee, .. }, .. } => {
10041 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), update_fee.as_ref().unwrap());
10042 check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError {
10043 err: "Peer's feerate much too low. Actual: 1000. Our expected lower limit: 5000".to_owned() },
10044 [nodes[0].node.get_our_node_id()], 100000);
10045 check_closed_broadcast!(nodes[1], true);
10046 check_added_monitors!(nodes[1], 1);
10048 _ => panic!("Unexpected event"),
10052 fn do_payment_with_custom_min_final_cltv_expiry(valid_delta: bool, use_user_hash: bool) {
10053 let mut chanmon_cfgs = create_chanmon_cfgs(2);
10054 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10055 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10056 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10057 let min_final_cltv_expiry_delta = 120;
10058 let final_cltv_expiry_delta = if valid_delta { min_final_cltv_expiry_delta + 2 } else {
10059 min_final_cltv_expiry_delta - 2 };
10060 let recv_value = 100_000;
10062 create_chan_between_nodes(&nodes[0], &nodes[1]);
10064 let payment_parameters = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), final_cltv_expiry_delta as u32);
10065 let (payment_hash, payment_preimage, payment_secret) = if use_user_hash {
10066 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[1],
10067 Some(recv_value), Some(min_final_cltv_expiry_delta));
10068 (payment_hash, payment_preimage, payment_secret)
10070 let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(recv_value), 7200, Some(min_final_cltv_expiry_delta)).unwrap();
10071 (payment_hash, nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap(), payment_secret)
10073 let route = get_route!(nodes[0], payment_parameters, recv_value).unwrap();
10074 nodes[0].node.send_payment_with_route(&route, payment_hash,
10075 RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
10076 check_added_monitors!(nodes[0], 1);
10077 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10078 assert_eq!(events.len(), 1);
10079 let mut payment_event = SendEvent::from_event(events.pop().unwrap());
10080 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10081 commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10082 expect_pending_htlcs_forwardable!(nodes[1]);
10085 expect_payment_claimable!(nodes[1], payment_hash, payment_secret, recv_value, if use_user_hash {
10086 None } else { Some(payment_preimage) }, nodes[1].node.get_our_node_id());
10088 claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage);
10090 expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
10092 check_added_monitors!(nodes[1], 1);
10094 let fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10095 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_updates.update_fail_htlcs[0]);
10096 commitment_signed_dance!(nodes[0], nodes[1], fail_updates.commitment_signed, false, true);
10098 expect_payment_failed!(nodes[0], payment_hash, true);
10103 fn test_payment_with_custom_min_cltv_expiry_delta() {
10104 do_payment_with_custom_min_final_cltv_expiry(false, false);
10105 do_payment_with_custom_min_final_cltv_expiry(false, true);
10106 do_payment_with_custom_min_final_cltv_expiry(true, false);
10107 do_payment_with_custom_min_final_cltv_expiry(true, true);
10111 fn test_disconnects_peer_awaiting_response_ticks() {
10112 // Tests that nodes which are awaiting on a response critical for channel responsiveness
10113 // disconnect their counterparty after `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10114 let mut chanmon_cfgs = create_chanmon_cfgs(2);
10115 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10116 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10117 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10119 // Asserts a disconnect event is queued to the user.
10120 let check_disconnect_event = |node: &Node, should_disconnect: bool| {
10121 let disconnect_event = node.node.get_and_clear_pending_msg_events().iter().find_map(|event|
10122 if let MessageSendEvent::HandleError { action, .. } = event {
10123 if let msgs::ErrorAction::DisconnectPeerWithWarning { .. } = action {
10132 assert_eq!(disconnect_event.is_some(), should_disconnect);
10135 // Fires timer ticks ensuring we only attempt to disconnect peers after reaching
10136 // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10137 let check_disconnect = |node: &Node| {
10138 // No disconnect without any timer ticks.
10139 check_disconnect_event(node, false);
10141 // No disconnect with 1 timer tick less than required.
10142 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS - 1 {
10143 node.node.timer_tick_occurred();
10144 check_disconnect_event(node, false);
10147 // Disconnect after reaching the required ticks.
10148 node.node.timer_tick_occurred();
10149 check_disconnect_event(node, true);
10151 // Disconnect again on the next tick if the peer hasn't been disconnected yet.
10152 node.node.timer_tick_occurred();
10153 check_disconnect_event(node, true);
10156 create_chan_between_nodes(&nodes[0], &nodes[1]);
10158 // We'll start by performing a fee update with Alice (nodes[0]) on the channel.
10159 *nodes[0].fee_estimator.sat_per_kw.lock().unwrap() *= 2;
10160 nodes[0].node.timer_tick_occurred();
10161 check_added_monitors!(&nodes[0], 1);
10162 let alice_fee_update = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10163 nodes[1].node.handle_update_fee(&nodes[0].node.get_our_node_id(), alice_fee_update.update_fee.as_ref().unwrap());
10164 nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &alice_fee_update.commitment_signed);
10165 check_added_monitors!(&nodes[1], 1);
10167 // This will prompt Bob (nodes[1]) to respond with his `CommitmentSigned` and `RevokeAndACK`.
10168 let (bob_revoke_and_ack, bob_commitment_signed) = get_revoke_commit_msgs!(&nodes[1], nodes[0].node.get_our_node_id());
10169 nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bob_revoke_and_ack);
10170 check_added_monitors!(&nodes[0], 1);
10171 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bob_commitment_signed);
10172 check_added_monitors(&nodes[0], 1);
10174 // Alice then needs to send her final `RevokeAndACK` to complete the commitment dance. We
10175 // pretend Bob hasn't received the message and check whether he'll disconnect Alice after
10176 // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10177 let alice_revoke_and_ack = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
10178 check_disconnect(&nodes[1]);
10180 // Now, we'll reconnect them to test awaiting a `ChannelReestablish` message.
10182 // Note that since the commitment dance didn't complete above, Alice is expected to resend her
10183 // final `RevokeAndACK` to Bob to complete it.
10184 nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id());
10185 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10186 let bob_init = msgs::Init {
10187 features: nodes[1].node.init_features(), networks: None, remote_network_address: None
10189 nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &bob_init, true).unwrap();
10190 let alice_init = msgs::Init {
10191 features: nodes[0].node.init_features(), networks: None, remote_network_address: None
10193 nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &alice_init, true).unwrap();
10195 // Upon reconnection, Alice sends her `ChannelReestablish` to Bob. Alice, however, hasn't
10196 // received Bob's yet, so she should disconnect him after reaching
10197 // `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10198 let alice_channel_reestablish = get_event_msg!(
10199 nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()
10201 nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &alice_channel_reestablish);
10202 check_disconnect(&nodes[0]);
10204 // Bob now sends his `ChannelReestablish` to Alice to resume the channel and consider it "live".
10205 let bob_channel_reestablish = nodes[1].node.get_and_clear_pending_msg_events().iter().find_map(|event|
10206 if let MessageSendEvent::SendChannelReestablish { node_id, msg } = event {
10207 assert_eq!(*node_id, nodes[0].node.get_our_node_id());
10213 nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bob_channel_reestablish);
10215 // Sanity check that Alice won't disconnect Bob since she's no longer waiting for any messages.
10216 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10217 nodes[0].node.timer_tick_occurred();
10218 check_disconnect_event(&nodes[0], false);
10221 // However, Bob is still waiting on Alice's `RevokeAndACK`, so he should disconnect her after
10222 // reaching `DISCONNECT_PEER_AWAITING_RESPONSE_TICKS`.
10223 check_disconnect(&nodes[1]);
10225 // Finally, have Bob process the last message.
10226 nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &alice_revoke_and_ack);
10227 check_added_monitors(&nodes[1], 1);
10229 // At this point, neither node should attempt to disconnect each other, since they aren't
10230 // waiting on any messages.
10231 for node in &nodes {
10232 for _ in 0..DISCONNECT_PEER_AWAITING_RESPONSE_TICKS {
10233 node.node.timer_tick_occurred();
10234 check_disconnect_event(node, false);
10240 fn test_remove_expired_outbound_unfunded_channels() {
10241 let chanmon_cfgs = create_chanmon_cfgs(2);
10242 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10243 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10244 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10246 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
10247 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10248 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10249 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10250 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10252 let events = nodes[0].node.get_and_clear_pending_events();
10253 assert_eq!(events.len(), 1);
10255 Event::FundingGenerationReady { .. } => (),
10256 _ => panic!("Unexpected event"),
10259 // Asserts the outbound channel has been removed from a nodes[0]'s peer state map.
10260 let check_outbound_channel_existence = |should_exist: bool| {
10261 let per_peer_state = nodes[0].node.per_peer_state.read().unwrap();
10262 let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap();
10263 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10266 // Channel should exist without any timer ticks.
10267 check_outbound_channel_existence(true);
10269 // Channel should exist with 1 timer tick less than required.
10270 for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10271 nodes[0].node.timer_tick_occurred();
10272 check_outbound_channel_existence(true)
10275 // Remove channel after reaching the required ticks.
10276 nodes[0].node.timer_tick_occurred();
10277 check_outbound_channel_existence(false);
10279 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
10280 assert_eq!(msg_events.len(), 1);
10281 match msg_events[0] {
10282 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10283 assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10285 _ => panic!("Unexpected event"),
10287 check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false, &[nodes[1].node.get_our_node_id()], 100000);
10291 fn test_remove_expired_inbound_unfunded_channels() {
10292 let chanmon_cfgs = create_chanmon_cfgs(2);
10293 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10294 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10295 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10297 let temp_channel_id = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
10298 let open_channel_message = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10299 nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_message);
10300 let accept_channel_message = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
10301 nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel_message);
10303 let events = nodes[0].node.get_and_clear_pending_events();
10304 assert_eq!(events.len(), 1);
10306 Event::FundingGenerationReady { .. } => (),
10307 _ => panic!("Unexpected event"),
10310 // Asserts the inbound channel has been removed from a nodes[1]'s peer state map.
10311 let check_inbound_channel_existence = |should_exist: bool| {
10312 let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10313 let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
10314 assert_eq!(chan_lock.channel_by_id.contains_key(&temp_channel_id), should_exist);
10317 // Channel should exist without any timer ticks.
10318 check_inbound_channel_existence(true);
10320 // Channel should exist with 1 timer tick less than required.
10321 for _ in 0..UNFUNDED_CHANNEL_AGE_LIMIT_TICKS - 1 {
10322 nodes[1].node.timer_tick_occurred();
10323 check_inbound_channel_existence(true)
10326 // Remove channel after reaching the required ticks.
10327 nodes[1].node.timer_tick_occurred();
10328 check_inbound_channel_existence(false);
10330 let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
10331 assert_eq!(msg_events.len(), 1);
10332 match msg_events[0] {
10333 MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
10334 assert_eq!(msg.data, "Force-closing pending channel due to timeout awaiting establishment handshake");
10336 _ => panic!("Unexpected event"),
10338 check_closed_event(&nodes[1], 1, ClosureReason::HolderForceClosed, false, &[nodes[0].node.get_our_node_id()], 100000);
10341 fn do_test_multi_post_event_actions(do_reload: bool) {
10342 // Tests handling multiple post-Event actions at once.
10343 // There is specific code in ChannelManager to handle channels where multiple post-Event
10344 // `ChannelMonitorUpdates` are pending at once. This test exercises that code.
10346 // Specifically, we test calling `get_and_clear_pending_events` while there are two
10347 // PaymentSents from different channels and one channel has two pending `ChannelMonitorUpdate`s
10348 // - one from an RAA and one from an inbound commitment_signed.
10349 let chanmon_cfgs = create_chanmon_cfgs(3);
10350 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10351 let (persister, chain_monitor);
10352 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10353 let nodes_0_deserialized;
10354 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10356 let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
10357 let chan_id_2 = create_announced_chan_between_nodes(&nodes, 0, 2).2;
10359 send_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10360 send_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10362 let (our_payment_preimage, our_payment_hash, ..) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
10363 let (payment_preimage_2, payment_hash_2, ..) = route_payment(&nodes[0], &[&nodes[2]], 1_000_000);
10365 nodes[1].node.claim_funds(our_payment_preimage);
10366 check_added_monitors!(nodes[1], 1);
10367 expect_payment_claimed!(nodes[1], our_payment_hash, 1_000_000);
10369 nodes[2].node.claim_funds(payment_preimage_2);
10370 check_added_monitors!(nodes[2], 1);
10371 expect_payment_claimed!(nodes[2], payment_hash_2, 1_000_000);
10373 for dest in &[1, 2] {
10374 let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[*dest], nodes[0].node.get_our_node_id());
10375 nodes[0].node.handle_update_fulfill_htlc(&nodes[*dest].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
10376 commitment_signed_dance!(nodes[0], nodes[*dest], htlc_fulfill_updates.commitment_signed, false);
10377 check_added_monitors(&nodes[0], 0);
10380 let (route, payment_hash_3, _, payment_secret_3) =
10381 get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
10382 let payment_id = PaymentId(payment_hash_3.0);
10383 nodes[1].node.send_payment_with_route(&route, payment_hash_3,
10384 RecipientOnionFields::secret_only(payment_secret_3), payment_id).unwrap();
10385 check_added_monitors(&nodes[1], 1);
10387 let send_event = SendEvent::from_node(&nodes[1]);
10388 nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &send_event.msgs[0]);
10389 nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &send_event.commitment_msg);
10390 assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
10393 let nodes_0_serialized = nodes[0].node.encode();
10394 let chan_0_monitor_serialized = get_monitor!(nodes[0], chan_id).encode();
10395 let chan_1_monitor_serialized = get_monitor!(nodes[0], chan_id_2).encode();
10396 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);
10398 nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10399 nodes[2].node.peer_disconnected(&nodes[0].node.get_our_node_id());
10401 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[1]));
10402 reconnect_nodes(ReconnectArgs::new(&nodes[0], &nodes[2]));
10405 let events = nodes[0].node.get_and_clear_pending_events();
10406 assert_eq!(events.len(), 4);
10407 if let Event::PaymentSent { payment_preimage, .. } = events[0] {
10408 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10409 } else { panic!(); }
10410 if let Event::PaymentSent { payment_preimage, .. } = events[1] {
10411 assert!(payment_preimage == our_payment_preimage || payment_preimage == payment_preimage_2);
10412 } else { panic!(); }
10413 if let Event::PaymentPathSuccessful { .. } = events[2] {} else { panic!(); }
10414 if let Event::PaymentPathSuccessful { .. } = events[3] {} else { panic!(); }
10416 // After the events are processed, the ChannelMonitorUpdates will be released and, upon their
10417 // completion, we'll respond to nodes[1] with an RAA + CS.
10418 get_revoke_commit_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
10419 check_added_monitors(&nodes[0], 3);
10423 fn test_multi_post_event_actions() {
10424 do_test_multi_post_event_actions(true);
10425 do_test_multi_post_event_actions(false);
10429 fn test_batch_channel_open() {
10430 let chanmon_cfgs = create_chanmon_cfgs(3);
10431 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10432 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10433 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10435 // Initiate channel opening and create the batch channel funding transaction.
10436 let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[
10437 (&nodes[1], 100_000, 0, 42, None),
10438 (&nodes[2], 200_000, 0, 43, None),
10441 // Go through the funding_created and funding_signed flow with node 1.
10442 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[0]);
10443 check_added_monitors(&nodes[1], 1);
10444 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
10446 let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10447 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
10448 check_added_monitors(&nodes[0], 1);
10450 // The transaction should not have been broadcast before all channels are ready.
10451 assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 0);
10453 // Go through the funding_created and funding_signed flow with node 2.
10454 nodes[2].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[1]);
10455 check_added_monitors(&nodes[2], 1);
10456 expect_channel_pending_event(&nodes[2], &nodes[0].node.get_our_node_id());
10458 let funding_signed_msg = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10459 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
10460 nodes[0].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &funding_signed_msg);
10461 check_added_monitors(&nodes[0], 1);
10463 // The transaction should not have been broadcast before persisting all monitors has been
10465 assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
10466 assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
10468 // Complete the persistence of the monitor.
10469 nodes[0].chain_monitor.complete_sole_pending_chan_update(
10470 &OutPoint { txid: tx.txid(), index: 1 }.to_channel_id()
10472 let events = nodes[0].node.get_and_clear_pending_events();
10474 // The transaction should only have been broadcast now.
10475 let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
10476 assert_eq!(broadcasted_txs.len(), 1);
10477 assert_eq!(broadcasted_txs[0], tx);
10479 assert_eq!(events.len(), 2);
10480 assert!(events.iter().any(|e| matches!(
10482 crate::events::Event::ChannelPending {
10483 ref counterparty_node_id,
10485 } if counterparty_node_id == &nodes[1].node.get_our_node_id(),
10487 assert!(events.iter().any(|e| matches!(
10489 crate::events::Event::ChannelPending {
10490 ref counterparty_node_id,
10492 } if counterparty_node_id == &nodes[2].node.get_our_node_id(),
10497 fn test_disconnect_in_funding_batch() {
10498 let chanmon_cfgs = create_chanmon_cfgs(3);
10499 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10500 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10501 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10503 // Initiate channel opening and create the batch channel funding transaction.
10504 let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[
10505 (&nodes[1], 100_000, 0, 42, None),
10506 (&nodes[2], 200_000, 0, 43, None),
10509 // Go through the funding_created and funding_signed flow with node 1.
10510 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[0]);
10511 check_added_monitors(&nodes[1], 1);
10512 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
10514 let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10515 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
10516 check_added_monitors(&nodes[0], 1);
10518 // The transaction should not have been broadcast before all channels are ready.
10519 assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
10521 // The remaining peer in the batch disconnects.
10522 nodes[0].node.peer_disconnected(&nodes[2].node.get_our_node_id());
10524 // The channels in the batch will close immediately.
10525 let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10526 let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10527 check_closed_events(&nodes[0], &[
10528 ExpectedCloseEvent {
10529 channel_id: Some(channel_id_1),
10530 discard_funding: true,
10531 ..Default::default()
10533 ExpectedCloseEvent {
10534 channel_id: Some(channel_id_2),
10535 discard_funding: true,
10536 ..Default::default()
10540 // The monitor should become closed.
10541 check_added_monitors(&nodes[0], 1);
10543 let mut monitor_updates = nodes[0].chain_monitor.monitor_updates.lock().unwrap();
10544 let monitor_updates_1 = monitor_updates.get(&channel_id_1).unwrap();
10545 assert_eq!(monitor_updates_1.len(), 1);
10546 assert_eq!(monitor_updates_1[0].update_id, CLOSED_CHANNEL_UPDATE_ID);
10549 // The funding transaction should not have been broadcast, and therefore, we don't need
10550 // to broadcast a force-close transaction for the closed monitor.
10551 assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
10553 // Ensure the channels don't exist anymore.
10554 assert!(nodes[0].node.list_channels().is_empty());
10558 fn test_batch_funding_close_after_funding_signed() {
10559 let chanmon_cfgs = create_chanmon_cfgs(3);
10560 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
10561 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
10562 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
10564 // Initiate channel opening and create the batch channel funding transaction.
10565 let (tx, funding_created_msgs) = create_batch_channel_funding(&nodes[0], &[
10566 (&nodes[1], 100_000, 0, 42, None),
10567 (&nodes[2], 200_000, 0, 43, None),
10570 // Go through the funding_created and funding_signed flow with node 1.
10571 nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[0]);
10572 check_added_monitors(&nodes[1], 1);
10573 expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
10575 let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10576 nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg);
10577 check_added_monitors(&nodes[0], 1);
10579 // Go through the funding_created and funding_signed flow with node 2.
10580 nodes[2].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msgs[1]);
10581 check_added_monitors(&nodes[2], 1);
10582 expect_channel_pending_event(&nodes[2], &nodes[0].node.get_our_node_id());
10584 let funding_signed_msg = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
10585 chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
10586 nodes[0].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &funding_signed_msg);
10587 check_added_monitors(&nodes[0], 1);
10589 // The transaction should not have been broadcast before all channels are ready.
10590 assert_eq!(nodes[0].tx_broadcaster.txn_broadcast().len(), 0);
10592 // Force-close the channel for which we've completed the initial monitor.
10593 let channel_id_1 = OutPoint { txid: tx.txid(), index: 0 }.to_channel_id();
10594 let channel_id_2 = OutPoint { txid: tx.txid(), index: 1 }.to_channel_id();
10595 nodes[0].node.force_close_broadcasting_latest_txn(&channel_id_1, &nodes[1].node.get_our_node_id()).unwrap();
10596 check_added_monitors(&nodes[0], 2);
10598 let mut monitor_updates = nodes[0].chain_monitor.monitor_updates.lock().unwrap();
10599 let monitor_updates_1 = monitor_updates.get(&channel_id_1).unwrap();
10600 assert_eq!(monitor_updates_1.len(), 1);
10601 assert_eq!(monitor_updates_1[0].update_id, CLOSED_CHANNEL_UPDATE_ID);
10602 let monitor_updates_2 = monitor_updates.get(&channel_id_2).unwrap();
10603 assert_eq!(monitor_updates_2.len(), 1);
10604 assert_eq!(monitor_updates_2[0].update_id, CLOSED_CHANNEL_UPDATE_ID);
10606 let msg_events = nodes[0].node.get_and_clear_pending_msg_events();
10607 match msg_events[0] {
10608 MessageSendEvent::HandleError { .. } => (),
10609 _ => panic!("Unexpected message."),
10612 // We broadcast the commitment transaction as part of the force-close.
10614 let broadcasted_txs = nodes[0].tx_broadcaster.txn_broadcast();
10615 assert_eq!(broadcasted_txs.len(), 1);
10616 assert!(broadcasted_txs[0].txid() != tx.txid());
10617 assert_eq!(broadcasted_txs[0].input.len(), 1);
10618 assert_eq!(broadcasted_txs[0].input[0].previous_output.txid, tx.txid());
10621 // All channels in the batch should close immediately.
10622 check_closed_events(&nodes[0], &[
10623 ExpectedCloseEvent {
10624 channel_id: Some(channel_id_1),
10625 discard_funding: true,
10626 ..Default::default()
10628 ExpectedCloseEvent {
10629 channel_id: Some(channel_id_2),
10630 discard_funding: true,
10631 ..Default::default()
10635 // Ensure the channels don't exist anymore.
10636 assert!(nodes[0].node.list_channels().is_empty());
10639 fn do_test_funding_and_commitment_tx_confirm_same_block(confirm_remote_commitment: bool) {
10640 // Tests that a node will forget the channel (when it only requires 1 confirmation) if the
10641 // funding and commitment transaction confirm in the same block.
10642 let chanmon_cfgs = create_chanmon_cfgs(2);
10643 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10644 let mut min_depth_1_block_cfg = test_default_channel_config();
10645 min_depth_1_block_cfg.channel_handshake_config.minimum_depth = 1;
10646 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(min_depth_1_block_cfg), Some(min_depth_1_block_cfg)]);
10647 let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10649 let funding_tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 1_000_000, 0);
10650 let chan_id = chain::transaction::OutPoint { txid: funding_tx.txid(), index: 0 }.to_channel_id();
10652 assert_eq!(nodes[0].node.list_channels().len(), 1);
10653 assert_eq!(nodes[1].node.list_channels().len(), 1);
10655 let (closing_node, other_node) = if confirm_remote_commitment {
10656 (&nodes[1], &nodes[0])
10658 (&nodes[0], &nodes[1])
10661 closing_node.node.force_close_broadcasting_latest_txn(&chan_id, &other_node.node.get_our_node_id()).unwrap();
10662 let mut msg_events = closing_node.node.get_and_clear_pending_msg_events();
10663 assert_eq!(msg_events.len(), 1);
10664 match msg_events.pop().unwrap() {
10665 MessageSendEvent::HandleError { action: msgs::ErrorAction::DisconnectPeer { .. }, .. } => {},
10666 _ => panic!("Unexpected event"),
10668 check_added_monitors(closing_node, 1);
10669 check_closed_event(closing_node, 1, ClosureReason::HolderForceClosed, false, &[other_node.node.get_our_node_id()], 1_000_000);
10671 let commitment_tx = {
10672 let mut txn = closing_node.tx_broadcaster.txn_broadcast();
10673 assert_eq!(txn.len(), 1);
10674 let commitment_tx = txn.pop().unwrap();
10675 check_spends!(commitment_tx, funding_tx);
10679 mine_transactions(&nodes[0], &[&funding_tx, &commitment_tx]);
10680 mine_transactions(&nodes[1], &[&funding_tx, &commitment_tx]);
10682 check_closed_broadcast(other_node, 1, true);
10683 check_added_monitors(other_node, 1);
10684 check_closed_event(other_node, 1, ClosureReason::CommitmentTxConfirmed, false, &[closing_node.node.get_our_node_id()], 1_000_000);
10686 assert!(nodes[0].node.list_channels().is_empty());
10687 assert!(nodes[1].node.list_channels().is_empty());
10691 fn test_funding_and_commitment_tx_confirm_same_block() {
10692 do_test_funding_and_commitment_tx_confirm_same_block(false);
10693 do_test_funding_and_commitment_tx_confirm_same_block(true);