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