electrum

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

commit 78896897cbb0678e2983582714484b163902d998
parent c339eabd31e6e516f0c37355d1a9442e4c230e48
Author: ThomasV <thomasv@electrum.org>
Date:   Wed, 21 Nov 2018 15:32:36 +0100

lnchan: replace properties with functions

Diffstat:
Melectrum/lnbase.py | 4++--
Melectrum/lnchan.py | 42+++++++++++++++++-------------------------
Melectrum/tests/test_lnchan.py | 10+++++-----
3 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/electrum/lnbase.py b/electrum/lnbase.py @@ -512,7 +512,7 @@ class Peer(PrintError): chan.receive_new_commitment(remote_sig, []) # broadcast funding tx await self.network.broadcast_transaction(funding_tx) - chan.remote_commitment_to_be_revoked = chan.pending_remote_commitment + chan.remote_commitment_to_be_revoked = chan.pending_remote_commitment() chan.config[REMOTE] = chan.config[REMOTE]._replace(ctn=0) chan.config[LOCAL] = chan.config[LOCAL]._replace(ctn=0, current_commitment_signature=remote_sig) chan.set_state('OPENING') @@ -598,7 +598,7 @@ class Peer(PrintError): signature=sig_64, ) chan.set_state('OPENING') - chan.remote_commitment_to_be_revoked = chan.pending_remote_commitment + chan.remote_commitment_to_be_revoked = chan.pending_remote_commitment() chan.config[REMOTE] = chan.config[REMOTE]._replace(ctn=0) chan.config[LOCAL] = chan.config[LOCAL]._replace(ctn=0, current_commitment_signature=remote_sig) self.lnworker.save_channel(chan) diff --git a/electrum/lnchan.py b/electrum/lnchan.py @@ -189,8 +189,8 @@ class Channel(PrintError): self.pending_fee = None - self.local_commitment = self.pending_local_commitment - self.remote_commitment = self.pending_remote_commitment + self.local_commitment = self.pending_commitment(LOCAL) + self.remote_commitment = self.pending_commitment(REMOTE) self._is_funding_txo_spent = None # "don't know" self._state = None @@ -296,7 +296,7 @@ class Channel(PrintError): old_logs = dict(self.lock_in_htlc_changes(LOCAL)) - pending_remote_commitment = self.pending_remote_commitment + pending_remote_commitment = self.pending_commitment(REMOTE) sig_64 = sign_and_get_sig_string(pending_remote_commitment, self.config[LOCAL], self.config[REMOTE]) their_remote_htlc_privkey_number = derive_privkey( @@ -357,7 +357,7 @@ class Channel(PrintError): assert len(htlc_sigs) == 0 or type(htlc_sigs[0]) is bytes - pending_local_commitment = self.pending_local_commitment + pending_local_commitment = self.pending_commitment(LOCAL) preimage_hex = pending_local_commitment.serialize_preimage(0) pre_hash = sha256d(bfh(preimage_hex)) if not ecc.verify_signature(self.config[REMOTE].multisig_key.pubkey, sig, pre_hash): @@ -386,8 +386,8 @@ class Channel(PrintError): self.process_new_offchain_ctx(pending_local_commitment, ours=True) def verify_htlc(self, htlc, htlc_sigs, we_receive): - _, this_point, _ = self.points - _script, htlc_tx = make_htlc_tx_with_open_channel(self, this_point, True, we_receive, self.pending_local_commitment, htlc) + _, this_point, _ = self.points() + _script, htlc_tx = make_htlc_tx_with_open_channel(self, this_point, True, we_receive, self.pending_commitment(LOCAL), htlc) pre_hash = sha256d(bfh(htlc_tx.serialize_preimage(0))) remote_htlc_pubkey = derive_pubkey(self.config[REMOTE].htlc_basepoint.pubkey, this_point) for idx, sig in enumerate(htlc_sigs): @@ -399,7 +399,7 @@ class Channel(PrintError): def revoke_current_commitment(self): self.print_error("revoke_current_commitment") - last_secret, this_point, next_point = self.points + last_secret, this_point, next_point = self.points() new_feerate = self.constraints.feerate @@ -420,11 +420,10 @@ class Channel(PrintError): feerate=new_feerate ) - self.local_commitment = self.pending_local_commitment + self.local_commitment = self.pending_commitment(LOCAL) return RevokeAndAck(last_secret, next_point), "current htlcs" - @property def points(self): last_small_num = self.config[LOCAL].ctn this_small_num = last_small_num + 1 @@ -486,7 +485,7 @@ class Channel(PrintError): # FIXME not sure this is correct... but it seems to work # if there are update_add_htlc msgs between commitment_signed and rev_ack, # this might break - prev_remote_commitment = self.pending_remote_commitment + prev_remote_commitment = self.pending_commitment(REMOTE) self.config[REMOTE].revocation_store.add_next_entry(revocation.per_commitment_secret) self.process_new_revocation_secret(revocation.per_commitment_secret) @@ -531,8 +530,8 @@ class Channel(PrintError): if self.constraints.is_initiator: self.pending_fee[FUNDEE_ACKED] = True - self.local_commitment = self.pending_local_commitment - self.remote_commitment = self.pending_remote_commitment + self.local_commitment = self.pending_commitment(LOCAL) + self.remote_commitment = self.pending_commitment(REMOTE) self.remote_commitment_to_be_revoked = prev_remote_commitment return received_this_batch, sent_this_batch @@ -601,11 +600,6 @@ class Channel(PrintError): fee_for_htlc = lambda htlc: htlc.amount_msat // 1000 - (weight * feerate // 1000) return filter(lambda htlc: fee_for_htlc(htlc) >= conf.dust_limit_sat, htlcs) - @property - def pending_remote_commitment(self): - this_point = self.config[REMOTE].next_per_commitment_point - return self.make_commitment(REMOTE, this_point) - def pending_feerate(self, subject): candidate = self.constraints.feerate if self.pending_fee is not None: @@ -614,10 +608,9 @@ class Channel(PrintError): candidate = x return candidate - @property - def pending_local_commitment(self): - _, this_point, _ = self.points - return self.make_commitment(LOCAL, this_point) + def pending_commitment(self, subject): + this_point = self.config[REMOTE].next_per_commitment_point if subject == REMOTE else self.points()[1] + return self.make_commitment(subject, this_point) def total_msat(self, sub): return sum(self.settled[sub]) @@ -677,9 +670,8 @@ class Channel(PrintError): def current_height(self): return {LOCAL: self.config[LOCAL].ctn, REMOTE: self.config[REMOTE].ctn} - @property def pending_local_fee(self): - return self.constraints.capacity - sum(x[2] for x in self.pending_local_commitment.outputs()) + return self.constraints.capacity - sum(x[2] for x in self.pending_commitment(LOCAL).outputs()) def update_fee(self, feerate, initiator): if self.constraints.is_initiator != initiator: @@ -821,7 +813,7 @@ class Channel(PrintError): fee_sat: Optional[int]=None) -> Tuple[bytes, int, str]: """ cooperative close """ if fee_sat is None: - fee_sat = self.pending_local_fee + fee_sat = self.pending_local_fee() _, outputs = make_commitment_outputs({ LOCAL: fee_sat * 1000 if self.constraints.is_initiator else 0, @@ -849,7 +841,7 @@ class Channel(PrintError): # but in this case, we want the current one. So substract one ctn number old_local_state = self.config[LOCAL] self.config[LOCAL]=self.config[LOCAL]._replace(ctn=self.config[LOCAL].ctn - 1) - tx = self.pending_local_commitment + tx = self.pending_commitment(LOCAL) self.config[LOCAL] = old_local_state tx.sign({bh2u(self.config[LOCAL].multisig_key.pubkey): (self.config[LOCAL].multisig_key.privkey, True)}) remote_sig = self.config[LOCAL].current_commitment_signature diff --git a/electrum/tests/test_lnchan.py b/electrum/tests/test_lnchan.py @@ -193,7 +193,7 @@ class TestChannel(unittest.TestCase): bob_idx = self.bob_channel.add_htlc(self.htlc_dict) alice_idx = self.alice_channel.receive_htlc(self.htlc_dict) self.alice_channel.receive_new_commitment(*self.bob_channel.sign_next_commitment()) - self.assertEqual(len(self.alice_channel.pending_remote_commitment.outputs()), 3) + self.assertEqual(len(self.alice_channel.pending_commitment(REMOTE).outputs()), 3) def test_SimpleAddSettleWorkflow(self): alice_channel, bob_channel = self.alice_channel, self.bob_channel @@ -577,13 +577,13 @@ class TestChanReserve(unittest.TestCase): bob_idx = self.bob_channel.receive_htlc(htlc_dict) force_state_transition(self.alice_channel, self.bob_channel) self.check_bals(one_bitcoin_in_msat*3\ - - self.alice_channel.pending_local_fee, + - self.alice_channel.pending_local_fee(), one_bitcoin_in_msat*5) self.bob_channel.settle_htlc(paymentPreimage, bob_idx) self.alice_channel.receive_htlc_settle(paymentPreimage, alice_idx) force_state_transition(self.alice_channel, self.bob_channel) self.check_bals(one_bitcoin_in_msat*3\ - - self.alice_channel.pending_local_fee, + - self.alice_channel.pending_local_fee(), one_bitcoin_in_msat*7) # And now let Bob add an HTLC of 1 BTC. This will take Bob's balance # all the way down to his channel reserve, but since he is not paying @@ -593,7 +593,7 @@ class TestChanReserve(unittest.TestCase): self.alice_channel.receive_htlc(htlc_dict) force_state_transition(self.alice_channel, self.bob_channel) self.check_bals(one_bitcoin_in_msat*3\ - - self.alice_channel.pending_local_fee, + - self.alice_channel.pending_local_fee(), one_bitcoin_in_msat*6) def check_bals(self, amt1, amt2): @@ -624,7 +624,7 @@ class TestDust(unittest.TestCase): self.assertEqual(len(alice_channel.local_commitment.outputs()), 3) self.assertEqual(len(bob_channel.local_commitment.outputs()), 2) default_fee = calc_static_fee(0) - self.assertEqual(bob_channel.pending_local_fee, default_fee + htlcAmt) + self.assertEqual(bob_channel.pending_local_fee(), default_fee + htlcAmt) bob_channel.settle_htlc(paymentPreimage, bobHtlcIndex) alice_channel.receive_htlc_settle(paymentPreimage, aliceHtlcIndex) force_state_transition(bob_channel, alice_channel)