From: Jeffrey Czyz Date: Mon, 10 Jan 2022 05:16:55 +0000 (-0600) Subject: f - Add liquidity_penalty_multiplier_msat parameter X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=2156d59c6d6286cad66e4a4a2daed505990c35a0;p=rust-lightning f - Add liquidity_penalty_multiplier_msat parameter --- diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 173a33cdd..a6a732e7e 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -463,7 +463,7 @@ impl Readable for ChannelFailure { /// /// [1]: https://arxiv.org/abs/2107.05322 pub struct ProbabilisticScorer> { - _params: ProbabilisticScoringParameters, + params: ProbabilisticScoringParameters, node_id: NodeId, network_graph: G, // TODO: Remove entries of closed channels. @@ -471,9 +471,19 @@ pub struct ProbabilisticScorer> { } /// Parameters for configuring [`ProbabilisticScorer`]. -pub struct ProbabilisticScoringParameters; +pub struct ProbabilisticScoringParameters { + /// A penalty applied after multiplying by the negative log of channel's success probability for + /// a payment. + /// + /// The success probability is determined by the effective channel capacity, the payment amount, + /// and knowledge learned from prior successful and unsuccessful payments. + /// + /// Default value: 1,000 msat + pub liquidity_penalty_multiplier_msat: u64, +} impl_writeable_tlv_based!(ProbabilisticScoringParameters, { + (0, liquidity_penalty_multiplier_msat, required), }); /// Accounting for channel liquidity balance uncertainty. @@ -504,10 +514,10 @@ impl> ProbabilisticScorer { /// Creates a new scorer using the given scoring parameters for sending payments from a node /// through a network graph. pub fn new( - _params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G + params: ProbabilisticScoringParameters, node_pubkey: PublicKey, network_graph: G ) -> Self { Self { - _params, + params, node_id: NodeId::from_pubkey(&node_pubkey), network_graph, channel_liquidities: HashMap::new(), @@ -523,7 +533,9 @@ impl> ProbabilisticScorer { impl Default for ProbabilisticScoringParameters { fn default() -> Self { - Self + Self { + liquidity_penalty_multiplier_msat: 1000, + } } } @@ -654,6 +666,7 @@ impl> Score for ProbabilisticScorer { return 0; } + let liquidity_penalty_multiplier_msat = self.params.liquidity_penalty_multiplier_msat; let success_probability = self.channel_liquidities .get(&short_channel_id) .unwrap_or(&ChannelLiquidity::new()) @@ -664,7 +677,7 @@ impl> Score for ProbabilisticScorer { Probability::One => 0, Probability::Ratio { numerator, denominator } => { let success_probability = numerator as f64 / denominator as f64; - (-(success_probability.log10()) * amount_msat as f64) as u64 + (-(success_probability.log10()) * liquidity_penalty_multiplier_msat as f64) as u64 }, } } @@ -732,7 +745,7 @@ impl> Score for ProbabilisticScorer { impl> Writeable for ProbabilisticScorer { #[inline] fn write(&self, w: &mut W) -> Result<(), io::Error> { - self._params.write(w)?; + self.params.write(w)?; self.node_id.write(w)?; self.channel_liquidities.write(w)?; write_tlv_fields!(w, {}); @@ -744,7 +757,7 @@ impl> ReadableArgs for ProbabilisticScorer #[inline] fn read(r: &mut R, args: G) -> Result { let res = Ok(Self { - _params: Readable::read(r)?, + params: Readable::read(r)?, node_id: Readable::read(r)?, network_graph: args, channel_liquidities: Readable::read(r)?, @@ -1370,16 +1383,16 @@ mod tests { assert_eq!(scorer.channel_penalty_msat(42, 100, 100_000, &source, &target), 0); assert_eq!(scorer.channel_penalty_msat(42, 1_000, 100_000, &source, &target), 4); - assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 457); - assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 500_000); + assert_eq!(scorer.channel_penalty_msat(42, 10_000, 100_000, &source, &target), 45); + assert_eq!(scorer.channel_penalty_msat(42, 100_000, 100_000, &source, &target), 5_000); - assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 7); - assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 31); - assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 76); - assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 150); - assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 265); - assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 450); - assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 787); + assert_eq!(scorer.channel_penalty_msat(42, 125, 1_000, &source, &target), 57); + assert_eq!(scorer.channel_penalty_msat(42, 250, 1_000, &source, &target), 124); + assert_eq!(scorer.channel_penalty_msat(42, 375, 1_000, &source, &target), 203); + assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300); + assert_eq!(scorer.channel_penalty_msat(42, 625, 1_000, &source, &target), 425); + assert_eq!(scorer.channel_penalty_msat(42, 750, 1_000, &source, &target), 600); + assert_eq!(scorer.channel_penalty_msat(42, 875, 1_000, &source, &target), 900); } #[test] @@ -1414,14 +1427,14 @@ mod tests { let path = payment_path(200); assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0); - assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), 94); - assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 35); + assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), 474); + assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 175); scorer.payment_path_successful(&path.iter().collect::>()); assert_eq!(scorer.channel_penalty_msat(41, 200, 1_000, &sender, &source), 0); assert_eq!(scorer.channel_penalty_msat(42, 200, 1_000, &source, &target), u64::max_value()); - assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 59); + assert_eq!(scorer.channel_penalty_msat(43, 200, 1_000, &target, &recipient), 299); } // TODO: Add more test coverage