def test_check_pfs_for_production(service_registry_address, private_keys, web3, contract_manager) -> None: chain_id = ChainID(int(web3.net.version)) service_registry, _ = deploy_service_registry_and_set_urls( private_keys=private_keys, web3=web3, contract_manager=contract_manager, service_registry_address=service_registry_address, ) # Configuring an address that doesn't match the registered url should error pfs_info = PFSInfo( url="http://ourgivenaddress", price=TokenAmount(0), chain_id=chain_id, token_network_registry_address= token_network_registry_address_test_default, payment_address=privatekey_to_address(private_keys[0]), message="", operator="", version="", user_deposit_address=privatekey_to_address(private_keys[1]), confirmed_block_number=BlockNumber(10), matrix_server="http://matrix.example.com", ) with pytest.raises(RaidenError): check_pfs_for_production(service_registry=service_registry, pfs_info=pfs_info) # Configuring an pfs payment address that isn't registered should error pfs_info = PFSInfo( url="http://foo", price=TokenAmount(0), chain_id=chain_id, token_network_registry_address= token_network_registry_address_test_default, payment_address=to_canonical_address( "0x2222222222222222222222222222222222222221"), message="", operator="", version="", user_deposit_address=privatekey_to_address(private_keys[1]), confirmed_block_number=BlockNumber(10), matrix_server="http://matrix.example.com", ) with pytest.raises(RaidenError): check_pfs_for_production(service_registry=service_registry, pfs_info=pfs_info)
def test_mediated_transfer_calls_pfs(raiden_network, token_addresses): app0, = raiden_network token_address = token_addresses[0] chain_state = views.state_from_app(app0) token_network_registry_address = app0.raiden.default_registry.address token_network_address = views.get_token_network_address_by_token_address( chain_state, token_network_registry_address, token_address) with patch("raiden.routing.query_paths", return_value=([], None)) as patched: app0.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=10, target=factories.HOP1, identifier=1, secret=b"1" * 32, ) assert not patched.called # Setup PFS config app0.raiden.config["pfs_config"] = PFSConfig( info=PFSInfo( url="mock-address", chain_id=app0.raiden.rpc_client.chain_id, token_network_registry_address=token_network_registry_address, payment_address=factories.make_address(), message="", operator="", version="", price=TokenAmount(0), ), maximum_fee=TokenAmount(100), iou_timeout=BlockNumber(100), max_paths=5, ) app0.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=11, target=factories.HOP2, identifier=2, secret=b"2" * 32, ) assert patched.call_count == 1 # Mediator should not re-query PFS locked_transfer = factories.create( factories.LockedTransferProperties( amount=TokenAmount(5), initiator=factories.HOP1, target=TargetAddress(factories.HOP2), sender=factories.HOP1, pkey=factories.HOP1_KEY, token=token_address, canonical_identifier=factories.make_canonical_identifier( token_network_address=token_network_address), )) app0.raiden.mediate_mediated_transfer(locked_transfer) assert patched.call_count == 1
def test_no_iou_when_pfs_price_0(query_paths_args): """ Test that no IOU is sent when PFS is for free """ query_paths_args["pfs_config"] = PFSConfig( info=PFSInfo( url="abc", price=TokenAmount(0), chain_id=ChainID(42), token_network_registry_address=factories. make_token_network_registry_address(), user_deposit_address=factories.make_address(), payment_address=factories.make_address(), confirmed_block_number=dict(number=BlockNumber(1)), message="", operator="", version="", matrix_server="http://matrix.example.com", matrix_room_id="!room-id:matrix.example.com", ), maximum_fee=TokenAmount(100), iou_timeout=BlockNumber(100), max_paths=5, ) with patch("raiden.network.pathfinding.get_pfs_info") as mocked_pfs_info: mocked_pfs_info.return_value = PFS_CONFIG.info with patch.object(pathfinding, "post_pfs_paths", return_value=mocked_json_response()) as post_path: query_paths( pfs_config=query_paths_args["pfs_config"], our_address=query_paths_args["our_address"], privkey=query_paths_args["privkey"], current_block_number=query_paths_args["current_block_number"], token_network_address=query_paths_args[ "token_network_address"], one_to_n_address=query_paths_args["one_to_n_address"], chain_id=query_paths_args["chain_id"], route_from=query_paths_args["route_from"], route_to=query_paths_args["route_to"], value=query_paths_args["value"], pfs_wait_for_block=query_paths_args["pfs_wait_for_block"], ) assert post_path.call_args == call( payload={ "from": to_checksum_address(query_paths_args["route_from"]), "to": to_checksum_address(query_paths_args["route_to"]), "value": query_paths_args["value"], "max_paths": query_paths_args["pfs_config"].max_paths, }, token_network_address=query_paths_args["token_network_address"], url=query_paths_args["pfs_config"].info.url, )
def test_check_pfs_transport_configuration(chain_id, private_keys, caplog): matrix_server_url = "http://matrix.example.com" matrix_room_id = "!room-id:matrix.example.com" pfs_info = PFSInfo( url="http://foo", price=TokenAmount(0), chain_id=chain_id, token_network_registry_address=token_network_registry_address_test_default, payment_address=to_canonical_address("0x2222222222222222222222222222222222222221"), message="", operator="", version="", user_deposit_address=privatekey_to_address(private_keys[1]), confirmed_block_number=BlockNumber(10), matrix_server=matrix_server_url, matrix_room_id=matrix_room_id, ) # Room id mismatch, must raise with pytest.raises(RaidenError): check_pfs_transport_configuration( pfs_info=pfs_info, pfs_was_autoselected=True, transport_pfs_broadcast_room_id="!this-is-not-the-room-youre-looking-for:example.com", matrix_server_url=matrix_server_url, matrix_server_was_autoselected=True, ) # Room ids match, must not raise check_pfs_transport_configuration( pfs_info=pfs_info, pfs_was_autoselected=True, transport_pfs_broadcast_room_id=matrix_room_id, matrix_server_url=matrix_server_url, matrix_server_was_autoselected=True, ) # With the matrix_room_id missing from the PFS response the check can't be performed pfs_info_no_room_id = dataclasses.replace(pfs_info, matrix_room_id=None) with caplog.at_level(logging.WARNING): check_pfs_transport_configuration( pfs_info=pfs_info_no_room_id, pfs_was_autoselected=True, transport_pfs_broadcast_room_id="!not-this-again:matrix.org", matrix_server_url=matrix_server_url, matrix_server_was_autoselected=True, ) assert "Can't check PFS transport configuration" in ( record.msg["event"] for record in caplog.records )
def test_setup_proxies_no_service_registry_but_pfs() -> None: """ Test that if no service registry is provided but a manual pfs address is given then startup still works Regression test for https://github.com/raiden-network/raiden/issues/3740 """ chain_id = ChainID(5) config = RaidenConfig( chain_id=chain_id, environment_type=Environment.DEVELOPMENT, services=ServiceConfig( pathfinding_max_fee=TokenAmount(100), pathfinding_iou_timeout=BlockTimeout(500), pathfinding_max_paths=5, ), ) config.transport.available_servers = ["http://matrix.example.com"] contracts = load_deployed_contracts_data(config, chain_id) proxy_manager = MockProxyManager(node_address=make_address()) PFS_INFO = PFSInfo( url="my-pfs", price=TokenAmount(12), chain_id=ChainID(5), token_network_registry_address=TokenNetworkRegistryAddress( to_canonical_address(contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["address"]) ), user_deposit_address=user_deposit_address_test_default, confirmed_block_number=BlockNumber(1), payment_address=pfs_payment_address_default, message="This is your favorite pathfinding service", operator="John Doe", version="0.0.3", matrix_server="http://matrix.example.com", matrix_room_id="!room-id:matrix.example.com", ) deployed_addresses = load_deployment_addresses_from_contracts(contracts) with patch.object(pathfinding, "get_pfs_info", return_value=PFS_INFO): services_bundle = services_bundle_from_contracts_deployment( config=config, proxy_manager=proxy_manager, # type: ignore deployed_addresses=deployed_addresses, routing_mode=RoutingMode.PFS, pathfinding_service_address="my-pfs", enable_monitoring=True, ) assert services_bundle
def test_setup_proxies_all_addresses_are_known(): """ Test that startup for proxies works fine if all addresses are given and routing is basic """ chain_id = ChainID(5) config = RaidenConfig(chain_id=chain_id, environment_type=Environment.DEVELOPMENT) config.transport.available_servers = ["http://matrix.example.com"] contracts = load_deployed_contracts_data(config, chain_id) proxy_manager = MockProxyManager(node_address=make_address()) PFS_INFO = PFSInfo( url="my-pfs", price=TokenAmount(12), chain_id=ChainID(5), token_network_registry_address=to_canonical_address( contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["address"]), user_deposit_address=user_deposit_address_test_default, payment_address=pfs_payment_address_default, confirmed_block_number=BlockNumber(1), message="This is your favorite pathfinding service", operator="John Doe", version="0.0.3", matrix_server="http://matrix.example.com", matrix_room_id="!room-id:matrix.example.com", ) deployed_addresses = load_deployment_addresses_from_contracts(contracts) with patch.object(pathfinding, "get_pfs_info", return_value=PFS_INFO): raiden_bundle = raiden_bundle_from_contracts_deployment( proxy_manager=proxy_manager, token_network_registry_address=deployed_addresses. token_network_registry_address, secret_registry_address=deployed_addresses.secret_registry_address, ) services_bundle = services_bundle_from_contracts_deployment( config=config, proxy_manager=proxy_manager, deployed_addresses=deployed_addresses, routing_mode=RoutingMode.PFS, pathfinding_service_address="my-pfs", enable_monitoring=False, ) assert raiden_bundle assert services_bundle assert raiden_bundle.token_network_registry assert raiden_bundle.secret_registry assert services_bundle.user_deposit assert services_bundle.service_registry
def test_no_iou_when_pfs_price_0(query_paths_args): """ Test that no IOU is sent when PFS is for free """ query_paths_args["pfs_config"] = PFSConfig( info=PFSInfo( url="abc", price=TokenAmount(0), chain_id=ChainID(42), token_network_registry_address=factories. make_token_network_address(), payment_address=factories.make_address(), message="", operator="", version="", ), maximum_fee=TokenAmount(100), iou_timeout=BlockNumber(100), max_paths=5, ) with patch.object(pathfinding, "post_pfs_paths", return_value=mocked_json_response()) as post_path: query_paths( pfs_config=query_paths_args["pfs_config"], our_address=query_paths_args["our_address"], privkey=query_paths_args["privkey"], current_block_number=query_paths_args["current_block_number"], token_network_address=query_paths_args["token_network_address"], one_to_n_address=query_paths_args["one_to_n_address"], chain_id=query_paths_args["chain_id"], route_from=query_paths_args["route_from"], route_to=query_paths_args["route_to"], value=query_paths_args["value"], ) assert post_path.call_args == call( payload={ "from": to_checksum_address(query_paths_args["route_from"]), "to": to_checksum_address(query_paths_args["route_to"]), "value": query_paths_args["value"], "max_paths": query_paths_args["pfs_config"].max_paths, }, token_network_address=query_paths_args["token_network_address"], url=query_paths_args["pfs_config"].info.url, )
from raiden_contracts.utils.type_aliases import ChainID token_network_registry_address_test_default = TokenNetworkRegistryAddress( to_canonical_address("0xB9633dd9a9a71F22C933bF121d7a22008f66B908")) user_deposit_address_test_default = Address( to_canonical_address("0x8888888888888888888888888888888888888888")) pfs_payment_address_default = to_canonical_address( "0xB9633dd9a9a71F22C933bF121d7a22008f66B907") PFS_INFO = PFSInfo( url="my-pfs", price=TokenAmount(12), chain_id=ChainID(5), token_network_registry_address=token_network_registry_address_test_default, user_deposit_address=user_deposit_address_test_default, payment_address=pfs_payment_address_default, confirmed_block_number=BlockNumber(1), message="This is your favorite pathfinding service", operator="John Doe", version="0.0.3", ) def test_check_network_id_raises_with_mismatching_ids(): check_ethereum_network_id(ChainID(68), MockWeb3(68)) with pytest.raises(RaidenError): check_ethereum_network_id(ChainID(61), MockWeb3(68)) @pytest.mark.parametrize("netid", [1, 3, 4, 5, 627])
def test_mediated_transfer_calls_pfs(raiden_chain: List[App], token_addresses: List[TokenAddress]): app0, app1, app2 = raiden_chain token_address = token_addresses[0] chain_state = views.state_from_app(app0) token_network_registry_address = app0.raiden.default_registry.address token_network_address = views.get_token_network_address_by_token_address( chain_state, token_network_registry_address, token_address ) assert token_network_address, "Fixture token_addresses don't have correspoding token_network" with patch("raiden.routing.query_paths", return_value=([], None)) as patched: app0.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=PaymentAmount(10), target=TargetAddress(app1.raiden.address), identifier=PaymentID(1), secret=Secret(b"1" * 32), ) assert not patched.called # Setup PFS config app0.raiden.config.pfs_config = PFSConfig( info=PFSInfo( url="mock-address", chain_id=app0.raiden.rpc_client.chain_id, token_network_registry_address=token_network_registry_address, user_deposit_address=factories.make_address(), payment_address=factories.make_address(), confirmed_block_number=chain_state.block_number, message="", operator="", version="", price=TokenAmount(0), ), maximum_fee=TokenAmount(100), iou_timeout=BlockTimeout(100), max_paths=5, ) app0.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=PaymentAmount(11), target=TargetAddress(app2.raiden.address), identifier=PaymentID(2), secret=Secret(b"2" * 32), ) assert patched.call_count == 1 # Mediator should not re-query PFS locked_transfer = factories.create( factories.LockedTransferProperties( amount=TokenAmount(5), initiator=factories.HOP1, target=TargetAddress(app2.raiden.address), sender=factories.HOP1, pkey=factories.HOP1_KEY, token=token_address, canonical_identifier=factories.make_canonical_identifier( token_network_address=token_network_address ), ) ) app0.raiden.on_messages([locked_transfer]) assert patched.call_count == 1
routes=routes, block_number=factories.make_block_number(), ) return new_state, [address1, address2, address3, address4], channels PFS_CONFIG = PFSConfig( info=PFSInfo( url="abc", price=TokenAmount(12), chain_id=ChainID(42), token_network_registry_address=factories. make_token_network_registry_address(), user_deposit_address=factories.make_address(), payment_address=factories.make_address(), message="", operator="", version="", confirmed_block_number=BlockNumber(10), matrix_server="http://matrix.example.com", matrix_room_id="!room-id:matrix.example.com", ), maximum_fee=TokenAmount(100), iou_timeout=BlockTimeout(100), max_paths=5, ) CONFIG = {"pfs_config": PFS_CONFIG} PRIVKEY = b"privkeyprivkeyprivkeyprivkeypriv"
token_network_state=token_network_state, our_address=our_address, routes=routes, block_number=factories.make_block_number(), ) return new_state, [address1, address2, address3, address4], channels PFS_CONFIG = PFSConfig( info=PFSInfo( url="abc", price=TokenAmount(12), chain_id=ChainID(42), token_network_registry_address=factories. make_token_network_registry_address(), payment_address=factories.make_address(), message="", operator="", version="", ), maximum_fee=TokenAmount(100), iou_timeout=BlockNumber(100), max_paths=5, ) CONFIG = {"pfs_config": PFS_CONFIG} PRIVKEY = b"privkeyprivkeyprivkeyprivkeypriv" def get_best_routes_with_iou_request_mocked(
CONTRACT_TOKEN_NETWORK_REGISTRY, CONTRACT_USER_DEPOSIT, ) from raiden_contracts.utils.type_aliases import ChainID token_network_registry_address_test_default = TokenNetworkRegistryAddress( to_canonical_address("0xB9633dd9a9a71F22C933bF121d7a22008f66B908") ) pfs_payment_address_default = to_canonical_address("0xB9633dd9a9a71F22C933bF121d7a22008f66B907") PFS_INFO = PFSInfo( url="my-pfs", price=TokenAmount(12), chain_id=ChainID(42), token_network_registry_address=token_network_registry_address_test_default, payment_address=pfs_payment_address_default, message="This is your favorite pathfinding service", operator="John Doe", version="0.0.3", ) def test_check_network_id_raises_with_mismatching_ids(): check_ethereum_network_id(68, MockWeb3(68)) with pytest.raises(SystemExit): check_ethereum_network_id(61, MockWeb3(68)) @pytest.mark.parametrize("netid", [1, 3, 4, 42, 5, 627]) def test_setup_does_not_raise_with_matching_ids(netid):
our_address=our_address, routes=routes, block_number=factories.make_block_number(), ) return new_state, [address1, address2, address3, address4], channels PFS_CONFIG = PFSConfig( info=PFSInfo( url="abc", price=TokenAmount(12), chain_id=ChainID(42), token_network_registry_address=factories. make_token_network_registry_address(), user_deposit_address=factories.make_address(), payment_address=factories.make_address(), message="", operator="", version="", confirmed_block_number=BlockNumber(10), ), maximum_fee=TokenAmount(100), iou_timeout=BlockTimeout(100), max_paths=5, ) CONFIG = {"pfs_config": PFS_CONFIG} PRIVKEY = b"privkeyprivkeyprivkeyprivkeypriv"