Skip to content
Merged
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
210 changes: 201 additions & 9 deletions pkgs/id/src/boards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,17 +1216,57 @@ impl BoardGraph {
// Current moderator frontier: a role attestation that another
// attestation supersedes is history, not authority. Following
// the signed supersedes links (never arrival order) keeps this
// identical on every peer holding the same set.
let mut attestation_superseded: BTreeSet<ArtifactHash> = BTreeSet::new();
for signed in self.objects.values() {
if let BoardObject::RoleAttestation(value) = &signed.object {
if &value.board == board {
if let Some(previous) = &value.supersedes {
attestation_superseded.insert(previous.clone());
}
// identical on every peer holding the same set. An attestation
// that is itself superseded — by an active generic supersede
// above, or by an active attestation's supersedes edge — has no
// effect, so its own supersedes edge does not apply either
// (#4003): superseding a revocation reinstates the grant it
// revoked. Like the generic frontier, this resolves to a fixed
// point over the active attestations.
let attestation_edges: Vec<(ArtifactHash, Option<ArtifactHash>)> = self
.objects
.iter()
.filter_map(|(hash, signed)| match &signed.object {
BoardObject::RoleAttestation(value) if &value.board == board => {
Some((hash.clone(), value.supersedes.clone()))
}
_ => None,
})
.collect();
let eligible_attestations: BTreeSet<ArtifactHash> = attestation_edges
.iter()
.map(|(hash, _)| hash.clone())
.filter(|hash| !superseded.contains(hash))
.collect();
// Recompute the active set from the eligible set on every pass,
// never by only shrinking the previous pass: in a chain
// grant -> revocation -> grant -> revocation, the middle
// revocation must reactivate once the grant superseding it is
// itself superseded, and a shrinking-only loop can never bring
// it back (Codex P1 on PR #4512). Supersedes edges always name
// an already-inserted record, so the graph is acyclic and this
// settles within one pass per chain link; the pass cap only
// bounds a graph that somehow broke that rule.
let mut active_attestations: BTreeSet<ArtifactHash> = eligible_attestations.clone();
let attestation_superseded: BTreeSet<ArtifactHash> = {
let mut named: BTreeSet<ArtifactHash> = BTreeSet::new();
for _ in 0..=attestation_edges.len() {
named = attestation_edges
.iter()
.filter(|(hash, _)| active_attestations.contains(hash))
.filter_map(|(_, previous)| previous.clone())
.collect();
let next: BTreeSet<ArtifactHash> = eligible_attestations
.difference(&named)
.cloned()
.collect();
if next == active_attestations {
break;
}
active_attestations = next;
}
}
named
};
let current_moderators: BTreeSet<String> = self
.objects
.iter()
Expand Down Expand Up @@ -2421,6 +2461,158 @@ mod tests {
assert_eq!(view.moderation_log.len(), 1);
}

#[test]
fn superseded_revocation_no_longer_revokes() {
// #4003: a revocation attestation that is itself superseded has
// no effect, so the grant it revoked is authority again.
let owner = key(41);
let moderator = key(42);
let author = key(43);
let other = key(44);
let moderator_key = hex_encode(&moderator.verifying_key().to_bytes());
let mut graph = BoardGraph::default();
let root_hash = graph.insert(board(&owner)).unwrap();
let thread_hash = graph.insert(thread(&owner, root_hash.clone(), "General")).unwrap();
let post_hash = graph
.insert(post(&author, root_hash.clone(), thread_hash, "hello"))
.unwrap();
let attestation = |seq: u64, member: String, granted: bool, supersedes: Option<ArtifactHash>| {
SignedBoardObject::sign(
&owner,
seq,
"",
BoardObject::RoleAttestation(RoleAttestation {
board: root_hash.clone(),
member,
role: BoardRole::Moderator,
granted,
supersedes,
}),
)
.unwrap()
};
let grant_hash = graph
.insert(attestation(2, moderator_key.clone(), true, None))
.unwrap();
graph
.insert(moderation(
&moderator,
1,
root_hash.clone(),
post_hash.clone(),
ModerationActionKind::Hide,
Some("spam"),
))
.unwrap();
let view = graph.compose_view(&root_hash, &ViewLayers::default()).unwrap();
assert_eq!(view.hidden.len(), 1);

// Revoke: the moderator's Hide stops applying.
let revocation_hash = graph
.insert(attestation(3, moderator_key.clone(), false, Some(grant_hash.clone())))
.unwrap();
let view = graph.compose_view(&root_hash, &ViewLayers::default()).unwrap();
assert!(view.objects.contains(&post_hash));
assert!(view.hidden.is_empty());

// Nullify the revocation with a generic Supersede whose
// replacement is an unrelated attestation: the original grant
// is the current frontier again and the Hide applies once more.
let other_grant_hash = graph
.insert(attestation(
4,
hex_encode(&other.verifying_key().to_bytes()),
true,
None,
))
.unwrap();
let supersede = SignedBoardObject::sign(
&owner,
5,
"",
BoardObject::Supersede(Supersede {
board: root_hash.clone(),
previous: revocation_hash,
replacement: other_grant_hash,
}),
)
.unwrap();
graph.insert(supersede).unwrap();
let view = graph.compose_view(&root_hash, &ViewLayers::default()).unwrap();
assert_eq!(view.hidden.len(), 1);
assert!(!view.objects.contains(&post_hash));
}

#[test]
fn attestation_chain_frontier_reinstates_and_re_revokes() {
// Chain grant -> revocation -> grant -> revocation, each
// attestation superseding its predecessor: only the last
// revocation is the frontier, the first revocation reactivates
// once the second grant falls, and the first grant stays
// revoked (Codex P1 on PR #4512).
let owner = key(45);
let moderator = key(46);
let author = key(47);
let moderator_key = hex_encode(&moderator.verifying_key().to_bytes());
let mut graph = BoardGraph::default();
let root_hash = graph.insert(board(&owner)).unwrap();
let thread_hash = graph.insert(thread(&owner, root_hash.clone(), "General")).unwrap();
let post_hash = graph
.insert(post(&author, root_hash.clone(), thread_hash, "hello"))
.unwrap();
let attestation = |seq: u64, granted: bool, supersedes: Option<ArtifactHash>| {
SignedBoardObject::sign(
&owner,
seq,
"",
BoardObject::RoleAttestation(RoleAttestation {
board: root_hash.clone(),
member: moderator_key.clone(),
role: BoardRole::Moderator,
granted,
supersedes,
}),
)
.unwrap()
};
let first_grant = graph.insert(attestation(2, true, None)).unwrap();
graph
.insert(moderation(
&moderator,
1,
root_hash.clone(),
post_hash.clone(),
ModerationActionKind::Hide,
Some("spam"),
))
.unwrap();
let hidden = |graph: &BoardGraph| {
graph
.compose_view(&root_hash, &ViewLayers::default())
.unwrap()
.hidden
.len()
};
assert_eq!(hidden(&graph), 1);

let first_revocation = graph
.insert(attestation(3, false, Some(first_grant)))
.unwrap();
assert_eq!(hidden(&graph), 0);

let second_grant = graph
.insert(attestation(4, true, Some(first_revocation)))
.unwrap();
assert_eq!(hidden(&graph), 1);

graph
.insert(attestation(5, false, Some(second_grant)))
.unwrap();
assert_eq!(hidden(&graph), 0);
let view = graph.compose_view(&root_hash, &ViewLayers::default()).unwrap();
assert!(view.objects.contains(&post_hash));
}

#[test]
fn superseded_supersede_record_has_no_effect() {
let owner = key(39);
Expand Down
Loading