def deploy_decoder_tester(asset_address, address1, address2, settle_timeout):
    state = tester.state(num_accounts=1)
    # make sure we are on HOMESTEAD
    state.block.number = 1150001
    nettingchannel_lib = state.abi_contract(
        None,
        path=os.path.join(get_project_root(), "smart_contracts", "NettingChannelLibrary.sol"),
        language='solidity'
    )
    state.mine(number_of_blocks=1)
    decode_tester = state.abi_contract(
        None,
        path=get_test_contract_path("DecoderTester.sol"),
        language='solidity',
        libraries={
            'NettingChannelLibrary': nettingchannel_lib.address.encode('hex')
        },
        constructor_parameters=(
            asset_address,
            address1,
            address2,
            settle_timeout
        ),
        extra_args="raiden={}".format(os.path.join(get_project_root(), "smart_contracts"))
    )
    state.mine(number_of_blocks=1)

    return decode_tester
Пример #2
0
def load_or_create_smoketest_config():
    # get the contract and compiler (if available) versions
    versions = dict()
    for file in os.listdir(get_contract_path('')):
        if file.endswith('.sol'):
            versions[file] = contract_checksum(get_contract_path(file))
    # if solc is available, record its version, too
    if get_solidity() is not None:
        solc_version_out, _ = subprocess.Popen(
            [get_solidity().compiler_available(), '--version'],
            stdout=subprocess.PIPE).communicate()
        versions['solc'] = solc_version_out.split()[-1]

    smoketest_config_path = os.path.join(get_project_root(),
                                         'smoketest_config.json')
    # try to load an existing smoketest genesis config
    smoketest_config = dict()
    if os.path.exists(smoketest_config_path):
        with open(smoketest_config_path) as handler:
            smoketest_config = json.load(handler)
        # if the file versions still fit, return the genesis config (ignore solc if not available)
        if all(versions[key] == smoketest_config['versions'][key]
               for key in versions.keys()):
            return smoketest_config

    # something did not fit -- we will create the genesis
    smoketest_config['versions'] = versions
    raiden_config, smoketest_genesis = complete_genesis()
    smoketest_config['genesis'] = smoketest_genesis
    smoketest_config.update(raiden_config)
    with open(os.path.join(get_project_root(), 'smoketest_config.json'),
              'w') as handler:
        json.dump(smoketest_config, handler)
    return smoketest_config
Пример #3
0
def deploy_decoder_tester(tester_state, token_address, address1, address2,
                          settle_timeout):
    netting_chnanel_library_path = os.path.join(
        get_project_root(),
        'smart_contracts',
        'NettingChannelLibrary.sol',
    )

    nettingchannel_lib = tester_state.abi_contract(
        None, path=netting_chnanel_library_path, language='solidity')
    tester_state.mine(number_of_blocks=1)
    decode_tester = tester_state.abi_contract(
        None,
        path=get_test_contract_path('DecoderTester.sol'),
        language='solidity',
        libraries={
            'NettingChannelLibrary': nettingchannel_lib.address.encode('hex')
        },
        constructor_parameters=(token_address, address1, address2,
                                settle_timeout),
        extra_args='raiden={}'.format(
            os.path.join(get_project_root(), 'smart_contracts')))
    tester_state.mine(number_of_blocks=1)

    return decode_tester
def deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address):
    contracts_path = os.path.join(get_project_root(), 'smart_contracts')
    raiden_remap = 'raiden={}'.format(contracts_path)

    contract_libraries = {
        'NettingChannelLibrary': hexlify(tester_nettingchannel_library_address),
    }

    auxiliary_tester_compiled = _solidity.compile_contract(
        get_relative_contract(__file__, 'AuxiliaryTester.sol'),
        'AuxiliaryTester',
        contract_libraries,
        extra_args=raiden_remap
    )
    auxiliary_tester_address = tester_chain.contract(
        auxiliary_tester_compiled['bin'],
        language='evm',
        sender=tester.k0
    )
    auxiliary = tester.ABIContract(
        tester_chain,
        auxiliary_tester_compiled['abi'],
        auxiliary_tester_address
    )
    tester_chain.mine(number_of_blocks=1)

    return auxiliary
Пример #5
0
def get_test_contract_path(contract_name):
    contract_path = os.path.join(
        get_project_root(),
        'tests',
        'smart_contracts',
        contract_name
    )
    return os.path.realpath(contract_path)
Пример #6
0
def load_smoketest_config():
    smoketest_config_path = os.path.join(get_project_root(), 'smoketest_config.json')

    # try to load the existing smoketest genesis config
    if os.path.exists(smoketest_config_path):
        with open(smoketest_config_path) as handler:
            return json.load(handler)

    return None
Пример #7
0
def load_smoketest_config():
    smoketest_config_path = os.path.join(get_project_root(), 'smoketest_config.json')

    # try to load the existing smoketest genesis config
    if os.path.exists(smoketest_config_path):
        with open(smoketest_config_path) as handler:
            return json.load(handler)

    return None
Пример #8
0
def test_keystore():
    keystore = os.path.join(get_project_root(), 'tests', 'test_files')
    # Create inaccessible keyfile
    inaccessible_file = os.path.join(keystore, KEYFILE_INACCESSIBLE)
    if not os.path.exists(inaccessible_file):
        open(inaccessible_file, 'w').close()
    os.chmod(inaccessible_file, 0)
    yield keystore
    # Cleanup to leave no undeletable files behind
    os.chmod(inaccessible_file, 0o600)
    os.unlink(inaccessible_file)
