mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
lnhtlc: handle fails asymmetrically
This commit is contained in:
parent
f47519bdf3
commit
fda6fb6521
2 changed files with 103 additions and 85 deletions
|
@ -23,8 +23,7 @@ class HTLCManager:
|
|||
# "side who offered htlc" -> action -> htlc_id -> whose ctx -> ctn
|
||||
log[sub]['locked_in'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['locked_in'].items()}
|
||||
log[sub]['settles'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['settles'].items()}
|
||||
# FIXME "fails" should be handled like "settles"
|
||||
log[sub]['fails'] = {int(x): y for x, y in log[sub]['fails'].items()}
|
||||
log[sub]['fails'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['fails'].items()}
|
||||
self.log = log
|
||||
|
||||
def to_save(self):
|
||||
|
@ -67,7 +66,8 @@ class HTLCManager:
|
|||
|
||||
def send_rev(self) -> None:
|
||||
self.ctn[LOCAL] += 1
|
||||
for htlc_id, ctns in self.log[LOCAL]['settles'].items():
|
||||
for log_action in ('settles', 'fails'):
|
||||
for htlc_id, ctns in self.log[LOCAL][log_action].items():
|
||||
if ctns[REMOTE] is None:
|
||||
ctns[REMOTE] = self.ctn[REMOTE] + 1
|
||||
|
||||
|
@ -77,7 +77,8 @@ class HTLCManager:
|
|||
if ctns[LOCAL] is None:
|
||||
assert ctns[REMOTE] == self.ctn[REMOTE]
|
||||
ctns[LOCAL] = self.ctn[LOCAL] + 1
|
||||
for htlc_id, ctns in self.log[REMOTE]['settles'].items():
|
||||
for log_action in ('settles', 'fails'):
|
||||
for htlc_id, ctns in self.log[REMOTE][log_action].items():
|
||||
if ctns[LOCAL] is None:
|
||||
ctns[LOCAL] = self.ctn[LOCAL] + 1
|
||||
|
||||
|
@ -105,9 +106,10 @@ class HTLCManager:
|
|||
include = htlc_height <= ctn
|
||||
if include:
|
||||
settles = self.log[party]['settles']
|
||||
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:
|
||||
not_settled = htlc_id not in settles or settles[htlc_id][subject] is None or settles[htlc_id][subject] > ctn
|
||||
not_failed = htlc_id not in fails or fails[htlc_id][subject] is None or fails[htlc_id][subject] > ctn
|
||||
if not_settled and not_failed:
|
||||
l.append(self.log[party]['adds'][htlc_id])
|
||||
return l
|
||||
|
||||
|
@ -179,7 +181,7 @@ class HTLCManager:
|
|||
if ctns[LOCAL] == ctn]
|
||||
|
||||
def send_fail(self, htlc_id: int) -> None:
|
||||
self.log[REMOTE]['fails'][htlc_id] = self.ctn[REMOTE] + 1
|
||||
self.log[REMOTE]['fails'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE] + 1}
|
||||
|
||||
def recv_fail(self, htlc_id: int) -> None:
|
||||
self.log[LOCAL]['fails'][htlc_id] = self.ctn[LOCAL] + 1
|
||||
self.log[LOCAL]['fails'][htlc_id] = {LOCAL: self.ctn[LOCAL] + 1, REMOTE: None}
|
||||
|
|
|
@ -9,7 +9,7 @@ class H(NamedTuple):
|
|||
htlc_id : int
|
||||
|
||||
class TestHTLCManager(unittest.TestCase):
|
||||
def test_race(self):
|
||||
def test_adding_htlcs_race(self):
|
||||
A = HTLCManager()
|
||||
B = HTLCManager()
|
||||
ah0, bh0 = H('A', 0), H('B', 0)
|
||||
|
@ -41,7 +41,8 @@ class TestHTLCManager(unittest.TestCase):
|
|||
self.assertEqual(B.current_htlcs(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1])
|
||||
self.assertEqual(A.current_htlcs(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1])
|
||||
|
||||
def test_no_race(self):
|
||||
def test_single_htlc_full_lifecycle(self):
|
||||
def htlc_lifecycle(htlc_success: bool):
|
||||
A = HTLCManager()
|
||||
B = HTLCManager()
|
||||
B.recv_htlc(A.send_htlc(H('A', 0)))
|
||||
|
@ -59,8 +60,12 @@ class TestHTLCManager(unittest.TestCase):
|
|||
B.recv_rev()
|
||||
self.assertEqual(len(A.current_htlcs(LOCAL)), 1)
|
||||
self.assertEqual(len(B.current_htlcs(LOCAL)), 1)
|
||||
if htlc_success:
|
||||
B.send_settle(0)
|
||||
A.recv_settle(0)
|
||||
else:
|
||||
B.send_fail(0)
|
||||
A.recv_fail(0)
|
||||
self.assertEqual(A.htlcs_by_direction(REMOTE, RECEIVED), [H('A', 0)])
|
||||
self.assertNotEqual(A.current_htlcs(LOCAL), [])
|
||||
self.assertNotEqual(B.current_htlcs(REMOTE), [])
|
||||
|
@ -84,9 +89,9 @@ class TestHTLCManager(unittest.TestCase):
|
|||
self.assertEqual(A.current_htlcs(LOCAL), [])
|
||||
self.assertEqual(A.current_htlcs(REMOTE), [])
|
||||
self.assertEqual(B.current_htlcs(REMOTE), [])
|
||||
self.assertEqual(len(A.all_settled_htlcs_ever(LOCAL)), 1)
|
||||
self.assertEqual(len(A.sent_in_ctn(2)), 1)
|
||||
self.assertEqual(len(B.received_in_ctn(2)), 1)
|
||||
self.assertEqual(len(A.all_settled_htlcs_ever(LOCAL)), int(htlc_success))
|
||||
self.assertEqual(len(A.sent_in_ctn(2)), int(htlc_success))
|
||||
self.assertEqual(len(B.received_in_ctn(2)), int(htlc_success))
|
||||
|
||||
A.recv_htlc(B.send_htlc(H('B', 0)))
|
||||
self.assertEqual(A.pending_htlcs(REMOTE), [])
|
||||
|
@ -104,7 +109,11 @@ class TestHTLCManager(unittest.TestCase):
|
|||
self.assertEqual(B.pending_htlcs(REMOTE), B.current_htlcs(REMOTE))
|
||||
self.assertNotEqual(B.pending_htlcs(LOCAL), B.pending_htlcs(REMOTE))
|
||||
|
||||
def test_settle_while_owing_commitment(self):
|
||||
htlc_lifecycle(htlc_success=True)
|
||||
htlc_lifecycle(htlc_success=False)
|
||||
|
||||
def test_remove_htlc_while_owing_commitment(self):
|
||||
def htlc_lifecycle(htlc_success: bool):
|
||||
A = HTLCManager()
|
||||
B = HTLCManager()
|
||||
B.recv_htlc(A.send_htlc(H('A', 0)))
|
||||
|
@ -112,10 +121,17 @@ class TestHTLCManager(unittest.TestCase):
|
|||
B.recv_ctx()
|
||||
B.send_rev()
|
||||
A.recv_rev()
|
||||
if htlc_success:
|
||||
B.send_settle(0)
|
||||
A.recv_settle(0)
|
||||
else:
|
||||
B.send_fail(0)
|
||||
A.recv_fail(0)
|
||||
self.assertEqual(B.pending_htlcs(REMOTE), [])
|
||||
B.send_ctx()
|
||||
A.recv_ctx()
|
||||
A.send_rev()
|
||||
B.recv_rev()
|
||||
|
||||
htlc_lifecycle(htlc_success=True)
|
||||
htlc_lifecycle(htlc_success=False)
|
||||
|
|
Loading…
Add table
Reference in a new issue