From b61724d1f1406afb678a857079ebb84ef641766e Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 10 Jun 2026 00:21:41 -0600 Subject: [PATCH] router: enforce max peering cost at announce ingest and bound stamp searches --- crates/lxmf-core/src/peer.rs | 30 +++++++++++++++++------- crates/lxmf-core/src/router.rs | 41 +++++++++++++++++++++++++++++++++ crates/lxmf-core/src/stamper.rs | 37 +++++++++++++++++++++++++++-- 3 files changed, 98 insertions(+), 10 deletions(-) diff --git a/crates/lxmf-core/src/peer.rs b/crates/lxmf-core/src/peer.rs index 4746edf..ace8471 100644 --- a/crates/lxmf-core/src/peer.rs +++ b/crates/lxmf-core/src/peer.rs @@ -291,18 +291,18 @@ impl LxmPeer { key_material.extend_from_slice(peer_identity_hash); key_material.extend_from_slice(our_identity_hash); - let workblock = crate::stamper::stamp_workblock( + // Bounded search (stamper::stamp_iteration_cap): an announce-supplied + // hostile peering_cost can no longer pin this thread forever. + match crate::stamper::generate_stamp( &key_material, + self.peering_cost, crate::constants::STAMP_WORKBLOCK_EXPAND_ROUNDS_PEERING, - ); - - loop { - let stamp: [u8; 32] = crate::stamper::rand_bytes(); - if crate::stamper::stamp_valid(&stamp, self.peering_cost, &workblock) { - let value = crate::stamper::stamp_value(&workblock, &stamp); + ) { + Some((stamp, value)) => { self.peering_key = Some((stamp, value)); - return true; + true } + None => false, } } @@ -467,6 +467,20 @@ mod tests { assert_eq!(peer.minimum_accepted_stamp_cost(), 0); } + /// T0-4: an absurd announce-supplied peering cost must fail the bounded + /// key search instead of spinning forever; sane costs still succeed. + #[test] + fn test_generate_peering_key_capped() { + let mut peer = LxmPeer::new([0xAA; 16]); + peer.peering_cost = 255; + assert!(!peer.generate_peering_key(&[0xBB; 16], &[0xCC; 16])); + assert!(peer.peering_key.is_none()); + + peer.peering_cost = 4; + assert!(peer.generate_peering_key(&[0xBB; 16], &[0xCC; 16])); + assert!(peer.peering_key.is_some()); + } + #[test] fn test_mark_unreachable() { let mut peer = LxmPeer::new([0; 16]); diff --git a/crates/lxmf-core/src/router.rs b/crates/lxmf-core/src/router.rs index c3b6b68..1f3be29 100644 --- a/crates/lxmf-core/src/router.rs +++ b/crates/lxmf-core/src/router.rs @@ -949,6 +949,15 @@ impl LxmRouter { if !self.config.autopeer { return false; } + // Python LXMRouter.peer() (LXMRouter.py:1896-1901): a peering cost + // above our accepted maximum refuses the peering — and breaks an + // existing one — before any PoW could be attempted. + if peering_cost.unwrap_or(0) > self.config.ext.max_peering_cost { + if self.peers.contains_key(&destination_hash) { + self.unpeer(&destination_hash); + } + return false; + } if self.peers.contains_key(&destination_hash) { return false; } @@ -3037,6 +3046,38 @@ mod tests { })); } + /// T0-4: peering cost above `max_peering_cost` refuses peering, and an + /// existing peering breaks when the peer raises its cost beyond the cap + /// (Python LXMRouter.py:1896-1901). + #[test] + fn test_autopeer_enforces_max_peering_cost() { + let mut router = LxmRouter::new(RouterConfig::default()); + let max = router.config.ext.max_peering_cost; + + let candidate = |cost: Option| AutopeerCandidate { + destination_hash: [0xAA; 16], + timebase: 1000.0, + transfer_limit: Some(256.0), + sync_limit: None, + stamp_cost: Some(16), + stamp_flexibility: Some(3), + peering_cost: cost, + hops: Some(2), + }; + + // Over-cost announce: refused outright. + assert!(!router.autopeer(candidate(Some(max + 1)))); + assert!(router.peers.is_empty()); + + // At-cost announce: accepted. + assert!(router.autopeer(candidate(Some(max)))); + assert_eq!(router.peers.len(), 1); + + // Existing peer raising its cost beyond the cap: peering breaks. + assert!(!router.autopeer(candidate(Some(max + 1)))); + assert!(router.peers.is_empty(), "over-cost announce must unpeer"); + } + #[test] fn test_autopeer_respects_configured_maxdepth() { let mut router = LxmRouter::new(RouterConfig { diff --git a/crates/lxmf-core/src/stamper.rs b/crates/lxmf-core/src/stamper.rs index eb71b07..8847049 100644 --- a/crates/lxmf-core/src/stamper.rs +++ b/crates/lxmf-core/src/stamper.rs @@ -80,24 +80,40 @@ pub fn stamp_valid(stamp: &[u8; 32], cost: u8, workblock: &[u8]) -> bool { stamp_value(workblock, stamp) >= cost as u32 } -/// Single-threaded brute-force stamp search. Blocks until a valid stamp is found. +/// Give-up bound for stamp searches: 64× the expected `2^cost` work, so the +/// failure probability for an honest cost is ~e^-64 while a hostile cost can +/// no longer pin a thread forever. Costs ≥ 58 (cap would overflow u64) are +/// unsatisfiable on real hardware and fail immediately. Python has no cap and +/// blocks until success — deliberate divergence, see fix-registry. +pub fn stamp_iteration_cap(cost: u8) -> Option { + if cost >= 58 { + return None; + } + Some(64u64 << cost) +} + +/// Single-threaded brute-force stamp search. Bounded by +/// [`stamp_iteration_cap`]; returns `None` when the cap is exhausted or the +/// cost is unsatisfiable. pub fn generate_stamp(material: &[u8], cost: u8, expand_rounds: usize) -> Option<([u8; 32], u32)> { if cost == 0 { return Some(([0u8; 32], 0)); } + let max_iterations = stamp_iteration_cap(cost)?; let workblock = stamp_workblock(material, expand_rounds); let mut base_hasher = Sha256::new(); base_hasher.update(&workblock); let mut rng = rand::thread_rng(); - loop { + for _ in 0..max_iterations { let stamp = rand_bytes_from(&mut rng); let value = stamp_value_from_base(&base_hasher, &stamp); if value >= cost as u32 { return Some((stamp, value)); } } + None } /// Stamp search with a configurable iteration limit (for tests). @@ -335,6 +351,23 @@ mod tests { assert_eq!(value, 0); } + /// T0-4: the stamp search is bounded — an unsatisfiable (hostile) cost + /// returns `None` immediately instead of pinning the thread forever. + #[test] + fn test_generate_stamp_capped_for_hostile_cost() { + assert_eq!(stamp_iteration_cap(1), Some(128)); + assert_eq!(stamp_iteration_cap(26), Some(64u64 << 26)); + assert!(stamp_iteration_cap(57).is_some()); + assert_eq!(stamp_iteration_cap(58), None); + assert_eq!(stamp_iteration_cap(255), None); + + let id = sha256(b"hostile cost"); + // Cost 255 must fail fast (cap is None — no workblock spin at all). + assert!(generate_stamp(&id, 255, 20).is_none()); + // Sane costs still succeed. + assert!(generate_stamp(&id, 4, 20).is_some()); + } + #[test] fn test_generate_and_validate_stamp() { let id = sha256(b"test message for stamping");