X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fpriv_short_conf_tests.rs;h=6d1df42d18e20e3bb0c7eaf1223b8343ea06a1d3;hb=8f308f98dda28f54e1c7603be5a440a8c0c101c8;hp=6e2cf2583c43567e41fd5e22697729de9f6c2431;hpb=838d48698384d41e8a9114f4f644dbb83ce5d6fa;p=rust-lightning diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index 6e2cf258..d24554ba 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -11,20 +11,18 @@ //! other behavior that exists only on private channels or with a semi-trusted counterparty (eg //! LSP). -use crate::chain::{ChannelMonitorUpdateStatus, Watch}; -use crate::chain::channelmonitor::ChannelMonitor; -use crate::chain::keysinterface::{Recipient, KeysInterface}; -use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA, PaymentId}; +use crate::chain::ChannelMonitorUpdateStatus; +use crate::sign::NodeSigner; +use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; +use crate::ln::channelmanager::{MIN_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields}; use crate::routing::gossip::RoutingFees; use crate::routing::router::{PaymentParameters, RouteHint, RouteHintHop}; use crate::ln::features::ChannelTypeFeatures; use crate::ln::msgs; use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction}; use crate::ln::wire::Encode; -use crate::util::enforcing_trait_impls::EnforcingSigner; -use crate::util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; -use crate::util::config::UserConfig; -use crate::util::ser::{Writeable, ReadableArgs}; +use crate::util::config::{UserConfig, MaxDustHTLCExposure}; +use crate::util::ser::Writeable; use crate::util::test_utils; use crate::prelude::*; @@ -32,12 +30,8 @@ use core::default::Default; use crate::ln::functional_test_utils::*; -use bitcoin::blockdata::constants::genesis_block; -use bitcoin::hash_types::BlockHash; -use bitcoin::hashes::Hash; -use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::blockdata::constants::ChainHash; use bitcoin::network::constants::Network; -use bitcoin::secp256k1::Secp256k1; #[test] fn test_priv_forwarding_rejection() { @@ -48,14 +42,14 @@ fn test_priv_forwarding_rejection() { let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); let mut no_announce_cfg = test_default_channel_config(); no_announce_cfg.accept_forwards_to_priv_channels = false; + let persister; + let new_chain_monitor; let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]); - let persister: test_utils::TestPersister; - let new_chain_monitor: test_utils::TestChainMonitor; - let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>; + let nodes_1_deserialized; let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); - let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2; - let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.channel_id; + let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000).2; + let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000).0.channel_id; // We should always be able to forward through nodes[1] as long as its out through a public // channel: @@ -72,12 +66,13 @@ fn test_priv_forwarding_rejection() { htlc_maximum_msat: None, }]); let last_hops = vec![route_hint]; - let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(channelmanager::provided_invoice_features()) - .with_route_hints(last_hops); - let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, TEST_FINAL_CLTV); + let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV) + .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap() + .with_route_hints(last_hops).unwrap(); + let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000); - nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap(); + nodes[0].node.send_payment_with_route(&route, our_payment_hash, + RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0)); nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]); @@ -96,56 +91,22 @@ fn test_priv_forwarding_rejection() { // Now disconnect nodes[1] from its peers and restart with accept_forwards_to_priv_channels set // to true. Sadly there is currently no way to change it at runtime. - nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); - nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); + nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id()); + nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id()); let nodes_1_serialized = nodes[1].node.encode(); - let mut monitor_a_serialized = test_utils::TestVecWriter(Vec::new()); - let mut monitor_b_serialized = test_utils::TestVecWriter(Vec::new()); - get_monitor!(nodes[1], chan_id_1).write(&mut monitor_a_serialized).unwrap(); - get_monitor!(nodes[1], chan_id_2).write(&mut monitor_b_serialized).unwrap(); - - persister = test_utils::TestPersister::new(); - let keys_manager = &chanmon_cfgs[1].keys_manager; - new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[1].chain_source), nodes[1].tx_broadcaster.clone(), nodes[1].logger, node_cfgs[1].fee_estimator, &persister, keys_manager); - nodes[1].chain_monitor = &new_chain_monitor; - - let mut monitor_a_read = &monitor_a_serialized.0[..]; - let mut monitor_b_read = &monitor_b_serialized.0[..]; - let (_, mut monitor_a) = <(BlockHash, ChannelMonitor)>::read(&mut monitor_a_read, keys_manager).unwrap(); - let (_, mut monitor_b) = <(BlockHash, ChannelMonitor)>::read(&mut monitor_b_read, keys_manager).unwrap(); - assert!(monitor_a_read.is_empty()); - assert!(monitor_b_read.is_empty()); + let monitor_a_serialized = get_monitor!(nodes[1], chan_id_1).encode(); + let monitor_b_serialized = get_monitor!(nodes[1], chan_id_2).encode(); no_announce_cfg.accept_forwards_to_priv_channels = true; - - let mut nodes_1_read = &nodes_1_serialized[..]; - let (_, nodes_1_deserialized_tmp) = { - let mut channel_monitors = HashMap::new(); - channel_monitors.insert(monitor_a.get_funding_txo().0, &mut monitor_a); - channel_monitors.insert(monitor_b.get_funding_txo().0, &mut monitor_b); - <(BlockHash, ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_1_read, ChannelManagerReadArgs { - default_config: no_announce_cfg, - keys_manager, - fee_estimator: node_cfgs[1].fee_estimator, - chain_monitor: nodes[1].chain_monitor, - tx_broadcaster: nodes[1].tx_broadcaster.clone(), - logger: nodes[1].logger, - channel_monitors, - }).unwrap() - }; - assert!(nodes_1_read.is_empty()); - nodes_1_deserialized = nodes_1_deserialized_tmp; - - assert_eq!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a), - ChannelMonitorUpdateStatus::Completed); - assert_eq!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b), - ChannelMonitorUpdateStatus::Completed); - check_added_monitors!(nodes[1], 2); - nodes[1].node = &nodes_1_deserialized; - - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + reload_node!(nodes[1], no_announce_cfg, &nodes_1_serialized, &[&monitor_a_serialized, &monitor_b_serialized], persister, new_chain_monitor, nodes_1_deserialized); + + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { + features: nodes[1].node.init_features(), networks: None, remote_network_address: None + }, true).unwrap(); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { + features: nodes[0].node.init_features(), networks: None, remote_network_address: None + }, false).unwrap(); let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap(); let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap(); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish); @@ -153,8 +114,12 @@ fn test_priv_forwarding_rejection() { get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); - nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); - nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { + features: nodes[2].node.init_features(), networks: None, remote_network_address: None + }, true).unwrap(); + nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { + features: nodes[1].node.init_features(), networks: None, remote_network_address: None + }, false).unwrap(); let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[2]).pop().unwrap(); let cs_reestablish = get_chan_reestablish_msgs!(nodes[2], nodes[1]).pop().unwrap(); nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish); @@ -162,7 +127,8 @@ fn test_priv_forwarding_rejection() { get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); - nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap(); + nodes[0].node.send_payment_with_route(&route, our_payment_hash, + RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 10_000, our_payment_hash, our_payment_secret); claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], our_payment_preimage); @@ -175,17 +141,19 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) { alice_config.channel_handshake_config.minimum_depth = 1; alice_config.channel_handshake_config.announced_channel = true; alice_config.channel_handshake_limits.force_announced_channel_preference = false; + alice_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253); let mut bob_config = UserConfig::default(); bob_config.channel_handshake_config.minimum_depth = 1; bob_config.channel_handshake_config.announced_channel = true; bob_config.channel_handshake_limits.force_announced_channel_preference = false; + bob_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253); 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, &[Some(alice_config), Some(bob_config)]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); *nodes[0].connect_style.borrow_mut() = connect_style; - let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001); mine_transaction(&nodes[1], &tx); nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id())); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); @@ -228,14 +196,14 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) { msg.clone() } else { panic!("Unexpected event"); }; let (bs_announcement, bs_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = bs_announce_events[1] { - (msg.clone(), update_msg.clone()) + (msg.clone(), update_msg.clone().unwrap()) } else { panic!("Unexpected event"); }; nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs); let as_announce_events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(as_announce_events.len(), 1); let (announcement, as_update) = if let MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } = as_announce_events[0] { - (msg.clone(), update_msg.clone()) + (msg.clone(), update_msg.clone().unwrap()) } else { panic!("Unexpected event"); }; assert_eq!(announcement, bs_announcement); @@ -262,8 +230,8 @@ fn test_routed_scid_alias() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]); let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2; - let mut as_channel_ready = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0; + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000).2; + let mut as_channel_ready = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000).0; let last_hop = nodes[2].node.list_usable_channels(); let hop_hints = vec![RouteHint(vec![RouteHintHop { @@ -277,15 +245,23 @@ fn test_routed_scid_alias() { htlc_maximum_msat: None, htlc_minimum_msat: None, }])]; - let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(channelmanager::provided_invoice_features()) - .with_route_hints(hop_hints); - let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42); - assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); - nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap(); + let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42) + .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap() + .with_route_hints(hop_hints).unwrap(); + let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000); + assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); + nodes[0].node.send_payment_with_route(&route, payment_hash, + RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret); + + as_channel_ready.short_channel_id_alias = Some(0xeadbeef); + nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready); + // Note that we always respond to a channel_ready with a channel_update. Not a lot of reason + // to bother updating that code, so just drop the message here. + get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); + claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); // Now test that if a peer sends us a second channel_ready after the channel is operational we @@ -321,8 +297,8 @@ fn test_scid_privacy_on_pub_channel() { open_channel.channel_type.as_mut().unwrap().set_scid_privacy_required(); assert_eq!(open_channel.channel_flags & 1, 1); // The `announce_channel` bit is set. - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); - let err = get_err_msg!(nodes[1], nodes[0].node.get_our_node_id()); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); + let err = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()); assert_eq!(err.data, "SCID Alias/Privacy Channel Type cannot be set on a public channel"); } @@ -354,8 +330,8 @@ fn test_scid_privacy_negotiation() { let second_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); assert!(!second_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &second_open_channel); - nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &second_open_channel); + 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())); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); @@ -379,7 +355,7 @@ fn test_inbound_scid_privacy() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]); let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); let mut no_announce_cfg = test_default_channel_config(); no_announce_cfg.channel_handshake_config.announced_channel = false; @@ -389,9 +365,9 @@ fn test_inbound_scid_privacy() { assert!(open_channel.channel_type.as_ref().unwrap().requires_scid_privacy()); - nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); + nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_channel); let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id()); - nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); + nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), &accept_channel); let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], &nodes[2].node.get_our_node_id(), 100_000, 42); nodes[1].node.funding_transaction_generated(&temporary_channel_id, &nodes[2].node.get_our_node_id(), tx.clone()).unwrap(); @@ -399,7 +375,10 @@ fn test_inbound_scid_privacy() { check_added_monitors!(nodes[2], 1); let cs_funding_signed = get_event_msg!(nodes[2], MessageSendEvent::SendFundingSigned, nodes[1].node.get_our_node_id()); + expect_channel_pending_event(&nodes[2], &nodes[1].node.get_our_node_id()); + nodes[1].node.handle_funding_signed(&nodes[2].node.get_our_node_id(), &cs_funding_signed); + expect_channel_pending_event(&nodes[1], &nodes[2].node.get_our_node_id()); check_added_monitors!(nodes[1], 1); let conf_height = core::cmp::max(nodes[1].best_block_info().1 + 1, nodes[2].best_block_info().1 + 1); @@ -432,12 +411,13 @@ fn test_inbound_scid_privacy() { htlc_maximum_msat: None, htlc_minimum_msat: None, }])]; - let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(channelmanager::provided_invoice_features()) - .with_route_hints(hop_hints.clone()); - let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42); - assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); - nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap(); + let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42) + .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap() + .with_route_hints(hop_hints.clone()).unwrap(); + let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000); + assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); + nodes[0].node.send_payment_with_route(&route, payment_hash, + RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret); @@ -447,12 +427,13 @@ fn test_inbound_scid_privacy() { // what channel we're talking about. hop_hints[0].0[0].short_channel_id = last_hop[0].short_channel_id.unwrap(); - let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(channelmanager::provided_invoice_features()) - .with_route_hints(hop_hints); - let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42); - assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap()); - nodes[0].node.send_payment(&route_2, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap(); + let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42) + .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap() + .with_route_hints(hop_hints).unwrap(); + let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000); + assert_eq!(route_2.paths[0].hops[1].short_channel_id, last_hop[0].short_channel_id.unwrap()); + nodes[0].node.send_payment_with_route(&route_2, payment_hash_2, + RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap(); check_added_monitors!(nodes[0], 1); let payment_event = SendEvent::from_node(&nodes[0]); @@ -460,7 +441,7 @@ fn test_inbound_scid_privacy() { nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]); commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, true, true); - nodes[1].logger.assert_log_regex("lightning::ln::channelmanager".to_string(), regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1); + nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1); let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]); @@ -483,8 +464,8 @@ fn test_scid_alias_returned() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]); let nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); - let chan = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0); + let chan = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0); let last_hop = nodes[2].node.list_usable_channels(); let mut hop_hints = vec![RouteHint(vec![RouteHintHop { @@ -498,16 +479,17 @@ fn test_scid_alias_returned() { htlc_maximum_msat: None, htlc_minimum_msat: None, }])]; - let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(channelmanager::provided_invoice_features()) - .with_route_hints(hop_hints); - let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42); - assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap()); + let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42) + .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap() + .with_route_hints(hop_hints).unwrap(); + let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000); + assert_eq!(route.paths[0].hops[1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap()); - route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value + route.paths[0].hops[1].fee_msat = 10_000_000; // Overshoot the last channel's value // Route the HTLC through to the destination. - nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap(); + nodes[0].node.send_payment_with_route(&route, payment_hash, + RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]); @@ -523,7 +505,7 @@ fn test_scid_alias_returned() { // Build the expected channel update let contents = msgs::UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: last_hop[0].inbound_scid_alias.unwrap(), timestamp: 21, flags: 1, @@ -534,8 +516,7 @@ fn test_scid_alias_returned() { fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths, excess_data: Vec::new(), }; - let msg_hash = Sha256dHash::hash(&contents.encode()[..]); - let signature = Secp256k1::new().sign_ecdsa(&hash_to_message!(&msg_hash[..]), &nodes[1].keys_manager.get_node_secret(Recipient::Node).unwrap()); + let signature = nodes[1].keys_manager.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelUpdate(&contents)).unwrap(); let msg = msgs::ChannelUpdate { signature, contents }; let mut err_data = Vec::new(); @@ -547,11 +528,12 @@ fn test_scid_alias_returned() { PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap()) .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data)); - route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount - route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0 + route.paths[0].hops[1].fee_msat = 10_000; // Reset to the correct payment amount + route.paths[0].hops[0].fee_msat = 0; // But set fee paid to the middle hop to 0 // Route the HTLC through to the destination. - nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap(); + nodes[0].node.send_payment_with_route(&route, payment_hash, + RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); let as_updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_updates.update_add_htlcs[0]); @@ -606,13 +588,13 @@ fn test_0conf_channel_with_async_monitor() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(chan_config), None]); let nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0); chan_config.channel_handshake_config.announced_channel = false; nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(chan_config)).unwrap(); let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { @@ -624,7 +606,7 @@ fn test_0conf_channel_with_async_monitor() { let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()); assert_eq!(accept_channel.minimum_depth, 0); - nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel); let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap(); @@ -637,6 +619,7 @@ fn test_0conf_channel_with_async_monitor() { let channel_id = funding_output.to_channel_id(); nodes[1].chain_monitor.complete_sole_pending_chan_update(&channel_id); + expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id()); let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events(); assert_eq!(bs_signed_locked.len(), 2); @@ -662,6 +645,22 @@ fn test_0conf_channel_with_async_monitor() { assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); nodes[0].chain_monitor.complete_sole_pending_chan_update(&channel_id); + + let events = nodes[0].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 2); + match events[0] { + crate::events::Event::ChannelPending { ref counterparty_node_id, .. } => { + assert_eq!(nodes[1].node.get_our_node_id(), *counterparty_node_id); + }, + _ => panic!("Unexpected event"), + } + match events[1] { + crate::events::Event::ChannelReady { ref counterparty_node_id, .. } => { + assert_eq!(nodes[1].node.get_our_node_id(), *counterparty_node_id); + }, + _ => panic!("Unexpected event"), + } + let as_locked_update = nodes[0].node.get_and_clear_pending_msg_events(); // Note that the funding transaction is actually released when @@ -676,7 +675,6 @@ fn test_0conf_channel_with_async_monitor() { } _ => panic!("Unexpected event"), } - expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id()); expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id()); let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); @@ -704,7 +702,8 @@ fn test_0conf_channel_with_async_monitor() { // failure before we've ever confirmed the funding transaction. This previously caused a panic. let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000); - nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret), PaymentId(payment_hash.0)).unwrap(); + nodes[0].node.send_payment_with_route(&route, payment_hash, + RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap(); check_added_monitors!(nodes[0], 1); let as_send = SendEvent::from_node(&nodes[0]); @@ -735,7 +734,7 @@ fn test_0conf_channel_with_async_monitor() { nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_send.msgs[0]); commitment_signed_dance!(nodes[2], nodes[1], bs_send.commitment_msg, false); expect_pending_htlcs_forwardable!(nodes[2]); - expect_payment_received!(nodes[2], payment_hash, payment_secret, 1_000_000); + expect_payment_claimable!(nodes[2], payment_hash, payment_secret, 1_000_000); claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); confirm_transaction(&nodes[0], &tx); @@ -765,8 +764,8 @@ fn test_0conf_close_no_early_chan_update() { nodes[0].node.force_close_all_channels_broadcasting_latest_txn(); check_added_monitors!(nodes[0], 1); - check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed); - let _ = get_err_msg!(nodes[0], nodes[1].node.get_our_node_id()); + check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000); + let _ = get_err_msg(&nodes[0], &nodes[1].node.get_our_node_id()); } #[test] @@ -802,7 +801,7 @@ fn test_public_0conf_channel() { match bs_announcement[0] { MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => { announcement = msg.clone(); - bs_update = update_msg.clone(); + bs_update = update_msg.clone().unwrap(); }, _ => panic!("Unexpected event"), }; @@ -812,6 +811,7 @@ fn test_public_0conf_channel() { match as_announcement[0] { MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => { assert!(announcement == *msg); + let update_msg = update_msg.as_ref().unwrap(); assert_eq!(update_msg.contents.short_channel_id, scid); assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id); assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id); @@ -849,7 +849,7 @@ fn test_0conf_channel_reorg() { assert_eq!(nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(), real_scid); let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000); - assert_eq!(route.paths[0][0].short_channel_id, real_scid); + assert_eq!(route.paths[0].hops[0].short_channel_id, real_scid); send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1]]], 10_000, payment_hash, payment_secret); claim_payment(&nodes[0], &[&nodes[1]], payment_preimage); @@ -861,12 +861,14 @@ fn test_0conf_channel_reorg() { // now we force-close the channel here. check_closed_event!(&nodes[0], 1, ClosureReason::ProcessingError { err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned() - }); + }, [nodes[1].node.get_our_node_id()], 100000); check_closed_broadcast!(nodes[0], true); + check_added_monitors(&nodes[0], 1); check_closed_event!(&nodes[1], 1, ClosureReason::ProcessingError { err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned() - }); + }, [nodes[0].node.get_our_node_id()], 100000); check_closed_broadcast!(nodes[1], true); + check_added_monitors(&nodes[1], 1); } #[test] @@ -885,7 +887,7 @@ fn test_zero_conf_accept_reject() { open_channel_msg.channel_type = Some(channel_type_features.clone()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel_msg); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); match msg_events[0] { @@ -913,8 +915,7 @@ fn test_zero_conf_accept_reject() { open_channel_msg.channel_type = Some(channel_type_features.clone()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), - &open_channel_msg); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`. assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); @@ -946,8 +947,7 @@ fn test_zero_conf_accept_reject() { open_channel_msg.channel_type = Some(channel_type_features); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), - &open_channel_msg); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); let events = nodes[1].node.get_and_clear_pending_events(); @@ -985,7 +985,7 @@ fn test_connect_before_funding() { nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_001, 42, None).unwrap(); let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel); let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { @@ -997,7 +997,7 @@ fn test_connect_before_funding() { let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()); assert_eq!(accept_channel.minimum_depth, 0); - nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); @@ -1009,3 +1009,38 @@ fn test_connect_before_funding() { connect_blocks(&nodes[0], 1); connect_blocks(&nodes[1], 1); } + +#[test] +fn test_0conf_ann_sigs_racing_conf() { + // Previously we had a bug where we'd panic when receiving a counterparty's + // announcement_signatures message for a 0conf channel pending confirmation on-chain. Here we + // check that we just error out, ignore the announcement_signatures message, and proceed + // instead. + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let mut chan_config = test_default_channel_config(); + chan_config.manually_accept_inbound_channels = true; + + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]); + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + + // This is the default but we force it on anyway + chan_config.channel_handshake_config.announced_channel = true; + let (tx, ..) = open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config)); + + // We can use the channel immediately, but we can't announce it until we get 6+ confirmations + send_payment(&nodes[0], &[&nodes[1]], 100_000); + + let scid = confirm_transaction(&nodes[0], &tx); + let as_announcement_sigs = get_event_msg!(nodes[0], MessageSendEvent::SendAnnouncementSignatures, nodes[1].node.get_our_node_id()); + + // Handling the announcement_signatures prior to the first confirmation would panic before. + nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs); + + assert_eq!(confirm_transaction(&nodes[1], &tx), scid); + let bs_announcement_sigs = get_event_msg!(nodes[1], MessageSendEvent::SendAnnouncementSignatures, nodes[0].node.get_our_node_id()); + + nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs); + let as_announcement = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(as_announcement.len(), 1); +}