From 3bb885cd632d57ec33f8d95da287fc65dbb8b5f3 Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:31:28 -0700 Subject: [PATCH] runtime: configure the destination stamper before the network starts --- api/snapshots/rns-runtime.txt | 1 + api/stability.json | 4 +- crates/rns-runtime/src/destination_stamper.rs | 45 +++++++++++++++++++ crates/rns-runtime/src/reticulum.rs | 25 +++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/api/snapshots/rns-runtime.txt b/api/snapshots/rns-runtime.txt index 2164a4c..d75be93 100644 --- a/api/snapshots/rns-runtime.txt +++ b/api/snapshots/rns-runtime.txt @@ -2067,6 +2067,7 @@ pub rns_runtime::reticulum::StartupRNodeRuntime::interface_id: rns_interface::tr pub rns_runtime::reticulum::StartupRNodeRuntime::observer: rns_runtime::reticulum::RNodeRuntimeObserver pub const rns_runtime::reticulum::BLACKHOLE_LINK_COST: f64 pub const rns_runtime::reticulum::DEFAULT_ANNOUNCE_SUBSCRIPTION_CAPACITY: usize +pub fn rns_runtime::reticulum::configure_destination_stamper(core::option::Option<&str>, &rns_runtime::reticulum::InitOptions) -> core::result::Result<(), rns_runtime::reticulum::ReticulumError> pub fn rns_runtime::reticulum::get_instance() -> core::option::Option<&'static rns_runtime::reticulum::ReticulumHandle> pub async fn rns_runtime::reticulum::init(core::option::Option<&str>, core::option::Option, rns_runtime::lifecycle::ShutdownSignal, alloc::sync::Arc) -> core::result::Result pub async fn rns_runtime::reticulum::init_with_options(core::option::Option<&str>, core::option::Option, rns_runtime::lifecycle::ShutdownSignal, alloc::sync::Arc, rns_runtime::reticulum::InitOptions) -> core::result::Result diff --git a/api/stability.json b/api/stability.json index 37dea29..ef706b5 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": 2461, - "sha256": "5080d5423276206703c0b547c334e8996a12fd912da4190e6325040a4260eade" + "itemCount": 2462, + "sha256": "d704669b199ab5f476134deb1396d298939085e3bdd0f77195b1cf10652ef703" }, { "name": "rns-tools", diff --git a/crates/rns-runtime/src/destination_stamper.rs b/crates/rns-runtime/src/destination_stamper.rs index cd32f4d..b259674 100644 --- a/crates/rns-runtime/src/destination_stamper.rs +++ b/crates/rns-runtime/src/destination_stamper.rs @@ -639,6 +639,51 @@ mod tests { let _ = std::fs::remove_dir_all(dir); } + #[test] + fn a_stamp_made_before_the_network_starts_is_saved_where_init_loads_it() { + let _settings = test_settings::blocking_unstamped(); + let dir = scratch_dir("early"); + std::fs::write( + dir.join("config"), + "[stamps]\n destination_value = 8\n destination_cost = 6\n", + ) + .unwrap(); + let configdir = dir.to_str().unwrap(); + crate::reticulum::configure_destination_stamper( + Some(configdir), + &crate::reticulum::InitOptions::default(), + ) + .unwrap(); + assert_eq!(policy().destination_value, 8.0); + let identity = Identity::new(); + let app = "test.prepare.early"; + let destination = + Destination::new(Some(&identity), Direction::In, DestType::Single, app).unwrap(); + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + runtime + .block_on(prepare(&destination, &identity).unwrap().ready()) + .unwrap(); + let material = destination.stamp_material(&identity.get_public_key()); + assert!( + load_destination_stamp(&dir.join("storage"), &destination.hash, &material, 6.0) + .is_some() + ); + + // Without a config the defaults apply, and none is written. + let bare = scratch_dir("bare"); + crate::reticulum::configure_destination_stamper( + bare.to_str(), + &crate::reticulum::InitOptions::default(), + ) + .unwrap(); + assert_eq!(policy().destination_value, 25.0); + assert!(!bare.join("config").exists()); + let _ = std::fs::remove_dir_all(dir); + let _ = std::fs::remove_dir_all(bare); + } + #[test] fn prepare_does_nothing_when_destinations_are_not_stamped() { let _settings = test_settings::blocking_unstamped(); diff --git a/crates/rns-runtime/src/reticulum.rs b/crates/rns-runtime/src/reticulum.rs index 13777b6..d95bc51 100644 --- a/crates/rns-runtime/src/reticulum.rs +++ b/crates/rns-runtime/src/reticulum.rs @@ -11278,6 +11278,31 @@ fn parse_blocked_ips(lines: &[String]) -> Vec { lines.iter().filter_map(|line| line.parse().ok()).collect() } +/// Configure the destination stamper as `init` would for this config +/// directory, without starting anything, so a stamp made before the network +/// starts is saved where `init` loads it. A missing config reads as the default. +pub fn configure_destination_stamper( + configdir: Option<&str>, + options: &InitOptions, +) -> Result<(), ReticulumError> { + let config_dir = resolve_config_dir(configdir); + let config_path = config_dir.join("config"); + let config = if config_path.exists() { + Config::from_file(&config_path) + } else { + Config::parse(Config::default_config()) + } + .map_err(ReticulumError::Config)?; + let mut rc = ReticulumConfig::try_from_config(&config).map_err(ReticulumError::Config)?; + rc.apply_init_options(options); + rc.stamp_workers.install(); + crate::destination_stamper::configure( + rc.stamps, + Some(StoragePaths::from_config_dir(&config_dir).storage_dir), + ); + Ok(()) +} + fn load_or_create_config(path: &Path) -> Result<(Config, bool), ReticulumError> { if path.exists() { Config::from_file(path)