From 3bdb902d6593b6531e0386d84ce071f2fb815950 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 21:49:06 -0800 Subject: [PATCH 01/11] chore(quest): claim quest/m2/multicast/switch.md Quest-Claim: ed8f90ca9728e60f From f7748987b46b8d74631da06449835b5bcbb489b6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 23:11:47 -0800 Subject: [PATCH 02/11] feat(moq-net)!: lite-07 sessions run the new route rule Work in progress: Rust sessions drive route::relay::Router; the old rule's router, proof, and tests are deleted. Co-Authored-By: Claude Fable 5.2 --- rs/moq-net/fixtures/lite.json | 184 +- rs/moq-net/src/client.rs | 4 - rs/moq-net/src/coding/writer.rs | 3 +- rs/moq-net/src/fuzz.rs | 811 +--- rs/moq-net/src/lite/announce.rs | 2154 +-------- rs/moq-net/src/lite/compress.rs | 6 +- rs/moq-net/src/lite/corpus.rs | 13 +- rs/moq-net/src/lite/datagram.rs | 20 +- rs/moq-net/src/lite/fetch.rs | 22 +- rs/moq-net/src/lite/instance.rs | 44 +- rs/moq-net/src/lite/mod.rs | 1 - rs/moq-net/src/lite/publisher.rs | 403 +- rs/moq-net/src/lite/publisher/routed.rs | 1151 ++--- rs/moq-net/src/lite/rekey.rs | 436 -- rs/moq-net/src/lite/session.rs | 37 +- rs/moq-net/src/lite/setup.rs | 218 +- rs/moq-net/src/lite/subscribe.rs | 22 +- rs/moq-net/src/lite/subscriber.rs | 788 +--- .../src/lite/subscriber/route_stream.rs | 117 +- rs/moq-net/src/lite/subscriber/routed.rs | 1512 ++----- .../src/lite/subscriber/routed/tests.rs | 1106 ++--- rs/moq-net/src/lite/track.rs | 42 +- rs/moq-net/src/lite/tracks.rs | 22 +- rs/moq-net/src/model/mod.rs | 1 - rs/moq-net/src/model/origin.rs | 369 +- rs/moq-net/src/path/mod.rs | 3 +- rs/moq-net/src/route/mod.rs | 121 +- rs/moq-net/src/route/node.rs | 2088 +++------ rs/moq-net/src/route/node/tests.rs | 1193 ++--- rs/moq-net/src/route/path.rs | 3892 ----------------- rs/moq-net/src/route/proof.rs | 188 - rs/moq-net/src/route/relay.rs | 269 +- rs/moq-net/src/route/tests/bakeoff.rs | 49 +- rs/moq-net/src/route/tests/cache.rs | 1640 ------- rs/moq-net/src/route/tests/compare.rs | 190 +- rs/moq-net/src/route/tests/cost.rs | 368 -- rs/moq-net/src/route/tests/flap.rs | 238 - rs/moq-net/src/route/tests/forward.rs | 509 --- rs/moq-net/src/route/tests/handoff.rs | 1705 -------- rs/moq-net/src/route/tests/interest.rs | 527 --- rs/moq-net/src/route/tests/mod.rs | 1071 +---- rs/moq-net/src/route/tests/residual.rs | 246 -- rs/moq-net/src/route/tests/routing.rs | 841 ---- rs/moq-net/src/route/tests/scope.rs | 1851 -------- rs/moq-net/src/route/tests/search.rs | 771 ---- rs/moq-net/src/route/tests/warm.rs | 11 +- rs/moq-net/src/server.rs | 39 +- rs/moq-net/src/test_interop.rs | 3 +- .../tests/integration/lazy_discovery.rs | 2 +- 49 files changed, 3438 insertions(+), 23863 deletions(-) delete mode 100644 rs/moq-net/src/lite/rekey.rs delete mode 100644 rs/moq-net/src/route/path.rs delete mode 100644 rs/moq-net/src/route/proof.rs delete mode 100644 rs/moq-net/src/route/tests/cache.rs delete mode 100644 rs/moq-net/src/route/tests/cost.rs delete mode 100644 rs/moq-net/src/route/tests/flap.rs delete mode 100644 rs/moq-net/src/route/tests/forward.rs delete mode 100644 rs/moq-net/src/route/tests/handoff.rs delete mode 100644 rs/moq-net/src/route/tests/interest.rs delete mode 100644 rs/moq-net/src/route/tests/residual.rs delete mode 100644 rs/moq-net/src/route/tests/routing.rs delete mode 100644 rs/moq-net/src/route/tests/scope.rs delete mode 100644 rs/moq-net/src/route/tests/search.rs diff --git a/rs/moq-net/fixtures/lite.json b/rs/moq-net/fixtures/lite.json index 43e5fec6977..0abbc214bce 100644 --- a/rs/moq-net/fixtures/lite.json +++ b/rs/moq-net/fixtures/lite.json @@ -42,248 +42,220 @@ "expect": "refused" }, { - "name": "a Route Offer counting more hops than its body holds is malformed, though a count past the hop limit is otherwise refused as content", + "name": "a ROUTE carries a path of hops, each a Node ID, its Start, and a session, then flags, priority, and cost", "version": "moq-lite-07-wip", "message": "announce", - "hex": "064b0700000083e80100020003000400050006000700080009000a000b000c000d000e000f0010001100120013001400150016001700180019001a001b001c001d001e001f0020002100008001", - "expect": "refused" - }, - { - "name": "a ROUTE_START names its publisher, Prefix ID, suffix, whether the route is exact, and offer: a path of node and session hops, then flags, priority, and metric", - "version": "moq-lite-07-wip", - "message": "announce", - "hex": "060d92340000000207000903018005", + "hex": "070a02070100090203018005", "expect": "decoded" }, { - "name": "a ROUTE_START's suffix extends the requested prefix", + "name": "a ROUTE's 0x2 flag marks a path through a draining node", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060c92340001610001090000ff00", + "hex": "070701090100027f03", "expect": "decoded" }, { - "name": "a ROUTE_START's Exact marks a route its sender keeps at its own prefix", + "name": "a ROUTE flag no version defines is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060d92340000010207000903018005", - "expect": "decoded" + "hex": "070701090100088003", + "expect": "refused" }, { - "name": "a ROUTE_START's Exact marks a route below the requested prefix too", + "name": "a ROUTE with more than 32 hops is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060c92340001610101090000ff00", - "expect": "decoded" + "hex": "0767210101000201000301000401000501000601000701000801000901000a01000b01000c01000d01000e01000f01001001001101001201001301001401001501001601001701001801001901001a01001b01001c01001d01001e01001f0100200100210100008001", + "expect": "refused" }, { - "name": "a ROUTE_START's Exact other than 0 or 1 is malformed", + "name": "a ROUTE with no hops is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060d92340000020207000903018005", + "hex": "070400008001", "expect": "refused" }, { - "name": "a Route Offer's 0x2 flag marks a path through a draining node", + "name": "a ROUTE missing a hop it counts is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060b9234000000010900027f03", - "expect": "decoded" + "hex": "070703070100008003", + "expect": "refused" }, { - "name": "a Route Offer flag no version defines is withdrawn, with whatever field it adds", + "name": "a ROUTE hop naming Node ID 0 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060c92340000000109000880032a", - "expect": "withdrawn" + "hex": "070701000100008001", + "expect": "refused" }, { - "name": "a Route Offer with more than 32 hops is withdrawn", + "name": "a ROUTE_END references its Route ID", "version": "moq-lite-07-wip", "message": "announce", - "hex": "064b9234000000210100020003000400050006000700080009000a000b000c000d000e000f0010001100120013001400150016001700180019001a001b001c001d001e001f0020002100008001", - "expect": "withdrawn" + "hex": "080101", + "expect": "decoded" }, { - "name": "a Route Offer with no hops is refused", + "name": "a DEAD names the hop whose session closed and the node at its far end", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0609923400000000008001", - "expect": "refused" + "hex": "09050701020901", + "expect": "decoded" }, { - "name": "a Route Offer missing a hop it counts is refused", + "name": "a DEAD naming Far Node ID 0 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060b9234000000030700008003", + "hex": "09050701020001", "expect": "refused" }, { - "name": "a ROUTE_START naming Node ID 0 is withdrawn", + "name": "a DRAIN names the node that sent GOAWAY", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060a00000000010700008001", - "expect": "withdrawn" + "hex": "0a020701", + "expect": "decoded" }, { - "name": "a Route Offer hop naming Node ID 0 is withdrawn", + "name": "a DRAIN naming Node ID 0 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060b9234000000010000008001", - "expect": "withdrawn" + "hex": "0a020001", + "expect": "refused" }, { - "name": "a ROUTE_START suffix no path spells is withdrawn", + "name": "an ANNOUNCE_REQUEST continues its edit, removes Prefix IDs, and adds prefixes, each with its Hidden", "version": "moq-lite-07-wip", "message": "announce", - "hex": "060e923400032f2f7800010700008001", - "expect": "withdrawn" + "hex": "050d010200070204726f6f6d010000", + "expect": "decoded" }, { - "name": "a ROUTE_UPDATE references its Route ID", + "name": "an ANNOUNCE_REQUEST ending its edit may be empty", "version": "moq-lite-07-wip", "message": "announce", - "hex": "070701010500008003", + "hex": "0503000000", "expect": "decoded" }, { - "name": "a ROUTE_UPDATE flag no version defines is withdrawn", + "name": "an ANNOUNCE_REQUEST whose More is neither 0 nor 1 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0708010105008080032a", - "expect": "withdrawn" + "hex": "0503020000", + "expect": "refused" }, { - "name": "a ROUTE_HOLD references its Route ID", + "name": "an ANNOUNCE_REQUEST whose Hidden is neither 0 nor 1 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0b0101", - "expect": "decoded" + "hex": "05050000010002", + "expect": "refused" }, { - "name": "a ROUTE_END references its Route ID", + "name": "an ANNOUNCE_OK is empty", "version": "moq-lite-07-wip", "message": "announce", - "hex": "080101", + "hex": "0600", "expect": "decoded" }, { - "name": "a DEAD CLOSED names the hop, the far node, Gone, and how many ROUTE_UPDATEs follow", + "name": "a SYNCED names its Prefix ID", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0f06000702090102", + "hex": "040103", "expect": "decoded" }, { - "name": "a DEAD DRAIN names the node that sent GOAWAY", + "name": "an ANNOUNCE_FLOOR names its Route ID and the Revision its batch is complete as of", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0f020107", + "hex": "03020105", "expect": "decoded" }, { - "name": "a DEAD of a kind no version defines is refused", + "name": "a lite-07 ANNOUNCE_START names its instance, epoch, cost, how far above it was claimed, flags, priority, and the relays it crossed", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0f020207", - "expect": "refused" + "hex": "000f000001780161000103000080020709", + "expect": "decoded" }, { - "name": "a DEAD whose Gone is neither 0 nor 1 is refused", + "name": "a claim's 0x1 flag says no route reaches it, and its 0x2 flag that a node on the way is draining", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0f06000702090200", - "expect": "refused" + "hex": "000d00000178016100010300038000", + "expect": "decoded" }, { - "name": "a DEAD naming Far Node ID 0 is refused", + "name": "a claim flag no version defines is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "0f06000702000000", + "hex": "000d00000178016100010300048000", "expect": "refused" }, { - "name": "an ANNOUNCE_REQUEST continues its edit, removes Prefix IDs, and adds prefixes, each with its Hidden", + "name": "a claim whose chain names Node ID 0 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "100d010200070204726f6f6d010000", - "expect": "decoded" - }, - { - "name": "an ANNOUNCE_REQUEST ending its edit may be empty", - "version": "moq-lite-07-wip", - "message": "announce", - "hex": "1003000000", - "expect": "decoded" + "hex": "000e0000017801610001030000800100", + "expect": "refused" }, { - "name": "an ANNOUNCE_REQUEST whose More is neither 0 nor 1 is refused", + "name": "a claim whose chain names more than 32 relays is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "1003020000", + "hex": "002e00000178016100010300008021070707070707070707070707070707070707070707070707070707070707070707", "expect": "refused" }, { - "name": "an ANNOUNCE_REQUEST whose Hidden is neither 0 nor 1 is refused", + "name": "a lite-07 ANNOUNCE_START with an invalid path is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "10050000010002", + "hex": "000f0000032f2f78016100010300008000", "expect": "refused" }, { - "name": "an ANNOUNCE_OK is empty", + "name": "a lite-07 ANNOUNCE_START with an invalid instance is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "1100", - "expect": "decoded" + "hex": "000c000001780000010300008000", + "expect": "refused" }, { - "name": "a SYNCED names its Prefix ID", + "name": "a lite-07 ANNOUNCE_START cut short is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "040103", - "expect": "decoded" + "hex": "0003000003", + "expect": "refused" }, { - "name": "an ANNOUNCE_FLOOR carries its Revision plus one", + "name": "a lite-07 ANNOUNCE_UPDATE references its announce id and carries the claim as it now stands", "version": "moq-lite-07-wip", "message": "announce", - "hex": "09020105", + "hex": "020701020500018000", "expect": "decoded" }, { - "name": "an ANNOUNCE_FLOOR of 0 certifies nothing", + "name": "a lite-07 ANNOUNCE_END says its publisher withdrew the claim", "version": "moq-lite-07-wip", "message": "announce", - "hex": "09020100", + "hex": "01020100", "expect": "decoded" }, { - "name": "a lite-07 ANNOUNCE_START names its Route ID in place of a hop chain", + "name": "a lite-07 ANNOUNCE_END with Lost says only the sender's way to the claim is gone", "version": "moq-lite-07-wip", "message": "announce", - "hex": "000b0000017802016100010000", + "hex": "01020101", "expect": "decoded" }, { - "name": "a lite-07 ANNOUNCE_START with an invalid path is withdrawn", + "name": "a lite-07 ANNOUNCE_END whose Lost is neither 0 nor 1 is refused", "version": "moq-lite-07-wip", "message": "announce", - "hex": "000d0000032f2f7802016100010000", - "expect": "withdrawn" - }, - { - "name": "a lite-07 ANNOUNCE_START with an invalid instance is withdrawn", - "version": "moq-lite-07-wip", - "message": "announce", - "hex": "0009000001780200000100", - "expect": "withdrawn" - }, - { - "name": "a lite-07 ANNOUNCE_START cut short is refused", - "version": "moq-lite-07-wip", - "message": "announce", - "hex": "0003000003", + "hex": "01020102", "expect": "refused" }, { diff --git a/rs/moq-net/src/client.rs b/rs/moq-net/src/client.rs index 43e87ec89f5..d9e9b7d4149 100644 --- a/rs/moq-net/src/client.rs +++ b/rs/moq-net/src/client.rs @@ -330,10 +330,6 @@ impl Client { token: self.setup_token.clone().filter(|_| version.has_auth()), ietf: self.extensions.ietf && version.has_ietf(), implementation: self.implementation(), - // Filled by `lite::start` from the attached origin handles. - route_key: None, - // Filled by `lite::start` from the version. - lite06_chain: false, } } else { lite::Setup::default() diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index 19753190b77..a23b3ee4ad7 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -878,8 +878,7 @@ mod tests { .buffer(&lite::Track { broadcast: crate::Path::from_static("test"), instance: None, - node: 0, - depth: 0, + node: None, track: "video".into(), }) .unwrap(); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 5db2419fd3f..9efc1fc3f4e 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -455,8 +455,7 @@ fn hop_chain(ids: impl IntoIterator) -> Hops { /// One announcement on an announce stream, resolved: what the application sees. /// -/// The hop chain is lite-06's, and lite-07's trailing Lite-06 Chain: a lite-07 claim -/// names its route instead, and carries a chain only when it was learned over lite-06. +/// The hop ids are lite-06's hop chain, and the relays a lite-07 claim's chain names. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Announced { /// ANNOUNCE_START: a route's suffix and wire hop ids, taking the next id. @@ -467,6 +466,18 @@ pub enum Announced { End(u64), } +/// A lite-07 claim whose chain names the relays `chain`. +fn announce_claim(chain: &[u64]) -> crate::route::node::Claim { + crate::route::relay::Claim { + item: crate::route::node::Attr { epoch: 0, above: 0 }, + cost: 0, + priority: i8::MAX, + draining: false, + unavailable: false, + chain: chain.to_vec(), + } +} + fn announce_version(compress: bool) -> lite::Version { match compress { true => lite::Version::Lite07, @@ -493,29 +504,39 @@ impl AnnounceWriter { /// Append one announcement to `data`. An update or end must name a live id. pub fn write(&mut self, announced: &Announced, data: &mut Vec) { + let routes = self.version.has_routes(); let msg = match announced { Announced::Start(suffix, hops) => { let (_, suffix) = self.encoder.start(suffix.clone()); - // A fixed instance, so the bytes stay pinned and match what `js/net` writes. - lite::AnnounceBroadcast::Active { - route: 0, - instance: self.version.has_instance().then(|| "a".parse().unwrap()), - epoch: 0, - suffix, - hops: hop_chain(hops.iter().copied()), - cost: Default::default(), - above: 0, + match routes { + // A fixed instance, so the bytes stay pinned and match what `js/net` writes. + true => lite::AnnounceBroadcast::Start { + suffix, + instance: "a".parse().unwrap(), + claim: announce_claim(hops), + }, + false => lite::AnnounceBroadcast::Active { + suffix, + hops: hop_chain(hops.iter().copied()), + cost: Default::default(), + }, } } + Announced::Update(id, hops) if routes => lite::AnnounceBroadcast::Change { + id: *id, + claim: announce_claim(hops), + }, Announced::Update(id, hops) => lite::AnnounceBroadcast::Update { id: *id, hops: hop_chain(hops.iter().copied()), cost: Default::default(), - above: 0, }, Announced::End(id) => { self.encoder.end(*id); - lite::AnnounceBroadcast::EndedId { id: *id } + match routes { + true => lite::AnnounceBroadcast::End { id: *id, lost: false }, + false => lite::AnnounceBroadcast::EndedId { id: *id }, + } } }; msg.encode(&mut Encoder::new(data, self.version.into()), self.version) @@ -594,22 +615,56 @@ fn route_clock() -> kio::clock::Deadlines { kio::clock::Deadlines::new(kio::clock::Instant::now()) } -/// The session `tag` to the node `node`. -fn route_peer(tag: u64, node: u64) -> crate::route::node::Peer { - crate::route::node::Peer { tag, node } +/// The forwarding node of the relay `relay`. +fn route_id(relay: u64) -> crate::route::NodeId { + crate::route::NodeId { relay, start: 1 } } -/// The route the publisher `node` sends about itself. -fn route_offer(node: u64) -> crate::route::path::Offer { - crate::route::path::Offer { - path: [crate::route::path::Hop { node, session: 0 }].into(), - draining: false, +/// A link to a neighbour in `role`. +fn route_port(role: crate::route::relay::Role) -> crate::route::relay::Port { + crate::route::relay::Port { + cost: 1, + priority: 0, + upstream: false, + role, + } +} + +/// A neighbour that forwards nothing, asked for nothing yet. +fn route_leaf() -> crate::route::relay::Port { + route_port(crate::route::relay::Role::Scoped(Vec::new())) +} + +/// A claim at `epoch`, `cost` from its publisher. +fn route_claim(epoch: u64, cost: u64) -> crate::route::node::Claim { + crate::route::relay::Claim { + item: crate::route::node::Attr { epoch, above: 0 }, + cost, priority: i8::MAX, - metric: 0, - down_only: false, + draining: false, + unavailable: false, + chain: Vec::new(), } } +/// A peer's request for `prefix` alone. +fn route_ask(prefix: Path) -> crate::route::node::Msg { + crate::route::node::Msg::Ask(crate::route::relay::Edit { + remove: Vec::new(), + add: vec![prefix], + }) +} + +/// Take everything queued on `streams`, returning how many messages and serve changes +/// there were. +fn route_drain<'a>(streams: impl IntoIterator) -> usize { + (streams.into_iter()) + .map(|stream| { + std::iter::from_fn(|| stream.try_wire()).count() + std::iter::from_fn(|| stream.try_serve()).count() + }) + .sum() +} + /// One origin's lite-07 route node answering announce streams, for the routes bench. pub struct RouteNode { node: crate::route::Node, @@ -619,19 +674,20 @@ pub struct RouteNode { impl RouteNode { /// A node holding `claims` claims of its own under `live/`, answering one session - /// per prefix in `streams`, each already sent its snapshot. + /// per prefix in `streams`, each already sent its initial set. pub fn new(claims: usize, streams: &[&str]) -> Self { - let node = crate::route::Node::new(1, 1, route_clock()); + let node = crate::route::Node::new(route_id(1), route_clock()); let instance = crate::Instance::random(); for i in 0..claims { - node.own(Path::new(&format!("live/{i}")).unwrap(), Some(Self::claim(&instance))); + let path = Path::new(&format!("live/{i}")).unwrap(); + let live = crate::route::relay::Change::Live(route_claim(0, 0)); + node.own(false, (path, instance.clone()), live); } let streams = (streams.iter().enumerate()) .map(|(i, prefix)| { - let link = crate::route::Link::default(); - let peer = route_peer(i as u64 + 1, i as u64 + 2); - let stream = node.connect(peer, link).unwrap(); - stream.edit(route_ask(Path::new(prefix).unwrap())).unwrap(); + let stream = node.connect(i as u64 + 1, route_leaf()).unwrap(); + stream.recv(route_ask(Path::new(prefix).unwrap())).unwrap(); + stream.flush(); stream }) .collect(); @@ -644,34 +700,24 @@ impl RouteNode { node } - fn claim(instance: &crate::Instance) -> crate::route::node::Claim { - crate::route::node::Claim { - instance: instance.clone(), - epoch: 0, - cost: 0, - upstream: false, - learned: false, - above: 0, - chain: crate::Hops::new(), - } - } - /// Start or withdraw the node's own claim at `path`. pub fn own(&self, path: &str, start: bool) { - let claim = start.then(|| Self::claim(&self.instance)); - self.node.own(Path::new(path).unwrap(), claim); + let change = match start { + true => crate::route::relay::Change::Live(route_claim(0, 0)), + false => crate::route::relay::Change::Ended, + }; + let broadcast = (Path::new(path).unwrap(), self.instance.clone()); + self.node.own(false, broadcast, change); } /// Take every output queued on the streams, returning how many there were. pub fn drain(&self) -> usize { - (self.streams.iter()) - .map(|stream| std::iter::from_fn(|| stream.try_output()).count()) - .sum() + route_drain(&self.streams) } } -/// A relay's lite-07 route node hearing one origin's claims on streams it asked for, -/// and forwarding them on streams it answers, for the routes bench. +/// A relay's lite-07 route node hearing one origin's claims on the sessions to it, and +/// telling them to the sessions that ask, for the routes bench. pub struct RouteRelay { // Dropped first: a stream disconnects from its node on drop. receive: Vec, @@ -680,9 +726,9 @@ pub struct RouteRelay { node: crate::route::Node, instance: crate::Instance, claims: usize, - /// The origin's revision as the first stream last heard it. + /// The origin's revision as the first session last heard it. rev: u64, - /// The one-claim batches the first stream heard that the second has yet to: each + /// The one-claim changes the first session heard that the second has yet to: each /// revision and the claim it changed. behind: std::collections::VecDeque<(u64, usize)>, } @@ -692,31 +738,44 @@ impl RouteRelay { const ORIGIN: u64 = 1; /// A relay that already heard the origin's `claims` claims under `live/` on each of - /// `streams` streams, and forwarded them on each of `answers` streams. + /// `streams` sessions, and told them to each of `answers` sessions. fn new(claims: usize, streams: usize, answers: usize) -> Self { - let node = crate::route::Node::new(2, 2, route_clock()); + let node = crate::route::Node::new(route_id(2), route_clock()); let answer = (0..answers as u64) .map(|i| { - let peer = route_peer(i + 1, i + 3); - let stream = node.connect(peer, crate::route::Link::default()).unwrap(); - stream.edit(route_ask(Path::empty())).unwrap(); + let stream = node.connect(i + 1, route_leaf()).unwrap(); + stream.recv(route_ask(Path::empty())).unwrap(); + stream.flush(); stream }) .collect(); let instance = crate::Instance::random(); let rev = claims as u64; + let origin = route_id(Self::ORIGIN); let receive = (0..streams as u64) .map(|i| { - let peer = route_peer(answers as u64 + i + 1, Self::ORIGIN); - let receive = node.connect(peer, crate::route::Link::default()).unwrap(); + let port = route_port(crate::route::relay::Role::Routing(origin)); + let receive = node.connect(answers as u64 + i + 1, port).unwrap(); // The peer answered what the node asks it for. - receive.edited(Default::default()); - let reach = crate::route::path::Reach::Finite(route_offer(Self::ORIGIN)); - receive.route(Self::ORIGIN, Path::empty(), Some(reach)); - let snapshot = (0..claims) - .map(|i| (Self::path(i), Some(Self::claim(&instance, 0)))) - .collect(); - receive.batch(Self::ORIGIN, Path::empty(), snapshot, Some(rev)); + receive.recv(crate::route::node::Msg::Edited).unwrap(); + let offer = crate::route::relay::Offer { + path: [crate::route::relay::Hop { + node: origin, + session: 0, + }] + .into(), + draining: false, + priority: i8::MAX, + cost: 0, + marked: false, + }; + let route = crate::route::node::Msg::Route { + origin, + offer: Some(offer), + }; + receive.recv(route).unwrap(); + let changes = (0..claims).map(|i| (i, route_claim(0, 0))).collect(); + Self::hear(&receive, &instance, changes, rev); receive }) .collect(); @@ -733,20 +792,19 @@ impl RouteRelay { relay } - /// A relay hearing the origin's `claims` claims on one stream. + /// A relay hearing the origin's `claims` claims on one session. pub fn heard(claims: usize) -> Self { Self::new(claims, 1, 1) } - /// A relay hearing the origin's `claims` claims on one stream and answering - /// `answers` streams, holding every claim back from them since none is confirmed - /// served. + /// A relay hearing the origin's `claims` claims on one session and answering + /// `answers` sessions. pub fn held(claims: usize, answers: usize) -> Self { Self::new(claims, 1, answers) } - /// A relay hearing the origin's `claims` claims on two streams, the second `lag` - /// one-claim batches behind the first. + /// A relay hearing the origin's `claims` claims on two sessions, the second `lag` + /// one-claim changes behind the first. pub fn lagging(claims: usize, lag: usize) -> Self { let mut relay = Self::new(claims, 2, 1); for _ in 0..lag { @@ -756,29 +814,34 @@ impl RouteRelay { relay } - fn path(i: usize) -> Path { - Path::new(&format!("live/{i}")).unwrap() - } - - fn claim(instance: &crate::Instance, cost: u64) -> crate::route::node::Claim { - crate::route::node::Claim { - instance: instance.clone(), - epoch: 0, - cost, - upstream: false, - learned: false, - above: 0, - chain: crate::Hops::new(), - } + /// `stream` hears the origin change the claims `changes` names, up to `rev`. + fn hear( + stream: &crate::route::node::Stream, + instance: &crate::Instance, + changes: Vec<(usize, crate::route::node::Claim)>, + rev: u64, + ) { + let changes = (changes.into_iter()) + .map(|(i, claim)| { + let path = Path::new(&format!("live/{i}")).unwrap(); + ((path, instance.clone()), crate::route::relay::Change::Live(claim)) + }) + .collect(); + let claims = crate::route::node::Msg::Claims { + origin: route_id(Self::ORIGIN), + changes, + floor: rev, + }; + stream.recv(claims).unwrap(); + stream.flush(); } - /// Hear one batch updating the first `changes` claims, returning how many outputs - /// the relay queued: what it serves into its origin, and what it forwards. + /// Hear the first `changes` claims change at once, returning how many outputs the + /// relay queued: what it serves into its origin, and what it tells its sessions. pub fn change(&mut self, changes: usize) -> usize { self.rev += 1; - let claim = Self::claim(&self.instance, self.rev); - let batch = (0..changes).map(|i| (Self::path(i), Some(claim.clone()))).collect(); - self.receive[0].batch(Self::ORIGIN, Path::empty(), batch, Some(self.rev)); + let changes = (0..changes).map(|i| (i, route_claim(0, self.rev))).collect(); + Self::hear(&self.receive[0], &self.instance, changes, self.rev); self.drain() } @@ -791,518 +854,130 @@ impl RouteRelay { /// The first claim changes to `epoch`, and the relay confirms serving it. fn confirm(&mut self, epoch: u64) -> usize { self.rev += 1; - let claim = crate::route::node::Claim { - epoch, - ..Self::claim(&self.instance, 0) - }; - let batch = vec![(Self::path(0), Some(claim))]; - self.receive[0].batch(Self::ORIGIN, Path::empty(), batch, Some(self.rev)); - let serves: Vec<_> = std::iter::from_fn(|| self.receive[0].try_output()).collect(); + Self::hear( + &self.receive[0], + &self.instance, + vec![(0, route_claim(epoch, 0))], + self.rev, + ); + let serves: Vec<_> = std::iter::from_fn(|| self.receive[0].try_serve()).collect(); for serve in &serves { if let crate::route::node::Output::Serve { - id, - claim, - prefix, - served, - .. + origin, path, instance, .. } = serve { - self.receive[0].serving(*id, claim, prefix, &served.claim); + self.receive[0].serving(*origin, &(path.clone(), instance.clone()), true); } } serves.len() + self.drain() } - /// The first stream hears the origin's next revision, which changes one claim. + /// The first session hears the origin's next revision, which changes one claim. fn advance(&mut self) { self.rev += 1; let i = self.rev as usize % self.claims; - let batch = vec![(Self::path(i), Some(Self::claim(&self.instance, self.rev)))]; - self.receive[0].batch(Self::ORIGIN, Path::empty(), batch, Some(self.rev)); + let changes = vec![(i, route_claim(0, self.rev))]; + Self::hear(&self.receive[0], &self.instance, changes, self.rev); self.behind.push_back((self.rev, i)); } - /// Each stream hears its next one-claim batch, the second still lagging, returning + /// Each session hears its next one-claim change, the second still lagging, returning /// how many outputs the relay queued. pub fn step(&mut self) -> usize { self.advance(); - let (rev, i) = self.behind.pop_front().expect("the second stream lags"); - let batch = vec![(Self::path(i), Some(Self::claim(&self.instance, rev)))]; - self.receive[1].batch(Self::ORIGIN, Path::empty(), batch, Some(rev)); + let (rev, i) = self.behind.pop_front().expect("the second session lags"); + Self::hear(&self.receive[1], &self.instance, vec![(i, route_claim(0, rev))], rev); self.drain() } fn drain(&self) -> usize { - (self.receive.iter().chain(&self.answer)) - .map(|stream| std::iter::from_fn(|| stream.try_output()).count()) - .sum() + route_drain(self.receive.iter().chain(&self.answer)) } } -/// A lite-07 route node following one origin on a stream per prefix, as a viewer -/// session holding many announce prefixes does, for the routes bench. +/// A lite-07 route node asking one peer for many prefixes, as a viewer session holding +/// many announce prefixes does, for the routes bench. pub struct RouteFollower { + #[allow(dead_code, reason = "held so the stream keeps a node to talk to")] node: crate::route::Node, - /// Each prefix's stream, `None` only while [`Self::refollow`] replaces it. - streams: Vec>, + stream: crate::route::node::Stream, instance: crate::Instance, + prefixes: usize, claims: usize, - /// The next stream [`Self::refollow`] replaces. + /// The next prefix [`Self::refollow`] replaces. next: usize, } impl RouteFollower { - /// The origin's identity and revision as the node hears them. - const ORIGIN: u64 = 1; - const REV: u64 = 1; - /// The viewer session every stream belongs to. - const TAG: u64 = 1; - - /// A node following `prefixes` prefixes `live/{k}`, each holding `claims` claims. + /// A node asking for `prefixes` prefixes `live/{k}`, each holding `claims` claims. pub fn new(prefixes: usize, claims: usize) -> Self { - let mut follower = Self { - node: crate::route::Node::new(2, 2, route_clock()), - streams: Vec::new(), + let node = crate::route::Node::new(route_id(2), route_clock()); + let stream = node.connect(1, route_leaf()).unwrap(); + let follower = Self { + node, + stream, instance: crate::Instance::random(), + prefixes, claims, next: 0, }; - follower.streams = (0..prefixes).map(|k| Some(follower.follow(k))).collect(); + for k in 0..prefixes { + follower.follow(k); + } follower.drain(); follower } - /// Open the session for prefix `k`, which hears the origin's route and snapshot. - fn follow(&self, k: usize) -> crate::route::node::Stream { - let prefix = Path::new(&format!("live/{k}")).unwrap(); - let peer = route_peer(Self::TAG + k as u64, Self::ORIGIN); - let link = crate::route::Link { - interest: [prefix.clone()].into(), - ..Default::default() - }; - let stream = self.node.connect(peer, link).unwrap(); - // The peer answered what the node asks it for. - stream.edited(Default::default()); - let reach = crate::route::path::Reach::Finite(route_offer(Self::ORIGIN)); - stream.route(Self::ORIGIN, prefix.clone(), Some(reach)); - let claim = crate::route::node::Claim { - instance: self.instance.clone(), - epoch: 0, - cost: 0, - upstream: false, - learned: false, - above: 0, - chain: crate::Hops::new(), + fn prefix(k: usize) -> Path { + Path::new(&format!("live/{k}")).unwrap() + } + + /// Ask for prefix `k`, which the peer answers with its initial set. + fn follow(&self, k: usize) { + let prefix = Self::prefix(k); + let edit = crate::route::relay::Edit { + remove: Vec::new(), + add: vec![prefix.clone()], }; - let snapshot = (0..self.claims) - .map(|j| (Path::new(&format!("live/{k}/{j}")).unwrap(), Some(claim.clone()))) + self.stream.ask(edit).unwrap(); + self.stream.recv(crate::route::node::Msg::Edited).unwrap(); + let changes = (0..self.claims) + .map(|j| { + let path = Path::new(&format!("live/{k}/{j}")).unwrap(); + let live = crate::route::relay::Change::Live(route_claim(0, 0)); + ((path, self.instance.clone()), live) + }) .collect(); - stream.batch(Self::ORIGIN, prefix, snapshot, Some(Self::REV)); - stream + let announce = crate::route::node::Msg::Announce { changes }; + self.stream.recv(announce).unwrap(); + self.stream.recv(crate::route::node::Msg::Synced(prefix)).unwrap(); + self.stream.flush(); } - /// Close one prefix's session and open it again, returning how many outputs the + /// Stop asking for one prefix and ask for it again, returning how many outputs the /// node queued. pub fn refollow(&mut self) -> usize { - let k = self.next % self.streams.len(); + let k = self.next % self.prefixes; self.next += 1; - // Closed first, so the node lets go of the prefix before hearing it again. - drop(self.streams[k].take()); - self.streams[k] = Some(self.follow(k)); + let edit = crate::route::relay::Edit { + remove: vec![Self::prefix(k)], + add: Vec::new(), + }; + self.stream.ask(edit).unwrap(); + // Answered first, so the node lets go of the prefix before hearing it again. + self.stream.recv(crate::route::node::Msg::Edited).unwrap(); + self.stream.flush(); + self.follow(k); self.drain() } - /// Close every session. + /// Close the session. pub fn teardown(self) { - drop(self.streams); + drop(self.stream); } fn drain(&self) -> usize { - (self.streams.iter().flatten()) - .map(|stream| std::iter::from_fn(|| stream.try_output()).count()) - .sum() - } -} - -/// A peer's request for `prefix` alone. -fn route_ask(prefix: Path) -> crate::route::path::Edit { - crate::route::path::Edit { - remove: Vec::new(), - add: vec![prefix], - } -} - -/// The path router originating claims and answering streams, for the routes bench. -pub struct PathRouteNode(crate::route::path::Router); - -impl PathRouteNode { - /// An origin with `claims` claims, answering the given interests. - pub fn new(claims: usize, interests: &[&str]) -> Self { - let mut node = Self(crate::route::path::Router::new(1, 1, Default::default())); - node.0.originate(1); - for i in 0..claims { - node.own(&format!("live/{i}"), true); - } - for (peer, prefix) in interests.iter().enumerate() { - node.0.connect(peer, peer as u64 + 2, Default::default()).unwrap(); - let add = vec![Path::new(prefix).unwrap().to_owned()]; - node.0 - .edit( - peer, - crate::route::path::Edit { - remove: Vec::new(), - add, - }, - ) - .unwrap(); - } - node.drain(); - node - } - - /// Start or withdraw a claim. - pub fn own(&mut self, claim: &str, start: bool) { - let claim = Path::new(claim).unwrap(); - if start { - self.0.announce(claim, ()).unwrap(); - } else { - self.0.unannounce(claim).unwrap(); - } - } - - /// Consume messages and reader notifications, returning the messages sent. - pub fn drain(&mut self) -> usize { - let sent = self.0.drain().len(); - self.0.events(); - self.0.changed(); - sent - } -} - -/// A path router receiving one publisher and forwarding its claims to a reader. -pub struct PathRouteRelay { - router: crate::route::path::Router, - revision: u64, -} - -impl PathRouteRelay { - /// Hear `claims` claims and answer one stream watching the root. - pub fn new(claims: usize) -> Self { - use crate::route::path::{Hop, Offer, Reach}; - let mut relay = Self { - router: crate::route::path::Router::new(2, 2, Default::default()), - revision: 1, - }; - relay.router.connect(0, 1, Default::default()).unwrap(); - relay.router.edited(0); - relay.router.route( - 0, - 1, - Path::empty(), - Some(Reach::Finite(Offer { - path: [Hop { node: 1, session: 0 }].into(), - draining: false, - priority: i8::MAX, - metric: 0, - down_only: false, - })), - ); - let snapshot = (0..claims) - .map(|i| (Path::new(&format!("live/{i}")).unwrap(), Some(1))) - .collect(); - relay.router.batch(0, 1, Path::empty(), snapshot, Some(1)); - relay.router.connect(1, 3, Default::default()).unwrap(); - let add = vec![Path::empty()]; - relay - .router - .edit( - 1, - crate::route::path::Edit { - remove: Vec::new(), - add, - }, - ) - .unwrap(); - relay.drain(); - relay - } - - /// Receive one batch and return its forwarded messages and reader changes. - pub fn change(&mut self, claims: usize) -> (usize, usize) { - self.revision += 1; - let changes = (0..claims) - .map(|i| (Path::new(&format!("live/{i}")).unwrap(), Some(self.revision))) - .collect(); - self.router.batch(0, 1, Path::empty(), changes, Some(self.revision)); - self.drain() - } - - fn drain(&mut self) -> (usize, usize) { - let sent = self.router.drain().len(); - let events = self.router.events().len(); - self.router.changed(); - (sent, events) - } -} - -/// The path router following one publisher on a session per prefix, for the routes bench. -pub struct PathRouteFollower { - router: crate::route::path::Router, - prefixes: Vec, - claims: usize, - next: usize, - now: kio::clock::Instant, -} - -impl PathRouteFollower { - /// A route for each `live/{k}`, each carrying `claims` claims. - pub fn new(prefixes: usize, claims: usize) -> Self { - let mut follower = Self { - router: crate::route::path::Router::new(2, 2, crate::route::path::Doubt::default()), - prefixes: (0..prefixes) - .map(|k| Path::new(&format!("live/{k}")).unwrap()) - .collect(), - claims, - next: 0, - now: kio::clock::Instant::now(), - }; - for k in 0..prefixes { - follower.follow(k); - } - follower.drain(); - assert_eq!(follower.router.shown().len(), prefixes * claims); - follower - } - - fn follow(&mut self, k: usize) { - use crate::route::path::{Hop, Offer, Reach}; - let prefix = self.prefixes[k].clone(); - self.router.connect(k, 1, crate::route::Link::default()).unwrap(); - self.router.edited(k); - self.router.route( - k, - 1, - prefix.clone(), - Some(Reach::Finite(Offer { - path: [Hop { node: 1, session: 0 }].into(), - draining: false, - priority: i8::MAX, - metric: 0, - down_only: false, - })), - ); - let changes = (0..self.claims) - .map(|j| (Path::new(&format!("live/{k}/{j}")).unwrap(), Some(()))) - .collect(); - self.router.batch(k, 1, prefix, changes, Some(1)); - } - - /// Close every session in one call, including the resulting timer work. - pub fn teardown(mut self) { - self.router.disconnect(0..self.prefixes.len(), self.now); - self.expire(); - assert!(self.router.shown().is_empty()); - self.drain(); - } - - /// Replace one prefix's session and snapshot, including the resulting timer work. - pub fn refollow(&mut self) { - let k = self.next % self.prefixes.len(); - self.next += 1; - self.router.disconnect([k], self.now); - self.follow(k); - self.expire(); - assert_eq!(self.router.shown().len(), self.prefixes.len() * self.claims); - self.drain(); - } - - fn expire(&mut self) { - // Advance the mocked input clock rather than waiting a real doubt interval. - self.now = self.now.saturating_add(crate::route::path::Doubt::default().timer); - self.router.timeout(self.now); - } - - fn drain(&mut self) { - self.router.drain(); - self.router.events(); - self.router.changed(); - } -} - -/// A path router hearing one publisher through `sessions` sessions of a node that -/// then sends GOAWAY, a route per session, and answering `recipients` streams that -/// watch the root, for the routes bench. -pub struct PathRouteDrain { - router: crate::route::path::Router, - recipients: usize, -} - -impl PathRouteDrain { - const LEAVING: u64 = 500; - - /// Hear a route over each of `sessions` sessions of the leaving node, and answer - /// `recipients` streams watching the root. - pub fn new(sessions: usize, recipients: usize) -> Self { - use crate::route::path::{Hop, Offer, Reach}; - let mut router = crate::route::path::Router::new(2, 2, crate::route::path::Doubt::default()); - for i in 0..sessions { - let prefix = Path::new(&format!("live/{i}")).unwrap(); - let via = 1_000 + i as u64; - router.connect(i, via, crate::route::Link::default()).unwrap(); - router.edited(i); - let path = [ - Hop { node: 1, session: 0 }, - Hop { - node: Self::LEAVING, - session: i as u64 + 1, - }, - Hop { node: via, session: 1 }, - ]; - router.route( - i, - 1, - prefix.clone(), - Some(Reach::Finite(Offer { - path: path.into(), - draining: false, - priority: i8::MAX, - metric: 1, - down_only: false, - })), - ); - let claim = Path::new(&format!("live/{i}/x")).unwrap(); - router.batch(i, 1, prefix, vec![(claim, Some(()))], Some(1)); - } - for r in 0..recipients { - let peer = sessions + r; - router - .connect(peer, 5_000 + r as u64, crate::route::Link::default()) - .unwrap(); - let add = vec![Path::empty()]; - router - .edit( - peer, - crate::route::path::Edit { - remove: Vec::new(), - add, - }, - ) - .unwrap(); - } - let mut node = Self { router, recipients }; - node.flush(); - node - } - - /// Hear the leaving node drain, returning the messages sent: each recipient's - /// DRAIN, then each route it was sent, now ranked draining. - pub fn drain(&mut self) -> usize { - use crate::route::path::{Dead, Out}; - let now = kio::clock::Instant::now(); - self.router.dead(0, Dead::Draining { node: Self::LEAVING }, now); - let out = self.router.drain(); - let told = (out.iter()) - .filter(|out| { - matches!( - out, - Out::Dead { - dead: Dead::Draining { .. }, - .. - } - ) - }) - .count(); - assert_eq!(told, self.recipients, "each recipient hears the drain once"); - self.router.events(); - self.router.changed(); - out.len() - } - - fn flush(&mut self) -> usize { - let sent = self.router.drain().len(); - self.router.events(); - self.router.changed(); - sent - } -} - -/// A path router hearing one publisher from a neighbour, under a prefix of its own with -/// `claims` claims, and answering `streams` streams that each watch a prefix it does not -/// claim under, for the routes bench. -pub struct PathRouteChange { - router: crate::route::path::Router, - far: u64, -} - -impl PathRouteChange { - const UP: u64 = 1_000; - - fn reach(id: u64, far: u64) -> crate::route::path::Reach { - use crate::route::path::{Hop, Offer, Reach}; - let path = [ - Hop { node: id, session: 0 }, - Hop { node: far, session: 1 }, - Hop { - node: Self::UP, - session: 1, - }, - ]; - Reach::Finite(Offer { - path: path.into(), - draining: false, - priority: i8::MAX, - metric: 1, - down_only: false, - }) - } - - /// Hear one publisher with `claims` claims, and answer `streams` streams its route - /// misses. - pub fn new(claims: usize, streams: usize) -> Self { - let mut router = crate::route::path::Router::new(2, 2, crate::route::path::Doubt::default()); - router.connect(0, Self::UP, crate::route::Link::default()).unwrap(); - router.edited(0); - let prefix = Path::new("o0").unwrap(); - router.route(0, 1, prefix.clone(), Some(Self::reach(1, 500))); - let changes = (0..claims) - .map(|c| (Path::new(&format!("o0/{c}")).unwrap(), Some(()))) - .collect(); - router.batch(0, 1, prefix, changes, Some(1)); - for s in 0..streams { - let peer = 1 + s; - router - .connect(peer, 5_000 + s as u64, crate::route::Link::default()) - .unwrap(); - let add = vec![Path::new(&format!("x{s}")).unwrap().to_owned()]; - router - .edit( - peer, - crate::route::path::Edit { - remove: Vec::new(), - add, - }, - ) - .unwrap(); - } - let mut node = Self { router, far: 500 }; - node.flush(); - node - } - - /// The neighbour moves the publisher's route onto another path, which no stream - /// watches. - pub fn repath(&mut self) { - self.far = if self.far == 500 { 501 } else { 500 }; - self.router - .route(0, 1, Path::new("o0").unwrap(), Some(Self::reach(1, self.far))); - assert_eq!(self.flush(), 0); - } - - fn flush(&mut self) -> usize { - let sent = self.router.drain().len(); - self.router.events(); - self.router.changed(); - sent + route_drain([&self.stream]) } } @@ -1409,8 +1084,12 @@ pub fn decode_announces(data: &[u8], compress: bool) -> Vec { .start(Some(suffix)) .and_then(|suffix| suffix.ok_or(crate::Error::ProtocolViolation)) .map(|suffix| Announced::Start(suffix, hops.into_ids())), - // A refused message assigns ids this harness does not model. - lite::AnnounceBroadcast::Refused { .. } => break, + lite::AnnounceBroadcast::Start { suffix, claim, .. } => decoder + .start(Some(suffix)) + .and_then(|suffix| suffix.ok_or(crate::Error::ProtocolViolation)) + .map(|suffix| Announced::Start(suffix, claim.chain)), + lite::AnnounceBroadcast::Change { id, claim } => decoder.get(id).map(|_| Announced::Update(id, claim.chain)), + lite::AnnounceBroadcast::End { id, .. } => decoder.end(id).map(|_| Announced::End(id)), lite::AnnounceBroadcast::Update { id, hops, .. } => { decoder.get(id).map(|_| Announced::Update(id, hops.into_ids())) } @@ -1516,8 +1195,7 @@ impl LiteSample { Self::Subscribe => lite::Subscribe { order: crate::track::Order::Descending, instance: None, - node: 0, - depth: 0, + node: None, id: 3, broadcast: Path::from_static("room/alice"), track: "video".into(), @@ -1717,8 +1395,7 @@ impl Default for Messages { lite_subscribe: lite::Subscribe { order: crate::track::Order::Descending, instance: None, - node: 0, - depth: 0, + node: None, id: 7, broadcast: Path::from_static("room/alice"), track: "video".into(), @@ -1903,8 +1580,7 @@ pub fn encode_subscribe(broadcast: &Path, track: &str, ietf: bool, out: &mut Vec id: 0, broadcast: broadcast.clone(), instance: None, - node: 0, - depth: 0, + node: None, track: track.into(), priority: 0, max_delay: std::time::Duration::ZERO, @@ -2148,25 +1824,22 @@ pub fn pattern(data: &[u8]) -> bool { } /// Lite seeds whose envelope bytes the uniform sweep never forms, on the newest -/// version: a typed empty TRACKS_SYNCED, an AUTH_OK reply, every route message, -/// both kinds of DEAD, and an ANNOUNCE_REQUEST and its ANNOUNCE_OK (the sweep sets -/// ROUTE_START's type byte but fills its size byte with the body pattern, so none -/// decodes whole), and a DATAGRAMS request, whose eight fields outgrow the sweep's -/// longest body. +/// version: a typed empty TRACKS_SYNCED, an AUTH_OK reply, every route message, and +/// an ANNOUNCE_REQUEST and its ANNOUNCE_OK (the sweep sets ROUTE's type byte but +/// fills its size byte with the body pattern, so none decodes whole), and a DATAGRAMS +/// request, whose seven fields outgrow the sweep's longest body. const TYPED_LITE_SEEDS: &[(u8, &[u8])] = &[ (23, &[4, 0]), - (30, &[8, 0, 0, 0, 0, 0, 0, 0, 0]), + (30, &[7, 0, 0, 0, 0, 0, 0, 0]), (27, &[0, 3, 0, 0, 0]), - (5, &[6, 9, 1, 0, 0, 1, 1, 0, 0, 0x80, 1]), - (5, &[7, 7, 0, 1, 1, 0, 0, 0x80, 1]), + // ROUTE, ROUTE_END, DEAD, DRAIN, ANNOUNCE_FLOOR, ANNOUNCE_REQUEST, ANNOUNCE_OK. + (5, &[7, 7, 1, 7, 1, 0, 0, 0x80, 1]), (5, &[8, 1, 0]), - (5, &[9, 2, 0, 1]), - (5, &[11, 1, 0]), - (5, &[15, 6, 0, 1, 1, 2, 0, 0]), - (5, &[15, 6, 0, 1, 1, 2, 1, 2]), - (5, &[15, 2, 1, 1]), - (5, &[16, 4, 0, 1, 0, 0]), - (5, &[17, 0]), + (5, &[9, 5, 7, 1, 2, 9, 1]), + (5, &[10, 2, 7, 1]), + (5, &[3, 2, 0, 1]), + (5, &[5, 4, 0, 1, 0, 0]), + (5, &[6, 0]), ]; /// The inputs the fuzzer starts from: every (version, type) pair the dispatch knows, diff --git a/rs/moq-net/src/lite/announce.rs b/rs/moq-net/src/lite/announce.rs index 18b8d9b14a3..ada0e28d9d3 100644 --- a/rs/moq-net/src/lite/announce.rs +++ b/rs/moq-net/src/lite/announce.rs @@ -1,12 +1,14 @@ -use std::sync::Arc; - use num_enum::{IntoPrimitive, TryFromPrimitive}; use crate::{ Hop, Hops, Instance, Path, coding::*, origin::Cost, - route::{self, path::Offer}, + route::{ + self, + node::{Attr, Claim}, + relay::{Hop as RouteHop, NodeId, Offer}, + }, }; use super::{ @@ -20,27 +22,17 @@ use super::{ const ANNOUNCE_START: u64 = 0; const ANNOUNCE_END: u64 = 1; const ANNOUNCE_UPDATE: u64 = 2; -// lite-07: the advertisement serves another instance, or a newer epoch of its own. -const ANNOUNCE_RESTART: u64 = 3; +// lite-07: one claims message per change on a routing neighbour's stream is closed by a floor. +const ANNOUNCE_FLOOR: u64 = 3; const SYNCED: u64 = 4; -// lite-07: the route layer. 10 was ROUTE_REQUEST, which the path rule retired. -const ROUTE_START: u64 = 6; -const ROUTE_UPDATE: u64 = 7; -const ROUTE_END: u64 = 8; -const ANNOUNCE_FLOOR: u64 = 9; -const ROUTE_HOLD: u64 = 11; -// 12 to 14 were WARM_START, WARM_UPDATE, and WARM_END, which return in a later wip version -// with an implementation that picks a source per broadcast. -// lite-07: news of a closed session or a draining node on the routes the sender offered. -const DEAD: u64 = 15; // lite-07: an edit of the prefixes the sender wants, and the marker answering one. -const ANNOUNCE_REQUEST: u64 = 16; -const ANNOUNCE_OK: u64 = 17; - -/// DEAD kind: a session on the routes' paths closed. -const DEAD_CLOSED: u64 = 0; -/// DEAD kind: a node on the routes' paths sent GOAWAY. -const DEAD_DRAIN: u64 = 1; +const ANNOUNCE_REQUEST: u64 = 5; +const ANNOUNCE_OK: u64 = 6; +// lite-07: the routes between forwarding nodes. +const ROUTE: u64 = 7; +const ROUTE_END: u64 = 8; +const DEAD: u64 = 9; +const DRAIN: u64 = 10; /// Whether the negotiated version can update a live advertisement's metadata. On lite-05 an /// update travels as a duplicate ANNOUNCE (a second `active` for an already-announced path); on @@ -58,66 +50,43 @@ pub fn update_supported(version: Version) -> bool { /// A message on the Announce Stream, advertising or retracting a broadcast. /// /// Lite-07 carries everything one side says about routing on its one unidirectional -/// stream: the edits of the prefixes it wants (`Request`), and the markers (`Ok`), -/// routes, and claims answering the prefixes its peer wants. +/// stream: the edits of the prefixes it wants (`Request`), and the markers (`Ok`, +/// `Synced`), routes, and claims answering the prefixes its peer wants. /// -/// On lite-06+ these are independently-typed messages (`ANNOUNCE_START`, -/// `ANNOUNCE_END`, `ANNOUNCE_UPDATE`, and on lite-07 `ANNOUNCE_RESTART`), each -/// framed as `Type | Length | Body` like the subscribe stream's responses. Each -/// `Active` (ANNOUNCE_START) implicitly assigns the next announce id (a -/// per-stream ordinal starting at 0); the others reference that id instead of -/// repeating the path. Older versions send a single `ANNOUNCE_BROADCAST` -/// message that retracts by path (`Ended`). +/// On lite-06+ these are independently-typed messages, each framed as +/// `Type | Length | Body` like the subscribe stream's responses. Each ANNOUNCE_START +/// implicitly assigns the next announce id (a per-stream ordinal starting at 0); the +/// others reference that id instead of repeating the path. Older versions send a single +/// `ANNOUNCE_BROADCAST` message that retracts by path (`Ended`). /// /// The path is as it appears on the wire: on lite-07 it may name a base announcement, /// resolved against the stream's history by [`AnnounceDecoder`](super::AnnounceDecoder). -/// -/// Lite-07 names the route each claim travels under in place of a hop chain, and `route` -/// must be 0 on every earlier version. There `hops` is the Lite-06 Chain instead: the -/// chain of a claim learned over moq-lite-06, empty for every other claim, and sent only -/// to a peer that offered the Lite-06 Chain Setup Parameter. #[derive(Clone, Debug, PartialEq, Eq)] pub enum AnnounceBroadcast { /// ANNOUNCE_START (lite-06) / active (older): a broadcast is now available. - /// Carries the path suffix, (lite-07+) the Route ID and the instance and epoch it - /// serves, (before lite-07) the hop chain, and (lite-06+) the cost, and assigns the - /// next announce id. The instance and epoch change only with a `Restart`. On lite-07, - /// `above` is how many segments the claim's own prefix lacks of the one its suffix names, - /// 0 for a claim exactly there; it is 0 on every earlier version. - Active { - suffix: PathRef, - route: u64, - instance: Option, - epoch: u64, - hops: Hops, - cost: Cost, - above: u64, - }, + /// Carries the path suffix, the hop chain, and (lite-06) the cost, and assigns the + /// next announce id. + Active { suffix: PathRef, hops: Hops, cost: Cost }, /// Pre-lite-06: a broadcast is no longer available, retracted by path. Ended { suffix: Path, hops: Hops }, - /// ANNOUNCE_END (lite-06+): a broadcast is no longer available, retracted by + /// ANNOUNCE_END (lite-06): a broadcast is no longer available, retracted by /// announce id. The id is retired; referencing it again is a protocol violation. EndedId { id: u64 }, - /// ANNOUNCE_UPDATE (lite-06+): atomically replace the metadata of the announcement - /// with this id: its cost, and before lite-07 its hop chain. The id stays live. - Update { - id: u64, - hops: Hops, - cost: Cost, - above: u64, - }, - /// ANNOUNCE_RESTART (lite-07+): the announcement with this id now serves the - /// instance and epoch given. Another instance is another stream, so whatever the - /// receiver resolved under its prefix is stale; a higher epoch of the same one - /// continues it. The id stays live. - Restart { - id: u64, - instance: Option, - epoch: u64, - cost: Cost, - hops: Hops, - above: u64, + /// ANNOUNCE_UPDATE (lite-06): atomically replace the hop chain and cost of the + /// announcement with this id. The id stays live. + Update { id: u64, hops: Hops, cost: Cost }, + /// ANNOUNCE_START (lite-07): a claim of the broadcast at `suffix` serving + /// `instance`, which assigns it the next announce id. + Start { + suffix: PathRef, + instance: Instance, + claim: Claim, }, + /// ANNOUNCE_UPDATE (lite-07): the claim with this id now stands as `claim`. + Change { id: u64, claim: Claim }, + /// ANNOUNCE_END (lite-07): the claim with this id is gone, and the id retired. Its + /// publisher withdrew it unless `lost`, when the sender only lost its way to it. + End { id: u64, lost: bool }, /// An unknown lite-06+ announce type. The length-prefixed body was skipped so /// the stream stays up; it does not assign an announce id. Skipped, @@ -134,289 +103,162 @@ pub enum AnnounceBroadcast { remove: Vec, add: Vec<(Path, bool)>, }, - /// ANNOUNCE_OK (lite-07+): the receiver's last edit takes effect here. + /// ANNOUNCE_OK (lite-07+): the receiver's oldest unanswered edit takes effect here. Ok, - /// ROUTE_START (lite-07+): the sender has a route to the publisher `node` covering - /// the prefix the receiver assigned the Prefix ID `prefix` joined with `suffix`, and - /// assigns it the next Route ID. `exact` marks a route the sender keeps at its own - /// prefix whatever prefix the request lies at, so an edit widening the request past - /// it leaves it there. - RouteStart { - node: route::Id, - prefix: u64, - suffix: Path, - exact: bool, - offer: Offer, - }, - /// ROUTE_UPDATE (lite-07+): the route with this id changed, and is usable. - RouteUpdate { id: u64, offer: Offer }, - /// ROUTE_HOLD (lite-07+): the sender has no usable route with this id, but still - /// holds its advertisements while it recovers. - RouteHold { id: u64 }, + /// ROUTE (lite-07+): the sender's route to the forwarding node its path begins + /// with, replacing the one it sent before, or else assigned the next Route ID. + Route { offer: Offer }, /// ROUTE_END (lite-07+): the sender let go of the route with this id, and of every - /// advertisement under it. + /// claim under it. RouteEnd { id: u64 }, - /// DEAD (lite-07+): a session or node on the routes the sender offered is gone or - /// going. - Dead(Dead), - /// ANNOUNCE_FLOOR (lite-07+): closes the batch of advertisements under the route - /// with this id, certifying them across the whole requested prefix at the origin's - /// revision, or certifying nothing (`None`). - Floor { id: u64, revision: Option }, - /// A route message (lite-07+) whose content, which the sender only forwarded, this - /// node refuses. The receiver withdraws what it names and keeps the session. Decoded - /// only as far as keeps the stream's ids in step; never sent. - Refused { message: Refusable, reason: Refusal }, -} - -/// What a refused route message still tells the receiver, so the ids that follow it -/// resolve. -#[derive(Clone, Debug, PartialEq, Eq)] -pub(crate) enum Refusable { - /// ROUTE_START: assigns the next Route ID to the publisher `node`, covering the - /// prefix with the Prefix ID `prefix` joined with `suffix`, or no prefix when the - /// suffix itself was refused, along `path`, at its own prefix when `exact`. - RouteStart { - node: route::Id, - prefix: u64, - suffix: Option, - exact: bool, - path: Arc<[route::path::Hop]>, - }, - /// ROUTE_UPDATE of the route with this Route ID, now along `path`. - RouteUpdate { id: u64, path: Arc<[route::path::Hop]> }, - /// ANNOUNCE_START under the route with Route ID `route`: assigns the next Announce - /// ID, to `suffix`, or to no path when the path itself was refused. - Start { route: u64, suffix: Option }, - /// ANNOUNCE_UPDATE or ANNOUNCE_RESTART of the claim with this Announce ID. - Change { id: u64 }, -} + /// DEAD (lite-07+): the session `hop` names closed, at the node `far` at its other + /// end, which ends every route the sender offered through it. + Dead { hop: RouteHop, far: NodeId }, + /// DRAIN (lite-07+): `node` sent GOAWAY, so every route through it ranks last. + Drain { node: NodeId }, + /// ANNOUNCE_FLOOR (lite-07+): closes the batch of claim messages since the last + /// one, which change the claims of the node the route with this id leads to, and + /// are complete as of that node's `revision`. + Floor { id: u64, revision: route::Rev }, +} + +/// Route flag: a marked link is on the path, so it is never sent over another. +const MARKED: u8 = 0x1; +/// Route and claim flag: a node on the way sent GOAWAY. +const DRAINING: u8 = 0x2; +/// Claim flag: no route reaches the claim from the node that told it. +const UNAVAILABLE: u8 = 0x1; -/// Why a node refuses content a peer forwarded: well framed, but nothing it can use. -#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] -pub(crate) enum Refusal { - #[error("the reserved node")] - Node, - #[error("unknown flags {0:#04x}")] - Flags(u8), - #[error("{0} hops, past the limit")] - Hops(u64), - #[error("an invalid path")] - Path, - #[error("an invalid instance")] - Instance, - #[error("an invalid lite-06 chain")] - Chain, +fn encode_node(w: &mut Encoder<'_>, node: NodeId) -> Result<(), EncodeError> { + // No node names itself 0. + if node.relay == 0 { + return Err(EncodeError::InvalidState); + } + w.varint(node.relay)?; + w.varint(node.start) } -/// Content a peer forwarded: `Ok(Err(..))` when it is well framed but refused. -type Content = Result, DecodeError>; - -/// Refuse the content at `r`, skipping the rest of its message's body: the refused field -/// is always the body's last, or leaves what follows unreadable. -fn refuse(r: &mut Decoder<'_>, reason: Refusal) -> Content { - r.rest(); - Ok(Err(reason)) +fn decode_node(r: &mut Decoder<'_>) -> Result { + let relay = match r.varint()? { + 0 => return Err(DecodeError::InvalidValue), + relay => relay, + }; + let start = r.varint()?; + Ok(NodeId { relay, start }) } -/// Route Offer flag: an upstream link is on the path, so the route is down-only. -const DOWN_ONLY: u8 = 0x1; -/// Route Offer flag: a node on the path sent GOAWAY. -const DRAINING: u8 = 0x2; - -/// News on DEAD about the routes the sender offered. -#[derive(Clone, Debug, PartialEq, Eq)] -pub(crate) enum Dead { - /// CLOSED: the session `hop` names closed, at the node `far` at its other end, which - /// the receiver doubts. The next `moved` messages on the stream are ROUTE_UPDATEs, - /// one per route on the stream through that hop that continues on another path, and - /// the receiver applies the DEAD and all of them as one. The rest end if `gone`, and - /// are held if not, unless the receiver is the node just before the sender on the - /// route's path. - Closed { - hop: route::path::Hop, - far: route::Id, - gone: bool, - moved: u64, - }, - /// DRAIN: `node` sent GOAWAY, so every path through it ranks as draining. - Drain { node: route::Id }, +fn encode_hop(w: &mut Encoder<'_>, hop: RouteHop) -> Result<(), EncodeError> { + encode_node(w, hop.node)?; + w.varint(hop.session) } -/// A Route Offer off the wire: usable, or refused with the path it still names. -pub(crate) type Heard = Result; - -/// A Route Offer this node refuses: the path it names, which a later DEAD may match, and -/// why. The offer is the route's, so refusing it refuses the route, not the session. -#[derive(Clone, Debug, PartialEq, Eq)] -pub(crate) struct Unusable { - pub path: Arc<[route::path::Hop]>, - pub reason: Refusal, +fn decode_hop(r: &mut Decoder<'_>) -> Result { + Ok(RouteHop { + node: decode_node(r)?, + session: r.varint()?, + }) } -/// Encode a Route Offer: the path, then flags, priority, and metric. No node offers an -/// empty path or one past the hop limit, nor names Node ID 0 in one. +/// Encode a route: the path, then flags, priority, and cost. No node offers an empty +/// path or one past the hop limit. fn encode_offer(w: &mut Encoder<'_>, offer: &Offer) -> Result<(), EncodeError> { - if offer.path.is_empty() || offer.path.len() > route::MAX_HOPS || offer.path.iter().any(|hop| hop.node == 0) { + if offer.path.is_empty() || offer.path.len() > route::MAX_HOPS { return Err(EncodeError::InvalidState); } w.varint(offer.path.len() as u64)?; for hop in offer.path.iter() { - w.varint(hop.node)?; - w.varint(hop.session)?; + encode_hop(w, *hop)?; } let mut flags = 0; - if offer.down_only { - flags |= DOWN_ONLY; + if offer.marked { + flags |= MARKED; } if offer.draining { flags |= DRAINING; } w.u8(flags); w.u8(super::priority::to_wire(offer.priority)); - w.varint(offer.metric)?; - Ok(()) + w.varint(offer.cost) } -/// Decode a Route Offer. Its path comes first, so a node refusing the rest still learns -/// the path a later DEAD may name. An empty path is the sender's own error, since it -/// names itself last. Hops past the limit, a hop naming Node ID 0, and a flag this node -/// does not know refuse the offer: a newer flag may change what the route means, or add -/// a field after the ones this node reads, so a refusal skips the rest of `r`. Every hop -/// a path counts is read, but of a path past the limit only those up to one past it are -/// kept: a sender within the limit never sends one, and the whole would hold a body's -/// worth for each route. -fn decode_offer(r: &mut Decoder<'_>) -> Result { +/// Decode a route. A sender within the limit never sends an empty path, one past the +/// hop limit, or a flag this version does not define, so each fails the stream. +fn decode_offer(r: &mut Decoder<'_>) -> Result { let count = r.varint()?; if count == 0 { return Err(DecodeError::InvalidValue); } - // Each hop takes at least two bytes, so the body bounds the count. - if count > (r.remaining() / 2) as u64 { - return Err(DecodeError::TooMany); - } - let mut path = Vec::with_capacity(count.min(route::MAX_HOPS as u64 + 1) as usize); - for index in 0..count { - let hop = route::path::Hop { - node: r.varint()?, - session: r.varint()?, - }; - if index <= route::MAX_HOPS as u64 { - path.push(hop); - } - } - let path: Arc<[route::path::Hop]> = path.into(); - let refuse = |r: &mut Decoder<'_>, reason| { - r.rest(); - Ok(Err(Unusable { - path: path.clone(), - reason, - })) - }; if count > route::MAX_HOPS as u64 { - return refuse(r, Refusal::Hops(count)); - } - if path.iter().any(|hop| hop.node == 0) { - return refuse(r, Refusal::Node); + return Err(DecodeError::TooMany); } + let path = (0..count).map(|_| decode_hop(r)).collect::, _>>()?; let flags = r.u8()?; - if flags & !(DOWN_ONLY | DRAINING) != 0 { - return refuse(r, Refusal::Flags(flags)); + if flags & !(MARKED | DRAINING) != 0 { + return Err(DecodeError::InvalidValue); } let priority = super::priority::from_wire(r.u8()?); - let metric = r.varint()?; - Ok(Ok(Offer { - path, + let cost = r.varint()?; + Ok(Offer { + path: path.into(), draining: flags & DRAINING != 0, priority, - metric, - down_only: flags & DOWN_ONLY != 0, - })) + cost, + marked: flags & MARKED != 0, + }) } -fn encode_dead(w: &mut Encoder<'_>, dead: &Dead) -> Result<(), EncodeError> { - match dead { - Dead::Closed { hop, far, gone, moved } => { - if hop.node == 0 || *far == 0 { - return Err(EncodeError::InvalidState); - } - w.varint(DEAD_CLOSED)?; - w.varint(hop.node)?; - w.varint(hop.session)?; - w.varint(*far)?; - w.u8(u8::from(*gone)); - w.varint(*moved)?; - } - Dead::Drain { node } => { - if *node == 0 { - return Err(EncodeError::InvalidState); - } - w.varint(DEAD_DRAIN)?; - w.varint(*node)?; - } +/// Encode what a claim says after its epoch: cost, above, flags, priority, and chain. +fn encode_claim(w: &mut Encoder<'_>, claim: &Claim) -> Result<(), EncodeError> { + if claim.chain.len() > route::MAX_HOPS || claim.chain.contains(&0) { + return Err(EncodeError::InvalidState); + } + w.varint(claim.cost)?; + w.varint(claim.item.above)?; + let mut flags = 0; + if claim.unavailable { + flags |= UNAVAILABLE; + } + if claim.draining { + flags |= DRAINING; + } + w.u8(flags); + w.u8(super::priority::to_wire(claim.priority)); + w.varint(claim.chain.len() as u64)?; + for relay in &claim.chain { + w.varint(*relay)?; } Ok(()) } -fn decode_dead(r: &mut Decoder<'_>) -> Result { - let nonzero = |id: u64| match id { - 0 => Err(DecodeError::InvalidValue), - id => Ok(id), - }; - Ok(match r.varint()? { - DEAD_CLOSED => { - let hop = route::path::Hop { - node: nonzero(r.varint()?)?, - session: r.varint()?, - }; - let far = nonzero(r.varint()?)?; - let gone = match r.u8()? { - 0 => false, - 1 => true, - _ => return Err(DecodeError::InvalidValue), - }; - let moved = r.varint()?; - Dead::Closed { hop, far, gone, moved } - } - DEAD_DRAIN => Dead::Drain { - node: nonzero(r.varint()?)?, - }, - _ => return Err(DecodeError::InvalidValue), - }) -} - -/// Decode a path a peer forwarded, refusing one this node cannot hold. -fn decode_path(r: &mut Decoder<'_>) -> Content { - let bytes = r.bytes()?; - let path = std::str::from_utf8(bytes).ok().and_then(|path| Path::new(path).ok()); - Ok(path.filter(|path| path.depth() <= Path::MAX_PARTS).ok_or(Refusal::Path)) -} - -/// Decode a lite-07 suffix: its base, which the sender picked, then the path it forwarded. -fn decode_suffix(r: &mut Decoder<'_>) -> Content { - let base = r.varint()?; - let keep = r.varint()?; - if base == 0 && keep != 0 { +/// Decode a claim of `epoch`. A chain past the hop limit, or naming Node ID 0, and a +/// flag this version does not define fail the stream: no sender builds one. +fn decode_claim(r: &mut Decoder<'_>, epoch: u64) -> Result { + let cost = r.varint()?; + let above = r.varint()?; + let flags = r.u8()?; + if flags & !(UNAVAILABLE | DRAINING) != 0 { return Err(DecodeError::InvalidValue); } - Ok(decode_path(r)?.map(|rest| PathRef { base, keep, rest })) -} - -/// Encode an ANNOUNCE_FLOOR body: the revision plus one, or 0 to certify nothing. -fn encode_floor(w: &mut Encoder<'_>, id: u64, revision: Option) -> Result<(), EncodeError> { - w.varint(id)?; - match revision { - // No origin advertises the last revision, so it never needs the offset. - Some(route::MAX) => Err(EncodeError::InvalidState), - Some(rev) => w.varint(rev + 1), - None => w.varint(0), + let priority = super::priority::from_wire(r.u8()?); + let count = r.varint()?; + if count > route::MAX_HOPS as u64 { + return Err(DecodeError::TooMany); } -} - -fn decode_floor(r: &mut Decoder<'_>) -> Result { - let id = r.varint()?; - let revision = r.varint()?.checked_sub(1); - Ok(AnnounceBroadcast::Floor { id, revision }) + let mut chain = Vec::with_capacity(count as usize); + for _ in 0..count { + match r.varint()? { + 0 => return Err(DecodeError::InvalidValue), + relay => chain.push(relay), + } + } + Ok(Claim { + item: Attr { epoch, above }, + cost, + priority, + draining: flags & DRAINING != 0, + unavailable: flags & UNAVAILABLE != 0, + chain, + }) } /// Encode an ANNOUNCE_REQUEST body: the Prefix IDs to remove, then the prefixes to add, @@ -584,150 +426,6 @@ impl<'a> Decode<'a, Version> for PathRef { } } -/// Encode the Route ID a claim travels under, which only lite-07 carries. -fn encode_route_id(w: &mut Encoder<'_>, version: Version, route: u64) -> Result<(), EncodeError> { - match version.has_routes() { - true => w.varint(route), - false if route != 0 => Err(EncodeError::InvalidState), - false => Ok(()), - } -} - -/// Encode a lite-06 announcement's hop chain, which lite-07 replaces with its Route ID. -fn encode_chain(w: &mut Encoder<'_>, version: Version, hops: &Hops) -> Result<(), EncodeError> { - match version.has_routes() { - true => Ok(()), - false => hops.encode(w, version), - } -} - -/// Decode a lite-06 announcement's hop chain, empty on lite-07. -fn decode_chain(r: &mut Decoder<'_>, version: Version) -> Result { - match version.has_routes() { - true => Ok(Hops::default()), - false => Hops::decode(r, version), - } -} - -/// Encode lite-07's Above, which no earlier version has: a claim there is only ever at the -/// path it names. -fn encode_above(w: &mut Encoder<'_>, version: Version, above: u64) -> Result<(), EncodeError> { - if version.has_routes() { - return w.varint(above); - } - match above { - 0 => Ok(()), - _ => Err(EncodeError::InvalidState), - } -} - -/// Encode lite-07's trailing Lite-06 Chain, omitted when empty. A lite-06 compatibility -/// field: the lite-07 to lite-08 migration deletes it. -fn encode_lite06_chain(w: &mut Encoder<'_>, version: Version, hops: &Hops) -> Result<(), EncodeError> { - match version.has_routes() && !hops.is_empty() { - true => hops.encode(w, version), - false => Ok(()), - } -} - -/// A lite-07 ANNOUNCE_START: its suffix, Route ID, instance and epoch, cost, and -/// Lite-06 Chain. -fn decode_start(r: &mut Decoder<'_>, version: Version) -> Result { - let suffix = decode_suffix(r)?; - let route = r.varint()?; - let refused = |r: &mut Decoder<'_>, suffix, reason| { - r.rest(); - AnnounceBroadcast::Refused { - message: Refusable::Start { route, suffix }, - reason, - } - }; - let suffix = match suffix { - Ok(suffix) => suffix, - Err(reason) => return Ok(refused(r, None, reason)), - }; - let Some((instance, epoch)) = super::instance::decode_forwarded(r)? else { - return Ok(refused(r, Some(suffix), Refusal::Instance)); - }; - let cost = Cost::decode(r, version)?; - let above = r.varint()?; - Ok(match decode_lite06_chain(r)? { - Ok(hops) => AnnounceBroadcast::Active { - suffix, - route, - instance: Some(instance), - epoch, - hops, - cost, - above, - }, - Err(reason) => refused(r, Some(suffix), reason), - }) -} - -/// A lite-07 ANNOUNCE_UPDATE: its Announce ID, cost, and Lite-06 Chain. -fn decode_update(r: &mut Decoder<'_>, version: Version) -> Result { - let id = r.varint()?; - let cost = Cost::decode(r, version)?; - let above = r.varint()?; - Ok(match decode_lite06_chain(r)? { - Ok(hops) => AnnounceBroadcast::Update { id, hops, cost, above }, - Err(reason) => AnnounceBroadcast::Refused { - message: Refusable::Change { id }, - reason, - }, - }) -} - -/// A lite-07 ANNOUNCE_RESTART: its Announce ID, instance and epoch, cost, and Lite-06 -/// Chain. -fn decode_restart(r: &mut Decoder<'_>, version: Version) -> Result { - let id = r.varint()?; - let refused = |r: &mut Decoder<'_>, reason| { - r.rest(); - AnnounceBroadcast::Refused { - message: Refusable::Change { id }, - reason, - } - }; - let Some((instance, epoch)) = super::instance::decode_forwarded(r)? else { - return Ok(refused(r, Refusal::Instance)); - }; - let cost = Cost::decode(r, version)?; - let above = r.varint()?; - Ok(match decode_lite06_chain(r)? { - Ok(hops) => AnnounceBroadcast::Restart { - id, - instance: Some(instance), - epoch, - cost, - hops, - above, - }, - Err(reason) => refused(r, reason), - }) -} - -/// Decode lite-07's trailing Lite-06 Chain: present only when bytes remain, and never -/// empty, so an empty chain has one spelling. A chain no lite-06 relay would send, empty, -/// too long, or revisiting a hop, is refused. -fn decode_lite06_chain(r: &mut Decoder<'_>) -> Content { - if r.is_empty() { - return Ok(Ok(Hops::default())); - } - let count = r.varint()?; - if count == 0 { - return refuse(r, Refusal::Chain); - } - let mut hops = Hops::new(); - for _ in 0..count { - if hops.push(Hop::decode(r, ())?).is_err() { - return refuse(r, Refusal::Chain); - } - } - Ok(Ok(hops)) -} - impl Encode for Cost { fn encode(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { if !version.has_route_cost() { @@ -764,104 +462,76 @@ impl Encode for AnnounceBroadcast { if version.has_announce_id() { // Lite06+: outer type discriminator, then a size-prefixed body (like the // subscribe stream). The body varies by type. + let routes = version.has_routes(); let typ = match self { - Self::Active { .. } => ANNOUNCE_START, - Self::Synced { .. } if version.has_synced() => SYNCED, - Self::Request { .. } if version.has_routes() => ANNOUNCE_REQUEST, - Self::Ok if version.has_routes() => ANNOUNCE_OK, - Self::Synced { .. } | Self::Request { .. } | Self::Ok => return Err(EncodeError::Version), - Self::EndedId { .. } => ANNOUNCE_END, - Self::Update { .. } => ANNOUNCE_UPDATE, - Self::Restart { .. } if version.has_announce_restart() => ANNOUNCE_RESTART, - // Older versions send an end and a start instead. - Self::Restart { .. } => return Err(EncodeError::Version), - Self::RouteStart { .. } if version.has_routes() => ROUTE_START, - Self::RouteUpdate { .. } if version.has_routes() => ROUTE_UPDATE, - Self::RouteHold { .. } if version.has_routes() => ROUTE_HOLD, - Self::RouteEnd { .. } if version.has_routes() => ROUTE_END, - Self::Dead(_) if version.has_routes() => DEAD, - Self::Floor { .. } if version.has_routes() => ANNOUNCE_FLOOR, - Self::RouteStart { .. } - | Self::RouteUpdate { .. } - | Self::RouteHold { .. } - | Self::RouteEnd { .. } - | Self::Dead(_) - | Self::Floor { .. } => return Err(EncodeError::Version), + // Each version has its own form of a claim. + Self::Active { .. } if !routes => ANNOUNCE_START, + Self::EndedId { .. } if !routes => ANNOUNCE_END, + Self::Update { .. } if !routes => ANNOUNCE_UPDATE, + Self::Start { .. } if routes => ANNOUNCE_START, + Self::End { .. } if routes => ANNOUNCE_END, + Self::Change { .. } if routes => ANNOUNCE_UPDATE, + Self::Synced { .. } if routes => SYNCED, + Self::Request { .. } if routes => ANNOUNCE_REQUEST, + Self::Ok if routes => ANNOUNCE_OK, + Self::Route { .. } if routes => ROUTE, + Self::RouteEnd { .. } if routes => ROUTE_END, + Self::Dead { .. } if routes => DEAD, + Self::Drain { .. } if routes => DRAIN, + Self::Floor { .. } if routes => ANNOUNCE_FLOOR, + // Decode-only: an unknown type is never sent. + Self::Skipped => return Err(EncodeError::Unsupported), // The pre-lite-06 path-form retraction has no place on lite-06. - Self::Ended { .. } => return Err(EncodeError::Version), - // Decode-only: an unknown type or refused content is never sent. - Self::Skipped | Self::Refused { .. } => return Err(EncodeError::Unsupported), + _ => return Err(EncodeError::Version), }; w.varint(typ)?; let prefix = w.prefix_varint(); match self { - Self::Active { - suffix, - route, - instance, - epoch, - hops, - cost, - above, - } => { + Self::Active { suffix, hops, cost } => { suffix.encode(w, version)?; - encode_route_id(w, version, *route)?; - super::instance::encode_route(w, version, instance.as_ref(), *epoch)?; - encode_chain(w, version, hops)?; + hops.encode(w, version)?; cost.encode(w, version)?; - encode_above(w, version, *above)?; - encode_lite06_chain(w, version, hops)?; } Self::EndedId { id } => w.varint(*id)?, - Self::Update { id, hops, cost, above } => { + Self::Update { id, hops, cost } => { w.varint(*id)?; - encode_chain(w, version, hops)?; + hops.encode(w, version)?; cost.encode(w, version)?; - encode_above(w, version, *above)?; - encode_lite06_chain(w, version, hops)?; } - Self::Restart { - id, + Self::Start { + suffix, instance, - epoch, - cost, - hops, - above, + claim, } => { + suffix.encode(w, version)?; + super::instance::encode_route(w, version, Some(instance), claim.item.epoch)?; + encode_claim(w, claim)?; + } + Self::Change { id, claim } => { w.varint(*id)?; - super::instance::encode_route(w, version, instance.as_ref(), *epoch)?; - cost.encode(w, version)?; - encode_above(w, version, *above)?; - encode_lite06_chain(w, version, hops)?; + w.varint(claim.item.epoch)?; + encode_claim(w, claim)?; + } + Self::End { id, lost } => { + w.varint(*id)?; + w.bool(*lost); } Self::Synced { prefix } => w.varint(*prefix)?, Self::Request { more, remove, add } => encode_request(w, version, *more, remove, add)?, Self::Ok => {} - Self::RouteStart { - node, - prefix, - suffix, - exact, - offer, - } => { - if *node == 0 { - return Err(EncodeError::InvalidState); - } - w.varint(*node)?; - w.varint(*prefix)?; - suffix.encode(w, version)?; - w.bool(*exact); - encode_offer(w, offer)?; + Self::Route { offer } => encode_offer(w, offer)?, + Self::RouteEnd { id } => w.varint(*id)?, + Self::Dead { hop, far } => { + encode_hop(w, *hop)?; + encode_node(w, *far)?; } - Self::RouteUpdate { id, offer } => { + Self::Drain { node } => encode_node(w, *node)?, + Self::Floor { id, revision } => { w.varint(*id)?; - encode_offer(w, offer)?; + w.varint(*revision)?; } - Self::RouteHold { id } | Self::RouteEnd { id } => w.varint(*id)?, - Self::Dead(dead) => encode_dead(w, dead)?, - Self::Floor { id, revision } => encode_floor(w, *id, *revision)?, - Self::Ended { .. } | Self::Skipped | Self::Refused { .. } => unreachable!("refused above"), + Self::Ended { .. } | Self::Skipped => unreachable!("refused above"), } if w.since(&prefix) > MAX_MESSAGE_SIZE { w.discard(prefix); @@ -875,16 +545,11 @@ impl Encode for AnnounceBroadcast { let prefix = w.prefix_varint(); match self { // The cost is a lite-06 addition, so it is simply not on the wire here. - Self::Active { - suffix, route, hops, .. - } => { + Self::Active { suffix, hops, .. } => { // Bases are a lite-07 addition; PathRef refuses one here. if suffix.base != 0 { return Err(EncodeError::Version); } - if *route != 0 { - return Err(EncodeError::InvalidState); - } AnnounceStatus::Active.encode(w, version)?; suffix.rest.encode(w, version)?; encode_hops(w, version, hops)?; @@ -894,23 +559,8 @@ impl Encode for AnnounceBroadcast { suffix.encode(w, version)?; encode_hops(w, version, hops)?; } - // The id-referencing forms only exist on lite-06+. - Self::EndedId { .. } - | Self::Update { .. } - | Self::Restart { .. } - | Self::Skipped - | Self::Refused { .. } - | Self::Synced { .. } - | Self::Request { .. } - | Self::Ok - | Self::RouteStart { .. } - | Self::RouteUpdate { .. } - | Self::RouteHold { .. } - | Self::RouteEnd { .. } - | Self::Dead(_) - | Self::Floor { .. } => { - return Err(EncodeError::Version); - } + // Everything else only exists on lite-06+. + _ => return Err(EncodeError::Version), } if w.since(&prefix) > MAX_MESSAGE_SIZE { w.discard(prefix); @@ -930,89 +580,61 @@ impl<'a> Decode<'a, Version> for AnnounceBroadcast { let mut body = buf.sub(size)?; let msg = (|| { Ok(match typ { - // Lite-07's claims and routes carry what the sender forwarded, which - // a receiver may refuse without failing the stream. - ANNOUNCE_START if version.has_routes() => decode_start(&mut body, version)?, - ANNOUNCE_UPDATE if version.has_routes() => decode_update(&mut body, version)?, - ANNOUNCE_RESTART if version.has_announce_restart() => decode_restart(&mut body, version)?, - ANNOUNCE_START => { + ANNOUNCE_START if version.has_routes() => { let suffix = PathRef::decode(&mut body, version)?; - let hops = decode_chain(&mut body, version)?; - let cost = Cost::decode(&mut body, version)?; - Self::Active { + // Every lite-07 claim names its instance. + let (instance, epoch) = + super::instance::decode_forwarded(&mut body)?.ok_or(DecodeError::InvalidValue)?; + let claim = decode_claim(&mut body, epoch)?; + Self::Start { suffix, - route: 0, - instance: None, - epoch: 0, - hops, - cost, - above: 0, + instance, + claim, } } - SYNCED if version.has_synced() => Self::Synced { prefix: body.varint()? }, - ANNOUNCE_REQUEST if version.has_routes() => decode_request(&mut body, version)?, - ANNOUNCE_OK if version.has_routes() => Self::Ok, - ANNOUNCE_END => Self::EndedId { id: body.varint()? }, - ANNOUNCE_UPDATE => { + ANNOUNCE_UPDATE if version.has_routes() => { let id = body.varint()?; - let hops = decode_chain(&mut body, version)?; - let cost = Cost::decode(&mut body, version)?; - Self::Update { + let epoch = body.varint()?; + Self::Change { id, - hops, - cost, - above: 0, + claim: decode_claim(&mut body, epoch)?, } } - ROUTE_START if version.has_routes() => { - let node = body.varint()?; - let prefix = body.varint()?; - // The suffix is sized, so the offer's path reads even when it is refused. - let suffix = decode_path(&mut body)?; - let exact = body.bool()?; - let heard = decode_offer(&mut body)?; - match (node, suffix, heard) { - (1.., Ok(suffix), Ok(offer)) => Self::RouteStart { - node, - prefix, - suffix, - exact, - offer, - }, - (node, suffix, heard) => { - let reason = match (node, &suffix, &heard) { - (0, _, _) => Refusal::Node, - (_, Err(reason), _) => *reason, - (_, _, Err(unusable)) => unusable.reason, - _ => unreachable!("matched above"), - }; - Self::Refused { - message: Refusable::RouteStart { - node, - prefix, - suffix: suffix.ok(), - exact, - path: heard.map_or_else(|unusable| unusable.path, |offer| offer.path), - }, - reason, - } - } - } + ANNOUNCE_END if version.has_routes() => Self::End { + id: body.varint()?, + lost: body.bool()?, + }, + ANNOUNCE_START => { + let suffix = PathRef::decode(&mut body, version)?; + let hops = Hops::decode(&mut body, version)?; + let cost = Cost::decode(&mut body, version)?; + Self::Active { suffix, hops, cost } } - ROUTE_UPDATE if version.has_routes() => { + ANNOUNCE_END => Self::EndedId { id: body.varint()? }, + ANNOUNCE_UPDATE => { let id = body.varint()?; - match decode_offer(&mut body)? { - Ok(offer) => Self::RouteUpdate { id, offer }, - Err(Unusable { path, reason }) => Self::Refused { - message: Refusable::RouteUpdate { id, path }, - reason, - }, - } + let hops = Hops::decode(&mut body, version)?; + let cost = Cost::decode(&mut body, version)?; + Self::Update { id, hops, cost } } - DEAD if version.has_routes() => Self::Dead(decode_dead(&mut body)?), - ROUTE_HOLD if version.has_routes() => Self::RouteHold { id: body.varint()? }, + SYNCED if version.has_routes() => Self::Synced { prefix: body.varint()? }, + ANNOUNCE_REQUEST if version.has_routes() => decode_request(&mut body, version)?, + ANNOUNCE_OK if version.has_routes() => Self::Ok, + ROUTE if version.has_routes() => Self::Route { + offer: decode_offer(&mut body)?, + }, ROUTE_END if version.has_routes() => Self::RouteEnd { id: body.varint()? }, - ANNOUNCE_FLOOR if version.has_routes() => decode_floor(&mut body)?, + DEAD if version.has_routes() => Self::Dead { + hop: decode_hop(&mut body)?, + far: decode_node(&mut body)?, + }, + DRAIN if version.has_routes() => Self::Drain { + node: decode_node(&mut body)?, + }, + ANNOUNCE_FLOOR if version.has_routes() => Self::Floor { + id: body.varint()?, + revision: body.varint()?, + }, // Unknown types are skipped by length so an earlier Lite06 build // negotiating the same ALPN does not kill the announce stream. _ => { @@ -1063,12 +685,8 @@ impl AnnounceBroadcast { Ok(match status { AnnounceStatus::Active => Self::Active { suffix: PathRef::literal(suffix), - route: 0, - instance: None, - epoch: 0, hops, cost: Cost::UNKNOWN, - above: 0, }, AnnounceStatus::Ended => Self::Ended { suffix, hops }, // On lite-05 an update travels as a duplicate ANNOUNCE (a second `Active`), so accept @@ -1078,12 +696,8 @@ impl AnnounceBroadcast { // invalid value there. AnnounceStatus::Update if update_supported(version) => Self::Active { suffix: PathRef::literal(suffix), - route: 0, - instance: None, - epoch: 0, hops, cost: Cost::UNKNOWN, - above: 0, }, AnnounceStatus::Update => return Err(DecodeError::InvalidValue), }) @@ -1277,1291 +891,3 @@ impl<'a> Message<'a> for AnnounceOk { } } -#[cfg(test)] -mod tests { - use super::*; - use bytes::Buf; - - /// A legacy initial set is held to the announce budget before any suffix decodes, so - /// a body of one-byte suffixes cannot become a list many times its size. - #[test] - fn announce_init_is_bounded_by_the_budget() { - let version = Version::Lite02; - let mut wire = Vec::new(); - AnnounceInit { - suffixes: vec![Path::from_static("a"), Path::from_static(""), Path::from_static("b")], - } - .encode(&mut Encoder::new(&mut wire, version.into()), version) - .unwrap(); - let decode = - |wire: &[u8], budget| AnnounceInit::decode_within(&mut Decoder::new(wire, version.into()), version, budget); - - assert_eq!(decode(&wire, 3).unwrap().suffixes.len(), 3); - assert!(matches!(decode(&wire, 2), Err(DecodeError::TooMany))); - - // A count the body cannot hold is malformed whatever the budget, before any allocation. - let mut wire = Vec::new(); - let mut w = Encoder::new(&mut wire, version.into()); - w.varint(8).unwrap(); - w.varint(u32::MAX.into()).unwrap(); - assert!(matches!(decode(&wire, usize::MAX), Err(DecodeError::InvalidValue))); - } - - /// Lite-06's ANNOUNCE_OK opens the stream with a Hop ID and a count. Lite-07's is a - /// message of the Announce Stream instead: SYNCED replaces the count, and routes - /// the Hop ID. - #[test] - fn announce_ok_opens_only_a_lite06_stream() { - let ok = AnnounceOk { - origin: Hop::new(1).unwrap(), - active: 7, - }; - let mut bytes = Vec::new(); - ok.encode(&mut Encoder::new(&mut bytes, Version::Lite06.into()), Version::Lite06) - .unwrap(); - assert_eq!(bytes, [2, 1, 7]); - let res = ok.encode( - &mut Encoder::new(&mut Vec::new(), Version::Lite07.into()), - Version::Lite07, - ); - assert!(matches!(res, Err(EncodeError::Version))); - assert!(crate::coding::decode_buf(&mut &[0u8][..], Version::Lite07, |r, v| AnnounceOk::decode(r, v)).is_err()); - } - - #[test] - fn synced_marks_lite07s_initial_set() { - let mut bytes = Vec::new(); - AnnounceBroadcast::Synced { prefix: 3 } - .encode(&mut Encoder::new(&mut bytes, Version::Lite07.into()), Version::Lite07) - .unwrap(); - assert_eq!(bytes, vec![4, 1, 3]); - assert!(matches!( - AnnounceBroadcast::Synced { prefix: 3 }.encode( - &mut Encoder::new(&mut Vec::new(), Version::Lite06.into()), - Version::Lite06 - ), - Err(EncodeError::Version) - )); - } - - // Forge an ANNOUNCE_BROADCAST with the draft's explicit `restart` status (2) for the given version. - fn encode_forged_restart(version: Version) -> bytes::Bytes { - // Encode a normal Active, then flip its status byte (1 -> 2). - let mut buf = Vec::new(); - AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("foo/bar")), - hops: Hops::default(), - cost: Cost::default(), - above: 0, - } - .encode(&mut Encoder::new(&mut buf, version.into()), version) - .expect("encode"); - - // Layout: <...>. The message is small, so the size is one byte and - // the status byte sits at index 1. - assert_eq!( - buf[1], - u8::from(AnnounceStatus::Active), - "expected an Active status byte" - ); - buf[1] = u8::from(AnnounceStatus::Update); - bytes::Bytes::from(buf) - } - - // On lite-05+ the explicit `restart` status is accepted and surfaced as an `Active` (the - // subscriber retires an already-announced path before republishing it). - #[test] - fn decodes_explicit_restart_status_as_active_on_lite05() { - let version = Version::Lite05; - let mut slice = encode_forged_restart(version); - let decoded = crate::coding::decode_buf(&mut slice, version, |r, v| AnnounceBroadcast::decode(r, v)) - .expect("explicit restart must decode"); - assert!(!slice.has_remaining(), "trailing bytes after decode"); - assert!( - matches!(decoded, AnnounceBroadcast::Active { .. }), - "restart should decode as Active" - ); - } - - // Older versions never defined the restart status, so it's an invalid value there. - #[test] - fn rejects_explicit_restart_status_before_lite05() { - let version = Version::Lite04; - let mut slice = encode_forged_restart(version); - assert!( - matches!( - crate::coding::decode_buf(&mut slice, version, |r, v| AnnounceBroadcast::decode(r, v)), - Err(DecodeError::InvalidValue) - ), - "restart status must be rejected before lite-05" - ); - } - - fn round_trip(msg: &AnnounceOk) -> AnnounceOk { - let mut buf = Vec::new(); - msg.encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05) - .unwrap(); - let mut slice = &buf[..]; - let got = crate::coding::decode_buf(&mut slice, Version::Lite05, |r, v| AnnounceOk::decode(r, v)).unwrap(); - assert!(slice.is_empty(), "trailing bytes after decode"); - got - } - - #[test] - fn announce_ok_round_trip() { - let msg = AnnounceOk { - origin: Hop::new(42).unwrap(), - active: 3, - }; - assert_eq!(round_trip(&msg), msg); - } - - #[test] - fn announce_ok_zero_active() { - let msg = AnnounceOk { - origin: Hop::new(7).unwrap(), - active: 0, - }; - assert_eq!(round_trip(&msg), msg); - } - - fn broadcast_round_trip(msg: &AnnounceBroadcast, version: Version) -> AnnounceBroadcast { - let mut buf = Vec::new(); - msg.encode(&mut Encoder::new(&mut buf, version.into()), version) - .unwrap(); - let mut slice = &buf[..]; - let got = crate::coding::decode_buf(&mut slice, version, |r, v| AnnounceBroadcast::decode(r, v)).unwrap(); - assert!(slice.is_empty(), "trailing bytes after decode"); - got - } - - #[test] - fn announce_broadcast_round_trip_on_lite05() { - let mut hops = Hops::new(); - hops.push(Hop::new(7).unwrap()).unwrap(); - let msg = AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("room/cam")), - hops: hops.clone(), - cost: Cost::UNKNOWN, - above: 0, - }; - assert_eq!(broadcast_round_trip(&msg, Version::Lite05), msg); - - let ended = AnnounceBroadcast::Ended { - suffix: Path::from_static("room/cam"), - hops: Hops::new(), - }; - assert_eq!(broadcast_round_trip(&ended, Version::Lite05), ended); - } - - #[test] - fn announce_broadcast_round_trip_on_lite06() { - let mut hops = Hops::new(); - hops.push(Hop::new(7).unwrap()).unwrap(); - - // A priced start and update preserve the same static value. - let cost = Cost::new(12); - - let active = AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("room/cam")), - hops: hops.clone(), - cost, - above: 0, - }; - assert_eq!(broadcast_round_trip(&active, Version::Lite06), active); - - let ended = AnnounceBroadcast::EndedId { id: 3 }; - assert_eq!(broadcast_round_trip(&ended, Version::Lite06), ended); - - let update = AnnounceBroadcast::Update { - id: 3, - hops, - cost, - above: 0, - }; - assert_eq!(broadcast_round_trip(&update, Version::Lite06), update); - } - - // Lite07 carries the path base as it travels, and the route in place of a hop chain; - // the codec resolves nothing. - #[test] - fn announce_broadcast_round_trip_on_lite07() { - let cost = Cost::new(12); - - let active = AnnounceBroadcast::Active { - route: 4, - instance: Some("a".parse().unwrap()), - epoch: 5, - suffix: PathRef { - base: 2, - keep: 3, - rest: Path::from_static("cam"), - }, - hops: Hops::default(), - cost, - above: 0, - }; - assert_eq!(broadcast_round_trip(&active, Version::Lite07), active); - - let update = AnnounceBroadcast::Update { - id: 3, - hops: Hops::default(), - cost, - above: 0, - }; - assert_eq!(broadcast_round_trip(&update, Version::Lite07), update); - - for epoch in [0, 9] { - let restart = AnnounceBroadcast::Restart { - id: 3, - instance: Some(Instance::random()), - epoch, - cost, - hops: Hops::default(), - above: 0, - }; - assert_eq!(broadcast_round_trip(&restart, Version::Lite07), restart); - } - } - - /// The draft's field order, byte for byte: ANNOUNCE_START is the path base, keep, and - /// rest, the Route ID, the instance, its minted flag, the epoch, and the cost; - /// ANNOUNCE_UPDATE is the Announce ID and the cost. - #[test] - fn lite07_announce_layout() { - let encode = |msg: AnnounceBroadcast| { - let mut buf = Vec::new(); - msg.encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07) - .unwrap(); - buf - }; - let suffix = PathRef { - base: 1, - keep: 2, - rest: Path::from_static("c"), - }; - assert_eq!( - encode(AnnounceBroadcast::Active { - suffix: suffix.clone(), - route: 3, - instance: Some("a".parse().unwrap()), - epoch: 5, - hops: Hops::default(), - cost: Cost::new(6), - above: 0, - }), - [0, 11, 1, 2, 1, b'c', 3, 1, b'a', 0, 5, 6, 0] - ); - assert_eq!( - encode(AnnounceBroadcast::Update { - id: 7, - hops: Hops::default(), - cost: Cost::new(6), - above: 0, - }), - [2, 3, 7, 6, 0] - ); - assert_eq!( - encode(AnnounceBroadcast::Restart { - id: 7, - instance: Some("a".parse().unwrap()), - epoch: 5, - cost: Cost::new(6), - hops: Hops::default(), - above: 0, - }), - [3, 7, 7, 1, b'a', 0, 5, 6, 0] - ); - } - - /// Lite-07 carries a lite-06 chain after the cost, only when it is not empty, so an - /// explicitly empty one is refused. - #[test] - fn lite07_carries_a_trailing_lite06_chain() { - let mut hops = Hops::new(); - hops.push(Hop::new(7).unwrap()).unwrap(); - hops.push(Hop::new(9).unwrap()).unwrap(); - let encode = |msg: &AnnounceBroadcast| { - let mut buf = Vec::new(); - msg.encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07) - .unwrap(); - buf - }; - let msgs = [ - AnnounceBroadcast::Active { - route: 3, - instance: Some("a".parse().unwrap()), - epoch: 5, - suffix: PathRef::literal(Path::from_static("c")), - hops: hops.clone(), - cost: Cost::new(6), - above: 0, - }, - AnnounceBroadcast::Update { - id: 7, - hops: hops.clone(), - cost: Cost::new(6), - above: 0, - }, - AnnounceBroadcast::Restart { - id: 7, - instance: Some("a".parse().unwrap()), - epoch: 5, - cost: Cost::new(6), - hops, - above: 0, - }, - ]; - let layouts: [&[u8]; 3] = [ - &[0, 14, 0, 0, 1, b'c', 3, 1, b'a', 0, 5, 6, 0, 2, 7, 9], - &[2, 6, 7, 6, 0, 2, 7, 9], - &[3, 10, 7, 1, b'a', 0, 5, 6, 0, 2, 7, 9], - ]; - for (msg, layout) in msgs.iter().zip(layouts) { - assert_eq!(encode(msg), layout, "{msg:?}"); - assert_eq!(broadcast_round_trip(msg, Version::Lite07), *msg); - } - - // A chain present but empty, or revisiting a hop, is refused, as is the claim it - // came with. - let decode = |mut wire: &[u8]| { - crate::coding::decode_buf(&mut wire, Version::Lite07, |r, v| AnnounceBroadcast::decode(r, v)).unwrap() - }; - let refused = AnnounceBroadcast::Refused { - message: Refusable::Change { id: 7 }, - reason: Refusal::Chain, - }; - assert_eq!(decode(&[2, 4, 7, 6, 0, 0]), refused); - assert_eq!(decode(&[2, 6, 7, 6, 0, 2, 9, 9]), refused); - assert_eq!(decode(&[3, 10, 7, 1, b'a', 0, 5, 6, 0, 2, 9, 9]), refused); - assert_eq!( - decode(&[0, 14, 0, 0, 1, b'c', 3, 1, b'a', 0, 5, 6, 0, 2, 9, 9]), - AnnounceBroadcast::Refused { - message: Refusable::Start { - route: 3, - suffix: Some(PathRef::literal(Path::from_static("c"))), - }, - reason: Refusal::Chain, - } - ); - } - - /// A claim presented above its path keeps how far above on every message that carries - /// it, and no earlier version can say it. - #[test] - fn an_above_survives_the_wire() { - let msgs = [ - AnnounceBroadcast::Active { - route: 1, - instance: Some("a".parse().unwrap()), - epoch: 0, - suffix: PathRef::literal(Path::empty()), - hops: Hops::default(), - cost: Cost::default(), - above: 2, - }, - AnnounceBroadcast::Update { - id: 1, - hops: Hops::default(), - cost: Cost::default(), - above: 2, - }, - AnnounceBroadcast::Restart { - id: 1, - instance: Some("a".parse().unwrap()), - epoch: 0, - cost: Cost::default(), - hops: Hops::default(), - above: 2, - }, - ]; - for msg in &msgs { - assert_eq!(broadcast_round_trip(msg, Version::Lite07), *msg); - } - let msg = AnnounceBroadcast::Update { - id: 1, - hops: Hops::default(), - cost: Cost::default(), - above: 1, - }; - let mut buf = Vec::new(); - let res = msg.encode(&mut Encoder::new(&mut buf, Version::Lite06.into()), Version::Lite06); - assert!(matches!(res, Err(EncodeError::InvalidState))); - } - - /// Only lite-07 carries a Route ID: a non-zero one has nowhere to go earlier, and an - /// earlier start decodes as route 0. - #[test] - fn a_route_id_needs_lite07() { - let msg = AnnounceBroadcast::Active { - route: 1, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("cam")), - hops: Hops::default(), - cost: Cost::default(), - above: 0, - }; - for version in [Version::Lite05, Version::Lite06] { - let mut buf = Vec::new(); - let res = msg.encode(&mut Encoder::new(&mut buf, version.into()), version); - assert!(matches!(res, Err(EncodeError::InvalidState)), "{version:?}"); - } - } - - // Only lite-07 carries ANNOUNCE_RESTART: older versions send an end and a start, and an - // older receiver skips the unknown type. - #[test] - fn announce_restart_needs_lite07() { - let restart = AnnounceBroadcast::Restart { - id: 1, - instance: Some("a".parse().unwrap()), - epoch: 0, - cost: Cost::default(), - hops: Hops::default(), - above: 0, - }; - let mut buf = Vec::new(); - assert!(matches!( - restart.encode(&mut Encoder::new(&mut buf, Version::Lite06.into()), Version::Lite06), - Err(EncodeError::Version) - )); - - let mut buf = Vec::new(); - restart - .encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07) - .unwrap(); - // The same bytes, read as lite-06: an unknown type, skipped by length. - let mut legacy = Vec::new(); - let mut encoder = Encoder::new(&mut legacy, Version::Lite06.into()); - encoder.varint(ANNOUNCE_RESTART).unwrap(); - encoder.varint(1).unwrap(); - encoder.varint(1).unwrap(); - assert_eq!( - crate::coding::decode_buf(&mut &legacy[..], Version::Lite06, |r, v| AnnounceBroadcast::decode( - r, v - )) - .unwrap(), - AnnounceBroadcast::Skipped - ); - } - - // A keep copies from a base, so one without a base is malformed. - #[test] - fn a_keep_without_a_base_is_rejected() { - // Path base, path keep, empty rest, route, instance "a", epoch, cost. - let body = [0, 1, 0, 0, 1, b'a', 0, 0]; - let mut buf = vec![ANNOUNCE_START as u8, body.len() as u8]; - buf.extend_from_slice(&body); - assert!(matches!( - crate::coding::decode_buf(&mut &buf[..], Version::Lite07, |r, v| AnnounceBroadcast::decode(r, v)), - Err(DecodeError::InvalidValue) - )); - } - - // Only lite-07 has room for a base. - #[test] - fn a_base_needs_lite07() { - let msg = AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef { - base: 1, - keep: 1, - rest: Path::from_static("cam"), - }, - hops: Hops::default(), - cost: Cost::default(), - above: 0, - }; - for version in [Version::Lite05, Version::Lite06] { - let mut buf = Vec::new(); - assert!(matches!( - msg.encode(&mut Encoder::new(&mut buf, version.into()), version), - Err(EncodeError::Version) - )); - } - } - - // The id-referencing forms don't exist before lite-06, and the path form is gone on lite-06. - #[test] - fn announce_broadcast_rejects_cross_version_forms() { - let mut buf = Vec::new(); - assert!(matches!( - AnnounceBroadcast::EndedId { id: 1 } - .encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05), - Err(EncodeError::Version) - )); - assert!(matches!( - AnnounceBroadcast::Update { - id: 1, - hops: Hops::default(), - cost: Cost::default(), - above: 0, - } - .encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05), - Err(EncodeError::Version) - )); - assert!(matches!( - AnnounceBroadcast::Ended { - suffix: Path::from_static("room/cam"), - hops: Hops::new() - } - .encode(&mut Encoder::new(&mut buf, Version::Lite06.into()), Version::Lite06), - Err(EncodeError::Version) - )); - } - - // Pre-lite-06 has no room for a cost on the wire, so one set locally is simply - // not sent and the peer decodes [`Cost::UNKNOWN`]: free to reach, which keeps a - // mixed-version mesh ranking those routes on hop count exactly as it did before, - // with only the arriving link price contributing to route selection. - #[test] - fn route_cost_is_dropped_before_lite06() { - let msg = AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("room/cam")), - hops: Hops::default(), - cost: Cost::new(9), - above: 0, - }; - let got = broadcast_round_trip(&msg, Version::Lite05); - assert_eq!( - got, - AnnounceBroadcast::Active { - route: 0, - instance: None, - epoch: 0, - suffix: PathRef::literal(Path::from_static("room/cam")), - hops: Hops::default(), - cost: Cost::UNKNOWN, - above: 0, - } - ); - } - - // Costs saturate at 2^62-1 on every version, lite-07's 64-bit varints included, so - // charging a link on top of the ceiling still re-encodes for a peer on any version. - #[test] - fn charged_cost_stays_encodable() { - let cost = Cost::new(u64::MAX).charged(1); - assert_eq!(cost, Cost::new((1 << 62) - 1)); - assert_eq!(Cost::MAX.charged(1), cost); - for version in [Version::Lite06, Version::Lite07] { - let buf = cost.encode_bytes(version).expect("a charged cost must stay encodable"); - assert_eq!(Cost::decode_slice(&buf, version).unwrap().0, cost, "{version}"); - } - } - - #[test] - fn unknown_announce_type_is_skipped() { - let mut body = Vec::new(); - Path::from_static("room/cam") - .encode(&mut Encoder::new(&mut body, Version::Lite06.into()), Version::Lite06) - .unwrap(); - Hops::new() - .encode(&mut Encoder::new(&mut body, Version::Lite06.into()), Version::Lite06) - .unwrap(); - Cost::default() - .encode(&mut Encoder::new(&mut body, Version::Lite06.into()), Version::Lite06) - .unwrap(); - - let mut buf = Vec::new(); - Encoder::new(&mut buf, Version::Lite06.into()).varint(4u64).unwrap(); - Encoder::new(&mut buf, Version::Lite06.into()) - .varint(body.len() as u64) - .unwrap(); - buf.extend_from_slice(&body); - - let mut slice = &buf[..]; - let got = crate::coding::decode_buf(&mut slice, Version::Lite06, |r, v| AnnounceBroadcast::decode(r, v)) - .expect("unknown type must not kill the stream"); - assert!(slice.is_empty()); - assert_eq!(got, AnnounceBroadcast::Skipped); - } - - // An ANNOUNCE_END message on lite-06 is tiny: type byte, size prefix, id varint. - #[test] - fn ended_by_id_is_three_bytes() { - let mut buf = Vec::new(); - AnnounceBroadcast::EndedId { id: 42 } - .encode(&mut Encoder::new(&mut buf, Version::Lite06.into()), Version::Lite06) - .unwrap(); - assert_eq!(buf.len(), 3); - } - - /// An edit too large for one message goes in as many as it takes, each fitting one, - /// every one but the last continuing it, and together holding the whole edit in order. - #[test] - fn an_edit_too_large_for_one_message_goes_in_parts() { - const VERSION: Version = Version::Lite07; - // Prefix IDs from 2^28 take five bytes. - let remove: Vec = (0..20_000).map(|i| (1 << 28) + i).collect(); - let add: Vec<(Path, bool)> = (0..3_000) - .map(|i| (Path::new(&format!("{i:0>20}")).unwrap(), i % 2 == 0)) - .collect(); - let parts = request_parts(remove.clone(), add.clone(), VERSION); - assert!(parts.len() > 2, "{}", parts.len()); - let (mut removed, mut added) = (Vec::new(), Vec::new()); - for (i, part) in parts.iter().enumerate() { - let mut bytes = Vec::new(); - part.encode(&mut Encoder::new(&mut bytes, VERSION.into()), VERSION) - .unwrap(); - assert_eq!(broadcast_round_trip(part, VERSION), *part); - let AnnounceBroadcast::Request { more, remove, add } = part else { - panic!("{part:?}"); - }; - assert_eq!(*more, i + 1 < parts.len()); - // The type and the length take at most 4 bytes. - assert!( - bytes.len() <= super::super::message::MAX_MESSAGE_SIZE + 4, - "{}", - bytes.len() - ); - removed.extend(remove.iter().copied()); - added.extend(add.iter().cloned()); - } - assert_eq!((removed, added), (remove, add)); - - // An edit that fits goes whole, and one with nothing in it still goes. - let small = request_parts(vec![1], vec![(Path::from_static("a"), false)], VERSION); - assert!(matches!( - small.as_slice(), - [AnnounceBroadcast::Request { more: false, .. }] - )); - let empty = request_parts(Vec::new(), Vec::new(), VERSION); - assert!(matches!( - empty.as_slice(), - [AnnounceBroadcast::Request { more: false, .. }] - )); - } - - fn request_round_trip(msg: &AnnounceRequest, version: Version) -> AnnounceRequest { - let mut buf = Vec::new(); - msg.encode(&mut Encoder::new(&mut buf, version.into()), version) - .unwrap(); - let mut slice = &buf[..]; - let got = crate::coding::decode_buf(&mut slice, version, |r, v| AnnounceRequest::decode(r, v)).unwrap(); - assert!(slice.is_empty(), "trailing bytes after decode"); - AnnounceRequest { - prefix: got.prefix.clone(), - exclude_hop: got.exclude_hop, - } - } - - // Lite-07 edits a set of prefixes on its one stream instead of opening a stream per - // prefix. - #[test] - fn announce_request_edits_a_set_on_lite07() { - let msg = AnnounceRequest { - prefix: Path::from_static("room"), - exclude_hop: Hop::UNKNOWN, - }; - let res = msg.encode( - &mut Encoder::new(&mut Vec::new(), Version::Lite07.into()), - Version::Lite07, - ); - assert!(matches!(res, Err(EncodeError::Version))); - - let edit = AnnounceBroadcast::Request { - more: true, - remove: vec![0, 7], - add: vec![(Path::from_static("room"), true), (Path::empty(), false)], - }; - assert_eq!(broadcast_round_trip(&edit, Version::Lite07), edit); - let mut bytes = Vec::new(); - edit.encode(&mut Encoder::new(&mut bytes, Version::Lite07.into()), Version::Lite07) - .unwrap(); - // Type, length, More, the removed Prefix IDs, then each added prefix with its Hidden. - assert_eq!(bytes, [&[16, 13, 1, 2, 0, 7, 2, 4][..], b"room", &[1, 0, 0]].concat()); - assert_eq!( - broadcast_round_trip(&AnnounceBroadcast::Ok, Version::Lite07), - AnnounceBroadcast::Ok - ); - for msg in [edit, AnnounceBroadcast::Ok] { - let res = msg.encode( - &mut Encoder::new(&mut Vec::new(), Version::Lite06.into()), - Version::Lite06, - ); - assert!(matches!(res, Err(EncodeError::Version)), "{msg:?}"); - } - } - - // A flag byte other than 0 or 1 is malformed, not a future extension, and a count - // the body cannot hold is refused before anything is allocated for it. - #[test] - fn announce_request_rejects_a_bad_body() { - let bad_flag = forge(ANNOUNCE_REQUEST, |w| { - w.varint(0u64).unwrap(); - w.varint(1u64).unwrap(); - Path::from_static("room").encode(w, Version::Lite07).unwrap(); - w.u8(2); - }); - assert!(decode_broadcast(&bad_flag, Version::Lite07).is_err()); - let overcount = forge(ANNOUNCE_REQUEST, |w| { - w.varint(u64::MAX >> 2).unwrap(); - }); - assert!(decode_broadcast(&overcount, Version::Lite07).is_err()); - } - - // The whole set has to fit the Adds of one message. - #[test] - fn a_request_set_fits_one_message() { - let long = Path::new(&"a".repeat(1000)).unwrap(); - let fits: Vec = vec![long.clone(); 65]; - assert!(request_fits(fits.iter(), Version::Lite07)); - let over: Vec = vec![long; 66]; - assert!(!request_fits(over.iter(), Version::Lite07)); - } - - // Lite04/05 carry the subscriber's origin id so the publisher can skip reflected - // announces before they hit the wire. - #[test] - fn announce_request_carries_exclude_hop_on_lite05() { - let msg = AnnounceRequest { - prefix: Path::from_static("room"), - exclude_hop: Hop::new(42).unwrap(), - }; - assert_eq!(request_round_trip(&msg, Version::Lite05).exclude_hop.id(), 42); - } - - // Lite06 dropped the field: the receiver's reflected-announce check catches the same - // loops, so a value set locally is simply not sent and decodes as zero. - #[test] - fn announce_request_drops_exclude_hop_on_lite06() { - let msg = AnnounceRequest { - prefix: Path::from_static("room"), - exclude_hop: Hop::new(42).unwrap(), - }; - assert_eq!(request_round_trip(&msg, Version::Lite06).exclude_hop, Hop::UNKNOWN); - - // And it costs nothing on the wire: the body is just the prefix. - let mut with = Vec::new(); - msg.encode(&mut Encoder::new(&mut with, Version::Lite05.into()), Version::Lite05) - .unwrap(); - let mut without = Vec::new(); - msg.encode(&mut Encoder::new(&mut without, Version::Lite06.into()), Version::Lite06) - .unwrap(); - assert!( - without.len() < with.len(), - "lite06 must not encode the exclude_hop varint" - ); - } - - #[test] - fn announce_ok_rejects_old_versions() { - let msg = AnnounceOk { - origin: Hop::new(1).unwrap(), - active: 0, - }; - let mut buf = Vec::new(); - assert!(matches!( - msg.encode(&mut Encoder::new(&mut buf, Version::Lite04.into()), Version::Lite04), - Err(EncodeError::Version) - )); - } - - #[test] - fn announce_ok_accepts_zero_origin() { - // Encode a well-formed message then patch the origin to 0 on the wire. - let mut buf = Vec::new(); - AnnounceOk { - origin: Hop::new(1).unwrap(), - active: 0, - } - .encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05) - .unwrap(); - // origin id 1 sits right after the size prefix; rewrite it to 0. - let bytes = &buf[..]; - let mut patched = bytes.to_vec(); - // size(1 byte) | origin varint(1 byte = 0x01) | active varint(1 byte) - patched[1] = 0x00; - let mut slice = &patched[..]; - let got = crate::coding::decode_buf(&mut slice, Version::Lite05, |r, v| AnnounceOk::decode(r, v)).unwrap(); - assert_eq!(got.origin.id(), 0); - assert_eq!(got.active, 0); - } - - #[test] - fn wip_cost_has_one_field_and_lite06_keeps_two() { - let mut wire = Vec::new(); - Cost::new(12) - .encode(&mut Encoder::new(&mut wire, Version::Lite07.into()), Version::Lite07) - .unwrap(); - assert_eq!(wire, [12]); - - wire.clear(); - Cost::new(12) - .encode(&mut Encoder::new(&mut wire, Version::Lite06.into()), Version::Lite06) - .unwrap(); - let mut input = Decoder::new(&wire, Version::Lite06.into()); - assert_eq!(input.varint().unwrap(), 12); - assert_eq!(input.varint().unwrap(), Cost::MAX.value()); - assert!(input.is_empty()); - - for cold in [0, 3, Cost::MAX.value()] { - wire.clear(); - let mut w = Encoder::new(&mut wire, Version::Lite06.into()); - w.varint(12).unwrap(); - w.varint(cold).unwrap(); - let mut input = Decoder::new(&wire, Version::Lite06.into()); - assert_eq!(Cost::decode(&mut input, Version::Lite06).unwrap(), Cost::new(12)); - assert!(input.is_empty()); - } - } - - /// An offer over `path`, each hop `(node, session)`, the publisher first. - fn offer(path: &[(u64, u64)], metric: u64, down_only: bool) -> Offer { - Offer { - path: path - .iter() - .map(|&(node, session)| route::path::Hop { node, session }) - .collect(), - draining: false, - priority: 0, - metric, - down_only, - } - } - - /// Encode a typed lite-07 announce message from its raw body, bypassing the encoder's checks. - fn forge(typ: u64, body: impl FnOnce(&mut Encoder<'_>)) -> Vec { - let version = Version::Lite07; - let mut inner = Vec::new(); - body(&mut Encoder::new(&mut inner, version.into())); - let mut buf = Vec::new(); - let mut w = Encoder::new(&mut buf, version.into()); - w.varint(typ).unwrap(); - w.varint(inner.len() as u64).unwrap(); - buf.extend_from_slice(&inner); - buf - } - - fn decode_broadcast(buf: &[u8], version: Version) -> Result { - let mut slice = buf; - crate::coding::decode_buf(&mut slice, version, |r, v| AnnounceBroadcast::decode(r, v)) - } - - #[test] - fn route_messages_round_trip_on_lite07() { - let long: Vec<(u64, u64)> = (1..=route::MAX_HOPS as u64).map(|node| (node, node * 3)).collect(); - for msg in [ - AnnounceBroadcast::RouteStart { - node: 0x7a3f, - prefix: 0, - suffix: Path::empty(), - exact: true, - offer: offer(&[(0x7a3f, 0)], 0, false), - }, - AnnounceBroadcast::RouteStart { - node: route::QUALIFIED | 9, - prefix: 0, - suffix: Path::from_static("room/a"), - exact: false, - offer: offer(&[(9, 0), (4, u64::MAX >> 2)], route::MAX, true), - }, - AnnounceBroadcast::RouteUpdate { - id: 0, - offer: Offer { - draining: true, - priority: i8::MIN, - ..offer(&[(5, 0), (6, 1)], 2, false) - }, - }, - AnnounceBroadcast::RouteStart { - node: 0x7a40, - prefix: 0, - suffix: Path::empty(), - exact: false, - offer: Offer { - priority: i8::MAX, - ..offer(&[(5, 0), (route::MAX, 7), (9, 2)], 0, false) - }, - }, - AnnounceBroadcast::RouteUpdate { - id: 1, - offer: offer(&long, 3, true), - }, - AnnounceBroadcast::RouteHold { id: 3 }, - AnnounceBroadcast::RouteEnd { id: 0 }, - AnnounceBroadcast::Dead(Dead::Closed { - hop: route::path::Hop { node: 4, session: 2 }, - far: 9, - gone: false, - moved: 0, - }), - AnnounceBroadcast::Dead(Dead::Closed { - hop: route::path::Hop { node: 4, session: 0 }, - far: route::MAX, - gone: true, - moved: 2, - }), - AnnounceBroadcast::Dead(Dead::Drain { node: 4 }), - AnnounceBroadcast::Floor { - id: 0, - revision: Some(0), - }, - AnnounceBroadcast::Floor { - id: 1, - revision: Some(route::MAX - 1), - }, - AnnounceBroadcast::Floor { id: 2, revision: None }, - ] { - assert_eq!(broadcast_round_trip(&msg, Version::Lite07), msg); - } - } - - /// The priority travels as the moq-lite priority byte, ordered as the model's i8. - #[test] - fn an_offer_carries_the_priority_byte() { - let mut buf = Vec::new(); - let msg = AnnounceBroadcast::RouteUpdate { - id: 0, - offer: Offer { - priority: -1, - ..offer(&[(1, 0)], 0, false) - }, - }; - msg.encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07) - .unwrap(); - // Type, length, Route ID, the one hop's count, node, and session, flags, then the - // priority byte. - assert_eq!(buf[7], 0x7f); - } - - /// A floor is a marker: the Route ID and the revision plus one, 0 when it certifies - /// nothing. - #[test] - fn a_floor_is_its_route_and_revision() { - let encode = |revision| { - let mut buf = Vec::new(); - AnnounceBroadcast::Floor { id: 0, revision } - .encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07) - .unwrap(); - buf - }; - assert_eq!(encode(Some(0)), vec![9, 2, 0, 1]); - assert_eq!(encode(Some(7)), vec![9, 2, 0, 8]); - assert_eq!(encode(None), vec![9, 2, 0, 0]); - } - - #[test] - fn route_messages_need_lite07() { - for msg in [ - AnnounceBroadcast::RouteStart { - node: 1, - prefix: 0, - suffix: Path::empty(), - exact: false, - offer: offer(&[(1, 0)], 0, false), - }, - AnnounceBroadcast::RouteUpdate { - id: 0, - offer: offer(&[(1, 0)], 0, false), - }, - AnnounceBroadcast::RouteHold { id: 0 }, - AnnounceBroadcast::RouteEnd { id: 0 }, - AnnounceBroadcast::Dead(Dead::Drain { node: 1 }), - AnnounceBroadcast::Floor { id: 0, revision: None }, - ] { - for version in [Version::Lite05, Version::Lite06] { - let mut buf = Vec::new(); - let res = msg.encode(&mut Encoder::new(&mut buf, version.into()), version); - assert!(matches!(res, Err(EncodeError::Version)), "{msg:?} on {version:?}"); - } - } - // A lite-06 receiver skips the type like any other it does not know. - let wire = forge(ROUTE_END, |w| w.varint(0u64).unwrap()); - assert_eq!( - decode_broadcast(&wire, Version::Lite06).unwrap(), - AnnounceBroadcast::Skipped - ); - } - - /// What no node offers is refused on both ends: node 0, an empty path or one past the - /// hop limit, and the last revision. - #[test] - fn route_messages_refuse_what_no_node_offers() { - let over: Vec<(u64, u64)> = (1..=route::MAX_HOPS as u64 + 1).map(|node| (node, 0)).collect(); - let refused = [ - AnnounceBroadcast::RouteStart { - node: 0, - prefix: 0, - suffix: Path::empty(), - exact: false, - offer: offer(&[(1, 0)], 1, false), - }, - AnnounceBroadcast::RouteStart { - node: 1, - prefix: 0, - suffix: Path::empty(), - exact: false, - offer: offer(&[], 1, false), - }, - AnnounceBroadcast::RouteUpdate { - id: 0, - offer: offer(&over, 1, false), - }, - AnnounceBroadcast::RouteUpdate { - id: 0, - offer: offer(&[(1, 0), (0, 1)], 1, false), - }, - AnnounceBroadcast::Dead(Dead::Drain { node: 0 }), - AnnounceBroadcast::Dead(Dead::Closed { - hop: route::path::Hop { node: 1, session: 1 }, - far: 0, - gone: false, - moved: 0, - }), - // The last revision has no room for the offset. - AnnounceBroadcast::Floor { - id: 0, - revision: Some(route::MAX), - }, - ]; - for msg in refused { - let mut buf = Vec::new(); - let res = msg.encode(&mut Encoder::new(&mut buf, Version::Lite07.into()), Version::Lite07); - assert!(matches!(res, Err(EncodeError::InvalidState)), "{msg:?}"); - } - - // An offer whose path counts `count` hops and names `named` of them, from Node ID - // `first` up, each on session 0, then these flags, the default priority, and a - // metric of 1. - let path = |flags: u8, count: u64, named: u64, first: u64| { - move |w: &mut Encoder<'_>| { - w.varint(count).unwrap(); - for hop in 0..named { - w.varint(first + hop).unwrap(); - w.varint(0u64).unwrap(); - } - w.u8(flags); - w.u8(0x80); - w.varint(1u64).unwrap(); - } - }; - let start = |node: u64, offer: Box)>| { - forge(ROUTE_START, move |w| { - w.varint(node).unwrap(); - // Prefix ID 0, an empty suffix, and not exact. - w.varint(0u64).unwrap(); - w.varint(0u64).unwrap(); - w.u8(0); - offer(w); - }) - }; - let hops = |count: u64, first: u64| -> Arc<[route::path::Hop]> { - (first..first + count) - .map(|node| route::path::Hop { node, session: 0 }) - .collect() - }; - let named = |node, path| Refusable::RouteStart { - node, - prefix: 0, - suffix: Some(Path::empty()), - exact: false, - path, - }; - let over = route::MAX_HOPS as u64 + 1; - // What the sender only forwarded is refused, the message decoded all the same and - // its path with it, so the receiver withdraws the route and keeps the stream. - let refused = [ - ( - start(1, Box::new(path(0, over, over, 1))), - named(1, hops(over, 1)), - Refusal::Hops(over), - ), - // Far past the limit: only the hops up to one past it are kept. - ( - start(1, Box::new(path(0, 4 * over, 4 * over, 1))), - named(1, hops(over, 1)), - Refusal::Hops(4 * over), - ), - ( - start(1, Box::new(path(0, 1, 1, 0))), - named(1, hops(1, 0)), - Refusal::Node, - ), - ( - start(0, Box::new(path(0, 1, 1, 1))), - named(0, hops(1, 1)), - Refusal::Node, - ), - // A flag no version defines, which may change what follows it. - ( - start(1, Box::new(path(8, 1, 1, 1))), - named(1, hops(1, 1)), - Refusal::Flags(8), - ), - ( - start(1, Box::new(path(0x86, 1, 1, 1))), - named(1, hops(1, 1)), - Refusal::Flags(0x86), - ), - // A suffix no path spells. - ( - forge(ROUTE_START, |w| { - w.varint(1u64).unwrap(); - w.varint(5u64).unwrap(); - w.bytes(b"x//y").unwrap(); - w.u8(0); - path(0, 1, 1, 1)(w); - }), - Refusable::RouteStart { - node: 1, - prefix: 5, - suffix: None, - exact: false, - path: hops(1, 1), - }, - Refusal::Path, - ), - ( - forge(ROUTE_UPDATE, |w| { - w.varint(0u64).unwrap(); - path(8, 1, 1, 1)(w); - }), - Refusable::RouteUpdate { - id: 0, - path: hops(1, 1), - }, - Refusal::Flags(8), - ), - ]; - for (wire, message, reason) in refused { - assert_eq!( - decode_broadcast(&wire, Version::Lite07).unwrap(), - AnnounceBroadcast::Refused { message, reason }, - "{wire:?}" - ); - } - - // Exact is a boolean. - let wire = forge(ROUTE_START, |w| { - w.varint(1u64).unwrap(); - w.varint(0u64).unwrap(); - w.varint(0u64).unwrap(); - w.u8(2); - path(0, 1, 1, 1)(w); - }); - let res = decode_broadcast(&wire, Version::Lite07); - assert!(matches!(res, Err(DecodeError::InvalidValue)), "{res:?}"); - - // A DEAD has a fixed size: it counts the ROUTE_UPDATEs following it. - let wire = forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 2] { - w.varint(field).unwrap(); - } - w.u8(1); - w.varint(2u64).unwrap(); - }); - assert_eq!( - decode_broadcast(&wire, Version::Lite07).unwrap(), - AnnounceBroadcast::Dead(Dead::Closed { - hop: route::path::Hop { node: 1, session: 1 }, - far: 2, - gone: true, - moved: 2, - }) - ); - - let malformed = [ - // No hops at all, and fewer than it counts. - start(1, Box::new(path(0, 0, 0, 1))), - start(1, Box::new(path(0, 3, 1, 1))), - // A byte past a usable offer. - forge(ROUTE_UPDATE, |w| { - w.varint(0u64).unwrap(); - path(0, 1, 1, 1)(w); - w.u8(0); - }), - // A moved offer short of its size. - forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 2] { - w.varint(field).unwrap(); - } - w.u8(1); - w.varint(1u64).unwrap(); - w.varint(4u64).unwrap(); - let prefix = w.prefix_varint(); - path(0, 1, 1, 3)(w); - w.u8(0); - w.fill(prefix).unwrap(); - }), - // A hold carries only its Route ID. - forge(ROUTE_HOLD, |w| { - w.varint(0u64).unwrap(); - w.varint(0u64).unwrap(); - }), - // A DEAD of a kind no version defines, naming Node ID 0, or Gone or More past 1. - forge(DEAD, |w| { - w.varint(2u64).unwrap(); - w.varint(1u64).unwrap(); - }), - forge(DEAD, |w| { - w.varint(DEAD_DRAIN).unwrap(); - w.varint(0u64).unwrap(); - }), - forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 0] { - w.varint(field).unwrap(); - } - w.u8(0); - w.u8(0); - w.varint(0u64).unwrap(); - }), - forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 2] { - w.varint(field).unwrap(); - } - w.u8(2); - w.u8(0); - w.varint(0u64).unwrap(); - }), - forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 2] { - w.varint(field).unwrap(); - } - w.u8(0); - w.u8(2); - w.varint(0u64).unwrap(); - }), - // More moved entries than the body could hold. - forge(DEAD, |w| { - for field in [DEAD_CLOSED, 1, 1, 2] { - w.varint(field).unwrap(); - } - w.u8(1); - w.u8(0); - w.varint(1u64 << 40).unwrap(); - }), - // A floor without its revision, and one with trailing bytes. - forge(ANNOUNCE_FLOOR, |w| w.varint(0u64).unwrap()), - forge(ANNOUNCE_FLOOR, |w| { - w.varint(0u64).unwrap(); - w.varint(1u64).unwrap(); - w.u8(0); - }), - // Trailing bytes after a complete body. - forge(ROUTE_END, |w| { - w.varint(0u64).unwrap(); - w.u8(0); - }), - ]; - for wire in malformed { - assert!(decode_broadcast(&wire, Version::Lite07).is_err(), "{wire:?}"); - } - } -} diff --git a/rs/moq-net/src/lite/compress.rs b/rs/moq-net/src/lite/compress.rs index 34befc00960..5d8a1f0e6c2 100644 --- a/rs/moq-net/src/lite/compress.rs +++ b/rs/moq-net/src/lite/compress.rs @@ -527,12 +527,12 @@ mod tests { /// A compressed stream pinned byte for byte. `js/net/src/lite/announce.test.ts` decodes /// this hex too. - const GOLDEN: &str = "001400000a726f6f6d2f612f63616d00016100000000000d0102036d6963000161000000000203000000010101000b0201016200016100000000"; + const GOLDEN: &str = "001600000a726f6f6d2f612f63616d01610000000000ff00000f0102036d696301610000000000ff0002070000000000ff0001020100000d0201016201610000000000ff00"; #[cfg(feature = "async")] fn golden() -> Vec { use crate::fuzz::Announced; - // Lite-07 names a route instead of a hop chain, so none travels. + // No relay announced these again, so their chains are empty. vec![ Announced::Start(Path::from_static("room/a/cam"), vec![]), Announced::Start(Path::from_static("room/a/mic"), vec![]), @@ -553,7 +553,7 @@ mod tests { /// The literal lite-07 stream `js/net` writes for the same announcements, never picking a /// base; `js/net/src/lite/announce.test.ts` pins its encoder to it. - const JS_LITERAL: &str = "001400000a726f6f6d2f612f63616d00016100000000001400000a726f6f6d2f612f6d69630001610000000002030000000101010010000006726f6f6d2f6200016100000000"; + const JS_LITERAL: &str = "001600000a726f6f6d2f612f63616d01610000000000ff00001600000a726f6f6d2f612f6d696301610000000000ff0002070000000000ff00010201000012000006726f6f6d2f6201610000000000ff00"; #[test] #[cfg(feature = "async")] diff --git a/rs/moq-net/src/lite/corpus.rs b/rs/moq-net/src/lite/corpus.rs index f003cc790a6..04578862508 100644 --- a/rs/moq-net/src/lite/corpus.rs +++ b/rs/moq-net/src/lite/corpus.rs @@ -4,10 +4,8 @@ //! //! Each case is one complete wire input at a negotiated `version` (its handshake name), with //! the outcome both must reach: `decoded`, where our own encoding of the decoded value must -//! equal the input, so a case also pins the value, `refused`, where the input is rejected -//! without asking for more bytes, or `withdrawn`, where a lite-07 route message decodes but its -//! forwarded content is refused, so the receiver withdraws the route or claim and keeps the -//! session (for a DEAD, any route a moved offer names). A fuzz crash on a single moq-lite message belongs here +//! equal the input, so a case also pins the value, or `refused`, where the input is rejected +//! without asking for more bytes. A fuzz crash on a single moq-lite message belongs here //! rather than in `fuzz/regressions/`, so JS replays it too. use crate::coding::{Decode, DecodeError, Decoder, Encode, Form}; @@ -44,7 +42,6 @@ enum Message { enum Expect { Decoded, Refused, - Withdrawn, } /// What a decode came to: our own encoding of what it decoded, as hex, or why it did not. @@ -72,10 +69,7 @@ fn run(case: &Case, data: &[u8]) -> String { }; let decoder = || Decoder::new(data, version.into()); match case.message { - Message::Announce => match super::AnnounceBroadcast::decode_slice(data, version) { - Ok((super::AnnounceBroadcast::Refused { .. }, used)) if used == data.len() => "withdrawn".to_string(), - decoded => outcome(data, version, decoded), - }, + Message::Announce => outcome(data, version, super::AnnounceBroadcast::decode_slice(data, version)), Message::AnnounceInit => { let mut r = decoder(); let decoded = super::AnnounceInit::decode_within(&mut r, version, case.budget.unwrap_or(usize::MAX)); @@ -116,7 +110,6 @@ fn corpus() { let want = match case.expect { Expect::Decoded => case.hex.clone(), Expect::Refused => "refused".to_string(), - Expect::Withdrawn => "withdrawn".to_string(), }; let got = run(case, &unhex(&case.hex)); if got != want { diff --git a/rs/moq-net/src/lite/datagram.rs b/rs/moq-net/src/lite/datagram.rs index d5643497ada..834927cc616 100644 --- a/rs/moq-net/src/lite/datagram.rs +++ b/rs/moq-net/src/lite/datagram.rs @@ -117,12 +117,9 @@ pub struct Datagrams<'a> { pub broadcast: Path, /// The instance the subscriber expects; see [`crate::origin::Route::instance`]. pub instance: Option, - /// The Node ID of the origin that serves the request, or 0 for whichever the - /// publisher selects. - pub node: u64, - /// How many leading segments of the broadcast path make up the prefix of the route - /// the request follows (lite-07); 0 with no node named. - pub depth: u64, + /// The forwarding node whose claim the request follows, or `None` for whichever the + /// publisher selects. Lite07+ only. + pub node: Option, pub track: Cow<'a, str>, /// The subscriber's priority. pub priority: i8, @@ -136,7 +133,6 @@ impl Datagrams<'_> { broadcast: self.broadcast, instance: self.instance, node: self.node, - depth: self.depth, track: Cow::Owned(self.track.into_owned()), priority: self.priority, } @@ -153,7 +149,7 @@ impl<'a> Message<'a> for Datagrams<'a> { let id = r.varint()?; let broadcast = Path::decode(r, version)?; let instance = super::instance::decode_pin(r, version)?; - let (node, depth) = super::instance::decode_node(r, version, &broadcast)?; + let node = super::instance::decode_node(r, version)?; let track = Cow::Borrowed(r.str()?); let priority = super::priority::from_wire(r.u8()?); Ok(Self { @@ -161,7 +157,6 @@ impl<'a> Message<'a> for Datagrams<'a> { broadcast, instance, node, - depth, track, priority, }) @@ -174,7 +169,7 @@ impl<'a> Message<'a> for Datagrams<'a> { w.varint(self.id)?; self.broadcast.encode(w, version)?; super::instance::encode_pin(w, version, self.instance.as_ref())?; - super::instance::encode_node(w, version, self.node, self.depth)?; + super::instance::encode_node(w, version, self.node)?; w.string(&self.track)?; w.u8(super::priority::to_wire(self.priority)); Ok(()) @@ -374,8 +369,7 @@ mod test { id: 7, broadcast: Path::from_static("room/alice"), instance: None, - node: 0x7a3f, - depth: 0, + node: Some(crate::route::NodeId { relay: 0x7a3f, start: 9 }), track: "audio".into(), priority: -3, }; @@ -383,7 +377,7 @@ mod test { let (decoded, _) = Datagrams::decode_slice(&buf, Version::Lite07).unwrap(); assert_eq!(decoded.id, 7); assert_eq!(decoded.broadcast, request.broadcast); - assert_eq!(decoded.node, 0x7a3f); + assert_eq!(decoded.node, Some(crate::route::NodeId { relay: 0x7a3f, start: 9 })); assert_eq!(decoded.track, "audio"); assert_eq!(decoded.priority, -3); assert!(matches!( diff --git a/rs/moq-net/src/lite/fetch.rs b/rs/moq-net/src/lite/fetch.rs index 0ef400e8d5d..0710d12711b 100644 --- a/rs/moq-net/src/lite/fetch.rs +++ b/rs/moq-net/src/lite/fetch.rs @@ -16,12 +16,9 @@ pub struct Fetch<'a> { /// The instance the subscriber expects; see [`crate::origin::RouteInfo::instance`]. /// Lite07+ only. pub instance: Option, - /// The Node ID of the origin that serves the request, or 0 for whichever the + /// The forwarding node whose claim the request follows, or `None` for whichever the /// publisher selects. Lite07+ only. - pub node: u64, - /// How many leading segments of the broadcast path make up the prefix of the route - /// the request follows (lite-07); 0 with no node named. - pub depth: u64, + pub node: Option, pub track: Cow<'a, str>, pub priority: i8, /// Reader freshness budget. Lite07+ only; omission uses the publisher's max age alone. @@ -41,7 +38,6 @@ impl Fetch<'_> { broadcast: self.broadcast, instance: self.instance, node: self.node, - depth: self.depth, track: Cow::Owned(self.track.into_owned()), priority: self.priority, max_delay: self.max_delay, @@ -64,7 +60,7 @@ impl<'a> Message<'a> for Fetch<'a> { let broadcast = Path::decode(r, version)?; let instance = super::instance::decode_pin(r, version)?; - let (node, depth) = super::instance::decode_node(r, version, &broadcast)?; + let node = super::instance::decode_node(r, version)?; let track = Cow::Borrowed(r.str()?); let priority = super::priority::from_wire(r.u8()?); let max_delay = match version.has_fetch_max_delay() { @@ -86,7 +82,6 @@ impl<'a> Message<'a> for Fetch<'a> { broadcast, instance, node, - depth, track, priority, max_delay, @@ -106,7 +101,7 @@ impl<'a> Message<'a> for Fetch<'a> { self.broadcast.encode(w, version)?; super::instance::encode_pin(w, version, self.instance.as_ref())?; - super::instance::encode_node(w, version, self.node, self.depth)?; + super::instance::encode_node(w, version, self.node)?; w.string(&self.track)?; w.u8(super::priority::to_wire(self.priority)); match version.has_fetch_max_delay() { @@ -140,8 +135,7 @@ mod test { fn fetch_sample() -> Fetch<'static> { Fetch { instance: None, - node: 0, - depth: 0, + node: None, broadcast: Path::from_static("room"), track: Cow::Borrowed("video"), priority: 3, @@ -190,9 +184,9 @@ mod test { #[test] fn fetch_node_roundtrips_only_on_lite07() { let mut msg = fetch_sample(); - msg.node = 0x7a3f; - assert_eq!(fetch_roundtrip(Version::Lite07, &msg).node, 0x7a3f); - assert_eq!(fetch_roundtrip(Version::Lite06, &msg).node, 0); + msg.node = Some(crate::route::NodeId { relay: 0x7a3f, start: 9 }); + assert_eq!(fetch_roundtrip(Version::Lite07, &msg).node, Some(crate::route::NodeId { relay: 0x7a3f, start: 9 })); + assert_eq!(fetch_roundtrip(Version::Lite06, &msg).node, None); } #[test] diff --git a/rs/moq-net/src/lite/instance.rs b/rs/moq-net/src/lite/instance.rs index 4bc7d9dbe7a..21c4c666b55 100644 --- a/rs/moq-net/src/lite/instance.rs +++ b/rs/moq-net/src/lite/instance.rs @@ -1,6 +1,7 @@ use crate::{ - Instance, Path, + Instance, coding::{DecodeError, Decoder, EncodeError, Encoder}, + route::NodeId, }; use super::Version; @@ -38,33 +39,36 @@ pub(super) fn decode_pin(r: &mut Decoder<'_>, version: Version) -> Result