quepaxa 0.5.0

QuePaxa consensus for state machine replication: a sans-IO, no_std replicated log that, unlike Raft or Multi-Paxos, never stalls on election timeouts
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//! Canonical wire encoding of network messages (spec ยง7).
//!
//! Little-endian, fixed-width integers, `u32` length prefixes, zero-copy payloads.
//! Every length is checked against the remaining input and the configured limit
//! before anything is allocated.

use alloc::vec::Vec;
use bytes::{BufMut, Bytes};

use crate::proposal::{Proposal, ValueKey};
use crate::types::{NodeId, Slot, Step};

pub(crate) const MAGIC: [u8; 2] = *b"QP";
pub(crate) const WIRE_VERSION: u8 = 1;

const KIND_RECORD_REQUEST: u8 = 1;
const KIND_RECORD_REPLY: u8 = 2;
const KIND_DECIDED: u8 = 3;
const KIND_CATCH_UP: u8 = 4;
const KIND_ENTRIES: u8 = 5;
const KIND_FORWARD: u8 = 6;
const KIND_COMPACTED: u8 = 7;
const KIND_READ_QUERY: u8 = 8;
const KIND_READ_REPLY: u8 = 9;

const VALUE_INLINE: u8 = 0;

const NIL: u8 = 0;
const PRESENT: u8 = 1;
const ELIDED: u8 = 2;

/// Fields every message carries.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Envelope {
    pub cluster_id: [u8; 16],
    pub config_epoch: u64,
    pub from: NodeId,
    pub to: NodeId,
    pub commit_index: u64,
}

/// A proposal as it appears in a reply: its value may be elided when the requester
/// already holds it (same key as the request's proposal).
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "model", derive(Hash))]
pub(crate) struct WireProposal {
    pub priority: u64,
    pub key: ValueKey,
    pub value: Option<Bytes>,
}

impl WireProposal {
    /// Restores an elided value from the request's proposal; `None` if the reply refers
    /// to a value the requester cannot have (protocol violation by the peer).
    pub fn resolve(self, request: &Proposal) -> Option<Proposal> {
        let value = match self.value {
            Some(v) => v,
            None if self.key == request.key => request.value.clone(),
            None => return None,
        };
        Some(Proposal { priority: self.priority, key: self.key, value })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "model", derive(Hash))]
