From 2c717c44804fad2976896974ffaba40eee4536dc Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 14:35:02 -0600 Subject: [PATCH 1/2] feat(id): wire the loaded server-config budgets into serve WorldProtocol (#2247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The [budgets] table was parsed at startup and held in _started_server_config, but cmd_serve built the production protocol with with_local_node, which hard-wires BudgetPolicy::default() — so configured per-counterparty caps were inert. cmd_serve now builds the protocol through world_protocol_for, which passes the loaded config budgets to WorldProtocol::with_budgets when a config was given and keeps the coded defaults otherwise. Tests: serve_world_protocol_enforces_configured_budgets (configured policy incl. a counterparty override reaches the evaluator and an over-cap charge is refused BudgetExhausted) and serve_world_protocol_defaults_budgets_without_config. NOTE: the id lib test target does not compile on current main for an unrelated reason (world_store.rs tests use JOURNAL_VERSION_EXECUTION_PROFILE unqualified — #7081, task 14503); cargo check --lib is clean and serve.rs itself compiles with no errors. --- pkgs/id/src/commands/serve.rs | 90 ++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/commands/serve.rs b/pkgs/id/src/commands/serve.rs index d45ee9ade9..65b8095327 100644 --- a/pkgs/id/src/commands/serve.rs +++ b/pkgs/id/src/commands/serve.rs @@ -1068,6 +1068,29 @@ pub fn start_server_config(path: &Path) -> Result { }) } + +/// The world protocol serve registers for a hub: the loaded operator +/// config's #2247 budget policy when a server config was given, the +/// coded defaults when none was. Before this wire, the budgets +/// table was parsed and held at startup scope but never reached the +/// protocol, so configured per-counterparty caps were inert in +/// production. +#[cfg(feature = "world")] +fn world_protocol_for( + hub: crate::world_hub::WorldHub, + local_node: [u8; 32], + started: Option<&StartedServerConfig>, +) -> crate::world_net::WorldProtocol { + match started { + Some(started) => crate::world_net::WorldProtocol::with_budgets( + hub, + local_node, + started.config.budgets().clone(), + ), + None => crate::world_net::WorldProtocol::with_local_node(hub, local_node), + } +} + pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { let ServeOptions { ephemeral, @@ -1158,7 +1181,7 @@ pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { ); } #[cfg(feature = "world")] - let _started_server_config = match &server_config { + let started_server_config = match &server_config { Some(path) => { let started = start_server_config(path)?; status!( @@ -1447,7 +1470,7 @@ pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { #[cfg(feature = "world")] let world_protocol = world_hub .as_ref() - .map(|hub| crate::world_net::WorldProtocol::with_local_node(hub.clone(), *node_id.as_bytes())); + .map(|hub| world_protocol_for(hub.clone(), *node_id.as_bytes(), started_server_config.as_ref())); #[cfg(feature = "world")] let abuse_handle = world_protocol @@ -3476,6 +3499,69 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + + // #2247: the loaded [budgets] must reach the world protocol serve + // registers, not just the parser. + + #[cfg(feature = "world")] + #[test] + fn serve_world_protocol_enforces_configured_budgets() { + let dir = serve_config_dir("budgets"); + let path = dir.join("server.toml"); + let ann = ed25519_dalek::SigningKey::from_bytes(&[1; 32]) + .verifying_key() + .to_bytes(); + let ann_hex = ann.iter().map(|b| format!("{b:02x}")).collect::(); + std::fs::write( + &path, + format!( + "[[users]]\nkey = \"ann\"\nid = \"{ann_hex}\"\n\n[budgets]\nper_peer_bytes_per_hour = 1000\n\n[[budgets.counterparty]]\npeer = \"ann\"\nbytes_per_window = 500\nwindow_seconds = 60\n" + ), + ) + .unwrap(); + let started = start_server_config(&path).unwrap(); + assert_ne!( + *started.config.budgets(), + crate::abuse::BudgetPolicy::default() + ); + let world = crate::world::WorldHandle::spawn( + crate::world::WorldCore::new("lobby", crate::world::WorldLimits::default()).unwrap(), + ); + let service = crate::world_session::WorldService::new(world, None); + let protocol = world_protocol_for(service.into(), [9; 32], Some(&started)); + let handle = protocol.abuse_handle(); + let mut evaluator = handle.lock().unwrap(); + // The configured policy — not the default — is the evaluator's. + assert_eq!(evaluator.budgets(), Some(started.config.budgets())); + // Ann's counterparty row replaced her per-peer limits. + let limits = evaluator.limits_for_peer(&ann); + assert_eq!(limits.len(), 1); + assert_eq!(limits[0].bytes_per_window, 500); + // A charge beyond the configured cap is refused as a budget + // refusal (the frame is dropped; the peer is not disconnected). + let refusal = evaluator + .check_budget(crate::abuse::Direction::Inbound, ann, None, 600, 1_000) + .unwrap_err(); + assert_eq!(refusal.reason, crate::abuse::RefusalReason::BudgetExhausted); + let _ = std::fs::remove_dir_all(&dir); + } + + #[cfg(feature = "world")] + #[test] + fn serve_world_protocol_defaults_budgets_without_config() { + let world = crate::world::WorldHandle::spawn( + crate::world::WorldCore::new("lobby", crate::world::WorldLimits::default()).unwrap(), + ); + let service = crate::world_session::WorldService::new(world, None); + let protocol = world_protocol_for(service.into(), [9; 32], None); + let handle = protocol.abuse_handle(); + let evaluator = handle.lock().unwrap(); + assert_eq!( + evaluator.budgets(), + Some(&crate::abuse::BudgetPolicy::default()) + ); + } + #[test] fn serve_server_config_flag_defaults_to_none() { use clap::Parser as _; From 275e20b04cd32390a6255ab787ad238dc225c98e Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:07:10 -0600 Subject: [PATCH 2/2] =?UTF-8?q?test(id):=20fix=20#2247=20serve=20budget=20?= =?UTF-8?q?tests=20=E2=80=94=20limits=5Ffor=5Fpeer=20lives=20on=20BudgetPo?= =?UTF-8?q?licy;=20world=20spawn=20needs=20a=20tokio=20runtime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/src/commands/serve.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkgs/id/src/commands/serve.rs b/pkgs/id/src/commands/serve.rs index 65b8095327..49721c7562 100644 --- a/pkgs/id/src/commands/serve.rs +++ b/pkgs/id/src/commands/serve.rs @@ -3504,8 +3504,8 @@ mod tests { // registers, not just the parser. #[cfg(feature = "world")] - #[test] - fn serve_world_protocol_enforces_configured_budgets() { + #[tokio::test] + async fn serve_world_protocol_enforces_configured_budgets() { let dir = serve_config_dir("budgets"); let path = dir.join("server.toml"); let ann = ed25519_dalek::SigningKey::from_bytes(&[1; 32]) @@ -3534,7 +3534,7 @@ mod tests { // The configured policy — not the default — is the evaluator's. assert_eq!(evaluator.budgets(), Some(started.config.budgets())); // Ann's counterparty row replaced her per-peer limits. - let limits = evaluator.limits_for_peer(&ann); + let limits = started.config.budgets().limits_for_peer(&ann); assert_eq!(limits.len(), 1); assert_eq!(limits[0].bytes_per_window, 500); // A charge beyond the configured cap is refused as a budget @@ -3547,8 +3547,8 @@ mod tests { } #[cfg(feature = "world")] - #[test] - fn serve_world_protocol_defaults_budgets_without_config() { + #[tokio::test] + async fn serve_world_protocol_defaults_budgets_without_config() { let world = crate::world::WorldHandle::spawn( crate::world::WorldCore::new("lobby", crate::world::WorldLimits::default()).unwrap(), );