Skip to content

Commit

Permalink
Sync payload & serde
Browse files Browse the repository at this point in the history
  • Loading branch information
SionoiS committed Mar 26, 2024
1 parent 94bdc0a commit ba3f9e9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
64 changes: 64 additions & 0 deletions waku/waku_sync/codec.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import std/options, stew/arrayops
import ../common/protobuf, ../waku_core, ./common

proc encode*(req: SyncPayload): ProtoBuffer =
var pb = initProtoBuffer()

if req.rangeStart.isSome() and req.rangeEnd.isSome():
pb.write3(1, req.rangeStart.get())
pb.write3(2, req.rangeEnd.get())

if req.frameSize.isSome():
pb.write3(3, req.frameSize.get())

if req.negentropy.len > 0:
pb.write3(4, req.negentropy)

if req.hashes.len > 0:
for hash in req.hashes:
pb.write3(5, hash)

return pb

proc decode*(T: type SyncPayload, buffer: seq[byte]): ProtobufResult[T] =
var req = SyncPayload()
let pb = initProtoBuffer(buffer)

var start: uint64
if ?pb.getField(1, start):
req.rangeStart = some(start)
else:
req.rangeStart = none(uint64)

var `end`: uint64
if ?pb.getField(2, `end`):
req.rangeEnd = some(`end`)
else:
req.rangeEnd = none(uint64)

var frame: uint64
if ?pb.getField(3, frame):
req.frameSize = some(frame)
else:
req.frameSize = none(uint64)

var negentropy: seq[byte]
if ?pb.getField(4, negentropy):
req.negentropy = negentropy
else:
req.negentropy = @[]

var buffer: seq[seq[byte]]
if not ?pb.getRepeatedField(5, buffer):
req.hashes = @[]
else:
req.hashes = newSeqOfCap[WakuMessageHash](buffer.len)
for buf in buffer:
req.messageHashes.add(WakuMessageHash.fromBytes(buf))

return ok(req)
19 changes: 19 additions & 0 deletions waku/waku_sync/common.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import std/[options]
import ../waku_core

const WakuSyncCodec* = "/vac/waku/sync/1.0.0"

type SyncPayload* = object
rangeStart*: Option[uint64]
rangeEnd*: Option[uint64]

frameSize*: Option[uint64]

negentropy*: seq[byte]

hashes*: seq[WakuMessageHash]
5 changes: 2 additions & 3 deletions waku/waku_sync/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import
../waku_core,
../waku_enr,
../node/peer_manager/peer_manager,
./raw_bindings
./raw_bindings,
./common

logScope:
topics = "waku sync"

const WakuSyncCodec* = "/vac/waku/sync/1.0.0"
const DefaultFrameSize = 153600 # using a random number for now
const DefaultSyncInterval = 60.minutes

type
Expand Down

0 comments on commit ba3f9e9

Please sign in to comment.