示例#1
0
def test_deploy_script(
    web3,
    contracts_manager,
    utils_contract,
    faucet_private_key,
    faucet_address,
    get_random_privkey,
):
    # normal deployment
    gas_limit = 5900000
    deployer = ContractDeployer(
        web3=web3,
        private_key=faucet_private_key,
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
    )
    res = deploy_raiden_contracts(deployer, )
    assert CONTRACT_ENDPOINT_REGISTRY in res
    assert CONTRACT_TOKEN_NETWORK_REGISTRY in res
    assert CONTRACT_SECRET_REGISTRY in res

    # check that it fails if sender has no eth
    deployer = ContractDeployer(
        web3=web3,
        private_key=get_random_privkey(),
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
    )
    with pytest.raises(ValidationError):
        deploy_raiden_contracts(deployer, )
示例#2
0
def test_red_eyes_deployer(web3):
    """ A smoke test for deploying RedEyes version contracts """
    deployer = ContractDeployer(
        web3=web3,
        private_key=FAUCET_PRIVATE_KEY,
        gas_limit=GAS_LIMIT,
        gas_price=1,
        wait=10,
        contracts_version='0.4.0',
    )
    deploy_raiden_contracts(
        deployer=deployer,
        max_num_of_token_networks=None,
    )
示例#3
0
def test_store_and_verify_raiden(fs_reload_deployer, web3):
    """ Store some raiden contract deployment information and verify them """
    fs_reload_deployer.add_real_directory(
        contracts_precompiled_path(version=None, ).parent)
    gas_limit = 5860000
    deployer = ContractDeployer(
        web3=web3,
        private_key=FAUCET_PRIVATE_KEY,
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
        contracts_version=None,
    )
    deployed_contracts_info = deploy_raiden_contracts(
        deployer=deployer,
        max_num_of_token_networks=30,
    )
    store_and_verify_deployment_info_raiden(
        contracts_version=None,
        deployer=deployer,
        deployed_contracts_info=deployed_contracts_info,
        save_info=False,
    )
    store_and_verify_deployment_info_raiden(
        contracts_version=None,
        deployer=deployer,
        deployed_contracts_info=deployed_contracts_info,
        save_info=True,
    )
示例#4
0
def test_deploy_script_register(
    web3,
    channel_participant_deposit_limit,
    token_network_deposit_limit,
):
    """ Run token register function used in the deployment script

    This checks if register_token_network() works correctly in the happy case,
    to make sure no code dependencies have been changed, affecting the deployment script.
    This does not check however that the cli command works correctly.
    """
    # normal deployment
    gas_limit = 5860000
    token_type = 'CustomToken'
    deployer = ContractDeployer(
        web3=web3,
        private_key=FAUCET_PRIVATE_KEY,
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
    )

    deployed_contracts_raiden = deploy_raiden_contracts(
        deployer=deployer,
        max_num_of_token_networks=1,
    )
    deployed_token = deploy_token_contract(
        deployer,
        token_supply=10000000,
        token_decimals=18,
        token_name='TestToken',
        token_symbol='TTT',
        token_type=token_type,
    )
    token_address = deployed_token[token_type]
    token_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY, )
    token_registry_address = deployed_contracts_raiden['contracts'][
        CONTRACT_TOKEN_NETWORK_REGISTRY]['address']
    token_network_address = register_token_network(
        web3=web3,
        caller=deployer.owner,
        token_registry_abi=token_registry_abi,
        token_registry_address=token_registry_address,
        token_registry_version=contract_version_string(version=None),
        token_address=token_address,
        channel_participant_deposit_limit=channel_participant_deposit_limit,
        token_network_deposit_limit=token_network_deposit_limit,
    )
    assert token_network_address is not None
    assert isinstance(token_network_address, T_Address)
