2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
|
2022-09-20 11:03:34 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../common/protobuf,
|
2022-09-20 11:03:34 +00:00
|
|
|
./rpc
|
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
|
2022-09-20 11:03:34 +00:00
|
|
|
proc encode*(rpc: PeerExchangeRequest): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, rpc.numPeers)
|
|
|
|
pb.finish3()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
proc decode*(T: type PeerExchangeRequest, buffer: seq[byte]): ProtoResult[T] =
|
2022-09-20 11:03:34 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
var rpc = PeerExchangeRequest(numPeers: 0)
|
|
|
|
|
|
|
|
var numPeers: uint64
|
|
|
|
if ?pb.getField(1, numPeers):
|
|
|
|
rpc.numPeers = numPeers
|
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
ok(rpc)
|
|
|
|
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
proc encode*(rpc: PeerExchangePeerInfo): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, rpc.enr)
|
|
|
|
pb.finish3()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
proc decode*(T: type PeerExchangePeerInfo, buffer: seq[byte]): ProtoResult[T] =
|
2022-09-20 11:03:34 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
var rpc = PeerExchangePeerInfo(enr: @[])
|
|
|
|
|
|
|
|
var peerInfoBuffer: seq[byte]
|
|
|
|
if ?pb.getField(1, peerInfoBuffer):
|
2022-11-07 15:24:16 +00:00
|
|
|
rpc.enr = peerInfoBuffer
|
|
|
|
|
|
|
|
ok(rpc)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc encode*(rpc: PeerExchangeResponse): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
for pi in rpc.peerInfos:
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, pi.encode())
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.finish3()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
proc decode*(T: type PeerExchangeResponse, buffer: seq[byte]): ProtoResult[T] =
|
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-09-20 11:03:34 +00:00
|
|
|
var rpc = PeerExchangeResponse(peerInfos: @[])
|
|
|
|
|
|
|
|
var peerInfoBuffers: seq[seq[byte]]
|
|
|
|
if ?pb.getRepeatedField(1, peerInfoBuffers):
|
|
|
|
for pib in peerInfoBuffers:
|
2022-11-07 15:24:16 +00:00
|
|
|
rpc.peerInfos.add(?PeerExchangePeerInfo.decode(pib))
|
|
|
|
|
|
|
|
ok(rpc)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc encode*(rpc: PeerExchangeRpc): ProtoBuffer =
|
2022-11-07 15:24:16 +00:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb.write3(1, rpc.request.encode())
|
|
|
|
pb.write3(2, rpc.response.encode())
|
|
|
|
pb.finish3()
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
pb
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
proc decode*(T: type PeerExchangeRpc, buffer: seq[byte]): ProtoResult[T] =
|
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-09-20 11:03:34 +00:00
|
|
|
var rpc = PeerExchangeRpc()
|
|
|
|
|
|
|
|
var requestBuffer: seq[byte]
|
2023-02-09 15:59:29 +00:00
|
|
|
if not ?pb.getField(1, requestBuffer):
|
|
|
|
return err(ProtoError.RequiredFieldMissing)
|
2022-11-07 15:24:16 +00:00
|
|
|
rpc.request = ?PeerExchangeRequest.decode(requestBuffer)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
var responseBuffer: seq[byte]
|
|
|
|
discard ?pb.getField(2, responseBuffer)
|
2022-11-07 15:24:16 +00:00
|
|
|
rpc.response = ?PeerExchangeResponse.decode(responseBuffer)
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
ok(rpc)
|