-
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(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
- Loading branch information
1 parent
6d0f6d8
commit 6553026
Showing
5 changed files
with
112 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import | ||
./test_rpc_codec |
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,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 |
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,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] |
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,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) | ||
|
||
|