mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-01 09:45:18 +00:00
blockchain: check best chain on disk is consistent with checkpoints
had a corrupted mainnet datadir that had testnet blockchain_headers file (I had probably corrupted it myself but electrum could not recover from it)
This commit is contained in:
parent
2484c52611
commit
73e2b09ba8
1 changed files with 18 additions and 9 deletions
|
@ -86,11 +86,20 @@ blockchains_lock = threading.RLock()
|
||||||
|
|
||||||
|
|
||||||
def read_blockchains(config: 'SimpleConfig'):
|
def read_blockchains(config: 'SimpleConfig'):
|
||||||
blockchains[constants.net.GENESIS] = Blockchain(config=config,
|
best_chain = Blockchain(config=config,
|
||||||
forkpoint=0,
|
forkpoint=0,
|
||||||
parent=None,
|
parent=None,
|
||||||
forkpoint_hash=constants.net.GENESIS,
|
forkpoint_hash=constants.net.GENESIS,
|
||||||
prev_hash=None)
|
prev_hash=None)
|
||||||
|
blockchains[constants.net.GENESIS] = best_chain
|
||||||
|
# consistency checks
|
||||||
|
if best_chain.height() > constants.net.max_checkpoint():
|
||||||
|
header_after_cp = best_chain.read_header(constants.net.max_checkpoint()+1)
|
||||||
|
if not header_after_cp or not best_chain.can_connect(header_after_cp, check_height=False):
|
||||||
|
util.print_error("[blockchain] deleting best chain. cannot connect header after last cp to last cp.")
|
||||||
|
os.unlink(best_chain.path())
|
||||||
|
best_chain.update_size()
|
||||||
|
# forks
|
||||||
fdir = os.path.join(util.get_headers_dir(config), 'forks')
|
fdir = os.path.join(util.get_headers_dir(config), 'forks')
|
||||||
util.make_dir(fdir)
|
util.make_dir(fdir)
|
||||||
# files are named as: fork2_{forkpoint}_{prev_hash}_{first_hash}
|
# files are named as: fork2_{forkpoint}_{prev_hash}_{first_hash}
|
||||||
|
@ -98,7 +107,7 @@ def read_blockchains(config: 'SimpleConfig'):
|
||||||
l = sorted(l, key=lambda x: int(x.split('_')[1])) # sort by forkpoint
|
l = sorted(l, key=lambda x: int(x.split('_')[1])) # sort by forkpoint
|
||||||
|
|
||||||
def delete_chain(filename, reason):
|
def delete_chain(filename, reason):
|
||||||
util.print_error("[blockchain]", reason, filename)
|
util.print_error(f"[blockchain] deleting chain {filename}: {reason}")
|
||||||
os.unlink(os.path.join(fdir, filename))
|
os.unlink(os.path.join(fdir, filename))
|
||||||
|
|
||||||
def instantiate_chain(filename):
|
def instantiate_chain(filename):
|
||||||
|
@ -222,10 +231,10 @@ class Blockchain(util.PrintError):
|
||||||
prev_hash=parent.get_hash(forkpoint-1))
|
prev_hash=parent.get_hash(forkpoint-1))
|
||||||
open(self.path(), 'w+').close()
|
open(self.path(), 'w+').close()
|
||||||
self.save_header(header)
|
self.save_header(header)
|
||||||
# put into global dict
|
# put into global dict. note that in some cases
|
||||||
|
# save_header might have already put it there but that's OK
|
||||||
chain_id = self.get_id()
|
chain_id = self.get_id()
|
||||||
with blockchains_lock:
|
with blockchains_lock:
|
||||||
assert chain_id not in blockchains, (chain_id, list(blockchains))
|
|
||||||
blockchains[chain_id] = self
|
blockchains[chain_id] = self
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -392,7 +401,7 @@ class Blockchain(util.PrintError):
|
||||||
delta = header.get('block_height') - self.forkpoint
|
delta = header.get('block_height') - self.forkpoint
|
||||||
data = bfh(serialize_header(header))
|
data = bfh(serialize_header(header))
|
||||||
# headers are only _appended_ to the end:
|
# headers are only _appended_ to the end:
|
||||||
assert delta == self.size()
|
assert delta == self.size(), (delta, self.size())
|
||||||
assert len(data) == HEADER_SIZE
|
assert len(data) == HEADER_SIZE
|
||||||
self.write(data, delta*HEADER_SIZE)
|
self.write(data, delta*HEADER_SIZE)
|
||||||
self.swap_with_parent()
|
self.swap_with_parent()
|
||||||
|
|
Loading…
Add table
Reference in a new issue