feat(core): WIRE_VERSION byte on every frame

This commit is contained in:
2026-10-22 08:44:09 -07:00
parent 9d4ed7db24
commit 99036514c2
6 changed files with 217 additions and 50 deletions
+8 -5
View File
@@ -7,6 +7,7 @@
use std::collections::VecDeque;
use tines_bt::{adv, BtTransport, Radio, RadioError, RadioEvent};
use tines_core::consts::WIRE_VERSION;
use tines_core::observation::NodeId;
use tines_core::transport::{Event, Transport};
@@ -73,12 +74,14 @@ fn advertising_payload_roundtrips_node_id() {
assert_eq!(adv::decode_node_id(&p), Some(node));
}
/// A framed envelope: one tag byte, then the 16-byte `from`. This is the layout
/// `frame_sender` peeks — the transport delivers this whole envelope to core
/// (which decodes it), so the `from` must be stamped with the sender's id.
/// A framed envelope: a `WIRE_VERSION` byte, a variant tag, then the 16-byte
/// `from`. This is the layout `frame_sender` peeks — the transport delivers this
/// whole envelope to core (which decodes it), so the `from` must be stamped with
/// the sender's id.
fn frame(from: NodeId, payload: &[u8]) -> Vec<u8> {
let mut b = Vec::with_capacity(1 + 16 + payload.len());
b.push(0u8); // version/tag byte
let mut b = Vec::with_capacity(2 + 16 + payload.len());
b.push(WIRE_VERSION); // wire version byte
b.push(0u8); // postcard externally-tagged variant tag
b.extend_from_slice(&from);
b.extend_from_slice(payload);
b
+7 -5
View File
@@ -9,6 +9,7 @@
use std::collections::{BTreeSet, HashMap, VecDeque};
use tines_bt::{adv, resolve_bound, BtTransport, Radio, RadioError, RadioEvent};
use tines_core::consts::WIRE_VERSION;
use tines_core::observation::NodeId;
use tines_core::transport::{Event, Transport};
@@ -58,11 +59,12 @@ impl Drop for MockRadio {
}
}
/// Build a minimal frame: one leading tag byte, then the 16-byte `from`.
/// `frame_sender` reads `from` from bytes 1..17, so this matches the wire.
/// Build a minimal frame: a `WIRE_VERSION` byte, then the 16-byte `from`.
/// `frame_sender` reads `from` from bytes 2..18, so this matches the wire.
fn frame(from: NodeId) -> Vec<u8> {
let mut b = vec![0u8; 1 + 16];
b[1..1 + 16].copy_from_slice(&from);
let mut b = vec![0u8; 2 + 16];
b[2..2 + 16].copy_from_slice(&from);
b[0] = WIRE_VERSION;
b
}
@@ -164,7 +166,7 @@ fn frame_boundaries_preserved_at_transport() {
let peer: NodeId = [2u8; 16];
let frame_a = frame(peer);
let mut frame_b = frame(peer);
frame_b[0] = 0xFF; // differ from frame_a via the tag byte, so `from` stays == peer
frame_b[1] = 0xFF; // differ via the variant tag (skipped by frame_sender), so `from` stays == peer
let a_expected = frame_a.clone();
let b_expected = frame_b.clone();