[KMS-ETH]- Reorganize test for PolicyManager usage; Reorg test utilities

pull/195/head^2
Kieran Prasch 2018-04-06 17:15:45 -07:00
parent 9f4c960963
commit 48e3d75b84
2 changed files with 45 additions and 27 deletions

View File

@ -20,9 +20,21 @@ class TesterBlockchain(TheBlockchain):
while self._chain.web3.eth.getBlock(self._chain.web3.eth.blockNumber).timestamp < end_timestamp:
self._chain.wait.for_block(self._chain.web3.eth.blockNumber + step)
def spawn_miners(self, miner_agent: MinerAgent, addresses: list, locktime: int, m: int) -> None:
"""
Deposit and lock a random amount of tokens in the miner escrow
from each address, "spawning" new Miners.
"""
for address in addresses:
miner = Miner(miner_agent=miner_agent, address=address)
amount = miner.token_balance() // 2
# amount = (10 + random.randrange(9000)) * m
miner.stake(amount=amount, locktime=locktime, auto_switch_lock=True)
class MockNuCypherKMSTokenDeployer(NuCypherKMSTokenDeployer):
_M = 10 ** 6 #TODO
_M = 10 ** 6 #TODO: Unify with config class's _M
def _global_airdrop(self, amount: int):
"""Airdrops from creator address to all other addresses!"""
@ -31,12 +43,14 @@ class MockNuCypherKMSTokenDeployer(NuCypherKMSTokenDeployer):
def txs():
for address in addresses:
yield self._contract.transact({'from': self._creator}).transfer(address, amount * (10 ** 6))
txhash = self._contract.transact({'from': self._creator}).transfer(address, amount)
yield txhash
for tx in txs():
self.blockchain._chain.wait.for_receipt(tx, timeout=10)
return self # for method chaining
receipts = []
for tx in txs(): # One at a time
receipt = self.blockchain.wait_for_receipt(tx)
receipts.append(receipt)
return receipts
class MockNuCypherMinerConfig(NuCypherMinerConfig):
@ -52,15 +66,3 @@ class MockMinerEscrowDeployer(MinerEscrowDeployer, MockNuCypherMinerConfig):
class MockMinerAgent(MinerAgent):
"""MinerAgent with faked config subclass"""
_deployer = MockMinerEscrowDeployer
def spawn_miners(addresses: list, miner_agent: MinerAgent, m: int, locktime: int) -> None:
"""
Deposit and lock a random amount of tokens in the miner escrow
from each address, "spawning" new Miners.
"""
# Create n Miners
for address in addresses:
miner = Miner(miner_agent=miner_agent, address=address)
amount = (10+random.randrange(9000)) * m
miner.lock(amount=amount, locktime=locktime)

View File

@ -1,11 +1,12 @@
import pytest
from nkms_eth.agents import NuCypherKMSTokenAgent, MinerAgent
from nkms_eth.agents import NuCypherKMSTokenAgent, MinerAgent, PolicyAgent
from nkms_eth.blockchain import TheBlockchain
from nkms_eth.utilities import TesterBlockchain, MockNuCypherKMSTokenDeployer, MockMinerEscrowDeployer
from nkms_eth.deployers import PolicyManagerDeployer
from nkms_eth.utilities import TesterBlockchain, MockNuCypherKMSTokenDeployer, MockMinerEscrowDeployer, MockMinerAgent
@pytest.fixture(scope='function')
@pytest.fixture()
def testerchain():
chain = TesterBlockchain()
yield chain
@ -13,7 +14,7 @@ def testerchain():
TheBlockchain._TheBlockchain__instance = None
@pytest.fixture(scope='function')
@pytest.fixture()
def mock_token_deployer(testerchain):
token_deployer = MockNuCypherKMSTokenDeployer(blockchain=testerchain)
token_deployer.arm()
@ -21,23 +22,38 @@ def mock_token_deployer(testerchain):
yield token_deployer
@pytest.fixture(scope='function')
@pytest.fixture()
def mock_miner_escrow_deployer(token_agent):
escrow = MockMinerEscrowDeployer(token_agent)
escrow = MockMinerEscrowDeployer(token_agent=token_agent)
escrow.arm()
import pdb; pdb.set_trace()
escrow.deploy()
yield escrow
@pytest.fixture()
def mock_policy_manager_deployer(token_agent):
policy_manager_deployer = PolicyManagerDeployer(miner_agent=mock_miner_agent)
policy_manager_deployer.arm()
policy_manager_deployer.deploy()
yield policy_manager_deployer
# Unused args preserve fixture dependency order #
@pytest.fixture(scope='function')
@pytest.fixture()
def token_agent(testerchain, mock_token_deployer):
token = NuCypherKMSTokenAgent(blockchain=testerchain)
yield token
@pytest.fixture(scope='function')
@pytest.fixture()
def mock_miner_agent(token_agent, mock_token_deployer, mock_miner_escrow_deployer):
miner_agent = MinerAgent(token_agent)
miner_agent = MinerAgent(token_agent=token_agent)
yield miner_agent
@pytest.fixture()
def mock_policy_agent(mock_miner_agent, token_agent, mock_token_deployer, mock_miner_escrow_deployer):
policy_agent = PolicyAgent(miner_agent=mock_miner_agent)
yield policy_agent