libnoa 0.4.0

AI-native distributed version control
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use async_trait::async_trait;
use serde::Deserialize;

use reqwest::{Client, StatusCode};

use crate::{
    error::{NoaError, Result},
    object::{sha256_hex, BlobId, ObjectStore, TreeEntries, TreeId},
};

const API_PATH: &str = "/api/v0";

fn base32_encode_lower_no_pad(data: &[u8]) -> String {
    const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz234567";
    let mut result = String::with_capacity((data.len() * 8).div_ceil(5));
    let mut buffer: u64 = 0;
    let mut bits_left: u32 = 0;

    for &byte in data {
        buffer = (buffer << 8) | byte as u64;
        bits_left += 8;
        while bits_left >= 5 {
            bits_left -= 5;
            let index = ((buffer >> bits_left) & 0x1f) as usize;
            result.push(ALPHABET[index] as char);
        }
    }

    if bits_left > 0 {
        let index = ((buffer << (5 - bits_left)) & 0x1f) as usize;
        result.push(ALPHABET[index] as char);
    }

    result
}

pub fn sha256_hex_to_cid(hex: &str) -> Result<String> {
    let hash = hex::decode(hex).map_err(|e| NoaError::InvalidCid {
        cid: format!("invalid SHA-256 hex '{hex}': {e}"),
    })?;
    if hash.len() != 32 {
        return Err(NoaError::InvalidCid {
            cid: format!("SHA-256 hash must be 32 bytes, got {}", hash.len()),
        }
        .into());
    }

    let mut cid_bytes = Vec::with_capacity(36);
    cid_bytes.push(0x01);
    cid_bytes.push(0x55);
    cid_bytes.push(0x12);
    cid_bytes.push(0x20);
    cid_bytes.extend_from_slice(&hash);

    Ok(format!("b{}", base32_encode_lower_no_pad(&cid_bytes)))
}

#[derive(Debug, Deserialize)]
struct BlockPutResponse {
    key: String,
}

pub struct IpfsObjectStore {
    client: Client,
    api_url: String,
    auth_token: Option<String>,
}

impl Clone for IpfsObjectStore {
    fn clone(&self) -> Self {
        IpfsObjectStore {
            client: self.client.clone(),
            api_url: self.api_url.clone(),
            auth_token: self.auth_token.clone(),
        }
    }
}

impl IpfsObjectStore {
    #[must_use]
    pub fn new(endpoint: &str, auth_token: Option<String>) -> Self {
        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(120))
            .build()
            .unwrap_or_else(|_| Client::new());

