1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1,Message};
4 use bitcoin::util::hash::Sha256dHash;
6 use ln::msgs::{HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
10 use std::sync::RwLock;
11 use std::collections::{HashMap,BinaryHeap};
12 use std::collections::hash_map::Entry;
16 pub pubkey: PublicKey,
17 /// The channel that should be used from the previous hop to reach this node.
18 pub short_channel_id: u64,
19 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
21 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
22 /// expected at the destination, NOT a delta.
23 pub cltv_expiry_delta: u32,
26 /// A route from us through the network to a destination
28 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
29 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
31 pub hops: Vec<RouteHop>,
34 struct DirectionalChannelInfo {
35 src_node_id: PublicKey,
38 cltv_expiry_delta: u16,
39 htlc_minimum_msat: u64,
41 fee_proportional_millionths: u32,
45 features: GlobalFeatures,
46 one_to_two: DirectionalChannelInfo,
47 two_to_one: DirectionalChannelInfo,
51 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
52 channels: Vec<(u64, Sha256dHash)>,
53 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
56 lowest_inbound_channel_fee_base_msat: u32,
57 lowest_inbound_channel_fee_proportional_millionths: u32,
59 features: GlobalFeatures,
63 addresses: Vec<NetAddress>,
67 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
68 channels: HashMap<(u64, Sha256dHash), ChannelInfo>,
69 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
70 channels: HashMap<u64, ChannelInfo>,
72 our_node_id: PublicKey,
73 nodes: HashMap<PublicKey, NodeInfo>,
77 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
79 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
80 (short_channel_id, chain_hash)
83 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
85 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
90 /// A channel descriptor which provides a last-hop route to get_route
91 pub struct RouteHint {
92 pub src_node_id: PublicKey,
93 pub short_channel_id: u64,
94 pub fee_base_msat: u64,
95 pub fee_proportional_millionths: u32,
96 pub cltv_expiry_delta: u16,
97 pub htlc_minimum_msat: u64,
100 /// Tracks a view of the network, receiving updates from peers and generating Routes to
101 /// payment destinations.
104 network_map: RwLock<NetworkMap>,
107 macro_rules! secp_verify_sig {
108 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
109 match $secp_ctx.verify($msg, $sig, $pubkey) {
111 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", msg: None}),
116 impl RoutingMessageHandler for Router {
117 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), HandleError> {
118 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
119 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
121 let mut network = self.network_map.write().unwrap();
122 match network.nodes.get_mut(&msg.contents.node_id) {
123 None => Err(HandleError{err: "No existing channels for node_announcement", msg: None}),
125 if node.last_update >= msg.contents.timestamp {
126 return Err(HandleError{err: "Update older than last processed update", msg: None});
129 node.features = msg.contents.features.clone();
130 node.last_update = msg.contents.timestamp;
131 node.rgb = msg.contents.rgb;
132 node.alias = msg.contents.alias;
133 node.addresses = msg.contents.addresses.clone();
139 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
140 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
141 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
142 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
143 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
144 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
146 //TODO: Call blockchain thing to ask if the short_channel_id is valid
147 //TODO: Only allow bitcoin chain_hash
149 if msg.contents.features.requires_unknown_bits() {
150 return Err(HandleError{err: "Channel announcement required unknown feature flags", msg: None});
153 let mut network = self.network_map.write().unwrap();
155 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
156 Entry::Occupied(_) => {
157 //TODO: because asking the blockchain if short_channel_id is valid is only optional
158 //in the blockchain API, we need to handle it smartly here, though its unclear
160 return Err(HandleError{err: "Already have knowledge of channel", msg: None})
162 Entry::Vacant(entry) => {
163 entry.insert(ChannelInfo {
164 features: msg.contents.features.clone(),
165 one_to_two: DirectionalChannelInfo {
166 src_node_id: msg.contents.node_id_1.clone(),
169 cltv_expiry_delta: u16::max_value(),
170 htlc_minimum_msat: u64::max_value(),
171 fee_base_msat: u32::max_value(),
172 fee_proportional_millionths: u32::max_value(),
174 two_to_one: DirectionalChannelInfo {
175 src_node_id: msg.contents.node_id_2.clone(),
178 cltv_expiry_delta: u16::max_value(),
179 htlc_minimum_msat: u64::max_value(),
180 fee_base_msat: u32::max_value(),
181 fee_proportional_millionths: u32::max_value(),
187 macro_rules! add_channel_to_node {
188 ( $node_id: expr ) => {
189 match network.nodes.entry($node_id) {
190 Entry::Occupied(node_entry) => {
191 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
193 Entry::Vacant(node_entry) => {
194 node_entry.insert(NodeInfo {
195 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
196 lowest_inbound_channel_fee_base_msat: u32::max_value(),
197 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
198 features: GlobalFeatures::new(),
202 addresses: Vec::new(),
209 add_channel_to_node!(msg.contents.node_id_1);
210 add_channel_to_node!(msg.contents.node_id_2);
212 Ok(!msg.contents.features.supports_unknown_bits())
215 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<(), HandleError> {
216 let mut network = self.network_map.write().unwrap();
218 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
219 let chan_was_enabled;
221 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
222 None => return Err(HandleError{err: "Couldn't find channel for update", msg: None}),
224 macro_rules! maybe_update_channel_info {
225 ( $target: expr) => {
226 if $target.last_update >= msg.contents.timestamp {
227 return Err(HandleError{err: "Update older than last processed update", msg: None});
229 chan_was_enabled = $target.enabled;
230 $target.last_update = msg.contents.timestamp;
231 $target.enabled = chan_enabled;
232 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
233 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
234 $target.fee_base_msat = msg.contents.fee_base_msat;
235 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
239 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
240 if msg.contents.flags & 1 == 1 {
241 dest_node_id = channel.one_to_two.src_node_id.clone();
242 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
243 maybe_update_channel_info!(channel.two_to_one);
245 dest_node_id = channel.two_to_one.src_node_id.clone();
246 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
247 maybe_update_channel_info!(channel.one_to_two);
253 let node = network.nodes.get_mut(&dest_node_id).unwrap();
254 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
255 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
256 } else if chan_was_enabled {
257 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
258 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
261 let node = network.nodes.get(&dest_node_id).unwrap();
263 for chan_id in node.channels.iter() {
264 let chan = network.channels.get(chan_id).unwrap();
265 if chan.one_to_two.src_node_id == dest_node_id {
266 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
267 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
269 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
270 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
275 //TODO: satisfy the borrow-checker without a double-map-lookup :(
276 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
277 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
278 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
285 #[derive(Eq, PartialEq)]
286 struct RouteGraphNode {
288 lowest_fee_to_peer_through_node: u64,
291 impl cmp::Ord for RouteGraphNode {
292 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
293 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
294 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
298 impl cmp::PartialOrd for RouteGraphNode {
299 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
300 Some(self.cmp(other))
305 pub fn new(our_pubkey: PublicKey) -> Router {
306 let mut nodes = HashMap::new();
307 nodes.insert(our_pubkey.clone(), NodeInfo {
308 channels: Vec::new(),
309 lowest_inbound_channel_fee_base_msat: u32::max_value(),
310 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
311 features: GlobalFeatures::new(),
315 addresses: Vec::new(),
318 secp_ctx: Secp256k1::new(),
319 network_map: RwLock::new(NetworkMap {
320 channels: HashMap::new(),
321 our_node_id: our_pubkey,
327 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
328 /// with an expotnential decay in node "badness". Note that there is deliberately no
329 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
330 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
331 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
332 /// behaving correctly, it will disable the failing channel and we will use it again next time.
333 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
337 /// Gets a route from us to the given target node.
338 /// Extra routing hops between known nodes and the target will be used if they are included in
340 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
341 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
342 /// *is* checked as they may change based on the receiving node.
343 pub fn get_route(&self, target: &PublicKey, last_hops: &Vec<RouteHint>, final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
344 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
345 // uptime/success in using a node in the past.
346 let network = self.network_map.read().unwrap();
348 if *target == network.our_node_id {
349 return Err(HandleError{err: "Cannot generate a route to ourselves", msg: None});
352 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
353 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
354 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
355 // to use as the A* heuristic beyond just the cost to get one node further than the current
358 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
359 let mut dist = HashMap::with_capacity(network.nodes.len());
360 for (key, node) in network.nodes.iter() {
361 dist.insert(key.clone(), (u64::max_value(),
362 node.lowest_inbound_channel_fee_base_msat as u64,
363 node.lowest_inbound_channel_fee_proportional_millionths as u64,
365 pubkey: PublicKey::new(),
368 cltv_expiry_delta: 0,
372 macro_rules! add_entry {
373 // Adds entry which goes from the node pointed to by $directional_info to
374 // $dest_node_id over the channel with id $chan_id with fees described in
375 // $directional_info.
376 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
377 //TODO: Explore simply adding fee to hit htlc_minimum_msat
378 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
379 let new_fee = $directional_info.fee_base_msat as u64 + ($starting_fee_msat + final_value_msat) * ($directional_info.fee_proportional_millionths as u64) / 1000000;
380 let mut total_fee = $starting_fee_msat as u64;
381 let old_entry = dist.get_mut(&$directional_info.src_node_id).unwrap();
382 if $directional_info.src_node_id != network.our_node_id {
383 // Ignore new_fee for channel-from-us as we assume all channels-from-us
384 // will have the same effective-fee
385 total_fee += new_fee;
386 total_fee += old_entry.2 * (final_value_msat + total_fee) / 1000000 + old_entry.1;
388 let new_graph_node = RouteGraphNode {
389 pubkey: $directional_info.src_node_id,
390 lowest_fee_to_peer_through_node: total_fee,
392 if old_entry.0 > total_fee {
393 targets.push(new_graph_node);
394 old_entry.0 = total_fee;
395 old_entry.3 = RouteHop {
396 pubkey: $dest_node_id.clone(),
397 short_channel_id: $chan_id.clone(),
398 fee_msat: new_fee, // This field is ignored on the last-hop anyway
399 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
406 macro_rules! add_entries_to_cheapest_to_target_node {
407 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
408 for chan_id in $node.channels.iter() {
409 let chan = network.channels.get(chan_id).unwrap();
410 if chan.one_to_two.src_node_id == *$node_id {
411 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
412 if chan.two_to_one.enabled {
413 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
416 if chan.one_to_two.enabled {
417 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
424 match network.nodes.get(target) {
427 add_entries_to_cheapest_to_target_node!(node, target, 0);
431 for hop in last_hops.iter() {
432 if network.nodes.get(&hop.src_node_id).is_some() {
433 add_entry!(hop.short_channel_id, target, hop, 0);
437 while let Some(RouteGraphNode { pubkey, lowest_fee_to_peer_through_node }) = targets.pop() {
438 if pubkey == network.our_node_id {
439 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
440 while res.last().unwrap().pubkey != *target {
441 let new_entry = dist.remove(&res.last().unwrap().pubkey).unwrap().3;
442 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
443 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
446 res.last_mut().unwrap().fee_msat = final_value_msat;
447 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
453 match network.nodes.get(&pubkey) {
456 let mut fee = lowest_fee_to_peer_through_node - node.lowest_inbound_channel_fee_base_msat as u64;
457 fee -= node.lowest_inbound_channel_fee_proportional_millionths as u64 * (fee + final_value_msat) / 1000000;
458 add_entries_to_cheapest_to_target_node!(node, &pubkey, fee);
463 Err(HandleError{err: "Failed to find a path to the given destination", msg: None})
469 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
470 use ln::msgs::GlobalFeatures;
472 use bitcoin::util::misc::hex_bytes;
473 use bitcoin::util::hash::Sha256dHash;
475 use secp256k1::key::{PublicKey,SecretKey};
476 use secp256k1::Secp256k1;
480 let secp_ctx = Secp256k1::new();
481 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap();
482 let router = Router::new(our_id);
484 // Build network from our_id to node8:
486 // -1(1)2- node1 -1(3)2-
490 // -1(2)2- node2 -1(4)2-
493 // chan1 1-to-2: disabled
494 // chan1 2-to-1: enabled, 0 fee
496 // chan2 1-to-2: enabled, ignored fee
497 // chan2 2-to-1: enabled, 0 fee
499 // chan3 1-to-2: enabled, 0 fee
500 // chan3 2-to-1: enabled, 100 msat fee
502 // chan4 1-to-2: enabled, 100% fee
503 // chan4 2-to-1: enabled, 0 fee
507 // -1(5)2- node4 -1(8)2--
511 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
513 // -1(7)2- node6 -1(10)2-
515 // chan5 1-to-2: enabled, 100 msat fee
516 // chan5 2-to-1: enabled, 0 fee
518 // chan6 1-to-2: enabled, 0 fee
519 // chan6 2-to-1: enabled, 0 fee
521 // chan7 1-to-2: enabled, 100% fee
522 // chan7 2-to-1: enabled, 0 fee
524 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
525 // chan8 2-to-1: enabled, 0 fee
527 // chan9 1-to-2: enabled, 1001 msat fee
528 // chan9 2-to-1: enabled, 0 fee
530 // chan10 1-to-2: enabled, 0 fee
531 // chan10 2-to-1: enabled, 0 fee
533 // chan11 1-to-2: enabled, 0 fee
534 // chan11 2-to-1: enabled, 0 fee
536 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()).unwrap();
537 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()).unwrap();
538 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()).unwrap();
539 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()).unwrap();
540 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()).unwrap();
541 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()).unwrap();
542 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()).unwrap();
544 let zero_hash = Sha256dHash::from_data(&[0; 32]);
547 let mut network = router.network_map.write().unwrap();
549 network.nodes.insert(node1.clone(), NodeInfo {
550 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
551 lowest_inbound_channel_fee_base_msat: 100,
552 lowest_inbound_channel_fee_proportional_millionths: 0,
553 features: GlobalFeatures::new(),
557 addresses: Vec::new(),
559 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
560 features: GlobalFeatures::new(),
561 one_to_two: DirectionalChannelInfo {
562 src_node_id: our_id.clone(),
565 cltv_expiry_delta: u16::max_value(), // This value should be ignored
566 htlc_minimum_msat: 0,
567 fee_base_msat: u32::max_value(), // This value should be ignored
568 fee_proportional_millionths: u32::max_value(), // This value should be ignored
569 }, two_to_one: DirectionalChannelInfo {
570 src_node_id: node1.clone(),
573 cltv_expiry_delta: 0,
574 htlc_minimum_msat: 0,
576 fee_proportional_millionths: 0,
579 network.nodes.insert(node2.clone(), NodeInfo {
580 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
581 lowest_inbound_channel_fee_base_msat: 0,
582 lowest_inbound_channel_fee_proportional_millionths: 0,
583 features: GlobalFeatures::new(),
587 addresses: Vec::new(),
589 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
590 features: GlobalFeatures::new(),
591 one_to_two: DirectionalChannelInfo {
592 src_node_id: our_id.clone(),
595 cltv_expiry_delta: u16::max_value(), // This value should be ignored
596 htlc_minimum_msat: 0,
597 fee_base_msat: u32::max_value(), // This value should be ignored
598 fee_proportional_millionths: u32::max_value(), // This value should be ignored
599 }, two_to_one: DirectionalChannelInfo {
600 src_node_id: node2.clone(),
603 cltv_expiry_delta: 0,
604 htlc_minimum_msat: 0,
606 fee_proportional_millionths: 0,
609 network.nodes.insert(node3.clone(), NodeInfo {
611 NetworkMap::get_key(3, zero_hash.clone()),
612 NetworkMap::get_key(4, zero_hash.clone()),
613 NetworkMap::get_key(5, zero_hash.clone()),
614 NetworkMap::get_key(6, zero_hash.clone()),
615 NetworkMap::get_key(7, zero_hash.clone())),
616 lowest_inbound_channel_fee_base_msat: 0,
617 lowest_inbound_channel_fee_proportional_millionths: 0,
618 features: GlobalFeatures::new(),
622 addresses: Vec::new(),
624 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
625 features: GlobalFeatures::new(),
626 one_to_two: DirectionalChannelInfo {
627 src_node_id: node1.clone(),
630 cltv_expiry_delta: (3 << 8) | 1,
631 htlc_minimum_msat: 0,
633 fee_proportional_millionths: 0,
634 }, two_to_one: DirectionalChannelInfo {
635 src_node_id: node3.clone(),
638 cltv_expiry_delta: (3 << 8) | 2,
639 htlc_minimum_msat: 0,
641 fee_proportional_millionths: 0,
644 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
645 features: GlobalFeatures::new(),
646 one_to_two: DirectionalChannelInfo {
647 src_node_id: node2.clone(),
650 cltv_expiry_delta: (4 << 8) | 1,
651 htlc_minimum_msat: 0,
653 fee_proportional_millionths: 1000000,
654 }, two_to_one: DirectionalChannelInfo {
655 src_node_id: node3.clone(),
658 cltv_expiry_delta: (4 << 8) | 2,
659 htlc_minimum_msat: 0,
661 fee_proportional_millionths: 0,
664 network.nodes.insert(node4.clone(), NodeInfo {
665 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
666 lowest_inbound_channel_fee_base_msat: 0,
667 lowest_inbound_channel_fee_proportional_millionths: 0,
668 features: GlobalFeatures::new(),
672 addresses: Vec::new(),
674 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
675 features: GlobalFeatures::new(),
676 one_to_two: DirectionalChannelInfo {
677 src_node_id: node3.clone(),
680 cltv_expiry_delta: (5 << 8) | 1,
681 htlc_minimum_msat: 0,
683 fee_proportional_millionths: 0,
684 }, two_to_one: DirectionalChannelInfo {
685 src_node_id: node4.clone(),
688 cltv_expiry_delta: (5 << 8) | 2,
689 htlc_minimum_msat: 0,
691 fee_proportional_millionths: 0,
694 network.nodes.insert(node5.clone(), NodeInfo {
695 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
696 lowest_inbound_channel_fee_base_msat: 0,
697 lowest_inbound_channel_fee_proportional_millionths: 0,
698 features: GlobalFeatures::new(),
702 addresses: Vec::new(),
704 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
705 features: GlobalFeatures::new(),
706 one_to_two: DirectionalChannelInfo {
707 src_node_id: node3.clone(),
710 cltv_expiry_delta: (6 << 8) | 1,
711 htlc_minimum_msat: 0,
713 fee_proportional_millionths: 0,
714 }, two_to_one: DirectionalChannelInfo {
715 src_node_id: node5.clone(),
718 cltv_expiry_delta: (6 << 8) | 2,
719 htlc_minimum_msat: 0,
721 fee_proportional_millionths: 0,
724 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
725 features: GlobalFeatures::new(),
726 one_to_two: DirectionalChannelInfo {
727 src_node_id: node5.clone(),
730 cltv_expiry_delta: (11 << 8) | 1,
731 htlc_minimum_msat: 0,
733 fee_proportional_millionths: 0,
734 }, two_to_one: DirectionalChannelInfo {
735 src_node_id: node4.clone(),
738 cltv_expiry_delta: (11 << 8) | 2,
739 htlc_minimum_msat: 0,
741 fee_proportional_millionths: 0,
744 network.nodes.insert(node6.clone(), NodeInfo {
745 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
746 lowest_inbound_channel_fee_base_msat: 0,
747 lowest_inbound_channel_fee_proportional_millionths: 0,
748 features: GlobalFeatures::new(),
752 addresses: Vec::new(),
754 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
755 features: GlobalFeatures::new(),
756 one_to_two: DirectionalChannelInfo {
757 src_node_id: node3.clone(),
760 cltv_expiry_delta: (7 << 8) | 1,
761 htlc_minimum_msat: 0,
763 fee_proportional_millionths: 1000000,
764 }, two_to_one: DirectionalChannelInfo {
765 src_node_id: node6.clone(),
768 cltv_expiry_delta: (7 << 8) | 2,
769 htlc_minimum_msat: 0,
771 fee_proportional_millionths: 0,
776 { // Simple route to 3 via 2
777 let route = router.get_route(&node3, &Vec::new(), 100, 42).unwrap();
778 assert_eq!(route.hops.len(), 2);
780 assert_eq!(route.hops[0].pubkey, node2);
781 assert_eq!(route.hops[0].short_channel_id, 2);
782 assert_eq!(route.hops[0].fee_msat, 100);
783 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
785 assert_eq!(route.hops[1].pubkey, node3);
786 assert_eq!(route.hops[1].short_channel_id, 4);
787 assert_eq!(route.hops[1].fee_msat, 100);
788 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
791 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
792 let route = router.get_route(&node1, &Vec::new(), 100, 42).unwrap();
793 assert_eq!(route.hops.len(), 3);
795 assert_eq!(route.hops[0].pubkey, node2);
796 assert_eq!(route.hops[0].short_channel_id, 2);
797 assert_eq!(route.hops[0].fee_msat, 200);
798 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
800 assert_eq!(route.hops[1].pubkey, node3);
801 assert_eq!(route.hops[1].short_channel_id, 4);
802 assert_eq!(route.hops[1].fee_msat, 100);
803 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
805 assert_eq!(route.hops[2].pubkey, node1);
806 assert_eq!(route.hops[2].short_channel_id, 3);
807 assert_eq!(route.hops[2].fee_msat, 100);
808 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
811 let mut last_hops = vec!(RouteHint {
812 src_node_id: node4.clone(),
815 fee_proportional_millionths: 0,
816 cltv_expiry_delta: (8 << 8) | 1,
817 htlc_minimum_msat: 0,
819 src_node_id: node5.clone(),
822 fee_proportional_millionths: 0,
823 cltv_expiry_delta: (9 << 8) | 1,
824 htlc_minimum_msat: 0,
826 src_node_id: node6.clone(),
827 short_channel_id: 10,
829 fee_proportional_millionths: 0,
830 cltv_expiry_delta: (10 << 8) | 1,
831 htlc_minimum_msat: 0,
834 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
835 let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
836 assert_eq!(route.hops.len(), 5);
838 assert_eq!(route.hops[0].pubkey, node2);
839 assert_eq!(route.hops[0].short_channel_id, 2);
840 assert_eq!(route.hops[0].fee_msat, 100);
841 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
843 assert_eq!(route.hops[1].pubkey, node3);
844 assert_eq!(route.hops[1].short_channel_id, 4);
845 assert_eq!(route.hops[1].fee_msat, 0);
846 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
848 assert_eq!(route.hops[2].pubkey, node5);
849 assert_eq!(route.hops[2].short_channel_id, 6);
850 assert_eq!(route.hops[2].fee_msat, 0);
851 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
853 assert_eq!(route.hops[3].pubkey, node4);
854 assert_eq!(route.hops[3].short_channel_id, 11);
855 assert_eq!(route.hops[3].fee_msat, 0);
856 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
858 assert_eq!(route.hops[4].pubkey, node7);
859 assert_eq!(route.hops[4].short_channel_id, 8);
860 assert_eq!(route.hops[4].fee_msat, 100);
861 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
864 last_hops[0].fee_base_msat = 1000;
866 { // Revert to via 6 as the fee on 8 goes up
867 let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
868 assert_eq!(route.hops.len(), 4);
870 assert_eq!(route.hops[0].pubkey, node2);
871 assert_eq!(route.hops[0].short_channel_id, 2);
872 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
873 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
875 assert_eq!(route.hops[1].pubkey, node3);
876 assert_eq!(route.hops[1].short_channel_id, 4);
877 assert_eq!(route.hops[1].fee_msat, 100);
878 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
880 assert_eq!(route.hops[2].pubkey, node6);
881 assert_eq!(route.hops[2].short_channel_id, 7);
882 assert_eq!(route.hops[2].fee_msat, 0);
883 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
885 assert_eq!(route.hops[3].pubkey, node7);
886 assert_eq!(route.hops[3].short_channel_id, 10);
887 assert_eq!(route.hops[3].fee_msat, 100);
888 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
891 { // ...but still use 8 for larger payments as 6 has a variable feerate
892 let route = router.get_route(&node7, &last_hops, 2000, 42).unwrap();
893 assert_eq!(route.hops.len(), 5);
895 assert_eq!(route.hops[0].pubkey, node2);
896 assert_eq!(route.hops[0].short_channel_id, 2);
897 assert_eq!(route.hops[0].fee_msat, 3000);
898 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
900 assert_eq!(route.hops[1].pubkey, node3);
901 assert_eq!(route.hops[1].short_channel_id, 4);
902 assert_eq!(route.hops[1].fee_msat, 0);
903 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
905 assert_eq!(route.hops[2].pubkey, node5);
906 assert_eq!(route.hops[2].short_channel_id, 6);
907 assert_eq!(route.hops[2].fee_msat, 0);
908 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
910 assert_eq!(route.hops[3].pubkey, node4);
911 assert_eq!(route.hops[3].short_channel_id, 11);
912 assert_eq!(route.hops[3].fee_msat, 1000);
913 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
915 assert_eq!(route.hops[4].pubkey, node7);
916 assert_eq!(route.hops[4].short_channel_id, 8);
917 assert_eq!(route.hops[4].fee_msat, 2000);
918 assert_eq!(route.hops[4].cltv_expiry_delta, 42);