Rename periods -> lock_periods

pull/270/head
Kieran R Prasch 2018-05-15 18:45:20 -07:00
parent a5bf95734d
commit bcc81682c6
7 changed files with 27 additions and 27 deletions

View File

@ -93,10 +93,10 @@ class Miner(TokenActor):
return txhash
def _send_tokens_to_escrow(self, amount, periods) -> str:
def _send_tokens_to_escrow(self, amount, lock_periods) -> str:
"""Send tokes to the escrow from the miner's address"""
deposit_txhash = self.miner_agent.contract.functions.deposit(amount, periods).transact({'from': self.address})
deposit_txhash = self.miner_agent.contract.functions.deposit(amount, lock_periods).transact({'from': self.address})
self.blockchain.wait_for_receipt(deposit_txhash)
@ -104,10 +104,10 @@ class Miner(TokenActor):
return deposit_txhash
def deposit(self, amount: int, periods: int) -> Tuple[str, str]:
def deposit(self, amount: int, lock_periods: int) -> Tuple[str, str]:
"""Public facing method for token locking."""
approve_txhash = self._approve_escrow(amount=amount)
deposit_txhash = self._send_tokens_to_escrow(amount=amount, periods=periods)
deposit_txhash = self._send_tokens_to_escrow(amount=amount, lock_periods=lock_periods)
return approve_txhash, deposit_txhash
@ -162,17 +162,17 @@ class Miner(TokenActor):
return collection_txhash
def __validate_stake(self, amount: int, periods: int) -> bool:
def __validate_stake(self, amount: int, lock_periods: int) -> bool:
assert self.miner_agent.validate_stake_amount(amount=amount)
assert self.miner_agent.validate_locktime(periods=periods)
assert self.miner_agent.validate_locktime(lock_periods=lock_periods)
if not self.token_balance() >= amount:
raise self.StakingError("Insufficient miner token balance ({balance})".format(balance=self.token_balance()))
else:
return True
def stake(self, amount, periods, entire_balance=False):
def stake(self, amount, lock_periods, entire_balance=False):
"""
High level staking method for Miners.
"""
@ -187,9 +187,9 @@ class Miner(TokenActor):
self.address, 0).call()
amount = self.blockchain.provider.w3.toInt(amount)
assert self.__validate_stake(amount=amount, periods=periods)
assert self.__validate_stake(amount=amount, lock_periods=lock_periods)
approve_txhash, initial_deposit_txhash = self.deposit(amount=amount, periods=periods)
approve_txhash, initial_deposit_txhash = self.deposit(amount=amount, lock_periods=lock_periods)
self._transactions.append((datetime.utcnow(), initial_deposit_txhash))
return staking_transactions

View File

@ -93,7 +93,7 @@ class TesterBlockchain(TheBlockchain, NuCypherMinerConfig):
querytime = list(filter(lambda t: bool(t), (hours, seconds, periods)))
if len(querytime) > 1:
raise ValueError("Specify hours, seconds, or periods, not a combination")
raise ValueError("Specify hours, seconds, or lock_periods, not a combination")
if periods:
duration = (self._hours_per_period * periods) * (60 * 60)
@ -102,7 +102,7 @@ class TesterBlockchain(TheBlockchain, NuCypherMinerConfig):
elif seconds:
duration = seconds
else:
raise ValueError("Specify either hours, seconds, or periods.")
raise ValueError("Specify either hours, seconds, or lock_periods.")
end_timestamp = self.provider.w3.eth.getBlock(block_identifier='latest').timestamp + duration
self.provider.w3.eth.web3.testing.timeTravel(timestamp=end_timestamp)

View File

