netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Bitswap 1.2.0 (and 1.1.0, 1.0.0): the message, and the two decisions Rust makes about it
//! before or after the model — which wanted blocks the model's content actually is
//! ([`answer_wants`]), and which received blocks are the ones that were asked for
//! ([`accept_blocks`]). In both, a block's identity is its hash, computed here.
//!
//! A Bitswap message is one uvarint-length-prefixed protobuf on a stream the sender opened;
//! answers travel on a stream the answerer opens. Every count and size a peer announces is
//! bounded before anything is collected for it.
use super::ipfs::{self, Cid};
use super::wire::{pb_bytes, pb_fields_bounded, pb_varint, PbValue};
use anyhow::{bail, ensure, Context, Result};
use std::collections::HashMap;

pub const BITSWAP_120: &str = "/ipfs/bitswap/1.2.0";
pub const BITSWAP_110: &str = "/ipfs/bitswap/1.1.0";
pub const BITSWAP_100: &str = "/ipfs/bitswap/1.0.0";
pub const BITSWAP_LEGACY: &str = "/ipfs/bitswap";
/// Every Bitswap protocol id NetGet accepts, newest first (the order it proposes them).
pub const PROTOCOLS: &[&str] = &[BITSWAP_120, BITSWAP_110, BITSWAP_100, BITSWAP_LEGACY];
/// A Bitswap message is at most this long (boxo's own `MessageSizeMax`).
pub const MAX_MESSAGE: usize = 4 * 1024 * 1024;
/// Wantlist entries one message may carry.
pub const MAX_WANTLIST_ENTRIES: usize = 1024;
/// Top-level fields (blocks, presences) one message may carry.
pub const MAX_MESSAGE_FIELDS: usize = 4096;
/// What NetGet packs into one outgoing message, leaving room for framing.
pub const MAX_OUTGOING: usize = MAX_MESSAGE - 64 * 1024;
/// Blocks the model may supply in one answer.
pub const MAX_SUPPLIED_BLOCKS: usize = 64;

pub fn is_bitswap(protocol: &str) -> bool {
    PROTOCOLS.contains(&protocol)
}

/// Which message format a protocol id speaks.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Version {
    V100,
    V110,
    V120,
}

