# HG changeset patch
# User Laman
# Date 2017-10-19 00:05:32
# Node ID 5813971dbeccbe1c8b6455fd70ed75ed8693d712
# Parent  9bfcb1e83e61e754e0ff90a816530f04e5931182

sending hashes in batches

diff --git a/src/client.py b/src/client.py
--- a/src/client.py
+++ b/src/client.py
@@ -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)
diff --git a/src/server.py b/src/server.py
--- a/src/server.py
+++ b/src/server.py
@@ -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)
 
diff --git a/src/stats.py b/src/stats.py
--- a/src/stats.py
+++ b/src/stats.py
@@ -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():