        IpfsObjectStore {
            client,
            api_url: format!("{}{}", endpoint.trim_end_matches('/'), API_PATH),
            auth_token,
        }
    }

    fn request(&self, path: &str) -> reqwest::RequestBuilder {
        let url = format!("{}{}", self.api_url, path);
        let mut req = self.client.post(&url);
        if let Some(ref token) = self.auth_token {
            req = req.header("Authorization", format!("Bearer {token}"));
        }
        req
    }

    async fn block_put(&self, data: Vec<u8>) -> Result<String> {
        let part = reqwest::multipart::Part::bytes(data)
            .file_name("block")
            .mime_str("application/octet-stream")
            .map_err(|e| NoaError::IpfsError {
                message: format!("multipart build error: {e}"),
            })?;

        let form = reqwest::multipart::Form::new().part("file", part);

        let resp = self
            .request("/block/put?cid-codec=raw&hash=sha2-256&mhtype=sha2-256")
            .multipart(form)
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(NoaError::IpfsError {
                message: format!("block/put returned {status}: {body}"),
            }
            .into());
        }

        let parsed: BlockPutResponse = resp.json().await.map_err(|e| NoaError::IpfsError {
            message: format!("failed to parse block/put response: {e}"),
        })?;

        Ok(parsed.key)
    }

    async fn block_get(&self, cid: &str) -> Result<Vec<u8>> {
        let resp = self
            .request(&format!("/block/get?arg={cid}"))
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        match resp.status() {
            StatusCode::OK => {
                let bytes = resp.bytes().await.map_err(|e| NoaError::IpfsError {
                    message: format!("failed to read block/get body: {e}"),
                })?;
                Ok(bytes.to_vec())
            }
            s if s.is_client_error() || s == StatusCode::INTERNAL_SERVER_ERROR => {
                let body = resp.text().await.unwrap_or_default();
                if body.contains("not found") {
                    Err(NoaError::ObjectNotFound {
                        id: cid.to_string(),
                    }
                    .into())
                } else {
                    Err(NoaError::IpfsError {
                        message: format!("block/get failed ({s}): {body}"),
                    }
                    .into())
                }
            }
            s => Err(NoaError::IpfsError {
                message: format!("block/get unexpected status {s}"),
            }
            .into()),
        }
    }

    async fn block_stat(&self, cid: &str) -> Result<bool> {
        let resp = self
            .request(&format!("/block/stat?arg={cid}"))
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        match resp.status() {
            StatusCode::OK => Ok(true),
            StatusCode::NOT_FOUND => Ok(false),
            StatusCode::INTERNAL_SERVER_ERROR => {
                let body = resp.text().await.unwrap_or_default();
                if body.contains("not found") {
                    Ok(false)
                } else {
                    Err(NoaError::IpfsError {
                        message: format!("block/stat failed: {body}"),
                    }
                    .into())
                }
            }
            s => Err(NoaError::IpfsError {
                message: format!("block/stat unexpected status {s}"),
            }
            .into()),
        }
    }

    pub async fn pin_add(&self, cid: &str) -> Result<()> {
        let resp = self
            .request(&format!("/pin/add?arg={cid}"))
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(NoaError::IpfsError {
                message: format!("pin/add failed ({status}): {body}"),
            }
            .into());
        }
        Ok(())
    }

    pub async fn pin_rm(&self, cid: &str) -> Result<()> {
        let resp = self
            .request(&format!("/pin/rm?arg={cid}"))
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(NoaError::IpfsError {
                message: format!("pin/rm failed ({status}): {body}"),
            }
            .into());
        }
        Ok(())
    }

    pub async fn version(&self) -> Result<String> {
        #[derive(Deserialize)]
        struct VersionResponse {
            #[serde(rename = "Version")]
            version: String,
        }

        let resp =
            self.request("/version")
                .send()
                .await
                .map_err(|_| NoaError::IpfsDaemonUnreachable {
                    endpoint: self.api_url.clone(),
                })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(NoaError::IpfsError {
                message: format!("version failed ({status}): {body}"),
            }
            .into());
        }

        let parsed: VersionResponse = resp.json().await.map_err(|e| NoaError::IpfsError {
            message: format!("failed to parse version response: {e}"),
        })?;

        Ok(parsed.version)
    }

    pub async fn repo_stat(&self) -> Result<RepoStat> {
        let resp = self
            .request("/repo/stat?size-only=true")
            .send()
            .await
            .map_err(|_| NoaError::IpfsDaemonUnreachable {
                endpoint: self.api_url.clone(),
            })?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().await.unwrap_or_default();
            return Err(NoaError::IpfsError {
                message: format!("repo/stat failed ({status}): {body}"),
            }
            .into());
        }

        let stat: RepoStat = resp.json().await.map_err(|e| NoaError::IpfsError {
            message: format!("failed to parse repo/stat response: {e}"),
        })?;
        Ok(stat)
    }

    pub async fn gateway_url(&self, cid: &str, gateway: &str) -> String {
        format!("{}/ipfs/{}", gateway.trim_end_matches('/'), cid)
    }

    pub async fn block_get_raw(&self, cid: &str) -> Result<Vec<u8>> {
        self.block_get(cid).await
    }
}

#[derive(Debug, Deserialize)]
pub struct RepoStat {
    #[serde(rename = "RepoSize")]
    pub repo_size: u64,
    #[serde(rename = "StorageMax")]
    pub storage_max: u64,
    #[serde(rename = "NumObjects")]
    pub num_objects: u64,
}

#[async_trait]
impl ObjectStore for IpfsObjectStore {
    async fn put_blob(&self, content: &[u8]) -> Result<BlobId> {
        let id = BlobId(sha256_hex(content));
        let cid = sha256_hex_to_cid(&id.0)?;
        let expected_cid = cid.clone();

        let returned_cid = self.block_put(content.to_vec()).await?;

        if returned_cid != expected_cid {
            tracing::warn!(
                returned = %returned_cid,
                expected = %expected_cid,
                "IPFS returned unexpected CID — hash mismatch or daemon uses different codec"
            );
        }

        Ok(id)
    }

