Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better proof handling in REST #3286

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/testlib/wakucore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ proc fakeWakuMessage*(
meta: string | seq[byte] = newSeq[byte](),
ts = now(),
ephemeral = false,
proof = newSeq[byte](),
): WakuMessage =
var payloadBytes: seq[byte]
var metaBytes: seq[byte]
Expand All @@ -71,4 +72,5 @@ proc fakeWakuMessage*(
version: 2,
timestamp: ts,
ephemeral: ephemeral,
proof: proof,
)
1 change: 0 additions & 1 deletion tests/waku_rln_relay/test_rln_group_manager_onchain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ suite "Onchain group manager":
(await manager.startGroupSync()).isOkOr:
raiseAssert $error


asyncTest "startGroupSync: should guard against uninitialized state":
(await manager.startGroupSync()).isErrOr:
raiseAssert "Expected error when not initialized"
Expand Down
23 changes: 23 additions & 0 deletions tests/wakunode_rest/test_rest_lightpush.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ proc shutdown(self: RestLightPushTest) {.async.} =
await allFutures(self.serviceNode.stop(), self.pushNode.stop())

suite "Waku v2 Rest API - lightpush":
asyncTest "Push message with proof":
let restLightPushTest = await RestLightPushTest.init()

let message: RelayWakuMessage = fakeWakuMessage(
contentTopic = DefaultContentTopic,
payload = toBytes("TEST-1"),
proof = toBytes("proof-test"),
)
.toRelayWakuMessage()

check message.proof.isSome()

let requestBody =
PushRequest(pubsubTopic: some(DefaultPubsubTopic), message: message)

let response = await restLightPushTest.client.sendPushRequest(body = requestBody)

## Validate that the push request failed because the node is not
## connected to other node but, doesn't fail because of not properly
## handling the proof message attribute within the REST request.
check:
response.data == "Failed to request a message push: not_published_to_any_peer"

asyncTest "Push message request":
# Given
let restLightPushTest = await RestLightPushTest.init()
Expand Down
10 changes: 10 additions & 0 deletions waku/waku_api/rest/relay/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type RelayWakuMessage* = object
timestamp*: Option[int64]
meta*: Option[Base64String]
ephemeral*: Option[bool]
proof*: Option[Base64String]

type
RelayGetMessagesResponse* = seq[RelayWakuMessage]
Expand All @@ -36,6 +37,7 @@ proc toRelayWakuMessage*(msg: WakuMessage): RelayWakuMessage =
else:
none(Base64String),
ephemeral: some(msg.ephemeral),
proof: some(base64.encode(msg.proof)),
)

proc toWakuMessage*(msg: RelayWakuMessage, version = 0): Result[WakuMessage, string] =
Expand All @@ -45,6 +47,7 @@ proc toWakuMessage*(msg: RelayWakuMessage, version = 0): Result[WakuMessage, str
version = uint32(msg.version.get(version))
meta = ?msg.meta.get(Base64String("")).decode()
ephemeral = msg.ephemeral.get(false)
proof = ?msg.proof.get(Base64String("")).decode()

var timestamp = msg.timestamp.get(0)

Expand All @@ -59,6 +62,7 @@ proc toWakuMessage*(msg: RelayWakuMessage, version = 0): Result[WakuMessage, str
timestamp: timestamp,
meta: meta,
ephemeral: ephemeral,
proof: proof,
)
)

Expand All @@ -79,6 +83,8 @@ proc writeValue*(
writer.writeField("meta", value.meta.get())
if value.ephemeral.isSome():
writer.writeField("ephemeral", value.ephemeral.get())
if value.proof.isSome():
writer.writeField("proof", value.proof.get())
writer.endRecord()

proc readValue*(
Expand All @@ -91,6 +97,7 @@ proc readValue*(
timestamp = none(int64)
meta = none(Base64String)
ephemeral = none(bool)
proof = none(Base64String)

var keys = initHashSet[string]()
for fieldName in readObjectFields(reader):
Expand All @@ -116,6 +123,8 @@ proc readValue*(
meta = some(reader.readValue(Base64String))
of "ephemeral":
ephemeral = some(reader.readValue(bool))
of "proof":
proof = some(reader.readValue(Base64String))
else:
unrecognizedFieldWarning(value)

Expand All @@ -132,4 +141,5 @@ proc readValue*(
timestamp: timestamp,
meta: meta,
ephemeral: ephemeral,
proof: proof,
)
3 changes: 1 addition & 2 deletions waku/waku_api/rest/serdes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ proc decodeFromJsonBytes*[T](
)
)
except SerializationError:
# TODO: Do better error reporting here
err("Unable to deserialize data")
err("Unable to deserialize data: " & getCurrentExceptionMsg())

proc encodeIntoJsonString*(value: auto): SerdesResult[string] =
var encoded: string
Expand Down
4 changes: 1 addition & 3 deletions waku/waku_store_sync.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{.push raises: [].}

import
./waku_store_sync/reconciliation,
./waku_store_sync/transfer,
./waku_store_sync/common
./waku_store_sync/reconciliation, ./waku_store_sync/transfer, ./waku_store_sync/common

export reconciliation, transfer, common
Loading