This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-11-28/src/class_objectHashHolder.py
Peter Surda 020a78b776
Exception prevention in hashCount
- I got a report that an exception can occur, and while I can't
reproduce it, this should avoid it
2016-11-01 14:44:39 +01:00

56 lines
2.3 KiB
Python

# objectHashHolder is a timer-driven thread. One objectHashHolder thread is used
# by each sendDataThread. The sendDataThread uses it whenever it needs to
# advertise an object to peers in an inv message, or advertise a peer to other
# peers in an addr message. Instead of sending them out immediately, it must
# wait a random number of seconds for each connection so that different peers
# get different objects at different times. Thus an attacker who is
# connecting to many network nodes who receives a message first from Alice
# cannot be sure if Alice is the node who originated the message.
import random
import time
import threading
class objectHashHolder(threading.Thread):
size = 10
def __init__(self, sendDataThreadMailbox):
threading.Thread.__init__(self, name="objectHashHolder")
self.shutdown = False
self.sendDataThreadMailbox = sendDataThreadMailbox # This queue is used to submit data back to our associated sendDataThread.
self.collectionOfHashLists = {}
self.collectionOfPeerLists = {}
for i in range(self.size):
self.collectionOfHashLists[i] = []
self.collectionOfPeerLists[i] = []
def run(self):
iterator = 0
while not self.shutdown:
if len(self.collectionOfHashLists[iterator]) > 0:
self.sendDataThreadMailbox.put((0, 'sendinv', self.collectionOfHashLists[iterator]))
self.collectionOfHashLists[iterator] = []
if len(self.collectionOfPeerLists[iterator]) > 0:
self.sendDataThreadMailbox.put((0, 'sendaddr', self.collectionOfPeerLists[iterator]))
self.collectionOfPeerLists[iterator] = []
iterator += 1
iterator %= self.size
time.sleep(1)
def holdHash(self,hash):
self.collectionOfHashLists[random.randrange(0, self.size)].append(hash)
def hasHash(self, hash):
if hash in (hashlist for hashlist in self.collectionOfHashLists):
logger.debug("Hash in hashHolder")
return True
return False
def holdPeer(self,peerDetails):
self.collectionOfPeerLists[random.randrange(0, self.size)].append(peerDetails)
def hashCount(self):
return sum([len(x) for x in self.collectionOfHashLists if type(x) is list])
def close(self):
self.shutdown = True