Browse Source

lightning: enable by default but without gossip

Enables lightning by creating a node private key and storing it in
the wallet. The gossiper is not launched at start up, only if there
are existing channels.
patch-4
bitromortac 4 years ago
committed by ThomasV
parent
commit
6045de759b
  1. 4
      electrum/lnworker.py
  2. 7
      electrum/network.py
  3. 4
      electrum/tests/test_wallet.py
  4. 21
      electrum/wallet.py

4
electrum/lnworker.py

@ -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()

7
electrum/network.py

@ -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'

4
electrum/tests/test_wallet.py

@ -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))

21
electrum/wallet.py

@ -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:
@ -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):
@ -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()
@ -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}

Loading…
Cancel
Save