Example #1
0
def _create_stakeholder(config_file, provider_uri, poa, light, registry_filepath,
                        staking_address, beneficiary_address, allocation_filepath):
    stakeholder_config = _get_stakeholder_config(config_file, provider_uri, poa, light, registry_filepath)

    # Now let's check whether we're dealing here with a regular staker or a preallocation staker
    is_preallocation_staker = (beneficiary_address and staking_address) or allocation_filepath

    # Configure the individual allocation registry, if needed
    individual_allocation = None
    if is_preallocation_staker:
        if allocation_filepath:
            if beneficiary_address or staking_address:
                message = "--allocation-filepath is incompatible with --beneficiary-address and --staking-address."
                raise click.BadOptionUsage(option_name="--allocation-filepath", message=message)

            # This assumes the user has an individual allocation file in disk
            individual_allocation = IndividualAllocationRegistry.from_allocation_file(allocation_filepath)
        elif beneficiary_address and staking_address:
            individual_allocation = IndividualAllocationRegistry(beneficiary_address=beneficiary_address,
                                                                 contract_address=staking_address)

        else:
            option = "--beneficiary_address" if beneficiary_address else "--staking-address"
            raise click.BadOptionUsage(option_name=option,
                                       message=f"You must specify both --beneficiary-address and --staking-address. "
                                               f"Only {option} was provided. As an alternative, you can simply "
                                               f"provide an individual allocation with --allocation-file <PATH>")

    # Lazy initialization of StakeHolder
    stakeholder = stakeholder_config.produce(initial_address=None,
                                             individual_allocation=individual_allocation)
    blockchain = BlockchainInterfaceFactory.get_interface(provider_uri=provider_uri)  # Eager connection

    return stakeholder, blockchain
Example #2
0
    def create_character(self, emitter, config_file):

        opts = self.staker_options
        stakeholder_config = opts.config_options.create_config(
            emitter, config_file)

        # Now let's check whether we're dealing here with a regular staker or a preallocation staker
        is_preallocation_staker = (
            self.beneficiary_address
            and opts.staking_address) or self.allocation_filepath

        if is_preallocation_staker:
            network = opts.config_options.network or list(
                stakeholder_config.domains)[
                    0]  # TODO: 1580 - ugly network/domains mapping
            if self.allocation_filepath:
                if self.beneficiary_address or opts.staking_address:
                    message = "--allocation-filepath is incompatible with --beneficiary-address and --staking-address."
                    raise click.BadOptionUsage(
                        option_name="--allocation-filepath", message=message)

                # This assumes the user has an individual allocation file in disk
                individual_allocation = IndividualAllocationRegistry.from_allocation_file(
                    self.allocation_filepath, network=network)
                initial_address = individual_allocation.beneficiary_address
            elif self.beneficiary_address and opts.staking_address:
                individual_allocation = IndividualAllocationRegistry(
                    beneficiary_address=self.beneficiary_address,
                    contract_address=opts.staking_address,
                    network=network)
                initial_address = self.beneficiary_address
            else:
                option = "--beneficiary_address" if self.beneficiary_address else "--staking-address"
                raise click.BadOptionUsage(
                    option_name=option,
                    message=
                    f"You must specify both --beneficiary-address and --staking-address. "
                    f"Only {option} was provided. As an alternative, you can simply "
                    f"provide an individual allocation with --allocation-file <PATH>"
                )
        else:
            individual_allocation = None
            initial_address = None

        return opts.create_character(
            emitter,
            config_file,
            individual_allocation=individual_allocation,
            initial_address=initial_address,
        )
def preallocation_escrow_agent(beneficiary, test_registry,
                               mock_allocation_registry):
    individual_allocation = IndividualAllocationRegistry.from_allocation_file(
        MOCK_INDIVIDUAL_ALLOCATION_FILEPATH)
    preallocation_escrow_agent = PreallocationEscrowAgent(
        beneficiary=beneficiary,
        registry=test_registry,
        allocation_registry=individual_allocation)
    return preallocation_escrow_agent
