hypercore-protocol 0.7.0

Replication protocol for Hypercore feeds
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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use compact_encoding::{
    CompactEncoding, EncodingError, map_decode, map_encode, sum_encoded_size, take_array,
    take_array_mut, write_array, write_slice,
};
use hypercore_schema::{
    DataBlock, DataHash, DataSeek, DataUpgrade, Proof, RequestBlock, RequestSeek, RequestUpgrade,
};
use tracing::instrument;

/// Open message
#[derive(Debug, Clone, PartialEq)]
pub struct Open {
    /// Channel id to open
    pub channel: u64,
    /// Protocol name
    pub protocol: String,
    /// Hypercore discovery key
    pub discovery_key: Vec<u8>,
    /// Capability hash
    pub capability: Option<Vec<u8>>,
}

impl CompactEncoding for Open {
    #[instrument(skip_all, ret, err)]
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        let out = sum_encoded_size!(self.channel, self.protocol, self.discovery_key);
        if self.capability.is_some() {
            return Ok(
                out
                    + 1  // flags for future use
                    + 32, // TODO capabalilities buff should always be 32 bytes, but it's a vec
            );
        }
        Ok(out)
    }

    #[instrument(skip_all)]
    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        let rest = map_encode!(buffer, self.channel, self.protocol, self.discovery_key);
        if let Some(cap) = &self.capability {
            let (_, rest) = take_array_mut::<1>(rest)?;
            return write_slice(cap, rest);
        }
        Ok(rest)
    }

    #[instrument(skip_all, err)]
    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ((channel, protocol, discovery_key), rest) =
            map_decode!(buffer, [u64, String, Vec<u8>]);
        // NB: Open/Close are only sent alone in their own Frame. So we're done when there is no
        // more data
        let (capability, rest) = if !rest.is_empty() {
            let (_, rest) = take_array::<1>(rest)?;
            let (capability, rest) = take_array::<32>(rest)?;
            (Some(capability.to_vec()), rest)
        } else {
            (None, rest)
        };
        Ok((
            Self {
                channel,
                protocol,
                discovery_key,
                capability,
            },
            rest,
        ))
    }
}

/// Close message
#[derive(Debug, Clone, PartialEq)]
pub struct Close {
    /// Channel id to close
    pub channel: u64,
}

impl CompactEncoding for Close {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        self.channel.encoded_size()
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        self.channel.encode(buffer)
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let (channel, rest) = u64::decode(buffer)?;
        Ok((Self { channel }, rest))
    }
}

/// Synchronize message. Type 0.
#[derive(Debug, Clone, PartialEq)]
pub struct Synchronize {
    /// Fork id, set to 0 for an un-forked hypercore.
    pub fork: u64,
    /// Length of hypercore
    pub length: u64,
    /// Known length of the remote party, 0 for unknown.
    pub remote_length: u64,
    /// Downloading allowed
    pub downloading: bool,
    /// Uploading allowed
    pub uploading: bool,
    /// Upgrade possible
    pub can_upgrade: bool,
}

impl CompactEncoding for Synchronize {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(1 + sum_encoded_size!(self.fork, self.length, self.remote_length))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        let mut flags: u8 = if self.can_upgrade { 1 } else { 0 };
        flags |= if self.uploading { 2 } else { 0 };
        flags |= if self.downloading { 4 } else { 0 };
        let rest = write_array(&[flags], buffer)?;
        Ok(map_encode!(
            rest,
            self.fork,
            self.length,
            self.remote_length
        ))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ([flags], rest) = take_array::<1>(buffer)?;
        let ((fork, length, remote_length), rest) = map_decode!(rest, [u64, u64, u64]);
        let can_upgrade = flags & 1 != 0;
        let uploading = flags & 2 != 0;
        let downloading = flags & 4 != 0;
        Ok((
            Synchronize {
                fork,
                length,
                remote_length,
                can_upgrade,
                uploading,
                downloading,
            },
            rest,
        ))
    }
}

/// Request message. Type 1.
#[derive(Debug, Clone, PartialEq)]
pub struct Request {
    /// Request id, will be returned with corresponding [Data]
    pub id: u64,
    /// Current fork, set to 0 for un-forked hypercore
    pub fork: u64,
    /// Request for data
    pub block: Option<RequestBlock>,
    /// Request hash
    pub hash: Option<RequestBlock>,
    /// Request seek
    pub seek: Option<RequestSeek>,
    /// Request upgrade
    pub upgrade: Option<RequestUpgrade>,
    // TODO what is this
    /// Request manifest
    pub manifest: bool,
    // TODO what is this
    // this could prob be usize
    /// Request priority
    pub priority: u64,
}

macro_rules! maybe_decode {
    ($cond:expr, $type:ty, $buf:ident) => {
        if $cond {
            let (result, rest) = <$type>::decode($buf)?;
            (Some(result), rest)
        } else {
            (None, $buf)
        }
    };
}

