Skip to content

Commit bac9e25

Browse files
committed
create_channel_storage: return dict instead of StoredDict
we should avoid dangling StoredDict; this does not make sense if the DB is not in memory. (a unit test with dangling StoredDict is in the levelDB branch)
1 parent 1f4c0fc commit bac9e25

5 files changed

Lines changed: 16 additions & 9 deletions

File tree

electrum/lnchannel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Iterable, Sequence, TYPE_CHECKING, Iterator, Union, Mapping)
2727
from abc import ABC, abstractmethod
2828
import itertools
29+
import threading
2930

3031
from aiorpcx import NetAddress
3132

@@ -790,7 +791,7 @@ def __init__(
790791
Logger.__init__(self) # should be after short_channel_id is set
791792
self.lnworker = lnworker
792793
self.storage = state
793-
self.db_lock = self.storage.lock
794+
self.db_lock = threading.RLock() if isinstance(self.storage, dict) else self.storage.lock
794795
self.config = {}
795796
self.config[LOCAL] = state["local_config"]
796797
self.config[REMOTE] = state["remote_config"]

electrum/lnhtlc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from copy import deepcopy
22
from typing import Sequence, Tuple, Dict, TYPE_CHECKING, Set
3+
import threading
34

45
from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate
56
from .util import bfh, with_lock
@@ -21,7 +22,7 @@
2122

2223
class HTLCManager:
2324

24-
def __init__(self, log: 'StoredDict', *, initiator=None, initial_feerate=None):
25+
def __init__(self, log: 'StoredDict', *, initiator=None, initial_feerate=None, lock=None):
2526

2627
if len(log) == 0:
2728
# note: "htlc_id" keys in dict are str! but due to json_db magic they can *almost* be treated as int...
@@ -41,7 +42,7 @@ def __init__(self, log: 'StoredDict', *, initiator=None, initial_feerate=None):
4142
# lnchannel sometimes calls us with Channel.db_lock (== log.lock) already taken,
4243
# and we ourselves often take log.lock (via StoredDict.__getitem__).
4344
# Hence, to avoid deadlocks, we reuse this same lock.
44-
self.lock = log.lock
45+
self.lock = lock if lock else threading.RLock()
4546

4647
self._init_maybe_active_htlc_ids()
4748

electrum/lnpeer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
from .lntransport import LNTransport, LNTransportBase, LightningPeerConnectionClosed, HandshakeFailed
5353
from .lnmsg import encode_msg, decode_msg, UnknownOptionalMsgType, FailedToParseMsg
5454
from .interface import GracefulDisconnect
55-
from .json_db import StoredDict
5655
from .invoices import PR_PAID
5756
from .fee_policy import FEE_LN_ETA_TARGET, FEERATE_PER_KW_MIN_RELAY_LIGHTNING
5857
from .channel_db import FLAG_DIRECTION
@@ -1235,7 +1234,7 @@ def create_channel_storage(self, channel_id, outpoint, local_config, remote_conf
12351234
"revocation_store": {},
12361235
"channel_type": channel_type,
12371236
}
1238-
return StoredDict(chan_dict, self.lnworker.db)
1237+
return chan_dict
12391238

12401239
@non_blocking_msg_handler
12411240
async def on_open_channel(self, payload):

electrum/lnworker.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,9 +1662,15 @@ def add_channel(self, chan: Channel):
16621662
self.lnwatcher.add_channel(chan)
16631663

16641664
def add_new_channel(self, chan: Channel):
1665-
self.add_channel(chan)
1665+
# delete the old channel object, becauses it uses a dict
1666+
assert type(chan.storage) is dict
1667+
channel_id = chan.channel_id.hex()
16661668
channels_db = self.db.get_dict('channels')
1667-
channels_db[chan.channel_id.hex()] = chan.storage
1669+
channels_db[channel_id] = chan.storage
1670+
del chan
1671+
storage = channels_db[channel_id] # StoredDict
1672+
chan = Channel(storage, lnworker=self)
1673+
self.add_channel(chan)
16681674
self.wallet.set_reserved_addresses_for_chan(chan, reserved=True)
16691675
try:
16701676
self.save_channel(chan)

electrum/stored_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ def lock(self):
138138

139139
@property
140140
def path(self) -> Sequence[_FLEX_KEY] | None:
141-
# return None iff we are pruned from root
141+
# raises iff we are pruned from root
142142
x = self
143143
s = [x._key]
144144
while x._parent is not None:
145145
x = x._parent
146146
s = [x._key] + s
147147
if x._key != '':
148-
return None
148+
raise KeyError()
149149
assert self._db is not None
150150
return s
151151

0 commit comments

Comments
 (0)