- }
-
- let our_node_id = NodeId::from_pubkey(our_node_pubkey);
- let mut hop_ids = [None; MAX_PATH_LENGTH_ESTIMATE as usize];
- for i in 0..hops.len() {
- hop_ids[i] = Some(NodeId::from_pubkey(&hops[i]));
- }
-
- let scorer = HopScorer { our_node_id, hop_ids };
-
- get_route(our_node_pubkey, payment_params, network_graph, None, final_value_msat,
- final_cltv_expiry_delta, logger, &scorer, random_seed_bytes)
-}
-
-#[cfg(test)]
-mod tests {
- use routing::network_graph::{NetworkGraph, NetGraphMsgHandler, NodeId};
- use routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
- PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
- DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
- use routing::scoring::{ChannelUsage, Score};
- use chain::transaction::OutPoint;
- use chain::keysinterface::KeysInterface;
- use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
- use ln::msgs::{ErrorAction, LightningError, OptionalField, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
- NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate};
- use ln::channelmanager;
- use util::test_utils;
- use util::chacha20::ChaCha20;
- use util::ser::Writeable;
- #[cfg(c_bindings)]
- use util::ser::Writer;
-
- 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::blockdata::transaction::TxOut;
-
- use hex;
-
- use bitcoin::secp256k1::{PublicKey,SecretKey};
- use bitcoin::secp256k1::{Secp256k1, All};
-
- use prelude::*;
- use sync::{self, Arc};
-
- fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
- features: InitFeatures, outbound_capacity_msat: u64) -> channelmanager::ChannelDetails {
- channelmanager::ChannelDetails {
- channel_id: [0; 32],
- counterparty: channelmanager::ChannelCounterparty {
- features,
- node_id,
- unspendable_punishment_reserve: 0,
- forwarding_info: None,
- outbound_htlc_minimum_msat: None,
- outbound_htlc_maximum_msat: None,
- },
- funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
- channel_type: None,
- short_channel_id,
- outbound_scid_alias: None,
- inbound_scid_alias: None,
- channel_value_satoshis: 0,
- user_channel_id: 0,
- balance_msat: 0,
- outbound_capacity_msat,
- next_outbound_htlc_limit_msat: outbound_capacity_msat,
- inbound_capacity_msat: 42,
- unspendable_punishment_reserve: None,
- confirmations_required: None,
- force_close_spend_delay: None,
- is_outbound: true, is_funding_locked: true,
- is_usable: true, is_public: true,
- inbound_htlc_minimum_msat: None,
- inbound_htlc_maximum_msat: None,
- }
- }
-
- // Using the same keys for LN and BTC ids
- fn add_channel(
- net_graph_msg_handler: &NetGraphMsgHandler<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
- secp_ctx: &Secp256k1<All>, 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.block_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_ecdsa(&msghash, node_1_privkey),
- node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
- bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
- bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
- contents: unsigned_announcement.clone(),
- };
- match net_graph_msg_handler.handle_channel_announcement(&valid_announcement) {
- Ok(res) => assert!(res),
- _ => panic!()
- };
- }
-
- fn update_channel(
- net_graph_msg_handler: &NetGraphMsgHandler<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
- secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
- ) {
- let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
- let valid_channel_update = ChannelUpdate {
- signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
- contents: update.clone()
- };
-
- match net_graph_msg_handler.handle_channel_update(&valid_channel_update) {
- Ok(res) => assert!(res),
- Err(_) => panic!()
- };
- }
-
- fn add_or_update_node(
- net_graph_msg_handler: &NetGraphMsgHandler<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
- secp_ctx: &Secp256k1<All>, 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_ecdsa(&msghash, node_privkey),
- contents: unsigned_announcement.clone()
- };
-
- match net_graph_msg_handler.handle_node_announcement(&valid_announcement) {
- Ok(_) => (),
- Err(_) => panic!()
- };
- }
-
- fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
- let privkeys: Vec<SecretKey> = (2..22).map(|i| {
- SecretKey::from_slice(&hex::decode(format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap()
- }).collect();
-
- let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
-
- let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap();
- let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
-
- (our_privkey, our_id, privkeys, pubkeys)
- }
-
- fn id_to_feature_flags(id: u8) -> Vec<u8> {
- // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
- // test for it later.
- let idx = (id - 1) * 2 + 1;
- if idx > 8*3 {
- vec![1 << (idx - 8*3), 0, 0, 0]
- } else if idx > 8*2 {
- vec![1 << (idx - 8*2), 0, 0]
- } else if idx > 8*1 {
- vec![1 << (idx - 8*1), 0]
- } else {
- vec![1 << idx]
- }
- }
-
- fn build_line_graph() -> (
- Secp256k1<All>, sync::Arc<NetworkGraph>, NetGraphMsgHandler<sync::Arc<NetworkGraph>,
- sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>,
- sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>,
- ) {
- let secp_ctx = Secp256k1::new();
- let logger = Arc::new(test_utils::TestLogger::new());
- let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
- let net_graph_msg_handler = NetGraphMsgHandler::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
-
- // Build network from our_id to node 19:
- // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19
- let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
-
- for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey)
- .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() {
- let cur_short_channel_id = (idx as u64) + 1;
- add_channel(&net_graph_msg_handler, &secp_ctx, &cur_privkey, &next_privkey,
- ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id);
- update_channel(&net_graph_msg_handler, &secp_ctx, &cur_privkey, UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: cur_short_channel_id,
- timestamp: idx as u32,
- flags: 0,
- cltv_expiry_delta: 0,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
- update_channel(&net_graph_msg_handler, &secp_ctx, &next_privkey, UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: cur_short_channel_id,
- timestamp: (idx as u32)+1,
- flags: 1,
- cltv_expiry_delta: 0,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
- add_or_update_node(&net_graph_msg_handler, &secp_ctx, next_privkey,
- NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
- }
-
- (secp_ctx, network_graph, net_graph_msg_handler, chain_monitor, logger)
- }
-
- fn build_graph() -> (
- Secp256k1<All>,
- sync::Arc<NetworkGraph>,
- NetGraphMsgHandler<sync::Arc<NetworkGraph>, sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>,
- sync::Arc<test_utils::TestChainSource>,
- sync::Arc<test_utils::TestLogger>,
- ) {
- let secp_ctx = Secp256k1::new();
- let logger = Arc::new(test_utils::TestLogger::new());
- let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
- let net_graph_msg_handler = NetGraphMsgHandler::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
- // Build network from our_id to node6:
- //
- // -1(1)2- node0 -1(3)2-
- // / \
- // our_id -1(12)2- node7 -1(13)2--- node2
- // \ /
- // -1(2)2- node1 -1(4)2-
- //
- //
- // chan1 1-to-2: disabled
- // chan1 2-to-1: enabled, 0 fee
- //
- // chan2 1-to-2: enabled, ignored fee
- // chan2 2-to-1: enabled, 0 fee
- //
- // chan3 1-to-2: enabled, 0 fee
- // chan3 2-to-1: enabled, 100 msat fee
- //
- // chan4 1-to-2: enabled, 100% fee
- // chan4 2-to-1: enabled, 0 fee
- //
- // chan12 1-to-2: enabled, ignored fee
- // chan12 2-to-1: enabled, 0 fee
- //
- // chan13 1-to-2: enabled, 200% fee
- // chan13 2-to-1: enabled, 0 fee
- //
- //
- // -1(5)2- node3 -1(8)2--
- // | 2 |
- // | (11) |
- // / 1 \
- // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
- // \ /
- // -1(7)2- node5 -1(10)2-
- //
- // Channels 5, 8, 9 and 10 are private channels.
- //
- // chan5 1-to-2: enabled, 100 msat fee
- // chan5 2-to-1: enabled, 0 fee
- //
- // chan6 1-to-2: enabled, 0 fee
- // chan6 2-to-1: enabled, 0 fee
- //
- // chan7 1-to-2: enabled, 100% fee
- // chan7 2-to-1: enabled, 0 fee
- //
- // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
- // chan8 2-to-1: enabled, 0 fee
- //
- // chan9 1-to-2: enabled, 1001 msat fee
- // chan9 2-to-1: enabled, 0 fee
- //
- // chan10 1-to-2: enabled, 0 fee
- // chan10 2-to-1: enabled, 0 fee
- //
- // chan11 1-to-2: enabled, 0 fee
- // chan11 2-to-1: enabled, 0 fee
-
- let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
-
- add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
- update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: 1,
- timestamp: 1,
- flags: 1,
- cltv_expiry_delta: 0,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
-
- add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
-
- add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[1], 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.block_hash(),
- short_channel_id: 2,
- timestamp: 1,
- flags: 0,
- cltv_expiry_delta: (5 << 4) | 3,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: u32::max_value(),
- fee_proportional_millionths: u32::max_value(),
- excess_data: Vec::new()
- });
- update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: 2,
- timestamp: 1,
- flags: 1,
- cltv_expiry_delta: 0,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
-
- add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
-
- add_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, &privkeys[7], 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.block_hash(),
- short_channel_id: 12,
- timestamp: 1,
- flags: 0,
- cltv_expiry_delta: (5 << 4) | 3,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: u32::max_value(),
- fee_proportional_millionths: u32::max_value(),
- excess_data: Vec::new()
- });
- update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: 12,
- timestamp: 1,
- flags: 1,
- cltv_expiry_delta: 0,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
-
- add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
-
- add_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
- update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: 3,
- timestamp: 1,
- flags: 0,
- cltv_expiry_delta: (3 << 4) | 1,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 0,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });
- update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
- chain_hash: genesis_block(Network::Testnet).header.block_hash(),
- short_channel_id: 3,
- timestamp: 1,
- flags: 1,
- cltv_expiry_delta: (3 << 4) | 2,
- htlc_minimum_msat: 0,
- htlc_maximum_msat: OptionalField::Absent,
- fee_base_msat: 100,
- fee_proportional_millionths: 0,
- excess_data: Vec::new()
- });