@ -72,17 +72,17 @@ class NuCypherMinerConfig:
self.__validate(rulebook=rulebook)
return all(rulebook)
def validate_locktime(self, periods: int, raise_on_fail=True) -> bool:
def validate_locktime(self, lock_periods: int, raise_on_fail=True) -> bool:
rulebook = (
(periods >= self.min_locked_periods,
(lock_periods >= self.min_locked_periods,
'Locktime ({locktime}) too short; must be at least {minimum}'
.format(minimum=self.min_locked_periods, locktime=periods)),
.format(minimum=self.min_locked_periods, locktime=lock_periods)),
(periods <= self.max_minting_periods,
(lock_periods <= self.max_minting_periods,
'Locktime ({locktime}) too long; must be no more than {maximum}'
.format(maximum=self.max_minting_periods, locktime=periods)),
.format(maximum=self.max_minting_periods, locktime=lock_periods)),
)
if raise_on_fail is True:

View File

@ -6,7 +6,7 @@ class BlockchainArrangement:
A relationship between Alice and a single Ursula as part of Blockchain Policy
"""
def __init__(self, author: str, miner: str, value: int, periods: int, arrangement_id: bytes=None):
def __init__(self, author: str, miner: str, value: int, lock_periods: int, arrangement_id: bytes=None):
self.id = arrangement_id
@ -17,11 +17,11 @@ class BlockchainArrangement:
self.miner = miner
# Arrangement value, rate, and duration
rate = value // periods
rate = value // lock_periods
self._rate = rate
self.value = value
self.periods = periods # TODO: datetime -> duration in blocks
self.lock_periods = lock_periods # TODO: datetime -> duration in blocks
self.is_published = False
@ -40,7 +40,7 @@ class BlockchainArrangement:
txhash = self.policy_agent.transact(payload).createPolicy(self.id,
self.miner.address,
self.periods)
self.lock_periods)
self.policy_agent._blockchain._chain.wait.for_receipt(txhash)
@ -64,16 +64,16 @@ class BlockchainPolicy:
def __init__(self):
self._arrangements = list()
def publish_arrangement(self, miner, periods: int, rate: int, arrangement_id: bytes=None) -> 'BlockchainArrangement':
def publish_arrangement(self, miner, lock_periods: int, rate: int, arrangement_id: bytes=None) -> 'BlockchainArrangement':
"""
Create a new arrangement to carry out a blockchain policy for the specified rate and time.
"""
value = rate * periods
value = rate * lock_periods
arrangement = BlockchainArrangement(author=self,
miner=miner,
value=value,
periods=periods)
lock_periods=lock_periods)
self._arrangements[arrangement.id] = {arrangement_id: arrangement}
return arrangement
@ -87,7 +87,7 @@ class BlockchainPolicy:
duration = end_block - start_block
miner = Miner(address=miner_address, miner_agent=self.policy_agent.miner_agent)
arrangement = BlockchainArrangement(author=self, miner=miner, periods=duration)
arrangement = BlockchainArrangement(author=self, miner=miner, lock_periods=duration)
arrangement.is_published = True
return arrangement

View File

@ -53,7 +53,7 @@ class Arrangement(BlockchainArrangement):
policy_duration = arrangement_delta.days
super().__init__(author=self.alice, miner=ursula,
value=self.deposit, periods=policy_duration,
value=self.deposit, lock_periods=policy_duration,
arrangement_id=self._make_arrangement_id())
def __bytes__(self):

View File

@ -19,7 +19,7 @@ def test_miner_locking_tokens(chain, miner, mock_miner_agent):
assert mock_miner_agent.min_allowed_locked < miner.token_balance(), "Insufficient miner balance"
miner.stake(amount=mock_miner_agent.min_allowed_locked, # Lock the minimum amount of tokens
periods=mock_miner_agent.min_locked_periods) # ... for the fewest number of periods
lock_periods=mock_miner_agent.min_locked_periods) # ... for the fewest number of periods
# Verify that the escrow is "approved" to receive tokens
assert mock_miner_agent.token_agent.contract.functions.allowance(miner.address, mock_miner_agent.contract_address).call() == 0

View File

@ -61,7 +61,7 @@ class MockMinerAgent(MinerAgent, MockNuCypherMinerConfig):
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)
miner.stake(amount=amount, lock_periods=periods)
return miners