impl Version {
    pub fn of(protocol: &str) -> Version {
        match protocol {
            BITSWAP_120 => Version::V120,
            BITSWAP_110 => Version::V110,
            _ => Version::V100,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WantType {
    Block,
    Have,
}

impl WantType {
    pub fn name(self) -> &'static str {
        match self {
            WantType::Block => "block",
            WantType::Have => "have",
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Entry {
    pub cid: Cid,
    pub priority: i32,
    pub cancel: bool,
    pub want_type: WantType,
    pub send_dont_have: bool,
}

/// One received block: the CID its prefix and bytes name (the bytes hashed here), and the
/// bytes. A 1.0.0 block carries no prefix and is read as CIDv0 dag-pb.
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
    pub cid: Cid,
    pub data: Vec<u8>,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Message {
    pub entries: Vec<Entry>,
    pub full: bool,
    pub blocks: Vec<Block>,
    /// (cid, have): HAVE when true, DONT_HAVE when false.
    pub presences: Vec<(Cid, bool)>,
    /// Entries or presences naming a CID NetGet cannot represent (another hash function, a
    /// CID version it does not know), each with why. A malformed message is refused whole;
    /// these are refused one by one.
    pub unreadable: Vec<String>,
}

/// Decode one Bitswap message. Malformed protobuf, too many entries or fields, and a block
/// over [`ipfs::MAX_BLOCK`] refuse the whole message.
pub fn decode(b: &[u8]) -> Result<Message> {
    ensure!(
        b.len() <= MAX_MESSAGE,
        "Bitswap message of {} bytes is over {MAX_MESSAGE}",
        b.len()
    );
    let mut m = Message::default();
    for (f, v) in pb_fields_bounded(b, MAX_MESSAGE_FIELDS)? {
        match (f, v) {
            (1, PbValue::Bytes(w)) => decode_wantlist(w, &mut m)?,
            (2, PbValue::Bytes(data)) => {
                ensure!(
                    data.len() <= ipfs::MAX_BLOCK,
                    "block over {} bytes",
                    ipfs::MAX_BLOCK
                );
                m.blocks.push(Block {
                    cid: Cid {
                        version: 0,
                        codec: ipfs::DAG_PB,
                        digest: ipfs::sha256(data),
                    },
                    data: data.to_vec(),
                });
            }
            (3, PbValue::Bytes(p)) => {
                let (mut prefix, mut data) = (None, None);
                for (bf, bv) in pb_fields_bounded(p, 8)? {
                    match (bf, bv) {
                        (1, PbValue::Bytes(x)) => prefix = Some(x),
                        (2, PbValue::Bytes(x)) => data = Some(x),
                        _ => bail!("malformed Bitswap block"),
                    }
                }
                let data = data.unwrap_or_default();
                ensure!(
                    data.len() <= ipfs::MAX_BLOCK,
                    "block over {} bytes",
                    ipfs::MAX_BLOCK
                );
                match Cid::from_prefix(prefix.context("a Bitswap block has no prefix")?, data) {
                    Ok(cid) => m.blocks.push(Block {
                        cid,
                        data: data.to_vec(),
                    }),
                    Err(e) => m.unreadable.push(format!("block: {e:#}")),
                }
            }
            (4, PbValue::Bytes(p)) => {
                let (mut cid, mut kind) = (None, 0);
                for (pf, pv) in pb_fields_bounded(p, 8)? {
                    match (pf, pv) {
                        (1, PbValue::Bytes(x)) => cid = Some(x),
                        (2, PbValue::Varint(k)) => kind = k,
                        _ => bail!("malformed Bitswap block presence"),
                    }
                }
                match Cid::from_bytes(cid.context("a block presence has no CID")?) {
                    Ok(c) => m.presences.push((c, kind == 0)),
                    Err(e) => m.unreadable.push(format!("presence: {e:#}")),
                }
            }
            // pendingBytes
            (5, PbValue::Varint(_)) => {}
            _ => bail!("malformed Bitswap message (field {f})"),
        }
    }
    Ok(m)
}

fn decode_wantlist(w: &[u8], m: &mut Message) -> Result<()> {
    for (f, v) in pb_fields_bounded(w, MAX_WANTLIST_ENTRIES + 1)? {
        match (f, v) {
            (1, PbValue::Bytes(e)) => {
                ensure!(
                    m.entries.len() < MAX_WANTLIST_ENTRIES,
                    "wantlist has more than {MAX_WANTLIST_ENTRIES} entries"
                );
                let mut cid = None;
                let mut entry = Entry {
                    cid: Cid {
                        version: 0,
                        codec: 0,
                        digest: [0; 32],
                    },
                    priority: 0,
                    cancel: false,
                    want_type: WantType::Block,
                    send_dont_have: false,
                };
                for (ef, ev) in pb_fields_bounded(e, 16)? {
                    match (ef, ev) {
                        (1, PbValue::Bytes(c)) => cid = Some(c),
                        (2, PbValue::Varint(p)) => entry.priority = p as i32,
                        (3, PbValue::Varint(c)) => entry.cancel = c != 0,
                        (4, PbValue::Varint(t)) => {
                            entry.want_type = if t == 1 {
                                WantType::Have
                            } else {
                                WantType::Block
                            }
                        }
                        (5, PbValue::Varint(s)) => entry.send_dont_have = s != 0,
                        _ => bail!("malformed wantlist entry"),
                    }
                }
                let cid = cid.context("a wantlist entry has no CID")?;
                match Cid::from_bytes(cid) {
                    Ok(c) => {
                        entry.cid = c;
                        m.entries.push(entry);
                    }
                    Err(e) => m.unreadable.push(format!("want: {e:#}")),
                }
            }
            (2, PbValue::Varint(full)) => m.full = full != 0,
            _ => bail!("malformed wantlist (field {f})"),
        }
    }
    Ok(())
}

/// Encode a message in the format `version` speaks: 1.0.0 carries raw blocks (CIDv0 only)
/// and no presences; 1.1.0 carries prefixed blocks and no presences; 1.2.0 carries both.
pub fn encode(m: &Message, version: Version) -> Vec<u8> {
    let mut out = Vec::new();
    if !m.entries.is_empty() || m.full {
        let mut w = Vec::new();
        for e in &m.entries {
            let mut entry = Vec::new();
            let cid = if version == Version::V100 {
                // 1.0.0 names blocks by their multihash, as CIDv0 did.
                e.cid.multihash()
            } else {
                e.cid.to_bytes()
            };
            pb_bytes(&mut entry, 1, &cid);
            pb_varint(&mut entry, 2, e.priority as u32 as u64);
            if e.cancel {
                pb_varint(&mut entry, 3, 1);
            }
            if version == Version::V120 {
                if e.want_type == WantType::Have {
                    pb_varint(&mut entry, 4, 1);
                }
                if e.send_dont_have {
                    pb_varint(&mut entry, 5, 1);
                }
            }
            pb_bytes(&mut w, 1, &entry);
        }
        if m.full {
            pb_varint(&mut w, 2, 1);
        }
        pb_bytes(&mut out, 1, &w);
    }
    for b in &m.blocks {
        if version == Version::V100 {
            pb_bytes(&mut out, 2, &b.data);
        } else {
            let mut block = Vec::new();
            pb_bytes(&mut block, 1, &b.cid.prefix());
            pb_bytes(&mut block, 2, &b.data);
            pb_bytes(&mut out, 3, &block);
        }
    }
    if version == Version::V120 {
        for (cid, have) in &m.presences {
            let mut p = Vec::new();
            pb_bytes(&mut p, 1, &cid.to_bytes());
            pb_varint(&mut p, 2, if *have { 0 } else { 1 });
            pb_bytes(&mut out, 4, &p);
        }
    }
    out
}

/// Split an answer into messages no longer than [`MAX_OUTGOING`] each.
pub fn split(m: Message) -> Vec<Message> {
    let mut out = Vec::new();
    let mut cur = Message {
        presences: m.presences,
        ..Default::default()
    };
    let mut size = cur.presences.len() * 48;
    for b in m.blocks {
        let len = b.data.len() + 64;
        if size + len > MAX_OUTGOING && (!cur.blocks.is_empty() || !cur.presences.is_empty()) {
            out.push(std::mem::take(&mut cur));
            size = 0;
        }
        size += len;
        cur.blocks.push(b);
    }
    if !cur.blocks.is_empty() || !cur.presences.is_empty() {
        out.push(cur);
    }
    out
}

// ---------------------------------------------------------------------------------------
// Serving: what the model supplied, checked against what was wanted.

/// The form a supplied block's content takes on the wire.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Format {
    /// codec raw: the block is the content.
    Raw,
    /// codec dag-pb: a single-block UnixFS file holding the content.
    UnixFsFile,
}

impl Format {
    pub fn parse(s: &str) -> Result<Format> {
        match s {
            "raw" => Ok(Format::Raw),
            "unixfs" | "unixfs-file" | "file" => Ok(Format::UnixFsFile),
            other => bail!("format must be raw or unixfs, not {other:?}"),
        }
    }
}

/// A block the model offered: its bytes as they will travel, and the CID it claimed, if any.
#[derive(Clone, Debug)]
pub struct Supplied {
    pub codec: u64,
    pub bytes: Vec<u8>,
    pub claimed: Option<Cid>,
}

impl Supplied {
    pub fn new(content: &[u8], format: Format, claimed: Option<Cid>) -> Result<Supplied> {
        let (codec, bytes) = match format {
            Format::Raw => (ipfs::RAW, content.to_vec()),
            Format::UnixFsFile => (ipfs::DAG_PB, ipfs::unixfs_file_block(content)),
        };
        ensure!(
            bytes.len() <= ipfs::MAX_BLOCK,
            "a block is at most {} bytes; this one would be {}",
            ipfs::MAX_BLOCK,
            bytes.len()
        );
        Ok(Supplied {
            codec,
            bytes,
            claimed,
        })
    }
    /// The CID these bytes actually have (version 1; a v0 want matches it too).
    pub fn cid(&self) -> Cid {
        Cid::v1(self.codec, &self.bytes)
    }
}

/// What to send back for one wantlist.
#[derive(Debug, Default)]
pub struct Answer {
    /// Blocks, each under the CID that was wanted (so the version the peer asked for).
    pub blocks: Vec<Block>,
    pub dont_have: Vec<Cid>,
    /// Supplied blocks that were not sent, and why.
    pub refused: Vec<String>,
}

/// Match the model's blocks to the wants. A want is satisfied only by a block whose codec
/// and sha2-256 digest are the wanted CID's — computed from the bytes, never taken from the
/// model. A supplied block that claims a CID its bytes do not hash to is refused. A want-have
/// is answered with the block itself (Bitswap allows it, and it spares the model a second
/// question whose answer has to hash the same). Wants left unsatisfied are answered
/// DONT_HAVE where the peer asked for that.
pub fn answer_wants(wants: &[Entry], supplied: &[Supplied]) -> Answer {
    let mut answer = Answer::default();
    let mut usable: Vec<(Cid, &Supplied)> = Vec::new();
    for s in supplied {
        let actual = s.cid();
        match s.claimed {
            Some(claimed) if !claimed.same_block(&actual) => answer.refused.push(format!(
                "a block claimed as {claimed} hashes to {actual}; not sent"
            )),
            _ => usable.push((actual, s)),
        }
    }
    let mut used = vec![false; usable.len()];
    for want in wants.iter().filter(|w| !w.cancel) {
        match usable.iter().position(|(c, _)| c.same_block(&want.cid)) {
            Some(i)
                if usable[i].1.bytes.len() <= ipfs::MAX_BLOCK
                    && want.cid.matches(&usable[i].1.bytes) =>
            {
                used[i] = true;
                answer.blocks.push(Block {
                    cid: want.cid,
                    data: usable[i].1.bytes.clone(),
                });
            }
            _ if want.send_dont_have => answer.dont_have.push(want.cid),
            _ => {}
        }
    }
    for (i, (cid, _)) in usable.iter().enumerate() {
        if !used[i] {
            answer
                .refused
                .push(format!("a block hashing to {cid} was not wanted; not sent"));
        }
    }
    answer
}

// ---------------------------------------------------------------------------------------
// Fetching: what arrived, checked against what was asked for.

/// Wants a dialler has outstanding, keyed by the block they name (codec and digest, so a v0
/// and a v1 want for one block are one want), with the text the model asked by.
#[derive(Default)]
pub struct WantSet {
    wants: HashMap<(u64, [u8; 32]), (Cid, WantType)>,
}

/// Outstanding wants one connection may hold.
pub const MAX_OUTSTANDING_WANTS: usize = 1024;

impl WantSet {
    pub fn add(&mut self, cid: Cid, want: WantType) -> Result<()> {
        cid.supported()?;
        let key = (cid.codec, cid.digest);
        if !self.wants.contains_key(&key) {
            ensure!(
                self.wants.len() < MAX_OUTSTANDING_WANTS,
                "{MAX_OUTSTANDING_WANTS} wants are already outstanding"
            );
        }
        self.wants.insert(key, (cid, want));
        Ok(())
    }
    pub fn remove(&mut self, cid: &Cid) -> Option<(Cid, WantType)> {
        self.wants.remove(&(cid.codec, cid.digest))
    }
    pub fn get(&self, cid: &Cid) -> Option<(Cid, WantType)> {
        self.wants.get(&(cid.codec, cid.digest)).copied()
    }
    pub fn len(&self) -> usize {
        self.wants.len()
    }
    pub fn is_empty(&self) -> bool {
        self.wants.is_empty()
    }
}

/// What a received message is worth reporting.
#[derive(Debug, Default)]
pub struct Received {
    /// Blocks whose bytes hash to an outstanding want, reported under the CID that was asked
    /// for. Each satisfies (and removes) its want.
    pub blocks: Vec<Block>,
    /// Presences for outstanding wants: (the CID asked for, have).
    pub presences: Vec<(Cid, bool)>,
    pub refused: Vec<String>,
}

/// Keep only the blocks that are what was asked for. A block's CID is computed from its own
/// bytes, so a block whose bytes were altered names a different CID, which nothing wanted;
/// it is refused, never reported.
pub fn accept_blocks(m: &Message, wants: &mut WantSet) -> Received {
    let mut r = Received::default();
    for b in &m.blocks {
        match wants.get(&b.cid) {
            Some((asked, _)) if b.cid.supported().is_ok() && asked.matches(&b.data) => {
                wants.remove(&asked);
                r.blocks.push(Block {
                    cid: asked,
                    data: b.data.clone(),
                });
            }
            _ => r.refused.push(format!(
                "a {}-byte block hashing to {} matches nothing that was wanted; refused",
                b.data.len(),
                b.cid
            )),
        }
    }
    for (cid, have) in &m.presences {
        if let Some((asked, _)) = wants.get(cid) {
            if !*have {
                wants.remove(&asked);
            }
            r.presences.push((asked, *have));
        }
    }
    r.refused.extend(m.unreadable.iter().cloned());
    r
}