use core::fmt;
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::auth::AuthToken;
use crate::base64::{self, Base64Error};
use crate::codec::{WireDecode, WireEncode};
use crate::common::node::NodeSpawnSpec;
use crate::error::WireError;
use crate::handshake::limits::NegotiatedLimits;
use crate::ids::{DaemonId, DataflowId, NodeId};
use crate::version::PROTOCOL_VERSION;
pub const ENV_NODE_CONFIG: &str = "ASTRS_NODE_CONFIG";
pub const ENV_RUN_PARENT_PID: &str = "ASTRS_RUN_PARENT_PID";
pub const DEFAULT_ZERO_COPY_THRESHOLD: u64 = 4_096;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
pub struct NodeConfig {
pub protocol: u16,
pub spec: NodeSpawnSpec,
pub daemon: DaemonId,
pub endpoints: Vec<String>,
pub auth: AuthToken,
pub limits: NegotiatedLimits,
pub zero_copy_threshold: u64,
pub shm_broker: Option<String>,
pub working_dir: Option<String>,
pub deterministic: bool,
}
impl NodeConfig {
#[must_use]
pub fn new(spec: NodeSpawnSpec, daemon: DaemonId, auth: AuthToken) -> Self {
Self {
protocol: PROTOCOL_VERSION,
spec,
daemon,
endpoints: Vec::new(),
auth,
limits: NegotiatedLimits::uds(),
zero_copy_threshold: DEFAULT_ZERO_COPY_THRESHOLD,
shm_broker: None,
working_dir: None,
deterministic: false,
}
}
#[must_use]
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoints.push(endpoint.into());
self
}
#[must_use]
pub fn with_endpoints<I, S>(mut self, endpoints: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.endpoints = endpoints.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub const fn with_limits(mut self, limits: NegotiatedLimits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn with_zero_copy_threshold(mut self, bytes: u64) -> Self {
self.zero_copy_threshold = bytes;
self
}
#[must_use]
pub fn with_shm_broker(mut self, path: impl Into<String>) -> Self {
self.shm_broker = Some(path.into());
self
}
#[must_use]
pub fn with_working_dir(mut self, path: impl Into<String>) -> Self {
self.working_dir = Some(path.into());
self
}
#[must_use]
pub const fn with_deterministic(mut self, deterministic: bool) -> Self {
self.deterministic = deterministic;
self
}
#[must_use]
pub const fn dataflow(&self) -> DataflowId {
self.spec.dataflow
}
#[must_use]
pub const fn node(&self) -> &NodeId {
&self.spec.node
}
#[must_use]
pub const fn generation(&self) -> u64 {
self.spec.generation
}
#[must_use]
pub const fn protocol_is_supported(&self) -> bool {
crate::version::supports_protocol(self.protocol)
}
pub fn to_env_value(&self) -> Result<String, WireError> {
Ok(base64::encode(&self.encode_to_vec()?))
}
pub fn from_env_value(value: &str) -> Result<Self, NodeConfigError> {
let bytes = base64::decode(value).map_err(NodeConfigError::Base64)?;
Self::decode_exact(&bytes).map_err(NodeConfigError::Decode)
}
}
impl fmt::Display for NodeConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}/{} generation {} via {}",
self.spec.dataflow,
self.spec.node,
self.spec.generation,
if self.endpoints.is_empty() {
"<no endpoint>"
} else {
self.endpoints
.first()
.map_or("<no endpoint>", String::as_str)
}
)
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum NodeConfigError {
#[error("ASTRS_NODE_CONFIG is not valid base64: {0}")]
Base64(#[source] Base64Error),
#[error("ASTRS_NODE_CONFIG is not a valid node configuration: {0}")]
Decode(#[source] WireError),
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::common::node::{InputSpec, NodeSource, OutputSpec};
use crate::ids::{DataId, PortRef};
fn spec() -> NodeSpawnSpec {
NodeSpawnSpec::new(
DataflowId::from_u128(42),
NodeId::new("detector").unwrap(),
7,
NodeSource::Executable {
path: "./target/release/detector".into(),
},
)
.with_input(InputSpec::new(
DataId::new("frames").unwrap(),
PortRef::new(
NodeId::new("camera").unwrap(),
DataId::new("image").unwrap(),
),
))
.with_output(OutputSpec::new(DataId::new("detections").unwrap()))
}
fn config() -> NodeConfig {
NodeConfig::new(spec(), DaemonId::generate(None), AuthToken::ZERO)
.with_endpoints(["uds:///tmp/astrs/daemon.sock", "tcp://127.0.0.1:7408"])
.with_zero_copy_threshold(8_192)
.with_shm_broker("/tmp/astrs/shm.sock")
.with_working_dir("/workspace")
.with_deterministic(true)
}
#[test]
fn a_config_round_trips_through_an_env_value() {
let original = config();
let value = original.to_env_value().unwrap();
assert_eq!(NodeConfig::from_env_value(&value).unwrap(), original);
}
#[test]
fn an_env_value_is_a_valid_c_string() {
let value = config().to_env_value().unwrap();
assert!(!value.contains('\0'));
assert!(value.is_ascii());
assert!(!value.contains('\n'));
}
#[test]
fn the_current_protocol_is_stamped_and_recognized() {
let config = config();
assert_eq!(config.protocol, PROTOCOL_VERSION);
assert!(config.protocol_is_supported());
let mut future = config;
future.protocol = u16::MAX;
assert!(!future.protocol_is_supported());
}
#[test]
fn accessors_read_through_to_the_spec() {
let config = config();
assert_eq!(config.dataflow(), DataflowId::from_u128(42));
assert_eq!(config.node().as_str(), "detector");
assert_eq!(config.generation(), 7);
}
#[test]
fn a_corrupt_blob_is_reported_not_guessed() {
assert!(matches!(
NodeConfig::from_env_value("not base64!"),
Err(NodeConfigError::Base64(_))
));
assert!(matches!(
NodeConfig::from_env_value(&base64::encode(b"garbage")),
Err(NodeConfigError::Decode(_))
));
}
#[test]
fn a_truncated_blob_is_refused_rather_than_half_read() {
let value = config().to_env_value().unwrap();
let bytes = base64::decode(&value).unwrap();
let truncated = base64::encode(&bytes[..bytes.len() / 2]);
assert!(NodeConfig::from_env_value(&truncated).is_err());
}
#[test]
fn defaults_follow_the_appendix() {
let config = NodeConfig::new(spec(), DaemonId::generate(None), AuthToken::ZERO);
assert_eq!(config.zero_copy_threshold, DEFAULT_ZERO_COPY_THRESHOLD);
assert_eq!(config.zero_copy_threshold, 4_096);
assert!(config.endpoints.is_empty());
assert!(config.shm_broker.is_none());
assert!(!config.deterministic);
assert!(!config.limits.require_crc, "UDS legs may skip the checksum");
}
#[test]
fn the_env_variable_names_are_the_documented_ones() {
assert_eq!(ENV_NODE_CONFIG, "ASTRS_NODE_CONFIG");
assert_eq!(ENV_RUN_PARENT_PID, "ASTRS_RUN_PARENT_PID");
}
#[test]
fn display_names_the_node_and_its_endpoint() {
let rendered = config().to_string();
assert!(rendered.contains("detector"), "{rendered}");
assert!(rendered.contains("generation 7"), "{rendered}");
assert!(rendered.contains("uds://"), "{rendered}");
let bare = NodeConfig::new(spec(), DaemonId::generate(None), AuthToken::ZERO).to_string();
assert!(bare.contains("<no endpoint>"), "{bare}");
}
#[test]
fn the_auth_token_survives_the_round_trip_exactly() {
let token = AuthToken::parse_hex(&"ab".repeat(32)).unwrap();
let config = NodeConfig::new(spec(), DaemonId::generate(None), token.clone());
let decoded = NodeConfig::from_env_value(&config.to_env_value().unwrap()).unwrap();
assert_eq!(decoded.auth.reveal_bytes(), token.reveal_bytes());
}
}