Ejemplo n.º 1
0
def staking_escrow_deployer(testerchain, threshold_staking,
                            application_economics, test_registry,
                            deployment_progress, transacting_power):
    deployer = StakingEscrowDeployer(
        staking_interface=threshold_staking.address,
        economics=application_economics,
        registry=test_registry)
    deployer.deploy(progress=deployment_progress,
                    deployment_mode=constants.INIT,
                    transacting_power=transacting_power)
    return deployer
Ejemplo n.º 2
0
def test_deployer_version_management(testerchain, test_registry,
                                     token_economics):
    deployer = StakingEscrowDeployer(registry=test_registry,
                                     economics=token_economics)

    untargeted_deployment = deployer.get_latest_enrollment()
    latest_targeted_deployment = deployer.get_principal_contract()

    proxy_deployer = deployer.get_proxy_deployer()
    proxy_target = proxy_deployer.target_contract.address
    assert latest_targeted_deployment.address == proxy_target
    assert untargeted_deployment.address != latest_targeted_deployment.address
Ejemplo n.º 3
0
def test_upgrade(testerchain, test_registry, application_economics,
                 transacting_power, threshold_staking):

    deployer = StakingEscrowDeployer(
        staking_interface=threshold_staking.address,
        registry=test_registry,
        economics=application_economics)

    receipts = deployer.upgrade(ignore_deployed=True,
                                confirmations=0,
                                transacting_power=transacting_power)
    for title, receipt in receipts.items():
        assert receipt['status'] == 1
Ejemplo n.º 4
0
def test_adjudicator_deployer(testerchain, token_economics,
                              deployment_progress, test_registry):
    testerchain = testerchain
    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(deployer_address=origin,
                                           registry=test_registry)
    token_deployer.deploy()

    stakers_escrow_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    registry=test_registry)

    staking_escrow_deployer.deploy(secret_hash=keccak(stakers_escrow_secret))
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Staker Escrow

    deployer = AdjudicatorDeployer(deployer_address=origin,
                                   registry=test_registry)
    deployment_receipts = deployer.deploy(secret_hash=os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH),
                                          progress=deployment_progress)

    # deployment steps must match expected number of steps
    assert deployment_progress.num_steps == len(
        deployer.deployment_steps) == len(deployment_receipts) == 3

    for step in deployer.deployment_steps:
        assert deployment_receipts[step]['status'] == 1

    # Create an AdjudicatorAgent instance
    adjudicator_agent = deployer.make_agent()

    # Check default Adjudicator deployment parameters
    assert staking_escrow_deployer.deployer_address != staking_agent.contract_address
    assert adjudicator_agent.staking_escrow_contract == staking_agent.contract_address
    assert adjudicator_agent.hash_algorithm == token_economics.hash_algorithm
    assert adjudicator_agent.base_penalty == token_economics.base_penalty
    assert adjudicator_agent.penalty_history_coefficient == token_economics.penalty_history_coefficient
    assert adjudicator_agent.percentage_penalty_coefficient == token_economics.percentage_penalty_coefficient
    assert adjudicator_agent.reward_coefficient == token_economics.reward_coefficient

    # Retrieve the AdjudicatorAgent singleton
    some_policy_agent = AdjudicatorAgent(registry=test_registry)
    assert adjudicator_agent == some_policy_agent  # __eq__

    # Compare the contract address for equality
    assert adjudicator_agent.contract_address == some_policy_agent.contract_address
