X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Ffunctional_tests.rs;h=e6b9939efd9f0557fd6bb04318c603b26afe18f2;hb=79c8491120ecc2bc305766a8249d9b004e642135;hp=9669c7852ccdf91b755585b028d985c580a6e9b2;hpb=1c7b6c82882d8776563bb03a1f5ef8357d7cdd36;p=rust-lightning diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 9669c785..e6b9939e 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -2988,6 +2988,76 @@ fn test_commitment_revoked_fail_backward_exhaustive_b() { do_test_commitment_revoked_fail_backward_exhaustive(true, false, true); } +#[test] +fn fail_backward_pending_htlc_upon_channel_failure() { + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); + let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::supported(), InitFeatures::supported()); + + // Alice -> Bob: Route a payment but without Bob sending revoke_and_ack. + { + let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]); + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV).unwrap(); + nodes[0].node.send_payment(route, payment_hash).unwrap(); + check_added_monitors!(nodes[0], 1); + + let payment_event = { + let mut events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); + SendEvent::from_event(events.remove(0)) + }; + assert_eq!(payment_event.node_id, nodes[1].node.get_our_node_id()); + assert_eq!(payment_event.msgs.len(), 1); + } + + // Alice -> Bob: Route another payment but now Alice waits for Bob's earlier revoke_and_ack. + let (_, failed_payment_hash) = get_payment_preimage_hash!(nodes[0]); + { + let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV).unwrap(); + nodes[0].node.send_payment(route, failed_payment_hash).unwrap(); + check_added_monitors!(nodes[0], 0); + + assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); + } + + // Alice <- Bob: Send a malformed update_add_htlc so Alice fails the channel. + { + let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV).unwrap(); + let (_, payment_hash) = get_payment_preimage_hash!(nodes[1]); + + let secp_ctx = Secp256k1::new(); + let session_priv = { + let mut session_key = [0; 32]; + let mut rng = thread_rng(); + rng.fill_bytes(&mut session_key); + SecretKey::from_slice(&session_key).expect("RNG is bad!") + }; + + let current_height = nodes[1].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; + let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(&route, current_height).unwrap(); + let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap(); + let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash); + + // Send a 0-msat update_add_htlc to fail the channel. + let update_add_htlc = msgs::UpdateAddHTLC { + channel_id: chan.2, + htlc_id: 0, + amount_msat: 0, + payment_hash, + cltv_expiry, + onion_routing_packet, + }; + nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &update_add_htlc); + } + + // Check that Alice fails backward the pending HTLC from the second payment. + expect_payment_failed!(nodes[0], failed_payment_hash, true); + check_closed_broadcast!(nodes[0], true); + check_added_monitors!(nodes[0], 1); +} + #[test] fn test_htlc_ignore_latest_remote_commitment() { // Test that HTLC transactions spending the latest remote commitment transaction are simply @@ -3839,6 +3909,13 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { create_announced_chan_between_nodes(&nodes, 2, 0, InitFeatures::supported(), InitFeatures::supported()); let (_, _, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 3, InitFeatures::supported(), InitFeatures::supported()); + let mut node_0_stale_monitors_serialized = Vec::new(); + for monitor in nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter() { + let mut writer = test_utils::TestVecWriter(Vec::new()); + monitor.1.write_for_disk(&mut writer).unwrap(); + node_0_stale_monitors_serialized.push(writer.0); + } + let (our_payment_preimage, _) = route_payment(&nodes[2], &[&nodes[0], &nodes[1]], 1000000); // Serialize the ChannelManager here, but the monitor we keep up-to-date @@ -3861,6 +3938,15 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 }; new_chan_monitor = test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), &fee_estimator); nodes[0].chan_monitor = &new_chan_monitor; + + let mut node_0_stale_monitors = Vec::new(); + for serialized in node_0_stale_monitors_serialized.iter() { + let mut read = &serialized[..]; + let (_, monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap(); + assert!(read.is_empty()); + node_0_stale_monitors.push(monitor); + } + let mut node_0_monitors = Vec::new(); for serialized in node_0_monitors_serialized.iter() { let mut read = &serialized[..]; @@ -3869,9 +3955,25 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { node_0_monitors.push(monitor); } - let mut nodes_0_read = &nodes_0_serialized[..]; keys_manager = test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet, Arc::new(test_utils::TestLogger::new())); - let (_, nodes_0_deserialized_tmp) = <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { + + let mut nodes_0_read = &nodes_0_serialized[..]; + if let Err(msgs::DecodeError::InvalidValue) = + <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { + default_config: UserConfig::default(), + keys_manager: &keys_manager, + fee_estimator: &fee_estimator, + monitor: nodes[0].chan_monitor, + tx_broadcaster: nodes[0].tx_broadcaster.clone(), + logger: Arc::new(test_utils::TestLogger::new()), + channel_monitors: &mut node_0_stale_monitors.iter_mut().map(|monitor| { (monitor.get_funding_txo().unwrap(), monitor) }).collect(), + }) { } else { + panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return"); + }; + + let mut nodes_0_read = &nodes_0_serialized[..]; + let (_, nodes_0_deserialized_tmp) = + <(Sha256dHash, ChannelManager)>::read(&mut nodes_0_read, ChannelManagerReadArgs { default_config: UserConfig::default(), keys_manager: &keys_manager, fee_estimator: &fee_estimator, @@ -3977,7 +4079,7 @@ macro_rules! check_spendable_outputs { let local_delaysig = secp_ctx.sign(&sighash, key); spend_tx.input[0].witness.push(local_delaysig.serialize_der().to_vec()); spend_tx.input[0].witness[0].push(SigHashType::All as u8); - spend_tx.input[0].witness.push(vec!(0)); + spend_tx.input[0].witness.push(vec!()); spend_tx.input[0].witness.push(witness_script.clone().into_bytes()); txn.push(spend_tx); },