Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
make NeoNode.AskForMoreBlocks threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
localhuman committed Aug 29, 2017
1 parent b87c441 commit f48f18e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 2 additions & 0 deletions neo/Core/Blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Blockchain(object):

__blockrequests = set()

BlockSearchTries=0

CACHELIM=4000
CMISSLIM=5
LOOPTIME = .1
Expand Down
4 changes: 3 additions & 1 deletion neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def Persist(self, block):


def PersistBlocks(self):
# self.__log.debug("PERRRRRSISST:: Hheight, b height, cache: %s/%s %s --%s " % (self.Height, self.HeaderHeight, len(self._block_cache), self.CurrentHeaderHash))
# self.__log.debug("PERRRRRSISST:: Hheight, b height, cache: %s/%s %s --%s %s" % (self.Height, self.HeaderHeight, len(self._block_cache), self.CurrentHeaderHash, self.BlockSearchTries))

while not self._disposed:
if len(self._header_index) <= self._current_block_height + 1:
Expand All @@ -627,8 +627,10 @@ def PersistBlocks(self):
hash = self._header_index[self._current_block_height + 1]

if not hash in self._block_cache:
self.BlockSearchTries +=1
break

self.BlockSearchTries=0
block = self._block_cache[hash]

try:
Expand Down
30 changes: 16 additions & 14 deletions neo/Network/NeoNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,23 @@ def connectionLost(self, reason=None):
# self.buffer_in = None
# self.pm = None

bcr = BC.Default().BlockRequests
#
toremove = []
for req in bcr:
if req in self.myblockrequests:
toremove.append(req)
[bcr.remove(req) for req in toremove]

self.myblockrequests = []

reactor.callInThread(self.ReleaseBlockRequests)
self.leader.RemoveConnectedPeer(self)

# self.leader = None
self.Log("%s disconnected %s" % (self.remote_nodeid, reason))


def ReleaseBlockRequests(self):
bcr = BC.Default().BlockRequests
#
toremove = []
for req in bcr:
if req in self.myblockrequests:
toremove.append(req)
[bcr.remove(req) for req in toremove]


self.myblockrequests = set()

def dataReceived(self, data):

Expand Down Expand Up @@ -203,14 +202,17 @@ def AskForMoreHeaders(self):
self.SendSerializedMessage(get_headers_message)



def AskForMoreBlocks(self):
reactor.callInThread(self.DoAskForMoreBlocks)

def DoAskForMoreBlocks(self):

hashes = []
hashstart = BC.Default().Height + 1

if BC.Default().BlockSearchTries > 400 and len(BC.Default().BlockRequests) > 0:
self.leader.ResetBlockRequestsAndCache()

self.Log("asking for more blocks ... %s " % hashstart)
first=None
while hashstart < BC.Default().HeaderHeight and len(hashes) < self.leader.BREQPART:
hash = BC.Default().GetHeaderHash(hashstart)
Expand All @@ -222,7 +224,7 @@ def AskForMoreBlocks(self):
hashes.append(hash)
hashstart += 1

self.Log("asked for more blocks ... %s thru %s" % (first,hashstart))
self.Log("asked for more blocks ... %s thru %s stale count %s " % (first,hashstart, BC.Default().BlockSearchTries))


if len(hashes) > 0:
Expand Down
11 changes: 9 additions & 2 deletions neo/Network/NodeLeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def Setup(self):
self.NodeId = random.randint(1294967200,4294967200)

def Restart(self):
print("will try restart!!!")
if len(self.Peers) == 0:
print("WILL DO RESTART!")
self.Start()

def Start(self):
Expand Down Expand Up @@ -104,6 +102,15 @@ def RemoveConnectedPeer(self, peer):
if len(self.Peers) == 0:
reactor.callLater(10, self.Restart)


def ResetBlockRequestsAndCache(self):
print("RESETTING BLOCK REQUESTS AND CACHE!")
BC.Default().BlockSearchTries = 0
for p in self.Peers:
p.myblockrequests= set()
BC.Default().__blockrequests = set()
BC.Default()._block_cache = {}

# @profile()
def InventoryReceived(self, inventory):

Expand Down

0 comments on commit f48f18e

Please sign in to comment.