2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
metrics,
|
|
|
|
bearssl/rand
|
|
|
|
import
|
2023-04-18 13:22:10 +00:00
|
|
|
../node/peer_manager,
|
|
|
|
../utils/requests,
|
2023-04-19 11:29:23 +00:00
|
|
|
../waku_core,
|
2022-10-25 12:55:31 +00:00
|
|
|
./protocol,
|
|
|
|
./protocol_metrics,
|
|
|
|
./rpc,
|
|
|
|
./rpc_codec
|
|
|
|
|
|
|
|
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
topics = "waku lightpush client"
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
type WakuLightPushClient* = ref object
|
|
|
|
peerManager*: PeerManager
|
|
|
|
rng*: ref rand.HmacDrbgContext
|
|
|
|
|
|
|
|
|
2023-04-18 13:22:10 +00:00
|
|
|
proc new*(T: type WakuLightPushClient,
|
|
|
|
peerManager: PeerManager,
|
2022-10-25 12:55:31 +00:00
|
|
|
rng: ref rand.HmacDrbgContext): T =
|
|
|
|
WakuLightPushClient(peerManager: peerManager, rng: rng)
|
|
|
|
|
|
|
|
|
2023-04-18 13:22:10 +00:00
|
|
|
proc sendPushRequest(wl: WakuLightPushClient, req: PushRequest, peer: PeerId|RemotePeerInfo): Future[WakuLightPushResult[void]] {.async, gcsafe.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
let connOpt = await wl.peerManager.dialPeer(peer, WakuLightPushCodec)
|
|
|
|
if connOpt.isNone():
|
|
|
|
waku_lightpush_errors.inc(labelValues = [dialFailure])
|
|
|
|
return err(dialFailure)
|
|
|
|
let connection = connOpt.get()
|
|
|
|
|
2022-11-18 19:01:01 +00:00
|
|
|
let rpc = PushRPC(requestId: generateRequestId(wl.rng), request: some(req))
|
2022-10-25 12:55:31 +00:00
|
|
|
await connection.writeLP(rpc.encode().buffer)
|
|
|
|
|
2022-10-28 14:30:01 +00:00
|
|
|
var buffer = await connection.readLp(MaxRpcSize.int)
|
2022-11-07 15:24:16 +00:00
|
|
|
let decodeRespRes = PushRPC.decode(buffer)
|
2022-10-25 12:55:31 +00:00
|
|
|
if decodeRespRes.isErr():
|
|
|
|
error "failed to decode response"
|
|
|
|
waku_lightpush_errors.inc(labelValues = [decodeRpcFailure])
|
|
|
|
return err(decodeRpcFailure)
|
|
|
|
|
|
|
|
let pushResponseRes = decodeRespRes.get()
|
2022-11-18 19:01:01 +00:00
|
|
|
if pushResponseRes.response.isNone():
|
2022-10-25 12:55:31 +00:00
|
|
|
waku_lightpush_errors.inc(labelValues = [emptyResponseBodyFailure])
|
|
|
|
return err(emptyResponseBodyFailure)
|
|
|
|
|
2022-11-18 19:01:01 +00:00
|
|
|
let response = pushResponseRes.response.get()
|
2022-10-25 12:55:31 +00:00
|
|
|
if not response.isSuccess:
|
2022-11-18 19:01:01 +00:00
|
|
|
if response.info.isSome():
|
|
|
|
return err(response.info.get())
|
2022-10-25 12:55:31 +00:00
|
|
|
else:
|
|
|
|
return err("unknown failure")
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
2023-08-17 12:11:18 +00:00
|
|
|
proc publish*(wl: WakuLightPushClient, pubSubTopic: PubsubTopic, message: WakuMessage, peer: PeerId|RemotePeerInfo): Future[WakuLightPushResult[void]] {.async, gcsafe.} =
|
|
|
|
let pushRequest = PushRequest(pubSubTopic: pubSubTopic, message: message)
|
|
|
|
return await wl.sendPushRequest(pushRequest, peer)
|