bones-core 0.24.4

Core data structures, CRDT event model, and projection engine for bones
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
//! Compact binary serialization for Interval Tree Clock stamps.
//!
//! # Wire format (v1)
//!
//! ```text
//! version:            u8        (currently 1)
//! id_bits_len:        varint    (number of meaningful bits)
//! id_bits:            bytes     (packed MSB-first)
//! event_bits_len:     varint    (number of meaningful bits)
//! event_bits:         bytes     (packed MSB-first)
//! event_values_len:   varint    (number of bytes)
//! event_values:       bytes     (varint-encoded node values)
//! ```
//!
//! `Id` encoding (preorder):
//! - `0` bit = leaf, followed by 1 bit for value (`0` => `Id::Zero`, `1` => `Id::One`)
//! - `1` bit = branch, followed by left subtree then right subtree
//!
//! `Event` encoding (preorder):
//! - node kind bit: `0` leaf / `1` branch
//! - node value/base: unsigned varint
//! - branch then continues with left subtree, then right subtree
//!
//! This split representation keeps branch/leaf structure bit-packed while preserving
//! compact integer encoding for counters.

use super::itc::{Event, Id, Stamp};
use std::error::Error;
use std::fmt;

const FORMAT_VERSION: u8 = 1;
const VARINT_CONTINUATION_BIT: u8 = 0x80;
const VARINT_PAYLOAD_MASK: u8 = 0x7f;

/// Errors returned by compact ITC serialization/deserialization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodecError {
    /// The input buffer was empty.
    EmptyInput,
    /// The input starts with an unknown format version.
    UnsupportedVersion(u8),
    /// More bytes or bits were required to complete decoding.
    UnexpectedEof,
    /// A varint value exceeded target integer capacity.
    VarintOverflow,
    /// A declared bit/byte length is internally inconsistent.
    InvalidLength,
    /// Bytes remained after a successful parse.
    TrailingBytes,
    /// Meaningful bits remained after a successful parse.
    TrailingBits,
}

impl fmt::Display for CodecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyInput => write!(f, "input is empty"),
            Self::UnsupportedVersion(version) => {
                write!(f, "unsupported compact ITC version: {version}")
            }
            Self::UnexpectedEof => write!(f, "unexpected end of input"),
            Self::VarintOverflow => write!(f, "varint overflow"),
            Self::InvalidLength => write!(f, "invalid length prefix"),
            Self::TrailingBytes => write!(f, "trailing bytes after decode"),
            Self::TrailingBits => write!(f, "trailing bits after decode"),
        }
    }
}

impl Error for CodecError {}

impl Stamp {
    /// Serialize this stamp using the compact binary ITC format.
    #[must_use]
    pub fn serialize_compact(&self) -> Vec<u8> {
        let mut id_bits = BitWriter::default();
        encode_id(&self.id, &mut id_bits);
        let (id_bytes, id_bit_len) = id_bits.into_parts();

        let mut event_bits = BitWriter::default();
        let mut event_values = Vec::new();
        encode_event(&self.event, &mut event_bits, &mut event_values);
        let (event_bit_bytes, event_bit_len) = event_bits.into_parts();

        let mut out = Vec::with_capacity(
            1 + id_bytes.len() + event_bit_bytes.len() + event_values.len() + 12,
        );
        out.push(FORMAT_VERSION);
        encode_usize_varint(id_bit_len, &mut out);
        out.extend_from_slice(&id_bytes);
        encode_usize_varint(event_bit_len, &mut out);
        out.extend_from_slice(&event_bit_bytes);
        encode_usize_varint(event_values.len(), &mut out);
        out.extend_from_slice(&event_values);
        out
    }

