def testerchain(_testerchain) -> TesterBlockchain: testerchain = _testerchain # Reset chain state pyevm_backend = testerchain.provider.ethereum_tester.backend snapshot = pyevm_backend.chain.get_canonical_block_by_number(0).hash pyevm_backend.revert_to_snapshot(snapshot) coinbase, *addresses = testerchain.client.accounts for address in addresses: balance = testerchain.client.get_balance(address) spent = DEVELOPMENT_ETH_AIRDROP_AMOUNT - balance if spent > 0: tx = {'to': address, 'from': coinbase, 'value': spent} txhash = testerchain.w3.eth.sendTransaction(tx) _receipt = testerchain.wait_for_receipt(txhash) eth_amount = Web3().fromWei(spent, 'ether') testerchain.log.info("Airdropped {} ETH {} -> {}".format( eth_amount, tx['from'], tx['to'])) BlockchainInterfaceFactory.register_interface(interface=testerchain, force=True) # Mock TransactingPower Consumption (Deployer) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(client=testerchain.client), account=testerchain.etherbase_account) testerchain.transacting_power.activate() yield testerchain
def _make_testerchain() -> TesterBlockchain: """ https://github.com/ethereum/eth-tester # available-backends """ # Monkey patch to prevent gas adjustment import eth eth._utils.headers.GAS_LIMIT_MINIMUM = TEST_GAS_LIMIT eth._utils.headers.GENESIS_GAS_LIMIT = TEST_GAS_LIMIT eth.vm.forks.frontier.headers.GENESIS_GAS_LIMIT = TEST_GAS_LIMIT # Monkey patch to prevent gas estimates def _get_buffered_gas_estimate(web3, transaction, gas_buffer=100000): return TEST_GAS_LIMIT import web3 web3.eth.get_buffered_gas_estimate = _get_buffered_gas_estimate # Create the blockchain testerchain = TesterBlockchain(eth_airdrop=True, free_transactions=True) BlockchainInterfaceFactory.register_interface(interface=testerchain, force=True) # Mock TransactingPower Consumption (Deployer) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(client=testerchain.client), account=testerchain.etherbase_account) testerchain.transacting_power.activate() return testerchain
def stakers(testerchain, agency, token_economics, test_registry): token_agent, _staking_agent, _policy_agent = agency blockchain = token_agent.blockchain # Mock Powerup consumption (Deployer) blockchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(client=testerchain.client), account=blockchain.etherbase_account) blockchain.transacting_power.activate() token_airdrop(origin=blockchain.etherbase_account, addresses=blockchain.stakers_accounts, token_agent=token_agent, amount=DEVELOPMENT_TOKEN_AIRDROP_AMOUNT) stakers = list() for index, account in enumerate(blockchain.stakers_accounts): staker = Staker(is_me=True, checksum_address=account, registry=test_registry) # Mock TransactingPower consumption staker.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(client=testerchain.client), account=account) staker.transacting_power.activate() amount = MIN_STAKE_FOR_TESTS + random.randrange(BONUS_TOKENS_FOR_TESTS) # for a random lock duration min_locktime, max_locktime = token_economics.minimum_locked_periods, token_economics.maximum_rewarded_periods periods = random.randint(min_locktime, max_locktime) staker.initialize_stake(amount=amount, lock_periods=periods) # We assume that the staker knows in advance the account of her worker worker_address = blockchain.ursula_account(index) staker.bond_worker(worker_address=worker_address) stakers.append(staker) # Stake starts next period (or else signature validation will fail) blockchain.time_travel(periods=1) yield stakers
def test_character_transacting_power_signing(testerchain, agency, test_registry): # Pretend to be a character. eth_address = testerchain.etherbase_account signer = Character(is_me=True, registry=test_registry, checksum_address=eth_address) # Manually consume the power up transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(testerchain.client), account=eth_address) signer._crypto_power.consume_power_up(transacting_power) # Retrieve the power up power = signer._crypto_power.power_ups(TransactingPower) assert power == transacting_power assert testerchain.transacting_power == power assert power.is_active is True assert power.is_unlocked is True assert testerchain.transacting_power.is_unlocked is True # Sign Message data_to_sign = b'Premium Select Luxury Pencil Holder' signature = power.sign_message(message=data_to_sign) is_verified = verify_eip_191(address=eth_address, message=data_to_sign, signature=signature) assert is_verified is True # Sign Transaction transaction_dict = { 'nonce': testerchain.client.w3.eth.getTransactionCount(eth_address), 'gasPrice': testerchain.client.w3.eth.gasPrice, 'gas': 100000, 'from': eth_address, 'to': testerchain.unassigned_accounts[1], 'value': 1, 'data': b'' } signed_transaction = power.sign_transaction( transaction_dict=transaction_dict) # Demonstrate that the transaction is valid RLP encoded. restored_transaction = Transaction.from_bytes( serialized_bytes=signed_transaction) restored_dict = restored_transaction.as_dict() assert to_checksum_address(restored_dict['to']) == transaction_dict['to']
def software_stakeholder(testerchain, agency, stakeholder_config_file_location, test_registry): token_agent, staking_agent, policy_agent = agency # Setup path = stakeholder_config_file_location if os.path.exists(path): os.remove(path) # 0xaAa482c790b4301bE18D75A0D1B11B2ACBEF798B stakeholder_private_key = '255f64a948eeb1595b8a2d1e76740f4683eca1c8f1433d13293db9b6e27676cc' address = testerchain.provider.ethereum_tester.add_account( private_key=stakeholder_private_key, password=INSECURE_DEVELOPMENT_PASSWORD) testerchain.provider.ethereum_tester.unlock_account( account=address, password=INSECURE_DEVELOPMENT_PASSWORD) tx = { 'to': address, 'from': testerchain.etherbase_account, 'value': Web3.toWei('1', 'ether') } txhash = testerchain.client.w3.eth.sendTransaction(tx) _receipt = testerchain.wait_for_receipt(txhash) # Mock TransactingPower consumption (Etherbase) transacting_power = TransactingPower( account=testerchain.etherbase_account, signer=Web3Signer(testerchain.client), password=INSECURE_DEVELOPMENT_PASSWORD) transacting_power.activate() token_agent.transfer(amount=NU(200_000, 'NU').to_nunits(), sender_address=testerchain.etherbase_account, target_address=address) # Create stakeholder from on-chain values given accounts over a web3 provider stakeholder = StakeHolder(registry=test_registry, initial_address=address) # Teardown yield stakeholder if os.path.exists(path): os.remove(path)
def __init__(self, account: str, signer: Signer = None, password: str = None, cache: bool = False): """ Instantiates a TransactingPower for the given checksum_address. """ # Auth if not signer: # TODO: Consider making this required blockchain = BlockchainInterfaceFactory.get_interface() signer = Web3Signer(client=blockchain.client) self._signer = signer self.__account = account self.__password = password # Config self.__is_unlocked = False self.__blockchain = None self.__cache = cache self.__activated = False
def _mock_transacting_power_activation(password, account): testerchain.transacting_power = TransactingPower( password=password, signer=Web3Signer(testerchain.client), account=account) testerchain.transacting_power.activate()
def _make_agency( testerchain, test_registry, token_economics ) -> Tuple[NucypherTokenAgent, StakingEscrowAgent, PolicyManagerAgent]: """ Launch the big three contracts on provided chain, make agents for each and return them. """ # Mock TransactingPower Consumption (Deployer) testerchain.transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(client=testerchain.client), account=testerchain.etherbase_account) testerchain.transacting_power.activate() origin = testerchain.etherbase_account token_deployer = NucypherTokenDeployer(deployer_address=origin, economics=token_economics, registry=test_registry) token_deployer.deploy() staking_escrow_deployer = StakingEscrowDeployer(deployer_address=origin, economics=token_economics, registry=test_registry, test_mode=True) staking_escrow_deployer.deploy() policy_manager_deployer = PolicyManagerDeployer(deployer_address=origin, economics=token_economics, registry=test_registry) policy_manager_deployer.deploy() adjudicator_deployer = AdjudicatorDeployer(deployer_address=origin, economics=token_economics, registry=test_registry) adjudicator_deployer.deploy() staking_interface_deployer = StakingInterfaceDeployer( deployer_address=origin, economics=token_economics, registry=test_registry) staking_interface_deployer.deploy() worklock_deployer = WorklockDeployer(deployer_address=origin, economics=token_economics, registry=test_registry) worklock_deployer.deploy() token_agent = token_deployer.make_agent() # 1 Token staking_agent = staking_escrow_deployer.make_agent() # 2 Staking Escrow policy_agent = policy_manager_deployer.make_agent() # 3 Policy Agent _adjudicator_agent = adjudicator_deployer.make_agent() # 4 Adjudicator _worklock_agent = worklock_deployer.make_agent() # 5 Worklock # Set additional parameters minimum, default, maximum = FEE_RATE_RANGE txhash = policy_agent.contract.functions.setFeeRateRange( minimum, default, maximum).transact() _receipt = testerchain.wait_for_receipt(txhash) # TODO: Get rid of returning these agents here. # What's important is deploying and creating the first agent for each contract, # and since agents are singletons, in tests it's only necessary to call the agent # constructor again to receive the existing agent. # # For example: # staking_agent = StakingEscrowAgent() # # This is more clear than how we currently obtain an agent instance in tests: # _, staking_agent, _ = agency # # Other advantages is that it's closer to how agents should be use (i.e., there # are no fixtures IRL) and it's more extensible (e.g., AdjudicatorAgent) return token_agent, staking_agent, policy_agent