2026-03-22 03:30:21 +00:00
|
|
|
//! Federation state sync and remote deployment.
|
2026-04-19 01:20:44 -04:00
|
|
|
//!
|
|
|
|
|
//! Requests prefer FIPS (direct ULA dial, ~LAN latency) and fall back to
|
|
|
|
|
//! Tor on any network failure. See `crate::fips::dial::PeerRequest` for
|
|
|
|
|
//! the fallback mechanics.
|
2026-03-22 03:30:21 +00:00
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
use super::storage::update_node_state;
|
|
|
|
|
use super::types::{AppStatus, FederatedNode, NodeStateSnapshot, TrustLevel};
|
2026-04-19 01:20:44 -04:00
|
|
|
use crate::fips::dial::PeerRequest;
|
2026-03-22 03:30:21 +00:00
|
|
|
|
2026-04-19 01:20:44 -04:00
|
|
|
/// Sync state with a single federated peer. Tries FIPS first; falls back
|
|
|
|
|
/// to Tor on any transport-level failure.
|
2026-03-22 03:30:21 +00:00
|
|
|
pub async fn sync_with_peer(
|
|
|
|
|
data_dir: &Path,
|
|
|
|
|
peer: &FederatedNode,
|
|
|
|
|
local_did: &str,
|
|
|
|
|
sign_fn: impl FnOnce(&[u8]) -> String,
|
|
|
|
|
) -> Result<NodeStateSnapshot> {
|
|
|
|
|
let timestamp = chrono::Utc::now().to_rfc3339();
|
|
|
|
|
let signature = sign_fn(timestamp.as_bytes());
|
|
|
|
|
|
|
|
|
|
let body = serde_json::json!({
|
|
|
|
|
"method": "federation.get-state",
|
|
|
|
|
"params": {}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-19 01:20:44 -04:00
|
|
|
let (resp, transport) = PeerRequest::new(peer.fips_npub.as_deref(), &peer.onion, "/rpc/v1")
|
2026-04-19 01:44:41 -04:00
|
|
|
.service(crate::settings::transport::PeerService::Federation)
|
2026-03-22 03:30:21 +00:00
|
|
|
.header("X-Federation-DID", local_did)
|
2026-04-19 01:20:44 -04:00
|
|
|
.header("X-Federation-Sig", signature)
|
|
|
|
|
.header("X-Federation-Timestamp", timestamp)
|
|
|
|
|
.timeout(std::time::Duration::from_secs(30))
|
|
|
|
|
.send_json(&body)
|
2026-03-22 03:30:21 +00:00
|
|
|
.await
|
|
|
|
|
.context("Failed to reach federated peer")?;
|
|
|
|
|
|
|
|
|
|
if !resp.status().is_success() {
|
2026-04-19 01:20:44 -04:00
|
|
|
anyhow::bail!("Peer returned {} (via {})", resp.status(), transport);
|
2026-03-22 03:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result: serde_json::Value = resp.json().await.context("Invalid response from peer")?;
|
|
|
|
|
let state_val = result
|
|
|
|
|
.get("result")
|
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("No result in peer response"))?;
|
|
|
|
|
|
|
|
|
|
let state: NodeStateSnapshot =
|
|
|
|
|
serde_json::from_value(state_val.clone()).context("Failed to parse peer state")?;
|
|
|
|
|
|
|
|
|
|
update_node_state(data_dir, &peer.did, state.clone()).await?;
|
|
|
|
|
|
|
|
|
|
Ok(state)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build the local node's state snapshot for sharing with peers.
|
|
|
|
|
pub fn build_local_state(
|
|
|
|
|
apps: Vec<AppStatus>,
|
|
|
|
|
cpu: f64,
|
|
|
|
|
mem_used: u64,
|
|
|
|
|
mem_total: u64,
|
|
|
|
|
disk_used: u64,
|
|
|
|
|
disk_total: u64,
|
|
|
|
|
uptime: u64,
|
|
|
|
|
tor_active: bool,
|
|
|
|
|
server_name: Option<String>,
|
2026-04-18 11:07:08 -04:00
|
|
|
nostr_npub: Option<String>,
|
2026-03-22 03:30:21 +00:00
|
|
|
) -> NodeStateSnapshot {
|
|
|
|
|
NodeStateSnapshot {
|
|
|
|
|
timestamp: chrono::Utc::now().to_rfc3339(),
|
|
|
|
|
node_name: server_name,
|
|
|
|
|
apps,
|
|
|
|
|
cpu_usage_percent: Some(cpu),
|
|
|
|
|
mem_used_bytes: Some(mem_used),
|
|
|
|
|
mem_total_bytes: Some(mem_total),
|
|
|
|
|
disk_used_bytes: Some(disk_used),
|
|
|
|
|
disk_total_bytes: Some(disk_total),
|
|
|
|
|
uptime_secs: Some(uptime),
|
|
|
|
|
tor_active: Some(tor_active),
|
2026-04-18 11:07:08 -04:00
|
|
|
nostr_npub,
|
2026-03-22 03:30:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deploy an app to a remote federated peer over Tor.
|
|
|
|
|
/// Only works if the peer is trusted and the app exists in our marketplace.
|
|
|
|
|
pub async fn deploy_to_peer(
|
|
|
|
|
peer: &FederatedNode,
|
|
|
|
|
app_id: &str,
|
|
|
|
|
version: &str,
|
|
|
|
|
marketplace_url: &str,
|
|
|
|
|
local_did: &str,
|
|
|
|
|
sign_fn: impl FnOnce(&[u8]) -> String,
|
|
|
|
|
) -> Result<serde_json::Value> {
|
|
|
|
|
if peer.trust_level != TrustLevel::Trusted {
|
chore(ci): rustfmt + clippy clean-up to unblock the Rust CI job
The .github/workflows/ci.yml Rust job runs cargo fmt --check, clippy
with -D warnings, and tests. All three were failing. This commit:
- Applies rustfmt across the tree (the bulk of the diff — untouched
since the last toolchain bump, so a wide sweep was unavoidable).
- Fixes the correctness-level clippy errors:
container/bitcoin_simulator.rs wildcard-in-or-pattern
container/manifest.rs from_str rename to parse (reserved name)
container/podman_client.rs .get(0) -> .first()
container/runtime.rs manual += collapse
archipelago/src/constants.rs doc-comment → module-doc
api/rpc/package/install.rs stray /// comment above a non-item
container/docker_packages.rs redundant field init
streaming/advertisement.rs missing Metric import in tests
tests/orchestration_tests.rs `vec!` in non-Vec contexts
mesh/listener/dispatch.rs unused store_plain_message import
api/rpc/tor/mod.rs and mesh/steganography.rs: push-after-new → vec!
- Quiets wide legacy surfaces with crate-level allows in main.rs for
stylistic lints (too_many_arguments, type_complexity, doc indent,
enum variant prefix, wildcard-in-or, assertions-on-constants,
drop_non_drop, unused_io_amount, ptr_arg) — these fired in dozens
of places with no correctness payoff and have been churning every
toolchain bump.
- Tags intentional-dead-code helpers: wallet/ and streaming/ modules
are WIP, mesh::send_chunked_payload and DM_V1_MARKER are kept for
rollback compatibility, vpn::get_nostr_vpn_status is surface-area
for a not-yet-landed RPC.
cargo fmt --check, cargo clippy --all-targets --all-features
-- -D warnings, and cargo test --all-features now all pass locally.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 17:23:46 -04:00
|
|
|
anyhow::bail!(
|
|
|
|
|
"Can only deploy to trusted peers (current: {})",
|
|
|
|
|
peer.trust_level
|
|
|
|
|
);
|
2026-03-22 03:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let timestamp = chrono::Utc::now().to_rfc3339();
|
|
|
|
|
let signature = sign_fn(timestamp.as_bytes());
|
|
|
|
|
|
|
|
|
|
let body = serde_json::json!({
|
|
|
|
|
"method": "package.install",
|
|
|
|
|
"params": {
|
|
|
|
|
"id": app_id,
|
|
|
|
|
"version": version,
|
|
|
|
|
"marketplace-url": marketplace_url,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-19 01:20:44 -04:00
|
|
|
let (resp, transport) = PeerRequest::new(peer.fips_npub.as_deref(), &peer.onion, "/rpc/v1")
|
2026-04-19 01:44:41 -04:00
|
|
|
.service(crate::settings::transport::PeerService::Federation)
|
2026-03-22 03:30:21 +00:00
|
|
|
.header("X-Federation-DID", local_did)
|
2026-04-19 01:20:44 -04:00
|
|
|
.header("X-Federation-Sig", signature)
|
|
|
|
|
.header("X-Federation-Timestamp", timestamp)
|
|
|
|
|
.timeout(std::time::Duration::from_secs(120))
|
|
|
|
|
.send_json(&body)
|
2026-03-22 03:30:21 +00:00
|
|
|
.await
|
|
|
|
|
.context("Failed to reach federated peer for deploy")?;
|
|
|
|
|
|
|
|
|
|
if !resp.status().is_success() {
|
2026-04-19 01:20:44 -04:00
|
|
|
anyhow::bail!("Remote node returned HTTP {} (via {})", resp.status(), transport);
|
2026-03-22 03:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result: serde_json::Value = resp.json().await.context("Invalid response from peer")?;
|
|
|
|
|
|
|
|
|
|
if let Some(err) = result.get("error") {
|
|
|
|
|
if !err.is_null() {
|
chore(ci): rustfmt + clippy clean-up to unblock the Rust CI job
The .github/workflows/ci.yml Rust job runs cargo fmt --check, clippy
with -D warnings, and tests. All three were failing. This commit:
- Applies rustfmt across the tree (the bulk of the diff — untouched
since the last toolchain bump, so a wide sweep was unavoidable).
- Fixes the correctness-level clippy errors:
container/bitcoin_simulator.rs wildcard-in-or-pattern
container/manifest.rs from_str rename to parse (reserved name)
container/podman_client.rs .get(0) -> .first()
container/runtime.rs manual += collapse
archipelago/src/constants.rs doc-comment → module-doc
api/rpc/package/install.rs stray /// comment above a non-item
container/docker_packages.rs redundant field init
streaming/advertisement.rs missing Metric import in tests
tests/orchestration_tests.rs `vec!` in non-Vec contexts
mesh/listener/dispatch.rs unused store_plain_message import
api/rpc/tor/mod.rs and mesh/steganography.rs: push-after-new → vec!
- Quiets wide legacy surfaces with crate-level allows in main.rs for
stylistic lints (too_many_arguments, type_complexity, doc indent,
enum variant prefix, wildcard-in-or, assertions-on-constants,
drop_non_drop, unused_io_amount, ptr_arg) — these fired in dozens
of places with no correctness payoff and have been churning every
toolchain bump.
- Tags intentional-dead-code helpers: wallet/ and streaming/ modules
are WIP, mesh::send_chunked_payload and DM_V1_MARKER are kept for
rollback compatibility, vpn::get_nostr_vpn_status is surface-area
for a not-yet-landed RPC.
cargo fmt --check, cargo clippy --all-targets --all-features
-- -D warnings, and cargo test --all-features now all pass locally.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 17:23:46 -04:00
|
|
|
let msg = err
|
|
|
|
|
.get("message")
|
|
|
|
|
.and_then(|m| m.as_str())
|
|
|
|
|
.unwrap_or("Unknown remote error");
|
2026-03-22 03:30:21 +00:00
|
|
|
anyhow::bail!("Remote node refused deploy: {}", msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(serde_json::json!({
|
|
|
|
|
"deployed": true,
|
|
|
|
|
"app_id": app_id,
|
|
|
|
|
"peer_did": peer.did,
|
|
|
|
|
"peer_onion": peer.onion,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_build_local_state() {
|
|
|
|
|
let state = build_local_state(
|
|
|
|
|
vec![AppStatus {
|
|
|
|
|
id: "lnd".to_string(),
|
|
|
|
|
status: "running".to_string(),
|
|
|
|
|
version: Some("0.18".to_string()),
|
|
|
|
|
}],
|
|
|
|
|
25.5,
|
|
|
|
|
2_000_000_000,
|
|
|
|
|
8_000_000_000,
|
|
|
|
|
100_000_000_000,
|
|
|
|
|
500_000_000_000,
|
|
|
|
|
3600,
|
|
|
|
|
true,
|
|
|
|
|
Some("Test Node".to_string()),
|
2026-04-18 11:07:08 -04:00
|
|
|
None,
|
2026-03-22 03:30:21 +00:00
|
|
|
);
|
|
|
|
|
assert_eq!(state.apps.len(), 1);
|
|
|
|
|
assert_eq!(state.cpu_usage_percent, Some(25.5));
|
|
|
|
|
assert_eq!(state.tor_active, Some(true));
|
|
|
|
|
assert_eq!(state.node_name, Some("Test Node".to_string()));
|
|
|
|
|
}
|
|
|
|
|
}
|