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

feat: Incentivization: add codec for eligibility proofs #2419

Merged
merged 4 commits into from
Mar 26, 2024
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
3 changes: 2 additions & 1 deletion tests/all_tests_waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import
./waku_filter_v2/test_all,
./waku_peer_exchange/test_all,
./waku_lightpush/test_all,
./waku_relay/test_all
./waku_relay/test_all,
./incentivization/test_all

import
# Waku v2 tests
Expand Down
2 changes: 2 additions & 0 deletions tests/incentivization/test_all.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import
./test_rpc_codec
32 changes: 32 additions & 0 deletions tests/incentivization/test_rpc_codec.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import
std/options,
std/strscans,
testutils/unittests,
chronicles,
chronos,
libp2p/crypto/crypto

import
../../../waku/incentivization/rpc,
../../../waku/incentivization/rpc_codec

s-tikhomirov marked this conversation as resolved.
Show resolved Hide resolved

suite "Waku Incentivization Eligibility Codec":

asyncTest "encode eligibility proof":
var byteSequence: seq[byte] = @[1, 2, 3, 4, 5, 6, 7, 8]
let epRpc = EligibilityProof(proofOfPayment: some(byteSequence))
let encoded = encode(epRpc)
let decoded = EligibilityProof.decode(encoded.buffer).get()
check:
epRpc == decoded

asyncTest "encode eligibility status":
let esRpc = EligibilityStatus(
statusCode: uint32(200),
statusDesc: some("OK")
)
let encoded = encode(esRpc)
let decoded = EligibilityStatus.decode(encoded.buffer).get()
check:
esRpc == decoded
17 changes: 17 additions & 0 deletions waku/incentivization/rpc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import
json_serialization,
std/options
import
../waku_core

# Implementing the RFC:
# https://github.com/vacp2p/rfc/tree/master/content/docs/rfcs/73

type

EligibilityProof* = object
proofOfPayment*: Option[seq[byte]]

EligibilityStatus* = object
statusCode*: uint32
statusDesc*: Option[string]
59 changes: 59 additions & 0 deletions waku/incentivization/rpc_codec.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import
std/options
import
../common/protobuf,
../waku_core,
./rpc


# Codec for EligibilityProof

proc encode*(epRpc: EligibilityProof): ProtoBuffer =
var pb = initProtoBuffer()
if epRpc.proofOfPayment.isSome():
let proofOfPayment = epRpc.proofOfPayment.get()
pb.write3(1, proofOfPayment)
else:
# there is no proof
discard
pb

proc decode*(T: type EligibilityProof, buffer: seq[byte]): ProtobufResult[T] =
let pb = initProtoBuffer(buffer)
var epRpc = EligibilityProof()
var proofOfPayment = newSeq[byte]()
if not ?pb.getField(1, proofOfPayment):
epRpc.proofOfPayment = none(seq[byte])
else:
epRpc.proofOfPayment = some(proofOfPayment)
ok(epRpc)


# Codec for EligibilityStatus

proc encode*(esRpc: EligibilityStatus): ProtoBuffer =
var pb = initProtoBuffer()
pb.write3(1, esRpc.statusCode)
if esRpc.statusDesc.isSome():
pb.write3(2, esRpc.statusDesc.get())
pb

proc decode*(T: type EligibilityStatus, buffer: seq[byte]): ProtobufResult[T] =
let pb = initProtoBuffer(buffer)
var esRpc = EligibilityStatus()
# status code
var code = uint32(0)
if not ?pb.getField(1, code):
s-tikhomirov marked this conversation as resolved.
Show resolved Hide resolved
# status code is mandatory
return err(ProtobufError.missingRequiredField("status_code"))
else:
esRpc.statusCode = code
# status description
var description = ""
if not ?pb.getField(2, description):
esRpc.statusDesc = none(string)
else:
esRpc.statusDesc = some(description)
ok(esRpc)


Loading