Simplify and fix list of addresses to send

- it didn't always send the maximum possible amount
- it probably was slow
This commit is contained in:
Peter Šurda 2017-02-20 22:32:49 +01:00
parent 579ba49f38
commit ea9f10a8bb
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -658,74 +658,54 @@ class receiveDataThread(threading.Thread):
with knownnodes.knownNodesLock: with knownnodes.knownNodesLock:
if len(knownnodes.knownNodes[stream]) > 0: if len(knownnodes.knownNodes[stream]) > 0:
ownPosition = random.randint(0, 499) filtered = {k: v for k, v in knownnodes.knownNodes[stream].items() if v > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)}
sentOwn = False elemCount = len(filtered)
for i in range(500): if elemCount > 500:
# if current connection is over a proxy, sent our own onion address at a random position elemCount = 500
if ownPosition == i and ".onion" in BMConfigParser().get("bitmessagesettings", "onionhostname") and \ # only if more recent than 3 hours
hasattr(self.sock, "getproxytype") and self.sock.getproxytype() != "none" and not sentOwn: addrsInMyStream = random.sample(filtered.items(), elemCount)
peer = state.Peer(BMConfigParser().get("bitmessagesettings", "onionhostname"), BMConfigParser().getint("bitmessagesettings", "onionport"))
else:
# still may contain own onion address, but we don't change it
peer, = random.sample(knownnodes.knownNodes[stream], 1)
if isHostInPrivateIPRange(peer.host):
continue
if peer.host == BMConfigParser().get("bitmessagesettings", "onionhostname") and peer.port == BMConfigParser().getint("bitmessagesettings", "onionport") :
sentOwn = True
addrsInMyStream[peer] = knownnodes.knownNodes[
stream][peer]
# sent 250 only if the remote isn't interested in it # sent 250 only if the remote isn't interested in it
if len(knownnodes.knownNodes[stream * 2]) > 0 and stream not in self.streamNumber: if len(knownnodes.knownNodes[stream * 2]) > 0 and stream not in self.streamNumber:
for i in range(250): filtered = {k: v for k, v in knownnodes.knownNodes[stream*2].items() if v > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)}
peer, = random.sample(knownnodes.knownNodes[ elemCount = len(filtered)
stream * 2], 1) if elemCount > 250:
if isHostInPrivateIPRange(peer.host): elemCount = 250
continue addrsInMyStreamLeft = random.sample(filtered.items(), elemCount)
addrsInChildStreamLeft[peer] = knownnodes.knownNodes[
stream * 2][peer]
if len(knownnodes.knownNodes[(stream * 2) + 1]) > 0 and stream not in self.streamNumber: if len(knownnodes.knownNodes[(stream * 2) + 1]) > 0 and stream not in self.streamNumber:
for i in range(250): filtered = {k: v for k, v in knownnodes.knownNodes[stream*2+1].items() if v > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)}
peer, = random.sample(knownnodes.knownNodes[ elemCount = len(filtered)
(stream * 2) + 1], 1) if elemCount > 250:
if isHostInPrivateIPRange(peer.host): elemCount = 250
continue addrsInMyStreamRight = random.sample(filtered.items(), elemCount)
addrsInChildStreamRight[peer] = knownnodes.knownNodes[
(stream * 2) + 1][peer]
numberOfAddressesInAddrMessage = 0 numberOfAddressesInAddrMessage = 0
payload = '' payload = ''
for (HOST, PORT), value in addrsInMyStream.items(): for (HOST, PORT), timeLastReceivedMessageFromThisNode in addrsInMyStream:
timeLastReceivedMessageFromThisNode = value numberOfAddressesInAddrMessage += 1
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. payload += pack(
numberOfAddressesInAddrMessage += 1 '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
payload += pack( payload += pack('>I', stream)
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time payload += pack(
payload += pack('>I', stream) '>q', 1) # service bit flags offered by this node
payload += pack( payload += protocol.encodeHost(HOST)
'>q', 1) # service bit flags offered by this node payload += pack('>H', PORT) # remote port
payload += protocol.encodeHost(HOST) for (HOST, PORT), timeLastReceivedMessageFromThisNode in addrsInChildStreamLeft:
payload += pack('>H', PORT) # remote port numberOfAddressesInAddrMessage += 1
for (HOST, PORT), value in addrsInChildStreamLeft.items(): payload += pack(
timeLastReceivedMessageFromThisNode = value '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. payload += pack('>I', stream * 2)
numberOfAddressesInAddrMessage += 1 payload += pack(
payload += pack( '>q', 1) # service bit flags offered by this node
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time payload += protocol.encodeHost(HOST)
payload += pack('>I', stream * 2) payload += pack('>H', PORT) # remote port
payload += pack( for (HOST, PORT), timeLastReceivedMessageFromThisNode in addrsInChildStreamRight:
'>q', 1) # service bit flags offered by this node numberOfAddressesInAddrMessage += 1
payload += protocol.encodeHost(HOST) payload += pack(
payload += pack('>H', PORT) # remote port '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
for (HOST, PORT), value in addrsInChildStreamRight.items(): payload += pack('>I', (stream * 2) + 1)
timeLastReceivedMessageFromThisNode = value payload += pack(
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. '>q', 1) # service bit flags offered by this node
numberOfAddressesInAddrMessage += 1 payload += protocol.encodeHost(HOST)
payload += pack( payload += pack('>H', PORT) # remote port
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
payload += pack('>I', (stream * 2) + 1)
payload += pack(
'>q', 1) # service bit flags offered by this node
payload += protocol.encodeHost(HOST)
payload += pack('>H', PORT) # remote port
payload = encodeVarint(numberOfAddressesInAddrMessage) + payload payload = encodeVarint(numberOfAddressesInAddrMessage) + payload
self.sendDataThreadQueue.put((0, 'sendRawData', protocol.CreatePacket('addr', payload))) self.sendDataThreadQueue.put((0, 'sendRawData', protocol.CreatePacket('addr', payload)))