X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Frouter.rs;h=bc9c868add32b948052df9b9cc9a4e6a6213b590;hb=7c24fea4fe8af6ae1b51c5ef6e792113d7830abb;hp=f30eb7912792df24f1dd6bcedae86cadb2e8f69a;hpb=07f08409f8908b5f1e0ddda6126433156ab3abbb;p=rust-lightning diff --git a/src/ln/router.rs b/src/ln/router.rs index f30eb791..bc9c868a 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -6,9 +6,10 @@ use bitcoin::util::hash::Sha256dHash; use ln::channelmanager; use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures}; use ln::msgs; +use util::logger::Logger; use std::cmp; -use std::sync::RwLock; +use std::sync::{RwLock,Arc}; use std::collections::{HashMap,BinaryHeap}; use std::collections::hash_map::Entry; @@ -94,7 +95,7 @@ impl NetworkMap { pub struct RouteHint { pub src_node_id: PublicKey, pub short_channel_id: u64, - pub fee_base_msat: u64, + pub fee_base_msat: u32, pub fee_proportional_millionths: u32, pub cltv_expiry_delta: u16, pub htlc_minimum_msat: u64, @@ -105,6 +106,7 @@ pub struct RouteHint { pub struct Router { secp_ctx: Secp256k1, network_map: RwLock, + logger: Arc, } macro_rules! secp_verify_sig { @@ -301,6 +303,7 @@ impl RoutingMessageHandler for Router { struct RouteGraphNode { pubkey: PublicKey, lowest_fee_to_peer_through_node: u64, + lowest_fee_to_node: u64, } impl cmp::Ord for RouteGraphNode { @@ -325,7 +328,7 @@ struct DummyDirectionalChannelInfo { } impl Router { - pub fn new(our_pubkey: PublicKey) -> Router { + pub fn new(our_pubkey: PublicKey, logger: Arc) -> Router { let mut nodes = HashMap::new(); nodes.insert(our_pubkey.clone(), NodeInfo { channels: Vec::new(), @@ -344,6 +347,7 @@ impl Router { our_node_id: our_pubkey, nodes: nodes, }), + logger, } } @@ -383,6 +387,10 @@ impl Router { return Err(HandleError{err: "Cannot generate a route to ourselves", action: None}); } + if final_value_msat > 21_000_000 * 1_0000_0000 * 1000 { + return Err(HandleError{err: "Cannot generate a route of more value than all existing satoshis", action: None}); + } + // 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 @@ -429,17 +437,18 @@ impl Router { //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(proportional_fee) = proportional_fee_millions { - let new_fee = $directional_info.fee_base_msat as u64 + proportional_fee / 1000000; + 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 mut hm_entry = dist.entry(&$directional_info.src_node_id); + 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 as u64, - node.lowest_inbound_channel_fee_proportional_millionths as u64, + node.lowest_inbound_channel_fee_base_msat, + node.lowest_inbound_channel_fee_proportional_millionths, RouteHop { - pubkey: PublicKey::new(), + pubkey: $dest_node_id.clone(), short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0, @@ -449,11 +458,17 @@ impl Router { // Ignore new_fee for channel-from-us as we assume all channels-from-us // will have the same effective-fee total_fee += new_fee; - total_fee += old_entry.2 * (final_value_msat + total_fee) / 1000000 + old_entry.1; + 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); @@ -518,11 +533,14 @@ impl Router { } } - while let Some(RouteGraphNode { pubkey, lowest_fee_to_peer_through_node }) = targets.pop() { + 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); while res.last().unwrap().pubkey != *target { - let new_entry = dist.remove(&res.last().unwrap().pubkey).unwrap().3; + let new_entry = match dist.remove(&res.last().unwrap().pubkey) { + Some(hop) => hop.3, + None => return Err(HandleError{err: "Failed to find a non-fee-overflowing path to the given destination", action: None}), + }; 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); @@ -537,9 +555,7 @@ impl Router { match network.nodes.get(&pubkey) { None => {}, Some(node) => { - let mut fee = lowest_fee_to_peer_through_node - node.lowest_inbound_channel_fee_base_msat as u64; - fee -= node.lowest_inbound_channel_fee_proportional_millionths as u64 * (fee + final_value_msat) / 1000000; - add_entries_to_cheapest_to_target_node!(node, &pubkey, fee); + add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node); }, } } @@ -553,18 +569,24 @@ mod tests { use ln::channelmanager; use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint}; use ln::msgs::GlobalFeatures; + use util::test_utils; + use util::logger::Logger; - use bitcoin::util::misc::hex_bytes; use bitcoin::util::hash::Sha256dHash; + use hex; + use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::Secp256k1; + use std::sync::Arc; + #[test] fn route_test() { let secp_ctx = Secp256k1::new(); - let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap(); - let router = Router::new(our_id); + let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap(); + let logger: Arc = Arc::new(test_utils::TestLogger::new()); + let router = Router::new(our_id, Arc::clone(&logger)); // Build network from our_id to node8: // @@ -623,14 +645,14 @@ 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(&secp_ctx, &hex_bytes("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()).unwrap(); - let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()).unwrap(); - let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()).unwrap(); - let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()).unwrap(); - let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()).unwrap(); - let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()).unwrap(); - let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()).unwrap(); - let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap()).unwrap(); + let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()).unwrap(); + let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()).unwrap(); + let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()).unwrap(); + let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()).unwrap(); + let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()).unwrap(); + let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()).unwrap(); + let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()).unwrap(); + let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap()).unwrap(); let zero_hash = Sha256dHash::from_data(&[0; 32]);