Browse Source

Do not convert StoredDict keys to str.

Instead, convert json keys to int when storage is read.
patch-4
ThomasV 3 years ago
parent
commit
eadd1bebb2
  1. 25
      electrum/json_db.py
  2. 8
      electrum/wallet_db.py

25
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)

8
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):

Loading…
Cancel
Save