Ejemplo n.º 5
0
def _make_agency(testerchain, test_registry):
    """
    Launch the big three contracts on provided chain,
    make agents for each and return them.
    """
    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(deployer_address=origin,
                                           registry=test_registry)
    token_deployer.deploy()

    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    registry=test_registry)
    staking_escrow_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin,
                                                    registry=test_registry)
    policy_manager_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin,
                                               registry=test_registry)
    adjudicator_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    staking_interface_deployer = StakingInterfaceDeployer(
        deployer_address=origin, registry=test_registry)
    staking_interface_deployer.deploy(
        secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    token_agent = token_deployer.make_agent()  # 1 Token
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Staking Escrow
    policy_agent = policy_manager_deployer.make_agent()  # 3 Policy Agent
    _adjudicator_agent = adjudicator_deployer.make_agent()  # 4 Adjudicator

    # TODO: Get rid of returning these agents here.
    # What's important is deploying and creating the first agent for each contract,
    # and since agents are singletons, in tests it's only necessary to call the agent
    # constructor again to receive the existing agent.
    #
    # For example:
    #     staking_agent = StakingEscrowAgent()
    #
    # This is more clear than how we currently obtain an agent instance in tests:
    #     _, staking_agent, _ = agency
    #
    # Other advantages is that it's closer to how agents should be use (i.e., there
    # are no fixtures IRL) and it's more extensible (e.g., AdjudicatorAgent)

    return token_agent, staking_agent, policy_agent
Ejemplo n.º 6
0
def staking_escrow_deployer(session_testerchain, token_deployer):
    token_deployer.deploy()

    staking_escrow_deployer = StakingEscrowDeployer(
        blockchain=session_testerchain,
        deployer_address=session_testerchain.etherbase_account)
    return staking_escrow_deployer
Ejemplo n.º 7
0
def test_upgrade(session_testerchain):
    wrong_secret = b"on second thoughts..."
    old_secret = bytes(STAKING_ESCROW_DEPLOYMENT_SECRET, encoding='utf-8')
    new_secret_hash = keccak(b'new'+old_secret)

    deployer = StakingEscrowDeployer(blockchain=session_testerchain,
                                     deployer_address=session_testerchain.etherbase_account)

    with pytest.raises(deployer.ContractDeploymentError):
        deployer.upgrade(existing_secret_plaintext=wrong_secret,
                         new_secret_hash=new_secret_hash)

    receipts = deployer.upgrade(existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret_hash)

    for title, receipt in receipts.items():
        assert receipt['status'] == 1
Ejemplo n.º 8
0
def _make_agency(testerchain):
    """
    Launch the big three contracts on provided chain,
    make agents for each and return them.
    """
    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(blockchain=testerchain,
                                           deployer_address=origin)
    token_deployer.deploy()

    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    blockchain=testerchain)
    staking_escrow_deployer.deploy(
        secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH))

    policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin,
                                                    blockchain=testerchain)
    policy_manager_deployer.deploy(
        secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH))

    adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin,
                                               blockchain=testerchain)
    adjudicator_deployer.deploy(
        secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH))

    token_agent = token_deployer.make_agent()  # 1 Token
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Miner Escrow
    policy_agent = policy_manager_deployer.make_agent()  # 3 Policy Agent
    _adjudicator_agent = adjudicator_deployer.make_agent()  # 4 Adjudicator

    # TODO: Perhaps we should get rid of returning these agents here.
    # What's important is deploying and creating the first agent for each contract,
    # and since agents are singletons, in tests it's only necessary to call the agent
    # constructor again to receive the existing agent.
    #
    # For example:
    #     staking_agent = StakingEscrowAgent()
    #
    # This is more clear than how we currently obtain an agent instance in tests:
    #     _, staking_agent, _ = agency
    #
    # Other advantages is that it's closer to how agents should be use (i.e., there
    # are no fixtures IRL) and it's more extensible (e.g., AdjudicatorAgent)

    return token_agent, staking_agent, policy_agent
def test_deployer_version_management(testerchain, test_registry,
                                     token_economics):
    deployer = StakingEscrowDeployer(
        deployer_address=testerchain.etherbase_account,
        registry=test_registry,
        economics=token_economics)

    untargeted_deployment = deployer.get_latest_enrollment(
        registry=test_registry)
    latest_targeted_deployment = deployer.get_principal_contract(
        registry=test_registry)

    proxy_deployer = deployer.get_proxy_deployer(
        registry=test_registry, provider_uri=TEST_PROVIDER_URI)
    proxy_target = proxy_deployer.target_contract.address
    assert latest_targeted_deployment.address == proxy_target
    assert untargeted_deployment.address != latest_targeted_deployment.address
Ejemplo n.º 10
0
def test_adjudicator_deployer(testerchain, token_economics,
                              deployment_progress, test_registry):

    origin = testerchain.etherbase_account
    tpower = TransactingPower(account=origin,
                              signer=Web3Signer(testerchain.client))

    token_deployer = NucypherTokenDeployer(registry=test_registry)
    token_deployer.deploy(transacting_power=tpower)

    staking_escrow_deployer = StakingEscrowDeployer(registry=test_registry)

    staking_escrow_deployer.deploy(transacting_power=tpower)
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Staker Escrow

    deployer = AdjudicatorDeployer(registry=test_registry)
    deployment_receipts = deployer.deploy(progress=deployment_progress,
                                          transacting_power=tpower)

    # deployment steps must match expected number of steps
    assert deployment_progress.num_steps == len(
        deployer.deployment_steps) == len(deployment_receipts) == 2

    for step in deployer.deployment_steps:
        assert deployment_receipts[step]['status'] == 1

    # Create an AdjudicatorAgent instance
    adjudicator_agent = deployer.make_agent()

    # Check default Adjudicator deployment parameters
    assert tpower.account != staking_agent.contract_address
    assert adjudicator_agent.staking_escrow_contract == staking_agent.contract_address
    assert adjudicator_agent.hash_algorithm == token_economics.hash_algorithm
    assert adjudicator_agent.base_penalty == token_economics.base_penalty
    assert adjudicator_agent.penalty_history_coefficient == token_economics.penalty_history_coefficient
    assert adjudicator_agent.percentage_penalty_coefficient == token_economics.percentage_penalty_coefficient
    assert adjudicator_agent.reward_coefficient == token_economics.reward_coefficient

    # Retrieve the AdjudicatorAgent singleton
    some_policy_agent = AdjudicatorAgent(registry=test_registry)
    assert adjudicator_agent == some_policy_agent  # __eq__

    # Compare the contract address for equality
    assert adjudicator_agent.contract_address == some_policy_agent.contract_address
Ejemplo n.º 11
0
def test_upgrade(testerchain, test_registry, token_economics):
    wrong_secret = b"on second thoughts..."
    old_secret = bytes(STAKING_ESCROW_DEPLOYMENT_SECRET, encoding='utf-8')
    new_secret_hash = keccak(b'new' + old_secret)

    deployer = StakingEscrowDeployer(
        registry=test_registry,
        economics=token_economics,
        deployer_address=testerchain.etherbase_account)

    with pytest.raises(deployer.ContractDeploymentError):
        deployer.upgrade(existing_secret_plaintext=wrong_secret,
                         new_secret_hash=new_secret_hash)

    receipts = deployer.upgrade(existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret_hash,
                                ignore_deployed=True)
    for title, receipt in receipts.items():
        assert receipt['status'] == 1
