Example #1
0
 def htlc_lifecycle(htlc_success: bool):
     A = HTLCManager(StoredDict({}, None, []))
     B = HTLCManager(StoredDict({}, None, []))
     A.channel_open_finished()
     B.channel_open_finished()
     ah0 = H('A', 0)
     B.recv_htlc(A.send_htlc(ah0))
     A.send_ctx()
     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([], 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))
Example #2
0
        def htlc_lifecycle(htlc_success: bool):
            A = HTLCManager(StoredDict({}, None, []))
            B = HTLCManager(StoredDict({}, None, []))
            A.channel_open_finished()
            B.channel_open_finished()
            B.recv_htlc(A.send_htlc(H('A', 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()
            A.recv_rev()
            B.send_ctx()
            A.recv_ctx()
            A.send_rev()
            B.recv_rev()
            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)
            else:
                B.send_fail(0)
                A.recv_fail(0)
            self.assertEqual(list(A.htlcs_by_direction(REMOTE, RECEIVED).values()), [H('A', 0)])
            self.assertNotEqual(A.get_htlcs_in_latest_ctx(LOCAL), [])
            self.assertNotEqual(B.get_htlcs_in_latest_ctx(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.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.get_htlcs_in_next_ctx(REMOTE), [])

            B.recv_rev()
            A.send_ctx()
            B.recv_ctx()
            B.send_rev()
            A.recv_rev()
            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.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.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))