mirror of https://github.com/nucypher/nucypher.git
[KMS-ETH]- Reorganize test for PolicyManager usage; Reorg test utilities
parent
9f4c960963
commit
48e3d75b84
|
@ -20,9 +20,21 @@ class TesterBlockchain(TheBlockchain):
|
||||||
while self._chain.web3.eth.getBlock(self._chain.web3.eth.blockNumber).timestamp < end_timestamp:
|
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)
|
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):
|
class MockNuCypherKMSTokenDeployer(NuCypherKMSTokenDeployer):
|
||||||
_M = 10 ** 6 #TODO
|
_M = 10 ** 6 #TODO: Unify with config class's _M
|
||||||
|
|
||||||
def _global_airdrop(self, amount: int):
|
def _global_airdrop(self, amount: int):
|
||||||
"""Airdrops from creator address to all other addresses!"""
|
"""Airdrops from creator address to all other addresses!"""
|
||||||
|
@ -31,12 +43,14 @@ class MockNuCypherKMSTokenDeployer(NuCypherKMSTokenDeployer):
|
||||||
|
|
||||||
def txs():
|
def txs():
|
||||||
for address in addresses:
|
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():
|
receipts = []
|
||||||
self.blockchain._chain.wait.for_receipt(tx, timeout=10)
|
for tx in txs(): # One at a time
|
||||||
|
receipt = self.blockchain.wait_for_receipt(tx)
|
||||||
return self # for method chaining
|
receipts.append(receipt)
|
||||||
|
return receipts
|
||||||
|
|
||||||
|
|
||||||
class MockNuCypherMinerConfig(NuCypherMinerConfig):
|
class MockNuCypherMinerConfig(NuCypherMinerConfig):
|
||||||
|
@ -52,15 +66,3 @@ class MockMinerEscrowDeployer(MinerEscrowDeployer, MockNuCypherMinerConfig):
|
||||||
class MockMinerAgent(MinerAgent):
|
class MockMinerAgent(MinerAgent):
|
||||||
"""MinerAgent with faked config subclass"""
|
"""MinerAgent with faked config subclass"""
|
||||||
_deployer = MockMinerEscrowDeployer
|
_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)
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import pytest
|
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.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():
|
def testerchain():
|
||||||
chain = TesterBlockchain()
|
chain = TesterBlockchain()
|
||||||
yield chain
|
yield chain
|
||||||
|
@ -13,7 +14,7 @@ def testerchain():
|
||||||
TheBlockchain._TheBlockchain__instance = None
|
TheBlockchain._TheBlockchain__instance = None
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture()
|
||||||
def mock_token_deployer(testerchain):
|
def mock_token_deployer(testerchain):
|
||||||
token_deployer = MockNuCypherKMSTokenDeployer(blockchain=testerchain)
|
token_deployer = MockNuCypherKMSTokenDeployer(blockchain=testerchain)
|
||||||
token_deployer.arm()
|
token_deployer.arm()
|
||||||
|
@ -21,23 +22,38 @@ def mock_token_deployer(testerchain):
|
||||||
yield token_deployer
|
yield token_deployer
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture()
|
||||||
def mock_miner_escrow_deployer(token_agent):
|
def mock_miner_escrow_deployer(token_agent):
|
||||||
escrow = MockMinerEscrowDeployer(token_agent)
|
escrow = MockMinerEscrowDeployer(token_agent=token_agent)
|
||||||
escrow.arm()
|
escrow.arm()
|
||||||
|
import pdb; pdb.set_trace()
|
||||||
|
|
||||||
escrow.deploy()
|
escrow.deploy()
|
||||||
yield escrow
|
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 #
|
# Unused args preserve fixture dependency order #
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture()
|
||||||
def token_agent(testerchain, mock_token_deployer):
|
def token_agent(testerchain, mock_token_deployer):
|
||||||
token = NuCypherKMSTokenAgent(blockchain=testerchain)
|
token = NuCypherKMSTokenAgent(blockchain=testerchain)
|
||||||
yield token
|
yield token
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture()
|
||||||
def mock_miner_agent(token_agent, mock_token_deployer, mock_miner_escrow_deployer):
|
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
|
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
|
||||||
|
|
Loading…
Reference in New Issue