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,44 +658,28 @@ 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
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1 numberOfAddressesInAddrMessage += 1
payload += pack( payload += pack(
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
@ -704,9 +688,7 @@ class receiveDataThread(threading.Thread):
'>q', 1) # service bit flags offered by this node '>q', 1) # service bit flags offered by this node
payload += protocol.encodeHost(HOST) payload += protocol.encodeHost(HOST)
payload += pack('>H', PORT) # remote port payload += pack('>H', PORT) # remote port
for (HOST, PORT), value in addrsInChildStreamLeft.items(): for (HOST, PORT), timeLastReceivedMessageFromThisNode in addrsInChildStreamLeft:
timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1 numberOfAddressesInAddrMessage += 1
payload += pack( payload += pack(
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time
@ -715,9 +697,7 @@ class receiveDataThread(threading.Thread):
'>q', 1) # service bit flags offered by this node '>q', 1) # service bit flags offered by this node
payload += protocol.encodeHost(HOST) payload += protocol.encodeHost(HOST)
payload += pack('>H', PORT) # remote port payload += pack('>H', PORT) # remote port
for (HOST, PORT), value in addrsInChildStreamRight.items(): for (HOST, PORT), timeLastReceivedMessageFromThisNode in addrsInChildStreamRight:
timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old..
numberOfAddressesInAddrMessage += 1 numberOfAddressesInAddrMessage += 1
payload += pack( payload += pack(
'>Q', timeLastReceivedMessageFromThisNode) # 64-bit time '>Q', timeLastReceivedMessageFromThisNode) # 64-bit time