Browse Source

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

parameters to avoid confusion
regtest_lnd
Janus 7 years ago
committed by SomberNight
parent
commit
97dfce6c52
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/commands.py
  2. 16
      lib/lnworker.py

11
electrum/commands.py

@ -769,8 +769,8 @@ class Commands:
# lightning network commands # lightning network commands
@command('wpn') @command('wpn')
def open_channel(self, node_id, amount, push_msat=0, password=None): def open_channel(self, node_id, amount, channel_push=0, password=None):
self.wallet.lnworker.open_channel(node_id, satoshis(amount), push_msat, password) self.wallet.lnworker.open_channel(node_id, satoshis(amount), satoshis(channel_push), password)
@command('wn') @command('wn')
def reestablish_channel(self): def reestablish_channel(self):
@ -781,8 +781,9 @@ class Commands:
self.wallet.lnworker.pay(invoice) self.wallet.lnworker.pay(invoice)
@command('wn') @command('wn')
def addinvoice(self, amount, message): def addinvoice(self, requested_amount, message):
return self.wallet.lnworker.add_invoice(satoshis(amount), message) # using requested_amount because it is documented in param_descriptions
return self.wallet.lnworker.add_invoice(satoshis(requested_amount), message)
@command('wn') @command('wn')
def listchannels(self): def listchannels(self):
@ -845,7 +846,7 @@ command_options = {
'timeout': (None, "Timeout in seconds"), 'timeout': (None, "Timeout in seconds"),
'force': (None, "Create new address beyond gap limit, if no more addresses are available."), 'force': (None, "Create new address beyond gap limit, if no more addresses are available."),
'pending': (None, "Show only pending requests."), '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."), 'expired': (None, "Show only expired requests."),
'paid': (None, "Show only paid requests."), 'paid': (None, "Show only paid requests."),
'show_addresses': (None, "Show input and output addresses"), 'show_addresses': (None, "Show input and output addresses"),

16
lib/lnworker.py

@ -171,14 +171,14 @@ class LNWorker(PrintError):
self.channel_state[chan.channel_id] = "OPEN" self.channel_state[chan.channel_id] = "OPEN"
# not aiosafe because we call .result() which will propagate an exception # 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)] 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.print_error("SAVING OPENING CHANNEL")
self.save_channel(openingchannel) self.save_channel(openingchannel)
def open_channel(self, node_id, local_amt, push_amt, pw): def open_channel(self, node_id, local_amt_sat, push_amt_sat, pw):
coro = self._open_channel_coroutine(node_id, local_amt, push_amt, None if pw == "" else 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() return asyncio.run_coroutine_threadsafe(coro, self.network.asyncio_loop).result()
def pay(self, invoice): 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) openchannel = await peer.pay(self.wallet, openchannel, msat_amt, payment_hash, pubkey, addr.min_final_cltv_expiry)
self.save_channel(openchannel) 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) payment_preimage = os.urandom(32)
RHASH = sha256(payment_preimage) 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) decoded = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
assert decoded.pubkey.serialize() == privkey_to_pubkey(self.privkey) assert decoded.pubkey.serialize() == privkey_to_pubkey(self.privkey)
self.invoices[bh2u(payment_preimage)] = pay_req self.invoices[bh2u(payment_preimage)] = pay_req

Loading…
Cancel
Save