]> git.bitcoin.ninja Git - rust-lightning/commitdiff
f - Add test coverage for serialization
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 11 Jan 2022 16:02:15 +0000 (10:02 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 20 Jan 2022 22:14:47 +0000 (16:14 -0600)
lightning/src/routing/scoring.rs

index 151df5af3ac31a423793354ecefb870fd757d4d5..f554b103b3325b0fa5699cda891c8b7357c2de28 100644 (file)
@@ -929,7 +929,7 @@ mod tests {
        use routing::scoring::Score;
        use routing::network_graph::{NetworkGraph, NodeId};
        use routing::router::RouteHop;
-       use util::ser::{Readable, Writeable};
+       use util::ser::{Readable, ReadableArgs, Writeable};
 
        use bitcoin::blockdata::constants::genesis_block;
        use bitcoin::hashes::Hash;
@@ -1274,7 +1274,7 @@ mod tests {
        // `ProbabilisticScorer` tests
 
        /// A probabilistic scorer for testing with time that can be manually advanced.
-       type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, SinceEpoch>;
+       type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, SinceEpoch>;
 
        fn sender_privkey() -> SecretKey {
                SecretKey::from_slice(&[41; 32]).unwrap()
@@ -1821,5 +1821,65 @@ mod tests {
                assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 280);
        }
 
-       // TODO: Add test coverage for serialization
+       #[test]
+       fn restores_persisted_liquidity_bounds() {
+               let network_graph = network_graph();
+               let params = ProbabilisticScoringParameters {
+                       liquidity_penalty_multiplier_msat: 1_000,
+                       liquidity_offset_half_life: Duration::from_secs(10),
+               };
+               let mut scorer = ProbabilisticScorer::new(params, &sender_pubkey(), &network_graph);
+               let source = source_node_id();
+               let target = target_node_id();
+
+               scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
+               assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2699);
+
+               SinceEpoch::advance(Duration::from_secs(10));
+               assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
+
+               scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
+               assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
+
+               let mut serialized_scorer = Vec::new();
+               scorer.write(&mut serialized_scorer).unwrap();
+
+               let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
+               let args = (&sender_pubkey(), &network_graph);
+               let deserialized_scorer =
+                       <ProbabilisticScorer>::read(&mut serialized_scorer, args).unwrap();
+               assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
+       }
+
+       #[test]
+       fn decays_persisted_liquidity_bounds() {
+               let network_graph = network_graph();
+               let params = ProbabilisticScoringParameters {
+                       liquidity_penalty_multiplier_msat: 1_000,
+                       liquidity_offset_half_life: Duration::from_secs(10),
+               };
+               let mut scorer = ProbabilisticScorer::new(params, &sender_pubkey(), &network_graph);
+               let source = source_node_id();
+               let target = target_node_id();
+
+               scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
+               assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2699);
+
+               let mut serialized_scorer = Vec::new();
+               scorer.write(&mut serialized_scorer).unwrap();
+
+               SinceEpoch::advance(Duration::from_secs(10));
+
+               let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
+               let args = (&sender_pubkey(), &network_graph);
+               let deserialized_scorer =
+                       <ProbabilisticScorer>::read(&mut serialized_scorer, args).unwrap();
+               assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
+
+               scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
+               assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
+
+               SinceEpoch::advance(Duration::from_secs(10));
+               assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 367);
+       }
 }