Skip to content

Commit

Permalink
feat(incentivization): add codec for eligibility proof and status (#2419
Browse files Browse the repository at this point in the history
)

* incentivization: add codec for eligibility proofs

* add codec for eligibility proof and eligibility status

* address minor comments

* make status code mandatory in eligibility status
  • Loading branch information
s-tikhomirov authored Mar 26, 2024
1 parent 6d0f6d8 commit 6553026
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
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


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):
# 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)


0 comments on commit 6553026

Please sign in to comment.