示例#1
0
文件: app.py 项目: ninjai2018/raiden
    def __init__(
        self,
        config: typing.Dict,
        chain: BlockChainService,
        query_start_block: typing.BlockNumber,
        default_registry: TokenNetworkRegistry,
        default_secret_registry: SecretRegistry,
        transport,
        raiden_event_handler,
        message_handler,
        discovery: Discovery = None,
    ):
        raiden = RaidenService(
            chain=chain,
            query_start_block=query_start_block,
            default_registry=default_registry,
            default_secret_registry=default_secret_registry,
            private_key_bin=unhexlify(config['privatekey_hex']),
            transport=transport,
            raiden_event_handler=raiden_event_handler,
            message_handler=message_handler,
            config=config,
            discovery=discovery,
        )

        # Check if files with older versions of the DB exist, emit a warning
        db_base_path = os.path.dirname(config['database_path'])
        if older_db_files_exist(db_base_path):
            log.warning(
                'Older versions of the database exist in '
                f'{db_base_path}. Since a newer breaking version is introduced, '
                'it is advised that you leave all token networks before upgrading and '
                'then proceed with the upgrade.', )

        # check that the settlement timeout fits the limits of the contract
        invalid_settle_timeout = (
            config['settle_timeout'] <
            default_registry.settlement_timeout_min()
            or config['settle_timeout'] >
            default_registry.settlement_timeout_max()
            or config['settle_timeout'] < config['reveal_timeout'] * 2)
        if invalid_settle_timeout:
            raise InvalidSettleTimeout(
                ('Settlement timeout for Registry contract {} must '
                 'be in range [{}, {}], is {}').format(
                     to_checksum_address(default_registry.address),
                     default_registry.settlement_timeout_min(),
                     default_registry.settlement_timeout_max(),
                     config['settle_timeout'],
                 ), )

        self.config = config
        self.discovery = discovery
        self.raiden = raiden
        self.start_console = self.config['console']

        # raiden.ui.console:Console assumes that a services
        # attribute is available for auto-registration
        self.services = dict()
示例#2
0
def setup_testchain_and_raiden(smoketest_config, transport, matrix_server,
                               print_step):
    print_step('Starting Ethereum node')
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])
    port = ethereum_config['rpc']
    web3_client = Web3(HTTPProvider(f'http://0.0.0.0:{port}'))
    web3_client.middleware_stack.inject(geth_poa_middleware, layer=0)
    random_marker = hexlify(b'raiden').decode()
    privatekeys = []
    geth_wait_and_check(web3_client, privatekeys, random_marker)

    print_step('Deploying Raiden contracts')
    host = '0.0.0.0'
    client = JSONRPCClient(
        host,
        ethereum_config['rpc'],
        get_private_key(),
        web3=web3_client,
    )
    contract_addresses = deploy_smoketest_contracts(client, 627)
    token_contract = deploy_token(client)
    token = token_contract(1000, 0, 'TKN', 'TKN')
    registry = TokenNetworkRegistry(
        client, contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY])
    registry.add_token(to_canonical_address(token.contract.address))

    print_step('Setting up Raiden')
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_ENDPOINT_REGISTRY], ),
        registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], ),
        secret_registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_SECRET_REGISTRY], ),
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(port),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
        network_id='627',
        sync_check=False,
        transport=transport,
        matrix_server='http://localhost:8008'
        if matrix_server == 'auto' else matrix_server,
    )
    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    return dict(
        args=args,
        contract_addresses=contract_addresses,
        ethereum=ethereum,
        ethereum_config=ethereum_config,
        token=token,
    )
示例#3
0
文件: app.py 项目: AlphaX-IBS/raiden
    def __init__(
            self,
            config: typing.Dict,
            chain: BlockChainService,
            query_start_block: typing.BlockNumber,
            default_registry: TokenNetworkRegistry,
            default_secret_registry: SecretRegistry,
            transport,
            discovery: Discovery = None,
    ):
        self.config = config
        self.discovery = discovery

        # check that the settlement timeout fits the limits of the contract
        invalid_timeout = (
            config['settle_timeout'] < default_registry.settlement_timeout_min() or
            config['settle_timeout'] > default_registry.settlement_timeout_max()
        )
        if invalid_timeout:
            raise InvalidSettleTimeout(
                (
                    'Settlement timeout for Registry contract {} must '
                    'be in range [{}, {}], is {}'
                ).format(
                    to_checksum_address(default_registry.address),
                    default_registry.settlement_timeout_min(),
                    default_registry.settlement_timeout_max(),
                    config['settle_timeout'],
                ),
            )

        try:
            self.raiden = RaidenService(
                chain=chain,
                query_start_block=query_start_block,
                default_registry=default_registry,
                default_secret_registry=default_secret_registry,
                private_key_bin=unhexlify(config['privatekey_hex']),
                transport=transport,
                config=config,
                discovery=discovery,
            )
        except filelock.Timeout:
            pubkey = to_normalized_address(
                privatekey_to_address(unhexlify(self.config['privatekey_hex'])),
            )
            print(
                f'FATAL: Another Raiden instance already running for account {pubkey} on '
                f'network id {chain.network_id}',
            )
            sys.exit(1)
