From: Gleb Naumenko Date: Sat, 2 May 2020 19:05:04 +0000 (-0400) Subject: Split routing from getting network messages X-Git-Tag: v0.0.12~66^2~11 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=e553d2c2c0280bc4e82b4685679e79307146b129;p=rust-lightning Split routing from getting network messages --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index cf8a86680..b49248d0c 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -23,7 +23,8 @@ use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface}; use lightning::ln::channelmonitor; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; -use lightning::routing::router::Router; +use lightning::routing::router::get_route; +use lightning::routing::network_graph::NetGraphMsgHandler; use lightning::util::events::{EventsProvider,Event}; use lightning::util::enforcing_trait_impls::EnforcingChannelKeys; use lightning::util::logger::Logger; @@ -332,12 +333,13 @@ pub fn do_test(data: &[u8], logger: &Arc) { config.channel_options.announced_channel = get_slice!(1)[0] != 0; config.peer_channel_config_limits.min_dust_limit_satoshis = 0; let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone(), broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()); - let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger))); + let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()); + let net_graph_msg_handler = Arc::new(NetGraphMsgHandler::new(watch.clone(), Arc::clone(&logger))); let peers = RefCell::new([false; 256]); let mut loss_detector = MoneyLossDetector::new(&peers, channelmanager.clone(), monitor.clone(), PeerManager::new(MessageHandler { chan_handler: channelmanager.clone(), - route_handler: router.clone(), + route_handler: net_graph_msg_handler.clone(), }, our_network_key, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger))); let mut should_forward = false; @@ -389,7 +391,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { }, 4 => { let value = slice_to_be24(get_slice!(3)) as u64; - let route = match router.get_route(&get_pubkey!(), None, &Vec::new(), value, 42) { + let route = match get_route(&our_id, &net_graph_msg_handler, &get_pubkey!(), None, &Vec::new(), value, 42, Arc::clone(&logger)) { Ok(route) => route, Err(_) => return, }; @@ -406,7 +408,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { }, 15 => { let value = slice_to_be24(get_slice!(3)) as u64; - let mut route = match router.get_route(&get_pubkey!(), None, &Vec::new(), value, 42) { + let mut route = match get_route(&our_id, &net_graph_msg_handler, &get_pubkey!(), None, &Vec::new(), value, 42, Arc::clone(&logger)) { Ok(route) => route, Err(_) => return, }; diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index 7dd439bef..dbf5ad885 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -8,9 +8,10 @@ use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::features::InitFeatures; use lightning::ln::msgs; use lightning::ln::msgs::RoutingMessageHandler; -use lightning::routing::router::{Router, RouteHint}; +use lightning::routing::router::{get_route, RouteHint}; use lightning::util::logger::Logger; use lightning::util::ser::Readable; +use lightning::routing::network_graph::{NetGraphMsgHandler, RoutingFees}; use bitcoin::secp256k1::key::PublicKey; @@ -156,7 +157,7 @@ pub fn do_test(data: &[u8], out: Out) { }); let our_pubkey = get_pubkey!(); - let router = Router::new(our_pubkey.clone(), chain_monitor, Arc::clone(&logger)); + let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger)); loop { match get_slice!(1)[0] { @@ -166,22 +167,22 @@ pub fn do_test(data: &[u8], out: Out) { if addr_len > (37+1)*4 { return; } - let _ = router.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288)); + let _ = net_graph_msg_handler.handle_node_announcement(&decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288)); }, 1 => { - let _ = router.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4)); + let _ = net_graph_msg_handler.handle_channel_announcement(&decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4)); }, 2 => { - let _ = router.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128)); + let _ = net_graph_msg_handler.handle_channel_update(&decode_msg!(msgs::ChannelUpdate, 128)); }, 3 => { match get_slice!(1)[0] { 0 => { - router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)}); + net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelUpdateMessage {msg: decode_msg!(msgs::ChannelUpdate, 128)}); }, 1 => { let short_channel_id = slice_to_be64(get_slice!(8)); - router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id, is_permanent: false}); + net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed {short_channel_id, is_permanent: false}); }, _ => return, } @@ -217,15 +218,16 @@ pub fn do_test(data: &[u8], out: Out) { last_hops_vec.push(RouteHint { src_node_id: get_pubkey!(), short_channel_id: slice_to_be64(get_slice!(8)), - fee_base_msat: slice_to_be32(get_slice!(4)), - fee_proportional_millionths: slice_to_be32(get_slice!(4)), - cltv_expiry_delta: slice_to_be16(get_slice!(2)), + fees: RoutingFees { + base_msat: slice_to_be32(get_slice!(4)), + proportional_millionths: slice_to_be32(get_slice!(4)), + }, cltv_expiry_delta: slice_to_be16(get_slice!(2)), htlc_minimum_msat: slice_to_be64(get_slice!(8)), }); } &last_hops_vec[..] }; - let _ = router.get_route(&target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4))); + let _ = get_route(&our_pubkey, &net_graph_msg_handler, &target, first_hops, last_hops, slice_to_be64(get_slice!(8)), slice_to_be32(get_slice!(4)), Arc::clone(&logger)); }, _ => return, } diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 411c12d99..cae757347 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -9,6 +9,7 @@ use ln::channelmonitor::ChannelMonitorUpdateErr; use ln::features::InitFeatures; use ln::msgs; use ln::msgs::{ChannelMessageHandler, ErrorAction, RoutingMessageHandler}; +use routing::router::get_route; use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider}; use util::errors::APIError; @@ -17,6 +18,9 @@ use bitcoin::hashes::Hash; use ln::functional_test_utils::*; +use std::sync::Arc; +use util::test_utils; + #[test] fn test_simple_monitor_permanent_update_fail() { // Test that we handle a simple permanent monitor update failure @@ -25,11 +29,13 @@ fn test_simple_monitor_permanent_update_fail() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (_, payment_hash_1) = get_payment_preimage_hash!(&nodes[0]); *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::PermanentFailure); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &None), true, APIError::ChannelUnavailable {..}, {}); check_added_monitors!(nodes[0], 2); @@ -58,14 +64,18 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(&nodes[0]); *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); - unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &None), false, APIError::MonitorUpdateFailed, {}); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_1, &None), false, APIError::MonitorUpdateFailed, {}); + check_added_monitors!(nodes[0], 1); + } assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); @@ -106,9 +116,13 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool) { // Now set it to failed again... let (_, payment_hash_2) = get_payment_preimage_hash!(&nodes[0]); - *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); - unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &None), false, APIError::MonitorUpdateFailed, {}); - check_added_monitors!(nodes[0], 1); + { + *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &None), false, APIError::MonitorUpdateFailed, {}); + check_added_monitors!(nodes[0], 1); + } assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); @@ -162,16 +176,19 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000); // Now try to send a second payment which will fail to send - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - - *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); - unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &None), false, APIError::MonitorUpdateFailed, {}); - check_added_monitors!(nodes[0], 1); + { + *nodes[0].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash_2, &None), false, APIError::MonitorUpdateFailed, {}); + check_added_monitors!(nodes[0], 1); + } assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); @@ -494,11 +511,15 @@ fn test_monitor_update_fail_cs() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let send_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(), &send_event.msgs[0]); @@ -579,11 +600,15 @@ fn test_monitor_update_fail_no_rebroadcast() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let send_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(), &send_event.msgs[0]); @@ -625,19 +650,25 @@ fn test_monitor_update_raa_while_paused() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); send_payment(&nodes[0], &[&nodes[1]], 5000000, 5_000_000); - - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, our_payment_hash_1) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, our_payment_hash_1, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, our_payment_hash_1, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let send_event_1 = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0)); - let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, our_payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[1].node.send_payment(&route, our_payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[1], 1); + { + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[1].node.send_payment(&route, our_payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[1], 1); + } let send_event_2 = SendEvent::from_event(nodes[1].node.get_and_clear_pending_msg_events().remove(0)); nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event_1.msgs[0]); @@ -697,6 +728,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); // Rebalance a bit so that we can send backwards from 2 to 1. send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000); @@ -723,9 +755,12 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { // While the second channel is AwaitingRAA, forward a second payment to get it into the // holding cell. let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); - nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let mut send_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(), &send_event.msgs[0]); @@ -746,11 +781,13 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { // Attempt to forward a third payment but fail due to the second channel being unavailable // for forwarding. - let (_, payment_hash_3) = get_payment_preimage_hash!(nodes[0]); - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); - nodes[0].node.send_payment(&route, payment_hash_3, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_3, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } *nodes[1].chan_monitor.update_ret.lock().unwrap() = Ok(()); // We succeed in updating the monitor for the first channel send_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0)); @@ -795,7 +832,8 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) { let (payment_preimage_4, payment_hash_4) = if test_ignore_second_cs { // Try to route another payment backwards from 2 to make sure 1 holds off on responding let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[0]); - let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[2].net_graph_msg_handler; + let route = get_route(&nodes[2].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[2].node.send_payment(&route, payment_hash_4, &None).unwrap(); check_added_monitors!(nodes[2], 1); @@ -1036,8 +1074,8 @@ fn raa_no_response_awaiting_raa_state() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]); @@ -1047,10 +1085,14 @@ fn raa_no_response_awaiting_raa_state() { // immediately after a CS. By setting failing the monitor update failure from the CS (which // requires only an RAA response due to AwaitingRAA) we can deliver the RAA and require the CS // generation during RAA while in monitor-update-failed state. - nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); - check_added_monitors!(nodes[0], 1); - nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[0], 0); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[0], 0); + } let mut events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); @@ -1097,10 +1139,13 @@ fn raa_no_response_awaiting_raa_state() { // We send a third payment here, which is somewhat of a redundant test, but the // chanmon_fail_consistency test required it to actually find the bug (by seeing out-of-sync // commitment transaction states) whereas here we can explicitly check for it. - nodes[0].node.send_payment(&route, payment_hash_3, &None).unwrap(); - check_added_monitors!(nodes[0], 0); - assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); - + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_3, &None).unwrap(); + check_added_monitors!(nodes[0], 0); + assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); + } nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_responses.0); check_added_monitors!(nodes[0], 1); let mut events = nodes[0].node.get_and_clear_pending_msg_events(); @@ -1153,6 +1198,7 @@ fn claim_while_disconnected_monitor_update_fail() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); // Forward a payment for B to claim let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000); @@ -1184,10 +1230,13 @@ fn claim_while_disconnected_monitor_update_fail() { // Send a second payment from A to B, resulting in a commitment update that gets swallowed with // the monitor still failed - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_2, &None).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]); @@ -1273,13 +1322,17 @@ fn monitor_failed_no_reestablish_response() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); // Route the payment and deliver the initial commitment_signed (with a monitor update failure // on receipt). - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } *nodes[1].chan_monitor.update_ret.lock().unwrap() = Err(ChannelMonitorUpdateErr::TemporaryFailure); let mut events = nodes[0].node.get_and_clear_pending_msg_events(); @@ -1343,13 +1396,17 @@ fn first_message_on_recv_ordering() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); // Route the first payment outbound, holding the last RAA for B until we are set up so that we // can deliver it and fail the monitor update. - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let mut events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); @@ -1368,10 +1425,13 @@ fn first_message_on_recv_ordering() { let as_raa = get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id()); // Route the second payment, generating an update_add_htlc/commitment_signed - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let mut events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); let payment_event = SendEvent::from_event(events.pop().unwrap()); @@ -1434,6 +1494,7 @@ fn test_monitor_update_fail_claim() { let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); // Rebalance a bit so that we can send backwards from 3 to 2. send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000); @@ -1444,10 +1505,13 @@ fn test_monitor_update_fail_claim() { assert!(nodes[1].node.claim_funds(payment_preimage_1, &None, 1_000_000)); check_added_monitors!(nodes[1], 1); - let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (_, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[2].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[2], 1); + { + let net_graph_msg_handler = &nodes[2].net_graph_msg_handler; + let route = get_route(&nodes[2].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[2].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[2], 1); + } // Successfully update the monitor on the 1<->2 channel, but the 0<->1 channel should still be // paused, so forward shouldn't succeed until we call channel_monitor_updated(). @@ -1511,6 +1575,7 @@ fn test_monitor_update_on_pending_forwards() { let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); // Rebalance a bit so that we can send backwards from 3 to 1. send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000, 5_000_000); @@ -1525,10 +1590,13 @@ fn test_monitor_update_on_pending_forwards() { commitment_signed_dance!(nodes[1], nodes[2], cs_fail_update.commitment_signed, true, true); assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); - let route = nodes[2].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[2].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[2], 1); + { + let net_graph_msg_handler = &nodes[2].net_graph_msg_handler; + let route = get_route(&nodes[2].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[2].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[2], 1); + } let mut events = nodes[2].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); @@ -1579,15 +1647,19 @@ fn monitor_update_claim_fail_no_response() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2; + let logger = Arc::new(test_utils::TestLogger::new()); // Forward a payment for B to claim let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000); // Now start forwarding a second payment, skipping the last RAA so B is in AwaitingRAA - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); - check_added_monitors!(nodes[0], 1); + { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); + check_added_monitors!(nodes[0], 1); + } let mut events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 1); @@ -1716,9 +1788,9 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &funding_locked)) }; for node in nodes.iter() { - assert!(node.router.handle_channel_announcement(&announcement).unwrap()); - node.router.handle_channel_update(&as_update).unwrap(); - node.router.handle_channel_update(&bs_update).unwrap(); + assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap()); + node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap(); + node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap(); } send_payment(&nodes[0], &[&nodes[1]], 8000000, 8_000_000); @@ -1745,10 +1817,11 @@ fn test_path_paused_mpp() { let (chan_2_ann, _, chan_2_id, _) = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()); let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id; let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id; + let logger = Arc::new(test_utils::TestLogger::new()); let (payment_preimage, payment_hash) = get_payment_preimage_hash!(&nodes[0]); let payment_secret = PaymentSecret([0xdb; 32]); - let mut route = nodes[0].router.get_route(&nodes[3].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let mut route = get_route(&nodes[0].node.get_our_node_id(), &nodes[0].net_graph_msg_handler, &nodes[3].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); // Set us up to take multiple routes, one 0 -> 1 -> 3 and one 0 -> 2 -> 3: let path = route.paths[0].clone(); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 4d7f5aa8b..8e7e3a050 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -819,7 +819,7 @@ impl ChannelMan } /// Gets the list of usable channels, in random order. Useful as an argument to - /// Router::get_route to ensure non-announced channels are used. + /// get_route to ensure non-announced channels are used. /// /// These are guaranteed to have their is_live value set to true, see the documentation for /// ChannelDetails::is_live for more info on exactly what the criteria are. @@ -1869,7 +1869,7 @@ impl ChannelMan .. } => { // we get a fail_malformed_htlc from the first hop // TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary - // failures here, but that would be insufficient as Router::get_route + // failures here, but that would be insufficient as get_route // generally ignores its view of our own channels as we provide them via // ChannelDetails. // TODO: For non-temporary failures, we really should be closing the diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 50dd6b956..53f86c279 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -6,7 +6,8 @@ use chain::transaction::OutPoint; use chain::keysinterface::KeysInterface; use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentPreimage, PaymentHash, PaymentSecret, PaymentSendFailure}; use ln::channelmonitor::{ChannelMonitor, ManyChannelMonitor}; -use routing::router::{Route, Router, RouterReadArgs}; +use routing::router::{Route, get_route}; +use routing::network_graph::{NetGraphMsgHandler, NetGraphMsgHandlerReadArgs}; use ln::features::InitFeatures; use ln::msgs; use ln::msgs::{ChannelMessageHandler,RoutingMessageHandler}; @@ -82,7 +83,8 @@ pub struct Node<'a, 'b: 'a, 'c: 'b> { pub chan_monitor: &'b test_utils::TestChannelMonitor<'c>, pub keys_manager: &'b test_utils::TestKeysInterface, pub node: &'a ChannelManager, &'c test_utils::TestBroadcaster, &'b test_utils::TestKeysInterface, &'c test_utils::TestFeeEstimator>, - pub router: Router, + pub net_graph_msg_handler: NetGraphMsgHandler, + pub our_node_id: PublicKey, pub node_seed: [u8; 32], pub network_payment_count: Rc>, pub network_chan_count: Rc>, @@ -100,15 +102,15 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> { // Check that if we serialize the Router, we can deserialize it again. { let mut w = test_utils::TestVecWriter(Vec::new()); - self.router.write(&mut w).unwrap(); - let deserialized_router = Router::read(&mut ::std::io::Cursor::new(&w.0), RouterReadArgs { + self.net_graph_msg_handler.write(&mut w).unwrap(); + let net_graph_msg_handler = NetGraphMsgHandler::read(&mut ::std::io::Cursor::new(&w.0), NetGraphMsgHandlerReadArgs { chain_monitor: Arc::clone(&self.chain_monitor) as Arc, logger: Arc::clone(&self.logger) as Arc }).unwrap(); let mut chan_progress = 0; loop { - let orig_announcements = self.router.get_next_channel_announcements(chan_progress, 255); - let deserialized_announcements = deserialized_router.get_next_channel_announcements(chan_progress, 255); + let orig_announcements = self.net_graph_msg_handler.get_next_channel_announcements(chan_progress, 255); + let deserialized_announcements = net_graph_msg_handler.get_next_channel_announcements(chan_progress, 255); assert!(orig_announcements == deserialized_announcements); chan_progress = match orig_announcements.last() { Some(announcement) => announcement.0.contents.short_channel_id + 1, @@ -117,8 +119,8 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> { } let mut node_progress = None; loop { - let orig_announcements = self.router.get_next_node_announcements(node_progress.as_ref(), 255); - let deserialized_announcements = deserialized_router.get_next_node_announcements(node_progress.as_ref(), 255); + let orig_announcements = self.net_graph_msg_handler.get_next_node_announcements(node_progress.as_ref(), 255); + let deserialized_announcements = net_graph_msg_handler.get_next_node_announcements(node_progress.as_ref(), 255); assert!(orig_announcements == deserialized_announcements); node_progress = match orig_announcements.last() { Some(announcement) => Some(announcement.contents.node_id), @@ -461,11 +463,11 @@ pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: &'a }; for node in nodes { - assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap()); - node.router.handle_channel_update(&chan_announcement.1).unwrap(); - node.router.handle_channel_update(&chan_announcement.2).unwrap(); - node.router.handle_node_announcement(&a_node_announcement).unwrap(); - node.router.handle_node_announcement(&b_node_announcement).unwrap(); + assert!(node.net_graph_msg_handler.handle_channel_announcement(&chan_announcement.0).unwrap()); + node.net_graph_msg_handler.handle_channel_update(&chan_announcement.1).unwrap(); + node.net_graph_msg_handler.handle_channel_update(&chan_announcement.2).unwrap(); + node.net_graph_msg_handler.handle_node_announcement(&a_node_announcement).unwrap(); + node.net_graph_msg_handler.handle_node_announcement(&b_node_announcement).unwrap(); } (chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4) } @@ -949,7 +951,9 @@ pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: pub const TEST_FINAL_CLTV: u32 = 32; pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash) { - let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &origin_node.net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&origin_node.our_node_id, net_graph_msg_handler, &expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV, logger.clone()).unwrap(); assert_eq!(route.paths.len(), 1); assert_eq!(route.paths[0].len(), expected_route.len()); for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) { @@ -960,7 +964,9 @@ pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: } pub fn route_over_limit<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) { - let route = origin_node.router.get_route(&expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); + let net_graph_msg_handler = &origin_node.net_graph_msg_handler; + let route = get_route(&origin_node.our_node_id, net_graph_msg_handler, &expected_route.last().unwrap().node.get_our_node_id(), None, &Vec::new(), recv_value, TEST_FINAL_CLTV, logger.clone()).unwrap(); assert_eq!(route.paths.len(), 1); assert_eq!(route.paths[0].len(), expected_route.len()); for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) { @@ -1102,12 +1108,13 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec); + let net_graph_msg_handler = NetGraphMsgHandler::new(cfgs[i].chain_monitor.clone(), cfgs[i].logger.clone() as Arc); nodes.push(Node{ chain_monitor: cfgs[i].chain_monitor.clone(), block_notifier, tx_broadcaster: cfgs[i].tx_broadcaster, chan_monitor: &cfgs[i].chan_monitor, - keys_manager: &cfgs[i].keys_manager, node: &chan_mgrs[i], router, + keys_manager: &cfgs[i].keys_manager, node: &chan_mgrs[i], net_graph_msg_handler, node_seed: cfgs[i].node_seed, network_chan_count: chan_count.clone(), network_payment_count: payment_count.clone(), logger: cfgs[i].logger.clone(), + our_node_id: PublicKey::from_secret_key(&secp_ctx, &cfgs[i].keys_manager.get_node_secret()), }) } @@ -1237,8 +1244,8 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec {{ - let route = nodes[0].router.get_route(&nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV).unwrap(); let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes.last().unwrap().node.get_our_node_id(), None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, logger.clone()).unwrap(); (route, payment_hash, payment_preimage) }} }; @@ -1787,6 +1810,7 @@ fn channel_reserve_in_flight_removes() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); let b_chan_values = get_channel_value_stat!(nodes[1], chan_1.2); // Route the first two HTLCs. @@ -1796,7 +1820,8 @@ fn channel_reserve_in_flight_removes() { // Start routing the third HTLC (this is just used to get everyone in the right state). let (payment_preimage_3, payment_hash_3) = get_payment_preimage_hash!(nodes[0]); let send_1 = { - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, payment_hash_3, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut events = nodes[0].node.get_and_clear_pending_msg_events(); @@ -1868,7 +1893,8 @@ fn channel_reserve_in_flight_removes() { // to A to ensure that A doesn't count the almost-removed HTLC in update_add processing. let (payment_preimage_4, payment_hash_4) = get_payment_preimage_hash!(nodes[1]); let send_2 = { - let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &[], 10000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &[], 10000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[1].node.send_payment(&route, payment_hash_4, &None).unwrap(); check_added_monitors!(nodes[1], 1); let mut events = nodes[1].node.get_and_clear_pending_msg_events(); @@ -2782,8 +2808,10 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use // Add a fourth HTLC, this one will get sequestered away in nodes[1]'s holding cell waiting // on nodes[2]'s RAA. - let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (_, fourth_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[1].node.send_payment(&route, fourth_payment_hash, &None).unwrap(); assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); @@ -2923,11 +2951,13 @@ fn fail_backward_pending_htlc_upon_channel_failure() { 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::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); // 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(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -2943,7 +2973,8 @@ fn fail_backward_pending_htlc_upon_channel_failure() { // 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(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, failed_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 0); @@ -2952,7 +2983,6 @@ fn fail_backward_pending_htlc_upon_channel_failure() { // 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(); @@ -2964,6 +2994,8 @@ fn fail_backward_pending_htlc_upon_channel_failure() { }; let current_height = nodes[1].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 50_000, TEST_FINAL_CLTV, logger.clone()).unwrap(); let (onion_payloads, _amount_msat, cltv_expiry) = onion_utils::build_onion_payloads(&route.paths[0], 50_000, &None, current_height).unwrap(); let onion_keys = onion_utils::construct_onion_keys(&secp_ctx, &route.paths[0], &session_priv).unwrap(); let onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash); @@ -3023,12 +3055,13 @@ fn test_force_close_fail_back() { let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); - - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, 42).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); let mut payment_event = { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 1000000, 42, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -3196,10 +3229,12 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) { create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); } - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_1, payment_hash_1) = get_payment_preimage_hash!(nodes[0]); + let logger = Arc::new(test_utils::TestLogger::new()); let payment_event = { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, payment_hash_1, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -3373,6 +3408,8 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) { reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false)); // Channel should still work fine... + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), Some(&nodes[0].node.list_usable_channels()), &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); let payment_preimage_2 = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000).0; claim_payment(&nodes[0], &[&nodes[1]], payment_preimage_2, 1_000_000); } @@ -3467,11 +3504,13 @@ fn test_funding_peer_disconnect() { _ => panic!("Unexpected event"), }; - nodes[0].router.handle_channel_announcement(&as_announcement).unwrap(); - nodes[0].router.handle_channel_update(&bs_update).unwrap(); - nodes[0].router.handle_channel_update(&as_update).unwrap(); + nodes[0].net_graph_msg_handler.handle_channel_announcement(&as_announcement).unwrap(); + nodes[0].net_graph_msg_handler.handle_channel_update(&bs_update).unwrap(); + nodes[0].net_graph_msg_handler.handle_channel_update(&as_update).unwrap(); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); let (payment_preimage, _) = send_along_route(&nodes[0], route, &[&nodes[1]], 1000000); claim_payment(&nodes[0], &[&nodes[1]], payment_preimage, 1_000_000); } @@ -3485,13 +3524,14 @@ fn test_drop_messages_peer_disconnect_dual_htlc() { let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); let (payment_preimage_1, _) = route_payment(&nodes[0], &[&nodes[1]], 1000000); // Now try to send a second payment which will fail to send - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); let (payment_preimage_2, payment_hash_2) = get_payment_preimage_hash!(nodes[0]); - + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, payment_hash_2, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -3628,9 +3668,11 @@ fn do_test_htlc_timeout(send_partial_mpp: bool) { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); let our_payment_hash = if send_partial_mpp { - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(&nodes[0]); let payment_secret = PaymentSecret([0xdb; 32]); // Use the utility function send_payment_along_path to send the payment with MPP data which @@ -3687,18 +3729,23 @@ fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) { let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); // Route a first payment to get the 1 -> 2 channel in awaiting_raa... - let route = nodes[1].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap(); let (_, first_payment_hash) = get_payment_preimage_hash!(nodes[0]); - nodes[1].node.send_payment(&route, first_payment_hash, &None).unwrap(); + { + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); + nodes[1].node.send_payment(&route, first_payment_hash, &None).unwrap(); + } assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1); check_added_monitors!(nodes[1], 1); // Now attempt to route a second payment, which should be placed in the holding cell let (_, second_payment_hash) = get_payment_preimage_hash!(nodes[0]); if forwarded_htlc { - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, second_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let payment_event = SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0)); @@ -3707,6 +3754,8 @@ fn do_test_holding_cell_htlc_add_timeouts(forwarded_htlc: bool) { expect_pending_htlcs_forwardable!(nodes[1]); check_added_monitors!(nodes[1], 0); } else { + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[1].node.send_payment(&route, second_payment_hash, &None).unwrap(); check_added_monitors!(nodes[1], 0); } @@ -3772,7 +3821,7 @@ fn test_invalid_channel_announcement() { let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap(); let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap(); - nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } ); + nodes[0].net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } ); let as_bitcoin_key = as_chan.get_local_keys().inner.local_channel_pubkeys.funding_pubkey; let bs_bitcoin_key = bs_chan.get_local_keys().inner.local_channel_pubkeys.funding_pubkey; @@ -3818,19 +3867,19 @@ fn test_invalid_channel_announcement() { let unsigned_msg = dummy_unsigned_msg!(); sign_msg!(unsigned_msg); - assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true); - let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } ); + assert_eq!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).unwrap(), true); + let _ = nodes[0].net_graph_msg_handler.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } ); // Configured with Network::Testnet let mut unsigned_msg = dummy_unsigned_msg!(); unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash(); sign_msg!(unsigned_msg); - assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); + assert!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).is_err()); let mut unsigned_msg = dummy_unsigned_msg!(); unsigned_msg.chain_hash = BlockHash::hash(&[1,2,3,4,5,6,7,8,9]); sign_msg!(unsigned_msg); - assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); + assert!(nodes[0].net_graph_msg_handler.handle_channel_announcement(&chan_announcement).is_err()); } #[test] @@ -3897,9 +3946,9 @@ fn test_no_txn_manager_serialize_deserialize() { let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx); let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked); for node in nodes.iter() { - assert!(node.router.handle_channel_announcement(&announcement).unwrap()); - node.router.handle_channel_update(&as_update).unwrap(); - node.router.handle_channel_update(&bs_update).unwrap(); + assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap()); + node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap(); + node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap(); } send_payment(&nodes[0], &[&nodes[1]], 1000000, 1_000_000); @@ -4815,6 +4864,7 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno let node_cfgs = create_node_cfgs(6, &chanmon_cfgs); let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]); let nodes = create_network(6, &node_cfgs, &node_chanmgrs); + let logger = Arc::new(test_utils::TestLogger::new()); create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); @@ -4832,7 +4882,9 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno 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 // 1st HTLC: 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 - let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let our_node_id = &nodes[1].node.get_our_node_id(); + let route = get_route(our_node_id, net_graph_msg_handler, &nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, logger.clone()).unwrap(); // 2nd HTLC: send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_1); // not added < dust limit + HTLC tx fee // 3rd HTLC: @@ -4841,7 +4893,7 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno let (_, payment_hash_3) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); // 5th HTLC: let (_, payment_hash_4) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); - let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + let route = get_route(our_node_id, net_graph_msg_handler, &nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); // 6th HTLC: send_along_route_with_hash(&nodes[1], route.clone(), &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_3); // 7th HTLC: @@ -4850,13 +4902,13 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno // 8th HTLC: let (_, payment_hash_5) = route_payment(&nodes[0], &[&nodes[2], &nodes[3], &nodes[4]], 1000000); // 9th HTLC: - let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV).unwrap(); + let route = get_route(our_node_id, net_graph_msg_handler, &nodes[5].node.get_our_node_id(), None, &Vec::new(), ds_dust_limit*1000, TEST_FINAL_CLTV, logger.clone()).unwrap(); send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], ds_dust_limit*1000, payment_hash_5); // not added < dust limit + HTLC tx fee // 10th HTLC: 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 // 11th HTLC: - let route = nodes[1].router.get_route(&nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV).unwrap(); + let route = get_route(our_node_id, net_graph_msg_handler, &nodes[5].node.get_our_node_id(), None, &Vec::new(), 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); send_along_route_with_hash(&nodes[1], route, &[&nodes[2], &nodes[3], &nodes[5]], 1000000, payment_hash_6); // Double-check that six of the new HTLC were added @@ -5168,9 +5220,11 @@ fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) { 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(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV).unwrap(); let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &Vec::new(), if use_dust { 50000 } else { 3000000 }, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -5494,7 +5548,9 @@ fn test_onion_failure() { } let channels = [create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()), create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known())]; let (_, payment_hash) = get_payment_preimage_hash!(nodes[0]); - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV, logger.clone()).unwrap(); // positve case send_payment(&nodes[0], &vec!(&nodes[1], &nodes[2])[..], 40000, 40_000); @@ -5791,9 +5847,11 @@ fn test_update_add_htlc_bolt2_sender_value_below_minimum_msat() { 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, 100000, 95000000, InitFeatures::known(), InitFeatures::known()); - let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); - let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let mut route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); route.paths[0][0].fee_msat = 100; unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { err }, @@ -5810,13 +5868,15 @@ fn test_update_add_htlc_bolt2_sender_zero_value_msat() { 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, 100000, 95000000, InitFeatures::known(), InitFeatures::known()); - let mut route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let mut route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); route.paths[0][0].fee_msat = 0; - unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { err }, assert_eq!(err, "Cannot send 0-msat HTLC")); + assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); nodes[0].logger.assert_log("lightning::ln::channelmanager".to_string(), "Cannot send 0-msat HTLC".to_string(), 1); } @@ -5829,9 +5889,11 @@ fn test_update_add_htlc_bolt2_receiver_zero_value_msat() { 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, 100000, 95000000, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); - let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -5852,9 +5914,12 @@ fn test_update_add_htlc_bolt2_sender_cltv_expiry_too_high() { 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, 100000, 0, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000000, 500000001, logger.clone()).unwrap(); unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::RouteError { err }, assert_eq!(err, "Channel CLTV overflowed?!")); } @@ -5871,10 +5936,12 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known()); let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().their_max_accepted_htlcs as u64; + let logger = Arc::new(test_utils::TestLogger::new()); for i in 0..max_accepted_htlcs { - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); let payment_event = { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -5894,8 +5961,9 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() expect_pending_htlcs_forwardable!(nodes[1]); expect_payment_received!(nodes[1], our_payment_hash, 100000); } - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { err }, assert_eq!(err, "Cannot push more than their max accepted HTLCs")); @@ -5916,8 +5984,10 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_value_in_flight() { send_payment(&nodes[0], &vec!(&nodes[1])[..], max_in_flight, max_in_flight); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], max_in_flight+1, TEST_FINAL_CLTV, logger.clone()).unwrap(); unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { err }, assert_eq!(err, "Cannot send value that would put us over the max HTLC value in flight our peer will accept")); @@ -5942,8 +6012,11 @@ fn test_update_add_htlc_bolt2_receiver_check_amount_received_more_than_min() { let channel = chan_lock.by_id.get(&chan.2).unwrap(); htlc_minimum_msat = channel.get_our_htlc_minimum_msat(); } - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV).unwrap(); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], htlc_minimum_msat, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -5966,8 +6039,10 @@ fn test_update_add_htlc_bolt2_receiver_sender_can_afford_amount_sent() { let their_channel_reserve = get_channel_value_stat!(nodes[0], chan.2).channel_reserve_msat; - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let logger = Arc::new(test_utils::TestLogger::new()); + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 5000000-their_channel_reserve, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -5990,7 +6065,8 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() { 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, 100000, 95000000, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); let session_priv = SecretKey::from_slice(&{ @@ -6000,6 +6076,9 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() { session_key }).expect("RNG is bad!"); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV, logger.clone()).unwrap(); + let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1; let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::signing_only(), &route.paths[0], &session_priv).unwrap(); let (onion_payloads, _htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route.paths[0], 3999999, &None, cur_height).unwrap(); @@ -6035,8 +6114,11 @@ fn test_update_add_htlc_bolt2_receiver_check_max_in_flight_msat() { 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, 1000000, 1000000, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -6056,9 +6138,12 @@ fn test_update_add_htlc_bolt2_receiver_check_cltv_expiry() { 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 logger = Arc::new(test_utils::TestLogger::new()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 3999999, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -6080,9 +6165,12 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() { 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 logger = Arc::new(test_utils::TestLogger::new()); + create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -6125,11 +6213,13 @@ fn test_update_fulfill_htlc_bolt2_update_fulfill_htlc_before_commitment() { 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 logger = Arc::new(test_utils::TestLogger::new()); let chan = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); - - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); let (our_payment_preimage, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); + check_added_monitors!(nodes[0], 1); let 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(), &updates.update_add_htlcs[0]); @@ -6157,9 +6247,11 @@ fn test_update_fulfill_htlc_bolt2_update_fail_htlc_before_commitment() { 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(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -6188,9 +6280,11 @@ fn test_update_fulfill_htlc_bolt2_update_fail_malformed_htlc_before_commitment() 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(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let updates = get_htlc_update_msgs!(nodes[0], nodes[1].node.get_our_node_id()); @@ -6302,8 +6396,11 @@ fn test_update_fulfill_htlc_bolt2_missing_badonion_bit_for_malformed_htlc_messag let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known()); - let route = nodes[0].router.get_route(&nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV).unwrap(); + let logger = Arc::new(test_utils::TestLogger::new()); + let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[1].node.get_our_node_id(), None, &[], 1000000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); @@ -6349,12 +6446,14 @@ fn test_update_fulfill_htlc_bolt2_after_malformed_htlc_message_must_forward_upda let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 1000000, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1000000, 1000000, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV).unwrap(); let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]); //First hop let mut payment_event = { + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &Vec::new(), 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); nodes[0].node.send_payment(&route, our_payment_hash, &None).unwrap(); check_added_monitors!(nodes[0], 1); let mut events = nodes[0].node.get_and_clear_pending_msg_events(); @@ -7033,8 +7132,12 @@ fn test_bump_penalty_txn_on_revoked_commitment() { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 59000000, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); + + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0; - let route = nodes[1].router.get_route(&nodes[0].node.get_our_node_id(), None, &Vec::new(), 3000000, 30).unwrap(); + let net_graph_msg_handler = &nodes[1].net_graph_msg_handler; + let route = get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 3000000, 30, logger.clone()).unwrap(); send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000); let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2); @@ -7581,10 +7684,12 @@ fn test_simple_payment_secret() { create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()); create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known()); + let logger = Arc::new(test_utils::TestLogger::new()); let (payment_preimage, payment_hash) = get_payment_preimage_hash!(&nodes[0]); let payment_secret = PaymentSecret([0xdb; 32]); - let route = nodes[0].router.get_route(&nodes[2].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[2].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2]]], 100000, payment_hash, Some(payment_secret.clone())); // Claiming with all the correct values but the wrong secret should result in nothing... assert_eq!(nodes[2].node.claim_funds(payment_preimage, &None, 100_000), false); @@ -7605,10 +7710,12 @@ fn test_simple_mpp() { let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id; let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id; let chan_4_id = create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id; + let logger = Arc::new(test_utils::TestLogger::new()); let (payment_preimage, payment_hash) = get_payment_preimage_hash!(&nodes[0]); let payment_secret = PaymentSecret([0xdb; 32]); - let mut route = nodes[0].router.get_route(&nodes[3].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV).unwrap(); + let net_graph_msg_handler = &nodes[0].net_graph_msg_handler; + let mut route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes[3].node.get_our_node_id(), None, &[], 100000, TEST_FINAL_CLTV, logger.clone()).unwrap(); let path = route.paths[0].clone(); route.paths.push(path); route.paths[0][0].pubkey = nodes[1].node.get_our_node_id(); diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 36c9bf5e9..de0d50c7f 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -36,7 +36,7 @@ pub struct MessageHandler where CM::Target: msgs::ChannelMessageHandl /// ChannelManager object. pub chan_handler: CM, /// A message handler which handles messages updating our knowledge of the network channel - /// graph. Usually this is just a Router object. + /// graph. Usually this is just a NetGraphMsgHandlerMonitor object. pub route_handler: Arc, } diff --git a/lightning/src/routing/mod.rs b/lightning/src/routing/mod.rs index 9e72d7c3e..d419c6e93 100644 --- a/lightning/src/routing/mod.rs +++ b/lightning/src/routing/mod.rs @@ -1,3 +1,4 @@ //! Structs and impls for receiving messages about the network and storing the topology live here. pub mod router; +pub mod network_graph; diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs new file mode 100644 index 000000000..35a7e868f --- /dev/null +++ b/lightning/src/routing/network_graph.rs @@ -0,0 +1,1603 @@ +//! The top-level network map tracking logic lives here. + +use bitcoin::secp256k1::key::PublicKey; +use bitcoin::secp256k1::Secp256k1; +use bitcoin::secp256k1; + +use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hashes::Hash; +use bitcoin::blockdata::script::Builder; +use bitcoin::blockdata::opcodes; + +use chain::chaininterface::{ChainError, ChainWatchInterface}; +use ln::features::{ChannelFeatures, NodeFeatures}; +use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress}; +use ln::msgs; +use util::ser::{Writeable, Readable, Writer, ReadableArgs}; +use util::logger::Logger; + +use std::cmp; +use std::sync::{RwLock,Arc}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::collections::BTreeMap; +use std::collections::btree_map::Entry as BtreeEntry; +use std; + +/// Receives network updates from peers to track view of the network. +pub struct NetGraphMsgHandler { + secp_ctx: Secp256k1, + /// Representation of the payment channel network + pub network_graph: RwLock, + chain_monitor: Arc, + full_syncs_requested: AtomicUsize, + logger: Arc, +} + +impl NetGraphMsgHandler { + /// Creates a new tracker of the actual state of the network of channels and nodes. + pub fn new(chain_monitor: Arc, logger: Arc) -> Self { + NetGraphMsgHandler { + secp_ctx: Secp256k1::verification_only(), + network_graph: RwLock::new(NetworkGraph { + channels: BTreeMap::new(), + nodes: BTreeMap::new(), + }), + full_syncs_requested: AtomicUsize::new(0), + chain_monitor, + logger: logger.clone(), + } + } + + /// Get network addresses by node id + pub fn get_addresses(&self, pubkey: &PublicKey) -> Option> { + let network = self.network_graph.read().unwrap(); + network.get_nodes().get(pubkey).map(|n| n.addresses.clone()) + } + + /// Dumps the entire network view of this NetGraphMsgHandler to the logger provided in the constructor at + /// level Trace + pub fn trace_state(&self) { + log_trace!(self, "{}", self.network_graph.read().unwrap()); + } +} + + +macro_rules! secp_verify_sig { + ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => { + match $secp_ctx.verify($msg, $sig, $pubkey) { + Ok(_) => {}, + Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}), + } + }; +} + +impl RoutingMessageHandler for NetGraphMsgHandler { + fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result { + self.network_graph.write().unwrap().process_node_announcement(msg, Some(&self.secp_ctx)) + } + + fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result { + if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 { + return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError}); + } + + let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) { + Ok((script_pubkey, _value)) => { + let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2) + .push_slice(&msg.contents.bitcoin_key_1.serialize()) + .push_slice(&msg.contents.bitcoin_key_2.serialize()) + .push_opcode(opcodes::all::OP_PUSHNUM_2) + .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh(); + if script_pubkey != expected_script { + return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError}); + } + //TODO: Check if value is worth storing, use it to inform routing, and compare it + //to the new HTLC max field in channel_update + true + }, + Err(ChainError::NotSupported) => { + // Tentatively accept, potentially exposing us to DoS attacks + false + }, + Err(ChainError::NotWatched) => { + return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError}); + }, + Err(ChainError::UnknownTx) => { + return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError}); + }, + }; + let result = self.network_graph.write().unwrap().process_channel_announcement(msg, checked_utxo, Some(&self.secp_ctx)); + log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !msg.contents.excess_data.is_empty() { " with excess uninterpreted data!" } else { "" }); + result + } + + fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) { + match update { + &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => { + let _ = self.network_graph.write().unwrap().process_channel_update(msg, Some(&self.secp_ctx)); + }, + &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => { + self.network_graph.write().unwrap().process_channel_closing(short_channel_id, &is_permanent); + }, + &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => { + self.network_graph.write().unwrap().process_node_failure(node_id, &is_permanent); + }, + } + } + + fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result { + self.network_graph.write().unwrap().process_channel_update(msg, Some(&self.secp_ctx)) + } + + fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option, Option)> { + let network_graph = self.network_graph.read().unwrap(); + let mut result = Vec::with_capacity(batch_amount as usize); + let mut iter = network_graph.get_channels().range(starting_point..); + while result.len() < batch_amount as usize { + if let Some((_, ref chan)) = iter.next() { + if chan.announcement_message.is_some() { + result.push((chan.announcement_message.clone().unwrap(), + chan.one_to_two.last_update_message.clone(), + chan.two_to_one.last_update_message.clone())); + } else { + // TODO: We may end up sending un-announced channel_updates if we are sending + // initial sync data while receiving announce/updates for this channel. + } + } else { + return result; + } + } + result + } + + fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec { + let network_graph = self.network_graph.read().unwrap(); + let mut result = Vec::with_capacity(batch_amount as usize); + let mut iter = if let Some(pubkey) = starting_point { + let mut iter = network_graph.get_nodes().range((*pubkey)..); + iter.next(); + iter + } else { + network_graph.get_nodes().range(..) + }; + while result.len() < batch_amount as usize { + if let Some((_, ref node)) = iter.next() { + if node.announcement_message.is_some() { + result.push(node.announcement_message.clone().unwrap()); + } + } else { + return result; + } + } + result + } + + fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool { + //TODO: Determine whether to request a full sync based on the network map. + const FULL_SYNCS_TO_REQUEST: usize = 5; + if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST { + self.full_syncs_requested.fetch_add(1, Ordering::AcqRel); + true + } else { + false + } + } +} + + +const SERIALIZATION_VERSION: u8 = 1; +const MIN_SERIALIZATION_VERSION: u8 = 1; + +impl Writeable for NetGraphMsgHandler { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&[SERIALIZATION_VERSION; 1])?; + writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?; + + let network = self.network_graph.read().unwrap(); + network.write(writer)?; + Ok(()) + } +} + +/// Arguments for the creation of a NetGraphMsgHandler that are not deserialized. +/// At a high-level, the process for deserializing a NetGraphMsgHandler and resuming normal operation is: +/// 1) Deserialize the NetGraphMsgHandler by filling in this struct and calling ::read(reaser, args). +/// 2) Register the new NetGraphMsgHandler with your ChainWatchInterface +pub struct NetGraphMsgHandlerReadArgs { + /// The ChainWatchInterface for use in the NetGraphMsgHandler in the future. + /// + /// No calls to the ChainWatchInterface will be made during deserialization. + pub chain_monitor: Arc, + /// The Logger for use in the ChannelManager and which may be used to log information during + /// deserialization. + pub logger: Arc, +} + +impl ReadableArgs for NetGraphMsgHandler { + fn read(reader: &mut R, args: NetGraphMsgHandlerReadArgs) -> Result { + let _ver: u8 = Readable::read(reader)?; + let min_ver: u8 = Readable::read(reader)?; + if min_ver > SERIALIZATION_VERSION { + return Err(DecodeError::UnknownVersion); + } + let network_graph = Readable::read(reader)?; + Ok(NetGraphMsgHandler { + secp_ctx: Secp256k1::verification_only(), + network_graph: RwLock::new(network_graph), + chain_monitor: args.chain_monitor, + full_syncs_requested: AtomicUsize::new(0), + logger: args.logger.clone(), + }) + } +} + +#[derive(PartialEq)] +/// Details regarding one direction of a channel +pub struct DirectionalChannelInfo { + /// A node from which the channel direction starts + pub src_node_id: PublicKey, + /// When the last update to the channel direction was issued + pub last_update: u32, + /// Whether the channel can be currently used for payments + pub enabled: bool, + /// The difference in CLTV values between the source and the destination node of the channel + pub cltv_expiry_delta: u16, + /// The minimum value, which must be relayed to the next hop via the channel + pub htlc_minimum_msat: u64, + /// Fees charged when the channel is used for routing + pub fees: RoutingFees, + /// Most recent update for the channel received from the network + pub last_update_message: Option, +} + +impl std::fmt::Display for DirectionalChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fees {:?}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fees)?; + Ok(()) + } +} + +impl_writeable!(DirectionalChannelInfo, 0, { + src_node_id, + last_update, + enabled, + cltv_expiry_delta, + htlc_minimum_msat, + fees, + last_update_message +}); + +#[derive(PartialEq)] +/// Details regarding a channel (both directions) +pub struct ChannelInfo { + /// Protocol features of a channel communicated during its announcement + pub features: ChannelFeatures, + /// Details regarding one of the directions of a channel + pub one_to_two: DirectionalChannelInfo, + /// Details regarding another direction of a channel + pub two_to_one: DirectionalChannelInfo, + /// An initial announcement of the channel + //this is cached here so we can send out it later if required by initial routing sync + //keep an eye on this to see if the extra memory is a problem + pub announcement_message: Option, +} + +impl std::fmt::Display for ChannelInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?; + Ok(()) + } +} + +impl_writeable!(ChannelInfo, 0, { + features, + one_to_two, + two_to_one, + announcement_message +}); + + +/// Fees for routing via a given channel or a node +#[derive(Eq, PartialEq, Copy, Clone, Debug)] +pub struct RoutingFees { + /// Flat routing fee + pub base_msat: u32, + /// Liquidity-based routing fee + pub proportional_millionths: u32, +} + +impl Readable for RoutingFees{ + fn read(reader: &mut R) -> Result { + let base_msat: u32 = Readable::read(reader)?; + let proportional_millionths: u32 = Readable::read(reader)?; + Ok(RoutingFees { + base_msat, + proportional_millionths, + }) + } +} + +impl Writeable for RoutingFees { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + self.base_msat.write(writer)?; + self.proportional_millionths.write(writer)?; + Ok(()) + } +} + + +#[derive(PartialEq)] +/// Details regarding a node in the network +pub struct NodeInfo { + /// All valid channels a node has announced + pub channels: Vec, + /// Lowest fees enabling routing via any of the known channels to a node + pub lowest_inbound_channel_fees: Option, + /// Protocol features the node announced support for + pub features: NodeFeatures, + /// When the last known update to the node state was issued + /// Unlike for channels, we may have a NodeInfo entry before having received a node_update. + /// Thus, we have to be able to capture "no update has been received", which we do with an + /// Option here. + pub last_update: Option, + /// Color assigned to the node + pub rgb: [u8; 3], + /// Moniker assigned to the node + pub alias: [u8; 32], + /// Internet-level addresses via which one can connect to the node + pub addresses: Vec, + /// An initial announcement of the node + //this is cached here so we can send out it later if required by initial routing sync + //keep an eye on this to see if the extra memory is a problem + pub announcement_message: Option, +} + +impl std::fmt::Display for NodeInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fees: {:?}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fees, &self.channels[..])?; + Ok(()) + } +} + +impl Writeable for NodeInfo { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + (self.channels.len() as u64).write(writer)?; + for ref chan in self.channels.iter() { + chan.write(writer)?; + } + self.lowest_inbound_channel_fees.write(writer)?; + self.features.write(writer)?; + self.last_update.write(writer)?; + self.rgb.write(writer)?; + self.alias.write(writer)?; + (self.addresses.len() as u64).write(writer)?; + for ref addr in &self.addresses { + addr.write(writer)?; + } + self.announcement_message.write(writer)?; + Ok(()) + } +} + +const MAX_ALLOC_SIZE: u64 = 64*1024; + +impl Readable for NodeInfo { + fn read(reader: &mut R) -> Result { + let channels_count: u64 = Readable::read(reader)?; + let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize); + for _ in 0..channels_count { + channels.push(Readable::read(reader)?); + } + let lowest_inbound_channel_fees = Readable::read(reader)?; + let features = Readable::read(reader)?; + let last_update = Readable::read(reader)?; + let rgb = Readable::read(reader)?; + let alias = Readable::read(reader)?; + let addresses_count: u64 = Readable::read(reader)?; + let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize); + for _ in 0..addresses_count { + match Readable::read(reader) { + Ok(Ok(addr)) => { addresses.push(addr); }, + Ok(Err(_)) => return Err(DecodeError::InvalidValue), + Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor), + _ => unreachable!(), + } + } + let announcement_message = Readable::read(reader)?; + Ok(NodeInfo { + channels, + lowest_inbound_channel_fees, + features, + last_update, + rgb, + alias, + addresses, + announcement_message + }) + } +} + +/// Represents the network as nodes and channels between them +#[derive(PartialEq)] +pub struct NetworkGraph { + channels: BTreeMap, + nodes: BTreeMap, +} + +impl Writeable for NetworkGraph { + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + (self.channels.len() as u64).write(writer)?; + for (ref chan_id, ref chan_info) in self.channels.iter() { + (*chan_id).write(writer)?; + chan_info.write(writer)?; + } + (self.nodes.len() as u64).write(writer)?; + for (ref node_id, ref node_info) in self.nodes.iter() { + node_id.write(writer)?; + node_info.write(writer)?; + } + Ok(()) + } +} + +impl Readable for NetworkGraph { + fn read(reader: &mut R) -> Result { + let channels_count: u64 = Readable::read(reader)?; + let mut channels = BTreeMap::new(); + for _ in 0..channels_count { + let chan_id: u64 = Readable::read(reader)?; + let chan_info = Readable::read(reader)?; + channels.insert(chan_id, chan_info); + } + let nodes_count: u64 = Readable::read(reader)?; + let mut nodes = BTreeMap::new(); + for _ in 0..nodes_count { + let node_id = Readable::read(reader)?; + let node_info = Readable::read(reader)?; + nodes.insert(node_id, node_info); + } + Ok(NetworkGraph { + channels, + nodes, + }) + } +} + +impl std::fmt::Display for NetworkGraph { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "Network map\n[Channels]\n")?; + for (key, val) in self.channels.iter() { + write!(f, " {}: {}\n", key, val)?; + } + write!(f, "[Nodes]\n")?; + for (key, val) in self.nodes.iter() { + write!(f, " {}: {}\n", log_pubkey!(key), val)?; + } + Ok(()) + } +} + +impl NetworkGraph { + /// Returns all known valid channels + pub fn get_channels<'a>(&'a self) -> &'a BTreeMap { &self.channels } + /// Returns all known nodes + pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap { &self.nodes } + + fn process_node_announcement(&mut self, msg: &msgs::NodeAnnouncement, secp_ctx: Option<&Secp256k1>) -> Result { + if let Some(sig_verifier) = secp_ctx { + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); + secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &msg.contents.node_id); + } + + match self.nodes.get_mut(&msg.contents.node_id) { + None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}), + Some(node) => { + match node.last_update { + Some(last_update) => if last_update >= msg.contents.timestamp { + return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError}); + }, + None => {}, + } + + node.features = msg.contents.features.clone(); + node.last_update = Some(msg.contents.timestamp); + node.rgb = msg.contents.rgb; + node.alias = msg.contents.alias; + node.addresses = msg.contents.addresses.clone(); + + let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty(); + node.announcement_message = if should_relay { Some(msg.clone()) } else { None }; + Ok(should_relay) + } + } + } + + fn process_channel_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool, secp_ctx: Option<&Secp256k1>) -> Result { + if let Some(sig_verifier) = secp_ctx { + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); + secp_verify_sig!(sig_verifier, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1); + secp_verify_sig!(sig_verifier, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2); + secp_verify_sig!(sig_verifier, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1); + secp_verify_sig!(sig_verifier, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2); + } + + let should_relay = msg.contents.excess_data.is_empty(); + + let chan_info = ChannelInfo { + features: msg.contents.features.clone(), + one_to_two: DirectionalChannelInfo { + src_node_id: msg.contents.node_id_1.clone(), + last_update: 0, + enabled: false, + cltv_expiry_delta: u16::max_value(), + htlc_minimum_msat: u64::max_value(), + fees: RoutingFees { + base_msat: u32::max_value(), + proportional_millionths: u32::max_value(), + }, + last_update_message: None, + }, + two_to_one: DirectionalChannelInfo { + src_node_id: msg.contents.node_id_2.clone(), + last_update: 0, + enabled: false, + cltv_expiry_delta: u16::max_value(), + htlc_minimum_msat: u64::max_value(), + fees: RoutingFees { + base_msat: u32::max_value(), + proportional_millionths: u32::max_value(), + }, + last_update_message: None, + }, + announcement_message: if should_relay { Some(msg.clone()) } else { None }, + }; + + match self.channels.entry(msg.contents.short_channel_id) { + BtreeEntry::Occupied(mut entry) => { + //TODO: because asking the blockchain if short_channel_id is valid is only optional + //in the blockchain API, we need to handle it smartly here, though it's unclear + //exactly how... + if checked_utxo { + // Either our UTXO provider is busted, there was a reorg, or the UTXO provider + // only sometimes returns results. In any case remove the previous entry. Note + // that the spec expects us to "blacklist" the node_ids involved, but we can't + // do that because + // a) we don't *require* a UTXO provider that always returns results. + // b) we don't track UTXOs of channels we know about and remove them if they + // get reorg'd out. + // c) it's unclear how to do so without exposing ourselves to massive DoS risk. + Self::remove_channel_in_nodes(&mut self.nodes, &entry.get(), msg.contents.short_channel_id); + *entry.get_mut() = chan_info; + } else { + return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError}) + } + }, + BtreeEntry::Vacant(entry) => { + entry.insert(chan_info); + } + }; + + macro_rules! add_channel_to_node { + ( $node_id: expr ) => { + match self.nodes.entry($node_id) { + BtreeEntry::Occupied(node_entry) => { + node_entry.into_mut().channels.push(msg.contents.short_channel_id); + }, + BtreeEntry::Vacant(node_entry) => { + node_entry.insert(NodeInfo { + channels: vec!(msg.contents.short_channel_id), + lowest_inbound_channel_fees: None, + features: NodeFeatures::empty(), + last_update: None, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + announcement_message: None, + }); + } + } + }; + } + + add_channel_to_node!(msg.contents.node_id_1); + add_channel_to_node!(msg.contents.node_id_2); + + Ok(should_relay) + } + + fn process_channel_closing(&mut self, short_channel_id: &u64, is_permanent: &bool) { + if *is_permanent { + if let Some(chan) = self.channels.remove(short_channel_id) { + Self::remove_channel_in_nodes(&mut self.nodes, &chan, *short_channel_id); + } + } else { + if let Some(chan) = self.channels.get_mut(&short_channel_id) { + chan.one_to_two.enabled = false; + chan.two_to_one.enabled = false; + } + } + } + + fn process_node_failure(&mut self, _node_id: &PublicKey, is_permanent: &bool) { + if *is_permanent { + // TODO: Wholly remove the node + } else { + // TODO: downgrade the node + } + } + + fn process_channel_update(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: Option<&Secp256k1>) -> Result { + let dest_node_id; + let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1); + let chan_was_enabled; + + match self.channels.get_mut(&msg.contents.short_channel_id) { + None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}), + Some(channel) => { + macro_rules! maybe_update_channel_info { + ( $target: expr) => { + if $target.last_update >= msg.contents.timestamp { + return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError}); + } + chan_was_enabled = $target.enabled; + $target.last_update = msg.contents.timestamp; + $target.enabled = chan_enabled; + $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta; + $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat; + $target.fees.base_msat = msg.contents.fee_base_msat; + $target.fees.proportional_millionths = msg.contents.fee_proportional_millionths; + $target.last_update_message = if msg.contents.excess_data.is_empty() { + Some(msg.clone()) + } else { + None + }; + } + } + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); + if msg.contents.flags & 1 == 1 { + dest_node_id = channel.one_to_two.src_node_id.clone(); + if let Some(sig_verifier) = secp_ctx { + secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id); + } + maybe_update_channel_info!(channel.two_to_one); + } else { + dest_node_id = channel.two_to_one.src_node_id.clone(); + if let Some(sig_verifier) = secp_ctx { + secp_verify_sig!(sig_verifier, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id); + } + maybe_update_channel_info!(channel.one_to_two); + } + } + } + + if chan_enabled { + let node = self.nodes.get_mut(&dest_node_id).unwrap(); + let mut base_msat = msg.contents.fee_base_msat; + let mut proportional_millionths = msg.contents.fee_proportional_millionths; + if let Some(fees) = node.lowest_inbound_channel_fees { + base_msat = cmp::min(base_msat, fees.base_msat); + proportional_millionths = cmp::min(proportional_millionths, fees.proportional_millionths); + } + node.lowest_inbound_channel_fees = Some(RoutingFees { + base_msat, + proportional_millionths + }); + } else if chan_was_enabled { + let mut lowest_inbound_channel_fee_base_msat = u32::max_value(); + let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value(); + + { + let node = self.nodes.get(&dest_node_id).unwrap(); + + for chan_id in node.channels.iter() { + let chan = self.channels.get(chan_id).unwrap(); + if chan.one_to_two.src_node_id == dest_node_id { + lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fees.base_msat); + lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fees.proportional_millionths); + } else { + lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fees.base_msat); + lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fees.proportional_millionths); + } + } + } + + //TODO: satisfy the borrow-checker without a double-map-lookup :( + let mut_node = self.nodes.get_mut(&dest_node_id).unwrap(); + if mut_node.channels.len() > 0 { + mut_node.lowest_inbound_channel_fees = Some(RoutingFees { + base_msat: lowest_inbound_channel_fee_base_msat, + proportional_millionths: lowest_inbound_channel_fee_proportional_millionths + }); + } + } + + Ok(msg.contents.excess_data.is_empty()) + } + + fn remove_channel_in_nodes(nodes: &mut BTreeMap, chan: &ChannelInfo, short_channel_id: u64) { + macro_rules! remove_from_node { + ($node_id: expr) => { + if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) { + entry.get_mut().channels.retain(|chan_id| { + short_channel_id != *chan_id + }); + if entry.get().channels.is_empty() { + entry.remove_entry(); + } + } else { + panic!("Had channel that pointed to unknown node (ie inconsistent network map)!"); + } + } + } + remove_from_node!(chan.one_to_two.src_node_id); + remove_from_node!(chan.two_to_one.src_node_id); + } +} + +#[cfg(test)] +mod tests { + use chain::chaininterface; + use ln::features::{ChannelFeatures, NodeFeatures}; + use routing::network_graph::{NetGraphMsgHandler, NetworkGraph}; + use ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement, + UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate}; + use util::test_utils; + use util::logger::Logger; + use util::ser::{Readable, Writeable}; + + use bitcoin::hashes::sha256d::Hash as Sha256dHash; + use bitcoin::hashes::Hash; + use bitcoin::network::constants::Network; + use bitcoin::blockdata::constants::genesis_block; + use bitcoin::blockdata::script::Builder; + use bitcoin::blockdata::opcodes; + use bitcoin::util::hash::BitcoinHash; + + use hex; + + use bitcoin::secp256k1::key::{PublicKey, SecretKey}; + use bitcoin::secp256k1::{All, Secp256k1}; + + use std::sync::Arc; + + fn create_net_graph_msg_handler() -> (Secp256k1, NetGraphMsgHandler) { + let secp_ctx = Secp256k1::new(); + let logger: Arc = Arc::new(test_utils::TestLogger::new()); + let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); + let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger)); + (secp_ctx, net_graph_msg_handler) + } + + #[test] + fn request_full_sync_finite_times() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()); + + assert!(net_graph_msg_handler.should_request_full_sync(&node_id)); + assert!(net_graph_msg_handler.should_request_full_sync(&node_id)); + assert!(net_graph_msg_handler.should_request_full_sync(&node_id)); + assert!(net_graph_msg_handler.should_request_full_sync(&node_id)); + assert!(net_graph_msg_handler.should_request_full_sync(&node_id)); + assert!(!net_graph_msg_handler.should_request_full_sync(&node_id)); + } + + #[test] + fn handling_node_announcements() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + let zero_hash = Sha256dHash::hash(&[0; 32]); + let first_announcement_time = 500; + + let mut unsigned_announcement = UnsignedNodeAnnouncement { + features: NodeFeatures::known(), + timestamp: first_announcement_time, + node_id: node_id_1, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: Vec::new(), + }; + let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }; + + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!("No existing channels for node_announcement", e.err) + }; + + { + // Announce a channel to add a corresponding node. + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::known(), + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 0, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; + } + + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(res) => assert!(res), + Err(_) => panic!() + }; + + let fake_msghash = hash_to_message!(&zero_hash); + match net_graph_msg_handler.handle_node_announcement( + &NodeAnnouncement { + signature: secp_ctx.sign(&fake_msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Invalid signature from remote node") + }; + + unsigned_announcement.timestamp += 1000; + unsigned_announcement.excess_data.push(1); + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let announcement_with_data = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }; + // Return false because contains excess data. + match net_graph_msg_handler.handle_node_announcement(&announcement_with_data) { + Ok(res) => assert!(!res), + Err(_) => panic!() + }; + unsigned_announcement.excess_data = Vec::new(); + + // Even though previous announcement was not relayed further, we still accepted it, + // so we now won't accept announcements before the previous one. + unsigned_announcement.timestamp -= 10; + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let outdated_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }; + match net_graph_msg_handler.handle_node_announcement(&outdated_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Update older than last processed update") + }; + } + + #[test] + fn handling_channel_announcements() { + let secp_ctx = Secp256k1::new(); + let logger: Arc = Arc::new(test_utils::TestLogger::new()); + let chain_monitor = Arc::new(test_utils::TestChainWatcher::new()); + let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor.clone(), Arc::clone(&logger)); + + + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2) + .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize()) + .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize()) + .push_opcode(opcodes::all::OP_PUSHNUM_2) + .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh(); + + + let mut unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::known(), + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 0, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + + // Test if the UTXO lookups were not supported + *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::NotSupported); + + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; + + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + match network.get_channels().get(&unsigned_announcement.short_channel_id) { + None => panic!(), + Some(_) => () + } + } + + + // If we receive announcement for the same channel (with UTXO lookups disabled), + // drop new one on the floor, since we can't see any changes. + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Already have knowledge of channel") + }; + + + // Test if an associated transaction were not on-chain (or not confirmed). + *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx); + unsigned_announcement.short_channel_id += 1; + + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry") + }; + + + // Now test if the transaction is found in the UTXO set and the script is correct. + unsigned_announcement.short_channel_id += 1; + *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script.clone(), 0)); + + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; + + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + match network.get_channels().get(&unsigned_announcement.short_channel_id) { + None => panic!(), + Some(_) => () + } + } + + // If we receive announcement for the same channel (but TX is not confirmed), + // drop new one on the floor, since we can't see any changes. + *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx); + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry") + }; + + // But if it is confirmed, replace the channel + *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script, 0)); + unsigned_announcement.features = ChannelFeatures::empty(); + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + match network.get_channels().get(&unsigned_announcement.short_channel_id) { + Some(channel_entry) => { + assert_eq!(channel_entry.features, ChannelFeatures::empty()); + }, + _ => panic!() + } + } + + // Don't relay valid channels with excess data + unsigned_announcement.short_channel_id += 1; + unsigned_announcement.excess_data.push(1); + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(!res), + _ => panic!() + }; + + unsigned_announcement.excess_data = Vec::new(); + let invalid_sig_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&invalid_sig_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Invalid signature from remote node") + }; + + unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let channel_to_itself_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_1_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&channel_to_itself_announcement) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself") + }; + } + + #[test] + fn handling_channel_update() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + let zero_hash = Sha256dHash::hash(&[0; 32]); + let short_channel_id = 0; + let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); + { + // Announce a channel we will update + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::empty(), + chain_hash, + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_channel_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + + } + + let mut unsigned_channel_update = UnsignedChannelUpdate { + chain_hash, + short_channel_id, + timestamp: 100, + flags: 0, + cltv_expiry_delta: 144, + htlc_minimum_msat: 1000000, + fee_base_msat: 10000, + fee_proportional_millionths: 20, + excess_data: Vec::new() + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(res) => assert!(res), + _ => panic!() + }; + + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + match network.get_channels().get(&short_channel_id) { + None => panic!(), + Some(channel_info) => { + assert_eq!(channel_info.one_to_two.cltv_expiry_delta, 144); + assert_eq!(channel_info.two_to_one.cltv_expiry_delta, u16::max_value()); + } + } + } + + unsigned_channel_update.timestamp += 100; + unsigned_channel_update.excess_data.push(1); + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + // Return false because contains excess data + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(res) => assert!(!res), + _ => panic!() + }; + + unsigned_channel_update.short_channel_id += 1; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Couldn't find channel for update") + }; + unsigned_channel_update.short_channel_id = short_channel_id; + + + // Even though previous update was not relayed further, we still accepted it, + // so we now won't accept update before the previous one. + unsigned_channel_update.timestamp -= 10; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Update older than last processed update") + }; + unsigned_channel_update.timestamp += 500; + + let fake_msghash = hash_to_message!(&zero_hash); + let invalid_sig_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&fake_msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + + match net_graph_msg_handler.handle_channel_update(&invalid_sig_channel_update) { + Ok(_) => panic!(), + Err(e) => assert_eq!(e.err, "Invalid signature from remote node") + }; + + } + + #[test] + fn handling_htlc_fail_channel_update() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + let short_channel_id = 0; + let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); + + { + // There is no nodes in the table at the beginning. + let network = net_graph_msg_handler.network_graph.read().unwrap(); + assert_eq!(network.get_nodes().len(), 0); + } + + { + // Announce a channel we will update + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::empty(), + chain_hash, + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_channel_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + + } + + let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed { + short_channel_id, + is_permanent: false + }; + + net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg); + + // Non-permanent closing just disables a channel + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + match network.get_channels().get(&short_channel_id) { + None => panic!(), + Some(channel_info) => { + assert!(!channel_info.one_to_two.enabled); + assert!(!channel_info.two_to_one.enabled); + } + } + } + + let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed { + short_channel_id, + is_permanent: true + }; + + net_graph_msg_handler.handle_htlc_fail_channel_update(&channel_close_msg); + + // Permanent closing deletes a channel + { + let network = net_graph_msg_handler.network_graph.read().unwrap(); + assert_eq!(network.get_channels().len(), 0); + // Nodes are also deleted because there are no associated channels anymore + assert_eq!(network.get_nodes().len(), 0); + } + // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet. + } + + #[test] + fn getting_next_channel_announcements() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + let short_channel_id = 1; + let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); + + // Channels were not announced yet. + let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(0, 1); + assert_eq!(channels_with_announcements.len(), 0); + + { + // Announce a channel we will update + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::empty(), + chain_hash, + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_channel_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + // Contains initial channel announcement now. + let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1); + assert_eq!(channels_with_announcements.len(), 1); + if let Some(channel_announcements) = channels_with_announcements.first() { + let &(_, ref update_1, ref update_2) = channel_announcements; + assert_eq!(update_1, &None); + assert_eq!(update_2, &None); + } else { + panic!(); + } + + + { + // Valid channel update + let unsigned_channel_update = UnsignedChannelUpdate { + chain_hash, + short_channel_id, + timestamp: 101, + flags: 0, + cltv_expiry_delta: 144, + htlc_minimum_msat: 1000000, + fee_base_msat: 10000, + fee_proportional_millionths: 20, + excess_data: Vec::new() + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + // Now contains an initial announcement and an update. + let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1); + assert_eq!(channels_with_announcements.len(), 1); + if let Some(channel_announcements) = channels_with_announcements.first() { + let &(_, ref update_1, ref update_2) = channel_announcements; + assert_ne!(update_1, &None); + assert_eq!(update_2, &None); + } else { + panic!(); + } + + + { + // Channel update with excess data. + let unsigned_channel_update = UnsignedChannelUpdate { + chain_hash, + short_channel_id, + timestamp: 102, + flags: 0, + cltv_expiry_delta: 144, + htlc_minimum_msat: 1000000, + fee_base_msat: 10000, + fee_proportional_millionths: 20, + excess_data: [1; 3].to_vec() + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_channel_update.clone() + }; + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + // Test that announcements with excess data won't be returned + let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id, 1); + assert_eq!(channels_with_announcements.len(), 1); + if let Some(channel_announcements) = channels_with_announcements.first() { + let &(_, ref update_1, ref update_2) = channel_announcements; + assert_eq!(update_1, &None); + assert_eq!(update_2, &None); + } else { + panic!(); + } + + // Further starting point have no channels after it + let channels_with_announcements = net_graph_msg_handler.get_next_channel_announcements(short_channel_id + 1000, 1); + assert_eq!(channels_with_announcements.len(), 0); + } + + #[test] + fn getting_next_node_announcements() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + let short_channel_id = 1; + let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); + + // No nodes yet. + let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 10); + assert_eq!(next_announcements.len(), 0); + + { + // Announce a channel to add 2 nodes + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::empty(), + chain_hash, + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_channel_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_channel_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + + // Nodes were never announced + let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3); + assert_eq!(next_announcements.len(), 0); + + { + let mut unsigned_announcement = UnsignedNodeAnnouncement { + features: NodeFeatures::known(), + timestamp: 1000, + node_id: node_id_1, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: Vec::new(), + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }; + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + + unsigned_announcement.node_id = node_id_2; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_2_privkey), + contents: unsigned_announcement.clone() + }; + + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + let next_announcements = net_graph_msg_handler.get_next_node_announcements(None, 3); + assert_eq!(next_announcements.len(), 2); + + // Skip the first node. + let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2); + assert_eq!(next_announcements.len(), 1); + + { + // Later announcement which should not be relayed (excess data) prevent us from sharing a node + let unsigned_announcement = UnsignedNodeAnnouncement { + features: NodeFeatures::known(), + timestamp: 1010, + node_id: node_id_2, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: [1; 3].to_vec(), + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_2_privkey), + contents: unsigned_announcement.clone() + }; + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(res) => assert!(!res), + Err(_) => panic!() + }; + } + + let next_announcements = net_graph_msg_handler.get_next_node_announcements(Some(&node_id_1), 2); + assert_eq!(next_announcements.len(), 0); + } + + #[test] + fn network_graph_serialization() { + let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(); + + let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); + let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); + let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); + + // Announce a channel to add a corresponding node. + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + let unsigned_announcement = UnsignedChannelAnnouncement { + features: ChannelFeatures::known(), + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 0, + node_id_1, + node_id_2, + bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), + bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; + + + let node_id = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let unsigned_announcement = UnsignedNodeAnnouncement { + features: NodeFeatures::known(), + timestamp: 100, + node_id, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: Vec::new(), + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_1_privkey), + contents: unsigned_announcement.clone() + }; + + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + + let network = net_graph_msg_handler.network_graph.write().unwrap(); + let mut w = test_utils::TestVecWriter(Vec::new()); + assert!(!network.get_nodes().is_empty()); + assert!(!network.get_channels().is_empty()); + network.write(&mut w).unwrap(); + assert!(::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network); + } +} diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 07798509d..5fa135218 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -4,28 +4,17 @@ //! interrogate it to get routes for your own payments. use bitcoin::secp256k1::key::PublicKey; -use bitcoin::secp256k1::Secp256k1; -use bitcoin::secp256k1; -use bitcoin::hashes::sha256d::Hash as Sha256dHash; -use bitcoin::hashes::Hash; -use bitcoin::blockdata::script::Builder; -use bitcoin::blockdata::opcodes; - -use chain::chaininterface::{ChainError, ChainWatchInterface}; use ln::channelmanager; use ln::features::{ChannelFeatures, NodeFeatures}; -use ln::msgs::{DecodeError,ErrorAction,LightningError,RoutingMessageHandler,NetAddress}; -use ln::msgs; -use util::ser::{Writeable, Readable, Writer, ReadableArgs}; -use util::logger::Logger; +use ln::msgs::{DecodeError,ErrorAction,LightningError}; +use routing::network_graph::{NetGraphMsgHandler, RoutingFees}; +use util::ser::{Writeable, Readable}; +use util::logger::{Logger, LogHolder}; use std::cmp; -use std::sync::{RwLock,Arc}; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::collections::{HashMap,BinaryHeap,BTreeMap}; -use std::collections::btree_map::Entry as BtreeEntry; -use std; +use std::sync::Arc; +use std::collections::{HashMap,BinaryHeap}; /// A hop in a route #[derive(Clone, PartialEq)] @@ -114,601 +103,20 @@ impl Readable for Route { } } -#[derive(PartialEq)] -struct DirectionalChannelInfo { - src_node_id: PublicKey, - last_update: u32, - enabled: bool, - cltv_expiry_delta: u16, - htlc_minimum_msat: u64, - fee_base_msat: u32, - fee_proportional_millionths: u32, - last_update_message: Option, -} - -impl std::fmt::Display for DirectionalChannelInfo { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!(f, "src_node_id {}, last_update {}, enabled {}, cltv_expiry_delta {}, htlc_minimum_msat {}, fee_base_msat {}, fee_proportional_millionths {}", log_pubkey!(self.src_node_id), self.last_update, self.enabled, self.cltv_expiry_delta, self.htlc_minimum_msat, self.fee_base_msat, self.fee_proportional_millionths)?; - Ok(()) - } -} - -impl_writeable!(DirectionalChannelInfo, 0, { - src_node_id, - last_update, - enabled, - cltv_expiry_delta, - htlc_minimum_msat, - fee_base_msat, - fee_proportional_millionths, - last_update_message -}); - -#[derive(PartialEq)] -struct ChannelInfo { - features: ChannelFeatures, - one_to_two: DirectionalChannelInfo, - two_to_one: DirectionalChannelInfo, - //this is cached here so we can send out it later if required by route_init_sync - //keep an eye on this to see if the extra memory is a problem - announcement_message: Option, -} - -impl std::fmt::Display for ChannelInfo { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!(f, "features: {}, one_to_two: {}, two_to_one: {}", log_bytes!(self.features.encode()), self.one_to_two, self.two_to_one)?; - Ok(()) - } -} - -impl_writeable!(ChannelInfo, 0, { - features, - one_to_two, - two_to_one, - announcement_message -}); - -#[derive(PartialEq)] -struct NodeInfo { - channels: Vec, - - lowest_inbound_channel_fee_base_msat: u32, - lowest_inbound_channel_fee_proportional_millionths: u32, - - features: NodeFeatures, - /// Unlike for channels, we may have a NodeInfo entry before having received a node_update. - /// Thus, we have to be able to capture "no update has been received", which we do with an - /// Option here. - last_update: Option, - rgb: [u8; 3], - alias: [u8; 32], - addresses: Vec, - //this is cached here so we can send out it later if required by route_init_sync - //keep an eye on this to see if the extra memory is a problem - announcement_message: Option, -} - -impl std::fmt::Display for NodeInfo { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fee_base_msat: {}, lowest_inbound_channel_fee_proportional_millionths: {}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fee_base_msat, self.lowest_inbound_channel_fee_proportional_millionths, &self.channels[..])?; - Ok(()) - } -} - -impl Writeable for NodeInfo { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - (self.channels.len() as u64).write(writer)?; - for ref chan in self.channels.iter() { - chan.write(writer)?; - } - self.lowest_inbound_channel_fee_base_msat.write(writer)?; - self.lowest_inbound_channel_fee_proportional_millionths.write(writer)?; - self.features.write(writer)?; - self.last_update.write(writer)?; - self.rgb.write(writer)?; - self.alias.write(writer)?; - (self.addresses.len() as u64).write(writer)?; - for ref addr in &self.addresses { - addr.write(writer)?; - } - self.announcement_message.write(writer)?; - Ok(()) - } -} - -const MAX_ALLOC_SIZE: u64 = 64*1024; - -impl Readable for NodeInfo { - fn read(reader: &mut R) -> Result { - let channels_count: u64 = Readable::read(reader)?; - let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize); - for _ in 0..channels_count { - channels.push(Readable::read(reader)?); - } - let lowest_inbound_channel_fee_base_msat = Readable::read(reader)?; - let lowest_inbound_channel_fee_proportional_millionths = Readable::read(reader)?; - let features = Readable::read(reader)?; - let last_update = Readable::read(reader)?; - let rgb = Readable::read(reader)?; - let alias = Readable::read(reader)?; - let addresses_count: u64 = Readable::read(reader)?; - let mut addresses = Vec::with_capacity(cmp::min(addresses_count, MAX_ALLOC_SIZE / 40) as usize); - for _ in 0..addresses_count { - match Readable::read(reader) { - Ok(Ok(addr)) => { addresses.push(addr); }, - Ok(Err(_)) => return Err(DecodeError::InvalidValue), - Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor), - _ => unreachable!(), - } - } - let announcement_message = Readable::read(reader)?; - Ok(NodeInfo { - channels, - lowest_inbound_channel_fee_base_msat, - lowest_inbound_channel_fee_proportional_millionths, - features, - last_update, - rgb, - alias, - addresses, - announcement_message - }) - } -} - -#[derive(PartialEq)] -struct NetworkMap { - channels: BTreeMap, - our_node_id: PublicKey, - nodes: BTreeMap, -} - -impl Writeable for NetworkMap { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - (self.channels.len() as u64).write(writer)?; - for (ref chan_id, ref chan_info) in self.channels.iter() { - (*chan_id).write(writer)?; - chan_info.write(writer)?; - } - self.our_node_id.write(writer)?; - (self.nodes.len() as u64).write(writer)?; - for (ref node_id, ref node_info) in self.nodes.iter() { - node_id.write(writer)?; - node_info.write(writer)?; - } - Ok(()) - } -} - -impl Readable for NetworkMap { - fn read(reader: &mut R) -> Result { - let channels_count: u64 = Readable::read(reader)?; - let mut channels = BTreeMap::new(); - for _ in 0..channels_count { - let chan_id: u64 = Readable::read(reader)?; - let chan_info = Readable::read(reader)?; - channels.insert(chan_id, chan_info); - } - let our_node_id = Readable::read(reader)?; - let nodes_count: u64 = Readable::read(reader)?; - let mut nodes = BTreeMap::new(); - for _ in 0..nodes_count { - let node_id = Readable::read(reader)?; - let node_info = Readable::read(reader)?; - nodes.insert(node_id, node_info); - } - Ok(NetworkMap { - channels, - our_node_id, - nodes, - }) - } -} - -impl std::fmt::Display for NetworkMap { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - write!(f, "Node id {} network map\n[Channels]\n", log_pubkey!(self.our_node_id))?; - for (key, val) in self.channels.iter() { - write!(f, " {}: {}\n", key, val)?; - } - write!(f, "[Nodes]\n")?; - for (key, val) in self.nodes.iter() { - write!(f, " {}: {}\n", log_pubkey!(key), val)?; - } - Ok(()) - } -} - /// A channel descriptor which provides a last-hop route to get_route pub struct RouteHint { /// The node_id of the non-target end of the route pub src_node_id: PublicKey, /// The short_channel_id of this channel pub short_channel_id: u64, - /// The static msat-denominated fee which must be paid to use this channel - pub fee_base_msat: u32, - /// The dynamic proportional fee which must be paid to use this channel, denominated in - /// millionths of the value being forwarded to the next hop. - pub fee_proportional_millionths: u32, + /// The fees which must be paid to use this channel + pub fees: RoutingFees, /// The difference in CLTV values between this node and the next node. pub cltv_expiry_delta: u16, /// The minimum value, in msat, which must be relayed to the next hop. pub htlc_minimum_msat: u64, } -/// Tracks a view of the network, receiving updates from peers and generating Routes to -/// payment destinations. -pub struct Router { - secp_ctx: Secp256k1, - network_map: RwLock, - full_syncs_requested: AtomicUsize, - chain_monitor: Arc, - logger: Arc, -} - -const SERIALIZATION_VERSION: u8 = 1; -const MIN_SERIALIZATION_VERSION: u8 = 1; - -impl Writeable for Router { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { - writer.write_all(&[SERIALIZATION_VERSION; 1])?; - writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?; - - let network = self.network_map.read().unwrap(); - network.write(writer)?; - Ok(()) - } -} - -/// Arguments for the creation of a Router that are not deserialized. -/// At a high-level, the process for deserializing a Router and resuming normal operation is: -/// 1) Deserialize the Router by filling in this struct and calling ::read(reaser, args). -/// 2) Register the new Router with your ChainWatchInterface -pub struct RouterReadArgs { - /// The ChainWatchInterface for use in the Router in the future. - /// - /// No calls to the ChainWatchInterface will be made during deserialization. - pub chain_monitor: Arc, - /// The Logger for use in the ChannelManager and which may be used to log information during - /// deserialization. - pub logger: Arc, -} - -impl ReadableArgs for Router { - fn read(reader: &mut R, args: RouterReadArgs) -> Result { - let _ver: u8 = Readable::read(reader)?; - let min_ver: u8 = Readable::read(reader)?; - if min_ver > SERIALIZATION_VERSION { - return Err(DecodeError::UnknownVersion); - } - let network_map = Readable::read(reader)?; - Ok(Router { - secp_ctx: Secp256k1::verification_only(), - network_map: RwLock::new(network_map), - full_syncs_requested: AtomicUsize::new(0), - chain_monitor: args.chain_monitor, - logger: args.logger, - }) - } -} - -macro_rules! secp_verify_sig { - ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => { - match $secp_ctx.verify($msg, $sig, $pubkey) { - Ok(_) => {}, - Err(_) => return Err(LightningError{err: "Invalid signature from remote node", action: ErrorAction::IgnoreError}), - } - }; -} - -impl RoutingMessageHandler for Router { - - fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result { - let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id); - - let mut network = self.network_map.write().unwrap(); - match network.nodes.get_mut(&msg.contents.node_id) { - None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}), - Some(node) => { - match node.last_update { - Some(last_update) => if last_update >= msg.contents.timestamp { - return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError}); - }, - None => {}, - } - - node.features = msg.contents.features.clone(); - node.last_update = Some(msg.contents.timestamp); - node.rgb = msg.contents.rgb; - node.alias = msg.contents.alias; - node.addresses = msg.contents.addresses.clone(); - - let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty(); - node.announcement_message = if should_relay { Some(msg.clone()) } else { None }; - Ok(should_relay) - } - } - } - - fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result { - if msg.contents.node_id_1 == msg.contents.node_id_2 || msg.contents.bitcoin_key_1 == msg.contents.bitcoin_key_2 { - return Err(LightningError{err: "Channel announcement node had a channel with itself", action: ErrorAction::IgnoreError}); - } - - let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2); - - let checked_utxo = match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) { - Ok((script_pubkey, _value)) => { - let expected_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2) - .push_slice(&msg.contents.bitcoin_key_1.serialize()) - .push_slice(&msg.contents.bitcoin_key_2.serialize()) - .push_opcode(opcodes::all::OP_PUSHNUM_2) - .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh(); - if script_pubkey != expected_script { - return Err(LightningError{err: "Channel announcement keys didn't match on-chain script", action: ErrorAction::IgnoreError}); - } - //TODO: Check if value is worth storing, use it to inform routing, and compare it - //to the new HTLC max field in channel_update - true - }, - Err(ChainError::NotSupported) => { - // Tentatively accept, potentially exposing us to DoS attacks - false - }, - Err(ChainError::NotWatched) => { - return Err(LightningError{err: "Channel announced on an unknown chain", action: ErrorAction::IgnoreError}); - }, - Err(ChainError::UnknownTx) => { - return Err(LightningError{err: "Channel announced without corresponding UTXO entry", action: ErrorAction::IgnoreError}); - }, - }; - - let mut network_lock = self.network_map.write().unwrap(); - let network = &mut *network_lock; - - let should_relay = msg.contents.excess_data.is_empty(); - - let chan_info = ChannelInfo { - features: msg.contents.features.clone(), - one_to_two: DirectionalChannelInfo { - src_node_id: msg.contents.node_id_1.clone(), - last_update: 0, - enabled: false, - cltv_expiry_delta: u16::max_value(), - htlc_minimum_msat: u64::max_value(), - fee_base_msat: u32::max_value(), - fee_proportional_millionths: u32::max_value(), - last_update_message: None, - }, - two_to_one: DirectionalChannelInfo { - src_node_id: msg.contents.node_id_2.clone(), - last_update: 0, - enabled: false, - cltv_expiry_delta: u16::max_value(), - htlc_minimum_msat: u64::max_value(), - fee_base_msat: u32::max_value(), - fee_proportional_millionths: u32::max_value(), - last_update_message: None, - }, - announcement_message: if should_relay { Some(msg.clone()) } else { None }, - }; - - match network.channels.entry(msg.contents.short_channel_id) { - BtreeEntry::Occupied(mut entry) => { - //TODO: because asking the blockchain if short_channel_id is valid is only optional - //in the blockchain API, we need to handle it smartly here, though it's unclear - //exactly how... - if checked_utxo { - // Either our UTXO provider is busted, there was a reorg, or the UTXO provider - // only sometimes returns results. In any case remove the previous entry. Note - // that the spec expects us to "blacklist" the node_ids involved, but we can't - // do that because - // a) we don't *require* a UTXO provider that always returns results. - // b) we don't track UTXOs of channels we know about and remove them if they - // get reorg'd out. - // c) it's unclear how to do so without exposing ourselves to massive DoS risk. - Self::remove_channel_in_nodes(&mut network.nodes, &entry.get(), msg.contents.short_channel_id); - *entry.get_mut() = chan_info; - } else { - return Err(LightningError{err: "Already have knowledge of channel", action: ErrorAction::IgnoreError}) - } - }, - BtreeEntry::Vacant(entry) => { - entry.insert(chan_info); - } - }; - - macro_rules! add_channel_to_node { - ( $node_id: expr ) => { - match network.nodes.entry($node_id) { - BtreeEntry::Occupied(node_entry) => { - node_entry.into_mut().channels.push(msg.contents.short_channel_id); - }, - BtreeEntry::Vacant(node_entry) => { - node_entry.insert(NodeInfo { - channels: vec!(msg.contents.short_channel_id), - lowest_inbound_channel_fee_base_msat: u32::max_value(), - lowest_inbound_channel_fee_proportional_millionths: u32::max_value(), - features: NodeFeatures::empty(), - last_update: None, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - } - } - }; - } - - add_channel_to_node!(msg.contents.node_id_1); - add_channel_to_node!(msg.contents.node_id_2); - - log_trace!(self, "Added channel_announcement for {}{}", msg.contents.short_channel_id, if !should_relay { " with excess uninterpreted data!" } else { "" }); - Ok(should_relay) - } - - fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) { - match update { - &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => { - let _ = self.handle_channel_update(msg); - }, - &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id, ref is_permanent } => { - let mut network = self.network_map.write().unwrap(); - if *is_permanent { - if let Some(chan) = network.channels.remove(short_channel_id) { - Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id); - } - } else { - if let Some(chan) = network.channels.get_mut(short_channel_id) { - chan.one_to_two.enabled = false; - chan.two_to_one.enabled = false; - } - } - }, - &msgs::HTLCFailChannelUpdate::NodeFailure { ref node_id, ref is_permanent } => { - if *is_permanent { - //TODO: Wholly remove the node - } else { - self.mark_node_bad(node_id, false); - } - }, - } - } - - fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result { - let mut network = self.network_map.write().unwrap(); - let dest_node_id; - let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1); - let chan_was_enabled; - - match network.channels.get_mut(&msg.contents.short_channel_id) { - None => return Err(LightningError{err: "Couldn't find channel for update", action: ErrorAction::IgnoreError}), - Some(channel) => { - macro_rules! maybe_update_channel_info { - ( $target: expr) => { - if $target.last_update >= msg.contents.timestamp { - return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError}); - } - chan_was_enabled = $target.enabled; - $target.last_update = msg.contents.timestamp; - $target.enabled = chan_enabled; - $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta; - $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat; - $target.fee_base_msat = msg.contents.fee_base_msat; - $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths; - $target.last_update_message = if msg.contents.excess_data.is_empty() { - Some(msg.clone()) - } else { - None - }; - } - } - let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); - if msg.contents.flags & 1 == 1 { - dest_node_id = channel.one_to_two.src_node_id.clone(); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id); - maybe_update_channel_info!(channel.two_to_one); - } else { - dest_node_id = channel.two_to_one.src_node_id.clone(); - secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id); - maybe_update_channel_info!(channel.one_to_two); - } - } - } - - if chan_enabled { - let node = network.nodes.get_mut(&dest_node_id).unwrap(); - node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat); - node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths); - } else if chan_was_enabled { - let mut lowest_inbound_channel_fee_base_msat = u32::max_value(); - let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value(); - - { - let node = network.nodes.get(&dest_node_id).unwrap(); - - for chan_id in node.channels.iter() { - let chan = network.channels.get(chan_id).unwrap(); - if chan.one_to_two.src_node_id == dest_node_id { - lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat); - lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths); - } else { - lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat); - lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths); - } - } - } - - //TODO: satisfy the borrow-checker without a double-map-lookup :( - let mut_node = network.nodes.get_mut(&dest_node_id).unwrap(); - mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat; - mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths; - } - - Ok(msg.contents.excess_data.is_empty()) - } - - fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option, Option)> { - let mut result = Vec::with_capacity(batch_amount as usize); - let network = self.network_map.read().unwrap(); - let mut iter = network.channels.range(starting_point..); - while result.len() < batch_amount as usize { - if let Some((_, ref chan)) = iter.next() { - if chan.announcement_message.is_some() { - result.push((chan.announcement_message.clone().unwrap(), - chan.one_to_two.last_update_message.clone(), - chan.two_to_one.last_update_message.clone())); - } else { - // TODO: We may end up sending un-announced channel_updates if we are sending - // initial sync data while receiving announce/updates for this channel. - } - } else { - return result; - } - } - result - } - - fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec { - let mut result = Vec::with_capacity(batch_amount as usize); - let network = self.network_map.read().unwrap(); - let mut iter = if let Some(pubkey) = starting_point { - let mut iter = network.nodes.range((*pubkey)..); - iter.next(); - iter - } else { - network.nodes.range(..) - }; - while result.len() < batch_amount as usize { - if let Some((_, ref node)) = iter.next() { - if node.announcement_message.is_some() { - result.push(node.announcement_message.clone().unwrap()); - } - } else { - return result; - } - } - result - } - - fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool { - //TODO: Determine whether to request a full sync based on the network map. - const FULL_SYNCS_TO_REQUEST: usize = 5; - if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST { - self.full_syncs_requested.fetch_add(1, Ordering::AcqRel); - true - } else { - false - } - } -} - #[derive(Eq, PartialEq)] struct RouteGraphNode { pubkey: PublicKey, @@ -733,360 +141,361 @@ struct DummyDirectionalChannelInfo { src_node_id: PublicKey, cltv_expiry_delta: u32, htlc_minimum_msat: u64, - fee_base_msat: u32, - fee_proportional_millionths: u32, + fees: RoutingFees, } -impl Router { - /// Creates a new router with the given node_id to be used as the source for get_route() - pub fn new(our_pubkey: PublicKey, chain_monitor: Arc, logger: Arc) -> Router { - let mut nodes = BTreeMap::new(); - nodes.insert(our_pubkey.clone(), NodeInfo { - channels: Vec::new(), - lowest_inbound_channel_fee_base_msat: u32::max_value(), - lowest_inbound_channel_fee_proportional_millionths: u32::max_value(), - features: NodeFeatures::empty(), - last_update: None, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - Router { - secp_ctx: Secp256k1::verification_only(), - network_map: RwLock::new(NetworkMap { - channels: BTreeMap::new(), - our_node_id: our_pubkey, - nodes: nodes, - }), - full_syncs_requested: AtomicUsize::new(0), - chain_monitor, - logger, - } - } - - /// Dumps the entire network view of this Router to the logger provided in the constructor at - /// level Trace - pub fn trace_state(&self) { - log_trace!(self, "{}", self.network_map.read().unwrap()); - } - - /// Get network addresses by node id - pub fn get_addresses(&self, pubkey: &PublicKey) -> Option> { - let network = self.network_map.read().unwrap(); - network.nodes.get(pubkey).map(|n| n.addresses.clone()) - } - /// Marks a node as having failed a route. This will avoid re-using the node in routes for now, - /// with an exponential decay in node "badness". Note that there is deliberately no - /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is - /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node - /// boolean will reduce the penalty, returning the node to usability faster. If the node is - /// behaving correctly, it will disable the failing channel and we will use it again next time. - pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) { - unimplemented!(); +/// Gets a route from us (as specified in the provided NetGraphMsgHandler) to the given target node. +/// +/// Extra routing hops between known nodes and the target will be used if they are included in +/// last_hops. +/// +/// If some channels aren't announced, it may be useful to fill in a first_hops with the +/// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our +/// (this Router's) view of our local channels will be ignored, and only those in first_hops +/// will be used. +/// +/// Panics if first_hops contains channels without short_channel_ids +/// (ChannelManager::list_usable_channels will never include such channels). +/// +/// The fees on channels from us to next-hops are ignored (as they are assumed to all be +/// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat +/// *is* checked as they may change based on the receiving node. +pub fn get_route(our_node_id: &PublicKey, net_graph_msg_handler: &NetGraphMsgHandler, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, + last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32, logger: Arc) -> Result { + // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by + // uptime/success in using a node in the past. + if *target == *our_node_id { + return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError}); } - fn remove_channel_in_nodes(nodes: &mut BTreeMap, chan: &ChannelInfo, short_channel_id: u64) { - macro_rules! remove_from_node { - ($node_id: expr) => { - if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) { - entry.get_mut().channels.retain(|chan_id| { - short_channel_id != *chan_id - }); - if entry.get().channels.is_empty() { - entry.remove_entry(); - } - } else { - panic!("Had channel that pointed to unknown node (ie inconsistent network map)!"); - } - } - } - remove_from_node!(chan.one_to_two.src_node_id); - remove_from_node!(chan.two_to_one.src_node_id); + if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 { + return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError}); } - /// Gets a route from us to the given target node. - /// - /// Extra routing hops between known nodes and the target will be used if they are included in - /// last_hops. - /// - /// If some channels aren't announced, it may be useful to fill in a first_hops with the - /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our - /// (this Router's) view of our local channels will be ignored, and only those in first_hops - /// will be used. - /// - /// Panics if first_hops contains channels without short_channel_ids - /// (ChannelManager::list_usable_channels will never include such channels). - /// - /// The fees on channels from us to next-hops are ignored (as they are assumed to all be - /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat - /// *is* checked as they may change based on the receiving node. - pub fn get_route(&self, target: &PublicKey, first_hops: Option<&[channelmanager::ChannelDetails]>, last_hops: &[RouteHint], final_value_msat: u64, final_cltv: u32) -> Result { - // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by - // uptime/success in using a node in the past. - let network = self.network_map.read().unwrap(); - - if *target == network.our_node_id { - return Err(LightningError{err: "Cannot generate a route to ourselves", action: ErrorAction::IgnoreError}); - } + // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination + // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*"). + // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff + // to use as the A* heuristic beyond just the cost to get one node further than the current + // one. - if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 { - return Err(LightningError{err: "Cannot generate a route of more value than all existing satoshis", action: ErrorAction::IgnoreError}); + let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes + src_node_id: our_node_id.clone(), + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fees: RoutingFees { + base_msat: 0, + proportional_millionths: 0, } + }; - // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination - // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*"). - // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff - // to use as the A* heuristic beyond just the cost to get one node further than the current - // one. - - let dummy_directional_info = DummyDirectionalChannelInfo { // used for first_hops routes - src_node_id: network.our_node_id.clone(), - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - }; - - let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap? - let mut dist = HashMap::with_capacity(network.nodes.len()); - - let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 }); - if let Some(hops) = first_hops { - for chan in hops { - let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones"); - if chan.remote_network_id == *target { - return Ok(Route { - paths: vec![vec![RouteHop { - pubkey: chan.remote_network_id, - node_features: chan.counterparty_features.to_context(), - short_channel_id, - channel_features: chan.counterparty_features.to_context(), - fee_msat: final_value_msat, - cltv_expiry_delta: final_cltv, - }]], - }); - } - first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone())); - } - if first_hop_targets.is_empty() { - return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError}); + let network = net_graph_msg_handler.network_graph.read().unwrap(); + let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap? + let mut dist = HashMap::with_capacity(network.get_nodes().len()); + + let mut first_hop_targets = HashMap::with_capacity(if first_hops.is_some() { first_hops.as_ref().unwrap().len() } else { 0 }); + if let Some(hops) = first_hops { + for chan in hops { + let short_channel_id = chan.short_channel_id.expect("first_hops should be filled in with usable channels, not pending ones"); + if chan.remote_network_id == *target { + return Ok(Route { + paths: vec![vec![RouteHop { + pubkey: chan.remote_network_id, + node_features: chan.counterparty_features.to_context(), + short_channel_id, + channel_features: chan.counterparty_features.to_context(), + fee_msat: final_value_msat, + cltv_expiry_delta: final_cltv, + }]], + }); } + first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone())); + } + if first_hop_targets.is_empty() { + return Err(LightningError{err: "Cannot route when there are no outbound routes away from us", action: ErrorAction::IgnoreError}); } + } - macro_rules! add_entry { - // Adds entry which goes from the node pointed to by $directional_info to - // $dest_node_id over the channel with id $chan_id with fees described in - // $directional_info. - ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => { - //TODO: Explore simply adding fee to hit htlc_minimum_msat - if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat { - let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fee_proportional_millionths as u64); - if let Some(new_fee) = proportional_fee_millions.and_then(|part| { - ($directional_info.fee_base_msat as u64).checked_add(part / 1000000) }) - { - let mut total_fee = $starting_fee_msat as u64; - let hm_entry = dist.entry(&$directional_info.src_node_id); - let old_entry = hm_entry.or_insert_with(|| { - let node = network.nodes.get(&$directional_info.src_node_id).unwrap(); - (u64::max_value(), - node.lowest_inbound_channel_fee_base_msat, - node.lowest_inbound_channel_fee_proportional_millionths, - RouteHop { - pubkey: $dest_node_id.clone(), - node_features: NodeFeatures::empty(), - short_channel_id: 0, - channel_features: $chan_features.clone(), - fee_msat: 0, - cltv_expiry_delta: 0, - }) - }); - if $directional_info.src_node_id != network.our_node_id { - // Ignore new_fee for channel-from-us as we assume all channels-from-us - // will have the same effective-fee - total_fee += new_fee; - if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) { - total_fee += fee_inc / 1000000 + (old_entry.1 as u64); - } else { - // max_value means we'll always fail the old_entry.0 > total_fee check - total_fee = u64::max_value(); - } - } - let new_graph_node = RouteGraphNode { - pubkey: $directional_info.src_node_id, - lowest_fee_to_peer_through_node: total_fee, - lowest_fee_to_node: $starting_fee_msat as u64 + new_fee, + macro_rules! add_entry { + // Adds entry which goes from the node pointed to by $directional_info to + // $dest_node_id over the channel with id $chan_id with fees described in + // $directional_info. + ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $chan_features: expr, $starting_fee_msat: expr ) => { + //TODO: Explore simply adding fee to hit htlc_minimum_msat + if $starting_fee_msat as u64 + final_value_msat >= $directional_info.htlc_minimum_msat { + let proportional_fee_millions = ($starting_fee_msat + final_value_msat).checked_mul($directional_info.fees.proportional_millionths as u64); + if let Some(new_fee) = proportional_fee_millions.and_then(|part| { + ($directional_info.fees.base_msat as u64).checked_add(part / 1000000) }) + { + let mut total_fee = $starting_fee_msat as u64; + let hm_entry = dist.entry(&$directional_info.src_node_id); + let old_entry = hm_entry.or_insert_with(|| { + let node = network.get_nodes().get(&$directional_info.src_node_id).unwrap(); + let mut fee_base_msat = u32::max_value(); + let mut fee_proportional_millionths = u32::max_value(); + if let Some(fees) = node.lowest_inbound_channel_fees { + fee_base_msat = fees.base_msat; + fee_proportional_millionths = fees.proportional_millionths; }; - if old_entry.0 > total_fee { - targets.push(new_graph_node); - old_entry.0 = total_fee; - old_entry.3 = RouteHop { + (u64::max_value(), + fee_base_msat, + fee_proportional_millionths, + RouteHop { pubkey: $dest_node_id.clone(), node_features: NodeFeatures::empty(), - short_channel_id: $chan_id.clone(), + short_channel_id: 0, channel_features: $chan_features.clone(), - fee_msat: new_fee, // This field is ignored on the last-hop anyway - cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32, - } + fee_msat: 0, + cltv_expiry_delta: 0, + }) + }); + if $directional_info.src_node_id != *our_node_id { + // Ignore new_fee for channel-from-us as we assume all channels-from-us + // will have the same effective-fee + total_fee += new_fee; + if let Some(fee_inc) = final_value_msat.checked_add(total_fee).and_then(|inc| { (old_entry.2 as u64).checked_mul(inc) }) { + total_fee += fee_inc / 1000000 + (old_entry.1 as u64); + } else { + // max_value means we'll always fail the old_entry.0 > total_fee check + total_fee = u64::max_value(); + } + } + let new_graph_node = RouteGraphNode { + pubkey: $directional_info.src_node_id, + lowest_fee_to_peer_through_node: total_fee, + lowest_fee_to_node: $starting_fee_msat as u64 + new_fee, + }; + if old_entry.0 > total_fee { + targets.push(new_graph_node); + old_entry.0 = total_fee; + old_entry.3 = RouteHop { + pubkey: $dest_node_id.clone(), + node_features: NodeFeatures::empty(), + short_channel_id: $chan_id.clone(), + channel_features: $chan_features.clone(), + fee_msat: new_fee, // This field is ignored on the last-hop anyway + cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32, } } } - }; - } + } + }; + } - macro_rules! add_entries_to_cheapest_to_target_node { - ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => { - if first_hops.is_some() { - if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) { - add_entry!(first_hop, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat); - } + macro_rules! add_entries_to_cheapest_to_target_node { + ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => { + if first_hops.is_some() { + if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) { + add_entry!(first_hop, $node_id, dummy_directional_info, features.to_context(), $fee_to_target_msat); } + } - if !$node.features.requires_unknown_bits() { - for chan_id in $node.channels.iter() { - let chan = network.channels.get(chan_id).unwrap(); - if !chan.features.requires_unknown_bits() { - if chan.one_to_two.src_node_id == *$node_id { - // ie $node is one, ie next hop in A* is two, via the two_to_one channel - if first_hops.is_none() || chan.two_to_one.src_node_id != network.our_node_id { - if chan.two_to_one.enabled { - add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat); - } + if !$node.features.requires_unknown_bits() { + for chan_id in $node.channels.iter() { + let chan = network.get_channels().get(chan_id).unwrap(); + if !chan.features.requires_unknown_bits() { + if chan.one_to_two.src_node_id == *$node_id { + // ie $node is one, ie next hop in A* is two, via the two_to_one channel + if first_hops.is_none() || chan.two_to_one.src_node_id != *our_node_id { + if chan.two_to_one.enabled { + add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, chan.features, $fee_to_target_msat); } - } else { - if first_hops.is_none() || chan.one_to_two.src_node_id != network.our_node_id { - if chan.one_to_two.enabled { - add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat); - } + } + } else { + if first_hops.is_none() || chan.one_to_two.src_node_id != *our_node_id { + if chan.one_to_two.enabled { + add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, chan.features, $fee_to_target_msat); } } } } } - }; - } + } + }; + } - match network.nodes.get(target) { - None => {}, - Some(node) => { - add_entries_to_cheapest_to_target_node!(node, target, 0); - }, - } + match network.get_nodes().get(target) { + None => {}, + Some(node) => { + add_entries_to_cheapest_to_target_node!(node, target, 0); + }, + } - for hop in last_hops.iter() { - if first_hops.is_none() || hop.src_node_id != network.our_node_id { // first_hop overrules last_hops - if network.nodes.get(&hop.src_node_id).is_some() { - if first_hops.is_some() { - if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) { - // Currently there are no channel-context features defined, so we are a - // bit lazy here. In the future, we should pull them out via our - // ChannelManager, but there's no reason to waste the space until we - // need them. - add_entry!(first_hop, hop.src_node_id, dummy_directional_info, features.to_context(), 0); - } + for hop in last_hops.iter() { + if first_hops.is_none() || hop.src_node_id != *our_node_id { // first_hop overrules last_hops + if network.get_nodes().get(&hop.src_node_id).is_some() { + if first_hops.is_some() { + if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) { + // Currently there are no channel-context features defined, so we are a + // bit lazy here. In the future, we should pull them out via our + // ChannelManager, but there's no reason to waste the space until we + // need them. + add_entry!(first_hop, hop.src_node_id, dummy_directional_info, features.to_context(), 0); } - // BOLT 11 doesn't allow inclusion of features for the last hop hints, which - // really sucks, cause we're gonna need that eventually. - add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0); } + // BOLT 11 doesn't allow inclusion of features for the last hop hints, which + // really sucks, cause we're gonna need that eventually. + add_entry!(hop.short_channel_id, target, hop, ChannelFeatures::empty(), 0); } } + } - while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() { - if pubkey == network.our_node_id { - let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3); - loop { - if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) { - res.last_mut().unwrap().node_features = features.to_context(); - } else if let Some(node) = network.nodes.get(&res.last().unwrap().pubkey) { - res.last_mut().unwrap().node_features = node.features.clone(); - } else { - // We should be able to fill in features for everything except the last - // hop, if the last hop was provided via a BOLT 11 invoice (though we - // should be able to extend it further as BOLT 11 does have feature - // flags for the last hop node itself). - assert!(res.last().unwrap().pubkey == *target); - } - if res.last().unwrap().pubkey == *target { - break; - } - - let new_entry = match dist.remove(&res.last().unwrap().pubkey) { - Some(hop) => hop.3, - None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}), - }; - res.last_mut().unwrap().fee_msat = new_entry.fee_msat; - res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta; - res.push(new_entry); + while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, .. }) = targets.pop() { + if pubkey == *our_node_id { + let mut res = vec!(dist.remove(&our_node_id).unwrap().3); + loop { + if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) { + res.last_mut().unwrap().node_features = features.to_context(); + } else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) { + res.last_mut().unwrap().node_features = node.features.clone(); + } else { + // We should be able to fill in features for everything except the last + // hop, if the last hop was provided via a BOLT 11 invoice (though we + // should be able to extend it further as BOLT 11 does have feature + // flags for the last hop node itself). + assert!(res.last().unwrap().pubkey == *target); + } + if res.last().unwrap().pubkey == *target { + break; } - res.last_mut().unwrap().fee_msat = final_value_msat; - res.last_mut().unwrap().cltv_expiry_delta = final_cltv; - let route = Route { paths: vec![res] }; - log_trace!(self, "Got route: {}", log_route!(route)); - return Ok(route); - } - match network.nodes.get(&pubkey) { - None => {}, - Some(node) => { - add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node); - }, + let new_entry = match dist.remove(&res.last().unwrap().pubkey) { + Some(hop) => hop.3, + None => return Err(LightningError{err: "Failed to find a non-fee-overflowing path to the given destination", action: ErrorAction::IgnoreError}), + }; + res.last_mut().unwrap().fee_msat = new_entry.fee_msat; + res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta; + res.push(new_entry); } + res.last_mut().unwrap().fee_msat = final_value_msat; + res.last_mut().unwrap().cltv_expiry_delta = final_cltv; + let route = Route { paths: vec![res] }; + let log_holder = LogHolder { logger: &logger }; + log_trace!(log_holder, "Got route: {}", log_route!(route)); + return Ok(route); } - Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError}) + match network.get_nodes().get(&pubkey) { + None => {}, + Some(node) => { + add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node); + }, + } } + + Err(LightningError{err: "Failed to find a path to the given destination", action: ErrorAction::IgnoreError}) } #[cfg(test)] mod tests { use chain::chaininterface; - use ln::channelmanager; - use routing::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint}; + use routing::router::{NetGraphMsgHandler, RoutingFees}; + use routing::router::{get_route, RouteHint}; use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; - use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement, - UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate}; + use ln::msgs::{ErrorAction, LightningError, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler, + NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate}; + use ln::channelmanager; use util::test_utils; - use util::test_utils::TestVecWriter; use util::logger::Logger; - use util::ser::{Writeable, Readable}; + use util::ser::Writeable; use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; use bitcoin::blockdata::constants::genesis_block; - use bitcoin::blockdata::script::Builder; - use bitcoin::blockdata::opcodes; use bitcoin::util::hash::BitcoinHash; use hex; use bitcoin::secp256k1::key::{PublicKey,SecretKey}; - use bitcoin::secp256k1::All; - use bitcoin::secp256k1::Secp256k1; + use bitcoin::secp256k1::{Secp256k1, All}; use std::sync::Arc; - use std::collections::btree_map::Entry as BtreeEntry; - fn create_router() -> (Secp256k1, PublicKey, Router) { - let secp_ctx = Secp256k1::new(); - let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()); - let logger: Arc = Arc::new(test_utils::TestLogger::new()); - let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); - let router = Router::new(our_id, chain_monitor, Arc::clone(&logger)); - (secp_ctx, our_id, router) + // Using the same keys for LN and BTC ids + fn add_channel(net_graph_msg_handler: &NetGraphMsgHandler, secp_ctx: &Secp256k1, node_1_privkey: &SecretKey, + node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64) { + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + + let unsigned_announcement = UnsignedChannelAnnouncement { + features, + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: node_id_1, + bitcoin_key_2: node_id_2, + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_privkey), + bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_privkey), + contents: unsigned_announcement.clone(), + }; + match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; } - #[test] - fn route_test() { - let (secp_ctx, our_id, router) = create_router(); + fn update_channel(net_graph_msg_handler: &NetGraphMsgHandler, secp_ctx: &Secp256k1, node_privkey: &SecretKey, update: UnsignedChannelUpdate) { + let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign(&msghash, node_privkey), + contents: update.clone() + }; - // Build network from our_id to node8: - // - // -1(1)2- node1 -1(3)2- - // / \ - // our_id -1(12)2- node8 -1(13)2--- node3 + match net_graph_msg_handler.handle_channel_update(&valid_channel_update) { + Ok(res) => assert!(res), + // Err(_) => panic!() + Err(e) => println!("{:?}", e.err) + }; + } + + + fn add_or_update_node(net_graph_msg_handler: &NetGraphMsgHandler, secp_ctx: &Secp256k1, node_privkey: &SecretKey, + features: NodeFeatures, timestamp: u32) { + let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey); + let unsigned_announcement = UnsignedNodeAnnouncement { + features, + timestamp, + node_id, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: Vec::new(), + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign(&msghash, node_privkey), + contents: unsigned_announcement.clone() + }; + + match net_graph_msg_handler.handle_node_announcement(&valid_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; + } + + #[test] + fn route_test() { + let secp_ctx = Secp256k1::new(); + let our_privkey = &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap(); + let our_id = PublicKey::from_secret_key(&secp_ctx, our_privkey); + let logger: Arc = Arc::new(test_utils::TestLogger::new()); + let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); + let net_graph_msg_handler = NetGraphMsgHandler::new(chain_monitor, Arc::clone(&logger)); + // Build network from our_id to node8: + // + // -1(1)2- node1 -1(3)2- + // / \ + // our_id -1(12)2- node8 -1(13)2--- node3 // \ / // -1(2)2- node2 -1(4)2- // @@ -1139,14 +548,24 @@ mod tests { // chan11 1-to-2: enabled, 0 fee // chan11 2-to-1: enabled, 0 fee - let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()); - let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()); - let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()); - let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()); - let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()); - let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()); - let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()); - let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap()); + let node1_privkey = &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap(); + let node2_privkey = &SecretKey::from_slice(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap(); + let node3_privkey = &SecretKey::from_slice(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap(); + let node4_privkey = &SecretKey::from_slice(&hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap(); + let node5_privkey = &SecretKey::from_slice(&hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap(); + let node6_privkey = &SecretKey::from_slice(&hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap(); + let node7_privkey = &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap(); + let node8_privkey = &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap(); + + + let node1 = PublicKey::from_secret_key(&secp_ctx, node1_privkey); + let node2 = PublicKey::from_secret_key(&secp_ctx, node2_privkey); + let node3 = PublicKey::from_secret_key(&secp_ctx, node3_privkey); + let node4 = PublicKey::from_secret_key(&secp_ctx, node4_privkey); + let node5 = PublicKey::from_secret_key(&secp_ctx, node5_privkey); + let node6 = PublicKey::from_secret_key(&secp_ctx, node6_privkey); + let node7 = PublicKey::from_secret_key(&secp_ctx, node7_privkey); + let node8 = PublicKey::from_secret_key(&secp_ctx, node8_privkey); macro_rules! id_to_feature_flags { // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can @@ -1165,1459 +584,600 @@ mod tests { } } } - { - let mut network = router.network_map.write().unwrap(); - - network.nodes.insert(node1.clone(), NodeInfo { - channels: vec!(1, 3), - lowest_inbound_channel_fee_base_msat: 100, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(1, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)), - one_to_two: DirectionalChannelInfo { - src_node_id: our_id.clone(), - last_update: 0, - enabled: false, - cltv_expiry_delta: u16::max_value(), // This value should be ignored - htlc_minimum_msat: 0, - fee_base_msat: u32::max_value(), // This value should be ignored - fee_proportional_millionths: u32::max_value(), // This value should be ignored - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node1.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node2.clone(), NodeInfo { - channels: vec!(2, 4), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(2, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)), - one_to_two: DirectionalChannelInfo { - src_node_id: our_id.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: u16::max_value(), // This value should be ignored - htlc_minimum_msat: 0, - fee_base_msat: u32::max_value(), // This value should be ignored - fee_proportional_millionths: u32::max_value(), // This value should be ignored - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node2.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node8.clone(), NodeInfo { - channels: vec!(12, 13), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(12, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)), - one_to_two: DirectionalChannelInfo { - src_node_id: our_id.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: u16::max_value(), // This value should be ignored - htlc_minimum_msat: 0, - fee_base_msat: u32::max_value(), // This value should be ignored - fee_proportional_millionths: u32::max_value(), // This value should be ignored - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node8.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node3.clone(), NodeInfo { - channels: vec!( - 3, - 4, - 13, - 5, - 6, - 7), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(3)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(3, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)), - one_to_two: DirectionalChannelInfo { - src_node_id: node1.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (3 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (3 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 100, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.channels.insert(4, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)), - one_to_two: DirectionalChannelInfo { - src_node_id: node2.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (4 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 1000000, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (4 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.channels.insert(13, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)), - one_to_two: DirectionalChannelInfo { - src_node_id: node8.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (13 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 2000000, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (13 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node4.clone(), NodeInfo { - channels: vec!(5, 11), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(4)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(5, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(5)), - one_to_two: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (5 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 100, - fee_proportional_millionths: 0, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node4.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (5 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node5.clone(), NodeInfo { - channels: vec!(6, 11), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(5)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(6, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)), - one_to_two: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (6 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node5.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (6 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.channels.insert(11, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)), - one_to_two: DirectionalChannelInfo { - src_node_id: node5.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (11 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node4.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (11 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - network.nodes.insert(node6.clone(), NodeInfo { - channels: vec!(7), - lowest_inbound_channel_fee_base_msat: 0, - lowest_inbound_channel_fee_proportional_millionths: 0, - features: NodeFeatures::from_le_bytes(id_to_feature_flags!(6)), - last_update: Some(1), - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - announcement_message: None, - }); - network.channels.insert(7, ChannelInfo { - features: ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)), - one_to_two: DirectionalChannelInfo { - src_node_id: node3.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (7 << 8) | 1, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 1000000, - last_update_message: None, - }, two_to_one: DirectionalChannelInfo { - src_node_id: node6.clone(), - last_update: 0, - enabled: true, - cltv_expiry_delta: (7 << 8) | 2, - htlc_minimum_msat: 0, - fee_base_msat: 0, - fee_proportional_millionths: 0, - last_update_message: None, - }, - announcement_message: None, - }); - } + add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node1_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(1)), 1); + update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 1, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 0); - { // Simple route to 3 via 2 - let route = router.get_route(&node3, None, &Vec::new(), 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 2); - - assert_eq!(route.paths[0][0].pubkey, node2); - assert_eq!(route.paths[0][0].short_channel_id, 2); - assert_eq!(route.paths[0][0].fee_msat, 100); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 4); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); - } + add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node2_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(2)), 2); + update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 2, + timestamp: 1, + flags: 0, + cltv_expiry_delta: u16::max_value(), + htlc_minimum_msat: 0, + fee_base_msat: u32::max_value(), + fee_proportional_millionths: u32::max_value(), + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 2, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); - { // Disable channels 4 and 12 by requiring unknown feature bits - let mut network = router.network_map.write().unwrap(); - network.channels.get_mut(&4).unwrap().features.set_required_unknown_bits(); - network.channels.get_mut(&12).unwrap().features.set_required_unknown_bits(); - } + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 0); - { // If all the channels require some features we don't understand, route should fail - if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) { - assert_eq!(err, "Failed to find a path to the given destination"); - } else { panic!(); } - } + add_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, node8_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(12)), 12); + update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 12, + timestamp: 1, + flags: 0, + cltv_expiry_delta: u16::max_value(), + htlc_minimum_msat: 0, + fee_base_msat: u32::max_value(), + fee_proportional_millionths: u32::max_value(), + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 12, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); - { // If we specify a channel to node8, that overrides our local channel view and that gets used - let our_chans = vec![channelmanager::ChannelDetails { - channel_id: [0; 32], - short_channel_id: Some(42), - remote_network_id: node8.clone(), - counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), - channel_value_satoshis: 0, - user_id: 0, - outbound_capacity_msat: 0, - inbound_capacity_msat: 0, - is_live: true, - }]; - let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 2); - - assert_eq!(route.paths[0][0].pubkey, node8); - assert_eq!(route.paths[0][0].short_channel_id, 42); - assert_eq!(route.paths[0][0].fee_msat, 200); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features - assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 13); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); - } - { // Re-enable channels 4 and 12 by wiping the unknown feature bits - let mut network = router.network_map.write().unwrap(); - network.channels.get_mut(&4).unwrap().features.clear_unknown_bits(); - network.channels.get_mut(&12).unwrap().features.clear_unknown_bits(); - } + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 0); - { // Disable nodes 1, 2, and 8 by requiring unknown feature bits - let mut network = router.network_map.write().unwrap(); - network.nodes.get_mut(&node1).unwrap().features.set_required_unknown_bits(); - network.nodes.get_mut(&node2).unwrap().features.set_required_unknown_bits(); - network.nodes.get_mut(&node8).unwrap().features.set_required_unknown_bits(); - } + add_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(3)), 3); + update_channel(&net_graph_msg_handler, &secp_ctx, node1_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 3, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (3 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 3, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (3 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 100, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); - { // If all nodes require some features we don't understand, route should fail - if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = router.get_route(&node3, None, &Vec::new(), 100, 42) { - assert_eq!(err, "Failed to find a path to the given destination"); - } else { panic!(); } - } - { // If we specify a channel to node8, that overrides our local channel view and that gets used - let our_chans = vec![channelmanager::ChannelDetails { - channel_id: [0; 32], - short_channel_id: Some(42), - remote_network_id: node8.clone(), - counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), - channel_value_satoshis: 0, - user_id: 0, - outbound_capacity_msat: 0, - inbound_capacity_msat: 0, - is_live: true, - }]; - let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 2); - - assert_eq!(route.paths[0][0].pubkey, node8); - assert_eq!(route.paths[0][0].short_channel_id, 42); - assert_eq!(route.paths[0][0].fee_msat, 200); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features - assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 13); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); - } + add_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(4)), 4); + update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 4, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (4 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 1000000, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 4, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (4 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); - { // Re-enable nodes 1, 2, and 8 - let mut network = router.network_map.write().unwrap(); - network.nodes.get_mut(&node1).unwrap().features.clear_unknown_bits(); - network.nodes.get_mut(&node2).unwrap().features.clear_unknown_bits(); - network.nodes.get_mut(&node8).unwrap().features.clear_unknown_bits(); - } + add_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, node3_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(13)), 13); + update_channel(&net_graph_msg_handler, &secp_ctx, node8_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 13, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (13 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 2000000, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 13, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (13 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node3_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(3)), 0); + add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node5_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(6)), 6); + + update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 6, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (6 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 6, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (6 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, node4_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(11)), 11); + update_channel(&net_graph_msg_handler, &secp_ctx, node5_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 11, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (11 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node4_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 11, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (11 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node5_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(5)), 0); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node4_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(4)), 0); + + add_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, node6_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags!(7)), 7); + + update_channel(&net_graph_msg_handler, &secp_ctx, node3_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 7, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (7 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 1000000, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, node6_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 7, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (7 << 8) | 2, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node6_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(6)), 0); + + // Simple route to 3 via 2 + let route = get_route(&our_id, &net_graph_msg_handler, &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 2); + + assert_eq!(route.paths[0][0].pubkey, node2); + assert_eq!(route.paths[0][0].short_channel_id, 2); + assert_eq!(route.paths[0][0].fee_msat, 100); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 4); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); + + + // // Disable channels 4 and 12 by flags=2 + update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 4, + timestamp: 2, + flags: 2, // to disable + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 12, + timestamp: 2, + flags: 2, // to disable + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + // If all the channels require some features we don't understand, route should fail + if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler, &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)) { + assert_eq!(err, "Failed to find a path to the given destination"); + } else { panic!(); } + + // If we specify a channel to node8, that overrides our local channel view and that gets used + let our_chans = vec![channelmanager::ChannelDetails { + channel_id: [0; 32], + short_channel_id: Some(42), + remote_network_id: node8.clone(), + counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), + channel_value_satoshis: 0, + user_id: 0, + outbound_capacity_msat: 0, + inbound_capacity_msat: 0, + is_live: true, + }]; + let route = get_route(&our_id, &net_graph_msg_handler, &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 2); + + assert_eq!(route.paths[0][0].pubkey, node8); + assert_eq!(route.paths[0][0].short_channel_id, 42); + assert_eq!(route.paths[0][0].fee_msat, 200); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features + assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 13); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); + + // Re-enable channels 4 and 12 + update_channel(&net_graph_msg_handler, &secp_ctx, node2_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 4, + timestamp: 3, + flags: 0, // to enable + cltv_expiry_delta: (4 << 8) | 1, + htlc_minimum_msat: 0, + fee_base_msat: 0, + fee_proportional_millionths: 1000000, + excess_data: Vec::new() + }); + update_channel(&net_graph_msg_handler, &secp_ctx, our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), + short_channel_id: 12, + timestamp: 3, + flags: 0, // to enable + cltv_expiry_delta: u16::max_value(), + htlc_minimum_msat: 0, + fee_base_msat: u32::max_value(), + fee_proportional_millionths: u32::max_value(), + excess_data: Vec::new() + }); + // Disable nodes 1, 2, and 8 by requiring unknown feature bits + let mut unknown_features = NodeFeatures::known(); + unknown_features.set_required_unknown_bits(); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, unknown_features.clone(), 1); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, unknown_features.clone(), 1); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, unknown_features.clone(), 1); + + // // If all nodes require some features we don't understand, route should fail + // if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler, &node3, None, &Vec::new(), 100, 42, Arc::clone(&logger)) { + // assert_eq!(err, "Failed to find a path to the given destination"); + // } else { panic!(); } + + // If we specify a channel to node8, that overrides our local channel view and that gets used + let our_chans = vec![channelmanager::ChannelDetails { + channel_id: [0; 32], + short_channel_id: Some(42), + remote_network_id: node8.clone(), + counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), + channel_value_satoshis: 0, + user_id: 0, + outbound_capacity_msat: 0, + inbound_capacity_msat: 0, + is_live: true, + }]; + let route = get_route(&our_id, &net_graph_msg_handler, &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 2); + + assert_eq!(route.paths[0][0].pubkey, node8); + assert_eq!(route.paths[0][0].short_channel_id, 42); + assert_eq!(route.paths[0][0].fee_msat, 200); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features + assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 13); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); + + // Re-enable nodes 1, 2, and 8 + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node1_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(1)), 2); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node2_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(2)), 2); + add_or_update_node(&net_graph_msg_handler, &secp_ctx, node8_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags!(8)), 2); // Note that we don't test disabling node 3 and failing to route to it, as we (somewhat // naively) assume that the user checked the feature bits on the invoice, which override // the node_announcement. - { // Route to 1 via 2 and 3 because our channel to 1 is disabled - let route = router.get_route(&node1, None, &Vec::new(), 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 3); - - assert_eq!(route.paths[0][0].pubkey, node2); - assert_eq!(route.paths[0][0].short_channel_id, 2); - assert_eq!(route.paths[0][0].fee_msat, 200); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 4); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); - - assert_eq!(route.paths[0][2].pubkey, node1); - assert_eq!(route.paths[0][2].short_channel_id, 3); - assert_eq!(route.paths[0][2].fee_msat, 100); - assert_eq!(route.paths[0][2].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1)); - assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3)); - } - - { // If we specify a channel to node8, that overrides our local channel view and that gets used - let our_chans = vec![channelmanager::ChannelDetails { - channel_id: [0; 32], - short_channel_id: Some(42), - remote_network_id: node8.clone(), - counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), - channel_value_satoshis: 0, - user_id: 0, - outbound_capacity_msat: 0, - inbound_capacity_msat: 0, - is_live: true, - }]; - let route = router.get_route(&node3, Some(&our_chans), &Vec::new(), 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 2); - - assert_eq!(route.paths[0][0].pubkey, node8); - assert_eq!(route.paths[0][0].short_channel_id, 42); - assert_eq!(route.paths[0][0].fee_msat, 200); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 13); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); - } - + // Route to 1 via 2 and 3 because our channel to 1 is disabled + let route = get_route(&our_id, &net_graph_msg_handler, &node1, None, &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 3); + + assert_eq!(route.paths[0][0].pubkey, node2); + assert_eq!(route.paths[0][0].short_channel_id, 2); + assert_eq!(route.paths[0][0].fee_msat, 200); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 4); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 8) | 2); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); + + assert_eq!(route.paths[0][2].pubkey, node1); + assert_eq!(route.paths[0][2].short_channel_id, 3); + assert_eq!(route.paths[0][2].fee_msat, 100); + assert_eq!(route.paths[0][2].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(1)); + assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(3)); + + // If we specify a channel to node8, that overrides our local channel view and that gets used + let our_chans = vec![channelmanager::ChannelDetails { + channel_id: [0; 32], + short_channel_id: Some(42), + remote_network_id: node8.clone(), + counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), + channel_value_satoshis: 0, + user_id: 0, + outbound_capacity_msat: 0, + inbound_capacity_msat: 0, + is_live: true, + }]; + let route = get_route(&our_id, &net_graph_msg_handler, &node3, Some(&our_chans), &Vec::new(), 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 2); + + assert_eq!(route.paths[0][0].pubkey, node8); + assert_eq!(route.paths[0][0].short_channel_id, 42); + assert_eq!(route.paths[0][0].fee_msat, 200); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 13); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(13)); + + let zero_fees = RoutingFees { + base_msat: 0, + proportional_millionths: 0, + }; let mut last_hops = vec!(RouteHint { src_node_id: node4.clone(), short_channel_id: 8, - fee_base_msat: 0, - fee_proportional_millionths: 0, + fees: zero_fees, cltv_expiry_delta: (8 << 8) | 1, htlc_minimum_msat: 0, }, RouteHint { src_node_id: node5.clone(), short_channel_id: 9, - fee_base_msat: 1001, - fee_proportional_millionths: 0, + fees: RoutingFees { + base_msat: 1001, + proportional_millionths: 0, + }, cltv_expiry_delta: (9 << 8) | 1, htlc_minimum_msat: 0, }, RouteHint { src_node_id: node6.clone(), short_channel_id: 10, - fee_base_msat: 0, - fee_proportional_millionths: 0, + fees: zero_fees, cltv_expiry_delta: (10 << 8) | 1, htlc_minimum_msat: 0, }); - { // Simple test across 2, 3, 5, and 4 via a last_hop channel - let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 5); - - assert_eq!(route.paths[0][0].pubkey, node2); - assert_eq!(route.paths[0][0].short_channel_id, 2); - assert_eq!(route.paths[0][0].fee_msat, 100); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 4); - assert_eq!(route.paths[0][1].fee_msat, 0); - assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); - - assert_eq!(route.paths[0][2].pubkey, node5); - assert_eq!(route.paths[0][2].short_channel_id, 6); - assert_eq!(route.paths[0][2].fee_msat, 0); - assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1); - assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5)); - assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6)); - - assert_eq!(route.paths[0][3].pubkey, node4); - assert_eq!(route.paths[0][3].short_channel_id, 11); - assert_eq!(route.paths[0][3].fee_msat, 0); - assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1); - // If we have a peer in the node map, we'll use their features here since we don't have - // a way of figuring out their features from the invoice: - assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4)); - assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11)); - - assert_eq!(route.paths[0][4].pubkey, node7); - assert_eq!(route.paths[0][4].short_channel_id, 8); - assert_eq!(route.paths[0][4].fee_msat, 100); - assert_eq!(route.paths[0][4].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet - assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly - } - - { // Simple test with outbound channel to 4 to test that last_hops and first_hops connect - let our_chans = vec![channelmanager::ChannelDetails { - channel_id: [0; 32], - short_channel_id: Some(42), - remote_network_id: node4.clone(), - counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), - channel_value_satoshis: 0, - user_id: 0, - outbound_capacity_msat: 0, - inbound_capacity_msat: 0, - is_live: true, - }]; - let route = router.get_route(&node7, Some(&our_chans), &last_hops, 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 2); - - assert_eq!(route.paths[0][0].pubkey, node4); - assert_eq!(route.paths[0][0].short_channel_id, 42); - assert_eq!(route.paths[0][0].fee_msat, 0); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion - - assert_eq!(route.paths[0][1].pubkey, node7); - assert_eq!(route.paths[0][1].short_channel_id, 8); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet - assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly - } - - last_hops[0].fee_base_msat = 1000; - - { // Revert to via 6 as the fee on 8 goes up - let route = router.get_route(&node7, None, &last_hops, 100, 42).unwrap(); - assert_eq!(route.paths[0].len(), 4); - - assert_eq!(route.paths[0][0].pubkey, node2); - assert_eq!(route.paths[0][0].short_channel_id, 2); - assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node - assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 4); - assert_eq!(route.paths[0][1].fee_msat, 100); - assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); - - assert_eq!(route.paths[0][2].pubkey, node6); - assert_eq!(route.paths[0][2].short_channel_id, 7); - assert_eq!(route.paths[0][2].fee_msat, 0); - assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1); - // If we have a peer in the node map, we'll use their features here since we don't have - // a way of figuring out their features from the invoice: - assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6)); - assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7)); - - assert_eq!(route.paths[0][3].pubkey, node7); - assert_eq!(route.paths[0][3].short_channel_id, 10); - assert_eq!(route.paths[0][3].fee_msat, 100); - assert_eq!(route.paths[0][3].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet - assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly - } - - { // ...but still use 8 for larger payments as 6 has a variable feerate - let route = router.get_route(&node7, None, &last_hops, 2000, 42).unwrap(); - assert_eq!(route.paths[0].len(), 5); - - assert_eq!(route.paths[0][0].pubkey, node2); - assert_eq!(route.paths[0][0].short_channel_id, 2); - assert_eq!(route.paths[0][0].fee_msat, 3000); - assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); - assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); - assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); - - assert_eq!(route.paths[0][1].pubkey, node3); - assert_eq!(route.paths[0][1].short_channel_id, 4); - assert_eq!(route.paths[0][1].fee_msat, 0); - assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1); - assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); - assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); - - assert_eq!(route.paths[0][2].pubkey, node5); - assert_eq!(route.paths[0][2].short_channel_id, 6); - assert_eq!(route.paths[0][2].fee_msat, 0); - assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1); - assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5)); - assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6)); - - assert_eq!(route.paths[0][3].pubkey, node4); - assert_eq!(route.paths[0][3].short_channel_id, 11); - assert_eq!(route.paths[0][3].fee_msat, 1000); - assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1); - // If we have a peer in the node map, we'll use their features here since we don't have - // a way of figuring out their features from the invoice: - assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4)); - assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11)); - - assert_eq!(route.paths[0][4].pubkey, node7); - assert_eq!(route.paths[0][4].short_channel_id, 8); - assert_eq!(route.paths[0][4].fee_msat, 2000); - assert_eq!(route.paths[0][4].cltv_expiry_delta, 42); - assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet - assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly - } - - { // Test Router serialization/deserialization - let mut w = TestVecWriter(Vec::new()); - let network = router.network_map.read().unwrap(); - assert!(!network.channels.is_empty()); - assert!(!network.nodes.is_empty()); - network.write(&mut w).unwrap(); - assert!(::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network); - } - } - - #[test] - fn request_full_sync_finite_times() { - let (secp_ctx, _, router) = create_router(); - let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()); - - assert!(router.should_request_full_sync(&node_id)); - assert!(router.should_request_full_sync(&node_id)); - assert!(router.should_request_full_sync(&node_id)); - assert!(router.should_request_full_sync(&node_id)); - assert!(router.should_request_full_sync(&node_id)); - assert!(!router.should_request_full_sync(&node_id)); - } - - #[test] - fn handling_node_announcements() { - let (secp_ctx, _, router) = create_router(); - - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - let zero_hash = Sha256dHash::hash(&[0; 32]); - let first_announcement_time = 500; - - let mut unsigned_announcement = UnsignedNodeAnnouncement { - features: NodeFeatures::known(), - timestamp: first_announcement_time, - node_id: node_id_1, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - excess_address_data: Vec::new(), - excess_data: Vec::new(), - }; - let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_announcement.clone() - }; - - match router.handle_node_announcement(&valid_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!("No existing channels for node_announcement", e.err) - }; - - { - // Announce a channel to add a corresponding node. - let unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::known(), - chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), - short_channel_id: 0, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(res), - _ => panic!() - }; - } - - match router.handle_node_announcement(&valid_announcement) { - Ok(res) => assert!(res), - Err(_) => panic!() - }; - - let fake_msghash = hash_to_message!(&zero_hash); - match router.handle_node_announcement( - &NodeAnnouncement { - signature: secp_ctx.sign(&fake_msghash, node_1_privkey), - contents: unsigned_announcement.clone() - }) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Invalid signature from remote node") - }; - - unsigned_announcement.timestamp += 1000; - unsigned_announcement.excess_data.push(1); - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let announcement_with_data = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_announcement.clone() - }; - // Return false because contains excess data. - match router.handle_node_announcement(&announcement_with_data) { - Ok(res) => assert!(!res), - Err(_) => panic!() - }; - unsigned_announcement.excess_data = Vec::new(); - - // Even though previous announcement was not relayed further, we still accepted it, - // so we now won't accept announcements before the previous one. - unsigned_announcement.timestamp -= 10; - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let outdated_announcement = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_announcement.clone() - }; - match router.handle_node_announcement(&outdated_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Update older than last processed update") - }; - } - - #[test] - fn handling_channel_announcements() { - let secp_ctx = Secp256k1::new(); - let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice( - &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()); - let logger: Arc = Arc::new(test_utils::TestLogger::new()); - let chain_monitor = Arc::new(test_utils::TestChainWatcher::new()); - let router = Router::new(our_id, chain_monitor.clone(), Arc::clone(&logger)); - - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - - let good_script = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2) - .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_1_btckey).serialize()) - .push_slice(&PublicKey::from_secret_key(&secp_ctx, node_2_btckey).serialize()) - .push_opcode(opcodes::all::OP_PUSHNUM_2) - .push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh(); - - - let mut unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::known(), - chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), - short_channel_id: 0, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let channel_key = unsigned_announcement.short_channel_id; - - let mut msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - - // Test if the UTXO lookups were not supported - *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::NotSupported); - - match router.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(res), - _ => panic!() - }; - { - let network = router.network_map.write().unwrap(); - match network.channels.get(&channel_key) { - None => panic!(), - Some(_) => () - } - } - - // If we receive announcement for the same channel (with UTXO lookups disabled), - // drop new one on the floor, since we can't see any changes. - match router.handle_channel_announcement(&valid_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Already have knowledge of channel") - }; - - - // Test if an associated transaction were not on-chain (or not confirmed). - *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx); - unsigned_announcement.short_channel_id += 1; - - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - - match router.handle_channel_announcement(&valid_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry") - }; - - - // Now test if the transaction is found in the UTXO set and the script is correct. - unsigned_announcement.short_channel_id += 1; - *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script.clone(), 0)); - let channel_key = unsigned_announcement.short_channel_id; - - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(res), - _ => panic!() - }; - { - let network = router.network_map.write().unwrap(); - match network.channels.get(&channel_key) { - None => panic!(), - Some(_) => () - } - } - - // If we receive announcement for the same channel (but TX is not confirmed), - // drop new one on the floor, since we can't see any changes. - *chain_monitor.utxo_ret.lock().unwrap() = Err(chaininterface::ChainError::UnknownTx); - match router.handle_channel_announcement(&valid_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry") - }; - - // But if it is confirmed, replace the channel - *chain_monitor.utxo_ret.lock().unwrap() = Ok((good_script, 0)); - unsigned_announcement.features = ChannelFeatures::empty(); - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(res), - _ => panic!() - }; - { - let mut network = router.network_map.write().unwrap(); - match network.channels.entry(channel_key) { - BtreeEntry::Occupied(channel_entry) => { - assert_eq!(channel_entry.get().features, ChannelFeatures::empty()); - }, - _ => panic!() - } - } - - // Don't relay valid channels with excess data - unsigned_announcement.short_channel_id += 1; - unsigned_announcement.excess_data.push(1); - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(!res), - _ => panic!() - }; - - unsigned_announcement.excess_data = Vec::new(); - let invalid_sig_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_1_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&invalid_sig_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Invalid signature from remote node") - }; - - unsigned_announcement.node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let channel_to_itself_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_1_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&channel_to_itself_announcement) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself") - }; - } - - #[test] - fn handling_channel_update() { - let (secp_ctx, _, router) = create_router(); - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - - let zero_hash = Sha256dHash::hash(&[0; 32]); - let short_channel_id = 0; - let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); - let channel_key = short_channel_id; - - - { - // Announce a channel we will update - let unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::empty(), - chain_hash, - short_channel_id, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_channel_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_channel_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - - } - - let mut unsigned_channel_update = UnsignedChannelUpdate { - chain_hash, - short_channel_id, - timestamp: 100, - flags: 0, - cltv_expiry_delta: 144, - htlc_minimum_msat: 1000000, - fee_base_msat: 10000, - fee_proportional_millionths: 20, - excess_data: Vec::new() - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - - match router.handle_channel_update(&valid_channel_update) { - Ok(res) => assert!(res), - _ => panic!() - }; - - { - let network = router.network_map.write().unwrap(); - match network.channels.get(&channel_key) { - None => panic!(), - Some(channel_info) => { - assert_eq!(channel_info.one_to_two.cltv_expiry_delta, 144); - assert_eq!(channel_info.two_to_one.cltv_expiry_delta, u16::max_value()); - } - } - } - - unsigned_channel_update.timestamp += 100; - unsigned_channel_update.excess_data.push(1); - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - // Return false because contains excess data - match router.handle_channel_update(&valid_channel_update) { - Ok(res) => assert!(!res), - _ => panic!() - }; - - unsigned_channel_update.short_channel_id += 1; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - - match router.handle_channel_update(&valid_channel_update) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Couldn't find channel for update") - }; - unsigned_channel_update.short_channel_id = short_channel_id; - - - // Even though previous update was not relayed further, we still accepted it, - // so we now won't accept update before the previous one. - unsigned_channel_update.timestamp -= 10; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - - match router.handle_channel_update(&valid_channel_update) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Update older than last processed update") - }; - unsigned_channel_update.timestamp += 500; - - let fake_msghash = hash_to_message!(&zero_hash); - let invalid_sig_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&fake_msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - - match router.handle_channel_update(&invalid_sig_channel_update) { - Ok(_) => panic!(), - Err(e) => assert_eq!(e.err, "Invalid signature from remote node") - }; - - } - - #[test] - fn handling_htlc_fail_channel_update() { - let (secp_ctx, our_id, router) = create_router(); - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - - let short_channel_id = 0; - let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); - let channel_key = short_channel_id; - - { - // There is only local node in the table at the beginning. - let network = router.network_map.read().unwrap(); - assert_eq!(network.nodes.len(), 1); - assert_eq!(network.nodes.contains_key(&our_id), true); - } - - { - // Announce a channel we will update - let unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::empty(), - chain_hash, - short_channel_id, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_channel_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_channel_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - - } - - let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed { - short_channel_id, - is_permanent: false - }; - - router.handle_htlc_fail_channel_update(&channel_close_msg); - - { - // Non-permanent closing just disables a channel - let network = router.network_map.write().unwrap(); - match network.channels.get(&channel_key) { - None => panic!(), - Some(channel_info) => { - assert!(!channel_info.one_to_two.enabled); - assert!(!channel_info.two_to_one.enabled); - } - } - } - - let channel_close_msg = HTLCFailChannelUpdate::ChannelClosed { - short_channel_id, - is_permanent: true - }; - - router.handle_htlc_fail_channel_update(&channel_close_msg); - - { - // Permanent closing deletes a channel - let network = router.network_map.read().unwrap(); - assert_eq!(network.channels.len(), 0); - // Nodes are also deleted because there are no associated channels anymore - // Only the local node remains in the table. - assert_eq!(network.nodes.len(), 1); - assert_eq!(network.nodes.contains_key(&our_id), true); - } - - // TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet. - } - - #[test] - fn getting_next_channel_announcements() { - let (secp_ctx, _, router) = create_router(); - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - - let short_channel_id = 1; - let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); - let channel_key = short_channel_id; - - // Channels were not announced yet. - let channels_with_announcements = router.get_next_channel_announcements(0, 1); - assert_eq!(channels_with_announcements.len(), 0); - - { - // Announce a channel we will update - let unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::empty(), - chain_hash, - short_channel_id, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_channel_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_channel_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - // Contains initial channel announcement now. - let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1); - assert_eq!(channels_with_announcements.len(), 1); - if let Some(channel_announcements) = channels_with_announcements.first() { - let &(_, ref update_1, ref update_2) = channel_announcements; - assert_eq!(update_1, &None); - assert_eq!(update_2, &None); - } else { - panic!(); - } - - - { - // Valid channel update - let unsigned_channel_update = UnsignedChannelUpdate { - chain_hash, - short_channel_id, - timestamp: 101, - flags: 0, - cltv_expiry_delta: 144, - htlc_minimum_msat: 1000000, - fee_base_msat: 10000, - fee_proportional_millionths: 20, - excess_data: Vec::new() - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - match router.handle_channel_update(&valid_channel_update) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - // Now contains an initial announcement and an update. - let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1); - assert_eq!(channels_with_announcements.len(), 1); - if let Some(channel_announcements) = channels_with_announcements.first() { - let &(_, ref update_1, ref update_2) = channel_announcements; - assert_ne!(update_1, &None); - assert_eq!(update_2, &None); - } else { - panic!(); - } - - - { - // Channel update with excess data. - let unsigned_channel_update = UnsignedChannelUpdate { - chain_hash, - short_channel_id, - timestamp: 102, - flags: 0, - cltv_expiry_delta: 144, - htlc_minimum_msat: 1000000, - fee_base_msat: 10000, - fee_proportional_millionths: 20, - excess_data: [1; 3].to_vec() - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_channel_update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_channel_update.clone() - }; - match router.handle_channel_update(&valid_channel_update) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - // Test that announcements with excess data won't be returned - let channels_with_announcements = router.get_next_channel_announcements(channel_key, 1); - assert_eq!(channels_with_announcements.len(), 1); - if let Some(channel_announcements) = channels_with_announcements.first() { - let &(_, ref update_1, ref update_2) = channel_announcements; - assert_eq!(update_1, &None); - assert_eq!(update_2, &None); - } else { - panic!(); - } - - // Further starting point have no channels after it - let channels_with_announcements = router.get_next_channel_announcements(channel_key + 1000, 1); - assert_eq!(channels_with_announcements.len(), 0); - } - - #[test] - fn getting_next_node_announcements() { - let (secp_ctx, _, router) = create_router(); - let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); - let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - let node_1_btckey = &SecretKey::from_slice(&[40; 32]).unwrap(); - let node_2_btckey = &SecretKey::from_slice(&[39; 32]).unwrap(); - - let short_channel_id = 1; - let chain_hash = genesis_block(Network::Testnet).header.bitcoin_hash(); - - // No nodes yet. - let next_announcements = router.get_next_node_announcements(None, 10); - assert_eq!(next_announcements.len(), 0); - - { - // Announce a channel to add 2 nodes - let unsigned_announcement = UnsignedChannelAnnouncement { - features: ChannelFeatures::empty(), - chain_hash, - short_channel_id, - node_id_1, - node_id_2, - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, node_2_btckey), - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_channel_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign(&msghash, node_1_btckey), - bitcoin_signature_2: secp_ctx.sign(&msghash, node_2_btckey), - contents: unsigned_announcement.clone(), - }; - match router.handle_channel_announcement(&valid_channel_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - - // Nodes were never announced - let next_announcements = router.get_next_node_announcements(None, 3); - assert_eq!(next_announcements.len(), 0); - - { - let mut unsigned_announcement = UnsignedNodeAnnouncement { - features: NodeFeatures::known(), - timestamp: 1000, - node_id: node_id_1, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - excess_address_data: Vec::new(), - excess_data: Vec::new(), - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_1_privkey), - contents: unsigned_announcement.clone() - }; - match router.handle_node_announcement(&valid_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - - unsigned_announcement.node_id = node_id_2; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_2_privkey), - contents: unsigned_announcement.clone() - }; - - match router.handle_node_announcement(&valid_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - let next_announcements = router.get_next_node_announcements(None, 3); - assert_eq!(next_announcements.len(), 2); - - // Skip the first node. - let next_announcements = router.get_next_node_announcements(Some(&node_id_1), 2); - assert_eq!(next_announcements.len(), 1); - - { - // Later announcement which should not be relayed (excess data) prevent us from sharing a node - let unsigned_announcement = UnsignedNodeAnnouncement { - features: NodeFeatures::known(), - timestamp: 1010, - node_id: node_id_2, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - excess_address_data: Vec::new(), - excess_data: [1; 3].to_vec(), - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = NodeAnnouncement { - signature: secp_ctx.sign(&msghash, node_2_privkey), - contents: unsigned_announcement.clone() - }; - match router.handle_node_announcement(&valid_announcement) { - Ok(res) => assert!(!res), - Err(_) => panic!() - }; - } - - let next_announcements = router.get_next_node_announcements(Some(&node_id_1), 2); - assert_eq!(next_announcements.len(), 0); - + // Simple test across 2, 3, 5, and 4 via a last_hop channel + let route = get_route(&our_id, &net_graph_msg_handler, &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 5); + + assert_eq!(route.paths[0][0].pubkey, node2); + assert_eq!(route.paths[0][0].short_channel_id, 2); + assert_eq!(route.paths[0][0].fee_msat, 100); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 4); + assert_eq!(route.paths[0][1].fee_msat, 0); + assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); + + assert_eq!(route.paths[0][2].pubkey, node5); + assert_eq!(route.paths[0][2].short_channel_id, 6); + assert_eq!(route.paths[0][2].fee_msat, 0); + assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1); + assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5)); + assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6)); + + assert_eq!(route.paths[0][3].pubkey, node4); + assert_eq!(route.paths[0][3].short_channel_id, 11); + assert_eq!(route.paths[0][3].fee_msat, 0); + assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1); + // If we have a peer in the node map, we'll use their features here since we don't have + // a way of figuring out their features from the invoice: + assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4)); + assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11)); + + assert_eq!(route.paths[0][4].pubkey, node7); + assert_eq!(route.paths[0][4].short_channel_id, 8); + assert_eq!(route.paths[0][4].fee_msat, 100); + assert_eq!(route.paths[0][4].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet + assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly + + // Simple test with outbound channel to 4 to test that last_hops and first_hops connect + let our_chans = vec![channelmanager::ChannelDetails { + channel_id: [0; 32], + short_channel_id: Some(42), + remote_network_id: node4.clone(), + counterparty_features: InitFeatures::from_le_bytes(vec![0b11]), + channel_value_satoshis: 0, + user_id: 0, + outbound_capacity_msat: 0, + inbound_capacity_msat: 0, + is_live: true, + }]; + let route = get_route(&our_id, &net_graph_msg_handler, &node7, Some(&our_chans), &last_hops, 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 2); + + assert_eq!(route.paths[0][0].pubkey, node4); + assert_eq!(route.paths[0][0].short_channel_id, 42); + assert_eq!(route.paths[0][0].fee_msat, 0); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::new()); // No feature flags will meet the relevant-to-channel conversion + + assert_eq!(route.paths[0][1].pubkey, node7); + assert_eq!(route.paths[0][1].short_channel_id, 8); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][1].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet + assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly + + last_hops[0].fees.base_msat = 1000; + + // Revert to via 6 as the fee on 8 goes up + let route = get_route(&our_id, &net_graph_msg_handler, &node7, None, &last_hops, 100, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 4); + + assert_eq!(route.paths[0][0].pubkey, node2); + assert_eq!(route.paths[0][0].short_channel_id, 2); + assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node + assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 4); + assert_eq!(route.paths[0][1].fee_msat, 100); + assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 8) | 1); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); + + assert_eq!(route.paths[0][2].pubkey, node6); + assert_eq!(route.paths[0][2].short_channel_id, 7); + assert_eq!(route.paths[0][2].fee_msat, 0); + assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 8) | 1); + // If we have a peer in the node map, we'll use their features here since we don't have + // a way of figuring out their features from the invoice: + assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(6)); + assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(7)); + + assert_eq!(route.paths[0][3].pubkey, node7); + assert_eq!(route.paths[0][3].short_channel_id, 10); + assert_eq!(route.paths[0][3].fee_msat, 100); + assert_eq!(route.paths[0][3].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][3].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet + assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly + + // ...but still use 8 for larger payments as 6 has a variable feerate + let route = get_route(&our_id, &net_graph_msg_handler, &node7, None, &last_hops, 2000, 42, Arc::clone(&logger)).unwrap(); + assert_eq!(route.paths[0].len(), 5); + + assert_eq!(route.paths[0][0].pubkey, node2); + assert_eq!(route.paths[0][0].short_channel_id, 2); + assert_eq!(route.paths[0][0].fee_msat, 3000); + assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 8) | 1); + assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags!(2)); + assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags!(2)); + + assert_eq!(route.paths[0][1].pubkey, node3); + assert_eq!(route.paths[0][1].short_channel_id, 4); + assert_eq!(route.paths[0][1].fee_msat, 0); + assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 8) | 1); + assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags!(3)); + assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags!(4)); + + assert_eq!(route.paths[0][2].pubkey, node5); + assert_eq!(route.paths[0][2].short_channel_id, 6); + assert_eq!(route.paths[0][2].fee_msat, 0); + assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 8) | 1); + assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags!(5)); + assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags!(6)); + + assert_eq!(route.paths[0][3].pubkey, node4); + assert_eq!(route.paths[0][3].short_channel_id, 11); + assert_eq!(route.paths[0][3].fee_msat, 1000); + assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 8) | 1); + // If we have a peer in the node map, we'll use their features here since we don't have + // a way of figuring out their features from the invoice: + assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags!(4)); + assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags!(11)); + + assert_eq!(route.paths[0][4].pubkey, node7); + assert_eq!(route.paths[0][4].short_channel_id, 8); + assert_eq!(route.paths[0][4].fee_msat, 2000); + assert_eq!(route.paths[0][4].cltv_expiry_delta, 42); + assert_eq!(route.paths[0][4].node_features.le_flags(), &Vec::new()); // We dont pass flags in from invoices yet + assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::new()); // We can't learn any flags from invoices, sadly } }