Ejemplo n.º 1
0
def test_select_divisible_stake(test_emitter, token_economics,
                                mock_staking_agent, test_registry,
                                mock_testerchain, mock_stdin, capsys,
                                divisible_stakes,
                                stakeholder_with_divisible_stakes):

    expected_stake = Stake.from_stake_info(
        stake_info=divisible_stakes[0],
        staking_agent=mock_staking_agent,  # stakinator
        index=0,
        checksum_address=stakeholder_with_divisible_stakes.checksum_address,
        economics=token_economics)

    # SUCCESS: Display all divisible-only stakes and make a selection
    mock_stdin.line(str(SELECTION))

    selected_stake = select_stake(
        emitter=test_emitter,
        divisible=True,
        stakeholder=stakeholder_with_divisible_stakes)

    assert isinstance(selected_stake, Stake)
    assert selected_stake == expected_stake

    # Examine the output
    captured = capsys.readouterr()
    assert NO_STAKES_FOUND not in captured.out
    assert ONLY_DISPLAYING_DIVISIBLE_STAKES_NOTE in captured.out
    assert_stake_table_painted(output=captured.out)
    assert mock_stdin.empty()
Ejemplo n.º 2
0
 def __read_stakes(self) -> None:
     stakes_reader = self.miner_agent.get_all_stakes(miner_address=self.checksum_public_address)
     stakes = dict()
     for index, stake_info in enumerate(stakes_reader):
         stake = Stake.from_stake_info(owner_address=self.checksum_public_address,
                                       stake_info=stake_info,
                                       index=index)
         stakes[index] = stake
     self.__stakes = stakes
Ejemplo n.º 3
0
def test_stake_init(click_runner,
                    stakeholder_configuration_file_location,
                    stake_value,
                    mock_registry_filepath,
                    token_economics,
                    testerchain,
                    agency,
                    manual_staker):

    # Simulate "Reconnection"
    cached_blockchain = BlockchainInterface.reconnect()
    registry = cached_blockchain.registry
    assert registry.filepath == mock_registry_filepath

    def from_dict(*args, **kwargs):
        return testerchain
    BlockchainInterface.from_dict = from_dict

    # Staker address has not stakes
    staking_agent = Agency.get_agent(StakingEscrowAgent)
    stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
    assert not stakes

    stake_args = ('stake', 'init',
                  '--config-file', stakeholder_configuration_file_location,
                  '--registry-filepath', mock_registry_filepath,
                  '--staking-address', manual_staker,
                  '--value', stake_value.to_tokens(),
                  '--duration', token_economics.minimum_locked_periods,
                  '--force')

    # TODO: This test it writing to the default system directory and ignoring updates to the passes filepath
    user_input = f'0\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + f'Y\n'
    result = click_runner.invoke(nucypher_cli, stake_args, input=user_input, catch_exceptions=False)
    assert result.exit_code == 0

    # Test integration with BaseConfiguration
    with open(stakeholder_configuration_file_location, 'r') as config_file:
        _config_data = json.loads(config_file.read())

    # Verify the stake is on-chain
    # Test integration with Agency
    stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
    assert len(stakes) == 1

    # Test integration with NU
    start_period, end_period, value = stakes[0]
    assert NU(int(value), 'NuNit') == stake_value
    assert (end_period - start_period) == token_economics.minimum_locked_periods - 1

    # Test integration with Stake
    stake = Stake.from_stake_info(index=0,
                                  checksum_address=manual_staker,
                                  stake_info=stakes[0])
    assert stake.value == stake_value
    assert stake.duration == token_economics.minimum_locked_periods