示例#4
0
    def __init__(
            self,
            config: typing.Dict,
            chain: BlockChainService,
            query_start_block: typing.BlockNumber,
            default_registry: TokenNetworkRegistry,
            default_secret_registry: SecretRegistry,
            transport,
            raiden_event_handler,
            message_handler,
            discovery: Discovery = None,
    ):
        raiden = RaidenService(
            chain=chain,
            query_start_block=query_start_block,
            default_registry=default_registry,
            default_secret_registry=default_secret_registry,
            transport=transport,
            raiden_event_handler=raiden_event_handler,
            message_handler=message_handler,
            config=config,
            discovery=discovery,
        )

        # check that the settlement timeout fits the limits of the contract
        invalid_settle_timeout = (
            config['settle_timeout'] < default_registry.settlement_timeout_min() or
            config['settle_timeout'] > default_registry.settlement_timeout_max() or
            config['settle_timeout'] < config['reveal_timeout'] * 2
        )
        if invalid_settle_timeout:
            raise InvalidSettleTimeout(
                (
                    'Settlement timeout for Registry contract {} must '
                    'be in range [{}, {}], is {}'
                ).format(
                    to_checksum_address(default_registry.address),
                    default_registry.settlement_timeout_min(),
                    default_registry.settlement_timeout_max(),
                    config['settle_timeout'],
                ),
            )

        self.config = config
        self.discovery = discovery
        self.raiden = raiden
        self.start_console = self.config['console']

        # raiden.ui.console:Console assumes that a services
        # attribute is available for auto-registration
        self.services = dict()
示例#5
0
    def __init__(
        self,
        config: Dict,
        chain: BlockChainService,
        default_registry: TokenNetworkRegistry,
        default_secret_registry: SecretRegistry,
        transport,
        discovery: Discovery = None,
    ):
        self.config = config
        self.discovery = discovery

        # check that the settlement timeout fits the limits of the contract
        invalid_timeout = (config['settle_timeout'] <
                           default_registry.settlement_timeout_min()
                           or config['settle_timeout'] >
                           default_registry.settlement_timeout_max())
        if invalid_timeout:
            raise InvalidSettleTimeout(
                ('Settlement timeout for Registry contract {} must '
                 'be in range [{}, {}], is {}').format(
                     to_checksum_address(default_registry.address),
                     default_registry.settlement_timeout_min(),
                     default_registry.settlement_timeout_max(),
                     config['settle_timeout'],
                 ), )

        try:
            self.raiden = RaidenService(
                chain,
                default_registry,
                default_secret_registry,
                unhexlify(config['privatekey_hex']),
                transport,
                config,
                discovery,
            )
        except filelock.Timeout:
            pubkey = to_normalized_address(
                privatekey_to_address(unhexlify(
                    self.config['privatekey_hex'])), )
            print(
                f'FATAL: Another Raiden instance already running for account {pubkey} on '
                f'network id {chain.network_id}', )
            sys.exit(1)
        self.start_console = self.config['console']

        # raiden.ui.console:Console assumes that a services
        # attribute is available for auto-registration
        self.services = dict()
示例#6
0
    def __init__(
        self,
        config: typing.Dict,
        chain: BlockChainService,
        query_start_block: typing.BlockNumber,
        default_registry: TokenNetworkRegistry,
        default_secret_registry: SecretRegistry,
        transport,
        discovery: Discovery = None,
    ):
        self.config = config
        self.discovery = discovery

        # check that the settlement timeout fits the limits of the contract
        invalid_timeout = (config['settle_timeout'] <
                           default_registry.settlement_timeout_min()
                           or config['settle_timeout'] >
                           default_registry.settlement_timeout_max())
        if invalid_timeout:
            raise InvalidSettleTimeout(
                ('Settlement timeout for Registry contract {} must '
                 'be in range [{}, {}], is {}').format(
                     to_checksum_address(default_registry.address),
                     default_registry.settlement_timeout_min(),
                     default_registry.settlement_timeout_max(),
                     config['settle_timeout'],
                 ), )

        try:
            self.raiden = RaidenService(
                chain=chain,
                query_start_block=query_start_block,
                default_registry=default_registry,
                default_secret_registry=default_secret_registry,
                private_key_bin=unhexlify(config['privatekey_hex']),
                transport=transport,
                config=config,
                discovery=discovery,
            )
        except filelock.Timeout:
            pubkey = to_normalized_address(
                privatekey_to_address(unhexlify(
                    self.config['privatekey_hex'])), )
            print(
                f'FATAL: Another Raiden instance already running for account {pubkey} on '
                f'network id {chain.network_id}', )
            sys.exit(1)
