diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index dc7426ac..e04f8809 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -249,7 +249,8 @@ class Main(object): # start network components if networking is enabled if state.enableNetwork: start_proxyconfig() - BMConnectionPool().connectToStream(1) + BMConnectionPool().connectToStream( + 1 if not state.enableObjProc else None) asyncoreThread = BMNetworkThread() asyncoreThread.daemon = True asyncoreThread.start() @@ -279,8 +280,8 @@ class Main(object): upnpThread = upnp.uPnPThread() upnpThread.start() else: - # Populate with hardcoded value (same as connectToStream above) - state.streamsInWhichIAmParticipating.append(1) + # Populate with hardcoded value just in case + state.streamsInWhichIAmParticipating.add(1) if not daemon and state.enableGUI: if state.curses: diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index fffc0bc3..e85b57e5 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -55,7 +55,7 @@ class BMConnectionPool(object): self.inboundConnections = {} self.listeningSockets = {} self.udpSockets = {} - self.streams = [] + self.streams = set() self._lastSpawned = 0 self._spawnWait = 2 self._bootstrapped = False @@ -87,10 +87,18 @@ class BMConnectionPool(object): return [ x for x in self.connections() if x.fullyEstablished] - def connectToStream(self, streamNumber): - """Connect to a bitmessage stream""" - self.streams.append(streamNumber) - state.streamsInWhichIAmParticipating.append(streamNumber) + def connectToStream(self, streamNumber=None): + """ + Connect to the *streamNumber* bitmessage stream if given; + otherwice connect to all streams listed in + `state.streamsInWhichIAmParticipating` + """ + if streamNumber: + self.streams.add(streamNumber) + state.streamsInWhichIAmParticipating.add(streamNumber) + else: + for streamNumber in state.streamsInWhichIAmParticipating: + self.streams.add(streamNumber) def getConnectionByAddr(self, addr): """ @@ -292,8 +300,8 @@ class BMConnectionPool(object): state.maximumNumberOfHalfOpenConnections - pending): try: chosen = self.trustedPeer or chooseConnection( - helper_random.randomchoice(self.streams)) - except ValueError: + helper_random.randomchoice(list(self.streams))) + except (ValueError, IndexError): continue if chosen in self.outboundConnections: continue diff --git a/src/network/knownnodes.py b/src/network/knownnodes.py index 4840aad9..e668843a 100644 --- a/src/network/knownnodes.py +++ b/src/network/knownnodes.py @@ -173,8 +173,9 @@ def readKnownNodes(): onionport = config.safeGetInt('bitmessagesettings', 'onionport') if onionport: self_peer = Peer(onionhostname, onionport) - addKnownNode(1, self_peer, is_self=True) state.ownAddresses[self_peer] = True + for stream in state.streamsInWhichIAmParticipating: + addKnownNode(stream, self_peer, is_self=True) def increaseRating(peer): diff --git a/src/shared.py b/src/shared.py index 5df98a0a..0931b216 100644 --- a/src/shared.py +++ b/src/shared.py @@ -122,6 +122,7 @@ def reloadMyAddressHashes(): hasEnabledKeys = True # status addressVersionNumber, streamNumber, hashobj = decodeAddress(addressInKeysFile)[1:] + state.streamsInWhichIAmParticipating.add(streamNumber) if addressVersionNumber in (2, 3, 4): # Returns a simple 32 bytes of information encoded # in 64 Hex characters, or null if there was an error. @@ -160,6 +161,7 @@ def reloadBroadcastSendersForWhichImWatching(): address, = row # status addressVersionNumber, streamNumber, hashobj = decodeAddress(address)[1:] + state.streamsInWhichIAmParticipating.add(streamNumber) if addressVersionNumber == 2: broadcastSendersForWhichImWatching[hashobj] = 0 # Now, for all addresses, even version 2 addresses, diff --git a/src/state.py b/src/state.py index be81992d..639b4798 100644 --- a/src/state.py +++ b/src/state.py @@ -3,7 +3,7 @@ Global runtime variables. """ neededPubkeys = {} -streamsInWhichIAmParticipating = [] +streamsInWhichIAmParticipating = set() extPort = None """For UPnP"""