lnpeer: add get_channel_by_id, for small speed-up
This commit is contained in:
@@ -107,7 +107,8 @@ class Peer(Logger):
|
||||
if not (message_name.startswith("update_") or is_commitment_signed):
|
||||
return
|
||||
assert channel_id
|
||||
chan = self.channels[channel_id]
|
||||
chan = self.get_channel_by_id(channel_id)
|
||||
assert chan
|
||||
chan.hm.store_local_update_raw_msg(raw_msg, is_commitment_signed=is_commitment_signed)
|
||||
if is_commitment_signed:
|
||||
# saving now, to ensure replaying updates works (in case of channel reestablishment)
|
||||
@@ -139,10 +140,17 @@ class Peer(Logger):
|
||||
|
||||
@property
|
||||
def channels(self) -> Dict[bytes, Channel]:
|
||||
# FIXME this iterates over all channels in lnworker,
|
||||
# so if we just want to lookup a channel by channel_id, it's wasteful
|
||||
return self.lnworker.channels_for_peer(self.pubkey)
|
||||
|
||||
def get_channel_by_id(self, channel_id: bytes) -> Optional[Channel]:
|
||||
# note: this is faster than self.channels.get(channel_id)
|
||||
chan = self.lnworker.get_channel_by_id(channel_id)
|
||||
if not chan:
|
||||
return None
|
||||
if chan.node_id != self.pubkey:
|
||||
return None
|
||||
return chan
|
||||
|
||||
def diagnostic_name(self):
|
||||
return self.lnworker.__class__.__name__ + ', ' + self.transport.name()
|
||||
|
||||
@@ -158,7 +166,7 @@ class Peer(Logger):
|
||||
self.ordered_message_queues[chan_id].put_nowait((message_type, payload))
|
||||
else:
|
||||
if message_type != 'error' and 'channel_id' in payload:
|
||||
chan = self.channels.get(payload['channel_id'])
|
||||
chan = self.get_channel_by_id(payload['channel_id'])
|
||||
if chan is None:
|
||||
raise Exception('Got unknown '+ message_type)
|
||||
args = (chan, payload)
|
||||
|
||||
@@ -504,6 +504,9 @@ class LNWallet(LNWorker):
|
||||
with self.lock:
|
||||
return self._channels.copy()
|
||||
|
||||
def get_channel_by_id(self, channel_id: bytes) -> Optional[Channel]:
|
||||
return self._channels.get(channel_id, None)
|
||||
|
||||
@ignore_exceptions
|
||||
@log_exceptions
|
||||
async def sync_with_local_watchtower(self):
|
||||
@@ -691,7 +694,8 @@ class LNWallet(LNWorker):
|
||||
|
||||
def channels_for_peer(self, node_id):
|
||||
assert type(node_id) is bytes
|
||||
return {x: y for (x, y) in self.channels.items() if y.node_id == node_id}
|
||||
return {chan_id: chan for (chan_id, chan) in self.channels.items()
|
||||
if chan.node_id == node_id}
|
||||
|
||||
def channel_state_changed(self, chan):
|
||||
self.save_channel(chan)
|
||||
|
||||
@@ -170,6 +170,7 @@ class MockLNWallet(Logger, NetworkRetryManager[LNPeerAddr]):
|
||||
try_force_closing = LNWallet.try_force_closing
|
||||
get_first_timestamp = lambda self: 0
|
||||
on_peer_successfully_established = LNWallet.on_peer_successfully_established
|
||||
get_channel_by_id = LNWallet.get_channel_by_id
|
||||
|
||||
|
||||
class MockTransport:
|
||||
|
||||
Reference in New Issue
Block a user