Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions pkgs/id/crates/id-core/src/membership/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,44 @@ impl Membership {
Ok(managers)
}

/// The key members of `group` and their access at the causal position that `after`
/// names, sorted by key. Group-valued members are not returned: a caller expanding a
/// group reference one level (writer-set spec §D4) must not follow a nested group
/// reference, and key members are the only ones that can be admitted by it. This is
/// the enumeration counterpart of [`Membership::access_at_position`]: the answer is
/// pinned to the position `after` names, never the live roster [`Membership::members`]
/// reports.
///
/// # Errors
///
/// Returns [`MembershipError`] when an `after` hash is not indexed.
pub fn members_at_position(
&mut self,
after: &[Hash],
group: Hash,
) -> Result<Vec<([u8; 32], Access<()>)>, MembershipError> {
let heads: HashSet<Hash> = self.index.frontier(after, group)?.into_iter().collect();
Ok(key_members_at(&self.state, heads, group))
}

/// The same as [`Membership::members_at_position`], borrowing the layer immutably. A
/// caller that holds the state can ask about a position this way; it copies the
/// frontier index rather than memoising into it, so prefer the mutable method on a
/// hot path.
///
/// # Errors
///
/// Returns [`MembershipError`] when an `after` hash is not indexed.
pub fn members_at_position_shared(
&self,
after: &[Hash],
group: Hash,
) -> Result<Vec<([u8; 32], Access<()>)>, MembershipError> {
let mut index = self.index.clone();
let heads: HashSet<Hash> = index.frontier(after, group)?.into_iter().collect();
Ok(key_members_at(&self.state, heads, group))
}

/// Whether a create has been decided for `group`.
pub fn has_group(&self, group: Hash) -> bool {
self.state.has_group(AuthId::Group(group))
Expand Down Expand Up @@ -587,6 +625,26 @@ fn canonical_feed_order(events: &[CausalEvent]) -> Vec<CausalEvent> {
order
}

/// The key members of `group` at the auth frontier `heads`, sorted by key. Group-valued
/// members are skipped (one-level expansion, writer-set spec §D4).
fn key_members_at(
state: &GroupState,
heads: HashSet<Hash>,
group: Hash,
) -> Vec<([u8; 32], Access<()>)> {
let mut members: Vec<([u8; 32], Access<()>)> =
Graph::members_at(state, heads, AuthId::Group(group))
.unwrap_or_default()
.into_iter()
.filter_map(|(id, access)| match id {
AuthId::Key(key) => Some((key, access)),
AuthId::Group(_) => None,
})
.collect();
members.sort_unstable_by_key(|(key, _)| *key);
members
}

/// Replays `accepted` on a fresh graph.
fn rebuild(accepted: &[AuthOperation]) -> Result<GroupState, MembershipError> {
accepted.iter().try_fold(Graph::init(), |state, op| {
Expand Down
10 changes: 7 additions & 3 deletions pkgs/id/src/idset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
//! from, so its `set_id` is an owner-chosen opaque value, unverifiable by
//! design.
//!
//! This module is types and encoding only. Expansion, projection, and
//! counting are later slices of the spec's routing (§9 items 2–4) and are
//! deliberately absent here.
//! This module is the types and encoding. Expansion now lives beside the
//! types: [`expand`] wires [`view`]'s pure projection to the membership
//! layer (spec §9 item 2). Counting is a later slice of the spec's routing
//! (§9 item 4) and is deliberately absent here.

use cbor_core::{DecodeOptions, Strictness, Value};
use p2panda_core::Hash;
Expand All @@ -67,6 +68,9 @@ use unicode_normalization::is_nfc;
use crate::groups::Role;
use crate::waiting::AuthorKey;

/// Store-wired expansion over the membership layer (spec §9 item 2).
pub mod expand;

/// View projection and discovery over an explicitly pinned snapshot.
pub mod view;

Expand Down
Loading
Loading