mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 01:11:35 +00:00
blockchain.py: rename 'checkpoint' to 'forkpoint'
This commit is contained in:
parent
7307c800d7
commit
531cdeffa9
6 changed files with 41 additions and 42 deletions
|
@ -79,12 +79,12 @@ def read_blockchains(config):
|
||||||
l = filter(lambda x: x.startswith('fork_'), os.listdir(fdir))
|
l = filter(lambda x: x.startswith('fork_'), os.listdir(fdir))
|
||||||
l = sorted(l, key = lambda x: int(x.split('_')[1]))
|
l = sorted(l, key = lambda x: int(x.split('_')[1]))
|
||||||
for filename in l:
|
for filename in l:
|
||||||
checkpoint = int(filename.split('_')[2])
|
forkpoint = int(filename.split('_')[2])
|
||||||
parent_id = int(filename.split('_')[1])
|
parent_id = int(filename.split('_')[1])
|
||||||
b = Blockchain(config, checkpoint, parent_id)
|
b = Blockchain(config, forkpoint, parent_id)
|
||||||
h = b.read_header(b.checkpoint)
|
h = b.read_header(b.forkpoint)
|
||||||
if b.parent().can_connect(h, check_height=False):
|
if b.parent().can_connect(h, check_height=False):
|
||||||
blockchains[b.checkpoint] = b
|
blockchains[b.forkpoint] = b
|
||||||
else:
|
else:
|
||||||
util.print_error("cannot connect", filename)
|
util.print_error("cannot connect", filename)
|
||||||
return blockchains
|
return blockchains
|
||||||
|
@ -109,10 +109,10 @@ class Blockchain(util.PrintError):
|
||||||
Manages blockchain headers and their verification
|
Manages blockchain headers and their verification
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config, checkpoint, parent_id):
|
def __init__(self, config, forkpoint, parent_id):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.catch_up = None # interface catching up
|
self.catch_up = None # interface catching up
|
||||||
self.checkpoint = checkpoint
|
self.forkpoint = forkpoint
|
||||||
self.checkpoints = constants.net.CHECKPOINTS
|
self.checkpoints = constants.net.CHECKPOINTS
|
||||||
self.parent_id = parent_id
|
self.parent_id = parent_id
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
|
@ -123,18 +123,18 @@ class Blockchain(util.PrintError):
|
||||||
return blockchains[self.parent_id]
|
return blockchains[self.parent_id]
|
||||||
|
|
||||||
def get_max_child(self):
|
def get_max_child(self):
|
||||||
children = list(filter(lambda y: y.parent_id==self.checkpoint, blockchains.values()))
|
children = list(filter(lambda y: y.parent_id==self.forkpoint, blockchains.values()))
|
||||||
return max([x.checkpoint for x in children]) if children else None
|
return max([x.forkpoint for x in children]) if children else None
|
||||||
|
|
||||||
def get_checkpoint(self):
|
def get_forkpoint(self):
|
||||||
mc = self.get_max_child()
|
mc = self.get_max_child()
|
||||||
return mc if mc is not None else self.checkpoint
|
return mc if mc is not None else self.forkpoint
|
||||||
|
|
||||||
def get_branch_size(self):
|
def get_branch_size(self):
|
||||||
return self.height() - self.get_checkpoint() + 1
|
return self.height() - self.get_forkpoint() + 1
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self.get_hash(self.get_checkpoint()).lstrip('00')[0:10]
|
return self.get_hash(self.get_forkpoint()).lstrip('00')[0:10]
|
||||||
|
|
||||||
def check_header(self, header):
|
def check_header(self, header):
|
||||||
header_hash = hash_header(header)
|
header_hash = hash_header(header)
|
||||||
|
@ -142,14 +142,14 @@ class Blockchain(util.PrintError):
|
||||||
return header_hash == self.get_hash(height)
|
return header_hash == self.get_hash(height)
|
||||||
|
|
||||||
def fork(parent, header):
|
def fork(parent, header):
|
||||||
checkpoint = header.get('block_height')
|
forkpoint = header.get('block_height')
|
||||||
self = Blockchain(parent.config, checkpoint, parent.checkpoint)
|
self = Blockchain(parent.config, forkpoint, parent.forkpoint)
|
||||||
open(self.path(), 'w+').close()
|
open(self.path(), 'w+').close()
|
||||||
self.save_header(header)
|
self.save_header(header)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def height(self):
|
def height(self):
|
||||||
return self.checkpoint + self.size() - 1
|
return self.forkpoint + self.size() - 1
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
@ -183,12 +183,11 @@ class Blockchain(util.PrintError):
|
||||||
|
|
||||||
def path(self):
|
def path(self):
|
||||||
d = util.get_headers_dir(self.config)
|
d = util.get_headers_dir(self.config)
|
||||||
filename = 'blockchain_headers' if self.parent_id is None else os.path.join('forks', 'fork_%d_%d'%(self.parent_id, self.checkpoint))
|
filename = 'blockchain_headers' if self.parent_id is None else os.path.join('forks', 'fork_%d_%d'%(self.parent_id, self.forkpoint))
|
||||||
return os.path.join(d, filename)
|
return os.path.join(d, filename)
|
||||||
|
|
||||||
def save_chunk(self, index, chunk):
|
def save_chunk(self, index, chunk):
|
||||||
filename = self.path()
|
d = (index * 2016 - self.forkpoint) * 80
|
||||||
d = (index * 2016 - self.checkpoint) * 80
|
|
||||||
if d < 0:
|
if d < 0:
|
||||||
chunk = chunk[-d:]
|
chunk = chunk[-d:]
|
||||||
d = 0
|
d = 0
|
||||||
|
@ -199,28 +198,28 @@ class Blockchain(util.PrintError):
|
||||||
def swap_with_parent(self):
|
def swap_with_parent(self):
|
||||||
if self.parent_id is None:
|
if self.parent_id is None:
|
||||||
return
|
return
|
||||||
parent_branch_size = self.parent().height() - self.checkpoint + 1
|
parent_branch_size = self.parent().height() - self.forkpoint + 1
|
||||||
if parent_branch_size >= self.size():
|
if parent_branch_size >= self.size():
|
||||||
return
|
return
|
||||||
self.print_error("swap", self.checkpoint, self.parent_id)
|
self.print_error("swap", self.forkpoint, self.parent_id)
|
||||||
parent_id = self.parent_id
|
parent_id = self.parent_id
|
||||||
checkpoint = self.checkpoint
|
forkpoint = self.forkpoint
|
||||||
parent = self.parent()
|
parent = self.parent()
|
||||||
self.assert_headers_file_available(self.path())
|
self.assert_headers_file_available(self.path())
|
||||||
with open(self.path(), 'rb') as f:
|
with open(self.path(), 'rb') as f:
|
||||||
my_data = f.read()
|
my_data = f.read()
|
||||||
self.assert_headers_file_available(parent.path())
|
self.assert_headers_file_available(parent.path())
|
||||||
with open(parent.path(), 'rb') as f:
|
with open(parent.path(), 'rb') as f:
|
||||||
f.seek((checkpoint - parent.checkpoint)*80)
|
f.seek((forkpoint - parent.forkpoint)*80)
|
||||||
parent_data = f.read(parent_branch_size*80)
|
parent_data = f.read(parent_branch_size*80)
|
||||||
self.write(parent_data, 0)
|
self.write(parent_data, 0)
|
||||||
parent.write(my_data, (checkpoint - parent.checkpoint)*80)
|
parent.write(my_data, (forkpoint - parent.forkpoint)*80)
|
||||||
# store file path
|
# store file path
|
||||||
for b in blockchains.values():
|
for b in blockchains.values():
|
||||||
b.old_path = b.path()
|
b.old_path = b.path()
|
||||||
# swap parameters
|
# swap parameters
|
||||||
self.parent_id = parent.parent_id; parent.parent_id = parent_id
|
self.parent_id = parent.parent_id; parent.parent_id = parent_id
|
||||||
self.checkpoint = parent.checkpoint; parent.checkpoint = checkpoint
|
self.forkpoint = parent.forkpoint; parent.forkpoint = forkpoint
|
||||||
self._size = parent._size; parent._size = parent_branch_size
|
self._size = parent._size; parent._size = parent_branch_size
|
||||||
# move files
|
# move files
|
||||||
for b in blockchains.values():
|
for b in blockchains.values():
|
||||||
|
@ -229,8 +228,8 @@ class Blockchain(util.PrintError):
|
||||||
self.print_error("renaming", b.old_path, b.path())
|
self.print_error("renaming", b.old_path, b.path())
|
||||||
os.rename(b.old_path, b.path())
|
os.rename(b.old_path, b.path())
|
||||||
# update pointers
|
# update pointers
|
||||||
blockchains[self.checkpoint] = self
|
blockchains[self.forkpoint] = self
|
||||||
blockchains[parent.checkpoint] = parent
|
blockchains[parent.forkpoint] = parent
|
||||||
|
|
||||||
def assert_headers_file_available(self, path):
|
def assert_headers_file_available(self, path):
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
|
@ -255,7 +254,7 @@ class Blockchain(util.PrintError):
|
||||||
self.update_size()
|
self.update_size()
|
||||||
|
|
||||||
def save_header(self, header):
|
def save_header(self, header):
|
||||||
delta = header.get('block_height') - self.checkpoint
|
delta = header.get('block_height') - self.forkpoint
|
||||||
data = bfh(serialize_header(header))
|
data = bfh(serialize_header(header))
|
||||||
assert delta == self.size()
|
assert delta == self.size()
|
||||||
assert len(data) == 80
|
assert len(data) == 80
|
||||||
|
@ -263,14 +262,14 @@ class Blockchain(util.PrintError):
|
||||||
self.swap_with_parent()
|
self.swap_with_parent()
|
||||||
|
|
||||||
def read_header(self, height):
|
def read_header(self, height):
|
||||||
assert self.parent_id != self.checkpoint
|
assert self.parent_id != self.forkpoint
|
||||||
if height < 0:
|
if height < 0:
|
||||||
return
|
return
|
||||||
if height < self.checkpoint:
|
if height < self.forkpoint:
|
||||||
return self.parent().read_header(height)
|
return self.parent().read_header(height)
|
||||||
if height > self.height():
|
if height > self.height():
|
||||||
return
|
return
|
||||||
delta = height - self.checkpoint
|
delta = height - self.forkpoint
|
||||||
name = self.path()
|
name = self.path()
|
||||||
self.assert_headers_file_available(name)
|
self.assert_headers_file_available(name)
|
||||||
with open(name, 'rb') as f:
|
with open(name, 'rb') as f:
|
||||||
|
|
|
@ -88,7 +88,7 @@ class ElectrumWindow(App):
|
||||||
balance = StringProperty('')
|
balance = StringProperty('')
|
||||||
fiat_balance = StringProperty('')
|
fiat_balance = StringProperty('')
|
||||||
is_fiat = BooleanProperty(False)
|
is_fiat = BooleanProperty(False)
|
||||||
blockchain_checkpoint = NumericProperty(0)
|
blockchain_forkpoint = NumericProperty(0)
|
||||||
|
|
||||||
auto_connect = BooleanProperty(False)
|
auto_connect = BooleanProperty(False)
|
||||||
def on_auto_connect(self, instance, x):
|
def on_auto_connect(self, instance, x):
|
||||||
|
@ -654,7 +654,7 @@ class ElectrumWindow(App):
|
||||||
self.num_nodes = len(self.network.get_interfaces())
|
self.num_nodes = len(self.network.get_interfaces())
|
||||||
self.num_chains = len(self.network.get_blockchains())
|
self.num_chains = len(self.network.get_blockchains())
|
||||||
chain = self.network.blockchain()
|
chain = self.network.blockchain()
|
||||||
self.blockchain_checkpoint = chain.get_checkpoint()
|
self.blockchain_forkpoint = chain.get_forkpoint()
|
||||||
self.blockchain_name = chain.get_name()
|
self.blockchain_name = chain.get_name()
|
||||||
interface = self.network.interface
|
interface = self.network.interface
|
||||||
if interface:
|
if interface:
|
||||||
|
|
|
@ -46,7 +46,7 @@ Popup:
|
||||||
|
|
||||||
CardSeparator
|
CardSeparator
|
||||||
SettingsItem:
|
SettingsItem:
|
||||||
title: _('Fork detected at block {}').format(app.blockchain_checkpoint) if app.num_chains>1 else _('No fork detected')
|
title: _('Fork detected at block {}').format(app.blockchain_forkpoint) if app.num_chains>1 else _('No fork detected')
|
||||||
fork_description: (_('You are following branch') if app.auto_connect else _("Your server is on branch")) + ' ' + app.blockchain_name
|
fork_description: (_('You are following branch') if app.auto_connect else _("Your server is on branch")) + ' ' + app.blockchain_name
|
||||||
description: self.fork_description if app.num_chains>1 else _('Connected nodes are on the same chain')
|
description: self.fork_description if app.num_chains>1 else _('Connected nodes are on the same chain')
|
||||||
action: app.choose_blockchain_dialog
|
action: app.choose_blockchain_dialog
|
||||||
|
|
|
@ -106,9 +106,9 @@ class NodesListWidget(QTreeWidget):
|
||||||
b = network.blockchains[k]
|
b = network.blockchains[k]
|
||||||
name = b.get_name()
|
name = b.get_name()
|
||||||
if n_chains >1:
|
if n_chains >1:
|
||||||
x = QTreeWidgetItem([name + '@%d'%b.get_checkpoint(), '%d'%b.height()])
|
x = QTreeWidgetItem([name + '@%d'%b.get_forkpoint(), '%d'%b.height()])
|
||||||
x.setData(0, Qt.UserRole, 1)
|
x.setData(0, Qt.UserRole, 1)
|
||||||
x.setData(1, Qt.UserRole, b.checkpoint)
|
x.setData(1, Qt.UserRole, b.forkpoint)
|
||||||
else:
|
else:
|
||||||
x = self
|
x = self
|
||||||
for i in items:
|
for i in items:
|
||||||
|
@ -357,9 +357,9 @@ class NetworkChoiceLayout(object):
|
||||||
chains = self.network.get_blockchains()
|
chains = self.network.get_blockchains()
|
||||||
if len(chains)>1:
|
if len(chains)>1:
|
||||||
chain = self.network.blockchain()
|
chain = self.network.blockchain()
|
||||||
checkpoint = chain.get_checkpoint()
|
forkpoint = chain.get_forkpoint()
|
||||||
name = chain.get_name()
|
name = chain.get_name()
|
||||||
msg = _('Chain split detected at block {0}').format(checkpoint) + '\n'
|
msg = _('Chain split detected at block {0}').format(forkpoint) + '\n'
|
||||||
msg += (_('You are following branch') if auto_connect else _('Your server is on branch'))+ ' ' + name
|
msg += (_('You are following branch') if auto_connect else _('Your server is on branch'))+ ' ' + name
|
||||||
msg += ' (%d %s)' % (chain.get_branch_size(), _('blocks'))
|
msg += ' (%d %s)' % (chain.get_branch_size(), _('blocks'))
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -964,7 +964,7 @@ class Network(util.DaemonThread):
|
||||||
interface.blockchain = branch.parent()
|
interface.blockchain = branch.parent()
|
||||||
next_height = interface.bad
|
next_height = interface.bad
|
||||||
else:
|
else:
|
||||||
interface.print_error('checkpoint conflicts with existing fork', branch.path())
|
interface.print_error('forkpoint conflicts with existing fork', branch.path())
|
||||||
branch.write(b'', 0)
|
branch.write(b'', 0)
|
||||||
branch.save_header(interface.bad_header)
|
branch.save_header(interface.bad_header)
|
||||||
interface.mode = 'catch_up'
|
interface.mode = 'catch_up'
|
||||||
|
@ -980,7 +980,7 @@ class Network(util.DaemonThread):
|
||||||
with self.blockchains_lock:
|
with self.blockchains_lock:
|
||||||
self.blockchains[interface.bad] = b
|
self.blockchains[interface.bad] = b
|
||||||
interface.blockchain = b
|
interface.blockchain = b
|
||||||
interface.print_error("new chain", b.checkpoint)
|
interface.print_error("new chain", b.forkpoint)
|
||||||
interface.mode = 'catch_up'
|
interface.mode = 'catch_up'
|
||||||
maybe_next_height = interface.bad + 1
|
maybe_next_height = interface.bad + 1
|
||||||
if maybe_next_height <= interface.tip:
|
if maybe_next_height <= interface.tip:
|
||||||
|
@ -1143,7 +1143,7 @@ class Network(util.DaemonThread):
|
||||||
@with_interface_lock
|
@with_interface_lock
|
||||||
def blockchain(self):
|
def blockchain(self):
|
||||||
if self.interface and self.interface.blockchain is not None:
|
if self.interface and self.interface.blockchain is not None:
|
||||||
self.blockchain_index = self.interface.blockchain.checkpoint
|
self.blockchain_index = self.interface.blockchain.forkpoint
|
||||||
return self.blockchains[self.blockchain_index]
|
return self.blockchains[self.blockchain_index]
|
||||||
|
|
||||||
@with_interface_lock
|
@with_interface_lock
|
||||||
|
|
|
@ -145,7 +145,7 @@ class SPV(ThreadJob):
|
||||||
raise InnerNodeOfSpvProofIsValidTx()
|
raise InnerNodeOfSpvProofIsValidTx()
|
||||||
|
|
||||||
def undo_verifications(self):
|
def undo_verifications(self):
|
||||||
height = self.blockchain.get_checkpoint()
|
height = self.blockchain.get_forkpoint()
|
||||||
tx_hashes = self.wallet.undo_verifications(self.blockchain, height)
|
tx_hashes = self.wallet.undo_verifications(self.blockchain, height)
|
||||||
for tx_hash in tx_hashes:
|
for tx_hash in tx_hashes:
|
||||||
self.print_error("redoing", tx_hash)
|
self.print_error("redoing", tx_hash)
|
||||||
|
|
Loading…
Add table
Reference in a new issue