2017-05-27 19:09:21 +02:00
|
|
|
from Queue import Queue
|
2017-05-24 16:51:49 +02:00
|
|
|
import time
|
2017-06-02 07:09:35 +02:00
|
|
|
from threading import RLock
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
from inventory import Inventory
|
|
|
|
from network.downloadqueue import DownloadQueue
|
|
|
|
from network.uploadqueue import UploadQueue
|
|
|
|
|
|
|
|
haveBloom = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
# pybloomfiltermmap
|
|
|
|
from pybloomfilter import BloomFilter
|
|
|
|
haveBloom = True
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
# pybloom
|
|
|
|
from pybloom import BloomFilter
|
|
|
|
haveBloom = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# it isn't actually implemented yet so no point in turning it on
|
|
|
|
haveBloom = False
|
|
|
|
|
2017-05-27 19:09:21 +02:00
|
|
|
class ObjectTracker(object):
|
2017-05-24 16:51:49 +02:00
|
|
|
invCleanPeriod = 300
|
|
|
|
invInitialCapacity = 50000
|
|
|
|
invErrorRate = 0.03
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.objectsNewToMe = {}
|
2017-06-02 07:09:35 +02:00
|
|
|
self.objectsNewToMeLock = RLock()
|
2017-05-24 16:51:49 +02:00
|
|
|
self.objectsNewToThem = {}
|
2017-06-02 07:09:35 +02:00
|
|
|
self.objectsNewToThemLock = RLock()
|
2017-05-27 19:09:21 +02:00
|
|
|
self.downloadPending = 0
|
|
|
|
self.downloadQueue = Queue()
|
2017-05-24 16:51:49 +02:00
|
|
|
self.initInvBloom()
|
|
|
|
self.initAddrBloom()
|
2017-06-02 07:09:35 +02:00
|
|
|
self.lastCleaned = time.time()
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def initInvBloom(self):
|
|
|
|
if haveBloom:
|
|
|
|
# lock?
|
2017-05-27 19:09:21 +02:00
|
|
|
self.invBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity,
|
|
|
|
error_rate=ObjectTracker.invErrorRate)
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def initAddrBloom(self):
|
|
|
|
if haveBloom:
|
|
|
|
# lock?
|
2017-05-27 19:09:21 +02:00
|
|
|
self.addrBloom = BloomFilter(capacity=ObjectTracker.invInitialCapacity,
|
|
|
|
error_rate=ObjectTracker.invErrorRate)
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def clean(self):
|
2017-06-02 07:09:35 +02:00
|
|
|
if self.lastCleaned < time.time() - ObjectTracker.invCleanPeriod:
|
2017-05-24 16:51:49 +02:00
|
|
|
if haveBloom:
|
2017-06-10 10:13:49 +02:00
|
|
|
# FIXME
|
2017-05-24 16:51:49 +02:00
|
|
|
if PendingDownloadQueue().size() == 0:
|
|
|
|
self.initInvBloom()
|
|
|
|
self.initAddrBloom()
|
2017-06-02 07:09:35 +02:00
|
|
|
else:
|
|
|
|
# release memory
|
|
|
|
with self.objectsNewToMeLock:
|
|
|
|
tmp = self.objectsNewToMe.copy()
|
|
|
|
self.objectsNewToMe = tmp
|
|
|
|
with self.objectsNewToThemLock:
|
|
|
|
tmp = self.objectsNewToThem.copy()
|
|
|
|
self.objectsNewToThem = tmp
|
|
|
|
self.lastCleaned = time.time()
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def hasObj(self, hashid):
|
|
|
|
if haveBloom:
|
|
|
|
return hashid in self.invBloom
|
|
|
|
else:
|
|
|
|
return hashid in self.objectsNewToMe
|
|
|
|
|
2017-05-27 19:09:21 +02:00
|
|
|
def handleReceivedInventory(self, hashId):
|
2017-05-24 16:51:49 +02:00
|
|
|
if haveBloom:
|
2017-05-27 19:09:21 +02:00
|
|
|
self.invBloom.add(hashId)
|
|
|
|
elif hashId in Inventory():
|
2017-05-24 16:51:49 +02:00
|
|
|
try:
|
2017-06-02 07:09:35 +02:00
|
|
|
with self.objectsNewToThemLock:
|
|
|
|
del self.objectsNewToThem[hashId]
|
2017-05-24 16:51:49 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
2017-06-02 07:09:35 +02:00
|
|
|
with self.objectsNewToMeLock:
|
|
|
|
self.objectsNewToMe[hashId] = True
|
2017-05-27 19:09:21 +02:00
|
|
|
# self.DownloadQueue.put(hashId)
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def hasAddr(self, addr):
|
|
|
|
if haveBloom:
|
|
|
|
return addr in self.invBloom
|
|
|
|
|
|
|
|
def addAddr(self, hashid):
|
|
|
|
if haveBloom:
|
|
|
|
self.addrBloom.add(hashid)
|
|
|
|
|
|
|
|
# addr sending -> per node upload queue, and flush every minute or so
|
|
|
|
# inv sending -> if not in bloom, inv immediately, otherwise put into a per node upload queue and flush every minute or so
|
2017-05-27 19:09:21 +02:00
|
|
|
# data sending -> a simple queue
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
# no bloom
|
|
|
|
# - if inv arrives
|
|
|
|
# - if we don't have it, add tracking and download queue
|
|
|
|
# - if we do have it, remove from tracking
|
|
|
|
# tracking downloads
|
|
|
|
# - per node hash of items the node has but we don't
|
|
|
|
# tracking inv
|
|
|
|
# - per node hash of items that neither the remote node nor we have
|
|
|
|
#
|
|
|
|
|