Ejemplo n.º 12
0
def test_staking_interface_deployer(testerchain, deployment_progress,
                                    test_registry):

    #
    # Setup
    #

    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(deployer_address=origin,
                                           registry=test_registry)
    token_deployer.deploy()

    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    registry=test_registry)
    staking_escrow_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin,
                                                    registry=test_registry)
    policy_manager_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin,
                                               registry=test_registry)
    adjudicator_deployer.deploy(secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH)

    #
    # Test
    #

    staking_interface_deployer = StakingInterfaceDeployer(
        deployer_address=origin, registry=test_registry)
    staking_interface_receipts = staking_interface_deployer.deploy(
        secret_hash=INSECURE_DEPLOYMENT_SECRET_HASH,
        progress=deployment_progress)

    # deployment steps must match expected number of steps
    assert deployment_progress.num_steps == len(
        staking_interface_deployer.deployment_steps) == 2
    assert len(staking_interface_receipts) == 2

    for step in staking_interface_deployer.deployment_steps:
        assert staking_interface_receipts[step]['status'] == 1
def test_deploy_bare_upgradeable_contract_deployment(testerchain, test_registry, token_economics, transacting_power):
    deployer = StakingEscrowDeployer(registry=test_registry, economics=token_economics)

    enrolled_names = list(test_registry.enrolled_names)
    old_number_of_enrollments = enrolled_names.count(StakingEscrowDeployer.contract_name)
    old_number_of_proxy_enrollments = enrolled_names.count(StakingEscrowDeployer._proxy_deployer.contract_name)

    receipts = deployer.deploy(deployment_mode=BARE, ignore_deployed=True, transacting_power=transacting_power)
    for title, receipt in receipts.items():
        assert receipt['status'] == 1

    enrolled_names = list(test_registry.enrolled_names)
    new_number_of_enrollments = enrolled_names.count(StakingEscrowDeployer.contract_name)
    new_number_of_proxy_enrollments = enrolled_names.count(StakingEscrowDeployer._proxy_deployer.contract_name)

    # The principal contract was deployed.
    assert new_number_of_enrollments == (old_number_of_enrollments + 1)

    # The Dispatcher was not deployed.
    assert new_number_of_proxy_enrollments == old_number_of_proxy_enrollments
Ejemplo n.º 14
0
def test_manual_proxy_retargeting(testerchain, click_runner, test_registry,
                                  token_economics):

    # A local, alternate filepath registry exists
    assert os.path.exists(ALTERNATE_REGISTRY_FILEPATH)
    local_registry = LocalContractRegistry(
        filepath=ALTERNATE_REGISTRY_FILEPATH)
    deployer = StakingEscrowDeployer(
        deployer_address=testerchain.etherbase_account,
        registry=local_registry,
        economics=token_economics)
    proxy_deployer = deployer.get_proxy_deployer(registry=local_registry)

    # Untargeted enrollment is indeed un targeted by the proxy
    untargeted_deployment = deployer.get_latest_enrollment(
        registry=local_registry)
    assert proxy_deployer.target_contract.address != untargeted_deployment.address

    # MichWill still owns this proxy.
    michwill = testerchain.unassigned_accounts[1]
    assert proxy_deployer.target_contract.functions.owner().call() == michwill

    command = ('upgrade', '--retarget', '--deployer-address', michwill,
               '--contract-name', StakingEscrowDeployer.contract_name,
               '--target-address', untargeted_deployment.address, '--provider',
               TEST_PROVIDER_URI, '--registry-infile',
               ALTERNATE_REGISTRY_FILEPATH, '--poa')

    # Reveal the secret and upgrade
    old_secret = INSECURE_DEPLOYMENT_SECRET_PLAINTEXT.decode()
    user_input = '0\n' + 'Y\n' + f'{old_secret}\n' + (
        f'{INSECURE_DEVELOPMENT_PASSWORD}\n' * 2) + 'Y\n'
    result = click_runner.invoke(deploy,
                                 command,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # The proxy target has been updated.
    proxy_deployer = deployer.get_proxy_deployer(registry=local_registry)
    assert proxy_deployer.target_contract.address == untargeted_deployment.address
Ejemplo n.º 15
0
def test_policy_manager_deployer(testerchain):
    origin, *everybody_else = testerchain.client.accounts

    token_deployer = NucypherTokenDeployer(blockchain=testerchain,
                                           deployer_address=origin)

    token_deployer.deploy()

    stakers_escrow_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    blockchain=testerchain)

    staking_escrow_deployer.deploy(secret_hash=keccak(stakers_escrow_secret))

    policy_manager_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    deployer = PolicyManagerDeployer(deployer_address=origin,
                                     blockchain=testerchain)

    deployment_txhashes = deployer.deploy(
        secret_hash=keccak(policy_manager_secret))
    assert len(deployment_txhashes) == 3

    for title, txhash in deployment_txhashes.items():
        receipt = testerchain.wait_for_receipt(txhash=txhash)
        assert receipt['status'] == 1, "Transaction Rejected {}:{}".format(
            title, txhash)

    # Create a PolicyAgent
    policy_agent = deployer.make_agent()

    # TODO: #1102 - Check that StakingEscrow contract address and public parameters are correct

    # Retrieve the PolicyAgent singleton
    some_policy_agent = PolicyAgent()
    assert policy_agent == some_policy_agent  # __eq__

    # Compare the contract address for equality
    assert policy_agent.contract_address == some_policy_agent.contract_address
