feat(content): owned-content persistence + Fedimint paid downloads, fmcd caps fix, FIPS warm-path perf
Buyer-side paid downloads now persist: purchases are cached on disk (content_owned.rs) keyed by (seller onion, content_id), the gallery shows an "Owned" badge unblurred, and items view/play in-app from the local cache with no re-payment or reliance on a browser download (which silently failed on the mobile companion). New RPCs content.owned-list / content.owned-get. Validated e2e .116<-.198 (paid 100 sats via Fedimint, 166KB jpeg returns, survives restart). fedimint-clientd manifest: restore the standard container capability set (CHOWN/DAC_OVERRIDE/FOWNER/SETUID/SETGID) so fmcd's startup chown of an existing-federation /data succeeds instead of dying EPERM (#7). Confirmed the orchestrator applies these to the running container. FIPS perf: tighten the supervisor warm-path keepalive 45s -> 25s so peer paths stay inside the ~30-60s NAT cold window. Dials now reliably land on FIPS instead of re-punching and falling back to Tor. Measured to the same peer: cloud browse 18-22s -> 0.4s; full Fedimint paid download 29s -> 11s (residual is the seller-side guardian reissue round-trip). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
b0c9bd2a0c
commit
db7d424bff
@@ -532,11 +532,49 @@ impl RpcHandler {
|
||||
}));
|
||||
}
|
||||
|
||||
// Capture the content type BEFORE consuming the body so the local cache
|
||||
// can render the right viewer (image vs video) later.
|
||||
let mime_type = response
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(|s| s.split(';').next().unwrap_or(s).trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
|
||||
let bytes = response
|
||||
.bytes()
|
||||
.await
|
||||
.context("Failed to read response body")?;
|
||||
|
||||
// Persist the purchase so it "stays unlocked" for this buyer: cache the
|
||||
// bytes + metadata keyed by (onion, content_id). The gallery then renders
|
||||
// it unblurred and views it in-app from this cache — no re-payment and no
|
||||
// reliance on a browser download (which silently fails on the mobile
|
||||
// companion, the original "paid but never unlocked" report). Best-effort:
|
||||
// a cache-write failure must not fail an already-paid download.
|
||||
let filename = params
|
||||
.get("filename")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or(content_id)
|
||||
.to_string();
|
||||
let purchased_at = chrono::Utc::now().to_rfc3339();
|
||||
if let Err(e) = crate::content_owned::record_purchase(
|
||||
&self.config.data_dir,
|
||||
onion,
|
||||
content_id,
|
||||
&filename,
|
||||
&mime_type,
|
||||
&bytes,
|
||||
price_sats,
|
||||
used_backend,
|
||||
&purchased_at,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!("paid download: failed to cache purchased content (non-fatal): {e:#}");
|
||||
}
|
||||
|
||||
use base64::Engine;
|
||||
let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
|
||||
|
||||
@@ -546,6 +584,8 @@ impl RpcHandler {
|
||||
"size": bytes.len(),
|
||||
"paid_sats": price_sats,
|
||||
"ecash_backend": used_backend,
|
||||
"mime_type": mime_type,
|
||||
"owned": true,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1017,4 +1057,43 @@ impl RpcHandler {
|
||||
"preview_mode": is_preview,
|
||||
}))
|
||||
}
|
||||
|
||||
/// `content.owned-list` — every paid item this node has purchased, so the
|
||||
/// gallery can render owned items unblurred/viewable without re-payment.
|
||||
pub(super) async fn handle_content_owned_list(&self) -> Result<serde_json::Value> {
|
||||
let items = crate::content_owned::list_owned(&self.config.data_dir).await;
|
||||
Ok(serde_json::json!({ "items": items }))
|
||||
}
|
||||
|
||||
/// `content.owned-get` — return a purchased item's bytes (base64) from the
|
||||
/// local cache for in-app viewing/saving. No network, no re-payment.
|
||||
pub(super) async fn handle_content_owned_get(
|
||||
&self,
|
||||
params: Option<serde_json::Value>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?;
|
||||
let onion = params
|
||||
.get("onion")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| anyhow::anyhow!("Missing onion address"))?;
|
||||
let content_id = params
|
||||
.get("content_id")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| anyhow::anyhow!("Missing content_id"))?;
|
||||
|
||||
match crate::content_owned::read_owned(&self.config.data_dir, onion, content_id).await {
|
||||
Some((mime_type, bytes)) => {
|
||||
use base64::Engine;
|
||||
let encoded = base64::engine::general_purpose::STANDARD.encode(&bytes);
|
||||
Ok(serde_json::json!({
|
||||
"data": encoded,
|
||||
"size": bytes.len(),
|
||||
"mime_type": mime_type,
|
||||
}))
|
||||
}
|
||||
None => Ok(serde_json::json!({
|
||||
"error": "You don't own this item yet, or its cached copy is missing."
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,6 +276,8 @@ impl RpcHandler {
|
||||
"content.browse-peer" => self.handle_content_browse_peer(params).await,
|
||||
"content.download-peer" => self.handle_content_download_peer(params).await,
|
||||
"content.download-peer-paid" => self.handle_content_download_peer_paid(params).await,
|
||||
"content.owned-list" => self.handle_content_owned_list().await,
|
||||
"content.owned-get" => self.handle_content_owned_get(params).await,
|
||||
"content.request-invoice" => self.handle_content_request_invoice(params).await,
|
||||
"content.invoice-status" => self.handle_content_invoice_status(params).await,
|
||||
"content.download-peer-invoice" => {
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
//! Buyer-side store of paid content the node has purchased.
|
||||
//!
|
||||
//! A paid peer download used to be ephemeral: the bytes were handed to the
|
||||
//! browser as a one-shot `<a download>` and then thrown away. On the mobile
|
||||
//! companion that download silently fails, so the item appeared to never
|
||||
//! "unlock" even though the ecash was spent. This module persists every
|
||||
//! successful purchase — bytes + metadata — keyed by (seller onion, content_id),
|
||||
//! so the gallery can render owned items unblurred and play/view them in-app
|
||||
//! from the local cache, with no re-payment and no reliance on a browser
|
||||
//! download. The buyer can still save the file later from the cached copy.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::{Path, PathBuf};
|
||||
use tokio::fs;
|
||||
|
||||
const OWNED_DIR: &str = "purchased-content";
|
||||
const OWNED_INDEX: &str = "owned.json";
|
||||
|
||||
/// One purchased item. `onion` + `content_id` are the identity; everything else
|
||||
/// is display/metadata captured at purchase time.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OwnedItem {
|
||||
pub onion: String,
|
||||
pub content_id: String,
|
||||
pub filename: String,
|
||||
pub mime_type: String,
|
||||
pub size_bytes: u64,
|
||||
pub paid_sats: u64,
|
||||
pub ecash_backend: String,
|
||||
/// RFC3339 timestamp; best-effort, empty if the clock was unavailable.
|
||||
pub purchased_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
struct OwnedIndex {
|
||||
items: Vec<OwnedItem>,
|
||||
}
|
||||
|
||||
fn owned_root(data_dir: &Path) -> PathBuf {
|
||||
data_dir.join(OWNED_DIR)
|
||||
}
|
||||
|
||||
fn index_path(data_dir: &Path) -> PathBuf {
|
||||
owned_root(data_dir).join(OWNED_INDEX)
|
||||
}
|
||||
|
||||
/// Sanitize an onion into a safe directory component (it's already [a-z2-7].onion
|
||||
/// for valid v3, but be defensive against path traversal regardless).
|
||||
fn sanitize(component: &str) -> String {
|
||||
component
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn bytes_path(data_dir: &Path, onion: &str, content_id: &str) -> PathBuf {
|
||||
owned_root(data_dir)
|
||||
.join(sanitize(onion))
|
||||
.join(sanitize(content_id))
|
||||
}
|
||||
|
||||
async fn load_index(data_dir: &Path) -> OwnedIndex {
|
||||
match fs::read_to_string(index_path(data_dir)).await {
|
||||
Ok(s) => serde_json::from_str(&s).unwrap_or_default(),
|
||||
Err(_) => OwnedIndex::default(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_index(data_dir: &Path, index: &OwnedIndex) -> Result<()> {
|
||||
let root = owned_root(data_dir);
|
||||
fs::create_dir_all(&root)
|
||||
.await
|
||||
.with_context(|| format!("creating {}", root.display()))?;
|
||||
let content = serde_json::to_string_pretty(index).context("serializing owned index")?;
|
||||
fs::write(index_path(data_dir), content)
|
||||
.await
|
||||
.context("writing owned index")
|
||||
}
|
||||
|
||||
/// Persist a successful purchase: write the bytes to disk and upsert the index
|
||||
/// entry. Idempotent on (onion, content_id) — re-buying overwrites with the
|
||||
/// latest copy/metadata rather than duplicating.
|
||||
pub async fn record_purchase(
|
||||
data_dir: &Path,
|
||||
onion: &str,
|
||||
content_id: &str,
|
||||
filename: &str,
|
||||
mime_type: &str,
|
||||
bytes: &[u8],
|
||||
paid_sats: u64,
|
||||
ecash_backend: &str,
|
||||
purchased_at: &str,
|
||||
) -> Result<()> {
|
||||
let path = bytes_path(data_dir, onion, content_id);
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.await
|
||||
.with_context(|| format!("creating {}", parent.display()))?;
|
||||
}
|
||||
fs::write(&path, bytes)
|
||||
.await
|
||||
.with_context(|| format!("writing purchased bytes to {}", path.display()))?;
|
||||
|
||||
let mut index = load_index(data_dir).await;
|
||||
let entry = OwnedItem {
|
||||
onion: onion.to_string(),
|
||||
content_id: content_id.to_string(),
|
||||
filename: filename.to_string(),
|
||||
mime_type: mime_type.to_string(),
|
||||
size_bytes: bytes.len() as u64,
|
||||
paid_sats,
|
||||
ecash_backend: ecash_backend.to_string(),
|
||||
purchased_at: purchased_at.to_string(),
|
||||
};
|
||||
if let Some(existing) = index
|
||||
.items
|
||||
.iter_mut()
|
||||
.find(|i| i.onion == onion && i.content_id == content_id)
|
||||
{
|
||||
*existing = entry;
|
||||
} else {
|
||||
index.items.push(entry);
|
||||
}
|
||||
save_index(data_dir, &index).await
|
||||
}
|
||||
|
||||
/// Every item this node owns.
|
||||
pub async fn list_owned(data_dir: &Path) -> Vec<OwnedItem> {
|
||||
load_index(data_dir).await.items
|
||||
}
|
||||
|
||||
/// True if the node has already purchased this (onion, content_id).
|
||||
#[allow(dead_code)] // used by the upcoming seller-side signed-entitlement path (#8)
|
||||
pub async fn is_owned(data_dir: &Path, onion: &str, content_id: &str) -> bool {
|
||||
bytes_path(data_dir, onion, content_id).exists()
|
||||
&& load_index(data_dir)
|
||||
.await
|
||||
.items
|
||||
.iter()
|
||||
.any(|i| i.onion == onion && i.content_id == content_id)
|
||||
}
|
||||
|
||||
/// Read a purchased item's bytes + mime type from the local cache, if present.
|
||||
pub async fn read_owned(
|
||||
data_dir: &Path,
|
||||
onion: &str,
|
||||
content_id: &str,
|
||||
) -> Option<(String, Vec<u8>)> {
|
||||
let bytes = fs::read(bytes_path(data_dir, onion, content_id)).await.ok()?;
|
||||
let mime = load_index(data_dir)
|
||||
.await
|
||||
.items
|
||||
.into_iter()
|
||||
.find(|i| i.onion == onion && i.content_id == content_id)
|
||||
.map(|i| i.mime_type)
|
||||
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||
Some((mime, bytes))
|
||||
}
|
||||
@@ -60,14 +60,23 @@ pub async fn ensure_activated(data_dir: &std::path::Path) {
|
||||
tracing::info!("FIPS auto-activated");
|
||||
}
|
||||
|
||||
/// Spawn the FIPS supervisor: every 45s it (1) auto-activates FIPS if onboarding
|
||||
/// Spawn the FIPS supervisor: every 25s it (1) auto-activates FIPS if onboarding
|
||||
/// is done but the service is down — so it comes up with zero user interaction,
|
||||
/// and (2) keeps hole-punched paths to known federation peers warm, so on-demand
|
||||
/// dials land on FIPS instead of falling back to Tor. Warms peers concurrently
|
||||
/// so one slow/offline peer doesn't delay the rest.
|
||||
///
|
||||
/// The interval MUST be shorter than the NAT/hole-punch cold window
|
||||
/// (`warm_path` docs it at ~30-60s). The previous 45s sat at the edge of that
|
||||
/// window: a path that went cold at ~30s stayed cold until the next 45s tick,
|
||||
/// so real peer dials in that gap hit a cold path and fell back to Tor (~18s
|
||||
/// onion latency instead of FIPS's ~2-3s). 25s keeps every path refreshed
|
||||
/// inside the minimum cold window, which is what actually makes FIPS — not Tor —
|
||||
/// the transport peer requests land on. Measured: warm FIPS browse ~2.6s vs a
|
||||
/// cold-path fallback browse ~18-22s over Tor to the same peer.
|
||||
pub fn spawn_fips_supervisor(data_dir: std::path::PathBuf) {
|
||||
tokio::spawn(async move {
|
||||
let mut tick = tokio::time::interval(std::time::Duration::from_secs(45));
|
||||
let mut tick = tokio::time::interval(std::time::Duration::from_secs(25));
|
||||
loop {
|
||||
tick.tick().await;
|
||||
// Bring FIPS up on its own once onboarding has materialised the key.
|
||||
|
||||
@@ -39,6 +39,7 @@ mod constants;
|
||||
mod container;
|
||||
mod content_hash;
|
||||
mod content_invoice;
|
||||
mod content_owned;
|
||||
mod content_server;
|
||||
mod crash_recovery;
|
||||
mod credentials;
|
||||
|
||||
@@ -169,6 +169,34 @@ pub async fn ensure_default_federation(data_dir: &Path) -> Result<()> {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Ok(()), // clientd not configured yet
|
||||
};
|
||||
|
||||
// Fast path: if fmcd already reports a joined federation, do NOT re-issue the
|
||||
// POST /v2/admin/join. That call re-syncs federation config against the
|
||||
// guardians and adds seconds of latency — and ensure_default_federation runs
|
||||
// on every wallet.fedimint-list / spend / reissue, so the join was being paid
|
||||
// on each balance refresh (the "mints take ages to load" report). The cheap
|
||||
// GET /v2/admin/info is enough to confirm membership; just reconcile the local
|
||||
// registry against the live joined set and return.
|
||||
let joined = client.joined_federation_ids().await;
|
||||
if !joined.is_empty() {
|
||||
let mut reg = load_registry(data_dir).await?;
|
||||
let mut changed = false;
|
||||
for id in joined {
|
||||
if !reg.federations.iter().any(|f| f.federation_id == id) {
|
||||
reg.federations.push(JoinedFederation {
|
||||
federation_id: id,
|
||||
name: Some("Archipelago Federation".to_string()),
|
||||
});
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
save_registry(data_dir, ®).await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Cold start only: nothing joined yet, so join the default federation once.
|
||||
let federation_id = match client.join(DEFAULT_FEDERATION_INVITE).await {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user