@@ -202,3 +202,35 @@ suite "GossipSub":
202
202
gossipSub.peerStats.withValue(peer.peerId, stats):
203
203
check:
204
204
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