oxedyne/fe2o3/fe2o3_o3db_sync/src/dist/codec.rs
18.7 KiB, 25 runs
created by r1870400018:67480, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The binary wire form of distributed Ozone's envelopes. |
| 2 | //! |
| 3 | //! An [`Envelope`] is the JDAT list `[version, from, to, body]`, written in the BDAT binary |
| 4 | //! encoding, and the body is `[kind, fields]`. The layouts are part of the protocol: a peer built |
| 5 | //! from another revision reads exactly the bytes this module writes, so |
| 6 | //! |
| 7 | //! - a message kind keeps its number for ever, and a retired kind leaves a gap; |
| 8 | //! - a change to any layout bumps [`WIRE_VERSION`], and a reader refuses a version it does not |
| 9 | //! know with a `Mismatch` rather than guess at its fields; |
| 10 | //! - a reader never trusts the bytes: [`Envelope::decode`] bounds their length, nesting and value count before |
| 11 | //! it decodes, names the field of every refusal, and accepts only a shape that |
| 12 | //! [`Envelope::encode`] can produce. |
| 13 | //! |
| 14 | //! The first form has never run in a cluster, so there is nothing older to stay compatible with. |
| 15 | //! |
| 16 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 17 | //! Anthropic Claude |
| 18 | |
| 19 | use super::hotstuff::types::{ |
| 20 | BlockHash, |
| 21 | NewView, |
| 22 | Phase, |
| 23 | Proposal, |
| 24 | Qc, |
| 25 | ReplicaId, |
| 26 | Vote, |
| 27 | }; |
| 28 | use super::record::{ |
| 29 | Record, |
| 30 | RecordId, |
| 31 | }; |
| 32 | use super::transport::{ |
| 33 | Envelope, |
| 34 | MsgKind, |
| 35 | }; |
| 36 | |
| 37 | use oxedyne_fe2o3_core::prelude::*; |
| 38 | use oxedyne_fe2o3_jdat::{ |
| 39 | bdat::DecodeLimits, |
| 40 | prelude::*, |
| 41 | }; |
| 42 | use crate::kademlia::id::NodeId; |
| 43 | |
| 44 | |
| 45 | pub const WIRE_VERSION: u8 = 1; // first field of every envelope |
| 46 | pub const MAX_ENVELOPE_BYTES: usize = 16 * 1024 * 1024; // longest envelope written or read |
| 47 | pub const MAX_ENVELOPE_DEPTH: usize = 12; // the deepest shape written is nine |
| 48 | // Most values a decoded envelope may hold. The densest legal shape is a record with no table and no |
| 49 | // value, 12 bytes a value, so a 16 MiB page of them is about 1.4 million values; a bound of one in |
| 50 | // 8 bytes leaves room above that and stops a buffer of one-byte values at 2 Mi `Dat`s, not 16 Mi. |
| 51 | pub const MAX_ENVELOPE_VALUES: usize = MAX_ENVELOPE_BYTES / 8; |
| 52 | |
| 53 | // Message kind numbers. A number is never reused, even after its kind is retired. |
| 54 | const KIND_REPLICATE_PUT: u8 = 1; |
| 55 | const KIND_GET_REQUEST: u8 = 2; |
| 56 | const KIND_GET_RESPONSE: u8 = 3; |
| 57 | const KIND_ANTI_ENTROPY_DIGEST: u8 = 4; |
| 58 | const KIND_ANTI_ENTROPY_REPLY: u8 = 5; |
| 59 | const KIND_ANTI_ENTROPY_PUSH: u8 = 6; |
| 60 | const KIND_COHORT_SUBMIT: u8 = 7; |
| 61 | const KIND_COHORT_PROPOSE: u8 = 8; |
| 62 | const KIND_COHORT_VOTE: u8 = 9; |
| 63 | const KIND_COHORT_NEW_VIEW: u8 = 10; |
| 64 | |
| 65 | // Phase numbers on the wire. |
| 66 | const PHASE_PREPARE: u8 = 0; |
| 67 | const PHASE_PRE_COMMIT: u8 = 1; |
| 68 | const PHASE_COMMIT: u8 = 2; |
| 69 | const PHASE_DECIDE: u8 = 3; |
| 70 | |
| 71 | |
| 72 | // The limits every received envelope is decoded under. |
| 73 | fn limits() -> DecodeLimits { |
| 74 | DecodeLimits::new(MAX_ENVELOPE_DEPTH, MAX_ENVELOPE_BYTES).with_max_items(MAX_ENVELOPE_VALUES) |
| 75 | } |
| 76 | |
| 77 | // A refusal of a decoded shape, naming the shape, the field and what was found. It holds the |
| 78 | // kind of the offending value and never the value, which a peer chooses and may make huge. |
| 79 | fn refuse(what: &str, field: &str, want: &str, found: &Dat) -> Error<ErrTag> { |
| 80 | err!( |
| 81 | "{} field '{}' expects {}, found {:?}.", |
| 82 | what, field, want, found.kind(); |
| 83 | Decode, Input, Mismatch) |
| 84 | } |
| 85 | |
| 86 | fn want_u8(dat: Dat, what: &str, field: &str) -> Outcome<u8> { |
| 87 | match dat { |
| 88 | Dat::U8(n) => Ok(n), |
| 89 | other => Err(refuse(what, field, "Dat::U8", &other)), |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn want_u16(dat: Dat, what: &str, field: &str) -> Outcome<u16> { |
| 94 | match dat { |
| 95 | Dat::U16(n) => Ok(n), |
| 96 | other => Err(refuse(what, field, "Dat::U16", &other)), |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn want_u64(dat: Dat, what: &str, field: &str) -> Outcome<u64> { |
| 101 | match dat { |
| 102 | Dat::U64(n) => Ok(n), |
| 103 | other => Err(refuse(what, field, "Dat::U64", &other)), |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fn want_bool(dat: Dat, what: &str, field: &str) -> Outcome<bool> { |
| 108 | match dat { |
| 109 | Dat::Bool(b) => Ok(b), |
| 110 | other => Err(refuse(what, field, "Dat::Bool", &other)), |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn want_str(dat: Dat, what: &str, field: &str) -> Outcome<String> { |
| 115 | match dat { |
| 116 | Dat::Str(s) => Ok(s), |
| 117 | other => Err(refuse(what, field, "Dat::Str", &other)), |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Value bytes are a `Dat::BU64`, never a `Dat::BU8`, whose length field holds 255 at most. |
| 122 | fn want_bytes(dat: Dat, what: &str, field: &str) -> Outcome<Vec<u8>> { |
| 123 | match dat { |
| 124 | Dat::BU64(v) => Ok(v), |
| 125 | other => Err(refuse(what, field, "Dat::BU64", &other)), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn want_b32(dat: Dat, what: &str, field: &str) -> Outcome<[u8; 32]> { |
| 130 | match dat { |
| 131 | Dat::B32(b) => Ok(b.0), |
| 132 | other => Err(refuse(what, field, "Dat::B32", &other)), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn want_list(dat: Dat, what: &str, field: &str) -> Outcome<Vec<Dat>> { |
| 137 | match dat { |
| 138 | Dat::List(v) => Ok(v), |
| 139 | other => Err(refuse(what, field, "Dat::List", &other)), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn want_opt(dat: Dat, what: &str, field: &str) -> Outcome<Option<Dat>> { |
| 144 | match dat { |
| 145 | Dat::Opt(b) => Ok(*b), |
| 146 | other => Err(refuse(what, field, "Dat::Opt", &other)), |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn opt_dat(o: Option<Dat>) -> Dat { |
| 151 | Dat::Opt(Box::new(o)) |
| 152 | } |
| 153 | |
| 154 | fn maybe_to_dat<T: ToDat>(item: &Option<T>) -> Outcome<Dat> { |
| 155 | match item { |
| 156 | Some(t) => Ok(opt_dat(Some(res!(t.to_dat())))), |
| 157 | None => Ok(opt_dat(None)), |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn list_to_dat<T: ToDat>(items: &[T]) -> Outcome<Dat> { |
| 162 | let mut v = Vec::with_capacity(items.len()); |
| 163 | for item in items { |
| 164 | v.push(res!(item.to_dat())); |
| 165 | } |
| 166 | Ok(Dat::List(v)) |
| 167 | } |
| 168 | |
| 169 | |
| 170 | // A cursor over the fields of a decoded list. `open` checks the count once, so a shape with a |
| 171 | // field too few or too many is refused before any field is read. |
| 172 | struct Fields { |
| 173 | what: &'static str, |
| 174 | items: std::vec::IntoIter<Dat>, |
| 175 | } |
| 176 | |
| 177 | impl Fields { |
| 178 | |
| 179 | fn open(dat: Dat, what: &'static str, len: usize) -> Outcome<Self> { |
| 180 | match dat { |
| 181 | Dat::List(v) if v.len() == len => Ok(Self { what, items: v.into_iter() }), |
| 182 | Dat::List(v) => Err(err!( |
| 183 | "{} expects a {}-element Dat::List, found {} elements.", |
| 184 | what, len, v.len(); |
| 185 | Decode, Input, Mismatch)), |
| 186 | other => Err(err!( |
| 187 | "{} expects a {}-element Dat::List, found {:?}.", |
| 188 | what, len, other.kind(); |
| 189 | Decode, Input, Mismatch)), |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | fn next(&mut self, field: &str) -> Outcome<Dat> { |
| 194 | match self.items.next() { |
| 195 | Some(dat) => Ok(dat), |
| 196 | None => Err(err!( |
| 197 | "{} has no field '{}'.", self.what, field; |
| 198 | Decode, Input, Missing)), |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | fn u8(&mut self, field: &str) -> Outcome<u8> { |
| 203 | let dat = res!(self.next(field)); |
| 204 | want_u8(dat, self.what, field) |
| 205 | } |
| 206 | |
| 207 | fn u16(&mut self, field: &str) -> Outcome<u16> { |
| 208 | let dat = res!(self.next(field)); |
| 209 | want_u16(dat, self.what, field) |
| 210 | } |
| 211 | |
| 212 | fn u64(&mut self, field: &str) -> Outcome<u64> { |
| 213 | let dat = res!(self.next(field)); |
| 214 | want_u64(dat, self.what, field) |
| 215 | } |
| 216 | |
| 217 | fn bool(&mut self, field: &str) -> Outcome<bool> { |
| 218 | let dat = res!(self.next(field)); |
| 219 | want_bool(dat, self.what, field) |
| 220 | } |
| 221 | |
| 222 | fn string(&mut self, field: &str) -> Outcome<String> { |
| 223 | let dat = res!(self.next(field)); |
| 224 | want_str(dat, self.what, field) |
| 225 | } |
| 226 | |
| 227 | fn bytes(&mut self, field: &str) -> Outcome<Vec<u8>> { |
| 228 | let dat = res!(self.next(field)); |
| 229 | want_bytes(dat, self.what, field) |
| 230 | } |
| 231 | |
| 232 | fn b32(&mut self, field: &str) -> Outcome<[u8; 32]> { |
| 233 | let dat = res!(self.next(field)); |
| 234 | want_b32(dat, self.what, field) |
| 235 | } |
| 236 | |
| 237 | fn list(&mut self, field: &str) -> Outcome<Vec<Dat>> { |
| 238 | let dat = res!(self.next(field)); |
| 239 | want_list(dat, self.what, field) |
| 240 | } |
| 241 | |
| 242 | fn opt(&mut self, field: &str) -> Outcome<Option<Dat>> { |
| 243 | let dat = res!(self.next(field)); |
| 244 | want_opt(dat, self.what, field) |
| 245 | } |
| 246 | |
| 247 | fn id(&mut self, field: &str) -> Outcome<RecordId> { |
| 248 | Ok(RecordId::from_bytes(res!(self.b32(field)))) |
| 249 | } |
| 250 | |
| 251 | fn phase(&mut self, field: &str) -> Outcome<Phase> { |
| 252 | let dat = res!(self.next(field)); |
| 253 | Phase::from_dat(dat) |
| 254 | } |
| 255 | |
| 256 | // A list whose items are each a `T`. |
| 257 | fn items<T: FromDat>(&mut self, field: &str) -> Outcome<Vec<T>> { |
| 258 | let dats = res!(self.list(field)); |
| 259 | let mut v = Vec::with_capacity(dats.len()); |
| 260 | for dat in dats { |
| 261 | v.push(res!(T::from_dat(dat))); |
| 262 | } |
| 263 | Ok(v) |
| 264 | } |
| 265 | |
| 266 | // An optional `T`. |
| 267 | fn maybe<T: FromDat>(&mut self, field: &str) -> Outcome<Option<T>> { |
| 268 | match res!(self.opt(field)) { |
| 269 | Some(dat) => Ok(Some(res!(T::from_dat(dat)))), |
| 270 | None => Ok(None), |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | impl ToDat for RecordId { |
| 277 | fn to_dat(&self) -> Outcome<Dat> { |
| 278 | Ok(Dat::B32(B32(self.0))) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | impl FromDat for RecordId { |
| 283 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 284 | Ok(Self::from_bytes(res!(want_b32(dat, "RecordId", "id")))) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | impl ToDat for NodeId { |
| 289 | fn to_dat(&self) -> Outcome<Dat> { |
| 290 | Ok(Dat::B32(B32(self.0))) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | impl FromDat for NodeId { |
| 295 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 296 | Ok(Self::from_bytes(res!(want_b32(dat, "NodeId", "id")))) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | impl ToDat for Record { |
| 301 | fn to_dat(&self) -> Outcome<Dat> { |
| 302 | Ok(Dat::List(vec![ |
| 303 | res!(self.id.to_dat()), |
| 304 | Dat::Str(self.table.clone()), |
| 305 | Dat::BU64(self.value.clone()), |
| 306 | ])) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | impl FromDat for Record { |
| 311 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 312 | let mut f = res!(Fields::open(dat, "Record", 3)); |
| 313 | Ok(Self { |
| 314 | id: res!(f.id("id")), |
| 315 | table: res!(f.string("table")), |
| 316 | value: res!(f.bytes("value")), |
| 317 | }) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl ToDat for Phase { |
| 322 | fn to_dat(&self) -> Outcome<Dat> { |
| 323 | Ok(Dat::U8(match self { |
| 324 | Self::Prepare => PHASE_PREPARE, |
| 325 | Self::PreCommit => PHASE_PRE_COMMIT, |
| 326 | Self::Commit => PHASE_COMMIT, |
| 327 | Self::Decide => PHASE_DECIDE, |
| 328 | })) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl FromDat for Phase { |
| 333 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 334 | match res!(want_u8(dat, "Phase", "phase")) { |
| 335 | PHASE_PREPARE => Ok(Self::Prepare), |
| 336 | PHASE_PRE_COMMIT => Ok(Self::PreCommit), |
| 337 | PHASE_COMMIT => Ok(Self::Commit), |
| 338 | PHASE_DECIDE => Ok(Self::Decide), |
| 339 | other => Err(err!( |
| 340 | "Phase number {} names no phase, which are {} to {}.", |
| 341 | other, PHASE_PREPARE, PHASE_DECIDE; |
| 342 | Decode, Input, Mismatch)), |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | impl ToDat for Qc { |
| 348 | fn to_dat(&self) -> Outcome<Dat> { |
| 349 | let mut sigs = Vec::with_capacity(self.signatures.len()); |
| 350 | for (voter, sig) in &self.signatures { |
| 351 | sigs.push(Dat::List(vec![ |
| 352 | Dat::U16(*voter), |
| 353 | Dat::BU64(sig.clone()), |
| 354 | ])); |
| 355 | } |
| 356 | Ok(Dat::List(vec![ |
| 357 | Dat::U64(self.view), |
| 358 | res!(self.phase.to_dat()), |
| 359 | Dat::B32(B32(self.block_hash)), |
| 360 | Dat::List(sigs), |
| 361 | ])) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | impl FromDat for Qc { |
| 366 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 367 | let mut f = res!(Fields::open(dat, "Qc", 4)); |
| 368 | let view = res!(f.u64("view")); |
| 369 | let phase = res!(f.phase("phase")); |
| 370 | let block_hash = res!(f.b32("block_hash")); |
| 371 | let mut signatures = Vec::new(); |
| 372 | for dat in res!(f.list("signatures")) { |
| 373 | let mut s = res!(Fields::open(dat, "Qc signature", 2)); |
| 374 | let voter: ReplicaId = res!(s.u16("voter")); |
| 375 | signatures.push((voter, res!(s.bytes("signature")))); |
| 376 | } |
| 377 | Ok(Self { view, phase, block_hash, signatures }) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | impl ToDat for Proposal { |
| 382 | fn to_dat(&self) -> Outcome<Dat> { |
| 383 | let block = match &self.block { |
| 384 | Some(b) => Some(Dat::BU64(b.clone())), |
| 385 | None => None, |
| 386 | }; |
| 387 | let justify = match &self.justify { |
| 388 | Some(qc) => Some(res!(qc.to_dat())), |
| 389 | None => None, |
| 390 | }; |
| 391 | Ok(Dat::List(vec![ |
| 392 | Dat::U64(self.view), |
| 393 | res!(self.phase.to_dat()), |
| 394 | Dat::B32(B32(self.block_hash)), |
| 395 | opt_dat(block), |
| 396 | opt_dat(justify), |
| 397 | ])) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | impl FromDat for Proposal { |
| 402 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 403 | let mut f = res!(Fields::open(dat, "Proposal", 5)); |
| 404 | let view = res!(f.u64("view")); |
| 405 | let phase = res!(f.phase("phase")); |
| 406 | let block_hash: BlockHash = res!(f.b32("block_hash")); |
| 407 | let block = match res!(f.opt("block")) { |
| 408 | Some(dat) => Some(res!(want_bytes(dat, "Proposal", "block"))), |
| 409 | None => None, |
| 410 | }; |
| 411 | let justify = res!(f.maybe::<Qc>("justify")); |
| 412 | Ok(Self { view, phase, block_hash, block, justify }) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | impl ToDat for Vote { |
| 417 | fn to_dat(&self) -> Outcome<Dat> { |
| 418 | Ok(Dat::List(vec![ |
| 419 | Dat::U64(self.view), |
| 420 | res!(self.phase.to_dat()), |
| 421 | Dat::B32(B32(self.block_hash)), |
| 422 | Dat::U16(self.voter), |
| 423 | Dat::BU64(self.signature.clone()), |
| 424 | ])) |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | impl FromDat for Vote { |
| 429 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 430 | let mut f = res!(Fields::open(dat, "Vote", 5)); |
| 431 | Ok(Self { |
| 432 | view: res!(f.u64("view")), |
| 433 | phase: res!(f.phase("phase")), |
| 434 | block_hash: res!(f.b32("block_hash")), |
| 435 | voter: res!(f.u16("voter")), |
| 436 | signature: res!(f.bytes("signature")), |
| 437 | }) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | impl ToDat for NewView { |
| 442 | fn to_dat(&self) -> Outcome<Dat> { |
| 443 | let prepare_qc = match &self.prepare_qc { |
| 444 | Some(qc) => Some(res!(qc.to_dat())), |
| 445 | None => None, |
| 446 | }; |
| 447 | Ok(Dat::List(vec![ |
| 448 | Dat::U64(self.view), |
| 449 | Dat::U16(self.sender), |
| 450 | opt_dat(prepare_qc), |
| 451 | ])) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | impl FromDat for NewView { |
| 456 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 457 | let mut f = res!(Fields::open(dat, "NewView", 3)); |
| 458 | Ok(Self { |
| 459 | view: res!(f.u64("view")), |
| 460 | sender: res!(f.u16("sender")), |
| 461 | prepare_qc: res!(f.maybe::<Qc>("prepare_qc")), |
| 462 | }) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | impl ToDat for MsgKind { |
| 467 | fn to_dat(&self) -> Outcome<Dat> { |
| 468 | let (kind, body) = match self { |
| 469 | Self::ReplicatePut { record } => ( |
| 470 | KIND_REPLICATE_PUT, |
| 471 | res!(record.to_dat()), |
| 472 | ), |
| 473 | Self::GetRequest { request_id, table, id } => ( |
| 474 | KIND_GET_REQUEST, |
| 475 | Dat::List(vec![ |
| 476 | Dat::U64(*request_id), |
| 477 | Dat::Str(table.clone()), |
| 478 | res!(id.to_dat()), |
| 479 | ]), |
| 480 | ), |
| 481 | Self::GetResponse { request_id, record } => ( |
| 482 | KIND_GET_RESPONSE, |
| 483 | Dat::List(vec![ |
| 484 | Dat::U64(*request_id), |
| 485 | opt_dat(match record { |
| 486 | Some(r) => Some(res!(r.to_dat())), |
| 487 | None => None, |
| 488 | }), |
| 489 | ]), |
| 490 | ), |
| 491 | Self::AntiEntropyDigest { table, sketch, after } => ( |
| 492 | KIND_ANTI_ENTROPY_DIGEST, |
| 493 | Dat::List(vec![ |
| 494 | Dat::Str(table.clone()), |
| 495 | Dat::BU64(sketch.clone()), |
| 496 | res!(maybe_to_dat(after)), |
| 497 | ]), |
| 498 | ), |
| 499 | Self::AntiEntropyReply { table, records, requested_ids, bulk, after, next } => ( |
| 500 | KIND_ANTI_ENTROPY_REPLY, |
| 501 | Dat::List(vec![ |
| 502 | Dat::Str(table.clone()), |
| 503 | res!(list_to_dat(records)), |
| 504 | res!(list_to_dat(requested_ids)), |
| 505 | Dat::Bool(*bulk), |
| 506 | res!(maybe_to_dat(after)), |
| 507 | res!(maybe_to_dat(next)), |
| 508 | ]), |
| 509 | ), |
| 510 | Self::AntiEntropyPush { table, records } => ( |
| 511 | KIND_ANTI_ENTROPY_PUSH, |
| 512 | Dat::List(vec![ |
| 513 | Dat::Str(table.clone()), |
| 514 | res!(list_to_dat(records)), |
| 515 | ]), |
| 516 | ), |
| 517 | Self::CohortSubmit { record } => ( |
| 518 | KIND_COHORT_SUBMIT, |
| 519 | res!(record.to_dat()), |
| 520 | ), |
| 521 | Self::CohortPropose { table, id, proposal } => ( |
| 522 | KIND_COHORT_PROPOSE, |
| 523 | Dat::List(vec![ |
| 524 | Dat::Str(table.clone()), |
| 525 | res!(id.to_dat()), |
| 526 | res!(proposal.to_dat()), |
| 527 | ]), |
| 528 | ), |
| 529 | Self::CohortVote { table, id, vote } => ( |
| 530 | KIND_COHORT_VOTE, |
| 531 | Dat::List(vec![ |
| 532 | Dat::Str(table.clone()), |
| 533 | res!(id.to_dat()), |
| 534 | res!(vote.to_dat()), |
| 535 | ]), |
| 536 | ), |
| 537 | Self::CohortNewView { table, id, new_view } => ( |
| 538 | KIND_COHORT_NEW_VIEW, |
| 539 | Dat::List(vec![ |
| 540 | Dat::Str(table.clone()), |
| 541 | res!(id.to_dat()), |
| 542 | res!(new_view.to_dat()), |
| 543 | ]), |
| 544 | ), |
| 545 | }; |
| 546 | Ok(Dat::List(vec![Dat::U8(kind), body])) |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | impl FromDat for MsgKind { |
| 551 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 552 | let mut f = res!(Fields::open(dat, "MsgKind", 2)); |
| 553 | let kind = res!(f.u8("kind")); |
| 554 | let body = res!(f.next("body")); |
| 555 | match kind { |
| 556 | KIND_REPLICATE_PUT => Ok(Self::ReplicatePut { |
| 557 | record: res!(Record::from_dat(body)), |
| 558 | }), |
| 559 | KIND_GET_REQUEST => { |
| 560 | let mut b = res!(Fields::open(body, "GetRequest", 3)); |
| 561 | Ok(Self::GetRequest { |
| 562 | request_id: res!(b.u64("request_id")), |
| 563 | table: res!(b.string("table")), |
| 564 | id: res!(b.id("id")), |
| 565 | }) |
| 566 | }, |
| 567 | KIND_GET_RESPONSE => { |
| 568 | let mut b = res!(Fields::open(body, "GetResponse", 2)); |
| 569 | Ok(Self::GetResponse { |
| 570 | request_id: res!(b.u64("request_id")), |
| 571 | record: res!(b.maybe::<Record>("record")), |
| 572 | }) |
| 573 | }, |
| 574 | KIND_ANTI_ENTROPY_DIGEST => { |
| 575 | let mut b = res!(Fields::open(body, "AntiEntropyDigest", 3)); |
| 576 | Ok(Self::AntiEntropyDigest { |
| 577 | table: res!(b.string("table")), |
| 578 | sketch: res!(b.bytes("sketch")), |
| 579 | after: res!(b.maybe::<RecordId>("after")), |
| 580 | }) |
| 581 | }, |
| 582 | KIND_ANTI_ENTROPY_REPLY => { |
| 583 | let mut b = res!(Fields::open(body, "AntiEntropyReply", 6)); |
| 584 | Ok(Self::AntiEntropyReply { |
| 585 | table: res!(b.string("table")), |
| 586 | records: res!(b.items::<Record>("records")), |
| 587 | requested_ids: res!(b.items::<RecordId>("requested_ids")), |
| 588 | bulk: res!(b.bool("bulk")), |
| 589 | after: res!(b.maybe::<RecordId>("after")), |
| 590 | next: res!(b.maybe::<RecordId>("next")), |
| 591 | }) |
| 592 | }, |
| 593 | KIND_ANTI_ENTROPY_PUSH => { |
| 594 | let mut b = res!(Fields::open(body, "AntiEntropyPush", 2)); |
| 595 | Ok(Self::AntiEntropyPush { |
| 596 | table: res!(b.string("table")), |
| 597 | records: res!(b.items::<Record>("records")), |
| 598 | }) |
| 599 | }, |
| 600 | KIND_COHORT_SUBMIT => Ok(Self::CohortSubmit { |
| 601 | record: res!(Record::from_dat(body)), |
| 602 | }), |
| 603 | KIND_COHORT_PROPOSE => { |
| 604 | let mut b = res!(Fields::open(body, "CohortPropose", 3)); |
| 605 | let table = res!(b.string("table")); |
| 606 | let id = res!(b.id("id")); |
| 607 | let dat = res!(b.next("proposal")); |
| 608 | Ok(Self::CohortPropose { table, id, proposal: res!(Proposal::from_dat(dat)) }) |
| 609 | }, |
| 610 | KIND_COHORT_VOTE => { |
| 611 | let mut b = res!(Fields::open(body, "CohortVote", 3)); |
| 612 | let table = res!(b.string("table")); |
| 613 | let id = res!(b.id("id")); |
| 614 | let dat = res!(b.next("vote")); |
| 615 | Ok(Self::CohortVote { table, id, vote: res!(Vote::from_dat(dat)) }) |
| 616 | }, |
| 617 | KIND_COHORT_NEW_VIEW => { |
| 618 | let mut b = res!(Fields::open(body, "CohortNewView", 3)); |
| 619 | let table = res!(b.string("table")); |
| 620 | let id = res!(b.id("id")); |
| 621 | let dat = res!(b.next("new_view")); |
| 622 | Ok(Self::CohortNewView { table, id, new_view: res!(NewView::from_dat(dat)) }) |
| 623 | }, |
| 624 | other => Err(err!( |
| 625 | "MsgKind number {} names no message kind that wire version {} knows, \ |
| 626 | which are {} to {}.", |
| 627 | other, WIRE_VERSION, KIND_REPLICATE_PUT, KIND_COHORT_NEW_VIEW; |
| 628 | Decode, Input, Mismatch)), |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | impl ToDat for Envelope { |
| 634 | fn to_dat(&self) -> Outcome<Dat> { |
| 635 | Ok(Dat::List(vec![ |
| 636 | Dat::U8(WIRE_VERSION), |
| 637 | res!(self.from.to_dat()), |
| 638 | res!(self.to.to_dat()), |
| 639 | res!(self.body.to_dat()), |
| 640 | ])) |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | impl FromDat for Envelope { |
| 645 | fn from_dat(dat: Dat) -> Outcome<Self> { |
| 646 | let mut f = res!(Fields::open(dat, "Envelope", 4)); |
| 647 | let version = res!(f.u8("version")); |
| 648 | if version != WIRE_VERSION { |
| 649 | return Err(err!( |
| 650 | "Envelope wire version {} is not the version {} this build reads.", |
| 651 | version, WIRE_VERSION; |
| 652 | Decode, Input, Mismatch)); |
| 653 | } |
| 654 | let from = NodeId::from_bytes(res!(f.b32("from"))); |
| 655 | let to = NodeId::from_bytes(res!(f.b32("to"))); |
| 656 | let body = res!(f.next("body")); |
| 657 | Ok(Self { from, to, body: res!(MsgKind::from_dat(body)) }) |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | impl Envelope { |
| 662 | |
| 663 | /// Writes the envelope, refusing one longer than [`MAX_ENVELOPE_BYTES`], which no peer would |
| 664 | /// accept. |
| 665 | pub fn encode(&self) -> Outcome<Vec<u8>> { |
| 666 | let dat = res!(self.to_dat()); |
| 667 | let bytes = res!(dat.to_bytes(Vec::new())); |
| 668 | if bytes.len() > MAX_ENVELOPE_BYTES { |
| 669 | return Err(err!( |
| 670 | "A {} envelope of {} bytes exceeds the maximum of {} bytes.", |
| 671 | self.body.label(), bytes.len(), MAX_ENVELOPE_BYTES; |
| 672 | Encode, Excessive, Size)); |
| 673 | } |
| 674 | Ok(bytes) |
| 675 | } |
| 676 | |
| 677 | /// Reads an envelope from bytes a peer sent. The length and nesting are bounded before |
| 678 | /// anything is decoded, and bytes after the envelope are refused. |
| 679 | pub fn decode(buf: &[u8]) -> Outcome<Self> { |
| 680 | let (dat, used) = match Dat::from_bytes_limited(buf, &limits()) { |
| 681 | Ok(pair) => pair, |
| 682 | Err(e) => return Err(err!( |
| 683 | e, "An envelope of {} bytes did not decode.", buf.len(); |
| 684 | Decode, Input, Mismatch)), |
| 685 | }; |
| 686 | if used != buf.len() { |
| 687 | return Err(err!( |
| 688 | "An envelope of {} bytes is followed by {} unread bytes.", |
| 689 | used, buf.len() - used; |
| 690 | Decode, Input, Mismatch)); |
| 691 | } |
| 692 | Self::from_dat(dat) |
| 693 | } |
| 694 | } |