X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fln%2Frouter.rs;h=1ca55fa260004c25e2cfafbe3856f1923257220a;hb=refs%2Fheads%2F2018-08-secp-0.10;hp=ad7ae675117f935561979b1ff97277dde314b48f;hpb=f98d4b37d3f291597bc869c3730f62ba274ac474;p=rust-lightning diff --git a/src/ln/router.rs b/src/ln/router.rs index ad7ae675..1ca55fa2 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -1,16 +1,19 @@ use secp256k1::key::PublicKey; use secp256k1::{Secp256k1,Message}; +use secp256k1; 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; +use std; /// A hop in a route #[derive(Clone)] @@ -44,12 +47,26 @@ struct DirectionalChannelInfo { fee_proportional_millionths: u32, } +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(()) + } +} + struct ChannelInfo { features: GlobalFeatures, one_to_two: DirectionalChannelInfo, two_to_one: DirectionalChannelInfo, } +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(()) + } +} + struct NodeInfo { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: Vec<(u64, Sha256dHash)>, @@ -66,6 +83,13 @@ struct NodeInfo { addresses: Vec, } +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(()) + } +} + struct NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] channels: HashMap<(u64, Sha256dHash), ChannelInfo>, @@ -76,6 +100,20 @@ struct NetworkMap { nodes: HashMap, } +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(()) + } +} + impl NetworkMap { #[cfg(feature = "non_bitcoin_chain_hash_routing")] #[inline] @@ -88,13 +126,25 @@ impl NetworkMap { fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 { short_channel_id } + + #[cfg(feature = "non_bitcoin_chain_hash_routing")] + #[inline] + fn get_short_id(id: &(u64, Sha256dHash)) -> &u64 { + &id.0 + } + + #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))] + #[inline] + fn get_short_id(id: &u64) -> &u64 { + id + } } /// A channel descriptor which provides a last-hop route to get_route 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, @@ -103,8 +153,9 @@ pub struct RouteHint { /// Tracks a view of the network, receiving updates from peers and generating Routes to /// payment destinations. pub struct Router { - secp_ctx: Secp256k1, + secp_ctx: Secp256k1, network_map: RwLock, + logger: Arc, } macro_rules! secp_verify_sig { @@ -222,7 +273,14 @@ impl RoutingMessageHandler for Router { }, &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id } => { let mut network = self.network_map.write().unwrap(); - network.channels.remove(short_channel_id); + if let Some(chan) = network.channels.remove(short_channel_id) { + network.nodes.get_mut(&chan.one_to_two.src_node_id).unwrap().channels.retain(|chan_id| { + chan_id != NetworkMap::get_short_id(chan_id) + }); + network.nodes.get_mut(&chan.two_to_one.src_node_id).unwrap().channels.retain(|chan_id| { + chan_id != NetworkMap::get_short_id(chan_id) + }); + } }, } } @@ -301,6 +359,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 +384,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(), @@ -338,15 +397,22 @@ impl Router { addresses: Vec::new(), }); Router { - secp_ctx: Secp256k1::new(), + secp_ctx: Secp256k1::verification_only(), network_map: RwLock::new(NetworkMap { channels: HashMap::new(), our_node_id: our_pubkey, nodes: nodes, }), + 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(); @@ -383,6 +449,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 @@ -428,39 +498,49 @@ impl Router { ( $chan_id: expr, $dest_node_id: expr, $directional_info: 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 new_fee = $directional_info.fee_base_msat as u64 + ($starting_fee_msat + final_value_msat) * ($directional_info.fee_proportional_millionths as u64) / 1000000; - let mut total_fee = $starting_fee_msat as u64; - let mut 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, - RouteHop { - pubkey: PublicKey::new(), - short_channel_id: 0, - 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; - total_fee += old_entry.2 * (final_value_msat + total_fee) / 1000000 + old_entry.1; - } - let new_graph_node = RouteGraphNode { - pubkey: $directional_info.src_node_id, - lowest_fee_to_peer_through_node: total_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(), - short_channel_id: $chan_id.clone(), - fee_msat: new_fee, // This field is ignored on the last-hop anyway - cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32, + 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(), + short_channel_id: 0, + 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, + }; + 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(), + short_channel_id: $chan_id.clone(), + fee_msat: new_fee, // This field is ignored on the last-hop anyway + cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32, + } } } } @@ -515,28 +595,29 @@ 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); } res.last_mut().unwrap().fee_msat = final_value_msat; res.last_mut().unwrap().cltv_expiry_delta = final_cltv; - return Ok(Route { - hops: res - }); + let route = Route { hops: res }; + log_trace!(self, "Got route: {}", log_route!(route)); + return Ok(route); } 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); }, } } @@ -550,18 +631,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()); + 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: // @@ -620,14 +707,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()); + let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()); + let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()); + let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()); + let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()); + let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()); + let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()); + let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap()); let zero_hash = Sha256dHash::from_data(&[0; 32]);