Ejemplo n.º 16
0
def test_manual_proxy_retargeting(testerchain, test_registry, token_economics):

    deployer = StakingEscrowDeployer(
        registry=test_registry,
        deployer_address=testerchain.etherbase_account,
        economics=token_economics)

    # Get Proxy-Direct
    proxy_deployer = deployer.get_proxy_deployer(
        registry=test_registry, provider_uri=TEST_PROVIDER_URI)

    # Re-Deploy Staking Escrow
    old_target = proxy_deployer.target_contract.address

    old_secret = bytes("...maybe not.", encoding='utf-8')
    new_secret = keccak_digest(bytes('thistimeforsure', encoding='utf-8'))

    # Get the latest un-targeted contract from the registry
    latest_deployment = deployer.get_latest_enrollment(registry=test_registry)
    receipt = deployer.retarget(target_address=latest_deployment.address,
                                existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret)

    assert receipt['status'] == 1

    # Check proxy targets
    new_target = proxy_deployer.contract.functions.target().call()
    assert old_target != new_target
    assert new_target == latest_deployment.address

    # Check address consistency
    new_bare_contract = deployer.get_principal_contract(
        registry=test_registry, provider_uri=TEST_PROVIDER_URI)
    assert new_bare_contract.address == latest_deployment.address == new_target
def test_manual_proxy_retargeting(monkeypatch, testerchain, click_runner,
                                  token_economics):

    # A local, alternate filepath registry exists
    assert os.path.exists(ALTERNATE_REGISTRY_FILEPATH)
    local_registry = LocalContractRegistry(
        filepath=ALTERNATE_REGISTRY_FILEPATH)
    deployer = StakingEscrowDeployer(
        deployer_address=testerchain.etherbase_account,
        registry=local_registry,
        economics=token_economics)
    proxy_deployer = deployer.get_proxy_deployer()

    # Un-targeted enrollment is indeed un targeted by the proxy
    untargeted_deployment = deployer.get_latest_enrollment()
    assert proxy_deployer.target_contract.address != untargeted_deployment.address

    # MichWill still owns this proxy.
    michwill = testerchain.unassigned_accounts[1]
    assert proxy_deployer.contract.functions.owner().call() == michwill

    command = ('upgrade', '--retarget', '--deployer-address', michwill,
               '--contract-name', StakingEscrowDeployer.contract_name,
               '--target-address', untargeted_deployment.address, '--provider',
               TEST_PROVIDER_URI, '--signer', TEST_PROVIDER_URI,
               '--registry-infile', ALTERNATE_REGISTRY_FILEPATH,
               '--confirmations', 4, '--network', TEMPORARY_DOMAIN)

    # Upgrade
    user_input = '0\n' + YES_ENTER + YES_ENTER + YES_ENTER
    result = click_runner.invoke(deploy,
                                 command,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # The proxy target has been updated.
    proxy_deployer = deployer.get_proxy_deployer()
    assert proxy_deployer.target_contract.address == untargeted_deployment.address
Ejemplo n.º 18
0
def test_adjudicator_deployer(testerchain, slashing_economics):
    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(blockchain=testerchain, deployer_address=origin)
    token_deployer.deploy()

    stakers_escrow_secret = os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin, blockchain=testerchain)

    staking_escrow_deployer.deploy(secret_hash=keccak(stakers_escrow_secret))
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Staker Escrow

    deployer = AdjudicatorDeployer(deployer_address=origin, blockchain=testerchain)
    deployment_txhashes = deployer.deploy(secret_hash=os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH))

    assert len(deployment_txhashes) == 3

    for title, txhash in deployment_txhashes.items():
        receipt = testerchain.wait_for_receipt(txhash=txhash)
        assert receipt['status'] == 1, "Transaction Rejected {}:{}".format(title, txhash)

    # Create an AdjudicatorAgent instance
    adjudicator_agent = deployer.make_agent()

    # Check default Adjudicator deployment parameters
    assert adjudicator_agent.staking_escrow_contract == staking_agent.contract_address
    assert adjudicator_agent.hash_algorithm == slashing_economics.hash_algorithm
    assert adjudicator_agent.base_penalty == slashing_economics.base_penalty
    assert adjudicator_agent.penalty_history_coefficient == slashing_economics.penalty_history_coefficient
    assert adjudicator_agent.percentage_penalty_coefficient == slashing_economics.percentage_penalty_coefficient
    assert adjudicator_agent.reward_coefficient == slashing_economics.reward_coefficient

    # Retrieve the AdjudicatorAgent singleton
    some_policy_agent = AdjudicatorAgent()
    assert adjudicator_agent == some_policy_agent  # __eq__

    # Compare the contract address for equality
    assert adjudicator_agent.contract_address == some_policy_agent.contract_address
def test_rollback(testerchain, test_registry, transacting_power):

    deployer = StakingEscrowDeployer(registry=test_registry)

    staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry)
    current_target = staking_agent.contract.functions.target().call()

    # Let's do one more upgrade
    receipts = deployer.upgrade(ignore_deployed=True, confirmations=0, transacting_power=transacting_power)

    for title, receipt in receipts.items():
        assert receipt['status'] == 1

    old_target = current_target
    current_target = staking_agent.contract.functions.target().call()
    assert current_target != old_target

    # It's time to rollback.
    receipt = deployer.rollback(transacting_power=transacting_power)
    assert receipt['status'] == 1

    new_target = staking_agent.contract.functions.target().call()
    assert new_target != current_target
    assert new_target == old_target