示例#7
0
def token_network_registry_proxy(deploy_client,
                                 token_network_registry_contract,
                                 contract_manager):
    return TokenNetworkRegistry(
        jsonrpc_client=deploy_client,
        registry_address=to_canonical_address(
            token_network_registry_contract.contract.address),
        contract_manager=contract_manager,
    )
示例#8
0
    def token_network_registry(self, address: Address) -> TokenNetworkRegistry:
        if not is_binary_address(address):
            raise ValueError('address must be a valid address')

        if address not in self.address_to_token_network_registry:
            self.address_to_token_network_registry[address] = TokenNetworkRegistry(
                self.client,
                address,
            )

        return self.address_to_token_network_registry[address]
    def token_network_registry(self, address: Address) -> TokenNetworkRegistry:
        if not is_binary_address(address):
            raise ValueError('address must be a valid address')

        with self._token_network_registry_creation_lock:
            if address not in self.address_to_token_network_registry:
                self.address_to_token_network_registry[address] = TokenNetworkRegistry(
                    jsonrpc_client=self.client,
                    registry_address=PaymentNetworkID(address),
                    contract_manager=self.contract_manager,
                )

        return self.address_to_token_network_registry[address]
示例#10
0
def setup_raiden(
    transport,
    matrix_server,
    print_step,
    contracts_version,
    testchain_setup,
):
    print_step('Deploying Raiden contracts')

    client = JSONRPCClient(testchain_setup['web3'],
                           get_private_key(testchain_setup['keystore']))
    contract_manager = ContractManager(
        contracts_precompiled_path(contracts_version), )

    contract_addresses = deploy_smoketest_contracts(
        client=client,
        chain_id=NETWORKNAME_TO_ID['smoketest'],
        contract_manager=contract_manager,
    )
    token = deploy_token(
        deploy_client=client,
        contract_manager=contract_manager,
        initial_amount=1000,
        decimals=0,
        token_name='TKN',
        token_symbol='TKN',
    )
    registry = TokenNetworkRegistry(
        jsonrpc_client=client,
        registry_address=contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
        contract_manager=contract_manager,
    )
    registry.add_token(
        token_address=to_canonical_address(token.contract.address),
        given_block_identifier='latest',
    )

    print_step('Setting up Raiden')

    endpoint_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_ENDPOINT_REGISTRY], )
    tokennetwork_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], )
    secret_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_SECRET_REGISTRY], )
    return {
        'args': {
            'address':
            to_checksum_address(TEST_ACCOUNT_ADDRESS),
            'datadir':
            testchain_setup['keystore'],
            'endpoint_registry_contract_address':
            endpoint_registry_contract_address,
            'eth_rpc_endpoint':
            testchain_setup['eth_rpc_endpoint'],
            'gas_price':
            'fast',
            'keystore_path':
            testchain_setup['keystore'],
            'matrix_server':
            matrix_server,
            'network_id':
            str(NETWORKNAME_TO_ID['smoketest']),
            'password_file':
            click.File()(os.path.join(testchain_setup['base_datadir'], 'pw')),
            'tokennetwork_registry_contract_address':
            tokennetwork_registry_contract_address,
            'secret_registry_contract_address':
            secret_registry_contract_address,
            'sync_check':
            False,
            'transport':
            transport,
        },
        'contract_addresses': contract_addresses,
        'ethereum': testchain_setup['processes_list'],
        'token': token,
    }
