def test_receive_mediatedtransfer_outoforder(raiden_network, private_keys): alice_app = raiden_network[0] messages = setup_messages_cb() graph = alice_app.raiden.channelgraphs.values()[0] token_address = graph.token_address mt_helper = MediatedTransferTestHelper(raiden_network, graph) initiator_address = alice_app.raiden.address path = mt_helper.get_paths_of_length(initiator_address, 2) # make sure we have no messages before the transfer assert len(messages) == 0 alice_address, bob_address, charlie_address = path amount = 10 result = alice_app.raiden.transfer_async( token_address, amount, charlie_address, ) # check for invitation ping assert len(messages) == 2 # Ping, Ack ping_message = decode(messages[0]) assert isinstance(ping_message, Ping) decoded = decode(messages[1]) assert isinstance(decoded, Ack) assert decoded.echo == sha3(ping_message.encode() + charlie_address) assert result.wait(timeout=10) gevent.sleep(1.) # check that transfer messages were added assert len(messages) == 22 # Ping, Ack + tranfer messages # make sure that the mediated transfer is sent after the invitation ping assert isinstance(decode(messages[2]), MediatedTransfer) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late locksroot = HASH lock = Lock(amount, 1, locksroot) identifier = create_default_identifier( alice_app.raiden.address, graph.token_address, charlie_address, ) mediated_transfer = MediatedTransfer(identifier=identifier, nonce=1, token=token_address, transferred_amount=amount, recipient=bob_address, locksroot=locksroot, lock=lock, target=charlie_address, initiator=initiator_address, fee=0) alice_key = PrivateKey(private_keys[0]) bob_app = mt_helper.get_app_from_address(bob_address) sign_and_send(mediated_transfer, alice_key, alice_address, bob_app)
def initiate_transfer(self, token_address, target_address, amount, identifier): if identifier is None: identifier = create_default_identifier() try: transfer_result = self.raiden_api.transfer( token_address=token_address, target=target_address, amount=amount, identifier=identifier) except (InvalidAmount, InvalidAddress, NoPathError) as e: return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT) except InsufficientFunds as e: return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED) if transfer_result is False: return api_error(errors="Payment couldn't be completed " "(insufficient funds or no route to target).", status_code=HTTPStatus.CONFLICT) transfer = { 'initiator_address': self.raiden_api.raiden.address, 'token_address': token_address, 'target_address': target_address, 'amount': amount, 'identifier': identifier, } result = self.transfer_schema.dump(transfer) return api_response(result=result.data)
def test_receive_mediatedtransfer_invalid_address(raiden_network, private_keys): alice_app = raiden_network[0] bob_app = raiden_network[1] graph = alice_app.raiden.token_to_channelgraph.values()[0] token_address = graph.token_address channel0 = channel(alice_app, bob_app, token_address) mt_helper = MediatedTransferTestHelper(raiden_network, graph) initiator_address = alice_app.raiden.address path = mt_helper.get_paths_of_length(initiator_address, 2) alice_address, bob_address, charlie_address = path amount = 10 result = alice_app.raiden.mediated_transfer_async( token_address, amount, charlie_address, identifier=1, ) assert result.wait(timeout=10) gevent.sleep(1.) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late lock = Lock(amount, 1, UNIT_HASHLOCK) identifier = create_default_identifier() mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, channel=channel0.channel_address, transferred_amount=amount, recipient=bob_address, locksroot=UNIT_HASHLOCK, lock=lock, target=charlie_address, initiator=initiator_address, fee=0 ) alice_key = PrivateKey(private_keys[0]) target_app = None for app in raiden_network: if app.raiden.address not in path: target_app = app break sign_and_send(mediated_transfer, alice_key, alice_address, target_app)
def test_receive_mediatedtransfer_invalid_address(raiden_network, private_keys): alice_app = raiden_network[0] bob_app = raiden_network[1] graph = list(alice_app.raiden.token_to_channelgraph.values())[0] token_address = graph.token_address channel0 = channel(alice_app, bob_app, token_address) mt_helper = MediatedTransferTestHelper(raiden_network, graph) initiator_address = alice_app.raiden.address path = mt_helper.get_paths_of_length(initiator_address, 2) alice_address, bob_address, charlie_address = path amount = 10 result = alice_app.raiden.mediated_transfer_async( token_address, amount, charlie_address, identifier=1, ) assert result.wait(timeout=10) gevent.sleep(1.) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late lock = Lock(amount, 1, UNIT_HASHLOCK) identifier = create_default_identifier() mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, channel=channel0.channel_address, transferred_amount=amount, recipient=bob_address, locksroot=UNIT_HASHLOCK, lock=lock, target=charlie_address, initiator=initiator_address, fee=0 ) alice_key = PrivateKey(private_keys[0]) target_app = None for app in raiden_network: if app.raiden.address not in path: target_app = app break sign_and_send(mediated_transfer, alice_key, alice_address, target_app)
def test_receive_mediatedtransfer_outoforder(raiden_network, private_keys): alice_app = raiden_network[0] bob_app = raiden_network[1] charlie_app = raiden_network[2] graph = alice_app.raiden.token_to_channelgraph.values()[0] token_address = graph.token_address channel0 = channel( alice_app, bob_app, token_address, ) amount = 10 result = alice_app.raiden.mediated_transfer_async( token_address, amount, charlie_app.raiden.address, identifier=1, ) assert result.wait(timeout=10) lock = Lock(amount, 1, UNIT_HASHLOCK) identifier = create_default_identifier() mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, channel=channel0.channel_address, transferred_amount=amount, recipient=bob_app.raiden.address, locksroot=UNIT_HASHLOCK, lock=lock, target=charlie_app.raiden.address, initiator=alice_app.raiden.address, fee=0 ) alice_key = PrivateKey(private_keys[0]) # send the invalid mediated transfer from alice to bob with the same nonce sign_and_send( mediated_transfer, alice_key, alice_app.raiden.address, bob_app, )
def test_receive_directtransfer_outoforder(raiden_network, private_keys): app0, app1 = raiden_network # pylint: disable=unbalanced-tuple-unpacking graph0 = app0.raiden.channelgraphs.values()[0] graph1 = app1.raiden.channelgraphs.values()[0] channel0 = graph0.partneraddress_channel[app1.raiden.address] channel1 = graph1.partneraddress_channel[app0.raiden.address] balance0 = channel0.balance balance1 = channel1.balance assert graph0.token_address == graph1.token_address assert app1.raiden.address in graph0.partneraddress_channel amount = 10 target = app1.raiden.address result = app0.raiden.transfer_async( graph0.token_address, amount, target, ) assert result.wait(timeout=10) gevent.sleep(1) assert_synched_channels( channel0, balance0 - amount, [], channel1, balance1 + amount, [] ) # and now send one more direct transfer with the same nonce, simulating # an out-of-order/resent message that arrives late identifier = create_default_identifier( app0.raiden.address, graph0.token_address, app1.raiden.address, ) direct_transfer = DirectTransfer( identifier=identifier, nonce=1, token=graph0.token_address, transferred_amount=10, recipient=app1.raiden.address, locksroot=HASH, ) app0_key = PrivateKey(private_keys[0]) sign_and_send(direct_transfer, app0_key, app0.raiden.address, app1)
def test_receive_mediatedtransfer_outoforder(raiden_network, private_keys): alice_app = raiden_network[0] bob_app = raiden_network[1] charlie_app = raiden_network[2] graph = list(alice_app.raiden.token_to_channelgraph.values())[0] token_address = graph.token_address channel0 = channel( alice_app, bob_app, token_address, ) amount = 10 result = alice_app.raiden.mediated_transfer_async( token_address, amount, charlie_app.raiden.address, identifier=1, ) assert result.wait(timeout=10) lock = Lock(amount, 1, UNIT_HASHLOCK) identifier = create_default_identifier() mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, channel=channel0.channel_address, transferred_amount=amount, recipient=bob_app.raiden.address, locksroot=UNIT_HASHLOCK, lock=lock, target=charlie_app.raiden.address, initiator=alice_app.raiden.address, fee=0 ) alice_key = PrivateKey(private_keys[0]) # send the invalid mediated transfer from alice to bob with the same nonce sign_and_send( mediated_transfer, alice_key, alice_app.raiden.address, bob_app, )
def test_receive_mediatedtransfer_invalid_address(raiden_network, private_keys): alice_app = raiden_network[0] setup_messages_cb() graph = alice_app.raiden.channelgraphs.values()[0] token_address = graph.token_address mt_helper = MediatedTransferTestHelper(raiden_network, graph) initiator_address = alice_app.raiden.address path = mt_helper.get_paths_of_length(initiator_address, 2) alice_address, bob_address, charlie_address = path amount = 10 alice_app.raiden.api.transfer( token_address, amount, charlie_address, ) gevent.sleep(1.) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late locksroot = HASH lock = Lock(amount, 1, locksroot) identifier = create_default_identifier( alice_app.raiden.address, graph.token_address, charlie_address, ) mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, transferred_amount=amount, recipient=bob_address, locksroot=locksroot, lock=lock, target=charlie_address, initiator=initiator_address, fee=0 ) alice_key = PrivateKey(private_keys[0]) target_app = None for app in raiden_network: if app.raiden.address not in path: target_app = app break sign_and_send(mediated_transfer, alice_key, alice_address, target_app)
def test_receive_directtransfer_outoforder(raiden_network, private_keys): app0, app1 = raiden_network # pylint: disable=unbalanced-tuple-unpacking graph0 = list(app0.raiden.token_to_channelgraph.values())[0] graph1 = list(app1.raiden.token_to_channelgraph.values())[0] channel0 = graph0.partneraddress_to_channel[app1.raiden.address] channel1 = graph1.partneraddress_to_channel[app0.raiden.address] balance0 = channel0.balance balance1 = channel1.balance assert graph0.token_address == graph1.token_address assert app1.raiden.address in graph0.partneraddress_to_channel amount = 10 target = app1.raiden.address result = app0.raiden.direct_transfer_async( graph0.token_address, amount, target, identifier=1, ) assert result.wait(timeout=10) gevent.sleep(1) assert_synched_channels( channel0, balance0 - amount, [], channel1, balance1 + amount, [] ) # and now send one more direct transfer with the same nonce, simulating # an out-of-order/resent message that arrives late identifier = create_default_identifier() direct_transfer_message = DirectTransfer( identifier=identifier, nonce=1, token=graph0.token_address, channel=channel0.channel_address, transferred_amount=10, recipient=app1.raiden.address, locksroot=UNIT_HASHLOCK, ) app0_key = PrivateKey(private_keys[0]) sign_and_send(direct_transfer_message, app0_key, app0.raiden.address, app1)
def test_receive_mediatedtransfer_outoforder(raiden_network, private_keys): alice_app = raiden_network[0] messages = setup_messages_cb() graph = alice_app.raiden.channelgraphs.values()[0] token_address = graph.token_address mt_helper = MediatedTransferTestHelper(raiden_network, graph) initiator_address = alice_app.raiden.address path = mt_helper.get_paths_of_length(initiator_address, 2) # make sure we have no messages before the transfer assert not messages alice_address, bob_address, charlie_address = path amount = 10 result = alice_app.raiden.transfer_async( token_address, amount, charlie_address, ) assert result.wait(timeout=10) gevent.sleep(1.) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late locksroot = HASH lock = Lock(amount, 1, locksroot) identifier = create_default_identifier() mediated_transfer = MediatedTransfer( identifier=identifier, nonce=1, token=token_address, transferred_amount=amount, recipient=bob_address, locksroot=locksroot, lock=lock, target=charlie_address, initiator=initiator_address, fee=0 ) alice_key = PrivateKey(private_keys[0]) bob_app = mt_helper.get_app_from_address(bob_address) sign_and_send(mediated_transfer, alice_key, alice_address, bob_app)
def test_receive_directtransfer_invalidlocksroot(raiden_network, private_keys): app0, app1 = raiden_network # pylint: disable=unbalanced-tuple-unpacking graph0 = app0.raiden.token_to_channelgraph.values()[0] graph1 = app1.raiden.token_to_channelgraph.values()[0] channel0 = graph0.partneraddress_to_channel[app1.raiden.address] channel1 = graph1.partneraddress_to_channel[app0.raiden.address] balance0 = channel0.balance balance1 = channel1.balance assert graph0.token_address == graph1.token_address assert app1.raiden.address in graph0.partneraddress_to_channel amount = 10 result = app0.raiden.direct_transfer_async( graph0.token_address, amount, target=app1.raiden.address, identifier=1, ) assert result.wait(timeout=10) gevent.sleep(1) assert_synched_channels( channel0, balance0 - amount, [], channel1, balance1 + amount, [] ) # and now send one more direct transfer with the locksroot not set correctly identifier = create_default_identifier() direct_transfer_message = DirectTransfer( identifier=identifier, nonce=2, token=graph0.token_address, channel=channel0.channel_address, transferred_amount=10, recipient=app1.raiden.address, locksroot=UNIT_HASHLOCK, ) app0_key = PrivateKey(private_keys[0]) sign_and_send(direct_transfer_message, app0_key, app0.raiden.address, app1)
def test_receive_directtransfer_wrongtoken(raiden_network, private_keys): app0, app1 = raiden_network # pylint: disable=unbalanced-tuple-unpacking graph0 = app0.raiden.channelgraphs.values()[0] graph1 = app1.raiden.channelgraphs.values()[0] channel0 = graph0.partneraddress_channel[app1.raiden.address] channel1 = graph1.partneraddress_channel[app0.raiden.address] balance0 = channel0.balance balance1 = channel1.balance assert graph0.token_address == graph1.token_address assert app1.raiden.address in graph0.partneraddress_channel amount = 10 app0.raiden.api.transfer( graph0.token_address, amount, target=app1.raiden.address, ) gevent.sleep(1) assert_synched_channels( channel0, balance0 - amount, [], channel1, balance1 + amount, [] ) # and now send one more direct transfer with a mistaken token address identifier = create_default_identifier( app0.raiden.address, graph0.token_address, app1.raiden.address, ) direct_transfer = DirectTransfer( identifier=identifier, nonce=2, token=HASH[0:20], transferred_amount=10, recipient=app1.raiden.address, locksroot=HASH, ) app0_key = PrivateKey(private_keys[0]) sign_and_send(direct_transfer, app0_key, app0.raiden.address, app1)
def initiate_transfer(self, token_address, target_address, amount, identifier): if identifier is None: identifier = create_default_identifier() try: transfer_result = self.raiden_api.transfer( token_address=token_address, target=target_address, amount=amount, identifier=identifier ) except (InvalidAmount, InvalidAddress, NoPathError) as e: return api_error( errors=str(e), status_code=HTTPStatus.CONFLICT ) except InsufficientFunds as e: return api_error( errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED ) if transfer_result is False: return api_error( errors="Payment couldn't be completed " "(insufficient funds or no route to target).", status_code=HTTPStatus.CONFLICT ) transfer = { 'initiator_address': self.raiden_api.raiden.address, 'token_address': token_address, 'target_address': target_address, 'amount': amount, 'identifier': identifier, } result = self.transfer_schema.dump(transfer) return api_response(result=result.data)
def initiate_transfer(self, token_address, target_address, amount, identifier): if identifier is None: identifier = create_default_identifier() try: self.raiden_api.transfer(token_address=token_address, target=target_address, amount=amount, identifier=identifier) except (InvalidAmount, InvalidAddress, NoPathError) as e: return make_response(str(e), httplib.CONFLICT) transfer = { 'initiator_address': self.raiden_api.raiden.address, 'token_address': token_address, 'target_address': target_address, 'amount': amount, 'identifier': identifier, } result = self.transfer_schema.dump(transfer) return jsonify(result.data)
def test_receive_mediatedtransfer_invalid_address(raiden_chain, token_addresses): app0, app1, app2 = raiden_chain token_address = token_addresses[0] amount = 10 result = app0.raiden.mediated_transfer_async( token_address, amount, app2.raiden.address, identifier=1, ) assert result.wait(timeout=10) # and now send one more mediated transfer with the same nonce, simulating # an out-of-order/resent message that arrives late channel0 = channel(app0, app1, token_address) lock = Lock(amount, 1, UNIT_HASHLOCK) identifier = create_default_identifier() mediated_transfer = MediatedTransfer(identifier=identifier, nonce=1, token=token_address, channel=channel0.channel_address, transferred_amount=amount, recipient=app1.raiden.address, locksroot=UNIT_HASHLOCK, lock=lock, target=app2.raiden.address, initiator=app0.raiden.address, fee=0) sign_and_send( mediated_transfer, app0.raiden.private_key, app0.raiden.address, app1, )
def assert_identifier_correct(initiator_app, token, target, expected_id): got_id = create_default_identifier(initiator_app.raiden.address, token, target) assert got_id == expected_id