Esempio n. 1
0
def make_ursula_for_staker(staker: Staker,
                           worker_address: str,
                           blockchain: BlockchainInterface,
                           ursula_config: UrsulaConfiguration,
                           ursulas_to_learn_about: Optional[
                               List[Ursula]] = None,
                           confirm_activity: bool = False,
                           **ursula_overrides) -> Ursula:

    # Assign worker to this staker
    staker.set_worker(worker_address=worker_address)

    worker = make_decentralized_ursulas(
        ursula_config=ursula_config,
        blockchain=blockchain,
        stakers_addresses=[staker.checksum_address],
        workers_addresses=[worker_address],
        confirm_activity=confirm_activity,
        **ursula_overrides).pop()

    for ursula_to_learn_about in (ursulas_to_learn_about or []):
        worker.remember_node(ursula_to_learn_about)
        ursula_to_learn_about.remember_node(worker)

    return worker
Esempio n. 2
0
def stakers(testerchain, agency, token_economics):
    token_agent, _staking_agent, _policy_agent = agency
    blockchain = token_agent.blockchain

    # Mock Powerup consumption (Deployer)
    blockchain.transacting_power = TransactingPower(
        blockchain=blockchain,
        password=INSECURE_DEVELOPMENT_PASSWORD,
        account=blockchain.etherbase_account)
    blockchain.transacting_power.activate()

    token_airdrop(origin=blockchain.etherbase_account,
                  addresses=blockchain.stakers_accounts,
                  token_agent=token_agent,
                  amount=DEVELOPMENT_TOKEN_AIRDROP_AMOUNT)

    stakers = list()
    for index, account in enumerate(blockchain.stakers_accounts):
        staker = Staker(is_me=True,
                        checksum_address=account,
                        blockchain=blockchain)

        # Mock TransactingPower consumption
        staker.blockchain.transacting_power = TransactingPower(
            blockchain=blockchain,
            password=INSECURE_DEVELOPMENT_PASSWORD,
            account=account)
        staker.blockchain.transacting_power.activate()

        min_stake, balance = token_economics.minimum_allowed_locked, staker.token_balance
        amount = random.randint(min_stake, balance)

        # for a random lock duration
        min_locktime, max_locktime = token_economics.minimum_locked_periods, token_economics.maximum_locked_periods
        periods = random.randint(min_locktime, max_locktime)

        staker.initialize_stake(amount=amount, lock_periods=periods)

        # We assume that the staker knows in advance the account of her worker
        worker_address = blockchain.ursula_account(index)
        staker.set_worker(worker_address=worker_address)

        stakers.append(staker)

    # Stake starts next period (or else signature validation will fail)
    blockchain.time_travel(periods=1)

    yield stakers
Esempio n. 3
0
def test_refund(click_runner, testerchain, agency_local_registry,
                token_economics):

    bidder = testerchain.unassigned_accounts[1]
    worker_address = testerchain.unassigned_accounts[-1]

    #
    # WorkLock Staker-Worker
    #

    worklock_agent = ContractAgency.get_agent(WorkLockAgent,
                                              registry=agency_local_registry)

    # Bidder is now STAKER. Bond a worker.
    staker = Staker(is_me=True,
                    checksum_address=bidder,
                    registry=agency_local_registry)
    receipt = staker.set_worker(worker_address=worker_address)
    assert receipt['status'] == 1

    worker = Ursula(is_me=True,
                    registry=agency_local_registry,
                    checksum_address=bidder,
                    worker_address=worker_address,
                    rest_host=MOCK_IP_ADDRESS,
                    rest_port=select_test_port())

    # Ensure there is work to do
    remaining_work = worklock_agent.get_remaining_work(checksum_address=bidder)
    assert remaining_work > 0

    # Do some work
    for i in range(3):
        receipt = worker.confirm_activity()
        assert receipt['status'] == 1
        testerchain.time_travel(periods=1)

    command = ('refund', '--bidder-address', bidder, '--registry-filepath',
               agency_local_registry.filepath, '--provider', TEST_PROVIDER_URI,
               '--poa', '--force')

    user_input = f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + 'Y\n'
    result = click_runner.invoke(worklock,
                                 command,
                                 input=user_input,
                                 catch_exceptions=False)
    assert result.exit_code == 0

    # Less work to do...
    new_remaining_work = worklock_agent.get_remaining_work(
        checksum_address=bidder)
    assert new_remaining_work < remaining_work
Esempio n. 4
0
def test_stake_in_idle_network(testerchain, token_economics, test_registry):

    # Let's fund a staker first
    token_agent = NucypherTokenAgent(registry=test_registry)
    token_airdrop(origin=testerchain.etherbase_account,
                  addresses=testerchain.stakers_accounts,
                  token_agent=token_agent,
                  amount=DEVELOPMENT_TOKEN_AIRDROP_AMOUNT)
    account = testerchain.stakers_accounts[0]
    staker = Staker(is_me=True, checksum_address=account, registry=test_registry)

    # Mock TransactingPower consumption
    staker.transacting_power = TransactingPower(password=INSECURE_DEVELOPMENT_PASSWORD, account=staker.checksum_address)
    staker.transacting_power.activate()

    # Since StakingEscrow hasn't been activated yet, deposit should work but confirmation must fail
    amount = token_economics.minimum_allowed_locked
    periods = token_economics.minimum_locked_periods
    staker.initialize_stake(amount=amount, lock_periods=periods)
    staker.set_worker(account)
    with pytest.raises((TransactionFailed, ValueError)):
        staker.staking_agent.confirm_activity(worker_address=account)