lnhtlc: handle settles like adds (asymmetrical across ctns)
This commit is contained in:
@@ -15,7 +15,7 @@ class HTLCManager:
|
||||
log[sub]['adds'] = {int(x): UpdateAddHtlc(*y) for x, y in log[sub]['adds'].items()}
|
||||
coerceHtlcOwner2IntMap = lambda x: {HTLCOwner(int(y)): z for y, z in x.items()}
|
||||
log[sub]['locked_in'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['locked_in'].items()}
|
||||
log[sub]['settles'] = {int(x): y for x, y in log[sub]['settles'].items()}
|
||||
log[sub]['settles'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['settles'].items()}
|
||||
log[sub]['fails'] = {int(x): y for x, y in log[sub]['fails'].items()}
|
||||
self.log = log
|
||||
|
||||
@@ -49,6 +49,7 @@ class HTLCManager:
|
||||
|
||||
for locked_in in self.log[REMOTE]['locked_in'].values():
|
||||
if locked_in[REMOTE] is None:
|
||||
print("setting locked_in remote")
|
||||
locked_in[REMOTE] = next_ctn
|
||||
|
||||
self.expect_sig[SENT] = False
|
||||
@@ -62,10 +63,13 @@ class HTLCManager:
|
||||
if locked_in[LOCAL] is None:
|
||||
locked_in[LOCAL] = next_ctn
|
||||
|
||||
self.expect_sig[SENT] = False
|
||||
self.expect_sig[RECEIVED] = False
|
||||
|
||||
def send_rev(self):
|
||||
self.log[LOCAL]['ctn'] += 1
|
||||
for htlc_id, ctnheights in self.log[LOCAL]['settles'].items():
|
||||
if ctnheights[REMOTE] is None:
|
||||
ctnheights[REMOTE] = self.log[REMOTE]['ctn'] + 1
|
||||
|
||||
def recv_rev(self):
|
||||
self.log[REMOTE]['ctn'] += 1
|
||||
@@ -74,7 +78,10 @@ class HTLCManager:
|
||||
if ctnheights[LOCAL] is None:
|
||||
did_set_htlc_height = True
|
||||
assert ctnheights[REMOTE] == self.log[REMOTE]['ctn']
|
||||
ctnheights[LOCAL] = ctnheights[REMOTE]
|
||||
ctnheights[LOCAL] = self.log[LOCAL]['ctn'] + 1
|
||||
for htlc_id, ctnheights in self.log[REMOTE]['settles'].items():
|
||||
if ctnheights[LOCAL] is None:
|
||||
ctnheights[LOCAL] = self.log[LOCAL]['ctn'] + 1
|
||||
return did_set_htlc_height
|
||||
|
||||
def htlcs_by_direction(self, subject, direction, ctn=None):
|
||||
@@ -95,12 +102,13 @@ class HTLCManager:
|
||||
for htlc_id, ctnheights in self.log[party]['locked_in'].items():
|
||||
htlc_height = ctnheights[subject]
|
||||
if htlc_height is None:
|
||||
include = not self.expect_sig[RECEIVED if party == LOCAL else SENT] and ctnheights[-subject] <= ctn
|
||||
expect_sig = self.expect_sig[RECEIVED if party != LOCAL else SENT]
|
||||
include = not expect_sig and ctnheights[-subject] <= ctn
|
||||
else:
|
||||
include = htlc_height <= ctn
|
||||
if include:
|
||||
settles = self.log[party]['settles']
|
||||
if htlc_id not in settles or settles[htlc_id] > ctn:
|
||||
if htlc_id not in settles or settles[htlc_id][subject] is None or settles[htlc_id][subject] > ctn:
|
||||
fails = self.log[party]['fails']
|
||||
if htlc_id not in fails or fails[htlc_id] > ctn:
|
||||
l.append(self.log[party]['adds'][htlc_id])
|
||||
@@ -126,16 +134,20 @@ class HTLCManager:
|
||||
return self.htlcs(subject, ctn)
|
||||
|
||||
def send_settle(self, htlc_id):
|
||||
self.log[REMOTE]['settles'][htlc_id] = self.log[REMOTE]['ctn'] + 1
|
||||
self.log[REMOTE]['settles'][htlc_id] = {LOCAL: None, REMOTE: self.log[REMOTE]['ctn'] + 1}
|
||||
|
||||
def recv_settle(self, htlc_id):
|
||||
self.log[LOCAL]['settles'][htlc_id] = self.log[LOCAL]['ctn'] + 1
|
||||
self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.log[LOCAL]['ctn'] + 1, REMOTE: None}
|
||||
|
||||
def settled_htlcs_by(self, subject, ctn=None):
|
||||
assert type(subject) is HTLCOwner
|
||||
if ctn is None:
|
||||
ctn = self.log[subject]['ctn']
|
||||
return [self.log[subject]['adds'][htlc_id] for htlc_id, height in self.log[subject]['settles'].items() if height <= ctn]
|
||||
d = []
|
||||
for htlc_id, ctnheights in self.log[subject]['settles'].items():
|
||||
if ctnheights[subject] <= ctn:
|
||||
d.append(self.log[subject]['adds'][htlc_id])
|
||||
return d
|
||||
|
||||
def settled_htlcs(self, subject, ctn=None):
|
||||
assert type(subject) is HTLCOwner
|
||||
@@ -147,10 +159,10 @@ class HTLCManager:
|
||||
return sent + received
|
||||
|
||||
def received_in_ctn(self, ctn):
|
||||
return [self.log[REMOTE]['adds'][htlc_id] for htlc_id, height in self.log[REMOTE]['settles'].items() if height == ctn]
|
||||
return [self.log[REMOTE]['adds'][htlc_id] for htlc_id, ctnheights in self.log[REMOTE]['settles'].items() if ctnheights[LOCAL] == ctn]
|
||||
|
||||
def sent_in_ctn(self, ctn):
|
||||
return [self.log[LOCAL]['adds'][htlc_id] for htlc_id, height in self.log[LOCAL]['settles'].items() if height == ctn]
|
||||
return [self.log[LOCAL]['adds'][htlc_id] for htlc_id, ctnheights in self.log[LOCAL]['settles'].items() if ctnheights[LOCAL] == ctn]
|
||||
|
||||
def send_fail(self, htlc_id):
|
||||
self.log[REMOTE]['fails'][htlc_id] = self.log[REMOTE]['ctn'] + 1
|
||||
|
||||
Reference in New Issue
Block a user