# compile time options here const libp2p_pubsub_sign {.booldefine.} = true libp2p_pubsub_verify {.booldefine.} = true libp2p_pubsub_anonymize {.booldefine.} = false import hashes, random, tables, sets, sequtils import chronos, stew/[byteutils, results], chronos/ratelimit import ../../libp2p/[builders, protocols/pubsub/errors, protocols/pubsub/pubsub, protocols/pubsub/pubsubpeer, protocols/pubsub/gossipsub, protocols/pubsub/floodsub, protocols/pubsub/rpc/messages, protocols/secure/secure] import ../helpers import chronicles export builders randomize() type TestGossipSub* = ref object of GossipSub proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer = proc getConn(): Future[Connection] = p.switch.dial(peerId, GossipSubCodec) let pubSubPeer = PubSubPeer.new(peerId, getConn, nil, GossipSubCodec, 1024 * 1024) debug "created new pubsub peer", peerId p.peers[peerId] = pubSubPeer onNewPeer(p, pubSubPeer) pubSubPeer proc randomPeerId*(): PeerId = try: PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).tryGet() except CatchableError as exc: raise newException(Defect, exc.msg) func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] = let mid = if m.seqno.len > 0 and m.fromPeer.data.len > 0: byteutils.toHex(m.seqno) & $m.fromPeer else: # This part is irrelevant because it's not standard, # We use it exclusively for testing basically and users should # implement their own logic in the case they use anonymization $m.data.hash & $m.topic.hash ok mid.toBytes() proc generateNodes*( num: Natural, secureManagers: openArray[SecureProtocol] = [ SecureProtocol.Noise ], msgIdProvider: MsgIdProvider = defaultMsgIdProvider, gossip: bool = false, triggerSelf: bool = false, verifySignature: bool = libp2p_pubsub_verify, anonymize: bool = libp2p_pubsub_anonymize, sign: bool = libp2p_pubsub_sign, sendSignedPeerRecord = false, unsubscribeBackoff = 1.seconds, maxMessageSize: int = 1024 * 1024, enablePX: bool = false, overheadRateLimit: Opt[tuple[bytes: int, interval: Duration]] = Opt.none(tuple[bytes: int, interval: Duration])): seq[PubSub] = for i in 0..