electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit c8bed8791a4134b3f38721a84ccf71e047e1615c
parent cbf1b5d9d5670fc84fbe47d6d20eddda24ea20f5
Author: SomberNight <somber.night@protonmail.com>
Date:   Sun, 29 Apr 2018 18:25:10 +0200

assert datadir available

Diffstat:
Mlib/blockchain.py | 27+++++++++++++++++----------
Mlib/interface.py | 2++
Mlib/simple_config.py | 6++----
Mlib/util.py | 21+++++++++++++++++++++
4 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/lib/blockchain.py b/lib/blockchain.py @@ -200,8 +200,10 @@ class Blockchain(util.PrintError): parent_id = self.parent_id checkpoint = self.checkpoint parent = self.parent() + self.assert_headers_file_available(self.path()) with open(self.path(), 'rb') as f: my_data = f.read() + self.assert_headers_file_available(parent.path()) with open(parent.path(), 'rb') as f: f.seek((checkpoint - parent.checkpoint)*80) parent_data = f.read(parent_branch_size*80) @@ -224,9 +226,18 @@ class Blockchain(util.PrintError): blockchains[self.checkpoint] = self blockchains[parent.checkpoint] = parent + def assert_headers_file_available(self, path): + if os.path.exists(path): + return + elif not os.path.exists(util.get_headers_dir(self.config)): + raise FileNotFoundError('Electrum headers_dir does not exist. Was it deleted while running?') + else: + raise FileNotFoundError('Cannot find headers file but headers_dir is there. Should be at {}'.format(path)) + def write(self, data, offset, truncate=True): filename = self.path() with self.lock: + self.assert_headers_file_available(filename) with open(filename, 'rb+') as f: if truncate and offset != self._size*80: f.seek(offset) @@ -255,16 +266,12 @@ class Blockchain(util.PrintError): return delta = height - self.checkpoint name = self.path() - if os.path.exists(name): - with open(name, 'rb') as f: - f.seek(delta * 80) - h = f.read(80) - if len(h) < 80: - raise Exception('Expected to read a full header. This was only {} bytes'.format(len(h))) - elif not os.path.exists(util.get_headers_dir(self.config)): - raise Exception('Electrum datadir does not exist. Was it deleted while running?') - else: - raise Exception('Cannot find headers file but datadir is there. Should be at {}'.format(name)) + self.assert_headers_file_available(name) + with open(name, 'rb') as f: + f.seek(delta * 80) + h = f.read(80) + if len(h) < 80: + raise Exception('Expected to read a full header. This was only {} bytes'.format(len(h))) if h == bytes([0])*80: return None return deserialize_header(h, height) diff --git a/lib/interface.py b/lib/interface.py @@ -172,6 +172,7 @@ class TcpConnection(threading.Thread, util.PrintError): # workaround android bug cert = re.sub("([^\n])-----END CERTIFICATE-----","\\1\n-----END CERTIFICATE-----",cert) temporary_path = cert_path + '.temp' + util.assert_datadir_available(self.config_path) with open(temporary_path, "w", encoding='utf-8') as f: f.write(cert) f.flush() @@ -201,6 +202,7 @@ class TcpConnection(threading.Thread, util.PrintError): os.unlink(rej) os.rename(temporary_path, rej) else: + util.assert_datadir_available(self.config_path) with open(cert_path, encoding='utf-8') as f: cert = f.read() try: diff --git a/lib/simple_config.py b/lib/simple_config.py @@ -6,6 +6,7 @@ import stat from copy import deepcopy +from . import util from .util import (user_dir, print_error, PrintError, NoDynamicFeeEstimates, format_fee_satoshis) from .i18n import _ @@ -236,10 +237,7 @@ class SimpleConfig(PrintError): return path # default path - if not os.path.exists(self.path): - raise FileNotFoundError( - _('Electrum datadir does not exist. Was it deleted while running?') + '\n' + - _('Should be at {}').format(self.path)) + util.assert_datadir_available(self.path) dirpath = os.path.join(self.path, "wallets") if not os.path.exists(dirpath): if os.path.islink(dirpath): diff --git a/lib/util.py b/lib/util.py @@ -326,10 +326,31 @@ def android_check_data_dir(): shutil.move(old_electrum_dir, data_dir) return data_dir + def get_headers_dir(config): return android_headers_dir() if 'ANDROID_DATA' in os.environ else config.path +def assert_datadir_available(config_path): + path = config_path + if os.path.exists(path): + return + else: + raise FileNotFoundError( + 'Electrum datadir does not exist. Was it deleted while running?' + '\n' + + 'Should be at {}'.format(path)) + + +def assert_file_in_datadir_available(path, config_path): + if os.path.exists(path): + return + else: + assert_datadir_available(config_path) + raise FileNotFoundError( + 'Cannot find file but datadir is there.' + '\n' + + 'Should be at {}'.format(path)) + + def assert_bytes(*args): """ porting helper, assert args type