Fix a bug during initial blockchain check

An invalid block in the locally stored blockchain caused
the node to crash.
This commit is contained in:
2024-03-17 18:49:22 +01:00
parent 833a300611
commit 1d627a83cb

View File

@@ -186,7 +186,7 @@ class Blockchain:
self.__lock = Lock()
self.__load_blocks_from_disk()
def __load_blocks_from_disk(self):
block_obj = None
last_valid = None
try:
with open("blockchain", "rb") as f:
while True:
@@ -196,10 +196,11 @@ class Blockchain:
block_obj = self.add_block(block)
if not block_obj.validate(self):
break
last_valid = block_obj
except FileNotFoundError:
pass
if block_obj is not None:
self.set_latest_block(block_obj.own_hash)
if last_valid is not None:
self.set_latest_block(last_valid.own_hash)
def set_latest_block(self, block_hash):
new_block = self.get_block(block_hash)
assert new_block is not None