pub(crate) enum Body {
    RecordRequest {
        slot: Slot,
        step: Step,
        proposal: Proposal,
    },
    RecordReply {
        slot: Slot,
        req_step: Step,
        step: Step,
        first: Option<WireProposal>,
        prev_agg: Option<WireProposal>,
    },
    /// A slot's decided value, broadcast by the decider or sent instead of a record reply.
    Decided {
        slot: Slot,
        proposal: Proposal,
    },
    /// Asks a peer for decided slots starting at `from`.
    CatchUp {
        from: Slot,
    },
    /// Contiguous decided slots starting at `first`.
    Entries {
        first: Slot,
        proposals: Vec<Proposal>,
    },
    /// Entries a follower hands to the leader (an encoded batch).
    Forward {
        batch: Bytes,
    },
    /// The sender no longer has slots up to `through`; fetch its snapshot instead.
    Compacted {
        through: Slot,
    },
    /// Asks a recorder for its high-water mark, for read round `round` of the asker's
    /// `incarnation`.
    ReadQuery {
        incarnation: u64,
        round: u64,
    },
    /// The sender holds no state for any slot at or after `high`.
    ReadReply {
        incarnation: u64,
        round: u64,
        high: Slot,
    },
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Message {
    pub env: Envelope,
    pub body: Body,
}

impl Body {
    fn largest_value(&self) -> usize {
        let wire = |p: &Option<WireProposal>| p.as_ref().and_then(|p| p.value.as_ref()).map_or(0, Bytes::len);
        match self {
            Body::RecordRequest { proposal, .. } | Body::Decided { proposal, .. } => proposal.value.len(),
            Body::RecordReply { first, prev_agg, .. } => wire(first).max(wire(prev_agg)),
            Body::Entries { proposals, .. } => proposals.iter().map(|p| p.value.len()).max().unwrap_or(0),
            Body::Forward { batch } => batch.len(),
            Body::CatchUp { .. }
            | Body::Compacted { .. }
            | Body::ReadQuery { .. }
            | Body::ReadReply { .. } => 0,
        }
    }
}

/// What the receiver expects from every message.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "model", derive(Hash))]
pub(crate) struct WireCtx {
    pub cluster_id: [u8; 16],
    pub config_epoch: u64,
    pub max_message_bytes: u32,
    /// Largest proposal value (encoded batch) accepted.
    pub max_value_bytes: u32,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DecodeError {
    TooLarge,
    Truncated,
    BadMagic,
    BadVersion,
    WrongCluster,
    WrongEpoch,
    UnknownKind,
    BadTag,
    Trailing,
    BadChecksum,
}

/// Builds a reply proposal, eliding its value when it is the request's own proposal.
pub(crate) fn reply_proposal(p: &Proposal, request_key: ValueKey) -> WireProposal {
    let value = (p.key != request_key).then(|| p.value.clone());
    WireProposal { priority: p.priority, key: p.key, value }
}

impl Message {
    pub fn encode(&self) -> Bytes {
        let mut out = Vec::with_capacity(64 + self.payload_len());
        out.put_slice(&MAGIC);
        out.put_u8(WIRE_VERSION);
        out.put_u8(match self.body {
            Body::RecordRequest { .. } => KIND_RECORD_REQUEST,
            Body::RecordReply { .. } => KIND_RECORD_REPLY,
            Body::Decided { .. } => KIND_DECIDED,
            Body::CatchUp { .. } => KIND_CATCH_UP,
            Body::Entries { .. } => KIND_ENTRIES,
            Body::Forward { .. } => KIND_FORWARD,
            Body::Compacted { .. } => KIND_COMPACTED,
            Body::ReadQuery { .. } => KIND_READ_QUERY,
            Body::ReadReply { .. } => KIND_READ_REPLY,
        });
        let e = &self.env;
        out.put_slice(&e.cluster_id);
        out.put_u64_le(e.config_epoch);
        out.put_u32_le(e.from.0);
        out.put_u32_le(e.to.0);
        out.put_u64_le(e.commit_index);
        match &self.body {
            Body::RecordRequest { slot, step, proposal } => {
                out.put_u64_le(slot.0);
                out.put_u32_le(*step);
                put_head(&mut out, proposal.priority, proposal.key);
                put_value(&mut out, &proposal.value);
            }
            Body::RecordReply { slot, req_step, step, first, prev_agg } => {
                out.put_u64_le(slot.0);
                out.put_u32_le(*req_step);
                out.put_u32_le(*step);
                put_wire_proposal(&mut out, first.as_ref());
                put_wire_proposal(&mut out, prev_agg.as_ref());
            }
            Body::Decided { slot, proposal } => {
                out.put_u64_le(slot.0);
                put_proposal(&mut out, proposal);
            }
            Body::CatchUp { from } => out.put_u64_le(from.0),
            Body::Entries { first, proposals } => {
                out.put_u64_le(first.0);
                out.put_u32_le(proposals.len() as u32);
                for p in proposals {
                    put_proposal(&mut out, p);
                }
            }
            Body::Forward { batch } => put_value(&mut out, batch),
            Body::Compacted { through } => out.put_u64_le(through.0),
            Body::ReadQuery { incarnation, round } => {
                out.put_u64_le(*incarnation);
                out.put_u64_le(*round);
            }
            Body::ReadReply { incarnation, round, high } => {
                out.put_u64_le(*incarnation);
                out.put_u64_le(*round);
                out.put_u64_le(high.0);
            }
        }
        Bytes::from(out)
    }

    fn payload_len(&self) -> usize {
        let len = |p: &Option<WireProposal>| p.as_ref().and_then(|p| p.value.as_ref()).map_or(0, Bytes::len);
        match &self.body {
            Body::RecordRequest { proposal, .. } | Body::Decided { proposal, .. } => proposal.value.len(),
            Body::RecordReply { first, prev_agg, .. } => len(first) + len(prev_agg),
            Body::CatchUp { .. }
            | Body::Compacted { .. }
            | Body::ReadQuery { .. }
            | Body::ReadReply { .. } => 0,
            Body::Forward { batch } => batch.len(),
            Body::Entries { proposals, .. } => proposals.iter().map(|p| p.value.len() + 33).sum(),
        }
    }

