|
|
|
@ -10,6 +10,11 @@ class HTLCManager: |
|
|
|
def __init__(self, local_ctn=0, remote_ctn=0, log=None): |
|
|
|
def __init__(self, local_ctn=0, remote_ctn=0, log=None): |
|
|
|
# self.ctn[sub] is the ctn for the oldest unrevoked ctx of sub |
|
|
|
# self.ctn[sub] is the ctn for the oldest unrevoked ctx of sub |
|
|
|
self.ctn = {LOCAL:local_ctn, REMOTE: remote_ctn} |
|
|
|
self.ctn = {LOCAL:local_ctn, REMOTE: remote_ctn} |
|
|
|
|
|
|
|
# self.ctn_latest[sub] is the ctn for the latest (newest that has a valid sig) ctx of sub |
|
|
|
|
|
|
|
self.ctn_latest = {LOCAL:local_ctn, REMOTE: remote_ctn} # FIXME does this need to be persisted? |
|
|
|
|
|
|
|
# after sending commitment_signed but before receiving revoke_and_ack, |
|
|
|
|
|
|
|
# self.ctn_latest[REMOTE] == self.ctn[REMOTE] + 1 |
|
|
|
|
|
|
|
# otherwise they are equal |
|
|
|
self.expect_sig = {SENT: False, RECEIVED: False} |
|
|
|
self.expect_sig = {SENT: False, RECEIVED: False} |
|
|
|
if log is None: |
|
|
|
if log is None: |
|
|
|
initial = {'adds': {}, 'locked_in': {}, 'settles': {}, 'fails': {}} |
|
|
|
initial = {'adds': {}, 'locked_in': {}, 'settles': {}, 'fails': {}} |
|
|
|
@ -35,33 +40,39 @@ class HTLCManager: |
|
|
|
x[sub]['adds'] = d |
|
|
|
x[sub]['adds'] = d |
|
|
|
return x |
|
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def channel_open_finished(self): |
|
|
|
|
|
|
|
self.ctn = {LOCAL: 0, REMOTE: 0} |
|
|
|
|
|
|
|
self.ctn_latest = {LOCAL: 0, REMOTE: 0} |
|
|
|
|
|
|
|
|
|
|
|
def send_htlc(self, htlc: UpdateAddHtlc) -> UpdateAddHtlc: |
|
|
|
def send_htlc(self, htlc: UpdateAddHtlc) -> UpdateAddHtlc: |
|
|
|
htlc_id = htlc.htlc_id |
|
|
|
htlc_id = htlc.htlc_id |
|
|
|
adds = self.log[LOCAL]['adds'] |
|
|
|
adds = self.log[LOCAL]['adds'] |
|
|
|
assert type(adds) is not str |
|
|
|
assert type(adds) is not str |
|
|
|
adds[htlc_id] = htlc |
|
|
|
adds[htlc_id] = htlc |
|
|
|
self.log[LOCAL]['locked_in'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE]+1} |
|
|
|
self.log[LOCAL]['locked_in'][htlc_id] = {LOCAL: None, REMOTE: self.ctn_latest[REMOTE]+1} |
|
|
|
self.expect_sig[SENT] = True |
|
|
|
self.expect_sig[SENT] = True |
|
|
|
return htlc |
|
|
|
return htlc |
|
|
|
|
|
|
|
|
|
|
|
def recv_htlc(self, htlc: UpdateAddHtlc) -> None: |
|
|
|
def recv_htlc(self, htlc: UpdateAddHtlc) -> None: |
|
|
|
htlc_id = htlc.htlc_id |
|
|
|
htlc_id = htlc.htlc_id |
|
|
|
self.log[REMOTE]['adds'][htlc_id] = htlc |
|
|
|
self.log[REMOTE]['adds'][htlc_id] = htlc |
|
|
|
l = self.log[REMOTE]['locked_in'][htlc_id] = {LOCAL: self.ctn[LOCAL]+1, REMOTE: None} |
|
|
|
l = self.log[REMOTE]['locked_in'][htlc_id] = {LOCAL: self.ctn_latest[LOCAL]+1, REMOTE: None} |
|
|
|
self.expect_sig[RECEIVED] = True |
|
|
|
self.expect_sig[RECEIVED] = True |
|
|
|
|
|
|
|
|
|
|
|
def send_ctx(self) -> None: |
|
|
|
def send_ctx(self) -> None: |
|
|
|
next_ctn = self.ctn[REMOTE] + 1 |
|
|
|
assert self.ctn_latest[REMOTE] == self.ctn[REMOTE], (self.ctn_latest[REMOTE], self.ctn[REMOTE]) |
|
|
|
|
|
|
|
self.ctn_latest[REMOTE] = self.ctn[REMOTE] + 1 |
|
|
|
for locked_in in self.log[REMOTE]['locked_in'].values(): |
|
|
|
for locked_in in self.log[REMOTE]['locked_in'].values(): |
|
|
|
if locked_in[REMOTE] is None: |
|
|
|
if locked_in[REMOTE] is None: |
|
|
|
locked_in[REMOTE] = next_ctn |
|
|
|
locked_in[REMOTE] = self.ctn_latest[REMOTE] |
|
|
|
self.expect_sig[SENT] = False |
|
|
|
self.expect_sig[SENT] = False |
|
|
|
|
|
|
|
|
|
|
|
def recv_ctx(self) -> None: |
|
|
|
def recv_ctx(self) -> None: |
|
|
|
next_ctn = self.ctn[LOCAL] + 1 |
|
|
|
assert self.ctn_latest[LOCAL] == self.ctn[LOCAL], (self.ctn_latest[LOCAL], self.ctn[LOCAL]) |
|
|
|
|
|
|
|
self.ctn_latest[LOCAL] = self.ctn[LOCAL] + 1 |
|
|
|
for locked_in in self.log[LOCAL]['locked_in'].values(): |
|
|
|
for locked_in in self.log[LOCAL]['locked_in'].values(): |
|
|
|
if locked_in[LOCAL] is None: |
|
|
|
if locked_in[LOCAL] is None: |
|
|
|
locked_in[LOCAL] = next_ctn |
|
|
|
locked_in[LOCAL] = self.ctn_latest[LOCAL] |
|
|
|
self.expect_sig[RECEIVED] = False |
|
|
|
self.expect_sig[RECEIVED] = False |
|
|
|
|
|
|
|
|
|
|
|
def send_rev(self) -> None: |
|
|
|
def send_rev(self) -> None: |
|
|
|
@ -69,18 +80,18 @@ class HTLCManager: |
|
|
|
for log_action in ('settles', 'fails'): |
|
|
|
for log_action in ('settles', 'fails'): |
|
|
|
for htlc_id, ctns in self.log[LOCAL][log_action].items(): |
|
|
|
for htlc_id, ctns in self.log[LOCAL][log_action].items(): |
|
|
|
if ctns[REMOTE] is None: |
|
|
|
if ctns[REMOTE] is None: |
|
|
|
ctns[REMOTE] = self.ctn[REMOTE] + 1 |
|
|
|
ctns[REMOTE] = self.ctn_latest[REMOTE] + 1 |
|
|
|
|
|
|
|
|
|
|
|
def recv_rev(self) -> None: |
|
|
|
def recv_rev(self) -> None: |
|
|
|
self.ctn[REMOTE] += 1 |
|
|
|
self.ctn[REMOTE] += 1 |
|
|
|
for htlc_id, ctns in self.log[LOCAL]['locked_in'].items(): |
|
|
|
for htlc_id, ctns in self.log[LOCAL]['locked_in'].items(): |
|
|
|
if ctns[LOCAL] is None: |
|
|
|
if ctns[LOCAL] is None: |
|
|
|
assert ctns[REMOTE] == self.ctn[REMOTE] |
|
|
|
#assert ctns[REMOTE] == self.ctn[REMOTE] # FIXME I don't think this assert is correct |
|
|
|
ctns[LOCAL] = self.ctn[LOCAL] + 1 |
|
|
|
ctns[LOCAL] = self.ctn_latest[LOCAL] + 1 |
|
|
|
for log_action in ('settles', 'fails'): |
|
|
|
for log_action in ('settles', 'fails'): |
|
|
|
for htlc_id, ctns in self.log[REMOTE][log_action].items(): |
|
|
|
for htlc_id, ctns in self.log[REMOTE][log_action].items(): |
|
|
|
if ctns[LOCAL] is None: |
|
|
|
if ctns[LOCAL] is None: |
|
|
|
ctns[LOCAL] = self.ctn[LOCAL] + 1 |
|
|
|
ctns[LOCAL] = self.ctn_latest[LOCAL] + 1 |
|
|
|
|
|
|
|
|
|
|
|
def htlcs_by_direction(self, subject: HTLCOwner, direction: Direction, |
|
|
|
def htlcs_by_direction(self, subject: HTLCOwner, direction: Direction, |
|
|
|
ctn: int = None) -> Sequence[UpdateAddHtlc]: |
|
|
|
ctn: int = None) -> Sequence[UpdateAddHtlc]: |
|
|
|
@ -136,10 +147,10 @@ class HTLCManager: |
|
|
|
return self.htlcs(subject, ctn) |
|
|
|
return self.htlcs(subject, ctn) |
|
|
|
|
|
|
|
|
|
|
|
def send_settle(self, htlc_id: int) -> None: |
|
|
|
def send_settle(self, htlc_id: int) -> None: |
|
|
|
self.log[REMOTE]['settles'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE] + 1} |
|
|
|
self.log[REMOTE]['settles'][htlc_id] = {LOCAL: None, REMOTE: self.ctn_latest[REMOTE] + 1} |
|
|
|
|
|
|
|
|
|
|
|
def recv_settle(self, htlc_id: int) -> None: |
|
|
|
def recv_settle(self, htlc_id: int) -> None: |
|
|
|
self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.ctn[LOCAL] + 1, REMOTE: None} |
|
|
|
self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.ctn_latest[LOCAL] + 1, REMOTE: None} |
|
|
|
|
|
|
|
|
|
|
|
def all_settled_htlcs_ever_by_direction(self, subject: HTLCOwner, direction: Direction, |
|
|
|
def all_settled_htlcs_ever_by_direction(self, subject: HTLCOwner, direction: Direction, |
|
|
|
ctn: int = None) -> Sequence[UpdateAddHtlc]: |
|
|
|
ctn: int = None) -> Sequence[UpdateAddHtlc]: |
|
|
|
@ -181,7 +192,7 @@ class HTLCManager: |
|
|
|
if ctns[LOCAL] == ctn] |
|
|
|
if ctns[LOCAL] == ctn] |
|
|
|
|
|
|
|
|
|
|
|
def send_fail(self, htlc_id: int) -> None: |
|
|
|
def send_fail(self, htlc_id: int) -> None: |
|
|
|
self.log[REMOTE]['fails'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE] + 1} |
|
|
|
self.log[REMOTE]['fails'][htlc_id] = {LOCAL: None, REMOTE: self.ctn_latest[REMOTE] + 1} |
|
|
|
|
|
|
|
|
|
|
|
def recv_fail(self, htlc_id: int) -> None: |
|
|
|
def recv_fail(self, htlc_id: int) -> None: |
|
|
|
self.log[LOCAL]['fails'][htlc_id] = {LOCAL: self.ctn[LOCAL] + 1, REMOTE: None} |
|
|
|
self.log[LOCAL]['fails'][htlc_id] = {LOCAL: self.ctn_latest[LOCAL] + 1, REMOTE: None} |
|
|
|
|