Respond to RFCs in PR #866

pull/866/head
Kieran Prasch 2019-04-12 19:59:51 +03:00
parent ef5468f45b
commit 07aa603d30
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
2 changed files with 42 additions and 12 deletions

View File

@ -460,47 +460,62 @@ class Miner(NucypherTokenActor):
""" """
# Validate function input
if additional_periods and expiration: if additional_periods and expiration:
raise ValueError("Pass the number of lock periods or an expiration MayaDT; not both.") raise ValueError("Pass the number of lock periods or an expiration MayaDT; not both.")
stake = self.__stakes[stake_index] # Re-read stakes from blockchain and select stake to divide
self.__read_stakes()
current_stake = self.stakes[stake_index]
# Ensure selected stake is active
if current_stake.is_expired:
raise self.MinerError(f'Cannot dive an expired stake')
# Validate stake division parameters
if expiration: if expiration:
additional_periods = datetime_to_period(datetime=expiration) - stake.end_period additional_periods = datetime_to_period(datetime=expiration) - current_stake.end_period
if additional_periods <= 0: if additional_periods <= 0:
raise self.MinerError("Expiration {} must be at least 1 period from now.".format(expiration)) raise self.MinerError("Expiration {} must be at least 1 period from now.".format(expiration))
if target_value >= stake.value: if target_value >= current_stake.value:
raise self.MinerError(f"Cannot divide stake; Value ({target_value}) must be less " raise self.MinerError(f"Cannot divide stake; Value ({target_value}) must be less "
f"than the existing stake value {stake.value}.") f"than the existing stake value {current_stake.value}.")
current_stake = self.stakes[stake_index] #
# Generate Stakes
#
# Modified Original Stake
new_stake_1 = Stake(owner_address=self.checksum_public_address, new_stake_1 = Stake(owner_address=self.checksum_public_address,
index=len(self.stakes)+1, index=len(self.stakes)+1,
start_period=current_stake.start_period, start_period=current_stake.start_period,
end_period=current_stake.end_period + additional_periods, end_period=current_stake.end_period,
value=target_value, value=target_value,
economics=self.economics) economics=self.economics)
# New Derived Stake
new_stake_2 = Stake(owner_address=self.checksum_public_address, new_stake_2 = Stake(owner_address=self.checksum_public_address,
index=len(self.stakes)+1, index=len(self.stakes)+1,
start_period=current_stake.start_period, start_period=current_stake.start_period,
end_period=current_stake.end_period + additional_periods, end_period=current_stake.end_period + additional_periods,
value=stake.value - target_value, value=current_stake.value - target_value,
economics=self.economics) economics=self.economics)
# Ensure both halves are for valid amounts # Ensure both halves are for valid amounts
new_stake_1.validate_value() new_stake_1.validate_value()
new_stake_2.validate_value() new_stake_2.validate_value()
# Transmit the stake division transaction
tx = self.miner_agent.divide_stake(miner_address=self.checksum_public_address, tx = self.miner_agent.divide_stake(miner_address=self.checksum_public_address,
stake_index=stake_index, stake_index=stake_index,
target_value=int(target_value), target_value=int(target_value),
periods=additional_periods) periods=additional_periods)
self.blockchain.wait_for_receipt(tx) self.blockchain.wait_for_receipt(tx)
self.__read_stakes() # update local on-chain stake cache
# Update stake cache
self.__read_stakes()
return tx return tx
@only_me @only_me
@ -545,8 +560,7 @@ class Miner(NucypherTokenActor):
current_period = self.miner_agent.get_current_period() current_period = self.miner_agent.get_current_period()
stake = Stake(owner_address=self.checksum_public_address, stake = Stake(owner_address=self.checksum_public_address,
index=len(self.stakes)+1, start_period=current_period+1,
start_period=current_period,
end_period=current_period + lock_periods, end_period=current_period + lock_periods,
value=amount, value=amount,
economics=self.economics) economics=self.economics)

View File

@ -136,10 +136,10 @@ class Stake:
def __init__(self, def __init__(self,
owner_address: str, owner_address: str,
index: int,
value: NU, value: NU,
start_period: int, start_period: int,
end_period: int, end_period: int,
index: int = None,
economics: TokenEconomics = None, economics: TokenEconomics = None,
validate_now: bool = True): validate_now: bool = True):
@ -173,6 +173,22 @@ class Stake:
def __eq__(self, other) -> bool: def __eq__(self, other) -> bool:
return bool(self.value == other.value) return bool(self.value == other.value)
@property
def is_active(self):
now = maya.now()
if now >= self.end_datetime:
return False
else:
return True
@property
def is_expired(self):
now = maya.now()
if now >= self.end_datetime:
return True
else:
return False
@classmethod @classmethod
def from_stake_info(cls, def from_stake_info(cls,
owner_address: str, owner_address: str,