codec.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! The chatview storage wire format.
  19. //!
  20. //! kv key: `[u64 BE ts][32-byte msg id]`
  21. //! kv value: `[u8 tag][type-owned bytes]` — the tag is the `MsgType`
  22. //! discriminant, the rest is owned by the message type.
  23. //!
  24. //! privmsg payload: `[nick: String][text: String][confirmed: bool][channel: String]`
  25. //! datemsg payload: `[local-midnight ts: u64]` (derived; never stored)
  26. //!
  27. //! The channel is always present so entries read from any tree — their
  28. //! own channel's or an aggregate like the global feed — identify their
  29. //! room.
  30. //!
  31. //! This is a clean break from the previous chatview: values it cannot
  32. //! decode are corrupt data and fail explicitly (panic) with a message
  33. //! identifying the entry — never a silent skip or a misread.
  34. use std::io::Cursor;
  35. use darkfi_serial::{Decodable, Encodable};
  36. use super::{MessageId, MsgRecord, MsgType, Timestamp};
  37. /// Serialized kv key length: 8-byte timestamp + 32-byte msg id.
  38. pub const KEY_LEN: usize = 8 + 32;
  39. /// Decode a wire tag. Unknown tags are corrupt data and panic — errors
  40. /// are always explicit, never a silent skip.
  41. pub fn msg_type_from_u8(tag: u8) -> MsgType {
  42. match tag {
  43. 0 => MsgType::PrivMsg,
  44. 1 => MsgType::FileMsg,
  45. 2 => MsgType::DateMsg,
  46. _ => panic!("unknown msg type tag {tag}"),
  47. }
  48. }
  49. /// Encode an entry key: `[u64 BE ts][msg id]`.
  50. pub fn encode_key(ts: Timestamp, id: &MessageId) -> [u8; KEY_LEN] {
  51. let mut key = [0u8; KEY_LEN];
  52. key[..8].copy_from_slice(&ts.to_be_bytes());
  53. key[8..].copy_from_slice(&id.0);
  54. key
  55. }
  56. /// Decode an entry key back into its `(ts, msg_id)` composite.
  57. ///
  58. /// ## Panics
  59. ///
  60. /// If the key is not [`KEY_LEN`] bytes.
  61. pub fn decode_key(key: &[u8]) -> (Timestamp, MessageId) {
  62. assert_eq!(key.len(), KEY_LEN, "corrupt chat entry key: {key:?} (expected {KEY_LEN} bytes)");
  63. let ts_bytes: [u8; 8] = key[..8].try_into().unwrap();
  64. let id: [u8; 32] = key[8..].try_into().unwrap();
  65. (Timestamp::from_be_bytes(ts_bytes), MessageId(id))
  66. }
  67. /// Encode an entry value: `[tag][type-owned payload]`.
  68. pub fn encode_value(msg_type: MsgType, payload: &[u8]) -> Vec<u8> {
  69. let mut val = Vec::with_capacity(1 + payload.len());
  70. val.push(msg_type as u8);
  71. val.extend_from_slice(payload);
  72. val
  73. }
  74. /// Decode an entry value into a record (height starts at zero; the
  75. /// owning type node measures once materialized).
  76. ///
  77. /// ## Panics
  78. ///
  79. /// On an empty value, an unknown type tag, or an undecodable payload —
  80. /// identifying the entry by ts/id and the failure. Corrupt data is
  81. /// never silently skipped.
  82. pub fn decode_value(val: &[u8], ts: Timestamp, id: &MessageId) -> MsgRecord {
  83. let Some((&tag, payload)) = val.split_first() else {
  84. panic!("corrupt chat entry: empty value [ts={ts} id={id}]")
  85. };
  86. let msg_type = msg_type_from_u8(tag);
  87. match msg_type {
  88. MsgType::PrivMsg => {
  89. let (_nick, _text, _confirmed, _channel) = decode_privmsg_payload(payload, ts, id);
  90. }
  91. MsgType::DateMsg => {
  92. let _midnight = decode_datemsg_payload(payload, ts, id);
  93. }
  94. MsgType::FileMsg => {
  95. panic!("corrupt chat entry: filemsg is derived and never stored [ts={ts} id={id}]")
  96. }
  97. }
  98. MsgRecord { ts, id: *id, msg_type, payload: payload.to_vec(), height: 0. }
  99. }
  100. /// Encode the privmsg payload: `[nick][text][confirmed][channel]`.
  101. pub fn encode_privmsg_payload(nick: &str, text: &str, confirmed: bool, channel: &str) -> Vec<u8> {
  102. let mut payload = vec![];
  103. nick.encode(&mut payload).unwrap();
  104. text.encode(&mut payload).unwrap();
  105. confirmed.encode(&mut payload).unwrap();
  106. channel.encode(&mut payload).unwrap();
  107. payload
  108. }
  109. /// Decode the privmsg payload back into
  110. /// `(nick, text, confirmed, channel)`.
  111. ///
  112. /// ## Panics
  113. ///
  114. /// If the payload does not decode, identifying the entry.
  115. pub fn decode_privmsg_payload(
  116. payload: &[u8],
  117. ts: Timestamp,
  118. id: &MessageId,
  119. ) -> (String, String, bool, String) {
  120. let mut cur = Cursor::new(payload);
  121. let ctx = |what: &str| format!("corrupt chat entry: {what} [ts={ts} id={id}]");
  122. let nick =
  123. String::decode(&mut cur).unwrap_or_else(|e| panic!("{}: {e}", ctx("bad privmsg nick")));
  124. let text =
  125. String::decode(&mut cur).unwrap_or_else(|e| panic!("{}: {e}", ctx("bad privmsg text")));
  126. let confirmed = bool::decode(&mut cur)
  127. .unwrap_or_else(|e| panic!("{}: {e}", ctx("bad privmsg confirmed flag")));
  128. let channel =
  129. String::decode(&mut cur).unwrap_or_else(|e| panic!("{}: {e}", ctx("bad privmsg channel")));
  130. (nick, text, confirmed, channel)
  131. }
  132. /// Encode the datemsg payload: the separator day's local-midnight ts.
  133. pub fn encode_datemsg_payload(midnight: Timestamp) -> Vec<u8> {
  134. let mut payload = vec![];
  135. midnight.encode(&mut payload).unwrap();
  136. payload
  137. }
  138. /// Decode the datemsg payload back into its midnight timestamp.
  139. ///
  140. /// ## Panics
  141. ///
  142. /// If the payload does not decode, identifying the entry.
  143. pub fn decode_datemsg_payload(payload: &[u8], ts: Timestamp, id: &MessageId) -> Timestamp {
  144. let mut cur = Cursor::new(payload);
  145. Timestamp::decode(&mut cur).unwrap_or_else(|e| {
  146. panic!("corrupt chat entry: bad datemsg midnight [ts={ts} id={id}]: {e}")
  147. })
  148. }
  149. #[cfg(test)]
  150. mod tests {
  151. use super::*;
  152. fn rid(b: u8) -> MessageId {
  153. MessageId([b; 32])
  154. }
  155. #[test]
  156. fn key_round_trip() {
  157. let key = encode_key(1_756_000_000_000, &rid(7));
  158. assert_eq!(key.len(), KEY_LEN);
  159. assert_eq!(decode_key(&key), (1_756_000_000_000, rid(7)));
  160. // BE ordering: the ts leads.
  161. assert_eq!(&key[..8], &1_756_000_000_000u64.to_be_bytes());
  162. }
  163. #[test]
  164. fn privmsg_value_round_trip() {
  165. let id = rid(1);
  166. let payload = encode_privmsg_payload("alice", "hello world", false, "#dev");
  167. let val = encode_value(MsgType::PrivMsg, &payload);
  168. assert_eq!(val[0], MsgType::PrivMsg as u8);
  169. let rec = decode_value(&val, 123, &id);
  170. assert_eq!(rec.ts, 123);
  171. assert_eq!(rec.id, id);
  172. assert_eq!(rec.msg_type, MsgType::PrivMsg);
  173. assert_eq!(rec.height, 0.);
  174. assert_eq!(
  175. decode_privmsg_payload(&rec.payload, 123, &id),
  176. ("alice".to_string(), "hello world".to_string(), false, "#dev".to_string())
  177. );
  178. // A feed-tree entry reads back with its room.
  179. let payload = encode_privmsg_payload("bob", "hi", true, "#ramen");
  180. let val = encode_value(MsgType::PrivMsg, &payload);
  181. let rec = decode_value(&val, 124, &id);
  182. assert_eq!(
  183. decode_privmsg_payload(&rec.payload, 124, &id),
  184. ("bob".to_string(), "hi".to_string(), true, "#ramen".to_string())
  185. );
  186. let payload = encode_privmsg_payload("NOTICE", "\u{1}ACTION waves\u{1}", true, "#dev");
  187. let val = encode_value(MsgType::PrivMsg, &payload);
  188. let rec = decode_value(&val, 125, &id);
  189. assert_eq!(
  190. decode_privmsg_payload(&rec.payload, 125, &id),
  191. ("NOTICE".to_string(), "\u{1}ACTION waves\u{1}".to_string(), true, "#dev".to_string())
  192. );
  193. }
  194. #[test]
  195. fn datemsg_value_round_trip() {
  196. let payload = encode_datemsg_payload(1_755_936_000_000);
  197. let val = encode_value(MsgType::DateMsg, &payload);
  198. let rec = decode_value(&val, 1_755_936_000_000, &rid(0));
  199. assert_eq!(rec.msg_type, MsgType::DateMsg);
  200. assert_eq!(decode_datemsg_payload(&rec.payload, 0, &rid(0)), 1_755_936_000_000);
  201. }
  202. #[test]
  203. fn discriminants_are_the_wire_tags() {
  204. assert_eq!(MsgType::PrivMsg as u8, 0);
  205. assert_eq!(MsgType::FileMsg as u8, 1);
  206. assert_eq!(MsgType::DateMsg as u8, 2);
  207. assert_eq!(msg_type_from_u8(0), MsgType::PrivMsg);
  208. assert_eq!(msg_type_from_u8(1), MsgType::FileMsg);
  209. assert_eq!(msg_type_from_u8(2), MsgType::DateMsg);
  210. }
  211. #[test]
  212. #[should_panic(expected = "unknown msg type tag 9")]
  213. fn unknown_tag_panics() {
  214. decode_value(&[9, 0, 0], 1, &rid(1));
  215. }
  216. #[test]
  217. #[should_panic(expected = "empty value")]
  218. fn empty_value_panics() {
  219. decode_value(&[], 1, &rid(1));
  220. }
  221. #[test]
  222. #[should_panic(expected = "bad privmsg text")]
  223. fn truncated_privmsg_payload_panics() {
  224. let mut payload = encode_privmsg_payload("alice", "hello", true, "#dev");
  225. payload.truncate(6);
  226. decode_privmsg_payload(&payload, 5, &rid(2));
  227. }
  228. #[test]
  229. #[should_panic(expected = "bad privmsg channel")]
  230. fn missing_channel_panics() {
  231. // v2-era payload `[nick][text][confirmed]` — no channel bytes.
  232. let mut payload = encode_privmsg_payload("alice", "hello", true, "#dev");
  233. // Strip the trailing channel string (its varint length + bytes).
  234. let channel_len = "#dev".len() + 1;
  235. payload.truncate(payload.len() - channel_len);
  236. decode_privmsg_payload(&payload, 5, &rid(2));
  237. }
  238. #[test]
  239. #[should_panic(expected = "bad privmsg confirmed flag")]
  240. fn missing_confirmed_flag_panics() {
  241. let mut payload = encode_privmsg_payload("alice", "hello", true, "#dev");
  242. // Strip confirmed flag and channel.
  243. payload.truncate(payload.len() - ("#dev".len() + 2));
  244. decode_privmsg_payload(&payload, 5, &rid(2));
  245. }
  246. #[test]
  247. #[should_panic(expected = "unknown msg type tag")]
  248. fn legacy_untagged_value_panics() {
  249. // The old chatview stored `nick, text` with no tag byte; the
  250. // first byte (the nick's varint length) reads as a tag and must
  251. // fail loudly, never misread.
  252. let mut legacy = vec![];
  253. "alice".encode(&mut legacy).unwrap();
  254. "hello".encode(&mut legacy).unwrap();
  255. decode_value(&legacy, 1, &rid(1));
  256. }
  257. #[test]
  258. #[should_panic(expected = "filemsg is derived and never stored")]
  259. fn stored_filemsg_panics() {
  260. decode_value(&[1, 0, 0], 1, &rid(1));
  261. }
  262. #[test]
  263. #[should_panic(expected = "expected 40 bytes")]
  264. fn bad_key_length_panics() {
  265. decode_key(&[0u8; 12]);
  266. }
  267. }