async def test_dil_receive_transfer(config): # initialize t = Transfer() t.payload = {'id': '001', 'data': '0xdummy_data'} init = MockInitiator([t]) resp = MockResponder() net_profile, channel_name, cc_name, cc_version, org_name, user_name, peer_name = config state_manager = FabricILStateManager(net_profile, channel_name, cc_name, cc_version, org_name, user_name, peer_name) dil = DecentralizedInterledger(init, resp, state_manager)
async def test_dil_receive_transfer(): # initialize t = Transfer() t.payload = {'id': '001', 'data': '0xdummy_data'} init = MockInitiator([t]) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) assert not dil.state_manager.local_transfers assert not dil.state_manager.transfers_ready assert not dil.state_manager.transfers_responded # start task = asyncio.ensure_future(dil.receive_transfer()) assert task.done() == False l = await task # check local transfers populated assert dil.state_manager.local_transfers assert len(dil.state_manager.local_transfers) == 1 local_transfer = dil.state_manager.local_transfers['001'] assert local_transfer.payload['id'] assert local_transfer.payload['nonce'] nonce = local_transfer.payload['nonce'] assert local_transfer.payload['data'] == '0xdummy_data' assert local_transfer.status == TransferStatus.READY # check transfer ready assert dil.state_manager.transfers_ready assert len(dil.state_manager.transfers_ready) == 1 transfer_ready = dil.state_manager.transfers_ready[0] assert transfer_ready.payload['id'] == '001' assert transfer_ready.payload['nonce'] == nonce assert transfer_ready.payload['data'] == '0xdummy_data' assert transfer_ready.status == TransferStatus.READY # TODO: test filtering of incoming transfer assert not dil.state_manager.transfers_responded
async def test_interledger_receive_transfer(): t = Transfer() t.payload = {} init = MockInitiator([t]) resp = MockResponder() interledger = Interledger(init, resp) # interledger.test_interrupt = True task = asyncio.ensure_future(interledger.receive_transfer()) assert task.done() == False l = await task assert init.events == [] assert len(interledger.transfers) == 1 assert l == 1 assert interledger.transfers[0].status == TransferStatus.READY
async def test_interledger_confirm_transfer_abort(): async def foo(): return {'abort_status': True, 'abort_tx_hash': '0x444'} i = Interledger(MockInitiator([]), MockResponder()) t = Transfer() t.payload = {} t.payload['id'] = str(uuid4().int) t.result = {} t.status = TransferStatus.CONFIRMING t.confirm_task = asyncio.ensure_future(foo()) i.results_aborting = [t] task = asyncio.ensure_future(i.confirm_transfer()) assert task.done() == False await task res = i.results_abort[0] assert t.status == TransferStatus.FINALIZED assert res['abort_status'] == True assert res['abort_tx_hash'] == '0x444' assert len(i.results_abort) == 1 assert len(i.results_commit) == 0 assert len(i.results_aborting) == 0
async def test_dil_send_transfer(): # initialize t = Transfer() t.payload = {'id': '001', 'data': '0xdummy_data'} t.payload['nonce'] = str(uuid4().int) t.send_accepted = True init = MockInitiator([t]) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) dil.transfers = [t] dil.state_manager.local_transfers = {'001': deepcopy(t)} dil.state_manager.transfers_ready = [t] assert len(dil.transfers_sent) == 0 # start task = asyncio.ensure_future(dil.send_transfer()) assert task.done() == False await task # check local transfers updated assert dil.state_manager.local_transfers assert len(dil.state_manager.local_transfers) == 1 local_transfer = dil.state_manager.local_transfers['001'] assert local_transfer.payload['id'] assert local_transfer.payload['nonce'] assert local_transfer.payload['data'] == '0xdummy_data' assert local_transfer.status == TransferStatus.SENT # check for transfer sent assert len(dil.transfers_sent) == 1 transfer_sent = dil.transfers[0] assert transfer_sent.status == TransferStatus.SENT assert asyncio.isfuture(transfer_sent.send_task) await transfer_sent.send_task assert transfer_sent.send_task.done() == True
async def test_interledger_transfer_result(): async def foo(): return 42 i = Interledger(MockInitiator([]), MockResponder()) t = Transfer() t.status = TransferStatus.SENT t.send_task = asyncio.ensure_future(foo()) i.transfers_sent = [t] task = asyncio.ensure_future(i.transfer_result()) assert task.done() == False await task assert len(i.transfers_sent) == 0 assert len(i.transfers_responded) == 1 tr = i.transfers_responded[0] assert tr.status == TransferStatus.RESPONDED assert tr.result == 42
async def test_interledger_send_transfer(): t = Transfer() t.payload = {} t.payload['nonce'] = str(uuid4().int) t.payload['data'] = b"dummy" i = Interledger(MockInitiator([]), MockResponder()) i.transfers = [t] task = asyncio.ensure_future(i.send_transfer()) assert task.done() == False await task assert len(i.transfers_sent) == 1 tr = i.transfers[0] assert tr.status == TransferStatus.SENT assert asyncio.isfuture(tr.send_task) await tr.send_task assert tr.send_task.done() == True
async def test_dil_process_result_abort(): # initialize t = Transfer() t.status = TransferStatus.RESPONDED t.payload = {'id': '001', 'data': '0xdummy_data'} t.payload['nonce'] = str(uuid4().int) t.result = {'status': False} init = MockInitiator([]) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) dil.state_manager.local_transfers = {'001': deepcopy(t)} dil.state_manager.transfers_responded = [t] async def foo(): return 42 t.confirm_task = asyncio.ensure_future(foo()) dil.transfers = [t] # start task = asyncio.ensure_future(dil.process_result()) assert task.done() == False await task # check local transfers updated assert dil.state_manager.local_transfers assert len(dil.state_manager.local_transfers) == 1 local_transfer = dil.state_manager.local_transfers['001'] assert local_transfer.status == TransferStatus.CONFIRMING assert local_transfer.result['status'] == False # check for transfer confirming assert len(dil.results_aborting) == 1 result_confirming = dil.results_aborting[0] assert result_confirming.status == TransferStatus.CONFIRMING assert result_confirming.result['status'] == False assert asyncio.isfuture(result_confirming.confirm_task) assert len(dil.results_commit) == 0 assert len(dil.results_abort) == 0 await result_confirming.confirm_task assert result_confirming.confirm_task.done() == True assert len(dil.state_manager.transfers_responded) == 0
async def test_dil_confirm_transfer_abort(): # initialize t = Transfer() t.status = TransferStatus.CONFIRMING t.payload = {'id': '001', 'data': '0xdummy_data'} t.payload['nonce'] = str(uuid4().int) t.result = {} init = MockInitiator([]) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) dil.state_manager.local_transfers = {'001': deepcopy(t)} async def foo(): return {'abort_status': True, 'abort_tx_hash': '0x444'} t.confirm_task = asyncio.ensure_future(foo()) dil.results_aborting = [t] # start task = asyncio.ensure_future(dil.confirm_transfer()) assert task.done() == False await task # check local transfers updated assert dil.state_manager.local_transfers assert len(dil.state_manager.local_transfers) == 1 local_transfer = dil.state_manager.local_transfers['001'] assert local_transfer.status == TransferStatus.FINALIZED assert local_transfer.result['abort_status'] == True # check for transfer confirming assert len(dil.results_abort) == 1 result_confirming = dil.results_abort[0] assert result_confirming.status == TransferStatus.FINALIZED assert result_confirming.result['abort_status'] == True assert result_confirming.result['abort_tx_hash'] == '0x444' assert asyncio.isfuture(result_confirming.confirm_task) assert len(dil.results_aborting) == 0 assert len(dil.results_commit) == 0
async def test_dil_run(): l1, l2, l3 = [], [], [] for i in range(2): t1, t2, t3 = Transfer(), Transfer(), Transfer() t1.payload, t2.payload, t3.payload = {}, {}, {} t1.payload['nonce'], t1.payload['id'], t1.payload['data'] = str( uuid4().int), '1', b"dummy1" t2.payload['nonce'], t2.payload['id'], t2.payload['data'] = str( uuid4().int), '2', b"dummy2" t3.payload['nonce'], t3.payload['id'], t3.payload['data'] = str( uuid4().int), '3', b"dummy3" l1.append(t1) l2.append(t2) l3.append(t3) init = MockInitiator(l1) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) task = asyncio.ensure_future(dil.run()) time = 0.5 # Consume l1 await asyncio.sleep(time) # Simulate interledger running # New events init.events = l2 # Consume l2 await asyncio.sleep(time) # Simulate interledger running # New events dil.responder = MockResponderAbort() init.events = l3 # Consume l3, but with a responder returning False -> abort await asyncio.sleep(time) # Simulate interledger running assert len(dil.transfers) == 0 assert len(dil.transfers_sent) == 0 assert len(dil.transfers_responded) == 0 assert len(dil.results_committing) == 0 assert len(dil.results_aborting) == 0 # below should not equal to 4, since transfer with repeated id will be rejected assert len(dil.results_commit) == 2 # below should not equal to 2, since transfer with repeated id will be rejected assert len(dil.results_abort) == 1 dil.stop() await task
async def test_interledger_run_no_cleanup(): l1, l2, l3 = [], [], [] for i in range(4): t1, t2, t3 = Transfer(), Transfer(), Transfer() t1.payload, t2.payload, t3.payload = {}, {}, {} t1.payload['nonce'], t1.payload['id'], t1.payload['data'] = str( uuid4().int), '1', b"dummy1" t2.payload['nonce'], t2.payload['id'], t2.payload['data'] = str( uuid4().int), '2', b"dummy2" t3.payload['nonce'], t3.payload['id'], t3.payload['data'] = str( uuid4().int), '3', b"dummy3" l1.append(t1) l2.append(t2) l3.append(t3) init = MockInitiator(l1) i = Interledger(init, MockResponder()) with patch("interledger.interledger.Interledger.cleanup") as mock_cleanup: task = asyncio.ensure_future(i.run()) time = 0.5 # Consume l1 await asyncio.sleep(time) # Simulate interledger running # New events init.events = l2 # Consume l2 await asyncio.sleep(time) # Simulate interledger running # New events i.responder = MockResponderAbort() init.events = l3 # Consume l3, but with a responder returning False -> abort await asyncio.sleep(time) # Simulate interledger running assert len(i.transfers) == 12 assert len(i.transfers_sent) == 0 assert len(i.transfers_responded) == 0 assert len(i.results_committing) == 0 assert len(i.results_aborting) == 0 assert len(i.results_commit) == 8 assert len(i.results_abort) == 4 i.stop() await task
async def test_dil_transfer_result(): # initialize t = Transfer() t.status = TransferStatus.SENT t.payload = {'id': '001', 'data': '0xdummy_data'} t.payload['nonce'] = str(uuid4().int) init = MockInitiator([]) resp = MockResponder() state_manager = LocalILStateManger() dil = DecentralizedInterledger(init, resp, state_manager) dil.state_manager.local_transfers = {'001': deepcopy(t)} async def foo(): return 42 t.send_task = asyncio.ensure_future(foo()) dil.transfers = [t] dil.transfers_sent = [t] # start task = asyncio.ensure_future(dil.transfer_result()) assert task.done() == False await task # check local transfers updated assert dil.state_manager.local_transfers assert len(dil.state_manager.local_transfers) == 1 local_transfer = dil.state_manager.local_transfers['001'] assert local_transfer.status == TransferStatus.RESPONDED assert local_transfer.result == 42 # check for transfer responded assert len(dil.state_manager.transfers_responded) == 1 transfer_responded = dil.transfers[0] assert transfer_responded.status == TransferStatus.RESPONDED assert transfer_responded.result == 42 assert not dil.transfers_sent
async def test_interledger_process_result_abort(): i = Interledger(MockInitiator([]), MockResponder()) t = Transfer() t.payload = {} t.payload['id'] = str(uuid4().int) t.status = TransferStatus.RESPONDED t.result = {"status": False} i.transfers_responded = [t] task = asyncio.ensure_future(i.process_result()) assert task.done() == False await task tr = i.results_aborting[0] assert tr.status == TransferStatus.CONFIRMING assert tr.result['status'] == False assert len(i.results_commit) == 0 assert len(i.results_abort) == 0 assert len(i.transfers_responded) == 0
def return_transfer_list(): t1 = Transfer() t2 = Transfer() t3 = Transfer() t4 = Transfer() t5 = Transfer() # t1 is State.ready by default t2.status = TransferStatus.SENT t3.status = TransferStatus.RESPONDED t4.status = TransferStatus.CONFIRMING t5.status = TransferStatus.FINALIZED return [t1, t2, t3, t4, t5]