Ejemplo n.º 20
0
def test_manual_proxy_retargeting(testerchain, test_registry, token_economics):

    deployer = StakingEscrowDeployer(
        registry=test_registry,
        deployer_address=testerchain.etherbase_account,
        economics=token_economics)

    # Get Proxy-Direct
    proxy_deployer = deployer.get_proxy_deployer(
        registry=test_registry, provider_uri=TEST_PROVIDER_URI)

    # Re-Deploy Staking Escrow
    old_target = proxy_deployer.target_contract.address

    old_secret = bytes("...maybe not.", encoding='utf-8')
    new_secret = keccak_digest(bytes('thistimeforsure', encoding='utf-8'))

    # Get the latest un-targeted contract from the registry
    latest_deployment = deployer.get_latest_enrollment(registry=test_registry)

    # Build retarget transaction (just for informational purposes)
    transaction = deployer.retarget(target_address=latest_deployment.address,
                                    existing_secret_plaintext=old_secret,
                                    new_secret_hash=new_secret,
                                    just_build_transaction=True)

    assert transaction['to'] == proxy_deployer.contract.address
    upgrade_function, _params = proxy_deployer.contract.decode_function_input(
        transaction['data'])
    assert upgrade_function.fn_name == proxy_deployer.contract.functions.upgrade.fn_name

    # Retarget, for real
    receipt = deployer.retarget(target_address=latest_deployment.address,
                                existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret)

    assert receipt['status'] == 1

    # Check proxy targets
    new_target = proxy_deployer.contract.functions.target().call()
    assert old_target != new_target
    assert new_target == latest_deployment.address

    # Check address consistency
    new_bare_contract = deployer.get_principal_contract(
        registry=test_registry, provider_uri=TEST_PROVIDER_URI)
    assert new_bare_contract.address == latest_deployment.address == new_target
Ejemplo n.º 21
0
def _make_agency(testerchain, test_registry, token_economics,
                 deployer_transacting_power):

    transacting_power = deployer_transacting_power

    token_deployer = NucypherTokenDeployer(economics=token_economics,
                                           registry=test_registry)
    token_deployer.deploy(transacting_power=transacting_power)

    staking_escrow_deployer = StakingEscrowDeployer(economics=token_economics,
                                                    registry=test_registry)
    staking_escrow_deployer.deploy(deployment_mode=INIT,
                                   transacting_power=transacting_power)

    policy_manager_deployer = PolicyManagerDeployer(economics=token_economics,
                                                    registry=test_registry)
    policy_manager_deployer.deploy(transacting_power=transacting_power)

    adjudicator_deployer = AdjudicatorDeployer(economics=token_economics,
                                               registry=test_registry)
    adjudicator_deployer.deploy(transacting_power=transacting_power)

    staking_interface_deployer = StakingInterfaceDeployer(
        economics=token_economics, registry=test_registry)
    staking_interface_deployer.deploy(transacting_power=transacting_power)

    worklock_deployer = WorklockDeployer(economics=token_economics,
                                         registry=test_registry)
    worklock_deployer.deploy(transacting_power=transacting_power)

    staking_escrow_deployer = StakingEscrowDeployer(economics=token_economics,
                                                    registry=test_registry)
    staking_escrow_deployer.deploy(deployment_mode=FULL,
                                   transacting_power=transacting_power)

    # Set additional parameters
    minimum, default, maximum = FEE_RATE_RANGE
    policy_agent = policy_manager_deployer.make_agent()
    txhash = policy_agent.contract.functions.setFeeRateRange(
        minimum, default, maximum).transact()
    testerchain.wait_for_receipt(txhash)
Ejemplo n.º 22
0
def test_manual_proxy_retargeting(testerchain, test_registry, token_economics,
                                  transacting_power):

    deployer = StakingEscrowDeployer(registry=test_registry,
                                     economics=token_economics)

    # Get Proxy-Direct
    proxy_deployer = deployer.get_proxy_deployer()

    # Re-Deploy Staking Escrow
    old_target = proxy_deployer.target_contract.address

    # Get the latest un-targeted contract from the registry
    latest_deployment = deployer.get_latest_enrollment()

    # Build retarget transaction (just for informational purposes)
    transaction = deployer.retarget(transacting_power=transacting_power,
                                    target_address=latest_deployment.address,
                                    just_build_transaction=True,
                                    confirmations=0)

    assert transaction['to'] == proxy_deployer.contract.address
    upgrade_function, _params = proxy_deployer.contract.decode_function_input(
        transaction['data'])  # TODO: this only tests for ethtester
    assert upgrade_function.fn_name == proxy_deployer.contract.functions.upgrade.fn_name

    # Retarget, for real
    receipt = deployer.retarget(transacting_power=transacting_power,
                                target_address=latest_deployment.address,
                                confirmations=0)

    assert receipt['status'] == 1

    # Check proxy targets
    new_target = proxy_deployer.contract.functions.target().call()
    assert old_target != new_target
    assert new_target == latest_deployment.address

    # Check address consistency
    new_bare_contract = deployer.get_principal_contract()
    assert new_bare_contract.address == latest_deployment.address == new_target
