diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index 7a32a191c..68e59c985 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -249,11 +249,13 @@ proc selectPeer*( var preSelectedPeers = if pm.reputationManager.isSome(): debug "Reputation enabled: consider only non-negative reputation peers" + # FIXME: remove double loop for peer in peers: let rep = try: pm.reputationManager.get().getReputation(peer.peerId) except KeyError: none(bool) + debug "Peer with reputation:", peer=peer, reputation=rep peers.filterIt: let rep = try: diff --git a/waku/waku_lightpush/protocol.nim b/waku/waku_lightpush/protocol.nim index 955b1ade5..aa4769f1c 100644 --- a/waku/waku_lightpush/protocol.nim +++ b/waku/waku_lightpush/protocol.nim @@ -16,7 +16,8 @@ import ./rpc, ./rpc_codec, ./protocol_metrics, - ../common/rate_limit/request_limiter + ../common/rate_limit/request_limiter, + ../incentivization/[eligibility_manager, rpc] logScope: topics = "waku lightpush" @@ -89,6 +90,48 @@ proc handleRequest*( statusDesc: some(desc), ) + # Check eligibility if manager is available + if wl.peerManager.eligibilityManager.isSome(): + debug "eligibilityManager is enabled" + let em = wl.peerManager.eligibilityManager.get() + + try: + debug "checking eligibilityProof..." + + # Check if eligibility proof is provided + if pushRequest.eligibilityProof.isNone(): + let msg = "Eligibility proof is required" + error "lightpush request handling error", error = msg + return LightpushResponse( + requestId: pushRequest.requestId, + statusCode: LightPushErrorCode.PAYMENT_REQUIRED, + statusDesc: some(msg), + ) + + let isEligible = await em.isEligibleTxId(pushRequest.eligibilityProof.get()) + if isEligible.isErr(): + let msg = "Eligibility check failed: " & isEligible.error + error "lightpush request handling error", error = msg + return LightpushResponse( + requestId: pushRequest.requestId, + statusCode: LightPushErrorCode.PAYMENT_REQUIRED, + statusDesc: some(msg), + ) + + debug "Eligibility check passed!" + + except CatchableError: + let msg = "Eligibility check threw exception: " & getCurrentExceptionMsg() + error "lightpush request handling error", error = msg + return LightpushResponse( + requestId: pushRequest.requestId, + statusCode: LightPushErrorCode.PAYMENT_REQUIRED, + statusDesc: some(msg), + ) + else: + # the service node doesn't want to check eligibility + debug "eligibilityManager is disabled - skipping eligibility check" + let relayPeerCount = (await handleRequest(wl, peerId, pushRequest)).valueOr: let desc = error.desc waku_lightpush_v3_errors.inc(labelValues = [$error.code])