    pub fn decode(buf: &Bytes, ctx: &WireCtx) -> Result<Self, DecodeError> {
        if buf.len() > ctx.max_message_bytes as usize {
            return Err(DecodeError::TooLarge);
        }
        let mut r = Reader::new(buf);
        if r.take(2)? != MAGIC {
            return Err(DecodeError::BadMagic);
        }
        if r.u8()? != WIRE_VERSION {
            return Err(DecodeError::BadVersion);
        }
        let kind = r.u8()?;
        let mut cluster_id = [0u8; 16];
        cluster_id.copy_from_slice(r.take(16)?);
        if cluster_id != ctx.cluster_id {
            return Err(DecodeError::WrongCluster);
        }
        let config_epoch = r.u64()?;
        if config_epoch != ctx.config_epoch {
            return Err(DecodeError::WrongEpoch);
        }
        let env = Envelope {
            cluster_id,
            config_epoch,
            from: NodeId(r.u32()?),
            to: NodeId(r.u32()?),
            commit_index: r.u64()?,
        };
        let body = match kind {
            KIND_RECORD_REQUEST => {
                let slot = Slot(r.u64()?);
                let step = r.u32()?;
                Body::RecordRequest { slot, step, proposal: r.proposal()? }
            }
            KIND_RECORD_REPLY => Body::RecordReply {
                slot: Slot(r.u64()?),
                req_step: r.u32()?,
                step: r.u32()?,
                first: r.wire_proposal()?,
                prev_agg: r.wire_proposal()?,
            },
            KIND_DECIDED => Body::Decided { slot: Slot(r.u64()?), proposal: r.proposal()? },
            KIND_CATCH_UP => Body::CatchUp { from: Slot(r.u64()?) },
            KIND_ENTRIES => {
                let first = Slot(r.u64()?);
                let n = r.u32()? as usize;
                // Each proposal takes at least 33 bytes, bounding the allocation by input size.
                let mut proposals = Vec::with_capacity(n.min(buf.len() / 33));
                for _ in 0..n {
                    proposals.push(r.proposal()?);
                }
                Body::Entries { first, proposals }
            }
            KIND_FORWARD => Body::Forward { batch: r.value()? },
            KIND_COMPACTED => Body::Compacted { through: Slot(r.u64()?) },
            KIND_READ_QUERY => Body::ReadQuery { incarnation: r.u64()?, round: r.u64()? },
            KIND_READ_REPLY => {
                Body::ReadReply { incarnation: r.u64()?, round: r.u64()?, high: Slot(r.u64()?) }
            }
            _ => return Err(DecodeError::UnknownKind),
        };
        r.finish()?;
        if body.largest_value() > ctx.max_value_bytes as usize {
            return Err(DecodeError::TooLarge);
        }
        Ok(Self { env, body })
    }
}

pub(crate) fn put_head(out: &mut Vec<u8>, priority: u64, key: ValueKey) {
    out.put_u64_le(priority);
    out.put_u32_le(key.origin.0);
    out.put_u64_le(key.incarnation);
    out.put_u64_le(key.seq);
}

pub(crate) fn put_value(out: &mut Vec<u8>, value: &Bytes) {
    out.put_u8(VALUE_INLINE);
    // Lengths beyond u32 are rejected earlier by `max_batch_bytes`.
    out.put_u32_le(value.len() as u32);
    out.put_slice(value);
}

pub(crate) fn put_proposal(out: &mut Vec<u8>, p: &Proposal) {
    put_head(out, p.priority, p.key);
    put_value(out, &p.value);
}

pub(crate) fn put_opt_proposal(out: &mut Vec<u8>, p: Option<&Proposal>) {
    match p {
        None => out.put_u8(NIL),
        Some(p) => {
            out.put_u8(PRESENT);
            put_proposal(out, p);
        }
    }
}

fn put_wire_proposal(out: &mut Vec<u8>, p: Option<&WireProposal>) {
    match p {
        None => out.put_u8(NIL),
        Some(p) => {
            out.put_u8(if p.value.is_some() { PRESENT } else { ELIDED });
            put_head(out, p.priority, p.key);
            if let Some(v) = &p.value {
                put_value(out, v);
            }
        }
    }
}

pub(crate) struct Reader<'a> {
    buf: &'a Bytes,
    pos: usize,
}

impl<'a> Reader<'a> {
    pub fn new(buf: &'a Bytes) -> Self {
        Self { buf, pos: 0 }
    }