Ejemplo n.º 23
0
def test_rollback(testerchain, test_registry):
    old_secret = bytes('new' + STAKING_ESCROW_DEPLOYMENT_SECRET,
                       encoding='utf-8')
    new_secret_hash = keccak(text="third time's the charm")

    deployer = StakingEscrowDeployer(
        registry=test_registry, deployer_address=testerchain.etherbase_account)

    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=test_registry)
    current_target = staking_agent.contract.functions.target().call()

    # Let's do one more upgrade
    receipts = deployer.upgrade(existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret_hash,
                                ignore_deployed=True)

    for title, receipt in receipts.items():
        assert receipt['status'] == 1

    old_target = current_target
    current_target = staking_agent.contract.functions.target().call()
    assert current_target != old_target

    # It's time to rollback. But first...
    wrong_secret = b"WRONG!!"
    with pytest.raises(deployer.ContractDeploymentError):
        deployer.rollback(existing_secret_plaintext=wrong_secret,
                          new_secret_hash=new_secret_hash)

    # OK, *now* is time for rollback
    old_secret = b"third time's the charm"
    new_secret_hash = keccak(text="...maybe not.")
    receipt = deployer.rollback(existing_secret_plaintext=old_secret,
                                new_secret_hash=new_secret_hash)

    assert receipt['status'] == 1

    new_target = staking_agent.contract.functions.target().call()
    assert new_target != current_target
    assert new_target == old_target
Ejemplo n.º 24
0
def test_activate_network(testerchain, token_economics, test_registry):
    staking_escrow_deployer = StakingEscrowDeployer(
        registry=test_registry, deployer_address=testerchain.etherbase_account)

    # Let's check we're in the position of activating StakingEscrow
    assert staking_escrow_deployer.is_deployed()
    assert not staking_escrow_deployer.is_active
    assert staking_escrow_deployer.ready_to_activate

    # OK, let's do it!
    receipts = staking_escrow_deployer.activate()
    for tx in receipts:
        assert receipts[tx]['status'] == 1

    # Yay!
    assert staking_escrow_deployer.is_active

    # Trying to activate now must fail
    assert not staking_escrow_deployer.ready_to_activate
    with pytest.raises(StakingEscrowDeployer.ContractDeploymentError):
        staking_escrow_deployer.activate()
