diff --git a/crates/rns-transport/src/discovery/receiver.rs b/crates/rns-transport/src/discovery/receiver.rs index 22386b3..61eaaae 100644 --- a/crates/rns-transport/src/discovery/receiver.rs +++ b/crates/rns-transport/src/discovery/receiver.rs @@ -411,9 +411,9 @@ impl ReceiverConfig { stamp: &[u8], ) -> Result { let infohash = rns_crypto::sha::full_hash(packed_info); - if !self.stamper.valid(&infohash, stamp, self.required_value) { + let Some(stamp_value) = self.stamper.validate(&infohash, stamp, self.required_value) else { return Err(Reason::StampInvalid); - } + }; let info: DiscoveryInfo = match app_data::decode_info(packed_info) { Ok(i) => i, @@ -426,7 +426,7 @@ impl ReceiverConfig { Ok(ValidatedMaterial { info, stamp: stamp.to_vec(), - stamp_value: self.stamper.value(&infohash, stamp), + stamp_value, }) } @@ -567,17 +567,17 @@ impl ReceiverState { } let infohash = rns_crypto::sha::full_hash(packed_info); - if !self - .config - .stamper - .valid(&infohash, stamp, self.config.required_value) - { + let Some(stamp_value) = + self.config + .stamper + .validate(&infohash, stamp, self.config.required_value) + else { let evicted = lock_unpoisoned(&self.caches).insert_invalid(key); if evicted { ReceiverMetrics::increment(&self.metrics.invalid_cache_evictions); } return Err(Reason::StampInvalid); - } + }; let info = match app_data::decode_info(packed_info) { Ok(info) => info, @@ -589,7 +589,7 @@ impl ReceiverState { let material = ValidatedMaterial { info, stamp: stamp.to_vec(), - stamp_value: self.config.stamper.value(&infohash, stamp), + stamp_value, }; let evicted = lock_unpoisoned(&self.caches).insert_valid(key, material.clone()); if evicted { @@ -874,6 +874,32 @@ mod tests { assert_eq!(listed[0].network_id, [0x11; 16]); } + #[test] + fn accepts_a_non_expanded_stamp_with_its_value() { + let dir = tmpdir("non_expanded"); + let store = Arc::new(DiscoveryStore::open(&dir).unwrap()); + let info = sample_info(); + let infohash = encode_info(&info).unwrap().infohash; + let (stamp, value) = rns_identity::stamps::generate_stamp( + &infohash, + 14.0, + 0, + STAMP_SIZE, + &StampControl::default(), + ) + .unwrap() + .unwrap(); + let (blob, _) = stamped_blob(&info, &stamp); + let cfg = make_cfg( + Arc::new(crate::discovery::stamper::RnsDiscoveryStamper), + store.clone(), + None, + None, + ); + assert_eq!(cfg.process_event(&event_with(blob)), Outcome::Accepted); + assert_eq!(store.list(None).unwrap()[0].stamp_value, value); + } + #[test] fn rejects_invalid_stamp() { let dir = tmpdir("badstamp"); diff --git a/crates/rns-transport/src/discovery/stamper.rs b/crates/rns-transport/src/discovery/stamper.rs index b26fccd..763a86e 100644 --- a/crates/rns-transport/src/discovery/stamper.rs +++ b/crates/rns-transport/src/discovery/stamper.rs @@ -1,10 +1,12 @@ //! Proof-of-work stamper for discovery announces. //! //! [`RnsDiscoveryStamper`] is the stamp construction upstream uses -//! (`self.stamper = RNS.Stamps`, Discovery.py at 1.6.0): a 32-byte -//! stamp over a 20-round workblock of the info hash, with fractional values. -//! The trait remains so embedders can substitute an implementation; methods -//! are synchronous and callers choose where expensive generation runs. +//! (`self.stamper = RNS.Stamps`, Discovery.py at 1.6.1): a 32-byte stamp over +//! a 20-round workblock of the info hash, with fractional values. Receivers +//! also accept a stamp over the info hash itself, the non-expanded form +//! upstream senders move to. The trait remains so embedders can substitute an +//! implementation; methods are synchronous and callers choose where expensive +//! generation runs. use std::num::NonZeroUsize; @@ -37,6 +39,14 @@ pub trait DiscoveryStamper: Send + Sync { /// Whether the stamp meets `required_value` for this info hash. fn valid(&self, infohash: &[u8; 32], stamp: &[u8], required_value: f64) -> bool; + + /// The stamp's value when it meets `required_value`, else `None`. The + /// value can depend on `required_value` when the stamper accepts more than + /// one workblock. + fn validate(&self, infohash: &[u8; 32], stamp: &[u8], required_value: f64) -> Option { + self.valid(infohash, stamp, required_value) + .then(|| self.value(infohash, stamp)) + } } /// Upstream `RNS.Stamps` discovery construction, built on @@ -93,12 +103,26 @@ impl DiscoveryStamper for RnsDiscoveryStamper { } } + /// Value over the 20-round workblock, the form this stamper generates. fn value(&self, infohash: &[u8; 32], stamp: &[u8]) -> f64 { stamps::stamp_value(&Self::workblock(infohash), stamp) } fn valid(&self, infohash: &[u8; 32], stamp: &[u8], required_value: f64) -> bool { - stamps::stamp_valid(stamp, required_value, &Self::workblock(infohash)).unwrap_or(false) + self.validate(infohash, stamp, required_value).is_some() + } + + /// Upstream 1.6.1 order: the non-expanded workblock, then the 20-round + /// one; the value comes from the workblock that passed. + fn validate(&self, infohash: &[u8; 32], stamp: &[u8], required_value: f64) -> Option { + let passes = |workblock: &[u8]| { + stamps::stamp_valid(stamp, required_value, workblock).unwrap_or(false) + }; + if passes(infohash) { + return Some(stamps::stamp_value(infohash, stamp)); + } + let workblock = Self::workblock(infohash); + passes(&workblock).then(|| stamps::stamp_value(&workblock, stamp)) } } @@ -192,6 +216,30 @@ mod tests { assert_eq!(generation_threads(13.999).get(), expected.max(1)); } + // Upstream 1.6.1 `InterfaceAnnounceHandler.received_announce` verdicts and + // values; generator in the K1 hand-back (gen_discovery_ne.py). + const VECTOR_INFOHASH: &str = + "bbfdbadfc3ed56d93f63b737078eed1ec59bc77d3096864f19f0689a7cc36964"; + const VECTOR_NE_STAMP: &str = + "2dbee58b18b27cbb784a32d2c6c72c09b9ba53f3a49894e093473980ea4bf25e"; + const VECTOR_EXPANDED_STAMP: &str = + "009b03b8a5195ee423480e7194f83441e875bbc67d96e46635be9c7c9b1143fe"; + + #[test] + fn both_workblock_forms_validate_as_upstream_reports() { + let ih: [u8; 32] = hex::decode(VECTOR_INFOHASH).unwrap().try_into().unwrap(); + let ne = hex::decode(VECTOR_NE_STAMP).unwrap(); + let expanded = hex::decode(VECTOR_EXPANDED_STAMP).unwrap(); + let s = RnsDiscoveryStamper; + assert_eq!(s.validate(&ih, &ne, 8.0), Some(8.265625)); + assert_eq!(s.validate(&ih, &expanded, 8.0), Some(12.15625)); + // At cost 0 the non-expanded check always passes first. + assert_eq!(s.validate(&ih, &expanded, 0.0), Some(2.29296875)); + assert_eq!(s.validate(&ih, &ne, 8.26953125), None); + assert!(s.valid(&ih, &ne, 8.0)); + assert!(!s.valid(&ih, &ne, 8.26953125)); + } + #[test] fn short_or_foreign_stamps_do_not_validate_at_a_real_cost() { let ih = infohash(b"foreign");