    /// Deserialize a stamp from the compact binary ITC format.
    ///
    /// # Errors
    ///
    /// Returns [`CodecError`] when the input is malformed, truncated,
    /// has an unknown format version, or contains trailing data.
    pub fn deserialize_compact(input: &[u8]) -> Result<Self, CodecError> {
        if input.is_empty() {
            return Err(CodecError::EmptyInput);
        }

        let version = input[0];
        if version != FORMAT_VERSION {
            return Err(CodecError::UnsupportedVersion(version));
        }

        let mut cursor = 1usize;

        let id_bit_len = decode_usize_varint(input, &mut cursor)?;
        let id_byte_len = bytes_for_bits(id_bit_len);
        let id_bytes = take_slice(input, &mut cursor, id_byte_len)?;

        let event_bit_len = decode_usize_varint(input, &mut cursor)?;
        let event_bit_byte_len = bytes_for_bits(event_bit_len);
        let event_bits = take_slice(input, &mut cursor, event_bit_byte_len)?;

        let event_values_len = decode_usize_varint(input, &mut cursor)?;
        let event_values = take_slice(input, &mut cursor, event_values_len)?;

        if cursor != input.len() {
            return Err(CodecError::TrailingBytes);
        }

        let mut id_reader = BitReader::new(id_bytes, id_bit_len)?;
        let id = decode_id(&mut id_reader)?;
        if !id_reader.is_exhausted() {
            return Err(CodecError::TrailingBits);
        }

        let mut event_reader = BitReader::new(event_bits, event_bit_len)?;
        let mut event_cursor = 0usize;
        let event = decode_event(&mut event_reader, event_values, &mut event_cursor)?;
        if !event_reader.is_exhausted() {
            return Err(CodecError::TrailingBits);
        }
        if event_cursor != event_values.len() {
            return Err(CodecError::TrailingBytes);
        }

        Ok(Self::new(id, event).normalize())
    }
}

#[derive(Default)]
struct BitWriter {
    bytes: Vec<u8>,
    bit_len: usize,
}

impl BitWriter {
    fn push_bit(&mut self, bit: bool) {
        let byte_index = self.bit_len / 8;
        let bit_offset = 7 - (self.bit_len % 8);

        if byte_index == self.bytes.len() {
            self.bytes.push(0);
        }

        if bit {
            self.bytes[byte_index] |= 1u8 << bit_offset;
        }

        self.bit_len += 1;
    }

    fn into_parts(self) -> (Vec<u8>, usize) {
        (self.bytes, self.bit_len)
    }
}

struct BitReader<'a> {
    bytes: &'a [u8],
    bit_len: usize,
    cursor: usize,
}

impl<'a> BitReader<'a> {
    fn new(bytes: &'a [u8], bit_len: usize) -> Result<Self, CodecError> {
        let total_bits = bytes
            .len()
            .checked_mul(8)
            .ok_or(CodecError::InvalidLength)?;
        if bit_len > total_bits {
            return Err(CodecError::InvalidLength);
        }

        Ok(Self {
            bytes,
            bit_len,
            cursor: 0,
        })
    }

    fn read_bit(&mut self) -> Result<bool, CodecError> {
        if self.cursor >= self.bit_len {
            return Err(CodecError::UnexpectedEof);
        }

        let byte_index = self.cursor / 8;
        let bit_offset = 7 - (self.cursor % 8);
        let bit = ((self.bytes[byte_index] >> bit_offset) & 1u8) == 1u8;
        self.cursor += 1;
        Ok(bit)
    }

    const fn is_exhausted(&self) -> bool {
        self.cursor == self.bit_len
    }
}

const fn bytes_for_bits(bit_len: usize) -> usize {
    bit_len.div_ceil(8)
}

fn take_slice<'a>(input: &'a [u8], cursor: &mut usize, len: usize) -> Result<&'a [u8], CodecError> {
    let end = cursor.checked_add(len).ok_or(CodecError::InvalidLength)?;
    if end > input.len() {
        return Err(CodecError::UnexpectedEof);
    }

    let slice = &input[*cursor..end];
    *cursor = end;
    Ok(slice)
}