Ejemplo n.º 25
0
def test_deploy_ethereum_contracts(testerchain, deployment_progress,
                                   test_registry):
    testerchain = testerchain

    origin, *everybody_else = testerchain.client.accounts

    #
    # Nucypher Token
    #
    token_deployer = NucypherTokenDeployer(registry=test_registry,
                                           deployer_address=origin)
    assert token_deployer.deployer_address == origin

    with pytest.raises(ContractDeployer.ContractDeploymentError):
        assert token_deployer.contract_address is constants.CONTRACT_NOT_DEPLOYED
    assert not token_deployer.is_deployed

    token_deployer.deploy(progress=deployment_progress)
    assert token_deployer.is_deployed
    assert len(token_deployer.contract_address) == 42

    token_agent = NucypherTokenAgent(registry=test_registry)
    assert len(token_agent.contract_address) == 42
    assert token_agent.contract_address == token_deployer.contract_address

    another_token_agent = token_deployer.make_agent()
    assert len(another_token_agent.contract_address) == 42
    assert another_token_agent.contract_address == token_deployer.contract_address == token_agent.contract_address

    #
    # StakingEscrow
    #
    stakers_escrow_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    staking_escrow_deployer = StakingEscrowDeployer(registry=test_registry,
                                                    deployer_address=origin)
    assert staking_escrow_deployer.deployer_address == origin

    with pytest.raises(ContractDeployer.ContractDeploymentError):
        assert staking_escrow_deployer.contract_address is constants.CONTRACT_NOT_DEPLOYED
    assert not staking_escrow_deployer.is_deployed

    staking_escrow_deployer.deploy(secret_hash=keccak(stakers_escrow_secret),
                                   progress=deployment_progress)
    assert staking_escrow_deployer.is_deployed
    assert len(staking_escrow_deployer.contract_address) == 42

    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=test_registry)
    assert len(staking_agent.contract_address) == 42
    assert staking_agent.contract_address == staking_escrow_deployer.contract_address

    another_staking_agent = staking_escrow_deployer.make_agent()
    assert len(another_staking_agent.contract_address) == 42
    assert another_staking_agent.contract_address == staking_escrow_deployer.contract_address == staking_agent.contract_address

    #
    # Policy Manager
    #
    policy_manager_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    policy_manager_deployer = PolicyManagerDeployer(registry=test_registry,
                                                    deployer_address=origin)

    assert policy_manager_deployer.deployer_address == origin

    with pytest.raises(ContractDeployer.ContractDeploymentError):
        assert policy_manager_deployer.contract_address is constants.CONTRACT_NOT_DEPLOYED
    assert not policy_manager_deployer.is_deployed

    policy_manager_deployer.deploy(secret_hash=keccak(policy_manager_secret),
                                   progress=deployment_progress)
    assert policy_manager_deployer.is_deployed
    assert len(policy_manager_deployer.contract_address) == 42

    policy_agent = policy_manager_deployer.make_agent()
    assert len(policy_agent.contract_address) == 42
    assert policy_agent.contract_address == policy_manager_deployer.contract_address

    another_policy_agent = policy_manager_deployer.make_agent()
    assert len(another_policy_agent.contract_address) == 42
    assert another_policy_agent.contract_address == policy_manager_deployer.contract_address == policy_agent.contract_address

    #
    # Adjudicator
    #
    adjudicator_secret = os.urandom(
        DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
    adjudicator_deployer = AdjudicatorDeployer(registry=test_registry,
                                               deployer_address=origin)

    assert adjudicator_deployer.deployer_address == origin

    with pytest.raises(ContractDeployer.ContractDeploymentError):
        assert adjudicator_deployer.contract_address is constants.CONTRACT_NOT_DEPLOYED
    assert not adjudicator_deployer.is_deployed

    adjudicator_deployer.deploy(secret_hash=keccak(adjudicator_secret),
                                progress=deployment_progress)
    assert adjudicator_deployer.is_deployed
    assert len(adjudicator_deployer.contract_address) == 42

    adjudicator_agent = adjudicator_deployer.make_agent()
    assert len(adjudicator_agent.contract_address) == 42
    assert adjudicator_agent.contract_address == adjudicator_deployer.contract_address

    another_adjudicator_agent = AdjudicatorAgent(registry=test_registry)
    assert len(another_adjudicator_agent.contract_address) == 42
    assert another_adjudicator_agent.contract_address == adjudicator_deployer.contract_address == adjudicator_agent.contract_address

    # overall deployment steps must match aggregated individual expected number of steps
    all_deployment_transactions = token_deployer.deployment_steps + staking_escrow_deployer.deployment_steps + \
                                  policy_manager_deployer.deployment_steps + adjudicator_deployer.deployment_steps
    assert deployment_progress.num_steps == len(all_deployment_transactions)
Ejemplo n.º 26
0
def staking_escrow_deployer(testerchain, adjudicator_deployer, test_registry):
    adjudicator_deployer.deploy()
    staking_escrow_deployer = StakingEscrowDeployer(
        registry=test_registry, deployer_address=testerchain.etherbase_account)
    return staking_escrow_deployer
Ejemplo n.º 27
0
def staking_escrow_stub_deployer(testerchain, token_deployer, test_registry):
    token_deployer.deploy()
    staking_escrow_deployer = StakingEscrowDeployer(
        registry=test_registry, deployer_address=testerchain.etherbase_account)
    return staking_escrow_deployer
Ejemplo n.º 28
0
def _make_agency(
    testerchain, test_registry, token_economics
) -> Tuple[NucypherTokenAgent, StakingEscrowAgent, PolicyManagerAgent]:
    """
    Launch the big three contracts on provided chain,
    make agents for each and return them.
    """

    # Mock TransactingPower Consumption (Deployer)
    testerchain.transacting_power = TransactingPower(
        password=INSECURE_DEVELOPMENT_PASSWORD,
        signer=Web3Signer(client=testerchain.client),
        account=testerchain.etherbase_account)
    testerchain.transacting_power.activate()

    origin = testerchain.etherbase_account

    token_deployer = NucypherTokenDeployer(deployer_address=origin,
                                           economics=token_economics,
                                           registry=test_registry)
    token_deployer.deploy()

    staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin,
                                                    economics=token_economics,
                                                    registry=test_registry,
                                                    test_mode=True)
    staking_escrow_deployer.deploy()

    policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin,
                                                    economics=token_economics,
                                                    registry=test_registry)
    policy_manager_deployer.deploy()

    adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin,
                                               economics=token_economics,
                                               registry=test_registry)
    adjudicator_deployer.deploy()

    staking_interface_deployer = StakingInterfaceDeployer(
        deployer_address=origin,
        economics=token_economics,
        registry=test_registry)
    staking_interface_deployer.deploy()

    worklock_deployer = WorklockDeployer(deployer_address=origin,
                                         economics=token_economics,
                                         registry=test_registry)
    worklock_deployer.deploy()

    token_agent = token_deployer.make_agent()  # 1 Token
    staking_agent = staking_escrow_deployer.make_agent()  # 2 Staking Escrow
    policy_agent = policy_manager_deployer.make_agent()  # 3 Policy Agent
    _adjudicator_agent = adjudicator_deployer.make_agent()  # 4 Adjudicator
    _worklock_agent = worklock_deployer.make_agent()  # 5 Worklock

    # Set additional parameters
    minimum, default, maximum = FEE_RATE_RANGE
    txhash = policy_agent.contract.functions.setFeeRateRange(
        minimum, default, maximum).transact()
    _receipt = testerchain.wait_for_receipt(txhash)

    # TODO: Get rid of returning these agents here.
    # What's important is deploying and creating the first agent for each contract,
    # and since agents are singletons, in tests it's only necessary to call the agent
    # constructor again to receive the existing agent.
    #
    # For example:
    #     staking_agent = StakingEscrowAgent()
    #
    # This is more clear than how we currently obtain an agent instance in tests:
    #     _, staking_agent, _ = agency
    #
    # Other advantages is that it's closer to how agents should be use (i.e., there
    # are no fixtures IRL) and it's more extensible (e.g., AdjudicatorAgent)

    return token_agent, staking_agent, policy_agent
