mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-22 23:10:33 +00:00
rngit: stamp_value and link_cost for both destinations
This commit is contained in:
1 parent
09bb5c84dc
commit
a9d534add4
4 files changed
+126
-4
No files matched your search
@@ -212,6 +212,8 @@ async fn start_page_node(
|
||||
Destination::hash_from_name_and_identity(rns_git::pages::APP_NAME, Some(&identity.hash));
|
||||
let options = DestinationRuntimeOptions {
|
||||
endpoint_dispatch: Some(handle.link_endpoint_dispatch_handle()),
|
||||
stamp_value: node.options.stamp_value.filter(|value| *value > 0.0),
|
||||
link_cost: Some(node.options.link_cost),
|
||||
..DestinationRuntimeOptions::default()
|
||||
};
|
||||
let registered = match handle
|
||||
@@ -1238,6 +1240,8 @@ async fn main() {
|
||||
|
||||
let options = DestinationRuntimeOptions {
|
||||
endpoint_dispatch: Some(handle.link_endpoint_dispatch_handle()),
|
||||
stamp_value: config.stamp_value.filter(|value| *value > 0.0),
|
||||
link_cost: Some(config.link_cost),
|
||||
..DestinationRuntimeOptions::default()
|
||||
};
|
||||
let page_identity = identity.clone();
|
||||
|
||||
@@ -111,10 +111,14 @@ const PLAIN_TEMPLATES: [&str; 12] = [
|
||||
];
|
||||
|
||||
/// The `[pages]` keys `NomadNetworkNode.__init__` reads (`pages.py`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct PageOptions {
|
||||
pub use_nerdfonts: bool,
|
||||
pub media_conversion: bool,
|
||||
/// Quantized; applied to the node destination only when above 0.
|
||||
pub stamp_value: Option<f64>,
|
||||
/// Quantized announced link request cost.
|
||||
pub link_cost: f64,
|
||||
}
|
||||
|
||||
impl Default for PageOptions {
|
||||
@@ -122,13 +126,15 @@ impl Default for PageOptions {
|
||||
Self {
|
||||
use_nerdfonts: true,
|
||||
media_conversion: true,
|
||||
stamp_value: None,
|
||||
link_cost: crate::server::config::DEFAULT_LINK_COST,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PageOptions {
|
||||
/// Read `unicode_icons` and `media_conversion` from the node config text;
|
||||
/// a value `as_bool` rejects is an error, as upstream raises.
|
||||
/// Read the `[pages]` keys from the node config text; a value `as_bool`
|
||||
/// or `as_float` rejects is an error, as upstream raises.
|
||||
pub fn from_config_text(text: &str) -> Result<Self, rns_runtime::config::ConfigError> {
|
||||
let mut options = Self::default();
|
||||
let config = rns_runtime::config::Config::parse(text)?;
|
||||
@@ -150,6 +156,16 @@ impl PageOptions {
|
||||
if section.has("media_conversion") {
|
||||
options.media_conversion = as_bool("media_conversion")?;
|
||||
}
|
||||
if section.has("link_cost") {
|
||||
options.link_cost = crate::server::config::as_cost(section, "pages", "link_cost")?;
|
||||
}
|
||||
if section.has("stamp_value") {
|
||||
options.stamp_value = Some(crate::server::config::as_cost(
|
||||
section,
|
||||
"pages",
|
||||
"stamp_value",
|
||||
)?);
|
||||
}
|
||||
Ok(options)
|
||||
}
|
||||
}
|
||||
@@ -798,3 +814,22 @@ pub(crate) fn field_num(value: &Value, key: &str) -> f64 {
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::PageOptions;
|
||||
|
||||
#[test]
|
||||
fn page_stamp_keys_follow_upstream() {
|
||||
let options = PageOptions::from_config_text("[pages]\nserve_nomadnet = yes\n").unwrap();
|
||||
assert_eq!((options.stamp_value, options.link_cost), (None, 18.0));
|
||||
let options =
|
||||
PageOptions::from_config_text("[pages]\nstamp_value = 27.5\nlink_cost = 18.7\n")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(options.stamp_value, options.link_cost),
|
||||
(Some(27.5), 4787.0 / 256.0)
|
||||
);
|
||||
assert!(PageOptions::from_config_text("[pages]\nlink_cost = x\n").is_err());
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@ use super::py_len;
|
||||
pub const DEFAULT_CONFIG: &str = include_str!("default_config.txt");
|
||||
|
||||
pub const DEFAULT_NODE_NAME: &str = "Anonymous Git Node";
|
||||
/// `DEFAULT_LINK_COST` of both rngit destinations (`server.py`, `pages.py`).
|
||||
pub const DEFAULT_LINK_COST: f64 = 18.0;
|
||||
const LOG_NONE: i64 = -1;
|
||||
const LOG_EXTREME: i64 = 8;
|
||||
|
||||
@@ -40,11 +42,16 @@ impl Default for FetchLimits {
|
||||
}
|
||||
|
||||
/// What `__apply_config` takes from the config file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct NodeConfig {
|
||||
pub node_name: String,
|
||||
/// Seconds; 0 never announces.
|
||||
pub announce_interval: i64,
|
||||
/// `[rngit] stamp_value`, quantized; applied to the repositories
|
||||
/// destination only when above 0, as upstream tests it for truth.
|
||||
pub stamp_value: Option<f64>,
|
||||
/// `[rngit] link_cost`, quantized: the announced link request cost.
|
||||
pub link_cost: f64,
|
||||
/// Seconds; 0 disables periodic mirror sync.
|
||||
pub mirror_interval: i64,
|
||||
pub record_stats: bool,
|
||||
@@ -68,6 +75,8 @@ impl Default for NodeConfig {
|
||||
Self {
|
||||
node_name: DEFAULT_NODE_NAME.to_string(),
|
||||
announce_interval: 0,
|
||||
stamp_value: None,
|
||||
link_cost: DEFAULT_LINK_COST,
|
||||
mirror_interval: 24 * 60 * 60,
|
||||
record_stats: false,
|
||||
serve_nomadnet: false,
|
||||
@@ -107,6 +116,15 @@ fn as_limit(section: &ConfigSection, name: &str, key: &str) -> Result<u64, Confi
|
||||
Ok(value.max(1) as u64)
|
||||
}
|
||||
|
||||
/// `Stamps.quantize_cost(section.as_float(key))`, which raises on a value
|
||||
/// `float()` rejects and on NaN or infinity.
|
||||
pub(crate) fn as_cost(section: &ConfigSection, name: &str, key: &str) -> Result<f64, ConfigError> {
|
||||
let value = section
|
||||
.get_float(key)
|
||||
.ok_or_else(|| invalid(name, key, "not a float"))?;
|
||||
rns_identity::stamps::quantize_cost(value).map_err(|e| invalid(name, key, &e.to_string()))
|
||||
}
|
||||
|
||||
/// ConfigObj `as_bool`, which raises on anything but its boolean words.
|
||||
fn as_bool(section: &ConfigSection, name: &str, key: &str) -> Result<bool, ConfigError> {
|
||||
section
|
||||
@@ -154,6 +172,12 @@ impl NodeConfig {
|
||||
if section.has("announce_interval") {
|
||||
node.announce_interval = as_int(section, name, "announce_interval")? * 60;
|
||||
}
|
||||
if section.has("stamp_value") {
|
||||
node.stamp_value = Some(as_cost(section, name, "stamp_value")?);
|
||||
}
|
||||
if section.has("link_cost") {
|
||||
node.link_cost = as_cost(section, name, "link_cost")?;
|
||||
}
|
||||
if section.has("mirror_interval") {
|
||||
node.mirror_interval = (as_int(section, name, "mirror_interval")? * 3600).max(0);
|
||||
}
|
||||
@@ -271,3 +295,42 @@ pub fn expand_user(path: &str) -> PathBuf {
|
||||
_ => PathBuf::from(path),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn stamp_value_and_link_cost_follow_upstream() {
|
||||
let node = NodeConfig::parse("[rngit]\n", 0).unwrap();
|
||||
assert_eq!((node.stamp_value, node.link_cost), (None, 18.0));
|
||||
let node = NodeConfig::parse("[rngit]\nstamp_value = 27.5\nlink_cost = 18.7\n", 0).unwrap();
|
||||
// quantize_cost: 18.7 is 4787/256.
|
||||
assert_eq!(
|
||||
(node.stamp_value, node.link_cost),
|
||||
(Some(27.5), 4787.0 / 256.0)
|
||||
);
|
||||
let node = NodeConfig::parse("[rngit]\nstamp_value = -3\nlink_cost = 150\n", 0).unwrap();
|
||||
assert_eq!((node.stamp_value, node.link_cost), (Some(0.0), 100.0));
|
||||
for body in ["stamp_value = high", "link_cost = nan", "link_cost = inf"] {
|
||||
assert!(
|
||||
NodeConfig::parse(&format!("[rngit]\n{body}\n"), 0).is_err(),
|
||||
"{body}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_config_carries_the_upstream_stamp_lines() {
|
||||
for (section, destination) in [("[rngit]", "repository node"), ("[pages]", "page node")] {
|
||||
let start = DEFAULT_CONFIG.find(section).unwrap();
|
||||
let text = &DEFAULT_CONFIG[start..];
|
||||
assert!(text.contains(&format!(
|
||||
"# You can configure the required link stamp cost for\n# connecting to the {destination}.\n\n# link_cost = 18.0\n"
|
||||
)));
|
||||
}
|
||||
assert_eq!(DEFAULT_CONFIG.matches("# stamp_value = 25.0\n").count(), 2);
|
||||
let node = NodeConfig::parse(DEFAULT_CONFIG, 0).unwrap();
|
||||
assert_eq!((node.stamp_value, node.link_cost), (None, 18.0));
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,16 @@ announce_interval = 360
|
||||
|
||||
# node_name = Anonymous Git Node
|
||||
|
||||
# You can configure the required link stamp cost for
|
||||
# connecting to the repository node.
|
||||
|
||||
# link_cost = 18.0
|
||||
|
||||
# The target stamp value for the repositories destination
|
||||
# can also be configured.
|
||||
|
||||
# stamp_value = 25.0
|
||||
|
||||
# You can enable collecting view, fetch and push statistics
|
||||
# which can be displayed on the stats pages of repositories.
|
||||
# Remember to set the "s" (stats) permission appropriately
|
||||
@@ -140,6 +150,16 @@ internal = rw:9710b86ba12c42d1d8f30f74fe509286
|
||||
|
||||
# serve_nomadnet = no
|
||||
|
||||
# You can configure the required link stamp cost for
|
||||
# connecting to the page node.
|
||||
|
||||
# link_cost = 18.0
|
||||
|
||||
# The target stamp value for the node destination can
|
||||
# also be configured.
|
||||
|
||||
# stamp_value = 25.0
|
||||
|
||||
# It is possible to disable Nerd Font icons and instead
|
||||
# use simpler (but more compatible) unicode icons.
|
||||
|
||||
|
||||
Reference in new issue
Block a user