lnchan: add available_to_spend()

This commit is contained in:
Janus 2018-10-17 20:01:45 +02:00 committed by SomberNight
parent c7ccaabcc2
commit 0b2f539c81
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
3 changed files with 18 additions and 5 deletions

View file

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

View file

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

View file

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