stake and locktime validators

pull/270/head
Kieran Prasch 2018-05-07 18:11:46 -07:00 committed by Kieran R Prasch
parent 4ec9df051f
commit c5d5cd0f3a
2 changed files with 66 additions and 16 deletions

View File

@ -166,6 +166,16 @@ class Miner(TokenActor):
return reward_txhash
def __validate_stake(self, amount: int, locktime: int) -> bool:
assert self.miner_agent.validate_stake_amount(amount=amount)
assert self.miner_agent.validate_locktime(periods=locktime)
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, locktime, entire_balance=False):
"""
High level staking method for Miners.
@ -176,19 +186,12 @@ class Miner(TokenActor):
if entire_balance and amount:
raise self.StakingError("Specify an amount or entire balance, not both")
if not locktime >= 0:
min_stake_time = self.miner_agent._deployer._min_release_periods
raise self.StakingError('Locktime must be at least {}'.format(min_stake_time))
if entire_balance is True:
balance_bytes = self.miner_agent.contract.functions.getMinerInfo(self.miner_agent.MinerInfo.VALUE.value,
self.address, 0).call()
self.address, 0).call()
amount = self.blockchain._chain.web3.toInt(balance_bytes)
else:
if not amount > 0:
raise self.StakingError('Staking amount must be greater than zero.')
assert self.__validate_stake(amount, locktime)
approve_txhash, initial_deposit_txhash = self.deposit(amount=amount, locktime=locktime)
self._transactions.append((datetime.utcnow(), initial_deposit_txhash))

View File

@ -1,4 +1,11 @@
NULL_ADDRESS = '0x' + '0' * 40
class NuCypherTokenConfig:
class TokenConfigError(ValueError):
pass
__subdigits = 18
_M = 10 ** __subdigits # Unit designation
__initial_supply = int(1e9) * _M # Initial token supply
@ -11,14 +18,17 @@ class NuCypherTokenConfig:
class NuCypherMinerConfig:
class MinerConfigError(ValueError):
pass
_hours_per_period = 24 # Hours in single period
_min_locked_periods = 30 # 720 Hours minimum
_min_locked_periods = 30 # 720 Hours minimum
__max_minting_periods = 365 # Maximum number of periods
_min_allowed_locked = 15000 * NuCypherTokenConfig._M
_max_allowed_locked = int(4e6) * NuCypherTokenConfig._M
_null_addr = '0x' + '0' * 40
__remaining_supply = NuCypherTokenConfig._remaining_supply
__mining_coeff = [ # TODO
@ -31,10 +41,6 @@ class NuCypherMinerConfig:
_max_allowed_locked
]
@property
def null_address(self):
return self._null_addr
@property
def mining_coefficient(self):
return self.__mining_coeff
@ -42,3 +48,44 @@ class NuCypherMinerConfig:
@property
def remaining_supply(self):
return self.__remaining_supply
def __validate(self, rulebook) -> bool:
for rule, failure_message in rulebook:
if not rule:
raise self.MinerConfigError(failure_message)
return True
def validate_stake_amount(self, amount: int, raise_on_fail=True) -> bool:
rulebook = (
(amount >= self._min_allowed_locked,
'Stake amount too low; ({amount}) must be at least {minimum}'
.format(minimum=self._min_allowed_locked, amount=amount)),
(amount <= self._max_allowed_locked,
'Stake amount too high; ({amount}) must be no more than {maximum}.'
.format(maximum=self._max_allowed_locked, amount=amount)),
)
if raise_on_fail is True:
self.__validate(rulebook=rulebook)
return all(rulebook)
def validate_locktime(self, periods: int, raise_on_fail=True) -> bool:
rulebook = (
(periods >= self._min_locked_periods,
'Locktime ({locktime}) too long; must be at least {minimum}'
.format(minimum=self._min_locked_periods, locktime=periods)),
(periods <= self.__max_minting_periods,
'Locktime ({locktime}) too short; must be no more than {maximum}'
.format(maximum=self._min_locked_periods, locktime=periods)),
)
if raise_on_fail is True:
self.__validate(rulebook=rulebook)
return all(rulebook)