def __init__(self, blockchain: Blockchain, deployer_address: str = None, bare: bool = True) -> None: self.blockchain = blockchain self.__deployer_address = NO_DEPLOYER_ADDRESS if deployer_address: self.deployer_address = deployer_address if not bare: self.token_agent = NucypherTokenAgent(blockchain=blockchain) self.miner_agent = MinerAgent(blockchain=blockchain) self.policy_agent = PolicyAgent(blockchain=blockchain) self.adjudicator_agent = MiningAdjudicatorAgent( blockchain=blockchain) self.user_escrow_deployers = dict() self.deployers = { NucypherTokenDeployer.contract_name: self.deploy_token_contract, MinerEscrowDeployer.contract_name: self.deploy_miner_contract, PolicyManagerDeployer.contract_name: self.deploy_policy_contract, UserEscrowProxyDeployer.contract_name: self.deploy_escrow_proxy, MiningAdjudicatorDeployer.contract_name: self.deploy_mining_adjudicator_contract, } self.log = Logger("Deployment-Actor")
def __init__(self, blockchain: Blockchain, deployer_address: str = None, allocation_registry: AllocationRegistry = None, bare: bool = True ) -> None: self.blockchain = blockchain self.__deployer_address = NO_DEPLOYER_ADDRESS if deployer_address: self.deployer_address = deployer_address if not bare: self.token_agent = NucypherTokenAgent(blockchain=blockchain) self.miner_agent = MinerAgent(blockchain=blockchain) self.policy_agent = PolicyAgent(blockchain=blockchain) self.allocation_registy = allocation_registry self.user_escrow_deployers = dict() self.deployers = { NucypherTokenDeployer.contract_name: self.deploy_token_contract, MinerEscrowDeployer.contract_name: self.deploy_miner_contract, PolicyManagerDeployer.contract_name: self.deploy_policy_contract, UserEscrowProxyDeployer.contract_name: self.deploy_escrow_proxy, }
def __init__(self, blockchain: BlockchainInterface, deployer_address: str = None, client_password: str = None, bare: bool = True) -> None: self.blockchain = blockchain self.__deployer_address = NO_DEPLOYER_ADDRESS self.deployer_address = deployer_address self.checksum_address = self.deployer_address if not bare: self.token_agent = NucypherTokenAgent(blockchain=blockchain) self.staking_agent = StakingEscrowAgent(blockchain=blockchain) self.policy_agent = PolicyAgent(blockchain=blockchain) self.adjudicator_agent = AdjudicatorAgent(blockchain=blockchain) self.user_escrow_deployers = dict() self.deployers = {d.contract_name: d for d in self.deployer_classes} blockchain.transacting_power = TransactingPower( blockchain=blockchain, account=deployer_address, password=client_password) blockchain.transacting_power.activate() self.log = Logger("Deployment-Actor")
def __init__(self, checksum_address: str, policy_agent: PolicyAgent = None, economics: TokenEconomics = None, *args, **kwargs) -> None: """ :param policy_agent: A policy agent with the blockchain attached; If not passed, a default policy agent and blockchain connection will be created from default values. """ super().__init__(checksum_address=checksum_address, *args, **kwargs) # From defaults if not policy_agent: self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.staking_agent = StakingEscrowAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain) # Injected else: self.policy_agent = policy_agent self.economics = economics or TokenEconomics()
def __init__(self, checksum_address: str, policy_agent: PolicyAgent = None, *args, **kwargs) -> None: """ :param policy_agent: A policy agent with the blockchain attached; If not passed, A default policy agent and blockchain connection will be created from default values. """ if policy_agent is None: # From defaults self.token_agent = NucypherTokenAgent() self.miner_agent = MinerAgent(token_agent=self.token_agent) self.policy_agent = PolicyAgent(miner_agent=self.miner_agent) else: # From agent self.policy_agent = policy_agent self.miner_agent = policy_agent.miner_agent super().__init__(token_agent=self.policy_agent.token_agent, checksum_address=checksum_address, *args, **kwargs)
def __init__(self, checksum_address: str, policy_agent=None, economics: TokenEconomics = None, *args, **kwargs) -> None: """ :param policy_agent: A policy agent with the blockchain attached; If not passed, a default policy agent and blockchain connection will be created from default values. """ super().__init__(checksum_address=checksum_address, *args, **kwargs) if not policy_agent: # From defaults self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain) else: # Injected self.policy_agent = policy_agent if not economics: economics = TokenEconomics() self.economics = economics
def collect_policy_reward(self, collector_address=None, policy_agent: PolicyAgent = None): """Collect rewarded ETH""" policy_agent = policy_agent if policy_agent is not None else PolicyAgent(blockchain=self.blockchain) withdraw_address = collector_address or self.checksum_public_address policy_reward_txhash = policy_agent.collect_policy_reward(collector_address=withdraw_address, miner_address=self.checksum_public_address) self._transaction_cache.append((datetime.utcnow(), policy_reward_txhash)) return policy_reward_txhash
def connect_to_contracts(self, simulation: bool = False): """Initialize contract agency and set them on config""" if simulation is True: self.blockchain.interface._registry._swap_registry( filepath=self.sim_registry_filepath) self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(token_agent=self.token_agent) self.policy_agent = PolicyAgent(miner_agent=self.miner_agent)
def __init__(self, allocation_registry: AllocationRegistry = None, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain) self.__beneficiary_address = NO_BENEFICIARY self.__allocation_registry = allocation_registry or self.__allocation_registry( )
def test_nucypher_deploy_contracts(testerchain, click_runner, mock_primary_registry_filepath): # We start with a blockchain node, and nothing else... assert not os.path.isfile(mock_primary_registry_filepath) command = ('contracts', '--registry-outfile', mock_primary_registry_filepath, '--provider-uri', TEST_PROVIDER_URI, '--poa') user_input = 'Y\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' * 8 result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # Ensure there is a report on each contract for registry_name in Deployer.contract_names: assert registry_name in result.output # Check that the primary contract registry was written # and peek at some of the registered entries assert os.path.isfile(mock_primary_registry_filepath) with open(mock_primary_registry_filepath, 'r') as file: # Ensure every contract's name was written to the file, somehow raw_registry_data = file.read() for registry_name in Deployer.contract_names: assert registry_name in raw_registry_data # Ensure the Registry is JSON deserializable registry_data = json.loads(raw_registry_data) # and that is has the correct number of entries assert len(registry_data) == 9 # Read several records token_record, escrow_record, dispatcher_record, *other_records = registry_data registered_name, registered_address, registered_abi = token_record token_agent = NucypherTokenAgent() assert token_agent.contract_name == registered_name assert token_agent.registry_contract_name == registered_name assert token_agent.contract_address == registered_address # Now show that we can use contract Agency and read from the blockchain assert token_agent.get_balance() == 0 miner_agent = MinerAgent() assert miner_agent.get_current_period() # and at least the others can be instantiated assert PolicyAgent() assert MiningAdjudicatorAgent() testerchain.sever_connection()
def collect_policy_reward(self, collector_address=None, policy_agent: PolicyAgent = None): """Collect rewarded ETH""" policy_agent = policy_agent if policy_agent is not None else PolicyAgent( blockchain=self.blockchain) withdraw_address = collector_address or self.checksum_address receipt = policy_agent.collect_policy_reward( collector_address=withdraw_address, staker_address=self.checksum_address) return receipt
def __init__(self, checksum_address: str, *args, **kwargs) -> None: """ :param policy_agent: A policy agent with the blockchain attached; If not passed, A default policy agent and blockchain connection will be created from default values. """ super().__init__(checksum_address=checksum_address, *args, **kwargs) # From defaults self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain)
def __init__(self, policy_agent: PolicyAgent=None, *args, **kwargs): if policy_agent is None: # all defaults token_agent = NucypherTokenAgent() miner_agent = MinerAgent(token_agent=token_agent) policy_agent = PolicyAgent(miner_agent=miner_agent) self.policy_agent = policy_agent super().__init__(token_agent=self.policy_agent.token_agent, *args, **kwargs) self._arrangements = OrderedDict() # Track authored policies by id
def __init__(self, blockchain: Blockchain, deployer_address: str = None, allocation_registry: AllocationRegistry = None, bare: bool = True) -> None: self.blockchain = blockchain self.__deployer_address = NO_DEPLOYER_ADDRESS if deployer_address: self.deployer_address = deployer_address if not bare: self.token_agent = NucypherTokenAgent(blockchain=blockchain) self.miner_agent = MinerAgent(blockchain=blockchain) self.policy_agent = PolicyAgent(blockchain=blockchain) self.allocation_registy = allocation_registry self.user_escrow_deployers = dict()
def test_policy_manager_deployer(testerchain): origin, *everybody_else = testerchain.interface.w3.eth.accounts token_deployer = NucypherTokenDeployer(blockchain=testerchain, deployer_address=origin) token_deployer.deploy() token_agent = token_deployer.make_agent() # 1: Token miners_escrow_secret = os.urandom(DISPATCHER_SECRET_LENGTH) miner_escrow_deployer = MinerEscrowDeployer( deployer_address=origin, secret_hash=testerchain.interface.w3.keccak(miners_escrow_secret)) miner_escrow_deployer.deploy() miner_agent = miner_escrow_deployer.make_agent() # 2 Miner Escrow policy_manager_secret = os.urandom(DISPATCHER_SECRET_LENGTH) deployer = PolicyManagerDeployer( deployer_address=origin, secret_hash=testerchain.interface.w3.keccak(policy_manager_secret)) deployment_txhashes = deployer.deploy() assert len(deployment_txhashes) == 3 for title, txhash in deployment_txhashes.items(): receipt = testerchain.wait_for_receipt(txhash=txhash) assert receipt['status'] == 1, "Transaction Rejected {}:{}".format( title, txhash) # Create a token instance policy_agent = deployer.make_agent() policy_manager_contract = policy_agent.contract # Retrieve the token from the blockchain some_policy_agent = PolicyAgent() assert some_policy_agent.contract.address == policy_manager_contract.address # Compare the contract address for equality assert policy_agent.contract_address == some_policy_agent.contract_address assert policy_agent == some_policy_agent # __eq__
def __init__(self, blockchain: Blockchain, deployer_address: str = None, bare: bool = True) -> None: self.blockchain = blockchain self.__deployer_address = NO_DEPLOYER_ADDRESS self.deployer_address = deployer_address self.checksum_address = self.deployer_address if not bare: self.token_agent = NucypherTokenAgent(blockchain=blockchain) self.miner_agent = MinerAgent(blockchain=blockchain) self.policy_agent = PolicyAgent(blockchain=blockchain) self.adjudicator_agent = MiningAdjudicatorAgent( blockchain=blockchain) self.user_escrow_deployers = dict() self.deployers = {d.contract_name: d for d in self.deployers} self.log = Logger("Deployment-Actor")
def test_policy_manager_deployer(testerchain): origin, *everybody_else = testerchain.client.accounts token_deployer = NucypherTokenDeployer(blockchain=testerchain, deployer_address=origin) token_deployer.deploy() stakers_escrow_secret = os.urandom( DispatcherDeployer.DISPATCHER_SECRET_LENGTH) staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin, blockchain=testerchain) staking_escrow_deployer.deploy(secret_hash=keccak(stakers_escrow_secret)) policy_manager_secret = os.urandom( DispatcherDeployer.DISPATCHER_SECRET_LENGTH) deployer = PolicyManagerDeployer(deployer_address=origin, blockchain=testerchain) deployment_txhashes = deployer.deploy( secret_hash=keccak(policy_manager_secret)) assert len(deployment_txhashes) == 3 for title, txhash in deployment_txhashes.items(): receipt = testerchain.wait_for_receipt(txhash=txhash) assert receipt['status'] == 1, "Transaction Rejected {}:{}".format( title, txhash) # Create a PolicyAgent policy_agent = deployer.make_agent() # TODO: #1102 - Check that StakingEscrow contract address and public parameters are correct # Retrieve the PolicyAgent singleton some_policy_agent = PolicyAgent() assert policy_agent == some_policy_agent # __eq__ # Compare the contract address for equality assert policy_agent.contract_address == some_policy_agent.contract_address
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain)
def estimate_gas(analyzer: AnalyzeGas = None) -> None: """ Execute a linear sequence of NyCypher transactions mimicking post-deployment usage on a local PyEVM blockchain; Record the resulting estimated transaction gas expenditure. Note: The function calls below are *order dependant* """ # # Setup # if analyzer is None: analyzer = AnalyzeGas() log = Logger(AnalyzeGas.LOG_NAME) # Blockchain testerchain, agents = TesterBlockchain.bootstrap_network() web3 = testerchain.interface.w3 # Accounts origin, ursula1, ursula2, ursula3, alice1, *everyone_else = testerchain.interface.w3.eth.accounts # Contracts token_agent = NucypherTokenAgent(blockchain=testerchain) miner_agent = MinerAgent(blockchain=testerchain) policy_agent = PolicyAgent(blockchain=testerchain) # Contract Callers token_functions = token_agent.contract.functions miner_functions = miner_agent.contract.functions policy_functions = policy_agent.contract.functions analyzer.start_collection() print("********* Estimating Gas *********") # # Pre deposit tokens # tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 5).transact( {'from': origin}) testerchain.wait_for_receipt(tx) log.info("Pre-deposit tokens for 5 owners = " + str( miner_functions.preDeposit(everyone_else[0:5], [MIN_ALLOWED_LOCKED] * 5, [MIN_LOCKED_PERIODS] * 5).estimateGas({'from': origin}))) # # Give Ursula and Alice some coins # log.info("Transfer tokens = " + str( token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).estimateGas({'from': origin}))) tx = token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) tx = token_functions.transfer(ursula2, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) tx = token_functions.transfer(ursula3, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) # # Ursula and Alice give Escrow rights to transfer # log.info("Approving transfer = " + str( token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).estimateGas({'from': ursula1}))) tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula1}) testerchain.wait_for_receipt(tx) tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula2}) testerchain.wait_for_receipt(tx) tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula3}) testerchain.wait_for_receipt(tx) # # Ursula and Alice transfer some tokens to the escrow and lock them # log.info("First initial deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second initial deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third initial deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and confirm activity # testerchain.time_travel(periods=1) log.info( "First confirm activity = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and mint tokens # testerchain.time_travel(periods=1) log.info("First mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula1}))) tx = miner_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula2}))) tx = miner_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third/last mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula3}))) tx = miner_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info( "First confirm activity again = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity again = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity again = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Confirm again # testerchain.time_travel(periods=1) log.info( "First confirm activity + mint = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity + mint = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity + mint = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Get locked tokens # log.info("Getting locked tokens = " + str(miner_functions.getLockedTokens(ursula1).estimateGas())) # # Wait 1 period and withdraw tokens # testerchain.time_travel(periods=1) log.info("First withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula1}))) tx = miner_functions.withdraw(1).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula2}))) tx = miner_functions.withdraw(1).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula3}))) tx = miner_functions.withdraw(1).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and confirm activity # testerchain.time_travel(periods=1) log.info( "First confirm activity after downtime = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity after downtime = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity after downtime = " + str(miner_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Ursula and Alice deposit some tokens to the escrow again # log.info("First deposit tokens again = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second deposit tokens again = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third deposit tokens again = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and mint tokens # testerchain.time_travel(periods=1) log.info("First mining again = " + str(miner_functions.mint().estimateGas({'from': ursula1}))) tx = miner_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining again = " + str(miner_functions.mint().estimateGas({'from': ursula2}))) tx = miner_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third/last mining again = " + str(miner_functions.mint().estimateGas({'from': ursula3}))) tx = miner_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Create policy # policy_id_1 = os.urandom(int(POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(POLICY_ID_LENGTH)) number_of_periods = 10 log.info("First creating policy (1 node, 10 periods) = " + str( policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).estimateGas({ 'from': alice1, 'value': 10000 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).transact({ 'from': alice1, 'value': 10000 }) testerchain.wait_for_receipt(tx) log.info("Second creating policy (1 node, 10 periods) = " + str( policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).estimateGas({ 'from': alice1, 'value': 10000 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).transact({ 'from': alice1, 'value': 10000 }) testerchain.wait_for_receipt(tx) # # Revoke policy # log.info("Revoking policy = " + str( policy_functions.revokePolicy(policy_id_1).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1}) testerchain.wait_for_receipt(tx) tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1}) testerchain.wait_for_receipt(tx) # # Create policy with more periods # policy_id_1 = os.urandom(int(POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(POLICY_ID_LENGTH)) policy_id_3 = os.urandom(int(POLICY_ID_LENGTH)) number_of_periods = 100 log.info( "First creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info( "Second creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) log.info( "Third creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) # # Mine and revoke policy # testerchain.time_travel(periods=10) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info("First mining after downtime = " + str(miner_functions.mint().estimateGas({'from': ursula1}))) tx = miner_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining after downtime = " + str(miner_functions.mint().estimateGas({'from': ursula2}))) tx = miner_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=10) log.info("First revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_1).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1}) testerchain.wait_for_receipt(tx) log.info("Second revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_2).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1}) testerchain.wait_for_receipt(tx) log.info("Second revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_3).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_3).transact({'from': alice1}) testerchain.wait_for_receipt(tx) # # Create policy with multiple nodes # policy_id_1 = os.urandom(int(POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(POLICY_ID_LENGTH)) policy_id_3 = os.urandom(int(POLICY_ID_LENGTH)) number_of_periods = 100 log.info("First creating policy (3 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3]).estimateGas({ 'from': alice1, 'value': 30150 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact({ 'from': alice1, 'value': 30150 }) testerchain.wait_for_receipt(tx) log.info("Second creating policy (3 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3]).estimateGas({ 'from': alice1, 'value': 30150 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact({ 'from': alice1, 'value': 30150 }) testerchain.wait_for_receipt(tx) log.info("Third creating policy (2 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_3, number_of_periods, 50, [ursula1, ursula2]).estimateGas({ 'from': alice1, 'value': 20100 }))) tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1, ursula2]).transact({ 'from': alice1, 'value': 20100 }) testerchain.wait_for_receipt(tx) for index in range(5): tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) tx = miner_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = miner_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = miner_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Check regular deposit # log.info("First deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third deposit tokens = " + str( miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = miner_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # ApproveAndCall # testerchain.time_travel(periods=1) tx = miner_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = miner_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = miner_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info("First approveAndCall = " + str( token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula1}))) tx = token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second approveAndCall = " + str( token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula2}))) tx = token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third approveAndCall = " + str( token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula3}))) tx = token_functions.approveAndCall( miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Locking tokens # testerchain.time_travel(periods=1) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = miner_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = miner_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info("First locking tokens = " + str( miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second locking tokens = " + str( miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third locking tokens = " + str( miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Divide stake # log.info("First divide stake = " + str( miner_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = miner_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second divide stake = " + str( miner_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = miner_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) # # Divide almost finished stake # testerchain.time_travel(periods=1) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info("Divide stake (next period is not confirmed) = " + str( miner_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = miner_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Divide stake (next period is confirmed) = " + str( miner_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) print("********* All Done! *********")
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.staking_agent = StakingEscrowAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain)
def test_nucypher_deploy_contracts(click_runner, mock_primary_registry_filepath, mock_allocation_infile, token_economics): Agency.clear() # # Setup # # We start with a blockchain node, and nothing else... if os.path.isfile(mock_primary_registry_filepath): os.remove(mock_primary_registry_filepath) assert not os.path.isfile(mock_primary_registry_filepath) # # Main # command = [ 'contracts', '--registry-outfile', mock_primary_registry_filepath, '--provider-uri', TEST_PROVIDER_URI, '--poa' ] user_input = '0\n' + 'Y\n' + (f'{INSECURE_SECRETS[1]}\n' * 8) + 'DEPLOY' result = click_runner.invoke(deploy, command, input=user_input, catch_exceptions=False) assert result.exit_code == 0 # Ensure there is a report on each contract for registry_name in Deployer.contract_names: assert registry_name in result.output # Check that the primary contract registry was written # and peek at some of the registered entries assert os.path.isfile(mock_primary_registry_filepath) with open(mock_primary_registry_filepath, 'r') as file: # Ensure every contract's name was written to the file, somehow raw_registry_data = file.read() for registry_name in Deployer.contract_names: assert registry_name in raw_registry_data # Ensure the Registry is JSON deserializable registry_data = json.loads(raw_registry_data) # and that is has the correct number of entries assert len(registry_data) == 9 # Read several records token_record, escrow_record, dispatcher_record, *other_records = registry_data registered_name, registered_address, registered_abi = token_record # # Agency # token_agent = NucypherTokenAgent() assert token_agent.contract_name == registered_name assert token_agent.registry_contract_name == registered_name assert token_agent.contract_address == registered_address # Now show that we can use contract Agency and read from the blockchain assert token_agent.get_balance() == 0 staking_agent = StakingEscrowAgent() assert staking_agent.get_current_period() # and at least the others can be instantiated assert PolicyAgent()
def estimate_gas(analyzer: AnalyzeGas = None) -> None: """ Execute a linear sequence of NyCypher transactions mimicking post-deployment usage on a local PyEVM blockchain; Record the resulting estimated transaction gas expenditure. Note: The function calls below are *order dependant* """ # # Setup # if analyzer is None: analyzer = AnalyzeGas() log = Logger(AnalyzeGas.LOG_NAME) # Blockchain testerchain, agents = TesterBlockchain.bootstrap_network() web3 = testerchain.w3 # Accounts origin, ursula1, ursula2, ursula3, alice1, *everyone_else = testerchain.client.accounts ursula_with_stamp = mock_ursula(testerchain, ursula1) # Contracts token_agent = NucypherTokenAgent(blockchain=testerchain) staking_agent = StakingEscrowAgent(blockchain=testerchain) policy_agent = PolicyAgent(blockchain=testerchain) adjudicator_agent = AdjudicatorAgent(blockchain=testerchain) # Contract Callers token_functions = token_agent.contract.functions staker_functions = staking_agent.contract.functions policy_functions = policy_agent.contract.functions adjudicator_functions = adjudicator_agent.contract.functions analyzer.start_collection() print("********* Estimating Gas *********") # # Pre deposit tokens # tx = token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 5).transact( {'from': origin}) testerchain.wait_for_receipt(tx) log.info("Pre-deposit tokens for 5 owners = " + str( staker_functions.preDeposit(everyone_else[0:5], [MIN_ALLOWED_LOCKED] * 5, [MIN_LOCKED_PERIODS] * 5).estimateGas({'from': origin}))) # # Give Ursula and Alice some coins # log.info("Transfer tokens = " + str( token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).estimateGas({'from': origin}))) tx = token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) tx = token_functions.transfer(ursula2, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) tx = token_functions.transfer(ursula3, MIN_ALLOWED_LOCKED * 10).transact( {'from': origin}) testerchain.wait_for_receipt(tx) # # Ursula and Alice give Escrow rights to transfer # log.info("Approving transfer = " + str( token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6).estimateGas({'from': ursula1}))) tx = token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula1}) testerchain.wait_for_receipt(tx) tx = token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula2}) testerchain.wait_for_receipt(tx) tx = token_functions.approve(staking_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact( {'from': ursula3}) testerchain.wait_for_receipt(tx) # # Ursula and Alice transfer some tokens to the escrow and lock them # log.info("First initial deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second initial deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third initial deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) tx = staker_functions.setWorker(ursula1).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.setWorker(ursula2).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.setWorker(ursula3).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and confirm activity # testerchain.time_travel(periods=1) log.info( "First confirm activity = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and mint tokens # testerchain.time_travel(periods=1) log.info("First mining (1 stake) = " + str(staker_functions.mint().estimateGas({'from': ursula1}))) tx = staker_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining (1 stake) = " + str(staker_functions.mint().estimateGas({'from': ursula2}))) tx = staker_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third/last mining (1 stake) = " + str(staker_functions.mint().estimateGas({'from': ursula3}))) tx = staker_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info( "First confirm activity again = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity again = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity again = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Confirm again # testerchain.time_travel(periods=1) log.info( "First confirm activity + mint = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity + mint = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity + mint = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Get locked tokens # log.info("Getting locked tokens = " + str(staker_functions.getLockedTokens(ursula1).estimateGas())) # # Wait 1 period and withdraw tokens # testerchain.time_travel(periods=1) log.info("First withdraw = " + str(staker_functions.withdraw(1).estimateGas({'from': ursula1}))) tx = staker_functions.withdraw(1).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second withdraw = " + str(staker_functions.withdraw(1).estimateGas({'from': ursula2}))) tx = staker_functions.withdraw(1).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third withdraw = " + str(staker_functions.withdraw(1).estimateGas({'from': ursula3}))) tx = staker_functions.withdraw(1).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Confirm activity with re-stake # tx = staker_functions.setReStake(True).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.setReStake(True).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.setReStake(True).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info( "First confirm activity + mint with re-stake = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity + mint with re-stake = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity + mint with re-stake = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) tx = staker_functions.setReStake(False).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.setReStake(False).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.setReStake(False).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 2 periods and confirm activity after downtime # testerchain.time_travel(periods=2) log.info( "First confirm activity after downtime = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info( "Second confirm activity after downtime = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula2}))) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info( "Third confirm activity after downtime = " + str(staker_functions.confirmActivity().estimateGas({'from': ursula3}))) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Ursula and Alice deposit some tokens to the escrow again # log.info("First deposit tokens again = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second deposit tokens again = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third deposit tokens again = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Wait 1 period and mint tokens # testerchain.time_travel(periods=1) log.info("First mining again = " + str(staker_functions.mint().estimateGas({'from': ursula1}))) tx = staker_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining again = " + str(staker_functions.mint().estimateGas({'from': ursula2}))) tx = staker_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third/last mining again = " + str(staker_functions.mint().estimateGas({'from': ursula3}))) tx = staker_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Create policy # policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH)) number_of_periods = 10 log.info("First creating policy (1 node, 10 periods) = " + str( policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).estimateGas({ 'from': alice1, 'value': 10000 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).transact({ 'from': alice1, 'value': 10000 }) testerchain.wait_for_receipt(tx) log.info("Second creating policy (1 node, 10 periods) = " + str( policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).estimateGas({ 'from': alice1, 'value': 10000 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).transact({ 'from': alice1, 'value': 10000 }) testerchain.wait_for_receipt(tx) # # Revoke policy # log.info("Revoking policy = " + str( policy_functions.revokePolicy(policy_id_1).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1}) testerchain.wait_for_receipt(tx) tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1}) testerchain.wait_for_receipt(tx) # # Create policy with more periods # policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH)) policy_id_3 = os.urandom(int(Policy.POLICY_ID_LENGTH)) number_of_periods = 100 log.info( "First creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info( "Second creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) log.info( "Third creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " + str( policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).estimateGas({ 'from': alice1, 'value': 10050 }))) tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).transact({ 'from': alice1, 'value': 10050 }) testerchain.wait_for_receipt(tx) # # Mine and revoke policy # testerchain.time_travel(periods=10) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info("First mining after downtime = " + str(staker_functions.mint().estimateGas({'from': ursula1}))) tx = staker_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second mining after downtime = " + str(staker_functions.mint().estimateGas({'from': ursula2}))) tx = staker_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=10) log.info("First revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_1).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1}) testerchain.wait_for_receipt(tx) log.info("Second revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_2).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1}) testerchain.wait_for_receipt(tx) log.info("Second revoking policy after downtime = " + str( policy_functions.revokePolicy(policy_id_3).estimateGas( {'from': alice1}))) tx = policy_functions.revokePolicy(policy_id_3).transact({'from': alice1}) testerchain.wait_for_receipt(tx) # # Create policy with multiple nodes # policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH)) policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH)) policy_id_3 = os.urandom(int(Policy.POLICY_ID_LENGTH)) number_of_periods = 100 log.info("First creating policy (3 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3]).estimateGas({ 'from': alice1, 'value': 30150 }))) tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact({ 'from': alice1, 'value': 30150 }) testerchain.wait_for_receipt(tx) log.info("Second creating policy (3 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3]).estimateGas({ 'from': alice1, 'value': 30150 }))) tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact({ 'from': alice1, 'value': 30150 }) testerchain.wait_for_receipt(tx) log.info("Third creating policy (2 nodes, 100 periods, first reward) = " + str( policy_functions.createPolicy( policy_id_3, number_of_periods, 50, [ursula1, ursula2]).estimateGas({ 'from': alice1, 'value': 20100 }))) tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1, ursula2]).transact({ 'from': alice1, 'value': 20100 }) testerchain.wait_for_receipt(tx) for index in range(5): tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) tx = staker_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Check regular deposit # log.info("First deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third deposit tokens = " + str( staker_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = staker_functions.deposit( MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # ApproveAndCall # testerchain.time_travel(periods=1) tx = staker_functions.mint().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.mint().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.mint().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info("First approveAndCall = " + str( token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula1}))) tx = token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second approveAndCall = " + str( token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula2}))) tx = token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third approveAndCall = " + str( token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula3}))) tx = token_functions.approveAndCall( staking_agent.contract_address, MIN_ALLOWED_LOCKED * 2, web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Locking tokens # testerchain.time_travel(periods=1) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula2}) testerchain.wait_for_receipt(tx) tx = staker_functions.confirmActivity().transact({'from': ursula3}) testerchain.wait_for_receipt(tx) log.info("First locking tokens = " + str( staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula1}))) tx = staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second locking tokens = " + str( staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula2}))) tx = staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2}) testerchain.wait_for_receipt(tx) log.info("Third locking tokens = " + str( staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS). estimateGas({'from': ursula3}))) tx = staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3}) testerchain.wait_for_receipt(tx) # # Divide stake # log.info("First divide stake = " + str( staker_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = staker_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Second divide stake = " + str( staker_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = staker_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) # # Divide almost finished stake # testerchain.time_travel(periods=1) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) log.info("Divide stake (next period is not confirmed) = " + str( staker_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) log.info("Divide stake (next period is confirmed) = " + str( staker_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas( {'from': ursula1}))) # # Slashing tests # tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) # # Slashing # slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Slash just value = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) deposit = staker_functions.stakerInfo(ursula1).call()[0] unlocked = deposit - staker_functions.getLockedTokens(ursula1).call() tx = staker_functions.withdraw(unlocked).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("First slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Second slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Third slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Slashing two sub stakes and saving old one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) for index in range(18): tx = staker_functions.confirmActivity().transact({'from': ursula1}) testerchain.wait_for_receipt(tx) testerchain.time_travel(periods=1) tx = staker_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) deposit = staker_functions.stakerInfo(ursula1).call()[0] unlocked = deposit - staker_functions.getLockedTokens(ursula1, 1).call() tx = staker_functions.withdraw(unlocked).transact({'from': ursula1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Slashing two sub stakes, shortest and new one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) sub_stakes_length = str( staker_functions.getSubStakesLength(ursula1).call()) slashing_args = generate_args_for_slashing(ursula_with_stamp) log.info("Slashing three sub stakes, two shortest and new one (" + sub_stakes_length + " sub stakes) = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) slashing_args = generate_args_for_slashing(ursula_with_stamp, corrupt_cfrag=False) log.info("Evaluating correct CFrag = " + str( adjudicator_functions.evaluateCFrag( *slashing_args).estimateGas({'from': alice1}))) tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact( {'from': alice1}) testerchain.wait_for_receipt(tx) print("********* All Done! *********")
def connect_to_contracts(self) -> None: """Initialize contract agency and set them on config""" self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.miner_agent = MinerAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain) self.log.debug("Established connection to nucypher contracts")
def acquire_agency(self) -> None: self.token_agent = NucypherTokenAgent(blockchain=self.blockchain) self.staking_agent = StakingEscrowAgent(blockchain=self.blockchain) self.policy_agent = PolicyAgent(blockchain=self.blockchain) self.log.debug("Established connection to nucypher contracts")