def test_stake_init(click_runner, stakeholder_configuration_file_location,
                    stake_value, token_economics, testerchain,
                    agency_local_registry, manual_staker):

    # Staker address has not stakes
    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=agency_local_registry)
    stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
    assert not stakes

    stake_args = ('stake', 'create', '--config-file',
                  stakeholder_configuration_file_location,
                  '--staking-address', manual_staker, '--value',
                  stake_value.to_tokens(), '--lock-periods',
                  token_economics.minimum_locked_periods, '--force')

    # TODO: This test is writing to the default system directory and ignoring updates to the passed filepath
    user_input = f'0\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + YES_ENTER
    result = click_runner.invoke(nucypher_cli,
                                 stake_args,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # Test integration with BaseConfiguration
    with open(stakeholder_configuration_file_location, 'r') as config_file:
        _config_data = json.loads(config_file.read())

    # Verify the stake is on-chain
    # Test integration with Agency
    stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
    assert len(stakes) == 1

    # Test integration with NU
    start_period, end_period, value = stakes[0]
    assert NU(int(value), 'NuNit') == stake_value
    assert (end_period -
            start_period) == token_economics.minimum_locked_periods - 1

    # Test integration with Stake
    stake = Stake.from_stake_info(index=0,
                                  checksum_address=manual_staker,
                                  stake_info=stakes[0],
                                  staking_agent=staking_agent,
                                  economics=token_economics)
    assert stake.value == stake_value
    assert stake.duration == token_economics.minimum_locked_periods
Ejemplo n.º 5
0
    def __read_stakes(self) -> None:
        """Rewrite the local staking cache by reading on-chain stakes"""

        existing_records = len(self.__stakes)

        # Candidate replacement cache values
        onchain_stakes, terminal_period = list(), 0

        # Read from blockchain
        stakes_reader = self.miner_agent.get_all_stakes(
            miner_address=self.checksum_address)

        for onchain_index, stake_info in enumerate(stakes_reader):

            if not stake_info:
                # This stake index is empty on-chain
                onchain_stake = EMPTY_STAKING_SLOT

            else:
                # On-chain stake detected
                onchain_stake = Stake.from_stake_info(miner=self,
                                                      stake_info=stake_info,
                                                      index=onchain_index)

                # Search for the terminal period
                if onchain_stake.end_period > terminal_period:
                    terminal_period = onchain_stake.end_period

            # Store the replacement stake
            onchain_stakes.append(onchain_stake)

        # Commit the new stake and terminal values to the cache
        if not onchain_stakes:
            self.__stakes = NO_STAKES.bool_value(False)
        else:
            self.__terminal_period = terminal_period
            self.__stakes = onchain_stakes

        # Record most recent cache update
        self.__updated = maya.now()
        new_records = existing_records - len(self.__stakes)
        self.log.debug(
            f"Updated local staking cache ({new_records} new records).")
Ejemplo n.º 6
0
def test_select_using_filter_function(
        test_emitter,
        stakeholder,
        mock_staking_agent,
        mock_testerchain,
        mock_stdin,  # used to assert user hasn't been prompted
        capsys,
        current_period,
        token_economics,
        sub_stakes_functions):
    # Setup
    mock_stakes = make_sub_stakes(current_period, token_economics,
                                  sub_stakes_functions)

    mock_staking_agent.get_all_stakes.return_value = mock_stakes
    staker = mock_testerchain.unassigned_accounts[0]
    stakeholder.set_staker(staker)

    selection = len(mock_stakes) - 1
    expected_stake = Stake.from_stake_info(
        stake_info=mock_stakes[selection],
        staking_agent=mock_staking_agent,  # stakinator
        index=selection,
        checksum_address=stakeholder.checksum_address,
        economics=token_economics)

    # SUCCESS: Display all editable-only stakes with specified final period
    mock_stdin.line(str(selection))
    selected_stake = select_stake(emitter=test_emitter,
                                  staker=stakeholder,
                                  stakes_status=Stake.Status.LOCKED,
                                  filter_function=lambda stake: stake.
                                  final_locked_period == current_period)

    assert isinstance(selected_stake, Stake)
    assert selected_stake == expected_stake

    # Examine the output
    captured = capsys.readouterr()
    assert NO_STAKES_FOUND not in captured.out
    assert_stake_table_painted(output=captured.out)
    assert mock_stdin.empty()
Ejemplo n.º 7
0
def test_select_divisible_stake(
        test_emitter,
        stakeholder,
        mock_staking_agent,
        mock_testerchain,
        mock_stdin,  # used to assert user hasn't been prompted
        capsys,
        current_period,
        token_economics,
        sub_stakes_functions):
    # Setup
    mock_stakes = make_sub_stakes(current_period, token_economics,
                                  sub_stakes_functions)

    mock_staking_agent.get_all_stakes.return_value = mock_stakes
    staker = mock_testerchain.unassigned_accounts[0]
    stakeholder.set_staker(staker)

    selection = len(mock_stakes) - 1
    expected_stake = Stake.from_stake_info(
        stake_info=mock_stakes[selection],
        staking_agent=mock_staking_agent,  # stakinator
        index=selection,
        checksum_address=stakeholder.checksum_address,
        economics=token_economics)

    # SUCCESS: Display all divisible-only stakes and make a selection
    mock_stdin.line(str(selection))
    selected_stake = select_stake(emitter=test_emitter,
                                  staker=stakeholder,
                                  stakes_status=Stake.Status.DIVISIBLE)

    assert isinstance(selected_stake, Stake)
    assert selected_stake == expected_stake

    # Examine the output
    captured = capsys.readouterr()
    assert NO_STAKES_FOUND not in captured.out
    assert ONLY_DISPLAYING_DIVISIBLE_STAKES_NOTE in captured.out
    assert_stake_table_painted(output=captured.out)
    assert mock_stdin.empty()
Ejemplo n.º 8
0
def test_select_editable_stake(test_emitter,
                               stakeholder,
                               mock_staking_agent,
                               mock_testerchain,
                               mock_stdin,  # used to assert user hasn't been prompted
                               capsys,
                               current_period,
                               token_economics,
                               sub_stakes_functions):
    mock_stakes = make_sub_stakes(current_period, token_economics, sub_stakes_functions)

    mock_staking_agent.get_all_stakes.return_value = mock_stakes
    staker = mock_testerchain.unassigned_accounts[0]
    stakeholder.assimilate(staker, password=INSECURE_DEVELOPMENT_PASSWORD)

    selection = len(mock_stakes) - 1
    expected_stake = Stake.from_stake_info(stake_info=mock_stakes[selection],
                                           staking_agent=mock_staking_agent,   # stakinator
                                           index=selection,
                                           checksum_address=stakeholder.checksum_address,
                                           economics=token_economics)

    # User's selection
    mock_stdin.line(str(selection))
    selected_stake = select_stake(emitter=test_emitter, staker=stakeholder.staker)

    # Check stake accuracy
    assert isinstance(selected_stake, Stake)
    assert selected_stake == expected_stake

    # Examine the output
    captured = capsys.readouterr()
    assert NO_STAKES_FOUND not in captured.out
    assert ONLY_DISPLAYING_DIVISIBLE_STAKES_NOTE not in captured.out
    assert_stake_table_painted(output=captured.out)
    assert mock_stdin.empty()
Ejemplo n.º 9
0
def test_stake_via_contract(click_runner,
                            custom_filepath,
                            agency_local_registry,
                            mock_allocation_registry,
                            testerchain,
                            stakeholder_configuration_file_location,
                            stake_value,
                            token_economics,
                            agency,
                            beneficiary,
                            preallocation_escrow_agent
                            ):

    #
    # Inital setup and checks: beneficiary and pre-allocation contract
    #

    # First, let's be sure the beneficiary is in the allocation registry...
    assert mock_allocation_registry.is_beneficiary_enrolled(beneficiary)

    # ... and that the pre-allocation contract has enough tokens
    preallocation_contract_address = preallocation_escrow_agent.principal_contract.address
    token_agent = ContractAgency.get_agent(NucypherTokenAgent, registry=agency_local_registry)
    assert token_agent.get_balance(preallocation_contract_address) >= token_economics.minimum_allowed_locked

    # Let's not forget to create a stakeholder
    init_args = ('stake', 'init-stakeholder',
                 '--poa',
                 '--config-root', custom_filepath,
                 '--provider', TEST_PROVIDER_URI,
                 '--network', TEMPORARY_DOMAIN,
                 '--registry-filepath', agency_local_registry.filepath)

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

    with open(stakeholder_configuration_file_location) as f:
        print(f.read())

    #
    # The good stuff: Using `nucypher stake create --escrow`
    #

    # Staking contract has no stakes yet
    staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=agency_local_registry)
    stakes = list(staking_agent.get_all_stakes(staker_address=preallocation_contract_address))
    assert not stakes

    stake_args = ('stake', 'create',
                  '--config-file', stakeholder_configuration_file_location,
                  '--allocation-filepath', MOCK_INDIVIDUAL_ALLOCATION_FILEPATH,
                  '--value', str(stake_value.to_tokens()),
                  '--lock-periods', token_economics.minimum_locked_periods,
                  '--force')

    # TODO: This test is writing to the default system directory and ignoring updates to the passed filepath
    user_input = '0\n' + 'Y\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + 'Y\n'
    result = click_runner.invoke(nucypher_cli, stake_args, input=user_input, catch_exceptions=False)
    assert result.exit_code == 0

    # Test integration with BaseConfiguration
    with open(stakeholder_configuration_file_location, 'r') as config_file:
        _config_data = json.loads(config_file.read())

    # Verify the stake is on-chain
    # Test integration with Agency
    stakes = list(staking_agent.get_all_stakes(staker_address=preallocation_contract_address))
    assert len(stakes) == 1

    # Test integration with NU
    start_period, end_period, value = stakes[0]
    assert NU(int(value), 'NuNit') == stake_value
    assert (end_period - start_period) == token_economics.minimum_locked_periods - 1

    # Test integration with Stake
    stake = Stake.from_stake_info(index=0,
                                  checksum_address=preallocation_contract_address,
                                  stake_info=stakes[0],
                                  staking_agent=staking_agent,
                                  economics=token_economics)
    assert stake.value == stake_value
    assert stake.duration == token_economics.minimum_locked_periods