Пример #9
0
def test_keystore():
    keystore = os.path.join(get_project_root(), 'tests', 'test_files')
    # Create inaccessible keyfile
    inaccessible_file = os.path.join(keystore, KEYFILE_INACCESSIBLE)
    if not os.path.exists(inaccessible_file):
        open(inaccessible_file, 'w').close()
    os.chmod(inaccessible_file, 0)
    yield keystore
    # Cleanup to leave no undeletable files behind
    os.chmod(inaccessible_file, 0o600)
    os.unlink(inaccessible_file)
Пример #10
0
def load_or_create_smoketest_config():
    # get the contract and compiler (if available) versions
    versions = dict()
    for file in os.listdir(get_contract_path('')):
        if file.endswith('.sol'):
            versions[file] = contract_checksum(get_contract_path(file))
    # if solc is available, record its version, too
    if get_solidity() is not None:
        solc_version_out, _ = subprocess.Popen(
            [get_solidity().compiler_available(), '--version'],
            stdout=subprocess.PIPE
        ).communicate()
        versions['solc'] = solc_version_out.split()[-1].decode()

    smoketest_config_path = os.path.join(
        get_project_root(),
        'smoketest_config.json'
    )
    # try to load an existing smoketest genesis config
    smoketest_config = dict()
    if os.path.exists(smoketest_config_path):
        with open(smoketest_config_path) as handler:
            smoketest_config = json.load(handler)
        # if the file versions still fit, return the genesis config (ignore solc if not available)
        config_matches = [
            versions[key] == smoketest_config['versions'][key]
            for key in versions.keys()
        ]
        if all(config_matches):
            return smoketest_config

    # something did not fit -- we will create the genesis
    smoketest_config['versions'] = versions
    raiden_config, smoketest_genesis = complete_genesis()
    smoketest_config['genesis'] = smoketest_genesis
    smoketest_config.update(raiden_config)
    with open(os.path.join(get_project_root(), 'smoketest_config.json'), 'w') as handler:
        json.dump(smoketest_config, handler)
    return smoketest_config
def deploy_decoder_tester(tester_state, tester_nettingchannel_library_address):
    contracts_path = os.path.join(get_project_root(), 'smart_contracts')
    raiden_remap = 'raiden={}'.format(contracts_path)

    decoder = tester_state.abi_contract(
        None,
        path=get_relative_contract(__file__, 'DecoderTester.sol'),
        language='solidity',
        libraries={'NettingChannelLibrary': tester_nettingchannel_library_address.encode('hex')},
        extra_args=raiden_remap,
    )
    tester_state.mine(number_of_blocks=1)

    return decoder
Пример #12
0
def deploy_decoder_tester(asset_address, address1, address2, settle_timeout):
    state = tester.state(num_accounts=1)
    # make sure we are on HOMESTEAD
    state.block.number = 1150001
    nettingchannel_lib = state.abi_contract(
        None,
        path=os.path.join(get_project_root(), "smart_contracts",
                          "NettingChannelLibrary.sol"),
        language='solidity')
    state.mine(number_of_blocks=1)
    decode_tester = state.abi_contract(
        None,
        path=get_test_contract_path("DecoderTester.sol"),
        language='solidity',
        libraries={
            'NettingChannelLibrary': nettingchannel_lib.address.encode('hex')
        },
        constructor_parameters=(asset_address, address1, address2,
                                settle_timeout),
        extra_args="raiden={}".format(
            os.path.join(get_project_root(), "smart_contracts")))
    state.mine(number_of_blocks=1)

    return decode_tester
Пример #13
0
def setup_testchain(print_step):
    print_step('Starting Ethereum node')

    ensure_executable('geth')

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

    description = EthNodeDescription(
        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 = eth_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]
    eth_node_config_set_bootnodes(nodes_configuration)
    keystore = os.path.join(eth_node_to_datadir(config, base_datadir),
                            'keystore')

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

    processes_list = eth_run_nodes(
        eth_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'))
        eth_wait_and_check(
            web3=web3,
            accounts_addresses=[],
            random_marker=random_marker,
            processes_list=processes_list,
        )
    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

    return dict(
        base_datadir=base_datadir,
        eth_rpc_endpoint=eth_rpc_endpoint,
        keystore=keystore,
        processes_list=processes_list,
        web3=web3,
    )
Пример #14
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,
    }
Пример #15
0
# -*- coding: utf-8 -*-
import os
import pytest

from ethereum.utils import encode_hex
from raiden.accounts import AccountManager
from raiden.utils import get_project_root

test_keystore = os.path.join(get_project_root(), 'tests', 'test_files')


def test_get_accounts():
    account_manager = AccountManager(test_keystore)
    expected_accounts = {
        '0d5a0e4fece4b84365b9b8dba6e6d41348c73645': os.path.join(
            test_keystore,
            'UTC--2016-10-26T16-55-53.551024336Z--0d5a0e4fece4b84365b9b8dba6e6d41348c73645'
        ),
        '3593403033d18b82f7b4a0f18e1ed24623d23b20': os.path.join(
            test_keystore,
            'valid_keystorefile_with_unexpected_name'
        )
    }
    assert expected_accounts == account_manager.accounts


def test_get_account_in_keystore():
    account_manager = AccountManager(test_keystore)
    assert account_manager.address_in_keystore('0d5a0e4fece4b84365b9b8dba6e6d41348c73645')
    assert account_manager.address_in_keystore('0x0d5a0e4fece4b84365b9b8dba6e6d41348c73645')
    assert account_manager.address_in_keystore('3593403033d18b82f7b4a0f18e1ed24623d23b20')