Browse Source

refactor a few lnchannel methods

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
ThomasV 6 years ago
parent
commit
fa3eefa479
  1. 64
      electrum/lnchannel.py
  2. 14
      electrum/lnpeer.py

64
electrum/lnchannel.py

@ -410,16 +410,10 @@ class Channel(Logger):
self.lnworker.payment_completed(self, SENT, htlc)
received_this_batch = htlcsum(received)
sent_this_batch = htlcsum(sent)
last_secret, last_point = self.local_points(offset=-1)
next_secret, next_point = self.local_points(offset=1)
last_secret, last_point = self.get_secret_and_point(LOCAL, new_ctn - 1)
next_secret, next_point = self.get_secret_and_point(LOCAL, new_ctn + 1)
return RevokeAndAck(last_secret, next_point), (received_this_batch, sent_this_batch)
def local_points(self, *, offset=0):
ctn = self.config[LOCAL].ctn + offset
secret = get_per_commitment_secret_from_seed(self.config[LOCAL].per_commitment_secret_seed, RevocationStore.START_INDEX - ctn)
point = secret_to_pubkey(int.from_bytes(secret, 'big'))
return secret, point
def receive_revocation(self, revocation: RevokeAndAck):
self.logger.info("receive_revocation")
@ -518,27 +512,46 @@ class Channel(Logger):
fee_for_htlc = lambda htlc: htlc.amount_msat // 1000 - (weight * feerate // 1000)
return list(filter(lambda htlc: fee_for_htlc(htlc) >= conf.dust_limit_sat, htlcs))
def pending_commitment(self, subject):
def get_secret_and_point(self, subject, ctn):
assert type(subject) is HTLCOwner
this_point = self.config[REMOTE].next_per_commitment_point if subject == REMOTE else self.local_points(offset=1)[1]
ctn = self.config[subject].ctn + 1
feerate = self.get_feerate(subject, ctn)
return self.make_commitment(subject, this_point, ctn, feerate, True)
offset = ctn - self.get_current_ctn(subject)
if subject == REMOTE:
assert offset <= 1, offset
conf = self.config[REMOTE]
if offset == 1:
secret = None
point = conf.next_per_commitment_point
elif offset == 0:
secret = None
point = conf.current_per_commitment_point
else:
secret = conf.revocation_store.retrieve_secret(RevocationStore.START_INDEX - ctn)
point = secret_to_pubkey(int.from_bytes(secret, 'big'))
else:
secret = get_per_commitment_secret_from_seed(self.config[LOCAL].per_commitment_secret_seed, RevocationStore.START_INDEX - ctn)
point = secret_to_pubkey(int.from_bytes(secret, 'big'))
return secret, point
def get_secret_and_commitment(self, subject, ctn):
secret, point = self.get_secret_and_point(subject, ctn)
ctx = self.make_commitment(subject, point, ctn)
return secret, ctx
def get_commitment(self, subject, ctn):
secret, ctx = self.get_secret_and_commitment(subject, ctn)
return ctx
def pending_commitment(self, subject):
ctn = self.get_current_ctn(subject)
return self.get_commitment(subject, ctn + 1)
def current_commitment(self, subject):
assert type(subject) is HTLCOwner
this_point = self.config[REMOTE].current_per_commitment_point if subject == REMOTE else self.local_points(offset=0)[1]
ctn = self.config[subject].ctn
feerate = self.get_feerate(subject, ctn)
return self.make_commitment(subject, this_point, ctn, feerate, False)
ctn = self.get_current_ctn(subject)
return self.get_commitment(subject, ctn)
def create_sweeptxs(self, ctn):
from .lnsweep import create_sweeptxs_for_watchtower
their_conf = self.config[REMOTE]
feerate = self.get_feerate(REMOTE, ctn)
secret = their_conf.revocation_store.retrieve_secret(RevocationStore.START_INDEX - ctn)
point = secret_to_pubkey(int.from_bytes(secret, 'big'))
ctx = self.make_commitment(REMOTE, point, ctn, feerate, False)
secret, ctx = self.get_secret_and_commitment(REMOTE, ctn)
return create_sweeptxs_for_watchtower(self, ctx, secret, self.sweep_address)
def get_current_ctn(self, subject):
@ -643,10 +656,9 @@ class Channel(Logger):
def __str__(self):
return str(self.serialize())
def make_commitment(self, subject, this_point, ctn, feerate, pending) -> Transaction:
#if subject == REMOTE and not pending:
# ctn -= 1
def make_commitment(self, subject, this_point, ctn) -> Transaction:
assert type(subject) is HTLCOwner
feerate = self.get_feerate(subject, ctn)
other = REMOTE if LOCAL == subject else LOCAL
local_msat = self.balance(subject, ctx_owner=subject, ctn=ctn)
remote_msat = self.balance(other, ctx_owner=subject, ctn=ctn)

14
electrum/lnpeer.py

@ -705,7 +705,8 @@ class Peer(Logger):
revocation_store = chan.config[REMOTE].revocation_store
last_rev_index = current_remote_ctn - 1
last_rev_secret = revocation_store.retrieve_secret(RevocationStore.START_INDEX - last_rev_index)
last_secret, last_point = chan.local_points(offset=0)
current_local_ctn = chan.get_current_ctn(LOCAL)
last_secret, last_point = chan.get_secret_and_point(LOCAL, current_local_ctn)
self.send_message(
"channel_reestablish",
channel_id=chan_id,
@ -731,16 +732,17 @@ class Peer(Logger):
self.try_to_get_remote_to_force_close_with_their_latest(chan_id)
return
# compare local ctns
if chan.config[LOCAL].ctn != their_next_remote_ctn:
if chan.config[LOCAL].ctn == their_next_remote_ctn + 1:
our_local_ctn = chan.get_current_ctn(LOCAL)
if our_local_ctn != their_next_remote_ctn:
if our_local_ctn == their_next_remote_ctn + 1:
# A node:
# if next_remote_revocation_number is equal to the
# commitment number of the last revoke_and_ack
# the receiving node sent, AND the receiving node
# hasn't already received a closing_signed:
# MUST re-send the revoke_and_ack.
last_secret, last_point = chan.local_points(offset=-1)
next_secret, next_point = chan.local_points(offset=1)
last_secret, last_point = chan.get_secret_and_point(LOCAL, our_local_ctn - 1)
next_secret, next_point = chan.get_secret_and_point(LOCAL, our_local_ctn + 1)
self.send_message(
"revoke_and_ack",
channel_id=chan.channel_id,
@ -762,7 +764,7 @@ class Peer(Logger):
# FIXME ...what now?
self.try_to_get_remote_to_force_close_with_their_latest(chan_id)
return
if their_next_local_ctn == chan.config[LOCAL].ctn+1 == 1 and chan.short_channel_id:
if their_next_local_ctn == our_local_ctn + 1 == 1 and chan.short_channel_id:
self.send_funding_locked(chan)
# checks done
if chan.config[LOCAL].funding_locked_received and chan.short_channel_id:

Loading…
Cancel
Save