Skip to content

Commit 11ab47d

Browse files
committed
f - Add test coverage for serialization
1 parent aaa12ae commit 11ab47d

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

lightning/src/routing/scoring.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ mod tests {
929929
use routing::scoring::Score;
930930
use routing::network_graph::{NetworkGraph, NodeId};
931931
use routing::router::RouteHop;
932-
use util::ser::{Readable, Writeable};
932+
use util::ser::{Readable, ReadableArgs, Writeable};
933933

934934
use bitcoin::blockdata::constants::genesis_block;
935935
use bitcoin::hashes::Hash;
@@ -1274,7 +1274,7 @@ mod tests {
12741274
// `ProbabilisticScorer` tests
12751275

12761276
/// A probabilistic scorer for testing with time that can be manually advanced.
1277-
type ProbabilisticScorer<G> = ProbabilisticScorerUsingTime::<G, SinceEpoch>;
1277+
type ProbabilisticScorer<'a> = ProbabilisticScorerUsingTime::<&'a NetworkGraph, SinceEpoch>;
12781278

12791279
fn sender_privkey() -> SecretKey {
12801280
SecretKey::from_slice(&[41; 32]).unwrap()
@@ -1821,5 +1821,65 @@ mod tests {
18211821
assert_eq!(scorer.channel_penalty_msat(42, 512, 1_024, &source, &target), 280);
18221822
}
18231823

1824-
// TODO: Add test coverage for serialization
1824+
#[test]
1825+
fn restores_persisted_liquidity_bounds() {
1826+
let network_graph = network_graph();
1827+
let params = ProbabilisticScoringParameters {
1828+
liquidity_penalty_multiplier_msat: 1_000,
1829+
liquidity_offset_half_life: Duration::from_secs(10),
1830+
};
1831+
let mut scorer = ProbabilisticScorer::new(params, &sender_pubkey(), &network_graph);
1832+
let source = source_node_id();
1833+
let target = target_node_id();
1834+
1835+
scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1836+
assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2699);
1837+
1838+
SinceEpoch::advance(Duration::from_secs(10));
1839+
assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1840+
1841+
scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1842+
assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1843+
1844+
let mut serialized_scorer = Vec::new();
1845+
scorer.write(&mut serialized_scorer).unwrap();
1846+
1847+
let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1848+
let args = (&sender_pubkey(), &network_graph);
1849+
let deserialized_scorer =
1850+
<ProbabilisticScorer>::read(&mut serialized_scorer, args).unwrap();
1851+
assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1852+
}
1853+
1854+
#[test]
1855+
fn decays_persisted_liquidity_bounds() {
1856+
let network_graph = network_graph();
1857+
let params = ProbabilisticScoringParameters {
1858+
liquidity_penalty_multiplier_msat: 1_000,
1859+
liquidity_offset_half_life: Duration::from_secs(10),
1860+
};
1861+
let mut scorer = ProbabilisticScorer::new(params, &sender_pubkey(), &network_graph);
1862+
let source = source_node_id();
1863+
let target = target_node_id();
1864+
1865+
scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
1866+
assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 2699);
1867+
1868+
let mut serialized_scorer = Vec::new();
1869+
scorer.write(&mut serialized_scorer).unwrap();
1870+
1871+
SinceEpoch::advance(Duration::from_secs(10));
1872+
1873+
let mut serialized_scorer = io::Cursor::new(&serialized_scorer);
1874+
let args = (&sender_pubkey(), &network_graph);
1875+
let deserialized_scorer =
1876+
<ProbabilisticScorer>::read(&mut serialized_scorer, args).unwrap();
1877+
assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 475);
1878+
1879+
scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
1880+
assert_eq!(scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 300);
1881+
1882+
SinceEpoch::advance(Duration::from_secs(10));
1883+
assert_eq!(deserialized_scorer.channel_penalty_msat(42, 500, 1_000, &source, &target), 367);
1884+
}
18251885
}

0 commit comments

Comments
 (0)