def deprecation_test_setup(deployer, token_amount):
    deployed_contracts = deploy_raiden_contracts(deployer)['contracts']

    token_network_registry_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY, )
    token_network_registry = deployer.web3.eth.contract(
        abi=token_network_registry_abi,
        address=deployed_contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]['address'],
    )

    token_decimals = 18
    multiplier = 10**token_decimals
    token_supply = 10**6 * multiplier
    token_amount = int(token_amount * multiplier)

    deployed_token = deploy_token_contract(
        deployer,
        token_supply,
        token_decimals,
        'TestToken',
        'TTT',
        CONTRACT_CUSTOM_TOKEN,
    )
    token_address = deployed_token[CONTRACT_CUSTOM_TOKEN]
    token_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_CUSTOM_TOKEN)
    token_contract = deployer.web3.eth.contract(
        abi=token_abi,
        address=token_address,
    )

    # Mint some tokens for the owner
    txhash = token_contract.functions.mint(token_amount).transact(
        deployer.transaction, )

    log.debug(f'Minting tokens txHash={encode_hex(txhash)}')
    check_succesful_tx(deployer.web3, txhash, deployer.wait)
    assert token_contract.functions.balanceOf(
        deployer.owner).call() >= token_amount

    abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)
    token_network_address = register_token_network(
        web3=deployer.web3,
        caller=deployer.owner,
        token_registry_abi=abi,
        token_registry_address=deployed_contracts[
            CONTRACT_TOKEN_NETWORK_REGISTRY]['address'],
        token_address=token_address,
        wait=deployer.wait,
    )

    token_network_abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK)
    token_network = deployer.web3.eth.contract(
        abi=token_network_abi,
        address=token_network_address,
    )
    log.info(
        f'Registered the token and created a TokenNetwork contract at {token_network_address}.',
    )

    txhash = token_contract.functions.approve(token_network.address,
                                              token_amount).transact(
                                                  deployer.transaction, )
    log.debug(
        f'Approving tokens for the TokenNetwork contract txHash={encode_hex(txhash)}'
    )
    check_succesful_tx(deployer.web3, txhash, deployer.wait)

    assert token_contract.functions.allowance(
        deployer.owner,
        token_network.address,
    ).call() >= token_amount
    log.info(
        f'Approved {token_amount} tokens for the TokenNetwork contract '
        f'from owner {deployer.owner}.', )

    return (token_network_registry, token_network, token_contract)
示例#6
0
def deployed_raiden_info(deployer):
    return deploy_raiden_contracts(
        deployer=deployer,
        max_num_of_token_networks=1,
    )
示例#7
0
def test_deploy_script_raiden(
        web3,
        version: Optional[str],
        max_num_of_token_networks: Optional[int],
        deployer,
        deployed_raiden_info,
):
    """ Run raiden contracts deployment function and tamper with deployed_contracts_info

    This checks if deploy_raiden_contracts() works correctly in the happy case,
    to make sure no code dependencies have been changed, affecting the deployment script.
    This does not check however that the cli command works correctly.
    This also tampers with deployed_contracts_info to make sure an error is raised in
    verify_deployed_contracts()
    """
    deployed_contracts_info = deployed_raiden_info

    deployer._verify_deployment_data(
        deployment_data=deployed_contracts_info,
    )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts_version'] = '0.0.0'
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployment_data=deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['chain_id'] = 0
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployment_data=deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][
        CONTRACT_ENDPOINT_REGISTRY
    ]['address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY]['address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][
        CONTRACT_TOKEN_NETWORK_REGISTRY
    ]['address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_ENDPOINT_REGISTRY]['block_number'] = 0
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY]['block_number'] = 0
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_TOKEN_NETWORK_REGISTRY]['block_number'] = 0
    with pytest.raises(AssertionError):
        deployer._verify_deployment_data(
            deployed_contracts_info_fail,
        )

    # check that it fails if sender has no eth
    deployer = ContractDeployer(
        web3=web3,
        private_key=get_random_privkey(),
        gas_limit=GAS_LIMIT,
        gas_price=1,
        wait=10,
    )
    with pytest.raises(ValidationError):
        deploy_raiden_contracts(deployer, 1)