    pub fn pos(&self) -> usize {
        self.pos
    }

    pub fn finish(&self) -> Result<(), DecodeError> {
        if self.pos == self.buf.len() { Ok(()) } else { Err(DecodeError::Trailing) }
    }

    pub fn proposal(&mut self) -> Result<Proposal, DecodeError> {
        let (priority, key) = self.head()?;
        Ok(Proposal { priority, key, value: self.value()? })
    }

    pub fn peek_u8(&self) -> Result<u8, DecodeError> {
        self.buf.get(self.pos).copied().ok_or(DecodeError::Truncated)
    }

    pub fn opt_proposal(&mut self) -> Result<Option<Proposal>, DecodeError> {
        match self.u8()? {
            NIL => Ok(None),
            PRESENT => Ok(Some(self.proposal()?)),
            _ => Err(DecodeError::BadTag),
        }
    }

    pub fn take(&mut self, n: usize) -> Result<&[u8], DecodeError> {
        let end = self.pos.checked_add(n).filter(|&e| e <= self.buf.len()).ok_or(DecodeError::Truncated)?;
        let s = &self.buf[self.pos..end];
        self.pos = end;
        Ok(s)
    }

    pub fn u8(&mut self) -> Result<u8, DecodeError> {
        Ok(self.take(1)?[0])
    }

    pub fn u32(&mut self) -> Result<u32, DecodeError> {
        let (b, _) = self.take(4)?.as_chunks::<4>();
        Ok(u32::from_le_bytes(b[0]))
    }

    pub fn u64(&mut self) -> Result<u64, DecodeError> {
        let (b, _) = self.take(8)?.as_chunks::<8>();
        Ok(u64::from_le_bytes(b[0]))
    }

    pub fn head(&mut self) -> Result<(u64, ValueKey), DecodeError> {
        let priority = self.u64()?;
        let key = ValueKey { origin: NodeId(self.u32()?), incarnation: self.u64()?, seq: self.u64()? };
        Ok((priority, key))
    }

    /// Zero-copy: the value shares the input buffer.
    pub fn value(&mut self) -> Result<Bytes, DecodeError> {
        if self.u8()? != VALUE_INLINE {
            return Err(DecodeError::BadTag);
        }
        let len = self.u32()? as usize;
        let start = self.pos;
        self.take(len)?;
        Ok(self.buf.slice(start..start + len))
    }

    fn wire_proposal(&mut self) -> Result<Option<WireProposal>, DecodeError> {
        let tag = self.u8()?;
        if tag == NIL {
            return Ok(None);
        }
        if tag != PRESENT && tag != ELIDED {
            return Err(DecodeError::BadTag);
        }
        let (priority, key) = self.head()?;
        let value = if tag == PRESENT { Some(self.value()?) } else { None };
        Ok(Some(WireProposal { priority, key, value }))
    }
}

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

    const CTX: WireCtx = WireCtx {
        cluster_id: [0xAB; 16],
        config_epoch: 7,
        max_message_bytes: 1 << 20,
        max_value_bytes: 1 << 19,
    };

    fn env() -> Envelope {
        Envelope { cluster_id: [0xAB; 16], config_epoch: 7, from: NodeId(1), to: NodeId(2), commit_index: 9 }
    }

    fn hex(s: &str) -> Vec<u8> {
        let s: Vec<u8> = s.bytes().filter(u8::is_ascii_hexdigit).collect();
        s.chunks(2).map(|c| u8::from_str_radix(core::str::from_utf8(c).unwrap(), 16).unwrap()).collect()
    }

    const ENVELOPE_HEX: &str =
        "5150 01 KK abababababababababababababababab 0700000000000000 01000000 02000000 0900000000000000";

