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,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