mirror of https://github.com/nucypher/nucypher.git
BYOK for stakeholder and worker - Passing collect inflation reward test.
parent
13ed3501dc
commit
f7d780639d
|
@ -2,10 +2,13 @@ import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from web3 import Web3
|
||||||
|
|
||||||
from nucypher.blockchain.eth.actors import StakeHolder, Worker
|
from nucypher.blockchain.eth.actors import StakeHolder, Worker
|
||||||
from nucypher.blockchain.eth.agents import Agency, StakingEscrowAgent
|
from nucypher.blockchain.eth.agents import Agency, StakingEscrowAgent, NucypherTokenAgent
|
||||||
from nucypher.blockchain.eth.interfaces import BlockchainInterface
|
from nucypher.blockchain.eth.interfaces import BlockchainInterface
|
||||||
|
from nucypher.blockchain.eth.token import NU
|
||||||
|
from nucypher.crypto.powers import TransactingPower
|
||||||
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD
|
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,11 +29,39 @@ def staking_software_stakeholder(testerchain,
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
||||||
|
# stakeholder_address = 0xaAa482c790b4301bE18D75A0D1B11B2ACBEF798B
|
||||||
|
stakeholder_private_key = '255f64a948eeb1595b8a2d1e76740f4683eca1c8f1433d13293db9b6e27676cc'
|
||||||
|
address = testerchain.provider.ethereum_tester.add_account(stakeholder_private_key,
|
||||||
|
password=INSECURE_DEVELOPMENT_PASSWORD)
|
||||||
|
|
||||||
|
testerchain.provider.ethereum_tester.unlock_account(address, password=INSECURE_DEVELOPMENT_PASSWORD)
|
||||||
|
|
||||||
|
tx = {'to': address,
|
||||||
|
'from': testerchain.etherbase_account,
|
||||||
|
'value': Web3.toWei('1', 'ether')}
|
||||||
|
|
||||||
|
txhash = testerchain.client.w3.eth.sendTransaction(tx)
|
||||||
|
_receipt = testerchain.wait_for_receipt(txhash)
|
||||||
|
|
||||||
|
# Mock TransactingPower consumption (Etherbase)
|
||||||
|
transacting_power = TransactingPower(account=testerchain.etherbase_account,
|
||||||
|
password=INSECURE_DEVELOPMENT_PASSWORD,
|
||||||
|
blockchain=testerchain)
|
||||||
|
transacting_power.activate()
|
||||||
|
|
||||||
|
token_agent = Agency.get_agent(NucypherTokenAgent)
|
||||||
|
token_agent.transfer(amount=NU(200_000, 'NU').to_nunits(),
|
||||||
|
sender_address=testerchain.etherbase_account,
|
||||||
|
target_address=address)
|
||||||
|
|
||||||
# Create stakeholder from on-chain values given accounts over a web3 provider
|
# Create stakeholder from on-chain values given accounts over a web3 provider
|
||||||
stakeholder = StakeHolder(blockchain=testerchain,
|
stakeholder = StakeHolder(blockchain=testerchain,
|
||||||
funding_account=testerchain.etherbase_account,
|
funding_account=address,
|
||||||
|
funding_password=INSECURE_DEVELOPMENT_PASSWORD,
|
||||||
trezor=False)
|
trezor=False)
|
||||||
|
|
||||||
|
assert stakeholder.funding_power.is_unlocked is True
|
||||||
|
|
||||||
# Teardown
|
# Teardown
|
||||||
yield stakeholder
|
yield stakeholder
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
|
@ -40,7 +71,15 @@ def staking_software_stakeholder(testerchain,
|
||||||
@pytest.fixture(scope='module')
|
@pytest.fixture(scope='module')
|
||||||
def manual_worker(testerchain):
|
def manual_worker(testerchain):
|
||||||
worker_private_key = '4115115f4159db59a06327aa29544c417c52ddb80a4a26517367ff4514e0f694'
|
worker_private_key = '4115115f4159db59a06327aa29544c417c52ddb80a4a26517367ff4514e0f694'
|
||||||
address = testerchain.provider.ethereum_tester.add_account(worker_private_key, password=INSECURE_DEVELOPMENT_PASSWORD)
|
address = testerchain.provider.ethereum_tester.add_account(worker_private_key,
|
||||||
|
password=INSECURE_DEVELOPMENT_PASSWORD)
|
||||||
|
|
||||||
|
tx = {'to': address,
|
||||||
|
'from': testerchain.etherbase_account,
|
||||||
|
'value': Web3.toWei('1', 'ether')}
|
||||||
|
|
||||||
|
txhash = testerchain.client.w3.eth.sendTransaction(tx)
|
||||||
|
_receipt = testerchain.wait_for_receipt(txhash)
|
||||||
yield address
|
yield address
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,7 +108,9 @@ def test_software_stakeholder_configuration(testerchain,
|
||||||
del stakeholder
|
del stakeholder
|
||||||
|
|
||||||
# Restore StakeHolder instance from JSON config
|
# Restore StakeHolder instance from JSON config
|
||||||
the_same_stakeholder = StakeHolder.from_configuration_file(filepath=path, blockchain=testerchain)
|
the_same_stakeholder = StakeHolder.from_configuration_file(filepath=path,
|
||||||
|
funding_password=INSECURE_DEVELOPMENT_PASSWORD,
|
||||||
|
blockchain=testerchain)
|
||||||
|
|
||||||
# Save the JSON config again
|
# Save the JSON config again
|
||||||
the_same_stakeholder.to_configuration_file(filepath=path, override=True)
|
the_same_stakeholder.to_configuration_file(filepath=path, override=True)
|
||||||
|
@ -121,15 +162,17 @@ def test_initialize_stake_with_new_account(staking_software_stakeholder, stake_v
|
||||||
def test_divide_stake(staking_software_stakeholder, token_economics):
|
def test_divide_stake(staking_software_stakeholder, token_economics):
|
||||||
stake = staking_software_stakeholder.stakes[1]
|
stake = staking_software_stakeholder.stakes[1]
|
||||||
|
|
||||||
|
target_value = token_economics.minimum_allowed_locked * 2
|
||||||
original_stake, new_stake = staking_software_stakeholder.divide_stake(address=stake.owner_address,
|
original_stake, new_stake = staking_software_stakeholder.divide_stake(address=stake.owner_address,
|
||||||
password=INSECURE_DEVELOPMENT_PASSWORD,
|
password=INSECURE_DEVELOPMENT_PASSWORD,
|
||||||
index=0,
|
index=0,
|
||||||
duration=10,
|
duration=10,
|
||||||
value=token_economics.minimum_allowed_locked * 2)
|
value=target_value)
|
||||||
|
|
||||||
staking_agent = Agency.get_agent(StakingEscrowAgent)
|
staking_agent = Agency.get_agent(StakingEscrowAgent)
|
||||||
stakes = list(staking_agent.get_all_stakes(staker_address=stake.owner_address))
|
stakes = list(staking_agent.get_all_stakes(staker_address=stake.owner_address))
|
||||||
assert len(stakes) == 2
|
assert len(stakes) == 2
|
||||||
|
assert new_stake.value == target_value
|
||||||
|
|
||||||
|
|
||||||
def test_set_worker(staking_software_stakeholder, manual_worker):
|
def test_set_worker(staking_software_stakeholder, manual_worker):
|
||||||
|
@ -145,20 +188,28 @@ def test_set_worker(staking_software_stakeholder, manual_worker):
|
||||||
def test_collect_inflation_rewards(staking_software_stakeholder, manual_worker, testerchain):
|
def test_collect_inflation_rewards(staking_software_stakeholder, manual_worker, testerchain):
|
||||||
|
|
||||||
# Get stake
|
# Get stake
|
||||||
stake = staking_software_stakeholder.stakes[0]
|
stake = staking_software_stakeholder.stakes[1]
|
||||||
blockchain = staking_software_stakeholder.blockchain
|
|
||||||
|
|
||||||
# Make assigned Worker
|
# Make assigned Worker
|
||||||
worker = Worker(is_me=True,
|
worker = Worker(is_me=True,
|
||||||
worker_address=manual_worker,
|
worker_address=manual_worker,
|
||||||
checksum_address=stake.owner_address,
|
checksum_address=stake.owner_address,
|
||||||
start_working_loop=False,
|
start_working_loop=False,
|
||||||
blockchain=blockchain)
|
blockchain=testerchain)
|
||||||
|
|
||||||
|
# Mock TransactingPower consumption (Worker-Ursula)
|
||||||
|
worker.blockchain.transacting_power = TransactingPower(account=manual_worker, blockchain=testerchain)
|
||||||
|
worker.blockchain.transacting_power.activate()
|
||||||
|
|
||||||
# Wait out stake duration, manually confirming activity once per period.
|
# Wait out stake duration, manually confirming activity once per period.
|
||||||
for period in range(stake.periods_remaining):
|
periods_remaining = stake.end_period - worker.staking_agent.get_current_period()
|
||||||
|
for period in range(periods_remaining):
|
||||||
|
testerchain.time_travel(periods=1)
|
||||||
worker.confirm_activity()
|
worker.confirm_activity()
|
||||||
staking_software_stakeholder.blockchain.time_travel(periods=1)
|
|
||||||
|
# Mock TransactingPower consumption (Staker-Ursula)
|
||||||
|
worker.blockchain.transacting_power = TransactingPower(account=testerchain.etherbase_account, blockchain=testerchain)
|
||||||
|
worker.blockchain.transacting_power.activate()
|
||||||
|
|
||||||
# Collect the staking reward in NU.
|
# Collect the staking reward in NU.
|
||||||
result = staking_software_stakeholder.collect_rewards(staker_address=stake.owner_address,
|
result = staking_software_stakeholder.collect_rewards(staker_address=stake.owner_address,
|
||||||
|
|
Loading…
Reference in New Issue