diff --git a/crates/lxmf-core/src/handlers.rs b/crates/lxmf-core/src/handlers.rs index 16f457a..e1db28d 100644 --- a/crates/lxmf-core/src/handlers.rs +++ b/crates/lxmf-core/src/handlers.rs @@ -66,7 +66,8 @@ impl PnLimit { } } - fn to_value(self) -> rmpv::Value { + /// The msgpack form Python packs: an int or a float, whichever is held. + pub fn to_value(self) -> rmpv::Value { match self { Self::Int(n) => rmpv::Value::from(n), Self::Float(f) => rmpv::Value::F64(f), diff --git a/crates/lxmf-core/src/persist.rs b/crates/lxmf-core/src/persist.rs index 05df789..d41c62e 100644 --- a/crates/lxmf-core/src/persist.rs +++ b/crates/lxmf-core/src/persist.rs @@ -37,7 +37,7 @@ const CLAIM_PROCESSED: u8 = 1; const CLAIM_RECORD_LEN: usize = 1 + 32 + 8; /// Durably append a claim (`processed = false` for delivered IDs). -pub fn append_delivery_claim( +pub(crate) fn append_delivery_claim( dir: &Path, id: &PropagationTransientId, processed: bool, @@ -74,7 +74,7 @@ fn open_claims_for_append(path: &Path, create: bool) -> io::Result { /// Read appended claims as (delivered, processed). A torn trailing record is ignored. #[allow(clippy::type_complexity)] -pub fn load_delivery_claims( +pub(crate) fn load_delivery_claims( dir: &Path, ) -> io::Result<( Vec<(PropagationTransientId, f64)>, @@ -110,7 +110,7 @@ fn claims_saving(dir: &Path) -> std::path::PathBuf { /// Set the claims appended so far aside for the snapshot being saved; later /// claims start a new file. Claims set aside by an unfinished save are kept. -pub fn rotate_delivery_claims(dir: &Path) -> io::Result<()> { +pub(crate) fn rotate_delivery_claims(dir: &Path) -> io::Result<()> { use std::io::Write; let current = dir.join(DELIVERY_CLAIMS_FILE); let saving = claims_saving(dir); @@ -133,7 +133,7 @@ pub fn rotate_delivery_claims(dir: &Path) -> io::Result<()> { } /// Drop the claims set aside once the caches holding them are saved. -pub fn clear_rotated_delivery_claims(dir: &Path) -> io::Result<()> { +pub(crate) fn clear_rotated_delivery_claims(dir: &Path) -> io::Result<()> { match fs::remove_file(claims_saving(dir)) { Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), _ => Ok(()), diff --git a/crates/lxmf-core/src/router.rs b/crates/lxmf-core/src/router.rs index d5b74d0..1f88cc4 100644 --- a/crates/lxmf-core/src/router.rs +++ b/crates/lxmf-core/src/router.rs @@ -13,6 +13,7 @@ use bytes::Bytes; use tokio::sync::{mpsc, oneshot}; use crate::constants::*; +use crate::handlers::PnLimit; use crate::inbound_resource::{ InboundResourceCancelRequest, InboundResourceEvent, InboundResourceHandle, InboundResourceKey, InboundResourceTracker, @@ -77,9 +78,11 @@ pub struct RouterConfig { pub propagation_enabled: bool, pub autopeer: bool, pub max_peers: usize, - pub propagation_limit_kb: f64, + /// Per-transfer propagation limit in decimal kB, announced as held. + pub propagation_limit_kb: PnLimit, pub delivery_limit_kb: f64, - pub sync_limit_kb: f64, + /// Per-sync propagation limit in decimal kB, announced as held. + pub sync_limit_kb: PnLimit, pub propagation_stamp_cost: f64, pub propagation_stamp_flex: f64, pub stamp_cost: Option, @@ -138,9 +141,9 @@ impl Default for RouterConfig { propagation_enabled: false, autopeer: AUTOPEER, max_peers: MAX_PEERS, - propagation_limit_kb: PROPAGATION_LIMIT as f64, + propagation_limit_kb: PnLimit::Int(PROPAGATION_LIMIT as u64), delivery_limit_kb: DELIVERY_LIMIT as f64, - sync_limit_kb: SYNC_LIMIT as f64, + sync_limit_kb: PnLimit::Int(SYNC_LIMIT as u64), propagation_stamp_cost: PROPAGATION_COST, propagation_stamp_flex: PROPAGATION_COST_FLEX, stamp_cost: None, @@ -339,16 +342,6 @@ pub struct LxmRouter { claims_dir: Option, } -/// `RouterConfig` holds limits as f64: a whole number announces as Python's int -/// default, anything else as the float LXMRouter was given. -fn router_limit(limit_kb: f64) -> crate::handlers::PnLimit { - if limit_kb.fract() == 0.0 && (0.0..=u64::MAX as f64).contains(&limit_kb) { - crate::handlers::PnLimit::Int(limit_kb as u64) - } else { - crate::handlers::PnLimit::Float(limit_kb) - } -} - /// Delivered destinations a router holds for the embedder to retain. const MAX_PENDING_RETAINS: usize = 256; @@ -1768,8 +1761,8 @@ impl LxmRouter { /// Set the advertised per-transfer propagation limit in decimal kB. /// This does not set total storage capacity or update a separate node owner. - pub fn set_propagation_limit(&mut self, limit_kb: f64) { - self.config.propagation_limit_kb = limit_kb; + pub fn set_propagation_limit(&mut self, limit_kb: impl Into) { + self.config.propagation_limit_kb = limit_kb.into(); } pub fn set_stamp_requirements(&mut self, cost: f64, flex: f64) { @@ -1790,8 +1783,8 @@ impl LxmRouter { let mut data = crate::handlers::PropagationNodeAnnounceData::new( self.config.propagation_enabled && !self.config.ext.from_static_only, - router_limit(self.config.propagation_limit_kb), - router_limit(self.config.sync_limit_kb), + self.config.propagation_limit_kb, + self.config.sync_limit_kb, self.config.propagation_stamp_cost, self.config.propagation_stamp_flex, self.config.ext.peering_cost, @@ -2808,8 +2801,8 @@ pub struct PeerStats { pub struct NodeStats { pub uptime: f64, pub delivery_limit: f64, - pub propagation_limit: f64, - pub sync_limit: f64, + pub propagation_limit: PnLimit, + pub sync_limit: PnLimit, pub stamp_cost: f64, pub stamp_flex: f64, pub peering_cost: f64, diff --git a/crates/lxmf-core/src/stamper.rs b/crates/lxmf-core/src/stamper.rs index dcab761..a97d7c0 100644 --- a/crates/lxmf-core/src/stamper.rs +++ b/crates/lxmf-core/src/stamper.rs @@ -7,9 +7,11 @@ use std::sync::atomic::Ordering; +/// The same type as `rns_identity::stamps::StampError`, not an LXMF-specific error. +pub use rns_identity::stamps::StampError; pub use rns_identity::stamps::{ - STAMP_COST_CAP, STAMP_COST_CAP_FP, StampControl, StampError, cost_from_fp, cost_to_fp, - quantize_cost, stamp_value, stamp_workblock, + STAMP_COST_CAP, STAMP_COST_CAP_FP, StampControl, cost_from_fp, cost_to_fp, quantize_cost, + stamp_value, stamp_workblock, }; use crate::constants::{ diff --git a/crates/lxmf-tools/src/commands/lxmd.rs b/crates/lxmf-tools/src/commands/lxmd.rs index 143ea8e..efb6241 100644 --- a/crates/lxmf-tools/src/commands/lxmd.rs +++ b/crates/lxmf-tools/src/commands/lxmd.rs @@ -2806,7 +2806,7 @@ impl LxmdRunner { self.router.set_autopeer(self.config.autopeer); self.router.set_max_peers(self.config.max_peers); self.router - .set_propagation_limit(self.config.propagation_limit_kb.as_f64()); + .set_propagation_limit(self.config.propagation_limit_kb); self.router.set_stamp_requirements( self.config.propagation_stamp_cost, self.config.propagation_stamp_flex, diff --git a/crates/lxmf-tools/src/daemon.rs b/crates/lxmf-tools/src/daemon.rs index 87e0a10..8f776a5 100644 --- a/crates/lxmf-tools/src/daemon.rs +++ b/crates/lxmf-tools/src/daemon.rs @@ -494,9 +494,9 @@ impl DaemonConfig { propagation_enabled: self.propagation_enabled, autopeer: self.autopeer, max_peers: self.max_peers, - propagation_limit_kb: self.propagation_limit_kb.as_f64(), + propagation_limit_kb: self.propagation_limit_kb, delivery_limit_kb: self.delivery_transfer_max_accepted_size, - sync_limit_kb: self.sync_limit_kb.as_f64(), + sync_limit_kb: self.sync_limit_kb, propagation_stamp_cost: self.propagation_stamp_cost, propagation_stamp_flex: self.propagation_stamp_flex, stamp_cost: self.stamp_cost, @@ -1006,8 +1006,8 @@ propagation_transfer_max_accepted_size = 12 assert_eq!(rc.max_peers, 20); assert_eq!(rc.delivery_limit_kb, 1.0); assert_eq!(RouterConfig::default().delivery_limit_kb, 1000.0); - assert_eq!(rc.propagation_limit_kb, 256.0); - assert_eq!(rc.sync_limit_kb, 10_240.0); + assert_eq!(rc.propagation_limit_kb, PnLimit::Int(256)); + assert_eq!(rc.sync_limit_kb, PnLimit::Int(10_240)); assert_eq!(rc.propagation_stamp_cost, 16.0); assert_eq!(rc.propagation_stamp_flex, 3.0); assert_eq!(rc.ext.autopeer_maxdepth, AUTOPEER_MAXDEPTH); @@ -1173,6 +1173,20 @@ from_static_only = yes } } + #[test] + fn configured_whole_limits_reach_the_router_as_floats() { + let config = rns_runtime::config::Config::parse( + "[propagation]\npropagation_message_max_accepted_size = 300\npropagation_sync_max_accepted_size = 900\n", + ) + .unwrap(); + let rc = DaemonConfig::from_config(&config) + .unwrap() + .to_router_config(); + + assert_eq!(rc.propagation_limit_kb, PnLimit::Float(300.0)); + assert_eq!(rc.sync_limit_kb, PnLimit::Float(900.0)); + } + #[test] fn fractional_delivery_limit_survives_daemon_and_router_config() { let config = rns_runtime::config::Config::parse( diff --git a/crates/lxmf-tools/src/lxmd_control.rs b/crates/lxmf-tools/src/lxmd_control.rs index 81d4aee..87e4222 100644 --- a/crates/lxmf-tools/src/lxmd_control.rs +++ b/crates/lxmf-tools/src/lxmd_control.rs @@ -69,15 +69,6 @@ pub async fn query_control( .await } -/// lxmd limits are integers by default and `as_float` values when configured. -fn kilobytes_value(kilobytes: f64) -> Value { - if kilobytes.fract() == 0.0 && kilobytes.abs() < i64::MAX as f64 { - Value::from(kilobytes as i64) - } else { - Value::F64(kilobytes) - } -} - /// Python adds the timeout to `time.time()`: a negative one has already /// expired, and an infinite or NaN one never does. fn control_deadline(timeout_secs: f64) -> Duration { @@ -322,11 +313,11 @@ pub fn encode_router_control_stats( ), ( Value::String("propagation_limit".into()), - kilobytes_value(router.config.propagation_limit_kb), + router.config.propagation_limit_kb.to_value(), ), ( Value::String("sync_limit".into()), - kilobytes_value(router.config.sync_limit_kb), + router.config.sync_limit_kb.to_value(), ), ( Value::String("target_stamp_cost".into()),