From 30038f4ace8334fd17853658b9ecb6cefa76707e Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sat, 23 Sep 2023 15:15:17 +0200 Subject: [PATCH] follow-up previous commit: write storage only if there was an upgrade --- electrum/json_db.py | 8 ++++---- electrum/wallet_db.py | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/electrum/json_db.py b/electrum/json_db.py index cde0f27ae..9c3fd6be8 100644 --- a/electrum/json_db.py +++ b/electrum/json_db.py @@ -173,24 +173,24 @@ class JsonDB(Logger): self.encoder = encoder self._modified = False # load data - data = self.load_data(s) + data, was_upgraded = self.load_data(s) # convert to StoredDict self.data = StoredDict(data, self, []) # 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() def load_data(self, s:str) -> dict: """ overloaded in wallet_db """ if s == '': - return {} + return {}, False try: data = json.loads(s) except Exception: raise WalletFileException("Cannot read wallet file. (parsing failed)") if not isinstance(data, dict): raise WalletFileException("Malformed wallet file (not dict)") - return data + return data, False def set_modified(self, b): with self.lock: diff --git a/electrum/wallet_db.py b/electrum/wallet_db.py index 69e34dd8b..1ed47434c 100644 --- a/electrum/wallet_db.py +++ b/electrum/wallet_db.py @@ -1184,7 +1184,7 @@ class WalletDB(JsonDB): def load_data(self, s): try: - data = JsonDB.load_data(self, s) + data, _ = JsonDB.load_data(self, s) except Exception: try: d = ast.literal_eval(s) @@ -1215,13 +1215,15 @@ class WalletDB(JsonDB): data["db_metadata"] = v dbu = WalletDBUpgrader(data) + was_upgraded = False if dbu.requires_split(): raise WalletRequiresSplit(dbu.get_split_accounts()) - if self._upgrade: + if dbu.requires_upgrade() and self._upgrade: dbu.upgrade() + was_upgraded = True if dbu.requires_upgrade(): raise WalletRequiresUpgrade() - return dbu.data + return dbu.data, was_upgraded @locked