From eadd1bebb2e4ed2f2bb61b208f4cae78d5049bcb Mon Sep 17 00:00:00 2001 From: ThomasV Date: Mon, 20 Sep 2021 11:26:56 +0200 Subject: [PATCH] Do not convert StoredDict keys to str. Instead, convert json keys to int when storage is read. --- electrum/json_db.py | 25 ------------------------- electrum/wallet_db.py | 8 ++++++++ 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/electrum/json_db.py b/electrum/json_db.py index 83154536b..9f06a3c6a 100644 --- a/electrum/json_db.py +++ b/electrum/json_db.py @@ -78,16 +78,8 @@ class StoredDict(dict): for k, v in list(data.items()): self.__setitem__(k, v) - def convert_key(self, key): - """Convert int keys to str keys, as only those are allowed in json.""" - # NOTE: this is evil. really hard to keep in mind and reason about. :( - # e.g.: imagine setting int keys everywhere, and then iterating over the dict: - # suddenly the keys are str... - return str(int(key)) if isinstance(key, int) else key - @locked def __setitem__(self, key, v): - key = self.convert_key(key) is_new = key not in self # early return to prevent unnecessary disk writes if not is_new and self[key] == v: @@ -119,24 +111,12 @@ class StoredDict(dict): @locked def __delitem__(self, key): - key = self.convert_key(key) dict.__delitem__(self, key) if self.db: self.db.set_modified(True) - @locked - def __getitem__(self, key): - key = self.convert_key(key) - return dict.__getitem__(self, key) - - @locked - def __contains__(self, key): - key = self.convert_key(key) - return dict.__contains__(self, key) - @locked def pop(self, key, v=_RaiseKeyError): - key = self.convert_key(key) if v is _RaiseKeyError: r = dict.pop(self, key) else: @@ -145,11 +125,6 @@ class StoredDict(dict): self.db.set_modified(True) return r - @locked - def get(self, key, default=None): - key = self.convert_key(key) - return dict.get(self, key, default) - diff --git a/electrum/wallet_db.py b/electrum/wallet_db.py index 1a01d3354..3e406a991 100644 --- a/electrum/wallet_db.py +++ b/electrum/wallet_db.py @@ -1357,6 +1357,14 @@ class WalletDB(JsonDB): v = dict((k, ShachainElement(bfh(x[0]), int(x[1]))) for k, x in v.items()) elif key == 'data_loss_protect_remote_pcp': v = dict((k, bfh(x)) for k, x in v.items()) + # convert htlc_id keys to int + if key in ['adds', 'locked_in', 'settles', 'fails', 'fee_updates', 'buckets']: + v = dict((int(k), x) for k, x in v.items()) + # convert keys to HTLCOwner + if key == 'log' or (path and path[-1] in ['locked_in', 'fails', 'settles']): + if "1" in v: + v[LOCAL] = v.pop("1") + v[REMOTE] = v.pop("-1") return v def _convert_value(self, path, key, v):