Relocate miner spawning test utility

pull/270/head
Kieran Prasch 2018-05-09 18:15:53 -07:00 committed by Kieran R Prasch
parent 8cde41da70
commit ceda960832
3 changed files with 32 additions and 30 deletions

View File

@ -94,38 +94,14 @@ class TesterBlockchain(TheBlockchain, NuCypherMinerConfig):
if hours and periods or (hours is None and periods is None):
raise ValueError("Specify either hours or periods.")
if hours:
duration = hours * 60 * 60
elif periods:
duration = (self._hours_per_period * periods) * (60 * 60)
if periods:
hours = (self._hours_per_period * periods)
duration = hours * (60 * 60)
end_timestamp = self.provider.w3.eth.getBlock('latest').timestamp + duration
self.provider.w3.eth.web3.testing.timeTravel(end_timestamp)
end_timestamp = self.provider.w3.eth.getBlock(block_identifier='latest').timestamp + duration
self.provider.w3.eth.web3.testing.timeTravel(timestamp=end_timestamp)
self.provider.w3.eth.web3.testing.mine(1)
def spawn_miners(self, miner_agent, addresses: list, locktime: int, random_amount=False) -> list:
"""
Deposit and lock a random amount of tokens in the miner escrow
from each address, "spawning" new Miners.
"""
from nucypher.blockchain.eth.actors import Miner
miners = list()
for address in addresses:
miner = Miner(miner_agent=miner_agent, address=address)
miners.append(miner)
if random_amount is True:
min_stake = miner_agent._min_allowed_locked #TODO
max_stake = miner_agent._max_allowed_locked
amount = random.randint(min_stake, max_stake)
else:
amount = miner.token_balance() // 2 # stake half
miner.stake(amount=amount, locktime=locktime)
return miners
def _global_airdrop(self, amount: int) -> List[str]:
"""Airdrops from creator address to all other addresses!"""
coinbase, *addresses = self.provider.w3.eth.accounts

View File

@ -1,4 +1,5 @@
import os
import pytest
from nucypher.blockchain.eth.actors import Miner

View File

@ -1,3 +1,4 @@
import random
from typing import List
import pkg_resources
@ -37,9 +38,33 @@ class MockTokenAgent(NuCypherTokenAgent):
return receipts
class MockMinerAgent(MinerAgent):
class MockMinerAgent(MinerAgent, MockNuCypherMinerConfig):
"""MinerAgent with faked config subclass"""
def spawn_random_miners(self, addresses: list) -> list:
"""
Deposit and lock a random amount of tokens in the miner escrow
from each address, "spawning" new Miners.
"""
from nkms.blockchain.eth.actors import Miner
miners = list()
for address in addresses:
miner = Miner(miner_agent=self, address=address)
miners.append(miner)
# stake a random amount
min_stake, balance = self.min_allowed_locked, miner.token_balance()
amount = random.randint(min_stake, balance)
# for a random lock duration
min_locktime, max_locktime = self.min_locked_periods, self.max_minting_periods
periods = random.randint(min_locktime, max_locktime)
miner.stake(amount=amount, periods=periods)
return miners
class MockNuCypherKMSTokenDeployer(NuCypherTokenDeployer):
"""Mock deployer with mock agency"""