示例#11
0
def setup_testchain_and_raiden(transport, matrix_server, print_step):
    print_step('Starting Ethereum node')

    ensure_executable('geth')

    free_port = get_free_port('127.0.0.1', 27854)
    rpc_port = next(free_port)
    p2p_port = next(free_port)
    base_datadir = os.environ['RST_DATADIR']

    description = GethNodeDescription(
        private_key=TEST_PRIVKEY,
        rpc_port=rpc_port,
        p2p_port=p2p_port,
        miner=True,
    )

    eth_rpc_endpoint = f'http://127.0.0.1:{rpc_port}'
    web3 = Web3(HTTPProvider(endpoint_uri=eth_rpc_endpoint))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)

    config = geth_node_config(
        description.private_key,
        description.p2p_port,
        description.rpc_port,
    )

    config.update({
        'unlock': 0,
        'mine': True,
        'password': os.path.join(base_datadir, 'pw'),
    })

    nodes_configuration = [config]
    geth_node_config_set_bootnodes(nodes_configuration)
    keystore = os.path.join(geth_node_to_datadir(config, base_datadir),
                            'keystore')

    logdir = os.path.join(base_datadir, 'logs')

    processes_list = geth_run_nodes(
        geth_nodes=[description],
        nodes_configuration=nodes_configuration,
        base_datadir=base_datadir,
        genesis_file=os.path.join(get_project_root(),
                                  'smoketest_genesis.json'),
        chain_id=NETWORKNAME_TO_ID['smoketest'],
        verbosity=0,
        logdir=logdir,
    )

    try:
        # the marker is hardcoded in the genesis file
        random_marker = remove_0x_prefix(encode_hex(b'raiden'))
        geth_wait_and_check(web3, [], random_marker)

        for process in processes_list:
            process.poll()

            if process.returncode is not None:
                raise ValueError(
                    f'geth process failed with exit code {process.returncode}')

    except (ValueError, RuntimeError) as e:
        # If geth_wait_and_check or the above loop throw an exception make sure
        # we don't end up with a rogue geth process running in the background
        for process in processes_list:
            process.terminate()
        raise e

    print_step('Deploying Raiden contracts')

    client = JSONRPCClient(web3, get_private_key(keystore))
    # for smoketest use the precompiled contracts
    contract_manager = ContractManager(contracts_precompiled_path())

    contract_addresses = deploy_smoketest_contracts(
        client=client,
        chain_id=NETWORKNAME_TO_ID['smoketest'],
        contract_manager=contract_manager,
    )
    token = deploy_token(
        deploy_client=client,
        contract_manager=contract_manager,
        initial_amount=1000,
        decimals=0,
        token_name='TKN',
        token_symbol='TKN',
    )
    registry = TokenNetworkRegistry(
        jsonrpc_client=client,
        registry_address=contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
        contract_manager=contract_manager,
    )
    registry.add_token(to_canonical_address(token.contract.address))

    print_step('Setting up Raiden')

    if matrix_server == 'auto':
        matrix_server = 'http://localhost:8008'

    endpoint_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_ENDPOINT_REGISTRY], )
    tokennetwork_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], )
    secret_registry_contract_address = to_checksum_address(
        contract_addresses[CONTRACT_SECRET_REGISTRY], )
    return {
        'args': {
            'address': to_checksum_address(TEST_ACCOUNT_ADDRESS),
            'datadir': keystore,
            'endpoint_registry_contract_address':
            endpoint_registry_contract_address,
            'eth_rpc_endpoint': eth_rpc_endpoint,
            'gas_price': 'fast',
            'keystore_path': keystore,
            'matrix_server': matrix_server,
            'network_id': str(NETWORKNAME_TO_ID['smoketest']),
            'password_file': click.File()(os.path.join(base_datadir, 'pw')),
            'tokennetwork_registry_contract_address':
            tokennetwork_registry_contract_address,
            'secret_registry_contract_address':
            secret_registry_contract_address,
            'sync_check': False,
            'transport': transport,
        },
        'contract_addresses': contract_addresses,
        'ethereum': processes_list,
        'token': token,
    }