    fn golden(kind: &str, body: &str) -> Vec<u8> {
        hex(&ENVELOPE_HEX.replace("KK", kind).replace(' ', "")).into_iter().chain(hex(body)).collect()
    }

    fn request() -> Message {
        let proposal = Proposal {
            priority: 0x0102,
            key: ValueKey { origin: NodeId(3), incarnation: 4, seq: 5 },
            value: Bytes::from_static(b"hi"),
        };
        Message { env: env(), body: Body::RecordRequest { slot: Slot(6), step: 4, proposal } }
    }

    #[test]
    fn record_request_matches_golden_bytes_v1() {
        let body = "0600000000000000 04000000 0201000000000000 03000000 0400000000000000 0500000000000000 00 02000000 6869";
        assert_eq!(request().encode().to_vec(), golden("01", body));
    }

    #[test]
    fn record_reply_matches_golden_bytes_v1() {
        let first = WireProposal {
            priority: 1,
            key: ValueKey { origin: NodeId(3), incarnation: 4, seq: 5 },
            value: None,
        };
        let msg = Message {
            env: env(),
            body: Body::RecordReply {
                slot: Slot(6),
                req_step: 4,
                step: 5,
                first: Some(first),
                prev_agg: None,
            },
        };
        let body = "0600000000000000 04000000 05000000 02 0100000000000000 03000000 0400000000000000 0500000000000000 00";
        assert_eq!(msg.encode().to_vec(), golden("02", body));
    }

    #[test]
    fn read_query_and_reply_match_golden_bytes_v1() {
        let query = Message { env: env(), body: Body::ReadQuery { incarnation: 3, round: 7 } };
        assert_eq!(query.encode().to_vec(), golden("08", "0300000000000000 0700000000000000"));
        let reply =
            Message { env: env(), body: Body::ReadReply { incarnation: 3, round: 7, high: Slot(0x0102) } };
        assert_eq!(
            reply.encode().to_vec(),
            golden("09", "0300000000000000 0700000000000000 0201000000000000")
        );
    }

    #[test]
    fn decode_shares_the_input_buffer() {
        let bytes = request().encode();
        let Body::RecordRequest { proposal, .. } = Message::decode(&bytes, &CTX).unwrap().body else {
            panic!()
        };
        assert_eq!(proposal.value.as_ptr(), bytes[bytes.len() - 2..].as_ptr());
    }

    #[test]
    fn foreign_cluster_epoch_and_version_are_rejected() {
        let bytes = request().encode();
        let mut other = CTX;
        other.cluster_id = [0; 16];
        assert_eq!(Message::decode(&bytes, &other), Err(DecodeError::WrongCluster));
        let mut other = CTX;
        other.config_epoch = 8;
        assert_eq!(Message::decode(&bytes, &other), Err(DecodeError::WrongEpoch));
        let mut v = bytes.to_vec();
        v[2] = 2;
        assert_eq!(Message::decode(&Bytes::from(v), &CTX), Err(DecodeError::BadVersion));
    }

    #[test]
    fn oversized_length_prefix_is_rejected_without_allocating() {
        let mut v = request().encode().to_vec();
        let n = v.len();
        v[n - 6..n - 2].copy_from_slice(&u32::MAX.to_le_bytes());
        assert_eq!(Message::decode(&Bytes::from(v), &CTX), Err(DecodeError::Truncated));
    }

    #[test]
    fn value_above_the_batch_limit_is_rejected() {
        let tight = WireCtx { max_value_bytes: 1, ..CTX };
        assert_eq!(Message::decode(&request().encode(), &tight), Err(DecodeError::TooLarge));
        let exact = WireCtx { max_value_bytes: 2, ..CTX };
        assert_eq!(Message::decode(&request().encode(), &exact), Ok(request()));
    }

    #[test]
    fn message_above_limit_is_rejected() {
        let len = request().encode().len() as u32;
        let small = WireCtx { max_message_bytes: len - 1, ..CTX };
        assert_eq!(Message::decode(&request().encode(), &small), Err(DecodeError::TooLarge));
        let exact = WireCtx { max_message_bytes: len, ..CTX };
        assert_eq!(Message::decode(&request().encode(), &exact), Ok(request()));
    }

