-
Notifications
You must be signed in to change notification settings - Fork 57
/
handlers.nim
294 lines (222 loc) · 10 KB
/
handlers.nim
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
{.push raises: [].}
import
std/sequtils,
stew/byteutils,
results,
chronicles,
json_serialization,
json_serialization/std/options,
presto/route,
presto/common
import
../../../waku_node,
../../../waku_relay/protocol,
../../../waku_rln_relay,
../../../node/waku_node,
../../message_cache,
../../handlers,
../serdes,
../responses,
../rest_serdes,
./types
from std/times import getTime
from std/times import toUnix
export types
logScope:
topics = "waku node rest relay_api"
##### Topic cache
const futTimeout* = 5.seconds # Max time to wait for futures
#### Request handlers
const ROUTE_RELAY_SUBSCRIPTIONSV1* = "/relay/v1/subscriptions"
const ROUTE_RELAY_MESSAGESV1* = "/relay/v1/messages/{pubsubTopic}"
const ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1* = "/relay/v1/auto/subscriptions"
const ROUTE_RELAY_AUTO_MESSAGESV1* = "/relay/v1/auto/messages/{contentTopic}"
const ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC* = "/relay/v1/auto/messages"
proc installRelayApiHandlers*(
router: var RestRouter, node: WakuNode, cache: MessageCache
) =
router.api(MethodOptions, ROUTE_RELAY_SUBSCRIPTIONSV1) do() -> RestApiResponse:
return RestApiResponse.ok()
router.api(MethodPost, ROUTE_RELAY_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
## Subscribes a node to a list of PubSub topics
debug "post_waku_v2_relay_v1_subscriptions"
# Check the request body
if contentBody.isNone():
return RestApiResponse.badRequest()
let req: seq[PubsubTopic] = decodeRequestBody[seq[PubsubTopic]](contentBody).valueOr:
return error
# Only subscribe to topics for which we have no subscribed topic handlers yet
let newTopics = req.filterIt(not cache.isPubsubSubscribed(it))
for pubsubTopic in newTopics:
cache.pubsubSubscribe(pubsubTopic)
node.subscribe(
(kind: PubsubSub, topic: pubsubTopic), some(messageCacheHandler(cache))
)
return RestApiResponse.ok()
router.api(MethodDelete, ROUTE_RELAY_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
# ## Subscribes a node to a list of PubSub topics
# debug "delete_waku_v2_relay_v1_subscriptions"
# Check the request body
if contentBody.isNone():
return RestApiResponse.badRequest()
let req: seq[PubsubTopic] = decodeRequestBody[seq[PubsubTopic]](contentBody).valueOr:
return error
# Unsubscribe all handlers from requested topics
for pubsubTopic in req:
cache.pubsubUnsubscribe(pubsubTopic)
node.unsubscribe((kind: PubsubUnsub, topic: pubsubTopic))
# Successfully unsubscribed from all requested topics
return RestApiResponse.ok()
router.api(MethodOptions, ROUTE_RELAY_MESSAGESV1) do(
pubsubTopic: string
) -> RestApiResponse:
return RestApiResponse.ok()
router.api(MethodGet, ROUTE_RELAY_MESSAGESV1) do(
pubsubTopic: string
) -> RestApiResponse:
# ## Returns all WakuMessages received on a PubSub topic since the
# ## last time this method was called
# ## TODO: ability to specify a return message limit
# debug "get_waku_v2_relay_v1_messages", topic=topic
if pubsubTopic.isErr():
return RestApiResponse.badRequest()
let pubSubTopic = pubsubTopic.get()
let messages = cache.getMessages(pubSubTopic, clear = true)
if messages.isErr():
debug "Not subscribed to topic", topic = pubSubTopic
return RestApiResponse.notFound()
let data = RelayGetMessagesResponse(messages.get().map(toRelayWakuMessage))
let resp = RestApiResponse.jsonResponse(data, status = Http200)
if resp.isErr():
debug "An error ocurred while building the json respose", error = resp.error
return RestApiResponse.internalServerError()
return resp.get()
router.api(MethodPost, ROUTE_RELAY_MESSAGESV1) do(
pubsubTopic: string, contentBody: Option[ContentBody]
) -> RestApiResponse:
if pubsubTopic.isErr():
return RestApiResponse.badRequest()
let pubSubTopic = pubsubTopic.get()
# ensure the node is subscribed to the topic. otherwise it risks publishing
# to a topic with no connected peers
if pubSubTopic notin node.wakuRelay.subscribedTopics():
return RestApiResponse.badRequest(
"Failed to publish: Node not subscribed to topic: " & pubsubTopic
)
# Check the request body
if contentBody.isNone():
return RestApiResponse.badRequest()
let reqWakuMessage: RelayWakuMessage = decodeRequestBody[RelayWakuMessage](
contentBody
).valueOr:
return error
var message: WakuMessage = reqWakuMessage.toWakuMessage(version = 0).valueOr:
return RestApiResponse.badRequest($error)
# if RLN is mounted, append the proof to the message
if not node.wakuRlnRelay.isNil():
# append the proof to the message
node.wakuRlnRelay.appendRLNProof(message, float64(getTime().toUnix())).isOkOr:
return RestApiResponse.internalServerError(
"Failed to publish: error appending RLN proof to message: " & $error
)
(await node.wakuRelay.validateMessage(pubsubTopic, message)).isOkOr:
return RestApiResponse.badRequest("Failed to publish: " & error)
# Log for message tracking purposes
logMessageInfo(node.wakuRelay, "rest", pubsubTopic, "none", message, onRecv = true)
# if we reach here its either a non-RLN message or a RLN message with a valid proof
debug "Publishing message",
pubSubTopic = pubSubTopic, rln = not node.wakuRlnRelay.isNil()
if not (waitFor node.publish(some(pubSubTopic), message).withTimeout(futTimeout)):
error "Failed to publish message to topic", pubSubTopic = pubSubTopic
return RestApiResponse.internalServerError("Failed to publish: timedout")
return RestApiResponse.ok()
# Autosharding API
router.api(MethodOptions, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do() -> RestApiResponse:
return RestApiResponse.ok()
router.api(MethodPost, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
## Subscribes a node to a list of content topics.
debug "post_waku_v2_relay_v1_auto_subscriptions"
let req: seq[ContentTopic] = decodeRequestBody[seq[ContentTopic]](contentBody).valueOr:
return error
# Only subscribe to topics for which we have no subscribed topic handlers yet
let newTopics = req.filterIt(not cache.isContentSubscribed(it))
for contentTopic in newTopics:
cache.contentSubscribe(contentTopic)
node.subscribe(
(kind: ContentSub, topic: contentTopic), some(messageCacheHandler(cache))
)
return RestApiResponse.ok()
router.api(MethodDelete, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
## Unsubscribes a node from a list of content topics.
debug "delete_waku_v2_relay_v1_auto_subscriptions"
let req: seq[ContentTopic] = decodeRequestBody[seq[ContentTopic]](contentBody).valueOr:
return error
for contentTopic in req:
cache.contentUnsubscribe(contentTopic)
node.unsubscribe((kind: ContentUnsub, topic: contentTopic))
return RestApiResponse.ok()
router.api(MethodOptions, ROUTE_RELAY_AUTO_MESSAGESV1) do(
contentTopic: string
) -> RestApiResponse:
return RestApiResponse.ok()
router.api(MethodGet, ROUTE_RELAY_AUTO_MESSAGESV1) do(
contentTopic: string
) -> RestApiResponse:
## Returns all WakuMessages received on a content topic since the
## last time this method was called.
debug "get_waku_v2_relay_v1_auto_messages", contentTopic = contentTopic
let contentTopic = contentTopic.valueOr:
return RestApiResponse.badRequest($error)
let messages = cache.getAutoMessages(contentTopic, clear = true).valueOr:
debug "Not subscribed to topic", topic = contentTopic
return RestApiResponse.notFound(contentTopic)
let data = RelayGetMessagesResponse(messages.map(toRelayWakuMessage))
return RestApiResponse.jsonResponse(data, status = Http200).valueOr:
debug "An error ocurred while building the json respose", error = error
return RestApiResponse.internalServerError($error)
router.api(MethodOptions, ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC) do() -> RestApiResponse:
return RestApiResponse.ok()
router.api(MethodPost, ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
# Check the request body
if contentBody.isNone():
return RestApiResponse.badRequest()
let req: RelayWakuMessage = decodeRequestBody[RelayWakuMessage](contentBody).valueOr:
return error
if req.contentTopic.isNone():
return RestApiResponse.badRequest()
var message: WakuMessage = req.toWakuMessage(version = 0).valueOr:
return RestApiResponse.badRequest()
let pubsubTopic = node.wakuSharding.getShard(message.contentTopic).valueOr:
let msg = "Autosharding error: " & error
error "publish error", err = msg
return RestApiResponse.badRequest("Failed to publish. " & msg)
# if RLN is mounted, append the proof to the message
if not node.wakuRlnRelay.isNil():
node.wakuRlnRelay.appendRLNProof(message, float64(getTime().toUnix())).isOkOr:
return RestApiResponse.internalServerError(
"Failed to publish: error appending RLN proof to message: " & $error
)
(await node.wakuRelay.validateMessage(pubsubTopic, message)).isOkOr:
return RestApiResponse.badRequest("Failed to publish: " & error)
# Log for message tracking purposes
logMessageInfo(node.wakuRelay, "rest", pubsubTopic, "none", message, onRecv = true)
# if we reach here its either a non-RLN message or a RLN message with a valid proof
debug "Publishing message",
contentTopic = message.contentTopic, rln = not node.wakuRlnRelay.isNil()
var publishFut = node.publish(some($pubsubTopic), message)
if not await publishFut.withTimeout(futTimeout):
return RestApiResponse.internalServerError("Failed to publish: timedout")
var res = publishFut.read()
if res.isErr():
return RestApiResponse.badRequest("Failed to publish. " & res.error)
return RestApiResponse.ok()