Changeset - 5813971dbecc
[Not reviewed]
default
0 3 0
Laman - 7 years ago 2017-10-19 00:05:32

sending hashes in batches
3 files changed with 26 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -59,24 +59,30 @@ class Client:
 
		print(datetime.now(), "negotiating:")
 
		progress=Progress(localTree.leafCount)
 
		while len(nodeStack)>0:
 
			i=nodeStack.pop()
 
			self._outcoming.writeMsg({"command":"req", "index":i, "dataType":"hash"})
 
			indices=[]
 
			for i in range(256):
 
				indices.append(nodeStack.pop())
 
				if len(nodeStack)==0: break
 
			indices.sort()
 
			self._outcoming.writeMsg({"command":"req", "index":indices, "dataType":"hash"})
 

	
 
			jsonData,binData=self._incoming.readMsg()
 
			assert jsonData["index"]==i
 
			assert jsonData["index"]==indices
 
			assert jsonData["dataType"]=="hash"
 
			stats.logExchangedNode()
 
			stats.logExchangedNode(len(indices))
 

	
 
			if localTree.store[i]!=binData:
 
				if 2*i+3<len(localTree.store): # inner node
 
					nodeStack.append(2*i+2)
 
					nodeStack.append(2*i+1)
 
				else:
 
					blocksToTransfer.append(i-localTree.leafStart) # leaf
 
					progress.p(i-localTree.leafStart)
 
			for (j,i) in enumerate(indices):
 
				(j1,j2)=[HashTree.HASH_LEN*ji for ji in (j,j+1)]
 
				if localTree.store[i]!=binData[j1:j2]:
 
					if 2*i+3<len(localTree.store): # inner node
 
						nodeStack.append(2*i+2)
 
						nodeStack.append(2*i+1)
 
					else:
 
						blocksToTransfer.append(i-localTree.leafStart) # leaf
 
						progress.p(i-localTree.leafStart)
 
		progress.done()
 

	
 
		return blocksToTransfer
 
		return sorted(blocksToTransfer)
 

	
 
	def sendData(self,blocksToTransfer):
 
		log.info(blocksToTransfer)
src/server.py
Show inline comments
 
@@ -111,13 +111,13 @@ class Server:
 

	
 
		return True
 

	
 
	def _requestHash(self,index):
 
		log.info("received request for node #{0}".format(index))
 
		assert index<len(self._tree.store)
 
		nodeHash=self._tree.store[index]
 
	def _requestHash(self,indices):
 
		log.info("received request for nodes #{0}".format(",".join(str(i) for i in indices)))
 
		assert all(i<len(self._tree.store) for i in indices)
 
		hashes=[self._tree.store[i] for i in indices]
 

	
 
		jsonResponse={"command":"send", "index":index, "dataType":"hash"}
 
		binResponse=nodeHash
 
		jsonResponse={"command":"send", "index":indices, "dataType":"hash"}
 
		binResponse=b"".join(hashes)
 

	
 
		return (jsonResponse,binResponse)
 

	
src/stats.py
Show inline comments
 
@@ -13,8 +13,8 @@ def logSent(data):
 
	Stats.sent+=len(data)
 

	
 

	
 
def logExchangedNode():
 
	Stats.exchangedNodes+=1
 
def logExchangedNode(k=1):
 
	Stats.exchangedNodes+=k
 

	
 

	
 
def logTransferredBlock():
0 comments (0 inline, 0 general)