impl CompactEncoding for Request {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        let mut out = 1; // flags
        out += sum_encoded_size!(self.id, self.fork);
        if let Some(block) = &self.block {
            out += block.encoded_size()?;
        }
        if let Some(hash) = &self.hash {
            out += hash.encoded_size()?;
        }
        if let Some(seek) = &self.seek {
            out += seek.encoded_size()?;
        }
        if let Some(upgrade) = &self.upgrade {
            out += upgrade.encoded_size()?;
        }
        if self.priority != 0 {
            out += self.priority.encoded_size()?;
        }
        Ok(out)
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        let mut flags: u8 = if self.block.is_some() { 1 } else { 0 };
        flags |= if self.hash.is_some() { 2 } else { 0 };
        flags |= if self.seek.is_some() { 4 } else { 0 };
        flags |= if self.upgrade.is_some() { 8 } else { 0 };
        flags |= if self.manifest { 16 } else { 0 };
        flags |= if self.priority != 0 { 32 } else { 0 };
        let mut rest = write_array(&[flags], buffer)?;
        rest = map_encode!(rest, self.id, self.fork);

        if let Some(block) = &self.block {
            rest = block.encode(rest)?;
        }
        if let Some(hash) = &self.hash {
            rest = hash.encode(rest)?;
        }
        if let Some(seek) = &self.seek {
            rest = seek.encode(rest)?;
        }
        if let Some(upgrade) = &self.upgrade {
            rest = upgrade.encode(rest)?;
        }

        if self.priority != 0 {
            rest = self.priority.encode(rest)?;
        }

        Ok(rest)
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ([flags], rest) = take_array::<1>(buffer)?;
        let ((id, fork), rest) = map_decode!(rest, [u64, u64]);

        let (block, rest) = maybe_decode!(flags & 1 != 0, RequestBlock, rest);
        let (hash, rest) = maybe_decode!(flags & 2 != 0, RequestBlock, rest);
        let (seek, rest) = maybe_decode!(flags & 4 != 0, RequestSeek, rest);
        let (upgrade, rest) = maybe_decode!(flags & 8 != 0, RequestUpgrade, rest);
        let manifest = flags & 16 != 0;
        let (priority, rest) = if flags & 32 != 0 {
            u64::decode(rest)?
        } else {
            (0, rest)
        };
        Ok((
            Request {
                id,
                fork,
                block,
                hash,
                seek,
                upgrade,
                manifest,
                priority,
            },
            rest,
        ))
    }
}

/// Cancel message for a [Request]. Type 2
#[derive(Debug, Clone, PartialEq)]
pub struct Cancel {
    /// Request to cancel, see field `id` in [Request]
    pub request: u64,
}

impl CompactEncoding for Cancel {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        self.request.encoded_size()
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        self.request.encode(buffer)
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let (request, rest) = u64::decode(buffer)?;
        Ok((Cancel { request }, rest))
    }
}

/// Data message responding to received [Request]. Type 3.
#[derive(Debug, Clone, PartialEq)]
pub struct Data {
    /// Request this data is for, see field `id` in [Request]
    pub request: u64,
    /// Fork id, set to 0 for un-forked hypercore
    pub fork: u64,
    /// Response for block request
    pub block: Option<DataBlock>,
    /// Response for hash request
    pub hash: Option<DataHash>,
    /// Response for seek request
    pub seek: Option<DataSeek>,
    /// Response for upgrade request
    pub upgrade: Option<DataUpgrade>,
}

macro_rules! opt_encoded_size {
    ($opt:expr, $sum:ident) => {
        if let Some(thing) = $opt {
            $sum += thing.encoded_size()?;
        }
    };
}

// TODO we could write a macro where it takes a $cond that returns an opt.
// if the option is Some(T) then do T::encode(buf)
// also if some add $flag.
// This would simplify some of these impls
macro_rules! opt_encoded_bytes {
    ($opt:expr, $buf:ident) => {
        if let Some(thing) = $opt {
            thing.encode($buf)?
        } else {
            $buf
        }
    };
}
impl CompactEncoding for Data {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        let mut out = 1; // flags
        out += sum_encoded_size!(self.request, self.fork);
        opt_encoded_size!(&self.block, out);
        opt_encoded_size!(&self.hash, out);
        opt_encoded_size!(&self.seek, out);
        opt_encoded_size!(&self.upgrade, out);
        Ok(out)
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        let mut flags: u8 = if self.block.is_some() { 1 } else { 0 };
        flags |= if self.hash.is_some() { 2 } else { 0 };
        flags |= if self.seek.is_some() { 4 } else { 0 };
        flags |= if self.upgrade.is_some() { 8 } else { 0 };
        let rest = write_array(&[flags], buffer)?;
        let rest = map_encode!(rest, self.request, self.fork);

