Skip to content

Commit ea2886b

Browse files
committed
stored_dict: perform type conversions at request time
1 parent 353d199 commit ea2886b

13 files changed

Lines changed: 188 additions & 144 deletions

electrum/invoices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def get_id(self) -> str:
236236
else: # on-chain
237237
return get_id_from_onchain_outputs(outputs=self.get_outputs(), timestamp=self.time)
238238

239-
def as_dict(self, status):
239+
def export(self, status):
240240
d = {
241241
'is_lightning': self.is_lightning(),
242242
'amount_BTC': format_satoshis(self.get_amount_sat()),
@@ -298,7 +298,7 @@ def can_be_paid_onchain(self) -> bool:
298298
return True
299299

300300
def to_debug_json(self) -> Dict[str, Any]:
301-
d = self.to_json()
301+
d = self.as_dict()
302302
d["lnaddr"] = self._lnaddr.to_debug_json()
303303
return d
304304

electrum/json_db.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import jsonpatch
3131
import jsonpointer
3232

33-
from .util import WalletFileException, profiler, sticky_property, MyEncoder
33+
from .util import WalletFileException, profiler, sticky_property
3434
from .logging import Logger
35-
from .stored_dict import _FLEX_KEY, BaseDB, _convert_dict_key, _convert_dict_value
35+
from .stored_dict import _FLEX_KEY, BaseDB
3636
from .storage import FileStorage
3737

3838

@@ -51,6 +51,8 @@
5151
setattr(jsonpatch.JsonPatchException, '__suppress_context__', sticky_property(True))
5252

5353

54+
55+
5456
def key_path(path: Sequence[_FLEX_KEY], key: _FLEX_KEY) -> str:
5557
def to_str(x: _FLEX_KEY) -> str:
5658
assert isinstance(x, _FLEX_KEY), repr(x)
@@ -66,6 +68,7 @@ def to_str(x: _FLEX_KEY) -> str:
6668
return '/'.join(items)
6769

6870

71+
6972
def modifier(func):
7073
def wrapper(self, *args, **kwargs):
7174
with self.lock:
@@ -89,14 +92,10 @@ def __init__(
8992
*,
9093
allow_partial_writes = True,
9194
init_db = True,
92-
encoder = MyEncoder,
93-
upgrader = None,
9495
):
9596
BaseDB.__init__(self, path)
9697
self._is_closed = True
9798
self.lock = threading.RLock()
98-
self.encoder = encoder
99-
self.upgrader = upgrader
10099
self.pending_changes = [] # type: List[str]
101100
self._modified = False
102101
if self.path:
@@ -107,21 +106,17 @@ def __init__(
107106
self.init_db()
108107
else:
109108
self.storage = None
110-
self.set_data('{}')
109+
self.json_data = {}
111110
self._is_closed = False
112111

113112
def set_data(self, json_str):
114-
data = self.load_data(json_str)
115-
if self.upgrader:
116-
data, was_upgraded = self.upgrader(data)
117-
self._modified |= was_upgraded
118-
self.json_data = self._convert_dict([], data)
113+
self.json_data = self.load_data(json_str)
119114

120115
def init_db(self):
121116
if self.storage.is_encrypted():
122117
assert self.storage.is_past_initial_decryption()
123118
json_str = self.storage.read()
124-
self.set_data(json_str)
119+
self.json_data = self.load_data(json_str)
125120
# write file in case there was a db upgrade
126121
self.write_and_force_consolidation()
127122
self._is_closed = False
@@ -176,7 +171,7 @@ def contains(self, path, key):
176171

177172
def replace(self, path, key, value):
178173
# called by setattr
179-
self.db_replace(path, key, value)
174+
self.put(path, key, value)
180175

181176
@modifier
182177
def put(self, path, key, value):
@@ -298,7 +293,7 @@ def modified(self):
298293

299294
@locked
300295
def add_patch(self, patch):
301-
self.pending_changes.append(json.dumps(patch, cls=self.encoder))
296+
self.pending_changes.append(json.dumps(patch))
302297
self.set_modified(True)
303298

304299
def db_add(self, path, key: _FLEX_KEY, value) -> None:
@@ -322,29 +317,8 @@ def dump(self, *, human_readable: bool = True) -> str:
322317
self.json_data,
323318
indent=4 if human_readable else None,
324319
sort_keys=bool(human_readable),
325-
cls=self.encoder,
326320
)
327321

328-
def _convert_dict_key(self, path: List[str], key: str) -> _FLEX_KEY:
329-
return _convert_dict_key(path, key)
330-
331-
def _convert_dict_value(self, path: List[str], v) -> Any:
332-
v = _convert_dict_value(path, v)
333-
if isinstance(v, dict):
334-
v = self._convert_dict(path, v)
335-
return v
336-
337-
def _convert_dict(self, path: List[str], data: dict):
338-
# recursively convert json dict to StoredDict
339-
assert all(isinstance(x, str) for x in path), repr(path)
340-
d = {}
341-
for k, v in list(data.items()):
342-
child_path = path + [k]
343-
k = self._convert_dict_key(path, k)
344-
v = self._convert_dict_value(child_path, v)
345-
d[k] = v
346-
return d
347-
348322
@locked
349323
def write(self):
350324
if not self.storage:

electrum/lnchannel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from .lnutil import ChannelBackupStorage, ImportedChannelBackupStorage, OnchainChannelBackupStorage
6363
from .lnutil import format_short_channel_id
6464
from .fee_policy import FEERATE_PER_KW_MIN_RELAY_LIGHTNING
65+
from .stored_dict import stored_at
6566

6667
if TYPE_CHECKING:
6768
from .lnworker import LNWallet

electrum/lnpeer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ async def channel_establishment_flow(
11841184
lnworker=self.lnworker,
11851185
initial_feerate=feerate
11861186
)
1187-
chan.storage['funding_inputs'] = [txin.prevout.to_json() for txin in funding_tx.inputs()]
1187+
chan.storage['funding_inputs'] = [txin.prevout for txin in funding_tx.inputs()]
11881188
chan.storage['has_onchain_backup'] = has_onchain_backup
11891189
chan.storage['init_height'] = self.lnworker.network.get_local_height()
11901190
chan.storage['init_timestamp'] = int(time.time())
@@ -1216,7 +1216,7 @@ async def channel_establishment_flow(
12161216
self.send_channel_ready(chan)
12171217
return chan, funding_tx
12181218

1219-
def create_channel_storage(self, channel_id, outpoint, local_config, remote_config, constraints, channel_type):
1219+
def create_channel_storage(self, channel_id, outpoint, local_config, remote_config, constraints, channel_type) -> dict:
12201220
chan_dict = {
12211221
"node_id": self.pubkey.hex(),
12221222
"channel_id": channel_id.hex(),

electrum/lnutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ def from_tuple(amount_msat, rhash, cltv_abs, htlc_id, timestamp) -> 'UpdateAddHt
19421942
htlc_id=htlc_id,
19431943
timestamp=timestamp)
19441944

1945-
def to_json(self):
1945+
def as_tuple(self):
19461946
self._validate()
19471947
return dataclasses.astuple(self)
19481948

0 commit comments

Comments
 (0)