示例#12
0
def smoketest(ctx, debug, local_matrix, **kwargs):  # pylint: disable=unused-argument
    """ Test, that the raiden installation is sane. """
    import binascii
    from web3 import Web3, HTTPProvider
    from web3.middleware import geth_poa_middleware
    from raiden.api.python import RaidenAPI
    from raiden.tests.utils.geth import geth_wait_and_check
    from raiden.tests.integration.contracts.fixtures.contracts import deploy_token
    from raiden.tests.utils.smoketest import (
        TEST_PARTNER_ADDRESS,
        TEST_DEPOSIT_AMOUNT,
        deploy_smoketest_contracts,
        get_private_key,
        load_smoketest_config,
        start_ethereum,
        run_smoketests,
    )

    report_file = tempfile.mktemp(suffix='.log')
    configure_logging({'': 'DEBUG'}, log_file=report_file)

    def append_report(subject, data):
        with open(report_file, 'a', encoding='UTF-8') as handler:
            handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}')
            if data is not None:
                if isinstance(data, bytes):
                    data = data.decode()
                handler.writelines([data + os.linesep])

    append_report('Raiden version', json.dumps(get_system_spec()))
    append_report('Raiden log', None)

    step_count = 7
    if ctx.parent.params['transport'] == 'matrix':
        step_count = 8
    step = 0

    def print_step(description, error=False):
        nonlocal step
        step += 1
        click.echo(
            '{} {}'.format(
                click.style(f'[{step}/{step_count}]', fg='blue'),
                click.style(description, fg='green' if not error else 'red'),
            ), )

    print_step('Getting smoketest configuration')
    smoketest_config = load_smoketest_config()
    if not smoketest_config:
        append_report(
            'Smoketest configuration',
            'Could not load the smoketest genesis configuration file.',
        )

    print_step('Starting Ethereum node')
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])
    port = ethereum_config['rpc']
    web3_client = Web3(HTTPProvider(f'http://0.0.0.0:{port}'))
    web3_client.middleware_stack.inject(geth_poa_middleware, layer=0)
    random_marker = binascii.hexlify(b'raiden').decode()
    privatekeys = []
    geth_wait_and_check(web3_client, privatekeys, random_marker)

    print_step('Deploying Raiden contracts')
    host = '0.0.0.0'
    client = JSONRPCClient(
        host,
        ethereum_config['rpc'],
        get_private_key(),
        web3=web3_client,
    )
    contract_addresses = deploy_smoketest_contracts(client, 627)

    token_contract = deploy_token(client)
    token = token_contract(1000, 0, 'TKN', 'TKN')

    registry = TokenNetworkRegistry(
        client, contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY])

    registry.add_token(to_canonical_address(token.contract.address))

    print_step('Setting up Raiden')
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_ENDPOINT_REGISTRY], ),
        registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], ),
        secret_registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_SECRET_REGISTRY], ),
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(port),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
        network_id='627',
        transport=ctx.parent.params['transport'],
        matrix_server='http://localhost:8008'
        if ctx.parent.params['matrix_server'] == 'auto' else
        ctx.parent.params['matrix_server'],
    )
    smoketest_config['transport'] = args['transport']
    for option_ in app.params:
        if option_.name in args.keys():
            args[option_.name] = option_.process_value(ctx, args[option_.name])
        else:
            args[option_.name] = option_.default

    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    port = next(get_free_port('127.0.0.1', 5001))
    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    args['api_address'] = 'localhost:' + str(port)
    args['sync_check'] = False

    def _run_smoketest():
        print_step('Starting Raiden')

        # invoke the raiden app
        app_ = ctx.invoke(app, **args)

        raiden_api = RaidenAPI(app_.raiden)
        rest_api = RestAPI(raiden_api)
        api_server = APIServer(rest_api)
        (api_host, api_port) = split_endpoint(args['api_address'])
        api_server.start(api_host, api_port)

        raiden_api.channel_open(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
            to_canonical_address(token.contract.address),
            to_canonical_address(TEST_PARTNER_ADDRESS),
            None,
            None,
        )
        raiden_api.set_total_channel_deposit(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
            to_canonical_address(token.contract.address),
            to_canonical_address(TEST_PARTNER_ADDRESS),
            TEST_DEPOSIT_AMOUNT,
        )

        smoketest_config['contracts'][
            'registry_address'] = to_checksum_address(
                contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY], )
        smoketest_config['contracts'][
            'secret_registry_address'] = to_checksum_address(
                contract_addresses[CONTRACT_SECRET_REGISTRY], )
        smoketest_config['contracts'][
            'discovery_address'] = to_checksum_address(
                contract_addresses[CONTRACT_ENDPOINT_REGISTRY], )
        smoketest_config['contracts']['token_address'] = to_checksum_address(
            token.contract.address, )

        success = False
        try:
            print_step('Running smoketest')
            error = run_smoketests(app_.raiden, smoketest_config, debug=debug)
            if error is not None:
                append_report('Smoketest assertion error', error)
            else:
                success = True
        finally:
            app_.stop()
            ethereum.send_signal(2)

            err, out = ethereum.communicate()
            append_report('Ethereum init stdout',
                          ethereum_config['init_log_out'].decode('utf-8'))
            append_report('Ethereum init stderr',
                          ethereum_config['init_log_err'].decode('utf-8'))
            append_report('Ethereum stdout', out)
            append_report('Ethereum stderr', err)
            append_report('Smoketest configuration',
                          json.dumps(smoketest_config))
        if success:
            print_step(
                f'Smoketest successful, report was written to {report_file}')
        else:
            print_step(
                f'Smoketest had errors, report was written to {report_file}',
                error=True)
        return success

    if args['transport'] == 'udp':
        with SocketFactory('127.0.0.1', port,
                           strategy='none') as mapped_socket:
            args['mapped_socket'] = mapped_socket
            success = _run_smoketest()
    elif args['transport'] == 'matrix' and local_matrix.lower() != 'none':
        args['mapped_socket'] = None
        print_step('Starting Matrix transport')
        try:
            with HTTPExecutor(
                    local_matrix,
                    status=r'^[24]\d\d$',
                    url=urljoin(args['matrix_server'],
                                '/_matrix/client/versions'),
                    shell=True,
            ):
                args['extra_config'] = {
                    'matrix': {
                        'discovery_room': {
                            'server': 'matrix.local.raiden'
                        },
                        'server_name': 'matrix.local.raiden',
                    },
                }
                success = _run_smoketest()
        except (PermissionError, ProcessExitedWithError):
            append_report('Matrix server start exception',
                          traceback.format_exc())
            print_step(
                f'Error during smoketest setup, report was written to {report_file}',
                error=True,
            )
            success = False
    elif args['transport'] == 'matrix' and local_matrix.lower() == "none":
        args['mapped_socket'] = None
        success = _run_smoketest()
    else:
        # Shouldn't happen
        raise RuntimeError(f"Invalid transport type '{args['transport']}'")

    if not success:
        sys.exit(1)
