Proof of Concept fast CLI test.

pull/1949/head
Kieran R. Prasch 2020-05-02 08:28:03 -07:00
parent 923832b3c5
commit cf1c40c919
2 changed files with 76 additions and 238 deletions

View File

@ -14,252 +14,91 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
import pytest
from web3 import Web3
from nucypher.blockchain.economics import StandardTokenEconomics, BaseEconomics
import pytest
from hexbytes import HexBytes
from nucypher.blockchain.economics import EconomicsFactory
from nucypher.blockchain.eth.actors import Bidder
from nucypher.blockchain.eth.agents import ContractAgency
from nucypher.blockchain.eth.token import NU
from nucypher.cli.commands.worklock import worklock
from nucypher.config.constants import NUCYPHER_ENVVAR_KEYRING_PASSWORD
from nucypher.crypto.powers import TransactingPower
from nucypher.utilities.sandbox.constants import (
TEST_PROVIDER_URI,
TEMPORARY_DOMAIN
TEMPORARY_DOMAIN, INSECURE_DEVELOPMENT_PASSWORD
)
#
# @pytest.fixture(scope='module', autouse=True)
# def llamas(module_mocker):
# module_mocker.patch.object(RPC, 'eth_estimateGas', autospec=True)
#
ENV = {NUCYPHER_ENVVAR_KEYRING_PASSWORD: INSECURE_DEVELOPMENT_PASSWORD}
YES = 'Y\n'
def test_status(click_runner):
@pytest.fixture(scope='module')
def surrogate_bidder(mock_testerchain, test_registry):
address = mock_testerchain.unassigned_accounts[0]
bidder = Bidder(checksum_address=address, registry=test_registry)
return bidder
command = ('status',
# '--registry-filepath', agency_local_registry.filepath,
@pytest.fixture(scope='module')
def mock_worklock_agent(module_mocker, mock_testerchain, token_economics):
blocktime = mock_testerchain.w3.eth.getBlock(block_identifier='latest')
now = blocktime.timestamp
current_block = blocktime.number
class MockWorkLockAgent:
# Fixtures
FAKE_RECEIPT = {'transactionHash': HexBytes(b'FAKE29890FAKE8349804'),
'gasUsed': 1,
'blockNumber': current_block,
'blockHash': HexBytes(b'FAKE43434343FAKE43443434')}
# Attributes
start_bidding_date = now - 10
end_bidding_date = now + 10
minimum_allowed_bid = token_economics.worklock_min_allowed_bid
blockchain = mock_testerchain
# Methods
bid = lambda *args, **kwargs: MockWorkLockAgent.FAKE_RECEIPT
eth_to_tokens = lambda *args, **kwargs: 1
get_deposited_eth = lambda *args, **kwargs: 1
special_agent = MockWorkLockAgent()
module_mocker.patch.object(ContractAgency, 'get_agent', return_value=special_agent)
module_mocker.patch.object(EconomicsFactory, 'get_economics', return_value=token_economics)
# TODO: Consider removal of this mock
module_mocker.patch.object(TransactingPower, 'activate', return_value=True)
return special_agent
def test_bid(mocker,
mock_worklock_agent,
click_runner,
mock_testerchain,
token_economics,
test_registry_source_manager,
test_registry,
surrogate_bidder):
mock_bidder = mocker.spy(Bidder, 'place_bid')
bid_value = 50_000
command = ('bid',
'--provider', TEST_PROVIDER_URI,
'--network', TEMPORARY_DOMAIN)
result = click_runner.invoke(worklock, command, catch_exceptions=False)
'--network', TEMPORARY_DOMAIN,
'--force',
'--bidder-address', surrogate_bidder.checksum_address,
'--value', bid_value)
result = click_runner.invoke(worklock, command, catch_exceptions=False, input=YES, env=ENV)
assert result.exit_code == 0
# assert str(NU.from_nunits(token_economics.worklock_supply)) in result.output
# assert str(Web3.fromWei(token_economics.worklock_min_allowed_bid, 'ether')) in result.output
#
#
# def test_bid(click_runner, mock_testerchain, agency_local_registry, token_economics, bids):
#
# # Wait until biding window starts
# mock_testerchain.time_travel(seconds=90)
#
# base_command = ('bid',
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--force')
#
# worklock_agent = ContractAgency.get_agent(WorkLockAgent, registry=agency_local_registry)
# total_bids = 0
# # Multiple bidders
# for bidder, bid_eth_value in bids.items():
# pre_bid_balance = mock_testerchain.client.get_balance(bidder)
# assert pre_bid_balance > to_wei(bid_eth_value, 'ether')
#
# command = (*base_command, '--bidder-address', bidder, '--value', bid_eth_value)
# 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
#
# post_bid_balance = mock_testerchain.client.get_balance(bidder)
# difference = pre_bid_balance - post_bid_balance
# assert difference >= to_wei(bid_eth_value, 'ether')
#
# total_bids += to_wei(bid_eth_value, 'ether')
# assert mock_testerchain.client.get_balance(worklock_agent.contract_address) == total_bids
#
#
# def test_cancel_bid(click_runner, mock_testerchain, agency_local_registry, token_economics, bids):
#
# bidders = list(bids.keys())
#
# bidder = bidders[-1]
# agent = ContractAgency.get_agent(WorkLockAgent, registry=agency_local_registry)
#
# command = ('cancel-bid',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--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
# assert not agent.get_deposited_eth(bidder) # No more bid
#
# # Wait until the end of the bidding period
# mock_testerchain.time_travel(seconds=token_economics.bidding_duration + 2)
#
# bidder = bidders[-2]
# command = ('cancel-bid',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--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
# assert not agent.get_deposited_eth(bidder) # No more bid
#
#
# def test_post_initialization(click_runner, mock_testerchain, agency_local_registry, token_economics):
#
# # Wait until the end of the cancellation period
# mock_testerchain.time_travel(seconds=token_economics.cancellation_window_duration+2)
#
# bidder = mock_testerchain.client.accounts[0]
# agent = ContractAgency.get_agent(WorkLockAgent, registry=agency_local_registry)
# assert not agent.is_claiming_available()
# assert not agent.bidders_checked()
#
# command = ('enable-claiming',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--force',
# '--network', TEMPORARY_DOMAIN,
# '--gas-limit', 100000)
#
# 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
# assert agent.is_claiming_available()
# assert agent.bidders_checked()
#
#
# def test_claim(click_runner, mock_testerchain, agency_local_registry, token_economics):
# agent = ContractAgency.get_agent(WorkLockAgent, registry=agency_local_registry)
#
# bidder = mock_testerchain.client.accounts[2]
# command = ('claim',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--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
#
# whale = mock_testerchain.client.accounts[0]
# assert agent.get_available_compensation(checksum_address=whale) > 0
# command = ('claim',
# '--bidder-address', whale,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--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
# assert agent.get_available_compensation(checksum_address=whale) == 0
#
# # TODO: Check successful new stake in StakingEscrow
#
#
# def test_remaining_work(click_runner, mock_testerchain, agency_local_registry, token_economics):
# bidder = mock_testerchain.client.accounts[2]
#
# # Ensure there is remaining work one layer below
# worklock_agent = ContractAgency.get_agent(WorkLockAgent, registry=agency_local_registry)
# remaining_work = worklock_agent.get_remaining_work(checksum_address=bidder)
# assert remaining_work > 0
#
# command = ('remaining-work',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN)
#
# result = click_runner.invoke(worklock, command, catch_exceptions=False)
# assert result.exit_code == 0
#
# # Ensure were displaying the bidder address and remaining work in the output
# assert bidder in result.output
# assert str(remaining_work) in result.output
#
#
# def test_refund(click_runner, mock_testerchain, agency_local_registry, token_economics):
#
# bidder = mock_testerchain.client.accounts[2]
# worker_address = mock_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
# mock_testerchain.time_travel(periods=1)
#
# command = ('refund',
# '--bidder-address', bidder,
# '--registry-filepath', agency_local_registry.filepath,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN,
# '--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
#
#
# def test_participant_status(click_runner, mock_testerchain, agency_local_registry, token_economics):
# bidder = Bidder(checksum_address=mock_testerchain.client.accounts[2], registry=agency_local_registry)
#
# command = ('status',
# '--registry-filepath', agency_local_registry.filepath,
# '--bidder-address', bidder.checksum_address,
# '--provider', TEST_PROVIDER_URI,
# '--network', TEMPORARY_DOMAIN)
#
# result = click_runner.invoke(worklock, command, catch_exceptions=False)
# assert result.exit_code == 0
#
# # Bidder-specific data is displayed
# assert bidder.checksum_address in result.output
# assert str(bidder.remaining_work) in result.output
# assert str(bidder.available_refund) in result.output
#
# # Worklock economics are displayed
# assert str(token_economics.worklock_boosting_refund_rate) in result.output
# assert str(NU.from_nunits(token_economics.worklock_supply)) in result.output
# OK - Let's see what happened
mock_bidder.assert_called_once()
mock_bidder.assert_called_once_with(surrogate_bidder, value=NU.from_tokens(bid_value).to_nunits())

View File

@ -398,10 +398,10 @@ def federated_ursulas(ursula_federated_test_config):
#
@pytest.fixture(scope='module')
def token_economics(testerchain):
def token_economics():
# Get current blocktime
blockchain = BlockchainInterfaceFactory.get_interface(provider_uri=testerchain.provider_uri)
blockchain = BlockchainInterfaceFactory.get_interface()
now = blockchain.w3.eth.getBlock(block_identifier='latest').timestamp
# Calculate instant start time
@ -472,7 +472,6 @@ def _make_testerchain(mock_backend: bool = False) -> TesterBlockchain:
@pytest.fixture(scope='module') # FIXME : make session
def _testerchain() -> TesterBlockchain:
assert False
testerchain = _make_testerchain()
yield testerchain