2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-10-25 12:55:31 +00:00
|
|
|
|
2024-11-29 14:31:08 +00:00
|
|
|
import std/options, results, chronicles, chronos, metrics, bearssl/rand, stew/byteutils
|
|
|
|
import libp2p/peerid
|
2022-10-25 12:55:31 +00:00
|
|
|
import
|
2024-11-29 14:31:08 +00:00
|
|
|
../waku_core/peers,
|
2023-04-18 13:22:10 +00:00
|
|
|
../node/peer_manager,
|
2024-08-27 14:49:46 +00:00
|
|
|
../node/delivery_monitor/publish_observer,
|
2023-04-18 13:22:10 +00:00
|
|
|
../utils/requests,
|
2023-04-19 11:29:23 +00:00
|
|
|
../waku_core,
|
2024-01-30 12:28:21 +00:00
|
|
|
./common,
|
2022-10-25 12:55:31 +00:00
|
|
|
./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
|
2024-03-15 23:08:47 +00:00
|
|
|
peerManager*: PeerManager
|
|
|
|
rng*: ref rand.HmacDrbgContext
|
2024-08-27 14:49:46 +00:00
|
|
|
publishObservers: seq[PublishObserver]
|
2022-10-25 12:55:31 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc new*(
|
|
|
|
T: type WakuLightPushClient, peerManager: PeerManager, rng: ref rand.HmacDrbgContext
|
|
|
|
): T =
|
2022-10-25 12:55:31 +00:00
|
|
|
WakuLightPushClient(peerManager: peerManager, rng: rng)
|
|
|
|
|
2024-08-27 14:49:46 +00:00
|
|
|
proc addPublishObserver*(wl: WakuLightPushClient, obs: PublishObserver) =
|
|
|
|
wl.publishObservers.add(obs)
|
|
|
|
|
2024-03-15 23:08:47 +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)
|
|
|
|
|
2024-02-06 16:37:42 +00:00
|
|
|
var buffer: seq[byte]
|
|
|
|
try:
|
2024-04-20 03:40:52 +00:00
|
|
|
buffer = await connection.readLp(DefaultMaxRpcSize.int)
|
2024-02-06 16:37:42 +00:00
|
|
|
except LPStreamRemoteClosedError:
|
|
|
|
return err("Exception reading: " & getCurrentExceptionMsg())
|
|
|
|
|
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()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc publish*(
|
|
|
|
wl: WakuLightPushClient,
|
|
|
|
pubSubTopic: PubsubTopic,
|
|
|
|
message: WakuMessage,
|
|
|
|
peer: PeerId | RemotePeerInfo,
|
|
|
|
): Future[WakuLightPushResult[void]] {.async, gcsafe.} =
|
2024-11-29 14:31:08 +00:00
|
|
|
when peer is PeerId:
|
|
|
|
info "publish",
|
|
|
|
peerId = shortLog(peer),
|
|
|
|
msg_hash = computeMessageHash(pubsubTopic, message).to0xHex
|
|
|
|
else:
|
|
|
|
info "publish",
|
|
|
|
peerId = shortLog(peer.peerId),
|
|
|
|
msg_hash = computeMessageHash(pubsubTopic, message).to0xHex
|
|
|
|
|
2023-08-17 12:11:18 +00:00
|
|
|
let pushRequest = PushRequest(pubSubTopic: pubSubTopic, message: message)
|
2024-08-27 14:49:46 +00:00
|
|
|
?await wl.sendPushRequest(pushRequest, peer)
|
|
|
|
|
|
|
|
for obs in wl.publishObservers:
|
|
|
|
obs.onMessagePublished(pubSubTopic, message)
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc publishToAny*(
|
|
|
|
wl: WakuLightPushClient, pubSubTopic: PubsubTopic, message: WakuMessage
|
|
|
|
): Future[WakuLightPushResult[void]] {.async, gcsafe.} =
|
|
|
|
## This proc is similar to the publish one but in this case
|
|
|
|
## we don't specify a particular peer and instead we get it from peer manager
|
|
|
|
|
2024-11-29 14:31:08 +00:00
|
|
|
info "publishToAny", msg_hash = computeMessageHash(pubsubTopic, message).to0xHex
|
|
|
|
|
2024-08-27 14:49:46 +00:00
|
|
|
let peer = wl.peerManager.selectPeer(WakuLightPushCodec).valueOr:
|
|
|
|
return err("could not retrieve a peer supporting WakuLightPushCodec")
|
|
|
|
|
|
|
|
let pushRequest = PushRequest(pubSubTopic: pubSubTopic, message: message)
|
|
|
|
?await wl.sendPushRequest(pushRequest, peer)
|
|
|
|
|
|
|
|
for obs in wl.publishObservers:
|
|
|
|
obs.onMessagePublished(pubSubTopic, message)
|
|
|
|
|
|
|
|
return ok()
|