    #[test]
    fn trailing_bytes_are_rejected() {
        let mut v = request().encode().to_vec();
        v.push(0);
        assert_eq!(Message::decode(&Bytes::from(v), &CTX), Err(DecodeError::Trailing));
    }

    #[test]
    fn elided_value_resolves_only_against_the_same_key() {
        let mine = prop(9, 1, 0);
        let elided = reply_proposal(&mine, mine.key);
        assert_eq!(elided.value, None);
        assert_eq!(elided.clone().resolve(&mine), Some(mine.clone()));
        assert_eq!(elided.resolve(&prop(9, 2, 0)), None);
    }

    #[test]
    fn other_proposals_keep_their_value_in_replies() {
        let other = prop(9, 2, 0);
        assert_eq!(reply_proposal(&other, prop(1, 1, 0).key).value, Some(other.value.clone()));
    }

    fn arb_prop() -> impl proptest::strategy::Strategy<Value = Proposal> {
        use proptest::prelude::*;
        (
            any::<u64>(),
            any::<u32>(),
            any::<u64>(),
            any::<u64>(),
            proptest::collection::vec(any::<u8>(), 0..64),
        )
            .prop_map(|(priority, o, i, s, v)| Proposal {
                priority,
                key: ValueKey { origin: NodeId(o), incarnation: i, seq: s },
                value: Bytes::from(v),
            })
    }

    fn arb_wire() -> impl proptest::strategy::Strategy<Value = Option<WireProposal>> {
        use proptest::prelude::*;
        proptest::option::of((arb_prop(), any::<bool>()).prop_map(|(p, elide)| WireProposal {
            priority: p.priority,
            key: p.key,
            value: (!elide).then_some(p.value),
        }))
    }

    proptest::proptest! {
        #[test]
        fn request_round_trips(slot: u64, step: u32, from: u32, to: u32, ci: u64, p in arb_prop()) {
            let env = Envelope { from: NodeId(from), to: NodeId(to), commit_index: ci, ..env() };
            let m = Message { env, body: Body::RecordRequest { slot: Slot(slot), step, proposal: p.clone() } };
            let d = Message::decode(&m.encode(), &CTX).unwrap();
            let Body::RecordRequest { proposal, .. } = &d.body else { panic!() };
            proptest::prop_assert_eq!(&proposal.value, &p.value);
            proptest::prop_assert_eq!(d, m);
        }

        #[test]
        fn reply_round_trips(slot: u64, rs: u32, s: u32, f in arb_wire(), a in arb_wire()) {
            let m = Message { env: env(), body: Body::RecordReply { slot: Slot(slot), req_step: rs, step: s, first: f, prev_agg: a } };
            proptest::prop_assert_eq!(Message::decode(&m.encode(), &CTX).unwrap(), m);
        }
    }

    proptest::proptest! {
        #[test]
        fn catch_up_kinds_round_trip(from: u64, round: u64, ps in proptest::collection::vec(arb_prop(), 0..4)) {
            let batch = ps.first().map(|p| p.value.clone()).unwrap_or_default();
            for body in [
                Body::CatchUp { from: Slot(from) },
                Body::Entries { first: Slot(from), proposals: ps.clone() },
                Body::Forward { batch },
                Body::Compacted { through: Slot(from) },
                Body::ReadQuery { incarnation: from, round },
                Body::ReadReply { incarnation: from, round, high: Slot(from) },
            ] {
                let m = Message { env: env(), body };
                proptest::prop_assert_eq!(Message::decode(&m.encode(), &CTX).unwrap(), m);
            }
        }
    }

    #[test]
    fn decode_never_panics_on_arbitrary_input() {
        bolero::check!().for_each(|input: &[u8]| {
            let _ = Message::decode(&Bytes::copy_from_slice(input), &CTX);
        });
    }

    #[test]
    fn decode_never_panics_on_mutated_valid_messages() {
        let base = request().encode().to_vec();
        bolero::check!().with_type::<(u16, u8, u8)>().for_each(|&(pos, val, cut)| {
            let mut v = base.clone();
            let i = pos as usize % v.len();
            v[i] = val;
            v.truncate(v.len() - (cut as usize % 4));
            let _ = Message::decode(&Bytes::from(v), &CTX);
        });
    }
}