MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / decode_versioned

Function decode_versioned

nodedb-cluster/src/wire_version/envelope.rs:59–87  ·  view source on GitHub ↗

Decode a versioned wire message. Bytes must begin with the reserved [`ENVELOPE_MARKER`] (`0xc1`). Bytes without the marker are rejected — raw v1 frames are not accepted. An envelope with `version > WireVersion::CURRENT.0` is rejected with [`WireVersionError::UnsupportedVersion`].

(bytes: &[u8])

Source from the content-addressed store, hash-verified

57/// An envelope with `version > WireVersion::CURRENT.0` is rejected
58/// with [`WireVersionError::UnsupportedVersion`].
59pub fn decode_versioned<T>(bytes: &[u8]) -> Result<T, WireVersionError>
60where
61 T: zerompk::FromMessagePackOwned,
62{
63 match parse_envelope(bytes)? {
64 Some((version_raw, inner_bytes)) => {
65 // Version 0 inside the envelope is malformed — reject loudly.
66 if version_raw == 0 {
67 return Err(WireVersionError::DecodeFailure(
68 "v2 envelope with version 0 is invalid".to_string(),
69 ));
70 }
71 let peer_version = WireVersion(version_raw);
72 if peer_version > WireVersion::CURRENT {
73 return Err(WireVersionError::UnsupportedVersion {
74 peer_version,
75 supported_min: WireVersion::CURRENT,
76 supported_max: WireVersion::CURRENT,
77 });
78 }
79 zerompk::from_msgpack(inner_bytes).map_err(|e| {
80 WireVersionError::DecodeFailure(format!("decode inner (v{peer_version}): {e}"))
81 })
82 }
83 None => Err(WireVersionError::DecodeFailure(
84 "missing envelope marker: raw v1 frames are not accepted".to_string(),
85 )),
86 }
87}
88
89/// Wrap arbitrary pre-encoded bytes in a v2 versioned envelope.
90pub fn wrap_bytes_versioned(inner: &[u8]) -> Result<Vec<u8>, WireVersionError> {

Callers 3

load_entries_afterMethod · 0.85
decode_entryFunction · 0.85
v2_roundtripFunction · 0.85

Calls 3

WireVersionClass · 0.85
to_stringMethod · 0.80
parse_envelopeFunction · 0.70

Tested by 1

v2_roundtripFunction · 0.68