示例#13
0
def token_network_registry_proxy(deploy_client, token_network_registry_contract):
    return TokenNetworkRegistry(
        deploy_client,
        to_canonical_address(token_network_registry_contract.contract.address),
    )
示例#14
0
文件: cli.py 项目: AlphaX-IBS/raiden
def smoketest(ctx, debug, local_matrix, **kwargs):  # pylint: disable=unused-argument
    """ Test, that the raiden installation is sane. """
    import binascii
    from web3 import Web3, HTTPProvider
    from web3.middleware import geth_poa_middleware
    from raiden.api.python import RaidenAPI
    from raiden.tests.utils.geth import geth_wait_and_check
    from raiden.tests.integration.contracts.fixtures.contracts import deploy_token
    from raiden.tests.utils.smoketest import (
        TEST_PARTNER_ADDRESS,
        TEST_DEPOSIT_AMOUNT,
        deploy_smoketest_contracts,
        get_private_key,
        load_smoketest_config,
        start_ethereum,
        run_smoketests,
    )

    report_file = tempfile.mktemp(suffix='.log')
    configure_logging({'': 'DEBUG'}, log_file=report_file)

    def append_report(subject, data):
        with open(report_file, 'a', encoding='UTF-8') as handler:
            handler.write(f'{f" {subject.upper()} ":=^80}{os.linesep}')
            if data is not None:
                if isinstance(data, bytes):
                    data = data.decode()
                handler.writelines([data + os.linesep])

    append_report('Raiden version', json.dumps(get_system_spec()))
    append_report('Raiden log', None)

    step_count = 7
    if ctx.parent.params['transport'] == 'matrix':
        step_count = 8
    step = 0

    def print_step(description, error=False):
        nonlocal step
        step += 1
        click.echo(
            '{} {}'.format(
                click.style(f'[{step}/{step_count}]', fg='blue'),
                click.style(description, fg='green' if not error else 'red'),
            ),
        )

    print_step('Getting smoketest configuration')
    smoketest_config = load_smoketest_config()
    if not smoketest_config:
        append_report(
            'Smoketest configuration',
            'Could not load the smoketest genesis configuration file.',
        )

    print_step('Starting Ethereum node')
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])
    port = ethereum_config['rpc']
    web3_client = Web3(HTTPProvider(f'http://0.0.0.0:{port}'))
    web3_client.middleware_stack.inject(geth_poa_middleware, layer=0)
    random_marker = binascii.hexlify(b'raiden').decode()
    privatekeys = []
    geth_wait_and_check(web3_client, privatekeys, random_marker)

    print_step('Deploying Raiden contracts')
    host = '0.0.0.0'
    client = JSONRPCClient(
        host,
        ethereum_config['rpc'],
        get_private_key(),
        web3=web3_client,
    )
    contract_addresses = deploy_smoketest_contracts(client, 627)

    token_contract = deploy_token(client)
    token = token_contract(1000, 0, 'TKN', 'TKN')

    registry = TokenNetworkRegistry(client, contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY])

    registry.add_token(to_canonical_address(token.contract.address))

    print_step('Setting up Raiden')
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_ENDPOINT_REGISTRY],
        ),
        registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
        ),
        secret_registry_contract_address=to_checksum_address(
            contract_addresses[CONTRACT_SECRET_REGISTRY],
        ),
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(port),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
        network_id='627',
        transport=ctx.parent.params['transport'],
        matrix_server='http://localhost:8008'
                      if ctx.parent.params['matrix_server'] == 'auto'
                      else ctx.parent.params['matrix_server'],
    )
    smoketest_config['transport'] = args['transport']
    for option_ in app.params:
        if option_.name in args.keys():
            args[option_.name] = option_.process_value(ctx, args[option_.name])
        else:
            args[option_.name] = option_.default

    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    port = next(get_free_port('127.0.0.1', 5001))
    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    args['api_address'] = 'localhost:' + str(port)
    args['sync_check'] = False

    def _run_smoketest():
        print_step('Starting Raiden')

        # invoke the raiden app
        app_ = ctx.invoke(app, **args)

        raiden_api = RaidenAPI(app_.raiden)
        rest_api = RestAPI(raiden_api)
        api_server = APIServer(rest_api)
        (api_host, api_port) = split_endpoint(args['api_address'])
        api_server.start(api_host, api_port)

        raiden_api.channel_open(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
            to_canonical_address(token.contract.address),
            to_canonical_address(TEST_PARTNER_ADDRESS),
            None,
            None,
        )
        raiden_api.set_total_channel_deposit(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
            to_canonical_address(token.contract.address),
            to_canonical_address(TEST_PARTNER_ADDRESS),
            TEST_DEPOSIT_AMOUNT,
        )

        smoketest_config['contracts']['registry_address'] = to_checksum_address(
            contract_addresses[CONTRACT_TOKEN_NETWORK_REGISTRY],
        )
        smoketest_config['contracts']['secret_registry_address'] = to_checksum_address(
            contract_addresses[CONTRACT_SECRET_REGISTRY],
        )
        smoketest_config['contracts']['discovery_address'] = to_checksum_address(
            contract_addresses[CONTRACT_ENDPOINT_REGISTRY],
        )
        smoketest_config['contracts']['token_address'] = to_checksum_address(
            token.contract.address,
        )

        success = False
        try:
            print_step('Running smoketest')
            error = run_smoketests(app_.raiden, smoketest_config, debug=debug)
            if error is not None:
                append_report('Smoketest assertion error', error)
            else:
                success = True
        finally:
            app_.stop()
            ethereum.send_signal(2)

            err, out = ethereum.communicate()
            append_report('Ethereum init stdout', ethereum_config['init_log_out'].decode('utf-8'))
            append_report('Ethereum init stderr', ethereum_config['init_log_err'].decode('utf-8'))
            append_report('Ethereum stdout', out)
            append_report('Ethereum stderr', err)
            append_report('Smoketest configuration', json.dumps(smoketest_config))
        if success:
            print_step(f'Smoketest successful, report was written to {report_file}')
        else:
            print_step(f'Smoketest had errors, report was written to {report_file}', error=True)
        return success

    if args['transport'] == 'udp':
        with SocketFactory('127.0.0.1', port, strategy='none') as mapped_socket:
            args['mapped_socket'] = mapped_socket
            success = _run_smoketest()
    elif args['transport'] == 'matrix' and local_matrix.lower() != 'none':
        args['mapped_socket'] = None
        print_step('Starting Matrix transport')
        try:
            with HTTPExecutor(
                local_matrix,
                status=r'^[24]\d\d$',
                url=urljoin(args['matrix_server'], '/_matrix/client/versions'),
                shell=True,
            ):
                args['extra_config'] = {
                    'matrix': {
                        'discovery_room': {'server': 'matrix.local.raiden'},
                        'server_name': 'matrix.local.raiden',
                    },
                }
                success = _run_smoketest()
        except (PermissionError, ProcessExitedWithError):
            append_report('Matrix server start exception', traceback.format_exc())
            print_step(
                f'Error during smoketest setup, report was written to {report_file}',
                error=True,
            )
            success = False
    elif args['transport'] == 'matrix' and local_matrix.lower() == "none":
        args['mapped_socket'] = None
        success = _run_smoketest()
    else:
        # Shouldn't happen
        raise RuntimeError(f"Invalid transport type '{args['transport']}'")

    if not success:
        sys.exit(1)