fn encode_id(id: &Id, out: &mut BitWriter) {
    match id {
        Id::Zero => {
            out.push_bit(false);
            out.push_bit(false);
        }
        Id::One => {
            out.push_bit(false);
            out.push_bit(true);
        }
        Id::Branch(left, right) => {
            out.push_bit(true);
            encode_id(left, out);
            encode_id(right, out);
        }
    }
}

fn decode_id(bits: &mut BitReader<'_>) -> Result<Id, CodecError> {
    let is_branch = bits.read_bit()?;
    if !is_branch {
        let leaf_is_one = bits.read_bit()?;
        return Ok(if leaf_is_one { Id::one() } else { Id::zero() });
    }

    let left = decode_id(bits)?;
    let right = decode_id(bits)?;
    Ok(Id::branch(left, right))
}

fn encode_event(event: &Event, bit_out: &mut BitWriter, value_out: &mut Vec<u8>) {
    match event {
        Event::Leaf(value) => {
            bit_out.push_bit(false);
            encode_u32_varint(*value, value_out);
        }
        Event::Branch(base, left, right) => {
            bit_out.push_bit(true);
            encode_u32_varint(*base, value_out);
            encode_event(left, bit_out, value_out);
            encode_event(right, bit_out, value_out);
        }
    }
}

fn decode_event(
    bits: &mut BitReader<'_>,
    values: &[u8],
    value_cursor: &mut usize,
) -> Result<Event, CodecError> {
    let is_branch = bits.read_bit()?;
    let value = decode_u32_varint(values, value_cursor)?;

    if !is_branch {
        return Ok(Event::leaf(value));
    }

    let left = decode_event(bits, values, value_cursor)?;
    let right = decode_event(bits, values, value_cursor)?;
    Ok(Event::branch(value, left, right))
}

fn encode_usize_varint(mut value: usize, out: &mut Vec<u8>) {
    loop {
        let low_bits = value & usize::from(VARINT_PAYLOAD_MASK);
        let [mut byte, ..] = low_bits.to_le_bytes();

        value >>= 7;
        if value != 0 {
            byte |= VARINT_CONTINUATION_BIT;
            out.push(byte);
            continue;
        }

        out.push(byte);
        break;
    }
}

fn decode_usize_varint(input: &[u8], cursor: &mut usize) -> Result<usize, CodecError> {
    let mut value = 0usize;
    let mut shift = 0u32;

    loop {
        if *cursor >= input.len() {
            return Err(CodecError::UnexpectedEof);
        }

        let byte = input[*cursor];
        *cursor += 1;

        let payload = usize::from(byte & VARINT_PAYLOAD_MASK);
        let shifted = payload
            .checked_shl(shift)
            .ok_or(CodecError::VarintOverflow)?;
        value = value
            .checked_add(shifted)
            .ok_or(CodecError::VarintOverflow)?;

        if (byte & VARINT_CONTINUATION_BIT) == 0 {
            return Ok(value);
        }

        shift = shift.checked_add(7).ok_or(CodecError::VarintOverflow)?;
        if shift >= usize::BITS {
            return Err(CodecError::VarintOverflow);
        }
    }
}

fn encode_u32_varint(mut value: u32, out: &mut Vec<u8>) {
    loop {
        let low_bits = value & u32::from(VARINT_PAYLOAD_MASK);
        let [mut byte, ..] = low_bits.to_le_bytes();

        value >>= 7;
        if value != 0 {
            byte |= VARINT_CONTINUATION_BIT;
            out.push(byte);
            continue;
        }

        out.push(byte);
        break;
    }
}

