electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 7d2a6d83d5fce332674375586acd8a43641aaa60
parent b74d4261af686c997d6229dd2befbe72dc0e34af
Author: Janus <ysangkok@gmail.com>
Date:   Wed, 30 May 2018 13:47:46 +0200

ln: don't make invoice if peer can't possibly pay, append _sat to sat
parameters to avoid confusion

Diffstat:
Melectrum/commands.py | 11++++++-----
Mlib/lnworker.py | 16++++++++++------
2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/electrum/commands.py b/electrum/commands.py @@ -765,8 +765,8 @@ class Commands: # lightning network commands @command('wpn') - def open_channel(self, node_id, amount, push_msat=0, password=None): - self.wallet.lnworker.open_channel(node_id, satoshis(amount), push_msat, password) + def open_channel(self, node_id, amount, channel_push=0, password=None): + self.wallet.lnworker.open_channel(node_id, satoshis(amount), satoshis(channel_push), password) @command('wn') def reestablish_channel(self): @@ -777,8 +777,9 @@ class Commands: self.wallet.lnworker.pay(invoice) @command('wn') - def addinvoice(self, amount, message): - return self.wallet.lnworker.add_invoice(satoshis(amount), message) + def addinvoice(self, requested_amount, message): + # using requested_amount because it is documented in param_descriptions + return self.wallet.lnworker.add_invoice(satoshis(requested_amount), message) @command('wn') def listchannels(self): @@ -841,7 +842,7 @@ command_options = { 'timeout': (None, "Timeout in seconds"), 'force': (None, "Create new address beyond gap limit, if no more addresses are available."), 'pending': (None, "Show only pending requests."), - 'push_msat': (None, 'push millisatoshis'), + 'channel_push':(None, 'Push initial amount (in BTC)'), 'expired': (None, "Show only expired requests."), 'paid': (None, "Show only paid requests."), 'show_addresses': (None, "Show input and output addresses"), diff --git a/lib/lnworker.py b/lib/lnworker.py @@ -171,14 +171,14 @@ class LNWorker(PrintError): self.channel_state[chan.channel_id] = "OPEN" # not aiosafe because we call .result() which will propagate an exception - async def _open_channel_coroutine(self, node_id, amount, push_msat, password): + async def _open_channel_coroutine(self, node_id, amount_sat, push_sat, password): peer = self.peers[bfh(node_id)] - openingchannel = await peer.channel_establishment_flow(self.wallet, self.config, password, amount, push_msat, temp_channel_id=os.urandom(32)) + openingchannel = await peer.channel_establishment_flow(self.wallet, self.config, password, amount_sat, push_sat * 1000, temp_channel_id=os.urandom(32)) self.print_error("SAVING OPENING CHANNEL") self.save_channel(openingchannel) - def open_channel(self, node_id, local_amt, push_amt, pw): - coro = self._open_channel_coroutine(node_id, local_amt, push_amt, None if pw == "" else pw) + def open_channel(self, node_id, local_amt_sat, push_amt_sat, pw): + coro = self._open_channel_coroutine(node_id, local_amt_sat, push_amt_sat, None if pw == "" else pw) return asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop).result() def pay(self, invoice): @@ -196,10 +196,14 @@ class LNWorker(PrintError): openchannel = await peer.pay(self.wallet, openchannel, msat_amt, payment_hash, pubkey, addr.min_final_cltv_expiry) self.save_channel(openchannel) - def add_invoice(self, amount, message='one cup of coffee'): + def add_invoice(self, amount_sat, message='one cup of coffee'): + is_open = lambda chan: self.channel_state[chan] == "OPEN" + # TODO doesn't account for fees!!! + if not any(openchannel.remote_state.amount_msat >= amount_sat * 1000 for openchannel in self.channels if is_open(chan)): + return "Not making invoice, no channel has enough balance" payment_preimage = os.urandom(32) RHASH = sha256(payment_preimage) - pay_req = lnencode(LnAddr(RHASH, amount/Decimal(COIN), tags=[('d', message)]), self.privkey) + pay_req = lnencode(LnAddr(RHASH, amount_sat/Decimal(COIN), tags=[('d', message)]), self.privkey) decoded = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP) assert decoded.pubkey.serialize() == privkey_to_pubkey(self.privkey) self.invoices[bh2u(payment_preimage)] = pay_req