def test_stake_detach_worker(click_runner, testerchain, token_economics,
                             beneficiary, preallocation_escrow_agent,
                             mock_allocation_registry, manual_worker,
                             test_registry,
                             stakeholder_configuration_file_location):

    staker_address = preallocation_escrow_agent.principal_contract.address

    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=test_registry)
    assert manual_worker == staking_agent.get_worker_from_staker(
        staker_address=staker_address)

    testerchain.time_travel(periods=token_economics.minimum_worker_periods)

    init_args = ('stake', 'detach-worker', '--config-file',
                 stakeholder_configuration_file_location,
                 '--allocation-filepath', MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                 '--force')

    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    individual_allocation = IndividualAllocationRegistry.from_allocation_file(
        MOCK_INDIVIDUAL_ALLOCATION_FILEPATH)
    staker = Staker(is_me=True,
                    checksum_address=beneficiary,
                    individual_allocation=individual_allocation,
                    registry=test_registry)

    assert not staker.worker_address

    # Ok ok, let's set the worker again.

    init_args = ('stake', 'set-worker', '--config-file',
                 stakeholder_configuration_file_location,
                 '--allocation-filepath', MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                 '--worker-address', manual_worker, '--force')

    user_input = INSECURE_DEVELOPMENT_PASSWORD
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    staker = Staker(is_me=True,
                    checksum_address=beneficiary,
                    individual_allocation=individual_allocation,
                    registry=test_registry)

    assert staker.worker_address == manual_worker
def test_stake_set_worker(click_runner, beneficiary, mock_allocation_registry,
                          test_registry, manual_worker,
                          stakeholder_configuration_file_location):

    init_args = ('stake', 'set-worker', '--config-file',
                 stakeholder_configuration_file_location,
                 '--allocation-filepath', MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                 '--worker-address', manual_worker, '--force')

    user_input = INSECURE_DEVELOPMENT_PASSWORD
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    individual_allocation = IndividualAllocationRegistry.from_allocation_file(
        MOCK_INDIVIDUAL_ALLOCATION_FILEPATH)
    staker = Staker(is_me=True,
                    checksum_address=beneficiary,
                    individual_allocation=individual_allocation,
                    registry=test_registry)

    assert staker.worker_address == manual_worker
Example #6
0
def individual_allocation():
    return IndividualAllocationRegistry.from_allocation_file(MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                                                             network=TEMPORARY_DOMAIN)
