Skip to content

Commit

Permalink
merge latest master
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed May 24, 2021
1 parent 604b760 commit 39645a8
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
5 changes: 3 additions & 2 deletions libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex],
protocols/[identify, secure/secure, secure/noise],
connmanager, upgrademngrs/muxedupgrade
connmanager, upgrademngrs/muxedupgrade,
errors

export
switch, peerid, peerinfo, connection, multiaddress, crypto
switch, peerid, peerinfo, connection, multiaddress, crypto, errors

type
SecureProtocol* {.pure.} = enum
Expand Down
2 changes: 2 additions & 0 deletions libp2p/dial.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
## This file may not be copied, modified, or distributed except according to
## those terms.

{.push raises: [Defect].}

import chronos
import peerid,
stream/connection
Expand Down
7 changes: 4 additions & 3 deletions libp2p/dialer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import dial,
multistream,
connmanager,
stream/connection,
transports/transport
transports/transport,
errors

export dial
export dial, errors

logScope:
topics = "libp2p dialer"
Expand All @@ -32,7 +33,7 @@ declareCounter(libp2p_failed_dials, "failed dials")
declareCounter(libp2p_failed_upgrades_outgoing, "outgoing connections failed upgrades")

type
DialFailedError* = object of CatchableError
DialFailedError* = object of LPError

Dialer* = ref object of Dial
peerInfo*: PeerInfo
Expand Down
5 changes: 2 additions & 3 deletions libp2p/muxers/mplex/coder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

{.push raises: [Defect].}

import chronos
import nimcrypto/utils, chronicles, stew/byteutils
import pkg/[chronos, nimcrypto/utils, chronicles, stew/byteutils]
import ../../stream/connection,
../../utility,
../../varint,
Expand Down Expand Up @@ -40,7 +39,7 @@ type
# https://github.com/libp2p/specs/tree/master/mplex#writing-to-a-stream
const MaxMsgSize* = 1 shl 20 # 1mb

proc newInvalidMplexMsgType(): ref InvalidMplexMsgType =
proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType =
newException(InvalidMplexMsgType, "invalid message type")

proc readMsg*(conn: Connection): Future[Msg] {.async, gcsafe.} =
Expand Down
14 changes: 14 additions & 0 deletions libp2p/protocols/pubsub/gossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,24 @@ method publish*(g: GossipSub,
topic

trace "Publishing message on topic", data = data.shortLog

if topic.len <= 0: # data could be 0/empty
debug "Empty topic, skipping publish"
return 0

var peers: HashSet[PubSubPeer]

if g.parameters.floodPublish:
# With flood publishing enabled, the mesh is used when propagating messages from other peers,
# but a peer's own messages will always be published to all known peers in the topic.
for peer in g.gossipsub.getOrDefault(topic):
if peer.score >= g.parameters.publishThreshold:
trace "publish: including flood/high score peer", peer
peers.incl(peer)

# add always direct peers
peers.incl(g.explicit.getOrDefault(topic))

if topic in g.topics: # if we're subscribed use the mesh
peers.incl(g.mesh.getOrDefault(topic))
else: # not subscribed, send to fanout peers
Expand Down
4 changes: 3 additions & 1 deletion libp2p/protocols/secure/secio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ proc newSecio*(rng: ref BrHmacDrbgContext, localPrivateKey: PrivateKey): Secio =
result = Secio(
rng: rng,
localPrivateKey: localPrivateKey,
localPublicKey: localPrivateKey.getKey().get(),
localPublicKey: localPrivateKey
.getKey()
.expect("Can't fetch local private key"),
)
result.init()
16 changes: 9 additions & 7 deletions libp2p/switch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ proc dial*(
dial(s, peerId, addrs, @[proto])

proc mount*[T: LPProtocol](s: Switch, proto: T, matcher: Matcher = nil)
{.gcsafe, raises: [Defect, CatchableError].} =
{.gcsafe, raises: [Defect, LPError].} =

if isNil(proto.handler):
raise newException(CatchableError,
"Protocol has to define a handle method or proc")
raise newException(LPError,
"Protocol has to define a `handle` method or proc")

if proto.codec.len == 0:
raise newException(CatchableError,
"Protocol has to define a codec string")
raise newException(LPError,
"Protocol has to define a `codec` string")

s.ms.addHandler(proto.codecs, proto, matcher)
s.peerInfo.protocols.add(proto.codec)
Expand Down Expand Up @@ -249,9 +250,10 @@ proc newSwitch*(peerInfo: PeerInfo,
muxers: Table[string, MuxerProvider],
secureManagers: openarray[Secure] = [],
connManager: ConnManager,
ms: MultistreamSelect): Switch =
ms: MultistreamSelect): Switch
{.raises: [Defect, LPError].} =
if secureManagers.len == 0:
raise (ref CatchableError)(msg: "Provide at least one secure manager")
raise newException(LPError, "Provide at least one secure manager")

let switch = Switch(
peerInfo: peerInfo,
Expand Down

0 comments on commit 39645a8

Please sign in to comment.