Repository navigation
Expand file tree
/
Copy pathRpc.lean
More file actions
303 lines (233 loc) · 11.7 KB
/
Copy pathRpc.lean
File metadata and controls
303 lines (233 loc) · 11.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
module
public import EthLib.Networking.Encoding.Protobuf
/-!
# `Networking.Gossip.Rpc`
The RPC that carries gossip between two peers, from [pubsub, "The RPC"][rpc].
An RPC holds subscriptions, published messages and control messages.
The control messages follow the schemas of gossipsub [v1.0][v10], [v1.1][v11] and [v1.2][v12].
The consensus layer applies the `StrictNoSign` [signature policy][policy].
So a message must not hold the field `from`, `seqno`, `signature` or `key`, not even empty.
A message must hold a topic, since [the message schema][message] makes it required.
Topics are UTF-8 strings, and [proto2][string] asks every string to be UTF-8.
An RPC is rejected when any topic or topic ID is not valid UTF-8, which is a choice of EthLib.
A field the schema does not name is skipped.
A field the schema names but with another wire type is rejected, which is a choice of EthLib.
The four forbidden fields are rejected whatever their wire type.
A missing optional field reads as its default: false, zero or empty.
A singular field that occurs twice keeps its last value, as [the encoding guide][last] says.
Each occurrence of the control field is decoded alone, then their lists are joined in order.
[rpc]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/README.md#the-rpc
[message]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/README.md#the-message
[policy]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/README.md#signature-policy-options
[v10]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/gossipsub/gossipsub-v1.0.md#protobuf
[v11]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/gossipsub/gossipsub-v1.1.md#protobuf
[v12]: https://github.com/libp2p/specs/blob/c750236de43cd8b246a824b377dfdb26bae686db/pubsub/gossipsub/gossipsub-v1.2.md#protobuf-extension
[string]: https://github.com/protocolbuffers/protocolbuffers.github.io/blob/bc2f346991e20631ed0a884e345110738228bfae/content/programming-guides/proto2.md#scalar-value-types-scalar
[last]: https://github.com/protocolbuffers/protocolbuffers.github.io/blob/bc2f346991e20631ed0a884e345110738228bfae/content/programming-guides/encoding.md#last-one-wins-last-one-wins
-/
@[expose] public section
namespace EthLib.Networking.Gossip
open EthLib.Networking.Encoding.Protobuf
/-- A subscription change: subscribe to a topic, or unsubscribe from it. -/
structure SubOpts where
subscribe : Bool := false
topicId : String := ""
deriving DecidableEq
/-- A published message, under the `StrictNoSign` policy. -/
structure Message where
data : ByteArray := .empty
topic : String
deriving DecidableEq
/-- IHAVE: the IDs of messages the sender has seen on a topic. -/
structure IHave where
topicId : String := ""
messageIds : List ByteArray := []
deriving DecidableEq
/-- IWANT: the IDs of messages the sender asks for. -/
structure IWant where
messageIds : List ByteArray := []
deriving DecidableEq
/-- GRAFT: the sender added the receiver to its mesh of a topic. -/
structure Graft where
topicId : String := ""
deriving DecidableEq
/-- A peer offered in a PRUNE, with its signed peer record when the sender has one. -/
structure PeerInfo where
peerId : ByteArray := .empty
signedPeerRecord : ByteArray := .empty
deriving DecidableEq
/-- PRUNE: the sender removed the receiver from its mesh of a topic.
It may offer other peers, and a backoff in seconds before the receiver grafts again. -/
structure Prune where
topicId : String := ""
peers : List PeerInfo := []
backoff : UInt64 := 0
deriving DecidableEq
/-- IDONTWANT: the IDs of messages the sender does not want to receive. -/
structure IDontWant where
messageIds : List ByteArray := []
deriving DecidableEq
/-- The control messages of one RPC. -/
structure Control where
ihave : List IHave := []
iwant : List IWant := []
graft : List Graft := []
prune : List Prune := []
idontwant : List IDontWant := []
deriving DecidableEq
/-- One RPC: subscription changes, published messages and control messages. -/
structure Rpc where
subscriptions : List SubOpts := []
publish : List Message := []
control : Control := {}
deriving DecidableEq
/-- Every way an RPC can be rejected. -/
inductive RpcError where
/-- The bytes are not a sequence of protobuf records. -/
| protobuf (error : ProtobufError)
/-- A message holds the field `from` (1), `seqno` (3), `signature` (5) or `key` (6). -/
| signed (field : Nat)
/-- A message holds no topic. -/
| missingTopic
/-- A field of the schema has another wire type than its type. -/
| wireType (field wire : Nat)
/-- A topic is not valid UTF-8. -/
| utf8 (field : Nat)
deriving Repr, DecidableEq
/-- The stable code of an error, one per constructor, for the C exports.
A protobuf error keeps one code; which one it is shows only in the message.
The codes start at 101, so they never collide with the codes 1 to 4 of the payload errors. -/
def RpcError.code : RpcError → UInt32
| .protobuf _ => 101
| .signed _ => 102
| .missingTopic => 103
| .wireType .. => 104
| .utf8 _ => 105
/-- The field numbers that `StrictNoSign` forbids in a message. -/
def signedFields : List Nat := [1, 3, 5, 6]
/-! ## Decoding -/
/-- The bytes of a length-delimited value. -/
def bytesOf (field : Nat) : Value → Except RpcError ByteArray
| .len bytes => .ok bytes
| v => .error (.wireType field v.wireType)
/-- The string of a length-delimited value, which must be valid UTF-8. -/
def stringOf (field : Nat) (v : Value) : Except RpcError String := do
match String.fromUTF8? (← bytesOf field v) with
| some s => return s
| none => throw (.utf8 field)
/-- The number of a varint value. -/
def uint64Of (field : Nat) : Value → Except RpcError UInt64
| .varint v => .ok v
| v => .error (.wireType field v.wireType)
/-- Every value of a repeated field, each read by the given reader. -/
def repeated (read : Nat → Value → Except RpcError α) (rs : List Record) (field : Nat) :
Except RpcError (List α) :=
(valuesOf rs field).mapM (read field)
/-- The last value of a singular field, or the default when it is missing.
Every occurrence must still be well formed. -/
def singular (read : Nat → Value → Except RpcError α) (default : α) (rs : List Record)
(field : Nat) : Except RpcError α :=
return (← repeated read rs field).getLastD default
/-- The records of a message. -/
def recordsOf (bytes : ByteArray) : Except RpcError (List Record) :=
(records bytes.data.toList).mapError .protobuf
/-- A subscription change. -/
def decodeSubOpts (bytes : ByteArray) : Except RpcError SubOpts := do
let rs ← recordsOf bytes
-- A bool is written as 0 or 1.
--
-- Reading any other value as true is a choice of EthLib.
let subscribe ← singular uint64Of 0 rs 1
return { subscribe := subscribe != 0, topicId := ← singular stringOf "" rs 2 }
/-- A published message, which `StrictNoSign` forbids to hold any of four fields. -/
def decodeMessage (bytes : ByteArray) : Except RpcError Message := do
let rs ← recordsOf bytes
if let some r := rs.find? (·.field ∈ signedFields) then throw (.signed r.field)
let data ← singular bytesOf .empty rs 2
let some topic := (← repeated stringOf rs 4).getLast? | throw .missingTopic
return { data, topic }
/-- An IHAVE message. -/
def decodeIHave (bytes : ByteArray) : Except RpcError IHave := do
let rs ← recordsOf bytes
return { topicId := ← singular stringOf "" rs 1, messageIds := ← repeated bytesOf rs 2 }
/-- An IWANT message. -/
def decodeIWant (bytes : ByteArray) : Except RpcError IWant := do
return { messageIds := ← repeated bytesOf (← recordsOf bytes) 1 }
/-- A GRAFT message. -/
def decodeGraft (bytes : ByteArray) : Except RpcError Graft := do
return { topicId := ← singular stringOf "" (← recordsOf bytes) 1 }
/-- A peer offered in a PRUNE. -/
def decodePeerInfo (bytes : ByteArray) : Except RpcError PeerInfo := do
let rs ← recordsOf bytes
return {
peerId := ← singular bytesOf .empty rs 1
signedPeerRecord := ← singular bytesOf .empty rs 2 }
/-- A PRUNE message. -/
def decodePrune (bytes : ByteArray) : Except RpcError Prune := do
let rs ← recordsOf bytes
let peers ← (← repeated bytesOf rs 2).mapM decodePeerInfo
return { topicId := ← singular stringOf "" rs 1, peers, backoff := ← singular uint64Of 0 rs 3 }
/-- An IDONTWANT message. -/
def decodeIDontWant (bytes : ByteArray) : Except RpcError IDontWant := do
return { messageIds := ← repeated bytesOf (← recordsOf bytes) 1 }
/-- The control messages of an RPC. -/
def decodeControl (bytes : ByteArray) : Except RpcError Control := do
let rs ← recordsOf bytes
return {
ihave := ← (← repeated bytesOf rs 1).mapM decodeIHave
iwant := ← (← repeated bytesOf rs 2).mapM decodeIWant
graft := ← (← repeated bytesOf rs 3).mapM decodeGraft
prune := ← (← repeated bytesOf rs 4).mapM decodePrune
idontwant := ← (← repeated bytesOf rs 5).mapM decodeIDontWant }
/-- Merge two control messages: each list of the second follows that of the first. -/
def Control.merge (a b : Control) : Control :=
⟨a.ihave ++ b.ihave, a.iwant ++ b.iwant, a.graft ++ b.graft, a.prune ++ b.prune,
a.idontwant ++ b.idontwant⟩
/-- An RPC.
The control field is singular: each occurrence is decoded alone, then they merge. -/
def decodeRpc (bytes : ByteArray) : Except RpcError Rpc := do
let rs ← recordsOf bytes
let subscriptions ← (← repeated bytesOf rs 1).mapM decodeSubOpts
let publish ← (← repeated bytesOf rs 2).mapM decodeMessage
let control := (← (← repeated bytesOf rs 3).mapM decodeControl).foldl Control.merge {}
return { subscriptions, publish, control }
/-! ## Encoding
The encoder writes every singular field.
It writes the control field only when a control list is not empty.
-/
/-- The bytes of a subscription change. -/
def SubOpts.encode (s : SubOpts) : ByteArray :=
serialize [⟨1, .varint (if s.subscribe then 1 else 0)⟩, stringRecord 2 s.topicId]
/-- The bytes of a message: its data, then its topic. -/
def Message.encode (m : Message) : ByteArray :=
serialize [lenRecord 2 m.data, stringRecord 4 m.topic]
/-- The bytes of an IHAVE message. -/
def IHave.encode (m : IHave) : ByteArray :=
serialize (stringRecord 1 m.topicId :: m.messageIds.map (lenRecord 2))
/-- The bytes of an IWANT message. -/
def IWant.encode (m : IWant) : ByteArray :=
serialize (m.messageIds.map (lenRecord 1))
/-- The bytes of a GRAFT message. -/
def Graft.encode (m : Graft) : ByteArray :=
serialize [stringRecord 1 m.topicId]
/-- The bytes of a peer offered in a PRUNE. -/
def PeerInfo.encode (p : PeerInfo) : ByteArray :=
serialize [lenRecord 1 p.peerId, lenRecord 2 p.signedPeerRecord]
/-- The bytes of a PRUNE message. -/
def Prune.encode (m : Prune) : ByteArray :=
serialize (stringRecord 1 m.topicId :: m.peers.map (lenRecord 2 ∘ PeerInfo.encode) ++
[⟨3, .varint m.backoff⟩])
/-- The bytes of an IDONTWANT message. -/
def IDontWant.encode (m : IDontWant) : ByteArray :=
serialize (m.messageIds.map (lenRecord 1))
/-- The bytes of the control messages of an RPC. -/
def Control.encode (c : Control) : ByteArray :=
serialize (c.ihave.map (lenRecord 1 ∘ IHave.encode) ++
c.iwant.map (lenRecord 2 ∘ IWant.encode) ++ c.graft.map (lenRecord 3 ∘ Graft.encode) ++
c.prune.map (lenRecord 4 ∘ Prune.encode) ++ c.idontwant.map (lenRecord 5 ∘ IDontWant.encode))
/-- The bytes of an RPC. -/
def Rpc.encode (rpc : Rpc) : ByteArray :=
serialize (rpc.subscriptions.map (lenRecord 1 ∘ SubOpts.encode) ++
rpc.publish.map (lenRecord 2 ∘ Message.encode) ++
if rpc.control = {} then [] else [lenRecord 3 rpc.control.encode])
end EthLib.Networking.Gossip