mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-07-26 06:23:14 +00:00
feat(store): add optional eligibility fields on Store query RPC (tag 30)
This commit is contained in:
parent
cbb601ec9f
commit
a081681977
@ -18,6 +18,16 @@ type WakuStoreResult*[T] = Result[T, string]
|
||||
## API types
|
||||
|
||||
type
|
||||
EligibilityStatusCode* {.pure, size: sizeof(uint32).} = enum
|
||||
OK = 0
|
||||
PARAMS_REJECTED = 1
|
||||
PROOF_INVALID = 2
|
||||
STREAM_NOT_ACTIVE = 3
|
||||
|
||||
EligibilityStatus* = object
|
||||
code*: EligibilityStatusCode
|
||||
desc*: string
|
||||
|
||||
StoreQueryRequest* = object
|
||||
requestId*: string
|
||||
includeData*: bool
|
||||
@ -33,6 +43,8 @@ type
|
||||
paginationForward*: PagingDirection
|
||||
paginationLimit*: Option[uint64]
|
||||
|
||||
eligibilityProof*: Option[seq[byte]]
|
||||
|
||||
WakuMessageKeyValue* = object
|
||||
messageHash*: WakuMessageHash
|
||||
message*: Option[WakuMessage]
|
||||
@ -48,6 +60,8 @@ type
|
||||
|
||||
paginationCursor*: Option[WakuMessageHash]
|
||||
|
||||
eligibilityStatus*: Option[EligibilityStatus]
|
||||
|
||||
# Types to be used by clients that use the hash in hex
|
||||
WakuMessageKeyValueHex* = object
|
||||
messageHash*: string
|
||||
|
||||
@ -5,6 +5,28 @@ import ../common/[protobuf, paging], ../waku_core, ./common
|
||||
|
||||
const DefaultMaxRpcSize* = -1
|
||||
|
||||
proc encode*(status: EligibilityStatus): ProtoBuffer =
|
||||
var pb = initProtoBuffer()
|
||||
pb.write3(1, uint32(status.code))
|
||||
pb.write3(2, status.desc)
|
||||
pb.finish3()
|
||||
return pb
|
||||
|
||||
proc decode*(
|
||||
T: type EligibilityStatus, buffer: seq[byte]
|
||||
): ProtobufResult[EligibilityStatus] =
|
||||
let pb = initProtoBuffer(buffer)
|
||||
var status = EligibilityStatus()
|
||||
var code: uint32
|
||||
if not ?pb.getField(1, code):
|
||||
return err(ProtobufError.missingRequiredField("code"))
|
||||
status.code = EligibilityStatusCode(code)
|
||||
var desc: string
|
||||
if not ?pb.getField(2, desc):
|
||||
return err(ProtobufError.missingRequiredField("desc"))
|
||||
status.desc = desc
|
||||
ok(status)
|
||||
|
||||
### Request ###
|
||||
|
||||
proc encode*(req: StoreQueryRequest): ProtoBuffer =
|
||||
@ -40,6 +62,8 @@ proc encode*(req: StoreQueryRequest): ProtoBuffer =
|
||||
pb.write3(52, uint32(req.paginationForward))
|
||||
pb.write3(53, req.paginationLimit)
|
||||
|
||||
pb.write3(30, req.eligibilityProof)
|
||||
|
||||
pb.finish3()
|
||||
|
||||
return pb
|
||||
@ -113,6 +137,12 @@ proc decode*(
|
||||
else:
|
||||
req.paginationLimit = some(limit)
|
||||
|
||||
var proofBytes: seq[byte]
|
||||
if not ?pb.getField(30, proofBytes):
|
||||
req.eligibilityProof = none(seq[byte])
|
||||
else:
|
||||
req.eligibilityProof = some(proofBytes)
|
||||
|
||||
return ok(req)
|
||||
|
||||
### Response ###
|
||||
@ -143,6 +173,9 @@ proc encode*(res: StoreQueryResponse): ProtoBuffer =
|
||||
|
||||
pb.write3(51, res.paginationCursor)
|
||||
|
||||
if res.eligibilityStatus.isSome():
|
||||
pb.write3(30, res.eligibilityStatus.get().encode())
|
||||
|
||||
pb.finish3()
|
||||
|
||||
return pb
|
||||
@ -210,4 +243,11 @@ proc decode*(
|
||||
discard copyFrom[byte](hash, cursor)
|
||||
res.paginationCursor = some(hash)
|
||||
|
||||
var statusBuf: seq[byte]
|
||||
if not ?pb.getField(30, statusBuf):
|
||||
res.eligibilityStatus = none(EligibilityStatus)
|
||||
else:
|
||||
let status = ?EligibilityStatus.decode(statusBuf)
|
||||
res.eligibilityStatus = some(status)
|
||||
|
||||
return ok(res)
|
||||
|
||||
@ -93,3 +93,39 @@ procSuite "Waku Store - RPC codec":
|
||||
check:
|
||||
# check the correctness of init and encode for an empty HistoryResponseRPC
|
||||
decodedEmptyRes.value == emptyRes
|
||||
|
||||
test "StoreQueryRequest protobuf codec - eligibility proof":
|
||||
## Given
|
||||
let proof = @[byte(0x01), 0x02, 0x03]
|
||||
let query = StoreQueryRequest(
|
||||
requestId: "proof-req", includeData: false, eligibilityProof: some(proof)
|
||||
)
|
||||
|
||||
## When
|
||||
let pb = query.encode()
|
||||
let decodedQuery = StoreQueryRequest.decode(pb.buffer)
|
||||
|
||||
## Then
|
||||
check:
|
||||
decodedQuery.isOk()
|
||||
decodedQuery.value == query
|
||||
|
||||
test "StoreQueryResponse protobuf codec - eligibility status":
|
||||
## Given
|
||||
let res = StoreQueryResponse(
|
||||
requestId: "2",
|
||||
statusCode: uint32(StatusCode.BAD_REQUEST),
|
||||
statusDesc: "bad",
|
||||
messages: @[],
|
||||
eligibilityStatus:
|
||||
some(EligibilityStatus(code: PROOF_INVALID, desc: "proof invalid")),
|
||||
)
|
||||
|
||||
## When
|
||||
let pb = res.encode()
|
||||
let decodedRes = StoreQueryResponse.decode(pb.buffer)
|
||||
|
||||
## Then
|
||||
check:
|
||||
decodedRes.isOk()
|
||||
decodedRes.value == res
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user