def test_stake_restake(click_runner, beneficiary, preallocation_escrow_agent,
                       mock_allocation_registry, test_registry, manual_worker,
                       testerchain, stakeholder_configuration_file_location):

    individual_allocation = IndividualAllocationRegistry.from_allocation_file(
        MOCK_INDIVIDUAL_ALLOCATION_FILEPATH)
    staker = Staker(is_me=True,
                    checksum_address=beneficiary,
                    registry=test_registry,
                    individual_allocation=individual_allocation)
    assert not staker.is_restaking

    restake_args = ('stake', 'restake', '--enable', '--config-file',
                    stakeholder_configuration_file_location,
                    '--allocation-filepath',
                    MOCK_INDIVIDUAL_ALLOCATION_FILEPATH, '--force')

    result = click_runner.invoke(nucypher_cli,
                                 restake_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0
    assert staker.is_restaking
    assert "Successfully enabled" in result.output

    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=test_registry)
    current_period = staking_agent.get_current_period()
    release_period = current_period + 1
    lock_args = ('stake', 'restake', '--lock-until', release_period,
                 '--config-file', stakeholder_configuration_file_location,
                 '--allocation-filepath', MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                 '--force')

    result = click_runner.invoke(nucypher_cli,
                                 lock_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # Still staking and the lock is enabled
    assert staker.is_restaking
    assert staker.restaking_lock_enabled

    # CLI Output includes success message
    assert "Successfully enabled" in result.output
    assert str(release_period) in result.output

    # Wait until release period
    testerchain.time_travel(periods=1)
    assert not staker.restaking_lock_enabled
    assert staker.is_restaking

    disable_args = ('stake', 'restake', '--disable', '--config-file',
                    stakeholder_configuration_file_location,
                    '--allocation-filepath',
                    MOCK_INDIVIDUAL_ALLOCATION_FILEPATH, '--force')

    result = click_runner.invoke(nucypher_cli,
                                 disable_args,
                                 input=INSECURE_DEVELOPMENT_PASSWORD,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    allocation_contract_address = preallocation_escrow_agent.principal_contract.address
    assert not staking_agent.is_restaking(allocation_contract_address)

    staker = Staker(is_me=True,
                    checksum_address=beneficiary,
                    registry=test_registry,
                    individual_allocation=individual_allocation)
    assert not staker.is_restaking
    assert "Successfully disabled" in result.output
Example #8
0
def test_individual_allocation_registry(get_random_checksum_address,
                                        test_registry,
                                        tempfile_path,
                                        testerchain,
                                        test_registry_source_manager):

    factory = testerchain.get_contract_factory(contract_name=PREALLOCATION_ESCROW_CONTRACT_NAME)
    allocation_contract_abi = factory.abi

    beneficiary = get_random_checksum_address()
    contract_address = get_random_checksum_address()
    allocation_registry = IndividualAllocationRegistry(beneficiary_address=beneficiary,
                                                       contract_address=contract_address,
                                                       network=TEMPORARY_DOMAIN)

    registry_data = allocation_registry.read()
    assert len(registry_data) == 1

    assert allocation_registry.search(beneficiary_address=beneficiary) == [contract_address, allocation_contract_abi]
    assert allocation_registry.search(contract_address=contract_address) == [beneficiary, allocation_contract_abi]

    # Check that searching for an unknown beneficiary or unknown contract raises
    with pytest.raises(IndividualAllocationRegistry.UnknownBeneficiary):
        allocation_registry.search(beneficiary_address=get_random_checksum_address())

    with pytest.raises(IndividualAllocationRegistry.UnknownContract):
        allocation_registry.search(contract_address=get_random_checksum_address())

    # Check that it gets the same data if using a file to create the allocation registry
    individual_allocation_file_data = {
        'beneficiary_address': beneficiary,
        'contract_address': contract_address
    }
    with open(tempfile_path, 'w') as outfile:
        json.dump(individual_allocation_file_data, outfile)

    allocation_registry = IndividualAllocationRegistry.from_allocation_file(filepath=tempfile_path,
                                                                            network=TEMPORARY_DOMAIN)
    assert registry_data == allocation_registry.read()
Example #9
0
def test_individual_allocation_registry(get_random_checksum_address, test_registry, tempfile_path):
    empty_allocation_escrow_deployer = PreallocationEscrowDeployer(registry=test_registry)
    allocation_contract_abi = empty_allocation_escrow_deployer.get_contract_abi()

    beneficiary = get_random_checksum_address()
    contract_address = get_random_checksum_address()
    allocation_registry = IndividualAllocationRegistry(beneficiary_address=beneficiary,
                                                       contract_address=contract_address)

    registry_data = allocation_registry.read()
    assert len(registry_data) == 1

    assert allocation_registry.search(beneficiary_address=beneficiary) == [contract_address, allocation_contract_abi]
    assert allocation_registry.search(contract_address=contract_address) == [beneficiary, allocation_contract_abi]

    # Check that searching for an unknown beneficiary or unknown contract raises
    with pytest.raises(IndividualAllocationRegistry.UnknownBeneficiary):
        allocation_registry.search(beneficiary_address=get_random_checksum_address())

    with pytest.raises(IndividualAllocationRegistry.UnknownContract):
        allocation_registry.search(contract_address=get_random_checksum_address())

    # Check that it gets the same data if using a file to create the allocation registry
    individual_allocation_file_data = {
        'beneficiary_address': beneficiary,
        'contract_address': contract_address
    }
    with open(tempfile_path, 'w') as outfile:
        json.dump(individual_allocation_file_data, outfile)

    allocation_registry = IndividualAllocationRegistry.from_allocation_file(filepath=tempfile_path)
    assert registry_data == allocation_registry.read()