2017-05-29 00:24:07 +02:00
|
|
|
import Queue
|
2017-09-30 13:42:04 +02:00
|
|
|
from random import randint, shuffle
|
2017-05-29 00:24:07 +02:00
|
|
|
import threading
|
2017-09-25 08:49:21 +02:00
|
|
|
from time import time
|
2017-05-29 00:24:07 +02:00
|
|
|
|
|
|
|
import addresses
|
2017-09-25 08:49:21 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-05-29 00:24:07 +02:00
|
|
|
from helper_threading import StoppableThread
|
|
|
|
from network.connectionpool import BMConnectionPool
|
2017-10-20 01:21:49 +02:00
|
|
|
from network.dandelion import Dandelion
|
2017-05-29 00:24:07 +02:00
|
|
|
from queues import invQueue
|
|
|
|
import protocol
|
|
|
|
import state
|
|
|
|
|
|
|
|
class InvThread(threading.Thread, StoppableThread):
|
|
|
|
def __init__(self):
|
2017-07-10 07:05:50 +02:00
|
|
|
threading.Thread.__init__(self, name="InvBroadcaster")
|
2017-05-29 00:24:07 +02:00
|
|
|
self.initStop()
|
2017-07-10 07:05:50 +02:00
|
|
|
self.name = "InvBroadcaster"
|
2017-05-29 00:24:07 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not state.shutdown:
|
2017-06-27 13:25:12 +02:00
|
|
|
chunk = []
|
2017-05-29 00:24:07 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2017-05-29 14:52:31 +02:00
|
|
|
data = invQueue.get(False)
|
2017-10-19 08:56:48 +02:00
|
|
|
chunk.append((data[0], data[1]))
|
2017-09-25 08:49:21 +02:00
|
|
|
# locally generated
|
2017-05-29 14:52:31 +02:00
|
|
|
if len(data) == 2:
|
2017-10-20 01:21:49 +02:00
|
|
|
Dandelion().addHash(data[1], None)
|
2017-06-24 12:13:35 +02:00
|
|
|
BMConnectionPool().handleReceivedObject(data[0], data[1])
|
2017-09-25 08:49:21 +02:00
|
|
|
# came over the network
|
2017-05-29 14:52:31 +02:00
|
|
|
else:
|
2017-07-08 18:02:47 +02:00
|
|
|
source = BMConnectionPool().getConnectionByAddr(data[2])
|
|
|
|
BMConnectionPool().handleReceivedObject(data[0], data[1], source)
|
2017-05-29 00:24:07 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
2017-07-08 18:02:47 +02:00
|
|
|
# connection not found, handle it as if generated locally
|
|
|
|
except KeyError:
|
|
|
|
BMConnectionPool().handleReceivedObject(data[0], data[1])
|
2017-05-29 00:24:07 +02:00
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
if chunk:
|
|
|
|
for connection in BMConnectionPool().inboundConnections.values() + \
|
|
|
|
BMConnectionPool().outboundConnections.values():
|
2017-09-25 08:49:21 +02:00
|
|
|
fluffs = []
|
|
|
|
stems = []
|
2017-06-27 13:25:12 +02:00
|
|
|
for inv in chunk:
|
|
|
|
if inv[0] not in connection.streams:
|
|
|
|
continue
|
2017-05-29 00:24:07 +02:00
|
|
|
try:
|
2017-06-27 13:25:12 +02:00
|
|
|
with connection.objectsNewToThemLock:
|
|
|
|
del connection.objectsNewToThem[inv[1]]
|
2017-05-29 00:24:07 +02:00
|
|
|
except KeyError:
|
|
|
|
continue
|
2017-10-20 01:21:49 +02:00
|
|
|
try:
|
|
|
|
if connection == Dandelion().hashMap[inv[1]]:
|
2017-09-30 13:42:04 +02:00
|
|
|
# Fluff trigger by RNG
|
|
|
|
# auto-ignore if config set to 0, i.e. dandelion is off
|
2017-10-20 01:21:49 +02:00
|
|
|
# send a normal inv if stem node doesn't support dandelion
|
|
|
|
if randint(1, 100) < BMConfigParser().safeGetBoolean("network", "dandelion") and \
|
|
|
|
connection.services | protocol.NODE_DANDELION > 0:
|
2017-09-30 13:42:04 +02:00
|
|
|
stems.append(inv[1])
|
|
|
|
else:
|
|
|
|
fluffs.append(inv[1])
|
2017-10-20 01:21:49 +02:00
|
|
|
except KeyError:
|
2017-09-30 13:42:04 +02:00
|
|
|
fluffs.append(inv[1])
|
2017-10-20 01:21:49 +02:00
|
|
|
|
2017-09-25 08:49:21 +02:00
|
|
|
if fluffs:
|
2017-09-30 13:42:04 +02:00
|
|
|
shuffle(fluffs)
|
2017-07-06 19:45:36 +02:00
|
|
|
connection.append_write_buf(protocol.CreatePacket('inv', \
|
2017-09-25 08:49:21 +02:00
|
|
|
addresses.encodeVarint(len(fluffs)) + "".join(fluffs)))
|
|
|
|
if stems:
|
2017-09-30 13:42:04 +02:00
|
|
|
shuffle(stems)
|
2017-09-25 08:49:21 +02:00
|
|
|
connection.append_write_buf(protocol.CreatePacket('dinv', \
|
|
|
|
addresses.encodeVarint(len(stems)) + "".join(stems)))
|
2017-10-20 01:21:49 +02:00
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
invQueue.iterate()
|
2017-10-19 08:56:48 +02:00
|
|
|
for i in range(len(chunk)):
|
|
|
|
invQueue.task_done()
|
2017-10-20 01:21:49 +02:00
|
|
|
|
|
|
|
if Dandelion().refresh < time():
|
|
|
|
BMConnectionPool().reRandomiseDandelionStems()
|
|
|
|
|
2017-05-29 00:24:07 +02:00
|
|
|
self.stop.wait(1)
|