storage: upgrade format version automatically in load_data()

This commit is contained in:
SomberNight 2017-10-12 07:08:00 +02:00
parent 1320b18d7e
commit a5ffa69a3e
3 changed files with 19 additions and 6 deletions

View File

@ -191,7 +191,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
def on_filename(filename): def on_filename(filename):
path = os.path.join(wallet_folder, filename) path = os.path.join(wallet_folder, filename)
try: try:
self.storage = WalletStorage(path) self.storage = WalletStorage(path, manual_upgrades=True)
except IOError: except IOError:
self.storage = None self.storage = None
if self.storage: if self.storage:

View File

@ -210,7 +210,7 @@ class Daemon(DaemonThread):
if path in self.wallets: if path in self.wallets:
wallet = self.wallets[path] wallet = self.wallets[path]
return wallet return wallet
storage = WalletStorage(path) storage = WalletStorage(path, manual_upgrades=True)
if not storage.file_exists(): if not storage.file_exists():
return return
if storage.is_encrypted(): if storage.is_encrypted():
@ -220,7 +220,6 @@ class Daemon(DaemonThread):
if storage.requires_split(): if storage.requires_split():
return return
if storage.requires_upgrade(): if storage.requires_upgrade():
self.print_error('upgrading wallet format')
storage.upgrade() storage.upgrade()
if storage.get_action(): if storage.get_action():
return return

View File

@ -62,8 +62,9 @@ def multisig_type(wallet_type):
class WalletStorage(PrintError): class WalletStorage(PrintError):
def __init__(self, path): def __init__(self, path, manual_upgrades=False):
self.print_error("wallet path", path) self.print_error("wallet path", path)
self.manual_upgrades = manual_upgrades
self.lock = threading.RLock() self.lock = threading.RLock()
self.data = {} self.data = {}
self.path = path self.path = path
@ -74,6 +75,9 @@ class WalletStorage(PrintError):
self.raw = f.read() self.raw = f.read()
if not self.is_encrypted(): if not self.is_encrypted():
self.load_data(self.raw) self.load_data(self.raw)
else:
# avoid new wallets getting 'upgraded'
self.put('seed_version', FINAL_SEED_VERSION)
def load_data(self, s): def load_data(self, s):
try: try:
@ -99,6 +103,12 @@ class WalletStorage(PrintError):
l = plugin_loaders.get(t) l = plugin_loaders.get(t)
if l: l() if l: l()
if not self.manual_upgrades:
if self.requires_split():
raise BaseException("This wallet has multiple accounts and must be split")
if self.requires_upgrade():
self.upgrade()
def is_encrypted(self): def is_encrypted(self):
try: try:
return base64.b64decode(self.raw)[0:4] == b'BIE1' return base64.b64decode(self.raw)[0:4] == b'BIE1'
@ -155,8 +165,6 @@ class WalletStorage(PrintError):
@profiler @profiler
def write(self): def write(self):
# this ensures that previous versions of electrum won't open the wallet
self.put('seed_version', FINAL_SEED_VERSION)
with self.lock: with self.lock:
self._write() self._write()
@ -244,10 +252,14 @@ class WalletStorage(PrintError):
return self.file_exists() and self.get_seed_version() != FINAL_SEED_VERSION return self.file_exists() and self.get_seed_version() != FINAL_SEED_VERSION
def upgrade(self): def upgrade(self):
self.print_error('upgrading wallet format')
self.convert_imported() self.convert_imported()
self.convert_wallet_type() self.convert_wallet_type()
self.convert_account() self.convert_account()
self.convert_version_14() self.convert_version_14()
self.put('seed_version', FINAL_SEED_VERSION)
self.write() self.write()
def convert_wallet_type(self): def convert_wallet_type(self):
@ -338,6 +350,8 @@ class WalletStorage(PrintError):
def convert_version_14(self): def convert_version_14(self):
# convert imported wallets for 3.0 # convert imported wallets for 3.0
if self.get_seed_version() >= 14:
return
if self.get('wallet_type') =='imported': if self.get('wallet_type') =='imported':
addresses = self.get('addresses') addresses = self.get('addresses')
if type(addresses) is list: if type(addresses) is list: