Skip to content

Commit

Permalink
Traffic score tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Oct 2, 2023
1 parent 7587181 commit f9b67f8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
67 changes: 66 additions & 1 deletion tests/pubsub/testgossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
{.used.}

import sequtils, options, tables, sets, sugar
import chronos, stew/byteutils
import chronos, stew/byteutils, chronos/ratelimit
import chronicles
import metrics
import utils, ../../libp2p/[errors,
peerid,
peerinfo,
Expand All @@ -20,6 +21,7 @@ import utils, ../../libp2p/[errors,
crypto/crypto,
protocols/pubsub/pubsub,
protocols/pubsub/gossipsub,
protocols/pubsub/gossipsub/scoring,
protocols/pubsub/pubsubpeer,
protocols/pubsub/peertable,
protocols/pubsub/timedcache,
Expand Down Expand Up @@ -928,3 +930,66 @@ suite "GossipSub":

await allFuturesThrowing(nodesFut.concat())

proc initializeGossipTest(): Future[(seq[PubSub], GossipSub, GossipSub)] {.async.} =
let nodes = generateNodes(
2,
gossip = true,
overheadRateLimit = Opt.some((20, 1.millis)))

discard await allFinished(
nodes[0].switch.start(),
nodes[1].switch.start(),
)

await subscribeNodes(nodes)

proc handle(topic: string, data: seq[byte]) {.async, gcsafe.} = discard

let gossip0 = GossipSub(nodes[0])
let gossip1 = GossipSub(nodes[1])

gossip0.subscribe("foobar", handle)
gossip1.subscribe("foobar", handle)
await waitSubGraph(nodes, "foobar")

return (nodes, gossip0, gossip1)

asyncTest "e2e - GossipSub should process valid messages":
let (nodes, gossip0, gossip1) = await initializeGossipTest()

gossip0.broadcast(gossip1.mesh["foobar"], RPCMsg(
messages: @[Message(topicIDs: @["foobar"], data: "Valid data".toBytes)])
)
await sleepAsync(300.millis)

expect(system.KeyError):
check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 0

await stopNodes(nodes)

asyncTest "e2e - GossipSub should rate limit undecodable messages above what is allowed":
let (nodes, gossip0, gossip1) = await initializeGossipTest()

# Simulate sending an undecodable message
await gossip0.peers[gossip1.switch.peerInfo.peerId].sendEncoded(newSeqWith[byte](30, 1.byte))
await sleepAsync(300.millis)

check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 1

await stopNodes(nodes)

asyncTest "e2e - GossipSub should rate limit messages with excessive useless data":
let (nodes, gossip0, gossip1) = await initializeGossipTest()

gossip0.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage(prune: @[
ControlPrune(
topicID: "foobar",
peers: @[PeerInfoMsg(peerId: PeerId(data: newSeq[byte](30)))],
backoff: 123'u64
)
]))))
await sleepAsync(300.millis)

check libp2p_gossipsub_peers_rate_limit_disconnections.valueByName("libp2p_gossipsub_peers_rate_limit_disconnections_total", @["nim-libp2p"]) == 1

await stopNodes(nodes)
7 changes: 4 additions & 3 deletions tests/pubsub/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const
libp2p_pubsub_anonymize {.booldefine.} = false

import hashes, random, tables, sets, sequtils
import chronos, stew/[byteutils, results]
import chronos, stew/[byteutils, results], chronos/ratelimit
import ../../libp2p/[builders,
protocols/pubsub/errors,
protocols/pubsub/pubsub,
Expand Down Expand Up @@ -67,7 +67,8 @@ proc generateNodes*(
sendSignedPeerRecord = false,
unsubscribeBackoff = 1.seconds,
maxMessageSize: int = 1024 * 1024,
enablePX: bool = false): seq[PubSub] =
enablePX: bool = false,
overheadRateLimit: Opt[tuple[bytes: int, interval: Duration]] = Opt.none(tuple[bytes: int, interval: Duration])): seq[PubSub] =

for i in 0..<num:
let switch = newStandardSwitch(secureManagers = secureManagers, sendSignedPeerRecord = sendSignedPeerRecord)
Expand All @@ -80,7 +81,7 @@ proc generateNodes*(
msgIdProvider = msgIdProvider,
anonymize = anonymize,
maxMessageSize = maxMessageSize,
parameters = (var p = GossipSubParams.init(); p.floodPublish = false; p.historyLength = 20; p.historyGossip = 20; p.unsubscribeBackoff = unsubscribeBackoff; p.enablePX = enablePX; p))
parameters = (var p = GossipSubParams.init(); p.floodPublish = false; p.historyLength = 20; p.historyGossip = 20; p.unsubscribeBackoff = unsubscribeBackoff; p.enablePX = enablePX; p.overheadRateLimit = overheadRateLimit; p))
# set some testing params, to enable scores
g.topicParams.mgetOrPut("foobar", TopicParams.init()).topicWeight = 1.0
g.topicParams.mgetOrPut("foo", TopicParams.init()).topicWeight = 1.0
Expand Down

0 comments on commit f9b67f8

Please sign in to comment.