mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-11 21:19:51 +00:00
blockchain write method, that flushes file
This commit is contained in:
parent
bfcfb1f240
commit
5f3e5866ad
2 changed files with 21 additions and 28 deletions
|
@ -191,11 +191,7 @@ class Blockchain(util.PrintError):
|
||||||
if d < 0:
|
if d < 0:
|
||||||
chunk = chunk[-d:]
|
chunk = chunk[-d:]
|
||||||
d = 0
|
d = 0
|
||||||
with self.lock:
|
self.write(chunk, d)
|
||||||
with open(filename, 'rb+') as f:
|
|
||||||
f.seek(d)
|
|
||||||
f.write(chunk)
|
|
||||||
self.update_size()
|
|
||||||
self.swap_with_parent()
|
self.swap_with_parent()
|
||||||
|
|
||||||
def swap_with_parent(self):
|
def swap_with_parent(self):
|
||||||
|
@ -208,23 +204,13 @@ class Blockchain(util.PrintError):
|
||||||
parent_id = self.parent_id
|
parent_id = self.parent_id
|
||||||
checkpoint = self.checkpoint
|
checkpoint = self.checkpoint
|
||||||
parent = self.parent()
|
parent = self.parent()
|
||||||
with open(parent.path(), 'rb+') as f:
|
with open(self.path(), 'rb') as f:
|
||||||
|
my_data = f.read()
|
||||||
|
with open(parent.path(), 'rb') as f:
|
||||||
f.seek((checkpoint - parent.checkpoint)*80)
|
f.seek((checkpoint - parent.checkpoint)*80)
|
||||||
parent_data = f.read(parent_branch_size*80)
|
parent_data = f.read(parent_branch_size*80)
|
||||||
with self.lock:
|
self.write(parent_data, 0)
|
||||||
with open(self.path(), 'rb+') as f:
|
parent.write(my_data, (checkpoint - parent.checkpoint)*80)
|
||||||
my_data = f.read()
|
|
||||||
f.seek(0)
|
|
||||||
f.truncate()
|
|
||||||
f.write(parent_data)
|
|
||||||
self.update_size()
|
|
||||||
with parent.lock:
|
|
||||||
with open(parent.path(), 'rb+') as f:
|
|
||||||
f.seek((checkpoint - parent.checkpoint)*80)
|
|
||||||
f.truncate()
|
|
||||||
f.seek((checkpoint - parent.checkpoint)*80)
|
|
||||||
f.write(my_data)
|
|
||||||
parent.update_size()
|
|
||||||
# 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()
|
||||||
|
@ -241,18 +227,25 @@ class Blockchain(util.PrintError):
|
||||||
blockchains[self.checkpoint] = self
|
blockchains[self.checkpoint] = self
|
||||||
blockchains[parent.checkpoint] = parent
|
blockchains[parent.checkpoint] = parent
|
||||||
|
|
||||||
def save_header(self, header):
|
def write(self, data, offset):
|
||||||
filename = self.path()
|
filename = self.path()
|
||||||
|
with self.lock:
|
||||||
|
with open(filename, 'rb+') as f:
|
||||||
|
if offset != self._size*80:
|
||||||
|
f.seek(offset)
|
||||||
|
f.truncate()
|
||||||
|
f.seek(offset)
|
||||||
|
f.write(data)
|
||||||
|
f.flush()
|
||||||
|
os.fsync(f.fileno())
|
||||||
|
self.update_size()
|
||||||
|
|
||||||
|
def save_header(self, header):
|
||||||
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 == self.size()
|
assert delta == self.size()
|
||||||
assert len(data) == 80
|
assert len(data) == 80
|
||||||
with self.lock:
|
self.write(data, delta*80)
|
||||||
with open(filename, 'rb+') as f:
|
|
||||||
f.seek(delta * 80)
|
|
||||||
f.write(data)
|
|
||||||
self.update_size()
|
|
||||||
# order files
|
|
||||||
self.swap_with_parent()
|
self.swap_with_parent()
|
||||||
|
|
||||||
def read_header(self, height):
|
def read_header(self, height):
|
||||||
|
|
|
@ -842,7 +842,7 @@ class Network(util.DaemonThread):
|
||||||
next_height = None
|
next_height = None
|
||||||
else:
|
else:
|
||||||
interface.print_error('checkpoint conflicts with existing fork', branch.path())
|
interface.print_error('checkpoint conflicts with existing fork', branch.path())
|
||||||
open(branch.path(), 'w+').close()
|
branch.write('', 0)
|
||||||
branch.save_header(interface.bad_header)
|
branch.save_header(interface.bad_header)
|
||||||
interface.mode = 'catch_up'
|
interface.mode = 'catch_up'
|
||||||
interface.blockchain = branch
|
interface.blockchain = branch
|
||||||
|
|
Loading…
Add table
Reference in a new issue