1 use secp256k1::key::PublicKey;
2 use secp256k1::{Secp256k1,Message};
4 use bitcoin::util::hash::Sha256dHash;
6 use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
10 use std::sync::RwLock;
11 use std::collections::{HashMap,BinaryHeap};
12 use std::collections::hash_map::Entry;
17 pub pubkey: PublicKey,
18 /// The channel that should be used from the previous hop to reach this node.
19 pub short_channel_id: u64,
20 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
22 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
23 /// expected at the destination, NOT a delta.
24 pub cltv_expiry_delta: u32,
27 /// A route from us through the network to a destination
30 /// The list of hops, NOT INCLUDING our own, where the last hop is the destination. Thus, this
31 /// must always be at least length one. By protocol rules, this may not currently exceed 20 in
33 pub hops: Vec<RouteHop>,
36 struct DirectionalChannelInfo {
37 src_node_id: PublicKey,
40 cltv_expiry_delta: u16,
41 htlc_minimum_msat: u64,
43 fee_proportional_millionths: u32,
47 features: GlobalFeatures,
48 one_to_two: DirectionalChannelInfo,
49 two_to_one: DirectionalChannelInfo,
53 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
54 channels: Vec<(u64, Sha256dHash)>,
55 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
58 lowest_inbound_channel_fee_base_msat: u32,
59 lowest_inbound_channel_fee_proportional_millionths: u32,
61 features: GlobalFeatures,
65 addresses: Vec<NetAddress>,
69 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
70 channels: HashMap<(u64, Sha256dHash), ChannelInfo>,
71 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
72 channels: HashMap<u64, ChannelInfo>,
74 our_node_id: PublicKey,
75 nodes: HashMap<PublicKey, NodeInfo>,
79 #[cfg(feature = "non_bitcoin_chain_hash_routing")]
81 fn get_key(short_channel_id: u64, chain_hash: Sha256dHash) -> (u64, Sha256dHash) {
82 (short_channel_id, chain_hash)
85 #[cfg(not(feature = "non_bitcoin_chain_hash_routing"))]
87 fn get_key(short_channel_id: u64, _: Sha256dHash) -> u64 {
92 /// A channel descriptor which provides a last-hop route to get_route
93 pub struct RouteHint {
94 pub src_node_id: PublicKey,
95 pub short_channel_id: u64,
96 pub fee_base_msat: u64,
97 pub fee_proportional_millionths: u32,
98 pub cltv_expiry_delta: u16,
99 pub htlc_minimum_msat: u64,
102 /// Tracks a view of the network, receiving updates from peers and generating Routes to
103 /// payment destinations.
106 network_map: RwLock<NetworkMap>,
109 macro_rules! secp_verify_sig {
110 ( $secp_ctx: expr, $msg: expr, $sig: expr, $pubkey: expr ) => {
111 match $secp_ctx.verify($msg, $sig, $pubkey) {
113 Err(_) => return Err(HandleError{err: "Invalid signature from remote node", action: None}),
118 impl RoutingMessageHandler for Router {
119 fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), HandleError> {
120 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
121 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
123 let mut network = self.network_map.write().unwrap();
124 match network.nodes.get_mut(&msg.contents.node_id) {
125 None => Err(HandleError{err: "No existing channels for node_announcement", action: Some(ErrorAction::IgnoreError)}),
127 if node.last_update >= msg.contents.timestamp {
128 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
131 node.features = msg.contents.features.clone();
132 node.last_update = msg.contents.timestamp;
133 node.rgb = msg.contents.rgb;
134 node.alias = msg.contents.alias;
135 node.addresses = msg.contents.addresses.clone();
141 fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result<bool, HandleError> {
142 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
143 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
144 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2);
145 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1);
146 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &msg.contents.bitcoin_key_2);
148 //TODO: Call blockchain thing to ask if the short_channel_id is valid
149 //TODO: Only allow bitcoin chain_hash
151 if msg.contents.features.requires_unknown_bits() {
152 return Err(HandleError{err: "Channel announcement required unknown feature flags", action: None});
155 let mut network = self.network_map.write().unwrap();
157 match network.channels.entry(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
158 Entry::Occupied(_) => {
159 //TODO: because asking the blockchain if short_channel_id is valid is only optional
160 //in the blockchain API, we need to handle it smartly here, though its unclear
162 return Err(HandleError{err: "Already have knowledge of channel", action: Some(ErrorAction::IgnoreError)})
164 Entry::Vacant(entry) => {
165 entry.insert(ChannelInfo {
166 features: msg.contents.features.clone(),
167 one_to_two: DirectionalChannelInfo {
168 src_node_id: msg.contents.node_id_1.clone(),
171 cltv_expiry_delta: u16::max_value(),
172 htlc_minimum_msat: u64::max_value(),
173 fee_base_msat: u32::max_value(),
174 fee_proportional_millionths: u32::max_value(),
176 two_to_one: DirectionalChannelInfo {
177 src_node_id: msg.contents.node_id_2.clone(),
180 cltv_expiry_delta: u16::max_value(),
181 htlc_minimum_msat: u64::max_value(),
182 fee_base_msat: u32::max_value(),
183 fee_proportional_millionths: u32::max_value(),
189 macro_rules! add_channel_to_node {
190 ( $node_id: expr ) => {
191 match network.nodes.entry($node_id) {
192 Entry::Occupied(node_entry) => {
193 node_entry.into_mut().channels.push(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash));
195 Entry::Vacant(node_entry) => {
196 node_entry.insert(NodeInfo {
197 channels: vec!(NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)),
198 lowest_inbound_channel_fee_base_msat: u32::max_value(),
199 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
200 features: GlobalFeatures::new(),
204 addresses: Vec::new(),
211 add_channel_to_node!(msg.contents.node_id_1);
212 add_channel_to_node!(msg.contents.node_id_2);
214 Ok(!msg.contents.features.supports_unknown_bits())
217 fn handle_htlc_fail_channel_update(&self, update: &msgs::HTLCFailChannelUpdate) {
219 &msgs::HTLCFailChannelUpdate::ChannelUpdateMessage { ref msg } => {
220 let _ = self.handle_channel_update(msg);
222 &msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id } => {
223 let mut network = self.network_map.write().unwrap();
224 network.channels.remove(short_channel_id);
229 fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result<(), HandleError> {
230 let mut network = self.network_map.write().unwrap();
232 let chan_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
233 let chan_was_enabled;
235 match network.channels.get_mut(&NetworkMap::get_key(msg.contents.short_channel_id, msg.contents.chain_hash)) {
236 None => return Err(HandleError{err: "Couldn't find channel for update", action: Some(ErrorAction::IgnoreError)}),
238 macro_rules! maybe_update_channel_info {
239 ( $target: expr) => {
240 if $target.last_update >= msg.contents.timestamp {
241 return Err(HandleError{err: "Update older than last processed update", action: Some(ErrorAction::IgnoreError)});
243 chan_was_enabled = $target.enabled;
244 $target.last_update = msg.contents.timestamp;
245 $target.enabled = chan_enabled;
246 $target.cltv_expiry_delta = msg.contents.cltv_expiry_delta;
247 $target.htlc_minimum_msat = msg.contents.htlc_minimum_msat;
248 $target.fee_base_msat = msg.contents.fee_base_msat;
249 $target.fee_proportional_millionths = msg.contents.fee_proportional_millionths;
253 let msg_hash = Message::from_slice(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]).unwrap();
254 if msg.contents.flags & 1 == 1 {
255 dest_node_id = channel.one_to_two.src_node_id.clone();
256 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id);
257 maybe_update_channel_info!(channel.two_to_one);
259 dest_node_id = channel.two_to_one.src_node_id.clone();
260 secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.one_to_two.src_node_id);
261 maybe_update_channel_info!(channel.one_to_two);
267 let node = network.nodes.get_mut(&dest_node_id).unwrap();
268 node.lowest_inbound_channel_fee_base_msat = cmp::min(node.lowest_inbound_channel_fee_base_msat, msg.contents.fee_base_msat);
269 node.lowest_inbound_channel_fee_proportional_millionths = cmp::min(node.lowest_inbound_channel_fee_proportional_millionths, msg.contents.fee_proportional_millionths);
270 } else if chan_was_enabled {
271 let mut lowest_inbound_channel_fee_base_msat = u32::max_value();
272 let mut lowest_inbound_channel_fee_proportional_millionths = u32::max_value();
275 let node = network.nodes.get(&dest_node_id).unwrap();
277 for chan_id in node.channels.iter() {
278 let chan = network.channels.get(chan_id).unwrap();
279 if chan.one_to_two.src_node_id == dest_node_id {
280 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.two_to_one.fee_base_msat);
281 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.two_to_one.fee_proportional_millionths);
283 lowest_inbound_channel_fee_base_msat = cmp::min(lowest_inbound_channel_fee_base_msat, chan.one_to_two.fee_base_msat);
284 lowest_inbound_channel_fee_proportional_millionths = cmp::min(lowest_inbound_channel_fee_proportional_millionths, chan.one_to_two.fee_proportional_millionths);
289 //TODO: satisfy the borrow-checker without a double-map-lookup :(
290 let mut_node = network.nodes.get_mut(&dest_node_id).unwrap();
291 mut_node.lowest_inbound_channel_fee_base_msat = lowest_inbound_channel_fee_base_msat;
292 mut_node.lowest_inbound_channel_fee_proportional_millionths = lowest_inbound_channel_fee_proportional_millionths;
299 #[derive(Eq, PartialEq)]
300 struct RouteGraphNode {
302 lowest_fee_to_peer_through_node: u64,
305 impl cmp::Ord for RouteGraphNode {
306 fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
307 other.lowest_fee_to_peer_through_node.cmp(&self.lowest_fee_to_peer_through_node)
308 .then_with(|| other.pubkey.serialize().cmp(&self.pubkey.serialize()))
312 impl cmp::PartialOrd for RouteGraphNode {
313 fn partial_cmp(&self, other: &RouteGraphNode) -> Option<cmp::Ordering> {
314 Some(self.cmp(other))
319 pub fn new(our_pubkey: PublicKey) -> Router {
320 let mut nodes = HashMap::new();
321 nodes.insert(our_pubkey.clone(), NodeInfo {
322 channels: Vec::new(),
323 lowest_inbound_channel_fee_base_msat: u32::max_value(),
324 lowest_inbound_channel_fee_proportional_millionths: u32::max_value(),
325 features: GlobalFeatures::new(),
329 addresses: Vec::new(),
332 secp_ctx: Secp256k1::new(),
333 network_map: RwLock::new(NetworkMap {
334 channels: HashMap::new(),
335 our_node_id: our_pubkey,
341 /// Get network addresses by node id
342 pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
343 let network = self.network_map.read().unwrap();
344 network.nodes.get(pubkey).map(|n| n.addresses.clone())
347 /// Marks a node as having failed a route. This will avoid re-using the node in routes for now,
348 /// with an expotnential decay in node "badness". Note that there is deliberately no
349 /// mark_channel_bad as a node may simply lie and suggest that an upstream channel from it is
350 /// what failed the route and not the node itself. Instead, setting the blamed_upstream_node
351 /// boolean will reduce the penalty, returning the node to usability faster. If the node is
352 /// behaving correctly, it will disable the failing channel and we will use it again next time.
353 pub fn mark_node_bad(&self, _node_id: &PublicKey, _blamed_upstream_node: bool) {
357 /// Gets a route from us to the given target node.
358 /// Extra routing hops between known nodes and the target will be used if they are included in
360 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
361 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
362 /// *is* checked as they may change based on the receiving node.
363 pub fn get_route(&self, target: &PublicKey, last_hops: &Vec<RouteHint>, final_value_msat: u64, final_cltv: u32) -> Result<Route, HandleError> {
364 // TODO: Obviously *only* using total fee cost sucks. We should consider weighting by
365 // uptime/success in using a node in the past.
366 let network = self.network_map.read().unwrap();
368 if *target == network.our_node_id {
369 return Err(HandleError{err: "Cannot generate a route to ourselves", action: None});
372 // We do a dest-to-source Dijkstra's sorting by each node's distance from the destination
373 // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
374 // TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
375 // to use as the A* heuristic beyond just the cost to get one node further than the current
378 let mut targets = BinaryHeap::new(); //TODO: Do we care about switching to eg Fibbonaci heap?
379 let mut dist = HashMap::with_capacity(network.nodes.len());
380 for (key, node) in network.nodes.iter() {
381 dist.insert(key.clone(), (u64::max_value(),
382 node.lowest_inbound_channel_fee_base_msat as u64,
383 node.lowest_inbound_channel_fee_proportional_millionths as u64,
385 pubkey: PublicKey::new(),
388 cltv_expiry_delta: 0,
392 macro_rules! add_entry {
393 // Adds entry which goes from the node pointed to by $directional_info to
394 // $dest_node_id over the channel with id $chan_id with fees described in
395 // $directional_info.
396 ( $chan_id: expr, $dest_node_id: expr, $directional_info: expr, $starting_fee_msat: expr ) => {
397 //TODO: Explore simply adding fee to hit htlc_minimum_msat
398 if $starting_fee_msat as u64 + final_value_msat > $directional_info.htlc_minimum_msat {
399 let new_fee = $directional_info.fee_base_msat as u64 + ($starting_fee_msat + final_value_msat) * ($directional_info.fee_proportional_millionths as u64) / 1000000;
400 let mut total_fee = $starting_fee_msat as u64;
401 let old_entry = dist.get_mut(&$directional_info.src_node_id).unwrap();
402 if $directional_info.src_node_id != network.our_node_id {
403 // Ignore new_fee for channel-from-us as we assume all channels-from-us
404 // will have the same effective-fee
405 total_fee += new_fee;
406 total_fee += old_entry.2 * (final_value_msat + total_fee) / 1000000 + old_entry.1;
408 let new_graph_node = RouteGraphNode {
409 pubkey: $directional_info.src_node_id,
410 lowest_fee_to_peer_through_node: total_fee,
412 if old_entry.0 > total_fee {
413 targets.push(new_graph_node);
414 old_entry.0 = total_fee;
415 old_entry.3 = RouteHop {
416 pubkey: $dest_node_id.clone(),
417 short_channel_id: $chan_id.clone(),
418 fee_msat: new_fee, // This field is ignored on the last-hop anyway
419 cltv_expiry_delta: $directional_info.cltv_expiry_delta as u32,
426 macro_rules! add_entries_to_cheapest_to_target_node {
427 ( $node: expr, $node_id: expr, $fee_to_target_msat: expr ) => {
428 for chan_id in $node.channels.iter() {
429 let chan = network.channels.get(chan_id).unwrap();
430 if chan.one_to_two.src_node_id == *$node_id {
431 // ie $node is one, ie next hop in A* is two, via the two_to_one channel
432 if chan.two_to_one.enabled {
433 add_entry!(chan_id, chan.one_to_two.src_node_id, chan.two_to_one, $fee_to_target_msat);
436 if chan.one_to_two.enabled {
437 add_entry!(chan_id, chan.two_to_one.src_node_id, chan.one_to_two, $fee_to_target_msat);
444 match network.nodes.get(target) {
447 add_entries_to_cheapest_to_target_node!(node, target, 0);
451 for hop in last_hops.iter() {
452 if network.nodes.get(&hop.src_node_id).is_some() {
453 add_entry!(hop.short_channel_id, target, hop, 0);
457 while let Some(RouteGraphNode { pubkey, lowest_fee_to_peer_through_node }) = targets.pop() {
458 if pubkey == network.our_node_id {
459 let mut res = vec!(dist.remove(&network.our_node_id).unwrap().3);
460 while res.last().unwrap().pubkey != *target {
461 let new_entry = dist.remove(&res.last().unwrap().pubkey).unwrap().3;
462 res.last_mut().unwrap().fee_msat = new_entry.fee_msat;
463 res.last_mut().unwrap().cltv_expiry_delta = new_entry.cltv_expiry_delta;
466 res.last_mut().unwrap().fee_msat = final_value_msat;
467 res.last_mut().unwrap().cltv_expiry_delta = final_cltv;
473 match network.nodes.get(&pubkey) {
476 let mut fee = lowest_fee_to_peer_through_node - node.lowest_inbound_channel_fee_base_msat as u64;
477 fee -= node.lowest_inbound_channel_fee_proportional_millionths as u64 * (fee + final_value_msat) / 1000000;
478 add_entries_to_cheapest_to_target_node!(node, &pubkey, fee);
483 Err(HandleError{err: "Failed to find a path to the given destination", action: None})
489 use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
490 use ln::msgs::GlobalFeatures;
492 use bitcoin::util::misc::hex_bytes;
493 use bitcoin::util::hash::Sha256dHash;
495 use secp256k1::key::{PublicKey,SecretKey};
496 use secp256k1::Secp256k1;
500 let secp_ctx = Secp256k1::new();
501 let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap();
502 let router = Router::new(our_id);
504 // Build network from our_id to node8:
506 // -1(1)2- node1 -1(3)2-
510 // -1(2)2- node2 -1(4)2-
513 // chan1 1-to-2: disabled
514 // chan1 2-to-1: enabled, 0 fee
516 // chan2 1-to-2: enabled, ignored fee
517 // chan2 2-to-1: enabled, 0 fee
519 // chan3 1-to-2: enabled, 0 fee
520 // chan3 2-to-1: enabled, 100 msat fee
522 // chan4 1-to-2: enabled, 100% fee
523 // chan4 2-to-1: enabled, 0 fee
527 // -1(5)2- node4 -1(8)2--
531 // node3--1(6)2- node5 -1(9)2--- node7 (not in global route map)
533 // -1(7)2- node6 -1(10)2-
535 // chan5 1-to-2: enabled, 100 msat fee
536 // chan5 2-to-1: enabled, 0 fee
538 // chan6 1-to-2: enabled, 0 fee
539 // chan6 2-to-1: enabled, 0 fee
541 // chan7 1-to-2: enabled, 100% fee
542 // chan7 2-to-1: enabled, 0 fee
544 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
545 // chan8 2-to-1: enabled, 0 fee
547 // chan9 1-to-2: enabled, 1001 msat fee
548 // chan9 2-to-1: enabled, 0 fee
550 // chan10 1-to-2: enabled, 0 fee
551 // chan10 2-to-1: enabled, 0 fee
553 // chan11 1-to-2: enabled, 0 fee
554 // chan11 2-to-1: enabled, 0 fee
556 let node1 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap()).unwrap();
557 let node2 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0303030303030303030303030303030303030303030303030303030303030303").unwrap()[..]).unwrap()).unwrap();
558 let node3 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0404040404040404040404040404040404040404040404040404040404040404").unwrap()[..]).unwrap()).unwrap();
559 let node4 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0505050505050505050505050505050505050505050505050505050505050505").unwrap()[..]).unwrap()).unwrap();
560 let node5 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0606060606060606060606060606060606060606060606060606060606060606").unwrap()[..]).unwrap()).unwrap();
561 let node6 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0707070707070707070707070707070707070707070707070707070707070707").unwrap()[..]).unwrap()).unwrap();
562 let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()).unwrap();
564 let zero_hash = Sha256dHash::from_data(&[0; 32]);
567 let mut network = router.network_map.write().unwrap();
569 network.nodes.insert(node1.clone(), NodeInfo {
570 channels: vec!(NetworkMap::get_key(1, zero_hash.clone()), NetworkMap::get_key(3, zero_hash.clone())),
571 lowest_inbound_channel_fee_base_msat: 100,
572 lowest_inbound_channel_fee_proportional_millionths: 0,
573 features: GlobalFeatures::new(),
577 addresses: Vec::new(),
579 network.channels.insert(NetworkMap::get_key(1, zero_hash.clone()), ChannelInfo {
580 features: GlobalFeatures::new(),
581 one_to_two: DirectionalChannelInfo {
582 src_node_id: our_id.clone(),
585 cltv_expiry_delta: u16::max_value(), // This value should be ignored
586 htlc_minimum_msat: 0,
587 fee_base_msat: u32::max_value(), // This value should be ignored
588 fee_proportional_millionths: u32::max_value(), // This value should be ignored
589 }, two_to_one: DirectionalChannelInfo {
590 src_node_id: node1.clone(),
593 cltv_expiry_delta: 0,
594 htlc_minimum_msat: 0,
596 fee_proportional_millionths: 0,
599 network.nodes.insert(node2.clone(), NodeInfo {
600 channels: vec!(NetworkMap::get_key(2, zero_hash.clone()), NetworkMap::get_key(4, zero_hash.clone())),
601 lowest_inbound_channel_fee_base_msat: 0,
602 lowest_inbound_channel_fee_proportional_millionths: 0,
603 features: GlobalFeatures::new(),
607 addresses: Vec::new(),
609 network.channels.insert(NetworkMap::get_key(2, zero_hash.clone()), ChannelInfo {
610 features: GlobalFeatures::new(),
611 one_to_two: DirectionalChannelInfo {
612 src_node_id: our_id.clone(),
615 cltv_expiry_delta: u16::max_value(), // This value should be ignored
616 htlc_minimum_msat: 0,
617 fee_base_msat: u32::max_value(), // This value should be ignored
618 fee_proportional_millionths: u32::max_value(), // This value should be ignored
619 }, two_to_one: DirectionalChannelInfo {
620 src_node_id: node2.clone(),
623 cltv_expiry_delta: 0,
624 htlc_minimum_msat: 0,
626 fee_proportional_millionths: 0,
629 network.nodes.insert(node3.clone(), NodeInfo {
631 NetworkMap::get_key(3, zero_hash.clone()),
632 NetworkMap::get_key(4, zero_hash.clone()),
633 NetworkMap::get_key(5, zero_hash.clone()),
634 NetworkMap::get_key(6, zero_hash.clone()),
635 NetworkMap::get_key(7, zero_hash.clone())),
636 lowest_inbound_channel_fee_base_msat: 0,
637 lowest_inbound_channel_fee_proportional_millionths: 0,
638 features: GlobalFeatures::new(),
642 addresses: Vec::new(),
644 network.channels.insert(NetworkMap::get_key(3, zero_hash.clone()), ChannelInfo {
645 features: GlobalFeatures::new(),
646 one_to_two: DirectionalChannelInfo {
647 src_node_id: node1.clone(),
650 cltv_expiry_delta: (3 << 8) | 1,
651 htlc_minimum_msat: 0,
653 fee_proportional_millionths: 0,
654 }, two_to_one: DirectionalChannelInfo {
655 src_node_id: node3.clone(),
658 cltv_expiry_delta: (3 << 8) | 2,
659 htlc_minimum_msat: 0,
661 fee_proportional_millionths: 0,
664 network.channels.insert(NetworkMap::get_key(4, zero_hash.clone()), ChannelInfo {
665 features: GlobalFeatures::new(),
666 one_to_two: DirectionalChannelInfo {
667 src_node_id: node2.clone(),
670 cltv_expiry_delta: (4 << 8) | 1,
671 htlc_minimum_msat: 0,
673 fee_proportional_millionths: 1000000,
674 }, two_to_one: DirectionalChannelInfo {
675 src_node_id: node3.clone(),
678 cltv_expiry_delta: (4 << 8) | 2,
679 htlc_minimum_msat: 0,
681 fee_proportional_millionths: 0,
684 network.nodes.insert(node4.clone(), NodeInfo {
685 channels: vec!(NetworkMap::get_key(5, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
686 lowest_inbound_channel_fee_base_msat: 0,
687 lowest_inbound_channel_fee_proportional_millionths: 0,
688 features: GlobalFeatures::new(),
692 addresses: Vec::new(),
694 network.channels.insert(NetworkMap::get_key(5, zero_hash.clone()), ChannelInfo {
695 features: GlobalFeatures::new(),
696 one_to_two: DirectionalChannelInfo {
697 src_node_id: node3.clone(),
700 cltv_expiry_delta: (5 << 8) | 1,
701 htlc_minimum_msat: 0,
703 fee_proportional_millionths: 0,
704 }, two_to_one: DirectionalChannelInfo {
705 src_node_id: node4.clone(),
708 cltv_expiry_delta: (5 << 8) | 2,
709 htlc_minimum_msat: 0,
711 fee_proportional_millionths: 0,
714 network.nodes.insert(node5.clone(), NodeInfo {
715 channels: vec!(NetworkMap::get_key(6, zero_hash.clone()), NetworkMap::get_key(11, zero_hash.clone())),
716 lowest_inbound_channel_fee_base_msat: 0,
717 lowest_inbound_channel_fee_proportional_millionths: 0,
718 features: GlobalFeatures::new(),
722 addresses: Vec::new(),
724 network.channels.insert(NetworkMap::get_key(6, zero_hash.clone()), ChannelInfo {
725 features: GlobalFeatures::new(),
726 one_to_two: DirectionalChannelInfo {
727 src_node_id: node3.clone(),
730 cltv_expiry_delta: (6 << 8) | 1,
731 htlc_minimum_msat: 0,
733 fee_proportional_millionths: 0,
734 }, two_to_one: DirectionalChannelInfo {
735 src_node_id: node5.clone(),
738 cltv_expiry_delta: (6 << 8) | 2,
739 htlc_minimum_msat: 0,
741 fee_proportional_millionths: 0,
744 network.channels.insert(NetworkMap::get_key(11, zero_hash.clone()), ChannelInfo {
745 features: GlobalFeatures::new(),
746 one_to_two: DirectionalChannelInfo {
747 src_node_id: node5.clone(),
750 cltv_expiry_delta: (11 << 8) | 1,
751 htlc_minimum_msat: 0,
753 fee_proportional_millionths: 0,
754 }, two_to_one: DirectionalChannelInfo {
755 src_node_id: node4.clone(),
758 cltv_expiry_delta: (11 << 8) | 2,
759 htlc_minimum_msat: 0,
761 fee_proportional_millionths: 0,
764 network.nodes.insert(node6.clone(), NodeInfo {
765 channels: vec!(NetworkMap::get_key(7, zero_hash.clone())),
766 lowest_inbound_channel_fee_base_msat: 0,
767 lowest_inbound_channel_fee_proportional_millionths: 0,
768 features: GlobalFeatures::new(),
772 addresses: Vec::new(),
774 network.channels.insert(NetworkMap::get_key(7, zero_hash.clone()), ChannelInfo {
775 features: GlobalFeatures::new(),
776 one_to_two: DirectionalChannelInfo {
777 src_node_id: node3.clone(),
780 cltv_expiry_delta: (7 << 8) | 1,
781 htlc_minimum_msat: 0,
783 fee_proportional_millionths: 1000000,
784 }, two_to_one: DirectionalChannelInfo {
785 src_node_id: node6.clone(),
788 cltv_expiry_delta: (7 << 8) | 2,
789 htlc_minimum_msat: 0,
791 fee_proportional_millionths: 0,
796 { // Simple route to 3 via 2
797 let route = router.get_route(&node3, &Vec::new(), 100, 42).unwrap();
798 assert_eq!(route.hops.len(), 2);
800 assert_eq!(route.hops[0].pubkey, node2);
801 assert_eq!(route.hops[0].short_channel_id, 2);
802 assert_eq!(route.hops[0].fee_msat, 100);
803 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
805 assert_eq!(route.hops[1].pubkey, node3);
806 assert_eq!(route.hops[1].short_channel_id, 4);
807 assert_eq!(route.hops[1].fee_msat, 100);
808 assert_eq!(route.hops[1].cltv_expiry_delta, 42);
811 { // Route to 1 via 2 and 3 because our channel to 1 is disabled
812 let route = router.get_route(&node1, &Vec::new(), 100, 42).unwrap();
813 assert_eq!(route.hops.len(), 3);
815 assert_eq!(route.hops[0].pubkey, node2);
816 assert_eq!(route.hops[0].short_channel_id, 2);
817 assert_eq!(route.hops[0].fee_msat, 200);
818 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
820 assert_eq!(route.hops[1].pubkey, node3);
821 assert_eq!(route.hops[1].short_channel_id, 4);
822 assert_eq!(route.hops[1].fee_msat, 100);
823 assert_eq!(route.hops[1].cltv_expiry_delta, (3 << 8) | 2);
825 assert_eq!(route.hops[2].pubkey, node1);
826 assert_eq!(route.hops[2].short_channel_id, 3);
827 assert_eq!(route.hops[2].fee_msat, 100);
828 assert_eq!(route.hops[2].cltv_expiry_delta, 42);
831 let mut last_hops = vec!(RouteHint {
832 src_node_id: node4.clone(),
835 fee_proportional_millionths: 0,
836 cltv_expiry_delta: (8 << 8) | 1,
837 htlc_minimum_msat: 0,
839 src_node_id: node5.clone(),
842 fee_proportional_millionths: 0,
843 cltv_expiry_delta: (9 << 8) | 1,
844 htlc_minimum_msat: 0,
846 src_node_id: node6.clone(),
847 short_channel_id: 10,
849 fee_proportional_millionths: 0,
850 cltv_expiry_delta: (10 << 8) | 1,
851 htlc_minimum_msat: 0,
854 { // Simple test across 2, 3, 5, and 4 via a last_hop channel
855 let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
856 assert_eq!(route.hops.len(), 5);
858 assert_eq!(route.hops[0].pubkey, node2);
859 assert_eq!(route.hops[0].short_channel_id, 2);
860 assert_eq!(route.hops[0].fee_msat, 100);
861 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
863 assert_eq!(route.hops[1].pubkey, node3);
864 assert_eq!(route.hops[1].short_channel_id, 4);
865 assert_eq!(route.hops[1].fee_msat, 0);
866 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
868 assert_eq!(route.hops[2].pubkey, node5);
869 assert_eq!(route.hops[2].short_channel_id, 6);
870 assert_eq!(route.hops[2].fee_msat, 0);
871 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
873 assert_eq!(route.hops[3].pubkey, node4);
874 assert_eq!(route.hops[3].short_channel_id, 11);
875 assert_eq!(route.hops[3].fee_msat, 0);
876 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
878 assert_eq!(route.hops[4].pubkey, node7);
879 assert_eq!(route.hops[4].short_channel_id, 8);
880 assert_eq!(route.hops[4].fee_msat, 100);
881 assert_eq!(route.hops[4].cltv_expiry_delta, 42);
884 last_hops[0].fee_base_msat = 1000;
886 { // Revert to via 6 as the fee on 8 goes up
887 let route = router.get_route(&node7, &last_hops, 100, 42).unwrap();
888 assert_eq!(route.hops.len(), 4);
890 assert_eq!(route.hops[0].pubkey, node2);
891 assert_eq!(route.hops[0].short_channel_id, 2);
892 assert_eq!(route.hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
893 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
895 assert_eq!(route.hops[1].pubkey, node3);
896 assert_eq!(route.hops[1].short_channel_id, 4);
897 assert_eq!(route.hops[1].fee_msat, 100);
898 assert_eq!(route.hops[1].cltv_expiry_delta, (7 << 8) | 1);
900 assert_eq!(route.hops[2].pubkey, node6);
901 assert_eq!(route.hops[2].short_channel_id, 7);
902 assert_eq!(route.hops[2].fee_msat, 0);
903 assert_eq!(route.hops[2].cltv_expiry_delta, (10 << 8) | 1);
905 assert_eq!(route.hops[3].pubkey, node7);
906 assert_eq!(route.hops[3].short_channel_id, 10);
907 assert_eq!(route.hops[3].fee_msat, 100);
908 assert_eq!(route.hops[3].cltv_expiry_delta, 42);
911 { // ...but still use 8 for larger payments as 6 has a variable feerate
912 let route = router.get_route(&node7, &last_hops, 2000, 42).unwrap();
913 assert_eq!(route.hops.len(), 5);
915 assert_eq!(route.hops[0].pubkey, node2);
916 assert_eq!(route.hops[0].short_channel_id, 2);
917 assert_eq!(route.hops[0].fee_msat, 3000);
918 assert_eq!(route.hops[0].cltv_expiry_delta, (4 << 8) | 1);
920 assert_eq!(route.hops[1].pubkey, node3);
921 assert_eq!(route.hops[1].short_channel_id, 4);
922 assert_eq!(route.hops[1].fee_msat, 0);
923 assert_eq!(route.hops[1].cltv_expiry_delta, (6 << 8) | 1);
925 assert_eq!(route.hops[2].pubkey, node5);
926 assert_eq!(route.hops[2].short_channel_id, 6);
927 assert_eq!(route.hops[2].fee_msat, 0);
928 assert_eq!(route.hops[2].cltv_expiry_delta, (11 << 8) | 1);
930 assert_eq!(route.hops[3].pubkey, node4);
931 assert_eq!(route.hops[3].short_channel_id, 11);
932 assert_eq!(route.hops[3].fee_msat, 1000);
933 assert_eq!(route.hops[3].cltv_expiry_delta, (8 << 8) | 1);
935 assert_eq!(route.hops[4].pubkey, node7);
936 assert_eq!(route.hops[4].short_channel_id, 8);
937 assert_eq!(route.hops[4].fee_msat, 2000);
938 assert_eq!(route.hops[4].cltv_expiry_delta, 42);