From ee9190db0fb55ffa39f38ba47adc13e1dbe72985 Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:03:45 -0700 Subject: [PATCH] runtime: make a destination's stamp ahead of its first announce --- api/snapshots/rns-runtime.txt | 6 + api/stability.json | 4 +- crates/rns-runtime/src/destination_stamper.rs | 108 ++++++++++++++++++ 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/api/snapshots/rns-runtime.txt b/api/snapshots/rns-runtime.txt index a5a5656..2164a4c 100644 --- a/api/snapshots/rns-runtime.txt +++ b/api/snapshots/rns-runtime.txt @@ -266,12 +266,18 @@ impl core::default::Default for rns_runtime::destination_stamper::Destination pub fn rns_runtime::destination_stamper::DestinationStamps::default() -> Self impl core::ops::drop::Drop for rns_runtime::destination_stamper::DestinationStamps pub fn rns_runtime::destination_stamper::DestinationStamps::drop(&mut self) +pub struct rns_runtime::destination_stamper::PreparedStamp +impl rns_runtime::destination_stamper::PreparedStamp +pub async fn rns_runtime::destination_stamper::PreparedStamp::ready(self) -> core::option::Option +impl core::ops::drop::Drop for rns_runtime::destination_stamper::PreparedStamp +pub fn rns_runtime::destination_stamper::PreparedStamp::drop(&mut self) pub struct rns_runtime::destination_stamper::StampOutcome pub rns_runtime::destination_stamper::StampOutcome::stamp: core::option::Option pub async fn rns_runtime::destination_stamper::announce_packet(&mut rns_identity::destination::Destination, &rns_identity::identity::Identity, core::option::Option<&[u8]>, core::option::Option<&[u8; 32]>, bool, core::option::Option<&[u8]>) -> core::result::Result>, rns_identity::destination::DestinationError> pub fn rns_runtime::destination_stamper::configure(rns_transport::stamps::StampPolicy, core::option::Option) pub fn rns_runtime::destination_stamper::load_stored_stamp(&mut rns_identity::destination::Destination, &rns_identity::identity::Identity) pub fn rns_runtime::destination_stamper::policy() -> rns_transport::stamps::StampPolicy +pub fn rns_runtime::destination_stamper::prepare(&rns_identity::destination::Destination, &rns_identity::identity::Identity) -> core::option::Option pub type rns_runtime::destination_stamper::DestinationStamp = ([u8; 14], f64) pub mod rns_runtime::interface_factory pub enum rns_runtime::interface_factory::InterfaceConfig diff --git a/api/stability.json b/api/stability.json index 813dc8e..37dea29 100644 --- a/api/stability.json +++ b/api/stability.json @@ -87,8 +87,8 @@ "tier": "provisional", "compatibility": "reviewed-snapshot", "snapshot": "api/snapshots/rns-runtime.txt", - "itemCount": 2455, - "sha256": "a524be641056e26ad32a59742a71efe64e4cdb79ab81191b50519595a5cbe524" + "itemCount": 2461, + "sha256": "5080d5423276206703c0b547c334e8996a12fd912da4190e6325040a4260eade" }, { "name": "rns-tools", diff --git a/crates/rns-runtime/src/destination_stamper.rs b/crates/rns-runtime/src/destination_stamper.rs index e46bd5d..cd32f4d 100644 --- a/crates/rns-runtime/src/destination_stamper.rs +++ b/crates/rns-runtime/src/destination_stamper.rs @@ -236,6 +236,58 @@ fn stamp_for(job: &Job) -> Option { Some(generated) } +/// A destination stamp being made ahead of the destination's first announce. +/// Dropping it cancels a job that has not finished. +pub struct PreparedStamp { + rx: mpsc::UnboundedReceiver, + cancel: Arc, +} + +impl PreparedStamp { + /// The stamp once made and saved; `None` when generation failed or was cancelled. + pub async fn ready(mut self) -> Option { + self.rx.recv().await.and_then(|outcome| outcome.stamp) + } +} + +impl Drop for PreparedStamp { + fn drop(&mut self) { + self.cancel.store(true, Ordering::Relaxed); + } +} + +/// Make and save `destination`'s stamp on the stamper worker without +/// announcing, so its first announce loads it instead of waiting. `None` when +/// the policy does not stamp, the destination already holds a stamp at its +/// target, or the queue is full. +pub fn prepare(destination: &Destination, identity: &Identity) -> Option { + let Settings { + policy, + storage_dir, + } = settings(); + if !policy.stamp_destinations { + return None; + } + let target = destination.target_value(policy.destination_value); + if destination.destination_stamp_meets(target) { + return None; + } + let (reply, rx) = mpsc::unbounded_channel(); + let cancel = Arc::new(AtomicBool::new(false)); + let job = Job { + owner: NEXT_OWNER.fetch_add(1, Ordering::Relaxed), + destination_hash: destination.hash, + material: destination + .stamp_material(&identity.get_public_key()) + .to_vec(), + target, + storage_dir, + cancel: cancel.clone(), + reply, + }; + submit(job, Priority::Announce).then_some(PreparedStamp { rx, cancel }) +} + /// What to do with a requested announce. pub enum Admission { /// Build and send now, with this extension when stamping is on. @@ -544,4 +596,60 @@ mod tests { assert_eq!(ext.stamp, [7; NETWORK_STAMP_SIZE]); assert_eq!(ext.lr_cost, 12.5); } + + fn quick_policy() -> StampPolicy { + StampPolicy { + destination_cost: 6.0, + destination_value: 8.0, + ..StampPolicy::default() + } + } + + fn scratch_dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("rns-prepare-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn prepare_saves_a_stamp_the_first_announce_loads_without_waiting() { + let dir = scratch_dir("saves"); + let _settings = test_settings::blocking_exclusive(quick_policy(), Some(dir.clone())); + let identity = Identity::new(); + let app = "test.prepare.saves"; + let destination = + Destination::new(Some(&identity), Direction::In, DestType::Single, app).unwrap(); + let prepared = prepare(&destination, &identity).expect("a stamp to make"); + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + let (_, value) = runtime.block_on(prepared.ready()).expect("made"); + assert!(value >= 8.0); + + // A destination built after it loads the saved stamp and announces at once. + let mut later = + Destination::new(Some(&identity), Direction::In, DestType::Single, app).unwrap(); + load_stored_stamp(&mut later, &identity); + assert!(matches!( + DestinationStamps::new().admit(&later, &identity, false, ()), + Admission::Ready((), Some(_)) + )); + assert!(prepare(&later, &identity).is_none(), "nothing left to make"); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn prepare_does_nothing_when_destinations_are_not_stamped() { + let _settings = test_settings::blocking_unstamped(); + let identity = Identity::new(); + let destination = Destination::new( + Some(&identity), + Direction::In, + DestType::Single, + "test.prepare.unstamped", + ) + .unwrap(); + assert!(prepare(&destination, &identity).is_none()); + } }