electrum

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

commit e59eb147c04572a313853e19b8c0e58416eb452f
parent ccf50dc98087f756aa6f94ad680a06a9260cc6a3
Author: SomberNight <somber.night@protonmail.com>
Date:   Wed, 10 Jun 2020 21:14:56 +0200

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

Diffstat:
Melectrum/lnchannel.py | 11++++++++++-
Melectrum/tests/test_lnchannel.py | 8++++----
2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/electrum/lnchannel.py b/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: diff --git a/electrum/tests/test_lnchannel.py b/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)