NagyZoltanPeter 6d35800fce
Chore: api shape phase2 (#3974)
* Move waku.nim from waku/factory to under waku/
* remove unused
* Realize Kernel API in scope of Waku class
* Refactor waku/api into messaging_client, waku/api/types and api_conf into logos_delivery/api
* Make liblogosdelivery and wakunode2 compile, remove waku/api.nim as it was just a import orchestrator
* make test compile and run
* Reconcile master's new send tests to LogosDelivery API after rebase

master commits #3965/#3669-followup added two test cases (Edge lightpush
delivery #3847, store-validation timeout) written against the removed
waku/api.nim createNode helper. Rewrite them to the LogosDelivery shape:
createNode -> LogosDelivery.new, node.node -> node.waku.node,
node.brokerCtx -> node.waku.brokerCtx, node.send -> node.messagingClient.send,
and drop the now-implicit mountMessagingClient calls (LogosDelivery.new
mounts the client internally).
2026-06-25 09:27:01 +02:00

90 lines
2.7 KiB
Nim

import logos_delivery/waku/compat/option_valueor
import std/[options, times], chronos
import brokers/broker_context
import
logos_delivery/waku/waku_core,
logos_delivery/api/types,
logos_delivery/waku/requests/node_requests
type DeliveryState* {.pure.} = enum
Entry
SuccessfullyPropagated
# message is known to be sent to the network but not yet validated
SuccessfullyValidated
# message is known to be stored at least on one store node, thus validated
FallbackRetry # retry sending with fallback processor if available
NextRoundRetry # try sending in next loop
FailedToDeliver # final state of failed delivery
type DeliveryTask* = ref object
requestId*: RequestId
pubsubTopic*: PubsubTopic
msg*: WakuMessage
msgHash*: WakuMessageHash
tryCount*: int
state*: DeliveryState
deliveryTime*: Moment
firstPropagatedTime*: Option[Moment]
## Set once on the first successful propagation; never reset on re-publish.
## Anchors the store-validation time cap (see propagationAge).
propagateEventEmitted*: bool
errorDesc*: string
proc new*(
T: typedesc[DeliveryTask],
requestId: RequestId,
envelop: MessageEnvelope,
brokerCtx: BrokerContext,
): Result[T, string] =
let msg = envelop.toWakuMessage()
# TODO: use sync request for such as soon as available
let relayShardRes = (
RequestRelayShard.request(brokerCtx, none[PubsubTopic](), envelop.contentTopic)
).valueOr:
error "RequestRelayShard.request failed", error = error
return err("Failed create DeliveryTask: " & $error)
let pubsubTopic = relayShardRes.relayShard.toPubsubTopic()
let msgHash = computeMessageHash(pubsubTopic, msg)
return ok(
T(
requestId: requestId,
pubsubTopic: pubsubTopic,
msg: msg,
msgHash: msgHash,
tryCount: 0,
state: DeliveryState.Entry,
)
)
func `==`*(r, l: DeliveryTask): bool =
if r.isNil() == l.isNil():
r.isNil() or r.msgHash == l.msgHash
else:
false
proc messageAge*(self: DeliveryTask): timer.Duration =
let actual = getNanosecondTime(getTime().toUnixFloat())
if self.msg.timestamp >= 0 and self.msg.timestamp < actual:
nanoseconds(actual - self.msg.timestamp)
else:
ZeroDuration
proc deliveryAge*(self: DeliveryTask): timer.Duration =
if self.state == DeliveryState.SuccessfullyPropagated:
timer.Moment.now() - self.deliveryTime
else:
ZeroDuration
proc propagationAge*(self: DeliveryTask): timer.Duration =
## Time elapsed since the message was first successfully propagated.
## Stable across re-publishes; ZeroDuration until first propagation.
if self.firstPropagatedTime.isSome():
timer.Moment.now() - self.firstPropagatedTime.get()
else:
ZeroDuration
proc isEphemeral*(self: DeliveryTask): bool =
return self.msg.ephemeral