mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-07-27 06:53:29 +00:00
* bump nim-libp2p pin to v2.0.0 tag * bump json_rpc to v0.6.1, lsquic to v0.5.1, boringssl to v0.0.8 (latest tags) * add libp2p_mix dep; repoint libp2p/protocols/mix -> libp2p_mix * pin nimble.lock: websock / protobuf_serialization / npeg / jwt * Makefile: add -d:libp2p_quic_support * regenerate nix/deps.nix (adds libp2p_mix, refreshes pins) * migrate rng ref HmacDrbgContext -> libp2p Rng across prod/channels/tests (interface-only; same DRBG) * waku_switch: TransportConfig factory; unified 2.0.0 connection limits (withMaxInOut, withMaxConnections); local MaxConnections * waku_relay/rendezvous/discv5/kademlia: v2.0.0 API (rng, config, ServiceDiscovery rename) * call Service.setup() on post-build switch services (2.0.0 split setup/start) * drop libp2p/utils/semaphore -> chronos AsyncSemaphore * add logos_delivery/waku/compat/option_valueor shim (Option[T] valueOr/withValue, dropped upstream) * add std/options where a transitive re-export was removed * add newStandardSwitch shim (libp2p removed it in 2.0.0); mounts yamux+mplex to match prod muxer * PeerId.random(rng); common.rng()/crypto.newRng(); hoist shared rng (instantiation cleanup) * update expectations for 2.0.0 defaults: DEFAULT_PROTOCOLS += /ipfs/id/push/1.0.0; agent "nim-libp2p" * drop relay reboot/reconnect test (asserted a Switch restart capability that is simply not supported) * fix up a few tests that were flaking on MacOS (libp2p upgrade may have exposed these)
106 lines
3.4 KiB
Nim
106 lines
3.4 KiB
Nim
import logos_delivery/waku/compat/option_valueor
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/strformat,
|
|
stew/byteutils,
|
|
chronicles,
|
|
json_serialization,
|
|
json_serialization/std/options,
|
|
presto/route,
|
|
presto/common
|
|
|
|
import
|
|
logos_delivery/waku/node/peer_manager,
|
|
logos_delivery/waku/waku_lightpush/common,
|
|
../../../waku_node,
|
|
../../handlers,
|
|
../serdes,
|
|
../responses,
|
|
../rest_serdes,
|
|
./types
|
|
|
|
export types
|
|
|
|
logScope:
|
|
topics = "waku node rest lightpush api"
|
|
|
|
const FutTimeoutForPushRequestProcessing* = 5.seconds
|
|
|
|
const NoPeerNoDiscoError = "No suitable service peer & no discovery method"
|
|
const NoPeerNoneFoundError = "No suitable service peer & none discovered"
|
|
|
|
proc useSelfHostedLightPush(node: WakuNode): bool =
|
|
return node.wakuLightPush != nil and node.wakuLightPushClient == nil
|
|
|
|
proc convertErrorKindToHttpStatus(statusCode: LightPushStatusCode): HttpCode =
|
|
## Lightpush status codes are matching HTTP status codes by design
|
|
return toHttpCode(statusCode.int).get(Http500)
|
|
|
|
proc makeRestResponse(response: WakuLightPushResult): RestApiResponse =
|
|
var httpStatus: HttpCode = Http200
|
|
var apiResponse: PushResponse
|
|
|
|
if response.isOk():
|
|
apiResponse.relayPeerCount = some(response.get())
|
|
else:
|
|
httpStatus = convertErrorKindToHttpStatus(response.error().code)
|
|
apiResponse.statusDesc = response.error().desc
|
|
|
|
let restResp = RestApiResponse.jsonResponse(apiResponse, status = httpStatus).valueOr:
|
|
error "An error ocurred while building the json respose: ", error = error
|
|
return RestApiResponse.internalServerError(
|
|
fmt("An error ocurred while building the json respose: {error}")
|
|
)
|
|
|
|
return restResp
|
|
|
|
#### Request handlers
|
|
const ROUTE_LIGHTPUSH = "/lightpush/v3/message"
|
|
|
|
proc installLightPushRequestHandler*(
|
|
router: var RestRouter,
|
|
node: WakuNode,
|
|
discHandler: Option[DiscoveryHandler] = none(DiscoveryHandler),
|
|
) =
|
|
router.api(MethodPost, ROUTE_LIGHTPUSH) do(
|
|
contentBody: Option[ContentBody]
|
|
) -> RestApiResponse:
|
|
## Send a request to push a waku message
|
|
info "post received", ROUTE_LIGHTPUSH
|
|
trace "content body", ROUTE_LIGHTPUSH, contentBody
|
|
|
|
let req: PushRequest = decodeRequestBody[PushRequest](contentBody).valueOr:
|
|
return
|
|
makeRestResponse(lightpushResultBadRequest("Invalid push request! " & $error))
|
|
|
|
let msg = req.message.toWakuMessage().valueOr:
|
|
return makeRestResponse(lightpushResultBadRequest("Invalid message! " & $error))
|
|
|
|
var toPeer = none(RemotePeerInfo)
|
|
if useSelfHostedLightPush(node):
|
|
discard
|
|
else:
|
|
let aPeer = node.peerManager.selectPeer(WakuLightPushCodec).valueOr:
|
|
let handler = discHandler.valueOr:
|
|
return makeRestResponse(lightpushResultServiceUnavailable(NoPeerNoDiscoError))
|
|
|
|
let peerOp = (await handler()).valueOr:
|
|
return makeRestResponse(
|
|
lightpushResultInternalError("No value in peerOp: " & $error)
|
|
)
|
|
|
|
peerOp.valueOr:
|
|
return
|
|
makeRestResponse(lightpushResultServiceUnavailable(NoPeerNoneFoundError))
|
|
toPeer = some(aPeer)
|
|
|
|
let subFut = node.lightpushPublish(req.pubsubTopic, msg, toPeer)
|
|
|
|
if not await subFut.withTimeout(FutTimeoutForPushRequestProcessing):
|
|
error "Failed to request a message push due to timeout!"
|
|
return
|
|
makeRestResponse(lightpushResultServiceUnavailable("Push request timed out"))
|
|
|
|
return makeRestResponse(subFut.value())
|