2024-06-28 16:04:57 +05:30
|
|
|
{.push raises: [].}
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2024-09-18 15:58:07 +02:00
|
|
|
import std/options
|
2024-03-16 00:08:47 +01:00
|
|
|
import ../common/protobuf, ./rpc
|
2022-11-07 16:24:16 +01:00
|
|
|
|
2022-09-20 13:03:34 +02:00
|
|
|
proc encode*(rpc: PeerExchangeRequest): ProtoBuffer =
|
2022-11-07 16:24:16 +01:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.write3(1, rpc.numPeers)
|
|
|
|
pb.finish3()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
proc decode*(T: type PeerExchangeRequest, buffer: seq[byte]): ProtoResult[T] =
|
2022-09-20 13:03:34 +02:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
var rpc = PeerExchangeRequest(numPeers: 0)
|
|
|
|
|
|
|
|
var numPeers: uint64
|
|
|
|
if ?pb.getField(1, numPeers):
|
|
|
|
rpc.numPeers = numPeers
|
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
ok(rpc)
|
|
|
|
|
2022-09-20 13:03:34 +02:00
|
|
|
proc encode*(rpc: PeerExchangePeerInfo): ProtoBuffer =
|
2022-11-07 16:24:16 +01:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.write3(1, rpc.enr)
|
|
|
|
pb.finish3()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
proc decode*(T: type PeerExchangePeerInfo, buffer: seq[byte]): ProtoResult[T] =
|
2022-09-20 13:03:34 +02:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
var rpc = PeerExchangePeerInfo(enr: @[])
|
|
|
|
|
|
|
|
var peerInfoBuffer: seq[byte]
|
|
|
|
if ?pb.getField(1, peerInfoBuffer):
|
2022-11-07 16:24:16 +01:00
|
|
|
rpc.enr = peerInfoBuffer
|
|
|
|
|
|
|
|
ok(rpc)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2024-09-18 15:58:07 +02:00
|
|
|
proc parse*(T: type PeerExchangeResponseStatusCode, status: uint32): T =
|
|
|
|
case status
|
|
|
|
of 200, 400, 429, 503:
|
|
|
|
PeerExchangeResponseStatusCode(status)
|
|
|
|
else:
|
|
|
|
PeerExchangeResponseStatusCode.UNKNOWN
|
|
|
|
|
2022-09-20 13:03:34 +02:00
|
|
|
proc encode*(rpc: PeerExchangeResponse): ProtoBuffer =
|
2022-11-07 16:24:16 +01:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
|
|
for pi in rpc.peerInfos:
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.write3(1, pi.encode())
|
2024-09-18 15:58:07 +02:00
|
|
|
pb.write3(10, rpc.status_code.uint32)
|
|
|
|
pb.write3(11, rpc.status_desc)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.finish3()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2024-09-18 15:58:07 +02:00
|
|
|
proc decode*(T: type PeerExchangeResponse, buffer: seq[byte]): ProtobufResult[T] =
|
2022-11-07 16:24:16 +01:00
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-09-20 13:03:34 +02:00
|
|
|
var rpc = PeerExchangeResponse(peerInfos: @[])
|
|
|
|
|
|
|
|
var peerInfoBuffers: seq[seq[byte]]
|
|
|
|
if ?pb.getRepeatedField(1, peerInfoBuffers):
|
|
|
|
for pib in peerInfoBuffers:
|
2022-11-07 16:24:16 +01:00
|
|
|
rpc.peerInfos.add(?PeerExchangePeerInfo.decode(pib))
|
|
|
|
|
2024-09-18 15:58:07 +02:00
|
|
|
var status_code: uint32
|
|
|
|
if ?pb.getField(10, status_code):
|
|
|
|
rpc.status_code = PeerExchangeResponseStatusCode.parse(status_code)
|
|
|
|
else:
|
2024-09-25 11:51:50 +02:00
|
|
|
# older peers may not support status_code field yet
|
|
|
|
if rpc.peerInfos.len() > 0:
|
|
|
|
rpc.status_code = PeerExchangeResponseStatusCode.SUCCESS
|
|
|
|
else:
|
|
|
|
rpc.status_code = PeerExchangeResponseStatusCode.SERVICE_UNAVAILABLE
|
2024-09-18 15:58:07 +02:00
|
|
|
|
|
|
|
var status_desc: string
|
|
|
|
if ?pb.getField(11, status_desc):
|
|
|
|
rpc.status_desc = some(status_desc)
|
|
|
|
else:
|
|
|
|
rpc.status_desc = none(string)
|
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
ok(rpc)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
|
|
proc encode*(rpc: PeerExchangeRpc): ProtoBuffer =
|
2022-11-07 16:24:16 +01:00
|
|
|
var pb = initProtoBuffer()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.write3(1, rpc.request.encode())
|
|
|
|
pb.write3(2, rpc.response.encode())
|
2024-09-18 15:58:07 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb.finish3()
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
pb
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2024-09-18 15:58:07 +02:00
|
|
|
proc decode*(T: type PeerExchangeRpc, buffer: seq[byte]): ProtobufResult[T] =
|
2022-11-07 16:24:16 +01:00
|
|
|
let pb = initProtoBuffer(buffer)
|
2022-09-20 13:03:34 +02:00
|
|
|
var rpc = PeerExchangeRpc()
|
|
|
|
|
|
|
|
var requestBuffer: seq[byte]
|
2023-02-09 16:59:29 +01:00
|
|
|
if not ?pb.getField(1, requestBuffer):
|
2024-09-18 15:58:07 +02:00
|
|
|
return err(ProtobufError.missingRequiredField("request"))
|
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
rpc.request = ?PeerExchangeRequest.decode(requestBuffer)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
|
|
|
var responseBuffer: seq[byte]
|
2024-09-18 15:58:07 +02:00
|
|
|
if not ?pb.getField(2, responseBuffer):
|
2024-09-24 12:47:52 +02:00
|
|
|
rpc.response =
|
|
|
|
PeerExchangeResponse(status_code: PeerExchangeResponseStatusCode.UNKNOWN)
|
|
|
|
else:
|
|
|
|
rpc.response = ?PeerExchangeResponse.decode(responseBuffer)
|
2022-09-20 13:03:34 +02:00
|
|
|
|
2022-11-07 16:24:16 +01:00
|
|
|
ok(rpc)
|