    async fn get_blob(&self, id: &BlobId) -> Result<Vec<u8>> {
        let cid = sha256_hex_to_cid(&id.0)?;
        self.block_get(&cid).await
    }

    async fn has_blob(&self, id: &BlobId) -> Result<bool> {
        let cid = sha256_hex_to_cid(&id.0)?;
        self.block_stat(&cid).await
    }

    async fn put_tree(&self, entries: &TreeEntries) -> Result<TreeId> {
        let data = rmp_serde::to_vec(entries)?;
        let id = TreeId(sha256_hex(&data));
        let cid = sha256_hex_to_cid(&id.0)?;

        let returned_cid = self.block_put(data).await?;

        if returned_cid != cid {
            tracing::warn!(
                returned = %returned_cid,
                expected = %cid,
                "IPFS tree CID mismatch"
            );
        }

        Ok(id)
    }

    async fn get_tree(&self, id: &TreeId) -> Result<TreeEntries> {
        let cid = sha256_hex_to_cid(&id.0)?;
        let bytes = self.block_get(&cid).await?;
        Ok(rmp_serde::from_slice::<TreeEntries>(&bytes)?)
    }

    async fn has_tree(&self, id: &TreeId) -> Result<bool> {
        let cid = sha256_hex_to_cid(&id.0)?;
        self.block_stat(&cid).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_base32_empty() {
        assert_eq!(base32_encode_lower_no_pad(&[]), "");
    }

    #[test]
    fn test_base32_single_byte() {
        // 0x66 ('f') -> base32: "my"
        // 0x66 = 01100110
        // First 5 bits: 01100 = 12 -> 'm'
        // Remaining 3 bits: 110 -> padded to 11000 = 24 -> 'y'
        assert_eq!(base32_encode_lower_no_pad(&[0x66]), "my");
    }

    #[test]
    fn test_base32_known_values() {
        // RFC 4648 test vectors (lowercase)
        assert_eq!(base32_encode_lower_no_pad(b"f"), "my");
        assert_eq!(base32_encode_lower_no_pad(b"fo"), "mzxq");
        assert_eq!(base32_encode_lower_no_pad(b"foo"), "mzxw6");
        assert_eq!(base32_encode_lower_no_pad(b"foob"), "mzxw6yq");
        assert_eq!(base32_encode_lower_no_pad(b"fooba"), "mzxw6ytb");
        assert_eq!(base32_encode_lower_no_pad(b"foobar"), "mzxw6ytboi");
    }

    #[test]
    fn test_cid_conversion_empty_content() {
        let hex = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
        let cid = sha256_hex_to_cid(hex).unwrap();
        assert!(cid.starts_with('b'));
        assert_eq!(cid.len(), 59); // 'b' prefix + 58 base32 chars for 36 bytes
    }

    #[test]
    fn test_cid_conversion_hello() {
        let hex = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
        let cid = sha256_hex_to_cid(hex).unwrap();
        assert!(cid.starts_with("bafk"));
        assert_eq!(cid.len(), 59);
    }

    #[test]
    fn test_cid_deterministic() {
        let hex = "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e";
        let cid1 = sha256_hex_to_cid(hex).unwrap();
        let cid2 = sha256_hex_to_cid(hex).unwrap();
        assert_eq!(cid1, cid2);
    }

    #[test]
    fn test_cid_invalid_hex() {
        assert!(sha256_hex_to_cid("xyz").is_err());
    }

    #[test]
    fn test_cid_wrong_length() {
        // 16 bytes instead of 32
        assert!(sha256_hex_to_cid("0123456789abcdef0123456789abcdef").is_err());
    }

    #[test]
    fn test_ipfs_config_default() {
        let config = crate::config::StorageConfig::ipfs("test", Some("http://127.0.0.1:5001"));
        assert_eq!(config.backend_type, crate::config::StorageProtocol::Ipfs);
        assert_eq!(config.effective_endpoint(), "http://127.0.0.1:5001");
        assert!(!config.auto_pin);
    }
}