Skip to content

Commit 268e245

Browse files
committed
lnpeer: only set initialized after both sent AND received "init"
had a trace where we tried to send "funding_locked" before being initialized: D | lnpeer.Peer.[iq7zhmhck54vcax2vlrdcavq2m32wao7ekh6jyeglmnuuvv3js57r4id.onion:9735] | Sending FUNDING_LOCKED E | lnworker.LNWallet | Exception in on_update_open_channel: AttributeError("'LNTransport' object has no attribute 'sk'") Traceback (most recent call last): File "...\electrum\electrum\util.py", line 999, in wrapper return await func(*args, **kwargs) File "...\electrum\electrum\lnworker.py", line 674, in on_update_open_channel peer.send_funding_locked(chan) File "...\electrum\electrum\lnpeer.py", line 876, in send_funding_locked self.send_message("funding_locked", channel_id=channel_id, next_per_commitment_point=per_commitment_point_second) File "...\electrum\electrum\lnpeer.py", line 102, in send_message self.transport.send_bytes(raw_msg) File "...\electrum\electrum\lntransport.py", line 93, in send_bytes lc = aead_encrypt(self.sk, self.sn(), b'', l) AttributeError: 'LNTransport' object has no attribute 'sk'
1 parent 61dfcba commit 268e245

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

electrum/lnpeer.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def channel_id_from_funding_tx(funding_txid: str, funding_index: int) -> Tuple[b
6565
class Peer(Logger):
6666

6767
def __init__(self, lnworker: Union['LNGossip', 'LNWallet'], pubkey:bytes, transport: LNTransportBase):
68+
self._sent_init = False # type: bool
69+
self._received_init = False # type: bool
6870
self.initialized = asyncio.Event()
6971
self.querying = asyncio.Event()
7072
self.transport = transport
@@ -97,6 +99,8 @@ def __init__(self, lnworker: Union['LNGossip', 'LNWallet'], pubkey:bytes, transp
9799
def send_message(self, message_name: str, **kwargs):
98100
assert type(message_name) is str
99101
self.logger.debug(f"Sending {message_name.upper()}")
102+
if message_name.upper() != "INIT" and not self.initialized.is_set():
103+
raise Exception("tried to send message before we are initialized")
100104
raw_msg = encode_msg(message_name, **kwargs)
101105
self._store_raw_msg_if_local_update(raw_msg, message_name=message_name, channel_id=kwargs.get("channel_id"))
102106
self.transport.send_bytes(raw_msg)
@@ -116,6 +120,7 @@ async def initialize(self):
116120
if isinstance(self.transport, LNTransport):
117121
await self.transport.handshake()
118122
self.send_message("init", gflen=0, lflen=1, localfeatures=self.localfeatures)
123+
self._sent_init = True
119124

120125
@property
121126
def channels(self) -> Dict[bytes, Channel]:
@@ -176,7 +181,7 @@ def on_funding_created(self, payload):
176181
self.funding_created[channel_id].put_nowait(payload)
177182

178183
def on_init(self, payload):
179-
if self.initialized.is_set():
184+
if self._received_init:
180185
self.logger.info("ALREADY INITIALIZED BUT RECEIVED INIT")
181186
return
182187
# if they required some even flag we don't have, they will close themselves
@@ -199,7 +204,9 @@ def on_init(self, payload):
199204
self.localfeatures |= 1 << get_ln_flag_pair_of_bit(flag)
200205
if isinstance(self.transport, LNTransport):
201206
self.channel_db.add_recent_peer(self.transport.peer_addr)
202-
self.initialized.set()
207+
self._received_init = True
208+
if self._sent_init and self._received_init:
209+
self.initialized.set()
203210

204211
def on_node_announcement(self, payload):
205212
self.gossip_queue.put_nowait(('node_announcement', payload))
@@ -436,6 +443,8 @@ async def _message_loop(self):
436443
await asyncio.wait_for(self.initialize(), LN_P2P_NETWORK_TIMEOUT)
437444
except (OSError, asyncio.TimeoutError, HandshakeFailed) as e:
438445
raise GracefulDisconnect(f'initialize failed: {repr(e)}') from e
446+
if self._sent_init and self._received_init:
447+
self.initialized.set()
439448
async for msg in self.transport.read_messages():
440449
self.process_message(msg)
441450
await asyncio.sleep(.01)

electrum/lnworker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ async def on_update_open_channel(self, event, funding_outpoint, funding_txid, fu
670670

671671
if chan.get_state() == channel_states.FUNDED:
672672
peer = self.peers.get(chan.node_id)
673-
if peer:
673+
if peer and peer.initialized.is_set():
674674
peer.send_funding_locked(chan)
675675

676676
elif chan.get_state() == channel_states.OPEN:

0 commit comments

Comments
 (0)