mirror of https://github.com/waku-org/nwaku.git
feat(incentivization): add codec for eligibility proof and status (#2419)
* incentivization: add codec for eligibility proofs * add codec for eligibility proof and eligibility status * address minor comments * make status code mandatory in eligibility status
This commit is contained in:
parent
6d0f6d822c
commit
65530264d5
|
@ -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
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
import
|
||||
./test_rpc_codec
|
|
@ -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
|
|
@ -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]
|
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue