blockchain: remove filename field

This commit is contained in:
ThomasV 2017-07-19 11:26:13 +02:00
parent 47e3630dd5
commit ac1ceb44ba
1 changed files with 12 additions and 18 deletions

View File

@ -64,11 +64,13 @@ def hash_header(header):
blockchains = {} blockchains = {}
def read_blockchains(config): def read_blockchains(config):
blockchains[0] = Blockchain(config, 'blockchain_headers') blockchains[0] = Blockchain(config, 0, None)
l = filter(lambda x: x.startswith('fork_'), os.listdir(config.path)) l = filter(lambda x: x.startswith('fork_'), os.listdir(config.path))
l = sorted(l, key = lambda x: int(x.split('_')[1])) l = sorted(l, key = lambda x: int(x.split('_')[1]))
for x in l: for filename in l:
b = Blockchain(config, x) checkpoint = int(filename.split('_')[2])
parent = blockchains[int(filename.split('_')[1])]
b = Blockchain(config, checkpoint, parent)
blockchains[b.checkpoint] = b blockchains[b.checkpoint] = b
return blockchains return blockchains
@ -91,18 +93,11 @@ class Blockchain(util.PrintError):
'''Manages blockchain headers and their verification''' '''Manages blockchain headers and their verification'''
def __init__(self, config, filename): def __init__(self, config, checkpoint, parent):
self.config = config self.config = config
self.filename = filename
self.catch_up = None # interface catching up self.catch_up = None # interface catching up
if filename == 'blockchain_headers': self.checkpoint = checkpoint
self.parent = None self.parent = parent
self.checkpoint = 0
elif filename.startswith('fork_'):
self.parent = blockchains[int(filename.split('_')[1])]
self.checkpoint = int(filename.split('_')[2])
else:
raise BaseException('')
def get_max_child(self): def get_max_child(self):
children = filter(lambda y: y.parent==self, blockchains.values()) children = filter(lambda y: y.parent==self, blockchains.values())
@ -124,8 +119,7 @@ class Blockchain(util.PrintError):
return header_hash == self.get_hash(height) return header_hash == self.get_hash(height)
def fork(parent, checkpoint): def fork(parent, checkpoint):
filename = 'fork_%d_%d'%(parent.checkpoint, checkpoint) self = Blockchain(parent.config, checkpoint, blockchains[parent.checkpoint])
self = Blockchain(parent.config, filename)
# create file # create file
open(self.path(), 'w+').close() open(self.path(), 'w+').close()
return self return self
@ -172,7 +166,8 @@ class Blockchain(util.PrintError):
def path(self): def path(self):
d = util.get_headers_dir(self.config) d = util.get_headers_dir(self.config)
return os.path.join(d, self.filename) filename = 'blockchain_headers' if self.parent is None else 'fork_%d_%d'%(self.parent.checkpoint, self.checkpoint)
return os.path.join(d, filename)
def save_chunk(self, index, chunk): def save_chunk(self, index, chunk):
filename = self.path() filename = self.path()
@ -185,7 +180,7 @@ class Blockchain(util.PrintError):
f.write(chunk) f.write(chunk)
def swap_with_parent(self): def swap_with_parent(self):
self.print_error("swap", self.filename, self.parent.filename) self.print_error("swap", self.checkpoint, self.parent.checkpoint)
assert self.size() == self.get_branch_size() assert self.size() == self.get_branch_size()
parent = self.parent parent = self.parent
checkpoint = self.checkpoint checkpoint = self.checkpoint
@ -204,7 +199,6 @@ class Blockchain(util.PrintError):
f.seek((checkpoint - parent.checkpoint)*80) f.seek((checkpoint - parent.checkpoint)*80)
f.write(my_data) f.write(my_data)
# swap parameters # swap parameters
fn = self.filename; self.filename = parent.filename; parent.filename = fn
self.parent = parent.parent; parent.parent = parent self.parent = parent.parent; parent.parent = parent
self.checkpoint = parent.checkpoint; parent.checkpoint = checkpoint self.checkpoint = parent.checkpoint; parent.checkpoint = checkpoint
# update pointers # update pointers