2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
metrics,
|
2022-10-25 12:55:31 +00:00
|
|
|
bearssl/rand
|
2022-08-02 23:47:42 +00:00
|
|
|
import
|
2023-02-06 09:03:30 +00:00
|
|
|
../../node/peer_manager,
|
2022-10-28 14:30:01 +00:00
|
|
|
../waku_message,
|
2022-08-02 23:47:42 +00:00
|
|
|
./rpc,
|
2022-10-25 12:55:31 +00:00
|
|
|
./rpc_codec,
|
|
|
|
./protocol_metrics
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku lightpush"
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
const WakuLightPushCodec* = "/vac/waku/lightpush/2.0.0-beta1"
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
WakuLightPushResult*[T] = Result[T, string]
|
2022-10-25 12:55:31 +00:00
|
|
|
|
2022-11-09 08:55:47 +00:00
|
|
|
PushMessageHandler* = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.gcsafe, closure.}
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
WakuLightPush* = ref object of LPProtocol
|
2022-09-07 15:31:27 +00:00
|
|
|
rng*: ref rand.HmacDrbgContext
|
2022-08-02 23:47:42 +00:00
|
|
|
peerManager*: PeerManager
|
2022-10-25 12:55:31 +00:00
|
|
|
pushHandler*: PushMessageHandler
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc initProtocolHandler*(wl: WakuLightPush) =
|
2022-08-02 23:47:42 +00:00
|
|
|
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
let buffer = await conn.readLp(MaxRpcSize.int)
|
2022-11-07 15:24:16 +00:00
|
|
|
let reqDecodeRes = PushRPC.decode(buffer)
|
2022-10-25 12:55:31 +00:00
|
|
|
if reqDecodeRes.isErr():
|
2022-08-02 23:47:42 +00:00
|
|
|
error "failed to decode rpc"
|
|
|
|
waku_lightpush_errors.inc(labelValues = [decodeRpcFailure])
|
|
|
|
return
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let req = reqDecodeRes.get()
|
2022-11-18 19:01:01 +00:00
|
|
|
if req.request.isNone():
|
2022-10-25 12:55:31 +00:00
|
|
|
error "invalid lightpush rpc received", error=emptyRequestBodyFailure
|
|
|
|
waku_lightpush_errors.inc(labelValues = [emptyRequestBodyFailure])
|
|
|
|
return
|
|
|
|
|
|
|
|
waku_lightpush_messages.inc(labelValues = ["PushRequest"])
|
|
|
|
let
|
2022-11-18 19:01:01 +00:00
|
|
|
pubSubTopic = req.request.get().pubSubTopic
|
|
|
|
message = req.request.get().message
|
2022-10-25 12:55:31 +00:00
|
|
|
debug "push request", peerId=conn.peerId, requestId=req.requestId, pubsubTopic=pubsubTopic
|
|
|
|
|
|
|
|
var response: PushResponse
|
|
|
|
let handleRes = await wl.pushHandler(conn.peerId, pubsubTopic, message)
|
|
|
|
if handleRes.isOk():
|
2022-11-18 19:01:01 +00:00
|
|
|
response = PushResponse(is_success: true, info: some("OK"))
|
2022-10-25 12:55:31 +00:00
|
|
|
else:
|
2022-11-18 19:01:01 +00:00
|
|
|
response = PushResponse(is_success: false, info: some(handleRes.error))
|
2022-10-25 12:55:31 +00:00
|
|
|
waku_lightpush_errors.inc(labelValues = [messagePushFailure])
|
|
|
|
error "pushed message handling failed", error=handleRes.error
|
|
|
|
|
2022-11-18 19:01:01 +00:00
|
|
|
let rpc = PushRPC(requestId: req.requestId, response: some(response))
|
2022-10-25 12:55:31 +00:00
|
|
|
await conn.writeLp(rpc.encode().buffer)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
wl.handler = handle
|
|
|
|
wl.codec = WakuLightPushCodec
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc new*(T: type WakuLightPush,
|
|
|
|
peerManager: PeerManager,
|
|
|
|
rng: ref rand.HmacDrbgContext,
|
|
|
|
pushHandler: PushMessageHandler): T =
|
|
|
|
let wl = WakuLightPush(rng: rng, peerManager: peerManager, pushHandler: pushHandler)
|
|
|
|
wl.initProtocolHandler()
|
2022-08-02 23:47:42 +00:00
|
|
|
return wl
|