Ejemplo n.º 29
0
def test_upgradeability(temp_dir_path):
    # Prepare remote source for compilation
    download_github_dir(GITHUB_SOURCE_LINK, temp_dir_path)
    solidity_compiler = SolidityCompiler(source_dirs=[
        SourceDirs(SolidityCompiler.default_contract_dir()),
        SourceDirs(temp_dir_path)
    ])

    # Prepare the blockchain
    provider_uri = 'tester://pyevm/2'
    try:
        blockchain_interface = BlockchainDeployerInterface(
            provider_uri=provider_uri,
            compiler=solidity_compiler,
            gas_strategy=free_gas_price_strategy)
        blockchain_interface.connect()
        origin = blockchain_interface.client.accounts[0]
        BlockchainInterfaceFactory.register_interface(
            interface=blockchain_interface)
        blockchain_interface.transacting_power = TransactingPower(
            password=INSECURE_DEVELOPMENT_PASSWORD, account=origin)
        blockchain_interface.transacting_power.activate()

        # Check contracts with multiple versions
        raw_contracts = blockchain_interface._raw_contract_cache
        contract_name = AdjudicatorDeployer.contract_name
        test_adjudicator = len(raw_contracts[contract_name]) > 1
        contract_name = StakingEscrowDeployer.contract_name
        test_staking_escrow = len(raw_contracts[contract_name]) > 1
        contract_name = PolicyManagerDeployer.contract_name
        test_policy_manager = len(raw_contracts[contract_name]) > 1

        if not test_adjudicator and not test_staking_escrow and not test_policy_manager:
            return

        # Prepare master version of contracts and upgrade to the latest
        registry = InMemoryContractRegistry()

        token_deployer = NucypherTokenDeployer(registry=registry,
                                               deployer_address=origin)
        token_deployer.deploy()

        staking_escrow_deployer = StakingEscrowDeployer(
            registry=registry, deployer_address=origin)
        deploy_earliest_contract(blockchain_interface, staking_escrow_deployer)
        if test_staking_escrow:
            staking_escrow_deployer.upgrade(contract_version="latest")

        if test_policy_manager:
            policy_manager_deployer = PolicyManagerDeployer(
                registry=registry, deployer_address=origin)
            deploy_earliest_contract(blockchain_interface,
                                     policy_manager_deployer)
            policy_manager_deployer.upgrade(contract_version="latest")

        if test_adjudicator:
            adjudicator_deployer = AdjudicatorDeployer(registry=registry,
                                                       deployer_address=origin)
            deploy_earliest_contract(blockchain_interface,
                                     adjudicator_deployer)
            adjudicator_deployer.upgrade(contract_version="latest")

    finally:
        # Unregister interface
        with contextlib.suppress(KeyError):
            del BlockchainInterfaceFactory._interfaces[provider_uri]
def test_upgradeability(temp_dir_path):
    # Prepare remote source for compilation
    download_github_dir(GITHUB_SOURCE_LINK, temp_dir_path)

    # Prepare the blockchain
    TesterBlockchain.SOURCES = [
        SourceBundle(base_path=SOLIDITY_SOURCE_ROOT,
                     other_paths=(TEST_SOLIDITY_SOURCE_ROOT, )),
        SourceBundle(base_path=Path(temp_dir_path))
    ]

    eth_provider_uri = 'tester://pyevm/2'  # TODO: Testerchain caching Issues
    try:
        blockchain_interface = TesterBlockchain(gas_strategy='free')
        blockchain_interface.eth_provider_uri = eth_provider_uri
        blockchain_interface.connect()
        origin = blockchain_interface.client.accounts[0]
        BlockchainInterfaceFactory.register_interface(
            interface=blockchain_interface)
        transacting_power = TransactingPower(
            password=INSECURE_DEVELOPMENT_PASSWORD,
            signer=Web3Signer(blockchain_interface.client),
            account=origin)

        economics = make_token_economics(blockchain_interface)

        # Check contracts with multiple versions
        contract_name = StakingEscrowDeployer.contract_name
        skip_staking_escrow_test = skip_test(blockchain_interface,
                                             contract_name)

        if skip_staking_escrow_test:
            return

        # Prepare master version of contracts and upgrade to the latest
        registry = InMemoryContractRegistry()

        token_deployer = NucypherTokenDeployer(registry=registry,
                                               economics=economics)
        token_deployer.deploy(transacting_power=transacting_power)

        staking_escrow_deployer = StakingEscrowDeployer(registry=registry,
                                                        economics=economics)
        staking_escrow_deployer.deploy(deployment_mode=constants.INIT,
                                       transacting_power=transacting_power)

        if not skip_staking_escrow_test:
            economics.worklock_supply = economics.maximum_allowed_locked
            worklock_deployer = WorklockDeployer(registry=registry,
                                                 economics=economics)
            worklock_deployer.deploy(transacting_power=transacting_power)

        staking_escrow_deployer = StakingEscrowDeployer(registry=registry,
                                                        economics=economics)
        deploy_base_contract(blockchain_interface,
                             staking_escrow_deployer,
                             transacting_power=transacting_power,
                             skipt_test=skip_staking_escrow_test)

        if not skip_staking_escrow_test:
            prepare_staker(blockchain_interface=blockchain_interface,
                           deployer=staking_escrow_deployer,
                           transacting_power=transacting_power)
            staking_escrow_deployer.upgrade(
                transacting_power=transacting_power,
                contract_version="latest",
                confirmations=0)

    finally:
        # Unregister interface  # TODO: Move to method?
        with contextlib.suppress(KeyError):
            del BlockchainInterfaceFactory._interfaces[eth_provider_uri]