tlnbase: move channel commitment number increment to function - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 2338d18ab8423b165f26f0d45f9ae961007cec1b DIR parent 2a594e9d0e6628fb5e4b4776e217698daca6d009 HTML Author: Janus <ysangkok@gmail.com> Date: Tue, 8 May 2018 17:32:36 +0200 lnbase: move channel commitment number increment to function Diffstat: M lib/lnbase.py | 51 ++++++++++++++++++------------- 1 file changed, 29 insertions(+), 22 deletions(-) --- DIR diff --git a/lib/lnbase.py b/lib/lnbase.py t@@ -941,8 +941,11 @@ class Peer(PrintError): # } if channel_reestablish_msg["my_current_per_commitment_point"] != bfh(chan.remote_state.commitment_points[-1]): raise Exception("Remote PCP mismatch") - n = chan.remote_state.ctn - self.send_message(gen_msg("channel_reestablish", channel_id=chan.channel_id, next_local_commitment_number=n+2, next_remote_revocation_number=n+1)) + self.send_message(gen_msg("channel_reestablish", + channel_id=chan.channel_id, + next_local_commitment_number=chan.local_state.ctn+1, + next_remote_revocation_number=chan.remote_state.ctn+1 + )) return chan t@@ -970,7 +973,7 @@ class Peer(PrintError): finally: del self.local_funding_locked[channel_id] - per_commitment_secret_index = 2**48 - (chan.local_state.ctn + 1) - 1 + per_commitment_secret_index = 2**48 - 2 per_commitment_point_second = secret_to_pubkey(int.from_bytes( get_per_commitment_secret_from_seed(chan.local_state.per_commitment_secret_seed, per_commitment_secret_index), 'big')) self.send_message(gen_msg("funding_locked", channel_id=channel_id, next_per_commitment_point=per_commitment_point_second)) t@@ -984,8 +987,18 @@ class Peer(PrintError): return chan._replace(remote_state=chan.remote_state._replace(next_per_commitment_point=remote_funding_locked_msg["next_per_commitment_point"])) async def receive_commitment_revoke_ack(self, chan, expected_received_sat, payment_preimage): + def derive_and_incr(): + nonlocal chan + last_secret = get_per_commitment_secret_from_seed(chan.local_state.per_commitment_secret_seed, 2**48-chan.local_state.ctn-1) + next_secret = get_per_commitment_secret_from_seed(chan.local_state.per_commitment_secret_seed, 2**48-chan.local_state.ctn-2) + next_point = secret_to_pubkey(int.from_bytes(next_secret, 'big')) + chan = chan._replace( + local_state=chan.local_state._replace( + ctn=chan.local_state.ctn + 1 + ) + ) + return last_secret, next_point channel_id = chan.channel_id - local_per_commitment_secret_seed = chan.local_state.per_commitment_secret_seed try: commitment_signed_msg = await self.commitment_signed[channel_id] finally: t@@ -999,13 +1012,12 @@ class Peer(PrintError): assert amount_msat // 1000 == expected_received_sat payment_hash = htlc["payment_hash"] - local_next_per_commitment_secret = get_per_commitment_secret_from_seed(local_per_commitment_secret_seed, 2**48-chan.local_state.ctn-2) - local_next_per_commitment_point = secret_to_pubkey(int.from_bytes(local_next_per_commitment_secret, 'big')) + last_secret, next_point = derive_and_incr() - remote_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, local_next_per_commitment_point) - local_htlc_pubkey = derive_pubkey(chan.local_config.htlc_basepoint.pubkey, local_next_per_commitment_point) + remote_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, next_point) + local_htlc_pubkey = derive_pubkey(chan.local_config.htlc_basepoint.pubkey, next_point) - remote_revocation_pubkey = derive_blinded_pubkey(chan.remote_config.revocation_basepoint.pubkey, local_next_per_commitment_point) + remote_revocation_pubkey = derive_blinded_pubkey(chan.remote_config.revocation_basepoint.pubkey, next_point) htlcs_in_local = [ ( t@@ -1014,7 +1026,7 @@ class Peer(PrintError): ) ] - new_commitment = make_commitment_using_open_channel(chan, chan.local_state.ctn+1, True, local_next_per_commitment_point, + new_commitment = make_commitment_using_open_channel(chan, chan.local_state.ctn, True, next_point, chan.local_state.amount_sat, chan.remote_state.amount_sat - expected_received_sat, htlcs_in_local) t@@ -1028,10 +1040,9 @@ class Peer(PrintError): if htlc_sigs_len != 64: raise Exception("unexpected number of htlc signatures: " + str(htlc_sigs_len)) - local_last_per_commitment_point = local_next_per_commitment_point - htlc_tx = make_htlc_tx_with_open_channel(chan, local_last_per_commitment_point, True, True, amount_msat, cltv_expiry, payment_hash, new_commitment, 0) + htlc_tx = make_htlc_tx_with_open_channel(chan, next_point, True, True, amount_msat, cltv_expiry, payment_hash, new_commitment, 0) pre_hash = bitcoin.Hash(bfh(htlc_tx.serialize_preimage(0))) - remote_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, local_last_per_commitment_point) + remote_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, next_point) if not bitcoin.verify_signature(remote_htlc_pubkey, commitment_signed_msg["htlc_signature"], pre_hash): raise Exception("failed verifying signature an HTLC tx spending from one of our commit tx'es HTLC outputs") t@@ -1039,8 +1050,8 @@ class Peer(PrintError): self.send_message(gen_msg("revoke_and_ack", channel_id=channel_id, - per_commitment_secret=get_per_commitment_secret_from_seed(local_per_commitment_secret_seed, 2**48 - (chan.local_state.ctn//2) - 1), - next_per_commitment_point=local_next_per_commitment_point)) + per_commitment_secret=last_secret, + next_per_commitment_point=next_point)) their_local_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, chan.remote_state.next_per_commitment_point) their_remote_htlc_pubkey = derive_pubkey(chan.local_config.htlc_basepoint.pubkey, chan.remote_state.next_per_commitment_point) t@@ -1096,20 +1107,16 @@ class Peer(PrintError): # TODO check commitment_signed results - local_last_per_commitment_secret = get_per_commitment_secret_from_seed(local_per_commitment_secret_seed, 2**48 - chan.local_state.ctn - 2) - - local_next_per_commitment_secret = get_per_commitment_secret_from_seed(local_per_commitment_secret_seed, 2**48 - chan.local_state.ctn - 4) - local_next_per_commitment_point = secret_to_pubkey(int.from_bytes(local_next_per_commitment_secret, 'big')) + last_secret, next_point = derive_and_incr() print("SENDING SECOND REVOKE AND ACK") self.send_message(gen_msg("revoke_and_ack", channel_id=channel_id, - per_commitment_secret=local_last_per_commitment_secret, - next_per_commitment_point=local_next_per_commitment_point)) + per_commitment_secret=last_secret, + next_per_commitment_point=next_point)) return chan._replace( local_state=chan.local_state._replace( - ctn=chan.local_state.ctn + 2, amount_sat=chan.local_state.amount_sat + expected_received_sat ), remote_state=chan.remote_state._replace(