2013-09-07 00:58:56 +02:00
|
|
|
# objectHashHolder is a timer-driven thread. One objectHashHolder thread is used
|
2013-09-08 00:23:20 +02:00
|
|
|
# by each sendDataThread. The sendDataThread uses it whenever it needs to
|
2013-09-10 01:26:32 +02:00
|
|
|
# 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
|
2013-09-07 00:58:56 +02:00
|
|
|
# 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):
|
2016-02-13 12:54:23 +01:00
|
|
|
size = 10
|
2013-09-07 00:58:56 +02:00
|
|
|
def __init__(self, sendDataThreadMailbox):
|
2016-10-05 20:06:47 +02:00
|
|
|
threading.Thread.__init__(self, name="objectHashHolder")
|
2013-09-07 00:58:56 +02:00
|
|
|
self.shutdown = False
|
|
|
|
self.sendDataThreadMailbox = sendDataThreadMailbox # This queue is used to submit data back to our associated sendDataThread.
|
2017-01-10 21:19:57 +01:00
|
|
|
self.collectionOfHashLists = []
|
|
|
|
self.collectionOfPeerLists = []
|
|
|
|
for i in range(objectHashHolder.size):
|
|
|
|
self.collectionOfHashLists.append([])
|
|
|
|
self.collectionOfPeerLists.append([])
|
2013-09-07 00:58:56 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
iterator = 0
|
|
|
|
while not self.shutdown:
|
2013-09-10 01:26:32 +02:00
|
|
|
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] = []
|
2013-09-07 00:58:56 +02:00
|
|
|
iterator += 1
|
2017-01-10 21:19:57 +01:00
|
|
|
iterator %= objectHashHolder.size
|
2013-09-07 00:58:56 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def holdHash(self,hash):
|
2017-01-10 21:19:57 +01:00
|
|
|
self.collectionOfHashLists[random.randrange(0, objectHashHolder.size)].append(hash)
|
2013-09-10 01:26:32 +02:00
|
|
|
|
2016-02-18 00:53:13 +01:00
|
|
|
def hasHash(self, hash):
|
|
|
|
if hash in (hashlist for hashlist in self.collectionOfHashLists):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-09-10 01:26:32 +02:00
|
|
|
def holdPeer(self,peerDetails):
|
2017-01-10 21:19:57 +01:00
|
|
|
self.collectionOfPeerLists[random.randrange(0, objectHashHolder.size)].append(peerDetails)
|
2016-04-20 15:33:01 +02:00
|
|
|
|
|
|
|
def hashCount(self):
|
2016-11-01 14:44:39 +01:00
|
|
|
return sum([len(x) for x in self.collectionOfHashLists if type(x) is list])
|
2013-09-07 00:58:56 +02:00
|
|
|
|
|
|
|
def close(self):
|
2016-02-13 12:54:23 +01:00
|
|
|
self.shutdown = True
|