Skip to content

Commit 940855b

Browse files
committed
test: add test failed validation
1 parent fa9e80c commit 940855b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/pubsub/testgossipsub.nim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,35 @@ suite "GossipSub":
202202
gossipSub.peerStats.withValue(peer.peerId, stats):
203203
check:
204204
stats[].topicInfos[topic].invalidMessageDeliveries == 1.0
205+
206+
asyncTest "Peer is punished when message validation is rejected":
207+
# Given a GossipSub instance with one peer
208+
const topic = "foobar"
209+
let
210+
(gossipSub, conns, peers) = setupGossipSubWithPeers(1, topic)
211+
peer = peers[0]
212+
defer:
213+
await teardownGossipSub(gossipSub, conns)
214+
215+
# And signature verification disabled to avoid message being dropped earlier
216+
gossipSub.verifySignature = false
217+
218+
# And a custom validator that always rejects messages
219+
proc rejectingValidator(
220+
topic: string, message: Message
221+
): Future[ValidationResult] {.async.} =
222+
return ValidationResult.Reject
223+
224+
# Register the rejecting validator for the topic
225+
gossipSub.addValidator(topic, rejectingValidator)
226+
227+
# And a message is created
228+
var msg = Message.init(peer.peerId, ("bar").toBytes(), topic, some(1'u64))
229+
230+
# When the GossipSub processes the message
231+
await gossipSub.rpcHandler(peer, encodeRpcMsg(RPCMsg(messages: @[msg]), false))
232+
233+
# Then the peer's invalidMessageDeliveries counter is incremented
234+
gossipSub.peerStats.withValue(peer.peerId, stats):
235+
check:
236+
stats[].topicInfos[topic].invalidMessageDeliveries == 1.0

0 commit comments

Comments
 (0)