Skip to main content

tuwunel_service/integrity/short_ids/
mod.rs

1//! Short event and state key ids, and the residue their damage leaves.
2//!
3//! The injectivity repair's final verification folds the same census into the
4//! outcome it records, so a run reports what verification would record for the
5//! database as it stands, each defect once. It compares columns, so it runs only
6//! where nothing else writes.
7
8pub(crate) mod check;
9pub(crate) mod gc;
10pub(crate) mod identity;
11pub(crate) mod references;
12pub(crate) mod rooms;
13pub(crate) mod state;
14
15use serde::{Deserialize, Serialize};
16use tuwunel_core::{
17	Result,
18	smallvec::SmallVec,
19	utils::{TryReadyExt, u64_from_bytes},
20};
21
22use self::check::check;
23use super::{Check, ScanExt, tally};
24use crate::Services;
25
26pub(crate) type Identity = SmallVec<[u8; 48]>;
27
28pub(super) static CHECK: Check = Check {
29	name: "short-ids",
30	about: "Short event and state key ids pair one to one, every state resolves cleanly onto \
31	        sound ones, and no unreachable state or unowned short room id remains",
32	quiescent: true,
33	walk: |services, limit| tally(check(services), limit),
34};
35
36/// Residue a completed repair leaves behind, as its outcome records it.
37///
38/// Each discriminant is the code a stored sample carries. The position in
39/// `SHAPES` is the slot its count occupies.
40#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
41#[serde(into = "u8", try_from = "u8")]
42pub(crate) enum Shape {
43	/// Alias, ambiguous or malformed event short-id rows.
44	EventAlias = 1,
45
46	/// Alias, ambiguous or malformed state-key short-id rows.
47	StatekeyAlias = 2,
48
49	/// A forward row whose short has no reverse row.
50	DanglingWinner = 3,
51
52	/// A reverse row whose identity has no forward row.
53	PromotableReverse = 4,
54
55	/// A state entry naming an alias or refused event short.
56	EventAliasEntry = 12,
57
58	/// A state entry naming an alias or refused state-key short.
59	StatekeyAliasEntry = 13,
60
61	/// A state whose history could not be verified or rebuilt.
62	AffectedState = 15,
63
64	/// A state diff that adds and removes the same entry.
65	DiffCollision = 19,
66
67	/// A state entry naming an event short without a reverse row.
68	EventOrphanEntry = 23,
69
70	/// A state entry naming a state-key short without a reverse row.
71	StatekeyOrphanEntry = 24,
72
73	/// A cached auth chain derived from a damaged short mapping.
74	AuthChainCache = 27,
75
76	/// An unreachable state the collector could not remove.
77	UnreachableState = 29,
78
79	/// A short room id left behind by a purged room.
80	PurgeResidue = 32,
81
82	/// A second short room id of a live room.
83	LiveRoomAlias = 33,
84
85	/// A legacy marker that declined the repair.
86	DeclinedMarker = 37,
87}
88
89#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
90#[serde(rename_all = "snake_case")]
91pub(crate) enum Reason {
92	Identity,
93	HistoricalState,
94	References,
95	Ownership,
96}
97
98pub(crate) fn short_of(bytes: &[u8]) -> Option<u64> { u64_from_bytes(bytes).ok() }
99
100#[tracing::instrument(level = "debug", skip(services, init, step))]
101async fn sweep<T: Send>(
102	services: &Services,
103	column: &str,
104	init: T,
105	step: impl Fn(T, &[u8], &[u8]) -> T + Sync,
106) -> Result<T> {
107	services.db[column]
108		.raw_stream()
109		.scanned(&services.server)
110		.ready_try_fold(init, |acc, (key, value)| Ok(step(acc, key, value)))
111		.await
112}