clm_rust_core/layer2/sync/
codec.rs1use crate::layer0::vocabulary::{HASH_PREFIX_BLAKE3};
2use crate::layer2::sync::types::{
3 FramedRecordPage, MCardRecord, RecordFrame, SyncPageCursor, TransitionPayload,
4};
5use std::collections::{BTreeMap, BTreeSet};
6
7pub use crate::layer0::runtime_contract::CODEC_B64_CHUNK_BYTES as B64_CHUNK_BYTES;
8pub const MAX_SYNC_PAGE_RECORDS: usize = 256;
9pub const MAX_SYNC_PAGE_BYTES: usize = 16 * 1024 * 1024;
10
11const B64_CHARS: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
12
13pub fn bytes_to_base64(bytes: &[u8]) -> String {
14 if bytes.is_empty() {
15 return String::new();
16 }
17 let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
18 for chunk in bytes.chunks(B64_CHUNK_BYTES) {
19 for g in chunk.chunks(3) {
20 let (a, b, c) = (g[0], *g.get(1).unwrap_or(&0), *g.get(2).unwrap_or(&0));
21 out.push(B64_CHARS[(a >> 2) as usize] as char);
22 out.push(B64_CHARS[(((a & 3) << 4) | (b >> 4)) as usize] as char);
23 out.push(if g.len() > 1 { B64_CHARS[(((b & 15) << 2) | (c >> 6)) as usize] as char } else { '=' });
24 out.push(if g.len() > 2 { B64_CHARS[(c & 63) as usize] as char } else { '=' });
25 }
26 }
27 out
28}
29
30fn b64_val(b: u8) -> Option<u8> {
31 match b {
32 b'A'..=b'Z' => Some(b - b'A'),
33 b'a'..=b'z' => Some(b - b'a' + 26),
34 b'0'..=b'9' => Some(b - b'0' + 52),
35 b'+' => Some(62),
36 b'/' => Some(63),
37 _ => None,
38 }
39}
40
41pub fn base64_to_bytes(b64: &str) -> Result<Vec<u8>, String> {
42 if b64.is_empty() {
43 return Ok(Vec::new());
44 }
45 if !b64.len().is_multiple_of(4) {
46 return Err("Invalid base64 string".to_string());
47 }
48 let mut out = Vec::with_capacity((b64.len() / 4) * 3);
49 for group in b64.as_bytes().chunks(4) {
50 let e1 = b64_val(group[0]).ok_or_else(|| "Invalid base64 string".to_string())?;
51 let e2 = b64_val(group[1]).ok_or_else(|| "Invalid base64 string".to_string())?;
52 out.push((e1 << 2) | (e2 >> 4));
53 if group[2] != b'=' {
54 let e3 = b64_val(group[2]).ok_or_else(|| "Invalid base64 string".to_string())?;
55 out.push(((e2 & 15) << 4) | (e3 >> 2));
56 if group[3] != b'=' {
57 let e4 = b64_val(group[3]).ok_or_else(|| "Invalid base64 string".to_string())?;
58 out.push(((e3 & 3) << 6) | e4);
59 }
60 } else if group[3] != b'=' {
61 return Err("Invalid base64 string".to_string());
62 }
63 }
64 Ok(out)
65}
66
67pub fn derive_transfer_id(qualified_ref: &str) -> String {
68 format!("rw:{}", qualified_ref.strip_prefix("rw:").unwrap_or(qualified_ref))
69}
70
71pub fn derive_delivery_id(qualified_ref: &str, target_node_id: &str) -> String {
72 format!(
73 "rw:deliver:{target_node_id}:{}",
74 qualified_ref.strip_prefix("rw:").unwrap_or(qualified_ref)
75 )
76}
77
78pub fn frame_record(record: &MCardRecord) -> RecordFrame {
79 RecordFrame {
80 hash: record.hash.clone(),
81 content_base64: bytes_to_base64(&record.content),
82 g_time: record.g_time.clone(),
83 }
84}
85
86pub fn unframe_record(
87 frame: &RecordFrame,
88 required_algorithm: &str,
89) -> Result<(MCardRecord, String), (String, String)> {
90 if frame.hash.is_empty() || frame.g_time.is_empty() {
91 return Err(("malformed".to_string(), "Missing hash or gTime".to_string()));
92 }
93 let content = base64_to_bytes(&frame.content_base64).map_err(|e| ("malformed".to_string(), e))?;
94 let parts: Vec<&str> = frame.g_time.split('|').collect();
95 if parts.len() == 1 && frame.g_time.starts_with("202") && frame.g_time.contains('T') {
96 return Err(("algorithm-downgrade".to_string(), format!("Expected {required_algorithm}")));
97 }
98 if parts.len() < 3 {
99 return Err(("malformed".to_string(), "Invalid g_time format".to_string()));
100 }
101 let algo = crate::layer0::gtime::canonical_algorithm_name(parts[0].split(':').next().unwrap_or(""));
102 if algo != crate::layer0::vocabulary::HASH_ALGORITHM_BLAKE3 && algo != crate::layer0::vocabulary::HASH_ALGORITHM_SHA256 {
103 return Err(("malformed".to_string(), "Unsupported algorithm".to_string()));
104 }
105 if algo != crate::layer0::gtime::canonical_algorithm_name(required_algorithm) {
106 return Err(("algorithm-downgrade".to_string(), format!("Expected {required_algorithm}")));
107 }
108 let record = MCardRecord {
109 hash: frame.hash.clone(),
110 content,
111 g_time: frame.g_time.clone(),
112 };
113 if !matches!(crate::layer0::mcard_record::verify_record(&record),
114 crate::layer0::mcard_record::RecordVerification::DeclaredOk { .. }) {
115 return Err(("declared-mismatch".to_string(), "Record hash mismatch".to_string()));
116 }
117 let digest = frame.hash.split(':').next_back().unwrap_or("").to_lowercase();
118 Ok((
119 record,
120 format!("{algo}:{digest}"),
121 ))
122}
123
124pub use crate::layer0::transition::{is_qualified_hash, validate_transition_payload};
125
126pub fn group_transitions_by_handle(
127 transitions: &[TransitionPayload],
128) -> BTreeMap<String, Vec<TransitionPayload>> {
129 let mut grouped: BTreeMap<String, Vec<TransitionPayload>> = BTreeMap::new();
130 let norm = |h: &str| h.strip_prefix(crate::layer0::vocabulary::HANDLE_MCARD).unwrap_or(h).trim_start_matches('/').to_string();
131 for t in transitions {
132 grouped.entry(norm(&t.handle)).or_default().push(t.clone());
133 if t.kind == "RELOCATE" || t.kind == "FORK" {
134 if let Some(to_h) = &t.to_handle {
135 if to_h != &t.handle {
136 grouped.entry(norm(to_h)).or_default().push(t.clone());
137 }
138 }
139 }
140 }
141 grouped
142}
143
144pub fn frame_record_page(
145 records: &[MCardRecord],
146 known_hashes: &BTreeSet<String>,
147 cursor: Option<&SyncPageCursor>,
148 max_records: usize,
149 max_frame_bytes: usize,
150) -> FramedRecordPage {
151 let eligible: Vec<&MCardRecord> = records
152 .iter()
153 .filter(|r| {
154 let bare = r.hash.strip_prefix(crate::layer0::vocabulary::HASH_PREFIX_BLAKE3).unwrap_or(&r.hash).to_lowercase();
155 !known_hashes.contains(&r.hash) && !known_hashes.contains(&bare) && !known_hashes.contains(&format!("{HASH_PREFIX_BLAKE3}{bare}"))
156 })
157 .collect();
158 let mut idx = cursor.map(|c| c.offset).unwrap_or(0);
159 let (mut out, mut total_bytes) = (Vec::new(), 64usize);
160 while idx < eligible.len() && out.len() < max_records {
161 let frame = frame_record(eligible[idx]);
162 let fb = frame.hash.len() + frame.content_base64.len() + frame.g_time.len() + 48;
163 if !out.is_empty() && total_bytes + fb > max_frame_bytes {
164 break;
165 }
166 out.push(frame);
167 total_bytes += fb;
168 idx += 1;
169 }
170 let has_more = idx < eligible.len();
171 let next_cursor = has_more.then(|| SyncPageCursor {
172 offset: idx,
173 last_hash: out.last().map(|f| f.hash.clone()),
174 });
175 FramedRecordPage { records: out, next_cursor, has_more, total_bytes }
176}