-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RLN proofs as a lightpush service (#2768)
- Loading branch information
Showing
4 changed files
with
163 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
../waku_core, | ||
../waku_relay, | ||
./common, | ||
./protocol, | ||
../waku_rln_relay, | ||
../waku_rln_relay/protocol_types, | ||
../common/ratelimit | ||
import | ||
std/times, | ||
libp2p/peerid, | ||
stew/byteutils | ||
|
||
proc checkAndGenerateRLNProof*(rlnPeer: Option[WakuRLNRelay], message: WakuMessage): Result[WakuMessage, string] = | ||
# check if the message already has RLN proof | ||
if message.proof.len > 0: | ||
return ok(message) | ||
|
||
if rlnPeer.isNone(): | ||
notice "Publishing message without RLN proof" | ||
return ok(message) | ||
# generate and append RLN proof | ||
let | ||
time = getTime().toUnix() | ||
senderEpochTime = float64(time) | ||
var msgWithProof = message | ||
rlnPeer.get().appendRLNProof(msgWithProof, senderEpochTime).isOkOr: | ||
return err(error) | ||
return ok(msgWithProof) | ||
|
||
proc getNilPushHandler*(): PushMessageHandler = | ||
return proc( | ||
peer: PeerId, pubsubTopic: string, message: WakuMessage | ||
): Future[WakuLightPushResult[void]] {.async.} = | ||
return err("no waku relay found") | ||
|
||
proc getRelayPushHandler*( | ||
wakuRelay: WakuRelay, | ||
rlnPeer: Option[WakuRLNRelay] = none[WakuRLNRelay]() | ||
): PushMessageHandler = | ||
return proc( | ||
peer: PeerId, pubsubTopic: string, message: WakuMessage | ||
): Future[WakuLightPushResult[void]] {.async.} = | ||
# append RLN proof | ||
let msgWithProof = checkAndGenerateRLNProof(rlnPeer, message) | ||
if msgWithProof.isErr(): | ||
return err(msgWithProof.error) | ||
|
||
(await wakuRelay.validateMessage(pubSubTopic, msgWithProof.value)).isOkOr: | ||
return err(error) | ||
|
||
let publishedCount = await wakuRelay.publish(pubsubTopic, msgWithProof.value) | ||
if publishedCount == 0: | ||
## Agreed change expected to the lightpush protocol to better handle such case. https://github.com/waku-org/pm/issues/93 | ||
let msgHash = computeMessageHash(pubsubTopic, message).to0xHex() | ||
notice "Lightpush request has not been published to any peers", msg_hash = msgHash | ||
|
||
return ok() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters