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 e6efbba
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
49 changes: 48 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,48 @@ suite "GossipSub":

await allFuturesThrowing(nodesFut.concat())

asyncTest "e2e - GossipSub should rate limit peer messages":
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
discard

let
nodes = generateNodes(
2,
gossip = true,
overheadRateLimit = Opt.some((20, 1.millis)))

# start switches
nodesFut = await allFinished(
nodes[0].switch.start(),
nodes[1].switch.start(),
)

await subscribeNodes(nodes)

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

gossip1.subscribe("foobar", handler)
gossip2.subscribe("foobar", handler)
await waitSubGraph(nodes, "foobar")

# Simulate sending a large message from node 1 to node 2 to trigger rate limiting
gossip1.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(400.millis)

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

await allFuturesThrowing(
nodes[0].switch.stop(),
nodes[1].switch.stop()
)

await allFuturesThrowing(nodesFut.concat())
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 e6efbba

Please sign in to comment.