diff --git a/doc/2026-11-11T19-15-00Z_design_block_lookup/2026-11-11T19-15-00Z_design_block_lookup.md b/doc/2026-11-11T19-15-00Z_design_block_lookup/2026-11-11T19-15-00Z_design_block_lookup.md new file mode 100644 index 0000000000..328545449c --- /dev/null +++ b/doc/2026-11-11T19-15-00Z_design_block_lookup/2026-11-11T19-15-00Z_design_block_lookup.md @@ -0,0 +1,93 @@ +# Design: block-list lookups that can say "the lookup failed" (#8750) + +## Intent + +`BlockList::is_blocked` (crates/id-core/src/block_list.rs) returned only +`bool`. Both store implementations erased lookup failures to `false` — +`EventStore` with `unwrap_or(false)`, `SharedEventStore` with a second +erasure for poisoned locks — and every enforcement consumer +(`AbuseEvaluator`, `PullGate`, the blob gate) treated that `false` as a +successful "not blocked". A corrupt block row, a transient storage +failure, or a poisoned lock therefore silently allowed connections and +pulls, indistinguishable from an ordinary unblocked result, with no +operator diagnostic anywhere. + +The ask from #8750: failure observability and an explicit per-surface +indeterminate-result policy. Not a unilateral change to fail-closed — +the documented fail-open default is an availability choice and stays. + +## Approach + +- **One distinguishing outcome type at the trait boundary.** + `BlockLookup` = `Blocked` | `Unblocked` | `Indeterminate(BlockLookupFailure)`, + added to the `BlockList` trait as `lookup()` with `peer_lookup` / + `topic_tag_lookup` / `ip_lookup` helpers mirroring the bool helpers. + The default `lookup()` derives from `is_blocked`, so implementations + that cannot report failures keep compiling and keep their semantics; + only the two real stores override it. +- **Failure as categories, never storage details.** + `BlockLookupFailure` = `Corrupt` | `Storage` | `Unavailable`. The + category is all that crosses the seam: a remote peer must never learn + store internals (file layout, lock state, error text) from an + enforcement decision. The full store error goes to the operator log + only, via `tracing::warn!` at the implementation that saw it. +- **The bool seam cannot diverge.** Each store's `is_blocked` is now + `lookup(...) == Blocked`, one code path for both answers. The bool + keeps its documented meaning: indeterminate fails open. +- **Store mapping.** `EventStore`: `StoreError::Corrupt` -> `Corrupt`, + any other store error -> `Storage`. `SharedEventStore`: the same for + errors from the backend, and a poisoned lock (or any other + trait-layer failure to run the read) -> `Unavailable`. +- **Operator surfaces.** `LocalEvents::lookup_block` exposes the + distinguishing answer next to `is_blocked`, and `PullGate` gains + `peer_lookup` / `topic_tag_lookup` returning `Option` + (`None` = no list attached, as before). + +## Decisions and rationale + +- **Fail-open at every enforcement seam, stated explicitly.** The + connection seam (`AbuseEvaluator::open_connection_inner`), the pull + gate (`PullGate::admits`), blob serving (`decide_with_blocks`), and + the log-sync topic check each match on the lookup: `Blocked` + refuses; `Indeterminate` logs a `tracing::warn!` diagnostic naming + the failure category and proceeds; `Unblocked` proceeds. Rationale: + a block store that cannot answer must not become a silent, + fleet-wide connection/pull refusal (availability), but the failure + must stop posing as a clean "not blocked" (observability). A future + seam that needs fail-closed can choose it on the same type without + another API change. +- **No change to definite-answer semantics.** Active, absent, and + expired lookups behave exactly as before; the pre-existing block + tests pass unmodified, which is the regression oracle. +- **Diagnostics at two layers.** The store implementations log the + failure with the underlying error; each enforcement seam logs its + own policy application. Either layer alone would leave a gap (a + failure with no decision context, or a decision with no cause). + +## Tests (the oracle) + +A failed lookup must be distinguishable from an ordinary unblocked +result, per surface: + +- id-events: a raw undecodable block row written into the store yields + `Indeterminate(Corrupt)`; a healthy row yields `Blocked`; an absent + one `Unblocked`; the bool seam still reads `false` for the corrupt + row. +- id-store: `SharedEventStore` distinguishes `Blocked` / `Unblocked`, + and a poisoned lock yields `Indeterminate(Unavailable)`. +- id-core abuse: an always-indeterminate list fails open at the + connection seam; definite blocks still refuse. +- id-events pull policy: the gate surfaces `Some(Indeterminate(..))`, + the bool seam reads false, and `admits` follows the stated policy. + +--- + +## 2026-11-11T19-15-00Z Update: record formalized during the implementing PR + +The design above was settled in the #8750 issue body and the queue +task packet before implementation (the packet's acceptance criteria +are the test oracle listed here). This file formalizes that record in +the repository's prescribed location; it was added to PR #8761 after +the Codex review (finding on block_list.rs) pointed out the missing +dated record. No design content changed in response — only its +location. diff --git a/pkgs/id/crates/id-core/src/abuse.rs b/pkgs/id/crates/id-core/src/abuse.rs index 23c0b3426d..c409986b56 100644 --- a/pkgs/id/crates/id-core/src/abuse.rs +++ b/pkgs/id/crates/id-core/src/abuse.rs @@ -52,7 +52,7 @@ use std::collections::{HashMap, HashSet}; use std::net::IpAddr; use std::sync::Arc; -use crate::block_list::BlockList; +use crate::block_list::{BlockList, BlockLookup}; /// A remote endpoint identity: the 32 bytes of its verifying key, /// the same bytes the event store uses as an author key. @@ -1138,16 +1138,37 @@ impl AbuseEvaluator { direct_ip: Option, now_ms: u64, ) -> Result { - if self - .blocks - .as_ref() - .is_some_and(|blocks| blocks.peer_blocked(&endpoint)) - { - return Err(Refusal::blocked_by_operator()); - } - if let (Some(blocks), Some(ip)) = (&self.blocks, direct_ip) { - if blocks.ip_blocked(&ip) { - return Err(Refusal::blocked_by_operator()); + if let Some(blocks) = &self.blocks { + // Explicit indeterminate-result policy (#8750): this seam + // fails open — a block store that cannot answer must not + // become a silent connection refusal — and the failure is + // logged for the operator instead of being treated as a + // clean "not blocked". Only a definite block refuses. + match blocks.peer_lookup(&endpoint) { + BlockLookup::Blocked => { + return Err(Refusal::blocked_by_operator()); + } + BlockLookup::Indeterminate(failure) => { + tracing::warn!( + "block-list peer lookup for a new connection failed ({}); failing open", + failure.as_str() + ); + } + BlockLookup::Unblocked => {} + } + if let Some(ip) = direct_ip { + match blocks.ip_lookup(&ip) { + BlockLookup::Blocked => { + return Err(Refusal::blocked_by_operator()); + } + BlockLookup::Indeterminate(failure) => { + tracing::warn!( + "block-list IP lookup for a new connection failed ({}); failing open", + failure.as_str() + ); + } + BlockLookup::Unblocked => {} + } } } let cap = self.policy.max_connections_per_peer; @@ -1479,6 +1500,39 @@ mod tests { .expect("an unblocked endpoint still connects"); } + /// A block list whose lookup never answers (#8750): every check is + /// indeterminate, as a failed store read is at the real seams. + #[derive(Debug)] + struct FailingBlocks; + + impl crate::block_list::BlockList for FailingBlocks { + fn is_blocked(&self, _kind: crate::block_list::BlockKind, _value: &[u8]) -> bool { + false + } + + fn lookup( + &self, + _kind: crate::block_list::BlockKind, + _value: &[u8], + ) -> crate::block_list::BlockLookup { + crate::block_list::BlockLookup::Indeterminate( + crate::block_list::BlockLookupFailure::Unavailable, + ) + } + } + + #[test] + fn an_indeterminate_block_lookup_fails_open_at_the_connection_seam() { + // #8750 policy, stated at the seam: an indeterminate block + // lookup fails open (and is logged) — the connection is + // admitted, not silently refused, and the failure stays + // visible through `lookup` instead of posing as "not blocked". + let mut eval = evaluator().with_block_list(Arc::new(FailingBlocks)); + eval.open_connection(endpoint(7), 0) + .expect("an indeterminate block lookup must fail open"); + assert_eq!(eval.active_connection_count(&endpoint(7)), 1); + } + #[test] fn a_blocked_direct_ip_is_refused_a_new_connection() { // #2245 listener seam: with a block list attached, a connection diff --git a/pkgs/id/crates/id-core/src/block_list.rs b/pkgs/id/crates/id-core/src/block_list.rs index a6683e29ca..94999c9365 100644 --- a/pkgs/id/crates/id-core/src/block_list.rs +++ b/pkgs/id/crates/id-core/src/block_list.rs @@ -42,6 +42,60 @@ impl BlockKind { } } +/// The outcome of one block-list lookup (#8750): a definite answer, or +/// the explicit admission that the lookup itself failed. +/// +/// The bool seam ([`BlockList::is_blocked`]) erases a failed lookup (a +/// corrupt block row, a storage error, a poisoned lock) to `false`, so +/// enforcement could not tell it from an ordinary unblocked result and +/// no diagnostic existed anywhere. A seam that needs the distinction +/// asks for a `BlockLookup` and chooses its indeterminate-result policy +/// explicitly; the store implementations report their real failures +/// here instead of erasing them. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlockLookup { + /// An active block names this value. + Blocked, + /// The lookup succeeded and no active block names this value. + Unblocked, + /// The lookup itself failed, so no answer exists. The failure is a + /// category, never a storage detail: nothing about the store's + /// internals may leak to a remote peer through this value. + Indeterminate(BlockLookupFailure), +} + +impl BlockLookup { + /// The bool verdict: only a definite block blocks. An indeterminate + /// lookup fails open, exactly as the bool seam always has. + #[must_use] + pub fn is_blocked(&self) -> bool { + matches!(self, BlockLookup::Blocked) + } +} + +/// Why a block-list lookup could not answer (#8750), in operator terms. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlockLookupFailure { + /// A stored block row does not decode. + Corrupt, + /// The storage layer failed to answer the read. + Storage, + /// The lookup could not run at all (a poisoned lock, a closed store). + Unavailable, +} + +impl BlockLookupFailure { + /// The failure's operator-facing label, for logs and diagnostics. + #[must_use] + pub fn as_str(&self) -> &'static str { + match self { + BlockLookupFailure::Corrupt => "corrupt block row", + BlockLookupFailure::Storage => "block storage read failed", + BlockLookupFailure::Unavailable => "block list unavailable", + } + } +} + /// The operator block list as the enforcement seams consult it (#2245). /// /// A seam holds an `Arc` and asks it about a peer, a topic @@ -61,8 +115,46 @@ impl BlockKind { pub trait BlockList: std::fmt::Debug + Send + Sync + 'static { /// Whether (`kind`, `value`) is blocked now. An expired block is not /// enforced, so this is false for one, however long it stays listed. + /// + /// The bool seam fails open by design — the documented default + /// availability behaviour: a lookup that cannot answer (see + /// [`BlockList::lookup`]) is `false` here, so serving never depends + /// on the block store answering. A caller that must tell a failed + /// lookup from an unblocked answer uses `lookup` and states its + /// indeterminate-result policy explicitly (#8750). fn is_blocked(&self, kind: BlockKind, value: &[u8]) -> bool; + /// The distinguishing form of [`BlockList::is_blocked`]: the same + /// question, with a failed lookup preserved as + /// [`BlockLookup::Indeterminate`] instead of erased to `false`. + /// + /// The default derives from `is_blocked`, so an implementation + /// that cannot report failures answers only the definite outcomes; + /// the store implementations override it with their real errors, + /// and log the failure for the operator when it happens. + fn lookup(&self, kind: BlockKind, value: &[u8]) -> BlockLookup { + if self.is_blocked(kind, value) { + BlockLookup::Blocked + } else { + BlockLookup::Unblocked + } + } + + /// The [`BlockLookup`] form of [`BlockList::peer_blocked`]. + fn peer_lookup(&self, peer: &[u8; 32]) -> BlockLookup { + self.lookup(BlockKind::PeerKey, peer) + } + + /// The [`BlockLookup`] form of [`BlockList::topic_tag_blocked`]. + fn topic_tag_lookup(&self, tag: &[u8]) -> BlockLookup { + self.lookup(BlockKind::TopicTag, tag) + } + + /// The [`BlockLookup`] form of [`BlockList::ip_blocked`]. + fn ip_lookup(&self, ip: &std::net::IpAddr) -> BlockLookup { + self.lookup(BlockKind::Ip, ip.to_string().as_bytes()) + } + /// Whether the endpoint with this 32-byte key is blocked. fn peer_blocked(&self, peer: &[u8; 32]) -> bool { self.is_blocked(BlockKind::PeerKey, peer) @@ -87,4 +179,8 @@ impl BlockList for std::sync::Arc { fn is_blocked(&self, kind: BlockKind, value: &[u8]) -> bool { (**self).is_blocked(kind, value) } + + fn lookup(&self, kind: BlockKind, value: &[u8]) -> BlockLookup { + (**self).lookup(kind, value) + } } diff --git a/pkgs/id/crates/id-events/src/event_store/mod.rs b/pkgs/id/crates/id-events/src/event_store/mod.rs index 39d7db09da..346264e51a 100644 --- a/pkgs/id/crates/id-events/src/event_store/mod.rs +++ b/pkgs/id/crates/id-events/src/event_store/mod.rs @@ -73,11 +73,13 @@ mod waiting_rows; mod writer_set; use id_core::waiting::{AuthorKey, WaitRefusal, WaitingLimits, WaitingPool}; +pub use id_store::event_types::{ + Block, BlockKind, BlockList, BlockLookup, BlockLookupFailure, Chain, FILE_NAME, StoreError, +}; pub use id_store::event_types::{Ingest, Refusal}; use ingest::{is_present, is_proof_header, present_in}; use waiting_rows::{Mirror, Row}; pub use writer_set::{DiscoveredWriterSet, WriterSetStoreError}; -pub use id_store::event_types::{Block, BlockKind, BlockList, Chain, StoreError, FILE_NAME}; impl EventStore { /// Adds `block` to the operator block list, replacing any row that names @@ -368,9 +370,30 @@ pub struct EventStore { impl BlockList for EventStore { fn is_blocked(&self, kind: BlockKind, value: &[u8]) -> bool { - // A read error invents no block: the seam fails open, and the - // store's error still surfaces on the store's own read paths. - EventStore::is_blocked(self, kind, value, id_core::time::unix_ms()).unwrap_or(false) + // The bool seam fails open (see the trait docs): a read error + // invents no block. `lookup` preserves the failure for the + // seams that distinguish it (#8750). + self.lookup(kind, value).is_blocked() + } + + fn lookup(&self, kind: BlockKind, value: &[u8]) -> BlockLookup { + match EventStore::is_blocked(self, kind, value, id_core::time::unix_ms()) { + Ok(true) => BlockLookup::Blocked, + Ok(false) => BlockLookup::Unblocked, + Err(err) => { + let failure = if matches!(err, StoreError::Corrupt(_)) { + BlockLookupFailure::Corrupt + } else { + BlockLookupFailure::Storage + }; + // The operator-visible diagnostic (#8750): the failure + // category and the store error stay on this side of the + // seam; a remote peer only ever sees the fail-open + // verdict the caller's policy chooses. + tracing::warn!("block-list lookup failed ({}): {err}", failure.as_str()); + BlockLookup::Indeterminate(failure) + } + } } } @@ -1824,6 +1847,50 @@ mod tests { assert_eq!(store.blocks().unwrap().len(), 4); } + /// #8750 error oracle: a block row that cannot decode is a failed + /// lookup, and the trait must say so — `Indeterminate(Corrupt)` — + /// instead of erasing it to the same `false` an ordinary unblocked + /// answer returns. The bool seam keeps its fail-open meaning. + #[test] + fn test_block_list_lookup_reports_a_corrupt_row_as_indeterminate() { + let (_dir, store) = open_temp(); + store + .add_block(&block(BlockKind::PeerKey, &[7u8; 32], None)) + .unwrap(); + // A row whose value cannot decode, written raw under a second + // peer's key, exactly as a damaged store file would hold it. + { + let mut key = vec![BlockKind::PeerKey.tag()]; + key.extend_from_slice(&[9u8; 32]); + let txn = store.db.begin_write().unwrap(); + { + let mut blocks = txn.open_table(BLOCKS).unwrap(); + blocks.insert(key.as_slice(), b"x".as_slice()).unwrap(); + } + txn.commit().unwrap(); + } + let list: &dyn BlockList = &store; + assert_eq!( + list.lookup(BlockKind::PeerKey, &[7u8; 32]), + BlockLookup::Blocked + ); + assert_eq!( + list.lookup(BlockKind::PeerKey, &[8u8; 32]), + BlockLookup::Unblocked + ); + assert_eq!( + list.lookup(BlockKind::PeerKey, &[9u8; 32]), + BlockLookup::Indeterminate(BlockLookupFailure::Corrupt), + "a failed lookup must not read as an ordinary unblocked result" + ); + assert_eq!( + list.peer_lookup(&[9u8; 32]), + BlockLookup::Indeterminate(BlockLookupFailure::Corrupt) + ); + // The bool seam keeps its documented fail-open meaning. + assert!(!list.peer_blocked(&[9u8; 32])); + } + /// The #2245 invariant: a block changes serving and subscribing only. /// Signed events from a blocked author are ingested and applied by the /// ordinary rules — if ingest ever consulted the block list, the graph diff --git a/pkgs/id/crates/id-events/src/local_events.rs b/pkgs/id/crates/id-events/src/local_events.rs index 6b0212a70e..01160a61e0 100644 --- a/pkgs/id/crates/id-events/src/local_events.rs +++ b/pkgs/id/crates/id-events/src/local_events.rs @@ -24,7 +24,7 @@ use p2panda_core::{Hash, SigningKey}; use id_store::chain::{ChainExt, Event, MAX_AFTER, sign}; use id_store::chat::{CHAT_MESSAGE_KIND, ChatMessage, ChatRecord, SendRate}; use id_store::documents::{DOCUMENT_KIND, DocumentRef, REMOVAL_KIND}; -use crate::event_store::{Block, BlockKind, EventStore, Ingest}; +use crate::event_store::{Block, BlockKind, BlockLookup, EventStore, Ingest}; use crate::local_claims::LocalClaims; use id_store::notes::{ AppliedNote, CREATE_KIND as NOTE_CREATE_KIND, NoteFoldError, NoteOp, NoteRecord, @@ -322,6 +322,15 @@ impl LocalEvents { crate::event_store::BlockList::is_blocked(&self.store, kind, value) } + /// The distinguishing form of [`LocalEvents::is_blocked`] (#8750): + /// a failed lookup is preserved as + /// [`BlockLookup::Indeterminate`] for the operator surfaces, + /// instead of erased to "not blocked". + #[must_use] + pub fn lookup_block(&self, kind: BlockKind, value: &[u8]) -> BlockLookup { + crate::event_store::BlockList::lookup(&self.store, kind, value) + } + /// Publishes `content` as a document of `media_type`, with a tag for each of `tags`. The event /// names the content by its BLAKE3 hash and size. The bytes are not kept here: they are a blob. /// Every tag is checked before anything is recorded, so a bad tag records nothing. diff --git a/pkgs/id/crates/id-events/src/pull_policy.rs b/pkgs/id/crates/id-events/src/pull_policy.rs index 8bf3502912..ab7d323587 100644 --- a/pkgs/id/crates/id-events/src/pull_policy.rs +++ b/pkgs/id/crates/id-events/src/pull_policy.rs @@ -32,7 +32,7 @@ use std::sync::Arc; use iroh_base::EndpointId; use crate::world_access::WorldAccess; -use id_store::event_types::BlockList; +use id_store::event_types::{BlockList, BlockLookup}; /// Who may pull a world's events from this server. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -100,12 +100,24 @@ impl PullGate { } /// Whether the operator block list blocks `peer` by key. False when - /// no list is attached. + /// no list is attached, and false when the lookup cannot answer + /// (the fail-open bool seam; see [`PullGate::peer_lookup`]). #[must_use] pub fn peer_blocked(&self, peer: &EndpointId) -> bool { + self.peer_lookup(peer) + .is_some_and(|lookup| lookup.is_blocked()) + } + + /// The block list's distinguishing answer for `peer` (#8750): + /// `None` when no list is attached, otherwise the lookup itself, + /// with a failed lookup preserved as + /// [`BlockLookup::Indeterminate`] instead of erased to "not + /// blocked". + #[must_use] + pub fn peer_lookup(&self, peer: &EndpointId) -> Option { self.blocks .as_ref() - .is_some_and(|blocks| blocks.peer_blocked(peer.as_bytes())) + .map(|blocks| blocks.peer_lookup(peer.as_bytes())) } /// Whether the operator block list blocks this topic tag (raw bytes: @@ -114,19 +126,47 @@ impl PullGate { /// not name — the `log_sync` handshake — check the tag through here. #[must_use] pub fn topic_tag_blocked(&self, tag: &[u8]) -> bool { + self.topic_tag_lookup(tag) + .is_some_and(|lookup| lookup.is_blocked()) + } + + /// The [`BlockLookup`] form of [`PullGate::topic_tag_blocked`] + /// (#8750): `None` when no list is attached. + #[must_use] + pub fn topic_tag_lookup(&self, tag: &[u8]) -> Option { self.blocks .as_ref() - .is_some_and(|blocks| blocks.topic_tag_blocked(tag)) + .map(|blocks| blocks.topic_tag_lookup(tag)) } /// Whether `peer` may pull this world's events now: not blocked by /// key, the world not blocked as a topic tag, and the policy admits /// the peer. + /// + /// Explicit indeterminate-result policy (#8750): the gate fails + /// open — a block lookup that cannot answer does not refuse the + /// pull (availability, as documented on + /// [`BlockList`](id_store::event_types::BlockList)) — and the + /// failure is logged for the operator instead of being treated as + /// a clean "not blocked". Only a definite block refuses here. #[must_use] pub fn admits(&self, peer: &EndpointId) -> bool { - !self.peer_blocked(peer) - && !self.topic_tag_blocked(self.world.as_bytes()) - && self.policy.admits(self.access.as_ref(), &self.world, peer) + for lookup in [ + self.peer_lookup(peer), + self.topic_tag_lookup(self.world.as_bytes()), + ] { + match lookup { + Some(BlockLookup::Blocked) => return false, + Some(BlockLookup::Indeterminate(failure)) => { + tracing::warn!( + "pull gate: block-list lookup failed ({}); failing open", + failure.as_str() + ); + } + Some(BlockLookup::Unblocked) | None => {} + } + } + self.policy.admits(self.access.as_ref(), &self.world, peer) } } @@ -420,6 +460,56 @@ mod tests { ); } + /// A block list whose lookup never answers (#8750): every check is + /// indeterminate, as a failed store read is at the real seams. + #[derive(Debug)] + struct FailingBlocks; + + impl id_store::event_types::BlockList for FailingBlocks { + fn is_blocked(&self, _kind: id_store::event_types::BlockKind, _value: &[u8]) -> bool { + false + } + + fn lookup( + &self, + _kind: id_store::event_types::BlockKind, + _value: &[u8], + ) -> id_store::event_types::BlockLookup { + id_store::event_types::BlockLookup::Indeterminate( + id_store::event_types::BlockLookupFailure::Storage, + ) + } + } + + /// #8750: the gate surfaces a failed lookup as indeterminate — + /// distinguishable from an ordinary unblocked answer — and its + /// stated policy fails open: the pull is admitted, the bool seam + /// reads false, and the failure is logged at the seam. + #[test] + fn test_pull_gate_surfaces_an_indeterminate_block_lookup_and_fails_open() { + let gate = PullGate::new( + PullPolicy::Open, + WORLD, + live(docs_world(key(1).public(), key(2).public(), false)), + ) + .with_blocks(Arc::new(FailingBlocks)); + assert_eq!( + gate.peer_lookup(&key(2).public()), + Some(id_store::event_types::BlockLookup::Indeterminate( + id_store::event_types::BlockLookupFailure::Storage + )), + "a failed lookup must not read as an ordinary unblocked result" + ); + assert!( + !gate.peer_blocked(&key(2).public()), + "the bool seam fails open" + ); + assert!( + gate.admits(&key(2).public()), + "the gate's stated policy fails open" + ); + } + /// #2245: a blocked topic tag is not pulled — a gate whose world is a /// blocked tag serves nothing, to members included. #[test] diff --git a/pkgs/id/crates/id-groups/src/blob_gate.rs b/pkgs/id/crates/id-groups/src/blob_gate.rs index 51f0913820..aa00dac8a3 100644 --- a/pkgs/id/crates/id-groups/src/blob_gate.rs +++ b/pkgs/id/crates/id-groups/src/blob_gate.rs @@ -35,7 +35,7 @@ use iroh_blobs::{ }; use id_core::access::AccessPolicy; -use id_events::event_store::BlockList; +use id_events::event_store::{BlockList, BlockLookup}; /// The two questions the gate asks of the world graph. The trait is /// sync-gating vocabulary owned by the events layer since step 2b of @@ -116,8 +116,22 @@ pub fn decide_with_blocks( hash: &Hash, request: Request, ) -> bool { - if blocks.is_some_and(|blocks| blocks.peer_blocked(requester.as_bytes())) { - return false; + if let Some(blocks) = blocks { + // Explicit indeterminate-result policy (#8750): blob serving + // fails open — a block lookup that cannot answer does not + // refuse the request — and the failure is logged for the + // operator instead of being treated as a clean "not blocked". + // Only a definite block refuses here. + match blocks.peer_lookup(requester.as_bytes()) { + BlockLookup::Blocked => return false, + BlockLookup::Indeterminate(failure) => { + tracing::warn!( + "blob gate: block-list lookup failed ({}); failing open", + failure.as_str() + ); + } + BlockLookup::Unblocked => {} + } } access.worlds_referencing(hash).iter().any(|world| { access.is_current_member(world, requester) diff --git a/pkgs/id/crates/id-store/src/event_types.rs b/pkgs/id/crates/id-store/src/event_types.rs index 8d34a3f910..f9a705b6bd 100644 --- a/pkgs/id/crates/id-store/src/event_types.rs +++ b/pkgs/id/crates/id-store/src/event_types.rs @@ -16,7 +16,7 @@ use redb::{CommitError, DatabaseError, StorageError, TableError, TransactionErro use id_core::rejection::Reason; use id_core::waiting::AuthorKey; -pub use id_core::block_list::{BlockKind, BlockList}; +pub use id_core::block_list::{BlockKind, BlockList, BlockLookup, BlockLookupFailure}; /// The store's file name in its directory. pub const FILE_NAME: &str = "events.redb"; diff --git a/pkgs/id/crates/id-store/src/store_traits.rs b/pkgs/id/crates/id-store/src/store_traits.rs index 7bec98e5f3..c9beb8eaa6 100644 --- a/pkgs/id/crates/id-store/src/store_traits.rs +++ b/pkgs/id/crates/id-store/src/store_traits.rs @@ -37,7 +37,9 @@ use p2panda_store::topics::TopicStore; use serde::{Deserialize, Serialize}; use crate::chain::Event; -use crate::event_types::{BlockKind, BlockList, Ingest, StoreError}; +use crate::event_types::{ + BlockKind, BlockList, BlockLookup, BlockLookupFailure, Ingest, StoreError, +}; use id_core::waiting::AuthorKey; @@ -393,10 +395,33 @@ impl BlockList for SharedEventStore { /// The wall clock judges expiry, and a read error or a poisoned lock /// invents no block. fn is_blocked(&self, kind: BlockKind, value: &[u8]) -> bool { - self.read(|store| { - Ok(store.is_blocked_at(kind, value, id_core::time::unix_ms()).unwrap_or(false)) - }) - .unwrap_or(false) + // The bool seam fails open (see the trait docs): a read error + // or a poisoned lock invents no block. `lookup` preserves the + // failure for the seams that distinguish it (#8750). + self.lookup(kind, value).is_blocked() + } + + fn lookup(&self, kind: BlockKind, value: &[u8]) -> BlockLookup { + let answered = + self.read(|store| Ok(store.is_blocked_at(kind, value, id_core::time::unix_ms())?)); + match answered { + Ok(true) => BlockLookup::Blocked, + Ok(false) => BlockLookup::Unblocked, + Err(TraitStoreError::Store(err)) => { + let failure = if matches!(err, StoreError::Corrupt(_)) { + BlockLookupFailure::Corrupt + } else { + BlockLookupFailure::Storage + }; + tracing::warn!("block-list lookup failed ({}): {err}", failure.as_str()); + BlockLookup::Indeterminate(failure) + } + Err(err) => { + let failure = BlockLookupFailure::Unavailable; + tracing::warn!("block-list lookup failed ({}): {err}", failure.as_str()); + BlockLookup::Indeterminate(failure) + } + } } } diff --git a/pkgs/id/crates/id-store/tests/store_traits.rs b/pkgs/id/crates/id-store/tests/store_traits.rs index 84aa9ec34f..cc9da6b8ed 100644 --- a/pkgs/id/crates/id-store/tests/store_traits.rs +++ b/pkgs/id/crates/id-store/tests/store_traits.rs @@ -21,7 +21,9 @@ use p2panda_store::topics::TopicStore; use id::event_store::EventStore; use id_core::waiting::{AuthorKey, WaitingLimits}; use id_store::chain::{ChainExt, Event, sign}; -use id_store::event_types::{BlockKind, Ingest, StoreError}; +use id_store::event_types::{ + Block, BlockKind, BlockList, BlockLookup, BlockLookupFailure, Ingest, StoreError, +}; use id_store::store_traits::{ChainLog, LogEntries, SharedEventStore, TraitStoreError}; @@ -700,3 +702,50 @@ async fn test_a_poisoned_lock_is_reported_not_unwrapped() { Err(TraitStoreError::Poisoned) )); } + +/// #8750: the shared store's block lookup distinguishes a definite +/// block and a definite unblocked answer from a lookup that could not +/// run at all — a poisoned lock is `Indeterminate(Unavailable)`, never +/// the same answer an unblocked peer gets. The bool seam keeps its +/// documented fail-open meaning. +#[test] +#[allow(clippy::panic)] +fn test_shared_store_block_lookup_distinguishes_blocked_unblocked_and_unavailable() { + let dir = tempfile::tempdir().unwrap(); + let backend = EventStore::open(dir.path(), WaitingLimits::default(), 0).unwrap(); + backend + .add_block(&Block { + kind: BlockKind::PeerKey, + value: vec![7u8; 32], + added_at_ms: 1_000, + reason: "test block".to_owned(), + expires_at_ms: None, + }) + .unwrap(); + let store = SharedEventStore::new(backend); + let list: &dyn BlockList = &store; + assert_eq!( + list.lookup(BlockKind::PeerKey, &[7u8; 32]), + BlockLookup::Blocked + ); + assert_eq!( + list.lookup(BlockKind::PeerKey, &[8u8; 32]), + BlockLookup::Unblocked + ); + let poisoner = store.clone(); + let panicked = std::thread::spawn(move || { + let _guard = poisoner.write_guard_for_test(); + panic!("poisoning the store lock on purpose"); + }) + .join(); + assert!(panicked.is_err()); + assert_eq!( + list.lookup(BlockKind::PeerKey, &[8u8; 32]), + BlockLookup::Indeterminate(BlockLookupFailure::Unavailable), + "a poisoned lock must not read as an ordinary unblocked result" + ); + assert!( + !list.is_blocked(BlockKind::PeerKey, &[8u8; 32]), + "the bool seam fails open on an indeterminate lookup" + ); +} diff --git a/pkgs/id/crates/id-sync/src/log_sync.rs b/pkgs/id/crates/id-sync/src/log_sync.rs index 0d48601391..923cee1473 100644 --- a/pkgs/id/crates/id-sync/src/log_sync.rs +++ b/pkgs/id/crates/id-sync/src/log_sync.rs @@ -50,7 +50,7 @@ use tokio::time::timeout; use id_store::chain::ChainExt; use id_core::connectivity::connect; -use id_events::event_store::Ingest; +use id_events::event_store::{BlockLookup, Ingest}; use id_core::frames::{read_frame, write_frame}; use id_events::pull_policy::PullGate; use id_events::event_store::sync::RangeStore; @@ -320,7 +320,18 @@ where // is also checked against the requested topic's hash bytes, the // form this seam names a topic in. Refused exactly as a pull // refusal, before the method is agreed. - if gate.topic_tag_blocked(request.topic.as_bytes()) { + let topic_lookup = gate.topic_tag_lookup(request.topic.as_bytes()); + if let Some(BlockLookup::Indeterminate(failure)) = topic_lookup { + // Explicit indeterminate-result policy (#8750): this seam + // fails open, like the gate itself, and logs the failure + // for the operator instead of treating it as a clean + // "tag not blocked". + tracing::warn!( + "log sync: block-list lookup for the requested topic failed ({}); failing open", + failure.as_str() + ); + } + if topic_lookup == Some(BlockLookup::Blocked) { send_message(&mut send, &Answer::Refuse).await?; finish_stream(&mut send, &mut recv).await; return Err(SyncError::PullRefused);