def test_deployment_with_args(blockchain_client, Named): deploy_txn_hash = deploy_contract(blockchain_client, Named, constructor_args=("John",)) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) named = Named(contract_addr, blockchain_client) name = named.name.call() assert name == "John"
def _deploy_future_block_call(contract_function, scheduler_address=None, target_block=None, grace_period=64, suggested_gas=100000, payment=1, fee=1, endowment=None): if endowment is None: endowment = deploy_client.get_max_gas() * deploy_client.get_gas_price() + payment + fee if target_block is None: target_block = deploy_client.get_block_number() + 40 if scheduler_address is None: scheduler_address = deploy_coinbase deploy_txn_hash = deploy_contract( deploy_client, FutureBlockCall, constructor_args=( scheduler_address, target_block, target_block + grace_period, contract_function._contract._meta.address, contract_function.encoded_abi_signature, suggested_gas, payment, fee, ), gas=int(deploy_client.get_max_gas() * 0.95), value=endowment, ) call_address = get_contract_address_from_txn(deploy_client, deploy_txn_hash, 180) call = FutureBlockCall(call_address, deploy_client) return call
def test_deployment_with_no_args(blockchain_client, Math): deploy_txn_hash = deploy_contract( blockchain_client, Math, ) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) assert contract_addr
def test_deployment_with_endowment(blockchain_client, Math): deploy_txn_hash = deploy_contract( blockchain_client, Math, value=1000, ) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) assert contract_addr math = Math(contract_addr, blockchain_client) assert math.get_balance() == 1000
def test_deployment_with_args(blockchain_client, Named): deploy_txn_hash = deploy_contract( blockchain_client, Named, constructor_args=("John", ), ) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) named = Named(contract_addr, blockchain_client) name = named.name.call() assert name == "John"
def deployed_math(Math, blockchain_client): deploy_txn_hash = deploy_contract( blockchain_client, Math, ) contract_addr = get_contract_address_from_txn( blockchain_client, deploy_txn_hash, ) assert contract_addr math = Math(contract_addr, blockchain_client) return math
def deployed_logs_events(LogsEvents, blockchain_client): deploy_txn_hash = deploy_contract( blockchain_client, LogsEvents, ) contract_addr = get_contract_address_from_txn( blockchain_client, deploy_txn_hash, ) assert contract_addr logs_events = LogsEvents(contract_addr, blockchain_client) return logs_events
def _deploy_computation_contract(verifier_contract): deploy_txn_hash = deploy_contract( deploy_client, contracts.Computation, constructor_args=( verifier_contract._meta.address, ), gas=int(deploy_client.get_max_gas() * 0.95), value=denoms.ether, ) computation_address = get_contract_address_from_txn(deploy_client, deploy_txn_hash, 180) computation = contracts.Computation(computation_address, deploy_client) return computation
def _deploy_canary_contract(endowment=None, scheduler_address=None): if endowment is None: endowment = 5 * denoms.ether if scheduler_address is None: scheduler_address = deployed_contracts.Scheduler._meta.address deploy_txn_hash = deploy_contract( deploy_client, Canary, constructor_args=(scheduler_address,), gas=int(deploy_client.get_max_gas() * 0.95), value=endowment, ) canary_address = get_contract_address_from_txn(deploy_client, deploy_txn_hash, 180) canary = Canary(canary_address, deploy_client) return canary
def _deploy_future_block_call(contract_function=None, scheduler_address=None, target_block=None, grace_period=255, required_gas=1000000, payment=1, donation=1, endowment=None, call_data="", require_depth=0, call_value=0): if endowment is None: endowment = deploy_client.get_max_gas() * deploy_client.get_gas_price() + payment + donation + call_value if target_block is None: target_block = deploy_client.get_block_number() if scheduler_address is None: scheduler_address = deploy_coinbase if contract_function is None: abi_signature = "" contract_address = scheduler_address else: abi_signature = contract_function.encoded_abi_signature contract_address = contract_function._contract._meta.address deploy_txn_hash = deploy_contract( deploy_client, FutureBlockCall, constructor_args=( scheduler_address, target_block, grace_period, contract_address, abi_signature, call_data, call_value, required_gas, require_depth, payment, donation, ), gas=int(deploy_client.get_max_gas() * 0.95), value=endowment, ) call_address = get_contract_address_from_txn(deploy_client, deploy_txn_hash, 180) call = FutureBlockCall(call_address, deploy_client) return call
def test_free_for_all_window_awards_mega_bonus(deploy_client, deployed_contracts, contracts): alarm = deployed_contracts.Alarm client_contract = deployed_contracts.NoArgs joiner_a = deployed_contracts.JoinsPool coinbase = deploy_client.get_coinbase() deploy_txn = deploy_contract(deploy_client, contracts.JoinsPool, _from=coinbase, gas=get_max_gas(deploy_client)) joiner_b = contracts.JoinsPool(get_contract_address_from_txn(deploy_client, deploy_txn, 30), deploy_client) coinbase = deploy_client.get_coinbase() # Put in our deposit with the alarm contract. deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20 alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount) wait_for_transaction(deploy_client, joiner_a.setCallerPool.sendTransaction(alarm._meta.address)) wait_for_transaction(deploy_client, joiner_b.setCallerPool.sendTransaction(alarm._meta.address)) assert alarm.getBondBalance(coinbase) == 0 deposit_amount = alarm.getMinimumBond() * 10 # Put in our bond wait_for_transaction( deploy_client, alarm.depositBond.sendTransaction(value=deposit_amount) ) # Put contract A's bond in wait_for_transaction( deploy_client, deploy_client.send_transaction(to=joiner_a._meta.address, value=deposit_amount) ) wait_for_transaction( deploy_client, joiner_a.deposit.sendTransaction(deposit_amount) ) # Put contract B's bond in wait_for_transaction( deploy_client, deploy_client.send_transaction(to=joiner_b._meta.address, value=deposit_amount) ) wait_for_transaction( deploy_client, joiner_b.deposit.sendTransaction(deposit_amount) ) # All join the pool wait_for_transaction(deploy_client, joiner_a.enter.sendTransaction()) wait_for_transaction(deploy_client, joiner_b.enter.sendTransaction()) wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction()) # New pool is formed but not active first_generation_id = alarm.getNextGenerationId() assert first_generation_id > 0 # Wait for it to become active deploy_client.wait_for_block(alarm.getGenerationStartAt(first_generation_id), 240) # We should both be in the pool assert alarm.getCurrentGenerationId() == first_generation_id assert alarm.isInGeneration(joiner_a._meta.address, first_generation_id) is True assert alarm.isInGeneration(joiner_b._meta.address, first_generation_id) is True assert alarm.isInGeneration(coinbase, first_generation_id) is True # Schedule the function call. txn_hash = client_contract.setGracePeriod.sendTransaction(alarm.getMinimumGracePeriod()) txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address) wait_for_transaction(deploy_client, txn_hash) call_key = alarm.getLastCallKey() assert call_key is not None target_block = alarm.getCallTargetBlock(call_key) grace_period = alarm.getCallGracePeriod(call_key) deploy_client.wait_for_block(target_block + grace_period - 4, 300) my_before_balance = alarm.getBondBalance(coinbase) before_balance_a = alarm.getBondBalance(joiner_a._meta.address) before_balance_b = alarm.getBondBalance(joiner_b._meta.address) assert alarm.checkIfCalled(call_key) is False call_txn_hash = alarm.doCall.sendTransaction(call_key) call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash) call_txn = deploy_client.get_transaction_by_hash(call_txn_hash) call_block = deploy_client.get_block_by_hash(call_txn_receipt['blockHash']) my_after_balance = alarm.getBondBalance(coinbase) after_balance_a = alarm.getBondBalance(joiner_a._meta.address) after_balance_b = alarm.getBondBalance(joiner_b._meta.address) assert alarm.checkIfCalled(call_key) is True assert after_balance_a < before_balance_a assert after_balance_b < before_balance_b minimum_bond = int(call_block['gasLimit'], 16) * int(call_txn['gasPrice'], 16) assert after_balance_a == before_balance_a - minimum_bond assert after_balance_b == before_balance_b - minimum_bond assert my_after_balance > my_before_balance assert my_after_balance == my_before_balance + 2 * minimum_bond assert alarm.getNextGenerationId() == 0
def test_deployment_with_endowment(blockchain_client, Math): deploy_txn_hash = deploy_contract(blockchain_client, Math, value=1000) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) assert contract_addr math = Math(contract_addr, blockchain_client) assert math.get_balance() == 1000
def test_deployment_with_no_args(blockchain_client, Math): deploy_txn_hash = deploy_contract(blockchain_client, Math) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) assert contract_addr
def test_free_for_all_window_awards_mega_bonus(deploy_client, deployed_contracts, contracts): alarm = deployed_contracts.Alarm client_contract = deployed_contracts.NoArgs joiner_a = deployed_contracts.JoinsPool coinbase = deploy_client.get_coinbase() deploy_txn = deploy_contract(deploy_client, contracts.JoinsPool, _from=coinbase, gas=get_max_gas(deploy_client)) joiner_b = contracts.JoinsPool( get_contract_address_from_txn(deploy_client, deploy_txn, 30), deploy_client) coinbase = deploy_client.get_coinbase() # Put in our deposit with the alarm contract. deposit_amount = get_max_gas( deploy_client) * deploy_client.get_gas_price() * 20 alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount) wait_for_transaction( deploy_client, joiner_a.setCallerPool.sendTransaction(alarm._meta.address)) wait_for_transaction( deploy_client, joiner_b.setCallerPool.sendTransaction(alarm._meta.address)) assert alarm.getBondBalance(coinbase) == 0 deposit_amount = alarm.getMinimumBond() * 10 # Put in our bond wait_for_transaction( deploy_client, alarm.depositBond.sendTransaction(value=deposit_amount)) # Put contract A's bond in wait_for_transaction( deploy_client, deploy_client.send_transaction(to=joiner_a._meta.address, value=deposit_amount)) wait_for_transaction(deploy_client, joiner_a.deposit.sendTransaction(deposit_amount)) # Put contract B's bond in wait_for_transaction( deploy_client, deploy_client.send_transaction(to=joiner_b._meta.address, value=deposit_amount)) wait_for_transaction(deploy_client, joiner_b.deposit.sendTransaction(deposit_amount)) # All join the pool wait_for_transaction(deploy_client, joiner_a.enter.sendTransaction()) wait_for_transaction(deploy_client, joiner_b.enter.sendTransaction()) wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction()) # New pool is formed but not active first_generation_id = alarm.getNextGenerationId() assert first_generation_id > 0 # Wait for it to become active deploy_client.wait_for_block( alarm.getGenerationStartAt(first_generation_id), 240) # We should both be in the pool assert alarm.getCurrentGenerationId() == first_generation_id assert alarm.isInGeneration(joiner_a._meta.address, first_generation_id) is True assert alarm.isInGeneration(joiner_b._meta.address, first_generation_id) is True assert alarm.isInGeneration(coinbase, first_generation_id) is True # Schedule the function call. txn_hash = client_contract.setGracePeriod.sendTransaction( alarm.getMinimumGracePeriod()) txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address) wait_for_transaction(deploy_client, txn_hash) call_key = alarm.getLastCallKey() assert call_key is not None target_block = alarm.getCallTargetBlock(call_key) grace_period = alarm.getCallGracePeriod(call_key) deploy_client.wait_for_block(target_block + grace_period - 4, 300) my_before_balance = alarm.getBondBalance(coinbase) before_balance_a = alarm.getBondBalance(joiner_a._meta.address) before_balance_b = alarm.getBondBalance(joiner_b._meta.address) assert alarm.checkIfCalled(call_key) is False call_txn_hash = alarm.doCall.sendTransaction(call_key) call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash) call_txn = deploy_client.get_transaction_by_hash(call_txn_hash) call_block = deploy_client.get_block_by_hash(call_txn_receipt['blockHash']) my_after_balance = alarm.getBondBalance(coinbase) after_balance_a = alarm.getBondBalance(joiner_a._meta.address) after_balance_b = alarm.getBondBalance(joiner_b._meta.address) assert alarm.checkIfCalled(call_key) is True assert after_balance_a < before_balance_a assert after_balance_b < before_balance_b minimum_bond = int(call_block['gasLimit'], 16) * int( call_txn['gasPrice'], 16) assert after_balance_a == before_balance_a - minimum_bond assert after_balance_b == before_balance_b - minimum_bond assert my_after_balance > my_before_balance assert my_after_balance == my_before_balance + 2 * minimum_bond assert alarm.getNextGenerationId() == 0
def deploy_contracts(deploy_client, contracts, deploy_at_block=0, max_wait_for_deploy=0, from_address=None, max_wait=0, contracts_to_deploy=None, dependencies=None, constructor_args=None, deploy_gas=None): _deployed_contracts = {} _receipts = {} if constructor_args is None: constructor_args = {} if dependencies is None: dependencies = {} # Potentiall wait until we've reached a specific block. deploy_client.wait_for_block(deploy_at_block, max_wait_for_deploy) # Extract and dependencies that exist due to library linking. linker_dependencies = get_linker_dependencies(contracts) deploy_dependencies = merge_dependencies( dependencies, linker_dependencies, ) # If a subset of contracts have been specified to be deployed, compute # their dependencies as well. contracts_to_deploy = set( itertools.chain.from_iterable( get_dependencies(contract_name, deploy_dependencies) for contract_name in ( contracts_to_deploy or []))).union(contracts_to_deploy) # If there are any dependencies either explicit or from libraries, sort the # contracts by their dependencies. if deploy_dependencies: dependencies = copy.copy(deploy_dependencies) for contract_name, _ in contracts: if contract_name not in deploy_dependencies: dependencies[contract_name] = set() sorted_contract_names = toposort.toposort_flatten(dependencies) contracts = sorted(contracts, key=lambda c: sorted_contract_names.index(c[0])) if from_address is None: from_address = deploy_client.get_coinbase() for contract_name, contract_class in contracts: # If a subset of contracts have been specified, only deploy those or # the contracts they depend upon. if contracts_to_deploy and contract_name not in contracts_to_deploy: continue args = constructor_args.get(contract_name, None) if callable(args): args = args(_deployed_contracts) if deploy_gas is None: deploy_gas_limit = int(deploy_client.get_max_gas() * 0.98) elif callable(deploy_gas): deploy_gas_limit = deploy_gas(contract_name, contract_class) else: deploy_gas_limit = deploy_gas if contract_name in linker_dependencies: for dependency_name in linker_dependencies[contract_name]: deployed_contract = _deployed_contracts[dependency_name] link_contract_dependency(contract_class, deployed_contract) txn_hash = deploy_contract( deploy_client, contract_class, constructor_args=args, _from=from_address, gas=deploy_gas_limit, ) contract_addr = get_contract_address_from_txn( deploy_client, txn_hash, max_wait=max_wait, ) _receipts[contract_name] = deploy_client.wait_for_transaction( txn_hash, max_wait, ) _deployed_contracts[contract_name] = contract_class( contract_addr, deploy_client, ) _dict = { '_deploy_receipts': _receipts, '__len__': lambda s: len(_deployed_contracts), '__iter__': lambda s: iter(_deployed_contracts.items()), '__getitem__': lambda s, k: _deployed_contracts.__getitem__[k], } _dict.update(_deployed_contracts) return type('deployed_contracts', (object, ), _dict)()
def test_free_for_all_window_awards_mega_bonus(deploy_client, deployed_contracts, contracts, deploy_coinbase, get_call, get_execution_data, denoms): scheduler = deployed_contracts.Scheduler client_contract = deployed_contracts.TestCallExecution joiner_a = deployed_contracts.JoinsPool deploy_txn = deploy_contract( deploy_client, contracts.JoinsPool, _from=deploy_coinbase, gas=deploy_client.get_max_gas(), ) joiner_b = contracts.JoinsPool(get_contract_address_from_txn(deploy_client, deploy_txn, 30), deploy_client) # Put in our deposit with the scheduler contract. deposit_amount = deploy_client.get_max_gas() * deploy_client.get_gas_price() * 20 deploy_client.wait_for_transaction( joiner_a.setCallerPool.sendTransaction(scheduler._meta.address), ) deploy_client.wait_for_transaction( joiner_b.setCallerPool.sendTransaction(scheduler._meta.address), ) assert scheduler.getBondBalance(deploy_coinbase) == 0 deposit_amount = scheduler.getMinimumBond() * 10 # Put in our bond deploy_client.wait_for_transaction( scheduler.depositBond.sendTransaction(value=deposit_amount) ) # Put contract A's bond in deploy_client.wait_for_transaction( deploy_client.send_transaction(to=joiner_a._meta.address, value=deposit_amount) ) deploy_client.wait_for_transaction( joiner_a.deposit.sendTransaction(deposit_amount) ) # Put contract B's bond in deploy_client.wait_for_transaction( deploy_client.send_transaction(to=joiner_b._meta.address, value=deposit_amount) ) deploy_client.wait_for_transaction( joiner_b.deposit.sendTransaction(deposit_amount) ) # All join the pool deploy_client.wait_for_transaction(joiner_a.enter.sendTransaction()) deploy_client.wait_for_transaction(joiner_b.enter.sendTransaction()) deploy_client.wait_for_transaction(scheduler.enterPool.sendTransaction()) # New pool is formed but not active first_generation_id = scheduler.getNextGenerationId() assert first_generation_id > 0 # Wait for it to become active deploy_client.wait_for_block(scheduler.getGenerationStartAt(first_generation_id), 240) # We should both be in the pool assert scheduler.getCurrentGenerationId() == first_generation_id assert scheduler.isInGeneration(joiner_a._meta.address, first_generation_id) is True assert scheduler.isInGeneration(joiner_b._meta.address, first_generation_id) is True assert scheduler.isInGeneration(deploy_coinbase, first_generation_id) is True # Schedule the function call. scheduling_txn = scheduler.scheduleCall( client_contract._meta.address, client_contract.setBool.encoded_abi_signature, deploy_client.get_block_number() + 45, 1000000, scheduler.getMinimumGracePeriod(), value=10 * denoms.ether, gas=3000000, ) scheduling_receipt = deploy_client.wait_for_transaction(scheduling_txn) call = get_call(scheduling_txn) target_block = call.targetBlock() grace_period = call.gracePeriod() deploy_client.wait_for_block(target_block + grace_period - 4, 300) my_before_balance = scheduler.getBondBalance(deploy_coinbase) before_balance_a = scheduler.getBondBalance(joiner_a._meta.address) before_balance_b = scheduler.getBondBalance(joiner_b._meta.address) call_txn_hash = scheduler.execute(call._meta.address) call_txn_receipt = deploy_client.wait_for_transaction(call_txn_hash) call_txn = deploy_client.get_transaction_by_hash(call_txn_hash) call_block = deploy_client.get_block_by_hash(call_txn_receipt['blockHash']) execution_data = get_execution_data(call_txn_hash) assert execution_data['success'] is True my_after_balance = scheduler.getBondBalance(deploy_coinbase) after_balance_a = scheduler.getBondBalance(joiner_a._meta.address) after_balance_b = scheduler.getBondBalance(joiner_b._meta.address) assert after_balance_a < before_balance_a assert after_balance_b < before_balance_b minimum_bond = int(call_block['gasLimit'], 16) * int(call_txn['gasPrice'], 16) assert after_balance_a == before_balance_a - minimum_bond assert after_balance_b == before_balance_b - minimum_bond assert my_after_balance > my_before_balance assert my_after_balance == my_before_balance + 2 * minimum_bond assert scheduler.getNextGenerationId() == 0
def deployed_logs_events(LogsEvents, blockchain_client): deploy_txn_hash = deploy_contract(blockchain_client, LogsEvents) contract_addr = get_contract_address_from_txn(blockchain_client, deploy_txn_hash) assert contract_addr logs_events = LogsEvents(contract_addr, blockchain_client) return logs_events