Changeset - e6752944fe6b
[Not reviewed]
default
0 1 0
Laman - 8 years ago 2017-05-07 17:29:12

hashtree loading and saving
1 file changed with 18 insertions and 3 deletions:
0 comments (0 inline, 0 general) First comment
src/hashtree.py
Show inline comments
 
import hashlib
 
import os
 
import collections
 

	
 

	
 
class HashTree:
 
@@ -9,7 +8,7 @@ class HashTree:
 
	
 
	## Prepares a tree containing leafCount leaves.
 
	def __init__(self,leafCount):
 
		self.store=[None]*(leafCount*2-1)
 
		self.store=[b""]*(leafCount*2-1)
 
		self.leafStart=leafCount-1
 
		self.index=self.leafStart
 
		self.leafCount=leafCount
 
@@ -27,7 +26,23 @@ class HashTree:
 
		res.buildTree()
 
		
 
		return res
 
		
 

	
 
	@classmethod
 
	def load(cls,filename):
 
		with open(filename,"rb") as f:
 
			stat=os.fstat(f.fileno())
 
			size=stat.st_size
 
			nodeCount=size//HashTree.HASH_LEN
 
			res=cls((nodeCount+1)//2)
 

	
 
			for i in range(nodeCount):
 
				res.store[i]=f.read(HashTree.HASH_LEN)
 

	
 
	def save(self,filename):
 
		with open(filename,"wb") as f:
 
			for h in self.store:
 
				f.write(h)
 

	
 
		
 
	## Inserts a leaf at the first empty position.
 
	#	
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now