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-04-18 13:22:10 +00:00
|
|
|
../node/peer_manager,
|
2023-04-19 11:29:23 +00:00
|
|
|
../waku_core,
|
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]
|
2023-04-11 11:12:45 +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
|
|
|
|
2023-10-09 14:38:23 +00:00
|
|
|
proc handleRequest*(wl: WakuLightPush, peerId: PeerId, buffer: seq[byte]): Future[PushRPC] {.async.} =
|
|
|
|
let reqDecodeRes = PushRPC.decode(buffer)
|
|
|
|
var
|
|
|
|
isSuccess = false
|
|
|
|
pushResponseInfo = ""
|
|
|
|
requestId = ""
|
|
|
|
|
|
|
|
if reqDecodeRes.isErr():
|
|
|
|
pushResponseInfo = decodeRpcFailure & ": " & $reqDecodeRes.error
|
|
|
|
elif reqDecodeRes.get().request.isNone():
|
|
|
|
pushResponseInfo = emptyRequestBodyFailure
|
|
|
|
else:
|
|
|
|
let pushRpcRequest = reqDecodeRes.get();
|
|
|
|
|
|
|
|
requestId = pushRpcRequest.requestId
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
let
|
2023-10-09 14:38:23 +00:00
|
|
|
request = pushRpcRequest.request
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2023-10-09 14:38:23 +00:00
|
|
|
pubSubTopic = request.get().pubSubTopic
|
|
|
|
message = request.get().message
|
|
|
|
waku_lightpush_messages.inc(labelValues = ["PushRequest"])
|
|
|
|
debug "push request", peerId=peerId, requestId=requestId, pubsubTopic=pubsubTopic
|
|
|
|
|
|
|
|
let handleRes = await wl.pushHandler(peerId, pubsubTopic, message)
|
|
|
|
isSuccess = handleRes.isOk()
|
|
|
|
pushResponseInfo = (if isSuccess: "OK" else: handleRes.error)
|
|
|
|
|
|
|
|
if not isSuccess:
|
|
|
|
waku_lightpush_errors.inc(labelValues = [pushResponseInfo])
|
|
|
|
error "failed to push message", error=pushResponseInfo
|
|
|
|
let response = PushResponse(isSuccess: isSuccess, info: some(pushResponseInfo))
|
|
|
|
let rpc = PushRPC(requestId: requestId, response: some(response))
|
|
|
|
return rpc
|
|
|
|
|
|
|
|
proc initProtocolHandler*(wl: WakuLightPush) =
|
|
|
|
proc handle(conn: Connection, proto: string) {.async.} =
|
|
|
|
let buffer = await conn.readLp(MaxRpcSize.int)
|
|
|
|
let rpc = await handleRequest(wl, conn.peerId, buffer)
|
|
|
|
await conn.writeLp(rpc.encode().buffer)
|
2022-08-02 23:47:42 +00:00
|
|
|
wl.handler = handle
|
|
|
|
wl.codec = WakuLightPushCodec
|
|
|
|
|
2023-04-11 11:12:45 +00:00
|
|
|
proc new*(T: type WakuLightPush,
|
|
|
|
peerManager: PeerManager,
|
2022-10-25 12:55:31 +00:00
|
|
|
rng: ref rand.HmacDrbgContext,
|
2023-04-11 11:12:45 +00:00
|
|
|
pushHandler: PushMessageHandler): T =
|
2022-10-25 12:55:31 +00:00
|
|
|
let wl = WakuLightPush(rng: rng, peerManager: peerManager, pushHandler: pushHandler)
|
|
|
|
wl.initProtocolHandler()
|
2022-08-02 23:47:42 +00:00
|
|
|
return wl
|