mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
blockchain: size method, various fixes
This commit is contained in:
parent
b2116d87ea
commit
b0277d5426
2 changed files with 45 additions and 38 deletions
|
@ -66,8 +66,8 @@ blockchains = {}
|
||||||
def read_blockchains(config):
|
def read_blockchains(config):
|
||||||
blockchains[0] = Blockchain(config, 'blockchain_headers')
|
blockchains[0] = Blockchain(config, 'blockchain_headers')
|
||||||
# fixme: sort
|
# fixme: sort
|
||||||
for x in os.listdir(config.path):
|
l = sorted(filter(lambda x: x.startswith('fork_'), os.listdir(config.path)))
|
||||||
if x.startswith('fork_'):
|
for x in l:
|
||||||
b = Blockchain(config, x)
|
b = Blockchain(config, x)
|
||||||
blockchains[b.checkpoint] = b
|
blockchains[b.checkpoint] = b
|
||||||
return blockchains
|
return blockchains
|
||||||
|
@ -75,10 +75,8 @@ def read_blockchains(config):
|
||||||
def get_blockchain(header):
|
def get_blockchain(header):
|
||||||
if type(header) is not dict:
|
if type(header) is not dict:
|
||||||
return False
|
return False
|
||||||
header_hash = hash_header(header)
|
|
||||||
height = header.get('block_height')
|
|
||||||
for b in blockchains.values():
|
for b in blockchains.values():
|
||||||
if header_hash == b.get_hash(height):
|
if b.check_header(header):
|
||||||
return b
|
return b
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -102,19 +100,27 @@ class Blockchain(util.PrintError):
|
||||||
self.checkpoint = int(filename.split('_')[2])
|
self.checkpoint = int(filename.split('_')[2])
|
||||||
else:
|
else:
|
||||||
raise BaseException('')
|
raise BaseException('')
|
||||||
self.set_local_height()
|
|
||||||
|
def check_header(self, header):
|
||||||
|
header_hash = hash_header(header)
|
||||||
|
height = header.get('block_height')
|
||||||
|
return header_hash == self.get_hash(height)
|
||||||
|
|
||||||
def fork(parent, checkpoint):
|
def fork(parent, checkpoint):
|
||||||
filename = 'fork_%d_%d'%(parent.checkpoint, checkpoint)
|
filename = 'fork_%d_%d'%(parent.checkpoint, checkpoint)
|
||||||
self = Blockchain(parent.config, filename)
|
self = Blockchain(parent.config, filename)
|
||||||
self.is_saved = False
|
self.is_saved = False
|
||||||
self.parent = parent
|
|
||||||
self.checkpoint = checkpoint
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def height(self):
|
def height(self):
|
||||||
local = self.local_height if self.is_saved else len(self.headers) - 1
|
return self.checkpoint + self.size() - 1
|
||||||
return self.checkpoint + local
|
|
||||||
|
def size(self):
|
||||||
|
if self.is_saved:
|
||||||
|
p = self.path()
|
||||||
|
return os.path.getsize(p)/80 if os.path.exists(p) else 0
|
||||||
|
else:
|
||||||
|
return len(self.headers)
|
||||||
|
|
||||||
def verify_header(self, header, prev_header, bits, target):
|
def verify_header(self, header, prev_header, bits, target):
|
||||||
prev_hash = hash_header(prev_header)
|
prev_hash = hash_header(prev_header)
|
||||||
|
@ -157,11 +163,13 @@ class Blockchain(util.PrintError):
|
||||||
if not self.is_saved:
|
if not self.is_saved:
|
||||||
self.save()
|
self.save()
|
||||||
filename = self.path()
|
filename = self.path()
|
||||||
|
d = (index * 2016 - self.checkpoint) * 80
|
||||||
|
if d < 0:
|
||||||
|
chunk = chunk[-d:]
|
||||||
|
d = 0
|
||||||
with open(filename, 'rb+') as f:
|
with open(filename, 'rb+') as f:
|
||||||
f.seek(index * 2016 * 80)
|
f.seek(d)
|
||||||
f.truncate()
|
f.write(chunk)
|
||||||
h = f.write(chunk)
|
|
||||||
self.set_local_height()
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
# recursively save parents if they have not been saved
|
# recursively save parents if they have not been saved
|
||||||
|
@ -179,28 +187,20 @@ class Blockchain(util.PrintError):
|
||||||
if not self.is_saved:
|
if not self.is_saved:
|
||||||
assert height == self.checkpoint + len(self.headers)
|
assert height == self.checkpoint + len(self.headers)
|
||||||
self.headers.append(header)
|
self.headers.append(header)
|
||||||
if len(self.headers) > 10:
|
if len(self.headers) > 10 and self.parent.size() > 10:
|
||||||
self.save()
|
self.save()
|
||||||
return
|
return
|
||||||
self.write_header(header)
|
self.write_header(header)
|
||||||
|
|
||||||
def write_header(self, header):
|
def write_header(self, header):
|
||||||
|
filename = self.path()
|
||||||
delta = header.get('block_height') - self.checkpoint
|
delta = header.get('block_height') - self.checkpoint
|
||||||
data = serialize_header(header).decode('hex')
|
data = serialize_header(header).decode('hex')
|
||||||
|
assert delta * 80 == os.path.getsize(filename)
|
||||||
assert len(data) == 80
|
assert len(data) == 80
|
||||||
filename = self.path()
|
|
||||||
with open(filename, 'rb+') as f:
|
with open(filename, 'rb+') as f:
|
||||||
f.seek(delta * 80)
|
f.seek(delta * 80)
|
||||||
f.truncate()
|
f.write(data)
|
||||||
h = f.write(data)
|
|
||||||
self.set_local_height()
|
|
||||||
|
|
||||||
def set_local_height(self):
|
|
||||||
self.local_height = 0
|
|
||||||
name = self.path()
|
|
||||||
if os.path.exists(name):
|
|
||||||
h = os.path.getsize(name)/80 - 1
|
|
||||||
self.local_height = h
|
|
||||||
|
|
||||||
def read_header(self, height):
|
def read_header(self, height):
|
||||||
if height < self.checkpoint:
|
if height < self.checkpoint:
|
||||||
|
@ -219,7 +219,7 @@ class Blockchain(util.PrintError):
|
||||||
h = f.read(80)
|
h = f.read(80)
|
||||||
f.close()
|
f.close()
|
||||||
if len(h) == 80:
|
if len(h) == 80:
|
||||||
h = deserialize_header(h, delta)
|
h = deserialize_header(h, height)
|
||||||
return h
|
return h
|
||||||
|
|
||||||
def get_hash(self, height):
|
def get_hash(self, height):
|
||||||
|
|
|
@ -851,14 +851,22 @@ class Network(util.DaemonThread):
|
||||||
else:
|
else:
|
||||||
interface.print_error("can connect at %d"% interface.good)
|
interface.print_error("can connect at %d"% interface.good)
|
||||||
b = self.blockchains.get(interface.good)
|
b = self.blockchains.get(interface.good)
|
||||||
|
# if there is a reorg we connect to the parent
|
||||||
|
if b is not None and interface.good == b.checkpoint:
|
||||||
|
interface.print_error('reorg', interface.good, interface.tip)
|
||||||
|
interface.blockchain = b.parent
|
||||||
|
interface.mode = 'default'
|
||||||
|
next_height = interface.tip
|
||||||
|
else:
|
||||||
if b is None:
|
if b is None:
|
||||||
b = interface.blockchain.fork(interface.good)
|
b = interface.blockchain.fork(interface.good)
|
||||||
b.catch_up = interface.server
|
|
||||||
interface.print_error("catching up with new chain")
|
|
||||||
self.blockchains[interface.good] = b
|
self.blockchains[interface.good] = b
|
||||||
|
interface.print_error("catching up on new blockchain", b.filename)
|
||||||
|
if b.catch_up is None:
|
||||||
|
b.catch_up = interface.server
|
||||||
|
interface.blockchain = b
|
||||||
interface.mode = 'catch_up'
|
interface.mode = 'catch_up'
|
||||||
next_height = interface.good
|
next_height = interface.good
|
||||||
interface.blockchain = b
|
|
||||||
# todo: garbage collect blockchain objects
|
# todo: garbage collect blockchain objects
|
||||||
self.notify('updated')
|
self.notify('updated')
|
||||||
|
|
||||||
|
@ -944,7 +952,6 @@ class Network(util.DaemonThread):
|
||||||
self.print_error("download failed. creating file", filename)
|
self.print_error("download failed. creating file", filename)
|
||||||
open(filename, 'wb+').close()
|
open(filename, 'wb+').close()
|
||||||
self.downloading_headers = False
|
self.downloading_headers = False
|
||||||
self.blockchains[0].set_local_height()
|
|
||||||
self.downloading_headers = True
|
self.downloading_headers = True
|
||||||
t = threading.Thread(target = download_thread)
|
t = threading.Thread(target = download_thread)
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
|
|
Loading…
Add table
Reference in a new issue