Slightly reduced TCPConnection.sendAddr() and changed

in order to send only nodes with non-negative rating
This commit is contained in:
Dmitri Bogomolov 2018-10-30 18:11:59 +02:00
parent 2d702e9647
commit e25fb857cb
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -130,36 +130,30 @@ class TCPConnection(BMProto, TLSDispatcher): # pylint: disable=too-many-instanc
# We are going to share a maximum number of 1000 addrs (per overlapping # We are going to share a maximum number of 1000 addrs (per overlapping
# stream) with our peer. 500 from overlapping streams, 250 from the # stream) with our peer. 500 from overlapping streams, 250 from the
# left child stream, and 250 from the right child stream. # left child stream, and 250 from the right child stream.
maxAddrCount = BMConfigParser().safeGetInt("bitmessagesettings", "maxaddrperstreamsend", 500) maxAddrCount = BMConfigParser().safeGetInt(
"bitmessagesettings", "maxaddrperstreamsend", 500)
# init
templist = [] templist = []
addrs = {} addrs = {}
for stream in self.streams: for stream in self.streams:
with knownnodes.knownNodesLock: with knownnodes.knownNodesLock:
if knownnodes.knownNodes[stream]: for n, s in enumerate((stream, stream * 2, stream * 2 + 1)):
filtered = {k: v for k, v in knownnodes.knownNodes[stream].items() nodes = knownnodes.knownNodes.get(s)
if v["lastseen"] > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)} if not nodes:
elemCount = len(filtered) continue
if elemCount > maxAddrCount:
elemCount = maxAddrCount
# only if more recent than 3 hours # only if more recent than 3 hours
addrs[stream] = helper_random.randomsample(filtered.items(), elemCount) # and having positive or neutral rating
# sent 250 only if the remote isn't interested in it filtered = [
if knownnodes.knownNodes[stream * 2] and stream not in self.streams: (k, v) for k, v in nodes.iteritems()
filtered = {k: v for k, v in knownnodes.knownNodes[stream * 2].items() if v["lastseen"] > int(time.time()) -
if v["lastseen"] > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)} shared.maximumAgeOfNodesThatIAdvertiseToOthers and
elemCount = len(filtered) v["rating"] >= 0
if elemCount > maxAddrCount / 2: ]
elemCount = int(maxAddrCount / 2) # sent 250 only if the remote isn't interested in it
addrs[stream * 2] = helper_random.randomsample(filtered.items(), elemCount) elemCount = min(
if knownnodes.knownNodes[(stream * 2) + 1] and stream not in self.streams: len(filtered),
filtered = {k: v for k, v in knownnodes.knownNodes[stream * 2 + 1].items() maxAddrCount / 2 if n else maxAddrCount)
if v["lastseen"] > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers)} addrs[s] = helper_random.randomsample(filtered, elemCount)
elemCount = len(filtered)
if elemCount > maxAddrCount / 2:
elemCount = int(maxAddrCount / 2)
addrs[stream * 2 + 1] = helper_random.randomsample(filtered.items(), elemCount)
for substream in addrs: for substream in addrs:
for peer, params in addrs[substream]: for peer, params in addrs[substream]:
templist.append((substream, peer, params["lastseen"])) templist.append((substream, peer, params["lastseen"]))