URI: 
       tlnpeer: add get_channel_by_id, for small speed-up - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 6f7a4ab048bbdb4215ae47f206a878aa6215bf31
   DIR parent b9b53e7f76eb054773a9a495593396b6b18af85a
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Thu, 30 Apr 2020 21:13:29 +0200
       
       lnpeer: add get_channel_by_id, for small speed-up
       
       Diffstat:
         M electrum/lnpeer.py                  |      16 ++++++++++++----
         M electrum/lnworker.py                |       6 +++++-
         M electrum/tests/test_lnpeer.py       |       1 +
       
       3 files changed, 18 insertions(+), 5 deletions(-)
       ---
   DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py
       t@@ -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)
       t@@ -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()
        
       t@@ -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)
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -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):
       t@@ -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)
   DIR diff --git a/electrum/tests/test_lnpeer.py b/electrum/tests/test_lnpeer.py
       t@@ -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: