1
0

follow-up previous commit: write storage only if there was an upgrade

This commit is contained in:
ThomasV
2023-09-23 15:15:17 +02:00
parent 517cef8248
commit 30038f4ace
2 changed files with 9 additions and 7 deletions

View File

@@ -173,24 +173,24 @@ class JsonDB(Logger):
self.encoder = encoder self.encoder = encoder
self._modified = False self._modified = False
# load data # load data
data = self.load_data(s) data, was_upgraded = self.load_data(s)
# convert to StoredDict # convert to StoredDict
self.data = StoredDict(data, self, []) self.data = StoredDict(data, self, [])
# write file in case there was a db upgrade # write file in case there was a db upgrade
if self.storage and self.storage.file_exists(): if was_upgraded and self.storage and self.storage.file_exists():
self.write() self.write()
def load_data(self, s:str) -> dict: def load_data(self, s:str) -> dict:
""" overloaded in wallet_db """ """ overloaded in wallet_db """
if s == '': if s == '':
return {} return {}, False
try: try:
data = json.loads(s) data = json.loads(s)
except Exception: except Exception:
raise WalletFileException("Cannot read wallet file. (parsing failed)") raise WalletFileException("Cannot read wallet file. (parsing failed)")
if not isinstance(data, dict): if not isinstance(data, dict):
raise WalletFileException("Malformed wallet file (not dict)") raise WalletFileException("Malformed wallet file (not dict)")
return data return data, False
def set_modified(self, b): def set_modified(self, b):
with self.lock: with self.lock:

View File

@@ -1184,7 +1184,7 @@ class WalletDB(JsonDB):
def load_data(self, s): def load_data(self, s):
try: try:
data = JsonDB.load_data(self, s) data, _ = JsonDB.load_data(self, s)
except Exception: except Exception:
try: try:
d = ast.literal_eval(s) d = ast.literal_eval(s)
@@ -1215,13 +1215,15 @@ class WalletDB(JsonDB):
data["db_metadata"] = v data["db_metadata"] = v
dbu = WalletDBUpgrader(data) dbu = WalletDBUpgrader(data)
was_upgraded = False
if dbu.requires_split(): if dbu.requires_split():
raise WalletRequiresSplit(dbu.get_split_accounts()) raise WalletRequiresSplit(dbu.get_split_accounts())
if self._upgrade: if dbu.requires_upgrade() and self._upgrade:
dbu.upgrade() dbu.upgrade()
was_upgraded = True
if dbu.requires_upgrade(): if dbu.requires_upgrade():
raise WalletRequiresUpgrade() raise WalletRequiresUpgrade()
return dbu.data return dbu.data, was_upgraded
@locked @locked