@@ -17,16 +17,29 @@ import
17
17
export jsonmarshal, routing_table, enr, node
18
18
19
19
# Portal Network JSON-RPC errors
20
+
20
21
const
21
22
# These errors are defined in the portal jsonrpc spec: https://github.com/ethereum/portal-network-specs/tree/master/jsonrpc
22
23
ContentNotFoundError * = (code: - 39001 , msg: " Content not found" )
23
24
ContentNotFoundErrorWithTrace * = (code: - 39002 , msg: " Content not found" )
25
+ PayloadTypeNotSupportedError * = (code: - 39004 , msg: " Payload type not supported" )
26
+ FailedToDecodePayloadError * = (code: - 39005 , msg: " Failed to decode payload" )
27
+ PayloadTypeRequiredError * =
28
+ (code: - 39006 , msg: " Payload type is required if payload is specified" )
29
+ UserSpecifiedPayloadBlockedByClientError * = (
30
+ code: - 39007 ,
31
+ msg: " The client has blocked users from specifying the payload for this extension" ,
32
+ )
33
+
24
34
# These errors are used by Fluffy but are not yet in the spec
25
35
InvalidContentKeyError * = (code: - 32602 , msg: " Invalid content key" )
26
36
InvalidContentValueError * = (code: - 32602 , msg: " Invalid content value" )
27
37
38
+ template applicationError (error: (int , string )): auto =
39
+ (ref ApplicationError )(code: error.code, msg: error.msg)
40
+
28
41
template contentNotFoundErr * (): auto =
29
- ( ref ApplicationError )(code: ContentNotFoundError .code, msg: ContentNotFoundError .msg )
42
+ ContentNotFoundError .applicationError ( )
30
43
31
44
template contentNotFoundErrWithTrace * (data: typed ): auto =
32
45
(ref ApplicationError )(
@@ -35,15 +48,30 @@ template contentNotFoundErrWithTrace*(data: typed): auto =
35
48
data: data,
36
49
)
37
50
38
- template invalidKeyErr * (): auto =
39
- (ref errors.InvalidRequest )(
40
- code: InvalidContentKeyError .code, msg: InvalidContentKeyError .msg
51
+ template payloadTypeNotSupportedError * (): auto =
52
+ (ref ApplicationError )(
53
+ code: PayloadTypeNotSupportedError .code,
54
+ msg: PayloadTypeNotSupportedError .msg,
55
+ data: Opt .some (JsonString (" { \" reason\" : \" client\" }" )),
41
56
)
42
57
58
+ template failedToDecodePayloadError * (): auto =
59
+ FailedToDecodePayloadError .applicationError ()
60
+
61
+ template payloadTypeRequiredError * (): auto =
62
+ PayloadTypeRequiredError .applicationError ()
63
+
64
+ template userSpecifiedPayloadBlockedByClientError * (): auto =
65
+ UserSpecifiedPayloadBlockedByClientError .applicationError ()
66
+
67
+ template invalidRequest (error: (int , string )): auto =
68
+ (ref errors.InvalidRequest )(code: error.code, msg: error.msg)
69
+
70
+ template invalidKeyErr * (): auto =
71
+ InvalidContentKeyError .invalidRequest ()
72
+
43
73
template invalidValueErr * (): auto =
44
- (ref errors.InvalidRequest )(
45
- code: InvalidContentValueError .code, msg: InvalidContentValueError .msg
46
- )
74
+ InvalidContentValueError .invalidRequest ()
47
75
48
76
type
49
77
NodeInfo * = object
54
82
localNodeId* : NodeId
55
83
buckets* : seq [seq [NodeId ]]
56
84
57
- PingResult * = tuple [enrSeq: uint64 , dataRadius: UInt256 ]
85
+ UnknownPayload * = tuple []
86
+
87
+ CapabilitiesPayload * =
88
+ tuple [clientInfo: string , dataRadius: UInt256 , capabilities: seq [uint16 ]]
89
+
90
+ PingResult * = object
91
+ enrSeq* : uint64
92
+ payloadType* : uint16
93
+ payload* : CapabilitiesPayload
58
94
59
95
ContentInfo * = object
60
96
content* : string
68
104
69
105
NodeInfo .useDefaultSerializationIn JrpcConv
70
106
RoutingTableInfo .useDefaultSerializationIn JrpcConv
107
+ PingResult .useDefaultSerializationIn JrpcConv
71
108
(string , string ).useDefaultSerializationIn JrpcConv
72
109
ContentInfo .useDefaultSerializationIn JrpcConv
73
110
PutContentResult .useDefaultSerializationIn JrpcConv
@@ -140,24 +177,30 @@ proc readValue*(
140
177
r.raiseUnexpectedValue (" seq[byte] parser error: " & exc.msg)
141
178
142
179
proc writeValue * (
143
- w: var JsonWriter [JrpcConv ], v: PingResult
180
+ w: var JsonWriter [JrpcConv ], v: CapabilitiesPayload
144
181
) {.gcsafe , raises : [IOError ].} =
145
182
w.beginRecord ()
146
- w.writeField (" enrSeq" , v.enrSeq)
183
+
184
+ w.writeField (" clientInfo" , v.clientInfo)
147
185
# Portal json-rpc specifications allows for dropping leading zeroes.
148
186
w.writeField (" dataRadius" , " 0x" & v.dataRadius.toHex ())
187
+ w.writeField (" capabilities" , v.capabilities)
188
+
149
189
w.endRecord ()
150
190
151
191
proc readValue * (
152
- r: var JsonReader [JrpcConv ], val: var PingResult
192
+ r: var JsonReader [JrpcConv ], val: var CapabilitiesPayload
153
193
) {.gcsafe , raises : [IOError , SerializationError ].} =
154
194
try :
155
195
for field in r.readObjectFields ():
156
196
case field
157
- of " enrSeq " :
158
- val.enrSeq = r.parseInt ( uint64 )
197
+ of " clientInfo " :
198
+ val.clientInfo = r.parseString ( )
159
199
of " dataRadius" :
160
200
val.dataRadius = UInt256 .fromHex (r.parseString ())
201
+ of " capabilities" :
202
+ r.parseArray:
203
+ val.capabilities.add (r.parseInt (uint16 ))
161
204
else :
162
205
discard
163
206
except ValueError as exc:
@@ -181,3 +224,21 @@ proc readValue*(
181
224
182
225
if count != value.len ():
183
226
r.raiseUnexpectedValue (" Array length mismatch" )
227
+
228
+ proc readValue * (
229
+ r: var JsonReader [JrpcConv ], val: var Opt [uint16 ]
230
+ ) {.gcsafe , raises : [IOError , SerializationError ].} =
231
+ if r.tokKind == JsonValueKind .Null :
232
+ reset val
233
+ r.parseNull ()
234
+ else :
235
+ val.ok r.readValue (uint16 )
236
+
237
+ proc readValue * (
238
+ r: var JsonReader [JrpcConv ], val: var Opt [UnknownPayload ]
239
+ ) {.gcsafe , raises : [IOError , SerializationError ].} =
240
+ if r.tokKind == JsonValueKind .Null :
241
+ reset val
242
+ r.parseNull ()
243
+ else :
244
+ val.ok default (UnknownPayload )
0 commit comments