lnbase: move channel commitment number increment to function

This commit is contained in:
Janus 2018-05-08 17:32:36 +02:00 committed by ThomasV
parent 2a594e9d0e
commit 2338d18ab8

View file

@ -941,8 +941,11 @@ class Peer(PrintError):
# } # }
if channel_reestablish_msg["my_current_per_commitment_point"] != bfh(chan.remote_state.commitment_points[-1]): if channel_reestablish_msg["my_current_per_commitment_point"] != bfh(chan.remote_state.commitment_points[-1]):
raise Exception("Remote PCP mismatch") raise Exception("Remote PCP mismatch")
n = chan.remote_state.ctn self.send_message(gen_msg("channel_reestablish",
self.send_message(gen_msg("channel_reestablish", channel_id=chan.channel_id, next_local_commitment_number=n+2, next_remote_revocation_number=n+1)) 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 return chan
@ -970,7 +973,7 @@ class Peer(PrintError):
finally: finally:
del self.local_funding_locked[channel_id] 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( 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')) 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)) self.send_message(gen_msg("funding_locked", channel_id=channel_id, next_per_commitment_point=per_commitment_point_second))
@ -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"])) 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): 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 channel_id = chan.channel_id
local_per_commitment_secret_seed = chan.local_state.per_commitment_secret_seed
try: try:
commitment_signed_msg = await self.commitment_signed[channel_id] commitment_signed_msg = await self.commitment_signed[channel_id]
finally: finally:
@ -999,13 +1012,12 @@ class Peer(PrintError):
assert amount_msat // 1000 == expected_received_sat assert amount_msat // 1000 == expected_received_sat
payment_hash = htlc["payment_hash"] 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) last_secret, next_point = derive_and_incr()
local_next_per_commitment_point = secret_to_pubkey(int.from_bytes(local_next_per_commitment_secret, 'big'))
remote_htlc_pubkey = derive_pubkey(chan.remote_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, local_next_per_commitment_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 = [ htlcs_in_local = [
( (
@ -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.local_state.amount_sat,
chan.remote_state.amount_sat - expected_received_sat, chan.remote_state.amount_sat - expected_received_sat,
htlcs_in_local) htlcs_in_local)
@ -1028,10 +1040,9 @@ class Peer(PrintError):
if htlc_sigs_len != 64: if htlc_sigs_len != 64:
raise Exception("unexpected number of htlc signatures: " + str(htlc_sigs_len)) 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, next_point, True, True, amount_msat, cltv_expiry, payment_hash, new_commitment, 0)
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)
pre_hash = bitcoin.Hash(bfh(htlc_tx.serialize_preimage(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): 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") raise Exception("failed verifying signature an HTLC tx spending from one of our commit tx'es HTLC outputs")
@ -1039,8 +1050,8 @@ class Peer(PrintError):
self.send_message(gen_msg("revoke_and_ack", self.send_message(gen_msg("revoke_and_ack",
channel_id=channel_id, 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), per_commitment_secret=last_secret,
next_per_commitment_point=local_next_per_commitment_point)) 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_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) their_remote_htlc_pubkey = derive_pubkey(chan.local_config.htlc_basepoint.pubkey, chan.remote_state.next_per_commitment_point)
@ -1096,20 +1107,16 @@ class Peer(PrintError):
# TODO check commitment_signed results # 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) last_secret, next_point = derive_and_incr()
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'))
print("SENDING SECOND REVOKE AND ACK") print("SENDING SECOND REVOKE AND ACK")
self.send_message(gen_msg("revoke_and_ack", self.send_message(gen_msg("revoke_and_ack",
channel_id=channel_id, channel_id=channel_id,
per_commitment_secret=local_last_per_commitment_secret, per_commitment_secret=last_secret,
next_per_commitment_point=local_next_per_commitment_point)) next_per_commitment_point=next_point))
return chan._replace( return chan._replace(
local_state=chan.local_state._replace( local_state=chan.local_state._replace(
ctn=chan.local_state.ctn + 2,
amount_sat=chan.local_state.amount_sat + expected_received_sat amount_sat=chan.local_state.amount_sat + expected_received_sat
), ),
remote_state=chan.remote_state._replace( remote_state=chan.remote_state._replace(