nwaku/tests/waku_peer_exchange/test_rpc_codec.nim
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

74 lines
2.1 KiB
Nim

{.used.}
import
std/[options, net],
testutils/unittests,
chronos,
libp2p/switch,
libp2p/peerId,
libp2p/crypto/crypto,
eth/keys,
eth/p2p/discoveryv5/enr
import
waku/[
node/peer_manager,
discovery/waku_discv5,
waku_peer_exchange/rpc,
waku_peer_exchange/rpc_codec,
],
../testlib/[wakucore]
suite "Peer Exchange RPC":
asyncTest "Encode - Decode":
# Setup
let rpcReq = PeerExchangeRpc.makeRequest(2)
let rpcReqBuffer: seq[byte] = rpcReq.encode().buffer
let resReq = PeerExchangeRpc.decode(rpcReqBuffer)
check:
resReq.isOk
resReq.get().request.numPeers == 2
var
enr1 = enr.Record(seqNum: 0, raw: @[])
enr2 = enr.Record(seqNum: 0, raw: @[])
check:
enr1.fromUri(
"enr:-JK4QPmO-sE2ELiWr8qVFs1kaY4jQZQpNaHvSPRmKiKcaDoqYRdki2c1BKSliImsxFeOD_UHnkddNL2l0XT9wlsP0WEBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQIMwKqlOl3zpwnrsKRKHuWPSuFzit1Cl6IZvL2uzBRe8oN0Y3CC6mKDdWRwgiMqhXdha3UyDw"
)
enr2.fromUri(
"enr:-Iu4QK_T7kzAmewG92u1pr7o6St3sBqXaiIaWIsFNW53_maJEaOtGLSN2FUbm6LmVxSfb1WfC7Eyk-nFYI7Gs3SlchwBgmlkgnY0gmlwhI5d6VKJc2VjcDI1NmsxoQLPYQDvrrFdCrhqw3JuFaGD71I8PtPfk6e7TJ3pg_vFQYN0Y3CC6mKDdWRwgiMq"
)
let peerInfos =
@[PeerExchangePeerInfo(enr: enr1.raw), PeerExchangePeerInfo(enr: enr2.raw)]
let rpc = PeerExchangeRpc.makeResponse(peerInfos)
# When encoding and decoding
let rpcBuffer: seq[byte] = rpc.encode().buffer
let res = PeerExchangeRpc.decode(rpcBuffer)
# Then the peerInfos match the originals
check:
res.isOk
res.get().response.status_code == PeerExchangeResponseStatusCode.SUCCESS
res.get().response.peerInfos == peerInfos
# When using the decoded responses to create new enrs
var
resEnr1 = enr.Record(seqNum: 0, raw: @[])
resEnr2 = enr.Record(seqNum: 0, raw: @[])
check:
res.get().response.status_code == PeerExchangeResponseStatusCode.SUCCESS
discard resEnr1.fromBytes(res.get().response.peerInfos[0].enr)
discard resEnr2.fromBytes(res.get().response.peerInfos[1].enr)
# Then they match the original enrs
check:
resEnr1 == enr1
resEnr2 == enr2