Browse Source

lnchannel.available_to_spend: LOCAL now respects "fee spike buffer"

bip39-recovery
SomberNight 5 years ago
parent
commit
e59eb147c0
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/lnchannel.py
  2. 8
      electrum/tests/test_lnchannel.py

11
electrum/lnchannel.py

@ -1073,7 +1073,16 @@ class Channel(AbstractChannel):
htlc_fee_msat = fee_for_htlc_output(feerate=feerate)
htlc_trim_func = received_htlc_trim_threshold_sat if ctx_owner == receiver else offered_htlc_trim_threshold_sat
htlc_trim_threshold_msat = htlc_trim_func(dust_limit_sat=self.config[ctx_owner].dust_limit_sat, feerate=feerate) * 1000
max_send_msat = sender_balance_msat - sender_reserve_msat - ctx_fees_msat[sender]
if sender == initiator == LOCAL: # see https://github.com/lightningnetwork/lightning-rfc/pull/740
fee_spike_buffer = calc_fees_for_commitment_tx(
num_htlcs=num_htlcs_in_ctx + int(not is_htlc_dust) + 1,
feerate=2 * feerate,
is_local_initiator=self.constraints.is_initiator,
round_to_sat=False,
)[sender]
max_send_msat = sender_balance_msat - sender_reserve_msat - fee_spike_buffer
else:
max_send_msat = sender_balance_msat - sender_reserve_msat - ctx_fees_msat[sender]
if is_htlc_dust:
return min(max_send_msat, htlc_trim_threshold_msat - 1)
else:

8
electrum/tests/test_lnchannel.py

@ -622,7 +622,7 @@ class TestChannel(ElectrumTestCase):
class TestAvailableToSpend(ElectrumTestCase):
def test_DesyncHTLCs(self):
alice_channel, bob_channel = create_test_channels()
self.assertEqual(499994624000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(499986152000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(500000000000, bob_channel.available_to_spend(LOCAL))
paymentPreimage = b"\x01" * 32
@ -636,13 +636,13 @@ class TestAvailableToSpend(ElectrumTestCase):
alice_idx = alice_channel.add_htlc(htlc_dict).htlc_id
bob_idx = bob_channel.receive_htlc(htlc_dict).htlc_id
self.assertEqual(89993592000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(89984088000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(500000000000, bob_channel.available_to_spend(LOCAL))
force_state_transition(alice_channel, bob_channel)
bob_channel.fail_htlc(bob_idx)
alice_channel.receive_fail_htlc(alice_idx, error_bytes=None)
self.assertEqual(89993592000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(89984088000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(500000000000, bob_channel.available_to_spend(LOCAL))
# Alice now has gotten all her original balance (5 BTC) back, however,
# adding a new HTLC at this point SHOULD fail, since if she adds the
@ -662,7 +662,7 @@ class TestAvailableToSpend(ElectrumTestCase):
# Now do a state transition, which will ACK the FailHTLC, making Alice
# able to add the new HTLC.
force_state_transition(alice_channel, bob_channel)
self.assertEqual(499994624000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(499986152000, alice_channel.available_to_spend(LOCAL))
self.assertEqual(500000000000, bob_channel.available_to_spend(LOCAL))
alice_channel.add_htlc(htlc_dict)

Loading…
Cancel
Save