示例#15
0
文件: app.py 项目: bokkypoobah/raiden
    def __init__(
        self,
        config: typing.Dict,
        chain: BlockChainService,
        query_start_block: typing.BlockNumber,
        default_registry: TokenNetworkRegistry,
        default_secret_registry: SecretRegistry,
        transport,
        raiden_event_handler,
        message_handler,
        discovery: Discovery = None,
    ):
        raiden = RaidenService(
            chain=chain,
            query_start_block=query_start_block,
            default_registry=default_registry,
            default_secret_registry=default_secret_registry,
            private_key_bin=decode_hex(config['privatekey_hex']),
            transport=transport,
            raiden_event_handler=raiden_event_handler,
            message_handler=message_handler,
            config=config,
            discovery=discovery,
        )

        # Check if files with older versions of the DB exist, emit a warning
        db_base_path = os.path.dirname(config['database_path'])
        old_version_path = older_db_file(db_base_path)
        if old_version_path:
            old_version_file = os.path.basename(old_version_path)
            raise RuntimeError(
                f'A database file for a previous version of Raiden exists '
                f'{old_version_path}. Because the new version of Raiden '
                f'introduces changes which break compatibility with the old '
                f'database, a new database is necessary. The new database '
                f'file will be created automatically for you, but as a side effect all '
                f'previous data will be unavailable. The only way to proceed '
                f'without the risk of losing funds is to leave all token networks '
                f'and *make a backup* of the existing database. Please, *after* all the '
                f'existing channels have been settled, make sure to make a backup by '
                f'renaming {old_version_file} to {old_version_file}_backup. Then the new '
                f'version of Raiden can be used.', )

        # check that the settlement timeout fits the limits of the contract
        invalid_settle_timeout = (
            config['settle_timeout'] <
            default_registry.settlement_timeout_min()
            or config['settle_timeout'] >
            default_registry.settlement_timeout_max()
            or config['settle_timeout'] < config['reveal_timeout'] * 2)
        if invalid_settle_timeout:
            raise InvalidSettleTimeout(
                ('Settlement timeout for Registry contract {} must '
                 'be in range [{}, {}], is {}').format(
                     to_checksum_address(default_registry.address),
                     default_registry.settlement_timeout_min(),
                     default_registry.settlement_timeout_max(),
                     config['settle_timeout'],
                 ), )

        self.config = config
        self.discovery = discovery
        self.raiden = raiden
        self.start_console = self.config['console']

        # raiden.ui.console:Console assumes that a services
        # attribute is available for auto-registration
        self.services = dict()
