//! Declared holdings — the consent layer between "this node has the bytes" //! and "this node may be advertised to third parties as a source for them". //! //! # Why this exists //! //! Until v0.8.1 holdings were **inferred from transport**: every receive path //! called `touch_file_holder(sender, Received)`, and the resulting rows went //! straight back out on the wire as `BlobResponsePayload.cdn_redirect_peers` //! and as worm-lookup holder answers. Merely exchanging bytes with a peer //! enrolled your node id *and addresses* into a list that peer republished to //! anyone who asked. //! //! Two consequences, both load-bearing: //! //! 1. The erasure/concealment model ("hold k shards quietly, announce exactly //! one slot") is impossible on an inferred foundation — the act of //! acquiring a shard registers you as its holder whether you wanted to be //! advertised or not. //! 2. It is the enumeration surface Round 10's per-(holder, author) listing //! cap was aimed at: an enumerator does not need to hold anything, it just //! asks holders who else holds. //! //! # The split //! //! * **Local hint** (`file_holders.declared = 0`) — "we exchanged bytes about //! this file with that peer." Usable locally for fetch retry, diff //! propagation targets and manifest-refresh sources. **Never published as a //! holder of a specific file** — no hint ever reaches `cdn_redirect_peers`, //! a worm holder answer, an HTTP 302 or a manifest. One documented //! exception, which is not a holder claim: `Storage::build_own_uniques` //! announces hinted peer IDs as bare reachable node ids in the uniques //! pools, with no file association, because the uniques index is explicitly //! an "IDs I can help you reach" index rather than a holder list. //! * **Declaration** (`file_holders.declared = 1`) — the peer explicitly told //! us "I hold this and you may advertise me for it", via the holding //! declaration message (`MessageType::PostDownstreamRegister`, 0xD3). //! Only these may appear in an outbound holder list. //! //! An *undeclared* holder stays invisible but still serves a direct request it //! receives — declaration governs advertisement, not willingness to serve. //! //! # What declaration does NOT hide (known limit) //! //! Declaration governs the per-FILE holder lists. It hides **which** and **how //! many** of an author's posts you hold; it does not hide **that** you hold //! some of that author's content. `Storage::build_own_uniques` puts every //! distinct `cdn_manifests.author` and every received-comment author into our //! bounce-1 uniques pool, which is announced to all ~20 mesh peers and is //! deliberately interest-blind-but-complete so discovery works at all. So //! [`SETTING_CAP_PER_AUTHOR`] caps the POST count per author, never the author //! set — the one-shard-announcement property ("declare one slot, the other k-1 //! stay invisible") holds at file granularity only. Closing the author-level //! surface means deriving the `authors` half of the uniques announce from //! `declared_holdings`, which is a discovery trade Round 10 has not ruled on. //! //! # Our own declarations //! //! `declared_holdings` records what *we* have declared, so the policy below //! can be enforced and so a declaration can be withdrawn when content is //! deleted or evicted. use crate::types::NodeId; /// Setting key: declare content we received via `ReplicationRequest`. pub const SETTING_DECLARE_CUSTODIAL: &str = "holdings.declare_custodial"; /// Setting key: declare content we fetched out of our own interest. pub const SETTING_DECLARE_INTEREST: &str = "holdings.declare_interest"; /// Setting key: per-(us, author) cap on distinct declared posts. `0` = no cap. pub const SETTING_CAP_PER_AUTHOR: &str = "holdings.cap_per_author"; /// Setting key: declare holdings of content WE authored. Default OFF — see /// [`HoldingsPolicy::declare_own_authored`]. pub const SETTING_DECLARE_OWN_AUTHORED: &str = "holdings.declare_own_authored"; /// Setting key: publish an introducer's address instead of our own. pub const SETTING_INTRODUCER_ENABLED: &str = "holdings.introducer_enabled"; /// Setting key: hex node id of the introducer to publish. Empty = auto-pick. pub const SETTING_INTRODUCER_PEER: &str = "holdings.introducer_peer"; /// Round 10's eventual target for `cap_per_author` (1 to keep the gossip tap, /// 2 as the ceiling). **Not** the alpha default — see [`HoldingsPolicy`]. pub const ROUND10_TARGET_CAP_PER_AUTHOR: u32 = 2; /// How long a received declaration stays publishable without being renewed. /// /// A declaration is soft state: the declarer's rotating re-declaration sweep /// (`Storage::next_declarations_to_announce`) refreshes it on every content /// sync. One that has not been renewed inside this window means the holder is /// gone or has shed the file, and publishing it would send fetchers to a dead /// end. Generous on purpose for the alpha — a quiet node must not vanish from /// holder lists over a long weekend. pub const DECLARATION_TTL_MS: u64 = 30 * 24 * 60 * 60 * 1000; /// Most addresses a single declaration may carry (ours or an introducer's). /// /// Every published address is fed to `EndpointAddr::with_ip_addr` by whoever /// fetches, and MEMORY records that iroh probes every path inserted into an /// endpoint in the background (the 22 MB/s EDM incident). An attacker-supplied, /// third-party-republished address list must therefore be short. pub const MAX_DECLARED_ADDRESSES: usize = 4; /// Why a holding was declared. Governs the default policy: a custodian exists /// *in order to* be findable, an interested holder does not. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum DeclareReason { /// Pushed to us by a `ReplicationRequest` — we never chose this content. /// Being findable IS the function, so these declare by default. Custodial, /// We fetched it because we wanted it (follow, pull, share link). These /// are the rows that carry the follow signal, so they declare sparingly. Interest, } impl DeclareReason { pub fn as_str(&self) -> &'static str { match self { DeclareReason::Custodial => "custodial", DeclareReason::Interest => "interest", } } pub fn from_str_label(s: &str) -> Self { match s { "custodial" => DeclareReason::Custodial, _ => DeclareReason::Interest, } } } /// Local declaration policy. Read from `settings` on every decision (cheap — /// three key lookups against an in-process SQLite) so a change takes effect /// without a restart. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct HoldingsPolicy { pub declare_custodial: bool, pub declare_interest: bool, /// Declare holdings of posts authored by one of OUR posting identities. /// /// **Default OFF, and not a discovery knob.** A declaration is a /// publishable "node N holds file F" claim. Made about a post we authored, /// it publishes `device ↔ posting identity` — the exact binding that /// Iteration A's AuthorManifest address strip and the v0.8 /// location-anonymity goal exist to prevent. Before Phase 2 the transport /// leaked this implicitly (the author served the bytes, so every recipient /// listed the author's device); re-creating it as a default would undo the /// change. Authors still SERVE direct requests; third-party findability /// comes from custodians and from interested holders, who declare. pub declare_own_authored: bool, /// Distinct declared posts per author. `0` = unlimited. pub cap_per_author: u32, } impl Default for HoldingsPolicy { /// **Alpha defaults are deliberately generous.** A tight cap makes content /// unfindable, and the 4-device alpha test has no spare holder population /// to absorb that. Both classes declare and there is no cap; the knob is /// implemented, defaulted open, and documented. Round 10's target is /// `cap_per_author = `[`ROUND10_TARGET_CAP_PER_AUTHOR`], to be adopted once /// the activity digest exists to carry coverage that the cap gives up. fn default() -> Self { Self { declare_custodial: true, declare_interest: true, declare_own_authored: false, cap_per_author: 0, } } } impl HoldingsPolicy { /// Is this declaration CLASS enabled? Checked separately from the cap /// because a disabled class must refuse even a file that was declared /// while the class was on — otherwise turning the knob off is write-only /// for the existing footprint. pub fn class_allows(&self, reason: DeclareReason) -> bool { match reason { DeclareReason::Custodial => self.declare_custodial, DeclareReason::Interest => self.declare_interest, } } /// Would this policy admit one more declaration? /// /// `distinct_posts_for_author` counts posts of that author we have ALREADY /// declared; `post_already_declared` is true when this declaration is for /// one of them (a second attachment of an already-declared post), in which /// case it does not grow the footprint and the cap must not reject it. pub fn allows( &self, reason: DeclareReason, distinct_posts_for_author: u32, post_already_declared: bool, ) -> bool { if !self.class_allows(reason) { return false; } if post_already_declared { return true; } self.cap_per_author == 0 || distinct_posts_for_author < self.cap_per_author } } /// How we want to be reached for files we declare: directly, or through an /// introducer that coordinates a hole punch. /// /// The introducer carries **no bulk data** — it answers `RelayIntroduce` /// (0xB0, always-on signalling) and the fetch then flows direct. This is not /// session relay and does not touch `relay.session_relay_enabled`. #[derive(Debug, Clone, PartialEq, Eq)] pub enum DeclaredReach { /// Publish our own addresses. Empty = bare node id, the default for every /// non-anchor (Round 10: addresses ride the wire only for anchors). Direct(Vec), /// Publish the introducer's address instead of ours. ViaIntroducer { node_id: NodeId, addresses: Vec }, } impl DeclaredReach { /// Split into the `(addresses, introducer)` pair the wire payload carries. pub fn into_payload_parts( self, ) -> (Vec, Option) { match self { DeclaredReach::Direct(addrs) => (addrs, None), DeclaredReach::ViaIntroducer { node_id, addresses } => ( Vec::new(), Some(crate::types::RedirectIntroducer { n: hex::encode(node_id), a: addresses, }), ), } } } #[cfg(test)] mod tests { use super::*; #[test] fn alpha_defaults_declare_everything_uncapped() { let p = HoldingsPolicy::default(); assert!(p.allows(DeclareReason::Custodial, 10_000, false)); assert!(p.allows(DeclareReason::Interest, 10_000, false)); assert!( !p.declare_own_authored, "declaring our own posts would republish device <-> persona" ); } #[test] fn cap_bites_only_on_new_posts() { let p = HoldingsPolicy { cap_per_author: ROUND10_TARGET_CAP_PER_AUTHOR, ..HoldingsPolicy::default() }; assert!(p.allows(DeclareReason::Interest, 0, false)); assert!(p.allows(DeclareReason::Interest, 1, false)); // At the cap a NEW post is refused... assert!(!p.allows(DeclareReason::Interest, 2, false)); // ...but another file of an already-declared post is not a new listing. assert!(p.allows(DeclareReason::Interest, 2, true)); } #[test] fn class_switch_overrides_cap() { let p = HoldingsPolicy { declare_interest: false, ..HoldingsPolicy::default() }; assert!(p.allows(DeclareReason::Custodial, 500, false)); assert!(!p.allows(DeclareReason::Interest, 0, false)); // A disabled class is disabled even for an already-declared post. assert!(!p.allows(DeclareReason::Interest, 0, true)); } #[test] fn reason_label_roundtrip() { for r in [DeclareReason::Custodial, DeclareReason::Interest] { assert_eq!(DeclareReason::from_str_label(r.as_str()), r); } } }