fix: swap parent

This commit is contained in:
ThomasV 2017-07-19 14:26:44 +02:00
parent 0891798d1b
commit 7a8f337d28
1 changed files with 6 additions and 5 deletions

View File

@ -119,7 +119,7 @@ class Blockchain(util.PrintError):
return header_hash == self.get_hash(height)
def fork(parent, checkpoint):
self = Blockchain(parent.config, checkpoint, blockchains[parent.checkpoint])
self = Blockchain(parent.config, checkpoint, parent)
# create file
open(self.path(), 'w+').close()
return self
@ -199,7 +199,7 @@ class Blockchain(util.PrintError):
f.seek((checkpoint - parent.checkpoint)*80)
f.write(my_data)
# swap parameters
self.parent = parent.parent; parent.parent = parent
self.parent = parent.parent; parent.parent = self
self.checkpoint = parent.checkpoint; parent.checkpoint = checkpoint
# update pointers
blockchains[self.checkpoint] = self
@ -219,8 +219,11 @@ class Blockchain(util.PrintError):
self.swap_with_parent()
def read_header(self, height):
assert self.parent != self
if height < self.checkpoint:
return self.parent.read_header(height)
if height > self.height():
return
delta = height - self.checkpoint
name = self.path()
if os.path.exists(name):
@ -228,9 +231,7 @@ class Blockchain(util.PrintError):
f.seek(delta * 80)
h = f.read(80)
f.close()
if len(h) == 80:
h = deserialize_header(h, height)
return h
return deserialize_header(h, height)
def get_hash(self, height):
return bitcoin.GENESIS if height == 0 else hash_header(self.read_header(height))