@rbxts/netweave 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +149 -2
- package/index.d.ts +1272 -0
- package/macros.d.ts +87 -0
- package/macros.luau +27 -0
- package/package.json +34 -5
- package/src/api/Audience.luau +156 -0
- package/src/api/Channel.luau +1288 -0
- package/src/api/Config.luau +884 -0
- package/src/api/Context.luau +385 -0
- package/src/api/Namespace.luau +540 -0
- package/src/api/Observer.luau +493 -0
- package/src/api/Policy.luau +457 -0
- package/src/api/Protocol.luau +593 -0
- package/src/api/Transport.luau +135 -0
- package/src/api/Trust.luau +393 -0
- package/src/api/View.luau +503 -0
- package/src/codec/Buffer.luau +1272 -0
- package/src/codec/Ir.luau +1353 -0
- package/src/codec/Serdes.luau +2694 -0
- package/src/netweave.luau +490 -0
- package/src/replication/Baseline.luau +314 -0
- package/src/replication/Delta.luau +613 -0
- package/src/replication/Store.luau +229 -0
- package/src/replication/Tick.luau +597 -0
- package/src/transport/Batch.luau +970 -0
- package/src/transport/Budget.luau +249 -0
- package/src/transport/Driver.luau +305 -0
- package/src/transport/Inbound.luau +1508 -0
- package/src/transport/Link.luau +162 -0
- package/src/transport/Outbound.luau +523 -0
- package/src/transport/Pacer.luau +120 -0
- package/src/transport/Query.luau +535 -0
- package/src/transport/Recipients.luau +507 -0
- package/src/types/init.luau +2064 -0
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--!optimize 2
|
|
3
|
+
|
|
4
|
+
--[=[
|
|
5
|
+
@class Batch
|
|
6
|
+
|
|
7
|
+
The envelope from `docs/WIRE-FORMAT.md`, and the only place that knows it.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
batch := version:u8 packet*
|
|
11
|
+
packet := id:varint [correlation if query] frame payload
|
|
12
|
+
frame := [length:varint if counted] [instances:varint if variable]
|
|
13
|
+
|
|
14
|
+
correlation := call:varint -- a request, read by the side that answers
|
|
15
|
+
| call:varint status:u8 -- a reply, read by the side that called
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
One version byte per batch rather than per packet: at 200 packets a batch it costs 0.005 bytes
|
|
19
|
+
each, and it is what lets the format have a v2 (§1).
|
|
20
|
+
|
|
21
|
+
Internal.
|
|
22
|
+
]=]
|
|
23
|
+
|
|
24
|
+
--[[
|
|
25
|
+
This file is guarantee G5. Everything else in the transport can be rewritten around it; the
|
|
26
|
+
length prefix is the thing that lets a refused packet be stepped over instead of taking the
|
|
27
|
+
rest of the batch with it. Blink and Zap have no length field, so one bad packet costs
|
|
28
|
+
everything behind it (`RESEARCH §3.8-R`).
|
|
29
|
+
|
|
30
|
+
**Both framing modes resynchronise, not just `counted`.** A `static` channel's size is fixed at
|
|
31
|
+
definition time and the id names the channel, so once the id resolves the reader already knows
|
|
32
|
+
where the packet ends — no prefix needed to skip it. `bench/src/shared/Modes/netweave.luau`
|
|
33
|
+
said otherwise and stopped the batch on a refused `static` packet; that was a limitation of the
|
|
34
|
+
stand-in, not of the format.
|
|
35
|
+
|
|
36
|
+
What genuinely cannot be recovered is an **unresolvable id**. Without a channel there is no
|
|
37
|
+
framing, so there is no way to know where the packet ends, and the batch stops. That is the one
|
|
38
|
+
honest limit and it is reported rather than hidden.
|
|
39
|
+
]]
|
|
40
|
+
|
|
41
|
+
local Buffer = require("../codec/Buffer")
|
|
42
|
+
--[[ For `Stage` alone. A leaf module with no requires of its own, and `Inbound` beside this one
|
|
43
|
+
already takes it for the severities (finding 48). ]]
|
|
44
|
+
local Config = require("../api/Config")
|
|
45
|
+
|
|
46
|
+
local Batch = {}
|
|
47
|
+
|
|
48
|
+
--[=[
|
|
49
|
+
@prop VERSION number
|
|
50
|
+
@within Batch
|
|
51
|
+
|
|
52
|
+
Wire format v1. A batch whose first byte is anything else is refused whole — a peer speaking a
|
|
53
|
+
different version is a deploy problem, not a packet problem.
|
|
54
|
+
]=]
|
|
55
|
+
Batch.VERSION = 1
|
|
56
|
+
|
|
57
|
+
--[[
|
|
58
|
+
One byte holds a varint up to 127. A `counted` payload longer than that needs the reservation
|
|
59
|
+
widened after the fact, which is what `Buffer.shift` is for; it happens only on large payloads
|
|
60
|
+
and never on the small ones that dominate.
|
|
61
|
+
]]
|
|
62
|
+
local ONE_BYTE_MAX = 127
|
|
63
|
+
local TWO_BYTE_MAX = 16383
|
|
64
|
+
|
|
65
|
+
--[[ Constant, because a peer chooses how often it is said. ]]
|
|
66
|
+
local UNCONSUMED = "the payload does not end where its declared length says it does"
|
|
67
|
+
|
|
68
|
+
--[[
|
|
69
|
+
One reason per ceiling, built on the first refusal and kept. The claim is the peer's to choose,
|
|
70
|
+
so a reason that quoted it was a distinct string per packet — past the observer's repeat
|
|
71
|
+
suppression, which keys on the reason, and onto the heap at a rate the peer controls, on the
|
|
72
|
+
one refusal that is supposed to cost the server nothing (SECURITY-REPORT-M4 74; `bench/residue`
|
|
73
|
+
rung 74). Keyed by the number rather than the channel because a query has two ceilings, and
|
|
74
|
+
channels with the same ceiling can share the sentence; the table is bounded by the number of
|
|
75
|
+
distinct ceilings a declaration has, not by anything a peer sends.
|
|
76
|
+
]]
|
|
77
|
+
local overCeiling: { [number]: string } = {}
|
|
78
|
+
|
|
79
|
+
local function overCeilingReason(ceiling: number): string
|
|
80
|
+
local reason = overCeiling[ceiling]
|
|
81
|
+
|
|
82
|
+
if not reason then
|
|
83
|
+
reason = `payload claims a length over the {ceiling} bytes this channel can hold`
|
|
84
|
+
overCeiling[ceiling] = reason
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
return reason
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
--[=[
|
|
91
|
+
@interface Sink
|
|
92
|
+
@within Batch
|
|
93
|
+
.resolve (id: number) -> any -- the channel with that wire id, or nil
|
|
94
|
+
.answers boolean -- true where a query packet is a request: the server
|
|
95
|
+
.admit (channel: any, bytes: number) -> (string?, string?) -- nil to decode it, or a reason and its stage
|
|
96
|
+
.deliver (channel: any, value: any, bytes: number, call: number?) -> ()
|
|
97
|
+
.reply (channel: any, call: number, status: number, value: any?, bytes: number) -> ()
|
|
98
|
+
.hello (hash: number) -> () -- a peer's protocol hash, off a control packet
|
|
99
|
+
.refuse (channel: any?, stage: Stage, reason: string, bytes: number, call: number?) -> ()
|
|
100
|
+
|
|
101
|
+
What [Batch.read] calls as it walks a batch. Every member is resolved once by the caller and
|
|
102
|
+
held, so nothing here allocates per packet.
|
|
103
|
+
|
|
104
|
+
`admit` runs **before** the payload is decoded. That ordering is the point: a packet over its
|
|
105
|
+
budget costs an id read and a seek rather than a full deserialisation, so refusing is cheaper
|
|
106
|
+
than accepting and a flood cannot be amplified into work (`PLAN-M2` R-2).
|
|
107
|
+
]=]
|
|
108
|
+
--[[ The stage a refusal is reported at, from the module that names them as rules. ]]
|
|
109
|
+
export type Stage = Config.Stage
|
|
110
|
+
|
|
111
|
+
export type Sink = {
|
|
112
|
+
resolve: (id: number) -> any,
|
|
113
|
+
--[[ Whether a query packet arriving here is a **request**. True on the server, false on the
|
|
114
|
+
client, and a property of the endpoint rather than of the packet — which is what lets one
|
|
115
|
+
wire id carry both directions of a query without a byte to say which. ]]
|
|
116
|
+
answers: boolean,
|
|
117
|
+
--[[ nil to decode the packet, or a reason and the stage to report it at. The stage is the
|
|
118
|
+
sink's to choose because the same refusal point serves two of them: over the rate is
|
|
119
|
+
`budget`, and a peer on a protocol this one does not share is `protocol`. ]]
|
|
120
|
+
admit: (channel: any, bytes: number) -> (string?, Stage?),
|
|
121
|
+
deliver: (channel: any, value: any, bytes: number, call: number?) -> (),
|
|
122
|
+
--[[ A replicated subject's change. The subject has been read; the change itself has not,
|
|
123
|
+
because folding it into a baseline needs one this module does not hold. The sink reads it
|
|
124
|
+
from the buffer, which is positioned at it and limited to the packet's end. ]]
|
|
125
|
+
change: (channel: any, subject: any, bytes: number) -> (),
|
|
126
|
+
reply: (channel: any, call: number, status: number, value: any?, bytes: number) -> (),
|
|
127
|
+
--[[ A peer's protocol hash, off a control packet. Once per session per peer, in the first batch
|
|
128
|
+
it sends — and `bytes` is what the whole control packet cost, so a mismatch is reported with
|
|
129
|
+
the same accounting as any other refusal. ]]
|
|
130
|
+
hello: (hash: number, bytes: number) -> (),
|
|
131
|
+
--[[ A peer asking for a replicated channel in full, because it can no longer read the changes.
|
|
132
|
+
The id is unresolved: this module reads control packets before it resolves anything, and
|
|
133
|
+
the sink is where a channel that does not exist is refused rather than assumed. ]]
|
|
134
|
+
resync: (id: number, bytes: number) -> (),
|
|
135
|
+
--[[ A peer's per-namespace digest, positioned and limited to its body but **not decoded**: the
|
|
136
|
+
sink calls [Batch.readDigest] if it wants it, so a peer sending three hundred of them costs
|
|
137
|
+
a table read each after the first (`PLAN-M5` D-7). Optional: a sink without one sees the
|
|
138
|
+
kind reported as one this build does not read, like any other it lacks. ]]
|
|
139
|
+
digest: ((bytes: number) -> ())?,
|
|
140
|
+
refuse: (channel: any?, stage: Stage, reason: string, bytes: number, call: number?) -> (),
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
--[=[
|
|
144
|
+
@interface NamespaceDigest
|
|
145
|
+
@within Batch
|
|
146
|
+
.name string
|
|
147
|
+
.hash number -- FNV-1a over that namespace's channel signatures, in id order
|
|
148
|
+
]=]
|
|
149
|
+
export type NamespaceDigest = { name: string, hash: number }
|
|
150
|
+
|
|
151
|
+
--[=[
|
|
152
|
+
@prop HELLO number
|
|
153
|
+
@within Batch
|
|
154
|
+
|
|
155
|
+
The control kinds carried on channel id 0, which is reserved (`WIRE-FORMAT.md` §3).
|
|
156
|
+
|
|
157
|
+
`HELLO` is a peer announcing the hash of its own declarations, once, in its first batch.
|
|
158
|
+
]=]
|
|
159
|
+
--[[
|
|
160
|
+
A control packet carries its body length, and an unknown kind is skipped rather than fatal.
|
|
161
|
+
|
|
162
|
+
Id 0 is reserved "for the handshake, and anything v2 needs", and a reserved slot that cannot be
|
|
163
|
+
extended without breaking every v1 reader is not reserved for anything. The length is what makes
|
|
164
|
+
a kind this build has never heard of steppable — three bytes of overhead, once per session,
|
|
165
|
+
against having to bump the batch version to add a control message.
|
|
166
|
+
]]
|
|
167
|
+
local HELLO = 1
|
|
168
|
+
|
|
169
|
+
--[[
|
|
170
|
+
`RESYNC` is a peer saying it has lost track of a replicated channel and needs all of it again.
|
|
171
|
+
|
|
172
|
+
It is a control kind rather than a channel because it is *about* a channel and cannot be sent on
|
|
173
|
+
one — the channel it names is server-to-client, so there is no direction for a client's packet
|
|
174
|
+
on it, and G6 refuses exactly that before decode. Reserved id 0 with a length in front of the
|
|
175
|
+
body is the extension point the format set aside for this (`WIRE-FORMAT.md` §3), so a v1 reader
|
|
176
|
+
that has never heard of it steps over it and says so rather than dying.
|
|
177
|
+
|
|
178
|
+
It is also the only packet in netweave a client sends that makes the server do work it did not
|
|
179
|
+
choose. ~~The bound is that the resend it provokes happens once in the tick that follows however
|
|
180
|
+
many arrive — so the worst a peer can extract is a whole state per tick.~~ That was the wrong bound,
|
|
181
|
+
measured in M4 phase 8: one per tick is one full state per frame. The bound that holds is in
|
|
182
|
+
`Inbound`, not here — one resend per `resyncTicks` window per (peer, channel), the asks inside a
|
|
183
|
+
window remembered and honoured when it closes (`WIRE-FORMAT.md` §2).
|
|
184
|
+
]]
|
|
185
|
+
local RESYNC = 2
|
|
186
|
+
|
|
187
|
+
--[[
|
|
188
|
+
`DIGEST` is a peer saying which namespaces it declares and what each one hashes to, so that a
|
|
189
|
+
refusal at `protocol` can name the namespace the two builds disagree in rather than two hashes.
|
|
190
|
+
|
|
191
|
+
Written beside every hello — a client's first packet, and the server's answer to a mismatch —
|
|
192
|
+
so it costs once per session on the happy path and once per mismatched peer otherwise. Read only
|
|
193
|
+
by a peer that has already refused the sender and not yet named the reason (`PLAN-M5` D-7).
|
|
194
|
+
]]
|
|
195
|
+
local DIGEST = 3
|
|
196
|
+
|
|
197
|
+
--[[ Bounds on what a digest body may claim, so decoding one is bounded by the declaration it
|
|
198
|
+
describes rather than by the peer: nobody declares a thousand namespaces or a name past a
|
|
199
|
+
line. Past either, the body is refused as malformed, which it is. ]]
|
|
200
|
+
local DIGEST_MAX_NAMESPACES = 1024
|
|
201
|
+
local DIGEST_MAX_NAME = 255
|
|
202
|
+
|
|
203
|
+
--[[ The repeat of the reason above it, without the kind. Constant, so a batch packed with control
|
|
204
|
+
kinds this build does not read allocates one string rather than one per packet. ]]
|
|
205
|
+
local ANOTHER_CONTROL_KIND = "another control kind this build does not read, in the same batch"
|
|
206
|
+
|
|
207
|
+
--[[ A control body that fails to parse inside the length it declared. Constant, because a peer
|
|
208
|
+
chooses how often it is said. ]]
|
|
209
|
+
local TRUNCATED_CONTROL_BODY = "a control packet's body ends before the value it carries does"
|
|
210
|
+
|
|
211
|
+
Batch.HELLO = HELLO
|
|
212
|
+
Batch.RESYNC = RESYNC
|
|
213
|
+
Batch.DIGEST = DIGEST
|
|
214
|
+
|
|
215
|
+
--[=[
|
|
216
|
+
@prop OK number
|
|
217
|
+
@within Batch
|
|
218
|
+
|
|
219
|
+
The reply statuses. `OK` carries an answer behind it; every other code ends the packet at the
|
|
220
|
+
status byte.
|
|
221
|
+
|
|
222
|
+
| Code | Meaning | What the caller can do |
|
|
223
|
+
|---|---|---|
|
|
224
|
+
| `OK` | the answer follows | use it |
|
|
225
|
+
| `REFUSED` | a policy said no, or the rate budget did | do not retry with the same input |
|
|
226
|
+
| `FAILED` | the handler raised, or the answer did not encode | report a bug |
|
|
227
|
+
| `UNHANDLED` | nothing is attached to answer this channel | wire up `:handle` |
|
|
228
|
+
| `BUSY` | this player already holds every call slot the server will | back off and retry |
|
|
229
|
+
]=]
|
|
230
|
+
--[[
|
|
231
|
+
Codes, not the server's reason string.
|
|
232
|
+
|
|
233
|
+
A refusal's reason is written for the game that owns the server: it names the policy, the field,
|
|
234
|
+
sometimes the player. Sending it to the client would publish the authorization model to the
|
|
235
|
+
machine that model exists to distrust, one denied request at a time. The caller learns *that* it
|
|
236
|
+
was refused and which class of thing went wrong, which is all it can act on; the reason itself
|
|
237
|
+
goes to the observer on the server, where the game can read it (`PLAN-M3` D-12).
|
|
238
|
+
]]
|
|
239
|
+
local OK = 0
|
|
240
|
+
|
|
241
|
+
Batch.OK = OK
|
|
242
|
+
Batch.REFUSED = 1
|
|
243
|
+
Batch.FAILED = 2
|
|
244
|
+
Batch.UNHANDLED = 3
|
|
245
|
+
Batch.BUSY = 4
|
|
246
|
+
|
|
247
|
+
-- ---------------------------------------------------------------------------------------------
|
|
248
|
+
-- Writing
|
|
249
|
+
-- ---------------------------------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
--[=[
|
|
252
|
+
@within Batch
|
|
253
|
+
@function begin
|
|
254
|
+
|
|
255
|
+
Writes the version byte if this buffer has not been started yet. Called before every packet;
|
|
256
|
+
the emptiness check is what makes it once per batch rather than once per packet.
|
|
257
|
+
]=]
|
|
258
|
+
function Batch.begin()
|
|
259
|
+
if Buffer.used() == 0 then
|
|
260
|
+
Buffer.writeU8(Batch.VERSION)
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
--[[
|
|
265
|
+
A varint whose value is not known until the bytes it measures have been written.
|
|
266
|
+
|
|
267
|
+
Reserve one byte, write, then patch — and widen if the value turned out not to fit. The widen
|
|
268
|
+
moves everything after the reservation, so the two reservations in a frame are patched from the
|
|
269
|
+
back: the instance count first, then the length, whose value counts payload bytes and is
|
|
270
|
+
therefore unaffected by a shift that happens after it.
|
|
271
|
+
]]
|
|
272
|
+
local function patchVarint(at: number, value: number)
|
|
273
|
+
if value <= ONE_BYTE_MAX then
|
|
274
|
+
Buffer.patchU8(at, value)
|
|
275
|
+
return
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
if value > TWO_BYTE_MAX then
|
|
279
|
+
--[[ 16,383 bytes is far past a `RemoteEvent`'s useful payload and past the 908-byte
|
|
280
|
+
unreliable limit; a schema that reaches it is a design problem, not a runtime one. ]]
|
|
281
|
+
error(`[netweave] a packet field of {value} does not fit two varint bytes`)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
Buffer.shift(at + 1, 1)
|
|
285
|
+
Buffer.patchU8(at, bit32.bor(bit32.band(value, 0x7F), 0x80))
|
|
286
|
+
Buffer.patchU8(at + 1, bit32.rshift(value, 7))
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
--[=[
|
|
290
|
+
@within Batch
|
|
291
|
+
@function writeBody
|
|
292
|
+
@param channel Channel
|
|
293
|
+
@param value any
|
|
294
|
+
|
|
295
|
+
The packet without the version byte, for a caller that is building one packet on its own rather
|
|
296
|
+
than appending to a batch.
|
|
297
|
+
|
|
298
|
+
The fan-out path needs this: it serialises once into a buffer of its own and copies the bytes
|
|
299
|
+
into each recipient, and a version byte in the scratch would have to be stripped on the way in —
|
|
300
|
+
an offset to get wrong on every broadcast for no gain.
|
|
301
|
+
]=]
|
|
302
|
+
--[[
|
|
303
|
+
The frame and the payload, for whichever codec the direction calls for.
|
|
304
|
+
|
|
305
|
+
A query writes its `args` codec one way and its `returns` codec the other, and everything from
|
|
306
|
+
the frame down is identical between them — so it is written once here rather than twice, which
|
|
307
|
+
is also what stops the two from drifting apart.
|
|
308
|
+
]]
|
|
309
|
+
local function writeFramed(codec: any, value: any)
|
|
310
|
+
local counted = codec.framing == "counted"
|
|
311
|
+
local variable = codec.instances == nil
|
|
312
|
+
|
|
313
|
+
local lengthAt = if counted then Buffer.reserve(1) else -1
|
|
314
|
+
local instancesAt = if variable then Buffer.reserve(1) else -1
|
|
315
|
+
|
|
316
|
+
local payloadStart = Buffer.used()
|
|
317
|
+
local instancesStart = Buffer.instancesWritten()
|
|
318
|
+
|
|
319
|
+
codec.write(value)
|
|
320
|
+
|
|
321
|
+
local length = Buffer.used() - payloadStart
|
|
322
|
+
|
|
323
|
+
--[[ Back to front: widening the instance count moves the payload but not the length byte in
|
|
324
|
+
front of it, and `length` counts payload bytes only, so it stays correct either way. ]]
|
|
325
|
+
if variable then
|
|
326
|
+
patchVarint(instancesAt, Buffer.instancesWritten() - instancesStart)
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
if counted then
|
|
330
|
+
patchVarint(lengthAt, length)
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
--[=[
|
|
335
|
+
@within Batch
|
|
336
|
+
@function writeChange
|
|
337
|
+
@param channel Channel -- a `replicate` channel
|
|
338
|
+
@param subject any -- which value this is
|
|
339
|
+
@param old any -- what the recipient already has, or nil
|
|
340
|
+
@param new any -- what it should have, or nil to say the subject is gone
|
|
341
|
+
@return boolean -- false when there was nothing to say, and nothing was written
|
|
342
|
+
|
|
343
|
+
One replicated subject's change, for one recipient.
|
|
344
|
+
|
|
345
|
+
The packet is `id`, a length, an instance count if the schema can carry Instances, the
|
|
346
|
+
**subject**, and the change. The subject is inside the frame rather than beside it, because a
|
|
347
|
+
packet whose length does not cover the subject could be stepped over into the middle of one —
|
|
348
|
+
and stepping over a refused packet is guarantee G5.
|
|
349
|
+
|
|
350
|
+
:::note
|
|
351
|
+
A change is always `counted`, even where the patch layout is statically sized. The same channel
|
|
352
|
+
emits a removal — flags and nothing else — and a change carrying values, so the reader cannot
|
|
353
|
+
know where a refused one ends from the schema alone. That is the length prefix earning its byte
|
|
354
|
+
rather than spending one.
|
|
355
|
+
:::
|
|
356
|
+
|
|
357
|
+
:::caution
|
|
358
|
+
Returns **false and writes nothing** when the recipient is already up to date, and the caller
|
|
359
|
+
must not have written anything else in between: everything from the channel id onward is rolled
|
|
360
|
+
back. `Delta.write` is what decides, and it can only decide by trying.
|
|
361
|
+
:::
|
|
362
|
+
]=]
|
|
363
|
+
function Batch.writeChange(channel: any, subject: any, old: any, new: any): boolean
|
|
364
|
+
--[[ The whole packet, not just the payload. A change that turns out to be empty has to take its
|
|
365
|
+
own id and length prefix with it, because a bare id would decode as the next packet. ]]
|
|
366
|
+
local used, instances = Buffer.mark()
|
|
367
|
+
|
|
368
|
+
--[[ Before the mark is used for anything: `begin` writes the version byte only into an empty
|
|
369
|
+
batch, so a rollback that takes it with it leaves the batch empty and the next packet
|
|
370
|
+
writes it again. Leaving it out entirely is what the first end-to-end run did, and the
|
|
371
|
+
reader then took the length prefix for a channel id — the batch decoded as garbage without
|
|
372
|
+
ever failing a check, because a version byte of 1 is also a channel id of 1. ]]
|
|
373
|
+
Batch.begin()
|
|
374
|
+
Buffer.discardBlock()
|
|
375
|
+
Buffer.writeVarint(channel.id)
|
|
376
|
+
|
|
377
|
+
local variable = channel.changeInstances
|
|
378
|
+
local lengthAt = Buffer.reserve(1)
|
|
379
|
+
local instancesAt = if variable then Buffer.reserve(1) else -1
|
|
380
|
+
|
|
381
|
+
local payloadStart = Buffer.used()
|
|
382
|
+
local instancesStart = Buffer.instancesWritten()
|
|
383
|
+
|
|
384
|
+
channel.subjectCodec.write(subject)
|
|
385
|
+
|
|
386
|
+
if not channel.delta.write(old, new) then
|
|
387
|
+
Buffer.rollback(used, instances)
|
|
388
|
+
return false
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
local length = Buffer.used() - payloadStart
|
|
392
|
+
|
|
393
|
+
if variable then
|
|
394
|
+
patchVarint(instancesAt, Buffer.instancesWritten() - instancesStart)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
patchVarint(lengthAt, length)
|
|
398
|
+
return true
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
function Batch.writeBody(channel: any, value: any)
|
|
402
|
+
--[[
|
|
403
|
+
A packet boundary is where a block left open by a raising encode gets cleaned up. Without
|
|
404
|
+
this, the length byte reserved below would be taken from that block's stale cursor — and
|
|
405
|
+
`Buffer.reserve` is block-aware precisely so that a bitfield inside a block works, so the one
|
|
406
|
+
that leaks is the one that matters.
|
|
407
|
+
]]
|
|
408
|
+
Buffer.discardBlock()
|
|
409
|
+
Buffer.writeVarint(channel.id)
|
|
410
|
+
writeFramed(channel.codec, value)
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
--[=[
|
|
414
|
+
@within Batch
|
|
415
|
+
@function writeRequest
|
|
416
|
+
@param channel Channel
|
|
417
|
+
@param call number -- the caller's id for this call, echoed back in the reply
|
|
418
|
+
@param value any
|
|
419
|
+
|
|
420
|
+
A query going out from the side that called. The same packet as any other but for the call id,
|
|
421
|
+
which sits between the channel id and the frame.
|
|
422
|
+
|
|
423
|
+
The id is framing rather than payload on purpose. A `counted` channel's length prefix counts the
|
|
424
|
+
bytes the schema produced, so the derived byte ceiling stays a statement about the schema and
|
|
425
|
+
does not have to be adjusted by however many bytes this particular call id took to write.
|
|
426
|
+
]=]
|
|
427
|
+
function Batch.writeRequest(channel: any, call: number, value: any)
|
|
428
|
+
Batch.begin()
|
|
429
|
+
Buffer.discardBlock()
|
|
430
|
+
Buffer.writeVarint(channel.id)
|
|
431
|
+
Buffer.writeVarint(call)
|
|
432
|
+
writeFramed(channel.codec, value)
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
--[=[
|
|
436
|
+
@within Batch
|
|
437
|
+
@function writeReply
|
|
438
|
+
@param channel Channel
|
|
439
|
+
@param call number -- the id the caller sent
|
|
440
|
+
@param status number -- `Batch.OK`, and the answer follows; anything else ends the packet
|
|
441
|
+
@param value any? -- the answer, when the status is `Batch.OK`
|
|
442
|
+
|
|
443
|
+
The answer going back. A non-`OK` status ends the packet at the status byte: no frame, no
|
|
444
|
+
payload.
|
|
445
|
+
|
|
446
|
+
The alternative was a zero-length payload, which a `counted` schema can express and a `static`
|
|
447
|
+
one cannot — a refused reply on a schema fixed at forty bytes would have had to carry forty
|
|
448
|
+
bytes of nothing. Ending the packet is uniform across both framings and stays resynchronisable,
|
|
449
|
+
because by then the reader has already read everything it needs to know where the packet ends.
|
|
450
|
+
]=]
|
|
451
|
+
function Batch.writeReply(channel: any, call: number, status: number, value: any?)
|
|
452
|
+
Batch.begin()
|
|
453
|
+
Buffer.discardBlock()
|
|
454
|
+
Buffer.writeVarint(channel.id)
|
|
455
|
+
Buffer.writeVarint(call)
|
|
456
|
+
Buffer.writeU8(status)
|
|
457
|
+
|
|
458
|
+
if status == Batch.OK then
|
|
459
|
+
writeFramed(channel.responseCodec, value)
|
|
460
|
+
end
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
--[=[
|
|
464
|
+
@within Batch
|
|
465
|
+
@function writePacket
|
|
466
|
+
@param channel Channel
|
|
467
|
+
@param value any
|
|
468
|
+
|
|
469
|
+
Appends one packet to the current outgoing buffer, version byte included if it is the first.
|
|
470
|
+
|
|
471
|
+
Raises if the value does not satisfy the schema — that is the encode-side posture, and the
|
|
472
|
+
caller owns the rewind (`Serdes.Codec.write`).
|
|
473
|
+
]=]
|
|
474
|
+
function Batch.writePacket(channel: any, value: any)
|
|
475
|
+
Batch.begin()
|
|
476
|
+
Batch.writeBody(channel, value)
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
--[=[
|
|
480
|
+
@within Batch
|
|
481
|
+
@function writeHello
|
|
482
|
+
@param hash number -- this peer's protocol hash
|
|
483
|
+
|
|
484
|
+
The handshake packet, on reserved channel id 0.
|
|
485
|
+
|
|
486
|
+
Written once per session, as the first packet this peer sends, so the peer on the other end
|
|
487
|
+
learns whether it is talking to a build it agrees with before it has read anything else in the
|
|
488
|
+
batch (`WIRE-FORMAT.md` §4).
|
|
489
|
+
]=]
|
|
490
|
+
function Batch.writeHello(hash: number)
|
|
491
|
+
Batch.begin()
|
|
492
|
+
Buffer.discardBlock()
|
|
493
|
+
Buffer.writeVarint(0)
|
|
494
|
+
Buffer.writeU8(HELLO)
|
|
495
|
+
Buffer.writeVarint(4)
|
|
496
|
+
Buffer.writeU32(hash)
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
--[=[
|
|
500
|
+
@within Batch
|
|
501
|
+
@function writeResync
|
|
502
|
+
@param id number -- the replicated channel this peer has lost track of
|
|
503
|
+
|
|
504
|
+
"I cannot read your changes any more; send me everything."
|
|
505
|
+
|
|
506
|
+
A change is only meaningful against the value it was computed from, so a change that did not
|
|
507
|
+
arrive makes every change after it unreadable — and the peer that refused the packet is the one
|
|
508
|
+
that knows. This is that peer saying so, on the reserved control id.
|
|
509
|
+
|
|
510
|
+
`PLAN-M4` D-2 expected a sequence number on the wire to find the gap. There is none, because the
|
|
511
|
+
gap does not have to be found: the endpoint that dropped the packet reported it at the time.
|
|
512
|
+
]=]
|
|
513
|
+
--[[ How many bytes a varint takes, so a digest's length can be written ahead of its body rather
|
|
514
|
+
than patched behind it: a body of several hundred bytes needs a two-byte length, and the
|
|
515
|
+
one-byte reserve the resync uses would not hold it. ]]
|
|
516
|
+
local function varintSize(value: number): number
|
|
517
|
+
local size = 1
|
|
518
|
+
|
|
519
|
+
while value >= 128 do
|
|
520
|
+
value = value // 128
|
|
521
|
+
size += 1
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
return size
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
--[=[
|
|
528
|
+
@within Batch
|
|
529
|
+
@function writeDigest
|
|
530
|
+
@param entries { NamespaceDigest } -- this peer's namespaces, sorted by name
|
|
531
|
+
|
|
532
|
+
"These are the namespaces I declare, and what each hashes to." Written beside a hello, on the
|
|
533
|
+
reserved control id, so the peer that refuses this one can say which namespace to look in.
|
|
534
|
+
]=]
|
|
535
|
+
function Batch.writeDigest(entries: { NamespaceDigest })
|
|
536
|
+
Batch.begin()
|
|
537
|
+
Buffer.discardBlock()
|
|
538
|
+
Buffer.writeVarint(0)
|
|
539
|
+
Buffer.writeU8(DIGEST)
|
|
540
|
+
|
|
541
|
+
local body = varintSize(#entries)
|
|
542
|
+
|
|
543
|
+
for _, entry in entries do
|
|
544
|
+
local length = #entry.name
|
|
545
|
+
body += varintSize(length) + length + 4
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
Buffer.writeVarint(body)
|
|
549
|
+
Buffer.writeVarint(#entries)
|
|
550
|
+
|
|
551
|
+
for _, entry in entries do
|
|
552
|
+
local length = #entry.name
|
|
553
|
+
Buffer.writeVarint(length)
|
|
554
|
+
Buffer.writeString(entry.name, length)
|
|
555
|
+
Buffer.writeU32(entry.hash)
|
|
556
|
+
end
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
--[=[
|
|
560
|
+
@within Batch
|
|
561
|
+
@function readDigest
|
|
562
|
+
@return { NamespaceDigest }? -- nil when the body is malformed or claims more than a declaration can hold
|
|
563
|
+
|
|
564
|
+
Decodes the digest the reader is positioned at. Called by a sink's `digest` handler, and only
|
|
565
|
+
when it wants the answer; the reader itself never decodes one, because a peer can send them
|
|
566
|
+
without limit and decoding allocates a table per namespace.
|
|
567
|
+
]=]
|
|
568
|
+
function Batch.readDigest(): { NamespaceDigest }?
|
|
569
|
+
local count = Buffer.readVarint()
|
|
570
|
+
|
|
571
|
+
if Buffer.rejected() or count > DIGEST_MAX_NAMESPACES then
|
|
572
|
+
return nil
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
--[[ `table.create` yields `{unknown}` and Luau arrays are invariant, so the cast is the only way
|
|
576
|
+
to pre-size this rather than growing it entry by entry. ]]
|
|
577
|
+
local entries: { NamespaceDigest } = (table.create(count) :: any) :: { NamespaceDigest }
|
|
578
|
+
|
|
579
|
+
for index = 1, count do
|
|
580
|
+
local length = Buffer.readVarint()
|
|
581
|
+
|
|
582
|
+
if Buffer.rejected() or length > DIGEST_MAX_NAME then
|
|
583
|
+
return nil
|
|
584
|
+
end
|
|
585
|
+
|
|
586
|
+
local name = Buffer.readString(length)
|
|
587
|
+
local hash = Buffer.readU32()
|
|
588
|
+
|
|
589
|
+
if Buffer.rejected() then
|
|
590
|
+
return nil
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
entries[index] = { name = name, hash = hash }
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
return entries
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
function Batch.writeResync(id: number)
|
|
600
|
+
Batch.begin()
|
|
601
|
+
Buffer.discardBlock()
|
|
602
|
+
Buffer.writeVarint(0)
|
|
603
|
+
Buffer.writeU8(RESYNC)
|
|
604
|
+
|
|
605
|
+
--[[ The body is one varint, and its length is written anyway. A control packet's length is what
|
|
606
|
+
makes an unknown kind steppable, so leaving it off a known one would make the format's
|
|
607
|
+
extension point depend on which kinds a reader happens to know. ]]
|
|
608
|
+
local at = Buffer.reserve(1)
|
|
609
|
+
local start = Buffer.used()
|
|
610
|
+
|
|
611
|
+
Buffer.writeVarint(id)
|
|
612
|
+
patchVarint(at, Buffer.used() - start)
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
-- ---------------------------------------------------------------------------------------------
|
|
616
|
+
-- Reading
|
|
617
|
+
-- ---------------------------------------------------------------------------------------------
|
|
618
|
+
|
|
619
|
+
--[=[
|
|
620
|
+
@within Batch
|
|
621
|
+
@function read
|
|
622
|
+
@param packed buffer
|
|
623
|
+
@param instances { Instance }?
|
|
624
|
+
@param sink Sink
|
|
625
|
+
|
|
626
|
+
Walks a batch, delivering what decodes and reporting what does not.
|
|
627
|
+
|
|
628
|
+
Nothing here raises. Every failure is a value handed to `sink.refuse`, because the bytes came
|
|
629
|
+
off the wire and a malicious client must not be able to abort the loop (`DESIGN-API.md` G4).
|
|
630
|
+
]=]
|
|
631
|
+
function Batch.read(packed: buffer, instances: { unknown }?, sink: Sink)
|
|
632
|
+
local size = buffer.len(packed)
|
|
633
|
+
|
|
634
|
+
if size == 0 then
|
|
635
|
+
return
|
|
636
|
+
end
|
|
637
|
+
|
|
638
|
+
Buffer.beginRead(packed, instances)
|
|
639
|
+
|
|
640
|
+
local sidecar = if instances then #instances else 0
|
|
641
|
+
local answers = sink.answers
|
|
642
|
+
local version = Buffer.readU8()
|
|
643
|
+
|
|
644
|
+
--[[ Whether a control kind this build cannot read has already been named in this batch. ]]
|
|
645
|
+
local unknownKind = false
|
|
646
|
+
|
|
647
|
+
if Buffer.rejected() or version ~= Batch.VERSION then
|
|
648
|
+
sink.refuse(nil, "parse", `batch version {version} is not {Batch.VERSION}`, size)
|
|
649
|
+
return
|
|
650
|
+
end
|
|
651
|
+
|
|
652
|
+
while Buffer.cursor() < size do
|
|
653
|
+
Buffer.setLimit(size)
|
|
654
|
+
|
|
655
|
+
local packetStart = Buffer.cursor()
|
|
656
|
+
local id = Buffer.readVarint()
|
|
657
|
+
|
|
658
|
+
if Buffer.rejected() then
|
|
659
|
+
sink.refuse(nil, "parse", "truncated channel id", size - packetStart)
|
|
660
|
+
return
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
--[[
|
|
664
|
+
Id 0 is control traffic and has no channel behind it (`WIRE-FORMAT.md` §3).
|
|
665
|
+
|
|
666
|
+
It is read before `resolve` because resolving 0 would fail, and the failure would be the
|
|
667
|
+
unrecoverable one: the batch would stop on the very packet that exists to explain why the
|
|
668
|
+
two peers disagree.
|
|
669
|
+
]]
|
|
670
|
+
if id == 0 then
|
|
671
|
+
local kind = Buffer.readU8()
|
|
672
|
+
local length = if Buffer.rejected() then 0 else Buffer.readVarint()
|
|
673
|
+
|
|
674
|
+
if Buffer.rejected() then
|
|
675
|
+
sink.refuse(nil, "parse", "truncated control packet", size - packetStart)
|
|
676
|
+
return
|
|
677
|
+
end
|
|
678
|
+
|
|
679
|
+
local bodyStart = Buffer.cursor()
|
|
680
|
+
local bodyEnd = bodyStart + length
|
|
681
|
+
|
|
682
|
+
if bodyEnd > size then
|
|
683
|
+
sink.refuse(nil, "parse", "control body runs past the batch", size - packetStart)
|
|
684
|
+
return
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
--[[
|
|
688
|
+
Only the kinds this build knows are read. Anything else is stepped over — that is what
|
|
689
|
+
the length is for — and **said out loud**, which is a separate decision.
|
|
690
|
+
|
|
691
|
+
The forward-compatibility argument says stay quiet: a v1 reader meeting a v2 control
|
|
692
|
+
message should carry on, not complain. The argument that wins is this milestone's:
|
|
693
|
+
nothing on the receive path is silent, because a peer sending bytes this build cannot
|
|
694
|
+
act on is exactly the skew a future deploy will need to diagnose, and the repeat
|
|
695
|
+
suppression already caps what it costs at three lines per stage. Found by
|
|
696
|
+
`tests/fuzz_runtime.luau`, whose "nothing vanishes" invariant this was the only
|
|
697
|
+
exception to.
|
|
698
|
+
]]
|
|
699
|
+
if kind == HELLO and length >= 4 then
|
|
700
|
+
Buffer.setLimit(bodyEnd)
|
|
701
|
+
|
|
702
|
+
local hash = Buffer.readU32()
|
|
703
|
+
|
|
704
|
+
if not Buffer.rejected() then
|
|
705
|
+
sink.hello(hash, size - packetStart)
|
|
706
|
+
else
|
|
707
|
+
sink.refuse(nil, "parse", TRUNCATED_CONTROL_BODY, length)
|
|
708
|
+
end
|
|
709
|
+
elseif kind == RESYNC and length >= 1 then
|
|
710
|
+
Buffer.setLimit(bodyEnd)
|
|
711
|
+
|
|
712
|
+
local wanted = Buffer.readVarint()
|
|
713
|
+
|
|
714
|
+
--[[ A body that fails to parse inside its own declared length — a resync id whose
|
|
715
|
+
varint asks for a byte the length did not provide — was stepped over without a
|
|
716
|
+
word. Found by the fuzzer the round its corpus learned to write a resync
|
|
717
|
+
(PLAN-M4-BUG phase 6): `01 00 02 01 89`, one flipped bit. ]]
|
|
718
|
+
if not Buffer.rejected() then
|
|
719
|
+
sink.resync(wanted, size - packetStart)
|
|
720
|
+
else
|
|
721
|
+
sink.refuse(nil, "parse", TRUNCATED_CONTROL_BODY, length)
|
|
722
|
+
end
|
|
723
|
+
elseif kind == DIGEST and sink.digest then
|
|
724
|
+
--[[ Positioned and limited, and handed over undecoded: whether the body is worth a
|
|
725
|
+
table per namespace is the sink's call, and for every digest after the one that
|
|
726
|
+
named a peer's mismatch the answer is no. The tail below seeks past it either
|
|
727
|
+
way. ]]
|
|
728
|
+
Buffer.setLimit(bodyEnd)
|
|
729
|
+
sink.digest(size - packetStart)
|
|
730
|
+
else
|
|
731
|
+
--[[ Named once per batch, counted every time. Every unknown kind is still reported —
|
|
732
|
+
`tests/fuzz_runtime.luau`'s invariant is that nothing vanishes — but a batch
|
|
733
|
+
packed with them builds one string rather than one per packet (M3 phase 9). ]]
|
|
734
|
+
if unknownKind then
|
|
735
|
+
sink.refuse(nil, "parse", ANOTHER_CONTROL_KIND, length)
|
|
736
|
+
else
|
|
737
|
+
unknownKind = true
|
|
738
|
+
sink.refuse(nil, "parse", `control kind {kind} is not one this build reads`, length)
|
|
739
|
+
end
|
|
740
|
+
end
|
|
741
|
+
|
|
742
|
+
--[[ The same tail every other packet takes, written out rather than jumped over. A
|
|
743
|
+
control packet carries no instances, so the sidecar cursor stands still — which is
|
|
744
|
+
a claim, not an omission, and is why the seek is here at all. ]]
|
|
745
|
+
Buffer.setLimit(size)
|
|
746
|
+
Buffer.seek(bodyEnd)
|
|
747
|
+
Buffer.seekInstances(Buffer.instanceCursor())
|
|
748
|
+
Buffer.clearRejection()
|
|
749
|
+
continue
|
|
750
|
+
end
|
|
751
|
+
|
|
752
|
+
local channel = sink.resolve(id)
|
|
753
|
+
|
|
754
|
+
if not channel then
|
|
755
|
+
--[[ The one unrecoverable case. Without a channel there is no framing, so the end of
|
|
756
|
+
this packet is unknowable and everything behind it is unreachable. ]]
|
|
757
|
+
sink.refuse(nil, "parse", `unknown channel id {id}`, size - packetStart)
|
|
758
|
+
return
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
--[[
|
|
762
|
+
Which half of a query this is comes from the endpoint, not from the packet. The server
|
|
763
|
+
only ever receives requests on a query id and the client only ever receives replies, so
|
|
764
|
+
one wire id carries both directions and neither peer has to be told which it is holding.
|
|
765
|
+
|
|
766
|
+
A malicious client cannot exploit that by "sending a reply": the server reads what it is
|
|
767
|
+
given as a request, and it decodes against the args schema or it is refused like anything
|
|
768
|
+
else.
|
|
769
|
+
]]
|
|
770
|
+
local query = channel.class == "query"
|
|
771
|
+
local change = channel.class == "replicate"
|
|
772
|
+
|
|
773
|
+
--[[ A change is framed by neither the state codec nor the patch's — it is the subject and
|
|
774
|
+
the change together, and `changeFraming` is the pair's shape. Always `counted`: the same
|
|
775
|
+
channel emits a removal, which is flags and nothing else, and a change carrying values,
|
|
776
|
+
so where a refused one ends cannot be known from the schema (`Batch.writeChange`). ]]
|
|
777
|
+
local codec = if change then channel.changeFraming else channel.codec
|
|
778
|
+
local ceiling = channel.maxBytes
|
|
779
|
+
local call: number? = nil
|
|
780
|
+
local status = OK
|
|
781
|
+
|
|
782
|
+
if query then
|
|
783
|
+
call = Buffer.readVarint()
|
|
784
|
+
|
|
785
|
+
if Buffer.rejected() then
|
|
786
|
+
--[[ Unrecoverable for the same reason a truncated id is: the varint ran off the end
|
|
787
|
+
of the batch, so there is nothing behind it to resynchronise on. ]]
|
|
788
|
+
sink.refuse(channel, "parse", "truncated call id", size - packetStart)
|
|
789
|
+
return
|
|
790
|
+
end
|
|
791
|
+
|
|
792
|
+
if not answers then
|
|
793
|
+
--[[ The reply is framed by the `returns` schema, and it has a ceiling of its own —
|
|
794
|
+
using the request's would police an answer against the size of the question. ]]
|
|
795
|
+
codec = channel.responseCodec
|
|
796
|
+
ceiling = channel.replyMaxBytes
|
|
797
|
+
status = Buffer.readU8()
|
|
798
|
+
|
|
799
|
+
if Buffer.rejected() then
|
|
800
|
+
sink.refuse(channel, "parse", "truncated reply status", size - packetStart)
|
|
801
|
+
return
|
|
802
|
+
end
|
|
803
|
+
end
|
|
804
|
+
end
|
|
805
|
+
|
|
806
|
+
local instanceCount = codec.instances
|
|
807
|
+
local payloadLength
|
|
808
|
+
|
|
809
|
+
if status ~= OK then
|
|
810
|
+
--[[ A refused reply is the status byte and nothing behind it (`Batch.writeReply`), so
|
|
811
|
+
the packet ends at the cursor and the loop's tail steps over zero bytes. Both of
|
|
812
|
+
these are read by that tail, which is why they are set rather than skipped. ]]
|
|
813
|
+
payloadLength = 0
|
|
814
|
+
instanceCount = 0
|
|
815
|
+
elseif codec.framing == "counted" then
|
|
816
|
+
payloadLength = Buffer.readVarint()
|
|
817
|
+
|
|
818
|
+
if Buffer.rejected() then
|
|
819
|
+
sink.refuse(channel, "parse", "truncated packet length", size - packetStart)
|
|
820
|
+
return
|
|
821
|
+
end
|
|
822
|
+
else
|
|
823
|
+
payloadLength = codec.fixedSize
|
|
824
|
+
end
|
|
825
|
+
|
|
826
|
+
if instanceCount == nil then
|
|
827
|
+
instanceCount = Buffer.readVarint()
|
|
828
|
+
|
|
829
|
+
if Buffer.rejected() then
|
|
830
|
+
sink.refuse(channel, "parse", "truncated instance count", size - packetStart)
|
|
831
|
+
return
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
local payloadStart = Buffer.cursor()
|
|
836
|
+
local payloadEnd = payloadStart + payloadLength
|
|
837
|
+
|
|
838
|
+
if payloadEnd > size then
|
|
839
|
+
--[[ A length that runs off the end of the batch. Refusing the packet is not enough:
|
|
840
|
+
the boundary it claims does not exist, so there is nothing to resynchronise on. ]]
|
|
841
|
+
sink.refuse(channel, "parse", "packet length runs past the batch", size - packetStart)
|
|
842
|
+
return
|
|
843
|
+
end
|
|
844
|
+
|
|
845
|
+
local instanceStart = Buffer.instanceCursor()
|
|
846
|
+
|
|
847
|
+
--[[
|
|
848
|
+
The sidecar's version of the same check, and it is a security one rather than a
|
|
849
|
+
robustness one.
|
|
850
|
+
|
|
851
|
+
A packet is stepped over by advancing the instance cursor past what it claimed, so a
|
|
852
|
+
sender who overstates the claim moves the cursor past entries that belong to the packets
|
|
853
|
+
behind it — and those packets then decode successfully while holding **another packet's
|
|
854
|
+
instances**. Handing a handler the wrong Instance is worse than handing it nothing, and
|
|
855
|
+
it would not look like a failure from either side.
|
|
856
|
+
|
|
857
|
+
An overstated claim is provably a lie, because the sidecar's length is known. There is
|
|
858
|
+
no way to recover the association from that point, so the batch stops.
|
|
859
|
+
]]
|
|
860
|
+
if instanceStart + instanceCount > sidecar then
|
|
861
|
+
sink.refuse(channel, "parse", "instance count runs past the sidecar", size - packetStart)
|
|
862
|
+
return
|
|
863
|
+
end
|
|
864
|
+
local bytes = payloadEnd - packetStart
|
|
865
|
+
|
|
866
|
+
if status ~= OK then
|
|
867
|
+
--[[
|
|
868
|
+
A reply the answering side already refused. There is nothing here to decode and
|
|
869
|
+
nothing to admit: the caller is sitting on a yielded thread and this *is* the answer,
|
|
870
|
+
arriving now rather than after the timeout — which is the whole reason the status
|
|
871
|
+
byte exists instead of a silence the caller has to wait out.
|
|
872
|
+
|
|
873
|
+
It falls through to the same tail as every other packet. An earlier version skipped
|
|
874
|
+
the tail with `continue` on the grounds that a status-only packet has nothing to step
|
|
875
|
+
over; the same shortcut in this loop once skipped the sidecar cursor and reattributed
|
|
876
|
+
instances, so there is exactly one exit from a packet here and this is not an
|
|
877
|
+
exception to it.
|
|
878
|
+
]]
|
|
879
|
+
sink.reply(channel, call :: number, status, nil, bytes)
|
|
880
|
+
else
|
|
881
|
+
--[[ Everything from here is recoverable: the packet's end is known, so whatever happens
|
|
882
|
+
the loop can step over it and carry on. ]]
|
|
883
|
+
local denial, stage = sink.admit(channel, bytes)
|
|
884
|
+
|
|
885
|
+
--[[
|
|
886
|
+
The declared length against the channel's ceiling, before a byte of the payload is
|
|
887
|
+
decoded.
|
|
888
|
+
|
|
889
|
+
The ceiling comes from the schema rather than from the game: every netweave type is
|
|
890
|
+
bounded, so `t.struct({ origin = t.vector3, seq = t.u16 })` can never be more than
|
|
891
|
+
fourteen bytes and a packet claiming more is provably a lie. `RESEARCH §3.7-F` records
|
|
892
|
+
that no surveyed library checks a payload size at all, and the reason is that they
|
|
893
|
+
would have to ask the author for the number — here the schema already knows it
|
|
894
|
+
(`PLAN-M3` D-5).
|
|
895
|
+
|
|
896
|
+
After `admit`, not before. An oversized packet still crossed the wire and still cost
|
|
897
|
+
the server a read, so it spends the sender's budget like any other; checking first
|
|
898
|
+
would let a flood of provably invalid packets run free of the limiter built to stop
|
|
899
|
+
floods.
|
|
900
|
+
]]
|
|
901
|
+
if not denial and ceiling and payloadLength > ceiling then
|
|
902
|
+
denial = overCeilingReason(ceiling)
|
|
903
|
+
end
|
|
904
|
+
|
|
905
|
+
if denial then
|
|
906
|
+
sink.refuse(channel, stage or "budget", denial, bytes, call)
|
|
907
|
+
else
|
|
908
|
+
Buffer.setLimit(payloadEnd)
|
|
909
|
+
|
|
910
|
+
if change then
|
|
911
|
+
--[[
|
|
912
|
+
The subject is read here and the change is not: folding a change into a
|
|
913
|
+
baseline needs one, and this module holds none. The sink is positioned at
|
|
914
|
+
the change with the limit still on the packet's end, so a body that lies
|
|
915
|
+
about its own length runs out of bytes rather than into the next packet.
|
|
916
|
+
|
|
917
|
+
Falls through to the same tail as everything else. An earlier draft of this
|
|
918
|
+
branch stepped the cursor itself and used `continue`, which is the shortcut
|
|
919
|
+
the comment below that tail was written about — it once skipped the sidecar
|
|
920
|
+
cursor and reattributed another packet's instances.
|
|
921
|
+
]]
|
|
922
|
+
local subject, refused = channel.subjectCodec.read()
|
|
923
|
+
|
|
924
|
+
if refused then
|
|
925
|
+
sink.refuse(channel, "parse", refused, bytes, call)
|
|
926
|
+
else
|
|
927
|
+
sink.change(channel, subject, bytes)
|
|
928
|
+
end
|
|
929
|
+
else
|
|
930
|
+
local value, failure = codec.read()
|
|
931
|
+
|
|
932
|
+
--[[ The payload has to end where its length said it would. A reader that stopped
|
|
933
|
+
short leaves bytes the sender declared and nobody read — padding a peer can
|
|
934
|
+
add for free, and a shape that hides a reader consuming less than it should.
|
|
935
|
+
G5 is per packet, and "consumed exactly what it claimed" is the packet-level
|
|
936
|
+
form of it (PLAN-M4-BUG phase 6). A constant reason: it is a per-packet path
|
|
937
|
+
a peer chooses the rate of. ]]
|
|
938
|
+
if not failure and Buffer.cursor() ~= payloadEnd then
|
|
939
|
+
failure = UNCONSUMED
|
|
940
|
+
end
|
|
941
|
+
|
|
942
|
+
if failure then
|
|
943
|
+
sink.refuse(channel, "parse", failure, bytes, call)
|
|
944
|
+
elseif query and not answers then
|
|
945
|
+
sink.reply(channel, call :: number, OK, value, bytes)
|
|
946
|
+
else
|
|
947
|
+
sink.deliver(channel, value, bytes, call)
|
|
948
|
+
end
|
|
949
|
+
end
|
|
950
|
+
end
|
|
951
|
+
end
|
|
952
|
+
|
|
953
|
+
--[[
|
|
954
|
+
Step over the packet, whatever happened to it, and clear the rejection before the next
|
|
955
|
+
id is read.
|
|
956
|
+
|
|
957
|
+
The clear is not tidiness. A rejection is sticky, so without it the *next* packet's id
|
|
958
|
+
read reports the previous packet's failure and the batch stops — the exact failure the
|
|
959
|
+
length prefix is paid for, reintroduced by the code meant to honour it. It was found in
|
|
960
|
+
`bench/envelope.luau` first and reintroduced here on the way out of the benchmark, which
|
|
961
|
+
is why `tests/transport_runtime.luau` pins it.
|
|
962
|
+
]]
|
|
963
|
+
Buffer.setLimit(size)
|
|
964
|
+
Buffer.seek(payloadEnd)
|
|
965
|
+
Buffer.seekInstances(instanceStart + instanceCount)
|
|
966
|
+
Buffer.clearRejection()
|
|
967
|
+
end
|
|
968
|
+
end
|
|
969
|
+
|
|
970
|
+
return Batch
|