Skip to content

Commit d7a72a0

Browse files
committed
Test anti-probing penalty.
1 parent 46e45be commit d7a72a0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lightning/src/routing/gossip.rs

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
128128
genesis_hash: BlockHash,
129129
logger: L,
130130
// Lock order: channels -> nodes
131+
#[cfg(test)]
132+
pub(crate) channels: RwLock<BTreeMap<u64, ChannelInfo>>,
133+
#[cfg(not(test))]
131134
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
132135
nodes: RwLock<BTreeMap<NodeId, NodeInfo>>,
133136
}

lightning/src/routing/scoring.rs

+52
Original file line numberDiff line numberDiff line change
@@ -2133,4 +2133,56 @@ mod tests {
21332133
let usage = ChannelUsage { amount_msat: 1_001, ..usage };
21342134
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
21352135
}
2136+
2137+
#[test]
2138+
fn adds_anti_probing_penalty() {
2139+
let logger = TestLogger::new();
2140+
let network_graph = network_graph(&logger);
2141+
let source = source_node_id();
2142+
let target = target_node_id();
2143+
let usage = ChannelUsage {
2144+
amount_msat: 512_000,
2145+
inflight_htlc_msat: 0,
2146+
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000 },
2147+
};
2148+
2149+
if let Some(chan) = network_graph.channels.write().unwrap().get_mut(&42) {
2150+
chan.capacity_sats = Some(1_024);
2151+
}
2152+
2153+
let params = ProbabilisticScoringParameters {
2154+
anti_probing_penalty_multiplier_msat: 10_000,
2155+
..ProbabilisticScoringParameters::zero_penalty()
2156+
};
2157+
let scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
2158+
2159+
// Check we receive a negligible penalty for a low htlc_maximum_msat.
2160+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 34);
2161+
2162+
// Now update the htlc_maximum_msat
2163+
let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
2164+
let secp_ctx = Secp256k1::new();
2165+
let unsigned_update = UnsignedChannelUpdate {
2166+
chain_hash: genesis_hash,
2167+
short_channel_id: 42,
2168+
timestamp: 101,
2169+
flags: 0,
2170+
cltv_expiry_delta: 18,
2171+
htlc_minimum_msat: 0,
2172+
htlc_maximum_msat: OptionalField::Present(1_024_000),
2173+
fee_base_msat: 1,
2174+
fee_proportional_millionths: 0,
2175+
excess_data: Vec::new(),
2176+
};
2177+
let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_update.encode()[..])[..]);
2178+
let signed_update = ChannelUpdate {
2179+
signature: secp_ctx.sign_ecdsa(&msghash, &source_privkey()),
2180+
contents: unsigned_update,
2181+
};
2182+
network_graph.update_channel(&signed_update).unwrap();
2183+
2184+
// Check we receive a sensible penalty for htlc_maximum_msat == channel_capacity.
2185+
// -log(1/1_024_000) * 10_000 = 60_102
2186+
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 60_102);
2187+
}
21362188
}

0 commit comments

Comments
 (0)