fn decode_u32_varint(input: &[u8], cursor: &mut usize) -> Result<u32, CodecError> {
    let mut value = 0u32;
    let mut shift = 0u32;

    loop {
        if *cursor >= input.len() {
            return Err(CodecError::UnexpectedEof);
        }

        let byte = input[*cursor];
        *cursor += 1;

        let payload = u32::from(byte & VARINT_PAYLOAD_MASK);
        let shifted = payload
            .checked_shl(shift)
            .ok_or(CodecError::VarintOverflow)?;
        value = value
            .checked_add(shifted)
            .ok_or(CodecError::VarintOverflow)?;

        if (byte & VARINT_CONTINUATION_BIT) == 0 {
            return Ok(value);
        }

        shift = shift.checked_add(7).ok_or(CodecError::VarintOverflow)?;
        if shift >= u32::BITS {
            return Err(CodecError::VarintOverflow);
        }
    }
}

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

    #[test]
    fn compact_roundtrip_seed_stamp() {
        let stamp = Stamp::seed();
        let bytes = stamp.serialize_compact();
        let decoded = Stamp::deserialize_compact(&bytes);
        assert_eq!(decoded, Ok(stamp));
    }

    #[test]
    fn compact_roundtrip_complex_stamp() {
        let stamp = sample_eight_agent_stamp();
        let bytes = stamp.serialize_compact();
        let decoded = Stamp::deserialize_compact(&bytes);
        assert_eq!(decoded, Ok(stamp));
    }

    #[test]
    fn compact_single_agent_size_stays_small() {
        let stamp = Stamp::seed();
        let bytes = stamp.serialize_compact();
        assert!(
            bytes.len() <= 20,
            "single-agent compact stamp too large: {} bytes",
            bytes.len()
        );
    }

    #[test]
    fn compact_eight_agent_size_stays_under_target() {
        let stamp = sample_eight_agent_stamp();
        let bytes = stamp.serialize_compact();
        assert!(
            bytes.len() <= 50,
            "8-agent compact stamp too large: {} bytes",
            bytes.len()
        );
    }

    #[test]
    fn rejects_unknown_version() {
        let err = Stamp::deserialize_compact(&[99]);
        assert_eq!(err, Err(CodecError::UnsupportedVersion(99)));
    }

    #[test]
    fn rejects_trailing_bytes() {
        let mut bytes = Stamp::seed().serialize_compact();
        bytes.push(0);
        let err = Stamp::deserialize_compact(&bytes);
        assert_eq!(err, Err(CodecError::TrailingBytes));
    }

    proptest! {
        #[test]
        fn random_stamps_roundtrip(stamp in arb_stamp()) {
            let bytes = stamp.serialize_compact();
            let decoded = Stamp::deserialize_compact(&bytes);
            prop_assert_eq!(decoded, Ok(stamp));
        }
    }

    fn sample_eight_agent_stamp() -> Stamp {
        let id = Id::branch(
            Id::branch(
                Id::branch(Id::one(), Id::zero()),
                Id::branch(Id::zero(), Id::one()),
            ),
            Id::branch(
                Id::branch(Id::one(), Id::zero()),
                Id::branch(Id::zero(), Id::one()),
            ),
        );

        let event = Event::branch(
            1,
            Event::branch(
                0,
                Event::branch(0, Event::leaf(3), Event::leaf(1)),
                Event::branch(1, Event::leaf(2), Event::leaf(0)),
            ),
            Event::branch(
                0,
                Event::branch(2, Event::leaf(1), Event::leaf(0)),
                Event::branch(0, Event::leaf(4), Event::leaf(2)),
            ),
        );

        Stamp::new(id, event).normalize()
    }

    fn arb_stamp() -> impl Strategy<Value = Stamp> {
        (arb_id(), arb_event()).prop_map(|(id, event)| Stamp::new(id, event).normalize())
    }

    fn arb_id() -> impl Strategy<Value = Id> {
        let leaf = prop_oneof![Just(Id::zero()), Just(Id::one())];
        leaf.prop_recursive(4, 64, 2, |inner| {
            (inner.clone(), inner).prop_map(|(left, right)| Id::branch(left, right))
        })
    }

    fn arb_event() -> impl Strategy<Value = Event> {
        let leaf = (0u32..=25).prop_map(Event::leaf);
        leaf.prop_recursive(4, 128, 2, |inner| {
            (0u32..=10, inner.clone(), inner)
                .prop_map(|(base, left, right)| Event::branch(base, left, right))
        })
    }
}