Decorator for enforcing actor ownership.

pull/330/head
Kieran R Prasch 2018-06-14 14:06:39 -07:00 committed by Kieran Prasch
parent b3e6ca2f34
commit 9a42ba89a8
2 changed files with 11 additions and 5 deletions

View File

@ -11,6 +11,14 @@ from nucypher.blockchain.eth.agents import NucypherTokenAgent, MinerAgent, Polic
from nucypher.blockchain.eth.constants import calculate_period_duration, datetime_to_period, validate_stake_amount
def only_me(func):
def wrapped(actor=None, *args, **kwargs):
if not actor.is_me:
raise actor.MinerError("Cannot execute contract staking functions with a non-self Miner instance.")
return func(actor, *args, **kwargs)
return wrapped
class NucypherTokenActor:
"""
Concrete base class for any actor that will interface with NuCypher's ethereum smart contracts.
@ -111,6 +119,7 @@ class Miner(NucypherTokenActor):
stakes_reader = self.miner_agent.get_all_stakes(miner_address=self.ether_address)
return stakes_reader
@only_me
def deposit(self, amount: int, lock_periods: int) -> Tuple[str, str]:
"""Public facing method for token locking."""
if not self.is_me:
@ -169,8 +178,6 @@ class Miner(NucypherTokenActor):
@only_me
def __validate_stake(self, amount: int, lock_periods: int) -> bool:
if not self.is_me:
raise self.MinerError("Cannot execute contract staking functions with a non-self Miner instance.")
from .constants import validate_locktime, validate_stake_amount
assert validate_stake_amount(amount=amount)
@ -181,6 +188,7 @@ class Miner(NucypherTokenActor):
else:
return True
@only_me
def stake(self, amount: int, lock_periods: int=None, expiration: maya.MayaDT=None, entire_balance: bool=False) -> dict:
"""
High level staking method for Miners.
@ -192,8 +200,6 @@ class Miner(NucypherTokenActor):
"""
if not self.is_me:
raise self.MinerError("Cannot execute contract staking functions with a non-self Miner instance.")
if lock_periods and expiration:
raise ValueError("Pass the number of lock periods or an expiration MayaDT; not both.")
if entire_balance and amount:

View File

@ -364,7 +364,7 @@ class DeployerCircumflex(ControlCircumflex):
# Build the deployment tx #
#
contract_factory = self.get_contract_factory(contract_name=contract_name)
deploy_transaction = {'from': self.deployer_address, 'gasPrice': self.w3.eth.gasPrice}
deploy_transaction = {'from': self.deployer_address, 'gasPrice': self.w3.eth.gasPrice} # TODO: price?
deploy_bytecode = contract_factory.constructor(*args, **kwargs).buildTransaction(deploy_transaction)
# TODO: Logging