tlightning: enable by default but without gossip - electrum - Electrum Bitcoin wallet
 (HTM) git clone https://git.parazyd.org/electrum
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
       ---
 (DIR) commit 6045de759bdd5b47361d1749e210ddbd863100fb
 (DIR) parent edc593a8866cd76c8d74875b1a2920bec7314632
 (HTM) Author: bitromortac <bitromortac@protonmail.com>
       Date:   Fri,  2 Oct 2020 07:39:36 +0200
       
       lightning: enable by default but without gossip
       
       Enables lightning by creating a node private key and storing it in
       tthe wallet. The gossiper is not launched at start up, only if there
       are existing channels.
       
       Diffstat:
         M electrum/lnworker.py                |       4 ++++
         M electrum/network.py                 |       7 ++++++-
         M electrum/tests/test_wallet.py       |       4 ++++
         M electrum/wallet.py                  |      21 ++++++++++++++++-----
       
       4 files changed, 30 insertions(+), 6 deletions(-)
       ---
 (DIR) diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -449,11 +449,15 @@ class LNGossip(LNWorker):
                self.features |= LnFeatures.GOSSIP_QUERIES_OPT
                self.features |= LnFeatures.GOSSIP_QUERIES_REQ
                self.unknown_ids = set()
       +        self.has_started = False
        
            def start_network(self, network: 'Network'):
                assert network
       +        if self.has_started:
       +            return
                super().start_network(network)
                asyncio.run_coroutine_threadsafe(self.taskgroup.spawn(self.maintain_db()), self.network.asyncio_loop)
       +        self.has_started = True
        
            async def maintain_db(self):
                await self.channel_db.load_data()
 (DIR) diff --git a/electrum/network.py b/electrum/network.py
       t@@ -355,7 +355,12 @@ class Network(Logger, NetworkRetryManager[ServerAddr]):
                    self.channel_db = channel_db.ChannelDB(self)
                    self.path_finder = lnrouter.LNPathFinder(self.channel_db)
                    self.lngossip = lnworker.LNGossip()
       -            self.lngossip.start_network(self)
       +
       +    def start_gossip(self):
       +        self.lngossip.start_network(self)
       +
       +    def stop_gossip(self):
       +        self.lngossip.stop()
        
            def run_from_another_thread(self, coro, *, timeout=None):
                assert self._loop_thread != threading.current_thread(), 'must not be called from network thread'
 (DIR) diff --git a/electrum/tests/test_wallet.py b/electrum/tests/test_wallet.py
       t@@ -163,6 +163,10 @@ class TestCreateRestoreWallet(WalletTestCase):
                                      gap_limit=1,
                                      config=self.config)
                wallet = d['wallet']  # type: Standard_Wallet
       +
       +        # lightning initialization
       +        self.assertTrue(wallet.db.get('lightning_privkey2').startswith('xprv'))
       +
                wallet.check_password(password)
                self.assertEqual(passphrase, wallet.keystore.get_passphrase(password))
                self.assertEqual(d['seed'], wallet.keystore.get_seed(password))
 (DIR) diff --git a/electrum/wallet.py b/electrum/wallet.py
       t@@ -283,10 +283,9 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                    self.db.put('wallet_type', self.wallet_type)
                self.contacts = Contacts(self.db)
                self._coin_price_cache = {}
       -        # lightning
       -        ln_xprv = self.db.get('lightning_privkey2')
       -        self.lnworker = LNWallet(self, ln_xprv) if ln_xprv else None
       -        self.lnbackups = LNBackups(self)
       +
       +        self.lnworker = None
       +        self.lnbackups = None
        
            def save_db(self):
                if self.storage:
       t@@ -366,6 +365,9 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                    if self.lnworker:
                        network.maybe_init_lightning()
                        self.lnworker.start_network(network)
       +                # only start gossiping when we already have channels
       +                if self.db.get('channels'):
       +                    self.network.start_gossip()
                    self.lnbackups.start_network(network)
        
            def load_and_cleanup(self):
       t@@ -2426,6 +2428,16 @@ class Deterministic_Wallet(Abstract_Wallet):
                # for a few seconds!
                self.synchronize()
        
       +        # create lightning keys
       +        if self.can_have_lightning():
       +            self.init_lightning()
       +        ln_xprv = self.db.get('lightning_privkey2')
       +        # lnworker can only be initialized once receiving addresses are available
       +        # therefore we instantiate lnworker in DeterministicWallet
       +        self.lnworker = LNWallet(self, ln_xprv) if ln_xprv else None
       +        # does it make sense to instantiate lnbackups without lnworker?
       +        self.lnbackups = LNBackups(self)
       +
            def has_seed(self):
                return self.keystore.has_seed()
        
       t@@ -2805,7 +2817,6 @@ def create_new_wallet(*, path, config: SimpleConfig, passphrase=None, password=N
            wallet.update_password(old_pw=None, new_pw=password, encrypt_storage=encrypt_file)
            wallet.synchronize()
            msg = "Please keep your seed in a safe place; if you lose it, you will not be able to restore your wallet."
       -
            wallet.save_db()
            return {'seed': seed, 'wallet': wallet, 'msg': msg}