Browse Source

lnchan: add available_to_spend()

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
Janus 6 years ago
committed by ThomasV
parent
commit
ac68c8f531
  1. 13
      electrum/gui/qt/channels_list.py
  2. 9
      electrum/lnchan.py
  3. 1
      electrum/tests/test_lnchan.py

13
electrum/gui/qt/channels_list.py

@ -23,10 +23,19 @@ class ChannelsList(MyTreeWidget):
self.status = QLabel('')
def format_fields(self, chan):
labels = {}
for subject in (REMOTE, LOCAL):
available = chan.available_to_spend(subject)//1000
label = self.parent.format_amount(available)
bal_other = chan.balance(-subject)//1000
available_other = chan.available_to_spend(-subject)//1000
if bal_other != available_other:
label += ' (+' + self.parent.format_amount(bal_other - available_other) + ')'
labels[subject] = label
return [
bh2u(chan.node_id),
self.parent.format_amount(chan.balance(LOCAL)//1000),
self.parent.format_amount(chan.balance(REMOTE)//1000),
labels[LOCAL],
labels[REMOTE],
chan.get_state()
]

9
electrum/lnchan.py

@ -155,11 +155,9 @@ class Channel(PrintError):
return self._state
def check_can_pay(self, amount_msat):
# FIXME what about channel_reserve_satoshis? will the remote fail the channel if we go below? test.
# FIXME what about tx fees
if self.get_state() != 'OPEN':
raise PaymentFailure('Channel not open')
if self.balance(LOCAL) < amount_msat:
if self.available_to_spend(LOCAL) < amount_msat:
raise PaymentFailure('Not enough local balance')
if len(self.htlcs(LOCAL, only_pending=True)) + 1 > self.config[REMOTE].max_accepted_htlcs:
raise PaymentFailure('Too many HTLCs already in channel')
@ -461,6 +459,11 @@ class Channel(PrintError):
assert initial == self.config[subject].amount_msat
return initial
def available_to_spend(self, subject):
# FIXME what about channel_reserve_satoshis? will the remote fail the channel if we go below? test.
# FIXME what about tx fees
return self.balance(subject) - htlcsum(self.htlcs(subject, only_pending=True))
def amounts(self):
remote_settled= htlcsum(self.htlcs(REMOTE, False))
local_settled= htlcsum(self.htlcs(LOCAL, False))

1
electrum/tests/test_lnchan.py

@ -339,6 +339,7 @@ class TestDust(unittest.TestCase):
aliceHtlcIndex = alice_channel.add_htlc(htlc)
bobHtlcIndex = bob_channel.receive_htlc(htlc)
force_state_transition(alice_channel, bob_channel)
self.assertEqual(alice_channel.available_to_spend(LOCAL), alice_channel.balance(LOCAL) - htlc['amount_msat'])
self.assertEqual(len(alice_channel.local_commitment.outputs()), 3)
self.assertEqual(len(bob_channel.local_commitment.outputs()), 2)
default_fee = calc_static_fee(0)

Loading…
Cancel
Save