Repository navigation
Expand file tree
/
Copy pathPayload.lean
More file actions
75 lines (55 loc) · 2.67 KB
/
Copy pathPayload.lean
File metadata and controls
75 lines (55 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module
public import EthLib.Networking.ReqResp.Encoding
public import EthLib.Networking.Snappy.Block
/-!
# `Networking.Gossip.Payload`
The payload of a gossip message, and the size limits on it.
The data field of a gossip message is the SSZ bytes of the message, as one Snappy block.
A client rejects data longer than the worst-case compression of the largest payload.
It also rejects a payload above the payload cap or the size bound of the message type.
The limits are the same at every fork.
-/
@[expose] public section
namespace EthLib.Networking.Gossip
open EthLib.Base (ArithError)
open EthLib.Networking.ReqResp (MAX_PAYLOAD_SIZE maxCompressedLen)
/-- Every way the data of a gossip message can be rejected. -/
inductive PayloadError where
/-- The bound on the compressed size is 2^64 or more. -/
| arith (error : ArithError)
/-- The data is longer than the worst-case compression of the largest payload. -/
| compressedTooLong (size bound : Nat)
/-- The data is not a valid Snappy block. -/
| snappy (error : Snappy.SnappyError)
/-- The payload is above the payload cap or the size bound of the message type. -/
| payloadTooLong (size bound : Nat)
deriving Repr, DecidableEq
/--
The stable code of an error, one per constructor, for the C exports.
The code ignores the arguments of the constructor, which the message carries.
A new constructor takes a new code, so that each code keeps its meaning.
-/
def PayloadError.code : PayloadError → UInt32
| .arith _ => 1
| .compressedTooLong .. => 2
| .snappy _ => 3
| .payloadTooLong .. => 4
/--
The SSZ bytes of the data of a gossip message, given the size bound of its SSZ type.
The data may be at most `32 + n + n / 6` bytes long, for the payload cap `n`.
The payload must decompress, and be at most the smaller of the cap and the bound.
A length above that is rejected from the preamble of the data, before anything is decompressed.
The caller deserializes the bytes, since it knows their SSZ type.
-/
def decodePayload (typeBound : UInt64) (data : ByteArray) : Except PayloadError ByteArray := do
let compressedBound ← (maxCompressedLen MAX_PAYLOAD_SIZE).mapError .arith
if compressedBound.toNat < data.size then
throw (.compressedTooLong data.size compressedBound.toNat)
let bound := (min typeBound MAX_PAYLOAD_SIZE).toNat
-- The preamble declares the payload length, so a long payload is rejected unread.
if let .ok declared := Snappy.declaredLength data then
if bound < declared then throw (.payloadTooLong declared bound)
let payload ← (Snappy.decompress data).mapError .snappy
if bound < payload.size then throw (.payloadTooLong payload.size bound)
return payload
end EthLib.Networking.Gossip