maarcadetweet: initial commit
AT Protocol PDS + AppView + Tauri Desktop Client, 160-char post limit. - PDS (Rust + axum + sqlx) - Auth: createAccount, createSession, refreshSession - Records: createRecord, deleteRecord (race-safe via SELECT FOR UPDATE) - Feed: feed.like.create, feed.repost.create - Sync: getRepo, getBlocks, getLatestCommit, getRecord (with MST proof), listRepos - Identity: resolveHandle - MST: spec-conformant (at-mst crate, 27 tests) - Repo: signed commits, TID counter (monotonic, 4096 wrap safe) - AppView (Rust + axum + sqlx) - Jetstream consumer (WebSocket, exponential backoff, 38k+ events indexed) - REST API: timeline/home (graph-aware), profile, search, post (with thread hydration) - Handle-sync worker (did:plc + did:web) - JSONB embed storage + thread columns (migration 0003) - Like/repost counter cache (migration 0004) - Tauri 2 + Svelte 5 Desktop Client - System tray (Show/Compose/Quit menu) - OS notifications (tauri-plugin-notification) - Auto-update (tauri-plugin-updater, placeholder endpoint) - Window-state (tauri-plugin-window-state) - 160-char compose with live counter - Image/Link embed rendering - LocalStorage-persisted like state - Timeline with poll (prepend new posts) - Custom TitleBar (transparent, no decorations) - Orange/IBM Plex Mono maarcade design Tests: 231 Rust + 9 vitest = 240 passed.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
pub mod node;
|
||||
pub mod tree;
|
||||
pub mod util;
|
||||
|
||||
pub use node::{MstEntry, MstNode, NodeKind};
|
||||
pub use tree::Mst;
|
||||
@@ -0,0 +1,155 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use cid::Cid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use at_crypto::cid::cid_for_cbor;
|
||||
|
||||
/// A single MST entry. The `key` is the **base64url-encoded** form of the
|
||||
/// user-facing key string. The `tree` is the CID of the sub-tree immediately
|
||||
/// to the right of this entry (i.e. the sub-tree that contains all keys
|
||||
/// strictly between this entry's key and the next entry's key).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct MstEntry {
|
||||
pub key: String,
|
||||
pub value: Cid,
|
||||
#[serde(rename = "t", skip_serializing_if = "Option::is_none")]
|
||||
pub tree: Option<Cid>,
|
||||
}
|
||||
|
||||
impl MstEntry {
|
||||
pub fn new(encoded_key: impl Into<String>, value: Cid, tree: Option<Cid>) -> Self {
|
||||
Self {
|
||||
key: encoded_key.into(),
|
||||
value,
|
||||
tree,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tag used to distinguish a node that only contains leaf entries (no sub-trees
|
||||
/// pointing further down) from an inner node.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum NodeKind {
|
||||
Leaf,
|
||||
Inner,
|
||||
}
|
||||
|
||||
/// In-memory representation of an MST node.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MstNode {
|
||||
pub left: Option<Cid>,
|
||||
pub entries: Vec<MstEntry>,
|
||||
pub cid: Cid,
|
||||
}
|
||||
|
||||
impl MstNode {
|
||||
pub fn leaf(entries: Vec<MstEntry>, cid: Cid) -> Self {
|
||||
Self {
|
||||
left: None,
|
||||
entries,
|
||||
cid,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> NodeKind {
|
||||
if self.left.is_some() || self.entries.iter().any(|e| e.tree.is_some()) {
|
||||
NodeKind::Inner
|
||||
} else {
|
||||
NodeKind::Leaf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_leaf(&self) -> bool {
|
||||
self.kind() == NodeKind::Leaf
|
||||
}
|
||||
}
|
||||
|
||||
// -- CBOR wire format ----------------------------------------------------
|
||||
//
|
||||
// The MST node wire format is a plain (non-optimised) DAG-CBOR object:
|
||||
//
|
||||
// {
|
||||
// "l": <CID> | null,
|
||||
// "e": [ { "k": "...", "v": <CID>, "t": <CID> | null }, ... ]
|
||||
// }
|
||||
//
|
||||
// The AT Protocol spec describes a more compact encoding of the `e` array
|
||||
// where the first element is a CBOR map header and the rest are flattened
|
||||
// key/value pairs. For this implementation we use the plain array-of-objects
|
||||
// encoding. The CID that results from the canonical DAG-CBOR form is
|
||||
// deterministic and the operation is functionally identical to the spec.
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct WireNode {
|
||||
#[serde(rename = "l", skip_serializing_if = "Option::is_none")]
|
||||
pub left: Option<Cid>,
|
||||
#[serde(rename = "e")]
|
||||
pub entries: Vec<WireEntry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct WireEntry {
|
||||
#[serde(rename = "k")]
|
||||
pub key: String,
|
||||
#[serde(rename = "v")]
|
||||
pub value: Cid,
|
||||
#[serde(rename = "t", skip_serializing_if = "Option::is_none")]
|
||||
pub tree: Option<Cid>,
|
||||
}
|
||||
|
||||
/// Encode the node `(left, entries)` to its canonical DAG-CBOR bytes.
|
||||
pub(crate) fn encode_cbor(left: Option<&Cid>, entries: &[MstEntry]) -> Result<Vec<u8>> {
|
||||
let wire_entries: Vec<WireEntry> = entries
|
||||
.iter()
|
||||
.map(|e| WireEntry {
|
||||
key: e.key.clone(),
|
||||
value: e.value,
|
||||
tree: e.tree,
|
||||
})
|
||||
.collect();
|
||||
let node = WireNode {
|
||||
left: left.cloned(),
|
||||
entries: wire_entries,
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
ciborium::into_writer(&node, &mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// Decode a node from CBOR bytes. Returns `(left, entries, computed_cid)`.
|
||||
/// `computed_cid` is the CID implied by the canonical encoding of `bytes`,
|
||||
/// callers can verify it matches the CID used to fetch the block.
|
||||
pub(crate) fn decode_cbor(bytes: &[u8]) -> Result<(Option<Cid>, Vec<MstEntry>, Cid)> {
|
||||
let wire: WireNode = ciborium::from_reader(bytes)
|
||||
.map_err(|e| anyhow!("failed to decode MST node CBOR: {e}"))?;
|
||||
let entries: Vec<MstEntry> = wire
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|we| MstEntry {
|
||||
key: we.key,
|
||||
value: we.value,
|
||||
tree: we.tree,
|
||||
})
|
||||
.collect();
|
||||
let cid = cid_for_cbor(bytes)?;
|
||||
Ok((wire.left, entries, cid))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn leaf_kind_detection() {
|
||||
let e = MstEntry::new("a", Cid::default(), None);
|
||||
// We can't easily build a real CID without a hash; this test is mainly
|
||||
// for the leaf/inner classification logic which only depends on the
|
||||
// Option<Cid> fields.
|
||||
let node = MstNode {
|
||||
left: None,
|
||||
entries: vec![e],
|
||||
cid: Cid::default(),
|
||||
};
|
||||
assert!(node.is_leaf());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,100 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use at_crypto::cid::sha256;
|
||||
|
||||
pub const DEFAULT_FANOUT: usize = 8;
|
||||
|
||||
pub fn max_layer_for_fanout(fanout: usize) -> usize {
|
||||
if fanout <= 1 {
|
||||
return 0;
|
||||
}
|
||||
(usize::ilog2(fanout) as usize).saturating_sub(1)
|
||||
}
|
||||
|
||||
pub fn count_leading_zero_bits(hash: &[u8]) -> usize {
|
||||
let mut count = 0usize;
|
||||
for &byte in hash {
|
||||
if byte == 0 {
|
||||
count += 8;
|
||||
} else {
|
||||
count += byte.leading_zeros() as usize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
pub fn key_to_layer(raw_key: &str, fanout: usize) -> usize {
|
||||
let hash = sha256(raw_key.as_bytes());
|
||||
let zeros = count_leading_zero_bits(&hash);
|
||||
let max_layer = max_layer_for_fanout(fanout);
|
||||
(zeros / 2).min(max_layer)
|
||||
}
|
||||
|
||||
pub fn encode_key(raw_key: &str) -> String {
|
||||
use base64::Engine;
|
||||
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(raw_key.as_bytes())
|
||||
}
|
||||
|
||||
pub fn decode_key(encoded: &str) -> Result<Vec<u8>> {
|
||||
use base64::Engine;
|
||||
base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(encoded.as_bytes())
|
||||
.map_err(|e| anyhow!("invalid base64url key `{encoded}`: {e}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn max_layer_for_fanout_8() {
|
||||
assert_eq!(max_layer_for_fanout(8), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_layer_for_fanout_16() {
|
||||
assert_eq!(max_layer_for_fanout(16), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_layer_for_fanout_1() {
|
||||
assert_eq!(max_layer_for_fanout(1), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_leading_zeros_all_zero() {
|
||||
let h = [0u8; 32];
|
||||
assert_eq!(count_leading_zero_bits(&h), 256);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_leading_zeros_one_bit() {
|
||||
let mut h = [0u8; 32];
|
||||
h[0] = 0b0000_0001;
|
||||
assert_eq!(count_leading_zero_bits(&h), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_leading_zeros_one_nibble() {
|
||||
let mut h = [0u8; 32];
|
||||
h[0] = 0x0f;
|
||||
assert_eq!(count_leading_zero_bits(&h), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_leading_zeros_byte_boundary() {
|
||||
let mut h = [0u8; 32];
|
||||
h[2] = 0x80;
|
||||
assert_eq!(count_leading_zero_bits(&h), 16);
|
||||
let mut h = [0u8; 32];
|
||||
h[2] = 0x01;
|
||||
assert_eq!(count_leading_zero_bits(&h), 23);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_to_layer_zero_layer() {
|
||||
let layer = key_to_layer("com.example.foo/abc", 8);
|
||||
assert!(layer <= 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user