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 making a commitment must fail amount = token_economics.minimum_allowed_locked periods = token_economics.minimum_locked_periods staker.initialize_stake(amount=amount, lock_periods=periods) staker.bond_worker(account) with pytest.raises((TransactionFailed, ValueError)): staker.staking_agent.commit_to_next_period(worker_address=account)
def test_stake_in_idle_network(testerchain, token_economics, test_registry): # Let's fund a staker first token_agent = NucypherTokenAgent(registry=test_registry) tpower = TransactingPower(account=testerchain.etherbase_account, signer=Web3Signer(testerchain.client)) token_airdrop(transacting_power=tpower, addresses=testerchain.stakers_accounts, token_agent=token_agent, amount=DEVELOPMENT_TOKEN_AIRDROP_AMOUNT) account = testerchain.stakers_accounts[0] tpower = TransactingPower(account=account, signer=Web3Signer(testerchain.client)) staker = Staker(transacting_power=tpower, domain=TEMPORARY_DOMAIN, registry=test_registry) # Since StakingEscrow hasn't been activated yet, deposit should work but making a commitment must fail amount = token_economics.minimum_allowed_locked periods = token_economics.minimum_locked_periods staker.initialize_stake(amount=amount, lock_periods=periods) staker.bond_worker(account) with pytest.raises((TransactionFailed, ValueError)): staker.staking_agent.commit_to_next_period(transacting_power=tpower)
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
def stakers(testerchain, agency, token_economics, test_registry, deployer_transacting_power): token_agent = ContractAgency.get_agent(NucypherTokenAgent, registry=test_registry) blockchain = token_agent.blockchain token_airdrop(transacting_power=deployer_transacting_power, addresses=blockchain.stakers_accounts, token_agent=token_agent, amount=DEVELOPMENT_TOKEN_AIRDROP_AMOUNT) stakers = list() for index, account in enumerate(blockchain.stakers_accounts): tpower = TransactingPower(account=account, signer=Web3Signer(testerchain.client)) tpower.unlock(password=INSECURE_DEVELOPMENT_PASSWORD) staker = Staker(transacting_power=tpower, domain=TEMPORARY_DOMAIN, registry=test_registry) amount = MIN_STAKE_FOR_TESTS + random.randrange(BONUS_TOKENS_FOR_TESTS) # for a random lock duration min_locktime, max_locktime = token_economics.minimum_locked_periods, token_economics.maximum_rewarded_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.bond_worker(worker_address=worker_address) stakers.append(staker) # Stake starts next period blockchain.time_travel(periods=1) yield stakers
def test_adjudicator_slashes(agency, testerchain, mock_ursula_reencrypts, token_economics, test_registry, mocker): staker_account = testerchain.staker_account(0) worker_account = testerchain.ursula_account(0) ##### STAKING ESCROW STUFF ##### token_agent, staking_agent, _policy_agent = agency locked_tokens = token_economics.minimum_allowed_locked * 5 # Mock Powerup consumption (Deployer) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, account=testerchain.etherbase_account) testerchain.transacting_power.activate() # The staker receives an initial amount of tokens _txhash = token_agent.transfer( amount=locked_tokens, target_address=staker_account, sender_address=testerchain.etherbase_account) # Mock Powerup consumption (Staker) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, account=staker_account) testerchain.transacting_power.activate() # Deposit: The staker deposits tokens in the StakingEscrow contract. staker = Staker(checksum_address=staker_account, is_me=True, registry=test_registry) staker.initialize_stake( amount=NU(locked_tokens, 'NuNit'), lock_periods=token_economics.minimum_locked_periods) assert staker.locked_tokens(periods=1) == locked_tokens # The staker hasn't set a worker yet assert BlockchainInterface.NULL_ADDRESS == staking_agent.get_worker_from_staker( staker_address=staker_account) _txhash = staking_agent.set_worker(staker_address=staker_account, worker_address=worker_account) assert worker_account == staking_agent.get_worker_from_staker( staker_address=staker_account) assert staker_account == staking_agent.get_staker_from_worker( worker_address=worker_account) ###### END OF STAKING ESCROW STUFF #### adjudicator_agent = AdjudicatorAgent(registry=test_registry) bob_account = testerchain.bob_account bobby = NucypherTokenActor(checksum_address=bob_account, registry=test_registry) ursula = mock_ursula(testerchain, worker_account, mocker=mocker) # Let's create a bad cfrag evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True) assert not adjudicator_agent.was_this_evidence_evaluated(evidence) bobby_old_balance = bobby.token_balance # Mock Powerup consumption (Bob) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, account=bob_account) testerchain.transacting_power.activate() adjudicator_agent.evaluate_cfrag(evidence=evidence, sender_address=bob_account) assert adjudicator_agent.was_this_evidence_evaluated(evidence) investigator_reward = bobby.token_balance - bobby_old_balance assert investigator_reward > 0 assert investigator_reward == token_economics.base_penalty / token_economics.reward_coefficient assert staker.locked_tokens(periods=1) < locked_tokens
def test_investigator_requests_slashing(testerchain, test_registry, agency, #mock_ursula_reencrypts, token_economics, mocker): staker_account = testerchain.staker_account(0) worker_account = testerchain.ursula_account(0) ##### STAKING ESCROW STUFF ##### token_agent = ContractAgency.get_agent(NucypherTokenAgent, registry=test_registry) staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=test_registry) locked_tokens = token_economics.minimum_allowed_locked * 5 # The staker receives an initial amount of tokens tpower = TransactingPower(account=testerchain.etherbase_account, signer=Web3Signer(testerchain.client)) _txhash = token_agent.transfer(amount=locked_tokens, target_address=staker_account, transacting_power=tpower) # Deposit: The staker deposits tokens in the StakingEscrow contract. staker_tpower = TransactingPower(account=staker_account, signer=Web3Signer(testerchain.client)) staker = Staker(transacting_power=staker_tpower, domain=TEMPORARY_DOMAIN, registry=test_registry) staker.initialize_stake(amount=NU(locked_tokens, 'NuNit'), lock_periods=token_economics.minimum_locked_periods) assert staker.locked_tokens(periods=1) == locked_tokens # The staker hasn't bond a worker yet assert NULL_ADDRESS == staking_agent.get_worker_from_staker(staker_address=staker_account) _txhash = staking_agent.bond_worker(transacting_power=staker_tpower, worker_address=worker_account) assert worker_account == staking_agent.get_worker_from_staker(staker_address=staker_account) assert staker_account == staking_agent.get_staker_from_worker(worker_address=worker_account) ###### END OF STAKING ESCROW STUFF #### bob_account = testerchain.bob_account bob_tpower = TransactingPower(account=bob_account, signer=Web3Signer(testerchain.client)) investigator = Investigator(registry=test_registry, transacting_power=bob_tpower, domain=TEMPORARY_DOMAIN) ursula = mock_ursula(testerchain, worker_account, mocker=mocker) # Let's create a bad cfrag evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True) assert not investigator.was_this_evidence_evaluated(evidence) bobby_old_balance = investigator.token_balance investigator.request_evaluation(evidence=evidence) assert investigator.was_this_evidence_evaluated(evidence) investigator_reward = investigator.token_balance - bobby_old_balance assert investigator_reward > 0 assert investigator_reward == token_economics.base_penalty / token_economics.reward_coefficient assert staker.locked_tokens(periods=1) < locked_tokens
def test_investigator_requests_slashing(testerchain, test_registry, agency, mock_ursula_reencrypts, token_economics, mock_transacting_power_activation, mocker): staker_account = testerchain.staker_account(0) worker_account = testerchain.ursula_account(0) ##### STAKING ESCROW STUFF ##### token_agent, staking_agent, _policy_agent = agency locked_tokens = token_economics.minimum_allowed_locked * 5 mock_transacting_power_activation(account=testerchain.etherbase_account, password=INSECURE_DEVELOPMENT_PASSWORD) # The staker receives an initial amount of tokens _txhash = token_agent.transfer( amount=locked_tokens, target_address=staker_account, sender_address=testerchain.etherbase_account) mock_transacting_power_activation(account=staker_account, password=INSECURE_DEVELOPMENT_PASSWORD) # Deposit: The staker deposits tokens in the StakingEscrow contract. staker = Staker(checksum_address=staker_account, is_me=True, registry=test_registry) staker.initialize_stake( amount=NU(locked_tokens, 'NuNit'), lock_periods=token_economics.minimum_locked_periods) assert staker.locked_tokens(periods=1) == locked_tokens # The staker hasn't bond a worker yet assert NULL_ADDRESS == staking_agent.get_worker_from_staker( staker_address=staker_account) _txhash = staking_agent.bond_worker(staker_address=staker_account, worker_address=worker_account) assert worker_account == staking_agent.get_worker_from_staker( staker_address=staker_account) assert staker_account == staking_agent.get_staker_from_worker( worker_address=worker_account) ###### END OF STAKING ESCROW STUFF #### bob_account = testerchain.bob_account investigator = Investigator(registry=test_registry, checksum_address=bob_account) ursula = mock_ursula(testerchain, worker_account, mocker=mocker) # Let's create a bad cfrag evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True) assert not investigator.was_this_evidence_evaluated(evidence) bobby_old_balance = investigator.token_balance mock_transacting_power_activation(account=bob_account, password=INSECURE_DEVELOPMENT_PASSWORD) investigator.request_evaluation(evidence=evidence) assert investigator.was_this_evidence_evaluated(evidence) investigator_reward = investigator.token_balance - bobby_old_balance assert investigator_reward > 0 assert investigator_reward == token_economics.base_penalty / token_economics.reward_coefficient assert staker.locked_tokens(periods=1) < locked_tokens
def test_investigator_requests_slashing(session_testerchain, session_agency, mock_ursula_reencrypts, token_economics, slashing_economics): testerchain = session_testerchain staker_account = testerchain.staker_account(0) worker_account = testerchain.ursula_account(0) ##### STAKING ESCROW STUFF ##### token_agent, staking_agent, _policy_agent = session_agency locked_tokens = token_economics.minimum_allowed_locked * 5 # Mock Powerup consumption (Deployer) testerchain.transacting_power = TransactingPower(blockchain=testerchain, account=testerchain.etherbase_account) testerchain.transacting_power.activate() # The staker receives an initial amount of tokens _txhash = token_agent.transfer(amount=locked_tokens, target_address=staker_account, sender_address=testerchain.etherbase_account) # Mock Powerup consumption (Staker) testerchain.transacting_power = TransactingPower(blockchain=testerchain, account=staker_account) testerchain.transacting_power.activate() # Deposit: The staker deposits tokens in the StakingEscrow contract. staker = Staker(checksum_address=staker_account, is_me=True, blockchain=testerchain) staker.initialize_stake(amount=NU(locked_tokens, 'NuNit'), lock_periods=token_economics.minimum_locked_periods) assert staker.locked_tokens(periods=1) == locked_tokens # The staker hasn't set a worker yet assert BlockchainInterface.NULL_ADDRESS == staking_agent.get_worker_from_staker(staker_address=staker_account) _txhash = staking_agent.set_worker(staker_address=staker_account, worker_address=worker_account) assert worker_account == staking_agent.get_worker_from_staker(staker_address=staker_account) assert staker_account == staking_agent.get_staker_from_worker(worker_address=worker_account) ###### END OF STAKING ESCROW STUFF #### bob_account = testerchain.bob_account investigator = Investigator(blockchain=testerchain, checksum_address=bob_account) ursula = mock_ursula(testerchain, worker_account) # Let's create a bad cfrag evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True) assert not investigator.was_this_evidence_evaluated(evidence) bobby_old_balance = investigator.token_balance # Mock Powerup consumption (Bob) testerchain.transacting_power = TransactingPower(blockchain=testerchain, account=bob_account) testerchain.transacting_power.activate() investigator.request_evaluation(evidence=evidence) assert investigator.was_this_evidence_evaluated(evidence) investigator_reward = investigator.token_balance - bobby_old_balance assert investigator_reward > 0 assert investigator_reward == slashing_economics.base_penalty / slashing_economics.reward_coefficient assert staker.locked_tokens(periods=1) < locked_tokens