NagyZoltanPeter 0a7f16a332
chore: rate limit peer exchange protocol, enhanced response status in RPC (#3035)
* Enhanced peer-ex protocol - added rate limiting, added response status and desc to the rpc

* Better error result handling for PeerEx request, adjusted tests

* Refactored RateLimit configuration option for better CLI UX - now possible to set separate limits per protocol. Adjusted mountings. Added and adjusted tests

* Fix libwaku due to changes of error return type of fetchPeerExchangePeers

* Fix rate limit setting tests due to changed defaults

* Introduce new gauge to help dasboard effectively show current rate limit applied for protocol

* Adjust timeing in filter rate limit test to let macos CI test run ok.

* Address review findings, namings, error logs, removed left-overs

* Changes to reflect latest spec agreement and changes. PeerExchange RPC is changed the now respond structure will contain status_code and status_desc.
2024-09-18 15:58:07 +02:00

62 lines
2.1 KiB
Nim

import std/options
type
PeerExchangeResponseStatusCode* {.pure.} = enum
UNKNOWN = uint32(000)
SUCCESS = uint32(200)
BAD_REQUEST = uint32(400)
BAD_RESPONSE = uint32(401)
TOO_MANY_REQUESTS = uint32(429)
SERVICE_UNAVAILABLE = uint32(503)
DIAL_FAILURE = uint32(599)
PeerExchangePeerInfo* = object
enr*: seq[byte] # RLP encoded ENR: https://eips.ethereum.org/EIPS/eip-778
PeerExchangeRequest* = object
numPeers*: uint64
PeerExchangeResponse* = object
peerInfos*: seq[PeerExchangePeerInfo]
status_code*: PeerExchangeResponseStatusCode
status_desc*: Option[string]
PeerExchangeResponseStatus* =
tuple[status_code: PeerExchangeResponseStatusCode, status_desc: Option[string]]
PeerExchangeRpc* = object
request*: PeerExchangeRequest
response*: PeerExchangeResponse
proc makeRequest*(T: type PeerExchangeRpc, numPeers: uint64): T =
return T(request: PeerExchangeRequest(numPeers: numPeers))
proc makeResponse*(T: type PeerExchangeRpc, peerInfos: seq[PeerExchangePeerInfo]): T =
return T(
response: PeerExchangeResponse(
peerInfos: peerInfos, status_code: PeerExchangeResponseStatusCode.SUCCESS
)
)
proc makeErrorResponse*(
T: type PeerExchangeRpc,
status_code: PeerExchangeResponseStatusCode,
status_desc: Option[string] = none(string),
): T =
return T(
response: PeerExchangeResponse(status_code: status_code, status_desc: status_desc)
)
proc `$`*(statusCode: PeerExchangeResponseStatusCode): string =
case statusCode
of PeerExchangeResponseStatusCode.UNKNOWN: "UNKNOWN"
of PeerExchangeResponseStatusCode.SUCCESS: "SUCCESS"
of PeerExchangeResponseStatusCode.BAD_REQUEST: "BAD_REQUEST"
of PeerExchangeResponseStatusCode.BAD_RESPONSE: "BAD_RESPONSE"
of PeerExchangeResponseStatusCode.TOO_MANY_REQUESTS: "TOO_MANY_REQUESTS"
of PeerExchangeResponseStatusCode.SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE"
of PeerExchangeResponseStatusCode.DIAL_FAILURE: "DIAL_FAILURE"
# proc `$`*(pxResponseStatus: PeerExchangeResponseStatus): string =
# return $pxResponseStatus.status & " - " & pxResponseStatus.desc.get("")