示例#16
0
文件: app.py 项目: hackaugusto/raiden
    def __init__(
            self,
            config: typing.Dict,
            chain: BlockChainService,
            query_start_block: typing.BlockNumber,
            default_registry: TokenNetworkRegistry,
            default_secret_registry: SecretRegistry,
            transport,
            raiden_event_handler,
            message_handler,
            discovery: Discovery = None,
    ):
        raiden = RaidenService(
            chain=chain,
            query_start_block=query_start_block,
            default_registry=default_registry,
            default_secret_registry=default_secret_registry,
            private_key_bin=decode_hex(config['privatekey_hex']),
            transport=transport,
            raiden_event_handler=raiden_event_handler,
            message_handler=message_handler,
            config=config,
            discovery=discovery,
        )

        # Check if files with older versions of the DB exist, emit a warning
        db_base_path = os.path.dirname(config['database_path'])
        old_version_path = older_db_file(db_base_path)
        if old_version_path:
            old_version_file = os.path.basename(old_version_path)
            raise RuntimeError(
                f'A database file for a previous version of Raiden exists '
                f'{old_version_path}. Because the new version of Raiden '
                f'introduces changes which break compatibility with the old '
                f'database, a new database is necessary. The new database '
                f'file will be created automatically for you, but as a side effect all '
                f'previous data will be unavailable. The only way to proceed '
                f'without the risk of losing funds is to leave all token networks '
                f'and *make a backup* of the existing database. Please, *after* all the '
                f'existing channels have been settled, make sure to make a backup by '
                f'renaming {old_version_file} to {old_version_file}_backup. Then the new '
                f'version of Raiden can be used.',
            )

        # check that the settlement timeout fits the limits of the contract
        invalid_settle_timeout = (
            config['settle_timeout'] < default_registry.settlement_timeout_min() or
            config['settle_timeout'] > default_registry.settlement_timeout_max() or
            config['settle_timeout'] < config['reveal_timeout'] * 2
        )
        if invalid_settle_timeout:
            raise InvalidSettleTimeout(
                (
                    'Settlement timeout for Registry contract {} must '
                    'be in range [{}, {}], is {}'
                ).format(
                    to_checksum_address(default_registry.address),
                    default_registry.settlement_timeout_min(),
                    default_registry.settlement_timeout_max(),
                    config['settle_timeout'],
                ),
            )

        self.config = config
        self.discovery = discovery
        self.raiden = raiden
        self.start_console = self.config['console']

        # raiden.ui.console:Console assumes that a services
        # attribute is available for auto-registration
        self.services = dict()