        let rest = opt_encoded_bytes!(&self.block, rest);
        let rest = opt_encoded_bytes!(&self.hash, rest);
        let rest = opt_encoded_bytes!(&self.seek, rest);
        let rest = opt_encoded_bytes!(&self.upgrade, rest);
        Ok(rest)
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ([flags], rest) = take_array::<1>(buffer)?;
        let ((request, fork), rest) = map_decode!(rest, [u64, u64]);
        let (block, rest) = maybe_decode!(flags & 1 != 0, DataBlock, rest);
        let (hash, rest) = maybe_decode!(flags & 2 != 0, DataHash, rest);
        let (seek, rest) = maybe_decode!(flags & 4 != 0, DataSeek, rest);
        let (upgrade, rest) = maybe_decode!(flags & 8 != 0, DataUpgrade, rest);
        Ok((
            Data {
                request,
                fork,
                block,
                hash,
                seek,
                upgrade,
            },
            rest,
        ))
    }
}

impl Data {
    /// Transform Data message into a [`Proof`]
    pub fn into_proof(self) -> Proof {
        Proof {
            fork: self.fork,
            block: self.block,
            hash: self.hash,
            seek: self.seek,
            upgrade: self.upgrade,
        }
    }
}

/// No data message. Type 4.
#[derive(Debug, Clone, PartialEq)]
pub struct NoData {
    /// Request this message is for, see field `id` in [Request]
    pub request: u64,
}

impl CompactEncoding for NoData {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(sum_encoded_size!(self.request))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        Ok(map_encode!(buffer, self.request))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let (request, rest) = u64::decode(buffer)?;
        Ok((Self { request }, rest))
    }
}

/// Want message. Type 5.
#[derive(Debug, Clone, PartialEq)]
pub struct Want {
    /// Start index
    pub start: u64,
    /// Length
    pub length: u64,
}

impl CompactEncoding for Want {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(sum_encoded_size!(self.start, self.length))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        Ok(map_encode!(buffer, self.start, self.length))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ((start, length), rest) = map_decode!(buffer, [u64, u64]);
        Ok((Self { start, length }, rest))
    }
}

/// Un-want message. Type 6.
#[derive(Debug, Clone, PartialEq)]
pub struct Unwant {
    /// Start index
    pub start: u64,
    /// Length
    pub length: u64,
}

impl CompactEncoding for Unwant {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(sum_encoded_size!(self.start, self.length))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        Ok(map_encode!(buffer, self.start, self.length))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ((start, length), rest) = map_decode!(buffer, [u64, u64]);
        Ok((Self { start, length }, rest))
    }
}

/// Bitfield message. Type 7.
#[derive(Debug, Clone, PartialEq)]
pub struct Bitfield {
    /// Start index of bitfield
    pub start: u64,
    /// Bitfield in 32 bit chunks beginning from `start`
    pub bitfield: Vec<u32>,
}
impl CompactEncoding for Bitfield {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(sum_encoded_size!(self.start, self.bitfield))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        Ok(map_encode!(buffer, self.start, self.bitfield))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ((start, bitfield), rest) = map_decode!(buffer, [u64, Vec<u32>]);
        Ok((Self { start, bitfield }, rest))
    }
}

/// Range message. Type 8.
/// Notifies Peer's that the Sender has a range of contiguous blocks.
#[derive(Debug, Clone, PartialEq)]
pub struct Range {
    /// If true, notifies that data has been cleared from this range.
    /// If false, notifies existing data range.
    pub drop: bool,
    /// Range starts at this index
    pub start: u64,
    /// Length of the range
    pub length: u64,
}

impl CompactEncoding for Range {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        let mut out = 1 + sum_encoded_size!(self.start);
        if self.length != 1 {
            out += self.length.encoded_size()?;
        }
        Ok(out)
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        let mut flags: u8 = if self.drop { 1 } else { 0 };
        flags |= if self.length == 1 { 2 } else { 0 };
        let rest = write_array(&[flags], buffer)?;
        let rest = self.start.encode(rest)?;
        if self.length != 1 {
            return self.length.encode(rest);
        }
        Ok(rest)
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ([flags], rest) = take_array::<1>(buffer)?;
        let (start, rest) = u64::decode(rest)?;
        let drop = flags & 1 != 0;
        let (length, rest) = if flags & 2 != 0 {
            (1, rest)
        } else {
            u64::decode(rest)?
        };
        Ok((
            Range {
                drop,
                length,
                start,
            },
            rest,
        ))
    }
}

/// Extension message. Type 9. Use this for custom messages in your application.
#[derive(Debug, Clone, PartialEq)]
pub struct Extension {
    /// Name of the custom message
    pub name: String,
    /// Message content, use empty vector for no data.
    pub message: Vec<u8>,
}
impl CompactEncoding for Extension {
    fn encoded_size(&self) -> Result<usize, EncodingError> {
        Ok(sum_encoded_size!(self.name, self.message))
    }

    fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
        Ok(map_encode!(buffer, self.name, self.message))
    }

    fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
    where
        Self: Sized,
    {
        let ((name, message), rest) = map_decode!(buffer, [String, Vec<u8>]);
        Ok((Self { name, message }, rest))
    }
}