This repository has been archived on 2024-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-03/src/network/node.py

67 lines
1.8 KiB
Python
Raw Normal View History

import time
2017-04-16 18:27:15 +02:00
from inventory import PendingDownloadQueue
2017-04-16 18:27:15 +02:00
try:
# pybloomfiltermmap
from pybloomfilter import BloomFilter
except ImportError:
try:
# pybloom
from pybloom import BloomFilter
except ImportError:
# bundled pybloom
from fallback.pybloom import BloomFilter
2017-04-16 18:27:15 +02:00
class Node(object):
invCleanPeriod = 300
invInitialCapacity = 50000
invErrorRate = 0.03
2017-04-16 18:27:15 +02:00
def __init__(self):
self.initInvBloom()
self.initAddrBloom()
2017-04-16 18:27:15 +02:00
def initInvBloom(self):
# lock?
self.invBloom = BloomFilter(capacity=Node.invInitialCapacity,
error_rate=Node.invErrorRate)
2017-04-16 18:27:15 +02:00
def initAddrBloom(self):
# lock?
self.addrBloom = BloomFilter(capacity=Node.invInitialCapacity,
error_rate=Node.invErrorRate)
2017-04-16 18:27:15 +02:00
def cleanBloom(self):
if self.lastcleaned < time.time() - Node.invCleanPeriod:
if PendingDownloadQueue().size() == 0:
self.initInvBloom()
self.initAddrBloom()
def hasInv(self, hashid):
return hashid in self.invBloom
def addInv(self, hashid):
self.invBloom.add(hashid)
def hasAddr(self, hashid):
return hashid in self.invBloom
def addInv(self, hashid):
self.invBloom.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
# 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
#
2017-04-16 18:27:15 +02:00