-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to count total fees in a Route #999 #1063
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,26 @@ pub struct Route { | |
pub paths: Vec<Vec<RouteHop>>, | ||
} | ||
|
||
impl Route { | ||
/// Returns the total amount of fees paid on this Route. | ||
/// This doesn't include any extra payment made to the recipient, | ||
/// which can happen in excess of the amount passed to `get_route`'s `final_value_msat`. | ||
pub fn get_total_fees(&self) -> u64 { | ||
// Do not count last hop of each path since that's the full value of the payment | ||
return self.paths.iter() | ||
.flat_map(|path| path.split_last().unwrap().1) | ||
.map(|hop| &hop.fee_msat) | ||
.sum(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: newline below |
||
/// Returns the total amount paid on this Route, excluding the fees. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: ticks around |
||
pub fn get_total_amount(&self) -> u64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idk if it's worth noting for these methods that they'll panic for 0-length routes? |
||
return self.paths.iter() | ||
.map(|path| path.split_last().unwrap().0) | ||
.map(|hop| &hop.fee_msat) | ||
.sum(); | ||
} | ||
} | ||
TheBlueMatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const SERIALIZATION_VERSION: u8 = 1; | ||
const MIN_SERIALIZATION_VERSION: u8 = 1; | ||
|
||
|
@@ -1191,7 +1211,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use routing::router::{get_route, Route, RouteHint, RouteHintHop, RoutingFees}; | ||
use routing::router::{get_route, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees}; | ||
use routing::network_graph::{NetworkGraph, NetGraphMsgHandler}; | ||
use chain::transaction::OutPoint; | ||
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures}; | ||
|
@@ -3425,6 +3445,7 @@ mod tests { | |
total_amount_paid_msat += path.last().unwrap().fee_msat; | ||
} | ||
assert_eq!(total_amount_paid_msat, 200_000); | ||
assert_eq!(route.get_total_fees(), 150_000); | ||
} | ||
|
||
} | ||
|
@@ -3831,6 +3852,64 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
fn total_fees_single_path() { | ||
TheBlueMatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let route = Route { | ||
paths: vec![vec![ | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 100, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit here and below: s/generateble/generatable (i think these comments can all be deleted though 😛) |
||
}, | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 150, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 225, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
]], | ||
}; | ||
|
||
assert_eq!(route.get_total_fees(), 250); | ||
assert_eq!(route.get_total_amount(), 225); | ||
} | ||
|
||
#[test] | ||
fn total_fees_multi_path() { | ||
let route = Route { | ||
paths: vec![vec![ | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 100, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 150, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
],vec![ | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 100, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
RouteHop { | ||
pubkey: PublicKey::from_slice(&hex::decode("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(), | ||
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(), | ||
short_channel_id: 0, fee_msat: 150, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually | ||
}, | ||
]], | ||
}; | ||
|
||
assert_eq!(route.get_total_fees(), 200); | ||
assert_eq!(route.get_total_amount(), 300); | ||
} | ||
|
||
#[cfg(not(feature = "no-std"))] | ||
pub(super) fn random_init_seed() -> u64 { | ||
// Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: lines should be 100chars each