def test_deploy_script(
    web3,
    contracts_manager,
    utils_contract,
    faucet_private_key,
    faucet_address,
    get_random_privkey,
):
    """ Tamper with deployed_contracts_info and see failures in verify_deployed_contracts() """
    # normal deployment
    gas_limit = 5900000
    deployer = ContractDeployer(
        web3=web3,
        private_key=faucet_private_key,
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
    )

    deployed_contracts_info = deploy_raiden_contracts(deployer)

    verify_deployed_contracts(deployer.web3, deployer.contract_manager,
                              deployed_contracts_info)

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts_version'] = '0.0.0'
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['chain_id'] = 0
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_ENDPOINT_REGISTRY][
        'address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY][
        'address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_TOKEN_NETWORK_REGISTRY][
        'address'] = EMPTY_ADDRESS
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_ENDPOINT_REGISTRY][
        'block_number'] = 0
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_SECRET_REGISTRY][
        'block_number'] = 0
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
    deployed_contracts_info_fail['contracts'][CONTRACT_TOKEN_NETWORK_REGISTRY][
        'block_number'] = 0
    with pytest.raises(AssertionError):
        verify_deployed_contracts(
            deployer.web3,
            deployer.contract_manager,
            deployed_contracts_info_fail,
        )

    # check that it fails if sender has no eth
    deployer = ContractDeployer(
        web3=web3,
        private_key=get_random_privkey(),
        gas_limit=gas_limit,
        gas_price=1,
        wait=10,
    )
    with pytest.raises(ValidationError):
        deploy_raiden_contracts(deployer)
def main(keystore_file: str, password: str, rpc_url: str, development: bool):
    web3 = Web3(HTTPProvider(rpc_url, request_kwargs={'timeout': 60}))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)

    contract_version = '0.4.0'
    channel_participant_deposit_limit = None
    token_network_deposit_limit = None
    max_num_of_networks = None

    if development:
        contract_version = None
        channel_participant_deposit_limit = UNLIMITED
        token_network_deposit_limit = UNLIMITED
        max_num_of_networks = 500

    with open(keystore_file, 'r') as keystore:
        encrypted_key = keystore.read()
        private_key = web3.eth.account.decrypt(encrypted_key, password)
        account = Account.privateKeyToAccount(private_key)

    if private_key is None:
        print('No private key found')
        exit(1)
    owner = account.address

    if web3.eth.getBalance(owner) == 0:
        print('Account with insuficient funds.')
        exit(1)

    deployer = ContractDeployer(
        web3=web3,
        private_key=private_key,
        gas_limit=GAS_LIMIT,
        gas_price=GAS_PRICE,
        wait=10,
        contracts_version=contract_version,
    )

    print('Deploying raiden contracts')
    deployed_contract_info = deploy_raiden_contracts(
        deployer=deployer,
        max_num_of_token_networks=max_num_of_networks,
    )
    deployed_contracts = {
        contract_name: info['address']
        for contract_name, info in deployed_contract_info['contracts'].items()
    }

    print('Deploying test token contract')
    tokens = TOKEN_SUPPLY * (10**TOKEN_DECIMALS)
    deployed_token = deploy_token_contract(deployer, tokens, TOKEN_DECIMALS,
                                           'TestToken', 'TTT')

    abi = deployer.contract_manager.get_contract_abi(
        CONTRACT_TOKEN_NETWORK_REGISTRY)

    token_address = deployed_token['CustomToken']

    expected_version = contract_version_string(
        deployer.contract_version_string())

    print('Registering test token contract')
    register_token_network(
        web3=web3,
        caller=deployer.owner,
        token_registry_abi=abi,
        token_registry_address=deployed_contracts[
            CONTRACT_TOKEN_NETWORK_REGISTRY],
        token_address=token_address,
        token_registry_version=expected_version,
        channel_participant_deposit_limit=channel_participant_deposit_limit,
        token_network_deposit_limit=token_network_deposit_limit,
        contracts_version=deployer.contract_version_string(),
    )

    print(f'Token Deployed at: {token_address}')
    print(json.dumps(deployed_contracts, indent=4))
    print('done')