|
|
@ -14,42 +14,54 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
B = HTLCManager() |
|
|
|
ah0, bh0 = H('A', 0), H('B', 0) |
|
|
|
B.recv_htlc(A.send_htlc(ah0)) |
|
|
|
self.assertTrue(B.expect_sig[RECEIVED]) |
|
|
|
self.assertTrue(A.expect_sig[SENT]) |
|
|
|
self.assertFalse(B.expect_sig[SENT]) |
|
|
|
self.assertFalse(A.expect_sig[RECEIVED]) |
|
|
|
self.assertEqual(B.log[REMOTE]['locked_in'][0][LOCAL], 1) |
|
|
|
A.recv_htlc(B.send_htlc(bh0)) |
|
|
|
self.assertTrue(B.expect_sig[RECEIVED]) |
|
|
|
self.assertTrue(A.expect_sig[SENT]) |
|
|
|
self.assertTrue(A.expect_sig[SENT]) |
|
|
|
self.assertTrue(B.expect_sig[RECEIVED]) |
|
|
|
self.assertEqual(B.current_htlcs(LOCAL), []) |
|
|
|
self.assertEqual(A.current_htlcs(LOCAL), []) |
|
|
|
self.assertEqual(B.pending_htlcs(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.pending_htlcs(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_next_ctx(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
self.assertEqual(B.pending_htlcs(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1]) |
|
|
|
self.assertEqual(A.pending_htlcs(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1]) |
|
|
|
self.assertEqual(B.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
B.send_rev() |
|
|
|
A.recv_rev() |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
self.assertEqual(B.current_htlcs(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1]) |
|
|
|
self.assertEqual(A.current_htlcs(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1]) |
|
|
|
self.assertEqual(B.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
self.assertEqual(B.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, ah0)]) |
|
|
|
self.assertEqual(A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, bh0)]) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1]) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1]) |
|
|
|
B.send_rev() |
|
|
|
A.recv_rev() |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
self.assertEqual(B.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1]) |
|
|
|
self.assertEqual(A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1]) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, ah0), (SENT, bh0)][::-1]) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), [(RECEIVED, bh0), (SENT, ah0)][::-1]) |
|
|
|
|
|
|
|
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))) |
|
|
|
self.assertEqual(len(B.pending_htlcs(REMOTE)), 0) |
|
|
|
self.assertEqual(len(A.pending_htlcs(REMOTE)), 1) |
|
|
|
self.assertEqual(len(B.pending_htlcs(LOCAL)), 1) |
|
|
|
self.assertEqual(len(A.pending_htlcs(LOCAL)), 0) |
|
|
|
self.assertEqual(len(B.get_htlcs_in_next_ctx(REMOTE)), 0) |
|
|
|
self.assertEqual(len(A.get_htlcs_in_next_ctx(REMOTE)), 1) |
|
|
|
self.assertEqual(len(B.get_htlcs_in_next_ctx(LOCAL)), 1) |
|
|
|
self.assertEqual(len(A.get_htlcs_in_next_ctx(LOCAL)), 0) |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
B.send_rev() |
|
|
@ -58,8 +70,8 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
A.recv_ctx() |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
self.assertEqual(len(A.current_htlcs(LOCAL)), 1) |
|
|
|
self.assertEqual(len(B.current_htlcs(LOCAL)), 1) |
|
|
|
self.assertEqual(len(A.get_htlcs_in_latest_ctx(LOCAL)), 1) |
|
|
|
self.assertEqual(len(B.get_htlcs_in_latest_ctx(LOCAL)), 1) |
|
|
|
if htlc_success: |
|
|
|
B.send_settle(0) |
|
|
|
A.recv_settle(0) |
|
|
@ -67,47 +79,47 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
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), []) |
|
|
|
self.assertNotEqual(A.get_htlcs_in_latest_ctx(LOCAL), []) |
|
|
|
self.assertNotEqual(B.get_htlcs_in_latest_ctx(REMOTE), []) |
|
|
|
|
|
|
|
self.assertEqual(A.pending_htlcs(LOCAL), []) |
|
|
|
self.assertNotEqual(A.pending_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(A.pending_htlcs(REMOTE), A.current_htlcs(REMOTE)) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(LOCAL), []) |
|
|
|
self.assertNotEqual(A.get_htlcs_in_next_ctx(REMOTE), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(REMOTE), A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
|
|
|
|
self.assertEqual(B.pending_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_next_ctx(REMOTE), []) |
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
A.send_rev() # here pending_htlcs(REMOTE) should become empty |
|
|
|
self.assertEqual(A.pending_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(REMOTE), []) |
|
|
|
|
|
|
|
B.recv_rev() |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
B.send_rev() |
|
|
|
A.recv_rev() |
|
|
|
self.assertEqual(B.current_htlcs(LOCAL), []) |
|
|
|
self.assertEqual(A.current_htlcs(LOCAL), []) |
|
|
|
self.assertEqual(A.current_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(B.current_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(LOCAL), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(LOCAL), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_latest_ctx(REMOTE), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_latest_ctx(REMOTE), []) |
|
|
|
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), []) |
|
|
|
self.assertNotEqual(A.pending_htlcs(LOCAL), []) |
|
|
|
self.assertNotEqual(B.pending_htlcs(REMOTE), []) |
|
|
|
self.assertEqual(B.pending_htlcs(LOCAL), []) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(REMOTE), []) |
|
|
|
self.assertNotEqual(A.get_htlcs_in_next_ctx(LOCAL), []) |
|
|
|
self.assertNotEqual(B.get_htlcs_in_next_ctx(REMOTE), []) |
|
|
|
self.assertEqual(B.get_htlcs_in_next_ctx(LOCAL), []) |
|
|
|
|
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
|
|
|
|
self.assertNotEqual(A.pending_htlcs(REMOTE), A.current_htlcs(REMOTE)) |
|
|
|
self.assertEqual(A.pending_htlcs(LOCAL), A.current_htlcs(LOCAL)) |
|
|
|
self.assertEqual(B.pending_htlcs(REMOTE), B.current_htlcs(REMOTE)) |
|
|
|
self.assertNotEqual(B.pending_htlcs(LOCAL), B.pending_htlcs(REMOTE)) |
|
|
|
self.assertNotEqual(A.get_htlcs_in_next_ctx(REMOTE), A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual(A.get_htlcs_in_next_ctx(LOCAL), A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual(B.get_htlcs_in_next_ctx(REMOTE), B.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertNotEqual(B.get_htlcs_in_next_ctx(LOCAL), B.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
|
|
|
|
htlc_lifecycle(htlc_success=True) |
|
|
|
htlc_lifecycle(htlc_success=False) |
|
|
@ -116,7 +128,8 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
def htlc_lifecycle(htlc_success: bool): |
|
|
|
A = HTLCManager() |
|
|
|
B = HTLCManager() |
|
|
|
B.recv_htlc(A.send_htlc(H('A', 0))) |
|
|
|
ah0 = H('A', 0) |
|
|
|
B.recv_htlc(A.send_htlc(ah0)) |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
B.send_rev() |
|
|
@ -127,11 +140,22 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
else: |
|
|
|
B.send_fail(0) |
|
|
|
A.recv_fail(0) |
|
|
|
self.assertEqual(B.pending_htlcs(REMOTE), []) |
|
|
|
self.assertEqual([], A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_oldest_unrevoked_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
self.assertEqual([], A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_oldest_unrevoked_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
|
|
|
|
htlc_lifecycle(htlc_success=True) |
|
|
|
htlc_lifecycle(htlc_success=False) |
|
|
@ -144,13 +168,38 @@ class TestHTLCManager(unittest.TestCase): |
|
|
|
B.send_rev() |
|
|
|
ah0 = H('A', 0) |
|
|
|
B.recv_htlc(A.send_htlc(ah0)) |
|
|
|
self.assertEqual([], A.current_htlcs(LOCAL)) |
|
|
|
self.assertEqual([], A.current_htlcs(REMOTE)) |
|
|
|
self.assertEqual([], A.pending_htlcs(LOCAL)) |
|
|
|
self.assertEqual([], A.pending_htlcs(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
A.recv_rev() |
|
|
|
self.assertEqual([], A.current_htlcs(LOCAL)) |
|
|
|
self.assertEqual([], A.current_htlcs(REMOTE)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.pending_htlcs(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.pending_htlcs(REMOTE)) |
|
|
|
|
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
A.send_ctx() |
|
|
|
B.recv_ctx() |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
B.send_rev() |
|
|
|
A.recv_rev() |
|
|
|
self.assertEqual([], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
B.send_ctx() |
|
|
|
A.recv_ctx() |
|
|
|
self.assertEqual([], A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|
A.send_rev() |
|
|
|
B.recv_rev() |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_oldest_unrevoked_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_latest_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_latest_ctx(REMOTE)) |
|
|
|
self.assertEqual([(Direction.SENT, ah0)], A.get_htlcs_in_next_ctx(LOCAL)) |
|
|
|
self.assertEqual([(Direction.RECEIVED, ah0)], A.get_htlcs_in_next_ctx(REMOTE)) |
|
|
|