Skip to content

Commit

Permalink
update the protocol negotiated in pubsubpeer
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Jun 5, 2024
1 parent 2956971 commit e8008e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions libp2p/protocols/pubsub/gossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,10 @@ method initPubSub*(g: GossipSub)
method getOrCreatePeer*(
g: GossipSub,
peerId: PeerId,
protos: seq[string]): PubSubPeer =
protosToDial: seq[string],
protoNegotiated: string = ""): PubSubPeer =

let peer = procCall PubSub(g).getOrCreatePeer(peerId, protos)
let peer = procCall PubSub(g).getOrCreatePeer(peerId, protosToDial, protoNegotiated)
g.parameters.overheadRateLimit.withValue(overheadRateLimit):
peer.overheadRateLimitOpt = Opt.some(TokenBucket.new(overheadRateLimit.bytes, overheadRateLimit.interval))
peer.maxNumElementsInNonPriorityQueue = g.parameters.maxNumElementsInNonPriorityQueue
Expand Down
11 changes: 7 additions & 4 deletions libp2p/protocols/pubsub/pubsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,22 @@ method onPubSubPeerEvent*(p: PubSub, peer: PubSubPeer, event: PubSubPeerEvent) {
method getOrCreatePeer*(
p: PubSub,
peerId: PeerId,
protos: seq[string]): PubSubPeer {.base, gcsafe.} =
protosToDial: seq[string],
protoNegotiated: string = ""): PubSubPeer {.base, gcsafe.} =

p.peers.withValue(peerId, peer):
if peer[].codec == "" and protoNegotiated != "":
peer[].codec = protoNegotiated
return peer[]

proc getConn(): Future[Connection] {.async.} =
return await p.switch.dial(peerId, protos)
return await p.switch.dial(peerId, protosToDial)

proc onEvent(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} =
p.onPubSubPeerEvent(peer, event)

# create new pubsub peer
let pubSubPeer = PubSubPeer.new(peerId, getConn, onEvent, protos[0], p.maxMessageSize)
let pubSubPeer = PubSubPeer.new(peerId, getConn, onEvent, protoNegotiated, p.maxMessageSize)
debug "created new pubsub peer", peerId

p.peers[peerId] = pubSubPeer
Expand Down Expand Up @@ -376,7 +379,7 @@ method handleConn*(p: PubSub,
# call pubsub rpc handler
p.rpcHandler(peer, data)

let peer = p.getOrCreatePeer(conn.peerId, @[proto])
let peer = p.getOrCreatePeer(conn.peerId, @[], proto)

try:
peer.handler = handler
Expand Down

0 comments on commit e8008e0

Please sign in to comment.