From 1c3616e3a5f1f7baadb5f808ca5cb42ff0cee3e2 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 24 May 2021 11:55:33 -0600 Subject: [PATCH] merge latest master --- libp2p/builders.nim | 5 +++-- libp2p/dial.nim | 2 ++ libp2p/dialer.nim | 7 ++++--- libp2p/muxers/mplex/coder.nim | 5 ++--- libp2p/protocols/pubsub/gossipsub.nim | 14 ++++++++++++++ libp2p/protocols/secure/secio.nim | 4 +++- libp2p/switch.nim | 5 +++-- 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/libp2p/builders.nim b/libp2p/builders.nim index 79008b663..9e31aaddb 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -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 diff --git a/libp2p/dial.nim b/libp2p/dial.nim index b4625400b..4e41da598 100644 --- a/libp2p/dial.nim +++ b/libp2p/dial.nim @@ -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 diff --git a/libp2p/dialer.nim b/libp2p/dialer.nim index e871aea38..961368466 100644 --- a/libp2p/dialer.nim +++ b/libp2p/dialer.nim @@ -19,9 +19,10 @@ import dial, multistream, connmanager, stream/connection, - transports/transport + transports/transport, + errors -export dial +export dial, errors logScope: topics = "libp2p dialer" @@ -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 diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index fc917b96d..3a8df9f5c 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -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, @@ -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.} = diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 898b02916..f85057e8b 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -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 diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index d89b6c736..19af9e7fe 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -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() diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 22443959b..759543ffd 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -249,9 +249,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 LPError)(msg: "Provide at least one secure manager") + raise newException(LPError, "Provide at least one secure manager") let switch = Switch( peerInfo: peerInfo,